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 IN_SET(r, -ENXIO, -ENOMEDIUM) ? -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 IN_SET(r, -ENXIO, -ENOMEDIUM) ? -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 IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
100 _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
101 #if 0 /// UNNEEDED by elogind
105 assert_return(pid >= 0, -EINVAL);
106 assert_return(name, -EINVAL);
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;
116 _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
119 assert_return(pid >= 0, -EINVAL);
120 assert_return(slice, -EINVAL);
122 r = cg_pid_get_slice(pid, slice);
123 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
126 _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
129 assert_return(pid >= 0, -EINVAL);
130 assert_return(slice, -EINVAL);
132 r = cg_pid_get_user_slice(pid, slice);
133 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
136 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
139 assert_return(pid >= 0, -EINVAL);
140 assert_return(uid, -EINVAL);
142 r = cg_pid_get_owner_uid(pid, uid);
143 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
146 _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
150 assert_return(pid >= 0, -EINVAL);
151 assert_return(cgroup, -EINVAL);
153 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
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. */
172 _public_ int sd_peer_get_session(int fd, char **session) {
173 struct ucred ucred = {};
176 assert_return(fd >= 0, -EBADF);
177 assert_return(session, -EINVAL);
179 r = getpeercred(fd, &ucred);
183 return cg_pid_get_session(ucred.pid, session);
186 _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
190 assert_return(fd >= 0, -EBADF);
191 assert_return(uid, -EINVAL);
193 r = getpeercred(fd, &ucred);
197 return cg_pid_get_owner_uid(ucred.pid, uid);
200 _public_ int sd_peer_get_unit(int fd, char **unit) {
204 assert_return(fd >= 0, -EBADF);
205 assert_return(unit, -EINVAL);
207 r = getpeercred(fd, &ucred);
211 #if 0 /// elogind does not support systemd units
212 return cg_pid_get_unit(ucred.pid, unit);
218 _public_ int sd_peer_get_user_unit(int fd, char **unit) {
222 assert_return(fd >= 0, -EBADF);
223 assert_return(unit, -EINVAL);
225 r = getpeercred(fd, &ucred);
229 #if 0 /// elogind does not support systemd units
230 return cg_pid_get_user_unit(ucred.pid, unit);
236 _public_ int sd_peer_get_machine_name(int fd, char **machine) {
240 assert_return(fd >= 0, -EBADF);
241 assert_return(machine, -EINVAL);
243 r = getpeercred(fd, &ucred);
247 #if 0 /// elogind does not support systemd units
248 return cg_pid_get_machine_name(ucred.pid, machine);
254 _public_ int sd_peer_get_slice(int fd, char **slice) {
258 assert_return(fd >= 0, -EBADF);
259 assert_return(slice, -EINVAL);
261 r = getpeercred(fd, &ucred);
265 return cg_pid_get_slice(ucred.pid, slice);
268 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
272 assert_return(fd >= 0, -EBADF);
273 assert_return(slice, -EINVAL);
275 r = getpeercred(fd, &ucred);
279 return cg_pid_get_user_slice(ucred.pid, slice);
282 _public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
286 assert_return(fd >= 0, -EBADF);
287 assert_return(cgroup, -EINVAL);
289 r = getpeercred(fd, &ucred);
293 return sd_pid_get_cgroup(ucred.pid, cgroup);
296 static int file_of_uid(uid_t uid, char **p) {
298 assert_return(uid_is_valid(uid), -EINVAL);
301 if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
307 _public_ int sd_uid_get_state(uid_t uid, char**state) {
308 _cleanup_free_ char *p = NULL;
312 assert_return(state, -EINVAL);
314 r = file_of_uid(uid, &p);
318 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
321 s = strdup("offline");
339 _public_ int sd_uid_get_display(uid_t uid, char **session) {
340 _cleanup_free_ char *p = NULL, *s = NULL;
343 assert_return(session, -EINVAL);
345 r = file_of_uid(uid, &p);
349 r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
363 static int file_of_seat(const char *seat, char **_p) {
370 if (!filename_is_valid(seat))
373 p = strappend("/run/systemd/seats/", seat);
375 _cleanup_free_ char *buf = NULL;
377 r = sd_session_get_seat(NULL, &buf);
381 p = strappend("/run/systemd/seats/", buf);
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;
396 const char *word, *variable, *state;
398 assert_return(uid_is_valid(uid), -EINVAL);
400 r = file_of_seat(seat, &p);
404 variable = require_active ? "ACTIVE_UID" : "UIDS";
406 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
414 if (asprintf(&t, UID_FMT, uid) < 0)
417 FOREACH_WORD(word, l, s, state)
418 if (strneq(t, word, l))
424 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
425 _cleanup_free_ char *p = NULL, *s = NULL;
431 r = file_of_uid(uid, &p);
435 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
436 if (r == -ENOENT || (r >= 0 && isempty(s))) {
444 a = strv_split(s, " ");
459 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
460 return uid_get_array(
462 require_active == 0 ? "ONLINE_SESSIONS" :
463 require_active > 0 ? "ACTIVE_SESSIONS" :
468 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
469 return uid_get_array(
471 require_active == 0 ? "ONLINE_SEATS" :
472 require_active > 0 ? "ACTIVE_SEATS" :
477 static int file_of_session(const char *session, char **_p) {
484 if (!session_id_valid(session))
487 p = strappend("/run/systemd/sessions/", session);
489 _cleanup_free_ char *buf = NULL;
491 r = sd_pid_get_session(0, &buf);
495 p = strappend("/run/systemd/sessions/", buf);
505 _public_ int sd_session_is_active(const char *session) {
506 _cleanup_free_ char *p = NULL, *s = NULL;
509 r = file_of_session(session, &p);
513 r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
521 return parse_boolean(s);
524 _public_ int sd_session_is_remote(const char *session) {
525 _cleanup_free_ char *p = NULL, *s = NULL;
528 r = file_of_session(session, &p);
532 r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
540 return parse_boolean(s);
543 _public_ int sd_session_get_state(const char *session, char **state) {
544 _cleanup_free_ char *p = NULL, *s = NULL;
547 assert_return(state, -EINVAL);
549 r = file_of_session(session, &p);
553 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
567 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
569 _cleanup_free_ char *p = NULL, *s = NULL;
571 assert_return(uid, -EINVAL);
573 r = file_of_session(session, &p);
577 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
585 return parse_uid(s, uid);
588 static int session_get_string(const char *session, const char *field, char **value) {
589 _cleanup_free_ char *p = NULL, *s = NULL;
592 assert_return(value, -EINVAL);
595 r = file_of_session(session, &p);
599 r = parse_env_file(p, NEWLINE, field, &s, NULL);
612 _public_ int sd_session_get_seat(const char *session, char **seat) {
613 return session_get_string(session, "SEAT", seat);
616 _public_ int sd_session_get_tty(const char *session, char **tty) {
617 return session_get_string(session, "TTY", tty);
620 _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
621 _cleanup_free_ char *vtnr_string = NULL;
625 assert_return(vtnr, -EINVAL);
627 r = session_get_string(session, "VTNR", &vtnr_string);
631 r = safe_atou(vtnr_string, &u);
639 _public_ int sd_session_get_service(const char *session, char **service) {
640 return session_get_string(session, "SERVICE", service);
643 _public_ int sd_session_get_type(const char *session, char **type) {
644 return session_get_string(session, "TYPE", type);
647 _public_ int sd_session_get_class(const char *session, char **class) {
648 return session_get_string(session, "CLASS", class);
651 _public_ int sd_session_get_desktop(const char *session, char **desktop) {
652 _cleanup_free_ char *escaped = NULL;
656 assert_return(desktop, -EINVAL);
658 r = session_get_string(session, "DESKTOP", &escaped);
662 r = cunescape(escaped, 0, &t);
670 _public_ int sd_session_get_display(const char *session, char **display) {
671 return session_get_string(session, "DISPLAY", display);
674 _public_ int sd_session_get_remote_user(const char *session, char **remote_user) {
675 return session_get_string(session, "REMOTE_USER", remote_user);
678 _public_ int sd_session_get_remote_host(const char *session, char **remote_host) {
679 return session_get_string(session, "REMOTE_HOST", remote_host);
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;
686 assert_return(session || uid, -EINVAL);
688 r = file_of_seat(seat, &p);
692 r = parse_env_file(p, NEWLINE,
708 r = parse_uid(t, uid);
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;
728 r = file_of_seat(seat, &p);
732 r = parse_env_file(p, NEWLINE,
742 a = strv_split(s, " ");
748 const char *word, *state;
751 FOREACH_WORD(word, l, t, state)
761 FOREACH_WORD(word, l, t, state) {
762 _cleanup_free_ char *k = NULL;
764 k = strndup(word, l);
768 r = parse_uid(k, b + i);
795 static int seat_get_can(const char *seat, const char *variable) {
796 _cleanup_free_ char *p = NULL, *s = NULL;
801 r = file_of_seat(seat, &p);
805 r = parse_env_file(p, NEWLINE,
815 return parse_boolean(s);
818 _public_ int sd_seat_can_multi_session(const char *seat) {
819 return seat_get_can(seat, "CAN_MULTI_SESSION");
822 _public_ int sd_seat_can_tty(const char *seat) {
823 return seat_get_can(seat, "CAN_TTY");
826 _public_ int sd_seat_can_graphical(const char *seat) {
827 return seat_get_can(seat, "CAN_GRAPHICAL");
830 _public_ int sd_get_seats(char ***seats) {
833 r = get_files_in_directory("/run/systemd/seats/", seats);
842 _public_ int sd_get_sessions(char ***sessions) {
845 r = get_files_in_directory("/run/systemd/sessions/", sessions);
854 _public_ int sd_get_uids(uid_t **users) {
855 _cleanup_closedir_ DIR *d;
859 _cleanup_free_ uid_t *l = NULL;
861 d = opendir("/run/systemd/users/");
863 if (errno == ENOENT) {
871 FOREACH_DIRENT_ALL(de, d, return -errno) {
875 dirent_ensure_type(d, de);
877 if (!dirent_is_file(de))
880 k = parse_uid(de->d_name, &uid);
885 if ((unsigned) r >= n) {
889 t = realloc(l, sizeof(uid_t) * n);
896 assert((unsigned) r < n);
910 _public_ int sd_get_machine_names(char ***machines) {
911 _cleanup_strv_free_ char **l = NULL;
915 r = get_files_in_directory("/run/systemd/machines/", &l);
927 /* Filter out the unit: symlinks */
928 for (a = b = l; *a; a++) {
929 if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
948 _public_ int sd_machine_get_class(const char *machine, char **class) {
949 _cleanup_free_ char *c = NULL;
953 assert_return(machine_name_is_valid(machine), -EINVAL);
954 assert_return(class, -EINVAL);
956 p = strjoina("/run/systemd/machines/", machine);
957 r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
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;
975 const char *p, *word, *state;
978 assert_return(machine_name_is_valid(machine), -EINVAL);
979 assert_return(ifindices, -EINVAL);
981 p = strjoina("/run/systemd/machines/", machine);
982 r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
992 FOREACH_WORD(word, l, netif, state) {
996 *(char*) (mempcpy(buf, word, l)) = 0;
998 if (parse_ifindex(buf, &ifi) < 0)
1001 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
1013 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
1014 return (int) (unsigned long) m - 1;
1017 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
1018 return (sd_login_monitor*) (unsigned long) (fd + 1);
1021 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
1025 assert_return(m, -EINVAL);
1027 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1031 if (!category || streq(category, "seat")) {
1032 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
1041 if (!category || streq(category, "session")) {
1042 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
1051 if (!category || streq(category, "uid")) {
1052 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
1061 if (!category || streq(category, "machine")) {
1062 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
1076 *m = FD_TO_MONITOR(fd);
1080 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1086 fd = MONITOR_TO_FD(m);
1092 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
1094 assert_return(m, -EINVAL);
1096 return flush_fd(MONITOR_TO_FD(m));
1099 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1101 assert_return(m, -EINVAL);
1103 return MONITOR_TO_FD(m);
1106 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1108 assert_return(m, -EINVAL);
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
1117 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1119 assert_return(m, -EINVAL);
1120 assert_return(timeout_usec, -EINVAL);
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;