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