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/>.
27 #include "cgroup-util.h"
31 _public_ int sd_pid_get_session(pid_t pid, char **session) {
33 char *cg_process, *cg_init, *p;
44 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &cg_process);
48 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &cg_init);
54 if (endswith(cg_init, "/system"))
55 cg_init[strlen(cg_init)-7] = 0;
56 else if (streq(cg_init, "/"))
59 if (startswith(cg_process, cg_init))
60 p = cg_process + strlen(cg_init);
66 if (!startswith(p, "/user/")) {
72 if (startswith(p, "shared/") || streq(p, "shared")) {
84 p = strndup(p, strcspn(p, "/"));
94 _public_ int sd_uid_get_state(uid_t uid, char**state) {
101 if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
104 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
109 s = strdup("offline");
125 static int uid_is_on_seat_internal(uid_t uid, const char *seat, const char *variable) {
126 char *p, *w, *t, *state, *s = NULL;
133 p = strappend("/run/systemd/seats/", seat);
137 r = parse_env_file(p, NEWLINE, "UIDS", &s, NULL);
148 if (asprintf(&t, "%lu", (unsigned long) uid) < 0) {
153 FOREACH_WORD(w, l, s, state) {
154 if (strncmp(t, w, l) == 0) {
168 _public_ int sd_uid_is_on_seat(uid_t uid, const char *seat) {
169 return uid_is_on_seat_internal(uid, seat, "UIDS");
172 _public_ int sd_uid_is_active_on_seat(uid_t uid, const char *seat) {
173 return uid_is_on_seat_internal(uid, seat, "ACTIVE_UID");
176 _public_ int sd_session_is_active(const char *session) {
183 p = strappend("/run/systemd/sessions/", session);
187 r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
198 r = parse_boolean(s);
204 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
214 p = strappend("/run/systemd/sessions/", session);
218 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
229 r = safe_atolu(s, &ul);
239 _public_ int sd_session_get_seat(const char *session, char **seat) {
248 p = strappend("/run/systemd/sessions/", session);
252 r = parse_env_file(p, NEWLINE, "SEAT", &s, NULL);
267 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
268 char *p, *s = NULL, *t = NULL;
273 if (!session && !uid)
276 p = strappend("/run/systemd/seats/", seat);
280 r = parse_env_file(p, NEWLINE,
305 r = safe_atolu(t, &ul);