X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flogin%2Fsd-login.c;h=c9a2e8a07fbb883e007f553d77779f92d726e880;hp=d2e95034e1388987637ac66ee8f153ae7b2d833a;hb=ec202eae8e84a4c99f054f771cb832046cb8769f;hpb=9444b1f20e311f073864d81e913bd4f32fe95cfd diff --git a/src/login/sd-login.c b/src/login/sd-login.c index d2e95034e..c9a2e8a07 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,6 @@ _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; @@ -261,8 +258,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 +281,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 +304,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 +331,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); } @@ -383,8 +394,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) @@ -592,40 +602,7 @@ _public_ int sd_get_uids(uid_t **users) { } _public_ 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_root_path(&md); - if (r < 0) - return r; - - r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, md, &d); - if (r < 0) - return r; - - while ((r = cg_read_subgroup(d, &n)) > 0) { - - r = strv_push(&l, n); - if (r < 0) { - free(n); - return -ENOMEM; - } - - c++; - } - - if (r < 0) - return r; - - if (machines) { - *machines = l; - l = NULL; - } - - return c; + return get_files_in_directory("/run/systemd/machines/", machines); } static inline int MONITOR_TO_FD(sd_login_monitor *m) { @@ -640,8 +617,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 +654,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_root_path(&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 +675,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 +685,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 +710,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