]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
update-utmp: use verbs
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 6 Apr 2023 19:07:17 +0000 (04:07 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 24 May 2023 11:04:33 +0000 (20:04 +0900)
No functional change, just refactoring.

src/update-utmp/update-utmp.c

index 4996272eb3a148033ea9c04ab5e107e01c7031b4..e2faace3f7f91a0f4927da0496cc74b43b1fb905 100644 (file)
@@ -25,6 +25,7 @@
 #include "strv.h"
 #include "unit-name.h"
 #include "utmp-wtmp.h"
+#include "verbs.h"
 
 typedef struct Context {
         sd_bus *bus;
@@ -105,12 +106,10 @@ static int get_current_runlevel(Context *c) {
         return 0;
 }
 
-static int on_reboot(Context *c) {
+static int on_reboot(int argc, char *argv[], void *userdata) {
+        Context *c = ASSERT_PTR(userdata);
+        usec_t t, boottime;
         int r = 0, q;
-        usec_t t;
-        usec_t boottime;
-
-        assert(c);
 
         /* We finished start-up, so let's write the utmp
          * record and send the audit msg */
@@ -139,15 +138,15 @@ static int on_reboot(Context *c) {
         return r;
 }
 
-static int on_shutdown(Context *c) {
+static int on_shutdown(int argc, char *argv[], void *userdata) {
         int r = 0, q;
 
-        assert(c);
-
         /* We started shut-down, so let's write the utmp
          * record and send the audit msg */
 
 #if HAVE_AUDIT
+        Context *c = ASSERT_PTR(userdata);
+
         if (c->audit_fd >= 0)
                 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
                     errno != EPERM)
@@ -161,11 +160,10 @@ static int on_shutdown(Context *c) {
         return r;
 }
 
-static int on_runlevel(Context *c) {
+static int on_runlevel(int argc, char *argv[], void *userdata) {
+        Context *c = ASSERT_PTR(userdata);
         int r = 0, q, previous, runlevel;
 
-        assert(c);
-
         /* We finished changing runlevel, so let's write the
          * utmp record and send the audit msg */
 
@@ -213,6 +211,13 @@ static int on_runlevel(Context *c) {
 }
 
 static int run(int argc, char *argv[]) {
+        static const Verb verbs[] = {
+                { "reboot",   1, 1, 0, on_reboot   },
+                { "shutdown", 1, 1, 0, on_shutdown },
+                { "runlevel", 1, 1, 0, on_runlevel },
+                {}
+        };
+
         _cleanup_(context_clear) Context c = {
 #if HAVE_AUDIT
                 .audit_fd = -EBADF,
@@ -220,10 +225,6 @@ static int run(int argc, char *argv[]) {
         };
         int r;
 
-        if (argc != 2)
-                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                       "This program requires one argument.");
-
         log_setup();
 
         umask(0022);
@@ -239,13 +240,7 @@ static int run(int argc, char *argv[]) {
         if (r < 0)
                 return log_error_errno(r, "Failed to get D-Bus connection: %m");
 
-        if (streq(argv[1], "reboot"))
-                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]);
+        return dispatch_verb(argc, argv, verbs, &c);
 }
 
 DEFINE_MAIN_FUNCTION(run);