1*cf84ac9aSAndroid Build Coastguard Worker /*
2*cf84ac9aSAndroid Build Coastguard Worker * Check SIGCHLD siginfo_t decoding.
3*cf84ac9aSAndroid Build Coastguard Worker *
4*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2015-2016 Dmitry V. Levin <[email protected]>
5*cf84ac9aSAndroid Build Coastguard Worker * All rights reserved.
6*cf84ac9aSAndroid Build Coastguard Worker *
7*cf84ac9aSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
8*cf84ac9aSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
9*cf84ac9aSAndroid Build Coastguard Worker * are met:
10*cf84ac9aSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
11*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
12*cf84ac9aSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
13*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
14*cf84ac9aSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
15*cf84ac9aSAndroid Build Coastguard Worker * 3. The name of the author may not be used to endorse or promote products
16*cf84ac9aSAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
17*cf84ac9aSAndroid Build Coastguard Worker *
18*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*cf84ac9aSAndroid Build Coastguard Worker * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*cf84ac9aSAndroid Build Coastguard Worker * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*cf84ac9aSAndroid Build Coastguard Worker * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*cf84ac9aSAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*cf84ac9aSAndroid Build Coastguard Worker * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*cf84ac9aSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*cf84ac9aSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*cf84ac9aSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*cf84ac9aSAndroid Build Coastguard Worker */
29*cf84ac9aSAndroid Build Coastguard Worker
30*cf84ac9aSAndroid Build Coastguard Worker #include "tests.h"
31*cf84ac9aSAndroid Build Coastguard Worker #include <assert.h>
32*cf84ac9aSAndroid Build Coastguard Worker #include <signal.h>
33*cf84ac9aSAndroid Build Coastguard Worker #include <string.h>
34*cf84ac9aSAndroid Build Coastguard Worker #include <unistd.h>
35*cf84ac9aSAndroid Build Coastguard Worker #include <sys/wait.h>
36*cf84ac9aSAndroid Build Coastguard Worker
37*cf84ac9aSAndroid Build Coastguard Worker static siginfo_t sinfo;
38*cf84ac9aSAndroid Build Coastguard Worker
39*cf84ac9aSAndroid Build Coastguard Worker static void
handler(int no,siginfo_t * si,void * uc)40*cf84ac9aSAndroid Build Coastguard Worker handler(int no, siginfo_t *si, void *uc)
41*cf84ac9aSAndroid Build Coastguard Worker {
42*cf84ac9aSAndroid Build Coastguard Worker memcpy(&sinfo, si, sizeof(sinfo));
43*cf84ac9aSAndroid Build Coastguard Worker }
44*cf84ac9aSAndroid Build Coastguard Worker
45*cf84ac9aSAndroid Build Coastguard Worker int
main(void)46*cf84ac9aSAndroid Build Coastguard Worker main(void)
47*cf84ac9aSAndroid Build Coastguard Worker {
48*cf84ac9aSAndroid Build Coastguard Worker tprintf("%s", "");
49*cf84ac9aSAndroid Build Coastguard Worker
50*cf84ac9aSAndroid Build Coastguard Worker int fds[2];
51*cf84ac9aSAndroid Build Coastguard Worker if (pipe(fds))
52*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("pipe");
53*cf84ac9aSAndroid Build Coastguard Worker
54*cf84ac9aSAndroid Build Coastguard Worker pid_t pid = fork();
55*cf84ac9aSAndroid Build Coastguard Worker if (pid < 0)
56*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("fork");
57*cf84ac9aSAndroid Build Coastguard Worker
58*cf84ac9aSAndroid Build Coastguard Worker if (!pid) {
59*cf84ac9aSAndroid Build Coastguard Worker char c;
60*cf84ac9aSAndroid Build Coastguard Worker (void) close(1);
61*cf84ac9aSAndroid Build Coastguard Worker assert(read(0, &c, sizeof(c)) == 1);
62*cf84ac9aSAndroid Build Coastguard Worker return 42;
63*cf84ac9aSAndroid Build Coastguard Worker }
64*cf84ac9aSAndroid Build Coastguard Worker
65*cf84ac9aSAndroid Build Coastguard Worker (void) close(0);
66*cf84ac9aSAndroid Build Coastguard Worker
67*cf84ac9aSAndroid Build Coastguard Worker struct sigaction sa = {
68*cf84ac9aSAndroid Build Coastguard Worker .sa_sigaction = handler,
69*cf84ac9aSAndroid Build Coastguard Worker .sa_flags = SA_SIGINFO
70*cf84ac9aSAndroid Build Coastguard Worker };
71*cf84ac9aSAndroid Build Coastguard Worker assert(sigaction(SIGCHLD, &sa, NULL) == 0);
72*cf84ac9aSAndroid Build Coastguard Worker
73*cf84ac9aSAndroid Build Coastguard Worker sigset_t block_mask, unblock_mask;
74*cf84ac9aSAndroid Build Coastguard Worker assert(sigprocmask(SIG_SETMASK, NULL, &block_mask) == 0);
75*cf84ac9aSAndroid Build Coastguard Worker sigaddset(&block_mask, SIGCHLD);
76*cf84ac9aSAndroid Build Coastguard Worker assert(sigprocmask(SIG_SETMASK, &block_mask, NULL) == 0);
77*cf84ac9aSAndroid Build Coastguard Worker
78*cf84ac9aSAndroid Build Coastguard Worker unblock_mask = block_mask;
79*cf84ac9aSAndroid Build Coastguard Worker sigdelset(&unblock_mask, SIGCHLD);
80*cf84ac9aSAndroid Build Coastguard Worker
81*cf84ac9aSAndroid Build Coastguard Worker assert(write(1, "", 1) == 1);
82*cf84ac9aSAndroid Build Coastguard Worker (void) close(1);
83*cf84ac9aSAndroid Build Coastguard Worker
84*cf84ac9aSAndroid Build Coastguard Worker sigsuspend(&unblock_mask);
85*cf84ac9aSAndroid Build Coastguard Worker tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED"
86*cf84ac9aSAndroid Build Coastguard Worker ", si_pid=%d, si_uid=%u, si_status=%d"
87*cf84ac9aSAndroid Build Coastguard Worker ", si_utime=%llu, si_stime=%llu} ---\n",
88*cf84ac9aSAndroid Build Coastguard Worker sinfo.si_pid, sinfo.si_uid, sinfo.si_status,
89*cf84ac9aSAndroid Build Coastguard Worker zero_extend_signed_to_ull(sinfo.si_utime),
90*cf84ac9aSAndroid Build Coastguard Worker zero_extend_signed_to_ull(sinfo.si_stime));
91*cf84ac9aSAndroid Build Coastguard Worker
92*cf84ac9aSAndroid Build Coastguard Worker int s;
93*cf84ac9aSAndroid Build Coastguard Worker assert(wait(&s) == pid);
94*cf84ac9aSAndroid Build Coastguard Worker assert(WIFEXITED(s) && WEXITSTATUS(s) == 42);
95*cf84ac9aSAndroid Build Coastguard Worker
96*cf84ac9aSAndroid Build Coastguard Worker if (pipe(fds))
97*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("pipe");
98*cf84ac9aSAndroid Build Coastguard Worker pid = fork();
99*cf84ac9aSAndroid Build Coastguard Worker if (pid < 0)
100*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("fork");
101*cf84ac9aSAndroid Build Coastguard Worker
102*cf84ac9aSAndroid Build Coastguard Worker if (!pid) {
103*cf84ac9aSAndroid Build Coastguard Worker (void) close(1);
104*cf84ac9aSAndroid Build Coastguard Worker char c;
105*cf84ac9aSAndroid Build Coastguard Worker assert(read(0, &c, sizeof(c)) == 1);
106*cf84ac9aSAndroid Build Coastguard Worker (void) raise(SIGUSR1);
107*cf84ac9aSAndroid Build Coastguard Worker return 1;
108*cf84ac9aSAndroid Build Coastguard Worker }
109*cf84ac9aSAndroid Build Coastguard Worker
110*cf84ac9aSAndroid Build Coastguard Worker (void) close(0);
111*cf84ac9aSAndroid Build Coastguard Worker
112*cf84ac9aSAndroid Build Coastguard Worker assert(write(1, "", 1) == 1);
113*cf84ac9aSAndroid Build Coastguard Worker (void) close(1);
114*cf84ac9aSAndroid Build Coastguard Worker
115*cf84ac9aSAndroid Build Coastguard Worker sigsuspend(&unblock_mask);
116*cf84ac9aSAndroid Build Coastguard Worker tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_KILLED"
117*cf84ac9aSAndroid Build Coastguard Worker ", si_pid=%d, si_uid=%u, si_status=SIGUSR1"
118*cf84ac9aSAndroid Build Coastguard Worker ", si_utime=%llu, si_stime=%llu} ---\n",
119*cf84ac9aSAndroid Build Coastguard Worker sinfo.si_pid, sinfo.si_uid,
120*cf84ac9aSAndroid Build Coastguard Worker zero_extend_signed_to_ull(sinfo.si_utime),
121*cf84ac9aSAndroid Build Coastguard Worker zero_extend_signed_to_ull(sinfo.si_stime));
122*cf84ac9aSAndroid Build Coastguard Worker
123*cf84ac9aSAndroid Build Coastguard Worker assert(wait(&s) == pid);
124*cf84ac9aSAndroid Build Coastguard Worker assert(WIFSIGNALED(s) && WTERMSIG(s) == SIGUSR1);
125*cf84ac9aSAndroid Build Coastguard Worker
126*cf84ac9aSAndroid Build Coastguard Worker if (pipe(fds))
127*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("pipe");
128*cf84ac9aSAndroid Build Coastguard Worker pid = fork();
129*cf84ac9aSAndroid Build Coastguard Worker if (pid < 0)
130*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("fork");
131*cf84ac9aSAndroid Build Coastguard Worker
132*cf84ac9aSAndroid Build Coastguard Worker if (!pid) {
133*cf84ac9aSAndroid Build Coastguard Worker (void) close(1);
134*cf84ac9aSAndroid Build Coastguard Worker raise(SIGSTOP);
135*cf84ac9aSAndroid Build Coastguard Worker char c;
136*cf84ac9aSAndroid Build Coastguard Worker assert(read(0, &c, sizeof(c)) == 1);
137*cf84ac9aSAndroid Build Coastguard Worker return 0;
138*cf84ac9aSAndroid Build Coastguard Worker }
139*cf84ac9aSAndroid Build Coastguard Worker
140*cf84ac9aSAndroid Build Coastguard Worker (void) close(0);
141*cf84ac9aSAndroid Build Coastguard Worker
142*cf84ac9aSAndroid Build Coastguard Worker sigsuspend(&unblock_mask);
143*cf84ac9aSAndroid Build Coastguard Worker tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_STOPPED"
144*cf84ac9aSAndroid Build Coastguard Worker ", si_pid=%d, si_uid=%u, si_status=SIGSTOP"
145*cf84ac9aSAndroid Build Coastguard Worker ", si_utime=%llu, si_stime=%llu} ---\n",
146*cf84ac9aSAndroid Build Coastguard Worker sinfo.si_pid, sinfo.si_uid,
147*cf84ac9aSAndroid Build Coastguard Worker zero_extend_signed_to_ull(sinfo.si_utime),
148*cf84ac9aSAndroid Build Coastguard Worker zero_extend_signed_to_ull(sinfo.si_stime));
149*cf84ac9aSAndroid Build Coastguard Worker
150*cf84ac9aSAndroid Build Coastguard Worker assert(kill(pid, SIGCONT) == 0);
151*cf84ac9aSAndroid Build Coastguard Worker
152*cf84ac9aSAndroid Build Coastguard Worker sigsuspend(&unblock_mask);
153*cf84ac9aSAndroid Build Coastguard Worker tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_CONTINUED"
154*cf84ac9aSAndroid Build Coastguard Worker ", si_pid=%d, si_uid=%u, si_status=SIGCONT"
155*cf84ac9aSAndroid Build Coastguard Worker ", si_utime=%llu, si_stime=%llu} ---\n",
156*cf84ac9aSAndroid Build Coastguard Worker sinfo.si_pid, sinfo.si_uid,
157*cf84ac9aSAndroid Build Coastguard Worker zero_extend_signed_to_ull(sinfo.si_utime),
158*cf84ac9aSAndroid Build Coastguard Worker zero_extend_signed_to_ull(sinfo.si_stime));
159*cf84ac9aSAndroid Build Coastguard Worker
160*cf84ac9aSAndroid Build Coastguard Worker assert(write(1, "", 1) == 1);
161*cf84ac9aSAndroid Build Coastguard Worker (void) close(1);
162*cf84ac9aSAndroid Build Coastguard Worker
163*cf84ac9aSAndroid Build Coastguard Worker sigsuspend(&unblock_mask);
164*cf84ac9aSAndroid Build Coastguard Worker tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED"
165*cf84ac9aSAndroid Build Coastguard Worker ", si_pid=%d, si_uid=%u, si_status=0"
166*cf84ac9aSAndroid Build Coastguard Worker ", si_utime=%llu, si_stime=%llu} ---\n",
167*cf84ac9aSAndroid Build Coastguard Worker sinfo.si_pid, sinfo.si_uid,
168*cf84ac9aSAndroid Build Coastguard Worker zero_extend_signed_to_ull(sinfo.si_utime),
169*cf84ac9aSAndroid Build Coastguard Worker zero_extend_signed_to_ull(sinfo.si_stime));
170*cf84ac9aSAndroid Build Coastguard Worker
171*cf84ac9aSAndroid Build Coastguard Worker assert(wait(&s) == pid && s == 0);
172*cf84ac9aSAndroid Build Coastguard Worker
173*cf84ac9aSAndroid Build Coastguard Worker tprintf("%s\n", "+++ exited with 0 +++");
174*cf84ac9aSAndroid Build Coastguard Worker return 0;
175*cf84ac9aSAndroid Build Coastguard Worker }
176