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) {
60 assert_return(pid >= 0, -EINVAL);
61 assert_return(session, -EINVAL);
63 return cg_pid_get_session(pid, session);
66 _public_ int sd_pid_get_unit(pid_t pid, char **unit) {
68 assert_return(pid >= 0, -EINVAL);
69 assert_return(unit, -EINVAL);
71 #if 0 /// elogind does not support systemd units
72 return cg_pid_get_unit(pid, unit);
78 _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
80 assert_return(pid >= 0, -EINVAL);
81 assert_return(unit, -EINVAL);
83 #if 0 /// elogind does not support systemd units
84 return cg_pid_get_user_unit(pid, unit);
90 _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
92 assert_return(pid >= 0, -EINVAL);
93 assert_return(name, -EINVAL);
95 #if 0 /// elogind does not support systemd units
96 return cg_pid_get_machine_name(pid, name);
102 _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
104 assert_return(pid >= 0, -EINVAL);
105 assert_return(slice, -EINVAL);
107 #if 0 /// elogind does not support systemd slices
108 return cg_pid_get_slice(pid, slice);
114 _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
116 assert_return(pid >= 0, -EINVAL);
117 assert_return(slice, -EINVAL);
119 #if 0 /// elogind does not support systemd slices
120 return cg_pid_get_user_slice(pid, slice);
126 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
128 assert_return(pid >= 0, -EINVAL);
129 assert_return(uid, -EINVAL);
131 #if 0 /// elogind does not support systemd slices
132 return cg_pid_get_owner_uid(pid, uid);
138 _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
142 assert_return(pid >= 0, -EINVAL);
143 assert_return(cgroup, -EINVAL);
145 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
149 /* The internal APIs return the empty string for the root
150 * cgroup, let's return the "/" in the public APIs instead, as
151 * that's easier and less ambiguous for people to grok. */
164 _public_ int sd_peer_get_session(int fd, char **session) {
165 struct ucred ucred = {};
168 assert_return(fd >= 0, -EBADF);
169 assert_return(session, -EINVAL);
171 r = getpeercred(fd, &ucred);
175 #if 0 /// elogind does not support systemd scopes
176 return cg_pid_get_session(ucred.pid, session);
182 _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
186 assert_return(fd >= 0, -EBADF);
187 assert_return(uid, -EINVAL);
189 r = getpeercred(fd, &ucred);
193 #if 0 /// elogind does not support systemd units
194 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 #if 0 /// elogind does not support systemd slices
266 return cg_pid_get_slice(ucred.pid, slice);
272 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
276 assert_return(fd >= 0, -EBADF);
277 assert_return(slice, -EINVAL);
279 r = getpeercred(fd, &ucred);
283 #if 0 /// elogind does not support systemd slices
284 return cg_pid_get_user_slice(ucred.pid, slice);
290 _public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
294 assert_return(fd >= 0, -EBADF);
295 assert_return(cgroup, -EINVAL);
297 r = getpeercred(fd, &ucred);
301 return sd_pid_get_cgroup(ucred.pid, cgroup);
304 static int file_of_uid(uid_t uid, char **p) {
306 assert_return(uid_is_valid(uid), -EINVAL);
309 if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
315 _public_ int sd_uid_get_state(uid_t uid, char**state) {
316 _cleanup_free_ char *p = NULL;
320 assert_return(state, -EINVAL);
322 r = file_of_uid(uid, &p);
326 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
329 s = strdup("offline");
347 _public_ int sd_uid_get_display(uid_t uid, char **session) {
348 _cleanup_free_ char *p = NULL, *s = NULL;
351 assert_return(session, -EINVAL);
353 r = file_of_uid(uid, &p);
357 r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
371 static int file_of_seat(const char *seat, char **_p) {
378 if (!filename_is_valid(seat))
381 p = strappend("/run/systemd/seats/", seat);
383 _cleanup_free_ char *buf = NULL;
385 r = sd_session_get_seat(NULL, &buf);
389 p = strappend("/run/systemd/seats/", buf);
400 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
401 _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
404 const char *word, *variable, *state;
406 assert_return(uid_is_valid(uid), -EINVAL);
408 r = file_of_seat(seat, &p);
412 variable = require_active ? "ACTIVE_UID" : "UIDS";
414 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
422 if (asprintf(&t, UID_FMT, uid) < 0)
425 FOREACH_WORD(word, l, s, state)
426 if (strneq(t, word, l))
432 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
433 _cleanup_free_ char *p = NULL, *s = NULL;
439 r = file_of_uid(uid, &p);
443 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
444 if (r == -ENOENT || (r >= 0 && isempty(s))) {
452 a = strv_split(s, " ");
467 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
468 return uid_get_array(
470 require_active == 0 ? "ONLINE_SESSIONS" :
471 require_active > 0 ? "ACTIVE_SESSIONS" :
476 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
477 return uid_get_array(
479 require_active == 0 ? "ONLINE_SEATS" :
480 require_active > 0 ? "ACTIVE_SEATS" :
485 static int file_of_session(const char *session, char **_p) {
492 if (!session_id_valid(session))
495 p = strappend("/run/systemd/sessions/", session);
497 _cleanup_free_ char *buf = NULL;
499 r = sd_pid_get_session(0, &buf);
503 p = strappend("/run/systemd/sessions/", buf);
513 _public_ int sd_session_is_active(const char *session) {
514 _cleanup_free_ char *p = NULL, *s = NULL;
517 r = file_of_session(session, &p);
521 r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
529 return parse_boolean(s);
532 _public_ int sd_session_is_remote(const char *session) {
533 _cleanup_free_ char *p = NULL, *s = NULL;
536 r = file_of_session(session, &p);
540 r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
548 return parse_boolean(s);
551 _public_ int sd_session_get_state(const char *session, char **state) {
552 _cleanup_free_ char *p = NULL, *s = NULL;
555 assert_return(state, -EINVAL);
557 r = file_of_session(session, &p);
561 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
575 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
577 _cleanup_free_ char *p = NULL, *s = NULL;
579 assert_return(uid, -EINVAL);
581 r = file_of_session(session, &p);
585 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
593 return parse_uid(s, uid);
596 static int session_get_string(const char *session, const char *field, char **value) {
597 _cleanup_free_ char *p = NULL, *s = NULL;
600 assert_return(value, -EINVAL);
603 r = file_of_session(session, &p);
607 r = parse_env_file(p, NEWLINE, field, &s, NULL);
620 _public_ int sd_session_get_seat(const char *session, char **seat) {
621 return session_get_string(session, "SEAT", seat);
624 _public_ int sd_session_get_tty(const char *session, char **tty) {
625 return session_get_string(session, "TTY", tty);
628 _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
629 _cleanup_free_ char *vtnr_string = NULL;
633 assert_return(vtnr, -EINVAL);
635 r = session_get_string(session, "VTNR", &vtnr_string);
639 r = safe_atou(vtnr_string, &u);
647 _public_ int sd_session_get_service(const char *session, char **service) {
648 return session_get_string(session, "SERVICE", service);
651 _public_ int sd_session_get_type(const char *session, char **type) {
652 return session_get_string(session, "TYPE", type);
655 _public_ int sd_session_get_class(const char *session, char **class) {
656 return session_get_string(session, "CLASS", class);
659 _public_ int sd_session_get_desktop(const char *session, char **desktop) {
660 _cleanup_free_ char *escaped = NULL;
664 assert_return(desktop, -EINVAL);
666 r = session_get_string(session, "DESKTOP", &escaped);
670 r = cunescape(escaped, 0, &t);
678 _public_ int sd_session_get_display(const char *session, char **display) {
679 return session_get_string(session, "DISPLAY", display);
682 _public_ int sd_session_get_remote_user(const char *session, char **remote_user) {
683 return session_get_string(session, "REMOTE_USER", remote_user);
686 _public_ int sd_session_get_remote_host(const char *session, char **remote_host) {
687 return session_get_string(session, "REMOTE_HOST", remote_host);
690 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
691 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
694 assert_return(session || uid, -EINVAL);
696 r = file_of_seat(seat, &p);
700 r = parse_env_file(p, NEWLINE,
716 r = parse_uid(t, uid);
729 _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
730 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
731 _cleanup_strv_free_ char **a = NULL;
732 _cleanup_free_ uid_t *b = NULL;
736 r = file_of_seat(seat, &p);
740 r = parse_env_file(p, NEWLINE,
742 "ACTIVE_SESSIONS", &t,
750 a = strv_split(s, " ");
756 const char *word, *state;
759 FOREACH_WORD(word, l, t, state)
769 FOREACH_WORD(word, l, t, state) {
770 _cleanup_free_ char *k = NULL;
772 k = strndup(word, l);
776 r = parse_uid(k, b + i);
803 static int seat_get_can(const char *seat, const char *variable) {
804 _cleanup_free_ char *p = NULL, *s = NULL;
809 r = file_of_seat(seat, &p);
813 r = parse_env_file(p, NEWLINE,
823 return parse_boolean(s);
826 _public_ int sd_seat_can_multi_session(const char *seat) {
827 return seat_get_can(seat, "CAN_MULTI_SESSION");
830 _public_ int sd_seat_can_tty(const char *seat) {
831 return seat_get_can(seat, "CAN_TTY");
834 _public_ int sd_seat_can_graphical(const char *seat) {
835 return seat_get_can(seat, "CAN_GRAPHICAL");
838 _public_ int sd_get_seats(char ***seats) {
839 return get_files_in_directory("/run/systemd/seats/", seats);
842 _public_ int sd_get_sessions(char ***sessions) {
843 return get_files_in_directory("/run/systemd/sessions/", sessions);
846 _public_ int sd_get_uids(uid_t **users) {
847 _cleanup_closedir_ DIR *d;
851 _cleanup_free_ uid_t *l = NULL;
853 d = opendir("/run/systemd/users/");
857 FOREACH_DIRENT_ALL(de, d, return -errno) {
861 dirent_ensure_type(d, de);
863 if (!dirent_is_file(de))
866 k = parse_uid(de->d_name, &uid);
871 if ((unsigned) r >= n) {
875 t = realloc(l, sizeof(uid_t) * n);
882 assert((unsigned) r < n);
896 _public_ int sd_get_machine_names(char ***machines) {
897 char **l = NULL, **a, **b;
900 assert_return(machines, -EINVAL);
902 r = get_files_in_directory("/run/systemd/machines/", &l);
909 /* Filter out the unit: symlinks */
910 for (a = l, b = l; *a; a++) {
911 if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
927 _public_ int sd_machine_get_class(const char *machine, char **class) {
928 _cleanup_free_ char *c = NULL;
932 assert_return(machine_name_is_valid(machine), -EINVAL);
933 assert_return(class, -EINVAL);
935 p = strjoina("/run/systemd/machines/", machine);
936 r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
950 _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
951 _cleanup_free_ char *netif = NULL;
952 size_t l, allocated = 0, nr = 0;
954 const char *p, *word, *state;
957 assert_return(machine_name_is_valid(machine), -EINVAL);
958 assert_return(ifindices, -EINVAL);
960 p = strjoina("/run/systemd/machines/", machine);
961 r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
971 FOREACH_WORD(word, l, netif, state) {
975 *(char*) (mempcpy(buf, word, l)) = 0;
977 if (parse_ifindex(buf, &ifi) < 0)
980 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
992 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
993 return (int) (unsigned long) m - 1;
996 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
997 return (sd_login_monitor*) (unsigned long) (fd + 1);
1000 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
1004 assert_return(m, -EINVAL);
1006 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1010 if (!category || streq(category, "seat")) {
1011 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
1020 if (!category || streq(category, "session")) {
1021 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
1030 if (!category || streq(category, "uid")) {
1031 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
1040 if (!category || streq(category, "machine")) {
1041 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
1055 *m = FD_TO_MONITOR(fd);
1059 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1065 fd = MONITOR_TO_FD(m);
1071 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
1073 assert_return(m, -EINVAL);
1075 return flush_fd(MONITOR_TO_FD(m));
1078 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1080 assert_return(m, -EINVAL);
1082 return MONITOR_TO_FD(m);
1085 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1087 assert_return(m, -EINVAL);
1089 /* For now we will only return POLLIN here, since we don't
1090 * need anything else ever for inotify. However, let's have
1091 * this API to keep our options open should we later on need
1096 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1098 assert_return(m, -EINVAL);
1099 assert_return(timeout_usec, -EINVAL);
1101 /* For now we will only return (uint64_t) -1, since we don't
1102 * need any timeout. However, let's have this API to keep our
1103 * options open should we later on need it. */
1104 *timeout_usec = (uint64_t) -1;