#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 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;
+
+ if (!s)
+ return -EIO;
+
+ return parse_boolean(s);
}
_public_ int sd_session_get_state(const char *session, char **state) {
_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)
return r;
r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
-
if (r < 0)
return r;
if (!s)
return -EIO;
- r = parse_uid(s, uid);
-
- return r;
+ return parse_uid(s, uid);
}
static int session_get_string(const char *session, const char *field, char **value) {
_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)
_cleanup_free_ char *p = NULL, *s = NULL;
int r;
+ assert_return(variable, -EINVAL);
+
r = file_of_seat(seat, &p);
if (r < 0)
return r;
NULL);
if (r < 0)
return r;
+ if (!s)
+ return 0;
- if (s)
- r = parse_boolean(s);
- else
- r = 0;
-
- return r;
+ return parse_boolean(s);
}
_public_ int sd_seat_can_multi_session(const char *seat) {
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;
}
+_public_ int sd_get_machine_names(char ***machines) {
+ char **l = NULL, **a, **b;
+ int r;
+
+ assert_return(machines, -EINVAL);
+
+ r = get_files_in_directory("/run/systemd/machines/", &l);
+ if (r < 0)
+ return r;
+
+ if (l) {
+ r = 0;
+
+ /* Filter out the unit: symlinks */
+ for (a = l, b = l; *a; a++) {
+ if (startswith(*a, "unit:"))
+ free(*a);
+ else {
+ *b = *a;
+ b++;
+ r++;
+ }
+ }
+
+ *b = NULL;
+ }
+
+ *machines = l;
+ return r;
+}
+
+_public_ int sd_machine_get_class(const char *machine, char **class) {
+ _cleanup_free_ char *c = NULL;
+ const char *p;
+ int r;
+
+ assert_return(filename_is_safe(machine), -EINVAL);
+ assert_return(class, -EINVAL);
+
+ p = strappenda("/run/systemd/machines/", machine);
+ r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
+ if (r < 0)
+ return r;
+ if (!c)
+ return -EIO;
+
+ *class = c;
+ c = NULL;
+
+ return 0;
+}
+
static inline int MONITOR_TO_FD(sd_login_monitor *m) {
return (int) (unsigned long) m - 1;
}
int fd, k;
bool good = false;
- if (!m)
- return -EINVAL;
+ assert_return(m, -EINVAL);
fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
if (fd < 0)
good = true;
}
+ if (!category || streq(category, "machine")) {
+ k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
+ if (k < 0) {
+ close_nointr_nofail(fd);
+ return -errno;
+ }
+
+ good = true;
+ }
+
if (!good) {
close_nointr(fd);
return -EINVAL;
_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