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) {
33 char *seat, *type, *class, *display;
38 char **seats, **sessions, **machines;
44 assert_se(sd_pid_get_session(0, &session) == 0);
45 printf("session = %s\n", session);
47 assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
48 printf("user = %lu\n", (unsigned long) u2);
50 r = sd_uid_get_sessions(u2, false, &sessions);
52 assert_se(r == (int) strv_length(sessions));
53 assert_se(t = strv_join(sessions, ", "));
55 printf("sessions = %s\n", t);
58 assert_se(r == sd_uid_get_sessions(u2, false, NULL));
60 r = sd_uid_get_seats(u2, false, &seats);
62 assert_se(r == (int) strv_length(seats));
63 assert_se(t = strv_join(seats, ", "));
65 printf("seats = %s\n", t);
68 assert_se(r == sd_uid_get_seats(u2, false, NULL));
70 r = sd_session_is_active(session);
72 printf("active = %s\n", yes_no(r));
74 r = sd_session_get_state(session, &state);
76 printf("state = %s\n", state);
79 assert_se(sd_session_get_uid(session, &u) >= 0);
80 printf("uid = %lu\n", (unsigned long) u);
83 assert_se(sd_session_get_type(session, &type) >= 0);
84 printf("type = %s\n", type);
87 assert_se(sd_session_get_class(session, &class) >= 0);
88 printf("class = %s\n", class);
91 assert_se(sd_session_get_display(session, &display) >= 0);
92 printf("display = %s\n", display);
95 assert_se(sd_session_get_seat(session, &seat) >= 0);
96 printf("seat = %s\n", seat);
98 r = sd_seat_can_multi_session(seat);
100 printf("can do multi session = %s\n", yes_no(r));
102 r = sd_seat_can_tty(seat);
104 printf("can do tty = %s\n", yes_no(r));
106 r = sd_seat_can_graphical(seat);
108 printf("can do graphical = %s\n", yes_no(r));
110 assert_se(sd_uid_get_state(u, &state) >= 0);
111 printf("state = %s\n", state);
113 assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
115 k = sd_uid_is_on_seat(u, 1, seat);
117 assert_se(!!r == !!r);
119 assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0);
120 printf("session2 = %s\n", session2);
121 printf("uid2 = %lu\n", (unsigned long) u2);
123 r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
125 printf("n_sessions = %i\n", r);
126 assert_se(r == (int) strv_length(sessions));
127 assert_se(t = strv_join(sessions, ", "));
129 printf("sessions = %s\n", t);
132 for (k = 0; k < (int) n; k++)
133 printf(" %lu", (unsigned long) uids[k]);
137 assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
144 r = sd_get_seats(&seats);
146 assert_se(r == (int) strv_length(seats));
147 assert_se(t = strv_join(seats, ", "));
149 printf("n_seats = %i\n", r);
150 printf("seats = %s\n", t);
153 assert_se(sd_get_seats(NULL) == r);
155 r = sd_seat_get_active(NULL, &t, NULL);
157 printf("active session on current seat = %s\n", t);
160 r = sd_get_sessions(&sessions);
162 assert_se(r == (int) strv_length(sessions));
163 assert_se(t = strv_join(sessions, ", "));
165 printf("n_sessions = %i\n", r);
166 printf("sessions = %s\n", t);
169 assert_se(sd_get_sessions(NULL) == r);
171 r = sd_get_uids(&uids);
175 for (k = 0; k < r; k++)
176 printf(" %lu", (unsigned long) uids[k]);
180 printf("n_uids = %i\n", r);
181 assert_se(sd_get_uids(NULL) == r);
183 r = sd_get_machine_names(&machines);
185 assert_se(r == (int) strv_length(machines));
186 assert_se(t = strv_join(machines, ", "));
188 printf("n_machines = %i\n", r);
189 printf("machines = %s\n", t);
192 r = sd_login_monitor_new("session", &m);
195 for (n = 0; n < 5; n++) {
199 assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0);
200 assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0);
202 assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0);
204 nw = now(CLOCK_MONOTONIC);
207 timeout == (uint64_t) -1 ? -1 :
208 timeout > nw ? (int) ((timeout - nw) / 1000) :
213 sd_login_monitor_flush(m);
217 sd_login_monitor_unref(m);
220 int main(int argc, char* argv[]) {
221 log_parse_environment();