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