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