2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
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.
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.
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/>.
23 #include <sys/inotify.h>
28 #include "alloc-util.h"
29 #include "cgroup-util.h"
30 #include "dirent-util.h"
34 #include "format-util.h"
36 #include "hostname-util.h"
38 #include "login-util.h"
40 #include "parse-util.h"
41 #include "path-util.h"
42 #include "socket-util.h"
43 #include "string-util.h"
45 #include "user-util.h"
50 * invalid input parameters → -EINVAL
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
58 _public_ int sd_pid_get_session(pid_t pid, char **session) {
61 assert_return(pid >= 0, -EINVAL);
62 assert_return(session, -EINVAL);
64 r = cg_pid_get_session(pid, session);
65 return r == -ENXIO ? -ENODATA : r;
68 _public_ int sd_pid_get_unit(pid_t pid, char **unit) {
69 #if 0 /// UNNEEDED by elogind
73 assert_return(pid >= 0, -EINVAL);
74 assert_return(unit, -EINVAL);
76 #if 0 /// elogind does not support systemd units
77 r = cg_pid_get_unit(pid, unit);
78 return r == -ENXIO ? -ENODATA : r;
84 _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
85 #if 0 /// UNNEEDED by elogind
89 assert_return(pid >= 0, -EINVAL);
90 assert_return(unit, -EINVAL);
92 #if 0 /// elogind does not support systemd units
93 r = cg_pid_get_user_unit(pid, unit);
94 return r == -ENXIO ? -ENODATA : r;
100 _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
102 assert_return(pid >= 0, -EINVAL);
103 assert_return(name, -EINVAL);
105 #if 0 /// elogind does not support systemd units
106 return cg_pid_get_machine_name(pid, name);
112 _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
114 assert_return(pid >= 0, -EINVAL);
115 assert_return(slice, -EINVAL);
117 #if 0 /// elogind does not support systemd slices
118 return cg_pid_get_slice(pid, slice);
124 _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
126 assert_return(pid >= 0, -EINVAL);
127 assert_return(slice, -EINVAL);
129 #if 0 /// elogind does not support systemd slices
130 return cg_pid_get_user_slice(pid, slice);
136 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
138 assert_return(pid >= 0, -EINVAL);
139 assert_return(uid, -EINVAL);
141 #if 0 /// elogind does not support systemd slices
142 return cg_pid_get_owner_uid(pid, uid);
148 _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
152 assert_return(pid >= 0, -EINVAL);
153 assert_return(cgroup, -EINVAL);
155 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
159 /* The internal APIs return the empty string for the root
160 * cgroup, let's return the "/" in the public APIs instead, as
161 * that's easier and less ambiguous for people to grok. */
174 _public_ int sd_peer_get_session(int fd, char **session) {
175 struct ucred ucred = {};
178 assert_return(fd >= 0, -EBADF);
179 assert_return(session, -EINVAL);
181 r = getpeercred(fd, &ucred);
185 #if 0 /// elogind does not support systemd scopes
186 return cg_pid_get_session(ucred.pid, session);
192 _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
196 assert_return(fd >= 0, -EBADF);
197 assert_return(uid, -EINVAL);
199 r = getpeercred(fd, &ucred);
203 #if 0 /// elogind does not support systemd units
204 return cg_pid_get_owner_uid(ucred.pid, uid);
210 _public_ int sd_peer_get_unit(int fd, char **unit) {
214 assert_return(fd >= 0, -EBADF);
215 assert_return(unit, -EINVAL);
217 r = getpeercred(fd, &ucred);
221 #if 0 /// elogind does not support systemd units
222 return cg_pid_get_unit(ucred.pid, unit);
228 _public_ int sd_peer_get_user_unit(int fd, char **unit) {
232 assert_return(fd >= 0, -EBADF);
233 assert_return(unit, -EINVAL);
235 r = getpeercred(fd, &ucred);
239 #if 0 /// elogind does not support systemd units
240 return cg_pid_get_user_unit(ucred.pid, unit);
246 _public_ int sd_peer_get_machine_name(int fd, char **machine) {
250 assert_return(fd >= 0, -EBADF);
251 assert_return(machine, -EINVAL);
253 r = getpeercred(fd, &ucred);
257 #if 0 /// elogind does not support systemd units
258 return cg_pid_get_machine_name(ucred.pid, machine);
264 _public_ int sd_peer_get_slice(int fd, char **slice) {
268 assert_return(fd >= 0, -EBADF);
269 assert_return(slice, -EINVAL);
271 r = getpeercred(fd, &ucred);
275 #if 0 /// elogind does not support systemd slices
276 return cg_pid_get_slice(ucred.pid, slice);
282 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
286 assert_return(fd >= 0, -EBADF);
287 assert_return(slice, -EINVAL);
289 r = getpeercred(fd, &ucred);
293 #if 0 /// elogind does not support systemd slices
294 return cg_pid_get_user_slice(ucred.pid, slice);
300 _public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
304 assert_return(fd >= 0, -EBADF);
305 assert_return(cgroup, -EINVAL);
307 r = getpeercred(fd, &ucred);
311 return sd_pid_get_cgroup(ucred.pid, cgroup);
314 static int file_of_uid(uid_t uid, char **p) {
316 assert_return(uid_is_valid(uid), -EINVAL);
319 if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
325 _public_ int sd_uid_get_state(uid_t uid, char**state) {
326 _cleanup_free_ char *p = NULL;
330 assert_return(state, -EINVAL);
332 r = file_of_uid(uid, &p);
336 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
339 s = strdup("offline");
357 _public_ int sd_uid_get_display(uid_t uid, char **session) {
358 _cleanup_free_ char *p = NULL, *s = NULL;
361 assert_return(session, -EINVAL);
363 r = file_of_uid(uid, &p);
367 r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
381 static int file_of_seat(const char *seat, char **_p) {
388 if (!filename_is_valid(seat))
391 p = strappend("/run/systemd/seats/", seat);
393 _cleanup_free_ char *buf = NULL;
395 r = sd_session_get_seat(NULL, &buf);
399 p = strappend("/run/systemd/seats/", buf);
410 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
411 _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
414 const char *word, *variable, *state;
416 assert_return(uid_is_valid(uid), -EINVAL);
418 r = file_of_seat(seat, &p);
422 variable = require_active ? "ACTIVE_UID" : "UIDS";
424 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
432 if (asprintf(&t, UID_FMT, uid) < 0)
435 FOREACH_WORD(word, l, s, state)
436 if (strneq(t, word, l))
442 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
443 _cleanup_free_ char *p = NULL, *s = NULL;
449 r = file_of_uid(uid, &p);
453 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
454 if (r == -ENOENT || (r >= 0 && isempty(s))) {
462 a = strv_split(s, " ");
477 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
478 return uid_get_array(
480 require_active == 0 ? "ONLINE_SESSIONS" :
481 require_active > 0 ? "ACTIVE_SESSIONS" :
486 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
487 return uid_get_array(
489 require_active == 0 ? "ONLINE_SEATS" :
490 require_active > 0 ? "ACTIVE_SEATS" :
495 static int file_of_session(const char *session, char **_p) {
502 if (!session_id_valid(session))
505 p = strappend("/run/systemd/sessions/", session);
507 _cleanup_free_ char *buf = NULL;
509 r = sd_pid_get_session(0, &buf);
513 p = strappend("/run/systemd/sessions/", buf);
523 _public_ int sd_session_is_active(const char *session) {
524 _cleanup_free_ char *p = NULL, *s = NULL;
527 r = file_of_session(session, &p);
531 r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
539 return parse_boolean(s);
542 _public_ int sd_session_is_remote(const char *session) {
543 _cleanup_free_ char *p = NULL, *s = NULL;
546 r = file_of_session(session, &p);
550 r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
558 return parse_boolean(s);
561 _public_ int sd_session_get_state(const char *session, char **state) {
562 _cleanup_free_ char *p = NULL, *s = NULL;
565 assert_return(state, -EINVAL);
567 r = file_of_session(session, &p);
571 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
585 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
587 _cleanup_free_ char *p = NULL, *s = NULL;
589 assert_return(uid, -EINVAL);
591 r = file_of_session(session, &p);
595 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
603 return parse_uid(s, uid);
606 static int session_get_string(const char *session, const char *field, char **value) {
607 _cleanup_free_ char *p = NULL, *s = NULL;
610 assert_return(value, -EINVAL);
613 r = file_of_session(session, &p);
617 r = parse_env_file(p, NEWLINE, field, &s, NULL);
630 _public_ int sd_session_get_seat(const char *session, char **seat) {
631 return session_get_string(session, "SEAT", seat);
634 _public_ int sd_session_get_tty(const char *session, char **tty) {
635 return session_get_string(session, "TTY", tty);
638 _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
639 _cleanup_free_ char *vtnr_string = NULL;
643 assert_return(vtnr, -EINVAL);
645 r = session_get_string(session, "VTNR", &vtnr_string);
649 r = safe_atou(vtnr_string, &u);
657 _public_ int sd_session_get_service(const char *session, char **service) {
658 return session_get_string(session, "SERVICE", service);
661 _public_ int sd_session_get_type(const char *session, char **type) {
662 return session_get_string(session, "TYPE", type);
665 _public_ int sd_session_get_class(const char *session, char **class) {
666 return session_get_string(session, "CLASS", class);
669 _public_ int sd_session_get_desktop(const char *session, char **desktop) {
670 _cleanup_free_ char *escaped = NULL;
674 assert_return(desktop, -EINVAL);
676 r = session_get_string(session, "DESKTOP", &escaped);
680 r = cunescape(escaped, 0, &t);
688 _public_ int sd_session_get_display(const char *session, char **display) {
689 return session_get_string(session, "DISPLAY", display);
692 _public_ int sd_session_get_remote_user(const char *session, char **remote_user) {
693 return session_get_string(session, "REMOTE_USER", remote_user);
696 _public_ int sd_session_get_remote_host(const char *session, char **remote_host) {
697 return session_get_string(session, "REMOTE_HOST", remote_host);
700 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
701 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
704 assert_return(session || uid, -EINVAL);
706 r = file_of_seat(seat, &p);
710 r = parse_env_file(p, NEWLINE,
726 r = parse_uid(t, uid);
739 _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
740 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
741 _cleanup_strv_free_ char **a = NULL;
742 _cleanup_free_ uid_t *b = NULL;
746 r = file_of_seat(seat, &p);
750 r = parse_env_file(p, NEWLINE,
760 a = strv_split(s, " ");
766 const char *word, *state;
769 FOREACH_WORD(word, l, t, state)
779 FOREACH_WORD(word, l, t, state) {
780 _cleanup_free_ char *k = NULL;
782 k = strndup(word, l);
786 r = parse_uid(k, b + i);
813 static int seat_get_can(const char *seat, const char *variable) {
814 _cleanup_free_ char *p = NULL, *s = NULL;
819 r = file_of_seat(seat, &p);
823 r = parse_env_file(p, NEWLINE,
833 return parse_boolean(s);
836 _public_ int sd_seat_can_multi_session(const char *seat) {
837 return seat_get_can(seat, "CAN_MULTI_SESSION");
840 _public_ int sd_seat_can_tty(const char *seat) {
841 return seat_get_can(seat, "CAN_TTY");
844 _public_ int sd_seat_can_graphical(const char *seat) {
845 return seat_get_can(seat, "CAN_GRAPHICAL");
848 _public_ int sd_get_seats(char ***seats) {
849 return get_files_in_directory("/run/systemd/seats/", seats);
852 _public_ int sd_get_sessions(char ***sessions) {
853 return get_files_in_directory("/run/systemd/sessions/", sessions);
856 _public_ int sd_get_uids(uid_t **users) {
857 _cleanup_closedir_ DIR *d;
861 _cleanup_free_ uid_t *l = NULL;
863 d = opendir("/run/systemd/users/");
867 FOREACH_DIRENT_ALL(de, d, return -errno) {
871 dirent_ensure_type(d, de);
873 if (!dirent_is_file(de))
876 k = parse_uid(de->d_name, &uid);
881 if ((unsigned) r >= n) {
885 t = realloc(l, sizeof(uid_t) * n);
892 assert((unsigned) r < n);
906 _public_ int sd_get_machine_names(char ***machines) {
907 char **l = NULL, **a, **b;
910 assert_return(machines, -EINVAL);
912 r = get_files_in_directory("/run/systemd/machines/", &l);
923 /* Filter out the unit: symlinks */
924 for (a = l, b = l; *a; a++) {
925 if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
941 _public_ int sd_machine_get_class(const char *machine, char **class) {
942 _cleanup_free_ char *c = NULL;
946 assert_return(machine_name_is_valid(machine), -EINVAL);
947 assert_return(class, -EINVAL);
949 p = strjoina("/run/systemd/machines/", machine);
950 r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
964 _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
965 _cleanup_free_ char *netif = NULL;
966 size_t l, allocated = 0, nr = 0;
968 const char *p, *word, *state;
971 assert_return(machine_name_is_valid(machine), -EINVAL);
972 assert_return(ifindices, -EINVAL);
974 p = strjoina("/run/systemd/machines/", machine);
975 r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
985 FOREACH_WORD(word, l, netif, state) {
989 *(char*) (mempcpy(buf, word, l)) = 0;
991 if (parse_ifindex(buf, &ifi) < 0)
994 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
1006 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
1007 return (int) (unsigned long) m - 1;
1010 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
1011 return (sd_login_monitor*) (unsigned long) (fd + 1);
1014 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
1018 assert_return(m, -EINVAL);
1020 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1024 if (!category || streq(category, "seat")) {
1025 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
1034 if (!category || streq(category, "session")) {
1035 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
1044 if (!category || streq(category, "uid")) {
1045 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
1054 if (!category || streq(category, "machine")) {
1055 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
1069 *m = FD_TO_MONITOR(fd);
1073 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1079 fd = MONITOR_TO_FD(m);
1085 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
1087 assert_return(m, -EINVAL);
1089 return flush_fd(MONITOR_TO_FD(m));
1092 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1094 assert_return(m, -EINVAL);
1096 return MONITOR_TO_FD(m);
1099 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1101 assert_return(m, -EINVAL);
1103 /* For now we will only return POLLIN here, since we don't
1104 * need anything else ever for inotify. However, let's have
1105 * this API to keep our options open should we later on need
1110 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1112 assert_return(m, -EINVAL);
1113 assert_return(timeout_usec, -EINVAL);
1115 /* For now we will only return (uint64_t) -1, since we don't
1116 * need any timeout. However, let's have this API to keep our
1117 * options open should we later on need it. */
1118 *timeout_usec = (uint64_t) -1;