diff --git a/distro-files/etc/rc b/distro-files/etc/rc index 9763373e..054be043 100644 --- a/distro-files/etc/rc +++ b/distro-files/etc/rc @@ -16,3 +16,5 @@ chmod 0755 /dev/pts # required to be owned by root and sticky for rootless Xorg. mkdir -p /tmp/.X11-unix chmod 1777 /tmp/.X11-unix + +/usr/sbin/acpid diff --git a/tools/init/Makefile b/tools/init/Makefile index 3884c4bc..5a73fc05 100644 --- a/tools/init/Makefile +++ b/tools/init/Makefile @@ -2,12 +2,14 @@ CC=x86_64-astral-gcc LD=x86_64-astral-gcc -all: init poweroff +all: init poweroff reboot acpid install: mkdir -pv "$(DESTDIR)/usr/sbin" cp -v init "$(DESTDIR)/init" cp -v poweroff "$(DESTDIR)/usr/sbin/poweroff" + cp -v reboot "$(DESTDIR)/usr/sbin/reboot" + cp -v acpid "$(DESTDIR)/usr/sbin/acpid" init: main.o $(LD) $(LDFLAGS) -o $@ $^ @@ -15,8 +17,20 @@ init: main.o poweroff: poweroff.o $(LD) $(LDFLAGS) -o $@ $^ +reboot: reboot.o + $(LD) $(LDFLAGS) -o $@ $^ + +acpid: acpid.o + $(LD) $(LDFLAGS) -o $@ $^ + main.o: main.c $(CC) -c $(CFLAGS) -o $@ $< poweroff.o: poweroff.c $(CC) -c $(CFLAGS) -o $@ $< + +reboot.o: reboot.c + $(CC) -c $(CFLAGS) -o $@ $< + +acpid.o: acpid.c + $(CC) -c $(CFLAGS) -o $@ $< diff --git a/tools/init/acpid.c b/tools/init/acpid.c new file mode 100644 index 00000000..ff619224 --- /dev/null +++ b/tools/init/acpid.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include + +#include "common.h" + +int main() { + if (daemon(0, 0)) { + perror("acpid: daemon"); + return EXIT_FAILURE; + } + + int fd = open("/dev/acpi", O_RDONLY); + if (fd == -1) { + perror("acpid: open"); + return EXIT_FAILURE; + } + + for (;;) { + char c; + int n = read(fd, &c, 1); + + int cmdfd = open(COMMAND_FIFO, O_WRONLY); + if (cmdfd == -1) { + perror("acpid: open"); + continue; + } + + switch (c) { + case 'p': + if (write(cmdfd, &c, 1) < 0) + perror("acpid: write"); + break; + default: + fprintf(stderr, "acpid: unknown acpi event %c\n", c); + } + + close(cmdfd); + } + + close(fd); + return EXIT_SUCCESS; +} diff --git a/tools/init/common.h b/tools/init/common.h index 4897a0b3..c20903cd 100644 --- a/tools/init/common.h +++ b/tools/init/common.h @@ -1,7 +1,8 @@ #ifndef _COMMON_H #define _COMMON_H -#define COMMAND_POWEROFF 'c' +#define COMMAND_POWEROFF 'p' +#define COMMAND_REBOOT 'r' #define COMMAND_FIFO "/tmp/.initcmd" #endif diff --git a/tools/init/main.c b/tools/init/main.c index 4a1f7c63..7c48db08 100644 --- a/tools/init/main.c +++ b/tools/init/main.c @@ -12,8 +12,24 @@ #include "common.h" -static void dopoweroff(void) { - printf("init: shutting down system\n"); +static void sendacpicommand(char c) { + int acpifd = open("/dev/acpi", O_WRONLY); + if (acpifd == -1) { + perror("init: open /dev/acpi"); + goto oops; + } + + write(acpifd, &c, 1); + perror("init: write /dev/acpi"); + + oops: + printf("init: failed to send acpi command. please manually do what is needed.\n"); + for (;;) + sleep(1); +} + +static void preparetodie(void) { + printf("init: bringing system down\n"); //printf("init: sending SIGTERM to all processes\n"); // TODO kill(-1, SIGTERM); // TODO wait 5 seconds or until all children have died @@ -23,20 +39,16 @@ static void dopoweroff(void) { printf("init: syncing disks\n"); sync(); +} + +static void doreboot(void) { + printf("init: rebooting\n"); + sendacpicommand('r'); +} + +static void dopoweroff(void) { printf("init: powering off\n"); - int acpifd = open("/dev/acpi", O_WRONLY); - if (acpifd == -1) { - perror("init: open /dev/acpi"); - goto oops; - } - - write(acpifd, "p", 1); - perror("init: write /dev/acpi"); - - oops: - printf("init: failed to send acpi power off. please manually turn the computer off.\n"); - for (;;) - sleep(1); + sendacpicommand('p'); } static void handlecommands(void) { @@ -65,8 +77,12 @@ static void handlecommands(void) { switch (c) { case COMMAND_POWEROFF: + preparetodie(); dopoweroff(); break; + case COMMAND_REBOOT: + preparetodie(); + doreboot(); default: printf("init: unknown command %c\n", c); } diff --git a/tools/init/poweroff.c b/tools/init/poweroff.c index a8b06429..e6a47b40 100644 --- a/tools/init/poweroff.c +++ b/tools/init/poweroff.c @@ -14,7 +14,7 @@ int main() { return EXIT_FAILURE; } - if (write(fd, &c, 1)) { + if (write(fd, &c, 1) < 0) { perror("poweroff: write"); return EXIT_FAILURE; } diff --git a/tools/init/reboot.c b/tools/init/reboot.c new file mode 100644 index 00000000..738a5e74 --- /dev/null +++ b/tools/init/reboot.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +#include "common.h" + +int main() { + char c = COMMAND_REBOOT; + int fd = open(COMMAND_FIFO, O_WRONLY); + + if (fd == -1) { + perror("poweroff: open"); + return EXIT_FAILURE; + } + + if (write(fd, &c, 1) < 0) { + perror("poweroff: write"); + return EXIT_FAILURE; + } + + close(fd); + return EXIT_SUCCESS; +}