]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
update-utmp: define main() through macro
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 5 Aug 2019 13:53:28 +0000 (15:53 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 16 Sep 2019 07:15:05 +0000 (09:15 +0200)
Update logging a bit: drop logging of the pid, nowadays pid1 and journald
do a very good job of logging that. Always log about failure to open audit
fd, but at DEBUG_LEVEL if not important.

src/update-utmp/update-utmp.c

index 56fb937647e5b09bc5e864ff4428cb83a65adc43..f0fc181b570d632a040fd93417264f3beb07faef 100644 (file)
@@ -18,6 +18,7 @@
 #include "format-util.h"
 #include "log.h"
 #include "macro.h"
+#include "main-func.h"
 #include "process-util.h"
 #include "special.h"
 #include "strv.h"
@@ -220,7 +221,7 @@ static int on_runlevel(Context *c) {
         return r;
 }
 
-int main(int argc, char *argv[]) {
+static int run(int argc, char *argv[]) {
         _cleanup_(context_clear) Context c = {
 #if HAVE_AUDIT
                 .audit_fd = -1
@@ -228,47 +229,35 @@ int main(int argc, char *argv[]) {
         };
         int r;
 
-        if (getppid() != 1) {
-                log_error("This program should be invoked by init only.");
-                return EXIT_FAILURE;
-        }
-
-        if (argc != 2) {
-                log_error("This program requires one argument.");
-                return EXIT_FAILURE;
-        }
+        if (getppid() != 1)
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "This program should be invoked by init only.");
+        if (argc != 2)
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "This program requires one argument.");
 
         log_setup_service();
 
         umask(0022);
 
 #if HAVE_AUDIT
-        /* If the kernel lacks netlink or audit support,
-         * don't worry about it. */
+        /* If the kernel lacks netlink or audit support, don't worry about it. */
         c.audit_fd = audit_open();
-        if (c.audit_fd < 0 && !IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT))
-                log_error_errno(errno, "Failed to connect to audit log: %m");
+        if (c.audit_fd < 0)
+                log_full_errno(IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT) ? LOG_DEBUG : LOG_ERR,
+                               errno, "Failed to connect to audit log: %m");
 #endif
         r = bus_connect_system_systemd(&c.bus);
-        if (r < 0) {
-                log_error_errno(r, "Failed to get D-Bus connection: %m");
-                return EXIT_FAILURE;
-        }
-
-        log_debug("systemd-update-utmp running as pid "PID_FMT, getpid_cached());
+        if (r < 0)
+                return log_error_errno(r, "Failed to get D-Bus connection: %m");
 
         if (streq(argv[1], "reboot"))
-                r = on_reboot(&c);
-        else if (streq(argv[1], "shutdown"))
-                r = on_shutdown(&c);
-        else if (streq(argv[1], "runlevel"))
-                r = on_runlevel(&c);
-        else {
-                log_error("Unknown command %s", argv[1]);
-                return EXIT_FAILURE;
-        }
-
-        log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid_cached());
-
-        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+                return on_reboot(&c);
+        if (streq(argv[1], "shutdown"))
+                return on_shutdown(&c);
+        if (streq(argv[1], "runlevel"))
+                return on_runlevel(&c);
+        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command %s", argv[1]);
 }
+
+DEFINE_MAIN_FUNCTION(run);