blob: 48cfaf3aa942a2a4e62cf72e36f887099a30e2f1 [file] [log] [blame]
Dmitry V. Levin056e3042016-06-10 09:22:12 +00001/*
2 * Check SIGCHLD siginfo_t decoding.
3 *
4 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
17 *
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.
28 */
29
30#include "tests.h"
31#include <assert.h>
32#include <signal.h>
33#include <string.h>
34#include <unistd.h>
35#include <sys/wait.h>
36
37static siginfo_t sinfo;
38
39static void
40handler(int no, siginfo_t *si, void *uc)
41{
42 memcpy(&sinfo, si, sizeof(sinfo));
43}
44
45int
46main(void)
47{
48 tprintf("%s", "");
49
50 int fds[2];
51 if (pipe(fds))
52 perror_msg_and_fail("pipe");
53
54 pid_t pid = fork();
55 if (pid < 0)
56 perror_msg_and_fail("fork");
57
58 if (!pid) {
59 char c;
60 (void) close(1);
61 assert(read(0, &c, sizeof(c)) == 1);
62 return 42;
63 }
64
65 (void) close(0);
66
67 struct sigaction sa = {
68 .sa_sigaction = handler,
69 .sa_flags = SA_SIGINFO
70 };
71 assert(sigaction(SIGCHLD, &sa, NULL) == 0);
72
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);
77
78 unblock_mask = block_mask;
79 sigdelset(&unblock_mask, SIGCHLD);
80
81 assert(write(1, "", 1) == 1);
82 (void) close(1);
83
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 widen_to_ull(sinfo.si_utime), widen_to_ull(sinfo.si_stime));
90
91 int s;
92 assert(wait(&s) == pid);
93 assert(WIFEXITED(s) && WEXITSTATUS(s) == 42);
94
95 if (pipe(fds))
96 perror_msg_and_fail("pipe");
97 pid = fork();
98 if (pid < 0)
99 perror_msg_and_fail("fork");
100
101 if (!pid) {
102 (void) close(1);
103 char c;
104 assert(read(0, &c, sizeof(c)) == 1);
105 (void) raise(SIGUSR1);
106 return 1;
107 }
108
109 (void) close(0);
110
111 assert(write(1, "", 1) == 1);
112 (void) close(1);
113
114 sigsuspend(&unblock_mask);
115 tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_KILLED"
116 ", si_pid=%d, si_uid=%u, si_status=SIGUSR1"
117 ", si_utime=%llu, si_stime=%llu} ---\n",
118 sinfo.si_pid, sinfo.si_uid,
119 widen_to_ull(sinfo.si_utime), widen_to_ull(sinfo.si_stime));
120
121 assert(wait(&s) == pid);
122 assert(WIFSIGNALED(s) && WTERMSIG(s) == SIGUSR1);
123
124 if (pipe(fds))
125 perror_msg_and_fail("pipe");
126 pid = fork();
127 if (pid < 0)
128 perror_msg_and_fail("fork");
129
130 if (!pid) {
131 (void) close(1);
132 raise(SIGSTOP);
133 char c;
134 assert(read(0, &c, sizeof(c)) == 1);
135 return 0;
136 }
137
138 (void) close(0);
139
140 sigsuspend(&unblock_mask);
141 tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_STOPPED"
142 ", si_pid=%d, si_uid=%u, si_status=SIGSTOP"
143 ", si_utime=%llu, si_stime=%llu} ---\n",
144 sinfo.si_pid, sinfo.si_uid,
145 widen_to_ull(sinfo.si_utime), widen_to_ull(sinfo.si_stime));
146
147 assert(kill(pid, SIGCONT) == 0);
148
149 sigsuspend(&unblock_mask);
150 tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_CONTINUED"
151 ", si_pid=%d, si_uid=%u, si_status=SIGCONT"
152 ", si_utime=%llu, si_stime=%llu} ---\n",
153 sinfo.si_pid, sinfo.si_uid,
154 widen_to_ull(sinfo.si_utime), widen_to_ull(sinfo.si_stime));
155
156 assert(write(1, "", 1) == 1);
157 (void) close(1);
158
159 sigsuspend(&unblock_mask);
160 tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED"
161 ", si_pid=%d, si_uid=%u, si_status=0"
162 ", si_utime=%llu, si_stime=%llu} ---\n",
163 sinfo.si_pid, sinfo.si_uid,
164 widen_to_ull(sinfo.si_utime), widen_to_ull(sinfo.si_stime));
165
166 assert(wait(&s) == pid && s == 0);
167
168 tprintf("%s\n", "+++ exited with 0 +++");
169 return 0;
170}