chiark / gitweb /
Prep 235: Enabled sd_peer_get_session() and sd_peer_get_owner_uid() to try to work...
[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         return cg_pid_get_session(ucred.pid, session);
184 }
185
186 _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
187         struct ucred ucred;
188         int r;
189
190         assert_return(fd >= 0, -EBADF);
191         assert_return(uid, -EINVAL);
192
193         r = getpeercred(fd, &ucred);
194         if (r < 0)
195                 return r;
196
197         return cg_pid_get_owner_uid(ucred.pid, uid);
198 }
199
200 _public_ int sd_peer_get_unit(int fd, char **unit) {
201         struct ucred ucred;
202         int r;
203
204         assert_return(fd >= 0, -EBADF);
205         assert_return(unit, -EINVAL);
206
207         r = getpeercred(fd, &ucred);
208         if (r < 0)
209                 return r;
210
211 #if 0 /// elogind does not support systemd units
212         return cg_pid_get_unit(ucred.pid, unit);
213 #else
214         return -ESRCH;
215 #endif // 0
216 }
217
218 _public_ int sd_peer_get_user_unit(int fd, char **unit) {
219         struct ucred ucred;
220         int r;
221
222         assert_return(fd >= 0, -EBADF);
223         assert_return(unit, -EINVAL);
224
225         r = getpeercred(fd, &ucred);
226         if (r < 0)
227                 return r;
228
229 #if 0 /// elogind does not support systemd units
230         return cg_pid_get_user_unit(ucred.pid, unit);
231 #else
232         return -ESRCH;
233 #endif // 0
234 }
235
236 _public_ int sd_peer_get_machine_name(int fd, char **machine) {
237         struct ucred ucred;
238         int r;
239
240         assert_return(fd >= 0, -EBADF);
241         assert_return(machine, -EINVAL);
242
243         r = getpeercred(fd, &ucred);
244         if (r < 0)
245                 return r;
246
247 #if 0 /// elogind does not support systemd units
248         return cg_pid_get_machine_name(ucred.pid, machine);
249 #else
250         return -ESRCH;
251 #endif // 0
252 }
253
254 _public_ int sd_peer_get_slice(int fd, char **slice) {
255         struct ucred ucred;
256         int r;
257
258         assert_return(fd >= 0, -EBADF);
259         assert_return(slice, -EINVAL);
260
261         r = getpeercred(fd, &ucred);
262         if (r < 0)
263                 return r;
264
265         return cg_pid_get_slice(ucred.pid, slice);
266 }
267
268 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
269         struct ucred ucred;
270         int r;
271
272         assert_return(fd >= 0, -EBADF);
273         assert_return(slice, -EINVAL);
274
275         r = getpeercred(fd, &ucred);
276         if (r < 0)
277                 return r;
278
279         return cg_pid_get_user_slice(ucred.pid, slice);
280 }
281
282 _public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
283         struct ucred ucred;
284         int r;
285
286         assert_return(fd >= 0, -EBADF);
287         assert_return(cgroup, -EINVAL);
288
289         r = getpeercred(fd, &ucred);
290         if (r < 0)
291                 return r;
292
293         return sd_pid_get_cgroup(ucred.pid, cgroup);
294 }
295
296 static int file_of_uid(uid_t uid, char **p) {
297
298         assert_return(uid_is_valid(uid), -EINVAL);
299         assert(p);
300
301         if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
302                 return -ENOMEM;
303
304         return 0;
305 }
306
307 _public_ int sd_uid_get_state(uid_t uid, char**state) {
308         _cleanup_free_ char *p = NULL;
309         char *s = NULL;
310         int r;
311
312         assert_return(state, -EINVAL);
313
314         r = file_of_uid(uid, &p);
315         if (r < 0)
316                 return r;
317
318         r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
319         if (r == -ENOENT) {
320                 free(s);
321                 s = strdup("offline");
322                 if (!s)
323                         return -ENOMEM;
324
325         }
326         else if (r < 0) {
327                 free(s);
328                 return r;
329         }
330         if (isempty(s)) {
331                 free(s);
332                 return -EIO;
333         }
334
335         *state = s;
336         return 0;
337 }
338
339 _public_ int sd_uid_get_display(uid_t uid, char **session) {
340         _cleanup_free_ char *p = NULL, *s = NULL;
341         int r;
342
343         assert_return(session, -EINVAL);
344
345         r = file_of_uid(uid, &p);
346         if (r < 0)
347                 return r;
348
349         r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
350         if (r == -ENOENT)
351                 return -ENODATA;
352         if (r < 0)
353                 return r;
354         if (isempty(s))
355                 return -ENODATA;
356
357         *session = s;
358         s = NULL;
359
360         return 0;
361 }
362
363 static int file_of_seat(const char *seat, char **_p) {
364         char *p;
365         int r;
366
367         assert(_p);
368
369         if (seat) {
370                 if (!filename_is_valid(seat))
371                         return -EINVAL;
372
373                 p = strappend("/run/systemd/seats/", seat);
374         } else {
375                 _cleanup_free_ char *buf = NULL;
376
377                 r = sd_session_get_seat(NULL, &buf);
378                 if (r < 0)
379                         return r;
380
381                 p = strappend("/run/systemd/seats/", buf);
382         }
383
384         if (!p)
385                 return -ENOMEM;
386
387         *_p = p;
388         p = NULL;
389         return 0;
390 }
391
392 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
393         _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
394         size_t l;
395         int r;
396         const char *word, *variable, *state;
397
398         assert_return(uid_is_valid(uid), -EINVAL);
399
400         r = file_of_seat(seat, &p);
401         if (r < 0)
402                 return r;
403
404         variable = require_active ? "ACTIVE_UID" : "UIDS";
405
406         r = parse_env_file(p, NEWLINE, variable, &s, NULL);
407         if (r == -ENOENT)
408                 return 0;
409         if (r < 0)
410                 return r;
411         if (isempty(s))
412                 return 0;
413
414         if (asprintf(&t, UID_FMT, uid) < 0)
415                 return -ENOMEM;
416
417         FOREACH_WORD(word, l, s, state)
418                 if (strneq(t, word, l))
419                         return 1;
420
421         return 0;
422 }
423
424 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
425         _cleanup_free_ char *p = NULL, *s = NULL;
426         char **a;
427         int r;
428
429         assert(variable);
430
431         r = file_of_uid(uid, &p);
432         if (r < 0)
433                 return r;
434
435         r = parse_env_file(p, NEWLINE, variable, &s, NULL);
436         if (r == -ENOENT || (r >= 0 && isempty(s))) {
437                 if (array)
438                         *array = NULL;
439                 return 0;
440         }
441         if (r < 0)
442                 return r;
443
444         a = strv_split(s, " ");
445         if (!a)
446                 return -ENOMEM;
447
448         strv_uniq(a);
449         r = strv_length(a);
450
451         if (array)
452                 *array = a;
453         else
454                 strv_free(a);
455
456         return r;
457 }
458
459 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
460         return uid_get_array(
461                         uid,
462                         require_active == 0 ? "ONLINE_SESSIONS" :
463                         require_active > 0  ? "ACTIVE_SESSIONS" :
464                                               "SESSIONS",
465                         sessions);
466 }
467
468 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
469         return uid_get_array(
470                         uid,
471                         require_active == 0 ? "ONLINE_SEATS" :
472                         require_active > 0  ? "ACTIVE_SEATS" :
473                                               "SEATS",
474                         seats);
475 }
476
477 static int file_of_session(const char *session, char **_p) {
478         char *p;
479         int r;
480
481         assert(_p);
482
483         if (session) {
484                 if (!session_id_valid(session))
485                         return -EINVAL;
486
487                 p = strappend("/run/systemd/sessions/", session);
488         } else {
489                 _cleanup_free_ char *buf = NULL;
490
491                 r = sd_pid_get_session(0, &buf);
492                 if (r < 0)
493                         return r;
494
495                 p = strappend("/run/systemd/sessions/", buf);
496         }
497
498         if (!p)
499                 return -ENOMEM;
500
501         *_p = p;
502         return 0;
503 }
504
505 _public_ int sd_session_is_active(const char *session) {
506         _cleanup_free_ char *p = NULL, *s = NULL;
507         int r;
508
509         r = file_of_session(session, &p);
510         if (r < 0)
511                 return r;
512
513         r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
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_boolean(s);
522 }
523
524 _public_ int sd_session_is_remote(const char *session) {
525         _cleanup_free_ char *p = NULL, *s = NULL;
526         int r;
527
528         r = file_of_session(session, &p);
529         if (r < 0)
530                 return r;
531
532         r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
533         if (r == -ENOENT)
534                 return -ENXIO;
535         if (r < 0)
536                 return r;
537         if (isempty(s))
538                 return -ENODATA;
539
540         return parse_boolean(s);
541 }
542
543 _public_ int sd_session_get_state(const char *session, char **state) {
544         _cleanup_free_ char *p = NULL, *s = NULL;
545         int r;
546
547         assert_return(state, -EINVAL);
548
549         r = file_of_session(session, &p);
550         if (r < 0)
551                 return r;
552
553         r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
554         if (r == -ENOENT)
555                 return -ENXIO;
556         if (r < 0)
557                 return r;
558         if (isempty(s))
559                 return -EIO;
560
561         *state = s;
562         s = NULL;
563
564         return 0;
565 }
566
567 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
568         int r;
569         _cleanup_free_ char *p = NULL, *s = NULL;
570
571         assert_return(uid, -EINVAL);
572
573         r = file_of_session(session, &p);
574         if (r < 0)
575                 return r;
576
577         r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
578         if (r == -ENOENT)
579                 return -ENXIO;
580         if (r < 0)
581                 return r;
582         if (isempty(s))
583                 return -EIO;
584
585         return parse_uid(s, uid);
586 }
587
588 static int session_get_string(const char *session, const char *field, char **value) {
589         _cleanup_free_ char *p = NULL, *s = NULL;
590         int r;
591
592         assert_return(value, -EINVAL);
593         assert(field);
594
595         r = file_of_session(session, &p);
596         if (r < 0)
597                 return r;
598
599         r = parse_env_file(p, NEWLINE, field, &s, NULL);
600         if (r == -ENOENT)
601                 return -ENXIO;
602         if (r < 0)
603                 return r;
604         if (isempty(s))
605                 return -ENODATA;
606
607         *value = s;
608         s = NULL;
609         return 0;
610 }
611
612 _public_ int sd_session_get_seat(const char *session, char **seat) {
613         return session_get_string(session, "SEAT", seat);
614 }
615
616 _public_ int sd_session_get_tty(const char *session, char **tty) {
617         return session_get_string(session, "TTY", tty);
618 }
619
620 _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
621         _cleanup_free_ char *vtnr_string = NULL;
622         unsigned u;
623         int r;
624
625         assert_return(vtnr, -EINVAL);
626
627         r = session_get_string(session, "VTNR", &vtnr_string);
628         if (r < 0)
629                 return r;
630
631         r = safe_atou(vtnr_string, &u);
632         if (r < 0)
633                 return r;
634
635         *vtnr = u;
636         return 0;
637 }
638
639 _public_ int sd_session_get_service(const char *session, char **service) {
640         return session_get_string(session, "SERVICE", service);
641 }
642
643 _public_ int sd_session_get_type(const char *session, char **type) {
644         return session_get_string(session, "TYPE", type);
645 }
646
647 _public_ int sd_session_get_class(const char *session, char **class) {
648         return session_get_string(session, "CLASS", class);
649 }
650
651 _public_ int sd_session_get_desktop(const char *session, char **desktop) {
652         _cleanup_free_ char *escaped = NULL;
653         char *t;
654         int r;
655
656         assert_return(desktop, -EINVAL);
657
658         r = session_get_string(session, "DESKTOP", &escaped);
659         if (r < 0)
660                 return r;
661
662         r = cunescape(escaped, 0, &t);
663         if (r < 0)
664                 return r;
665
666         *desktop = t;
667         return 0;
668 }
669
670 _public_ int sd_session_get_display(const char *session, char **display) {
671         return session_get_string(session, "DISPLAY", display);
672 }
673
674 _public_ int sd_session_get_remote_user(const char *session, char **remote_user) {
675         return session_get_string(session, "REMOTE_USER", remote_user);
676 }
677
678 _public_ int sd_session_get_remote_host(const char *session, char **remote_host) {
679         return session_get_string(session, "REMOTE_HOST", remote_host);
680 }
681
682 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
683         _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
684         int r;
685
686         assert_return(session || uid, -EINVAL);
687
688         r = file_of_seat(seat, &p);
689         if (r < 0)
690                 return r;
691
692         r = parse_env_file(p, NEWLINE,
693                            "ACTIVE", &s,
694                            "ACTIVE_UID", &t,
695                            NULL);
696         if (r == -ENOENT)
697                 return -ENXIO;
698         if (r < 0)
699                 return r;
700
701         if (session && !s)
702                 return -ENODATA;
703
704         if (uid && !t)
705                 return -ENODATA;
706
707         if (uid && t) {
708                 r = parse_uid(t, uid);
709                 if (r < 0)
710                         return r;
711         }
712
713         if (session && s) {
714                 *session = s;
715                 s = NULL;
716         }
717
718         return 0;
719 }
720
721 _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
722         _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
723         _cleanup_strv_free_ char **a = NULL;
724         _cleanup_free_ uid_t *b = NULL;
725         unsigned n = 0;
726         int r;
727
728         r = file_of_seat(seat, &p);
729         if (r < 0)
730                 return r;
731
732         r = parse_env_file(p, NEWLINE,
733                            "SESSIONS", &s,
734                            "UIDS", &t,
735                            NULL);
736         if (r == -ENOENT)
737                 return -ENXIO;
738         if (r < 0)
739                 return r;
740
741         if (s) {
742                 a = strv_split(s, " ");
743                 if (!a)
744                         return -ENOMEM;
745         }
746
747         if (uids && t) {
748                 const char *word, *state;
749                 size_t l;
750
751                 FOREACH_WORD(word, l, t, state)
752                         n++;
753
754                 if (n > 0) {
755                         unsigned i = 0;
756
757                         b = new(uid_t, n);
758                         if (!b)
759                                 return -ENOMEM;
760
761                         FOREACH_WORD(word, l, t, state) {
762                                 _cleanup_free_ char *k = NULL;
763
764                                 k = strndup(word, l);
765                                 if (!k)
766                                         return -ENOMEM;
767
768                                 r = parse_uid(k, b + i);
769                                 if (r < 0)
770                                         return r;
771
772                                 i++;
773                         }
774                 }
775         }
776
777         r = strv_length(a);
778
779         if (sessions) {
780                 *sessions = a;
781                 a = NULL;
782         }
783
784         if (uids) {
785                 *uids = b;
786                 b = NULL;
787         }
788
789         if (n_uids)
790                 *n_uids = n;
791
792         return r;
793 }
794
795 static int seat_get_can(const char *seat, const char *variable) {
796         _cleanup_free_ char *p = NULL, *s = NULL;
797         int r;
798
799         assert(variable);
800
801         r = file_of_seat(seat, &p);
802         if (r < 0)
803                 return r;
804
805         r = parse_env_file(p, NEWLINE,
806                            variable, &s,
807                            NULL);
808         if (r == -ENOENT)
809                 return -ENXIO;
810         if (r < 0)
811                 return r;
812         if (isempty(s))
813                 return -ENODATA;
814
815         return parse_boolean(s);
816 }
817
818 _public_ int sd_seat_can_multi_session(const char *seat) {
819         return seat_get_can(seat, "CAN_MULTI_SESSION");
820 }
821
822 _public_ int sd_seat_can_tty(const char *seat) {
823         return seat_get_can(seat, "CAN_TTY");
824 }
825
826 _public_ int sd_seat_can_graphical(const char *seat) {
827         return seat_get_can(seat, "CAN_GRAPHICAL");
828 }
829
830 _public_ int sd_get_seats(char ***seats) {
831         int r;
832
833         r = get_files_in_directory("/run/systemd/seats/", seats);
834         if (r == -ENOENT) {
835                 if (seats)
836                         *seats = NULL;
837                 return 0;
838         }
839         return r;
840 }
841
842 _public_ int sd_get_sessions(char ***sessions) {
843         int r;
844
845         r = get_files_in_directory("/run/systemd/sessions/", sessions);
846         if (r == -ENOENT) {
847                 if (sessions)
848                         *sessions = NULL;
849                 return 0;
850         }
851         return r;
852 }
853
854 _public_ int sd_get_uids(uid_t **users) {
855         _cleanup_closedir_ DIR *d;
856         struct dirent *de;
857         int r = 0;
858         unsigned n = 0;
859         _cleanup_free_ uid_t *l = NULL;
860
861         d = opendir("/run/systemd/users/");
862         if (!d) {
863                 if (errno == ENOENT) {
864                         if (users)
865                                 *users = NULL;
866                         return 0;
867                 }
868                 return -errno;
869         }
870
871         FOREACH_DIRENT_ALL(de, d, return -errno) {
872                 int k;
873                 uid_t uid;
874
875                 dirent_ensure_type(d, de);
876
877                 if (!dirent_is_file(de))
878                         continue;
879
880                 k = parse_uid(de->d_name, &uid);
881                 if (k < 0)
882                         continue;
883
884                 if (users) {
885                         if ((unsigned) r >= n) {
886                                 uid_t *t;
887
888                                 n = MAX(16, 2*r);
889                                 t = realloc(l, sizeof(uid_t) * n);
890                                 if (!t)
891                                         return -ENOMEM;
892
893                                 l = t;
894                         }
895
896                         assert((unsigned) r < n);
897                         l[r++] = uid;
898                 } else
899                         r++;
900         }
901
902         if (users) {
903                 *users = l;
904                 l = NULL;
905         }
906
907         return r;
908 }
909
910 _public_ int sd_get_machine_names(char ***machines) {
911         _cleanup_strv_free_ char **l = NULL;
912         char **a, **b;
913         int r;
914
915         r = get_files_in_directory("/run/systemd/machines/", &l);
916         if (r == -ENOENT) {
917                 if (machines)
918                         *machines = NULL;
919                 return 0;
920         }
921         if (r < 0)
922                 return r;
923
924         if (l) {
925                 r = 0;
926
927                 /* Filter out the unit: symlinks */
928                 for (a = b = l; *a; a++) {
929                         if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
930                                 free(*a);
931                         else {
932                                 *b = *a;
933                                 b++;
934                                 r++;
935                         }
936                 }
937
938                 *b = NULL;
939         }
940
941         if (machines) {
942                 *machines = l;
943                 l = NULL;
944         }
945         return r;
946 }
947
948 _public_ int sd_machine_get_class(const char *machine, char **class) {
949         _cleanup_free_ char *c = NULL;
950         const char *p;
951         int r;
952
953         assert_return(machine_name_is_valid(machine), -EINVAL);
954         assert_return(class, -EINVAL);
955
956         p = strjoina("/run/systemd/machines/", machine);
957         r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
958         if (r == -ENOENT)
959                 return -ENXIO;
960         if (r < 0)
961                 return r;
962         if (!c)
963                 return -EIO;
964
965         *class = c;
966         c = NULL;
967
968         return 0;
969 }
970
971 _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
972         _cleanup_free_ char *netif = NULL;
973         size_t l, allocated = 0, nr = 0;
974         int *ni = NULL;
975         const char *p, *word, *state;
976         int r;
977
978         assert_return(machine_name_is_valid(machine), -EINVAL);
979         assert_return(ifindices, -EINVAL);
980
981         p = strjoina("/run/systemd/machines/", machine);
982         r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
983         if (r == -ENOENT)
984                 return -ENXIO;
985         if (r < 0)
986                 return r;
987         if (!netif) {
988                 *ifindices = NULL;
989                 return 0;
990         }
991
992         FOREACH_WORD(word, l, netif, state) {
993                 char buf[l+1];
994                 int ifi;
995
996                 *(char*) (mempcpy(buf, word, l)) = 0;
997
998                 if (parse_ifindex(buf, &ifi) < 0)
999                         continue;
1000
1001                 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
1002                         free(ni);
1003                         return -ENOMEM;
1004                 }
1005
1006                 ni[nr++] = ifi;
1007         }
1008
1009         *ifindices = ni;
1010         return nr;
1011 }
1012
1013 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
1014         return (int) (unsigned long) m - 1;
1015 }
1016
1017 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
1018         return (sd_login_monitor*) (unsigned long) (fd + 1);
1019 }
1020
1021 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
1022         int fd, k;
1023         bool good = false;
1024
1025         assert_return(m, -EINVAL);
1026
1027         fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1028         if (fd < 0)
1029                 return -errno;
1030
1031         if (!category || streq(category, "seat")) {
1032                 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
1033                 if (k < 0) {
1034                         safe_close(fd);
1035                         return -errno;
1036                 }
1037
1038                 good = true;
1039         }
1040
1041         if (!category || streq(category, "session")) {
1042                 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
1043                 if (k < 0) {
1044                         safe_close(fd);
1045                         return -errno;
1046                 }
1047
1048                 good = true;
1049         }
1050
1051         if (!category || streq(category, "uid")) {
1052                 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
1053                 if (k < 0) {
1054                         safe_close(fd);
1055                         return -errno;
1056                 }
1057
1058                 good = true;
1059         }
1060
1061         if (!category || streq(category, "machine")) {
1062                 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
1063                 if (k < 0) {
1064                         safe_close(fd);
1065                         return -errno;
1066                 }
1067
1068                 good = true;
1069         }
1070
1071         if (!good) {
1072                 close_nointr(fd);
1073                 return -EINVAL;
1074         }
1075
1076         *m = FD_TO_MONITOR(fd);
1077         return 0;
1078 }
1079
1080 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1081         int fd;
1082
1083         if (!m)
1084                 return NULL;
1085
1086         fd = MONITOR_TO_FD(m);
1087         close_nointr(fd);
1088
1089         return NULL;
1090 }
1091
1092 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
1093
1094         assert_return(m, -EINVAL);
1095
1096         return flush_fd(MONITOR_TO_FD(m));
1097 }
1098
1099 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1100
1101         assert_return(m, -EINVAL);
1102
1103         return MONITOR_TO_FD(m);
1104 }
1105
1106 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1107
1108         assert_return(m, -EINVAL);
1109
1110         /* For now we will only return POLLIN here, since we don't
1111          * need anything else ever for inotify.  However, let's have
1112          * this API to keep our options open should we later on need
1113          * it. */
1114         return POLLIN;
1115 }
1116
1117 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1118
1119         assert_return(m, -EINVAL);
1120         assert_return(timeout_usec, -EINVAL);
1121
1122         /* For now we will only return (uint64_t) -1, since we don't
1123          * need any timeout. However, let's have this API to keep our
1124          * options open should we later on need it. */
1125         *timeout_usec = (uint64_t) -1;
1126         return 0;
1127 }