1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/inotify.h>
28 #include "cgroup-util.h"
33 static int pid_get_cgroup(pid_t pid, char **root, char **cgroup) {
34 char *cg_process, *cg_init, *p;
43 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &cg_process);
47 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &cg_init);
53 if (endswith(cg_init, "/system"))
54 cg_init[strlen(cg_init)-7] = 0;
55 else if (streq(cg_init, "/"))
58 if (startswith(cg_process, cg_init))
59 p = cg_process + strlen(cg_init);
78 cg_process[p-cg_process] = 0;
86 _public_ int sd_pid_get_session(pid_t pid, char **session) {
93 r = pid_get_cgroup(pid, NULL, &cgroup);
97 if (!startswith(cgroup, "/user/")) {
102 p = strchr(cgroup + 6, '/');
109 if (startswith(p, "shared/") || streq(p, "shared")) {
114 p = strndup(p, strcspn(p, "/"));
124 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
126 char *root, *cgroup, *p, *cc;
132 r = pid_get_cgroup(pid, &root, &cgroup);
136 if (!startswith(cgroup, "/user/")) {
142 p = strchr(cgroup + 6, '/');
149 p += strcspn(p, "/");
152 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, cgroup, &cc);
165 if (!S_ISDIR(st.st_mode))
172 _public_ int sd_uid_get_state(uid_t uid, char**state) {
179 if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
182 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
187 s = strdup("offline");
203 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
204 char *p, *w, *t, *state, *s = NULL;
207 const char *variable;
212 variable = require_active ? "ACTIVE_UID" : "UIDS";
214 p = strappend("/run/systemd/seats/", seat);
218 r = parse_env_file(p, NEWLINE, variable, &s, NULL);
229 if (asprintf(&t, "%lu", (unsigned long) uid) < 0) {
234 FOREACH_WORD(w, l, s, state) {
235 if (strncmp(t, w, l) == 0) {
249 static int uid_get_array(uid_t uid, const char *variable, char ***array) {
254 if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
257 r = parse_env_file(p, NEWLINE,
280 a = strv_split(s, " ");
297 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
298 return uid_get_array(uid, require_active ? "ACTIVE_SESSIONS" : "SESSIONS", sessions);
301 _public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
302 return uid_get_array(uid, require_active ? "ACTIVE_SEATS" : "SEATS", seats);
305 _public_ int sd_session_is_active(const char *session) {
312 p = strappend("/run/systemd/sessions/", session);
316 r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
327 r = parse_boolean(s);
333 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
342 p = strappend("/run/systemd/sessions/", session);
346 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
357 r = parse_uid(s, uid);
363 _public_ int sd_session_get_seat(const char *session, char **seat) {
372 p = strappend("/run/systemd/sessions/", session);
376 r = parse_env_file(p, NEWLINE, "SEAT", &s, NULL);
391 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
392 char *p, *s = NULL, *t = NULL;
397 if (!session && !uid)
400 p = strappend("/run/systemd/seats/", seat);
404 r = parse_env_file(p, NEWLINE,
427 r = parse_uid(t, uid);
445 _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
446 char *p, *s = NULL, *t = NULL, **a = NULL;
454 p = strappend("/run/systemd/seats/", seat);
458 r = parse_env_file(p, NEWLINE,
460 "ACTIVE_SESSIONS", &t,
471 a = strv_split(s, " ");
486 FOREACH_WORD(w, l, t, state)
495 FOREACH_WORD(w, l, t, state) {
505 r = parse_uid(k, b + i);
532 _public_ int sd_seat_can_multi_session(const char *seat) {
539 p = strappend("/run/systemd/seats/", seat);
543 r = parse_env_file(p, NEWLINE,
554 r = parse_boolean(s);
562 _public_ int sd_get_seats(char ***seats) {
563 return get_files_in_directory("/run/systemd/seats/", seats);
566 _public_ int sd_get_sessions(char ***sessions) {
567 return get_files_in_directory("/run/systemd/sessions/", sessions);
570 _public_ int sd_get_uids(uid_t **users) {
576 d = opendir("/run/systemd/users/");
578 struct dirent buffer, *de;
582 k = readdir_r(d, &buffer, &de);
591 dirent_ensure_type(d, de);
593 if (!dirent_is_file(de))
596 k = parse_uid(de->d_name, &uid);
601 if ((unsigned) r >= n) {
605 t = realloc(l, sizeof(uid_t) * n);
614 assert((unsigned) r < n);
633 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
634 return (int) (unsigned long) m - 1;
637 static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
638 return (sd_login_monitor*) (unsigned long) (fd + 1);
641 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
648 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
652 if (!category || streq(category, "seat")) {
653 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
655 close_nointr_nofail(fd);
662 if (!category || streq(category, "session")) {
663 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
665 close_nointr_nofail(fd);
672 if (!category || streq(category, "uid")) {
673 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
675 close_nointr_nofail(fd);
687 *m = FD_TO_MONITOR(fd);
691 _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
697 fd = MONITOR_TO_FD(m);
703 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
708 return flush_fd(MONITOR_TO_FD(m));
711 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
716 return MONITOR_TO_FD(m);