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