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