1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>
#include <string.h>
/* Make sure that a blocking syscall restarts if hit by a signal,
and SA_RESTART is set */
static void handler(int s)
{
}
int main()
{
int pid;
int fds[2];
if (pipe(fds) == -1) {
perror("FAIL: pipe\n");
return 1;
}
pid = fork();
if (pid == -1) {
perror("fork failed");
return 1;
}
if (pid == 0) {
char ch = '?';
int ret;
struct sigaction sa;
sa.sa_handler = handler;
sigfillset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGUSR1, &sa, NULL);
close(fds[1]);
ret = read(fds[0], &ch, 1);
if (ret != 1 || ch != 'x')
fprintf(stderr, "FAIL: expected 1 byte, not %d/%s/%c\n",
ret, strerror(errno), ch);
} else {
signal(SIGPIPE, SIG_IGN);
close(fds[0]);
sleep(1);
kill(pid, SIGUSR1);
sleep(1);
write(fds[1], "x", 1);
waitpid(pid, NULL, 0);
}
return 0;
}
|