2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
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.
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.
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/>.
25 #include "alloc-util.h"
27 #include "formats-util.h"
28 #include "string-util.h"
32 /// Additional includes needed by elogind
33 #include "musl_missing.h"
35 static void test_login(void) {
36 _cleanup_close_pair_ int pair[2] = { -1, -1 };
37 _cleanup_free_ char *pp = NULL, *qq = NULL;
40 char *seat, *type, *class, *display, *remote_user, *remote_host, *display_session, *cgroup;
45 char **seats, **sessions, **machines;
49 sd_login_monitor *m = NULL;
51 assert_se(sd_pid_get_session(0, &session) == 0);
52 printf("session = %s\n", session);
54 assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
55 printf("user = "UID_FMT"\n", u2);
57 assert_se(sd_pid_get_cgroup(0, &cgroup) == 0);
58 printf("cgroup = %s\n", cgroup);
61 display_session = NULL;
62 r = sd_uid_get_display(u2, &display_session);
63 assert_se(r >= 0 || r == -ENODATA);
64 printf("user's display session = %s\n", strna(display_session));
65 free(display_session);
67 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
68 sd_peer_get_session(pair[0], &pp);
69 sd_peer_get_session(pair[1], &qq);
70 assert_se(streq_ptr(pp, qq));
72 r = sd_uid_get_sessions(u2, false, &sessions);
74 assert_se(r == (int) strv_length(sessions));
75 assert_se(t = strv_join(sessions, ", "));
77 printf("sessions = %s\n", t);
80 assert_se(r == sd_uid_get_sessions(u2, false, NULL));
82 r = sd_uid_get_seats(u2, false, &seats);
84 assert_se(r == (int) strv_length(seats));
85 assert_se(t = strv_join(seats, ", "));
87 printf("seats = %s\n", t);
90 assert_se(r == sd_uid_get_seats(u2, false, NULL));
92 r = sd_session_is_active(session);
94 printf("active = %s\n", yes_no(r));
96 r = sd_session_is_remote(session);
98 printf("remote = %s\n", yes_no(r));
100 r = sd_session_get_state(session, &state);
102 printf("state = %s\n", state);
105 assert_se(sd_session_get_uid(session, &u) >= 0);
106 printf("uid = "UID_FMT"\n", u);
109 assert_se(sd_session_get_type(session, &type) >= 0);
110 printf("type = %s\n", type);
113 assert_se(sd_session_get_class(session, &class) >= 0);
114 printf("class = %s\n", class);
118 r = sd_session_get_display(session, &display);
119 assert_se(r >= 0 || r == -ENODATA);
120 printf("display = %s\n", strna(display));
124 r = sd_session_get_remote_user(session, &remote_user);
125 assert_se(r >= 0 || r == -ENODATA);
126 printf("remote_user = %s\n", strna(remote_user));
130 r = sd_session_get_remote_host(session, &remote_host);
131 assert_se(r >= 0 || r == -ENODATA);
132 printf("remote_host = %s\n", strna(remote_host));
135 assert_se(sd_session_get_seat(session, &seat) >= 0);
136 printf("seat = %s\n", seat);
138 r = sd_seat_can_multi_session(seat);
140 printf("can do multi session = %s\n", yes_no(r));
142 r = sd_seat_can_tty(seat);
144 printf("can do tty = %s\n", yes_no(r));
146 r = sd_seat_can_graphical(seat);
148 printf("can do graphical = %s\n", yes_no(r));
150 assert_se(sd_uid_get_state(u, &state) >= 0);
151 printf("state = %s\n", state);
153 assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
155 k = sd_uid_is_on_seat(u, 1, seat);
157 assert_se(!!r == !!r);
159 assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0);
160 printf("session2 = %s\n", session2);
161 printf("uid2 = "UID_FMT"\n", u2);
163 r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
165 printf("n_sessions = %i\n", r);
166 assert_se(r == (int) strv_length(sessions));
167 assert_se(t = strv_join(sessions, ", "));
169 printf("sessions = %s\n", t);
172 for (k = 0; k < (int) n; k++)
173 printf(" "UID_FMT, uids[k]);
177 assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
184 r = sd_get_seats(&seats);
186 assert_se(r == (int) strv_length(seats));
187 assert_se(t = strv_join(seats, ", "));
189 printf("n_seats = %i\n", r);
190 printf("seats = %s\n", t);
193 assert_se(sd_get_seats(NULL) == r);
195 r = sd_seat_get_active(NULL, &t, NULL);
197 printf("active session on current seat = %s\n", t);
200 r = sd_get_sessions(&sessions);
202 assert_se(r == (int) strv_length(sessions));
203 assert_se(t = strv_join(sessions, ", "));
205 printf("n_sessions = %i\n", r);
206 printf("sessions = %s\n", t);
209 assert_se(sd_get_sessions(NULL) == r);
211 r = sd_get_uids(&uids);
215 for (k = 0; k < r; k++)
216 printf(" "UID_FMT, uids[k]);
220 printf("n_uids = %i\n", r);
221 assert_se(sd_get_uids(NULL) == r);
223 r = sd_get_machine_names(&machines);
225 assert_se(r == (int) strv_length(machines));
226 assert_se(t = strv_join(machines, ", "));
228 printf("n_machines = %i\n", r);
229 printf("machines = %s\n", t);
232 r = sd_login_monitor_new("session", &m);
235 for (n = 0; n < 5; n++) {
239 assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0);
240 assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0);
242 assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0);
244 nw = now(CLOCK_MONOTONIC);
247 timeout == (uint64_t) -1 ? -1 :
248 timeout > nw ? (int) ((timeout - nw) / 1000) :
253 sd_login_monitor_flush(m);
257 sd_login_monitor_unref(m);
260 int main(int argc, char* argv[]) {
261 elogind_set_program_name(argv[0]);
262 log_parse_environment();