2 * Check decoding of wait4 syscall.
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.
36 #include <sys/resource.h>
39 sprint_rusage(const struct rusage *const ru)
41 static char buf[1024];
42 snprintf(buf, sizeof(buf),
43 "{ru_utime={tv_sec=%lu, tv_usec=%lu}"
44 ", ru_stime={tv_sec=%lu, tv_usec=%lu}"
63 , (long) ru->ru_utime.tv_sec
64 , (long) ru->ru_utime.tv_usec
65 , (long) ru->ru_stime.tv_sec
66 , (long) ru->ru_stime.tv_usec
68 , (long) ru->ru_maxrss
72 , (long) ru->ru_minflt
73 , (long) ru->ru_majflt
75 , (long) ru->ru_inblock
76 , (long) ru->ru_oublock
77 , (long) ru->ru_msgsnd
78 , (long) ru->ru_msgrcv
79 , (long) ru->ru_nsignals
81 , (long) ru->ru_nivcsw
88 do_wait4(pid_t pid, int *wstatus, int options, struct rusage *ru)
91 sigaddset(&mask, SIGCHLD);
93 assert(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
94 pid_t rc = wait4(pid, wstatus, options, ru);
95 assert(sigprocmask(SIG_UNBLOCK, &mask, NULL) == 0);
106 perror_msg_and_fail("pipe");
111 perror_msg_and_fail("fork");
116 assert(read(0, &c, sizeof(c)) == 1);
122 int *const s = tail_alloc(sizeof(*s));
123 if (wait4(pid, s, WNOHANG|__WALL, NULL))
124 perror_msg_and_fail("wait4 #1");
125 tprintf("wait4(%d, %p, WNOHANG|__WALL, NULL) = 0\n", pid, s);
127 struct rusage *const rusage = tail_alloc(sizeof(*rusage));
128 if (wait4(pid, s, WNOHANG|__WALL, rusage))
129 perror_msg_and_fail("wait4 #2");
130 tprintf("wait4(%d, %p, WNOHANG|__WALL, %p) = 0\n", pid, s, rusage);
132 assert(write(1, "", 1) == 1);
135 assert(do_wait4(pid, s, 0, rusage) == pid);
136 assert(WIFEXITED(*s) && WEXITSTATUS(*s) == 42);
137 tprintf("wait4(%d, [{WIFEXITED(s) && WEXITSTATUS(s) == 42}], 0, %s)"
138 " = %d\n", pid, sprint_rusage(rusage), pid);
142 perror_msg_and_fail("fork");
145 (void) raise(SIGUSR1);
149 assert(do_wait4(pid, s, __WALL, rusage) == pid);
150 assert(WIFSIGNALED(*s) && WTERMSIG(*s) == SIGUSR1);
151 tprintf("wait4(%d, [{WIFSIGNALED(s) && WTERMSIG(s) == SIGUSR1}]"
152 ", __WALL, %s) = %d\n", pid, sprint_rusage(rusage), pid);
155 perror_msg_and_fail("pipe");
158 perror_msg_and_fail("fork");
164 assert(read(0, &c, sizeof(c)) == 1);
170 assert(do_wait4(pid, s, WSTOPPED, rusage) == pid);
171 assert(WIFSTOPPED(*s) && WSTOPSIG(*s) == SIGSTOP);
172 tprintf("wait4(%d, [{WIFSTOPPED(s) && WSTOPSIG(s) == SIGSTOP}]"
173 ", WSTOPPED, %s) = %d\n", pid, sprint_rusage(rusage), pid);
175 if (kill(pid, SIGCONT))
176 perror_msg_and_fail("kill(SIGCONT)");
178 #if defined WCONTINUED && defined WIFCONTINUED
179 assert(do_wait4(pid, s, WCONTINUED, rusage) == pid);
180 assert(WIFCONTINUED(*s));
181 tprintf("wait4(%d, [{WIFCONTINUED(s)}], WCONTINUED"
182 ", %s) = %d\n", pid, sprint_rusage(rusage), pid);
183 #endif /* WCONTINUED && WIFCONTINUED */
185 assert(write(1, "", 1) == 1);
188 assert(do_wait4(pid, s, 0, rusage) == pid);
189 assert(WIFEXITED(*s) && WEXITSTATUS(*s) == 0);
190 tprintf("wait4(%d, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0"
191 ", %s) = %d\n", pid, sprint_rusage(rusage), pid);
193 assert(wait4(-1, s, WNOHANG|WSTOPPED|__WALL, rusage) == -1);
194 tprintf("wait4(-1, %p, WNOHANG|WSTOPPED|__WALL, %p) = -1 %s (%m)\n",
195 s, rusage, errno2name());
197 tprintf("%s\n", "+++ exited with 0 +++");