]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-login/sd-login.c
Merge pull request #9084 from yuwata/fix-8965
[thirdparty/systemd.git] / src / libsystemd / sd-login / sd-login.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2011 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <poll.h>
10 #include <string.h>
11 #include <sys/inotify.h>
12 #include <unistd.h>
13
14 #include "sd-login.h"
15
16 #include "alloc-util.h"
17 #include "cgroup-util.h"
18 #include "dirent-util.h"
19 #include "escape.h"
20 #include "fd-util.h"
21 #include "fileio.h"
22 #include "format-util.h"
23 #include "fs-util.h"
24 #include "hostname-util.h"
25 #include "io-util.h"
26 #include "login-util.h"
27 #include "macro.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "socket-util.h"
31 #include "string-util.h"
32 #include "strv.h"
33 #include "user-util.h"
34 #include "util.h"
35
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
46 _public_ int sd_pid_get_session(pid_t pid, char **session) {
47 int r;
48
49 assert_return(pid >= 0, -EINVAL);
50 assert_return(session, -EINVAL);
51
52 r = cg_pid_get_session(pid, session);
53 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
54 }
55
56 _public_ int sd_pid_get_unit(pid_t pid, char **unit) {
57 int r;
58
59 assert_return(pid >= 0, -EINVAL);
60 assert_return(unit, -EINVAL);
61
62 r = cg_pid_get_unit(pid, unit);
63 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
64 }
65
66 _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
67 int r;
68
69 assert_return(pid >= 0, -EINVAL);
70 assert_return(unit, -EINVAL);
71
72 r = cg_pid_get_user_unit(pid, unit);
73 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
74 }
75
76 _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
77 int r;
78
79 assert_return(pid >= 0, -EINVAL);
80 assert_return(name, -EINVAL);
81
82 r = cg_pid_get_machine_name(pid, name);
83 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
84 }
85
86 _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
87 int r;
88
89 assert_return(pid >= 0, -EINVAL);
90 assert_return(slice, -EINVAL);
91
92 r = cg_pid_get_slice(pid, slice);
93 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
94 }
95
96 _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
97 int r;
98
99 assert_return(pid >= 0, -EINVAL);
100 assert_return(slice, -EINVAL);
101
102 r = cg_pid_get_user_slice(pid, slice);
103 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
104 }
105
106 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
107 int r;
108
109 assert_return(pid >= 0, -EINVAL);
110 assert_return(uid, -EINVAL);
111
112 r = cg_pid_get_owner_uid(pid, uid);
113 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
114 }
115
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
129 * that's easier and less ambiguous for people to grok. */
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
142 _public_ int sd_peer_get_session(int fd, char **session) {
143 struct ucred ucred = {};
144 int r;
145
146 assert_return(fd >= 0, -EBADF);
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
160 assert_return(fd >= 0, -EBADF);
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
174 assert_return(fd >= 0, -EBADF);
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
188 assert_return(fd >= 0, -EBADF);
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
202 assert_return(fd >= 0, -EBADF);
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
216 assert_return(fd >= 0, -EBADF);
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
226 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
227 struct ucred ucred;
228 int r;
229
230 assert_return(fd >= 0, -EBADF);
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
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
254 static int file_of_uid(uid_t uid, char **p) {
255
256 assert_return(uid_is_valid(uid), -EINVAL);
257 assert(p);
258
259 if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
260 return -ENOMEM;
261
262 return 0;
263 }
264
265 _public_ int sd_uid_get_state(uid_t uid, char**state) {
266 _cleanup_free_ char *p = NULL;
267 char *s = NULL;
268 int r;
269
270 assert_return(state, -EINVAL);
271
272 r = file_of_uid(uid, &p);
273 if (r < 0)
274 return r;
275
276 r = parse_env_file(NULL, p, NEWLINE, "STATE", &s, NULL);
277 if (r == -ENOENT) {
278 free(s);
279 s = strdup("offline");
280 if (!s)
281 return -ENOMEM;
282
283 }
284 else if (r < 0) {
285 free(s);
286 return r;
287 }
288 if (isempty(s)) {
289 free(s);
290 return -EIO;
291 }
292
293 *state = s;
294 return 0;
295 }
296
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(NULL, p, NEWLINE, "DISPLAY", &s, NULL);
308 if (r == -ENOENT)
309 return -ENODATA;
310 if (r < 0)
311 return r;
312 if (isempty(s))
313 return -ENODATA;
314
315 *session = TAKE_PTR(s);
316
317 return 0;
318 }
319
320 static 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
344 *_p = TAKE_PTR(p);
345 return 0;
346 }
347
348 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
349 _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
350 size_t l;
351 int r;
352 const char *word, *variable, *state;
353
354 assert_return(uid_is_valid(uid), -EINVAL);
355
356 r = file_of_seat(seat, &p);
357 if (r < 0)
358 return r;
359
360 variable = require_active ? "ACTIVE_UID" : "UIDS";
361
362 r = parse_env_file(NULL, p, NEWLINE, variable, &s, NULL);
363 if (r == -ENOENT)
364 return 0;
365 if (r < 0)
366 return r;
367 if (isempty(s))
368 return 0;
369
370 if (asprintf(&t, UID_FMT, uid) < 0)
371 return -ENOMEM;
372
373 FOREACH_WORD(word, l, s, state)
374 if (strneq(t, word, l))
375 return 1;
376
377 return 0;
378 }
379
380 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
381 _cleanup_free_ char *p = NULL, *s = NULL;
382 char **a;
383 int r;
384
385 assert(variable);
386
387 r = file_of_uid(uid, &p);
388 if (r < 0)
389 return r;
390
391 r = parse_env_file(NULL, p, NEWLINE, variable, &s, NULL);
392 if (r == -ENOENT || (r >= 0 && isempty(s))) {
393 if (array)
394 *array = NULL;
395 return 0;
396 }
397 if (r < 0)
398 return r;
399
400 a = strv_split(s, " ");
401 if (!a)
402 return -ENOMEM;
403
404 strv_uniq(a);
405 r = (int) strv_length(a);
406
407 if (array)
408 *array = a;
409 else
410 strv_free(a);
411
412 return r;
413 }
414
415 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
416 return uid_get_array(
417 uid,
418 require_active == 0 ? "ONLINE_SESSIONS" :
419 require_active > 0 ? "ACTIVE_SESSIONS" :
420 "SESSIONS",
421 sessions);
422 }
423
424 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
425 return uid_get_array(
426 uid,
427 require_active == 0 ? "ONLINE_SEATS" :
428 require_active > 0 ? "ACTIVE_SEATS" :
429 "SEATS",
430 seats);
431 }
432
433 static int file_of_session(const char *session, char **_p) {
434 char *p;
435 int r;
436
437 assert(_p);
438
439 if (session) {
440 if (!session_id_valid(session))
441 return -EINVAL;
442
443 p = strappend("/run/systemd/sessions/", session);
444 } else {
445 _cleanup_free_ char *buf = NULL;
446
447 r = sd_pid_get_session(0, &buf);
448 if (r < 0)
449 return r;
450
451 p = strappend("/run/systemd/sessions/", buf);
452 }
453
454 if (!p)
455 return -ENOMEM;
456
457 *_p = p;
458 return 0;
459 }
460
461 _public_ int sd_session_is_active(const char *session) {
462 _cleanup_free_ char *p = NULL, *s = NULL;
463 int r;
464
465 r = file_of_session(session, &p);
466 if (r < 0)
467 return r;
468
469 r = parse_env_file(NULL, p, NEWLINE, "ACTIVE", &s, NULL);
470 if (r == -ENOENT)
471 return -ENXIO;
472 if (r < 0)
473 return r;
474 if (isempty(s))
475 return -EIO;
476
477 return parse_boolean(s);
478 }
479
480 _public_ int sd_session_is_remote(const char *session) {
481 _cleanup_free_ char *p = NULL, *s = NULL;
482 int r;
483
484 r = file_of_session(session, &p);
485 if (r < 0)
486 return r;
487
488 r = parse_env_file(NULL, p, NEWLINE, "REMOTE", &s, NULL);
489 if (r == -ENOENT)
490 return -ENXIO;
491 if (r < 0)
492 return r;
493 if (isempty(s))
494 return -ENODATA;
495
496 return parse_boolean(s);
497 }
498
499 _public_ int sd_session_get_state(const char *session, char **state) {
500 _cleanup_free_ char *p = NULL, *s = NULL;
501 int r;
502
503 assert_return(state, -EINVAL);
504
505 r = file_of_session(session, &p);
506 if (r < 0)
507 return r;
508
509 r = parse_env_file(NULL, p, NEWLINE, "STATE", &s, NULL);
510 if (r == -ENOENT)
511 return -ENXIO;
512 if (r < 0)
513 return r;
514 if (isempty(s))
515 return -EIO;
516
517 *state = TAKE_PTR(s);
518
519 return 0;
520 }
521
522 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
523 int r;
524 _cleanup_free_ char *p = NULL, *s = NULL;
525
526 assert_return(uid, -EINVAL);
527
528 r = file_of_session(session, &p);
529 if (r < 0)
530 return r;
531
532 r = parse_env_file(NULL, p, NEWLINE, "UID", &s, NULL);
533 if (r == -ENOENT)
534 return -ENXIO;
535 if (r < 0)
536 return r;
537 if (isempty(s))
538 return -EIO;
539
540 return parse_uid(s, uid);
541 }
542
543 static int session_get_string(const char *session, const char *field, char **value) {
544 _cleanup_free_ char *p = NULL, *s = NULL;
545 int r;
546
547 assert_return(value, -EINVAL);
548 assert(field);
549
550 r = file_of_session(session, &p);
551 if (r < 0)
552 return r;
553
554 r = parse_env_file(NULL, p, NEWLINE, field, &s, NULL);
555 if (r == -ENOENT)
556 return -ENXIO;
557 if (r < 0)
558 return r;
559 if (isempty(s))
560 return -ENODATA;
561
562 *value = TAKE_PTR(s);
563 return 0;
564 }
565
566 _public_ int sd_session_get_seat(const char *session, char **seat) {
567 return session_get_string(session, "SEAT", seat);
568 }
569
570 _public_ int sd_session_get_tty(const char *session, char **tty) {
571 return session_get_string(session, "TTY", tty);
572 }
573
574 _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
575 _cleanup_free_ char *vtnr_string = NULL;
576 unsigned u;
577 int r;
578
579 assert_return(vtnr, -EINVAL);
580
581 r = session_get_string(session, "VTNR", &vtnr_string);
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
593 _public_ int sd_session_get_service(const char *session, char **service) {
594 return session_get_string(session, "SERVICE", service);
595 }
596
597 _public_ int sd_session_get_type(const char *session, char **type) {
598 return session_get_string(session, "TYPE", type);
599 }
600
601 _public_ int sd_session_get_class(const char *session, char **class) {
602 return session_get_string(session, "CLASS", class);
603 }
604
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
616 r = cunescape(escaped, 0, &t);
617 if (r < 0)
618 return r;
619
620 *desktop = t;
621 return 0;
622 }
623
624 _public_ int sd_session_get_display(const char *session, char **display) {
625 return session_get_string(session, "DISPLAY", display);
626 }
627
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
636 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
637 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
638 int r;
639
640 assert_return(session || uid, -EINVAL);
641
642 r = file_of_seat(seat, &p);
643 if (r < 0)
644 return r;
645
646 r = parse_env_file(NULL, p, NEWLINE,
647 "ACTIVE", &s,
648 "ACTIVE_UID", &t,
649 NULL);
650 if (r == -ENOENT)
651 return -ENXIO;
652 if (r < 0)
653 return r;
654
655 if (session && !s)
656 return -ENODATA;
657
658 if (uid && !t)
659 return -ENODATA;
660
661 if (uid && t) {
662 r = parse_uid(t, uid);
663 if (r < 0)
664 return r;
665 }
666
667 if (session && s)
668 *session = TAKE_PTR(s);
669
670 return 0;
671 }
672
673 _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
674 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
675 _cleanup_strv_free_ char **a = NULL;
676 _cleanup_free_ uid_t *b = NULL;
677 unsigned n = 0;
678 int r;
679
680 r = file_of_seat(seat, &p);
681 if (r < 0)
682 return r;
683
684 r = parse_env_file(NULL, p, NEWLINE,
685 "SESSIONS", &s,
686 "UIDS", &t,
687 NULL);
688 if (r == -ENOENT)
689 return -ENXIO;
690 if (r < 0)
691 return r;
692
693 if (s) {
694 a = strv_split(s, " ");
695 if (!a)
696 return -ENOMEM;
697 }
698
699 if (uids && t) {
700 const char *word, *state;
701 size_t l;
702
703 FOREACH_WORD(word, l, t, state)
704 n++;
705
706 if (n > 0) {
707 unsigned i = 0;
708
709 b = new(uid_t, n);
710 if (!b)
711 return -ENOMEM;
712
713 FOREACH_WORD(word, l, t, state) {
714 _cleanup_free_ char *k = NULL;
715
716 k = strndup(word, l);
717 if (!k)
718 return -ENOMEM;
719
720 r = parse_uid(k, b + i);
721 if (r < 0)
722 return r;
723
724 i++;
725 }
726 }
727 }
728
729 r = (int) strv_length(a);
730
731 if (sessions)
732 *sessions = TAKE_PTR(a);
733
734 if (uids)
735 *uids = TAKE_PTR(b);
736
737 if (n_uids)
738 *n_uids = n;
739
740 return r;
741 }
742
743 static int seat_get_can(const char *seat, const char *variable) {
744 _cleanup_free_ char *p = NULL, *s = NULL;
745 int r;
746
747 assert(variable);
748
749 r = file_of_seat(seat, &p);
750 if (r < 0)
751 return r;
752
753 r = parse_env_file(NULL, p, NEWLINE,
754 variable, &s,
755 NULL);
756 if (r == -ENOENT)
757 return -ENXIO;
758 if (r < 0)
759 return r;
760 if (isempty(s))
761 return -ENODATA;
762
763 return parse_boolean(s);
764 }
765
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
778 _public_ int sd_get_seats(char ***seats) {
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;
788 }
789
790 _public_ int sd_get_sessions(char ***sessions) {
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;
800 }
801
802 _public_ int sd_get_uids(uid_t **users) {
803 _cleanup_closedir_ DIR *d;
804 struct dirent *de;
805 int r = 0;
806 unsigned n = 0;
807 _cleanup_free_ uid_t *l = NULL;
808
809 d = opendir("/run/systemd/users/");
810 if (!d) {
811 if (errno == ENOENT) {
812 if (users)
813 *users = NULL;
814 return 0;
815 }
816 return -errno;
817 }
818
819 FOREACH_DIRENT_ALL(de, d, return -errno) {
820 int k;
821 uid_t uid;
822
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
832 if (users) {
833 if ((unsigned) r >= n) {
834 uid_t *t;
835
836 n = MAX(16, 2*r);
837 t = realloc(l, sizeof(uid_t) * n);
838 if (!t)
839 return -ENOMEM;
840
841 l = t;
842 }
843
844 assert((unsigned) r < n);
845 l[r++] = uid;
846 } else
847 r++;
848 }
849
850 if (users)
851 *users = TAKE_PTR(l);
852
853 return r;
854 }
855
856 _public_ int sd_get_machine_names(char ***machines) {
857 _cleanup_strv_free_ char **l = NULL;
858 char **a, **b;
859 int r;
860
861 r = get_files_in_directory("/run/systemd/machines/", &l);
862 if (r == -ENOENT) {
863 if (machines)
864 *machines = NULL;
865 return 0;
866 }
867 if (r < 0)
868 return r;
869
870 if (l) {
871 r = 0;
872
873 /* Filter out the unit: symlinks */
874 for (a = b = l; *a; a++) {
875 if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
876 free(*a);
877 else {
878 *b = *a;
879 b++;
880 r++;
881 }
882 }
883
884 *b = NULL;
885 }
886
887 if (machines)
888 *machines = TAKE_PTR(l);
889
890 return r;
891 }
892
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
898 assert_return(machine_name_is_valid(machine), -EINVAL);
899 assert_return(class, -EINVAL);
900
901 p = strjoina("/run/systemd/machines/", machine);
902 r = parse_env_file(NULL, p, NEWLINE, "CLASS", &c, NULL);
903 if (r == -ENOENT)
904 return -ENXIO;
905 if (r < 0)
906 return r;
907 if (!c)
908 return -EIO;
909
910 *class = TAKE_PTR(c);
911
912 return 0;
913 }
914
915 _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
916 _cleanup_free_ char *netif = NULL;
917 size_t l, allocated = 0, nr = 0;
918 int *ni = NULL;
919 const char *p, *word, *state;
920 int r;
921
922 assert_return(machine_name_is_valid(machine), -EINVAL);
923 assert_return(ifindices, -EINVAL);
924
925 p = strjoina("/run/systemd/machines/", machine);
926 r = parse_env_file(NULL, p, NEWLINE, "NETIF", &netif, NULL);
927 if (r == -ENOENT)
928 return -ENXIO;
929 if (r < 0)
930 return r;
931 if (!netif) {
932 *ifindices = NULL;
933 return 0;
934 }
935
936 FOREACH_WORD(word, l, netif, state) {
937 char buf[l+1];
938 int ifi;
939
940 *(char*) (mempcpy(buf, word, l)) = 0;
941
942 if (parse_ifindex(buf, &ifi) < 0)
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
953 *ifindices = ni;
954 return nr;
955 }
956
957 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
958 return (int) (unsigned long) m - 1;
959 }
960
961 static 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) {
966 _cleanup_close_ int fd = -1;
967 bool good = false;
968 int k;
969
970 assert_return(m, -EINVAL);
971
972 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
973 if (fd < 0)
974 return -errno;
975
976 if (!category || streq(category, "seat")) {
977 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
978 if (k < 0)
979 return -errno;
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);
986 if (k < 0)
987 return -errno;
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);
994 if (k < 0)
995 return -errno;
996
997 good = true;
998 }
999
1000 if (!category || streq(category, "machine")) {
1001 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
1002 if (k < 0)
1003 return -errno;
1004
1005 good = true;
1006 }
1007
1008 if (!good)
1009 return -EINVAL;
1010
1011 *m = FD_TO_MONITOR(fd);
1012 fd = -1;
1013
1014 return 0;
1015 }
1016
1017 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1018 int fd;
1019
1020 if (!m)
1021 return NULL;
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) {
1030 int r;
1031
1032 assert_return(m, -EINVAL);
1033
1034 r = flush_fd(MONITOR_TO_FD(m));
1035 if (r < 0)
1036 return r;
1037
1038 return 0;
1039 }
1040
1041 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1042
1043 assert_return(m, -EINVAL);
1044
1045 return MONITOR_TO_FD(m);
1046 }
1047
1048 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1049
1050 assert_return(m, -EINVAL);
1051
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. */
1056 return POLLIN;
1057 }
1058
1059 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1060
1061 assert_return(m, -EINVAL);
1062 assert_return(timeout_usec, -EINVAL);
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 }