init: reboot, acpid

This commit is contained in:
Matheus
2024-09-01 23:14:12 -03:00
parent a876a61d08
commit 7a9f5c48c7
7 changed files with 119 additions and 18 deletions
+2
View File
@@ -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
+15 -1
View File
@@ -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 $@ $<
+44
View File
@@ -0,0 +1,44 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#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;
}
+2 -1
View File
@@ -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
+29 -13
View File
@@ -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();
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");
static void doreboot(void) {
printf("init: rebooting\n");
sendacpicommand('r');
}
oops:
printf("init: failed to send acpi power off. please manually turn the computer off.\n");
for (;;)
sleep(1);
static void dopoweroff(void) {
printf("init: powering off\n");
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);
}
+1 -1
View File
@@ -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;
}
+24
View File
@@ -0,0 +1,24 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#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;
}