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