/***
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 <errno.h>
#include <poll.h>
#include <string.h>
#include <unistd.h>
#include "sd-login.h"
#include "alloc-util.h"
#include "cgroup-util.h"
#include "dirent-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "formats-util.h"
#include "fs-util.h"
#include "hostname-util.h"
#include "io-util.h"
#include "login-util.h"
#include "macro.h"
#include "parse-util.h"
#include "path-util.h"
#include "socket-util.h"
#include "string-util.h"
#include "strv.h"
#include "user-util.h"
#include "util.h"
/* Error codes:
*
* invalid input parameters → -EINVAL
* invalid fd → -EBADF
* process does not exist → -ESRCH
* cgroup does not exist → -ENOENT
* machine, session does not exist → -ENXIO
* requested metadata on object is missing → -ENODATA
*/
}
}
}
}
}
}
}
char *c;
int r;
if (r < 0)
return r;
/* The internal APIs return the empty string for the root
* cgroup, let's return the "/" in the public APIs instead, as
* that's easier and less ambigious for people to grok. */
if (isempty(c)) {
free(c);
c = strdup("/");
if (!c)
return -ENOMEM;
}
*cgroup = c;
return 0;
}
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;
}
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;
}
if (r < 0) {
free(s);
return r;
}
if (isempty(s)) {
free(s);
return -EIO;
}
*state = s;
return 0;
}
int r;
r = file_of_uid(uid, &p);
if (r < 0)
return r;
if (r == -ENOENT)
return -ENODATA;
if (r < 0)
return r;
if (isempty(s))
return -ENODATA;
*session = s;
s = NULL;
return 0;
}
char *p;
int r;
if (seat) {
if (!filename_is_valid(seat))
return -EINVAL;
} else {
if (r < 0)
return r;
}
if (!p)
return -ENOMEM;
*_p = p;
p = NULL;
return 0;
}
size_t l;
int r;
r = file_of_seat(seat, &p);
if (r < 0)
return r;
if (r == -ENOENT)
return 0;
if (r < 0)
return r;
if (isempty(s))
return 0;
return -ENOMEM;
return 1;
return 0;
}
char **a;
int r;
r = file_of_uid(uid, &p);
if (r < 0)
return r;
if (array)
return 0;
}
if (r < 0)
return r;
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 == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (isempty(s))
return -EIO;
return parse_boolean(s);
}
int r;
r = file_of_session(session, &p);
if (r < 0)
return r;
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (isempty(s))
return -ENODATA;
return parse_boolean(s);
}
int r;
r = file_of_session(session, &p);
if (r < 0)
return r;
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (isempty(s))
return -EIO;
*state = s;
s = NULL;
return 0;
}
int r;
r = file_of_session(session, &p);
if (r < 0)
return r;
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (isempty(s))
return -EIO;
}
int r;
r = file_of_session(session, &p);
if (r < 0)
return r;
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (isempty(s))
return -ENODATA;
*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 (r < 0)
return r;
*desktop = t;
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 == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (session && !s)
return -ENODATA;
if (uid && !t)
return -ENODATA;
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 == -ENOENT)
return -ENXIO;
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 == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (isempty(s))
return -ENODATA;
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++) {
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 == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (!c)
return -EIO;
*class = c;
c = NULL;
return 0;
}
int r;
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
if (!netif) {
return 0;
}
int ifi;
continue;
return -ENOMEM;
}
}
return nr;
}
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;
if (!m)
return 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;
}