sd-login.c revision a7f7d1bde43fc825c49afea3f946f5b4b3d563e0
/*-*- 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 <poll.h>
#include "util.h"
#include "cgroup-util.h"
#include "macro.h"
#include "strv.h"
#include "fileio.h"
#include "login-shared.h"
#include "sd-login.h"
}
}
}
}
}
}
int r;
if (r < 0)
return r;
}
int r;
if (r < 0)
return r;
}
int r;
if (r < 0)
return r;
}
int r;
if (r < 0)
return r;
}
int r;
if (r < 0)
return r;
}
int r;
if (r < 0)
return r;
}
assert(p);
return -ENOMEM;
return 0;
}
_cleanup_free_ char *p = NULL;
char *s = NULL;
int r;
r = file_of_uid(uid, &p);
if (r < 0)
return r;
if (r == -ENOENT) {
free(s);
s = strdup("offline");
if (!s)
return -ENOMEM;
} else if (r < 0) {
free(s);
return r;
} else if (!s)
return -EIO;
*state = s;
return 0;
}
int r;
r = file_of_uid(uid, &p);
if (r < 0)
return r;
if (r < 0)
return r;
if (isempty(s))
return -ENOENT;
*session = s;
s = NULL;
return 0;
}
size_t l;
int r;
if (!p)
return -ENOMEM;
if (r < 0)
return r;
if (!s)
return -EIO;
return -ENOMEM;
return 1;
}
return 0;
}
char **a;
int r;
r = file_of_uid(uid, &p);
if (r < 0)
return r;
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) {
if (!session_id_valid(session))
return -EINVAL;
} else {
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;
return parse_boolean(s);
}
int r;
r = file_of_session(session, &p);
if (r < 0)
return r;
if (r < 0)
return r;
if (!s)
return -EIO;
return parse_boolean(s);
}
int r;
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;
r = file_of_session(session, &p);
if (r < 0)
return r;
if (r < 0)
return r;
if (!s)
return -EIO;
}
int r;
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;
}
}
}
unsigned u;
int r;
if (r < 0)
return r;
r = safe_atou(vtnr_string, &u);
if (r < 0)
return r;
*vtnr = u;
return 0;
}
}
}
}
char *t;
int r;
if (r < 0)
return r;
if (!t)
return -ENOMEM;
*desktop = t;
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;
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) {
size_t l;
n++;
if (n > 0) {
unsigned i = 0;
if (!b)
return -ENOMEM;
_cleanup_free_ char *k = NULL;
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)
return 0;
return parse_boolean(s);
}
}
}
}
}
}
int r = 0;
unsigned n = 0;
if (!d)
return -errno;
for (;;) {
int k;
errno = 0;
return -errno;
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;
}
char **l = NULL, **a, **b;
int r;
r = get_files_in_directory("/run/systemd/machines/", &l);
if (r < 0)
return r;
if (l) {
r = 0;
/* Filter out the unit: symlinks */
for (a = l, b = l; *a; a++) {
if (startswith(*a, "unit:"))
free(*a);
else {
*b = *a;
b++;
r++;
}
}
*b = NULL;
}
*machines = l;
return r;
}
_cleanup_free_ char *c = NULL;
const char *p;
int r;
if (r < 0)
return r;
if (!c)
return -EIO;
*class = c;
c = NULL;
return 0;
}
int r;
if (r < 0)
return r;
if (!netif) {
return 0;
}
char buf[l+1];
int ifi;
continue;
if (ifi <= 0)
continue;
return -ENOMEM;
}
}
return nr;
}
static inline int MONITOR_TO_FD(sd_login_monitor *m) {
return (int) (unsigned long) m - 1;
}
}
int fd, k;
bool good = false;
assert_return(m, -EINVAL);
if (fd < 0)
return -errno;
if (k < 0) {
safe_close(fd);
return -errno;
}
good = true;
}
if (k < 0) {
safe_close(fd);
return -errno;
}
good = true;
}
if (k < 0) {
safe_close(fd);
return -errno;
}
good = true;
}
if (k < 0) {
safe_close(fd);
return -errno;
}
good = true;
}
if (!good) {
return -EINVAL;
}
*m = FD_TO_MONITOR(fd);
return 0;
}
int fd;
assert_return(m, NULL);
fd = MONITOR_TO_FD(m);
return NULL;
}
assert_return(m, -EINVAL);
return flush_fd(MONITOR_TO_FD(m));
}
assert_return(m, -EINVAL);
return MONITOR_TO_FD(m);
}
assert_return(m, -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;
}
assert_return(m, -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;
}