source: mainline/uspace/srv/system/system.c@ 77a0119

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

Quiesce devices during restart, but not poweroff.

  • Property mode set to 100644
File size: 15.7 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2005 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 init
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <devman.h>
38#include <fibril.h>
39#include <futil.h>
40#include <io/log.h>
41#include <stdio.h>
42#include <stdarg.h>
43#include <vfs/vfs.h>
44#include <stdbool.h>
45#include <errno.h>
46#include <task.h>
47#include <stdlib.h>
48#include <macros.h>
49#include <str.h>
50#include <loc.h>
51#include <shutdown.h>
52#include <str_error.h>
53#include <config.h>
54#include <io/logctl.h>
55#include <vfs/vfs.h>
56#include <vol.h>
57#include <system.h>
58#include <system_srv.h>
59#include "system.h"
60
61#define BANNER_LEFT "######> "
62#define BANNER_RIGHT " <######"
63
64#define LOCFS_FS_TYPE "locfs"
65#define LOCFS_MOUNT_POINT "/loc"
66
67#define TMPFS_FS_TYPE "tmpfs"
68#define TMPFS_MOUNT_POINT "/tmp"
69
70#define SRV_CONSOLE "/srv/hid/console"
71#define APP_GETTERM "/app/getterm"
72
73#define SRV_DISPLAY "/srv/hid/display"
74
75#define HID_INPUT "hid/input"
76#define HID_OUTPUT "hid/output"
77
78#define srv_start(path, ...) \
79 srv_startl(path, path, ##__VA_ARGS__, NULL)
80
81static const char *sys_dirs[] = {
82 "/w/cfg",
83 "/w/data",
84 NULL,
85};
86
87static void system_srv_conn(ipc_call_t *, void *);
88static errno_t system_srv_poweroff(void *);
89static errno_t system_srv_restart(void *);
90
91system_ops_t system_srv_ops = {
92 .poweroff = system_srv_poweroff,
93 .restart = system_srv_restart
94};
95
96/** Print banner */
97static void info_print(void)
98{
99 printf("%s: HelenOS system server\n", NAME);
100}
101
102static void oom_check(errno_t rc, const char *path)
103{
104 if (rc == ENOMEM) {
105 printf("%sOut-of-memory condition detected%s\n", BANNER_LEFT,
106 BANNER_RIGHT);
107 printf("%sBailing out of the boot process after %s%s\n",
108 BANNER_LEFT, path, BANNER_RIGHT);
109 printf("%sMore physical memory is required%s\n", BANNER_LEFT,
110 BANNER_RIGHT);
111 exit(ENOMEM);
112 }
113}
114
115/** Report mount operation success */
116static bool mount_report(const char *desc, const char *mntpt,
117 const char *fstype, const char *dev, errno_t rc)
118{
119 switch (rc) {
120 case EOK:
121 if ((dev != NULL) && (str_cmp(dev, "") != 0))
122 printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
123 fstype, dev);
124 else
125 printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
126 break;
127 case EBUSY:
128 printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
129 return false;
130 case ELIMIT:
131 printf("%s: %s limit exceeded\n", NAME, desc);
132 return false;
133 case ENOENT:
134 printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
135 return false;
136 default:
137 printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
138 str_error(rc));
139 return false;
140 }
141
142 return true;
143}
144
145/** Mount locfs file system
146 *
147 * The operation blocks until the locfs file system
148 * server is ready for mounting.
149 *
150 * @return True on success.
151 * @return False on failure.
152 *
153 */
154static bool mount_locfs(void)
155{
156 errno_t rc = vfs_mount_path(LOCFS_MOUNT_POINT, LOCFS_FS_TYPE, "", "",
157 IPC_FLAG_BLOCKING, 0);
158 return mount_report("Location service file system", LOCFS_MOUNT_POINT,
159 LOCFS_FS_TYPE, NULL, rc);
160}
161
162static errno_t srv_startl(const char *path, ...)
163{
164 vfs_stat_t s;
165 if (vfs_stat_path(path, &s) != EOK) {
166 printf("%s: Unable to stat %s\n", NAME, path);
167 return ENOENT;
168 }
169
170 printf("%s: Starting %s\n", NAME, path);
171
172 va_list ap;
173 const char *arg;
174 int cnt = 0;
175
176 va_start(ap, path);
177 do {
178 arg = va_arg(ap, const char *);
179 cnt++;
180 } while (arg != NULL);
181 va_end(ap);
182
183 va_start(ap, path);
184 task_id_t id;
185 task_wait_t wait;
186 errno_t rc = task_spawn(&id, &wait, path, cnt, ap);
187 va_end(ap);
188
189 if (rc != EOK) {
190 oom_check(rc, path);
191 printf("%s: Error spawning %s (%s)\n", NAME, path,
192 str_error(rc));
193 return rc;
194 }
195
196 if (!id) {
197 printf("%s: Error spawning %s (invalid task id)\n", NAME,
198 path);
199 return EINVAL;
200 }
201
202 task_exit_t texit;
203 int retval;
204 rc = task_wait(&wait, &texit, &retval);
205 if (rc != EOK) {
206 printf("%s: Error waiting for %s (%s)\n", NAME, path,
207 str_error(rc));
208 return rc;
209 }
210
211 if (texit != TASK_EXIT_NORMAL) {
212 printf("%s: Server %s failed to start (unexpectedly "
213 "terminated)\n", NAME, path);
214 return EINVAL;
215 }
216
217 if (retval != 0)
218 printf("%s: Server %s failed to start (exit code %d)\n", NAME,
219 path, retval);
220
221 return retval == 0 ? EOK : EPARTY;
222}
223
224static errno_t console(const char *isvc, const char *osvc)
225{
226 /* Wait for the input service to be ready */
227 service_id_t service_id;
228 errno_t rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
229 if (rc != EOK) {
230 printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
231 str_error(rc));
232 return rc;
233 }
234
235 /* Wait for the output service to be ready */
236 rc = loc_service_get_id(osvc, &service_id, IPC_FLAG_BLOCKING);
237 if (rc != EOK) {
238 printf("%s: Error waiting on %s (%s)\n", NAME, osvc,
239 str_error(rc));
240 return rc;
241 }
242
243 return srv_start(SRV_CONSOLE, isvc, osvc);
244}
245
246#ifdef CONFIG_WINSYS
247
248static errno_t display_server(void)
249{
250 return srv_start(SRV_DISPLAY);
251}
252
253static int app_start(const char *app, const char *arg)
254{
255 printf("%s: Spawning %s\n", NAME, app);
256
257 task_id_t id;
258 task_wait_t wait;
259 errno_t rc = task_spawnl(&id, &wait, app, app, arg, NULL);
260 if (rc != EOK) {
261 oom_check(rc, app);
262 printf("%s: Error spawning %s (%s)\n", NAME, app,
263 str_error(rc));
264 return -1;
265 }
266
267 task_exit_t texit;
268 int retval;
269 rc = task_wait(&wait, &texit, &retval);
270 if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
271 printf("%s: Error retrieving retval from %s (%s)\n", NAME,
272 app, str_error(rc));
273 return rc;
274 }
275
276 return retval;
277}
278
279#endif
280
281static void getterm(const char *svc, const char *app, bool msg)
282{
283 if (msg) {
284 printf("%s: Spawning %s %s %s --msg --wait -- %s\n", NAME,
285 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
286
287 errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
288 LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
289 if (rc != EOK) {
290 oom_check(rc, APP_GETTERM);
291 printf("%s: Error spawning %s %s %s --msg --wait -- %s\n",
292 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
293 }
294 } else {
295 printf("%s: Spawning %s %s %s --wait -- %s\n", NAME,
296 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
297
298 errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
299 LOCFS_MOUNT_POINT, "--wait", "--", app, NULL);
300 if (rc != EOK) {
301 oom_check(rc, APP_GETTERM);
302 printf("%s: Error spawning %s %s %s --wait -- %s\n",
303 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
304 }
305 }
306}
307
308static bool mount_tmpfs(void)
309{
310 errno_t rc = vfs_mount_path(TMPFS_MOUNT_POINT, TMPFS_FS_TYPE, "", "", 0, 0);
311 return mount_report("Temporary file system", TMPFS_MOUNT_POINT,
312 TMPFS_FS_TYPE, NULL, rc);
313}
314
315/** Init system volume.
316 *
317 * See if system volume is configured. If so, try to wait for it to become
318 * available. If not, create basic directories for live image omde.
319 */
320static errno_t init_sysvol(void)
321{
322 vol_t *vol = NULL;
323 vol_info_t vinfo;
324 volume_id_t *volume_ids = NULL;
325 service_id_t *part_ids = NULL;
326 vol_part_info_t pinfo;
327 size_t nvols;
328 size_t nparts;
329 bool sv_mounted;
330 size_t i;
331 errno_t rc;
332 bool found_cfg;
333 const char **cp;
334
335 rc = vol_create(&vol);
336 if (rc != EOK) {
337 printf("Error contacting volume service.\n");
338 goto error;
339 }
340
341 rc = vol_get_volumes(vol, &volume_ids, &nvols);
342 if (rc != EOK) {
343 printf("Error getting list of volumes.\n");
344 goto error;
345 }
346
347 /* XXX This could be handled more efficiently by volsrv itself */
348 found_cfg = false;
349 for (i = 0; i < nvols; i++) {
350 rc = vol_info(vol, volume_ids[i], &vinfo);
351 if (rc != EOK) {
352 printf("Error getting volume information.\n");
353 rc = EIO;
354 goto error;
355 }
356
357 if (str_cmp(vinfo.path, "/w") == 0) {
358 found_cfg = true;
359 break;
360 }
361 }
362
363 free(volume_ids);
364 volume_ids = NULL;
365
366 if (!found_cfg) {
367 /* Prepare directory structure for live image mode */
368 printf("%s: Creating live image directory structure.\n", NAME);
369 cp = sys_dirs;
370 while (*cp != NULL) {
371 rc = vfs_link_path(*cp, KIND_DIRECTORY, NULL);
372 if (rc != EOK) {
373 printf("%s: Error creating directory '%s'.\n",
374 NAME, *cp);
375 goto error;
376 }
377
378 ++cp;
379 }
380
381 /* Copy initial configuration files */
382 rc = futil_rcopy_contents("/cfg", "/w/cfg");
383 if (rc != EOK)
384 goto error;
385 } else {
386 printf("%s: System volume is configured.\n", NAME);
387
388 /* Verify that system volume is mounted */
389 sv_mounted = false;
390
391 rc = vol_get_parts(vol, &part_ids, &nparts);
392 if (rc != EOK) {
393 printf("Error getting list of volumes.\n");
394 goto error;
395 }
396
397 for (i = 0; i < nparts; i++) {
398 rc = vol_part_info(vol, part_ids[i], &pinfo);
399 if (rc != EOK) {
400 printf("Error getting partition "
401 "information.\n");
402 rc = EIO;
403 goto error;
404 }
405
406 if (str_cmp(pinfo.cur_mp, "/w") == 0) {
407 sv_mounted = true;
408 break;
409 }
410 }
411
412 if (sv_mounted == false) {
413 printf("System volume not found.\n");
414 rc = EIO;
415 goto error;
416 }
417
418 free(part_ids);
419 part_ids = NULL;
420
421 }
422
423 vol_destroy(vol);
424 return EOK;
425error:
426 vol_destroy(vol);
427 if (volume_ids != NULL)
428 free(volume_ids);
429 if (part_ids != NULL)
430 free(part_ids);
431
432 return rc;
433}
434
435/** Perform sytem startup tasks.
436 *
437 * @return EOK on success or an error code
438 */
439static errno_t system_startup(void)
440{
441 errno_t rc;
442
443 /* Make sure file systems are running. */
444 if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
445 srv_start("/srv/fs/tmpfs");
446 if (str_cmp(STRING(RDFMT), "exfat") != 0)
447 srv_start("/srv/fs/exfat");
448 if (str_cmp(STRING(RDFMT), "fat") != 0)
449 srv_start("/srv/fs/fat");
450 srv_start("/srv/fs/cdfs");
451 srv_start("/srv/fs/mfs");
452
453 srv_start("/srv/klog");
454 srv_start("/srv/fs/locfs");
455
456 if (!mount_locfs()) {
457 printf("%s: Exiting\n", NAME);
458 return EIO;
459 }
460
461 mount_tmpfs();
462
463 srv_start("/srv/devman");
464 srv_start("/srv/hid/s3c24xx_uart");
465 srv_start("/srv/hid/s3c24xx_ts");
466
467 srv_start("/srv/bd/vbd");
468 srv_start("/srv/volsrv");
469
470 init_sysvol();
471
472 srv_start("/srv/taskmon");
473
474 srv_start("/srv/net/loopip");
475 srv_start("/srv/net/ethip");
476 srv_start("/srv/net/dhcp");
477 srv_start("/srv/net/inetsrv");
478 srv_start("/srv/net/tcp");
479 srv_start("/srv/net/udp");
480 srv_start("/srv/net/dnsrsrv");
481
482 srv_start("/srv/clipboard");
483 srv_start("/srv/hid/remcons");
484
485 srv_start("/srv/hid/input", HID_INPUT);
486 srv_start("/srv/hid/output", HID_OUTPUT);
487 srv_start("/srv/audio/hound");
488
489#ifdef CONFIG_WINSYS
490 if (!config_key_exists("console")) {
491 rc = display_server();
492 if (rc == EOK) {
493 app_start("/app/taskbar", NULL);
494 app_start("/app/terminal", "-topleft");
495 }
496 }
497#endif
498 rc = console(HID_INPUT, HID_OUTPUT);
499 if (rc == EOK) {
500 getterm("term/vc0", "/app/bdsh", true);
501 getterm("term/vc1", "/app/bdsh", false);
502 getterm("term/vc2", "/app/bdsh", false);
503 getterm("term/vc3", "/app/bdsh", false);
504 getterm("term/vc4", "/app/bdsh", false);
505 getterm("term/vc5", "/app/bdsh", false);
506 }
507
508 return EOK;
509}
510
511/** Perform sytem shutdown tasks.
512 *
513 * @return EOK on success or an error code
514 */
515static errno_t system_sys_shutdown(void)
516{
517 vol_t *vol = NULL;
518 service_id_t *part_ids = NULL;
519 size_t nparts;
520 size_t i;
521 errno_t rc;
522
523 /* Eject all volumes. */
524
525 log_msg(LOG_DEFAULT, LVL_NOTE, "Ejecting volumes.");
526
527 rc = vol_create(&vol);
528 if (rc != EOK) {
529 log_msg(LOG_DEFAULT, LVL_ERROR, "Error contacting volume "
530 "service.");
531 goto error;
532 }
533
534 rc = vol_get_parts(vol, &part_ids, &nparts);
535 if (rc != EOK) {
536 log_msg(LOG_DEFAULT, LVL_ERROR, "Error getting volume list.");
537 goto error;
538 }
539
540 for (i = 0; i < nparts; i++) {
541 rc = vol_part_eject(vol, part_ids[i], vef_none);
542 if (rc != EOK) {
543 log_msg(LOG_DEFAULT, LVL_ERROR, "Error ejecting "
544 "volume %zu", (size_t)part_ids[i]);
545 goto error;
546 }
547 }
548
549 free(part_ids);
550 vol_destroy(vol);
551
552 return EOK;
553error:
554 if (part_ids != NULL)
555 free(part_ids);
556 if (vol != NULL)
557 vol_destroy(vol);
558 return rc;
559}
560
561/** Initialize system control service. */
562static errno_t system_srv_init(sys_srv_t *syssrv)
563{
564 port_id_t port;
565 loc_srv_t *srv = NULL;
566 service_id_t sid = 0;
567 errno_t rc;
568
569 (void)system;
570
571 log_msg(LOG_DEFAULT, LVL_DEBUG, "system_srv_init()");
572
573 rc = async_create_port(INTERFACE_SYSTEM, system_srv_conn, syssrv,
574 &port);
575 if (rc != EOK)
576 goto error;
577
578 rc = loc_server_register(NAME, &srv);
579 if (rc != EOK) {
580 log_msg(LOG_DEFAULT, LVL_ERROR,
581 "Failed registering server: %s.", str_error(rc));
582 rc = EEXIST;
583 goto error;
584 }
585
586 rc = loc_service_register(srv, SYSTEM_DEFAULT, &sid);
587 if (rc != EOK) {
588 log_msg(LOG_DEFAULT, LVL_ERROR,
589 "Failed registering service: %s.", str_error(rc));
590 rc = EEXIST;
591 goto error;
592 }
593
594 return EOK;
595error:
596 if (sid != 0)
597 loc_service_unregister(srv, sid);
598 if (srv != NULL)
599 loc_server_unregister(srv);
600 // XXX destroy port
601 return rc;
602}
603
604/** Handle connection to system server. */
605static void system_srv_conn(ipc_call_t *icall, void *arg)
606{
607 sys_srv_t *syssrv = (sys_srv_t *)arg;
608
609 /* Set up protocol structure */
610 system_srv_initialize(&syssrv->srv);
611 syssrv->srv.ops = &system_srv_ops;
612 syssrv->srv.arg = syssrv;
613
614 /* Handle connection */
615 system_conn(icall, &syssrv->srv);
616}
617
618/** System poweroff request.
619 *
620 * @param arg Argument (sys_srv_t *)
621 */
622static errno_t system_srv_poweroff(void *arg)
623{
624 sys_srv_t *syssrv = (sys_srv_t *)arg;
625 errno_t rc;
626
627 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_poweroff");
628
629 rc = system_sys_shutdown();
630 if (rc != EOK) {
631 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_poweroff failed");
632 system_srv_shutdown_failed(&syssrv->srv);
633 }
634
635 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_poweroff complete");
636 system_srv_shutdown_complete(&syssrv->srv);
637 return EOK;
638}
639
640/** System restart request.
641 *
642 * @param arg Argument (sys_srv_t *)
643 */
644static errno_t system_srv_restart(void *arg)
645{
646 sys_srv_t *syssrv = (sys_srv_t *)arg;
647 errno_t rc;
648
649 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_restart");
650
651 rc = system_sys_shutdown();
652 if (rc != EOK) {
653 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_restart failed");
654 system_srv_shutdown_failed(&syssrv->srv);
655 }
656
657 /* Quiesce the device tree. */
658
659 log_msg(LOG_DEFAULT, LVL_NOTE, "Quiescing devices.");
660
661 rc = devman_quiesce_devices("/hw");
662 if (rc != EOK) {
663 log_msg(LOG_DEFAULT, LVL_ERROR,
664 "Failed to quiesce device tree.");
665 return rc;
666 }
667
668 sys_reboot();
669
670 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_restart complete");
671 system_srv_shutdown_complete(&syssrv->srv);
672 return EOK;
673}
674
675int main(int argc, char *argv[])
676{
677 errno_t rc;
678 sys_srv_t srv;
679
680 info_print();
681
682 if (log_init(NAME) != EOK) {
683 printf(NAME ": Failed to initialize logging.\n");
684 return 1;
685 }
686
687 /* Perform startup tasks. */
688 rc = system_startup();
689 if (rc != EOK)
690 return 1;
691
692 rc = system_srv_init(&srv);
693 if (rc != EOK)
694 return 1;
695
696 printf(NAME ": Accepting connections.\n");
697 task_retval(0);
698 async_manager();
699
700 return 0;
701}
702
703/** @}
704 */
Note: See TracBrowser for help on using the repository browser.