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