sd-login.c revision 2826d14091e43ed3397d862dee79d09d0115c84e
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "util.h"
#include "cgroup-util.h"
#include "macro.h"
#include "sd-login.h"
#include "strv.h"
int r;
char *cgroup, *p;
if (pid < 0)
return -EINVAL;
if (!session)
return -EINVAL;
if (r < 0)
return r;
return -ENOENT;
}
if (!p) {
return -ENOENT;
}
p++;
return -ENOENT;
}
if (!p)
return -ENOMEM;
*session = p;
return 0;
}
if (pid < 0)
return -EINVAL;
if (!unit)
return -EINVAL;
}
int r;
if (pid < 0)
return -EINVAL;
if (!uid)
return -EINVAL;
if (r < 0)
return r;
return -ENOENT;
}
if (!p) {
return -ENOENT;
}
p++;
p += strcspn(p, "/");
*p = 0;
if (r < 0)
return -ENOMEM;
if (r < 0)
return -errno;
return -ENOTDIR;
return 0;
}
char *p, *s = NULL;
int r;
if (!state)
return -EINVAL;
return -ENOMEM;
free(p);
if (r == -ENOENT) {
free(s);
s = strdup("offline");
if (!s)
return -ENOMEM;
*state = s;
return 0;
} else if (r < 0) {
free(s);
return r;
} else if (!s)
return -EIO;
*state = s;
return 0;
}
size_t l;
int r;
const char *variable;
if (!seat)
return -EINVAL;
if (!p)
return -ENOMEM;
free(p);
if (r < 0) {
free(s);
return r;
}
if (!s)
return -EIO;
free(s);
return -ENOMEM;
}
FOREACH_WORD(w, l, s, state) {
if (strneq(t, w, l)) {
free(s);
free(t);
return 1;
}
}
free(s);
free(t);
return 0;
}
char *p, *s = NULL;
char **a;
int r;
return -ENOMEM;
r = parse_env_file(p, NEWLINE,
variable, &s,
NULL);
free(p);
if (r < 0) {
free(s);
if (r == -ENOENT) {
if (array)
return 0;
}
return r;
}
if (!s) {
if (array)
return 0;
}
a = strv_split(s, " ");
free(s);
if (!a)
return -ENOMEM;
strv_uniq(a);
r = strv_length(a);
if (array)
*array = a;
else
strv_free(a);
return r;
}
return uid_get_array(
uid,
require_active == 0 ? "ONLINE_SESSIONS" :
require_active > 0 ? "ACTIVE_SESSIONS" :
"SESSIONS",
sessions);
}
return uid_get_array(
uid,
require_active == 0 ? "ONLINE_SEATS" :
require_active > 0 ? "ACTIVE_SEATS" :
"SEATS",
seats);
}
char *p;
int r;
if (session)
else {
char *buf;
r = sd_pid_get_session(0, &buf);
if (r < 0)
return r;
}
if (!p)
return -ENOMEM;
*_p = p;
return 0;
}
int r;
char *p, *s = NULL;
r = file_of_session(session, &p);
if (r < 0)
return r;
free(p);
if (r < 0) {
free(s);
return r;
}
if (!s)
return -EIO;
r = parse_boolean(s);
free(s);
return r;
}
char *p, *s = NULL;
int r;
if (!state)
return -EINVAL;
r = file_of_session(session, &p);
if (r < 0)
return r;
free(p);
if (r < 0) {
free(s);
return r;
} else if (!s)
return -EIO;
*state = s;
return 0;
}
int r;
char *p, *s = NULL;
if (!uid)
return -EINVAL;
r = file_of_session(session, &p);
if (r < 0)
return r;
free(p);
if (r < 0) {
free(s);
return r;
}
if (!s)
return -EIO;
free(s);
return r;
}
char *p, *s = NULL;
int r;
if (!value)
return -EINVAL;
r = file_of_session(session, &p);
if (r < 0)
return r;
free(p);
if (r < 0) {
free(s);
return r;
}
if (isempty(s))
return -ENOENT;
*value = s;
return 0;
}
}
}
}
}
}
}
char *p;
int r;
if (seat)
else {
char *buf;
if (r < 0)
return r;
}
if (!p)
return -ENOMEM;
*_p = p;
return 0;
}
int r;
return -EINVAL;
r = file_of_seat(seat, &p);
if (r < 0)
return r;
r = parse_env_file(p, NEWLINE,
"ACTIVE", &s,
"ACTIVE_UID", &t,
NULL);
free(p);
if (r < 0) {
free(s);
free(t);
return r;
}
if (session && !s) {
free(t);
return -ENOENT;
}
if (uid && !t) {
free(s);
return -ENOENT;
}
if (uid && t) {
if (r < 0) {
free(t);
free(s);
return r;
}
}
free(t);
if (session && s)
*session = s;
else
free(s);
return 0;
}
_public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
unsigned n = 0;
int r;
r = file_of_seat(seat, &p);
if (r < 0)
return r;
r = parse_env_file(p, NEWLINE,
"SESSIONS", &s,
"ACTIVE_SESSIONS", &t,
NULL);
free(p);
if (r < 0) {
free(s);
free(t);
return r;
}
if (s) {
a = strv_split(s, " ");
if (!a) {
free(s);
free(t);
return -ENOMEM;
}
}
free(s);
if (uids && t) {
char *w, *state;
size_t l;
FOREACH_WORD(w, l, t, state)
n++;
if (n == 0)
b = NULL;
else {
unsigned i = 0;
if (!b) {
strv_free(a);
return -ENOMEM;
}
FOREACH_WORD(w, l, t, state) {
char *k;
k = strndup(w, l);
if (!k) {
free(t);
free(b);
strv_free(a);
return -ENOMEM;
}
r = parse_uid(k, b + i);
free(k);
if (r < 0)
continue;
i++;
}
}
}
free(t);
r = strv_length(a);
if (sessions)
*sessions = a;
else
strv_free(a);
if (uids)
*uids = b;
if (n_uids)
*n_uids = n;
return r;
}
char *p, *s = NULL;
int r;
r = file_of_seat(seat, &p);
if (r < 0)
return r;
r = parse_env_file(p, NEWLINE,
variable, &s,
NULL);
free(p);
if (r < 0) {
free(s);
return r;
}
if (s) {
r = parse_boolean(s);
free(s);
} else
r = 0;
return r;
}
}
}
}
}
}
DIR *d;
int r = 0;
unsigned n = 0;
if (!d)
return -errno;
for (;;) {
union dirent_storage buf;
int k;
if (k != 0) {
r = -k;
goto finish;
}
if (!de)
break;
dirent_ensure_type(d, de);
if (!dirent_is_file(de))
continue;
if (k < 0)
continue;
if (users) {
if ((unsigned) r >= n) {
uid_t *t;
if (!t) {
r = -ENOMEM;
goto finish;
}
l = t;
}
assert((unsigned) r < n);
l[r++] = uid;
} else
r++;
}
if (d)
closedir(d);
if (r >= 0) {
if (users)
*users = l;
} else
free(l);
return r;
}
static inline int MONITOR_TO_FD(sd_login_monitor *m) {
return (int) (unsigned long) m - 1;
}
}
int fd, k;
bool good = false;
if (!m)
return -EINVAL;
if (fd < 0)
return errno;
if (k < 0) {
return -errno;
}
good = true;
}
if (k < 0) {
return -errno;
}
good = true;
}
if (k < 0) {
return -errno;
}
good = true;
}
if (!good) {
return -EINVAL;
}
*m = FD_TO_MONITOR(fd);
return 0;
}
int fd;
if (!m)
return NULL;
fd = MONITOR_TO_FD(m);
return NULL;
}
if (!m)
return -EINVAL;
return flush_fd(MONITOR_TO_FD(m));
}
if (!m)
return -EINVAL;
return MONITOR_TO_FD(m);
}