chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.git] / src / libelogind / sd-login / sd-login.c
index bc5f83eb3069fe3f134b36f328e5e3a01877d247..0a9f246480ed87ccaa0ace79423b06dc1dc0ea7c 100644 (file)
@@ -1,21 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
   Copyright 2011 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
 #include <errno.h>
@@ -316,7 +301,7 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, "STATE", &s, NULL);
         if (r == -ENOENT) {
                 free(s);
                 s = strdup("offline");
@@ -347,7 +332,7 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, "DISPLAY", &s, NULL);
         if (r == -ENOENT)
                 return -ENODATA;
         if (r < 0)
@@ -355,8 +340,7 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) {
         if (isempty(s))
                 return -ENODATA;
 
-        *session = s;
-        s = NULL;
+        *session = TAKE_PTR(s);
 
         return 0;
 }
@@ -385,8 +369,7 @@ static int file_of_seat(const char *seat, char **_p) {
         if (!p)
                 return -ENOMEM;
 
-        *_p = p;
-        p = NULL;
+        *_p = TAKE_PTR(p);
         return 0;
 }
 
@@ -404,7 +387,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(p, NEWLINE, variable, &s, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, variable, &s, NULL);
         if (r == -ENOENT)
                 return 0;
         if (r < 0)
@@ -433,7 +416,7 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE, variable, &s, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, variable, &s, NULL);
         if (r == -ENOENT || (r >= 0 && isempty(s))) {
                 if (array)
                         *array = NULL;
@@ -447,7 +430,7 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
                 return -ENOMEM;
 
         strv_uniq(a);
-        r = strv_length(a);
+        r = (int) strv_length(a);
 
         if (array)
                 *array = a;
@@ -511,7 +494,7 @@ _public_ int sd_session_is_active(const char *session) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, "ACTIVE", &s, NULL);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -530,7 +513,7 @@ _public_ int sd_session_is_remote(const char *session) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, "REMOTE", &s, NULL);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -551,7 +534,7 @@ _public_ int sd_session_get_state(const char *session, char **state) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, "STATE", &s, NULL);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -559,8 +542,7 @@ _public_ int sd_session_get_state(const char *session, char **state) {
         if (isempty(s))
                 return -EIO;
 
-        *state = s;
-        s = NULL;
+        *state = TAKE_PTR(s);
 
         return 0;
 }
@@ -575,7 +557,7 @@ _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, "UID", &s, NULL);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -597,7 +579,7 @@ static int session_get_string(const char *session, const char *field, char **val
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE, field, &s, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, field, &s, NULL);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -605,8 +587,7 @@ static int session_get_string(const char *session, const char *field, char **val
         if (isempty(s))
                 return -ENODATA;
 
-        *value = s;
-        s = NULL;
+        *value = TAKE_PTR(s);
         return 0;
 }
 
@@ -690,7 +671,7 @@ _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE,
+        r = parse_env_file(NULL, p, NEWLINE,
                            "ACTIVE", &s,
                            "ACTIVE_UID", &t,
                            NULL);
@@ -711,10 +692,8 @@ _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
                         return r;
         }
 
-        if (session && s) {
-                *session = s;
-                s = NULL;
-        }
+        if (session && s)
+                *session = TAKE_PTR(s);
 
         return 0;
 }
@@ -730,7 +709,7 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE,
+        r = parse_env_file(NULL, p, NEWLINE,
                            "SESSIONS", &s,
                            "UIDS", &t,
                            NULL);
@@ -775,17 +754,13 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
                 }
         }
 
-        r = strv_length(a);
+        r = (int) strv_length(a);
 
-        if (sessions) {
-                *sessions = a;
-                a = NULL;
-        }
+        if (sessions)
+                *sessions = TAKE_PTR(a);
 
-        if (uids) {
-                *uids = b;
-                b = NULL;
-        }
+        if (uids)
+                *uids = TAKE_PTR(b);
 
         if (n_uids)
                 *n_uids = n;
@@ -803,7 +778,7 @@ static int seat_get_can(const char *seat, const char *variable) {
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE,
+        r = parse_env_file(NULL, p, NEWLINE,
                            variable, &s,
                            NULL);
         if (r == -ENOENT)
@@ -900,10 +875,8 @@ _public_ int sd_get_uids(uid_t **users) {
                         r++;
         }
 
-        if (users) {
-                *users = l;
-                l = NULL;
-        }
+        if (users)
+                *users = TAKE_PTR(l);
 
         return r;
 }
@@ -939,10 +912,9 @@ _public_ int sd_get_machine_names(char ***machines) {
                 *b = NULL;
         }
 
-        if (machines) {
-                *machines = l;
-                l = NULL;
-        }
+        if (machines)
+                *machines = TAKE_PTR(l);
+
         return r;
 }
 
@@ -955,7 +927,7 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
         assert_return(class, -EINVAL);
 
         p = strjoina("/run/systemd/machines/", machine);
-        r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, "CLASS", &c, NULL);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -963,8 +935,7 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
         if (!c)
                 return -EIO;
 
-        *class = c;
-        c = NULL;
+        *class = TAKE_PTR(c);
 
         return 0;
 }
@@ -980,7 +951,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(p, NEWLINE, "NETIF", &netif, NULL);
+        r = parse_env_file(NULL, p, NEWLINE, "NETIF", &netif, NULL);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
@@ -1020,8 +991,9 @@ static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
 }
 
 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
-        int fd, k;
+        _cleanup_close_ int fd = -1;
         bool good = false;
+        int k;
 
         assert_return(m, -EINVAL);
 
@@ -1031,50 +1003,42 @@ _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) {
-                        safe_close(fd);
+                if (k < 0)
                         return -errno;
-                }
 
                 good = true;
         }
 
         if (!category || streq(category, "session")) {
                 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
-                if (k < 0) {
-                        safe_close(fd);
+                if (k < 0)
                         return -errno;
-                }
 
                 good = true;
         }
 
         if (!category || streq(category, "uid")) {
                 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
-                if (k < 0) {
-                        safe_close(fd);
+                if (k < 0)
                         return -errno;
-                }
 
                 good = true;
         }
 
         if (!category || streq(category, "machine")) {
                 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
-                if (k < 0) {
-                        safe_close(fd);
+                if (k < 0)
                         return -errno;
-                }
 
                 good = true;
         }
 
-        if (!good) {
-                close_nointr(fd);
+        if (!good)
                 return -EINVAL;
-        }
 
         *m = FD_TO_MONITOR(fd);
+        fd = -1;
+
         return 0;
 }