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