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