2 * Check decoding of wait4 syscall.
4 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016-2017 The strace developers.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/resource.h>
40 sprint_rusage(const struct rusage *const ru)
42 static char buf[1024];
43 snprintf(buf, sizeof(buf),
44 "{ru_utime={tv_sec=%lld, tv_usec=%llu}"
45 ", ru_stime={tv_sec=%lld, tv_usec=%llu}"
64 , (long long) ru->ru_utime.tv_sec
65 , zero_extend_signed_to_ull(ru->ru_utime.tv_usec)
66 , (long long) ru->ru_stime.tv_sec
67 , zero_extend_signed_to_ull(ru->ru_stime.tv_usec)
69 , (long) ru->ru_maxrss
73 , (long) ru->ru_minflt
74 , (long) ru->ru_majflt
76 , (long) ru->ru_inblock
77 , (long) ru->ru_oublock
78 , (long) ru->ru_msgsnd
79 , (long) ru->ru_msgrcv
80 , (long) ru->ru_nsignals
82 , (long) ru->ru_nivcsw
89 do_wait4(pid_t pid, int *wstatus, int options, struct rusage *ru)
92 sigaddset(&mask, SIGCHLD);
94 assert(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
95 pid_t rc = wait4(pid, wstatus, options, ru);
96 assert(sigprocmask(SIG_UNBLOCK, &mask, NULL) == 0);
107 perror_msg_and_fail("pipe");
112 perror_msg_and_fail("fork");
117 assert(read(0, &c, sizeof(c)) == 1);
123 TAIL_ALLOC_OBJECT_CONST_PTR(int, s);
124 if (wait4(pid, s, WNOHANG|__WALL, NULL))
125 perror_msg_and_fail("wait4 #1");
126 tprintf("wait4(%d, %p, WNOHANG|__WALL, NULL) = 0\n", pid, s);
128 TAIL_ALLOC_OBJECT_CONST_PTR(struct rusage, rusage);
129 if (wait4(pid, s, WNOHANG|__WALL, rusage))
130 perror_msg_and_fail("wait4 #2");
131 tprintf("wait4(%d, %p, WNOHANG|__WALL, %p) = 0\n", pid, s, rusage);
133 assert(write(1, "", 1) == 1);
136 assert(do_wait4(pid, s, 0, rusage) == pid);
137 assert(WIFEXITED(*s) && WEXITSTATUS(*s) == 42);
138 tprintf("wait4(%d, [{WIFEXITED(s) && WEXITSTATUS(s) == 42}], 0, %s)"
139 " = %d\n", pid, sprint_rusage(rusage), pid);
143 perror_msg_and_fail("fork");
146 (void) raise(SIGUSR1);
150 assert(do_wait4(pid, s, __WALL, rusage) == pid);
151 assert(WIFSIGNALED(*s) && WTERMSIG(*s) == SIGUSR1);
152 tprintf("wait4(%d, [{WIFSIGNALED(s) && WTERMSIG(s) == SIGUSR1}]"
153 ", __WALL, %s) = %d\n", pid, sprint_rusage(rusage), pid);
156 perror_msg_and_fail("pipe");
159 perror_msg_and_fail("fork");
165 assert(read(0, &c, sizeof(c)) == 1);
171 assert(do_wait4(pid, s, WSTOPPED, rusage) == pid);
172 assert(WIFSTOPPED(*s) && WSTOPSIG(*s) == SIGSTOP);
173 tprintf("wait4(%d, [{WIFSTOPPED(s) && WSTOPSIG(s) == SIGSTOP}]"
174 ", WSTOPPED, %s) = %d\n", pid, sprint_rusage(rusage), pid);
176 if (kill(pid, SIGCONT))
177 perror_msg_and_fail("kill(SIGCONT)");
179 #if defined WCONTINUED && defined WIFCONTINUED
180 assert(do_wait4(pid, s, WCONTINUED, rusage) == pid);
181 assert(WIFCONTINUED(*s));
182 tprintf("wait4(%d, [{WIFCONTINUED(s)}], WCONTINUED"
183 ", %s) = %d\n", pid, sprint_rusage(rusage), pid);
184 #endif /* WCONTINUED && WIFCONTINUED */
186 assert(write(1, "", 1) == 1);
189 assert(do_wait4(pid, s, 0, rusage) == pid);
190 assert(WIFEXITED(*s) && WEXITSTATUS(*s) == 0);
191 tprintf("wait4(%d, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0"
192 ", %s) = %d\n", pid, sprint_rusage(rusage), pid);
194 assert(wait4(-1, s, WNOHANG|WSTOPPED|__WALL, rusage) == -1);
195 tprintf("wait4(-1, %p, WNOHANG|WSTOPPED|__WALL, %p) = -1 %s (%m)\n",
196 s, rusage, errno2name());
198 tprintf("%s\n", "+++ exited with 0 +++");