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