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"
30 static void test_login(void) {
31 _cleanup_close_pair_ int pair[2] = { -1, -1 };
32 _cleanup_free_ char *pp = NULL, *qq = NULL;
35 char *seat, *type, *class, *display, *remote_user, *remote_host;
40 char **seats, **sessions, **machines;
44 sd_login_monitor *m = NULL;
46 assert_se(sd_pid_get_session(0, &session) == 0);
47 printf("session = %s\n", session);
49 assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
50 printf("user = "UID_FMT"\n", u2);
52 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
53 sd_peer_get_session(pair[0], &pp);
54 sd_peer_get_session(pair[1], &qq);
55 assert_se(streq_ptr(pp, qq));
57 r = sd_uid_get_sessions(u2, false, &sessions);
59 assert_se(r == (int) strv_length(sessions));
60 assert_se(t = strv_join(sessions, ", "));
62 printf("sessions = %s\n", t);
65 assert_se(r == sd_uid_get_sessions(u2, false, NULL));
67 r = sd_uid_get_seats(u2, false, &seats);
69 assert_se(r == (int) strv_length(seats));
70 assert_se(t = strv_join(seats, ", "));
72 printf("seats = %s\n", t);
75 assert_se(r == sd_uid_get_seats(u2, false, NULL));
77 r = sd_session_is_active(session);
79 printf("active = %s\n", yes_no(r));
81 r = sd_session_is_remote(session);
83 printf("remote = %s\n", yes_no(r));
85 r = sd_session_get_state(session, &state);
87 printf("state = %s\n", state);
90 assert_se(sd_session_get_uid(session, &u) >= 0);
91 printf("uid = "UID_FMT"\n", u);
94 assert_se(sd_session_get_type(session, &type) >= 0);
95 printf("type = %s\n", type);
98 assert_se(sd_session_get_class(session, &class) >= 0);
99 printf("class = %s\n", class);
102 assert_se(sd_session_get_display(session, &display) >= 0);
103 printf("display = %s\n", display);
106 assert_se(sd_session_get_remote_user(session, &remote_user) >= 0);
107 printf("remote_user = %s\n", remote_user);
110 assert_se(sd_session_get_remote_host(session, &remote_host) >= 0);
111 printf("remote_host = %s\n", remote_host);
114 assert_se(sd_session_get_seat(session, &seat) >= 0);
115 printf("seat = %s\n", seat);
117 r = sd_seat_can_multi_session(seat);
119 printf("can do multi session = %s\n", yes_no(r));
121 r = sd_seat_can_tty(seat);
123 printf("can do tty = %s\n", yes_no(r));
125 r = sd_seat_can_graphical(seat);
127 printf("can do graphical = %s\n", yes_no(r));
129 assert_se(sd_uid_get_state(u, &state) >= 0);
130 printf("state = %s\n", state);
132 assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
134 k = sd_uid_is_on_seat(u, 1, seat);
136 assert_se(!!r == !!r);
138 assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0);
139 printf("session2 = %s\n", session2);
140 printf("uid2 = "UID_FMT"\n", u2);
142 r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
144 printf("n_sessions = %i\n", r);
145 assert_se(r == (int) strv_length(sessions));
146 assert_se(t = strv_join(sessions, ", "));
148 printf("sessions = %s\n", t);
151 for (k = 0; k < (int) n; k++)
152 printf(" "UID_FMT, uids[k]);
156 assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
163 r = sd_get_seats(&seats);
165 assert_se(r == (int) strv_length(seats));
166 assert_se(t = strv_join(seats, ", "));
168 printf("n_seats = %i\n", r);
169 printf("seats = %s\n", t);
172 assert_se(sd_get_seats(NULL) == r);
174 r = sd_seat_get_active(NULL, &t, NULL);
176 printf("active session on current seat = %s\n", t);
179 r = sd_get_sessions(&sessions);
181 assert_se(r == (int) strv_length(sessions));
182 assert_se(t = strv_join(sessions, ", "));
184 printf("n_sessions = %i\n", r);
185 printf("sessions = %s\n", t);
188 assert_se(sd_get_sessions(NULL) == r);
190 r = sd_get_uids(&uids);
194 for (k = 0; k < r; k++)
195 printf(" "UID_FMT, uids[k]);
199 printf("n_uids = %i\n", r);
200 assert_se(sd_get_uids(NULL) == r);
202 r = sd_get_machine_names(&machines);
204 assert_se(r == (int) strv_length(machines));
205 assert_se(t = strv_join(machines, ", "));
207 printf("n_machines = %i\n", r);
208 printf("machines = %s\n", t);
211 r = sd_login_monitor_new("session", &m);
214 for (n = 0; n < 5; n++) {
218 assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0);
219 assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0);
221 assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0);
223 nw = now(CLOCK_MONOTONIC);
226 timeout == (uint64_t) -1 ? -1 :
227 timeout > nw ? (int) ((timeout - nw) / 1000) :
232 sd_login_monitor_flush(m);
236 sd_login_monitor_unref(m);
239 int main(int argc, char* argv[]) {
240 log_parse_environment();