chiark / gitweb /
build-sys: create "compatibility libraries" section
[elogind.git] / src / login / test-login.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <sys/poll.h>
23 #include <string.h>
24
25 #include <systemd/sd-login.h>
26
27 #include "util.h"
28 #include "strv.h"
29
30 static void test_login(void) {
31         int r, k;
32         uid_t u, u2;
33         char *seat, *type, *class, *display, *remote_user, *remote_host;
34         char *session;
35         char *state;
36         char *session2;
37         char *t;
38         char **seats, **sessions, **machines;
39         uid_t *uids;
40         unsigned n;
41         struct pollfd pollfd;
42         sd_login_monitor *m;
43
44         assert_se(sd_pid_get_session(0, &session) == 0);
45         printf("session = %s\n", session);
46
47         assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
48         printf("user = %lu\n", (unsigned long) u2);
49
50         r = sd_uid_get_sessions(u2, false, &sessions);
51         assert_se(r >= 0);
52         assert_se(r == (int) strv_length(sessions));
53         assert_se(t = strv_join(sessions, ", "));
54         strv_free(sessions);
55         printf("sessions = %s\n", t);
56         free(t);
57
58         assert_se(r == sd_uid_get_sessions(u2, false, NULL));
59
60         r = sd_uid_get_seats(u2, false, &seats);
61         assert_se(r >= 0);
62         assert_se(r == (int) strv_length(seats));
63         assert_se(t = strv_join(seats, ", "));
64         strv_free(seats);
65         printf("seats = %s\n", t);
66         free(t);
67
68         assert_se(r == sd_uid_get_seats(u2, false, NULL));
69
70         r = sd_session_is_active(session);
71         assert_se(r >= 0);
72         printf("active = %s\n", yes_no(r));
73
74         r = sd_session_is_remote(session);
75         assert_se(r >= 0);
76         printf("remote = %s\n", yes_no(r));
77
78         r = sd_session_get_state(session, &state);
79         assert_se(r >= 0);
80         printf("state = %s\n", state);
81         free(state);
82
83         assert_se(sd_session_get_uid(session, &u) >= 0);
84         printf("uid = %lu\n", (unsigned long) u);
85         assert_se(u == u2);
86
87         assert_se(sd_session_get_type(session, &type) >= 0);
88         printf("type = %s\n", type);
89         free(type);
90
91         assert_se(sd_session_get_class(session, &class) >= 0);
92         printf("class = %s\n", class);
93         free(class);
94
95         assert_se(sd_session_get_display(session, &display) >= 0);
96         printf("display = %s\n", display);
97         free(display);
98
99         assert_se(sd_session_get_remote_user(session, &remote_user) >= 0);
100         printf("remote_user = %s\n", remote_user);
101         free(remote_user);
102
103         assert_se(sd_session_get_remote_host(session, &remote_host) >= 0);
104         printf("remote_host = %s\n", remote_host);
105         free(remote_host);
106
107         assert_se(sd_session_get_seat(session, &seat) >= 0);
108         printf("seat = %s\n", seat);
109
110         r = sd_seat_can_multi_session(seat);
111         assert_se(r >= 0);
112         printf("can do multi session = %s\n", yes_no(r));
113
114         r = sd_seat_can_tty(seat);
115         assert_se(r >= 0);
116         printf("can do tty = %s\n", yes_no(r));
117
118         r = sd_seat_can_graphical(seat);
119         assert_se(r >= 0);
120         printf("can do graphical = %s\n", yes_no(r));
121
122         assert_se(sd_uid_get_state(u, &state) >= 0);
123         printf("state = %s\n", state);
124
125         assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
126
127         k = sd_uid_is_on_seat(u, 1, seat);
128         assert_se(k >= 0);
129         assert_se(!!r == !!r);
130
131         assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0);
132         printf("session2 = %s\n", session2);
133         printf("uid2 = %lu\n", (unsigned long) u2);
134
135         r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
136         assert_se(r >= 0);
137         printf("n_sessions = %i\n", r);
138         assert_se(r == (int) strv_length(sessions));
139         assert_se(t = strv_join(sessions, ", "));
140         strv_free(sessions);
141         printf("sessions = %s\n", t);
142         free(t);
143         printf("uids =");
144         for (k = 0; k < (int) n; k++)
145                 printf(" %lu", (unsigned long) uids[k]);
146         printf("\n");
147         free(uids);
148
149         assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
150
151         free(session);
152         free(state);
153         free(session2);
154         free(seat);
155
156         r = sd_get_seats(&seats);
157         assert_se(r >= 0);
158         assert_se(r == (int) strv_length(seats));
159         assert_se(t = strv_join(seats, ", "));
160         strv_free(seats);
161         printf("n_seats = %i\n", r);
162         printf("seats = %s\n", t);
163         free(t);
164
165         assert_se(sd_get_seats(NULL) == r);
166
167         r = sd_seat_get_active(NULL, &t, NULL);
168         assert_se(r >= 0);
169         printf("active session on current seat = %s\n", t);
170         free(t);
171
172         r = sd_get_sessions(&sessions);
173         assert_se(r >= 0);
174         assert_se(r == (int) strv_length(sessions));
175         assert_se(t = strv_join(sessions, ", "));
176         strv_free(sessions);
177         printf("n_sessions = %i\n", r);
178         printf("sessions = %s\n", t);
179         free(t);
180
181         assert_se(sd_get_sessions(NULL) == r);
182
183         r = sd_get_uids(&uids);
184         assert_se(r >= 0);
185
186         printf("uids =");
187         for (k = 0; k < r; k++)
188                 printf(" %lu", (unsigned long) uids[k]);
189         printf("\n");
190         free(uids);
191
192         printf("n_uids = %i\n", r);
193         assert_se(sd_get_uids(NULL) == r);
194
195         r = sd_get_machine_names(&machines);
196         assert_se(r >= 0);
197         assert_se(r == (int) strv_length(machines));
198         assert_se(t = strv_join(machines, ", "));
199         strv_free(machines);
200         printf("n_machines = %i\n", r);
201         printf("machines = %s\n", t);
202         free(t);
203
204         r = sd_login_monitor_new("session", &m);
205         assert_se(r >= 0);
206
207         for (n = 0; n < 5; n++) {
208                 usec_t timeout, nw;
209
210                 zero(pollfd);
211                 assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0);
212                 assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0);
213
214                 assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0);
215
216                 nw = now(CLOCK_MONOTONIC);
217
218                 r = poll(&pollfd, 1,
219                          timeout == (uint64_t) -1 ? -1 :
220                          timeout > nw ? (int) ((timeout - nw) / 1000) :
221                          0);
222
223                 assert_se(r >= 0);
224
225                 sd_login_monitor_flush(m);
226                 printf("Wake!\n");
227         }
228
229         sd_login_monitor_unref(m);
230 }
231
232 int main(int argc, char* argv[]) {
233         log_parse_environment();
234         log_open();
235
236         test_login();
237
238         return 0;
239 }