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