Skip to content

Commit 7a54f46

Browse files
shenkitorvalds
authored andcommitted
kernel/reboot.c: add orderly_reboot for graceful reboot
The kernel has orderly_poweroff which allows the kernel to initiate a graceful shutdown of userspace, by running /sbin/poweroff. This adds orderly_reboot that will cause userspace to shut itself down by calling /sbin/reboot. This will be used for shutdown initiated by a system controller on platforms that do not use ACPI. orderly_reboot() should be used when the system wants to allow userspace to gracefully shut itself down. For cases where the system may imminently catch on fire, the existing emergency_restart() provides an immediate reboot without involving userspace. Signed-off-by: Joel Stanley <joel@jms.id.au> Cc: Fabian Frederick <fabf@skynet.be> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Rusty Russell <rusty@rustcorp.com.au> Cc: Jeremy Kerr <jk@ozlabs.org> Cc: David S. Miller <davem@davemloft.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7975a9b commit 7a54f46

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎include/linux/reboot.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ void ctrl_alt_del(void);
7070
#define POWEROFF_CMD_PATH_LEN 256
7171
extern char poweroff_cmd[POWEROFF_CMD_PATH_LEN];
7272

73-
extern int orderly_poweroff(bool force);
73+
extern void orderly_poweroff(bool force);
74+
extern void orderly_reboot(void);
7475

7576
/*
7677
* Emergency restart, callable from an interrupt handler.

‎kernel/reboot.c‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,9 @@ void ctrl_alt_del(void)
387387
}
388388

389389
char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
390+
static const char reboot_cmd[] = "/sbin/reboot";
390391

391-
static int __orderly_poweroff(bool force)
392+
static int run_cmd(const char *cmd)
392393
{
393394
char **argv;
394395
static char *envp[] = {
@@ -397,17 +398,41 @@ static int __orderly_poweroff(bool force)
397398
NULL
398399
};
399400
int ret;
400-
401-
argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL);
401+
argv = argv_split(GFP_KERNEL, cmd, NULL);
402402
if (argv) {
403403
ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
404404
argv_free(argv);
405405
} else {
406406
ret = -ENOMEM;
407407
}
408408

409+
return ret;
410+
}
411+
412+
static int __orderly_reboot(void)
413+
{
414+
int ret;
415+
416+
ret = run_cmd(reboot_cmd);
417+
418+
if (ret) {
419+
pr_warn("Failed to start orderly reboot: forcing the issue\n");
420+
emergency_sync();
421+
kernel_restart(NULL);
422+
}
423+
424+
return ret;
425+
}
426+
427+
static int __orderly_poweroff(bool force)
428+
{
429+
int ret;
430+
431+
ret = run_cmd(poweroff_cmd);
432+
409433
if (ret && force) {
410434
pr_warn("Failed to start orderly shutdown: forcing the issue\n");
435+
411436
/*
412437
* I guess this should try to kick off some daemon to sync and
413438
* poweroff asap. Or not even bother syncing if we're doing an
@@ -436,15 +461,33 @@ static DECLARE_WORK(poweroff_work, poweroff_work_func);
436461
* This may be called from any context to trigger a system shutdown.
437462
* If the orderly shutdown fails, it will force an immediate shutdown.
438463
*/
439-
int orderly_poweroff(bool force)
464+
void orderly_poweroff(bool force)
440465
{
441466
if (force) /* do not override the pending "true" */
442467
poweroff_force = true;
443468
schedule_work(&poweroff_work);
444-
return 0;
445469
}
446470
EXPORT_SYMBOL_GPL(orderly_poweroff);
447471

472+
static void reboot_work_func(struct work_struct *work)
473+
{
474+
__orderly_reboot();
475+
}
476+
477+
static DECLARE_WORK(reboot_work, reboot_work_func);
478+
479+
/**
480+
* orderly_reboot - Trigger an orderly system reboot
481+
*
482+
* This may be called from any context to trigger a system reboot.
483+
* If the orderly reboot fails, it will force an immediate reboot.
484+
*/
485+
void orderly_reboot(void)
486+
{
487+
schedule_work(&reboot_work);
488+
}
489+
EXPORT_SYMBOL_GPL(orderly_reboot);
490+
448491
static int __init reboot_setup(char *str)
449492
{
450493
for (;;) {

0 commit comments

Comments
 (0)