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