aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/pause.c
blob: 054c5a48b9fbed8c22636a70fd7840c10544e895 (plain)
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
#include <assert.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/wait.h>

static void noop(int x) {
	(void)x;
}

int main() {
	signal(SIGUSR1, &noop);

	int pid;
	switch(pid = fork()) {
	case -1:
		perror("fork");
		abort();
	case 0:
		pause();
		assert(errno == EINTR);
		return 0;
	default:
		while (1) {
			usleep(100);
			kill(pid, SIGUSR1);
			usleep(100);
			int status;

			errno = 0;
			if (waitpid(-1, &status, WNOHANG) <= 0) {
				if (errno && errno != EAGAIN) {
					perror("wait");
					kill(pid, SIGKILL);
				}
				continue;
			}

			if (!WIFEXITED(status)) {
				printf("wait returned %x\n", status);
				abort();
			}

			return WEXITSTATUS(status);
		}
	}
}