X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flogin%2Fsd-login.c;h=ef67040ebc065f0570a6d30ffdb1bd416cb8b8b7;hb=f39d4a08e746e703d562076a0f622eb91dbdcd3e;hp=66c4487ff88ff6a3f37f6fe117816cb540530579;hpb=38158b920e772ea3a7cc9dfcf705666ce3aa5ce3;p=elogind.git diff --git a/src/login/sd-login.c b/src/login/sd-login.c index 66c4487ff..ef67040eb 100644 --- a/src/login/sd-login.c +++ b/src/login/sd-login.c @@ -28,57 +28,55 @@ #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); } @@ -87,8 +85,7 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) { 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; @@ -121,8 +118,7 @@ _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) int r; const char *variable; - if (!seat) - return -EINVAL; + assert_return(seat, -EINVAL); variable = require_active ? "ACTIVE_UID" : "UIDS"; @@ -216,17 +212,19 @@ static int file_of_session(const char *session, char **_p) { 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) @@ -245,7 +243,26 @@ _public_ int sd_session_is_active(const char *session) { 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; @@ -261,8 +278,7 @@ _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) @@ -285,8 +301,7 @@ _public_ int sd_session_get_uid(const char *session, uid_t *uid) { 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) @@ -309,8 +324,7 @@ static int session_get_string(const char *session, const char *field, char **val _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) @@ -337,6 +351,23 @@ _public_ int sd_session_get_tty(const char *session, char **tty) { 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); } @@ -353,6 +384,14 @@ _public_ int sd_session_get_display(const char *session, char **display) { 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; @@ -383,8 +422,7 @@ _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) { _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) @@ -545,13 +583,13 @@ _public_ int sd_get_uids(uid_t **users) { 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; @@ -591,41 +629,33 @@ _public_ int sd_get_uids(uid_t **users) { 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) { @@ -640,8 +670,7 @@ _public_ int sd_login_monitor_new(const char *category, 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) @@ -678,18 +707,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) { } 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; @@ -710,8 +728,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) { _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); @@ -721,24 +738,21 @@ _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) { _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 @@ -749,10 +763,8 @@ _public_ int sd_login_monitor_get_events(sd_login_monitor *m) { _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