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