chiark / gitweb /
sd-event: fix sd_event_source_get_priority() (#4712)
[elogind.git] / src / libelogind / sd-login / test-login.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2011 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <poll.h>
21 #include <string.h>
22
23 #include "sd-login.h"
24
25 #include "alloc-util.h"
26 #include "fd-util.h"
27 #include "format-util.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "util.h"
31
32 /// Additional includes needed by elogind
33 #include "musl_missing.h"
34
35 static void test_login(void) {
36         _cleanup_close_pair_ int pair[2] = { -1, -1 };
37         _cleanup_free_ char *pp = NULL, *qq = NULL;
38         int r, k;
39         uid_t u, u2;
40         char *seat, *type, *class, *display, *remote_user, *remote_host, *display_session, *cgroup;
41         char *session;
42         char *state;
43         char *session2;
44         char *t;
45         char **seats, **sessions, **machines;
46         uid_t *uids;
47         unsigned n;
48         struct pollfd pollfd;
49         sd_login_monitor *m = NULL;
50
51         assert_se(sd_pid_get_session(0, &session) == 0);
52         printf("session = %s\n", session);
53
54         assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
55         printf("user = "UID_FMT"\n", u2);
56
57         assert_se(sd_pid_get_cgroup(0, &cgroup) == 0);
58         printf("cgroup = %s\n", cgroup);
59         free(cgroup);
60
61         display_session = NULL;
62         r = sd_uid_get_display(u2, &display_session);
63         assert_se(r >= 0 || r == -ENODATA);
64         printf("user's display session = %s\n", strna(display_session));
65         free(display_session);
66
67         assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
68         sd_peer_get_session(pair[0], &pp);
69         sd_peer_get_session(pair[1], &qq);
70         assert_se(streq_ptr(pp, qq));
71
72         r = sd_uid_get_sessions(u2, false, &sessions);
73         assert_se(r >= 0);
74         assert_se(r == (int) strv_length(sessions));
75         assert_se(t = strv_join(sessions, ", "));
76         strv_free(sessions);
77         printf("sessions = %s\n", t);
78         free(t);
79
80         assert_se(r == sd_uid_get_sessions(u2, false, NULL));
81
82         r = sd_uid_get_seats(u2, false, &seats);
83         assert_se(r >= 0);
84         assert_se(r == (int) strv_length(seats));
85         assert_se(t = strv_join(seats, ", "));
86         strv_free(seats);
87         printf("seats = %s\n", t);
88         free(t);
89
90         assert_se(r == sd_uid_get_seats(u2, false, NULL));
91
92         r = sd_session_is_active(session);
93         assert_se(r >= 0);
94         printf("active = %s\n", yes_no(r));
95
96         r = sd_session_is_remote(session);
97         assert_se(r >= 0);
98         printf("remote = %s\n", yes_no(r));
99
100         r = sd_session_get_state(session, &state);
101         assert_se(r >= 0);
102         printf("state = %s\n", state);
103         free(state);
104
105         assert_se(sd_session_get_uid(session, &u) >= 0);
106         printf("uid = "UID_FMT"\n", u);
107         assert_se(u == u2);
108
109         assert_se(sd_session_get_type(session, &type) >= 0);
110         printf("type = %s\n", type);
111         free(type);
112
113         assert_se(sd_session_get_class(session, &class) >= 0);
114         printf("class = %s\n", class);
115         free(class);
116
117         display = NULL;
118         r = sd_session_get_display(session, &display);
119         assert_se(r >= 0 || r == -ENODATA);
120         printf("display = %s\n", strna(display));
121         free(display);
122
123         remote_user = NULL;
124         r = sd_session_get_remote_user(session, &remote_user);
125         assert_se(r >= 0 || r == -ENODATA);
126         printf("remote_user = %s\n", strna(remote_user));
127         free(remote_user);
128
129         remote_host = NULL;
130         r = sd_session_get_remote_host(session, &remote_host);
131         assert_se(r >= 0 || r == -ENODATA);
132         printf("remote_host = %s\n", strna(remote_host));
133         free(remote_host);
134
135         assert_se(sd_session_get_seat(session, &seat) >= 0);
136         printf("seat = %s\n", seat);
137
138         r = sd_seat_can_multi_session(seat);
139         assert_se(r >= 0);
140         printf("can do multi session = %s\n", yes_no(r));
141
142         r = sd_seat_can_tty(seat);
143         assert_se(r >= 0);
144         printf("can do tty = %s\n", yes_no(r));
145
146         r = sd_seat_can_graphical(seat);
147         assert_se(r >= 0);
148         printf("can do graphical = %s\n", yes_no(r));
149
150         assert_se(sd_uid_get_state(u, &state) >= 0);
151         printf("state = %s\n", state);
152
153         assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
154
155         k = sd_uid_is_on_seat(u, 1, seat);
156         assert_se(k >= 0);
157         assert_se(!!r == !!r);
158
159         assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0);
160         printf("session2 = %s\n", session2);
161         printf("uid2 = "UID_FMT"\n", u2);
162
163         r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
164         assert_se(r >= 0);
165         printf("n_sessions = %i\n", r);
166         assert_se(r == (int) strv_length(sessions));
167         assert_se(t = strv_join(sessions, ", "));
168         strv_free(sessions);
169         printf("sessions = %s\n", t);
170         free(t);
171         printf("uids =");
172         for (k = 0; k < (int) n; k++)
173                 printf(" "UID_FMT, uids[k]);
174         printf("\n");
175         free(uids);
176
177         assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
178
179         free(session);
180         free(state);
181         free(session2);
182         free(seat);
183
184         r = sd_get_seats(&seats);
185         assert_se(r >= 0);
186         assert_se(r == (int) strv_length(seats));
187         assert_se(t = strv_join(seats, ", "));
188         strv_free(seats);
189         printf("n_seats = %i\n", r);
190         printf("seats = %s\n", t);
191         free(t);
192
193         assert_se(sd_get_seats(NULL) == r);
194
195         r = sd_seat_get_active(NULL, &t, NULL);
196         assert_se(r >= 0);
197         printf("active session on current seat = %s\n", t);
198         free(t);
199
200         r = sd_get_sessions(&sessions);
201         assert_se(r >= 0);
202         assert_se(r == (int) strv_length(sessions));
203         assert_se(t = strv_join(sessions, ", "));
204         strv_free(sessions);
205         printf("n_sessions = %i\n", r);
206         printf("sessions = %s\n", t);
207         free(t);
208
209         assert_se(sd_get_sessions(NULL) == r);
210
211         r = sd_get_uids(&uids);
212         assert_se(r >= 0);
213
214         printf("uids =");
215         for (k = 0; k < r; k++)
216                 printf(" "UID_FMT, uids[k]);
217         printf("\n");
218         free(uids);
219
220         printf("n_uids = %i\n", r);
221         assert_se(sd_get_uids(NULL) == r);
222
223         r = sd_get_machine_names(&machines);
224         assert_se(r >= 0);
225         assert_se(r == (int) strv_length(machines));
226         assert_se(t = strv_join(machines, ", "));
227         strv_free(machines);
228         printf("n_machines = %i\n", r);
229         printf("machines = %s\n", t);
230         free(t);
231
232         r = sd_login_monitor_new("session", &m);
233         assert_se(r >= 0);
234
235         for (n = 0; n < 5; n++) {
236                 usec_t timeout, nw;
237
238                 zero(pollfd);
239                 assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0);
240                 assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0);
241
242                 assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0);
243
244                 nw = now(CLOCK_MONOTONIC);
245
246                 r = poll(&pollfd, 1,
247                          timeout == (uint64_t) -1 ? -1 :
248                          timeout > nw ? (int) ((timeout - nw) / 1000) :
249                          0);
250
251                 assert_se(r >= 0);
252
253                 sd_login_monitor_flush(m);
254                 printf("Wake!\n");
255         }
256
257         sd_login_monitor_unref(m);
258 }
259
260 int main(int argc, char* argv[]) {
261         elogind_set_program_name(argv[0]);
262         log_parse_environment();
263         log_open();
264
265         test_login();
266
267         return 0;
268 }