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 r == -ENXIO ? -ENODATA : r;
68 _public_ int sd_pid_get_unit(pid_t pid, char **unit) {
70 assert_return(pid >= 0, -EINVAL);
71 assert_return(unit, -EINVAL);
73 #if 0 /// elogind does not support systemd units
74 return cg_pid_get_unit(pid, unit);
80 _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
81 #if 0 /// UNNEEDED by elogind
85 assert_return(pid >= 0, -EINVAL);
86 assert_return(unit, -EINVAL);
88 #if 0 /// elogind does not support systemd units
89 r = cg_pid_get_user_unit(pid, unit);
90 return r == -ENXIO ? -ENODATA : r;
96 _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
98 assert_return(pid >= 0, -EINVAL);
99 assert_return(name, -EINVAL);
101 #if 0 /// elogind does not support systemd units
102 return cg_pid_get_machine_name(pid, name);
108 _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
110 assert_return(pid >= 0, -EINVAL);
111 assert_return(slice, -EINVAL);
113 #if 0 /// elogind does not support systemd slices
114 return cg_pid_get_slice(pid, slice);
120 _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
122 assert_return(pid >= 0, -EINVAL);
123 assert_return(slice, -EINVAL);
125 #if 0 /// elogind does not support systemd slices
126 return cg_pid_get_user_slice(pid, slice);
132 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
134 assert_return(pid >= 0, -EINVAL);
135 assert_return(uid, -EINVAL);
137 #if 0 /// elogind does not support systemd slices
138 return cg_pid_get_owner_uid(pid, uid);
144 _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
148 assert_return(pid >= 0, -EINVAL);
149 assert_return(cgroup, -EINVAL);
151 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
155 /* The internal APIs return the empty string for the root
156 * cgroup, let's return the "/" in the public APIs instead, as
157 * that's easier and less ambiguous for people to grok. */
170 _public_ int sd_peer_get_session(int fd, char **session) {
171 struct ucred ucred = {};
174 assert_return(fd >= 0, -EBADF);
175 assert_return(session, -EINVAL);
177 r = getpeercred(fd, &ucred);
181 #if 0 /// elogind does not support systemd scopes
182 return cg_pid_get_session(ucred.pid, session);
188 _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
192 assert_return(fd >= 0, -EBADF);
193 assert_return(uid, -EINVAL);
195 r = getpeercred(fd, &ucred);
199 #if 0 /// elogind does not support systemd units
200 return cg_pid_get_owner_uid(ucred.pid, uid);
206 _public_ int sd_peer_get_unit(int fd, char **unit) {
210 assert_return(fd >= 0, -EBADF);
211 assert_return(unit, -EINVAL);
213 r = getpeercred(fd, &ucred);
217 #if 0 /// elogind does not support systemd units
218 return cg_pid_get_unit(ucred.pid, unit);
224 _public_ int sd_peer_get_user_unit(int fd, char **unit) {
228 assert_return(fd >= 0, -EBADF);
229 assert_return(unit, -EINVAL);
231 r = getpeercred(fd, &ucred);
235 #if 0 /// elogind does not support systemd units
236 return cg_pid_get_user_unit(ucred.pid, unit);
242 _public_ int sd_peer_get_machine_name(int fd, char **machine) {
246 assert_return(fd >= 0, -EBADF);
247 assert_return(machine, -EINVAL);
249 r = getpeercred(fd, &ucred);
253 #if 0 /// elogind does not support systemd units
254 return cg_pid_get_machine_name(ucred.pid, machine);
260 _public_ int sd_peer_get_slice(int fd, char **slice) {
264 assert_return(fd >= 0, -EBADF);
265 assert_return(slice, -EINVAL);
267 r = getpeercred(fd, &ucred);
271 #if 0 /// elogind does not support systemd slices
272 return cg_pid_get_slice(ucred.pid, slice);
278 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
282 assert_return(fd >= 0, -EBADF);
283 assert_return(slice, -EINVAL);
285 r = getpeercred(fd, &ucred);
289 #if 0 /// elogind does not support systemd slices
290 return cg_pid_get_user_slice(ucred.pid, slice);
296 _public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
300 assert_return(fd >= 0, -EBADF);
301 assert_return(cgroup, -EINVAL);
303 r = getpeercred(fd, &ucred);
307 return sd_pid_get_cgroup(ucred.pid, cgroup);
310 static int file_of_uid(uid_t uid, char **p) {
312 assert_return(uid_is_valid(uid), -EINVAL);
315 if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
321 _public_ int sd_uid_get_state(uid_t uid, char**state) {
322 _cleanup_free_ char *p = NULL;
326 assert_return(state, -EINVAL);
328 r = file_of_uid(uid, &p);
332 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
335 s = strdup("offline");
353 _public_ int sd_uid_get_display(uid_t uid, char **session) {
354 _cleanup_free_ char *p = NULL, *s = NULL;
357 assert_return(session, -EINVAL);
359 r = file_of_uid(uid, &p);
363 r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
377 static int file_of_seat(const char *seat, char **_p) {
384 if (!filename_is_valid(seat))
387 p = strappend("/run/systemd/seats/", seat);
389 _cleanup_free_ char *buf = NULL;
391 r = sd_session_get_seat(NULL, &buf);
395 p = strappend("/run/systemd/seats/", buf);
406 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
407 _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
410 const char *word, *variable, *state;
412 assert_return(uid_is_valid(uid), -EINVAL);
414 r = file_of_seat(seat, &p);
418 variable = require_active ? "ACTIVE_UID" : "UIDS";
420 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
428 if (asprintf(&t, UID_FMT, uid) < 0)
431 FOREACH_WORD(word, l, s, state)
432 if (strneq(t, word, l))
438 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
439 _cleanup_free_ char *p = NULL, *s = NULL;
445 r = file_of_uid(uid, &p);
449 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
450 if (r == -ENOENT || (r >= 0 && isempty(s))) {
458 a = strv_split(s, " ");
473 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
474 return uid_get_array(
476 require_active == 0 ? "ONLINE_SESSIONS" :
477 require_active > 0 ? "ACTIVE_SESSIONS" :
482 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
483 return uid_get_array(
485 require_active == 0 ? "ONLINE_SEATS" :
486 require_active > 0 ? "ACTIVE_SEATS" :
491 static int file_of_session(const char *session, char **_p) {
498 if (!session_id_valid(session))
501 p = strappend("/run/systemd/sessions/", session);
503 _cleanup_free_ char *buf = NULL;
505 r = sd_pid_get_session(0, &buf);
509 p = strappend("/run/systemd/sessions/", buf);
519 _public_ int sd_session_is_active(const char *session) {
520 _cleanup_free_ char *p = NULL, *s = NULL;
523 r = file_of_session(session, &p);
527 r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
535 return parse_boolean(s);
538 _public_ int sd_session_is_remote(const char *session) {
539 _cleanup_free_ char *p = NULL, *s = NULL;
542 r = file_of_session(session, &p);
546 r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
554 return parse_boolean(s);
557 _public_ int sd_session_get_state(const char *session, char **state) {
558 _cleanup_free_ char *p = NULL, *s = NULL;
561 assert_return(state, -EINVAL);
563 r = file_of_session(session, &p);
567 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
581 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
583 _cleanup_free_ char *p = NULL, *s = NULL;
585 assert_return(uid, -EINVAL);
587 r = file_of_session(session, &p);
591 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
599 return parse_uid(s, uid);
602 static int session_get_string(const char *session, const char *field, char **value) {
603 _cleanup_free_ char *p = NULL, *s = NULL;
606 assert_return(value, -EINVAL);
609 r = file_of_session(session, &p);
613 r = parse_env_file(p, NEWLINE, field, &s, NULL);
626 _public_ int sd_session_get_seat(const char *session, char **seat) {
627 return session_get_string(session, "SEAT", seat);
630 _public_ int sd_session_get_tty(const char *session, char **tty) {
631 return session_get_string(session, "TTY", tty);
634 _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
635 _cleanup_free_ char *vtnr_string = NULL;
639 assert_return(vtnr, -EINVAL);
641 r = session_get_string(session, "VTNR", &vtnr_string);
645 r = safe_atou(vtnr_string, &u);
653 _public_ int sd_session_get_service(const char *session, char **service) {
654 return session_get_string(session, "SERVICE", service);
657 _public_ int sd_session_get_type(const char *session, char **type) {
658 return session_get_string(session, "TYPE", type);
661 _public_ int sd_session_get_class(const char *session, char **class) {
662 return session_get_string(session, "CLASS", class);
665 _public_ int sd_session_get_desktop(const char *session, char **desktop) {
666 _cleanup_free_ char *escaped = NULL;
670 assert_return(desktop, -EINVAL);
672 r = session_get_string(session, "DESKTOP", &escaped);
676 r = cunescape(escaped, 0, &t);
684 _public_ int sd_session_get_display(const char *session, char **display) {
685 return session_get_string(session, "DISPLAY", display);
688 _public_ int sd_session_get_remote_user(const char *session, char **remote_user) {
689 return session_get_string(session, "REMOTE_USER", remote_user);
692 _public_ int sd_session_get_remote_host(const char *session, char **remote_host) {
693 return session_get_string(session, "REMOTE_HOST", remote_host);
696 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
697 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
700 assert_return(session || uid, -EINVAL);
702 r = file_of_seat(seat, &p);
706 r = parse_env_file(p, NEWLINE,
722 r = parse_uid(t, uid);
735 _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
736 _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
737 _cleanup_strv_free_ char **a = NULL;
738 _cleanup_free_ uid_t *b = NULL;
742 r = file_of_seat(seat, &p);
746 r = parse_env_file(p, NEWLINE,
756 a = strv_split(s, " ");
762 const char *word, *state;
765 FOREACH_WORD(word, l, t, state)
775 FOREACH_WORD(word, l, t, state) {
776 _cleanup_free_ char *k = NULL;
778 k = strndup(word, l);
782 r = parse_uid(k, b + i);
809 static int seat_get_can(const char *seat, const char *variable) {
810 _cleanup_free_ char *p = NULL, *s = NULL;
815 r = file_of_seat(seat, &p);
819 r = parse_env_file(p, NEWLINE,
829 return parse_boolean(s);
832 _public_ int sd_seat_can_multi_session(const char *seat) {
833 return seat_get_can(seat, "CAN_MULTI_SESSION");
836 _public_ int sd_seat_can_tty(const char *seat) {
837 return seat_get_can(seat, "CAN_TTY");
840 _public_ int sd_seat_can_graphical(const char *seat) {
841 return seat_get_can(seat, "CAN_GRAPHICAL");
844 _public_ int sd_get_seats(char ***seats) {
845 return get_files_in_directory("/run/systemd/seats/", seats);
848 _public_ int sd_get_sessions(char ***sessions) {
849 return get_files_in_directory("/run/systemd/sessions/", sessions);
852 _public_ int sd_get_uids(uid_t **users) {
853 _cleanup_closedir_ DIR *d;
857 _cleanup_free_ uid_t *l = NULL;
859 d = opendir("/run/systemd/users/");
863 FOREACH_DIRENT_ALL(de, d, return -errno) {
867 dirent_ensure_type(d, de);
869 if (!dirent_is_file(de))
872 k = parse_uid(de->d_name, &uid);
877 if ((unsigned) r >= n) {
881 t = realloc(l, sizeof(uid_t) * n);
888 assert((unsigned) r < n);
902 _public_ int sd_get_machine_names(char ***machines) {
903 char **l = NULL, **a, **b;
906 assert_return(machines, -EINVAL);
908 r = get_files_in_directory("/run/systemd/machines/", &l);
915 /* Filter out the unit: symlinks */
916 for (a = l, b = l; *a; a++) {
917 if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
933 _public_ int sd_machine_get_class(const char *machine, char **class) {
934 _cleanup_free_ char *c = NULL;
938 assert_return(machine_name_is_valid(machine), -EINVAL);
939 assert_return(class, -EINVAL);
941 p = strjoina("/run/systemd/machines/", machine);
942 r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
956 _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
957 _cleanup_free_ char *netif = NULL;
958 size_t l, allocated = 0, nr = 0;
960 const char *p, *word, *state;
963 assert_return(machine_name_is_valid(machine), -EINVAL);
964 assert_return(ifindices, -EINVAL);
966 p = strjoina("/run/systemd/machines/", machine);
967 r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
977 FOREACH_WORD(word, l, netif, state) {
981 *(char*) (mempcpy(buf, word, l)) = 0;
983 if (parse_ifindex(buf, &ifi) < 0)
986 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
998 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
999 return (int) (unsigned long) m - 1;
1002 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
1003 return (sd_login_monitor*) (unsigned long) (fd + 1);
1006 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
1010 assert_return(m, -EINVAL);
1012 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1016 if (!category || streq(category, "seat")) {
1017 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
1026 if (!category || streq(category, "session")) {
1027 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
1036 if (!category || streq(category, "uid")) {
1037 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
1046 if (!category || streq(category, "machine")) {
1047 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
1061 *m = FD_TO_MONITOR(fd);
1065 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
1071 fd = MONITOR_TO_FD(m);
1077 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
1079 assert_return(m, -EINVAL);
1081 return flush_fd(MONITOR_TO_FD(m));
1084 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
1086 assert_return(m, -EINVAL);
1088 return MONITOR_TO_FD(m);
1091 _public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
1093 assert_return(m, -EINVAL);
1095 /* For now we will only return POLLIN here, since we don't
1096 * need anything else ever for inotify. However, let's have
1097 * this API to keep our options open should we later on need
1102 _public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
1104 assert_return(m, -EINVAL);
1105 assert_return(timeout_usec, -EINVAL);
1107 /* For now we will only return (uint64_t) -1, since we don't
1108 * need any timeout. However, let's have this API to keep our
1109 * options open should we later on need it. */
1110 *timeout_usec = (uint64_t) -1;