source: mainline/uspace/app/shutdown/shutdown.c@ 832cbe7

Last change on this file since 832cbe7 was 0d00e53, checked in by Jiri Svoboda <jiri@…>, 10 months ago

Shut down dialog

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[ad9e225]1/*
2 * Copyright (c) 2024 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup lprint
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Shut the system down
36 *
37 */
38
39#include <fibril_synch.h>
40#include <nchoice.h>
41#include <stdio.h>
42#include <stdbool.h>
43#include <str.h>
44#include <system.h>
45#include "shutdown.h"
46
47#define NAME "shutdown"
48
49static void syntax_print(void);
50
51static sd_action_t action;
52
53static void sd_shutdown_complete(void *);
54static void sd_shutdown_failed(void *);
55
56static system_cb_t sd_system_cb = {
57 .shutdown_complete = sd_shutdown_complete,
58 .shutdown_failed = sd_shutdown_failed
59};
60
61/** System shutdown complete.
62 *
63 * @param arg Argument (shutdown_t *)
64 */
65static void sd_shutdown_complete(void *arg)
66{
67 shutdown_t *shutdown = (shutdown_t *)arg;
68
69 fibril_mutex_lock(&shutdown->lock);
70 shutdown->stopped = true;
71 shutdown->failed = false;
72 fibril_condvar_broadcast(&shutdown->cv);
73 fibril_mutex_unlock(&shutdown->lock);
74}
75
76/** System shutdown failed.
77 *
78 * @param arg Argument (not used)
79 */
80static void sd_shutdown_failed(void *arg)
81{
82 shutdown_t *shutdown = (shutdown_t *)arg;
83
84 fibril_mutex_lock(&shutdown->lock);
85 shutdown->stopped = true;
86 shutdown->failed = true;
87 fibril_condvar_broadcast(&shutdown->cv);
88 fibril_mutex_unlock(&shutdown->lock);
89}
90
91/** Interactively choose the shutdown action to perform
92 *
93 * @return EOK on success or an error code
94 */
95static errno_t choose_action(void)
96{
97 errno_t rc;
98 nchoice_t *nchoice = NULL;
99 void *choice;
100
101 rc = nchoice_create(&nchoice);
102 if (rc != EOK) {
103 printf(NAME ": Out of memory.\n");
104 goto error;
105 }
106
[0d00e53]107 rc = nchoice_set_prompt(nchoice, "Do you want to shut the system down? "
108 "Select action:");
[ad9e225]109 if (rc != EOK) {
110 printf(NAME ": Out of memory.\n");
111 goto error;
112 }
113
114 rc = nchoice_add(nchoice, "Power off", (void *)(uintptr_t)sd_poweroff,
115 0);
116 if (rc != EOK) {
117 printf(NAME ": Out of memory.\n");
118 goto error;
119 }
120
121 rc = nchoice_add(nchoice, "Cancel", (void *)(uintptr_t)sd_cancel,
122 ncf_default);
123 if (rc != EOK) {
124 printf(NAME ": Out of memory.\n");
125 goto error;
126 }
127
128 rc = nchoice_get(nchoice, &choice);
129 if (rc != EOK) {
130 if (rc != ENOENT)
131 printf(NAME ": Error getting user choice.\n");
132 goto error;
133 }
134
135 action = (sd_action_t)choice;
136 nchoice_destroy(nchoice);
137 return EOK;
138error:
139 if (nchoice != NULL)
140 nchoice_destroy(nchoice);
141 return rc;
142}
143
144int main(int argc, char **argv)
145{
146 errno_t rc;
147 system_t *system = NULL;
148 shutdown_t shutdown;
149
150 --argc;
151 ++argv;
152
153 while (*argv != NULL && *argv[0] == '-') {
154 if (str_cmp(*argv, "-p") == 0) {
155 --argc;
156 ++argv;
157 action = sd_poweroff;
158 continue;
159 }
160
161 printf(NAME ": Error, invalid option.\n");
162 syntax_print();
163 return 1;
164 }
165
166 if (argc >= 1) {
167 printf(NAME ": Error, unexpected argument.\n");
168 syntax_print();
169 return 1;
170 }
171
172 if (action == 0)
173 choose_action();
174
175 if (action == sd_cancel)
176 return 0;
177
178 fibril_mutex_initialize(&shutdown.lock);
179 fibril_condvar_initialize(&shutdown.cv);
180 shutdown.stopped = false;
181 shutdown.failed = false;
182
183 rc = system_open(SYSTEM_DEFAULT, &sd_system_cb, &shutdown, &system);
184 if (rc != EOK) {
185 printf(NAME ": Failed opening system control service.\n");
186 return 1;
187 }
188
189 rc = system_shutdown(system);
190 if (rc != EOK) {
191 system_close(system);
192 printf(NAME ": Failed requesting system shutdown.\n");
193 return 1;
194 }
195
196 fibril_mutex_lock(&shutdown.lock);
[0d00e53]197 printf("The system is shutting down...\n");
[ad9e225]198 while (!shutdown.stopped)
199 fibril_condvar_wait(&shutdown.cv, &shutdown.lock);
200
201 if (shutdown.failed) {
202 printf("Shutdown failed.\n");
203 system_close(system);
204 return 1;
205 }
206
207 printf("Shutdown complete. It is now safe to remove power.\n");
208
209 /* Sleep forever */
210 while (true)
211 fibril_condvar_wait(&shutdown.cv, &shutdown.lock);
212
213 fibril_mutex_unlock(&shutdown.lock);
214
215 system_close(system);
216 return 0;
217}
218
219/** Print syntax help. */
220static void syntax_print(void)
221{
222 printf("syntax:\n"
223 "\tshutdown [<options>]\n"
224 "options:\n"
225 "\t-p Power off\n");
226}
227
228/**
229 * @}
230 */
Note: See TracBrowser for help on using the repository browser.