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