Why I can't receive SIGPOLL signal from ioctl function? - c

I got a strange problem that I can't solve. This is my code.
#include <stdio.h>
#include <stropts.h>
#include <signal.h>
#include <sys/types.h>
void handle_signal(int s)
{
char c = getchar();
printf("got char '%c'\n");
if(c == 'q')
{
exit(0);
}
}
int main(int argc, char** argv)
{
sigset(SIGPOLL, handle_signal);
ioctl(0, I_SETSIG, S_RDNORM);
printf("type q to exit");
while(1);
return 0;
}
When I run this program, I type character in terminal but it did not work!!! I can not receive SIGPOLL signal. Have someone can give me some advice? By the way, my operating system is ubuntu 12.04.

On Linux it needs to set O_ASYNC flag and F_SETOWN property on the file descriptor to get SIGIO signal (a synonym of SIGPOLL). And the signal handler can only call async-signal safe functions:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <ctype.h>
void handle_signal(int) { // Can only use async-signal safe functions here.
char msg[] = "got char c\n";
char* c = msg + (sizeof msg - 3);
if(1 != read(STDIN_FILENO, c, 1) || !isprint(*c))
return;
write(STDOUT_FILENO, msg, sizeof msg - 1);
if(*c == 'q')
exit(EXIT_SUCCESS);
}
int main() {
printf("type q to exit\n");
signal(SIGIO, handle_signal);
fcntl(STDIN_FILENO, F_SETFL, O_ASYNC | fcntl(STDIN_FILENO, F_GETFL));
fcntl(STDIN_FILENO, F_SETOWN, getpid());
sigset_t mask;
sigemptyset(&mask);
for(;;)
sigsuspend(&mask);
return EXIT_SUCCESS;
}
You may also like to have a look at F_SETSIG that allows receiving a signal of your choosing and extra information into the signal handler.

Related

reading a line from stdin until a signal is sent

I want to write some code that takes input from stdin, and prints the input on the next line, until a signal is sent, the signal being SIGINT in this case. So far I have this code which just prints a message when SIGINT is sent:
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
bool signalSent = false;
void flag(int signal) {
signalSent = true;
}
int main(int argc, char** argv) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = flag;
sa.sa_flags = SA_RESTART;
sigaction(SIGINT, &sa, 0);
while (true) {
while (!signalSent) {
usleep(500000);
}
printf("signal sent\n");
signalSent = false;
}
return 0;
}
I tried using fgets() to get the input from stdin and print to stdout, but when I enter ^C (SIGINT) I have to press enter, but I want it to send the message ("signal sent\n"); as soon as it is pressed like the code I currently have. Code using fgets():
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
bool signalSent = false;
void flag(int signal) {
signalSent = true;
}
int main(int argc, char** argv) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = flag;
sa.sa_flags = SA_RESTART;
sigaction(SIGINT, &sa, 0);
while (true) {
while (!signalSent) {
char buffer[80];
char* t = fgets(buffer, sizeof(buffer), stdin);
printf("%s", t);
fflush(stdout);
}
printf("signal sent\n");
signalSent = false;
}
return 0;
}
Output from code:
For reference I want my output to look like the picture below. Im very new to using signals in C as well.
As detailed in the manual pages for sigaction(2) and signal(7), the SA_RESTART flag causes certain system calls, including read(2), to automatically restart after the signal handler has ended.
It seems that you do not want this behaviour.
fgets is implemented by way of read, and can fail for the same reasons it can. Without SA_RESTART, while waiting on input from a "slow" device (this includes terminals), these functions will set errno to EINTR if interrupted by a signal.
Note that what can and cannot be done in a signal handler is complicated.
Consider using sig_atomic_t objects, declared as volatile, for these types of flags.
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
static volatile sig_atomic_t signal_sent = 0;
void flag(int signal) {
(void) signal;
signal_sent = 1;
}
int main(void) {
struct sigaction sa = { .sa_handler = flag };
sigaction(SIGINT, &sa, 0);
while (!signal_sent) {
char buffer[80];
if (fgets(buffer, sizeof buffer, stdin))
printf("%s", buffer);
else if (errno == EINTR && signal_sent) {
puts("signal sent");
signal_sent = 0;
} else
return 1;
}
}
Example of running this program (^D to terminate):
$ ./a.out
foo
foo
^Csignal sent
^Csignal sent
bar
bar
hello wor^Csignal sent

The usage of execlp() in a c-program

I have a question regarding execlp() in c.
I have the following programm:
#include <stdio.h>
#include <unistd.h>
#include <sys/unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <limits.h>
#include <signal.h>
void INThandler(int);
int main(int argc, char* argv[]) {
struct passwd *pwd;
char *lgn;
char *cwd;
char buff[PATH_MAX + 1];
char s1[10], s2[10];
/*Um den Namen zu bekommen*/
lgn = getlogin();
pwd = getpwnam(lgn);
/*Um den Hostnamen zu bekommen*/
char hostname[128];
gethostname(hostname, sizeof hostname);
/*Um das Arbeitsverzeichnis zu bekommen*/
cwd = getcwd(buff, PATH_MAX + 1);
if((cwd!=NULL)&& hostname!=NULL && ((lgn=getlogin())!=NULL ||
(pwd!=NULL)))
{
signal(SIGINT, INThandler);
while(1)
{
printf("%s#%s %s$", pwd->pw_name, hostname, cwd);
if(scanf("%s %s",s1, s2)<1)
return 1;
printf("Befehl: %s\nArgument: %s\n",s1,s2);
execlp(s1, s1, NULL);
printf("Zhopa");
return 1;
}
}
return 0;
}
void INThandler(int sig) {
char c;
signal(sig, SIG_IGN);
printf("Wollen Sie Program Verlassen? [y/n]");
c = getchar();
if(c == 'y' || c=='Y')
exit(0);
else
signal(SIGINT, INThandler);
getchar();
}
it should print the users name#hostname folder$ and take a linux command as an argument "ls -al"
after that it should start it with execlp(), but it doesn't work as I think t should.
I read all the articles here regarding this command, but I guess, I still don't understand, how to use it.
I would appreciate someone's help.
Your signal handler invokes undefined behavior.
You can only call async-signal safe functions from within a signal handler. Per the POSIX standard:
... the behavior is undefined ... if the signal handler calls any
function defined in this standard other than one of the functions
listed in the following table.
The following table defines a set of functions that shall be
async-signal-safe. Therefore, applications can call them, without
restriction, from signal-catching functions. ...
[table of async-signal-safe functions]
Any function not in the above table may be unsafe with respect to signals. ...
This signal handler
void INThandler(int sig) {
char c;
signal(sig, SIG_IGN);
printf("Wollen Sie Program Verlassen? [y/n]");
c = getchar();
if(c == 'y' || c=='Y')
exit(0);
else
signal(SIGINT, INThandler);
getchar();
}
has multiple non-async-signal-safe functions in it:
printf()
getchar()
exit()
Those functions can not be safely called from within a signal handler.
Given your code, a likely place for this signal handler to be called would be when the process is blocked in scanf() - trying to read from stdin. That likely involves a lock or mutex of some kind. Yet your signal handler calls getchar() which also tries to read from stdin, which may be locked or in some indeterminate state. If the main thread is blocked in scanf(), the asynchronous call to getchar() in the signal handler may deadlock or corrupt the internal structures used for stdin.
You've to create a new process with a fork() and then in the new process (child) use the execlp. Here is the sample code. It doesn't handle any error and it works just with a command with exactly 1 parameter because that's what I've understood(e.g. ls -all)
#include <stdio.h>
#include <unistd.h>
#include <sys/unistd.h>
#include <sys/wait.h> /*Lib for waitpid*/
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <limits.h>
#include <signal.h>
void INThandler(int);
int main(int argc, char* argv[]) {
int pid = 0; /*PROCESS ID*/
struct passwd *pwd;
char *lgn;
char *cwd;
char buff[PATH_MAX + 1];
char s1[10], s2[10];
/*Um den Namen zu bekommen*/
lgn = getlogin();
pwd = getpwnam(lgn);
/*Um den Hostnamen zu bekommen*/
char hostname[128];
gethostname(hostname, sizeof hostname);
/*Um das Arbeitsverzeichnis zu bekommen*/
cwd = getcwd(buff, PATH_MAX + 1);
if((cwd!=NULL)&& hostname!=NULL && ((lgn=getlogin())!=NULL ||
(pwd!=NULL)))
{
signal(SIGINT, INThandler);
while(1)
{
printf("%s#%s %s$", pwd->pw_name, hostname, cwd);
if(scanf("%s %s",s1, s2)<1)
return 1;
printf("Befehl: %s\nArgument: %s\n",s1,s2);
pid = fork();
if(pid == 0){ /*Child*/
execlp(s1, s1, s2,(char*) NULL);
}else if(pid > 0){ /*Father*/
/*father waiting for the child*/
waitpid(pid,NULL,0);
}
printf("Zhopa");
}
}
return 0;
}
void INThandler(int sig) {
char c;
signal(sig, SIG_IGN);
printf("Wollen Sie Program Verlassen? [y/n]");
c = getchar();
if(c == 'y' || c=='Y')
exit(0);
else
signal(SIGINT, INThandler);
getchar();
}

pthread_kill() gives segmentation fault when called from second thread

I am trying to manually interrupt the main thread of a program when it is blocked on a read() system call. I do this in a second thread with a call to pthread_kill() however a segmentation fault occurs. However if I place the call to read() in the scond thread, i.e. NOT the main thread and call pthread_kill() from the main thread then all works as expected.
For example, the following code results in a segmentation fault, where I call pthread_kill() in the second thread, approximatelt 2 seconds after it is started. It uses the pthread_t of the main thread obtained by a call (in the main thread) to pthread_self():
Example 1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
static int fd = 0;
unsigned char buf[255];
static pthread_t s;
void sigHandler(int sig){
printf("Signal handler called.\n");
}
void * closeFD(void *arg){
printf("Second thread started.\n");
sleep(2);
int r = pthread_kill(s, SIGUSR1);
}
int main(char *argv[], int argc){
struct termios newtio;
pthread_t t1;
unsigned char buf[255];
void *res;
struct sigaction int_handler = {.sa_handler=sigHandler};
sigaction(SIGUSR1,&int_handler,0);
s = pthread_self();
printf("Process id is: %d.\n", getpid());
fd = open("/dev/ttyS0", O_RDONLY | O_NOCTTY);
if (fd != -1){
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = B2400 | CS7 | CLOCAL | CREAD ;
newtio.c_iflag = ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ~ICANON;
newtio.c_cc[VMIN] = 14;
tcsetattr(fd,TCSANOW,&newtio);
pthread_create(&t1, NULL, closeFD, NULL);
printf("Reading ..\n");
read(fd,buf,255);
close(fd);
}
return 0;
}
The following code is the same except I place the call to read() in the second thread (in closeFD()) and works as expected. The second thread unblocks and terminates while the main thread waits for it to exit then exits itself.
Example 2:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
static int fd = 0;
unsigned char buf[255];
static pthread_t s;
void sigHandler(int sig){
printf("Signal handler called.\n");
}
void * closeFD(void *arg){
printf("Second thread started.\n");
read(fd,buf,255);
printf("Read interrupted.\n");
}
int main(char *argv[], int argc){
struct termios newtio;
pthread_t t1;
unsigned char buf[255];
void *res;
struct sigaction int_handler = {.sa_handler=sigHandler};
sigaction(SIGUSR1,&int_handler,0);
s = pthread_self();
printf("Process id is: %d.\n", getpid());
fd = open("/dev/ttyS0", O_RDONLY | O_NOCTTY);
if (fd != -1){
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = B2400 | CS7 | CLOCAL | CREAD ;
newtio.c_iflag = ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ~ICANON;
newtio.c_cc[VMIN] = 14;
tcsetattr(fd,TCSANOW,&newtio);
pthread_create(&t1, NULL, closeFD, NULL);
sleep(2);
int r = pthread_kill(t1, SIGUSR1);
pthread_join(t1, &res);
close(fd);
}
return 0;
}
So far I have not been able to find a specific reference stating that terminating the main thread from a second (within the same process) is an illegal operation, so is there something I am doing wrong?
UPDATE #1
Thanks for all those that have replied, however I should make a few points clear:
I am aware that using printf in the signal handler is unsafe however this is an example and it's not the cause of the segmentation fault, though it is a valid point. Taking the printf() out of the signal handler still results in a segmentation fault. Example 2 works with printf() either in or out of the signal handler.
I know sending a SIGUSR will not terminate the program. However by using the pthread_kill(pthread_t thread, int signal) it WILL send a signal to the thread thread and it will unblock (if indeed it is blocked). This is the action I desire, this is what actually happens in Example 2 and this is what I understand should happen in either example, but does not in example 1.
When describing example 1, I used the term 'method' when I meant 'thread', where I mention the call to pthread_kill().
Further, quoting from 'Programming with POSIX Threads', David R. Butenhof, section 6.6.3 p217 'pthread_kill':
Within a process, one thread can send a signal to a specific thread
(including itself) by calling pthread_kill.
With that said, the following example ALSO gives a segmentation fault:
Example 3
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <signal.h>
static pthread_t s;
int value = 0;
void sigHandler(int sig){
value = 1;
}
int main(char *argv[], int argc){
struct sigaction int_handler = {.sa_handler=sigHandler};
sigaction(SIGUSR1,&int_handler,0);
s = pthread_self();
printf("The value of 'value' is %d.\n", value);
printf("Process id is: %d.\n", getpid());
int r = pthread_kill(s, SIGUSR1);
printf("The value of 'value' is %d.\n", value);
return 0;
}
This also fails if instead of a call to sigaction() is replaced by the (non-portable) call to signal(). With the third example in mind, which is very simple, I am not able to find any documentation that expressly states it is an illegal action. In fact the quoted reference indicates it's allowable!
You forgot to #include <pthread.h>. That fixes your segfault for me in example #3 on a recent Linux system.
--- pthread_kill-self.c.orig 2015-01-06 14:08:54.949000690 -0600
+++ pthread_kill-self.c 2015-01-06 14:08:59.820998965 -0600
## -1,6 +1,6 ##
#include <stdio.h>
#include <string.h>
-#include <string.h>
+#include <pthread.h>
#include <signal.h>
and then...
$:- gcc -o pthread_kill-self pthread_kill-self.c -pthread
$:- ./pthread_kill-self
The value of 'value' is 0.
Process id is: 3152.
The value of 'value' is 1.
You're using printf(), which is not async-signal safe, and you're not initializing your struct sigaction properly (in particular, the signal mask is left undefined).
Third, sending a SIGUSR1 signal, with a handler installed, does not and should not terminate the main thread. You're just sending it a signal, that's all.
As Jens Gustedt mentioned in his comment to the original question, both of the programs have undefined behaviour. Therefore, I'm not going to try and guess exactly what part of the undefined behaviour causes the segmentation fault (in the first program).
Instead, I'll show you a working example.
For debugging/testing purposes, I like to start with async-signal safe output functions, based on write(2):
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <termios.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>
#define MYSIGNAL SIGUSR1
#define SECONDS 10
static int wrstr(const int descriptor, const char *p, const char *const q)
{
while (p < q) {
ssize_t n;
n = write(descriptor, p, (size_t)(q - p));
if (n > (ssize_t)0)
p += n;
else
if (n != (ssize_t)-1)
return EIO;
else
if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)
return errno;
}
return 0;
}
static const char *ends(const char *s)
{
if (s)
while (*s != '\0')
s++;
return s;
}
static int wrout(const char *const p)
{
if (p != NULL && *p != '\0') {
int saved_errno, result;
saved_errno = errno;
result = wrstr(STDOUT_FILENO, p, ends(p));
errno = saved_errno;
return result;
} else
return 0;
}
static int wrouti(const int value)
{
char buffer[32];
char *p = buffer + sizeof buffer;
unsigned int u;
if (value < 0)
u = -(long)value;
else
u = value;
do {
*(--p) = '0' + (u % 10U);
u /= 10U;
} while (u > 0U);
if (value < 0)
*(--p) = '-';
return wrstr(STDOUT_FILENO, p, buffer + sizeof buffer);
}
static int wrerr(const char *const p)
{
if (p != NULL && *p != '\0') {
int saved_errno, result;
saved_errno = errno;
result = wrstr(STDERR_FILENO, p, ends(p));
errno = saved_errno;
return result;
} else
return 0;
}
The above functions are async-signal safe, and therefore okay to use in a signal handler. wrout() and wrerr() also retain errno unchanged, which is useful. Saving and restoring errno in a signal handler is usually omitted, by the way, although I do believe there are some odd corner cases it might matter. The wrouti() is just a crude decimal signed integer printer, also async-signal-safe, but it does not retain errno unchanged.
Next, let's define the signal handler itself, and an installer function for it. (I like to do it this way, to make the main() simpler.)
static volatile sig_atomic_t handled = 0;
static void handler(int signum)
{
wrerr("Signal received.\n");
handled = signum;
}
static int install_handler(const int signum)
{
struct sigaction act;
/* memset(&act, 0, sizeof act); */
sigemptyset(&act.sa_mask);
act.sa_handler = handler;
act.sa_flags = 0;
if (sigaction(signum, &act, NULL))
return errno;
return 0;
}
The commented-out memset is recommended, but not required for proper operation. The sigemptyset(), however, is required, to clear the set of blocked signals.
Next, let's look at the thread function. You shouldn't use sleep(), as that interacts with signals; use POSIX.1-2001 nanosleep() instead.
static void *worker(void *target)
{
struct timespec duration, left;
int retval;
wrout("Worker started. Sleeping ");
wrouti((int)SECONDS);
wrout(" seconds...\n");
duration.tv_sec = SECONDS;
duration.tv_nsec = 0;
left.tv_sec = 0;
left.tv_nsec = 0;
while (1) {
retval = nanosleep(&duration, &left);
if (retval == 0)
break;
if (left.tv_sec <= 0 ||
(left.tv_sec == 0 && left.tv_nsec <= 0))
break;
duration = left;
left.tv_sec = 0;
left.tv_nsec = 0;
}
wrout("Sleep complete.\n");
if (target) {
wrout("Sending signal...\n");
retval = pthread_kill(*(pthread_t *)target, MYSIGNAL);
if (retval == 0)
wrout("Signal sent successfully.\n");
else {
const char *const errmsg = strerror(retval);
wrout("Failed to send signal: ");
wrout(errmsg);
wrout(".\n");
}
}
wrout("Thread done.\n");
return NULL;
}
The pointer given to the thread function should point to the thread identifier (pthread_t) the signal is directed to.
Note that above, nanosleep() can be interrupted by a signal delivery, if the signal is delivered to or caught by this particular thread. If that occurs, nanosleep() tells us how much time was left to sleep. The loop above shows how to make sure you sleep at least the specified time, even if interrupted by signal delivery.
Finally, the main(). Instead of opening a specific device, I use standard input. To reproduce OP's program, redirect standard input from /dev/ttyUSB0, i.e. ./program < /dev/ttyUSB0, when executing it.
int main(void)
{
pthread_t main_thread, worker_thread;
pthread_attr_t attrs;
struct termios original, settings;
int result;
if (!isatty(STDIN_FILENO)) {
wrerr("Standard input is not a terminal.\n");
return EXIT_FAILURE;
}
if (tcgetattr(STDIN_FILENO, &original) != 0 ||
tcgetattr(STDIN_FILENO, &settings) != 0) {
const char *const errmsg = strerror(errno);
wrerr("Cannot get terminal settings: ");
wrerr(errmsg);
wrerr(".\n");
return EXIT_FAILURE;
}
settings.c_lflag = ~ICANON;
settings.c_cc[VMIN] = 14;
if (tcsetattr(STDIN_FILENO, TCSANOW, &settings) != 0) {
const char *const errmsg = strerror(errno);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
wrerr("Cannot set terminal settings: ");
wrerr(errmsg);
wrerr(".\n");
return EXIT_FAILURE;
}
wrout("Terminal is now in raw mode.\n");
if (install_handler(MYSIGNAL)) {
const char *const errmsg = strerror(errno);
wrerr("Cannot install signal handler: ");
wrerr(errmsg);
wrerr(".\n");
return EXIT_FAILURE;
}
main_thread = pthread_self();
pthread_attr_init(&attrs);
pthread_attr_setstacksize(&attrs, 65536);
result = pthread_create(&worker_thread, &attrs, worker, &main_thread);
if (result != 0) {
const char *const errmsg = strerror(errno);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
wrerr("Cannot create a worker thread: ");
wrerr(errmsg);
wrerr(".\n");
return EXIT_FAILURE;
}
pthread_attr_destroy(&attrs);
wrout("Waiting for input...\n");
while (1) {
char buffer[256];
ssize_t n;
if (handled) {
wrout("Because signal was received, no more input is read.\n");
break;
}
n = read(STDIN_FILENO, buffer, sizeof buffer);
if (n > (ssize_t)0) {
wrout("Read ");
wrouti((int)n);
wrout(" bytes.\n");
continue;
} else
if (n == (ssize_t)0) {
wrout("End of input.\n");
break;
} else
if (n != (ssize_t)-1) {
wrout("read() returned an invalid value.\n");
break;
} else {
result = errno;
wrout("read() == -1, errno == ");
wrouti(result);
wrout(": ");
wrout(strerror(result));
wrout(".\n");
break;
}
}
wrout("Reaping the worker thread..\n");
result = pthread_join(worker_thread, NULL);
if (result != 0) {
wrout("Failed to reap worker thread: ");
wrout(strerror(result));
wrout(".\n");
} else
wrout("Worker thread reaped successfully.\n");
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
wrout("Terminal reverted back to original mode.\n");
return EXIT_SUCCESS;
}
Because it's much more fun to test using the terminal, the above tries hard to restore the terminal to its original state before returning.
Note that since the VMIN field in the termios structure is set to 14, the read() blocks until at least 14 bytes are available in the buffer. If a signal is delivered, a short count is returned if there is at least one byte in the buffer. Therefore, you cannot expect the read() to always return 14 bytes, and you cannot expect it to return -1 with errno == EINTR whenever a signal is delivered! Experimenting with this program is very useful, to clarify these in your mind.
I don't remember whether the USB serial drivers in Linux ever produce EPIPE or raise SIGPIPE, but that can definitely occur when using pipes. When using pipes, the most common reason is trying to read after read has already returned zero (end of input). Unless ignored or caught with a signal handler, the process dies much like in a segmentation fault, except that the cause is SIGPIPE signal instead of SIGSEGV. With terminal-like character devices, it depends on the driver, I seem to recall.
Finally, I wrote the above code under the weather (flu), so there might be bugs in tharrr. It should be POSIX.1 C99 code, and gcc -Wall -pedantic does not complain, but having a stuffed head, I'm not making any promises here. Fixes are more than welcome!
Questions? Comments?

How to exit with Ctrl+D like bash with execl?

I'm doing :
execl("/bin/bash", "/bin/bash", NULL);
When I do a Ctrl+D, it directly exit. How can I do the same as bash and write exit before exiting ?
Do I have to add a flag or something to execl?
When I compile execl(...), it prints exit on Ctrl-D just fine
#include <unistd.h>
int main(int argc, char **argv)
{
execl("/bin/bash", "/bin/bash", 0);
return 0;
}
maybe, you do a fork() or detach from the terminal or do something else, which lets bash assume it is non-interactive.
Ctrl-D is usually interpreted by the terminal. If you want to do this yourself, you must reset VEOF in the termios structure see c_cc for details.
This is a simplified example for handling Ctrl-D yourself. It still reads a whole line before processing anything, but you get the idea
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char buf[100];
int fd;
struct termios tio;
fd = open("/dev/tty", O_RDWR);
if (fd < 0) {
perror("open tty");
exit(1);
}
memset(&tio, 0, sizeof(tio));
tcgetattr(fd, &tio);
tio.c_cc[VEOF] = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &tio);
while (fgets(buf, sizeof(buf), stdin)) {
if (buf[0] == 4) {
printf("Got Ctrl-D\n");
break;
}
}
return 0;
}
This program reads a line from the terminal, until it receives a line starting with Ctrl-D.
For more examples, see the Serial Programming HOWTO.

IPC FIFO Producer-Consumer Deadlock

This is the producer.
// speak.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_NAME "american_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for readers...\n");
fd = open(FIFO_NAME, O_WRONLY);
printf("got a reader--type some stuff\n");
while (gets(s), !feof(stdin)) {
if ((num = write(fd, s, strlen(s))) == -1)
perror("write");
else
printf("speak: wrote %d bytes\n", num);
}
return 0;
}
And this is the consumer.
//tick.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_NAME "american_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for writers...\n");
fd = open(FIFO_NAME, O_RDONLY);
printf("got a writer\n");
do {
if ((num = read(fd, s, 300)) == -1)
perror("read");
else {
s[num] = '\0';
printf("tick: read %d bytes: \"%s\"\n", num, s);
}
} while (num > 0);
return 0;
}
When I run them, Producer outputs,
waiting for readers...
And consumer outputs,
waiting for writers...
speak doesn't find the reader, tick. As from the theory here I got that, open() (speak.c) will be keep blocked until open() (tick.c) is opened. And the vice versa. So I guess there a deadlock or something happening. I need a solution of this.
It looks like you have a race condition between the reader and the writer.
To fix this, you need a method of not launching the reader until the writer is "active". For this, I'd suggest making a pipe and writing to it when the writer is ready. Then, when reading from the read end of the fork succeeds, the fifo is prepared and the reader should work.
You need to use forks here because coordinating mutexes between a parent and a child process is non-trivial and properly done pipes is easier.
Also, you called mknod() twice. Granted, it'll return -1 with errno == EEXIST, but be more careful. To avoid this, make the reader and writer a function that takes a path as an argument.
Rewrite your writer as int speak(const char *fifo, int pipefd) and your reader as int tick(const char *fifo).
Then make a wrapper like this:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
const char fifo_name[] /* = ... */;
int speak(const char *fifo, int pipefd);
int tick(const char *fifo);
int main() {
int pipefd[2];
pipe(pipefd);
mknod(fifo_name, S_IFIFO | 0666, 0);
if (fork() == 0) {
close(pipefd[0]);
return speak(fifo_name, pipefd[1]);
} else {
close(pipefd[1]);
char foo;
read(pipefd[0], &foo, 1);
return tick(fifo_name);
}
}
Modify your writer to print a byte (of anything) to the passed fd after the fifo is created (i.e. right after the call to open(..., O_WRONLY)).
Don't use my code verbatim, as I've omitted error checking for the sake of brevity.
it runs ok in my env. and if reader and writer is ready, open will return. because open is blocked, so in my opinion, mknod function is success. May be you excute these two process at different path.

Resources