update-utmp: use verbs

No functional change, just refactoring.
This commit is contained in:
Yu Watanabe
2023-05-24 20:04:33 +09:00
parent 5f1f05b961
commit 230f663e3e
+17 -22
View 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);