Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libc / siginfo / sigchld / sigchld.c
blob138c1da4e473aa5385e2bdabb3dd8beb61c87a10
1 /* $NetBSD: sigchld.c,v 1.6 2006/05/10 19:07:22 mrg Exp $ */
3 #include <sys/ucontext.h>
4 #include <sys/wait.h>
5 #include <sys/resource.h>
7 #include <assert.h>
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
12 pid_t child;
13 int code;
14 int status;
16 void
17 handler(int signo, siginfo_t *info, void *ptr)
19 #ifdef DEBUG
20 if (info != NULL) {
21 printf("info=%p\n", info);
22 printf("ptr=%p\n", ptr);
23 printf("si_signo=%d\n", info->si_signo);
24 printf("si_errno=%d\n", info->si_errno);
25 printf("si_code=%d\n", info->si_code);
26 printf("si_uid=%d\n", info->si_uid);
27 printf("si_pid=%d\n", info->si_pid);
28 printf("si_status=%d\n", info->si_status);
29 printf("si_utime=%d\n", info->si_utime);
30 printf("si_stime=%d\n", info->si_stime);
32 #endif
33 assert(info->si_code == code);
34 assert(info->si_signo == SIGCHLD);
35 assert(info->si_uid == getuid());
36 assert(info->si_pid == child);
37 if (WIFEXITED(info->si_status))
38 assert(WEXITSTATUS(info->si_status) == status);
39 else if (WIFSTOPPED(info->si_status))
40 assert(WSTOPSIG(info->si_status) == status);
41 else if (WIFSIGNALED(info->si_status))
42 assert(WTERMSIG(info->si_status) == status);
45 static void
46 sethandler(void (*action)(int, siginfo_t *, void *))
48 struct sigaction sa;
49 sa.sa_flags = SA_SIGINFO;
50 sa.sa_sigaction = action;
51 sigemptyset(&sa.sa_mask);
52 sigaction(SIGCHLD, &sa, NULL);
56 static void
57 runnormal()
59 sigset_t set;
60 status = 25;
61 code = CLD_EXITED;
63 switch ((child = fork())) {
64 case 0:
65 sleep(1);
66 exit(status);
67 case -1:
68 err(1, "fork");
69 default:
70 sigemptyset(&set);
71 sigsuspend(&set);
75 static void
76 rundump()
78 sigset_t set;
79 status = SIGSEGV;
80 code = CLD_DUMPED;
82 switch ((child = fork())) {
83 case 0:
84 sleep(1);
85 *(long *)0 = 0;
86 break;
87 case -1:
88 err(1, "fork");
89 default:
90 sigemptyset(&set);
91 sigsuspend(&set);
95 static void
96 runkill()
98 sigset_t set;
99 status = SIGPIPE;
100 code = CLD_KILLED;
102 switch ((child = fork())) {
103 case 0:
104 sigemptyset(&set);
105 sigsuspend(&set);
106 break;
107 case -1:
108 err(1, "fork");
109 default:
110 kill(child, SIGPIPE);
111 sigemptyset(&set);
112 sigsuspend(&set);
116 main(void)
118 sigset_t set;
119 struct rlimit rlim;
120 (void)getrlimit(RLIMIT_CORE, &rlim);
121 rlim.rlim_cur = rlim.rlim_max;
122 (void)setrlimit(RLIMIT_CORE, &rlim);
123 sethandler(handler);
124 sigemptyset(&set);
125 sigaddset(&set, SIGCHLD);
126 sigprocmask(SIG_BLOCK, &set, NULL);
127 runnormal();
128 rundump();
129 runkill();
131 return 0;