]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-login/sd-login.c
tree-wide: use reallocarray() where appropriate
[thirdparty/systemd.git] / src / libsystemd / sd-login / sd-login.c
index c2f7133e425690f1781b26a1b49a210e6f03b12b..0dc368e6e25f69d27df834216373629a3a89321a 100644 (file)
@@ -1,6 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-***/
 
 #include <errno.h>
 #include <poll.h>
@@ -13,9 +11,9 @@
 #include "alloc-util.h"
 #include "cgroup-util.h"
 #include "dirent-util.h"
+#include "env-file.h"
 #include "escape.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "format-util.h"
 #include "fs-util.h"
 #include "hostname-util.h"
@@ -260,8 +258,7 @@ static int file_of_uid(uid_t uid, char **p) {
 }
 
 _public_ int sd_uid_get_state(uid_t uid, char**state) {
-        _cleanup_free_ char *p = NULL;
-        char *s = NULL;
+        _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
         assert_return(state, -EINVAL);
@@ -270,24 +267,17 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE, "STATE", &s, NULL);
+        r = parse_env_file(NULL, p, "STATE", &s);
         if (r == -ENOENT) {
-                free(s);
-                s = strdup("offline");
-                if (!s)
-                        return -ENOMEM;
-
-        }
-        else if (r < 0) {
-                free(s);
+                r = free_and_strdup(&s, "offline");
+                if (r < 0)
+                        return r;
+        } else if (r < 0)
                 return r;
-        }
-        if (isempty(s)) {
-                free(s);
+        else if (isempty(s))
                 return -EIO;
-        }
 
-        *state = s;
+        *state = TAKE_PTR(s);
         return 0;
 }
 
@@ -301,7 +291,7 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE, "DISPLAY", &s, NULL);
+        r = parse_env_file(NULL, p, "DISPLAY", &s);
         if (r == -ENOENT)
                 return -ENODATA;
         if (r < 0)
@@ -356,7 +346,7 @@ _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat)
 
         variable = require_active ? "ACTIVE_UID" : "UIDS";
 
-        r = parse_env_file(NULL, p, NEWLINE, variable, &s, NULL);
+        r = parse_env_file(NULL, p, variable, &s);
         if (r == -ENOENT)
                 return 0;
         if (r < 0)
@@ -385,7 +375,7 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE, variable, &s, NULL);
+        r = parse_env_file(NULL, p, variable, &s);
         if (r == -ENOENT || (r >= 0 && isempty(s))) {
                 if (array)
                         *array = NULL;
@@ -463,7 +453,7 @@ _public_ int sd_session_is_active(const char *session) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE, "ACTIVE", &s, NULL);
+        r = parse_env_file(NULL, p, "ACTIVE", &s);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -482,7 +472,7 @@ _public_ int sd_session_is_remote(const char *session) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE, "REMOTE", &s, NULL);
+        r = parse_env_file(NULL, p, "REMOTE", &s);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -503,7 +493,7 @@ _public_ int sd_session_get_state(const char *session, char **state) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE, "STATE", &s, NULL);
+        r = parse_env_file(NULL, p, "STATE", &s);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -526,7 +516,7 @@ _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE, "UID", &s, NULL);
+        r = parse_env_file(NULL, p, "UID", &s);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -548,7 +538,7 @@ static int session_get_string(const char *session, const char *field, char **val
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE, field, &s, NULL);
+        r = parse_env_file(NULL, p, field, &s);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -640,10 +630,9 @@ _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE,
+        r = parse_env_file(NULL, p,
                            "ACTIVE", &s,
-                           "ACTIVE_UID", &t,
-                           NULL);
+                           "ACTIVE_UID", &t);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -678,10 +667,9 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE,
+        r = parse_env_file(NULL, p,
                            "SESSIONS", &s,
-                           "UIDS", &t,
-                           NULL);
+                           "UIDS", &t);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -747,9 +735,8 @@ static int seat_get_can(const char *seat, const char *variable) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(NULL, p, NEWLINE,
-                           variable, &s,
-                           NULL);
+        r = parse_env_file(NULL, p,
+                           variable, &s);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -831,7 +818,7 @@ _public_ int sd_get_uids(uid_t **users) {
                                 uid_t *t;
 
                                 n = MAX(16, 2*r);
-                                t = realloc(l, sizeof(uid_t) * n);
+                                t = reallocarray(l, sizeof(uid_t), n);
                                 if (!t)
                                         return -ENOMEM;
 
@@ -892,20 +879,27 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
         const char *p;
         int r;
 
-        assert_return(machine_name_is_valid(machine), -EINVAL);
         assert_return(class, -EINVAL);
 
-        p = strjoina("/run/systemd/machines/", machine);
-        r = parse_env_file(NULL, p, NEWLINE, "CLASS", &c, NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
-        if (r < 0)
-                return r;
-        if (!c)
-                return -EIO;
+        if (streq(machine, ".host")) {
+                c = strdup("host");
+                if (!c)
+                        return -ENOMEM;
+        } else {
+                if (!machine_name_is_valid(machine))
+                        return -EINVAL;
 
-        *class = TAKE_PTR(c);
+                p = strjoina("/run/systemd/machines/", machine);
+                r = parse_env_file(NULL, p, "CLASS", &c);
+                if (r == -ENOENT)
+                        return -ENXIO;
+                if (r < 0)
+                        return r;
+                if (!c)
+                        return -EIO;
+        }
 
+        *class = TAKE_PTR(c);
         return 0;
 }
 
@@ -920,7 +914,7 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
         assert_return(ifindices, -EINVAL);
 
         p = strjoina("/run/systemd/machines/", machine);
-        r = parse_env_file(NULL, p, NEWLINE, "NETIF", &netif, NULL);
+        r = parse_env_file(NULL, p, "NETIF", &netif);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -951,11 +945,11 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
         return nr;
 }
 
-static inline int MONITOR_TO_FD(sd_login_monitor *m) {
+static int MONITOR_TO_FD(sd_login_monitor *m) {
         return (int) (unsigned long) m - 1;
 }
 
-static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
+static sd_login_monitor* FD_TO_MONITOR(int fd) {
         return (sd_login_monitor*) (unsigned long) (fd + 1);
 }