chiark / gitweb /
Various fixes for typos found by lintian (#3705)
[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 "formats-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
60         assert_return(pid >= 0, -EINVAL);
61         assert_return(session, -EINVAL);
62
63         return cg_pid_get_session(pid, session);
64 }
65
66 _public_ int sd_pid_get_unit(pid_t pid, char **unit) {
67
68         assert_return(pid >= 0, -EINVAL);
69         assert_return(unit, -EINVAL);
70
71 #if 0 /// elogind does not support systemd units
72         return cg_pid_get_unit(pid, unit);
73 #else
74         return -ESRCH;
75 #endif // 0
76 }
77
78 _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
79
80         assert_return(pid >= 0, -EINVAL);
81         assert_return(unit, -EINVAL);
82
83 #if 0 /// elogind does not support systemd units
84         return cg_pid_get_user_unit(pid, unit);
85 #else
86         return -ESRCH;
87 #endif // 0
88 }
89
90 _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
91
92         assert_return(pid >= 0, -EINVAL);
93         assert_return(name, -EINVAL);
94
95 #if 0 /// elogind does not support systemd units
96         return cg_pid_get_machine_name(pid, name);
97 #else
98         return -ESRCH;
99 #endif // 0
100 }
101
102 _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
103
104         assert_return(pid >= 0, -EINVAL);
105         assert_return(slice, -EINVAL);
106
107 #if 0 /// elogind does not support systemd slices
108         return cg_pid_get_slice(pid, slice);
109 #else
110         return -ESRCH;
111 #endif // 0
112 }
113
114 _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
115
116         assert_return(pid >= 0, -EINVAL);
117         assert_return(slice, -EINVAL);
118
119 #if 0 /// elogind does not support systemd slices
120         return cg_pid_get_user_slice(pid, slice);
121 #else
122         return -ESRCH;
123 #endif // 0
124 }
125
126 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
127
128         assert_return(pid >= 0, -EINVAL);
129         assert_return(uid, -EINVAL);
130
131 #if 0 /// elogind does not support systemd slices
132         return cg_pid_get_owner_uid(pid, uid);
133 #else
134         return -ESRCH;
135 #endif // 0
136 }
137
138 _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
139         char *c;
140         int r;
141
142         assert_return(pid >= 0, -EINVAL);
143         assert_return(cgroup, -EINVAL);
144
145         r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
146         if (r < 0)
147                 return r;
148
149         /* The internal APIs return the empty string for the root
150          * cgroup, let's return the "/" in the public APIs instead, as
151          * that's easier and less ambiguous for people to grok. */
152         if (isempty(c)) {
153                 free(c);
154                 c = strdup("/");
155                 if (!c)
156                         return -ENOMEM;
157
158         }
159
160         *cgroup = c;
161         return 0;
162 }
163
164 _public_ int sd_peer_get_session(int fd, char **session) {
165         struct ucred ucred = {};
166         int r;
167
168         assert_return(fd >= 0, -EBADF);
169         assert_return(session, -EINVAL);
170
171         r = getpeercred(fd, &ucred);
172         if (r < 0)
173                 return r;
174
175 #if 0 /// elogind does not support systemd scopes
176         return cg_pid_get_session(ucred.pid, session);
177 #else
178         return -ESRCH;
179 #endif // 0
180 }
181
182 _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
183         struct ucred ucred;
184         int r;
185
186         assert_return(fd >= 0, -EBADF);
187         assert_return(uid, -EINVAL);
188
189         r = getpeercred(fd, &ucred);
190         if (r < 0)
191                 return r;
192
193 #if 0 /// elogind does not support systemd units
194         return cg_pid_get_owner_uid(ucred.pid, uid);
195 #else
196         return -ESRCH;
197 #endif // 0
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 #if 0 /// elogind does not support systemd slices
266         return cg_pid_get_slice(ucred.pid, slice);
267 #else
268         return -ESRCH;
269 #endif // 0
270 }
271
272 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
273         struct ucred ucred;
274         int r;
275
276         assert_return(fd >= 0, -EBADF);
277         assert_return(slice, -EINVAL);
278
279         r = getpeercred(fd, &ucred);
280         if (r < 0)
281                 return r;
282
283 #if 0 /// elogind does not support systemd slices
284         return cg_pid_get_user_slice(ucred.pid, slice);
285 #else
286         return -ESRCH;
287 #endif // 0
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         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                            "ACTIVE_SESSIONS", &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                                         continue;
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         return get_files_in_directory("/run/systemd/seats/", seats);
840 }
841
842 _public_ int sd_get_sessions(char ***sessions) {
843         return get_files_in_directory("/run/systemd/sessions/", sessions);
844 }
845
846 _public_ int sd_get_uids(uid_t **users) {
847         _cleanup_closedir_ DIR *d;
848         int r = 0;
849         unsigned n = 0;
850         _cleanup_free_ uid_t *l = NULL;
851
852         d = opendir("/run/systemd/users/");
853         if (!d)
854                 return -errno;
855
856         for (;;) {
857                 struct dirent *de;
858                 int k;
859                 uid_t uid;
860
861                 errno = 0;
862                 de = readdir(d);
863                 if (!de && errno > 0)
864                         return -errno;
865
866                 if (!de)
867                         break;
868
869                 dirent_ensure_type(d, de);
870
871                 if (!dirent_is_file(de))
872                         continue;
873
874                 k = parse_uid(de->d_name, &uid);
875                 if (k < 0)
876                         continue;
877
878                 if (users) {
879                         if ((unsigned) r >= n) {
880                                 uid_t *t;
881
882                                 n = MAX(16, 2*r);
883                                 t = realloc(l, sizeof(uid_t) * n);
884                                 if (!t)
885                                         return -ENOMEM;
886
887                                 l = t;
888                         }
889
890                         assert((unsigned) r < n);
891                         l[r++] = uid;
892                 } else
893                         r++;
894         }
895
896         if (users) {
897                 *users = l;
898                 l = NULL;
899         }
900
901         return r;
902 }
903
904 _public_ int sd_get_machine_names(char ***machines) {
905         char **l = NULL, **a, **b;
906         int r;
907
908         assert_return(machines, -EINVAL);
909
910         r = get_files_in_directory("/run/systemd/machines/", &l);
911         if (r < 0)
912                 return r;
913
914         if (l) {
915                 r = 0;
916
917                 /* Filter out the unit: symlinks */
918                 for (a = l, b = l; *a; a++) {
919                         if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
920                                 free(*a);
921                         else {
922                                 *b = *a;
923                                 b++;
924                                 r++;
925                         }
926                 }
927
928                 *b = NULL;
929         }
930
931         *machines = l;
932         return r;
933 }
934
935 _public_ int sd_machine_get_class(const char *machine, char **class) {
936         _cleanup_free_ char *c = NULL;
937         const char *p;
938         int r;
939
940         assert_return(machine_name_is_valid(machine), -EINVAL);
941         assert_return(class, -EINVAL);
942
943         p = strjoina("/run/systemd/machines/", machine);
944         r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
945         if (r == -ENOENT)
946                 return -ENXIO;
947         if (r < 0)
948                 return r;
949         if (!c)
950                 return -EIO;
951
952         *class = c;
953         c = NULL;
954
955         return 0;
956 }
957
958 _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
959         _cleanup_free_ char *netif = NULL;
960         size_t l, allocated = 0, nr = 0;
961         int *ni = NULL;
962         const char *p, *word, *state;
963         int r;
964
965         assert_return(machine_name_is_valid(machine), -EINVAL);
966         assert_return(ifindices, -EINVAL);
967
968         p = strjoina("/run/systemd/machines/", machine);
969         r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
970         if (r == -ENOENT)
971                 return -ENXIO;
972         if (r < 0)
973                 return r;
974         if (!netif) {
975                 *ifindices = NULL;
976                 return 0;
977         }
978
979         FOREACH_WORD(word, l, netif, state) {
980                 char buf[l+1];
981                 int ifi;
982
983                 *(char*) (mempcpy(buf, word, l)) = 0;
984
985                 if (parse_ifindex(buf, &ifi) < 0)
986                         continue;
987
988                 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
989                         free(ni);
990                         return -ENOMEM;
991                 }
992
993                 ni[nr++] = ifi;
994         }
995
996         *ifindices = ni;
997         return nr;
998 }
999
1000 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
1001         return (int) (unsigned long) m - 1;
1002 }
1003
1004 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
1005         return (sd_login_monitor*) (unsigned long) (fd + 1);
1006 }
1007
1008 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
1009         int fd, k;
1010         bool good = false;
1011
1012         assert_return(m, -EINVAL);
1013
1014         fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1015         if (fd < 0)
1016                 return -errno;
1017
1018         if (!category || streq(category, "seat")) {
1019                 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
1020                 if (k < 0) {
1021                         safe_close(fd);
1022                         return -errno;
1023                 }
1024
1025                 good = true;
1026         }
1027
1028         if (!category || streq(category, "session")) {
1029                 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
1030                 if (k < 0) {
1031                         safe_close(fd);
1032                         return -errno;
1033                 }
1034
1035                 good = true;
1036         }
1037
1038         if (!category || streq(category, "uid")) {
1039                 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
1040                 if (k < 0) {
1041                         safe_close(fd);
1042                         return -errno;
1043                 }
1044
1045                 good = true;
1046         }
1047
1048         if (!category || streq(category, "machine")) {
1049                 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
1050                 if (k < 0) {
1051                         safe_close(fd);
1052                         return -errno;
1053                 }
1054
1055                 good = true;
1056         }
1057
1058         if (!good) {
1059                 close_nointr(fd);
1060                 return -EINVAL;
1061         }
1062
1063         *m = FD_TO_MONITOR(fd);
1064         return 0;
1065 }
1066
1067 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1068         int fd;
1069
1070         if (!m)
1071                 return NULL;
1072
1073         fd = MONITOR_TO_FD(m);
1074         close_nointr(fd);
1075
1076         return NULL;
1077 }
1078
1079 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
1080
1081         assert_return(m, -EINVAL);
1082
1083         return flush_fd(MONITOR_TO_FD(m));
1084 }
1085
1086 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1087
1088         assert_return(m, -EINVAL);
1089
1090         return MONITOR_TO_FD(m);
1091 }
1092
1093 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1094
1095         assert_return(m, -EINVAL);
1096
1097         /* For now we will only return POLLIN here, since we don't
1098          * need anything else ever for inotify.  However, let's have
1099          * this API to keep our options open should we later on need
1100          * it. */
1101         return POLLIN;
1102 }
1103
1104 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1105
1106         assert_return(m, -EINVAL);
1107         assert_return(timeout_usec, -EINVAL);
1108
1109         /* For now we will only return (uint64_t) -1, since we don't
1110          * need any timeout. However, let's have this API to keep our
1111          * options open should we later on need it. */
1112         *timeout_usec = (uint64_t) -1;
1113         return 0;
1114 }