source: mainline/kernel/generic/src/main/shutdown.c

Last change on this file was f35749e, checked in by Jiri Svoboda <jiri@…>, 4 months ago

System restart via shutdown -r

  • Property mode set to 100644
File size: 2.7 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2007 Martin Decky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup kernel_generic
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Shutdown procedures.
37 */
38
39#include <arch.h>
40#include <errno.h>
41#include <halt.h>
42#include <log.h>
43#include <main/main.h>
44#include <main/shutdown.h>
45#include <proc/task.h>
46#include <proc/thread.h>
47
48static thread_t *reboot_thrd = NULL;
49SPINLOCK_INITIALIZE(reboot_lock);
50
51void reboot(void)
52{
53 task_done(kernel_task);
54
55#ifdef CONFIG_DEBUG
56 log(LF_OTHER, LVL_DEBUG, "Rebooting the system");
57#endif
58
59 arch_reboot();
60 halt();
61}
62
63/** Thread procedure for rebooting the system.
64 *
65 * @param arg Argument (unused)
66 */
67static void reboot_thrd_proc(void *arg)
68{
69 (void)arg;
70
71 reboot();
72}
73
74/** Reboot the system.
75 *
76 * @return EOK if reboot started successfully. EBUSY if reboot already
77 * started, ENOMEM if out of memory.
78 */
79sys_errno_t sys_reboot(void)
80{
81 thread_t *thread;
82
83 thread = thread_create(reboot_thrd_proc, NULL, kernel_task,
84 THREAD_FLAG_NONE, "reboot");
85 if (thread == NULL)
86 return ENOMEM;
87
88 spinlock_lock(&reboot_lock);
89
90 if (reboot_thrd != NULL) {
91 spinlock_unlock(&reboot_lock);
92 thread_put(thread);
93 return EBUSY;
94 }
95
96 reboot_thrd = thread;
97
98 spinlock_unlock(&reboot_lock);
99
100 thread_start(thread);
101 thread_detach(thread);
102
103 return EOK;
104}
105
106/** @}
107 */
Note: See TracBrowser for help on using the repository browser.