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