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