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) {
149 #if 0 /// UNNEEDED by elogind
153 assert_return(pid >= 0, -EINVAL);
154 assert_return(uid, -EINVAL);
156 #if 0 /// elogind does not support systemd slices
157 r = cg_pid_get_owner_uid(pid, uid);
158 return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
164 _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
168 assert_return(pid >= 0, -EINVAL);
169 assert_return(cgroup, -EINVAL);
171 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
175 /* The internal APIs return the empty string for the root
176 * cgroup, let's return the "/" in the public APIs instead, as
177 * that's easier and less ambiguous for people to grok. */
190 _public_ int sd_peer_get_session(int fd, char **session) {
191 struct ucred ucred = {};
194 assert_return(fd >= 0, -EBADF);
195 assert_return(session, -EINVAL);
197 r = getpeercred(fd, &ucred);
201 #if 0 /// elogind does not support systemd scopes
202 return cg_pid_get_session(ucred.pid, session);
208 _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
212 assert_return(fd >= 0, -EBADF);
213 assert_return(uid, -EINVAL);
215 r = getpeercred(fd, &ucred);
219 #if 0 /// elogind does not support systemd units
220 return cg_pid_get_owner_uid(ucred.pid, uid);
226 _public_ int sd_peer_get_unit(int fd, char **unit) {
230 assert_return(fd >= 0, -EBADF);
231 assert_return(unit, -EINVAL);
233 r = getpeercred(fd, &ucred);
237 #if 0 /// elogind does not support systemd units
238 return cg_pid_get_unit(ucred.pid, unit);
244 _public_ int sd_peer_get_user_unit(int fd, char **unit) {
248 assert_return(fd >= 0, -EBADF);
249 assert_return(unit, -EINVAL);
251 r = getpeercred(fd, &ucred);
255 #if 0 /// elogind does not support systemd units
256 return cg_pid_get_user_unit(ucred.pid, unit);
262 _public_ int sd_peer_get_machine_name(int fd, char **machine) {
266 assert_return(fd >= 0, -EBADF);
267 assert_return(machine, -EINVAL);
269 r = getpeercred(fd, &ucred);
273 #if 0 /// elogind does not support systemd units
274 return cg_pid_get_machine_name(ucred.pid, machine);
280 _public_ int sd_peer_get_slice(int fd, char **slice) {
284 assert_return(fd >= 0, -EBADF);
285 assert_return(slice, -EINVAL);
287 r = getpeercred(fd, &ucred);
291 #if 0 /// elogind does not support systemd slices
292 return cg_pid_get_slice(ucred.pid, slice);
298 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
302 assert_return(fd >= 0, -EBADF);
303 assert_return(slice, -EINVAL);
305 r = getpeercred(fd, &ucred);
309 #if 0 /// elogind does not support systemd slices
310 return cg_pid_get_user_slice(ucred.pid, slice);
316 _public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
320 assert_return(fd >= 0, -EBADF);
321 assert_return(cgroup, -EINVAL);
323 r = getpeercred(fd, &ucred);
327 return sd_pid_get_cgroup(ucred.pid, cgroup);
330 static int file_of_uid(uid_t uid, char **p) {
332 assert_return(uid_is_valid(uid), -EINVAL);
335 if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
341 _public_ int sd_uid_get_state(uid_t uid, char**state) {
342 _cleanup_free_ char *p = NULL;
346 assert_return(state, -EINVAL);
348 r = file_of_uid(uid, &p);
352 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
355 s = strdup("offline");
373 _public_ int sd_uid_get_display(uid_t uid, char **session) {
374 _cleanup_free_ char *p = NULL, *s = NULL;
377 assert_return(session, -EINVAL);
379 r = file_of_uid(uid, &p);
383 r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
397 static int file_of_seat(const char *seat, char **_p) {
404 if (!filename_is_valid(seat))
407 p = strappend("/run/systemd/seats/", seat);
409 _cleanup_free_ char *buf = NULL;
411 r = sd_session_get_seat(NULL, &buf);
415 p = strappend("/run/systemd/seats/", buf);
426 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
427 _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
430 const char *word, *variable, *state;
432 assert_return(uid_is_valid(uid), -EINVAL);
434 r = file_of_seat(seat, &p);
438 variable = require_active ? "ACTIVE_UID" : "UIDS";
440 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
448 if (asprintf(&t, UID_FMT, uid) < 0)
451 FOREACH_WORD(word, l, s, state)
452 if (strneq(t, word, l))
458 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
459 _cleanup_free_ char *p = NULL, *s = NULL;
465 r = file_of_uid(uid, &p);
469 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
470 if (r == -ENOENT || (r >= 0 && isempty(s))) {
478 a = strv_split(s, " ");
493 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
494 return uid_get_array(
496 require_active == 0 ? "ONLINE_SESSIONS" :
497 require_active > 0 ? "ACTIVE_SESSIONS" :
502 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
503 return uid_get_array(
505 require_active == 0 ? "ONLINE_SEATS" :
506 require_active > 0 ? "ACTIVE_SEATS" :
511 static int file_of_session(const char *session, char **_p) {
518 if (!session_id_valid(session))
521 p = strappend("/run/systemd/sessions/", session);
523 _cleanup_free_ char *buf = NULL;
525 r = sd_pid_get_session(0, &buf);
529 p = strappend("/run/systemd/sessions/", buf);
539 _public_ int sd_session_is_active(const char *session) {
540 _cleanup_free_ char *p = NULL, *s = NULL;
543 r = file_of_session(session, &p);
547 r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
555 return parse_boolean(s);
558 _public_ int sd_session_is_remote(const char *session) {
559 _cleanup_free_ char *p = NULL, *s = NULL;
562 r = file_of_session(session, &p);
566 r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
574 return parse_boolean(s);
577 _public_ int sd_session_get_state(const char *session, char **state) {
578 _cleanup_free_ char *p = NULL, *s = NULL;
581 assert_return(state, -EINVAL);
583 r = file_of_session(session, &p);
587 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
601 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
603 _cleanup_free_ char *p = NULL, *s = NULL;
605 assert_return(uid, -EINVAL);
607 r = file_of_session(session, &p);
611 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
619 return parse_uid(s, uid);
622 static int session_get_string(const char *session, const char *field, char **value) {
623 _cleanup_free_ char *p = NULL, *s = NULL;
626 assert_return(value, -EINVAL);
629 r = file_of_session(session, &p);
633 r = parse_env_file(p, NEWLINE, field, &s, NULL);
646 _public_ int sd_session_get_seat(const char *session, char **seat) {
647 return session_get_string(session, "SEAT", seat);
650 _public_ int sd_session_get_tty(const char *session, char **tty) {
651 return session_get_string(session, "TTY", tty);
654 _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
655 _cleanup_free_ char *vtnr_string = NULL;
659 assert_return(vtnr, -EINVAL);
661 r = session_get_string(session, "VTNR", &vtnr_string);
665 r = safe_atou(vtnr_string, &u);
673 _public_ int sd_session_get_service(const char *session, char **service) {
674 return session_get_string(session, "SERVICE", service);
677 _public_ int sd_session_get_type(const char *session, char **type) {
678 return session_get_string(session, "TYPE", type);
681 _public_ int sd_session_get_class(const char *session, char **class) {
682 return session_get_string(session, "CLASS", class);
685 _public_ int sd_session_get_desktop(const char *session, char **desktop) {
686 _cleanup_free_ char *escaped = NULL;
690 assert_return(desktop, -EINVAL);
692 r = session_get_string(session, "DESKTOP", &escaped);
696 r = cunescape(escaped, 0, &t);
704 _public_ int sd_session_get_display(const char *session, char **display) {
705 return session_get_string(session, "DISPLAY", display);
708 _public_ int sd_session_get_remote_user(const char *session, char **remote_user) {
709 return session_get_string(session, "REMOTE_USER", remote_user);
712 _public_ int sd_session_get_remote_host(const char *session, char **remote_host) {
713 return session_get_string(session, "REMOTE_HOST", remote_host);
716 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
717 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
720 assert_return(session || uid, -EINVAL);
722 r = file_of_seat(seat, &p);
726 r = parse_env_file(p, NEWLINE,
742 r = parse_uid(t, uid);
755 _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
756 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
757 _cleanup_strv_free_ char **a = NULL;
758 _cleanup_free_ uid_t *b = NULL;
762 r = file_of_seat(seat, &p);
766 r = parse_env_file(p, NEWLINE,
776 a = strv_split(s, " ");
782 const char *word, *state;
785 FOREACH_WORD(word, l, t, state)
795 FOREACH_WORD(word, l, t, state) {
796 _cleanup_free_ char *k = NULL;
798 k = strndup(word, l);
802 r = parse_uid(k, b + i);
829 static int seat_get_can(const char *seat, const char *variable) {
830 _cleanup_free_ char *p = NULL, *s = NULL;
835 r = file_of_seat(seat, &p);
839 r = parse_env_file(p, NEWLINE,
849 return parse_boolean(s);
852 _public_ int sd_seat_can_multi_session(const char *seat) {
853 return seat_get_can(seat, "CAN_MULTI_SESSION");
856 _public_ int sd_seat_can_tty(const char *seat) {
857 return seat_get_can(seat, "CAN_TTY");
860 _public_ int sd_seat_can_graphical(const char *seat) {
861 return seat_get_can(seat, "CAN_GRAPHICAL");
864 _public_ int sd_get_seats(char ***seats) {
865 return get_files_in_directory("/run/systemd/seats/", seats);
868 _public_ int sd_get_sessions(char ***sessions) {
869 return 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/");
883 FOREACH_DIRENT_ALL(de, d, return -errno) {
887 dirent_ensure_type(d, de);
889 if (!dirent_is_file(de))
892 k = parse_uid(de->d_name, &uid);
897 if ((unsigned) r >= n) {
901 t = realloc(l, sizeof(uid_t) * n);
908 assert((unsigned) r < n);
922 _public_ int sd_get_machine_names(char ***machines) {
923 char **l = NULL, **a, **b;
926 assert_return(machines, -EINVAL);
928 r = get_files_in_directory("/run/systemd/machines/", &l);
939 /* Filter out the unit: symlinks */
940 for (a = l, b = l; *a; a++) {
941 if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
957 _public_ int sd_machine_get_class(const char *machine, char **class) {
958 _cleanup_free_ char *c = NULL;
962 assert_return(machine_name_is_valid(machine), -EINVAL);
963 assert_return(class, -EINVAL);
965 p = strjoina("/run/systemd/machines/", machine);
966 r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
980 _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
981 _cleanup_free_ char *netif = NULL;
982 size_t l, allocated = 0, nr = 0;
984 const char *p, *word, *state;
987 assert_return(machine_name_is_valid(machine), -EINVAL);
988 assert_return(ifindices, -EINVAL);
990 p = strjoina("/run/systemd/machines/", machine);
991 r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
1001 FOREACH_WORD(word, l, netif, state) {
1005 *(char*) (mempcpy(buf, word, l)) = 0;
1007 if (parse_ifindex(buf, &ifi) < 0)
1010 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
1022 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
1023 return (int) (unsigned long) m - 1;
1026 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
1027 return (sd_login_monitor*) (unsigned long) (fd + 1);
1030 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
1034 assert_return(m, -EINVAL);
1036 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1040 if (!category || streq(category, "seat")) {
1041 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
1050 if (!category || streq(category, "session")) {
1051 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
1060 if (!category || streq(category, "uid")) {
1061 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
1070 if (!category || streq(category, "machine")) {
1071 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
1085 *m = FD_TO_MONITOR(fd);
1089 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1095 fd = MONITOR_TO_FD(m);
1101 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
1103 assert_return(m, -EINVAL);
1105 return flush_fd(MONITOR_TO_FD(m));
1108 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1110 assert_return(m, -EINVAL);
1112 return MONITOR_TO_FD(m);
1115 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1117 assert_return(m, -EINVAL);
1119 /* For now we will only return POLLIN here, since we don't
1120 * need anything else ever for inotify. However, let's have
1121 * this API to keep our options open should we later on need
1126 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1128 assert_return(m, -EINVAL);
1129 assert_return(timeout_usec, -EINVAL);
1131 /* For now we will only return (uint64_t) -1, since we don't
1132 * need any timeout. However, let's have this API to keep our
1133 * options open should we later on need it. */
1134 *timeout_usec = (uint64_t) -1;