I have a signal handler set up using sigaction like so:
struct sigaction act, oldact;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = sig_handler;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGALRM);
sigaddset(&act.sa_mask, SIGINT);
sigaddset(&act.sa_mask, SIGTERM);
sigaddset(&act.sa_mask, SIGTSTP);
sigaction(SIGALRM, &act, &oldact);
sigaction(SIGINT, &act, &oldact);
sigaction(SIGTERM, &act, &oldact);
sigaction(SIGTSTP, &act, &oldact);
act.sa_flags = 0;
Following this I run a for loop to gather input and print it back out (basically acting like cat).
char *linebuf = NULL;
size_t n = 0;
int len;
while(1){
len = getline(&linebuf, &n, stdin);
if(len>0){
printf("%s", linebuf);
}
}
However, once I return from handling a signal, getline()
no longer blocks for input, and instead consistently returns -1
while setting errno
to EINTR
which is an interrupted system call. I obviously have intended to interrupt getline()
, but how do I reset it so that I can continue reading input?
There were a few similar questions that didn't really solve my problem, but it might help you understand the issue better. 1 2
Another interesting tidbit is that I did NOT have this problem when I was using signal()
for my signal handling, but I changed to sigaction()
because it is POSIX compliant.