Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/kernel/reboot.c
0003  *
0004  *  Copyright (C) 2013  Linus Torvalds
0005  */
0006 
0007 #define pr_fmt(fmt) "reboot: " fmt
0008 
0009 #include <linux/ctype.h>
0010 #include <linux/export.h>
0011 #include <linux/kexec.h>
0012 #include <linux/kmod.h>
0013 #include <linux/kmsg_dump.h>
0014 #include <linux/reboot.h>
0015 #include <linux/suspend.h>
0016 #include <linux/syscalls.h>
0017 #include <linux/syscore_ops.h>
0018 #include <linux/uaccess.h>
0019 
0020 /*
0021  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
0022  */
0023 
0024 int C_A_D = 1;
0025 struct pid *cad_pid;
0026 EXPORT_SYMBOL(cad_pid);
0027 
0028 #if defined(CONFIG_ARM) || defined(CONFIG_UNICORE32)
0029 #define DEFAULT_REBOOT_MODE     = REBOOT_HARD
0030 #else
0031 #define DEFAULT_REBOOT_MODE
0032 #endif
0033 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
0034 
0035 /*
0036  * This variable is used privately to keep track of whether or not
0037  * reboot_type is still set to its default value (i.e., reboot= hasn't
0038  * been set on the command line).  This is needed so that we can
0039  * suppress DMI scanning for reboot quirks.  Without it, it's
0040  * impossible to override a faulty reboot quirk without recompiling.
0041  */
0042 int reboot_default = 1;
0043 int reboot_cpu;
0044 enum reboot_type reboot_type = BOOT_ACPI;
0045 int reboot_force;
0046 
0047 /*
0048  * If set, this is used for preparing the system to power off.
0049  */
0050 
0051 void (*pm_power_off_prepare)(void);
0052 
0053 /**
0054  *  emergency_restart - reboot the system
0055  *
0056  *  Without shutting down any hardware or taking any locks
0057  *  reboot the system.  This is called when we know we are in
0058  *  trouble so this is our best effort to reboot.  This is
0059  *  safe to call in interrupt context.
0060  */
0061 void emergency_restart(void)
0062 {
0063     kmsg_dump(KMSG_DUMP_EMERG);
0064     machine_emergency_restart();
0065 }
0066 EXPORT_SYMBOL_GPL(emergency_restart);
0067 
0068 void kernel_restart_prepare(char *cmd)
0069 {
0070     blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
0071     system_state = SYSTEM_RESTART;
0072     usermodehelper_disable();
0073     device_shutdown();
0074 }
0075 
0076 /**
0077  *  register_reboot_notifier - Register function to be called at reboot time
0078  *  @nb: Info about notifier function to be called
0079  *
0080  *  Registers a function with the list of functions
0081  *  to be called at reboot time.
0082  *
0083  *  Currently always returns zero, as blocking_notifier_chain_register()
0084  *  always returns zero.
0085  */
0086 int register_reboot_notifier(struct notifier_block *nb)
0087 {
0088     return blocking_notifier_chain_register(&reboot_notifier_list, nb);
0089 }
0090 EXPORT_SYMBOL(register_reboot_notifier);
0091 
0092 /**
0093  *  unregister_reboot_notifier - Unregister previously registered reboot notifier
0094  *  @nb: Hook to be unregistered
0095  *
0096  *  Unregisters a previously registered reboot
0097  *  notifier function.
0098  *
0099  *  Returns zero on success, or %-ENOENT on failure.
0100  */
0101 int unregister_reboot_notifier(struct notifier_block *nb)
0102 {
0103     return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
0104 }
0105 EXPORT_SYMBOL(unregister_reboot_notifier);
0106 
0107 /*
0108  *  Notifier list for kernel code which wants to be called
0109  *  to restart the system.
0110  */
0111 static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
0112 
0113 /**
0114  *  register_restart_handler - Register function to be called to reset
0115  *                 the system
0116  *  @nb: Info about handler function to be called
0117  *  @nb->priority:  Handler priority. Handlers should follow the
0118  *          following guidelines for setting priorities.
0119  *          0:  Restart handler of last resort,
0120  *              with limited restart capabilities
0121  *          128:    Default restart handler; use if no other
0122  *              restart handler is expected to be available,
0123  *              and/or if restart functionality is
0124  *              sufficient to restart the entire system
0125  *          255:    Highest priority restart handler, will
0126  *              preempt all other restart handlers
0127  *
0128  *  Registers a function with code to be called to restart the
0129  *  system.
0130  *
0131  *  Registered functions will be called from machine_restart as last
0132  *  step of the restart sequence (if the architecture specific
0133  *  machine_restart function calls do_kernel_restart - see below
0134  *  for details).
0135  *  Registered functions are expected to restart the system immediately.
0136  *  If more than one function is registered, the restart handler priority
0137  *  selects which function will be called first.
0138  *
0139  *  Restart handlers are expected to be registered from non-architecture
0140  *  code, typically from drivers. A typical use case would be a system
0141  *  where restart functionality is provided through a watchdog. Multiple
0142  *  restart handlers may exist; for example, one restart handler might
0143  *  restart the entire system, while another only restarts the CPU.
0144  *  In such cases, the restart handler which only restarts part of the
0145  *  hardware is expected to register with low priority to ensure that
0146  *  it only runs if no other means to restart the system is available.
0147  *
0148  *  Currently always returns zero, as atomic_notifier_chain_register()
0149  *  always returns zero.
0150  */
0151 int register_restart_handler(struct notifier_block *nb)
0152 {
0153     return atomic_notifier_chain_register(&restart_handler_list, nb);
0154 }
0155 EXPORT_SYMBOL(register_restart_handler);
0156 
0157 /**
0158  *  unregister_restart_handler - Unregister previously registered
0159  *                   restart handler
0160  *  @nb: Hook to be unregistered
0161  *
0162  *  Unregisters a previously registered restart handler function.
0163  *
0164  *  Returns zero on success, or %-ENOENT on failure.
0165  */
0166 int unregister_restart_handler(struct notifier_block *nb)
0167 {
0168     return atomic_notifier_chain_unregister(&restart_handler_list, nb);
0169 }
0170 EXPORT_SYMBOL(unregister_restart_handler);
0171 
0172 /**
0173  *  do_kernel_restart - Execute kernel restart handler call chain
0174  *
0175  *  Calls functions registered with register_restart_handler.
0176  *
0177  *  Expected to be called from machine_restart as last step of the restart
0178  *  sequence.
0179  *
0180  *  Restarts the system immediately if a restart handler function has been
0181  *  registered. Otherwise does nothing.
0182  */
0183 void do_kernel_restart(char *cmd)
0184 {
0185     atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
0186 }
0187 
0188 void migrate_to_reboot_cpu(void)
0189 {
0190     /* The boot cpu is always logical cpu 0 */
0191     int cpu = reboot_cpu;
0192 
0193     cpu_hotplug_disable();
0194 
0195     /* Make certain the cpu I'm about to reboot on is online */
0196     if (!cpu_online(cpu))
0197         cpu = cpumask_first(cpu_online_mask);
0198 
0199     /* Prevent races with other tasks migrating this task */
0200     current->flags |= PF_NO_SETAFFINITY;
0201 
0202     /* Make certain I only run on the appropriate processor */
0203     set_cpus_allowed_ptr(current, cpumask_of(cpu));
0204 }
0205 
0206 /**
0207  *  kernel_restart - reboot the system
0208  *  @cmd: pointer to buffer containing command to execute for restart
0209  *      or %NULL
0210  *
0211  *  Shutdown everything and perform a clean reboot.
0212  *  This is not safe to call in interrupt context.
0213  */
0214 void kernel_restart(char *cmd)
0215 {
0216     kernel_restart_prepare(cmd);
0217     migrate_to_reboot_cpu();
0218     syscore_shutdown();
0219     if (!cmd)
0220         pr_emerg("Restarting system\n");
0221     else
0222         pr_emerg("Restarting system with command '%s'\n", cmd);
0223     kmsg_dump(KMSG_DUMP_RESTART);
0224     machine_restart(cmd);
0225 }
0226 EXPORT_SYMBOL_GPL(kernel_restart);
0227 
0228 static void kernel_shutdown_prepare(enum system_states state)
0229 {
0230     blocking_notifier_call_chain(&reboot_notifier_list,
0231         (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
0232     system_state = state;
0233     usermodehelper_disable();
0234     device_shutdown();
0235 }
0236 /**
0237  *  kernel_halt - halt the system
0238  *
0239  *  Shutdown everything and perform a clean system halt.
0240  */
0241 void kernel_halt(void)
0242 {
0243     kernel_shutdown_prepare(SYSTEM_HALT);
0244     migrate_to_reboot_cpu();
0245     syscore_shutdown();
0246     pr_emerg("System halted\n");
0247     kmsg_dump(KMSG_DUMP_HALT);
0248     machine_halt();
0249 }
0250 EXPORT_SYMBOL_GPL(kernel_halt);
0251 
0252 /**
0253  *  kernel_power_off - power_off the system
0254  *
0255  *  Shutdown everything and perform a clean system power_off.
0256  */
0257 void kernel_power_off(void)
0258 {
0259     kernel_shutdown_prepare(SYSTEM_POWER_OFF);
0260     if (pm_power_off_prepare)
0261         pm_power_off_prepare();
0262     migrate_to_reboot_cpu();
0263     syscore_shutdown();
0264     pr_emerg("Power down\n");
0265     kmsg_dump(KMSG_DUMP_POWEROFF);
0266     machine_power_off();
0267 }
0268 EXPORT_SYMBOL_GPL(kernel_power_off);
0269 
0270 static DEFINE_MUTEX(reboot_mutex);
0271 
0272 /*
0273  * Reboot system call: for obvious reasons only root may call it,
0274  * and even root needs to set up some magic numbers in the registers
0275  * so that some mistake won't make this reboot the whole machine.
0276  * You can also set the meaning of the ctrl-alt-del-key here.
0277  *
0278  * reboot doesn't sync: do that yourself before calling this.
0279  */
0280 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
0281         void __user *, arg)
0282 {
0283     struct pid_namespace *pid_ns = task_active_pid_ns(current);
0284     char buffer[256];
0285     int ret = 0;
0286 
0287     /* We only trust the superuser with rebooting the system. */
0288     if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
0289         return -EPERM;
0290 
0291     /* For safety, we require "magic" arguments. */
0292     if (magic1 != LINUX_REBOOT_MAGIC1 ||
0293             (magic2 != LINUX_REBOOT_MAGIC2 &&
0294             magic2 != LINUX_REBOOT_MAGIC2A &&
0295             magic2 != LINUX_REBOOT_MAGIC2B &&
0296             magic2 != LINUX_REBOOT_MAGIC2C))
0297         return -EINVAL;
0298 
0299     /*
0300      * If pid namespaces are enabled and the current task is in a child
0301      * pid_namespace, the command is handled by reboot_pid_ns() which will
0302      * call do_exit().
0303      */
0304     ret = reboot_pid_ns(pid_ns, cmd);
0305     if (ret)
0306         return ret;
0307 
0308     /* Instead of trying to make the power_off code look like
0309      * halt when pm_power_off is not set do it the easy way.
0310      */
0311     if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
0312         cmd = LINUX_REBOOT_CMD_HALT;
0313 
0314     mutex_lock(&reboot_mutex);
0315     switch (cmd) {
0316     case LINUX_REBOOT_CMD_RESTART:
0317         kernel_restart(NULL);
0318         break;
0319 
0320     case LINUX_REBOOT_CMD_CAD_ON:
0321         C_A_D = 1;
0322         break;
0323 
0324     case LINUX_REBOOT_CMD_CAD_OFF:
0325         C_A_D = 0;
0326         break;
0327 
0328     case LINUX_REBOOT_CMD_HALT:
0329         kernel_halt();
0330         do_exit(0);
0331         panic("cannot halt");
0332 
0333     case LINUX_REBOOT_CMD_POWER_OFF:
0334         kernel_power_off();
0335         do_exit(0);
0336         break;
0337 
0338     case LINUX_REBOOT_CMD_RESTART2:
0339         ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
0340         if (ret < 0) {
0341             ret = -EFAULT;
0342             break;
0343         }
0344         buffer[sizeof(buffer) - 1] = '\0';
0345 
0346         kernel_restart(buffer);
0347         break;
0348 
0349 #ifdef CONFIG_KEXEC_CORE
0350     case LINUX_REBOOT_CMD_KEXEC:
0351         ret = kernel_kexec();
0352         break;
0353 #endif
0354 
0355 #ifdef CONFIG_HIBERNATION
0356     case LINUX_REBOOT_CMD_SW_SUSPEND:
0357         ret = hibernate();
0358         break;
0359 #endif
0360 
0361     default:
0362         ret = -EINVAL;
0363         break;
0364     }
0365     mutex_unlock(&reboot_mutex);
0366     return ret;
0367 }
0368 
0369 static void deferred_cad(struct work_struct *dummy)
0370 {
0371     kernel_restart(NULL);
0372 }
0373 
0374 /*
0375  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
0376  * As it's called within an interrupt, it may NOT sync: the only choice
0377  * is whether to reboot at once, or just ignore the ctrl-alt-del.
0378  */
0379 void ctrl_alt_del(void)
0380 {
0381     static DECLARE_WORK(cad_work, deferred_cad);
0382 
0383     if (C_A_D)
0384         schedule_work(&cad_work);
0385     else
0386         kill_cad_pid(SIGINT, 1);
0387 }
0388 
0389 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
0390 static const char reboot_cmd[] = "/sbin/reboot";
0391 
0392 static int run_cmd(const char *cmd)
0393 {
0394     char **argv;
0395     static char *envp[] = {
0396         "HOME=/",
0397         "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
0398         NULL
0399     };
0400     int ret;
0401     argv = argv_split(GFP_KERNEL, cmd, NULL);
0402     if (argv) {
0403         ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
0404         argv_free(argv);
0405     } else {
0406         ret = -ENOMEM;
0407     }
0408 
0409     return ret;
0410 }
0411 
0412 static int __orderly_reboot(void)
0413 {
0414     int ret;
0415 
0416     ret = run_cmd(reboot_cmd);
0417 
0418     if (ret) {
0419         pr_warn("Failed to start orderly reboot: forcing the issue\n");
0420         emergency_sync();
0421         kernel_restart(NULL);
0422     }
0423 
0424     return ret;
0425 }
0426 
0427 static int __orderly_poweroff(bool force)
0428 {
0429     int ret;
0430 
0431     ret = run_cmd(poweroff_cmd);
0432 
0433     if (ret && force) {
0434         pr_warn("Failed to start orderly shutdown: forcing the issue\n");
0435 
0436         /*
0437          * I guess this should try to kick off some daemon to sync and
0438          * poweroff asap.  Or not even bother syncing if we're doing an
0439          * emergency shutdown?
0440          */
0441         emergency_sync();
0442         kernel_power_off();
0443     }
0444 
0445     return ret;
0446 }
0447 
0448 static bool poweroff_force;
0449 
0450 static void poweroff_work_func(struct work_struct *work)
0451 {
0452     __orderly_poweroff(poweroff_force);
0453 }
0454 
0455 static DECLARE_WORK(poweroff_work, poweroff_work_func);
0456 
0457 /**
0458  * orderly_poweroff - Trigger an orderly system poweroff
0459  * @force: force poweroff if command execution fails
0460  *
0461  * This may be called from any context to trigger a system shutdown.
0462  * If the orderly shutdown fails, it will force an immediate shutdown.
0463  */
0464 void orderly_poweroff(bool force)
0465 {
0466     if (force) /* do not override the pending "true" */
0467         poweroff_force = true;
0468     schedule_work(&poweroff_work);
0469 }
0470 EXPORT_SYMBOL_GPL(orderly_poweroff);
0471 
0472 static void reboot_work_func(struct work_struct *work)
0473 {
0474     __orderly_reboot();
0475 }
0476 
0477 static DECLARE_WORK(reboot_work, reboot_work_func);
0478 
0479 /**
0480  * orderly_reboot - Trigger an orderly system reboot
0481  *
0482  * This may be called from any context to trigger a system reboot.
0483  * If the orderly reboot fails, it will force an immediate reboot.
0484  */
0485 void orderly_reboot(void)
0486 {
0487     schedule_work(&reboot_work);
0488 }
0489 EXPORT_SYMBOL_GPL(orderly_reboot);
0490 
0491 static int __init reboot_setup(char *str)
0492 {
0493     for (;;) {
0494         /*
0495          * Having anything passed on the command line via
0496          * reboot= will cause us to disable DMI checking
0497          * below.
0498          */
0499         reboot_default = 0;
0500 
0501         switch (*str) {
0502         case 'w':
0503             reboot_mode = REBOOT_WARM;
0504             break;
0505 
0506         case 'c':
0507             reboot_mode = REBOOT_COLD;
0508             break;
0509 
0510         case 'h':
0511             reboot_mode = REBOOT_HARD;
0512             break;
0513 
0514         case 's':
0515         {
0516             int rc;
0517 
0518             if (isdigit(*(str+1))) {
0519                 rc = kstrtoint(str+1, 0, &reboot_cpu);
0520                 if (rc)
0521                     return rc;
0522             } else if (str[1] == 'm' && str[2] == 'p' &&
0523                    isdigit(*(str+3))) {
0524                 rc = kstrtoint(str+3, 0, &reboot_cpu);
0525                 if (rc)
0526                     return rc;
0527             } else
0528                 reboot_mode = REBOOT_SOFT;
0529             break;
0530         }
0531         case 'g':
0532             reboot_mode = REBOOT_GPIO;
0533             break;
0534 
0535         case 'b':
0536         case 'a':
0537         case 'k':
0538         case 't':
0539         case 'e':
0540         case 'p':
0541             reboot_type = *str;
0542             break;
0543 
0544         case 'f':
0545             reboot_force = 1;
0546             break;
0547         }
0548 
0549         str = strchr(str, ',');
0550         if (str)
0551             str++;
0552         else
0553             break;
0554     }
0555     return 1;
0556 }
0557 __setup("reboot=", reboot_setup);