2 * Check decoding of waitid 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.
37 #include <sys/resource.h>
38 #include <asm/unistd.h>
41 sprint_rusage(const struct rusage *const ru)
43 static char buf[1024];
44 snprintf(buf, sizeof(buf),
45 "{ru_utime={tv_sec=%lld, tv_usec=%lld}"
46 ", ru_stime={tv_sec=%lld, tv_usec=%lld}"
65 , (long long) ru->ru_utime.tv_sec
66 , (long long) ru->ru_utime.tv_usec
67 , (long long) ru->ru_stime.tv_sec
68 , (long long) ru->ru_stime.tv_usec
70 , zero_extend_signed_to_ull(ru->ru_maxrss)
71 , zero_extend_signed_to_ull(ru->ru_ixrss)
72 , zero_extend_signed_to_ull(ru->ru_idrss)
73 , zero_extend_signed_to_ull(ru->ru_isrss)
74 , zero_extend_signed_to_ull(ru->ru_minflt)
75 , zero_extend_signed_to_ull(ru->ru_majflt)
76 , zero_extend_signed_to_ull(ru->ru_nswap)
77 , zero_extend_signed_to_ull(ru->ru_inblock)
78 , zero_extend_signed_to_ull(ru->ru_oublock)
79 , zero_extend_signed_to_ull(ru->ru_msgsnd)
80 , zero_extend_signed_to_ull(ru->ru_msgrcv)
81 , zero_extend_signed_to_ull(ru->ru_nsignals)
82 , zero_extend_signed_to_ull(ru->ru_nvcsw)
83 , zero_extend_signed_to_ull(ru->ru_nivcsw)
89 #define CASE(x) case x: return #x
92 si_code_2_name(const int code)
113 default: perror_msg_and_fail("unknown si_code %d", code);
118 sprint_siginfo(const siginfo_t *const si, const char *const status_text)
120 static char buf[1024];
121 snprintf(buf, sizeof(buf),
129 si_code_2_name(si->si_code),
133 zero_extend_signed_to_ull(si->si_utime),
134 zero_extend_signed_to_ull(si->si_stime));
139 poison(unsigned int v)
141 return (unsigned long) 0xfacefeed00000000ULL | v;
145 do_waitid(const unsigned int idtype,
146 const unsigned int id,
147 const siginfo_t *const infop,
148 const unsigned int options,
149 const struct rusage *const rusage)
152 sigaddset(&mask, SIGCHLD);
154 assert(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
155 long rc = syscall(__NR_waitid, poison(idtype), poison(id),
156 infop, poison(options), rusage);
157 assert(sigprocmask(SIG_UNBLOCK, &mask, NULL) == 0);
168 perror_msg_and_fail("pipe");
173 perror_msg_and_fail("fork");
178 assert(read(0, &c, sizeof(c)) == 1);
184 if (do_waitid(P_PID, pid, 0, WNOHANG|WEXITED, 0))
185 perror_msg_and_fail("waitid #1");
186 tprintf("waitid(P_PID, %d, NULL, WNOHANG|WEXITED, NULL) = 0\n", pid);
188 TAIL_ALLOC_OBJECT_CONST_PTR(siginfo_t, sinfo);
189 memset(sinfo, 0, sizeof(*sinfo));
190 TAIL_ALLOC_OBJECT_CONST_PTR(struct rusage, rusage);
191 if (do_waitid(P_PID, pid, sinfo, WNOHANG|WEXITED|WSTOPPED, rusage))
192 perror_msg_and_fail("waitid #2");
193 tprintf("waitid(P_PID, %d, {}, WNOHANG|WEXITED|WSTOPPED, %s) = 0\n",
194 pid, sprint_rusage(rusage));
196 assert(write(1, "", 1) == 1);
199 if (do_waitid(P_PID, pid, sinfo, WEXITED, rusage))
200 perror_msg_and_fail("waitid #3");
201 tprintf("waitid(P_PID, %d, %s, WEXITED, %s) = 0\n",
202 pid, sprint_siginfo(sinfo, "42"), sprint_rusage(rusage));
206 perror_msg_and_fail("fork");
209 (void) raise(SIGUSR1);
213 if (do_waitid(P_PID, pid, sinfo, WEXITED, rusage))
214 perror_msg_and_fail("waitid #4");
215 tprintf("waitid(P_PID, %d, %s, WEXITED, %s) = 0\n",
216 pid, sprint_siginfo(sinfo, "SIGUSR1"), sprint_rusage(rusage));
219 perror_msg_and_fail("pipe");
222 perror_msg_and_fail("fork");
228 assert(read(0, &c, sizeof(c)) == 1);
234 if (do_waitid(P_PID, pid, sinfo, WSTOPPED, rusage))
235 perror_msg_and_fail("waitid #5");
236 tprintf("waitid(P_PID, %d, %s, WSTOPPED, %s) = 0\n",
237 pid, sprint_siginfo(sinfo, "SIGSTOP"), sprint_rusage(rusage));
239 if (kill(pid, SIGCONT))
240 perror_msg_and_fail("kill(SIGCONT)");
242 #if defined WCONTINUED
243 if (do_waitid(P_PID, pid, sinfo, WCONTINUED, rusage))
244 perror_msg_and_fail("waitid #6");
245 tprintf("waitid(P_PID, %d, %s, WCONTINUED, %s) = 0\n",
246 pid, sprint_siginfo(sinfo, "SIGCONT"), sprint_rusage(rusage));
247 #endif /* WCONTINUED */
249 assert(write(1, "", 1) == 1);
252 if (do_waitid(P_PID, pid, sinfo, WEXITED, rusage))
253 perror_msg_and_fail("waitid #7");
254 tprintf("waitid(P_PID, %d, %s, WEXITED, %s) = 0\n",
255 pid, sprint_siginfo(sinfo, "0"), sprint_rusage(rusage));
257 long rc = do_waitid(P_ALL, -1, sinfo, WEXITED|WSTOPPED, rusage);
258 tprintf("waitid(P_ALL, -1, %p, WEXITED|WSTOPPED, %p)"
259 " = %ld %s (%m)\n", sinfo, rusage, rc, errno2name());
261 tprintf("%s\n", "+++ exited with 0 +++");