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