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) {
117 #if 0 /// UNNEEDED by elogind
121 assert_return(pid >= 0, -EINVAL);
122 assert_return(slice, -EINVAL);
124 #if 0 /// elogind does not support systemd slices
125 r = cg_pid_get_slice(pid, slice);
126 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
132 _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
133 #if 0 /// UNNEEDED by elogind
137 assert_return(pid >= 0, -EINVAL);
138 assert_return(slice, -EINVAL);
140 #if 0 /// elogind does not support systemd slices
141 r = cg_pid_get_user_slice(pid, slice);
142 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
148 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
151 assert_return(pid >= 0, -EINVAL);
152 assert_return(uid, -EINVAL);
154 r = cg_pid_get_owner_uid(pid, uid);
155 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
158 _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
162 assert_return(pid >= 0, -EINVAL);
163 assert_return(cgroup, -EINVAL);
165 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
169 /* The internal APIs return the empty string for the root
170 * cgroup, let's return the "/" in the public APIs instead, as
171 * that's easier and less ambiguous for people to grok. */
184 _public_ int sd_peer_get_session(int fd, char **session) {
185 struct ucred ucred = {};
188 assert_return(fd >= 0, -EBADF);
189 assert_return(session, -EINVAL);
191 r = getpeercred(fd, &ucred);
195 #if 0 /// elogind does not support systemd scopes
196 return cg_pid_get_session(ucred.pid, session);
202 _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
206 assert_return(fd >= 0, -EBADF);
207 assert_return(uid, -EINVAL);
209 r = getpeercred(fd, &ucred);
213 #if 0 /// elogind does not support systemd units
214 return cg_pid_get_owner_uid(ucred.pid, uid);
220 _public_ int sd_peer_get_unit(int fd, char **unit) {
224 assert_return(fd >= 0, -EBADF);
225 assert_return(unit, -EINVAL);
227 r = getpeercred(fd, &ucred);
231 #if 0 /// elogind does not support systemd units
232 return cg_pid_get_unit(ucred.pid, unit);
238 _public_ int sd_peer_get_user_unit(int fd, char **unit) {
242 assert_return(fd >= 0, -EBADF);
243 assert_return(unit, -EINVAL);
245 r = getpeercred(fd, &ucred);
249 #if 0 /// elogind does not support systemd units
250 return cg_pid_get_user_unit(ucred.pid, unit);
256 _public_ int sd_peer_get_machine_name(int fd, char **machine) {
260 assert_return(fd >= 0, -EBADF);
261 assert_return(machine, -EINVAL);
263 r = getpeercred(fd, &ucred);
267 #if 0 /// elogind does not support systemd units
268 return cg_pid_get_machine_name(ucred.pid, machine);
274 _public_ int sd_peer_get_slice(int fd, char **slice) {
278 assert_return(fd >= 0, -EBADF);
279 assert_return(slice, -EINVAL);
281 r = getpeercred(fd, &ucred);
285 #if 0 /// elogind does not support systemd slices
286 return cg_pid_get_slice(ucred.pid, slice);
292 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
296 assert_return(fd >= 0, -EBADF);
297 assert_return(slice, -EINVAL);
299 r = getpeercred(fd, &ucred);
303 #if 0 /// elogind does not support systemd slices
304 return cg_pid_get_user_slice(ucred.pid, slice);
310 _public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
314 assert_return(fd >= 0, -EBADF);
315 assert_return(cgroup, -EINVAL);
317 r = getpeercred(fd, &ucred);
321 return sd_pid_get_cgroup(ucred.pid, cgroup);
324 static int file_of_uid(uid_t uid, char **p) {
326 assert_return(uid_is_valid(uid), -EINVAL);
329 if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
335 _public_ int sd_uid_get_state(uid_t uid, char**state) {
336 _cleanup_free_ char *p = NULL;
340 assert_return(state, -EINVAL);
342 r = file_of_uid(uid, &p);
346 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
349 s = strdup("offline");
367 _public_ int sd_uid_get_display(uid_t uid, char **session) {
368 _cleanup_free_ char *p = NULL, *s = NULL;
371 assert_return(session, -EINVAL);
373 r = file_of_uid(uid, &p);
377 r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
391 static int file_of_seat(const char *seat, char **_p) {
398 if (!filename_is_valid(seat))
401 p = strappend("/run/systemd/seats/", seat);
403 _cleanup_free_ char *buf = NULL;
405 r = sd_session_get_seat(NULL, &buf);
409 p = strappend("/run/systemd/seats/", buf);
420 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
421 _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
424 const char *word, *variable, *state;
426 assert_return(uid_is_valid(uid), -EINVAL);
428 r = file_of_seat(seat, &p);
432 variable = require_active ? "ACTIVE_UID" : "UIDS";
434 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
442 if (asprintf(&t, UID_FMT, uid) < 0)
445 FOREACH_WORD(word, l, s, state)
446 if (strneq(t, word, l))
452 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
453 _cleanup_free_ char *p = NULL, *s = NULL;
459 r = file_of_uid(uid, &p);
463 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
464 if (r == -ENOENT || (r >= 0 && isempty(s))) {
472 a = strv_split(s, " ");
487 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
488 return uid_get_array(
490 require_active == 0 ? "ONLINE_SESSIONS" :
491 require_active > 0 ? "ACTIVE_SESSIONS" :
496 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
497 return uid_get_array(
499 require_active == 0 ? "ONLINE_SEATS" :
500 require_active > 0 ? "ACTIVE_SEATS" :
505 static int file_of_session(const char *session, char **_p) {
512 if (!session_id_valid(session))
515 p = strappend("/run/systemd/sessions/", session);
517 _cleanup_free_ char *buf = NULL;
519 r = sd_pid_get_session(0, &buf);
523 p = strappend("/run/systemd/sessions/", buf);
533 _public_ int sd_session_is_active(const char *session) {
534 _cleanup_free_ char *p = NULL, *s = NULL;
537 r = file_of_session(session, &p);
541 r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
549 return parse_boolean(s);
552 _public_ int sd_session_is_remote(const char *session) {
553 _cleanup_free_ char *p = NULL, *s = NULL;
556 r = file_of_session(session, &p);
560 r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
568 return parse_boolean(s);
571 _public_ int sd_session_get_state(const char *session, char **state) {
572 _cleanup_free_ char *p = NULL, *s = NULL;
575 assert_return(state, -EINVAL);
577 r = file_of_session(session, &p);
581 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
595 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
597 _cleanup_free_ char *p = NULL, *s = NULL;
599 assert_return(uid, -EINVAL);
601 r = file_of_session(session, &p);
605 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
613 return parse_uid(s, uid);
616 static int session_get_string(const char *session, const char *field, char **value) {
617 _cleanup_free_ char *p = NULL, *s = NULL;
620 assert_return(value, -EINVAL);
623 r = file_of_session(session, &p);
627 r = parse_env_file(p, NEWLINE, field, &s, NULL);
640 _public_ int sd_session_get_seat(const char *session, char **seat) {
641 return session_get_string(session, "SEAT", seat);
644 _public_ int sd_session_get_tty(const char *session, char **tty) {
645 return session_get_string(session, "TTY", tty);
648 _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
649 _cleanup_free_ char *vtnr_string = NULL;
653 assert_return(vtnr, -EINVAL);
655 r = session_get_string(session, "VTNR", &vtnr_string);
659 r = safe_atou(vtnr_string, &u);
667 _public_ int sd_session_get_service(const char *session, char **service) {
668 return session_get_string(session, "SERVICE", service);
671 _public_ int sd_session_get_type(const char *session, char **type) {
672 return session_get_string(session, "TYPE", type);
675 _public_ int sd_session_get_class(const char *session, char **class) {
676 return session_get_string(session, "CLASS", class);
679 _public_ int sd_session_get_desktop(const char *session, char **desktop) {
680 _cleanup_free_ char *escaped = NULL;
684 assert_return(desktop, -EINVAL);
686 r = session_get_string(session, "DESKTOP", &escaped);
690 r = cunescape(escaped, 0, &t);
698 _public_ int sd_session_get_display(const char *session, char **display) {
699 return session_get_string(session, "DISPLAY", display);
702 _public_ int sd_session_get_remote_user(const char *session, char **remote_user) {
703 return session_get_string(session, "REMOTE_USER", remote_user);
706 _public_ int sd_session_get_remote_host(const char *session, char **remote_host) {
707 return session_get_string(session, "REMOTE_HOST", remote_host);
710 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
711 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
714 assert_return(session || uid, -EINVAL);
716 r = file_of_seat(seat, &p);
720 r = parse_env_file(p, NEWLINE,
736 r = parse_uid(t, uid);
749 _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
750 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
751 _cleanup_strv_free_ char **a = NULL;
752 _cleanup_free_ uid_t *b = NULL;
756 r = file_of_seat(seat, &p);
760 r = parse_env_file(p, NEWLINE,
770 a = strv_split(s, " ");
776 const char *word, *state;
779 FOREACH_WORD(word, l, t, state)
789 FOREACH_WORD(word, l, t, state) {
790 _cleanup_free_ char *k = NULL;
792 k = strndup(word, l);
796 r = parse_uid(k, b + i);
823 static int seat_get_can(const char *seat, const char *variable) {
824 _cleanup_free_ char *p = NULL, *s = NULL;
829 r = file_of_seat(seat, &p);
833 r = parse_env_file(p, NEWLINE,
843 return parse_boolean(s);
846 _public_ int sd_seat_can_multi_session(const char *seat) {
847 return seat_get_can(seat, "CAN_MULTI_SESSION");
850 _public_ int sd_seat_can_tty(const char *seat) {
851 return seat_get_can(seat, "CAN_TTY");
854 _public_ int sd_seat_can_graphical(const char *seat) {
855 return seat_get_can(seat, "CAN_GRAPHICAL");
858 _public_ int sd_get_seats(char ***seats) {
861 r = get_files_in_directory("/run/systemd/seats/", seats);
870 _public_ int sd_get_sessions(char ***sessions) {
873 r = get_files_in_directory("/run/systemd/sessions/", sessions);
882 _public_ int sd_get_uids(uid_t **users) {
883 _cleanup_closedir_ DIR *d;
887 _cleanup_free_ uid_t *l = NULL;
889 d = opendir("/run/systemd/users/");
891 if (errno == ENOENT) {
899 FOREACH_DIRENT_ALL(de, d, return -errno) {
903 dirent_ensure_type(d, de);
905 if (!dirent_is_file(de))
908 k = parse_uid(de->d_name, &uid);
913 if ((unsigned) r >= n) {
917 t = realloc(l, sizeof(uid_t) * n);
924 assert((unsigned) r < n);
938 _public_ int sd_get_machine_names(char ***machines) {
939 _cleanup_strv_free_ char **l = NULL;
943 r = get_files_in_directory("/run/systemd/machines/", &l);
955 /* Filter out the unit: symlinks */
956 for (a = b = l; *a; a++) {
957 if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
976 _public_ int sd_machine_get_class(const char *machine, char **class) {
977 _cleanup_free_ char *c = NULL;
981 assert_return(machine_name_is_valid(machine), -EINVAL);
982 assert_return(class, -EINVAL);
984 p = strjoina("/run/systemd/machines/", machine);
985 r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
999 _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
1000 _cleanup_free_ char *netif = NULL;
1001 size_t l, allocated = 0, nr = 0;
1003 const char *p, *word, *state;
1006 assert_return(machine_name_is_valid(machine), -EINVAL);
1007 assert_return(ifindices, -EINVAL);
1009 p = strjoina("/run/systemd/machines/", machine);
1010 r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
1020 FOREACH_WORD(word, l, netif, state) {
1024 *(char*) (mempcpy(buf, word, l)) = 0;
1026 if (parse_ifindex(buf, &ifi) < 0)
1029 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
1041 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
1042 return (int) (unsigned long) m - 1;
1045 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
1046 return (sd_login_monitor*) (unsigned long) (fd + 1);
1049 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
1053 assert_return(m, -EINVAL);
1055 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1059 if (!category || streq(category, "seat")) {
1060 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
1069 if (!category || streq(category, "session")) {
1070 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
1079 if (!category || streq(category, "uid")) {
1080 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
1089 if (!category || streq(category, "machine")) {
1090 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
1104 *m = FD_TO_MONITOR(fd);
1108 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1114 fd = MONITOR_TO_FD(m);
1120 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
1122 assert_return(m, -EINVAL);
1124 return flush_fd(MONITOR_TO_FD(m));
1127 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1129 assert_return(m, -EINVAL);
1131 return MONITOR_TO_FD(m);
1134 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1136 assert_return(m, -EINVAL);
1138 /* For now we will only return POLLIN here, since we don't
1139 * need anything else ever for inotify. However, let's have
1140 * this API to keep our options open should we later on need
1145 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1147 assert_return(m, -EINVAL);
1148 assert_return(timeout_usec, -EINVAL);
1150 /* For now we will only return (uint64_t) -1, since we don't
1151 * need any timeout. However, let's have this API to keep our
1152 * options open should we later on need it. */
1153 *timeout_usec = (uint64_t) -1;