-
Notifications
You must be signed in to change notification settings - Fork 0
/
reboot.c
101 lines (77 loc) · 1.96 KB
/
reboot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#define _POSIX_C_SOURCE 200819L
#define _XOPEN_SOURCE 700
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>
#include "os/pw.h"
#include "os/chvt.h"
#include "os/proc.h"
#include "os/mnt.h"
#include "fileio.h"
#include "tools.h"
#include "respawn.h"
#include "config.h"
#ifndef CONFIG_TERM_WAITTIME
#define CONFIG_TERM_WAITTIME 2
#endif /* CONFIG_TERM_WAITTIME */
static void killproc(int signo)
{
pid_t pid;
if (ueld_os_for_each_process() < 0)
return;
while (ueld_os_next_process(&pid)) {
if (pid != 1)
kill(pid, signo);
}
ueld_os_end_each_process();
}
#ifndef CONFIG_CONSOLE_VT
#define CONFIG_CONSOLE_VT 1
#endif /* CONFIG_CONSOLE_VT */
int ueld_reboot(int cmd)
{
int status, vt;
pid_t pid;
if (getpid() != 1) {
ueld_echo("Must run as pid 1");
return -1;
}
vt = (int)ueld_readconfiglong("ueld_console_vt", CONFIG_CONSOLE_VT);
ueld_os_chvt(vt);
clearpid(0);
ueld_closeconfig();
ueld_echo("Sending SIGTERM to all process...");
killproc(SIGTERM);
ueld_os_chvt(vt);
sleep(CONFIG_TERM_WAITTIME);
ueld_echo("Sending SIGKILL to all process...");
killproc(SIGKILL);
killproc(SIGKILL);
ueld_os_chvt(vt);
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {}
ueld_echo("Running syshalt.sh...");
ueld_run("/etc/ueld/syshalt.sh", URF_WAIT|URF_CONSOLE, 0, NULL);
ueld_echo("Syncing disk...");
sync();
ueld_echo("Unmounting/Re-mounting file systems...");
if ((ueld_os_umount_all() != 0) && (ueld_readconfiglong("ueld_must_remount_before_poweroff", -1) == 1)) {
ueld_echo("Remount some filesystems failed, droping to a shell...");
char* sh = ueld_readconfig("system_shell");
if (!sh)
sh = "/bin/sh";
ueld_run(sh, URF_CONSOLE, 0, NULL);
return -1;
}
ueld_echo("Doing poweroff...");
if (ueld_os_reboot(cmd) < 0)
ueld_print("reboot: %s", strerror(errno));
exit(1);
}