fixed init

This commit is contained in:
Your Name 2023-12-24 13:27:51 +03:00
parent a5080fbb48
commit 9563cf2009
4 changed files with 20 additions and 23 deletions

View File

@ -31,8 +31,8 @@
/* Init scripts */ /* Init scripts */
#ifdef INIT #ifdef INIT
char *INIT_POWEROFF[] = {"/etc/rc.poweroff"}; char *INIT_POWEROFF[] = {"/etc/rc.poweroff", NULL};
char *INIT_START[] = {"/etc/rc.init"}; char *INIT_START[] = {"/etc/rc.init", NULL};
#define INIT_MSG "Starting micro-init..." #define INIT_MSG "Starting micro-init..."
#endif #endif

View File

@ -1,12 +1,12 @@
#!/bin/sh #!/bin/sh
echo "Closing processes..."
kill -a -s TERM
sleep 5
echo "Umounting..." echo "Umounting..."
umount /proc umount /proc
umount /sys umount /sys
umount /dev umount /dev
umount /tmp umount /tmp
echo "Closing processes..."
kill -a -s TERM
sleep 5
echo "Poweroff..." echo "Poweroff..."

View File

@ -11,31 +11,24 @@
void poweroff(int sig) { void poweroff(int sig) {
printf("Syncing...\n"); printf("Syncing...\n");
sync(); sync();
sleep(5);
if (sig == SIGUSR1) if (sig == SIGUSR1)
reboot(RB_POWER_OFF); reboot(RB_POWER_OFF);
else if (sig == SIGKILL) else if (sig == SIGUSR2)
reboot(RB_AUTOBOOT); reboot(RB_AUTOBOOT);
} }
void execute(char *argv[], sigset_t *set) { void execute(char *argv[]) {
pid_t pid; pid_t pid;
if ((pid = fork()) == 0) { if ((pid = fork()) == 0) {
sigprocmask(SIG_UNBLOCK, set, NULL);
setsid(); setsid();
execvp(argv[0], argv); execvp(argv[0], argv);
fprintf(stderr, "init: %s\n", strerror(errno)); fprintf(stderr, "init: Failed to run %s\nRebooting...\n", argv[0]);
exit(1);
}
int status = 0;
waitpid(pid, &status, 0);
if (status != 0) {
fprintf(stderr, "init: Init script returned %d\nRebooting...\n", status);
poweroff(SIGKILL); poweroff(SIGKILL);
} }
} }
@ -49,17 +42,21 @@ int main(void) {
chdir("/"); chdir("/");
printf("%s\n", INIT_MSG); printf("%s\n", INIT_MSG);
/* Start main service */
sigset_t set; sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR2);
sigaddset(&set, SIGUSR1);
/* Start main service */
sigprocmask(SIG_BLOCK, &set, NULL); sigprocmask(SIG_BLOCK, &set, NULL);
execute(INIT_START, &set); execute(INIT_START);
/* Sig handler */ /* Sig handler */
int sig = 0; int sig = 0;
while (1) { while (1) {
if (sigwait(&set, &sig) == 0) if (sigwait(&set, &sig) == 0)
if (sig == SIGKILL || sig == SIGUSR1) { if (sig == SIGUSR2 || sig == SIGUSR1) {
execute(INIT_POWEROFF, &set); execute(INIT_POWEROFF);
poweroff(sig); poweroff(sig);
} }
} }

View File

@ -1,2 +1,2 @@
#!/bin/sh #!/bin/sh
kill -s KILL 1 kill -s USR2 1