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