~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/reboot.c

Version: ~ [ linux-5.5 ] ~ [ linux-5.4.15 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.98 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.167 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.211 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.211 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/kernel/reboot.c
  3  *
  4  *  Copyright (C) 2013  Linus Torvalds
  5  */
  6 
  7 #define pr_fmt(fmt)     "reboot: " fmt
  8 
  9 #include <linux/ctype.h>
 10 #include <linux/export.h>
 11 #include <linux/kexec.h>
 12 #include <linux/kmod.h>
 13 #include <linux/kmsg_dump.h>
 14 #include <linux/reboot.h>
 15 #include <linux/suspend.h>
 16 #include <linux/syscalls.h>
 17 #include <linux/syscore_ops.h>
 18 #include <linux/uaccess.h>
 19 #include <linux/ccsecurity.h>
 20 
 21 /*
 22  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
 23  */
 24 
 25 int C_A_D = 1;
 26 struct pid *cad_pid;
 27 EXPORT_SYMBOL(cad_pid);
 28 
 29 #if defined(CONFIG_ARM) || defined(CONFIG_UNICORE32)
 30 #define DEFAULT_REBOOT_MODE             = REBOOT_HARD
 31 #else
 32 #define DEFAULT_REBOOT_MODE
 33 #endif
 34 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
 35 
 36 /*
 37  * This variable is used privately to keep track of whether or not
 38  * reboot_type is still set to its default value (i.e., reboot= hasn't
 39  * been set on the command line).  This is needed so that we can
 40  * suppress DMI scanning for reboot quirks.  Without it, it's
 41  * impossible to override a faulty reboot quirk without recompiling.
 42  */
 43 int reboot_default = 1;
 44 int reboot_cpu;
 45 enum reboot_type reboot_type = BOOT_ACPI;
 46 int reboot_force;
 47 
 48 /*
 49  * If set, this is used for preparing the system to power off.
 50  */
 51 
 52 void (*pm_power_off_prepare)(void);
 53 
 54 /**
 55  *      emergency_restart - reboot the system
 56  *
 57  *      Without shutting down any hardware or taking any locks
 58  *      reboot the system.  This is called when we know we are in
 59  *      trouble so this is our best effort to reboot.  This is
 60  *      safe to call in interrupt context.
 61  */
 62 void emergency_restart(void)
 63 {
 64         kmsg_dump(KMSG_DUMP_EMERG);
 65         machine_emergency_restart();
 66 }
 67 EXPORT_SYMBOL_GPL(emergency_restart);
 68 
 69 void kernel_restart_prepare(char *cmd)
 70 {
 71         blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
 72         system_state = SYSTEM_RESTART;
 73         usermodehelper_disable();
 74         device_shutdown();
 75 }
 76 
 77 /**
 78  *      register_reboot_notifier - Register function to be called at reboot time
 79  *      @nb: Info about notifier function to be called
 80  *
 81  *      Registers a function with the list of functions
 82  *      to be called at reboot time.
 83  *
 84  *      Currently always returns zero, as blocking_notifier_chain_register()
 85  *      always returns zero.
 86  */
 87 int register_reboot_notifier(struct notifier_block *nb)
 88 {
 89         return blocking_notifier_chain_register(&reboot_notifier_list, nb);
 90 }
 91 EXPORT_SYMBOL(register_reboot_notifier);
 92 
 93 /**
 94  *      unregister_reboot_notifier - Unregister previously registered reboot notifier
 95  *      @nb: Hook to be unregistered
 96  *
 97  *      Unregisters a previously registered reboot
 98  *      notifier function.
 99  *
100  *      Returns zero on success, or %-ENOENT on failure.
101  */
102 int unregister_reboot_notifier(struct notifier_block *nb)
103 {
104         return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
105 }
106 EXPORT_SYMBOL(unregister_reboot_notifier);
107 
108 /*
109  *      Notifier list for kernel code which wants to be called
110  *      to restart the system.
111  */
112 static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
113 
114 /**
115  *      register_restart_handler - Register function to be called to reset
116  *                                 the system
117  *      @nb: Info about handler function to be called
118  *      @nb->priority:  Handler priority. Handlers should follow the
119  *                      following guidelines for setting priorities.
120  *                      0:      Restart handler of last resort,
121  *                              with limited restart capabilities
122  *                      128:    Default restart handler; use if no other
123  *                              restart handler is expected to be available,
124  *                              and/or if restart functionality is
125  *                              sufficient to restart the entire system
126  *                      255:    Highest priority restart handler, will
127  *                              preempt all other restart handlers
128  *
129  *      Registers a function with code to be called to restart the
130  *      system.
131  *
132  *      Registered functions will be called from machine_restart as last
133  *      step of the restart sequence (if the architecture specific
134  *      machine_restart function calls do_kernel_restart - see below
135  *      for details).
136  *      Registered functions are expected to restart the system immediately.
137  *      If more than one function is registered, the restart handler priority
138  *      selects which function will be called first.
139  *
140  *      Restart handlers are expected to be registered from non-architecture
141  *      code, typically from drivers. A typical use case would be a system
142  *      where restart functionality is provided through a watchdog. Multiple
143  *      restart handlers may exist; for example, one restart handler might
144  *      restart the entire system, while another only restarts the CPU.
145  *      In such cases, the restart handler which only restarts part of the
146  *      hardware is expected to register with low priority to ensure that
147  *      it only runs if no other means to restart the system is available.
148  *
149  *      Currently always returns zero, as atomic_notifier_chain_register()
150  *      always returns zero.
151  */
152 int register_restart_handler(struct notifier_block *nb)
153 {
154         return atomic_notifier_chain_register(&restart_handler_list, nb);
155 }
156 EXPORT_SYMBOL(register_restart_handler);
157 
158 /**
159  *      unregister_restart_handler - Unregister previously registered
160  *                                   restart handler
161  *      @nb: Hook to be unregistered
162  *
163  *      Unregisters a previously registered restart handler function.
164  *
165  *      Returns zero on success, or %-ENOENT on failure.
166  */
167 int unregister_restart_handler(struct notifier_block *nb)
168 {
169         return atomic_notifier_chain_unregister(&restart_handler_list, nb);
170 }
171 EXPORT_SYMBOL(unregister_restart_handler);
172 
173 /**
174  *      do_kernel_restart - Execute kernel restart handler call chain
175  *
176  *      Calls functions registered with register_restart_handler.
177  *
178  *      Expected to be called from machine_restart as last step of the restart
179  *      sequence.
180  *
181  *      Restarts the system immediately if a restart handler function has been
182  *      registered. Otherwise does nothing.
183  */
184 void do_kernel_restart(char *cmd)
185 {
186         atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
187 }
188 
189 void migrate_to_reboot_cpu(void)
190 {
191         /* The boot cpu is always logical cpu 0 */
192         int cpu = reboot_cpu;
193 
194         cpu_hotplug_disable();
195 
196         /* Make certain the cpu I'm about to reboot on is online */
197         if (!cpu_online(cpu))
198                 cpu = cpumask_first(cpu_online_mask);
199 
200         /* Prevent races with other tasks migrating this task */
201         current->flags |= PF_NO_SETAFFINITY;
202 
203         /* Make certain I only run on the appropriate processor */
204         set_cpus_allowed_ptr(current, cpumask_of(cpu));
205 }
206 
207 /**
208  *      kernel_restart - reboot the system
209  *      @cmd: pointer to buffer containing command to execute for restart
210  *              or %NULL
211  *
212  *      Shutdown everything and perform a clean reboot.
213  *      This is not safe to call in interrupt context.
214  */
215 void kernel_restart(char *cmd)
216 {
217         kernel_restart_prepare(cmd);
218         migrate_to_reboot_cpu();
219         syscore_shutdown();
220         if (!cmd)
221                 pr_emerg("Restarting system\n");
222         else
223                 pr_emerg("Restarting system with command '%s'\n", cmd);
224         kmsg_dump(KMSG_DUMP_RESTART);
225         machine_restart(cmd);
226 }
227 EXPORT_SYMBOL_GPL(kernel_restart);
228 
229 static void kernel_shutdown_prepare(enum system_states state)
230 {
231         blocking_notifier_call_chain(&reboot_notifier_list,
232                 (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
233         system_state = state;
234         usermodehelper_disable();
235         device_shutdown();
236 }
237 /**
238  *      kernel_halt - halt the system
239  *
240  *      Shutdown everything and perform a clean system halt.
241  */
242 void kernel_halt(void)
243 {
244         kernel_shutdown_prepare(SYSTEM_HALT);
245         migrate_to_reboot_cpu();
246         syscore_shutdown();
247         pr_emerg("System halted\n");
248         kmsg_dump(KMSG_DUMP_HALT);
249         machine_halt();
250 }
251 EXPORT_SYMBOL_GPL(kernel_halt);
252 
253 /**
254  *      kernel_power_off - power_off the system
255  *
256  *      Shutdown everything and perform a clean system power_off.
257  */
258 void kernel_power_off(void)
259 {
260         kernel_shutdown_prepare(SYSTEM_POWER_OFF);
261         if (pm_power_off_prepare)
262                 pm_power_off_prepare();
263         migrate_to_reboot_cpu();
264         syscore_shutdown();
265         pr_emerg("Power down\n");
266         kmsg_dump(KMSG_DUMP_POWEROFF);
267         machine_power_off();
268 }
269 EXPORT_SYMBOL_GPL(kernel_power_off);
270 
271 static DEFINE_MUTEX(reboot_mutex);
272 
273 /*
274  * Reboot system call: for obvious reasons only root may call it,
275  * and even root needs to set up some magic numbers in the registers
276  * so that some mistake won't make this reboot the whole machine.
277  * You can also set the meaning of the ctrl-alt-del-key here.
278  *
279  * reboot doesn't sync: do that yourself before calling this.
280  */
281 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
282                 void __user *, arg)
283 {
284         struct pid_namespace *pid_ns = task_active_pid_ns(current);
285         char buffer[256];
286         int ret = 0;
287 
288         /* We only trust the superuser with rebooting the system. */
289         if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
290                 return -EPERM;
291 
292         /* For safety, we require "magic" arguments. */
293         if (magic1 != LINUX_REBOOT_MAGIC1 ||
294                         (magic2 != LINUX_REBOOT_MAGIC2 &&
295                         magic2 != LINUX_REBOOT_MAGIC2A &&
296                         magic2 != LINUX_REBOOT_MAGIC2B &&
297                         magic2 != LINUX_REBOOT_MAGIC2C))
298                 return -EINVAL;
299         if (!ccs_capable(CCS_SYS_REBOOT))
300                 return -EPERM;
301 
302         /*
303          * If pid namespaces are enabled and the current task is in a child
304          * pid_namespace, the command is handled by reboot_pid_ns() which will
305          * call do_exit().
306          */
307         ret = reboot_pid_ns(pid_ns, cmd);
308         if (ret)
309                 return ret;
310 
311         /* Instead of trying to make the power_off code look like
312          * halt when pm_power_off is not set do it the easy way.
313          */
314         if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
315                 cmd = LINUX_REBOOT_CMD_HALT;
316 
317         mutex_lock(&reboot_mutex);
318         switch (cmd) {
319         case LINUX_REBOOT_CMD_RESTART:
320                 kernel_restart(NULL);
321                 break;
322 
323         case LINUX_REBOOT_CMD_CAD_ON:
324                 C_A_D = 1;
325                 break;
326 
327         case LINUX_REBOOT_CMD_CAD_OFF:
328                 C_A_D = 0;
329                 break;
330 
331         case LINUX_REBOOT_CMD_HALT:
332                 kernel_halt();
333                 do_exit(0);
334                 panic("cannot halt");
335 
336         case LINUX_REBOOT_CMD_POWER_OFF:
337                 kernel_power_off();
338                 do_exit(0);
339                 break;
340 
341         case LINUX_REBOOT_CMD_RESTART2:
342                 ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
343                 if (ret < 0) {
344                         ret = -EFAULT;
345                         break;
346                 }
347                 buffer[sizeof(buffer) - 1] = '\0';
348 
349                 kernel_restart(buffer);
350                 break;
351 
352 #ifdef CONFIG_KEXEC_CORE
353         case LINUX_REBOOT_CMD_KEXEC:
354                 ret = kernel_kexec();
355                 break;
356 #endif
357 
358 #ifdef CONFIG_HIBERNATION
359         case LINUX_REBOOT_CMD_SW_SUSPEND:
360                 ret = hibernate();
361                 break;
362 #endif
363 
364         default:
365                 ret = -EINVAL;
366                 break;
367         }
368         mutex_unlock(&reboot_mutex);
369         return ret;
370 }
371 
372 static void deferred_cad(struct work_struct *dummy)
373 {
374         kernel_restart(NULL);
375 }
376 
377 /*
378  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
379  * As it's called within an interrupt, it may NOT sync: the only choice
380  * is whether to reboot at once, or just ignore the ctrl-alt-del.
381  */
382 void ctrl_alt_del(void)
383 {
384         static DECLARE_WORK(cad_work, deferred_cad);
385 
386         if (C_A_D)
387                 schedule_work(&cad_work);
388         else
389                 kill_cad_pid(SIGINT, 1);
390 }
391 
392 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
393 static const char reboot_cmd[] = "/sbin/reboot";
394 
395 static int run_cmd(const char *cmd)
396 {
397         char **argv;
398         static char *envp[] = {
399                 "HOME=/",
400                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
401                 NULL
402         };
403         int ret;
404         argv = argv_split(GFP_KERNEL, cmd, NULL);
405         if (argv) {
406                 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
407                 argv_free(argv);
408         } else {
409                 ret = -ENOMEM;
410         }
411 
412         return ret;
413 }
414 
415 static int __orderly_reboot(void)
416 {
417         int ret;
418 
419         ret = run_cmd(reboot_cmd);
420 
421         if (ret) {
422                 pr_warn("Failed to start orderly reboot: forcing the issue\n");
423                 emergency_sync();
424                 kernel_restart(NULL);
425         }
426 
427         return ret;
428 }
429 
430 static int __orderly_poweroff(bool force)
431 {
432         int ret;
433 
434         ret = run_cmd(poweroff_cmd);
435 
436         if (ret && force) {
437                 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
438 
439                 /*
440                  * I guess this should try to kick off some daemon to sync and
441                  * poweroff asap.  Or not even bother syncing if we're doing an
442                  * emergency shutdown?
443                  */
444                 emergency_sync();
445                 kernel_power_off();
446         }
447 
448         return ret;
449 }
450 
451 static bool poweroff_force;
452 
453 static void poweroff_work_func(struct work_struct *work)
454 {
455         __orderly_poweroff(poweroff_force);
456 }
457 
458 static DECLARE_WORK(poweroff_work, poweroff_work_func);
459 
460 /**
461  * orderly_poweroff - Trigger an orderly system poweroff
462  * @force: force poweroff if command execution fails
463  *
464  * This may be called from any context to trigger a system shutdown.
465  * If the orderly shutdown fails, it will force an immediate shutdown.
466  */
467 void orderly_poweroff(bool force)
468 {
469         if (force) /* do not override the pending "true" */
470                 poweroff_force = true;
471         schedule_work(&poweroff_work);
472 }
473 EXPORT_SYMBOL_GPL(orderly_poweroff);
474 
475 static void reboot_work_func(struct work_struct *work)
476 {
477         __orderly_reboot();
478 }
479 
480 static DECLARE_WORK(reboot_work, reboot_work_func);
481 
482 /**
483  * orderly_reboot - Trigger an orderly system reboot
484  *
485  * This may be called from any context to trigger a system reboot.
486  * If the orderly reboot fails, it will force an immediate reboot.
487  */
488 void orderly_reboot(void)
489 {
490         schedule_work(&reboot_work);
491 }
492 EXPORT_SYMBOL_GPL(orderly_reboot);
493 
494 static int __init reboot_setup(char *str)
495 {
496         for (;;) {
497                 /*
498                  * Having anything passed on the command line via
499                  * reboot= will cause us to disable DMI checking
500                  * below.
501                  */
502                 reboot_default = 0;
503 
504                 switch (*str) {
505                 case 'w':
506                         reboot_mode = REBOOT_WARM;
507                         break;
508 
509                 case 'c':
510                         reboot_mode = REBOOT_COLD;
511                         break;
512 
513                 case 'h':
514                         reboot_mode = REBOOT_HARD;
515                         break;
516 
517                 case 's':
518                 {
519                         int rc;
520 
521                         if (isdigit(*(str+1))) {
522                                 rc = kstrtoint(str+1, 0, &reboot_cpu);
523                                 if (rc)
524                                         return rc;
525                         } else if (str[1] == 'm' && str[2] == 'p' &&
526                                    isdigit(*(str+3))) {
527                                 rc = kstrtoint(str+3, 0, &reboot_cpu);
528                                 if (rc)
529                                         return rc;
530                         } else
531                                 reboot_mode = REBOOT_SOFT;
532                         break;
533                 }
534                 case 'g':
535                         reboot_mode = REBOOT_GPIO;
536                         break;
537 
538                 case 'b':
539                 case 'a':
540                 case 'k':
541                 case 't':
542                 case 'e':
543                 case 'p':
544                         reboot_type = *str;
545                         break;
546 
547                 case 'f':
548                         reboot_force = 1;
549                         break;
550                 }
551 
552                 str = strchr(str, ',');
553                 if (str)
554                         str++;
555                 else
556                         break;
557         }
558         return 1;
559 }
560 __setup("reboot=", reboot_setup);
561 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp