1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
/*
* Check decoding of waitid syscall.
*
* Copyright (c) 2015-2016 Dmitry V. Levin <ldv@strace.io>
* Copyright (c) 2016-2022 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "tests.h"
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "kernel_rusage.h"
#include "scno.h"
#ifndef MY_COMM
# define MY_COMM ""
#endif
#ifndef SKIP_IF_PROC_IS_UNAVAILABLE
# define SKIP_IF_PROC_IS_UNAVAILABLE
#endif
static const char *
sprint_rusage(const kernel_rusage_t *const ru)
{
static char buf[1024];
snprintf(buf, sizeof(buf),
"{ru_utime={tv_sec=%lld, tv_usec=%llu}"
", ru_stime={tv_sec=%lld, tv_usec=%llu}"
#if VERBOSE
", ru_maxrss=%llu"
", ru_ixrss=%llu"
", ru_idrss=%llu"
", ru_isrss=%llu"
", ru_minflt=%llu"
", ru_majflt=%llu"
", ru_nswap=%llu"
", ru_inblock=%llu"
", ru_oublock=%llu"
", ru_msgsnd=%llu"
", ru_msgrcv=%llu"
", ru_nsignals=%llu"
", ru_nvcsw=%llu"
", ru_nivcsw=%llu}"
#else
", ...}"
#endif
, (long long) ru->ru_utime.tv_sec
, zero_extend_signed_to_ull(ru->ru_utime.tv_usec)
, (long long) ru->ru_stime.tv_sec
, zero_extend_signed_to_ull(ru->ru_stime.tv_usec)
#if VERBOSE
, zero_extend_signed_to_ull(ru->ru_maxrss)
, zero_extend_signed_to_ull(ru->ru_ixrss)
, zero_extend_signed_to_ull(ru->ru_idrss)
, zero_extend_signed_to_ull(ru->ru_isrss)
, zero_extend_signed_to_ull(ru->ru_minflt)
, zero_extend_signed_to_ull(ru->ru_majflt)
, zero_extend_signed_to_ull(ru->ru_nswap)
, zero_extend_signed_to_ull(ru->ru_inblock)
, zero_extend_signed_to_ull(ru->ru_oublock)
, zero_extend_signed_to_ull(ru->ru_msgsnd)
, zero_extend_signed_to_ull(ru->ru_msgrcv)
, zero_extend_signed_to_ull(ru->ru_nsignals)
, zero_extend_signed_to_ull(ru->ru_nvcsw)
, zero_extend_signed_to_ull(ru->ru_nivcsw)
#endif
);
return buf;
}
#define CASE(x) case x: return #x
static const char *
si_code_2_name(const int code)
{
switch (code) {
#ifdef CLD_EXITED
CASE(CLD_EXITED);
#endif
#ifdef CLD_KILLED
CASE(CLD_KILLED);
#endif
#ifdef CLD_DUMPED
CASE(CLD_DUMPED);
#endif
#ifdef CLD_TRAPPED
CASE(CLD_TRAPPED);
#endif
#ifdef CLD_STOPPED
CASE(CLD_STOPPED);
#endif
#ifdef CLD_CONTINUED
CASE(CLD_CONTINUED);
#endif
default:
perror_msg_and_fail("unknown si_code %d", code);
}
}
static const char *
sprint_siginfo(const siginfo_t *const si, const char *const status_text,
const char *const comm)
{
static char buf[1024];
char utime_str[64];
char stime_str[64];
snprintf(buf, sizeof(buf),
"{si_signo=SIGCHLD"
", si_code=%s"
", si_pid=%d%s"
", si_uid=%d"
", si_status=%s"
", si_utime=%s"
", si_stime=%s}",
si_code_2_name(si->si_code),
si->si_pid,
comm,
si->si_uid,
status_text,
clock_t_str(zero_extend_signed_to_ull(si->si_utime),
ARRSZ_PAIR(utime_str)),
clock_t_str(zero_extend_signed_to_ull(si->si_stime),
ARRSZ_PAIR(stime_str)));
return buf;
}
static unsigned long
poison(unsigned int v)
{
return (unsigned long) 0xfacefeed00000000ULL | v;
}
static const char *errstr;
static long
do_waitid(const unsigned int idtype,
const unsigned int id,
const siginfo_t *const infop,
const unsigned int options,
const kernel_rusage_t *const rusage)
{
sigset_t mask = {};
sigaddset(&mask, SIGCHLD);
assert(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
long rc = syscall(__NR_waitid, poison(idtype), poison(id),
infop, poison(options), rusage);
assert(sigprocmask(SIG_UNBLOCK, &mask, NULL) == 0);
errstr = sprintrc(rc);
return rc;
}
int
main(void)
{
SKIP_IF_PROC_IS_UNAVAILABLE;
tprintf("%s", "");
int fds[2];
if (pipe(fds))
perror_msg_and_fail("pipe");
pid_t pid;
pid = fork();
if (pid < 0)
perror_msg_and_fail("fork");
if (!pid) {
char c;
(void) close(1);
assert(read(0, &c, sizeof(c)) == 1);
return 42;
}
(void) close(0);
if (do_waitid(P_PID, pid, 0, WNOHANG|WEXITED, 0))
perror_msg_and_fail("waitid #1");
tprintf("waitid(P_PID, %d%s, NULL, WNOHANG|WEXITED, NULL) = 0\n",
pid, MY_COMM);
TAIL_ALLOC_OBJECT_CONST_PTR(siginfo_t, sinfo);
memset(sinfo, 0, sizeof(*sinfo));
TAIL_ALLOC_OBJECT_CONST_PTR(kernel_rusage_t, rusage);
if (do_waitid(P_PID, pid, sinfo, WNOHANG|WEXITED|WSTOPPED, rusage))
perror_msg_and_fail("waitid #2");
tprintf("waitid(P_PID, %d%s, {}, WNOHANG|WEXITED|WSTOPPED, %s) = 0\n",
pid, MY_COMM, sprint_rusage(rusage));
assert(write(1, "", 1) == 1);
(void) close(1);
if (do_waitid(P_PID, pid, sinfo, WEXITED, rusage))
perror_msg_and_fail("waitid #3");
tprintf("waitid(P_PID, %d%s, %s, WEXITED, %s) = 0\n",
pid, MY_COMM, sprint_siginfo(sinfo, "42", ""), sprint_rusage(rusage));
pid = fork();
if (pid < 0)
perror_msg_and_fail("fork");
if (!pid) {
(void) raise(SIGUSR1);
return 1;
}
if (do_waitid(P_PID, pid, sinfo, WEXITED, rusage))
perror_msg_and_fail("waitid #4");
tprintf("waitid(P_PID, %d%s, %s, WEXITED, %s) = 0\n",
pid, MY_COMM, sprint_siginfo(sinfo, "SIGUSR1", ""),
sprint_rusage(rusage));
if (pipe(fds))
perror_msg_and_fail("pipe");
pid = fork();
if (pid < 0)
perror_msg_and_fail("fork");
if (!pid) {
(void) close(1);
raise(SIGSTOP);
char c;
assert(read(0, &c, sizeof(c)) == 1);
return 0;
}
(void) close(0);
if (do_waitid(P_PID, pid, sinfo, WSTOPPED, rusage))
perror_msg_and_fail("waitid #5");
tprintf("waitid(P_PID, %d%s, %s, WSTOPPED, %s) = 0\n",
pid, MY_COMM, sprint_siginfo(sinfo, "SIGSTOP", MY_COMM),
sprint_rusage(rusage));
if (kill(pid, SIGCONT))
perror_msg_and_fail("kill(SIGCONT)");
#if defined WCONTINUED
if (do_waitid(P_PID, pid, sinfo, WCONTINUED, rusage))
perror_msg_and_fail("waitid #6");
tprintf("waitid(P_PID, %d%s, %s, WCONTINUED, %s) = 0\n",
pid, MY_COMM, sprint_siginfo(sinfo, "SIGCONT", MY_COMM),
sprint_rusage(rusage));
#endif /* WCONTINUED */
assert(write(1, "", 1) == 1);
(void) close(1);
if (do_waitid(P_PID, pid, sinfo, WEXITED, rusage))
perror_msg_and_fail("waitid #7");
tprintf("waitid(P_PID, %d%s, %s, WEXITED, %s) = 0\n",
pid, MY_COMM, sprint_siginfo(sinfo, "0", ""),
sprint_rusage(rusage));
pid_t pgid = getpgid(pid);
do_waitid(P_PGID, pgid, sinfo, WEXITED, rusage);
tprintf("waitid(P_PGID, %d, %p, WEXITED, %p) = %s\n",
pgid, sinfo, rusage, errstr);
do_waitid(P_ALL, -1, sinfo, WEXITED|WSTOPPED, rusage);
tprintf("waitid(P_ALL, -1, %p, WEXITED|WSTOPPED, %p) = %s\n",
sinfo, rusage, errstr);
tprintf("%s\n", "+++ exited with 0 +++");
return 0;
}
|