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