]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-login/test-login.c
util: replace close_pipe() with new safe_close_pair()
[thirdparty/systemd.git] / src / libsystemd / sd-login / test-login.c
CommitLineData
74b91131
LP
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
5430f7f2
LP
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
74b91131
LP
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
5430f7f2 16 Lesser General Public License for more details.
74b91131 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
74b91131
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
034a2a52
LP
22#include <sys/poll.h>
23#include <string.h>
24
81527be1
LP
25#include <systemd/sd-login.h>
26
74b91131 27#include "util.h"
034a2a52 28#include "strv.h"
74b91131 29
e9e506ed 30static void test_login(void) {
3d94f76c 31 _cleanup_close_pair_ int pair[2] = { -1, -1 };
bf34ab14 32 _cleanup_free_ char *pp = NULL, *qq = NULL;
034a2a52
LP
33 int r, k;
34 uid_t u, u2;
5b04fe60 35 char *seat, *type, *class, *display, *remote_user, *remote_host;
74b91131
LP
36 char *session;
37 char *state;
38 char *session2;
034a2a52 39 char *t;
a20affe2 40 char **seats, **sessions, **machines;
034a2a52
LP
41 uid_t *uids;
42 unsigned n;
43 struct pollfd pollfd;
39883f62 44 sd_login_monitor *m = NULL;
74b91131
LP
45
46 assert_se(sd_pid_get_session(0, &session) == 0);
47 printf("session = %s\n", session);
48
034a2a52
LP
49 assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
50 printf("user = %lu\n", (unsigned long) u2);
51
bf34ab14
LP
52 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
53 sd_peer_get_session(pair[0], &pp);
54 sd_peer_get_session(pair[1], &qq);
55 assert_se(streq_ptr(pp, qq));
56
034a2a52
LP
57 r = sd_uid_get_sessions(u2, false, &sessions);
58 assert_se(r >= 0);
d60ef526 59 assert_se(r == (int) strv_length(sessions));
034a2a52
LP
60 assert_se(t = strv_join(sessions, ", "));
61 strv_free(sessions);
62 printf("sessions = %s\n", t);
63 free(t);
64
d60ef526
LP
65 assert_se(r == sd_uid_get_sessions(u2, false, NULL));
66
034a2a52
LP
67 r = sd_uid_get_seats(u2, false, &seats);
68 assert_se(r >= 0);
d60ef526 69 assert_se(r == (int) strv_length(seats));
034a2a52
LP
70 assert_se(t = strv_join(seats, ", "));
71 strv_free(seats);
72 printf("seats = %s\n", t);
73 free(t);
74
d60ef526
LP
75 assert_se(r == sd_uid_get_seats(u2, false, NULL));
76
74b91131
LP
77 r = sd_session_is_active(session);
78 assert_se(r >= 0);
79 printf("active = %s\n", yes_no(r));
80
5b04fe60
MM
81 r = sd_session_is_remote(session);
82 assert_se(r >= 0);
83 printf("remote = %s\n", yes_no(r));
84
0604381b
LP
85 r = sd_session_get_state(session, &state);
86 assert_se(r >= 0);
87 printf("state = %s\n", state);
88 free(state);
89
74b91131
LP
90 assert_se(sd_session_get_uid(session, &u) >= 0);
91 printf("uid = %lu\n", (unsigned long) u);
034a2a52 92 assert_se(u == u2);
74b91131 93
51f58f08
LP
94 assert_se(sd_session_get_type(session, &type) >= 0);
95 printf("type = %s\n", type);
96 free(type);
97
98 assert_se(sd_session_get_class(session, &class) >= 0);
99 printf("class = %s\n", class);
100 free(class);
101
fc8af9ff
LP
102 assert_se(sd_session_get_display(session, &display) >= 0);
103 printf("display = %s\n", display);
104 free(display);
105
5b04fe60
MM
106 assert_se(sd_session_get_remote_user(session, &remote_user) >= 0);
107 printf("remote_user = %s\n", remote_user);
108 free(remote_user);
109
110 assert_se(sd_session_get_remote_host(session, &remote_host) >= 0);
111 printf("remote_host = %s\n", remote_host);
112 free(remote_host);
113
74b91131
LP
114 assert_se(sd_session_get_seat(session, &seat) >= 0);
115 printf("seat = %s\n", seat);
116
add30678
LP
117 r = sd_seat_can_multi_session(seat);
118 assert_se(r >= 0);
119 printf("can do multi session = %s\n", yes_no(r));
120
20747498
LP
121 r = sd_seat_can_tty(seat);
122 assert_se(r >= 0);
123 printf("can do tty = %s\n", yes_no(r));
124
125 r = sd_seat_can_graphical(seat);
126 assert_se(r >= 0);
127 printf("can do graphical = %s\n", yes_no(r));
128
74b91131
LP
129 assert_se(sd_uid_get_state(u, &state) >= 0);
130 printf("state = %s\n", state);
131
034a2a52 132 assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
74b91131 133
034a2a52 134 k = sd_uid_is_on_seat(u, 1, seat);
74b91131
LP
135 assert_se(k >= 0);
136 assert_se(!!r == !!r);
137
138 assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0);
139 printf("session2 = %s\n", session2);
140 printf("uid2 = %lu\n", (unsigned long) u2);
141
d60ef526
LP
142 r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
143 assert_se(r >= 0);
144 printf("n_sessions = %i\n", r);
145 assert_se(r == (int) strv_length(sessions));
034a2a52
LP
146 assert_se(t = strv_join(sessions, ", "));
147 strv_free(sessions);
148 printf("sessions = %s\n", t);
149 free(t);
150 printf("uids =");
151 for (k = 0; k < (int) n; k++)
152 printf(" %lu", (unsigned long) uids[k]);
153 printf("\n");
154 free(uids);
155
d60ef526
LP
156 assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
157
74b91131
LP
158 free(session);
159 free(state);
160 free(session2);
161 free(seat);
162
d60ef526
LP
163 r = sd_get_seats(&seats);
164 assert_se(r >= 0);
165 assert_se(r == (int) strv_length(seats));
034a2a52
LP
166 assert_se(t = strv_join(seats, ", "));
167 strv_free(seats);
d60ef526 168 printf("n_seats = %i\n", r);
034a2a52
LP
169 printf("seats = %s\n", t);
170 free(t);
171
51f58f08
LP
172 assert_se(sd_get_seats(NULL) == r);
173
50b1678a
LP
174 r = sd_seat_get_active(NULL, &t, NULL);
175 assert_se(r >= 0);
176 printf("active session on current seat = %s\n", t);
177 free(t);
178
d60ef526
LP
179 r = sd_get_sessions(&sessions);
180 assert_se(r >= 0);
181 assert_se(r == (int) strv_length(sessions));
034a2a52
LP
182 assert_se(t = strv_join(sessions, ", "));
183 strv_free(sessions);
d60ef526 184 printf("n_sessions = %i\n", r);
034a2a52
LP
185 printf("sessions = %s\n", t);
186 free(t);
187
d60ef526
LP
188 assert_se(sd_get_sessions(NULL) == r);
189
034a2a52
LP
190 r = sd_get_uids(&uids);
191 assert_se(r >= 0);
192
193 printf("uids =");
194 for (k = 0; k < r; k++)
195 printf(" %lu", (unsigned long) uids[k]);
196 printf("\n");
034a2a52
LP
197 free(uids);
198
d60ef526
LP
199 printf("n_uids = %i\n", r);
200 assert_se(sd_get_uids(NULL) == r);
201
a20affe2 202 r = sd_get_machine_names(&machines);
b9b2b042 203 assert_se(r >= 0);
a20affe2
LP
204 assert_se(r == (int) strv_length(machines));
205 assert_se(t = strv_join(machines, ", "));
206 strv_free(machines);
207 printf("n_machines = %i\n", r);
208 printf("machines = %s\n", t);
209 free(t);
034a2a52 210
a20affe2
LP
211 r = sd_login_monitor_new("session", &m);
212 assert_se(r >= 0);
034a2a52 213
b9b2b042 214 for (n = 0; n < 5; n++) {
667c24a6
LP
215 usec_t timeout, nw;
216
217 zero(pollfd);
218 assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0);
219 assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0);
220
221 assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0);
222
223 nw = now(CLOCK_MONOTONIC);
224
225 r = poll(&pollfd, 1,
226 timeout == (uint64_t) -1 ? -1 :
227 timeout > nw ? (int) ((timeout - nw) / 1000) :
228 0);
229
b9b2b042 230 assert_se(r >= 0);
034a2a52 231
b9b2b042
LP
232 sd_login_monitor_flush(m);
233 printf("Wake!\n");
234 }
034a2a52 235
b9b2b042 236 sd_login_monitor_unref(m);
e9e506ed
ZJS
237}
238
239int main(int argc, char* argv[]) {
240 log_parse_environment();
241 log_open();
242
243 test_login();
034a2a52 244
74b91131
LP
245 return 0;
246}