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 * SPDX-License-Identifier: GPL-2.0-or-later
17 #include <sys/resource.h>
20 sprint_rusage(const struct rusage *const ru)
22 static char buf[1024];
23 snprintf(buf, sizeof(buf),
24 "{ru_utime={tv_sec=%lld, tv_usec=%llu}"
25 ", ru_stime={tv_sec=%lld, tv_usec=%llu}"
44 , (long long) ru->ru_utime.tv_sec
45 , zero_extend_signed_to_ull(ru->ru_utime.tv_usec)
46 , (long long) ru->ru_stime.tv_sec
47 , zero_extend_signed_to_ull(ru->ru_stime.tv_usec)
49 , (long) ru->ru_maxrss
53 , (long) ru->ru_minflt
54 , (long) ru->ru_majflt
56 , (long) ru->ru_inblock
57 , (long) ru->ru_oublock
58 , (long) ru->ru_msgsnd
59 , (long) ru->ru_msgrcv
60 , (long) ru->ru_nsignals
62 , (long) ru->ru_nivcsw
69 do_wait4(pid_t pid, int *wstatus, int options, struct rusage *ru)
72 sigaddset(&mask, SIGCHLD);
74 assert(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
75 pid_t rc = wait4(pid, wstatus, options, ru);
76 assert(sigprocmask(SIG_UNBLOCK, &mask, NULL) == 0);
87 perror_msg_and_fail("pipe");
92 perror_msg_and_fail("fork");
97 assert(read(0, &c, sizeof(c)) == 1);
103 TAIL_ALLOC_OBJECT_CONST_PTR(int, s);
104 if (wait4(pid, s, WNOHANG|__WALL, NULL))
105 perror_msg_and_fail("wait4 #1");
106 tprintf("wait4(%d, %p, WNOHANG|__WALL, NULL) = 0\n", pid, s);
108 TAIL_ALLOC_OBJECT_CONST_PTR(struct rusage, rusage);
109 if (wait4(pid, s, WNOHANG|__WALL, rusage))
110 perror_msg_and_fail("wait4 #2");
111 tprintf("wait4(%d, %p, WNOHANG|__WALL, %p) = 0\n", pid, s, rusage);
113 assert(write(1, "", 1) == 1);
116 assert(do_wait4(pid, s, 0, rusage) == pid);
117 assert(WIFEXITED(*s) && WEXITSTATUS(*s) == 42);
118 tprintf("wait4(%d, [{WIFEXITED(s) && WEXITSTATUS(s) == 42}], 0, %s)"
119 " = %d\n", pid, sprint_rusage(rusage), pid);
123 perror_msg_and_fail("fork");
126 (void) raise(SIGUSR1);
130 assert(do_wait4(pid, s, __WALL, rusage) == pid);
131 assert(WIFSIGNALED(*s) && WTERMSIG(*s) == SIGUSR1);
132 tprintf("wait4(%d, [{WIFSIGNALED(s) && WTERMSIG(s) == SIGUSR1}]"
133 ", __WALL, %s) = %d\n", pid, sprint_rusage(rusage), pid);
136 perror_msg_and_fail("pipe");
139 perror_msg_and_fail("fork");
145 assert(read(0, &c, sizeof(c)) == 1);
151 assert(do_wait4(pid, s, WSTOPPED, rusage) == pid);
152 assert(WIFSTOPPED(*s) && WSTOPSIG(*s) == SIGSTOP);
153 tprintf("wait4(%d, [{WIFSTOPPED(s) && WSTOPSIG(s) == SIGSTOP}]"
154 ", WSTOPPED, %s) = %d\n", pid, sprint_rusage(rusage), pid);
156 if (kill(pid, SIGCONT))
157 perror_msg_and_fail("kill(SIGCONT)");
159 #if defined WCONTINUED && defined WIFCONTINUED
160 assert(do_wait4(pid, s, WCONTINUED, rusage) == pid);
161 assert(WIFCONTINUED(*s));
162 tprintf("wait4(%d, [{WIFCONTINUED(s)}], WCONTINUED"
163 ", %s) = %d\n", pid, sprint_rusage(rusage), pid);
164 #endif /* WCONTINUED && WIFCONTINUED */
166 assert(write(1, "", 1) == 1);
169 assert(do_wait4(pid, s, 0, rusage) == pid);
170 assert(WIFEXITED(*s) && WEXITSTATUS(*s) == 0);
171 tprintf("wait4(%d, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0"
172 ", %s) = %d\n", pid, sprint_rusage(rusage), pid);
174 assert(wait4(-1, s, WNOHANG|WSTOPPED|__WALL, rusage) == -1);
175 tprintf("wait4(-1, %p, WNOHANG|WSTOPPED|__WALL, %p) = -1 %s (%m)\n",
176 s, rusage, errno2name());
178 tprintf("%s\n", "+++ exited with 0 +++");