]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Define _cleanup_ helper for setutxent()+endutxent()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 2 Jul 2020 14:24:04 +0000 (16:24 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 5 Jul 2020 10:16:01 +0000 (12:16 +0200)
src/login/logind-core.c
src/shared/utmp-wtmp.c
src/shared/utmp-wtmp.h

index 475759c79b514b3879246d9831c7ae6b84b08b7f..480ec1927b03cc5b513815b26686fb9306823ad9 100644 (file)
@@ -4,9 +4,6 @@
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <linux/vt.h>
-#if ENABLE_UTMP
-#include <utmpx.h>
-#endif
 
 #include "sd-device.h"
 
@@ -29,6 +26,7 @@
 #include "udev-util.h"
 #include "user-util.h"
 #include "userdb.h"
+#include "utmp-wtmp.h"
 
 void manager_reset_config(Manager *m) {
         assert(m);
@@ -685,13 +683,14 @@ bool manager_all_buttons_ignored(Manager *m) {
 int manager_read_utmp(Manager *m) {
 #if ENABLE_UTMP
         int r;
+        _cleanup_(utxent_cleanup) bool utmpx = false;
 
         assert(m);
 
         if (utmpxname(_PATH_UTMPX) < 0)
                 return log_error_errno(errno, "Failed to set utmp path to " _PATH_UTMPX ": %m");
 
-        setutxent();
+        utmpx = utxent_start();
 
         for (;;) {
                 _cleanup_free_ char *t = NULL;
@@ -704,8 +703,7 @@ int manager_read_utmp(Manager *m) {
                 if (!u) {
                         if (errno != 0)
                                 log_warning_errno(errno, "Failed to read " _PATH_UTMPX ", ignoring: %m");
-                        r = 0;
-                        break;
+                        return 0;
                 }
 
                 if (u->ut_type != USER_PROCESS)
@@ -715,18 +713,14 @@ int manager_read_utmp(Manager *m) {
                         continue;
 
                 t = strndup(u->ut_line, sizeof(u->ut_line));
-                if (!t) {
-                        r = log_oom();
-                        break;
-                }
+                if (!t)
+                        return log_oom();
 
                 c = path_startswith(t, "/dev/");
                 if (c) {
                         r = free_and_strdup(&t, c);
-                        if (r < 0) {
-                                log_oom();
-                                break;
-                        }
+                        if (r < 0)
+                                return log_oom();
                 }
 
                 if (isempty(t))
@@ -756,8 +750,6 @@ int manager_read_utmp(Manager *m) {
                 log_debug("Acquired TTY information '%s' from utmp for session '%s'.", s->tty, s->id);
         }
 
-        endutxent();
-        return r;
 #else
         return 0;
 #endif
index 9debb54390e6d80b5d09ab2da12b689726f4b4e4..096f99ec1a770785e9d8307593bf9a6a582d5447 100644 (file)
@@ -25,8 +25,8 @@
 #include "utmp-wtmp.h"
 
 int utmp_get_runlevel(int *runlevel, int *previous) {
+        _cleanup_(utxent_cleanup) bool utmpx = false;
         struct utmpx *found, lookup = { .ut_type = RUN_LVL };
-        int r;
         const char *e;
 
         assert(runlevel);
@@ -58,27 +58,17 @@ int utmp_get_runlevel(int *runlevel, int *previous) {
         if (utmpxname(_PATH_UTMPX) < 0)
                 return -errno;
 
-        setutxent();
+        utmpx = utxent_start();
 
         found = getutxid(&lookup);
         if (!found)
-                r = -errno;
-        else {
-                int a, b;
-
-                a = found->ut_pid & 0xFF;
-                b = (found->ut_pid >> 8) & 0xFF;
-
-                *runlevel = a;
-                if (previous)
-                        *previous = b;
-
-                r = 0;
-        }
+                return -errno;
 
-        endutxent();
+        *runlevel = found->ut_pid & 0xFF;
+        if (previous)
+                *previous = (found->ut_pid >> 8) & 0xFF;
 
-        return r;
+        return 0;
 }
 
 static void init_timestamp(struct utmpx *store, usec_t t) {
@@ -106,7 +96,7 @@ static void init_entry(struct utmpx *store, usec_t t) {
 }
 
 static int write_entry_utmp(const struct utmpx *store) {
-        int r;
+        _cleanup_(utxent_cleanup) bool utmpx = false;
 
         assert(store);
 
@@ -117,16 +107,11 @@ static int write_entry_utmp(const struct utmpx *store) {
         if (utmpxname(_PATH_UTMPX) < 0)
                 return -errno;
 
-        setutxent();
+        utmpx = utxent_start();
 
         if (!pututxline(store))
-                r = -errno;
-        else
-                r = 0;
-
-        endutxent();
-
-        return r;
+                return -errno;
+        return 0;
 }
 
 static int write_entry_wtmp(const struct utmpx *store) {
index 9e433cf73ec5a968ad5149b3768b3b1369c1f4c4..fe55bad12d632bd85b11938eca87a0fba780bbf2 100644 (file)
@@ -8,6 +8,8 @@
 #include "util.h"
 
 #if ENABLE_UTMP
+#include <utmpx.h>
+
 int utmp_get_runlevel(int *runlevel, int *previous);
 
 int utmp_put_shutdown(void);
@@ -24,6 +26,15 @@ int utmp_wall(
         bool (*match_tty)(const char *tty, void *userdata),
         void *userdata);
 
+static inline bool utxent_start(void) {
+        setutxent();
+        return true;
+}
+static inline void utxent_cleanup(bool *initialized) {
+        if (initialized)
+                endutxent();
+}
+
 #else /* ENABLE_UTMP */
 
 static inline int utmp_get_runlevel(int *runlevel, int *previous) {