]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
74b91131 | 2 | |
74b91131 | 3 | #include <errno.h> |
0a6f50c0 | 4 | #include <poll.h> |
4f5dd394 LP |
5 | #include <string.h> |
6 | #include <sys/inotify.h> | |
7 | #include <unistd.h> | |
74b91131 | 8 | |
07630cea LP |
9 | #include "sd-login.h" |
10 | ||
b5efdb8a | 11 | #include "alloc-util.h" |
74b91131 | 12 | #include "cgroup-util.h" |
f4f15635 | 13 | #include "dirent-util.h" |
686d13b9 | 14 | #include "env-file.h" |
4f5dd394 | 15 | #include "escape.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
f97b34a6 | 17 | #include "format-util.h" |
f4f15635 | 18 | #include "fs-util.h" |
25300b5a | 19 | #include "hostname-util.h" |
c004493c | 20 | #include "io-util.h" |
4f5dd394 LP |
21 | #include "login-util.h" |
22 | #include "macro.h" | |
6bedfcbb | 23 | #include "parse-util.h" |
bb15fafe | 24 | #include "path-util.h" |
2583fbea | 25 | #include "socket-util.h" |
07630cea | 26 | #include "string-util.h" |
4f5dd394 | 27 | #include "strv.h" |
b1d4f8e1 | 28 | #include "user-util.h" |
4f5dd394 | 29 | #include "util.h" |
74b91131 | 30 | |
707b66c6 LP |
31 | /* Error codes: |
32 | * | |
33 | * invalid input parameters → -EINVAL | |
34 | * invalid fd → -EBADF | |
35 | * process does not exist → -ESRCH | |
36 | * cgroup does not exist → -ENOENT | |
37 | * machine, session does not exist → -ENXIO | |
38 | * requested metadata on object is missing → -ENODATA | |
39 | */ | |
40 | ||
034a2a52 | 41 | _public_ int sd_pid_get_session(pid_t pid, char **session) { |
19d64d10 | 42 | int r; |
ba1261bc | 43 | |
1ae464e0 TA |
44 | assert_return(pid >= 0, -EINVAL); |
45 | assert_return(session, -EINVAL); | |
034a2a52 | 46 | |
19d64d10 | 47 | r = cg_pid_get_session(pid, session); |
bc9e9af1 | 48 | return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; |
74b91131 LP |
49 | } |
50 | ||
94fb446e | 51 | _public_ int sd_pid_get_unit(pid_t pid, char **unit) { |
171f8f59 | 52 | int r; |
9847946e | 53 | |
1ae464e0 TA |
54 | assert_return(pid >= 0, -EINVAL); |
55 | assert_return(unit, -EINVAL); | |
9847946e | 56 | |
171f8f59 | 57 | r = cg_pid_get_unit(pid, unit); |
bc9e9af1 | 58 | return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; |
9847946e LP |
59 | } |
60 | ||
97e13058 | 61 | _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) { |
d440fb97 | 62 | int r; |
97e13058 | 63 | |
1ae464e0 TA |
64 | assert_return(pid >= 0, -EINVAL); |
65 | assert_return(unit, -EINVAL); | |
97e13058 | 66 | |
d440fb97 | 67 | r = cg_pid_get_user_unit(pid, unit); |
bc9e9af1 | 68 | return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; |
97e13058 LP |
69 | } |
70 | ||
7027ff61 | 71 | _public_ int sd_pid_get_machine_name(pid_t pid, char **name) { |
bc9e9af1 | 72 | int r; |
7027ff61 | 73 | |
1ae464e0 TA |
74 | assert_return(pid >= 0, -EINVAL); |
75 | assert_return(name, -EINVAL); | |
7027ff61 | 76 | |
bc9e9af1 ZJS |
77 | r = cg_pid_get_machine_name(pid, name); |
78 | return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; | |
7027ff61 LP |
79 | } |
80 | ||
1021b21b | 81 | _public_ int sd_pid_get_slice(pid_t pid, char **slice) { |
bc9e9af1 | 82 | int r; |
1021b21b | 83 | |
1ae464e0 TA |
84 | assert_return(pid >= 0, -EINVAL); |
85 | assert_return(slice, -EINVAL); | |
1021b21b | 86 | |
bc9e9af1 ZJS |
87 | r = cg_pid_get_slice(pid, slice); |
88 | return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; | |
1021b21b LP |
89 | } |
90 | ||
329ac4bc | 91 | _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) { |
bc9e9af1 | 92 | int r; |
329ac4bc LP |
93 | |
94 | assert_return(pid >= 0, -EINVAL); | |
95 | assert_return(slice, -EINVAL); | |
96 | ||
bc9e9af1 ZJS |
97 | r = cg_pid_get_user_slice(pid, slice); |
98 | return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; | |
329ac4bc LP |
99 | } |
100 | ||
034a2a52 | 101 | _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) { |
bc9e9af1 | 102 | int r; |
034a2a52 | 103 | |
1ae464e0 TA |
104 | assert_return(pid >= 0, -EINVAL); |
105 | assert_return(uid, -EINVAL); | |
034a2a52 | 106 | |
bc9e9af1 ZJS |
107 | r = cg_pid_get_owner_uid(pid, uid); |
108 | return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; | |
034a2a52 LP |
109 | } |
110 | ||
f5aaf575 LP |
111 | _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) { |
112 | char *c; | |
113 | int r; | |
114 | ||
115 | assert_return(pid >= 0, -EINVAL); | |
116 | assert_return(cgroup, -EINVAL); | |
117 | ||
118 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c); | |
119 | if (r < 0) | |
120 | return r; | |
121 | ||
122 | /* The internal APIs return the empty string for the root | |
123 | * cgroup, let's return the "/" in the public APIs instead, as | |
595bfe7d | 124 | * that's easier and less ambiguous for people to grok. */ |
f5aaf575 LP |
125 | if (isempty(c)) { |
126 | free(c); | |
127 | c = strdup("/"); | |
128 | if (!c) | |
129 | return -ENOMEM; | |
130 | ||
131 | } | |
132 | ||
133 | *cgroup = c; | |
134 | return 0; | |
135 | } | |
136 | ||
bf34ab14 | 137 | _public_ int sd_peer_get_session(int fd, char **session) { |
a7f7d1bd | 138 | struct ucred ucred = {}; |
bf34ab14 LP |
139 | int r; |
140 | ||
8ac43fee | 141 | assert_return(fd >= 0, -EBADF); |
bf34ab14 LP |
142 | assert_return(session, -EINVAL); |
143 | ||
144 | r = getpeercred(fd, &ucred); | |
145 | if (r < 0) | |
146 | return r; | |
147 | ||
148 | return cg_pid_get_session(ucred.pid, session); | |
149 | } | |
150 | ||
151 | _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) { | |
152 | struct ucred ucred; | |
153 | int r; | |
154 | ||
8ac43fee | 155 | assert_return(fd >= 0, -EBADF); |
bf34ab14 LP |
156 | assert_return(uid, -EINVAL); |
157 | ||
158 | r = getpeercred(fd, &ucred); | |
159 | if (r < 0) | |
160 | return r; | |
161 | ||
162 | return cg_pid_get_owner_uid(ucred.pid, uid); | |
163 | } | |
164 | ||
165 | _public_ int sd_peer_get_unit(int fd, char **unit) { | |
166 | struct ucred ucred; | |
167 | int r; | |
168 | ||
8ac43fee | 169 | assert_return(fd >= 0, -EBADF); |
bf34ab14 LP |
170 | assert_return(unit, -EINVAL); |
171 | ||
172 | r = getpeercred(fd, &ucred); | |
173 | if (r < 0) | |
174 | return r; | |
175 | ||
176 | return cg_pid_get_unit(ucred.pid, unit); | |
177 | } | |
178 | ||
179 | _public_ int sd_peer_get_user_unit(int fd, char **unit) { | |
180 | struct ucred ucred; | |
181 | int r; | |
182 | ||
8ac43fee | 183 | assert_return(fd >= 0, -EBADF); |
bf34ab14 LP |
184 | assert_return(unit, -EINVAL); |
185 | ||
186 | r = getpeercred(fd, &ucred); | |
187 | if (r < 0) | |
188 | return r; | |
189 | ||
190 | return cg_pid_get_user_unit(ucred.pid, unit); | |
191 | } | |
192 | ||
193 | _public_ int sd_peer_get_machine_name(int fd, char **machine) { | |
194 | struct ucred ucred; | |
195 | int r; | |
196 | ||
8ac43fee | 197 | assert_return(fd >= 0, -EBADF); |
bf34ab14 LP |
198 | assert_return(machine, -EINVAL); |
199 | ||
200 | r = getpeercred(fd, &ucred); | |
201 | if (r < 0) | |
202 | return r; | |
203 | ||
204 | return cg_pid_get_machine_name(ucred.pid, machine); | |
205 | } | |
206 | ||
207 | _public_ int sd_peer_get_slice(int fd, char **slice) { | |
208 | struct ucred ucred; | |
209 | int r; | |
210 | ||
8ac43fee | 211 | assert_return(fd >= 0, -EBADF); |
bf34ab14 LP |
212 | assert_return(slice, -EINVAL); |
213 | ||
214 | r = getpeercred(fd, &ucred); | |
215 | if (r < 0) | |
216 | return r; | |
217 | ||
218 | return cg_pid_get_slice(ucred.pid, slice); | |
219 | } | |
220 | ||
329ac4bc LP |
221 | _public_ int sd_peer_get_user_slice(int fd, char **slice) { |
222 | struct ucred ucred; | |
223 | int r; | |
224 | ||
8ac43fee | 225 | assert_return(fd >= 0, -EBADF); |
329ac4bc LP |
226 | assert_return(slice, -EINVAL); |
227 | ||
228 | r = getpeercred(fd, &ucred); | |
229 | if (r < 0) | |
230 | return r; | |
231 | ||
232 | return cg_pid_get_user_slice(ucred.pid, slice); | |
233 | } | |
234 | ||
f5aaf575 LP |
235 | _public_ int sd_peer_get_cgroup(int fd, char **cgroup) { |
236 | struct ucred ucred; | |
237 | int r; | |
238 | ||
239 | assert_return(fd >= 0, -EBADF); | |
240 | assert_return(cgroup, -EINVAL); | |
241 | ||
242 | r = getpeercred(fd, &ucred); | |
243 | if (r < 0) | |
244 | return r; | |
245 | ||
246 | return sd_pid_get_cgroup(ucred.pid, cgroup); | |
247 | } | |
248 | ||
a077b666 | 249 | static int file_of_uid(uid_t uid, char **p) { |
707b66c6 LP |
250 | |
251 | assert_return(uid_is_valid(uid), -EINVAL); | |
a077b666 LP |
252 | assert(p); |
253 | ||
254 | if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0) | |
255 | return -ENOMEM; | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
74b91131 | 260 | _public_ int sd_uid_get_state(uid_t uid, char**state) { |
13df9c39 | 261 | _cleanup_free_ char *p = NULL, *s = NULL; |
74b91131 LP |
262 | int r; |
263 | ||
1ae464e0 | 264 | assert_return(state, -EINVAL); |
74b91131 | 265 | |
a077b666 LP |
266 | r = file_of_uid(uid, &p); |
267 | if (r < 0) | |
268 | return r; | |
74b91131 | 269 | |
13df9c39 | 270 | r = parse_env_file(NULL, p, "STATE", &s); |
74b91131 | 271 | if (r == -ENOENT) { |
13df9c39 LP |
272 | r = free_and_strdup(&s, "offline"); |
273 | if (r < 0) | |
274 | return r; | |
275 | } else if (r < 0) | |
74b91131 | 276 | return r; |
13df9c39 | 277 | else if (isempty(s)) |
74b91131 LP |
278 | return -EIO; |
279 | ||
13df9c39 | 280 | *state = TAKE_PTR(s); |
74b91131 LP |
281 | return 0; |
282 | } | |
283 | ||
a077b666 LP |
284 | _public_ int sd_uid_get_display(uid_t uid, char **session) { |
285 | _cleanup_free_ char *p = NULL, *s = NULL; | |
286 | int r; | |
287 | ||
288 | assert_return(session, -EINVAL); | |
289 | ||
290 | r = file_of_uid(uid, &p); | |
291 | if (r < 0) | |
292 | return r; | |
293 | ||
13df9c39 | 294 | r = parse_env_file(NULL, p, "DISPLAY", &s); |
fc60d815 | 295 | if (r == -ENOENT) |
707b66c6 | 296 | return -ENODATA; |
a077b666 LP |
297 | if (r < 0) |
298 | return r; | |
a077b666 | 299 | if (isempty(s)) |
707b66c6 | 300 | return -ENODATA; |
a077b666 | 301 | |
ae2a15bc | 302 | *session = TAKE_PTR(s); |
a077b666 LP |
303 | |
304 | return 0; | |
305 | } | |
306 | ||
707b66c6 LP |
307 | static int file_of_seat(const char *seat, char **_p) { |
308 | char *p; | |
309 | int r; | |
310 | ||
311 | assert(_p); | |
312 | ||
313 | if (seat) { | |
314 | if (!filename_is_valid(seat)) | |
315 | return -EINVAL; | |
316 | ||
317 | p = strappend("/run/systemd/seats/", seat); | |
318 | } else { | |
319 | _cleanup_free_ char *buf = NULL; | |
320 | ||
321 | r = sd_session_get_seat(NULL, &buf); | |
322 | if (r < 0) | |
323 | return r; | |
324 | ||
325 | p = strappend("/run/systemd/seats/", buf); | |
326 | } | |
327 | ||
328 | if (!p) | |
329 | return -ENOMEM; | |
330 | ||
ae2a15bc | 331 | *_p = TAKE_PTR(p); |
707b66c6 LP |
332 | return 0; |
333 | } | |
334 | ||
034a2a52 | 335 | _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) { |
d70964d0 | 336 | _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL; |
74b91131 LP |
337 | size_t l; |
338 | int r; | |
a2a5291b | 339 | const char *word, *variable, *state; |
74b91131 | 340 | |
a1f686da | 341 | assert_return(uid_is_valid(uid), -EINVAL); |
74b91131 | 342 | |
707b66c6 LP |
343 | r = file_of_seat(seat, &p); |
344 | if (r < 0) | |
345 | return r; | |
034a2a52 | 346 | |
707b66c6 | 347 | variable = require_active ? "ACTIVE_UID" : "UIDS"; |
74b91131 | 348 | |
13df9c39 | 349 | r = parse_env_file(NULL, p, variable, &s); |
707b66c6 LP |
350 | if (r == -ENOENT) |
351 | return 0; | |
d70964d0 | 352 | if (r < 0) |
74b91131 | 353 | return r; |
707b66c6 LP |
354 | if (isempty(s)) |
355 | return 0; | |
74b91131 | 356 | |
de0671ee | 357 | if (asprintf(&t, UID_FMT, uid) < 0) |
74b91131 | 358 | return -ENOMEM; |
74b91131 | 359 | |
707b66c6 | 360 | FOREACH_WORD(word, l, s, state) |
a2a5291b | 361 | if (strneq(t, word, l)) |
74b91131 | 362 | return 1; |
74b91131 | 363 | |
74b91131 LP |
364 | return 0; |
365 | } | |
366 | ||
034a2a52 | 367 | static int uid_get_array(uid_t uid, const char *variable, char ***array) { |
d70964d0 | 368 | _cleanup_free_ char *p = NULL, *s = NULL; |
034a2a52 LP |
369 | char **a; |
370 | int r; | |
371 | ||
707b66c6 | 372 | assert(variable); |
a1f686da | 373 | |
a077b666 LP |
374 | r = file_of_uid(uid, &p); |
375 | if (r < 0) | |
376 | return r; | |
034a2a52 | 377 | |
13df9c39 | 378 | r = parse_env_file(NULL, p, variable, &s); |
707b66c6 | 379 | if (r == -ENOENT || (r >= 0 && isempty(s))) { |
d60ef526 LP |
380 | if (array) |
381 | *array = NULL; | |
034a2a52 LP |
382 | return 0; |
383 | } | |
707b66c6 LP |
384 | if (r < 0) |
385 | return r; | |
034a2a52 LP |
386 | |
387 | a = strv_split(s, " "); | |
034a2a52 LP |
388 | if (!a) |
389 | return -ENOMEM; | |
390 | ||
d60ef526 | 391 | strv_uniq(a); |
da6053d0 | 392 | r = (int) strv_length(a); |
d60ef526 LP |
393 | |
394 | if (array) | |
395 | *array = a; | |
396 | else | |
397 | strv_free(a); | |
398 | ||
399 | return r; | |
034a2a52 LP |
400 | } |
401 | ||
402 | _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) { | |
d18dff43 LP |
403 | return uid_get_array( |
404 | uid, | |
405 | require_active == 0 ? "ONLINE_SESSIONS" : | |
406 | require_active > 0 ? "ACTIVE_SESSIONS" : | |
407 | "SESSIONS", | |
408 | sessions); | |
74b91131 LP |
409 | } |
410 | ||
034a2a52 | 411 | _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) { |
d18dff43 LP |
412 | return uid_get_array( |
413 | uid, | |
414 | require_active == 0 ? "ONLINE_SEATS" : | |
415 | require_active > 0 ? "ACTIVE_SEATS" : | |
416 | "SEATS", | |
417 | seats); | |
74b91131 LP |
418 | } |
419 | ||
50b1678a LP |
420 | static int file_of_session(const char *session, char **_p) { |
421 | char *p; | |
74b91131 | 422 | int r; |
74b91131 | 423 | |
50b1678a LP |
424 | assert(_p); |
425 | ||
4b549144 ZJS |
426 | if (session) { |
427 | if (!session_id_valid(session)) | |
428 | return -EINVAL; | |
429 | ||
50b1678a | 430 | p = strappend("/run/systemd/sessions/", session); |
4b549144 ZJS |
431 | } else { |
432 | _cleanup_free_ char *buf = NULL; | |
50b1678a LP |
433 | |
434 | r = sd_pid_get_session(0, &buf); | |
435 | if (r < 0) | |
436 | return r; | |
437 | ||
438 | p = strappend("/run/systemd/sessions/", buf); | |
50b1678a | 439 | } |
74b91131 | 440 | |
74b91131 LP |
441 | if (!p) |
442 | return -ENOMEM; | |
443 | ||
50b1678a LP |
444 | *_p = p; |
445 | return 0; | |
446 | } | |
447 | ||
448 | _public_ int sd_session_is_active(const char *session) { | |
d70964d0 | 449 | _cleanup_free_ char *p = NULL, *s = NULL; |
707b66c6 | 450 | int r; |
50b1678a LP |
451 | |
452 | r = file_of_session(session, &p); | |
453 | if (r < 0) | |
454 | return r; | |
455 | ||
13df9c39 | 456 | r = parse_env_file(NULL, p, "ACTIVE", &s); |
707b66c6 LP |
457 | if (r == -ENOENT) |
458 | return -ENXIO; | |
d70964d0 | 459 | if (r < 0) |
74b91131 | 460 | return r; |
707b66c6 | 461 | if (isempty(s)) |
74b91131 LP |
462 | return -EIO; |
463 | ||
0325941f | 464 | return parse_boolean(s); |
74b91131 LP |
465 | } |
466 | ||
5b04fe60 | 467 | _public_ int sd_session_is_remote(const char *session) { |
5b04fe60 | 468 | _cleanup_free_ char *p = NULL, *s = NULL; |
707b66c6 | 469 | int r; |
5b04fe60 MM |
470 | |
471 | r = file_of_session(session, &p); | |
472 | if (r < 0) | |
473 | return r; | |
474 | ||
13df9c39 | 475 | r = parse_env_file(NULL, p, "REMOTE", &s); |
707b66c6 LP |
476 | if (r == -ENOENT) |
477 | return -ENXIO; | |
5b04fe60 MM |
478 | if (r < 0) |
479 | return r; | |
707b66c6 LP |
480 | if (isempty(s)) |
481 | return -ENODATA; | |
5b04fe60 | 482 | |
0325941f | 483 | return parse_boolean(s); |
5b04fe60 MM |
484 | } |
485 | ||
0604381b | 486 | _public_ int sd_session_get_state(const char *session, char **state) { |
d70964d0 | 487 | _cleanup_free_ char *p = NULL, *s = NULL; |
0604381b LP |
488 | int r; |
489 | ||
1ae464e0 | 490 | assert_return(state, -EINVAL); |
0604381b LP |
491 | |
492 | r = file_of_session(session, &p); | |
493 | if (r < 0) | |
494 | return r; | |
495 | ||
13df9c39 | 496 | r = parse_env_file(NULL, p, "STATE", &s); |
707b66c6 LP |
497 | if (r == -ENOENT) |
498 | return -ENXIO; | |
d70964d0 | 499 | if (r < 0) |
0604381b | 500 | return r; |
707b66c6 | 501 | if (isempty(s)) |
0604381b LP |
502 | return -EIO; |
503 | ||
ae2a15bc | 504 | *state = TAKE_PTR(s); |
d70964d0 | 505 | |
0604381b LP |
506 | return 0; |
507 | } | |
508 | ||
74b91131 LP |
509 | _public_ int sd_session_get_uid(const char *session, uid_t *uid) { |
510 | int r; | |
d70964d0 | 511 | _cleanup_free_ char *p = NULL, *s = NULL; |
74b91131 | 512 | |
1ae464e0 | 513 | assert_return(uid, -EINVAL); |
74b91131 | 514 | |
50b1678a LP |
515 | r = file_of_session(session, &p); |
516 | if (r < 0) | |
517 | return r; | |
74b91131 | 518 | |
13df9c39 | 519 | r = parse_env_file(NULL, p, "UID", &s); |
707b66c6 LP |
520 | if (r == -ENOENT) |
521 | return -ENXIO; | |
d70964d0 | 522 | if (r < 0) |
74b91131 | 523 | return r; |
707b66c6 | 524 | if (isempty(s)) |
74b91131 LP |
525 | return -EIO; |
526 | ||
0325941f | 527 | return parse_uid(s, uid); |
74b91131 LP |
528 | } |
529 | ||
51f58f08 | 530 | static int session_get_string(const char *session, const char *field, char **value) { |
d70964d0 | 531 | _cleanup_free_ char *p = NULL, *s = NULL; |
74b91131 LP |
532 | int r; |
533 | ||
1ae464e0 | 534 | assert_return(value, -EINVAL); |
707b66c6 | 535 | assert(field); |
74b91131 | 536 | |
50b1678a LP |
537 | r = file_of_session(session, &p); |
538 | if (r < 0) | |
539 | return r; | |
74b91131 | 540 | |
13df9c39 | 541 | r = parse_env_file(NULL, p, field, &s); |
707b66c6 LP |
542 | if (r == -ENOENT) |
543 | return -ENXIO; | |
d70964d0 | 544 | if (r < 0) |
74b91131 | 545 | return r; |
74b91131 | 546 | if (isempty(s)) |
707b66c6 | 547 | return -ENODATA; |
74b91131 | 548 | |
ae2a15bc | 549 | *value = TAKE_PTR(s); |
74b91131 LP |
550 | return 0; |
551 | } | |
552 | ||
51f58f08 LP |
553 | _public_ int sd_session_get_seat(const char *session, char **seat) { |
554 | return session_get_string(session, "SEAT", seat); | |
555 | } | |
eff40633 | 556 | |
c84f5e4a LP |
557 | _public_ int sd_session_get_tty(const char *session, char **tty) { |
558 | return session_get_string(session, "TTY", tty); | |
559 | } | |
560 | ||
44ded3ab | 561 | _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) { |
3f4fee03 | 562 | _cleanup_free_ char *vtnr_string = NULL; |
44ded3ab GC |
563 | unsigned u; |
564 | int r; | |
565 | ||
707b66c6 LP |
566 | assert_return(vtnr, -EINVAL); |
567 | ||
0581dac2 | 568 | r = session_get_string(session, "VTNR", &vtnr_string); |
44ded3ab GC |
569 | if (r < 0) |
570 | return r; | |
571 | ||
572 | r = safe_atou(vtnr_string, &u); | |
573 | if (r < 0) | |
574 | return r; | |
575 | ||
576 | *vtnr = u; | |
577 | return 0; | |
578 | } | |
579 | ||
51f58f08 LP |
580 | _public_ int sd_session_get_service(const char *session, char **service) { |
581 | return session_get_string(session, "SERVICE", service); | |
582 | } | |
eff40633 | 583 | |
51f58f08 LP |
584 | _public_ int sd_session_get_type(const char *session, char **type) { |
585 | return session_get_string(session, "TYPE", type); | |
586 | } | |
eff40633 | 587 | |
51f58f08 LP |
588 | _public_ int sd_session_get_class(const char *session, char **class) { |
589 | return session_get_string(session, "CLASS", class); | |
eff40633 LP |
590 | } |
591 | ||
c72d5456 DH |
592 | _public_ int sd_session_get_desktop(const char *session, char **desktop) { |
593 | _cleanup_free_ char *escaped = NULL; | |
594 | char *t; | |
595 | int r; | |
596 | ||
597 | assert_return(desktop, -EINVAL); | |
598 | ||
599 | r = session_get_string(session, "DESKTOP", &escaped); | |
600 | if (r < 0) | |
601 | return r; | |
602 | ||
527b7a42 LP |
603 | r = cunescape(escaped, 0, &t); |
604 | if (r < 0) | |
605 | return r; | |
c72d5456 DH |
606 | |
607 | *desktop = t; | |
608 | return 0; | |
609 | } | |
610 | ||
fc8af9ff LP |
611 | _public_ int sd_session_get_display(const char *session, char **display) { |
612 | return session_get_string(session, "DISPLAY", display); | |
613 | } | |
614 | ||
5b04fe60 MM |
615 | _public_ int sd_session_get_remote_user(const char *session, char **remote_user) { |
616 | return session_get_string(session, "REMOTE_USER", remote_user); | |
617 | } | |
618 | ||
619 | _public_ int sd_session_get_remote_host(const char *session, char **remote_host) { | |
620 | return session_get_string(session, "REMOTE_HOST", remote_host); | |
621 | } | |
622 | ||
74b91131 | 623 | _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) { |
d70964d0 | 624 | _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL; |
74b91131 LP |
625 | int r; |
626 | ||
1ae464e0 | 627 | assert_return(session || uid, -EINVAL); |
74b91131 | 628 | |
50b1678a LP |
629 | r = file_of_seat(seat, &p); |
630 | if (r < 0) | |
631 | return r; | |
74b91131 | 632 | |
aa8fbc74 | 633 | r = parse_env_file(NULL, p, |
74b91131 | 634 | "ACTIVE", &s, |
13df9c39 | 635 | "ACTIVE_UID", &t); |
707b66c6 LP |
636 | if (r == -ENOENT) |
637 | return -ENXIO; | |
d70964d0 | 638 | if (r < 0) |
74b91131 | 639 | return r; |
74b91131 | 640 | |
d70964d0 | 641 | if (session && !s) |
4211d5bd | 642 | return -ENODATA; |
74b91131 | 643 | |
d70964d0 | 644 | if (uid && !t) |
4211d5bd | 645 | return -ENODATA; |
74b91131 LP |
646 | |
647 | if (uid && t) { | |
034a2a52 | 648 | r = parse_uid(t, uid); |
d70964d0 | 649 | if (r < 0) |
74b91131 | 650 | return r; |
74b91131 LP |
651 | } |
652 | ||
ae2a15bc LP |
653 | if (session && s) |
654 | *session = TAKE_PTR(s); | |
74b91131 LP |
655 | |
656 | return 0; | |
657 | } | |
034a2a52 LP |
658 | |
659 | _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) { | |
d70964d0 HH |
660 | _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL; |
661 | _cleanup_strv_free_ char **a = NULL; | |
662 | _cleanup_free_ uid_t *b = NULL; | |
034a2a52 LP |
663 | unsigned n = 0; |
664 | int r; | |
665 | ||
50b1678a LP |
666 | r = file_of_seat(seat, &p); |
667 | if (r < 0) | |
668 | return r; | |
034a2a52 | 669 | |
aa8fbc74 | 670 | r = parse_env_file(NULL, p, |
034a2a52 | 671 | "SESSIONS", &s, |
13df9c39 | 672 | "UIDS", &t); |
707b66c6 LP |
673 | if (r == -ENOENT) |
674 | return -ENXIO; | |
d70964d0 | 675 | if (r < 0) |
034a2a52 | 676 | return r; |
034a2a52 | 677 | |
d60ef526 | 678 | if (s) { |
034a2a52 | 679 | a = strv_split(s, " "); |
d70964d0 | 680 | if (!a) |
034a2a52 | 681 | return -ENOMEM; |
034a2a52 LP |
682 | } |
683 | ||
034a2a52 | 684 | if (uids && t) { |
a2a5291b | 685 | const char *word, *state; |
034a2a52 | 686 | size_t l; |
034a2a52 | 687 | |
a2a5291b | 688 | FOREACH_WORD(word, l, t, state) |
034a2a52 LP |
689 | n++; |
690 | ||
d70964d0 | 691 | if (n > 0) { |
de3756ab | 692 | unsigned i = 0; |
034a2a52 | 693 | |
de3756ab | 694 | b = new(uid_t, n); |
d70964d0 | 695 | if (!b) |
034a2a52 | 696 | return -ENOMEM; |
034a2a52 | 697 | |
a2a5291b | 698 | FOREACH_WORD(word, l, t, state) { |
d70964d0 | 699 | _cleanup_free_ char *k = NULL; |
034a2a52 | 700 | |
a2a5291b | 701 | k = strndup(word, l); |
d70964d0 | 702 | if (!k) |
de3756ab | 703 | return -ENOMEM; |
de3756ab LP |
704 | |
705 | r = parse_uid(k, b + i); | |
de3756ab | 706 | if (r < 0) |
cc6182e8 | 707 | return r; |
de3756ab LP |
708 | |
709 | i++; | |
710 | } | |
034a2a52 LP |
711 | } |
712 | } | |
713 | ||
da6053d0 | 714 | r = (int) strv_length(a); |
d60ef526 | 715 | |
1cc6c93a YW |
716 | if (sessions) |
717 | *sessions = TAKE_PTR(a); | |
034a2a52 | 718 | |
1cc6c93a YW |
719 | if (uids) |
720 | *uids = TAKE_PTR(b); | |
034a2a52 LP |
721 | |
722 | if (n_uids) | |
723 | *n_uids = n; | |
724 | ||
d60ef526 | 725 | return r; |
034a2a52 LP |
726 | } |
727 | ||
20747498 | 728 | static int seat_get_can(const char *seat, const char *variable) { |
d70964d0 | 729 | _cleanup_free_ char *p = NULL, *s = NULL; |
add30678 LP |
730 | int r; |
731 | ||
707b66c6 | 732 | assert(variable); |
0325941f | 733 | |
50b1678a LP |
734 | r = file_of_seat(seat, &p); |
735 | if (r < 0) | |
736 | return r; | |
add30678 | 737 | |
aa8fbc74 | 738 | r = parse_env_file(NULL, p, |
13df9c39 | 739 | variable, &s); |
707b66c6 LP |
740 | if (r == -ENOENT) |
741 | return -ENXIO; | |
d70964d0 | 742 | if (r < 0) |
add30678 | 743 | return r; |
707b66c6 LP |
744 | if (isempty(s)) |
745 | return -ENODATA; | |
add30678 | 746 | |
0325941f | 747 | return parse_boolean(s); |
add30678 LP |
748 | } |
749 | ||
20747498 LP |
750 | _public_ int sd_seat_can_multi_session(const char *seat) { |
751 | return seat_get_can(seat, "CAN_MULTI_SESSION"); | |
752 | } | |
753 | ||
754 | _public_ int sd_seat_can_tty(const char *seat) { | |
755 | return seat_get_can(seat, "CAN_TTY"); | |
756 | } | |
757 | ||
758 | _public_ int sd_seat_can_graphical(const char *seat) { | |
759 | return seat_get_can(seat, "CAN_GRAPHICAL"); | |
760 | } | |
761 | ||
034a2a52 | 762 | _public_ int sd_get_seats(char ***seats) { |
2b5e9267 YW |
763 | int r; |
764 | ||
765 | r = get_files_in_directory("/run/systemd/seats/", seats); | |
766 | if (r == -ENOENT) { | |
767 | if (seats) | |
768 | *seats = NULL; | |
769 | return 0; | |
770 | } | |
771 | return r; | |
034a2a52 LP |
772 | } |
773 | ||
774 | _public_ int sd_get_sessions(char ***sessions) { | |
2b5e9267 YW |
775 | int r; |
776 | ||
777 | r = get_files_in_directory("/run/systemd/sessions/", sessions); | |
778 | if (r == -ENOENT) { | |
779 | if (sessions) | |
780 | *sessions = NULL; | |
781 | return 0; | |
782 | } | |
783 | return r; | |
034a2a52 LP |
784 | } |
785 | ||
786 | _public_ int sd_get_uids(uid_t **users) { | |
d70964d0 | 787 | _cleanup_closedir_ DIR *d; |
8fb3f009 | 788 | struct dirent *de; |
034a2a52 LP |
789 | int r = 0; |
790 | unsigned n = 0; | |
d70964d0 | 791 | _cleanup_free_ uid_t *l = NULL; |
034a2a52 | 792 | |
034a2a52 | 793 | d = opendir("/run/systemd/users/"); |
2b5e9267 YW |
794 | if (!d) { |
795 | if (errno == ENOENT) { | |
796 | if (users) | |
797 | *users = NULL; | |
798 | return 0; | |
799 | } | |
8ea913b2 | 800 | return -errno; |
2b5e9267 | 801 | } |
8ea913b2 | 802 | |
8fb3f009 | 803 | FOREACH_DIRENT_ALL(de, d, return -errno) { |
034a2a52 LP |
804 | int k; |
805 | uid_t uid; | |
806 | ||
034a2a52 LP |
807 | dirent_ensure_type(d, de); |
808 | ||
809 | if (!dirent_is_file(de)) | |
810 | continue; | |
811 | ||
812 | k = parse_uid(de->d_name, &uid); | |
813 | if (k < 0) | |
814 | continue; | |
815 | ||
d60ef526 LP |
816 | if (users) { |
817 | if ((unsigned) r >= n) { | |
818 | uid_t *t; | |
034a2a52 | 819 | |
d60ef526 LP |
820 | n = MAX(16, 2*r); |
821 | t = realloc(l, sizeof(uid_t) * n); | |
d70964d0 HH |
822 | if (!t) |
823 | return -ENOMEM; | |
034a2a52 | 824 | |
d60ef526 LP |
825 | l = t; |
826 | } | |
034a2a52 | 827 | |
d60ef526 LP |
828 | assert((unsigned) r < n); |
829 | l[r++] = uid; | |
830 | } else | |
831 | r++; | |
034a2a52 LP |
832 | } |
833 | ||
1cc6c93a YW |
834 | if (users) |
835 | *users = TAKE_PTR(l); | |
034a2a52 LP |
836 | |
837 | return r; | |
838 | } | |
839 | ||
4d5fb962 | 840 | _public_ int sd_get_machine_names(char ***machines) { |
96480634 ZJS |
841 | _cleanup_strv_free_ char **l = NULL; |
842 | char **a, **b; | |
89f7c846 LP |
843 | int r; |
844 | ||
845 | r = get_files_in_directory("/run/systemd/machines/", &l); | |
0543105b | 846 | if (r == -ENOENT) { |
76ed21e1 YW |
847 | if (machines) |
848 | *machines = NULL; | |
0543105b ZJS |
849 | return 0; |
850 | } | |
89f7c846 LP |
851 | if (r < 0) |
852 | return r; | |
853 | ||
854 | if (l) { | |
855 | r = 0; | |
856 | ||
857 | /* Filter out the unit: symlinks */ | |
76ed21e1 | 858 | for (a = b = l; *a; a++) { |
b9a8d250 | 859 | if (startswith(*a, "unit:") || !machine_name_is_valid(*a)) |
89f7c846 LP |
860 | free(*a); |
861 | else { | |
862 | *b = *a; | |
863 | b++; | |
864 | r++; | |
865 | } | |
866 | } | |
867 | ||
868 | *b = NULL; | |
869 | } | |
870 | ||
ae2a15bc LP |
871 | if (machines) |
872 | *machines = TAKE_PTR(l); | |
873 | ||
89f7c846 | 874 | return r; |
a20affe2 LP |
875 | } |
876 | ||
0325941f LP |
877 | _public_ int sd_machine_get_class(const char *machine, char **class) { |
878 | _cleanup_free_ char *c = NULL; | |
879 | const char *p; | |
880 | int r; | |
881 | ||
0325941f LP |
882 | assert_return(class, -EINVAL); |
883 | ||
a8c9b7a0 LP |
884 | if (streq(machine, ".host")) { |
885 | c = strdup("host"); | |
886 | if (!c) | |
887 | return -ENOMEM; | |
888 | } else { | |
889 | if (!machine_name_is_valid(machine)) | |
890 | return -EINVAL; | |
0325941f | 891 | |
a8c9b7a0 | 892 | p = strjoina("/run/systemd/machines/", machine); |
13df9c39 | 893 | r = parse_env_file(NULL, p, "CLASS", &c); |
a8c9b7a0 LP |
894 | if (r == -ENOENT) |
895 | return -ENXIO; | |
896 | if (r < 0) | |
897 | return r; | |
898 | if (!c) | |
899 | return -EIO; | |
900 | } | |
0325941f | 901 | |
a8c9b7a0 | 902 | *class = TAKE_PTR(c); |
0325941f LP |
903 | return 0; |
904 | } | |
905 | ||
634af566 | 906 | _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) { |
cabb0bc6 LP |
907 | _cleanup_free_ char *netif = NULL; |
908 | size_t l, allocated = 0, nr = 0; | |
cabb0bc6 | 909 | int *ni = NULL; |
a2a5291b | 910 | const char *p, *word, *state; |
cabb0bc6 LP |
911 | int r; |
912 | ||
913 | assert_return(machine_name_is_valid(machine), -EINVAL); | |
634af566 | 914 | assert_return(ifindices, -EINVAL); |
cabb0bc6 | 915 | |
63c372cb | 916 | p = strjoina("/run/systemd/machines/", machine); |
13df9c39 | 917 | r = parse_env_file(NULL, p, "NETIF", &netif); |
707b66c6 LP |
918 | if (r == -ENOENT) |
919 | return -ENXIO; | |
cabb0bc6 LP |
920 | if (r < 0) |
921 | return r; | |
922 | if (!netif) { | |
634af566 | 923 | *ifindices = NULL; |
cabb0bc6 LP |
924 | return 0; |
925 | } | |
926 | ||
a2a5291b | 927 | FOREACH_WORD(word, l, netif, state) { |
cabb0bc6 LP |
928 | char buf[l+1]; |
929 | int ifi; | |
930 | ||
a2a5291b | 931 | *(char*) (mempcpy(buf, word, l)) = 0; |
cabb0bc6 | 932 | |
6ad623a3 | 933 | if (parse_ifindex(buf, &ifi) < 0) |
cabb0bc6 LP |
934 | continue; |
935 | ||
936 | if (!GREEDY_REALLOC(ni, allocated, nr+1)) { | |
937 | free(ni); | |
938 | return -ENOMEM; | |
939 | } | |
940 | ||
941 | ni[nr++] = ifi; | |
942 | } | |
943 | ||
634af566 | 944 | *ifindices = ni; |
cabb0bc6 LP |
945 | return nr; |
946 | } | |
947 | ||
a1e92eee | 948 | static int MONITOR_TO_FD(sd_login_monitor *m) { |
034a2a52 LP |
949 | return (int) (unsigned long) m - 1; |
950 | } | |
951 | ||
a1e92eee | 952 | static sd_login_monitor* FD_TO_MONITOR(int fd) { |
034a2a52 LP |
953 | return (sd_login_monitor*) (unsigned long) (fd + 1); |
954 | } | |
955 | ||
956 | _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) { | |
4cbbc2a2 | 957 | _cleanup_close_ int fd = -1; |
034a2a52 | 958 | bool good = false; |
4cbbc2a2 | 959 | int k; |
034a2a52 | 960 | |
1ae464e0 | 961 | assert_return(m, -EINVAL); |
034a2a52 LP |
962 | |
963 | fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); | |
964 | if (fd < 0) | |
bcb161b0 | 965 | return -errno; |
034a2a52 LP |
966 | |
967 | if (!category || streq(category, "seat")) { | |
968 | k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE); | |
4cbbc2a2 | 969 | if (k < 0) |
034a2a52 | 970 | return -errno; |
034a2a52 LP |
971 | |
972 | good = true; | |
973 | } | |
974 | ||
975 | if (!category || streq(category, "session")) { | |
976 | k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE); | |
4cbbc2a2 | 977 | if (k < 0) |
034a2a52 | 978 | return -errno; |
034a2a52 LP |
979 | |
980 | good = true; | |
981 | } | |
982 | ||
983 | if (!category || streq(category, "uid")) { | |
984 | k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE); | |
4cbbc2a2 | 985 | if (k < 0) |
034a2a52 | 986 | return -errno; |
034a2a52 LP |
987 | |
988 | good = true; | |
989 | } | |
990 | ||
e10375f2 | 991 | if (!category || streq(category, "machine")) { |
ba73ed85 | 992 | k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE); |
4cbbc2a2 | 993 | if (k < 0) |
e10375f2 | 994 | return -errno; |
e10375f2 LP |
995 | |
996 | good = true; | |
997 | } | |
998 | ||
4cbbc2a2 | 999 | if (!good) |
034a2a52 | 1000 | return -EINVAL; |
034a2a52 LP |
1001 | |
1002 | *m = FD_TO_MONITOR(fd); | |
4cbbc2a2 LP |
1003 | fd = -1; |
1004 | ||
034a2a52 LP |
1005 | return 0; |
1006 | } | |
1007 | ||
1008 | _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) { | |
1009 | int fd; | |
1010 | ||
4afd3348 LP |
1011 | if (!m) |
1012 | return NULL; | |
034a2a52 LP |
1013 | |
1014 | fd = MONITOR_TO_FD(m); | |
1015 | close_nointr(fd); | |
1016 | ||
1017 | return NULL; | |
1018 | } | |
1019 | ||
1020 | _public_ int sd_login_monitor_flush(sd_login_monitor *m) { | |
665dfe93 | 1021 | int r; |
034a2a52 | 1022 | |
1ae464e0 | 1023 | assert_return(m, -EINVAL); |
034a2a52 | 1024 | |
665dfe93 LP |
1025 | r = flush_fd(MONITOR_TO_FD(m)); |
1026 | if (r < 0) | |
1027 | return r; | |
1028 | ||
1029 | return 0; | |
034a2a52 LP |
1030 | } |
1031 | ||
1032 | _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) { | |
1033 | ||
1ae464e0 | 1034 | assert_return(m, -EINVAL); |
034a2a52 LP |
1035 | |
1036 | return MONITOR_TO_FD(m); | |
1037 | } | |
dace83cb LP |
1038 | |
1039 | _public_ int sd_login_monitor_get_events(sd_login_monitor *m) { | |
1040 | ||
1ae464e0 | 1041 | assert_return(m, -EINVAL); |
dace83cb | 1042 | |
667c24a6 LP |
1043 | /* For now we will only return POLLIN here, since we don't |
1044 | * need anything else ever for inotify. However, let's have | |
1045 | * this API to keep our options open should we later on need | |
1046 | * it. */ | |
dace83cb LP |
1047 | return POLLIN; |
1048 | } | |
667c24a6 LP |
1049 | |
1050 | _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) { | |
1051 | ||
1ae464e0 TA |
1052 | assert_return(m, -EINVAL); |
1053 | assert_return(timeout_usec, -EINVAL); | |
667c24a6 LP |
1054 | |
1055 | /* For now we will only return (uint64_t) -1, since we don't | |
1056 | * need any timeout. However, let's have this API to keep our | |
1057 | * options open should we later on need it. */ | |
1058 | *timeout_usec = (uint64_t) -1; | |
1059 | return 0; | |
1060 | } |