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