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