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