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) {
127 #if 0 /// UNNEEDED by elogind
131 assert_return(pid >= 0, -EINVAL);
132 assert_return(slice, -EINVAL);
134 #if 0 /// elogind does not support systemd slices
135 r = cg_pid_get_user_slice(pid, slice);
136 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
142 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
145 assert_return(pid >= 0, -EINVAL);
146 assert_return(uid, -EINVAL);
148 r = cg_pid_get_owner_uid(pid, uid);
149 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
152 _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
156 assert_return(pid >= 0, -EINVAL);
157 assert_return(cgroup, -EINVAL);
159 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
163 /* The internal APIs return the empty string for the root
164 * cgroup, let's return the "/" in the public APIs instead, as
165 * that's easier and less ambiguous for people to grok. */
178 _public_ int sd_peer_get_session(int fd, char **session) {
179 struct ucred ucred = {};
182 assert_return(fd >= 0, -EBADF);
183 assert_return(session, -EINVAL);
185 r = getpeercred(fd, &ucred);
189 #if 0 /// elogind does not support systemd scopes
190 return cg_pid_get_session(ucred.pid, session);
196 _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
200 assert_return(fd >= 0, -EBADF);
201 assert_return(uid, -EINVAL);
203 r = getpeercred(fd, &ucred);
207 #if 0 /// elogind does not support systemd units
208 return cg_pid_get_owner_uid(ucred.pid, uid);
214 _public_ int sd_peer_get_unit(int fd, char **unit) {
218 assert_return(fd >= 0, -EBADF);
219 assert_return(unit, -EINVAL);
221 r = getpeercred(fd, &ucred);
225 #if 0 /// elogind does not support systemd units
226 return cg_pid_get_unit(ucred.pid, unit);
232 _public_ int sd_peer_get_user_unit(int fd, char **unit) {
236 assert_return(fd >= 0, -EBADF);
237 assert_return(unit, -EINVAL);
239 r = getpeercred(fd, &ucred);
243 #if 0 /// elogind does not support systemd units
244 return cg_pid_get_user_unit(ucred.pid, unit);
250 _public_ int sd_peer_get_machine_name(int fd, char **machine) {
254 assert_return(fd >= 0, -EBADF);
255 assert_return(machine, -EINVAL);
257 r = getpeercred(fd, &ucred);
261 #if 0 /// elogind does not support systemd units
262 return cg_pid_get_machine_name(ucred.pid, machine);
268 _public_ int sd_peer_get_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_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) {
851 r = get_files_in_directory("/run/systemd/seats/", seats);
860 _public_ int sd_get_sessions(char ***sessions) {
863 r = get_files_in_directory("/run/systemd/sessions/", sessions);
872 _public_ int sd_get_uids(uid_t **users) {
873 _cleanup_closedir_ DIR *d;
877 _cleanup_free_ uid_t *l = NULL;
879 d = opendir("/run/systemd/users/");
881 if (errno == ENOENT) {
889 FOREACH_DIRENT_ALL(de, d, return -errno) {
893 dirent_ensure_type(d, de);
895 if (!dirent_is_file(de))
898 k = parse_uid(de->d_name, &uid);
903 if ((unsigned) r >= n) {
907 t = realloc(l, sizeof(uid_t) * n);
914 assert((unsigned) r < n);
928 _public_ int sd_get_machine_names(char ***machines) {
929 _cleanup_strv_free_ char **l = NULL;
933 r = get_files_in_directory("/run/systemd/machines/", &l);
945 /* Filter out the unit: symlinks */
946 for (a = b = l; *a; a++) {
947 if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
966 _public_ int sd_machine_get_class(const char *machine, char **class) {
967 _cleanup_free_ char *c = NULL;
971 assert_return(machine_name_is_valid(machine), -EINVAL);
972 assert_return(class, -EINVAL);
974 p = strjoina("/run/systemd/machines/", machine);
975 r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
989 _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
990 _cleanup_free_ char *netif = NULL;
991 size_t l, allocated = 0, nr = 0;
993 const char *p, *word, *state;
996 assert_return(machine_name_is_valid(machine), -EINVAL);
997 assert_return(ifindices, -EINVAL);
999 p = strjoina("/run/systemd/machines/", machine);
1000 r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
1010 FOREACH_WORD(word, l, netif, state) {
1014 *(char*) (mempcpy(buf, word, l)) = 0;
1016 if (parse_ifindex(buf, &ifi) < 0)
1019 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
1031 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
1032 return (int) (unsigned long) m - 1;
1035 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
1036 return (sd_login_monitor*) (unsigned long) (fd + 1);
1039 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
1043 assert_return(m, -EINVAL);
1045 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1049 if (!category || streq(category, "seat")) {
1050 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
1059 if (!category || streq(category, "session")) {
1060 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
1069 if (!category || streq(category, "uid")) {
1070 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
1079 if (!category || streq(category, "machine")) {
1080 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
1094 *m = FD_TO_MONITOR(fd);
1098 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1104 fd = MONITOR_TO_FD(m);
1110 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
1112 assert_return(m, -EINVAL);
1114 return flush_fd(MONITOR_TO_FD(m));
1117 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1119 assert_return(m, -EINVAL);
1121 return MONITOR_TO_FD(m);
1124 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1126 assert_return(m, -EINVAL);
1128 /* For now we will only return POLLIN here, since we don't
1129 * need anything else ever for inotify. However, let's have
1130 * this API to keep our options open should we later on need
1135 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1137 assert_return(m, -EINVAL);
1138 assert_return(timeout_usec, -EINVAL);
1140 /* For now we will only return (uint64_t) -1, since we don't
1141 * need any timeout. However, let's have this API to keep our
1142 * options open should we later on need it. */
1143 *timeout_usec = (uint64_t) -1;