]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-login/sd-login.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / libsystemd / sd-login / sd-login.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <poll.h>
5 #include <sys/inotify.h>
6 #include <unistd.h>
7
8 #include "sd-login.h"
9
10 #include "alloc-util.h"
11 #include "cgroup-util.h"
12 #include "dirent-util.h"
13 #include "env-file.h"
14 #include "escape.h"
15 #include "extract-word.h"
16 #include "fd-util.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 "stdio-util.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "user-util.h"
30 #include "util.h"
31
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
42 _public_ int sd_pid_get_session(pid_t pid, char **session) {
43 int r;
44
45 assert_return(pid >= 0, -EINVAL);
46 assert_return(session, -EINVAL);
47
48 r = cg_pid_get_session(pid, session);
49 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
50 }
51
52 _public_ int sd_pid_get_unit(pid_t pid, char **unit) {
53 int r;
54
55 assert_return(pid >= 0, -EINVAL);
56 assert_return(unit, -EINVAL);
57
58 r = cg_pid_get_unit(pid, unit);
59 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
60 }
61
62 _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
63 int r;
64
65 assert_return(pid >= 0, -EINVAL);
66 assert_return(unit, -EINVAL);
67
68 r = cg_pid_get_user_unit(pid, unit);
69 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
70 }
71
72 _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
73 int r;
74
75 assert_return(pid >= 0, -EINVAL);
76 assert_return(name, -EINVAL);
77
78 r = cg_pid_get_machine_name(pid, name);
79 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
80 }
81
82 _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
83 int r;
84
85 assert_return(pid >= 0, -EINVAL);
86 assert_return(slice, -EINVAL);
87
88 r = cg_pid_get_slice(pid, slice);
89 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
90 }
91
92 _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
93 int r;
94
95 assert_return(pid >= 0, -EINVAL);
96 assert_return(slice, -EINVAL);
97
98 r = cg_pid_get_user_slice(pid, slice);
99 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
100 }
101
102 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
103 int r;
104
105 assert_return(pid >= 0, -EINVAL);
106 assert_return(uid, -EINVAL);
107
108 r = cg_pid_get_owner_uid(pid, uid);
109 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
110 }
111
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
125 * that's easier and less ambiguous for people to grok. */
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
138 _public_ int sd_peer_get_session(int fd, char **session) {
139 struct ucred ucred = {};
140 int r;
141
142 assert_return(fd >= 0, -EBADF);
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
156 assert_return(fd >= 0, -EBADF);
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
170 assert_return(fd >= 0, -EBADF);
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
184 assert_return(fd >= 0, -EBADF);
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
198 assert_return(fd >= 0, -EBADF);
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
212 assert_return(fd >= 0, -EBADF);
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
222 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
223 struct ucred ucred;
224 int r;
225
226 assert_return(fd >= 0, -EBADF);
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
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
250 static int file_of_uid(uid_t uid, char **p) {
251
252 assert_return(uid_is_valid(uid), -EINVAL);
253 assert(p);
254
255 if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
256 return -ENOMEM;
257
258 return 0;
259 }
260
261 _public_ int sd_uid_get_state(uid_t uid, char**state) {
262 _cleanup_free_ char *p = NULL, *s = NULL;
263 int r;
264
265 assert_return(state, -EINVAL);
266
267 r = file_of_uid(uid, &p);
268 if (r < 0)
269 return r;
270
271 r = parse_env_file(NULL, p, "STATE", &s);
272 if (r == -ENOENT) {
273 r = free_and_strdup(&s, "offline");
274 if (r < 0)
275 return r;
276 } else if (r < 0)
277 return r;
278 else if (isempty(s))
279 return -EIO;
280
281 *state = TAKE_PTR(s);
282 return 0;
283 }
284
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
295 r = parse_env_file(NULL, p, "DISPLAY", &s);
296 if (r == -ENOENT)
297 return -ENODATA;
298 if (r < 0)
299 return r;
300 if (isempty(s))
301 return -ENODATA;
302
303 *session = TAKE_PTR(s);
304
305 return 0;
306 }
307
308 static 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
318 p = path_join("/run/systemd/seats", seat);
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
326 p = path_join("/run/systemd/seats", buf);
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 *filename = NULL, *content = NULL;
337 int r;
338
339 assert_return(uid_is_valid(uid), -EINVAL);
340
341 r = file_of_seat(seat, &filename);
342 if (r < 0)
343 return r;
344
345 r = parse_env_file(NULL, filename,
346 require_active ? "ACTIVE_UID" : "UIDS",
347 &content);
348 if (r == -ENOENT)
349 return 0;
350 if (r < 0)
351 return r;
352 if (isempty(content))
353 return 0;
354
355 char t[DECIMAL_STR_MAX(uid_t)];
356 xsprintf(t, UID_FMT, uid);
357
358 return string_contains_word(content, NULL, t);
359 }
360
361 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
362 _cleanup_free_ char *p = NULL, *s = NULL;
363 char **a;
364 int r;
365
366 assert(variable);
367
368 r = file_of_uid(uid, &p);
369 if (r < 0)
370 return r;
371
372 r = parse_env_file(NULL, p, variable, &s);
373 if (r == -ENOENT || (r >= 0 && isempty(s))) {
374 if (array)
375 *array = NULL;
376 return 0;
377 }
378 if (r < 0)
379 return r;
380
381 a = strv_split(s, NULL);
382 if (!a)
383 return -ENOMEM;
384
385 strv_uniq(a);
386 r = (int) strv_length(a);
387
388 if (array)
389 *array = a;
390 else
391 strv_free(a);
392
393 return r;
394 }
395
396 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
397 return uid_get_array(
398 uid,
399 require_active == 0 ? "ONLINE_SESSIONS" :
400 require_active > 0 ? "ACTIVE_SESSIONS" :
401 "SESSIONS",
402 sessions);
403 }
404
405 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
406 return uid_get_array(
407 uid,
408 require_active == 0 ? "ONLINE_SEATS" :
409 require_active > 0 ? "ACTIVE_SEATS" :
410 "SEATS",
411 seats);
412 }
413
414 static int file_of_session(const char *session, char **_p) {
415 char *p;
416 int r;
417
418 assert(_p);
419
420 if (session) {
421 if (!session_id_valid(session))
422 return -EINVAL;
423
424 p = path_join("/run/systemd/sessions", session);
425 } else {
426 _cleanup_free_ char *buf = NULL;
427
428 r = sd_pid_get_session(0, &buf);
429 if (r < 0)
430 return r;
431
432 p = path_join("/run/systemd/sessions", buf);
433 }
434
435 if (!p)
436 return -ENOMEM;
437
438 *_p = p;
439 return 0;
440 }
441
442 _public_ int sd_session_is_active(const char *session) {
443 _cleanup_free_ char *p = NULL, *s = NULL;
444 int r;
445
446 r = file_of_session(session, &p);
447 if (r < 0)
448 return r;
449
450 r = parse_env_file(NULL, p, "ACTIVE", &s);
451 if (r == -ENOENT)
452 return -ENXIO;
453 if (r < 0)
454 return r;
455 if (isempty(s))
456 return -EIO;
457
458 return parse_boolean(s);
459 }
460
461 _public_ int sd_session_is_remote(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, "REMOTE", &s);
470 if (r == -ENOENT)
471 return -ENXIO;
472 if (r < 0)
473 return r;
474 if (isempty(s))
475 return -ENODATA;
476
477 return parse_boolean(s);
478 }
479
480 _public_ int sd_session_get_state(const char *session, char **state) {
481 _cleanup_free_ char *p = NULL, *s = NULL;
482 int r;
483
484 assert_return(state, -EINVAL);
485
486 r = file_of_session(session, &p);
487 if (r < 0)
488 return r;
489
490 r = parse_env_file(NULL, p, "STATE", &s);
491 if (r == -ENOENT)
492 return -ENXIO;
493 if (r < 0)
494 return r;
495 if (isempty(s))
496 return -EIO;
497
498 *state = TAKE_PTR(s);
499
500 return 0;
501 }
502
503 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
504 int r;
505 _cleanup_free_ char *p = NULL, *s = NULL;
506
507 assert_return(uid, -EINVAL);
508
509 r = file_of_session(session, &p);
510 if (r < 0)
511 return r;
512
513 r = parse_env_file(NULL, p, "UID", &s);
514 if (r == -ENOENT)
515 return -ENXIO;
516 if (r < 0)
517 return r;
518 if (isempty(s))
519 return -EIO;
520
521 return parse_uid(s, uid);
522 }
523
524 static int session_get_string(const char *session, const char *field, char **value) {
525 _cleanup_free_ char *p = NULL, *s = NULL;
526 int r;
527
528 assert_return(value, -EINVAL);
529 assert(field);
530
531 r = file_of_session(session, &p);
532 if (r < 0)
533 return r;
534
535 r = parse_env_file(NULL, p, field, &s);
536 if (r == -ENOENT)
537 return -ENXIO;
538 if (r < 0)
539 return r;
540 if (isempty(s))
541 return -ENODATA;
542
543 *value = TAKE_PTR(s);
544 return 0;
545 }
546
547 _public_ int sd_session_get_seat(const char *session, char **seat) {
548 return session_get_string(session, "SEAT", seat);
549 }
550
551 _public_ int sd_session_get_tty(const char *session, char **tty) {
552 return session_get_string(session, "TTY", tty);
553 }
554
555 _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
556 _cleanup_free_ char *vtnr_string = NULL;
557 unsigned u;
558 int r;
559
560 assert_return(vtnr, -EINVAL);
561
562 r = session_get_string(session, "VTNR", &vtnr_string);
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
574 _public_ int sd_session_get_service(const char *session, char **service) {
575 return session_get_string(session, "SERVICE", service);
576 }
577
578 _public_ int sd_session_get_type(const char *session, char **type) {
579 return session_get_string(session, "TYPE", type);
580 }
581
582 _public_ int sd_session_get_class(const char *session, char **class) {
583 return session_get_string(session, "CLASS", class);
584 }
585
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
597 r = cunescape(escaped, 0, &t);
598 if (r < 0)
599 return r;
600
601 *desktop = t;
602 return 0;
603 }
604
605 _public_ int sd_session_get_display(const char *session, char **display) {
606 return session_get_string(session, "DISPLAY", display);
607 }
608
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
617 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
618 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
619 int r;
620
621 assert_return(session || uid, -EINVAL);
622
623 r = file_of_seat(seat, &p);
624 if (r < 0)
625 return r;
626
627 r = parse_env_file(NULL, p,
628 "ACTIVE", &s,
629 "ACTIVE_UID", &t);
630 if (r == -ENOENT)
631 return -ENXIO;
632 if (r < 0)
633 return r;
634
635 if (session && !s)
636 return -ENODATA;
637
638 if (uid && !t)
639 return -ENODATA;
640
641 if (uid && t) {
642 r = parse_uid(t, uid);
643 if (r < 0)
644 return r;
645 }
646
647 if (session && s)
648 *session = TAKE_PTR(s);
649
650 return 0;
651 }
652
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;
663 int r;
664
665 r = file_of_seat(seat, &fname);
666 if (r < 0)
667 return r;
668
669 r = parse_env_file(NULL, fname,
670 "SESSIONS", &session_line,
671 "UIDS", &uid_line);
672 if (r == -ENOENT)
673 return -ENXIO;
674 if (r < 0)
675 return r;
676
677 if (session_line) {
678 sessions = strv_split(session_line, NULL);
679 if (!sessions)
680 return -ENOMEM;
681
682 n_sessions = strv_length(sessions);
683 };
684
685 if (ret_uids && uid_line) {
686 uids = new(uid_t, n_sessions);
687 if (!uids)
688 return -ENOMEM;
689
690 size_t n = 0;
691 for (const char *p = uid_line;;) {
692 _cleanup_free_ char *word = NULL;
693
694 r = extract_first_word(&p, &word, NULL, 0);
695 if (r < 0)
696 return r;
697 if (r == 0)
698 break;
699
700 r = parse_uid(word, &uids[n++]);
701 if (r < 0)
702 return r;
703 }
704
705 if (n != n_sessions)
706 return -EUCLEAN;
707 }
708
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;
715
716 return n_sessions;
717 }
718
719 static int seat_get_can(const char *seat, const char *variable) {
720 _cleanup_free_ char *p = NULL, *s = NULL;
721 int r;
722
723 assert(variable);
724
725 r = file_of_seat(seat, &p);
726 if (r < 0)
727 return r;
728
729 r = parse_env_file(NULL, p,
730 variable, &s);
731 if (r == -ENOENT)
732 return -ENXIO;
733 if (r < 0)
734 return r;
735 if (isempty(s))
736 return -ENODATA;
737
738 return parse_boolean(s);
739 }
740
741 _public_ int sd_seat_can_multi_session(const char *seat) {
742 return true;
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
753 _public_ int sd_get_seats(char ***seats) {
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;
763 }
764
765 _public_ int sd_get_sessions(char ***sessions) {
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;
775 }
776
777 _public_ int sd_get_uids(uid_t **users) {
778 _cleanup_closedir_ DIR *d;
779 struct dirent *de;
780 int r = 0;
781 unsigned n = 0;
782 _cleanup_free_ uid_t *l = NULL;
783
784 d = opendir("/run/systemd/users/");
785 if (!d) {
786 if (errno == ENOENT) {
787 if (users)
788 *users = NULL;
789 return 0;
790 }
791 return -errno;
792 }
793
794 FOREACH_DIRENT_ALL(de, d, return -errno) {
795 int k;
796 uid_t uid;
797
798 dirent_ensure_type(d, de);
799
800 if (!dirent_is_file(de))
801 continue;
802
803 k = parse_uid(de->d_name, &uid);
804 if (k < 0)
805 continue;
806
807 if (users) {
808 if ((unsigned) r >= n) {
809 uid_t *t;
810
811 n = MAX(16, 2*r);
812 t = reallocarray(l, sizeof(uid_t), n);
813 if (!t)
814 return -ENOMEM;
815
816 l = t;
817 }
818
819 assert((unsigned) r < n);
820 l[r++] = uid;
821 } else
822 r++;
823 }
824
825 if (users)
826 *users = TAKE_PTR(l);
827
828 return r;
829 }
830
831 _public_ int sd_get_machine_names(char ***machines) {
832 _cleanup_strv_free_ char **l = NULL;
833 char **a, **b;
834 int r;
835
836 r = get_files_in_directory("/run/systemd/machines/", &l);
837 if (r == -ENOENT) {
838 if (machines)
839 *machines = NULL;
840 return 0;
841 }
842 if (r < 0)
843 return r;
844
845 if (l) {
846 r = 0;
847
848 /* Filter out the unit: symlinks */
849 for (a = b = l; *a; a++) {
850 if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
851 free(*a);
852 else {
853 *b = *a;
854 b++;
855 r++;
856 }
857 }
858
859 *b = NULL;
860 }
861
862 if (machines)
863 *machines = TAKE_PTR(l);
864
865 return r;
866 }
867
868 _public_ int sd_machine_get_class(const char *machine, char **class) {
869 _cleanup_free_ char *c = NULL;
870 const char *p;
871 int r;
872
873 assert_return(class, -EINVAL);
874
875 if (streq(machine, ".host")) {
876 c = strdup("host");
877 if (!c)
878 return -ENOMEM;
879 } else {
880 if (!machine_name_is_valid(machine))
881 return -EINVAL;
882
883 p = strjoina("/run/systemd/machines/", machine);
884 r = parse_env_file(NULL, p, "CLASS", &c);
885 if (r == -ENOENT)
886 return -ENXIO;
887 if (r < 0)
888 return r;
889 if (!c)
890 return -EIO;
891 }
892
893 *class = TAKE_PTR(c);
894 return 0;
895 }
896
897 _public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices) {
898 _cleanup_free_ char *netif_line = NULL;
899 const char *p;
900 int r;
901
902 assert_return(machine_name_is_valid(machine), -EINVAL);
903
904 p = strjoina("/run/systemd/machines/", machine);
905 r = parse_env_file(NULL, p, "NETIF", &netif_line);
906 if (r == -ENOENT)
907 return -ENXIO;
908 if (r < 0)
909 return r;
910 if (!netif_line) {
911 *ret_ifindices = NULL;
912 return 0;
913 }
914
915 _cleanup_strv_free_ char **tt = strv_split(netif_line, NULL);
916 if (!tt)
917 return -ENOMEM;
918
919 _cleanup_free_ int *ifindices = NULL;
920 if (ret_ifindices) {
921 ifindices = new(int, strv_length(tt));
922 if (!ifindices)
923 return -ENOMEM;
924 }
925
926 size_t n = 0;
927 for (size_t i = 0; tt[i]; i++) {
928 int ind;
929
930 ind = parse_ifindex(tt[i]);
931 if (ind < 0)
932 /* Return -EUCLEAN to distinguish from -EINVAL for invalid args */
933 return ind == -EINVAL ? -EUCLEAN : ind;
934
935 if (ret_ifindices)
936 ifindices[n] = ind;
937 n++;
938 }
939
940 if (ret_ifindices)
941 *ret_ifindices = TAKE_PTR(ifindices);
942
943 return n;
944 }
945
946 static int MONITOR_TO_FD(sd_login_monitor *m) {
947 return (int) (unsigned long) m - 1;
948 }
949
950 static sd_login_monitor* FD_TO_MONITOR(int fd) {
951 return (sd_login_monitor*) (unsigned long) (fd + 1);
952 }
953
954 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
955 _cleanup_close_ int fd = -1;
956 bool good = false;
957 int k;
958
959 assert_return(m, -EINVAL);
960
961 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
962 if (fd < 0)
963 return -errno;
964
965 if (!category || streq(category, "seat")) {
966 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
967 if (k < 0)
968 return -errno;
969
970 good = true;
971 }
972
973 if (!category || streq(category, "session")) {
974 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
975 if (k < 0)
976 return -errno;
977
978 good = true;
979 }
980
981 if (!category || streq(category, "uid")) {
982 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
983 if (k < 0)
984 return -errno;
985
986 good = true;
987 }
988
989 if (!category || streq(category, "machine")) {
990 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
991 if (k < 0)
992 return -errno;
993
994 good = true;
995 }
996
997 if (!good)
998 return -EINVAL;
999
1000 *m = FD_TO_MONITOR(TAKE_FD(fd));
1001 return 0;
1002 }
1003
1004 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1005 if (m)
1006 close_nointr(MONITOR_TO_FD(m));
1007
1008 return NULL;
1009 }
1010
1011 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
1012 int r;
1013
1014 assert_return(m, -EINVAL);
1015
1016 r = flush_fd(MONITOR_TO_FD(m));
1017 if (r < 0)
1018 return r;
1019
1020 return 0;
1021 }
1022
1023 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1024
1025 assert_return(m, -EINVAL);
1026
1027 return MONITOR_TO_FD(m);
1028 }
1029
1030 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1031
1032 assert_return(m, -EINVAL);
1033
1034 /* For now we will only return POLLIN here, since we don't
1035 * need anything else ever for inotify. However, let's have
1036 * this API to keep our options open should we later on need
1037 * it. */
1038 return POLLIN;
1039 }
1040
1041 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1042
1043 assert_return(m, -EINVAL);
1044 assert_return(timeout_usec, -EINVAL);
1045
1046 /* For now we will only return (uint64_t) -1, since we don't
1047 * need any timeout. However, let's have this API to keep our
1048 * options open should we later on need it. */
1049 *timeout_usec = (uint64_t) -1;
1050 return 0;
1051 }