fiss

Friedel's Initialization and Service Supervision
Log | Files | Refs | LICENSE

commit e9e41609626596f6135cc00a225a0b32d50e1186
parent efd2e16f5ca9948f765e74cb2f59efb8c2b1ed80
Author: Friedel Schön <[email protected]>
Date:   Tue, 23 May 2023 22:12:17 +0200

merge poweroff/reboot/halt

Diffstat:
MMakefile | 7++++++-
Msrc/exec/halt.c | 43+++++++++++++++++++++++++++++++++++--------
Dsrc/exec/poweroff.c | 57---------------------------------------------------------
Asrc/exec/poweroff.lnk | 2++
Dsrc/exec/reboot.c | 57---------------------------------------------------------
Asrc/exec/reboot.lnk | 2++
6 files changed, 45 insertions(+), 123 deletions(-)

diff --git a/Makefile b/Makefile @@ -20,7 +20,8 @@ SOURCE_FILES := $(wildcard $(SRC_DIR)/*.c) EXEC_FILES := $(wildcard $(EXEC_DIR)/*) OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SOURCE_FILES)) BIN_FILES := $(patsubst $(EXEC_DIR)/%.c,$(BIN_DIR)/%,$(EXEC_FILES)) \ - $(patsubst $(EXEC_DIR)/%.sh,$(BIN_DIR)/%,$(EXEC_FILES)) + $(patsubst $(EXEC_DIR)/%.sh,$(BIN_DIR)/%,$(EXEC_FILES)) \ + $(patsubst $(EXEC_DIR)/%.lnk,$(BIN_DIR)/%,$(EXEC_FILES)) INCLUDE_FILES := $(wildcard $(INCLUDE_DIR)/*.h) MAN_FILES := $(wildcard $(MAN_DIR)/*) @@ -63,6 +64,10 @@ $(BIN_DIR)/%: $(EXEC_DIR)/%.sh | $(BIN_DIR) cp $< $@ chmod +x $@ +$(BIN_DIR)/%: $(EXEC_DIR)/%.lnk | $(BIN_DIR) + ln -sf $(shell cat $<) $@ + + # Manual targets $(ROFF_DIR)/%: $(MAN_DIR)/%.md | $(ROFF_DIR) diff --git a/src/exec/halt.c b/src/exec/halt.c @@ -1,17 +1,42 @@ +#include "util.h" #include "wtmp.h" -#include <err.h> +#include <errno.h> #include <stdbool.h> #include <string.h> #include <sys/reboot.h> #include <unistd.h> + int main(int argc, char* argv[]) { bool do_sync = true, do_force = false, do_wtmp = true, noop = false; - int opt; + int opt; + char* progname; + int rebootnum; + const char* initarg; + + if ((progname = strrchr(argv[0], '/')) != NULL) { + progname++; // to hide '/' + } else { + progname = argv[0]; + } + + if (streq(progname, "halt")) { + rebootnum = RB_HALT_SYSTEM; + initarg = "0"; + } else if (streq(progname, "poweroff")) { + rebootnum = RB_POWER_OFF; + initarg = "0"; + } else if (streq(progname, "reboot")) { + rebootnum = RB_AUTOBOOT; + initarg = "6"; + } else { + fprintf(stderr, "invalid mode: %s\n", progname); + return 1; + } while ((opt = getopt(argc, argv, "dfhinwB")) != -1) switch (opt) { @@ -36,7 +61,8 @@ int main(int argc, char* argv[]) { write_wtmp(1); return 0; default: - errx(1, "Usage: reboot [-n] [-f] [-d] [-w] [-B]"); + fprintf(stderr, "Usage: %s [-n] [-f] [-d] [-w] [-B]", progname); + return 1; } if (do_wtmp) @@ -46,11 +72,12 @@ int main(int argc, char* argv[]) { sync(); if (!noop) { - if (do_force) - reboot(RB_HALT_SYSTEM); - else - execl("/sbin/finit", "init", "0", NULL); - err(1, "reboot failed"); + if (do_force) { + reboot(rebootnum); + } else { + execl("/sbin/finit", "init", initarg, NULL); + } + print_error("reboot failed: %s\n"); } return 0; diff --git a/src/exec/poweroff.c b/src/exec/poweroff.c @@ -1,57 +0,0 @@ -#include "wtmp.h" - -#include <err.h> -#include <stdbool.h> -#include <string.h> -#include <sys/reboot.h> -#include <unistd.h> - -int main(int argc, char* argv[]) { - bool do_sync = true, - do_force = false, - do_wtmp = true, - noop = false; - int opt; - - while ((opt = getopt(argc, argv, "dfhinwB")) != -1) - switch (opt) { - case 'n': - do_sync = 0; - break; - case 'w': - noop = 1; - do_sync = 0; - break; - case 'd': - do_wtmp = 0; - break; - case 'h': - case 'i': - /* silently ignored. */ - break; - case 'f': - do_force = 1; - break; - case 'B': - write_wtmp(1); - return 0; - default: - errx(1, "Usage: poweroff [-n] [-f] [-d] [-w] [-B]"); - } - - if (do_wtmp) - write_wtmp(0); - - if (do_sync) - sync(); - - if (!noop) { - if (do_force) - reboot(RB_POWER_OFF); - else - execl("/sbin/finit", "init", "0", NULL); - err(1, "poweroff failed"); - } - - return 0; -} diff --git a/src/exec/poweroff.lnk b/src/exec/poweroff.lnk @@ -0,0 +1 @@ +halt +\ No newline at end of file diff --git a/src/exec/reboot.c b/src/exec/reboot.c @@ -1,57 +0,0 @@ -#include "wtmp.h" - -#include <err.h> -#include <stdbool.h> -#include <string.h> -#include <sys/reboot.h> -#include <unistd.h> - -int main(int argc, char* argv[]) { - bool do_sync = true, - do_force = false, - do_wtmp = true, - noop = false; - int opt; - - while ((opt = getopt(argc, argv, "dfhinwB")) != -1) - switch (opt) { - case 'n': - do_sync = 0; - break; - case 'w': - noop = 1; - do_sync = 0; - break; - case 'd': - do_wtmp = 0; - break; - case 'h': - case 'i': - /* silently ignored. */ - break; - case 'f': - do_force = 1; - break; - case 'B': - write_wtmp(1); - return 0; - default: - errx(1, "Usage: reboot [-n] [-f] [-d] [-w] [-B]"); - } - - if (do_wtmp) - write_wtmp(0); - - if (do_sync) - sync(); - - if (!noop) { - if (do_force) - reboot(RB_AUTOBOOT); - else - execl("/sbin/finit", "init", "6", NULL); - err(1, "reboot failed"); - } - - return 0; -} diff --git a/src/exec/reboot.lnk b/src/exec/reboot.lnk @@ -0,0 +1 @@ +halt +\ No newline at end of file