2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2011-07-14 22:50:01 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-02-12 14:06:32 +01:00
|
|
|
#include <poll.h>
|
2015-10-23 18:52:53 +02:00
|
|
|
#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"
|
2018-11-30 22:08:41 +01:00
|
|
|
#include "env-file.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "escape.h"
|
2020-07-29 16:05:45 +02:00
|
|
|
#include "extract-word.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.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"
|
2020-07-29 16:05:45 +02:00
|
|
|
#include "stdio-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) {
|
2018-11-12 14:18:03 +01: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(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
|
|
|
|
2018-11-12 14:18:03 +01:00
|
|
|
r = parse_env_file(NULL, p, "STATE", &s);
|
2011-07-14 22:50:01 +02:00
|
|
|
if (r == -ENOENT) {
|
2018-11-12 14:18:03 +01:00
|
|
|
r = free_and_strdup(&s, "offline");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
} else if (r < 0)
|
2011-07-14 22:50:01 +02:00
|
|
|
return r;
|
2018-11-12 14:18:03 +01:00
|
|
|
else if (isempty(s))
|
2011-07-14 22:50:01 +02:00
|
|
|
return -EIO;
|
|
|
|
|
2018-11-12 14:18:03 +01:00
|
|
|
*state = TAKE_PTR(s);
|
2011-07-14 22:50:01 +02:00
|
|
|
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;
|
|
|
|
|
2018-11-12 14:18:03 +01:00
|
|
|
r = parse_env_file(NULL, p, "DISPLAY", &s);
|
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;
|
|
|
|
|
2019-07-11 19:14:16 +02:00
|
|
|
p = path_join("/run/systemd/seats", seat);
|
2015-09-04 09:05:52 +02:00
|
|
|
} else {
|
|
|
|
_cleanup_free_ char *buf = NULL;
|
|
|
|
|
|
|
|
r = sd_session_get_seat(NULL, &buf);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2019-07-11 19:14:16 +02:00
|
|
|
p = path_join("/run/systemd/seats", buf);
|
2015-09-04 09:05:52 +02:00
|
|
|
}
|
|
|
|
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) {
|
2020-07-29 16:05:45 +02:00
|
|
|
_cleanup_free_ char *filename = NULL, *content = NULL;
|
2011-07-14 22:50:01 +02:00
|
|
|
int r;
|
|
|
|
|
2015-09-03 13:29:53 +02:00
|
|
|
assert_return(uid_is_valid(uid), -EINVAL);
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2020-07-29 16:05:45 +02:00
|
|
|
r = file_of_seat(seat, &filename);
|
2015-09-04 09:05:52 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2020-07-29 16:05:45 +02:00
|
|
|
r = parse_env_file(NULL, filename,
|
|
|
|
require_active ? "ACTIVE_UID" : "UIDS",
|
|
|
|
&content);
|
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;
|
2020-07-29 16:05:45 +02:00
|
|
|
if (isempty(content))
|
2015-09-04 09:05:52 +02:00
|
|
|
return 0;
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2020-07-29 16:05:45 +02:00
|
|
|
char t[DECIMAL_STR_MAX(uid_t)];
|
|
|
|
xsprintf(t, UID_FMT, uid);
|
2011-07-14 22:50:01 +02:00
|
|
|
|
2020-11-03 15:13:20 +01:00
|
|
|
return string_contains_word(content, NULL, t);
|
2011-07-14 22:50:01 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-11-12 14:18:03 +01:00
|
|
|
r = parse_env_file(NULL, p, variable, &s);
|
2015-09-04 09:05:52 +02:00
|
|
|
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
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
a = strv_split(s, NULL);
|
2011-07-22 21:01:15 +02:00
|
|
|
if (!a)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-07-29 03:08:49 +02:00
|
|
|
strv_uniq(a);
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
r = (int) strv_length(a);
|
2011-07-29 03:08:49 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-07-11 19:14:16 +02:00
|
|
|
p = path_join("/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;
|
|
|
|
|
2019-07-11 19:14:16 +02:00
|
|
|
p = path_join("/run/systemd/sessions", buf);
|
2012-02-11 03:45:23 +01:00
|
|
|
}
|
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;
|
|
|
|
|
2018-11-12 14:18:03 +01:00
|
|
|
r = parse_env_file(NULL, p, "ACTIVE", &s);
|
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;
|
|
|
|
|
2018-11-12 14:18:03 +01:00
|
|
|
r = parse_env_file(NULL, p, "REMOTE", &s);
|
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;
|
|
|
|
|
2018-11-12 14:18:03 +01:00
|
|
|
r = parse_env_file(NULL, p, "STATE", &s);
|
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
|
|
|
|
2018-11-12 14:18:03 +01:00
|
|
|
r = parse_env_file(NULL, p, "UID", &s);
|
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
|
|
|
|
2018-11-12 14:18:03 +01:00
|
|
|
r = parse_env_file(NULL, p, field, &s);
|
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
|
|
|
|
2018-11-12 14:04:47 +01:00
|
|
|
r = parse_env_file(NULL, p,
|
2011-07-14 22:50:01 +02:00
|
|
|
"ACTIVE", &s,
|
2018-11-12 14:18:03 +01:00
|
|
|
"ACTIVE_UID", &t);
|
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
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
_public_ int sd_seat_get_sessions(
|
|
|
|
const char *seat,
|
|
|
|
char ***ret_sessions,
|
|
|
|
uid_t **ret_uids,
|
|
|
|
unsigned *ret_n_uids) {
|
|
|
|
|
|
|
|
_cleanup_free_ char *fname = NULL, *session_line = NULL, *uid_line = NULL;
|
|
|
|
_cleanup_strv_free_ char **sessions = NULL;
|
|
|
|
_cleanup_free_ uid_t *uids = NULL;
|
|
|
|
unsigned n_sessions = 0;
|
2011-07-22 21:01:15 +02:00
|
|
|
int r;
|
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
r = file_of_seat(seat, &fname);
|
2012-02-11 03:45:23 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
r = parse_env_file(NULL, fname,
|
|
|
|
"SESSIONS", &session_line,
|
|
|
|
"UIDS", &uid_line);
|
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;
|
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
if (session_line) {
|
|
|
|
sessions = strv_split(session_line, NULL);
|
|
|
|
if (!sessions)
|
2011-07-22 21:01:15 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
n_sessions = strv_length(sessions);
|
|
|
|
};
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
if (ret_uids && uid_line) {
|
|
|
|
uids = new(uid_t, n_sessions);
|
|
|
|
if (!uids)
|
|
|
|
return -ENOMEM;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
size_t n = 0;
|
|
|
|
for (const char *p = uid_line;;) {
|
|
|
|
_cleanup_free_ char *word = NULL;
|
2011-09-23 02:39:28 +02:00
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
r = extract_first_word(&p, &word, NULL, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
break;
|
2011-09-23 02:39:28 +02:00
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
r = parse_uid(word, &uids[n++]);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-07-22 21:01:15 +02:00
|
|
|
}
|
2011-07-29 03:08:49 +02:00
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
if (n != n_sessions)
|
|
|
|
return -EUCLEAN;
|
|
|
|
}
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
if (ret_sessions)
|
|
|
|
*ret_sessions = TAKE_PTR(sessions);
|
|
|
|
if (ret_uids)
|
|
|
|
*ret_uids = TAKE_PTR(uids);
|
|
|
|
if (ret_n_uids)
|
|
|
|
*ret_n_uids = n_sessions;
|
2011-07-22 21:01:15 +02:00
|
|
|
|
2020-07-30 12:43:07 +02:00
|
|
|
return n_sessions;
|
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
|
|
|
|
2018-11-12 14:04:47 +01:00
|
|
|
r = parse_env_file(NULL, p,
|
2018-11-12 14:18:03 +01:00
|
|
|
variable, &s);
|
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) {
|
2020-04-17 13:57:40 +02:00
|
|
|
return true;
|
2012-06-21 16:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_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);
|
2019-04-02 14:50:15 +02:00
|
|
|
t = reallocarray(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
|
|
|
}
|
|
|
|
|
2018-04-05 07:26:26 +02:00
|
|
|
if (users)
|
|
|
|
*users = TAKE_PTR(l);
|
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++) {
|
2020-12-11 16:40:45 +01:00
|
|
|
if (startswith(*a, "unit:") || !hostname_is_valid(*a, 0))
|
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;
|
|
|
|
|
|
|
|
assert_return(class, -EINVAL);
|
|
|
|
|
2018-07-17 12:24:50 +02:00
|
|
|
if (streq(machine, ".host")) {
|
|
|
|
c = strdup("host");
|
|
|
|
if (!c)
|
|
|
|
return -ENOMEM;
|
|
|
|
} else {
|
2020-12-11 16:40:45 +01:00
|
|
|
if (!hostname_is_valid(machine, 0))
|
2018-07-17 12:24:50 +02:00
|
|
|
return -EINVAL;
|
2014-03-12 20:54:29 +01:00
|
|
|
|
2018-07-17 12:24:50 +02:00
|
|
|
p = strjoina("/run/systemd/machines/", machine);
|
2018-11-12 14:18:03 +01:00
|
|
|
r = parse_env_file(NULL, p, "CLASS", &c);
|
2018-07-17 12:24:50 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
return -ENXIO;
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (!c)
|
|
|
|
return -EIO;
|
|
|
|
}
|
2014-03-12 20:54:29 +01:00
|
|
|
|
2018-07-17 12:24:50 +02:00
|
|
|
*class = TAKE_PTR(c);
|
2014-03-12 20:54:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:56:51 +02:00
|
|
|
_public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices) {
|
|
|
|
_cleanup_free_ char *netif_line = NULL;
|
|
|
|
const char *p;
|
2014-07-11 03:15:21 +02:00
|
|
|
int r;
|
|
|
|
|
2020-12-11 16:40:45 +01:00
|
|
|
assert_return(hostname_is_valid(machine, 0), -EINVAL);
|
2014-07-11 03:15:21 +02:00
|
|
|
|
2015-02-03 02:05:59 +01:00
|
|
|
p = strjoina("/run/systemd/machines/", machine);
|
2020-07-30 12:56:51 +02:00
|
|
|
r = parse_env_file(NULL, p, "NETIF", &netif_line);
|
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;
|
2020-07-30 12:56:51 +02:00
|
|
|
if (!netif_line) {
|
|
|
|
*ret_ifindices = NULL;
|
2014-07-11 03:15:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:56:51 +02:00
|
|
|
_cleanup_strv_free_ char **tt = strv_split(netif_line, NULL);
|
|
|
|
if (!tt)
|
|
|
|
return -ENOMEM;
|
2014-07-11 03:15:21 +02:00
|
|
|
|
2020-09-09 19:38:04 +02:00
|
|
|
_cleanup_free_ int *ifindices = NULL;
|
2020-07-30 13:08:52 +02:00
|
|
|
if (ret_ifindices) {
|
|
|
|
ifindices = new(int, strv_length(tt));
|
|
|
|
if (!ifindices)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2014-07-11 03:15:21 +02:00
|
|
|
|
2020-09-09 19:38:04 +02:00
|
|
|
size_t n = 0;
|
2020-07-30 12:56:51 +02:00
|
|
|
for (size_t i = 0; tt[i]; i++) {
|
|
|
|
int ind;
|
2014-07-11 03:15:21 +02:00
|
|
|
|
2020-07-30 12:56:51 +02:00
|
|
|
ind = parse_ifindex(tt[i]);
|
|
|
|
if (ind < 0)
|
|
|
|
/* Return -EUCLEAN to distinguish from -EINVAL for invalid args */
|
|
|
|
return ind == -EINVAL ? -EUCLEAN : ind;
|
2014-07-11 03:15:21 +02:00
|
|
|
|
2020-07-30 13:08:52 +02:00
|
|
|
if (ret_ifindices)
|
|
|
|
ifindices[n] = ind;
|
|
|
|
n++;
|
2014-07-11 03:15:21 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 13:08:52 +02:00
|
|
|
if (ret_ifindices)
|
2020-09-09 19:38:04 +02:00
|
|
|
*ret_ifindices = TAKE_PTR(ifindices);
|
|
|
|
|
2020-07-30 12:56:51 +02:00
|
|
|
return n;
|
2014-07-11 03:15:21 +02:00
|
|
|
}
|
|
|
|
|
2019-01-15 08:12:28 +01:00
|
|
|
static int MONITOR_TO_FD(sd_login_monitor *m) {
|
2011-07-22 21:01:15 +02:00
|
|
|
return (int) (unsigned long) m - 1;
|
|
|
|
}
|
|
|
|
|
2019-01-15 08:12:28 +01:00
|
|
|
static sd_login_monitor* FD_TO_MONITOR(int fd) {
|
2011-07-22 21:01:15 +02:00
|
|
|
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;
|
|
|
|
|
2020-09-22 17:33:06 +02:00
|
|
|
*m = FD_TO_MONITOR(TAKE_FD(fd));
|
2011-07-22 21:01:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
|
2020-09-22 17:33:06 +02:00
|
|
|
if (m)
|
|
|
|
close_nointr(MONITOR_TO_FD(m));
|
2011-07-22 21:01:15 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|