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