]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-login/test-login.c
networkd: handle ignoring ll gateway being link ll
[thirdparty/systemd.git] / src / libsystemd / sd-login / test-login.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
74b91131 2
0a6f50c0 3#include <poll.h>
034a2a52 4
07630cea 5#include "sd-login.h"
81527be1 6
b5efdb8a 7#include "alloc-util.h"
1b5b507c 8#include "errno-list.h"
3ffd4af2 9#include "fd-util.h"
f97b34a6 10#include "format-util.h"
ce737f46 11#include "log.h"
07630cea
LP
12#include "string-util.h"
13#include "strv.h"
ca78ad1d 14#include "time-util.h"
1b5b507c 15#include "user-util.h"
74b91131 16
ce737f46 17static char* format_uids(char **buf, uid_t* uids, int count) {
1b5b507c 18 int pos = 0, inc;
ce737f46
ZJS
19 size_t size = (DECIMAL_STR_MAX(uid_t) + 1) * count + 1;
20
21 assert_se(*buf = malloc(size));
22
1b5b507c 23 for (int k = 0; k < count; k++) {
ce737f46
ZJS
24 sprintf(*buf + pos, "%s"UID_FMT"%n", k > 0 ? " " : "", uids[k], &inc);
25 pos += inc;
26 }
27
0bf7d7cc 28 assert_se(pos < (ssize_t)size);
ce737f46
ZJS
29 (*buf)[pos] = '\0';
30
31 return *buf;
32}
33
1b5b507c
ZJS
34static const char *e(int r) {
35 return r == 0 ? "OK" : errno_to_name(r);
36}
37
e9e506ed 38static void test_login(void) {
3d94f76c 39 _cleanup_close_pair_ int pair[2] = { -1, -1 };
ce737f46
ZJS
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;
e6f44233 46 int r;
1b5b507c
ZJS
47 uid_t u, u2 = UID_INVALID;
48 char *t, **seats = NULL, **sessions = NULL;
74b91131 49
171f8f59 50 r = sd_pid_get_unit(0, &unit);
1b5b507c
ZJS
51 log_info("sd_pid_get_unit(0, …) → %s / \"%s\"", e(r), strnull(unit));
52 assert_se(IN_SET(r, 0, -ENODATA));
74b91131 53
ce737f46 54 r = sd_pid_get_user_unit(0, &user_unit);
1b5b507c
ZJS
55 log_info("sd_pid_get_user_unit(0, …) → %s / \"%s\"", e(r), strnull(user_unit));
56 assert_se(IN_SET(r, 0, -ENODATA));
034a2a52 57
bc9e9af1 58 r = sd_pid_get_slice(0, &slice);
1b5b507c
ZJS
59 log_info("sd_pid_get_slice(0, …) → %s / \"%s\"", e(r), strnull(slice));
60 assert_se(IN_SET(r, 0, -ENODATA));
61
62 r = sd_pid_get_owner_uid(0, &u2);
63 log_info("sd_pid_get_owner_uid(0, …) → %s / "UID_FMT, e(r), u2);
64 assert_se(IN_SET(r, 0, -ENODATA));
f5aaf575 65
ce737f46 66 r = sd_pid_get_session(0, &session);
1b5b507c
ZJS
67 log_info("sd_pid_get_session(0, …) → %s / \"%s\"", e(r), strnull(session));
68
69 r = sd_pid_get_cgroup(0, &cgroup);
70 log_info("sd_pid_get_cgroup(0, …) → %s / \"%s\"", e(r), strnull(cgroup));
352ab9d7 71 assert_se(IN_SET(r, 0, -ENOMEDIUM));
1b5b507c
ZJS
72
73 r = sd_uid_get_display(u2, &display_session);
74 log_info("sd_uid_get_display("UID_FMT", …) → %s / \"%s\"", u2, e(r), strnull(display_session));
75 if (u2 == UID_INVALID)
76 assert_se(r == -EINVAL);
77 else
78 assert_se(IN_SET(r, 0, -ENODATA));
79
80 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
81 sd_peer_get_session(pair[0], &pp);
82 sd_peer_get_session(pair[1], &qq);
83 assert_se(streq_ptr(pp, qq));
84
85 r = sd_uid_get_sessions(u2, false, &sessions);
86 assert_se(t = strv_join(sessions, " "));
87 log_info("sd_uid_get_sessions("UID_FMT", …) → %s \"%s\"", u2, e(r), t);
88 if (u2 == UID_INVALID)
89 assert_se(r == -EINVAL);
90 else {
ce737f46
ZJS
91 assert_se(r >= 0);
92 assert_se(r == (int) strv_length(sessions));
1b5b507c
ZJS
93 }
94 sessions = strv_free(sessions);
95 free(t);
d60ef526 96
1b5b507c 97 assert_se(r == sd_uid_get_sessions(u2, false, NULL));
74b91131 98
1b5b507c
ZJS
99 r = sd_uid_get_seats(u2, false, &seats);
100 assert_se(t = strv_join(seats, " "));
101 log_info("sd_uid_get_seats("UID_FMT", …) → %s \"%s\"", u2, e(r), t);
102 if (u2 == UID_INVALID)
103 assert_se(r == -EINVAL);
104 else {
ce737f46
ZJS
105 assert_se(r >= 0);
106 assert_se(r == (int) strv_length(seats));
ce737f46 107 }
1b5b507c
ZJS
108 seats = strv_free(seats);
109 free(t);
110
111 assert_se(r == sd_uid_get_seats(u2, false, NULL));
0604381b 112
ce737f46
ZJS
113 if (session) {
114 r = sd_session_is_active(session);
115 assert_se(r >= 0);
116 log_info("sd_session_is_active(\"%s\") → %s", session, yes_no(r));
74b91131 117
ce737f46
ZJS
118 r = sd_session_is_remote(session);
119 assert_se(r >= 0);
120 log_info("sd_session_is_remote(\"%s\") → %s", session, yes_no(r));
51f58f08 121
ce737f46 122 r = sd_session_get_state(session, &state);
1b5b507c 123 assert_se(r == 0);
ce737f46 124 log_info("sd_session_get_state(\"%s\") → \"%s\"", session, state);
51f58f08 125
ce737f46
ZJS
126 assert_se(sd_session_get_uid(session, &u) >= 0);
127 log_info("sd_session_get_uid(\"%s\") → "UID_FMT, session, u);
128 assert_se(u == u2);
fc8af9ff 129
ce737f46
ZJS
130 assert_se(sd_session_get_type(session, &type) >= 0);
131 log_info("sd_session_get_type(\"%s\") → \"%s\"", session, type);
5b04fe60 132
ce737f46
ZJS
133 assert_se(sd_session_get_class(session, &class) >= 0);
134 log_info("sd_session_get_class(\"%s\") → \"%s\"", session, class);
5b04fe60 135
ce737f46 136 r = sd_session_get_display(session, &display);
1b5b507c 137 assert_se(IN_SET(r, 0, -ENODATA));
ce737f46 138 log_info("sd_session_get_display(\"%s\") → \"%s\"", session, strna(display));
74b91131 139
ce737f46 140 r = sd_session_get_remote_user(session, &remote_user);
1b5b507c 141 assert_se(IN_SET(r, 0, -ENODATA));
ce737f46
ZJS
142 log_info("sd_session_get_remote_user(\"%s\") → \"%s\"",
143 session, strna(remote_user));
add30678 144
ce737f46 145 r = sd_session_get_remote_host(session, &remote_host);
1b5b507c 146 assert_se(IN_SET(r, 0, -ENODATA));
ce737f46
ZJS
147 log_info("sd_session_get_remote_host(\"%s\") → \"%s\"",
148 session, strna(remote_host));
20747498 149
ce737f46
ZJS
150 r = sd_session_get_seat(session, &seat);
151 if (r >= 0) {
152 assert_se(seat);
20747498 153
5947643c 154 log_info("sd_session_get_seat(\"%s\") → \"%s\"", session, seat);
74b91131 155
8f8cc84b
ZJS
156#pragma GCC diagnostic push
157#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
ce737f46 158 r = sd_seat_can_multi_session(seat);
8f8cc84b
ZJS
159#pragma GCC diagnostic pop
160 assert_se(r == 1);
ce737f46 161 log_info("sd_session_can_multi_seat(\"%s\") → %s", seat, yes_no(r));
74b91131 162
ce737f46
ZJS
163 r = sd_seat_can_tty(seat);
164 assert_se(r >= 0);
165 log_info("sd_session_can_tty(\"%s\") → %s", seat, yes_no(r));
74b91131 166
ce737f46
ZJS
167 r = sd_seat_can_graphical(seat);
168 assert_se(r >= 0);
169 log_info("sd_session_can_graphical(\"%s\") → %s", seat, yes_no(r));
170 } else {
5947643c 171 log_info_errno(r, "sd_session_get_seat(\"%s\"): %m", session);
ce737f46
ZJS
172 assert_se(r == -ENODATA);
173 }
74b91131 174
1b5b507c 175 assert_se(sd_uid_get_state(u, &state2) == 0);
ce737f46
ZJS
176 log_info("sd_uid_get_state("UID_FMT", …) → %s", u, state2);
177 }
178
179 if (seat) {
180 _cleanup_free_ char *session2 = NULL, *buf = NULL;
181 _cleanup_free_ uid_t *uids = NULL;
182 unsigned n;
183
184 assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
034a2a52 185
e6f44233 186 r = sd_seat_get_active(seat, &session2, &u2);
1b5b507c 187 assert_se(r == 0);
ce737f46
ZJS
188 log_info("sd_seat_get_active(\"%s\", …) → \"%s\", "UID_FMT, seat, session2, u2);
189
e6f44233 190 r = sd_uid_is_on_seat(u, 1, seat);
1b5b507c 191 assert_se(IN_SET(r, 0, 1));
e6f44233
AJ
192 assert_se(!!r == streq(session, session2));
193
ce737f46
ZJS
194 r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
195 assert_se(r >= 0);
196 assert_se(r == (int) strv_length(sessions));
197 assert_se(t = strv_join(sessions, " "));
198 strv_free(sessions);
1b5b507c
ZJS
199 log_info("sd_seat_get_sessions(\"%s\", …) → %s, \"%s\", [%u] {%s}",
200 seat, e(r), t, n, format_uids(&buf, uids, n));
ce737f46
ZJS
201 free(t);
202
203 assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
204 }
74b91131 205
d60ef526
LP
206 r = sd_get_seats(&seats);
207 assert_se(r >= 0);
208 assert_se(r == (int) strv_length(seats));
034a2a52
LP
209 assert_se(t = strv_join(seats, ", "));
210 strv_free(seats);
ce737f46
ZJS
211 log_info("sd_get_seats(…) → [%i] \"%s\"", r, t);
212 t = mfree(t);
034a2a52 213
51f58f08
LP
214 assert_se(sd_get_seats(NULL) == r);
215
50b1678a 216 r = sd_seat_get_active(NULL, &t, NULL);
ce737f46 217 assert_se(IN_SET(r, 0, -ENODATA));
1b5b507c 218 log_info("sd_seat_get_active(NULL, …) (active session on current seat) → %s / \"%s\"", e(r), strnull(t));
50b1678a
LP
219 free(t);
220
d60ef526
LP
221 r = sd_get_sessions(&sessions);
222 assert_se(r >= 0);
223 assert_se(r == (int) strv_length(sessions));
034a2a52
LP
224 assert_se(t = strv_join(sessions, ", "));
225 strv_free(sessions);
ce737f46 226 log_info("sd_get_sessions(…) → [%i] \"%s\"", r, t);
034a2a52
LP
227 free(t);
228
d60ef526
LP
229 assert_se(sd_get_sessions(NULL) == r);
230
ce737f46
ZJS
231 {
232 _cleanup_free_ uid_t *uids = NULL;
233 _cleanup_free_ char *buf = NULL;
234
235 r = sd_get_uids(&uids);
236 assert_se(r >= 0);
237 log_info("sd_get_uids(…) → [%i] {%s}", r, format_uids(&buf, uids, r));
034a2a52 238
ce737f46
ZJS
239 assert_se(sd_get_uids(NULL) == r);
240 }
034a2a52 241
ce737f46
ZJS
242 {
243 _cleanup_strv_free_ char **machines = NULL;
244 _cleanup_free_ char *buf = NULL;
d60ef526 245
ce737f46
ZJS
246 r = sd_get_machine_names(&machines);
247 assert_se(r >= 0);
248 assert_se(r == (int) strv_length(machines));
249 assert_se(buf = strv_join(machines, " "));
ce737f46 250 log_info("sd_get_machines(…) → [%i] \"%s\"", r, buf);
76ed21e1
YW
251
252 assert_se(sd_get_machine_names(NULL) == r);
ce737f46
ZJS
253 }
254}
255
256static void test_monitor(void) {
257 sd_login_monitor *m = NULL;
ce737f46 258 int r;
034a2a52 259
1b5b507c 260 assert_se(sd_login_monitor_new("session", &m) == 0);
034a2a52 261
1b5b507c 262 for (unsigned n = 0; n < 5; n++) {
ce737f46 263 struct pollfd pollfd = {};
667c24a6
LP
264 usec_t timeout, nw;
265
667c24a6
LP
266 assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0);
267 assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0);
268
269 assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0);
270
271 nw = now(CLOCK_MONOTONIC);
272
273 r = poll(&pollfd, 1,
274 timeout == (uint64_t) -1 ? -1 :
275 timeout > nw ? (int) ((timeout - nw) / 1000) :
276 0);
277
b9b2b042 278 assert_se(r >= 0);
034a2a52 279
b9b2b042
LP
280 sd_login_monitor_flush(m);
281 printf("Wake!\n");
282 }
034a2a52 283
b9b2b042 284 sd_login_monitor_unref(m);
e9e506ed
ZJS
285}
286
287int main(int argc, char* argv[]) {
288 log_parse_environment();
289 log_open();
290
ce737f46
ZJS
291 log_info("/* Information printed is from the live system */");
292
e9e506ed 293 test_login();
034a2a52 294
ce737f46
ZJS
295 if (streq_ptr(argv[1], "-m"))
296 test_monitor();
297
74b91131
LP
298 return 0;
299}