chiark / gitweb /
sd-login: use the same code for verfiying machine names everywhere
[elogind.git] / src / libsystemd / sd-login / sd-login.c
index d24b2ed1fdd698f1b62bc04d5cdbe54b89c20df4..d1478ddb785a43703dd61dc479d616306a9e3a3b 100644 (file)
@@ -165,6 +165,15 @@ _public_ int sd_peer_get_slice(int fd, char **slice) {
         return cg_pid_get_slice(ucred.pid, slice);
 }
 
+static int file_of_uid(uid_t uid, char **p) {
+        assert(p);
+
+        if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
+                return -ENOMEM;
+
+        return 0;
+}
+
 _public_ int sd_uid_get_state(uid_t uid, char**state) {
         _cleanup_free_ char *p = NULL;
         char *s = NULL;
@@ -172,8 +181,9 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) {
 
         assert_return(state, -EINVAL);
 
-        if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
-                return -ENOMEM;
+        r = file_of_uid(uid, &p);
+        if (r < 0)
+                return r;
 
         r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
         if (r == -ENOENT) {
@@ -192,6 +202,29 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) {
         return 0;
 }
 
+_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;
+
+        r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
+        if (r < 0)
+                return r;
+
+        if (isempty(s))
+                return -ENOENT;
+
+        *session = s;
+        s = NULL;
+
+        return 0;
+}
+
 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
         char *w, *state;
         _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
@@ -215,7 +248,7 @@ _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat)
         if (!s)
                 return -EIO;
 
-        if (asprintf(&t, "%lu", (unsigned long) uid) < 0)
+        if (asprintf(&t, UID_FMT, uid) < 0)
                 return -ENOMEM;
 
         FOREACH_WORD(w, l, s, state) {
@@ -231,8 +264,9 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
         char **a;
         int r;
 
-        if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
-                return -ENOMEM;
+        r = file_of_uid(uid, &p);
+        if (r < 0)
+                return r;
 
         r = parse_env_file(p, NEWLINE,
                            variable, &s,
@@ -362,7 +396,6 @@ _public_ int sd_session_get_state(const char *session, char **state) {
                 return r;
 
         r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
-
         if (r < 0)
                 return r;
         else if (!s)
@@ -405,7 +438,6 @@ static int session_get_string(const char *session, const char *field, char **val
                 return r;
 
         r = parse_env_file(p, NEWLINE, field, &s, NULL);
-
         if (r < 0)
                 return r;
 
@@ -738,7 +770,7 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
         const char *p;
         int r;
 
-        assert_return(filename_is_safe(machine), -EINVAL);
+        assert_return(machine_name_is_valid(machine), -EINVAL);
         assert_return(class, -EINVAL);
 
         p = strappenda("/run/systemd/machines/", machine);
@@ -775,7 +807,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
         if (!category || streq(category, "seat")) {
                 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -errno;
                 }
 
@@ -785,7 +817,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
         if (!category || streq(category, "session")) {
                 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -errno;
                 }
 
@@ -795,7 +827,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
         if (!category || streq(category, "uid")) {
                 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -errno;
                 }
 
@@ -805,7 +837,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
         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);
+                        safe_close(fd);
                         return -errno;
                 }