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