Debian Codegraph @

src/libsystemd/sd-login/test-login.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <poll.h>
4 #include <sys/socket.h>
5
6 #include "sd-login.h"
7
8 #include "alloc-util.h"
9 #include "argv-util.h"
10 #include "errno-list.h"
11 #include "fd-util.h"
12 #include "format-util.h"
13 #include "log.h"
14 #include "pidfd-util.h"
15 #include "process-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "tests.h"
19 #include "time-util.h"
20 #include "user-util.h"
21
22 static char* format_uids(char **buf, uid_t* uids, int count) {
23 int pos = 0, inc;
24 size_t size = (DECIMAL_STR_MAX(uid_t) + 1) * count + 1;
25
26 assert_se(*buf = malloc(size));
27
28 for (int k = 0; k < count; k++) {
29 sprintf(*buf + pos, "%s"UID_FMT"%n", k > 0 ? " " : "", uids[k], &inc);
30 pos += inc;
31 }
32
33 assert_se(pos < (ssize_t)size);
34 (*buf)[pos] = '\0';
35
36 return *buf;
37 }
38
39 #define e(r) (r == 0 ? "OK" : ERRNO_NAME(r))
40
41 TEST(login) {
42 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
43 _cleanup_free_ char *pp = NULL, *qq = NULL,
44 *display_session = NULL, *cgroup = NULL,
45 *display = NULL, *remote_user = NULL, *remote_host = NULL,
46 *type = NULL, *class = NULL, *state = NULL, *state2 = NULL,
47 *seat = NULL, *session = NULL,
48 *unit = NULL, *user_unit = NULL, *slice = NULL;
49 _cleanup_close_ int pidfd = -EBADF;
50 int r;
51 uid_t u, u2 = UID_INVALID;
52 char *t, **seats = NULL, **sessions = NULL;
53
54 r = sd_pid_get_unit(0, &unit);
55 log_info("sd_pid_get_unit(0, …) → %s / \"%s\"", e(r), strnull(unit));
56 assert_se(IN_SET(r, 0, -ENODATA));
57
58 r = sd_pid_get_user_unit(0, &user_unit);
59 log_info("sd_pid_get_user_unit(0, …) → %s / \"%s\"", e(r), strnull(user_unit));
60 assert_se(IN_SET(r, 0, -ENODATA));
61
62 /* Coverage for https://github.com/systemd/systemd/issues/39949 */
63 assert_se(!unit || !user_unit || !streq(unit, user_unit));
64
65 r = sd_pid_get_slice(0, &slice);
66 log_info("sd_pid_get_slice(0, …) → %s / \"%s\"", e(r), strnull(slice));
67 assert_se(IN_SET(r, 0, -ENODATA));
68
69 r = sd_pid_get_owner_uid(0, &u2);
70 log_info("sd_pid_get_owner_uid(0, …) → %s / "UID_FMT, e(r), u2);
71 assert_se(IN_SET(r, 0, -ENODATA));
72
73 r = sd_pid_get_session(0, &session);
74 log_info("sd_pid_get_session(0, …) → %s / \"%s\"", e(r), strnull(session));
75
76 r = sd_pid_get_cgroup(0, &cgroup);
77 log_info("sd_pid_get_cgroup(0, …) → %s / \"%s\"", e(r), strnull(cgroup));
78 assert_se(IN_SET(r, 0, -ENOMEDIUM));
79
80 pidfd = pidfd_open(getpid_cached(), 0);
81 if (pidfd >= 0) {
82 _cleanup_free_ char *cgroup2 = NULL, *session2 = NULL,
83 *unit2 = NULL, *user_unit2 = NULL, *slice2 = NULL;
84
85 r = sd_pidfd_get_unit(pidfd, &unit2);
86 log_info("sd_pidfd_get_unit(pidfd, …) → %s / \"%s\"", e(r), strnull(unit2));
87 assert_se(IN_SET(r, 0, -ENODATA));
88
89 r = sd_pidfd_get_user_unit(pidfd, &user_unit2);
90 log_info("sd_pidfd_get_user_unit(pidfd, …) → %s / \"%s\"", e(r), strnull(user_unit2));
91 assert_se(IN_SET(r, 0, -ENODATA));
92
93 r = sd_pidfd_get_slice(pidfd, &slice2);
94 log_info("sd_pidfd_get_slice(pidfd, …) → %s / \"%s\"", e(r), strnull(slice2));
95 assert_se(IN_SET(r, 0, -ENODATA));
96
97 r = sd_pidfd_get_owner_uid(pidfd, &u2);
98 log_info("sd_pidfd_get_owner_uid(pidfd, …) → %s / "UID_FMT, e(r), u2);
99 assert_se(IN_SET(r, 0, -ENODATA));
100
101 r = sd_pidfd_get_session(pidfd, &session2);
102 log_info("sd_pidfd_get_session(pidfd, …) → %s / \"%s\"", e(r), strnull(session2));
103
104 r = sd_pidfd_get_cgroup(pidfd, &cgroup2);
105 log_info("sd_pidfd_get_cgroup(pidfd, …) → %s / \"%s\"", e(r), strnull(cgroup2));
106 assert_se(IN_SET(r, 0, -ENOMEDIUM));
107 }
108
109 r = ASSERT_RETURN_IS_CRITICAL(uid_is_valid(u2), sd_uid_get_display(u2, &display_session));
110 log_info("sd_uid_get_display("UID_FMT", …) → %s / \"%s\"", u2, e(r), strnull(display_session));
111 if (u2 == UID_INVALID)
112 assert_se(r == -EINVAL);
113 else
114 assert_se(IN_SET(r, 0, -ENODATA));
115
116 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
117 sd_peer_get_session(pair[0], &pp);
118 sd_peer_get_session(pair[1], &qq);
119 assert_se(streq_ptr(pp, qq));
120
121 r = ASSERT_RETURN_IS_CRITICAL(uid_is_valid(u2), sd_uid_get_sessions(u2, false, &sessions));
122 assert_se(t = strv_join(sessions, " "));
123 log_info("sd_uid_get_sessions("UID_FMT", …) → %s \"%s\"", u2, e(r), t);
124 if (u2 == UID_INVALID)
125 assert_se(r == -EINVAL);
126 else {
127 assert_se(r >= 0);
128 assert_se(r == (int) strv_length(sessions));
129 }
130 sessions = strv_free(sessions);
131 free(t);
132
133 assert_se(r == ASSERT_RETURN_IS_CRITICAL(uid_is_valid(u2), sd_uid_get_sessions(u2, false, NULL)));
134
135 r = ASSERT_RETURN_IS_CRITICAL(uid_is_valid(u2), sd_uid_get_seats(u2, false, &seats));
136 assert_se(t = strv_join(seats, " "));
137 log_info("sd_uid_get_seats("UID_FMT", …) → %s \"%s\"", u2, e(r), t);
138 if (u2 == UID_INVALID)
139 assert_se(r == -EINVAL);
140 else {
141 assert_se(r >= 0);
142 assert_se(r == (int) strv_length(seats));
143 }
144 seats = strv_free(seats);
145 free(t);
146
147 assert_se(r == ASSERT_RETURN_IS_CRITICAL(uid_is_valid(u2), sd_uid_get_seats(u2, false, NULL)));
148
149 if (session) {
150 r = sd_session_is_active(session);
151 if (r == -ENXIO)
152 log_notice("sd_session_is_active() failed with ENXIO, it seems logind is not running.");
153 else {
154 /* All those tests will fail with ENXIO, so let's skip them. */
155
156 assert_se(r >= 0);
157 log_info("sd_session_is_active(\"%s\") → %s", session, yes_no(r));
158
159 r = sd_session_is_remote(session);
160 assert_se(r >= 0);
161 log_info("sd_session_is_remote(\"%s\") → %s", session, yes_no(r));
162
163 r = sd_session_get_state(session, &state);
164 assert_se(r == 0);
165 log_info("sd_session_get_state(\"%s\") → \"%s\"", session, state);
166
167 assert_se(sd_session_get_uid(session, &u) >= 0);
168 log_info("sd_session_get_uid(\"%s\") → "UID_FMT, session, u);
169 assert_se(u == u2);
170
171 assert_se(sd_session_get_type(session, &type) >= 0);
172 log_info("sd_session_get_type(\"%s\") → \"%s\"", session, type);
173
174 assert_se(sd_session_get_class(session, &class) >= 0);
175 log_info("sd_session_get_class(\"%s\") → \"%s\"", session, class);
176
177 r = sd_session_get_display(session, &display);
178 assert_se(IN_SET(r, 0, -ENODATA));
179 log_info("sd_session_get_display(\"%s\") → \"%s\"", session, strna(display));
180
181 r = sd_session_get_remote_user(session, &remote_user);
182 assert_se(IN_SET(r, 0, -ENODATA));
183 log_info("sd_session_get_remote_user(\"%s\") → \"%s\"",
184 session, strna(remote_user));
185
186 r = sd_session_get_remote_host(session, &remote_host);
187 assert_se(IN_SET(r, 0, -ENODATA));
188 log_info("sd_session_get_remote_host(\"%s\") → \"%s\"",
189 session, strna(remote_host));
190
191 r = sd_session_get_seat(session, &seat);
192 if (r >= 0) {
193 assert_se(seat);
194
195 log_info("sd_session_get_seat(\"%s\") → \"%s\"", session, seat);
196
197 #pragma GCC diagnostic push
198 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
199 r = sd_seat_can_multi_session(seat);
200 #pragma GCC diagnostic pop
201 assert_se(r == 1);
202 log_info("sd_session_can_multi_seat(\"%s\") → %s", seat, yes_no(r));
203
204 r = sd_seat_can_tty(seat);
205 assert_se(r >= 0);
206 log_info("sd_session_can_tty(\"%s\") → %s", seat, yes_no(r));
207
208 r = sd_seat_can_graphical(seat);
209 assert_se(r >= 0);
210 log_info("sd_session_can_graphical(\"%s\") → %s", seat, yes_no(r));
211 } else {
212 log_info_errno(r, "sd_session_get_seat(\"%s\"): %m", session);
213 assert_se(r == -ENODATA);
214 }
215
216 assert_se(sd_uid_get_state(u, &state2) == 0);
217 log_info("sd_uid_get_state("UID_FMT", …) → %s", u, state2);
218 }
219 }
220
221 if (seat) {
222 _cleanup_free_ char *session2 = NULL, *buf = NULL;
223 _cleanup_free_ uid_t *uids = NULL;
224 unsigned n;
225
226 assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
227
228 r = sd_seat_get_active(seat, &session2, &u2);
229 assert_se(r == 0);
230 log_info("sd_seat_get_active(\"%s\", …) → \"%s\", "UID_FMT, seat, session2, u2);
231
232 r = sd_uid_is_on_seat(u, 1, seat);
233 assert_se(IN_SET(r, 0, 1));
234 assert_se(!!r == streq(session, session2));
235
236 r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
237 assert_se(r >= 0);
238 assert_se(r == (int) strv_length(sessions));
239 assert_se(t = strv_join(sessions, " "));
240 strv_free(sessions);
241 log_info("sd_seat_get_sessions(\"%s\", …) → %s, \"%s\", [%u] {%s}",
242 seat, e(r), t, n, format_uids(&buf, uids, n));
243 free(t);
244
245 assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
246 }
247
248 r = sd_get_seats(&seats);
249 assert_se(r >= 0);
250 assert_se(r == (int) strv_length(seats));
251 assert_se(t = strv_join(seats, ", "));
252 strv_free(seats);
253 log_info("sd_get_seats(…) → [%i] \"%s\"", r, t);
254 t = mfree(t);
255
256 assert_se(sd_get_seats(NULL) == r);
257
258 r = sd_seat_get_active(NULL, &t, NULL);
259 assert_se(IN_SET(r, 0, -ENODATA, -ENXIO));
260 log_info("sd_seat_get_active(NULL, …) (active session on current seat) → %s / \"%s\"", e(r), strnull(t));
261 free(t);
262
263 r = sd_get_sessions(&sessions);
264 assert_se(r >= 0);
265 assert_se(r == (int) strv_length(sessions));
266 assert_se(t = strv_join(sessions, ", "));
267 strv_free(sessions);
268 log_info("sd_get_sessions(…) → [%i] \"%s\"", r, t);
269 free(t);
270
271 assert_se(sd_get_sessions(NULL) == r);
272
273 {
274 _cleanup_free_ uid_t *uids = NULL;
275 _cleanup_free_ char *buf = NULL;
276
277 r = sd_get_uids(&uids);
278 assert_se(r >= 0);
279 log_info("sd_get_uids(…) → [%i] {%s}", r, format_uids(&buf, uids, r));
280
281 assert_se(sd_get_uids(NULL) == r);
282 }
283
284 {
285 _cleanup_strv_free_ char **machines = NULL;
286 _cleanup_free_ char *buf = NULL;
287
288 r = sd_get_machine_names(&machines);
289 assert_se(r >= 0);
290 assert_se(r == (int) strv_length(machines));
291 assert_se(buf = strv_join(machines, " "));
292 log_info("sd_get_machines(…) → [%i] \"%s\"", r, buf);
293
294 assert_se(sd_get_machine_names(NULL) == r);
295 }
296 }
297
298 TEST(monitor) {
299 sd_login_monitor *m = NULL;
300 int r;
301
302 if (!streq_ptr(saved_argv[1], "-m"))
303 return;
304
305 assert_se(sd_login_monitor_new("session", &m) == 0);
306
307 for (unsigned n = 0; n < 5; n++) {
308 struct pollfd pollfd = {};
309 usec_t timeout, nw;
310
311 assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0);
312 assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0);
313
314 assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0);
315
316 nw = now(CLOCK_MONOTONIC);
317
318 r = poll(&pollfd, 1,
319 timeout == UINT64_MAX ? -1 :
320 timeout > nw ? (int) ((timeout - nw) / 1000) :
321 0);
322
323 assert_se(r >= 0);
324
325 sd_login_monitor_flush(m);
326 printf("Wake!\n");
327 }
328
329 sd_login_monitor_unref(m);
330 }
331
332 static int intro(void) {
333 if (cg_is_available() <= 0)
334 return log_tests_skipped("cgroupfs v2 is not mounted");
335
336 log_info("/* Information printed is from the live system */");
337 return EXIT_SUCCESS;
338 }
339
340 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);