sd-login.c revision 9444b1f20e311f073864d81e913bd4f32fe95cfd
/*-*- 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"
#include "fileio.h"
if (pid < 0)
return -EINVAL;
if (!session)
return -EINVAL;
}
if (pid < 0)
return -EINVAL;
if (!unit)
return -EINVAL;
}
if (pid < 0)
return -EINVAL;
if (!unit)
return -EINVAL;
}
if (pid < 0)
return -EINVAL;
if (!name)
return -EINVAL;
}
if (pid < 0)
return -EINVAL;
if (!uid)
return -EINVAL;
}
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;
}
char *w, *state;
size_t l;
int r;
const char *variable;
if (!seat)
return -EINVAL;
if (!p)
return -ENOMEM;
if (r < 0)
return r;
if (!s)
return -EIO;
return -ENOMEM;
FOREACH_WORD(w, l, s, state) {
if (strneq(t, w, l))
return 1;
}
return 0;
}
char **a;
int r;
return -ENOMEM;
r = parse_env_file(p, NEWLINE,
variable, &s,
NULL);
if (r < 0) {
if (r == -ENOENT) {
if (array)
return 0;
}
return r;
}
if (!s) {
if (array)
return 0;
}
a = strv_split(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;
r = file_of_session(session, &p);
if (r < 0)
return r;
if (r < 0)
return r;
if (!s)
return -EIO;
r = parse_boolean(s);
return r;
}
int r;
if (!state)
return -EINVAL;
r = file_of_session(session, &p);
if (r < 0)
return r;
if (r < 0)
return r;
else if (!s)
return -EIO;
*state = s;
s = NULL;
return 0;
}
int r;
if (!uid)
return -EINVAL;
r = file_of_session(session, &p);
if (r < 0)
return r;
if (r < 0)
return r;
if (!s)
return -EIO;
return r;
}
int r;
if (!value)
return -EINVAL;
r = file_of_session(session, &p);
if (r < 0)
return r;
if (r < 0)
return r;
if (isempty(s))
return -ENOENT;
*value = s;
s = NULL;
return 0;
}
}
}
}
}
}
}
char *p;
int r;
if (seat)
else {
if (r < 0)
return r;
}
if (!p)
return -ENOMEM;
*_p = p;
p = NULL;
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);
if (r < 0)
return r;
if (session && !s)
return -ENOENT;
if (uid && !t)
return -ENOENT;
if (uid && t) {
if (r < 0)
return r;
}
if (session && s) {
*session = s;
s = NULL;
}
return 0;
}
_public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
_cleanup_strv_free_ char **a = NULL;
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);
if (r < 0)
return r;
if (s) {
a = strv_split(s, " ");
if (!a)
return -ENOMEM;
}
if (uids && t) {
char *w, *state;
size_t l;
FOREACH_WORD(w, l, t, state)
n++;
if (n > 0) {
unsigned i = 0;
if (!b)
return -ENOMEM;
FOREACH_WORD(w, l, t, state) {
_cleanup_free_ char *k = NULL;
k = strndup(w, l);
if (!k)
return -ENOMEM;
r = parse_uid(k, b + i);
if (r < 0)
continue;
i++;
}
}
}
r = strv_length(a);
if (sessions) {
*sessions = a;
a = NULL;
}
if (uids) {
*uids = b;
b = NULL;
}
if (n_uids)
*n_uids = n;
return r;
}
int r;
r = file_of_seat(seat, &p);
if (r < 0)
return r;
r = parse_env_file(p, NEWLINE,
variable, &s,
NULL);
if (r < 0)
return r;
if (s)
r = parse_boolean(s);
else
r = 0;
return r;
}
}
}
}
}
}
int r = 0;
unsigned n = 0;
if (!d)
return -errno;
for (;;) {
union dirent_storage buf;
int k;
if (k != 0)
return -k;
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)
return -ENOMEM;
l = t;
}
assert((unsigned) r < n);
l[r++] = uid;
} else
r++;
}
if (users) {
*users = l;
l = NULL;
}
return r;
}
_cleanup_strv_free_ char **l = NULL;
char *n;
int c = 0, r;
r = cg_get_root_path(&md);
if (r < 0)
return r;
if (r < 0)
return r;
while ((r = cg_read_subgroup(d, &n)) > 0) {
r = strv_push(&l, n);
if (r < 0) {
free(n);
return -ENOMEM;
}
c++;
}
if (r < 0)
return r;
if (machines) {
*machines = l;
l = NULL;
}
return c;
}
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;
}
int r;
r = cg_get_root_path(&md);
if (r < 0)
return r;
if (r < 0)
return r;
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);
}
if (!m)
return -EINVAL;
/* For now we will only return POLLIN here, since we don't
* need anything else ever for inotify. However, let's have
* this API to keep our options open should we later on need
* it. */
return POLLIN;
}
if (!m)
return -EINVAL;
if (!timeout_usec)
return -EINVAL;
/* For now we will only return (uint64_t) -1, since we don't
* need any timeout. However, let's have this API to keep our
* options open should we later on need it. */
return 0;
}