2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2011-07-14 22:50:01 +02:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2011 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
2012-04-12 00:20:58 +02:00
|
|
|
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
|
2011-07-14 22:50:01 +02:00
|
|
|
(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
|
2012-04-12 00:20:58 +02:00
|
|
|
Lesser General Public License for more details.
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2011-07-14 22:50:01 +02:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2015-02-12 14:06:32 +01:00
|
|
|
#include <poll.h>
|
2015-10-23 18:52:53 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <unistd.h>
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "sd-login.h"
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2011-07-14 22:50:01 +02:00
|
|
|
#include "cgroup-util.h"
|
2015-10-26 21:16:26 +01:00
|
|
|
#include "dirent-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "escape.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2013-02-14 12:26:13 +01:00
|
|
|
#include "fileio.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2015-10-26 21:16:26 +01:00
|
|
|
#include "fs-util.h"
|
2015-08-23 14:30:52 +02:00
|
|
|
#include "hostname-util.h"
|
2015-10-25 14:08:25 +01:00
|
|
|
#include "io-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "login-util.h"
|
|
|
|
#include "macro.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "parse-util.h"
|
2015-10-26 18:59:36 +01:00
|
|
|
#include "path-util.h"
|
2015-10-26 01:09:02 +01:00
|
|
|
#include "socket-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "strv.h"
|
2015-10-25 22:32:30 +01:00
|
|
|
#include "user-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "util.h"
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2015-09-04 09:05:52 +02:00
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2011-07-22 21:01:15 +02:00
|
|
|
_public_ int sd_pid_get_session(pid_t pid, char **session) {
|
2017-05-19 03:55:44 +02:00
|
|
|
int r;
|
2012-05-30 22:25:01 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(pid >= 0, -EINVAL);
|
|
|
|
assert_return(session, -EINVAL);
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2017-05-19 03:55:44 +02:00
|
|
|
r = cg_pid_get_session(pid, session);
|
2017-05-31 02:21:45 +02:00
|
|
|
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
|
2011-07-14 22:50:01 +02:00
|
|
|
}
|
|
|
|
|
2012-01-04 15:35:30 +01:00
|
|
|
_public_ int sd_pid_get_unit(pid_t pid, char **unit) {
|
2017-05-19 04:32:19 +02:00
|
|
|
int r;
|
2012-01-03 20:51:38 +01:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(pid >= 0, -EINVAL);
|
|
|
|
assert_return(unit, -EINVAL);
|
2012-01-03 20:51:38 +01:00
|
|
|
|
2017-05-19 04:32:19 +02:00
|
|
|
r = cg_pid_get_unit(pid, unit);
|
2017-05-31 02:21:45 +02:00
|
|
|
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
|
2012-01-03 20:51:38 +01:00
|
|
|
}
|
|
|
|
|
2013-04-15 14:16:45 +02:00
|
|
|
_public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
|
2017-05-19 04:11:08 +02:00
|
|
|
int r;
|
2013-04-15 14:16:45 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(pid >= 0, -EINVAL);
|
|
|
|
assert_return(unit, -EINVAL);
|
2013-04-15 14:16:45 +02:00
|
|
|
|
2017-05-19 04:11:08 +02:00
|
|
|
r = cg_pid_get_user_unit(pid, unit);
|
2017-05-31 02:21:45 +02:00
|
|
|
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
|
2013-04-15 14:16:45 +02:00
|
|
|
}
|
|
|
|
|
2013-04-16 04:36:06 +02:00
|
|
|
_public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
|
2017-05-31 02:21:45 +02:00
|
|
|
int r;
|
2013-04-16 04:36:06 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(pid >= 0, -EINVAL);
|
|
|
|
assert_return(name, -EINVAL);
|
2013-04-16 04:36:06 +02:00
|
|
|
|
2017-05-31 02:21:45 +02:00
|
|
|
r = cg_pid_get_machine_name(pid, name);
|
|
|
|
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
|
2013-04-16 04:36:06 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 01:46:27 +02:00
|
|
|
_public_ int sd_pid_get_slice(pid_t pid, char **slice) {
|
2017-05-31 02:21:45 +02:00
|
|
|
int r;
|
2013-06-21 01:46:27 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(pid >= 0, -EINVAL);
|
|
|
|
assert_return(slice, -EINVAL);
|
2013-06-21 01:46:27 +02:00
|
|
|
|
2017-05-31 02:21:45 +02:00
|
|
|
r = cg_pid_get_slice(pid, slice);
|
|
|
|
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
|
2013-06-21 01:46:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 11:58:06 +02:00
|
|
|
_public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
|
2017-05-31 02:21:45 +02:00
|
|
|
int r;
|
2015-04-30 11:58:06 +02:00
|
|
|
|
|
|
|
assert_return(pid >= 0, -EINVAL);
|
|
|
|
assert_return(slice, -EINVAL);
|
|
|
|
|
2017-05-31 02:21:45 +02:00
|
|
|
r = cg_pid_get_user_slice(pid, slice);
|
|
|
|
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
|
2015-04-30 11:58:06 +02:00
|
|
|
}
|
|
|
|
|
2011-07-22 21:01:15 +02:00
|
|
|
_public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
|
2017-05-31 02:21:45 +02:00
|
|
|
int r;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(pid >= 0, -EINVAL);
|
|
|
|
assert_return(uid, -EINVAL);
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2017-05-31 02:21:45 +02:00
|
|
|
r = cg_pid_get_owner_uid(pid, uid);
|
|
|
|
return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
|
2011-07-22 21:01:15 +02:00
|
|
|
}
|
|
|
|
|
2015-09-04 09:54:14 +02:00
|
|
|
_public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
|
|
|
|
char *c;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(pid >= 0, -EINVAL);
|
|
|
|
assert_return(cgroup, -EINVAL);
|
|
|
|
|
|
|
|
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
|
|
|
|
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
|
2016-07-12 12:52:11 +02:00
|
|
|
* that's easier and less ambiguous for people to grok. */
|
2015-09-04 09:54:14 +02:00
|
|
|
if (isempty(c)) {
|
|
|
|
free(c);
|
|
|
|
c = strdup("/");
|
|
|
|
if (!c)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*cgroup = c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-17 18:14:26 +01:00
|
|
|
_public_ int sd_peer_get_session(int fd, char **session) {
|
2015-03-27 12:02:49 +01:00
|
|
|
struct ucred ucred = {};
|
2014-03-17 18:14:26 +01:00
|
|
|
int r;
|
|
|
|
|
2015-08-14 12:45:06 +02:00
|
|
|
assert_return(fd >= 0, -EBADF);
|
2014-03-17 18:14:26 +01:00
|
|
|
assert_return(session, -EINVAL);
|
|
|
|
|
|
|
|
r = getpeercred(fd, &ucred);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return cg_pid_get_session(ucred.pid, session);
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
|
|
|
|
struct ucred ucred;
|
|
|
|
int r;
|
|
|
|
|
2015-08-14 12:45:06 +02:00
|
|
|
assert_return(fd >= 0, -EBADF);
|
2014-03-17 18:14:26 +01:00
|
|
|
assert_return(uid, -EINVAL);
|
|
|
|
|
|
|
|
r = getpeercred(fd, &ucred);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return cg_pid_get_owner_uid(ucred.pid, uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_peer_get_unit(int fd, char **unit) {
|
|
|
|
struct ucred ucred;
|
|
|
|
int r;
|
|
|
|
|
2015-08-14 12:45:06 +02:00
|
|
|
assert_return(fd >= 0, -EBADF);
|
2014-03-17 18:14:26 +01:00
|
|
|
assert_return(unit, -EINVAL);
|
|
|
|
|
|
|
|
r = getpeercred(fd, &ucred);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return cg_pid_get_unit(ucred.pid, unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_peer_get_user_unit(int fd, char **unit) {
|
|
|
|
struct ucred ucred;
|
|
|
|
int r;
|
|
|
|
|
2015-08-14 12:45:06 +02:00
|
|
|
assert_return(fd >= 0, -EBADF);
|
2014-03-17 18:14:26 +01:00
|
|
|
assert_return(unit, -EINVAL);
|
|
|
|
|
|
|
|
r = getpeercred(fd, &ucred);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return cg_pid_get_user_unit(ucred.pid, unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_peer_get_machine_name(int fd, char **machine) {
|
|
|
|
struct ucred ucred;
|
|
|
|
int r;
|
|
|
|
|
2015-08-14 12:45:06 +02:00
|
|
|
assert_return(fd >= 0, -EBADF);
|
2014-03-17 18:14:26 +01:00
|
|
|
assert_return(machine, -EINVAL);
|
|
|
|
|
|
|
|
r = getpeercred(fd, &ucred);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return cg_pid_get_machine_name(ucred.pid, machine);
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_peer_get_slice(int fd, char **slice) {
|
|
|
|
struct ucred ucred;
|
|
|
|
int r;
|
|
|
|
|
2015-08-14 12:45:06 +02:00
|
|
|
assert_return(fd >= 0, -EBADF);
|
2014-03-17 18:14:26 +01:00
|
|
|
assert_return(slice, -EINVAL);
|
|
|
|
|
|
|
|
r = getpeercred(fd, &ucred);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return cg_pid_get_slice(ucred.pid, slice);
|
|
|
|
}
|
|
|
|
|
2015-04-30 11:58:06 +02:00
|
|
|
_public_ int sd_peer_get_user_slice(int fd, char **slice) {
|
|
|
|
struct ucred ucred;
|
|
|
|
int r;
|
|
|
|
|
2015-08-14 12:45:06 +02:00
|
|
|
assert_return(fd >= 0, -EBADF);
|
2015-04-30 11:58:06 +02:00
|
|
|
assert_return(slice, -EINVAL);
|
|
|
|
|
|
|
|
r = getpeercred(fd, &ucred);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return cg_pid_get_user_slice(ucred.pid, slice);
|
|
|
|
}
|
|
|
|
|
2015-09-04 09:54:14 +02:00
|
|
|
_public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
|
|
|
|
struct ucred ucred;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(fd >= 0, -EBADF);
|
|
|
|
assert_return(cgroup, -EINVAL);
|
|
|
|
|
|
|
|
r = getpeercred(fd, &ucred);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return sd_pid_get_cgroup(ucred.pid, cgroup);
|
|
|
|
}
|
|
|
|
|
2014-05-19 02:33:20 +02:00
|
|
|
static int file_of_uid(uid_t uid, char **p) {
|
2015-09-04 09:05:52 +02:00
|
|
|
|
|
|
|
assert_return(uid_is_valid(uid), -EINVAL);
|
2014-05-19 02:33:20 +02:00
|
|
|
assert(p);
|
|
|
|
|
|
|
|
if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-14 22:50:01 +02:00
|
|
|
_public_ int sd_uid_get_state(uid_t uid, char**state) {
|
2014-03-17 18:14:26 +01:00
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
char *s = NULL;
|
2011-07-14 22:50:01 +02:00
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(state, -EINVAL);
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2014-05-19 02:33:20 +02:00
|
|
|
r = file_of_uid(uid, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
|
|
|
r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
|
|
|
|
if (r == -ENOENT) {
|
|
|
|
free(s);
|
|
|
|
s = strdup("offline");
|
|
|
|
if (!s)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-09-04 09:05:52 +02:00
|
|
|
}
|
2017-07-09 00:59:29 +02:00
|
|
|
else if (r < 0) {
|
2011-07-14 22:50:01 +02:00
|
|
|
free(s);
|
|
|
|
return r;
|
2015-09-04 09:05:52 +02:00
|
|
|
}
|
|
|
|
if (isempty(s)) {
|
|
|
|
free(s);
|
2011-07-14 22:50:01 +02:00
|
|
|
return -EIO;
|
2015-09-04 09:05:52 +02:00
|
|
|
}
|
2011-07-14 22:50:01 +02:00
|
|
|
|
|
|
|
*state = s;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-19 02:33:20 +02:00
|
|
|
_public_ int sd_uid_get_display(uid_t uid, char **session) {
|
|
|
|
_cleanup_free_ char *p = NULL, *s = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(session, -EINVAL);
|
|
|
|
|
|
|
|
r = file_of_uid(uid, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
|
2015-09-01 00:40:20 +02:00
|
|
|
if (r == -ENOENT)
|
2015-09-04 09:05:52 +02:00
|
|
|
return -ENODATA;
|
2014-05-19 02:33:20 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (isempty(s))
|
2015-09-04 09:05:52 +02:00
|
|
|
return -ENODATA;
|
2014-05-19 02:33:20 +02:00
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
*session = TAKE_PTR(s);
|
2014-05-19 02:33:20 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-04 09:05:52 +02:00
|
|
|
static int file_of_seat(const char *seat, char **_p) {
|
|
|
|
char *p;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(_p);
|
|
|
|
|
|
|
|
if (seat) {
|
|
|
|
if (!filename_is_valid(seat))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
p = strappend("/run/systemd/seats/", seat);
|
|
|
|
} else {
|
|
|
|
_cleanup_free_ char *buf = NULL;
|
|
|
|
|
|
|
|
r = sd_session_get_seat(NULL, &buf);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
p = strappend("/run/systemd/seats/", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
*_p = TAKE_PTR(p);
|
2015-09-04 09:05:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-22 21:01:15 +02:00
|
|
|
_public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
|
2011-07-14 22:50:01 +02:00
|
|
|
size_t l;
|
|
|
|
int r;
|
2014-07-30 04:01:36 +02:00
|
|
|
const char *word, *variable, *state;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2015-09-03 13:29:53 +02:00
|
|
|
assert_return(uid_is_valid(uid), -EINVAL);
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2015-09-04 09:05:52 +02:00
|
|
|
r = file_of_seat(seat, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2015-09-04 09:05:52 +02:00
|
|
|
variable = require_active ? "ACTIVE_UID" : "UIDS";
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2011-07-22 21:01:15 +02:00
|
|
|
r = parse_env_file(p, NEWLINE, variable, &s, NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return 0;
|
2013-04-17 14:05:24 +02:00
|
|
|
if (r < 0)
|
2011-07-14 22:50:01 +02:00
|
|
|
return r;
|
2015-09-04 09:05:52 +02:00
|
|
|
if (isempty(s))
|
|
|
|
return 0;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2014-04-25 13:45:15 +02:00
|
|
|
if (asprintf(&t, UID_FMT, uid) < 0)
|
2011-07-14 22:50:01 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-09-04 09:05:52 +02:00
|
|
|
FOREACH_WORD(word, l, s, state)
|
2014-07-30 04:01:36 +02:00
|
|
|
if (strneq(t, word, l))
|
2011-07-14 22:50:01 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-22 21:01:15 +02:00
|
|
|
static int uid_get_array(uid_t uid, const char *variable, char ***array) {
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ char *p = NULL, *s = NULL;
|
2011-07-22 21:01:15 +02:00
|
|
|
char **a;
|
|
|
|
int r;
|
|
|
|
|
2015-09-04 09:05:52 +02:00
|
|
|
assert(variable);
|
2015-09-03 13:29:53 +02:00
|
|
|
|
2014-05-19 02:33:20 +02:00
|
|
|
r = file_of_uid(uid, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2015-09-04 09:05:52 +02:00
|
|
|
r = parse_env_file(p, NEWLINE, variable, &s, NULL);
|
|
|
|
if (r == -ENOENT || (r >= 0 && isempty(s))) {
|
2011-07-29 03:08:49 +02:00
|
|
|
if (array)
|
|
|
|
*array = NULL;
|
2011-07-22 21:01:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
a = strv_split(s, " ");
|
|
|
|
if (!a)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-07-29 03:08:49 +02:00
|
|
|
strv_uniq(a);
|
|
|
|
r = strv_length(a);
|
|
|
|
|
|
|
|
if (array)
|
|
|
|
*array = a;
|
|
|
|
else
|
|
|
|
strv_free(a);
|
|
|
|
|
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
|
2012-09-12 08:30:35 +02:00
|
|
|
return uid_get_array(
|
|
|
|
uid,
|
|
|
|
require_active == 0 ? "ONLINE_SESSIONS" :
|
|
|
|
require_active > 0 ? "ACTIVE_SESSIONS" :
|
|
|
|
"SESSIONS",
|
|
|
|
sessions);
|
2011-07-14 22:50:01 +02:00
|
|
|
}
|
|
|
|
|
2011-07-22 21:01:15 +02:00
|
|
|
_public_ int sd_uid_get_seats(uid_t uid, int require_active, char ***seats) {
|
2012-09-12 08:30:35 +02:00
|
|
|
return uid_get_array(
|
|
|
|
uid,
|
|
|
|
require_active == 0 ? "ONLINE_SEATS" :
|
|
|
|
require_active > 0 ? "ACTIVE_SEATS" :
|
|
|
|
"SEATS",
|
|
|
|
seats);
|
2011-07-14 22:50:01 +02:00
|
|
|
}
|
|
|
|
|
2012-02-11 03:45:23 +01:00
|
|
|
static int file_of_session(const char *session, char **_p) {
|
|
|
|
char *p;
|
2011-07-14 22:50:01 +02:00
|
|
|
int r;
|
|
|
|
|
2012-02-11 03:45:23 +01:00
|
|
|
assert(_p);
|
|
|
|
|
2013-09-16 04:26:56 +02:00
|
|
|
if (session) {
|
|
|
|
if (!session_id_valid(session))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-02-11 03:45:23 +01:00
|
|
|
p = strappend("/run/systemd/sessions/", session);
|
2013-09-16 04:26:56 +02:00
|
|
|
} else {
|
|
|
|
_cleanup_free_ char *buf = NULL;
|
2012-02-11 03:45:23 +01:00
|
|
|
|
|
|
|
r = sd_pid_get_session(0, &buf);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
p = strappend("/run/systemd/sessions/", buf);
|
|
|
|
}
|
2011-07-14 22:50:01 +02:00
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-02-11 03:45:23 +01:00
|
|
|
*_p = p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_session_is_active(const char *session) {
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ char *p = NULL, *s = NULL;
|
2015-09-04 09:05:52 +02:00
|
|
|
int r;
|
2012-02-11 03:45:23 +01:00
|
|
|
|
|
|
|
r = file_of_session(session, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2011-07-14 22:50:01 +02:00
|
|
|
r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
2013-04-17 14:05:24 +02:00
|
|
|
if (r < 0)
|
2011-07-14 22:50:01 +02:00
|
|
|
return r;
|
2015-09-04 09:05:52 +02:00
|
|
|
if (isempty(s))
|
2011-07-14 22:50:01 +02:00
|
|
|
return -EIO;
|
|
|
|
|
2014-03-12 20:54:29 +01:00
|
|
|
return parse_boolean(s);
|
2011-07-14 22:50:01 +02:00
|
|
|
}
|
|
|
|
|
2013-12-22 01:48:46 +01:00
|
|
|
_public_ int sd_session_is_remote(const char *session) {
|
|
|
|
_cleanup_free_ char *p = NULL, *s = NULL;
|
2015-09-04 09:05:52 +02:00
|
|
|
int r;
|
2013-12-22 01:48:46 +01:00
|
|
|
|
|
|
|
r = file_of_session(session, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
2013-12-22 01:48:46 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2015-09-04 09:05:52 +02:00
|
|
|
if (isempty(s))
|
|
|
|
return -ENODATA;
|
2013-12-22 01:48:46 +01:00
|
|
|
|
2014-03-12 20:54:29 +01:00
|
|
|
return parse_boolean(s);
|
2013-12-22 01:48:46 +01:00
|
|
|
}
|
|
|
|
|
2012-06-21 16:14:53 +02:00
|
|
|
_public_ int sd_session_get_state(const char *session, char **state) {
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ char *p = NULL, *s = NULL;
|
2012-06-21 16:14:53 +02:00
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(state, -EINVAL);
|
2012-06-21 16:14:53 +02:00
|
|
|
|
|
|
|
r = file_of_session(session, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
2013-04-17 14:05:24 +02:00
|
|
|
if (r < 0)
|
2012-06-21 16:14:53 +02:00
|
|
|
return r;
|
2015-09-04 09:05:52 +02:00
|
|
|
if (isempty(s))
|
2012-06-21 16:14:53 +02:00
|
|
|
return -EIO;
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
*state = TAKE_PTR(s);
|
2013-04-17 14:05:24 +02:00
|
|
|
|
2012-06-21 16:14:53 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-14 22:50:01 +02:00
|
|
|
_public_ int sd_session_get_uid(const char *session, uid_t *uid) {
|
|
|
|
int r;
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ char *p = NULL, *s = NULL;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(uid, -EINVAL);
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2012-02-11 03:45:23 +01:00
|
|
|
r = file_of_session(session, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
|
|
|
r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
2013-04-17 14:05:24 +02:00
|
|
|
if (r < 0)
|
2011-07-14 22:50:01 +02:00
|
|
|
return r;
|
2015-09-04 09:05:52 +02:00
|
|
|
if (isempty(s))
|
2011-07-14 22:50:01 +02:00
|
|
|
return -EIO;
|
|
|
|
|
2014-03-12 20:54:29 +01:00
|
|
|
return parse_uid(s, uid);
|
2011-07-14 22:50:01 +02:00
|
|
|
}
|
|
|
|
|
2012-02-14 21:54:00 +01:00
|
|
|
static int session_get_string(const char *session, const char *field, char **value) {
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ char *p = NULL, *s = NULL;
|
2011-07-14 22:50:01 +02:00
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(value, -EINVAL);
|
2015-09-04 09:05:52 +02:00
|
|
|
assert(field);
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2012-02-11 03:45:23 +01:00
|
|
|
r = file_of_session(session, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2012-02-14 21:54:00 +01:00
|
|
|
r = parse_env_file(p, NEWLINE, field, &s, NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
2013-04-17 14:05:24 +02:00
|
|
|
if (r < 0)
|
2011-07-14 22:50:01 +02:00
|
|
|
return r;
|
|
|
|
if (isempty(s))
|
2015-09-04 09:05:52 +02:00
|
|
|
return -ENODATA;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
*value = TAKE_PTR(s);
|
2011-07-14 22:50:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-14 21:54:00 +01:00
|
|
|
_public_ int sd_session_get_seat(const char *session, char **seat) {
|
|
|
|
return session_get_string(session, "SEAT", seat);
|
|
|
|
}
|
2012-01-05 23:14:22 +01:00
|
|
|
|
2013-01-15 03:00:33 +01:00
|
|
|
_public_ int sd_session_get_tty(const char *session, char **tty) {
|
|
|
|
return session_get_string(session, "TTY", tty);
|
|
|
|
}
|
|
|
|
|
2013-09-11 18:48:20 +02:00
|
|
|
_public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
|
2013-10-10 13:09:37 +02:00
|
|
|
_cleanup_free_ char *vtnr_string = NULL;
|
2013-09-11 18:48:20 +02:00
|
|
|
unsigned u;
|
|
|
|
int r;
|
|
|
|
|
2015-09-04 09:05:52 +02:00
|
|
|
assert_return(vtnr, -EINVAL);
|
|
|
|
|
2013-10-10 13:11:27 +02:00
|
|
|
r = session_get_string(session, "VTNR", &vtnr_string);
|
2013-09-11 18:48:20 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = safe_atou(vtnr_string, &u);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
*vtnr = u;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-14 21:54:00 +01:00
|
|
|
_public_ int sd_session_get_service(const char *session, char **service) {
|
|
|
|
return session_get_string(session, "SERVICE", service);
|
|
|
|
}
|
2012-01-05 23:14:22 +01:00
|
|
|
|
2012-02-14 21:54:00 +01:00
|
|
|
_public_ int sd_session_get_type(const char *session, char **type) {
|
|
|
|
return session_get_string(session, "TYPE", type);
|
|
|
|
}
|
2012-01-05 23:14:22 +01:00
|
|
|
|
2012-02-14 21:54:00 +01:00
|
|
|
_public_ int sd_session_get_class(const char *session, char **class) {
|
|
|
|
return session_get_string(session, "CLASS", class);
|
2012-01-05 23:14:22 +01:00
|
|
|
}
|
|
|
|
|
2014-09-20 18:42:29 +02:00
|
|
|
_public_ int sd_session_get_desktop(const char *session, char **desktop) {
|
|
|
|
_cleanup_free_ char *escaped = NULL;
|
|
|
|
char *t;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(desktop, -EINVAL);
|
|
|
|
|
|
|
|
r = session_get_string(session, "DESKTOP", &escaped);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-04-06 20:11:41 +02:00
|
|
|
r = cunescape(escaped, 0, &t);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-09-20 18:42:29 +02:00
|
|
|
|
|
|
|
*desktop = t;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-15 04:56:10 +01:00
|
|
|
_public_ int sd_session_get_display(const char *session, char **display) {
|
|
|
|
return session_get_string(session, "DISPLAY", display);
|
|
|
|
}
|
|
|
|
|
2013-12-22 01:48:46 +01:00
|
|
|
_public_ int sd_session_get_remote_user(const char *session, char **remote_user) {
|
|
|
|
return session_get_string(session, "REMOTE_USER", remote_user);
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_session_get_remote_host(const char *session, char **remote_host) {
|
|
|
|
return session_get_string(session, "REMOTE_HOST", remote_host);
|
|
|
|
}
|
|
|
|
|
2011-07-14 22:50:01 +02:00
|
|
|
_public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
|
2011-07-14 22:50:01 +02:00
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(session || uid, -EINVAL);
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2012-02-11 03:45:23 +01:00
|
|
|
r = file_of_seat(seat, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
|
|
|
r = parse_env_file(p, NEWLINE,
|
|
|
|
"ACTIVE", &s,
|
|
|
|
"ACTIVE_UID", &t,
|
|
|
|
NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
2013-04-17 14:05:24 +02:00
|
|
|
if (r < 0)
|
2011-07-14 22:50:01 +02:00
|
|
|
return r;
|
|
|
|
|
2013-04-17 14:05:24 +02:00
|
|
|
if (session && !s)
|
2015-09-08 14:03:22 +02:00
|
|
|
return -ENODATA;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2013-04-17 14:05:24 +02:00
|
|
|
if (uid && !t)
|
2015-09-08 14:03:22 +02:00
|
|
|
return -ENODATA;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
|
|
|
if (uid && t) {
|
2011-07-22 21:01:15 +02:00
|
|
|
r = parse_uid(t, uid);
|
2013-04-17 14:05:24 +02:00
|
|
|
if (r < 0)
|
2011-07-14 22:50:01 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
if (session && s)
|
|
|
|
*session = TAKE_PTR(s);
|
2011-07-14 22:50:01 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
_public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
|
|
|
|
_cleanup_strv_free_ char **a = NULL;
|
|
|
|
_cleanup_free_ uid_t *b = NULL;
|
2011-07-22 21:01:15 +02:00
|
|
|
unsigned n = 0;
|
|
|
|
int r;
|
|
|
|
|
2012-02-11 03:45:23 +01:00
|
|
|
r = file_of_seat(seat, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
r = parse_env_file(p, NEWLINE,
|
|
|
|
"SESSIONS", &s,
|
2017-05-19 02:58:23 +02:00
|
|
|
"UIDS", &t,
|
2011-07-22 21:01:15 +02:00
|
|
|
NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
2013-04-17 14:05:24 +02:00
|
|
|
if (r < 0)
|
2011-07-22 21:01:15 +02:00
|
|
|
return r;
|
|
|
|
|
2011-07-29 03:08:49 +02:00
|
|
|
if (s) {
|
2011-07-22 21:01:15 +02:00
|
|
|
a = strv_split(s, " ");
|
2013-04-17 14:05:24 +02:00
|
|
|
if (!a)
|
2011-07-22 21:01:15 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uids && t) {
|
2014-07-30 04:01:36 +02:00
|
|
|
const char *word, *state;
|
2011-07-22 21:01:15 +02:00
|
|
|
size_t l;
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
FOREACH_WORD(word, l, t, state)
|
2011-07-22 21:01:15 +02:00
|
|
|
n++;
|
|
|
|
|
2013-04-17 14:05:24 +02:00
|
|
|
if (n > 0) {
|
2011-09-23 02:39:28 +02:00
|
|
|
unsigned i = 0;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2011-09-23 02:39:28 +02:00
|
|
|
b = new(uid_t, n);
|
2013-04-17 14:05:24 +02:00
|
|
|
if (!b)
|
2011-07-22 21:01:15 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
FOREACH_WORD(word, l, t, state) {
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ char *k = NULL;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
k = strndup(word, l);
|
2013-04-17 14:05:24 +02:00
|
|
|
if (!k)
|
2011-09-23 02:39:28 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
r = parse_uid(k, b + i);
|
|
|
|
if (r < 0)
|
2017-05-19 02:48:09 +02:00
|
|
|
return r;
|
2011-09-23 02:39:28 +02:00
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
2011-07-22 21:01:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 03:08:49 +02:00
|
|
|
r = strv_length(a);
|
|
|
|
|
2013-04-17 14:05:24 +02:00
|
|
|
if (sessions) {
|
2011-07-22 21:01:15 +02:00
|
|
|
*sessions = a;
|
2013-04-17 14:05:24 +02:00
|
|
|
a = NULL;
|
|
|
|
}
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2013-04-17 14:05:24 +02:00
|
|
|
if (uids) {
|
2011-07-22 21:01:15 +02:00
|
|
|
*uids = b;
|
2013-04-17 14:05:24 +02:00
|
|
|
b = NULL;
|
|
|
|
}
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
if (n_uids)
|
|
|
|
*n_uids = n;
|
|
|
|
|
2011-07-29 03:08:49 +02:00
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
}
|
|
|
|
|
2012-06-21 16:52:54 +02:00
|
|
|
static int seat_get_can(const char *seat, const char *variable) {
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ char *p = NULL, *s = NULL;
|
2011-07-26 23:09:09 +02:00
|
|
|
int r;
|
|
|
|
|
2015-09-04 09:05:52 +02:00
|
|
|
assert(variable);
|
2014-03-12 20:54:29 +01:00
|
|
|
|
2012-02-11 03:45:23 +01:00
|
|
|
r = file_of_seat(seat, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-26 23:09:09 +02:00
|
|
|
|
|
|
|
r = parse_env_file(p, NEWLINE,
|
2012-06-21 16:52:54 +02:00
|
|
|
variable, &s,
|
2011-07-26 23:09:09 +02:00
|
|
|
NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
2013-04-17 14:05:24 +02:00
|
|
|
if (r < 0)
|
2011-07-26 23:09:09 +02:00
|
|
|
return r;
|
2015-09-04 09:05:52 +02:00
|
|
|
if (isempty(s))
|
|
|
|
return -ENODATA;
|
2011-07-26 23:09:09 +02:00
|
|
|
|
2014-03-12 20:54:29 +01:00
|
|
|
return parse_boolean(s);
|
2011-07-26 23:09:09 +02:00
|
|
|
}
|
|
|
|
|
2012-06-21 16:52:54 +02:00
|
|
|
_public_ int sd_seat_can_multi_session(const char *seat) {
|
|
|
|
return seat_get_can(seat, "CAN_MULTI_SESSION");
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_seat_can_tty(const char *seat) {
|
|
|
|
return seat_get_can(seat, "CAN_TTY");
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_seat_can_graphical(const char *seat) {
|
|
|
|
return seat_get_can(seat, "CAN_GRAPHICAL");
|
|
|
|
}
|
|
|
|
|
2011-07-22 21:01:15 +02:00
|
|
|
_public_ int sd_get_seats(char ***seats) {
|
2017-05-31 16:48:47 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
r = get_files_in_directory("/run/systemd/seats/", seats);
|
|
|
|
if (r == -ENOENT) {
|
|
|
|
if (seats)
|
|
|
|
*seats = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_get_sessions(char ***sessions) {
|
2017-05-31 16:48:47 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
r = get_files_in_directory("/run/systemd/sessions/", sessions);
|
|
|
|
if (r == -ENOENT) {
|
|
|
|
if (sessions)
|
|
|
|
*sessions = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_get_uids(uid_t **users) {
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_closedir_ DIR *d;
|
2016-12-09 10:04:30 +01:00
|
|
|
struct dirent *de;
|
2011-07-22 21:01:15 +02:00
|
|
|
int r = 0;
|
|
|
|
unsigned n = 0;
|
2013-04-17 14:05:24 +02:00
|
|
|
_cleanup_free_ uid_t *l = NULL;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
d = opendir("/run/systemd/users/");
|
2017-05-31 16:48:47 +02:00
|
|
|
if (!d) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
if (users)
|
|
|
|
*users = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-09-23 01:43:28 +02:00
|
|
|
return -errno;
|
2017-05-31 16:48:47 +02:00
|
|
|
}
|
2011-09-23 01:43:28 +02:00
|
|
|
|
2016-12-09 10:04:30 +01:00
|
|
|
FOREACH_DIRENT_ALL(de, d, return -errno) {
|
2011-07-22 21:01:15 +02:00
|
|
|
int k;
|
|
|
|
uid_t uid;
|
|
|
|
|
|
|
|
dirent_ensure_type(d, de);
|
|
|
|
|
|
|
|
if (!dirent_is_file(de))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
k = parse_uid(de->d_name, &uid);
|
|
|
|
if (k < 0)
|
|
|
|
continue;
|
|
|
|
|
2011-07-29 03:08:49 +02:00
|
|
|
if (users) {
|
|
|
|
if ((unsigned) r >= n) {
|
|
|
|
uid_t *t;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2011-07-29 03:08:49 +02:00
|
|
|
n = MAX(16, 2*r);
|
|
|
|
t = realloc(l, sizeof(uid_t) * n);
|
2013-04-17 14:05:24 +02:00
|
|
|
if (!t)
|
|
|
|
return -ENOMEM;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2011-07-29 03:08:49 +02:00
|
|
|
l = t;
|
|
|
|
}
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2011-07-29 03:08:49 +02:00
|
|
|
assert((unsigned) r < n);
|
|
|
|
l[r++] = uid;
|
|
|
|
} else
|
|
|
|
r++;
|
2011-07-22 21:01:15 +02:00
|
|
|
}
|
|
|
|
|
2013-04-17 14:05:24 +02:00
|
|
|
if (users) {
|
|
|
|
*users = l;
|
|
|
|
l = NULL;
|
|
|
|
}
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-05-07 12:47:30 +02:00
|
|
|
_public_ int sd_get_machine_names(char ***machines) {
|
2017-07-09 00:04:44 +02:00
|
|
|
_cleanup_strv_free_ char **l = NULL;
|
|
|
|
char **a, **b;
|
2014-02-11 17:15:38 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
r = get_files_in_directory("/run/systemd/machines/", &l);
|
2017-05-19 13:52:00 +02:00
|
|
|
if (r == -ENOENT) {
|
2017-05-31 17:12:32 +02:00
|
|
|
if (machines)
|
|
|
|
*machines = NULL;
|
2017-05-19 13:52:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-02-11 17:15:38 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (l) {
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
/* Filter out the unit: symlinks */
|
2017-05-31 17:12:32 +02:00
|
|
|
for (a = b = l; *a; a++) {
|
2015-08-23 14:33:50 +02:00
|
|
|
if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
|
2014-02-11 17:15:38 +01:00
|
|
|
free(*a);
|
|
|
|
else {
|
|
|
|
*b = *a;
|
|
|
|
b++;
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*b = NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
if (machines)
|
|
|
|
*machines = TAKE_PTR(l);
|
|
|
|
|
2014-02-11 17:15:38 +01:00
|
|
|
return r;
|
2013-04-24 22:54:17 +02:00
|
|
|
}
|
|
|
|
|
2014-03-12 20:54:29 +01:00
|
|
|
_public_ int sd_machine_get_class(const char *machine, char **class) {
|
|
|
|
_cleanup_free_ char *c = NULL;
|
|
|
|
const char *p;
|
|
|
|
int r;
|
|
|
|
|
2014-07-03 01:19:26 +02:00
|
|
|
assert_return(machine_name_is_valid(machine), -EINVAL);
|
2014-03-12 20:54:29 +01:00
|
|
|
assert_return(class, -EINVAL);
|
|
|
|
|
2015-02-03 02:05:59 +01:00
|
|
|
p = strjoina("/run/systemd/machines/", machine);
|
2014-03-12 20:54:29 +01:00
|
|
|
r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
2014-03-12 20:54:29 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (!c)
|
|
|
|
return -EIO;
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
*class = TAKE_PTR(c);
|
2014-03-12 20:54:29 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-17 01:48:40 +02:00
|
|
|
_public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
|
2014-07-11 03:15:21 +02:00
|
|
|
_cleanup_free_ char *netif = NULL;
|
|
|
|
size_t l, allocated = 0, nr = 0;
|
|
|
|
int *ni = NULL;
|
2014-07-30 04:01:36 +02:00
|
|
|
const char *p, *word, *state;
|
2014-07-11 03:15:21 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(machine_name_is_valid(machine), -EINVAL);
|
2014-07-17 01:48:40 +02:00
|
|
|
assert_return(ifindices, -EINVAL);
|
2014-07-11 03:15:21 +02:00
|
|
|
|
2015-02-03 02:05:59 +01:00
|
|
|
p = strjoina("/run/systemd/machines/", machine);
|
2014-07-11 03:15:21 +02:00
|
|
|
r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
2014-07-11 03:15:21 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (!netif) {
|
2014-07-17 01:48:40 +02:00
|
|
|
*ifindices = NULL;
|
2014-07-11 03:15:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
FOREACH_WORD(word, l, netif, state) {
|
2014-07-11 03:15:21 +02:00
|
|
|
char buf[l+1];
|
|
|
|
int ifi;
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
*(char*) (mempcpy(buf, word, l)) = 0;
|
2014-07-11 03:15:21 +02:00
|
|
|
|
2015-11-02 23:57:21 +01:00
|
|
|
if (parse_ifindex(buf, &ifi) < 0)
|
2014-07-11 03:15:21 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
|
|
|
|
free(ni);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
ni[nr++] = ifi;
|
|
|
|
}
|
|
|
|
|
2014-07-17 01:48:40 +02:00
|
|
|
*ifindices = ni;
|
2014-07-11 03:15:21 +02:00
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
2011-07-22 21:01:15 +02:00
|
|
|
static inline int MONITOR_TO_FD(sd_login_monitor *m) {
|
|
|
|
return (int) (unsigned long) m - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
|
|
|
|
return (sd_login_monitor*) (unsigned long) (fd + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
|
2018-02-26 18:45:45 +01:00
|
|
|
_cleanup_close_ int fd = -1;
|
2011-07-22 21:01:15 +02:00
|
|
|
bool good = false;
|
2018-02-26 18:45:45 +01:00
|
|
|
int k;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(m, -EINVAL);
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
|
|
|
|
if (fd < 0)
|
2013-04-12 00:57:42 +02:00
|
|
|
return -errno;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
if (!category || streq(category, "seat")) {
|
|
|
|
k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
|
2018-02-26 18:45:45 +01:00
|
|
|
if (k < 0)
|
2011-07-22 21:01:15 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
good = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!category || streq(category, "session")) {
|
|
|
|
k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
|
2018-02-26 18:45:45 +01:00
|
|
|
if (k < 0)
|
2011-07-22 21:01:15 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
good = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!category || streq(category, "uid")) {
|
|
|
|
k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
|
2018-02-26 18:45:45 +01:00
|
|
|
if (k < 0)
|
2011-07-22 21:01:15 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
good = true;
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:54:55 +02:00
|
|
|
if (!category || streq(category, "machine")) {
|
2013-06-20 04:03:38 +02:00
|
|
|
k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
|
2018-02-26 18:45:45 +01:00
|
|
|
if (k < 0)
|
2013-04-24 22:54:55 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
good = true;
|
|
|
|
}
|
|
|
|
|
2018-02-26 18:45:45 +01:00
|
|
|
if (!good)
|
2011-07-22 21:01:15 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*m = FD_TO_MONITOR(fd);
|
2018-02-26 18:45:45 +01:00
|
|
|
fd = -1;
|
|
|
|
|
2011-07-22 21:01:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
|
|
|
|
int fd;
|
|
|
|
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
if (!m)
|
|
|
|
return NULL;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
fd = MONITOR_TO_FD(m);
|
|
|
|
close_nointr(fd);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_login_monitor_flush(sd_login_monitor *m) {
|
2017-12-12 23:21:09 +01:00
|
|
|
int r;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(m, -EINVAL);
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2017-12-12 23:21:09 +01:00
|
|
|
r = flush_fd(MONITOR_TO_FD(m));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
2011-07-22 21:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(m, -EINVAL);
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
return MONITOR_TO_FD(m);
|
|
|
|
}
|
2013-04-04 17:38:08 +02:00
|
|
|
|
|
|
|
_public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(m, -EINVAL);
|
2013-04-04 17:38:08 +02:00
|
|
|
|
2013-04-04 18:31:22 +02:00
|
|
|
/* 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. */
|
2013-04-04 17:38:08 +02:00
|
|
|
return POLLIN;
|
|
|
|
}
|
2013-04-04 18:31:22 +02:00
|
|
|
|
|
|
|
_public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(timeout_usec, -EINVAL);
|
2013-04-04 18:31:22 +02:00
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
*timeout_usec = (uint64_t) -1;
|
|
|
|
return 0;
|
|
|
|
}
|