2 * Check SIGCHLD siginfo_t decoding.
4 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 static siginfo_t sinfo;
40 handler(int no, siginfo_t *si, void *uc)
42 memcpy(&sinfo, si, sizeof(sinfo));
52 perror_msg_and_fail("pipe");
56 perror_msg_and_fail("fork");
61 assert(read(0, &c, sizeof(c)) == 1);
67 struct sigaction sa = {
68 .sa_sigaction = handler,
69 .sa_flags = SA_SIGINFO
71 assert(sigaction(SIGCHLD, &sa, NULL) == 0);
73 sigset_t block_mask, unblock_mask;
74 assert(sigprocmask(SIG_SETMASK, NULL, &block_mask) == 0);
75 sigaddset(&block_mask, SIGCHLD);
76 assert(sigprocmask(SIG_SETMASK, &block_mask, NULL) == 0);
78 unblock_mask = block_mask;
79 sigdelset(&unblock_mask, SIGCHLD);
81 assert(write(1, "", 1) == 1);
84 sigsuspend(&unblock_mask);
85 tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED"
86 ", si_pid=%d, si_uid=%u, si_status=%d"
87 ", si_utime=%llu, si_stime=%llu} ---\n",
88 sinfo.si_pid, sinfo.si_uid, sinfo.si_status,
89 zero_extend_signed_to_ull(sinfo.si_utime),
90 zero_extend_signed_to_ull(sinfo.si_stime));
93 assert(wait(&s) == pid);
94 assert(WIFEXITED(s) && WEXITSTATUS(s) == 42);
97 perror_msg_and_fail("pipe");
100 perror_msg_and_fail("fork");
105 assert(read(0, &c, sizeof(c)) == 1);
106 (void) raise(SIGUSR1);
112 assert(write(1, "", 1) == 1);
115 sigsuspend(&unblock_mask);
116 tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_KILLED"
117 ", si_pid=%d, si_uid=%u, si_status=SIGUSR1"
118 ", si_utime=%llu, si_stime=%llu} ---\n",
119 sinfo.si_pid, sinfo.si_uid,
120 zero_extend_signed_to_ull(sinfo.si_utime),
121 zero_extend_signed_to_ull(sinfo.si_stime));
123 assert(wait(&s) == pid);
124 assert(WIFSIGNALED(s) && WTERMSIG(s) == SIGUSR1);
127 perror_msg_and_fail("pipe");
130 perror_msg_and_fail("fork");
136 assert(read(0, &c, sizeof(c)) == 1);
142 sigsuspend(&unblock_mask);
143 tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_STOPPED"
144 ", si_pid=%d, si_uid=%u, si_status=SIGSTOP"
145 ", si_utime=%llu, si_stime=%llu} ---\n",
146 sinfo.si_pid, sinfo.si_uid,
147 zero_extend_signed_to_ull(sinfo.si_utime),
148 zero_extend_signed_to_ull(sinfo.si_stime));
150 assert(kill(pid, SIGCONT) == 0);
152 sigsuspend(&unblock_mask);
153 tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_CONTINUED"
154 ", si_pid=%d, si_uid=%u, si_status=SIGCONT"
155 ", si_utime=%llu, si_stime=%llu} ---\n",
156 sinfo.si_pid, sinfo.si_uid,
157 zero_extend_signed_to_ull(sinfo.si_utime),
158 zero_extend_signed_to_ull(sinfo.si_stime));
160 assert(write(1, "", 1) == 1);
163 sigsuspend(&unblock_mask);
164 tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED"
165 ", si_pid=%d, si_uid=%u, si_status=0"
166 ", si_utime=%llu, si_stime=%llu} ---\n",
167 sinfo.si_pid, sinfo.si_uid,
168 zero_extend_signed_to_ull(sinfo.si_utime),
169 zero_extend_signed_to_ull(sinfo.si_stime));
171 assert(wait(&s) == pid && s == 0);
173 tprintf("%s\n", "+++ exited with 0 +++");