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