mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
init: reboot, acpid
This commit is contained in:
+15
-1
@@ -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 $@ $<
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
+31
-15
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user