xref: /OK3568_Linux_fs/buildroot/package/busybox/0005-halt-Support-rebooting-with-arg.patch (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1From 1643107aa592271413603601161b47681287065e Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Tue, 18 Sep 2018 11:32:04 +0800
4Subject: [PATCH 5/8] halt: Support rebooting with arg
5
6Support passing reboot arg(e.g. loader, fastboot, etc.) to kernel.
7
8Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
9---
10 init/halt.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
11 1 file changed, 49 insertions(+)
12
13diff --git a/init/halt.c b/init/halt.c
14index fe3cb9e..2e8a0a5 100644
15--- a/init/halt.c
16+++ b/init/halt.c
17@@ -93,6 +93,8 @@
18
19 #include "libbb.h"
20 #include "reboot.h"
21+#include <linux/reboot.h>
22+#include <sys/syscall.h>
23
24 #if ENABLE_FEATURE_WTMP
25 #include <sys/utsname.h>
26@@ -161,6 +163,48 @@ static int init_was_not_there(void)
27 # define init_was_not_there() 0
28 #endif
29
30+static volatile int caught_sigterm = FALSE;
31+static void signal_handler(int sig)
32+{
33+	bb_error_msg("Caught signal %d", sig);
34+
35+	if (sig == SIGTERM)
36+		caught_sigterm = TRUE;
37+}
38+
39+static int reboot_with_arg(const char *arg)
40+{
41+	struct sigaction sa;
42+	int pid;
43+
44+	/* Fork new thread to handle reboot */
45+	if ((pid = fork()))
46+		return pid < 0 ? pid : 0;
47+
48+	/* Handle signal and reboot in child thread */
49+	sigemptyset(&sa.sa_mask);
50+	sa.sa_flags = 0;
51+	sa.sa_handler = signal_handler;
52+	sigaction_set(SIGTERM, &sa);
53+
54+	bb_error_msg("Waiting for SIGTERM");
55+
56+	/* The init will send SIGTERM to us after SHUTDOWN actions */
57+	while (!caught_sigterm)
58+		usleep(50000);
59+
60+	bb_error_msg("Ready to reboot");
61+
62+	/* Wait 200ms for other processes to exit */
63+	usleep(200000);
64+	sync();
65+
66+	bb_error_msg("Rebooting with arg(%s)", arg);
67+	return syscall(__NR_reboot, LINUX_REBOOT_MAGIC1,
68+			LINUX_REBOOT_MAGIC2,
69+			LINUX_REBOOT_CMD_RESTART2, arg);
70+}
71+
72 int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
73 int halt_main(int argc UNUSED_PARAM, char **argv)
74 {
75@@ -239,6 +283,11 @@ int halt_main(int argc UNUSED_PARAM, char **argv)
76 						CONFIG_TELINIT_PATH);
77 			}
78 		}
79+
80+		/* Handle rebooting with arg */
81+		if (signals[which] == SIGTERM && argc > 1 && argv[1][0] != '-')
82+			rc = reboot_with_arg(argv[1]);
83+
84 	} else {
85 		rc = reboot(magic[which]);
86 	}
87--
882.20.1
89
90