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