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