Debian Codegraph @

src/test/test-pidref.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "fd-util.h"
4 #include "format-util.h"
5 #include "pidref.h"
6 #include "process-util.h"
7 #include "stdio-util.h"
8 #include "tests.h"
9 #include "time-util.h"
10
11 TEST(pidref_is_set) {
12 ASSERT_FALSE(pidref_is_set(NULL));
13 ASSERT_FALSE(pidref_is_set(&PIDREF_NULL));
14 ASSERT_TRUE(pidref_is_set(&PIDREF_MAKE_FROM_PID(1)));
15 }
16
17 TEST(pidref_equal) {
18 ASSERT_TRUE(pidref_equal(NULL, NULL));
19 ASSERT_TRUE(pidref_equal(NULL, &PIDREF_NULL));
20 ASSERT_TRUE(pidref_equal(&PIDREF_NULL, NULL));
21 ASSERT_TRUE(pidref_equal(&PIDREF_NULL, &PIDREF_NULL));
22
23 ASSERT_FALSE(pidref_equal(NULL, &PIDREF_MAKE_FROM_PID(1)));
24 ASSERT_FALSE(pidref_equal(&PIDREF_MAKE_FROM_PID(1), NULL));
25 ASSERT_FALSE(pidref_equal(&PIDREF_NULL, &PIDREF_MAKE_FROM_PID(1)));
26 ASSERT_FALSE(pidref_equal(&PIDREF_MAKE_FROM_PID(1), &PIDREF_NULL));
27 ASSERT_TRUE(pidref_equal(&PIDREF_MAKE_FROM_PID(1), &PIDREF_MAKE_FROM_PID(1)));
28 ASSERT_FALSE(pidref_equal(&PIDREF_MAKE_FROM_PID(1), &PIDREF_MAKE_FROM_PID(2)));
29 }
30
31 TEST(pidref_set_pid) {
32 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
33 int r;
34
35 r = pidref_set_pid(&pidref, 1);
36 if (r == -ESRCH)
37 return (void) log_tests_skipped_errno(r, "PID1 does not exist");
38 ASSERT_OK(r);
39
40 ASSERT_TRUE(pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(1)));
41 ASSERT_FALSE(pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(2)));
42 }
43
44 TEST(pidref_set_self) {
45 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
46
47 ASSERT_OK(pidref_set_self(&pidref));
48 ASSERT_TRUE(pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(getpid_cached())));
49 ASSERT_FALSE(pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(getpid_cached()+1)));
50 }
51
52 TEST(pidref_set_pidstr) {
53 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
54 char buf[DECIMAL_STR_MAX(pid_t)];
55
56 xsprintf(buf, PID_FMT, getpid_cached());
57 ASSERT_OK(pidref_set_pidstr(&pidref, buf));
58 ASSERT_TRUE(pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(getpid_cached())));
59 ASSERT_FALSE(pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(getpid_cached()+1)));
60 }
61
62 TEST(pidref_set_pidfd) {
63 _cleanup_(pidref_done) PidRef a = PIDREF_NULL, b = PIDREF_NULL, c = PIDREF_NULL, d = PIDREF_NULL;
64
65 ASSERT_OK(pidref_set_self(&a));
66 if (a.fd < 0)
67 return (void) log_tests_skipped("PIDFD not supported");
68
69 ASSERT_OK(pidref_set_pidfd(&b, a.fd));
70 ASSERT_TRUE(pidref_equal(&a, &b));
71 ASSERT_OK(pidref_set_pidfd_take(&c, b.fd));
72 b.fd = -EBADF;
73 ASSERT_TRUE(pidref_equal(&a, &c));
74 ASSERT_OK(pidref_set_pidfd_consume(&d, TAKE_FD(c.fd)));
75 ASSERT_TRUE(pidref_equal(&a, &d));
76 }
77
78 TEST(pidref_is_self) {
79 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
80
81 ASSERT_OK(pidref_set_self(&pidref));
82 ASSERT_TRUE(pidref_is_self(&pidref));
83
84 ASSERT_FALSE(pidref_is_self(NULL));
85 ASSERT_FALSE(pidref_is_self(&PIDREF_NULL));
86 ASSERT_TRUE(pidref_is_self(&PIDREF_MAKE_FROM_PID(getpid_cached())));
87 ASSERT_FALSE(pidref_is_self(&PIDREF_MAKE_FROM_PID(getpid_cached()+1)));
88 }
89
90 TEST(pidref_copy) {
91 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
92 int r;
93
94 ASSERT_OK(pidref_copy(NULL, &pidref));
95 ASSERT_FALSE(pidref_is_set(&pidref));
96
97 ASSERT_OK(pidref_copy(&PIDREF_NULL, &pidref));
98 ASSERT_FALSE(pidref_is_set(&pidref));
99
100 ASSERT_OK(pidref_copy(&PIDREF_MAKE_FROM_PID(getpid_cached()), &pidref));
101 ASSERT_TRUE(pidref_is_self(&pidref));
102 pidref_done(&pidref);
103
104 r = pidref_copy(&PIDREF_MAKE_FROM_PID(1), &pidref);
105 if (r == -ESRCH)
106 return (void) log_tests_skipped_errno(r, "PID1 does not exist");
107 ASSERT_OK(r);
108 ASSERT_TRUE(pidref_equal(&pidref, &PIDREF_MAKE_FROM_PID(1)));
109 }
110
111 TEST(pidref_dup) {
112 _cleanup_(pidref_freep) PidRef *pidref = NULL;
113 int r;
114
115 ASSERT_OK(pidref_dup(NULL, &pidref));
116 ASSERT_NOT_NULL(pidref);
117 ASSERT_FALSE(pidref_is_set(pidref));
118 pidref = pidref_free(pidref);
119
120 ASSERT_OK(pidref_dup(&PIDREF_NULL, &pidref));
121 ASSERT_NOT_NULL(pidref);
122 ASSERT_FALSE(pidref_is_set(pidref));
123 pidref = pidref_free(pidref);
124
125 ASSERT_OK(pidref_dup(&PIDREF_MAKE_FROM_PID(getpid_cached()), &pidref));
126 ASSERT_TRUE(pidref_is_self(pidref));
127 pidref = pidref_free(pidref);
128
129 r = pidref_dup(&PIDREF_MAKE_FROM_PID(1), &pidref);
130 if (r == -ESRCH)
131 return (void) log_tests_skipped_errno(r, "PID1 does not exist");
132 ASSERT_OK(r);
133 ASSERT_TRUE(pidref_equal(pidref, &PIDREF_MAKE_FROM_PID(1)));
134 }
135
136 TEST(pidref_new_from_pid) {
137 _cleanup_(pidref_freep) PidRef *pidref = NULL;
138 int r;
139
140 ASSERT_ERROR(pidref_new_from_pid(-1, &pidref), ESRCH);
141 ASSERT_NULL(pidref);
142
143 ASSERT_OK(pidref_new_from_pid(0, &pidref));
144 ASSERT_TRUE(pidref_is_self(pidref));
145 pidref = pidref_free(pidref);
146
147 ASSERT_OK(pidref_new_from_pid(getpid_cached(), &pidref));
148 ASSERT_TRUE(pidref_is_self(pidref));
149 pidref = pidref_free(pidref);
150
151 r = pidref_new_from_pid(1, &pidref);
152 if (r == -ESRCH)
153 return (void) log_tests_skipped_errno(r, "PID1 does not exist");
154 ASSERT_OK(r);
155 ASSERT_TRUE(pidref_equal(pidref, &PIDREF_MAKE_FROM_PID(1)));
156 }
157
158 TEST(pidref_kill) {
159 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
160 siginfo_t si;
161
162 ASSERT_OK_POSITIVE(pidref_safe_fork("(test-pidref-kill)", FORK_DEATHSIG_SIGKILL|FORK_FREEZE, &pidref));
163
164 ASSERT_OK(pidref_kill(&pidref, SIGKILL));
165 ASSERT_OK(pidref_wait_for_terminate(&pidref, &si));
166 ASSERT_EQ(si.si_signo, SIGCHLD);
167 }
168
169 TEST(pidref_kill_and_sigcont) {
170 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
171 siginfo_t si;
172
173 ASSERT_OK_POSITIVE(pidref_safe_fork("(test-pidref-kill-and-sigcont)", FORK_DEATHSIG_SIGTERM|FORK_FREEZE, &pidref));
174
175 ASSERT_OK(pidref_kill_and_sigcont(&pidref, SIGTERM));
176 ASSERT_OK(pidref_wait_for_terminate(&pidref, &si));
177 ASSERT_EQ(si.si_signo, SIGCHLD);
178 }
179
180 TEST(pidref_sigqueue) {
181 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
182 siginfo_t si;
183
184 ASSERT_OK_POSITIVE(pidref_safe_fork("(test-pidref-sigqueue)", FORK_DEATHSIG_SIGTERM|FORK_FREEZE, &pidref));
185
186 ASSERT_OK(pidref_sigqueue(&pidref, SIGTERM, 42));
187 ASSERT_OK(pidref_wait_for_terminate(&pidref, &si));
188 ASSERT_EQ(si.si_signo, SIGCHLD);
189 }
190
191 TEST(pidref_done_sigkill_wait) {
192 _cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
193
194 ASSERT_OK_POSITIVE(pidref_safe_fork("(test-pidref-done-sigkill-wait)", FORK_DEATHSIG_SIGKILL|FORK_FREEZE, &pidref));
195 }
196
197 TEST(pidref_verify) {
198 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
199 int r;
200
201 ASSERT_ERROR(pidref_verify(NULL), ESRCH);
202 ASSERT_ERROR(pidref_verify(&PIDREF_NULL), ESRCH);
203
204 ASSERT_OK_POSITIVE(pidref_verify(&PIDREF_MAKE_FROM_PID(1)));
205 ASSERT_OK_ZERO(pidref_verify(&PIDREF_MAKE_FROM_PID(getpid_cached())));
206
207 ASSERT_OK(pidref_set_self(&pidref));
208 ASSERT_OK(r = pidref_verify(&pidref));
209 ASSERT_EQ(r, (pidref.fd >= 0));
210 }
211
212 TEST(pidref_is_automatic) {
213 ASSERT_FALSE(pidref_is_automatic(NULL));
214 ASSERT_FALSE(pidref_is_automatic(&PIDREF_NULL));
215 ASSERT_FALSE(pidref_is_automatic(&PIDREF_MAKE_FROM_PID(1)));
216 ASSERT_FALSE(pidref_is_automatic(&PIDREF_MAKE_FROM_PID(getpid_cached())));
217 ASSERT_TRUE(pidref_is_automatic(&PIDREF_AUTOMATIC));
218
219 ASSERT_FALSE(pid_is_automatic(0));
220 ASSERT_FALSE(pid_is_automatic(1));
221 ASSERT_FALSE(pid_is_automatic(getpid_cached()));
222 ASSERT_TRUE(pid_is_automatic(PID_AUTOMATIC));
223
224 ASSERT_FALSE(pidref_is_set(&PIDREF_AUTOMATIC));
225 ASSERT_FALSE(pid_is_valid(PID_AUTOMATIC));
226 }
227
228 TEST(pidref_is_remote) {
229 ASSERT_FALSE(pidref_is_remote(NULL));
230 ASSERT_FALSE(pidref_is_remote(&PIDREF_NULL));
231 ASSERT_FALSE(pidref_is_remote(&PIDREF_MAKE_FROM_PID(1)));
232 ASSERT_FALSE(pidref_is_remote(&PIDREF_MAKE_FROM_PID(getpid_cached())));
233 ASSERT_FALSE(pidref_is_remote(&PIDREF_AUTOMATIC));
234
235 PidRef p = {
236 .pid = 1,
237 .fd = -EREMOTE,
238 .fd_id = 4711,
239 };
240
241 ASSERT_TRUE(pidref_is_set(&p));
242 ASSERT_TRUE(pidref_is_remote(&p));
243 ASSERT_FALSE(pidref_is_automatic(&p));
244 ASSERT_ERROR(pidref_kill(&p, SIGTERM), EREMOTE);
245 ASSERT_ERROR(pidref_kill_and_sigcont(&p, SIGTERM), EREMOTE);
246 ASSERT_ERROR(pidref_wait_for_terminate(&p, NULL), EREMOTE);
247 ASSERT_ERROR(pidref_verify(&p), EREMOTE);
248 }
249
250 TEST(pidref_wait_for_terminate_timeout) {
251 _cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
252 siginfo_t si;
253
254 /* Test successful termination within timeout */
255 ASSERT_OK(pidref_safe_fork("(test-pidref-wait-timeout)", FORK_DEATHSIG_SIGKILL|FORK_FREEZE, &pidref));
256
257 assert_se(pidref_kill(&pidref, SIGKILL) >= 0);
258 ASSERT_OK(pidref_wait_for_terminate_full(&pidref, 5 * USEC_PER_SEC, &si));
259 ASSERT_EQ(si.si_signo, SIGCHLD);
260
261 pidref_done(&pidref);
262
263 /* Test timeout when process doesn't terminate */
264 ASSERT_OK(pidref_safe_fork("(test-pidref-wait-timeout-expired)", FORK_DEATHSIG_SIGKILL|FORK_FREEZE, &pidref));
265 ASSERT_ERROR(pidref_wait_for_terminate_full(&pidref, 100 * USEC_PER_MSEC, NULL), ETIMEDOUT);
266 }
267
268 DEFINE_TEST_MAIN(LOG_DEBUG);