1 /*	$OpenBSD: sig-stop.c,v 1.1 2020/09/16 14:02:23 mpi Exp $	*/
2 /*
3  *	Written by Artur Grabowski <art@openbsd.org> 2007 Public Domain.
4  */
5 #include <sys/types.h>
6 #include <sys/time.h>
7 #include <sys/wait.h>
8 
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <time.h>
13 #include <err.h>
14 #include <signal.h>
15 
16 int
17 main(int argc, char **argv)
18 {
19 	struct timespec ts;
20 	pid_t child;
21 	int status;
22 	int count;
23 	int toggle = 0;
24 
25 	switch((child = fork())) {
26 	case -1:
27 		err(1, "fork");
28 	case 0:
29 		ts.tv_sec = 0;
30 		ts.tv_nsec = 1000;
31 		for (count = 0; count < 100; count++) {
32 			nanosleep(&ts, NULL);
33 		}
34 		exit(0);
35 	default:
36 		break;
37 	}
38 
39 	ts.tv_sec = 1;
40 	ts.tv_nsec = 0;
41 	nanosleep(&ts, NULL);
42 
43 	do {
44 		toggle ^= 1;
45 		if (kill(child, toggle ? SIGSTOP : SIGCONT)) {
46 			if (wait(&status) < 0)
47 				err(1, "wait");
48 			break;
49 		}
50 	} while(waitpid(child, &status, WCONTINUED|WUNTRACED) > 0 &&
51 	    (toggle ? WIFSTOPPED(status) : WIFCONTINUED(status)));
52 
53 	if (!WIFEXITED(status))
54 		err(1, "bad status: %d\n", status);
55 
56 	return 0;
57 }
58