1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "systemd/sd-login.h"
29 #include "formats-util.h"
31 static void test_login(void) {
32 _cleanup_close_pair_ int pair[2] = { -1, -1 };
33 _cleanup_free_ char *pp = NULL, *qq = NULL;
36 char *seat, *type, *class, *display, *remote_user, *remote_host;
41 char **seats, **sessions, **machines;
45 sd_login_monitor *m = NULL;
47 assert_se(sd_pid_get_session(0, &session) == 0);
48 printf("session = %s\n", session);
50 assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
51 printf("user = "UID_FMT"\n", u2);
53 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
54 sd_peer_get_session(pair[0], &pp);
55 sd_peer_get_session(pair[1], &qq);
56 assert_se(streq_ptr(pp, qq));
58 r = sd_uid_get_sessions(u2, false, &sessions);
60 assert_se(r == (int) strv_length(sessions));
61 assert_se(t = strv_join(sessions, ", "));
63 printf("sessions = %s\n", t);
66 assert_se(r == sd_uid_get_sessions(u2, false, NULL));
68 r = sd_uid_get_seats(u2, false, &seats);
70 assert_se(r == (int) strv_length(seats));
71 assert_se(t = strv_join(seats, ", "));
73 printf("seats = %s\n", t);
76 assert_se(r == sd_uid_get_seats(u2, false, NULL));
78 r = sd_session_is_active(session);
80 printf("active = %s\n", yes_no(r));
82 r = sd_session_is_remote(session);
84 printf("remote = %s\n", yes_no(r));
86 r = sd_session_get_state(session, &state);
88 printf("state = %s\n", state);
91 assert_se(sd_session_get_uid(session, &u) >= 0);
92 printf("uid = "UID_FMT"\n", u);
95 assert_se(sd_session_get_type(session, &type) >= 0);
96 printf("type = %s\n", type);
99 assert_se(sd_session_get_class(session, &class) >= 0);
100 printf("class = %s\n", class);
103 assert_se(sd_session_get_display(session, &display) >= 0);
104 printf("display = %s\n", display);
107 assert_se(sd_session_get_remote_user(session, &remote_user) >= 0);
108 printf("remote_user = %s\n", remote_user);
111 assert_se(sd_session_get_remote_host(session, &remote_host) >= 0);
112 printf("remote_host = %s\n", remote_host);
115 assert_se(sd_session_get_seat(session, &seat) >= 0);
116 printf("seat = %s\n", seat);
118 r = sd_seat_can_multi_session(seat);
120 printf("can do multi session = %s\n", yes_no(r));
122 r = sd_seat_can_tty(seat);
124 printf("can do tty = %s\n", yes_no(r));
126 r = sd_seat_can_graphical(seat);
128 printf("can do graphical = %s\n", yes_no(r));
130 assert_se(sd_uid_get_state(u, &state) >= 0);
131 printf("state = %s\n", state);
133 assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
135 k = sd_uid_is_on_seat(u, 1, seat);
137 assert_se(!!r == !!r);
139 assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0);
140 printf("session2 = %s\n", session2);
141 printf("uid2 = "UID_FMT"\n", u2);
143 r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
145 printf("n_sessions = %i\n", r);
146 assert_se(r == (int) strv_length(sessions));
147 assert_se(t = strv_join(sessions, ", "));
149 printf("sessions = %s\n", t);
152 for (k = 0; k < (int) n; k++)
153 printf(" "UID_FMT, uids[k]);
157 assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
164 r = sd_get_seats(&seats);
166 assert_se(r == (int) strv_length(seats));
167 assert_se(t = strv_join(seats, ", "));
169 printf("n_seats = %i\n", r);
170 printf("seats = %s\n", t);
173 assert_se(sd_get_seats(NULL) == r);
175 r = sd_seat_get_active(NULL, &t, NULL);
177 printf("active session on current seat = %s\n", t);
180 r = sd_get_sessions(&sessions);
182 assert_se(r == (int) strv_length(sessions));
183 assert_se(t = strv_join(sessions, ", "));
185 printf("n_sessions = %i\n", r);
186 printf("sessions = %s\n", t);
189 assert_se(sd_get_sessions(NULL) == r);
191 r = sd_get_uids(&uids);
195 for (k = 0; k < r; k++)
196 printf(" "UID_FMT, uids[k]);
200 printf("n_uids = %i\n", r);
201 assert_se(sd_get_uids(NULL) == r);
203 r = sd_get_machine_names(&machines);
205 assert_se(r == (int) strv_length(machines));
206 assert_se(t = strv_join(machines, ", "));
208 printf("n_machines = %i\n", r);
209 printf("machines = %s\n", t);
212 r = sd_login_monitor_new("session", &m);
215 for (n = 0; n < 5; n++) {
219 assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0);
220 assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0);
222 assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0);
224 nw = now(CLOCK_MONOTONIC);
227 timeout == (uint64_t) -1 ? -1 :
228 timeout > nw ? (int) ((timeout - nw) / 1000) :
233 sd_login_monitor_flush(m);
237 sd_login_monitor_unref(m);
240 int main(int argc, char* argv[]) {
241 log_parse_environment();