File: siginfo.c

package info (click to toggle)
strace 6.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 71,488 kB
  • sloc: ansic: 176,497; sh: 9,675; makefile: 4,133; cpp: 885; awk: 353; perl: 267; exp: 62; sed: 9
file content (177 lines) | stat: -rw-r--r-- 4,167 bytes parent folder | download | duplicates (9)
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
/*
 * Check SIGCHLD siginfo_t decoding.
 *
 * Copyright (c) 2015-2018 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 <string.h>
#include <unistd.h>
#include <sys/wait.h>

#include "time_enjoyment.h"

enum {
	CPUTIME_LIMIT_NSEC = 100000000,
};

static siginfo_t sinfo;

static void
handler(int no, siginfo_t *si, void *uc)
{
	memcpy(&sinfo, si, sizeof(sinfo));
}

int
main(void)
{
	char utime_str[64];
	char stime_str[64];

	tprintf("%s", "");

	int fds[2];
	if (pipe(fds))
		perror_msg_and_fail("pipe");

	pid_t 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);

	struct sigaction sa = {
		.sa_sigaction = handler,
		.sa_flags = SA_SIGINFO
	};
	assert(sigaction(SIGCHLD, &sa, NULL) == 0);

	sigset_t block_mask, unblock_mask;
	assert(sigprocmask(SIG_SETMASK, NULL, &block_mask) == 0);
	sigaddset(&block_mask, SIGCHLD);
	assert(sigprocmask(SIG_SETMASK, &block_mask, NULL) == 0);

	unblock_mask = block_mask;
	sigdelset(&unblock_mask, SIGCHLD);

	assert(write(1, "", 1) == 1);
	(void) close(1);

	sigsuspend(&unblock_mask);
	tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED"
		", si_pid=%d, si_uid=%d, si_status=%d"
		", si_utime=%s, si_stime=%s} ---\n",
		sinfo.si_pid, sinfo.si_uid, sinfo.si_status,
		clock_t_str(zero_extend_signed_to_ull(sinfo.si_utime),
			    ARRSZ_PAIR(utime_str)),
		clock_t_str(zero_extend_signed_to_ull(sinfo.si_stime),
			    ARRSZ_PAIR(stime_str)));

	int s;
	assert(wait(&s) == pid);
	assert(WIFEXITED(s) && WEXITSTATUS(s) == 42);

	if (pipe(fds))
		perror_msg_and_fail("pipe");
	pid = fork();
	if (pid < 0)
		perror_msg_and_fail("fork");

	if (!pid) {
		(void) close(1);
		char c;
		assert(read(0, &c, sizeof(c)) == 1);
		enjoy_time(CPUTIME_LIMIT_NSEC);
		(void) raise(SIGUSR1);
		return 1;
	}

	(void) close(0);

	assert(write(1, "", 1) == 1);
	(void) close(1);

	sigsuspend(&unblock_mask);
	tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_KILLED"
		", si_pid=%d, si_uid=%d, si_status=SIGUSR1"
		", si_utime=%s, si_stime=%s} ---\n",
		sinfo.si_pid, sinfo.si_uid,
		clock_t_str(zero_extend_signed_to_ull(sinfo.si_utime),
			    ARRSZ_PAIR(utime_str)),
		clock_t_str(zero_extend_signed_to_ull(sinfo.si_stime),
			    ARRSZ_PAIR(stime_str)));

	assert(wait(&s) == pid);
	assert(WIFSIGNALED(s) && WTERMSIG(s) == SIGUSR1);

	if (pipe(fds))
		perror_msg_and_fail("pipe");
	pid = fork();
	if (pid < 0)
		perror_msg_and_fail("fork");

	if (!pid) {
		(void) close(1);
		enjoy_time(CPUTIME_LIMIT_NSEC);
		raise(SIGSTOP);
		char c;
		assert(read(0, &c, sizeof(c)) == 1);
		return 0;
	}

	(void) close(0);

	sigsuspend(&unblock_mask);
	tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_STOPPED"
		", si_pid=%d, si_uid=%d, si_status=SIGSTOP"
		", si_utime=%s, si_stime=%s} ---\n",
		sinfo.si_pid, sinfo.si_uid,
		clock_t_str(zero_extend_signed_to_ull(sinfo.si_utime),
			    ARRSZ_PAIR(utime_str)),
		clock_t_str(zero_extend_signed_to_ull(sinfo.si_stime),
			    ARRSZ_PAIR(stime_str)));

	assert(kill(pid, SIGCONT) == 0);

	sigsuspend(&unblock_mask);
	tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_CONTINUED"
		", si_pid=%d, si_uid=%d, si_status=SIGCONT"
		", si_utime=%s, si_stime=%s} ---\n",
		sinfo.si_pid, sinfo.si_uid,
		clock_t_str(zero_extend_signed_to_ull(sinfo.si_utime),
			    ARRSZ_PAIR(utime_str)),
		clock_t_str(zero_extend_signed_to_ull(sinfo.si_stime),
			    ARRSZ_PAIR(stime_str)));

	assert(write(1, "", 1) == 1);
	(void) close(1);

	sigsuspend(&unblock_mask);
	tprintf("--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED"
		", si_pid=%d, si_uid=%d, si_status=0"
		", si_utime=%s, si_stime=%s} ---\n",
		sinfo.si_pid, sinfo.si_uid,
		clock_t_str(zero_extend_signed_to_ull(sinfo.si_utime),
			    ARRSZ_PAIR(utime_str)),
		clock_t_str(zero_extend_signed_to_ull(sinfo.si_stime),
			    ARRSZ_PAIR(stime_str)));

	assert(wait(&s) == pid && s == 0);

	tprintf("%s\n", "+++ exited with 0 +++");
	return 0;
}