#include "util.h"
#include "cgroup-util.h"
#include "macro.h"
-#include "sd-login.h"
#include "strv.h"
#include "fileio.h"
+#include "login-shared.h"
+#include "sd-login.h"
_public_ int sd_pid_get_session(pid_t pid, char **session) {
- if (pid < 0)
- return -EINVAL;
- if (!session)
- return -EINVAL;
+ assert_return(pid >= 0, -EINVAL);
+ assert_return(session, -EINVAL);
return cg_pid_get_session(pid, session);
}
_public_ int sd_pid_get_unit(pid_t pid, char **unit) {
- if (pid < 0)
- return -EINVAL;
- if (!unit)
- return -EINVAL;
+ assert_return(pid >= 0, -EINVAL);
+ assert_return(unit, -EINVAL);
return cg_pid_get_unit(pid, unit);
}
_public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
- if (pid < 0)
- return -EINVAL;
- if (!unit)
- return -EINVAL;
+ assert_return(pid >= 0, -EINVAL);
+ assert_return(unit, -EINVAL);
return cg_pid_get_user_unit(pid, unit);
}
_public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
- if (pid < 0)
- return -EINVAL;
- if (!name)
- return -EINVAL;
+ assert_return(pid >= 0, -EINVAL);
+ assert_return(name, -EINVAL);
return cg_pid_get_machine_name(pid, name);
}
-_public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
+_public_ int sd_pid_get_slice(pid_t pid, char **slice) {
- if (pid < 0)
- return -EINVAL;
+ assert_return(pid >= 0, -EINVAL);
+ assert_return(slice, -EINVAL);
- if (!uid)
- return -EINVAL;
+ return cg_pid_get_slice(pid, slice);
+}
+
+_public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
+
+ assert_return(pid >= 0, -EINVAL);
+ assert_return(uid, -EINVAL);
return cg_pid_get_owner_uid(pid, uid);
}
char *p, *s = NULL;
int r;
- if (!state)
- return -EINVAL;
+ assert_return(state, -EINVAL);
if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
return -ENOMEM;
int r;
const char *variable;
- if (!seat)
- return -EINVAL;
+ assert_return(seat, -EINVAL);
variable = require_active ? "ACTIVE_UID" : "UIDS";
assert(_p);
- if (session)
+ if (session) {
+ if (!session_id_valid(session))
+ return -EINVAL;
+
p = strappend("/run/systemd/sessions/", session);
- else {
- char *buf;
+ } else {
+ _cleanup_free_ char *buf = NULL;
r = sd_pid_get_session(0, &buf);
if (r < 0)
return r;
p = strappend("/run/systemd/sessions/", buf);
- free(buf);
}
if (!p)
return r;
r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
+ if (r < 0)
+ return r;
+
+ if (!s)
+ return -EIO;
+ r = parse_boolean(s);
+
+ return r;
+}
+
+_public_ int sd_session_is_remote(const char *session) {
+ int r;
+ _cleanup_free_ char *p = NULL, *s = NULL;
+
+ r = file_of_session(session, &p);
+ if (r < 0)
+ return r;
+
+ r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
if (r < 0)
return r;
_cleanup_free_ char *p = NULL, *s = NULL;
int r;
- if (!state)
- return -EINVAL;
+ assert_return(state, -EINVAL);
r = file_of_session(session, &p);
if (r < 0)
int r;
_cleanup_free_ char *p = NULL, *s = NULL;
- if (!uid)
- return -EINVAL;
+ assert_return(uid, -EINVAL);
r = file_of_session(session, &p);
if (r < 0)
_cleanup_free_ char *p = NULL, *s = NULL;
int r;
- if (!value)
- return -EINVAL;
+ assert_return(value, -EINVAL);
r = file_of_session(session, &p);
if (r < 0)
return session_get_string(session, "TTY", tty);
}
+_public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
+ _cleanup_free_ char *vtnr_string = NULL;
+ unsigned u;
+ int r;
+
+ r = session_get_string(session, "VTNR", &vtnr_string);
+ if (r < 0)
+ return r;
+
+ r = safe_atou(vtnr_string, &u);
+ if (r < 0)
+ return r;
+
+ *vtnr = u;
+ return 0;
+}
+
_public_ int sd_session_get_service(const char *session, char **service) {
return session_get_string(session, "SERVICE", service);
}
return session_get_string(session, "DISPLAY", display);
}
+_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);
+}
+
static int file_of_seat(const char *seat, char **_p) {
char *p;
int r;
_cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
int r;
- if (!session && !uid)
- return -EINVAL;
+ assert_return(session || uid, -EINVAL);
r = file_of_seat(seat, &p);
if (r < 0)
for (;;) {
struct dirent *de;
- union dirent_storage buf;
int k;
uid_t uid;
- k = readdir_r(d, &buf.de, &de);
- if (k != 0)
- return -k;
+ errno = 0;
+ de = readdir(d);
+ if (!de && errno != 0)
+ return -errno;
if (!de)
break;
return r;
}
-int sd_get_machine_names(char ***machines) {
- _cleanup_closedir_ DIR *d = NULL;
- _cleanup_strv_free_ char **l = NULL;
- _cleanup_free_ char *md = NULL;
- char *n;
- int c = 0, r;
-
- r = cg_get_machine_path(NULL, &md);
- if (r < 0)
- return r;
+_public_ int sd_get_machine_names(char ***machines) {
+ char **l = NULL, **a, **b;
+ int r;
- r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, md, &d);
+ r = get_files_in_directory("/run/systemd/machines/", &l);
if (r < 0)
return r;
- while ((r = cg_read_subgroup(d, &n)) > 0) {
+ if (l) {
+ r = 0;
- r = strv_push(&l, n);
- if (r < 0) {
- free(n);
- return -ENOMEM;
+ /* Filter out the unit: symlinks */
+ for (a = l, b = l; *a; a++) {
+ if (startswith(*a, "unit:"))
+ free(*a);
+ else {
+ *b = *a;
+ b++;
+ r++;
+ }
}
- c++;
+ *b = NULL;
}
- if (r < 0)
- return r;
-
- if (machines) {
- *machines = l;
- l = NULL;
- }
-
- return c;
+ *machines = l;
+ return r;
}
static inline int MONITOR_TO_FD(sd_login_monitor *m) {
int fd, k;
bool good = false;
- if (!m)
- return -EINVAL;
+ assert_return(m, -EINVAL);
fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
if (fd < 0)
}
if (!category || streq(category, "machine")) {
- _cleanup_free_ char *md = NULL, *p = NULL;
- int r;
-
- r = cg_get_machine_path(NULL, &md);
- if (r < 0)
- return r;
-
- r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, md, NULL, &p);
- if (r < 0)
- return r;
-
- k = inotify_add_watch(fd, p, IN_MOVED_TO|IN_CREATE|IN_DELETE);
+ k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
if (k < 0) {
close_nointr_nofail(fd);
return -errno;
_public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
int fd;
- if (!m)
- return NULL;
+ assert_return(m, NULL);
fd = MONITOR_TO_FD(m);
close_nointr(fd);
_public_ int sd_login_monitor_flush(sd_login_monitor *m) {
- if (!m)
- return -EINVAL;
+ assert_return(m, -EINVAL);
return flush_fd(MONITOR_TO_FD(m));
}
_public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {
- if (!m)
- return -EINVAL;
+ assert_return(m, -EINVAL);
return MONITOR_TO_FD(m);
}
_public_ int sd_login_monitor_get_events(sd_login_monitor *m) {
- if (!m)
- return -EINVAL;
+ 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
_public_ int sd_login_monitor_get_timeout(sd_login_monitor *m, uint64_t *timeout_usec) {
- if (!m)
- return -EINVAL;
- if (!timeout_usec)
- return -EINVAL;
+ assert_return(m, -EINVAL);
+ assert_return(timeout_usec, -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