chiark / gitweb /
Prep v235: Update test-login.c
[elogind.git] / src / libelogind / sd-login / test-login.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2011 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <poll.h>
21 #include <string.h>
22
23 #include "sd-login.h"
24
25 #include "alloc-util.h"
26 #include "fd-util.h"
27 #include "format-util.h"
28 #include "log.h"
29 #include "string-util.h"
30 #include "strv.h"
31 #include "util.h"
32
33 /// Additional includes needed by elogind
34 #include "musl_missing.h"
35 static char* format_uids(char **buf, uid_t* uids, int count) {
36         int pos = 0, k, inc;
37         size_t size = (DECIMAL_STR_MAX(uid_t) + 1) * count + 1;
38
39         assert_se(*buf = malloc(size));
40
41         for (k = 0; k < count; k++) {
42                 sprintf(*buf + pos, "%s"UID_FMT"%n", k > 0 ? " " : "", uids[k], &inc);
43                 pos += inc;
44         }
45
46         assert_se(pos < (ssize_t)size);
47         (*buf)[pos] = '\0';
48
49         return *buf;
50 }
51
52 static void test_login(void) {
53         _cleanup_close_pair_ int pair[2] = { -1, -1 };
54         _cleanup_free_ char *pp = NULL, *qq = NULL,
55                 *display_session = NULL, *cgroup = NULL,
56                 *display = NULL, *remote_user = NULL, *remote_host = NULL,
57                 *type = NULL, *class = NULL, *state = NULL, *state2 = NULL,
58                 *seat = NULL, *session = NULL,
59                 *unit = NULL, *user_unit = NULL, *slice = NULL;
60         int r;
61         uid_t u, u2;
62         char *t, **seats, **sessions;
63
64 #if 0 /// elogind does not support systemd slices
65         r = sd_pid_get_unit(0, &unit);
66         assert_se(r >= 0 || r == -ENODATA);
67         log_info("sd_pid_get_unit(0, …) → \"%s\"", strna(unit));
68
69         r = sd_pid_get_user_unit(0, &user_unit);
70         assert_se(r >= 0 || r == -ENODATA);
71         log_info("sd_pid_get_user_unit(0, …) → \"%s\"", strna(user_unit));
72
73         r = sd_pid_get_slice(0, &slice);
74         assert_se(r >= 0 || r == -ENODATA);
75         log_info("sd_pid_get_slice(0, …) → \"%s\"", strna(slice));
76 #endif // 0
77
78         r = sd_pid_get_session(0, &session);
79         if (r < 0) {
80                 log_warning_errno(r, "sd_pid_get_session(0, …): %m");
81                 if (r == -ENODATA)
82                         log_info("Seems we are not running in a session, skipping some tests.");
83         } else {
84                 log_info("sd_pid_get_session(0, …) → \"%s\"", session);
85
86                 assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
87                 log_info("sd_pid_get_owner_uid(0, …) → "UID_FMT, u2);
88
89                 assert_se(sd_pid_get_cgroup(0, &cgroup) == 0);
90                 log_info("sd_pid_get_cgroup(0, …) → \"%s\"", cgroup);
91
92                 r = sd_uid_get_display(u2, &display_session);
93                 assert_se(r >= 0 || r == -ENODATA);
94                 log_info("sd_uid_get_display("UID_FMT", …) → \"%s\"",
95                          u2, strnull(display_session));
96
97                 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
98                 sd_peer_get_session(pair[0], &pp);
99                 sd_peer_get_session(pair[1], &qq);
100                 assert_se(streq_ptr(pp, qq));
101
102                 r = sd_uid_get_sessions(u2, false, &sessions);
103                 assert_se(r >= 0);
104                 assert_se(r == (int) strv_length(sessions));
105                 assert_se(t = strv_join(sessions, " "));
106                 strv_free(sessions);
107                 log_info("sd_uid_get_sessions("UID_FMT", …) → [%i] \"%s\"", u2, r, t);
108                 free(t);
109
110                 assert_se(r == sd_uid_get_sessions(u2, false, NULL));
111
112                 r = sd_uid_get_seats(u2, false, &seats);
113                 assert_se(r >= 0);
114                 assert_se(r == (int) strv_length(seats));
115                 assert_se(t = strv_join(seats, " "));
116                 strv_free(seats);
117                 log_info("sd_uid_get_seats("UID_FMT", …) → [%i] \"%s\"", u2, r, t);
118                 free(t);
119
120                 assert_se(r == sd_uid_get_seats(u2, false, NULL));
121         }
122
123         if (session) {
124                 r = sd_session_is_active(session);
125                 assert_se(r >= 0);
126                 log_info("sd_session_is_active(\"%s\") → %s", session, yes_no(r));
127
128                 r = sd_session_is_remote(session);
129                 assert_se(r >= 0);
130                 log_info("sd_session_is_remote(\"%s\") → %s", session, yes_no(r));
131
132                 r = sd_session_get_state(session, &state);
133                 assert_se(r >= 0);
134                 log_info("sd_session_get_state(\"%s\") → \"%s\"", session, state);
135
136                 assert_se(sd_session_get_uid(session, &u) >= 0);
137                 log_info("sd_session_get_uid(\"%s\") → "UID_FMT, session, u);
138                 assert_se(u == u2);
139
140                 assert_se(sd_session_get_type(session, &type) >= 0);
141                 log_info("sd_session_get_type(\"%s\") → \"%s\"", session, type);
142
143                 assert_se(sd_session_get_class(session, &class) >= 0);
144                 log_info("sd_session_get_class(\"%s\") → \"%s\"", session, class);
145
146                 r = sd_session_get_display(session, &display);
147                 assert_se(r >= 0 || r == -ENODATA);
148                 log_info("sd_session_get_display(\"%s\") → \"%s\"", session, strna(display));
149
150                 r = sd_session_get_remote_user(session, &remote_user);
151                 assert_se(r >= 0 || r == -ENODATA);
152                 log_info("sd_session_get_remote_user(\"%s\") → \"%s\"",
153                          session, strna(remote_user));
154
155                 r = sd_session_get_remote_host(session, &remote_host);
156                 assert_se(r >= 0 || r == -ENODATA);
157                 log_info("sd_session_get_remote_host(\"%s\") → \"%s\"",
158                          session, strna(remote_host));
159
160                 r = sd_session_get_seat(session, &seat);
161                 if (r >= 0) {
162                         assert_se(seat);
163
164                         log_info("sd_session_get_seat(\"%s\") → \"%s\"", session, seat);
165
166                         r = sd_seat_can_multi_session(seat);
167                         assert_se(r >= 0);
168                         log_info("sd_session_can_multi_seat(\"%s\") → %s", seat, yes_no(r));
169
170                         r = sd_seat_can_tty(seat);
171                         assert_se(r >= 0);
172                         log_info("sd_session_can_tty(\"%s\") → %s", seat, yes_no(r));
173
174                         r = sd_seat_can_graphical(seat);
175                         assert_se(r >= 0);
176                         log_info("sd_session_can_graphical(\"%s\") → %s", seat, yes_no(r));
177                 } else {
178                         log_info_errno(r, "sd_session_get_seat(\"%s\"): %m", session);
179                         assert_se(r == -ENODATA);
180                 }
181
182                 assert_se(sd_uid_get_state(u, &state2) >= 0);
183                 log_info("sd_uid_get_state("UID_FMT", …) → %s", u, state2);
184         }
185
186         if (seat) {
187                 _cleanup_free_ char *session2 = NULL, *buf = NULL;
188                 _cleanup_free_ uid_t *uids = NULL;
189                 unsigned n;
190
191                 assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
192
193                 r = sd_seat_get_active(seat, &session2, &u2);
194                 assert_se(r >= 0);
195                 log_info("sd_seat_get_active(\"%s\", …) → \"%s\", "UID_FMT, seat, session2, u2);
196
197                 r = sd_uid_is_on_seat(u, 1, seat);
198                 assert_se(r >= 0);
199                 assert_se(!!r == streq(session, session2));
200
201                 r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
202                 assert_se(r >= 0);
203                 assert_se(r == (int) strv_length(sessions));
204                 assert_se(t = strv_join(sessions, " "));
205                 strv_free(sessions);
206                 log_info("sd_seat_get_sessions(\"%s\", …) → %i, \"%s\", [%i] {%s}",
207                          seat, r, t, n, format_uids(&buf, uids, n));
208                 free(t);
209
210                 assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
211         }
212
213         r = sd_get_seats(&seats);
214         assert_se(r >= 0);
215         assert_se(r == (int) strv_length(seats));
216         assert_se(t = strv_join(seats, ", "));
217         strv_free(seats);
218         log_info("sd_get_seats(…) → [%i] \"%s\"", r, t);
219         t = mfree(t);
220
221         assert_se(sd_get_seats(NULL) == r);
222
223         r = sd_seat_get_active(NULL, &t, NULL);
224         assert_se(IN_SET(r, 0, -ENODATA));
225         log_info("sd_seat_get_active(NULL, …) (active session on current seat) → %s", strnull(t));
226         free(t);
227
228         r = sd_get_sessions(&sessions);
229         assert_se(r >= 0);
230         assert_se(r == (int) strv_length(sessions));
231         assert_se(t = strv_join(sessions, ", "));
232         strv_free(sessions);
233         log_info("sd_get_sessions(…) → [%i] \"%s\"", r, t);
234         free(t);
235
236         assert_se(sd_get_sessions(NULL) == r);
237
238         {
239                 _cleanup_free_ uid_t *uids = NULL;
240                 _cleanup_free_ char *buf = NULL;
241
242                 r = sd_get_uids(&uids);
243                 assert_se(r >= 0);
244                 log_info("sd_get_uids(…) → [%i] {%s}", r, format_uids(&buf, uids, r));
245
246                 assert_se(sd_get_uids(NULL) == r);
247         }
248
249         {
250                 _cleanup_strv_free_ char **machines = NULL;
251                 _cleanup_free_ char *buf = NULL;
252
253                 r = sd_get_machine_names(&machines);
254                 assert_se(r >= 0);
255                 assert_se(r == (int) strv_length(machines));
256                 assert_se(buf = strv_join(machines, " "));
257                 log_info("sd_get_machines(…) → [%i] \"%s\"", r, buf);
258
259                 assert_se(sd_get_machine_names(NULL) == r);
260         }
261 }
262
263 static void test_monitor(void) {
264         sd_login_monitor *m = NULL;
265         unsigned n;
266         int r;
267
268         r = sd_login_monitor_new("session", &m);
269         assert_se(r >= 0);
270
271         for (n = 0; n < 5; n++) {
272                 struct pollfd pollfd = {};
273                 usec_t timeout, nw;
274
275                 assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0);
276                 assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0);
277
278                 assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0);
279
280                 nw = now(CLOCK_MONOTONIC);
281
282                 r = poll(&pollfd, 1,
283                          timeout == (uint64_t) -1 ? -1 :
284                          timeout > nw ? (int) ((timeout - nw) / 1000) :
285                          0);
286
287                 assert_se(r >= 0);
288
289                 sd_login_monitor_flush(m);
290                 printf("Wake!\n");
291         }
292
293         sd_login_monitor_unref(m);
294 }
295
296 int main(int argc, char* argv[]) {
297         log_parse_environment();
298         log_open();
299
300         log_info("/* Information printed is from the live system */");
301
302         test_login();
303
304         if (streq_ptr(argv[1], "-m"))
305                 test_monitor();
306
307         return 0;
308 }
309