source: mainline/uspace/srv/system/system.c@ f0cc1c64

Last change on this file since f0cc1c64 was f0cc1c64, checked in by Miroslav Cimerman <mc@…>, 9 days ago

Merge upstream into helenraid

  • Property mode set to 100644
File size: 16.0 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 futil_t *futil = NULL;
331 size_t i;
332 errno_t rc;
333 bool found_cfg;
334 const char **cp;
335
336 rc = vol_create(&vol);
337 if (rc != EOK) {
338 printf("Error contacting volume service.\n");
339 goto error;
340 }
341
342 rc = vol_get_volumes(vol, &volume_ids, &nvols);
343 if (rc != EOK) {
344 printf("Error getting list of volumes.\n");
345 goto error;
346 }
347
348 /* XXX This could be handled more efficiently by volsrv itself */
349 found_cfg = false;
350 for (i = 0; i < nvols; i++) {
351 rc = vol_info(vol, volume_ids[i], &vinfo);
352 if (rc != EOK) {
353 printf("Error getting volume information.\n");
354 rc = EIO;
355 goto error;
356 }
357
358 if (str_cmp(vinfo.path, "/w") == 0) {
359 found_cfg = true;
360 break;
361 }
362 }
363
364 free(volume_ids);
365 volume_ids = NULL;
366
367 if (!found_cfg) {
368 /* Prepare directory structure for live image mode */
369 printf("%s: Creating live image directory structure.\n", NAME);
370 cp = sys_dirs;
371 while (*cp != NULL) {
372 rc = vfs_link_path(*cp, KIND_DIRECTORY, NULL);
373 if (rc != EOK) {
374 printf("%s: Error creating directory '%s'.\n",
375 NAME, *cp);
376 goto error;
377 }
378
379 ++cp;
380 }
381
382 /* Copy initial configuration files */
383 rc = futil_create(NULL, NULL, &futil);
384 if (rc != EOK)
385 goto error;
386
387 rc = futil_rcopy_contents(futil, "/cfg", "/w/cfg");
388 if (rc != EOK)
389 goto error;
390
391 futil_destroy(futil);
392 futil = NULL;
393 } else {
394 printf("%s: System volume is configured.\n", NAME);
395
396 /* Verify that system volume is mounted */
397 sv_mounted = false;
398
399 rc = vol_get_parts(vol, &part_ids, &nparts);
400 if (rc != EOK) {
401 printf("Error getting list of volumes.\n");
402 goto error;
403 }
404
405 for (i = 0; i < nparts; i++) {
406 rc = vol_part_info(vol, part_ids[i], &pinfo);
407 if (rc != EOK) {
408 printf("Error getting partition "
409 "information.\n");
410 rc = EIO;
411 goto error;
412 }
413
414 if (str_cmp(pinfo.cur_mp, "/w") == 0) {
415 sv_mounted = true;
416 break;
417 }
418 }
419
420 if (sv_mounted == false) {
421 printf("System volume not found.\n");
422 rc = EIO;
423 goto error;
424 }
425
426 free(part_ids);
427 part_ids = NULL;
428
429 }
430
431 vol_destroy(vol);
432 return EOK;
433error:
434 vol_destroy(vol);
435 if (volume_ids != NULL)
436 free(volume_ids);
437 if (part_ids != NULL)
438 free(part_ids);
439 if (futil != NULL)
440 futil_destroy(futil);
441
442 return rc;
443}
444
445/** Perform sytem startup tasks.
446 *
447 * @return EOK on success or an error code
448 */
449static errno_t system_startup(void)
450{
451 errno_t rc;
452
453 /* Make sure file systems are running. */
454 if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
455 srv_start("/srv/fs/tmpfs");
456 if (str_cmp(STRING(RDFMT), "exfat") != 0)
457 srv_start("/srv/fs/exfat");
458 if (str_cmp(STRING(RDFMT), "fat") != 0)
459 srv_start("/srv/fs/fat");
460 srv_start("/srv/fs/cdfs");
461 srv_start("/srv/fs/mfs");
462
463 srv_start("/srv/klog");
464 srv_start("/srv/fs/locfs");
465
466 if (!mount_locfs()) {
467 printf("%s: Exiting\n", NAME);
468 return EIO;
469 }
470
471 mount_tmpfs();
472
473 srv_start("/srv/devman");
474 srv_start("/srv/hid/s3c24xx_uart");
475 srv_start("/srv/hid/s3c24xx_ts");
476
477 srv_start("/srv/bd/vbd");
478 srv_start("/srv/volsrv");
479 srv_start("/srv/bd/hr");
480
481 init_sysvol();
482
483 srv_start("/srv/taskmon");
484
485 srv_start("/srv/net/loopip");
486 srv_start("/srv/net/ethip");
487 srv_start("/srv/net/dhcp");
488 srv_start("/srv/net/inetsrv");
489 srv_start("/srv/net/tcp");
490 srv_start("/srv/net/udp");
491 srv_start("/srv/net/dnsrsrv");
492
493 srv_start("/srv/clipboard");
494 srv_start("/srv/hid/remcons");
495
496 srv_start("/srv/hid/input", HID_INPUT);
497 srv_start("/srv/hid/output", HID_OUTPUT);
498 srv_start("/srv/audio/hound");
499
500#ifdef CONFIG_WINSYS
501 if (!config_key_exists("console")) {
502 rc = display_server();
503 if (rc == EOK) {
504 app_start("/app/taskbar", NULL);
505 app_start("/app/terminal", "-topleft");
506 }
507 }
508#endif
509 rc = console(HID_INPUT, HID_OUTPUT);
510 if (rc == EOK) {
511 getterm("term/vc0", "/app/bdsh", true);
512 getterm("term/vc1", "/app/bdsh", false);
513 getterm("term/vc2", "/app/bdsh", false);
514 getterm("term/vc3", "/app/bdsh", false);
515 getterm("term/vc4", "/app/bdsh", false);
516 getterm("term/vc5", "/app/bdsh", false);
517 }
518
519 return EOK;
520}
521
522/** Perform sytem shutdown tasks.
523 *
524 * @return EOK on success or an error code
525 */
526static errno_t system_sys_shutdown(void)
527{
528 vol_t *vol = NULL;
529 service_id_t *part_ids = NULL;
530 size_t nparts;
531 size_t i;
532 errno_t rc;
533
534 /* Eject all volumes. */
535
536 log_msg(LOG_DEFAULT, LVL_NOTE, "Ejecting volumes.");
537
538 rc = vol_create(&vol);
539 if (rc != EOK) {
540 log_msg(LOG_DEFAULT, LVL_ERROR, "Error contacting volume "
541 "service.");
542 goto error;
543 }
544
545 rc = vol_get_parts(vol, &part_ids, &nparts);
546 if (rc != EOK) {
547 log_msg(LOG_DEFAULT, LVL_ERROR, "Error getting volume list.");
548 goto error;
549 }
550
551 for (i = 0; i < nparts; i++) {
552 rc = vol_part_eject(vol, part_ids[i], vef_none);
553 if (rc != EOK) {
554 log_msg(LOG_DEFAULT, LVL_ERROR, "Error ejecting "
555 "volume %zu", (size_t)part_ids[i]);
556 goto error;
557 }
558 }
559
560 free(part_ids);
561 vol_destroy(vol);
562
563 return EOK;
564error:
565 if (part_ids != NULL)
566 free(part_ids);
567 if (vol != NULL)
568 vol_destroy(vol);
569 return rc;
570}
571
572/** Initialize system control service. */
573static errno_t system_srv_init(sys_srv_t *syssrv)
574{
575 port_id_t port = 0;
576 loc_srv_t *srv = NULL;
577 service_id_t sid = 0;
578 errno_t rc;
579
580 (void)system;
581
582 log_msg(LOG_DEFAULT, LVL_DEBUG, "system_srv_init()");
583
584 rc = async_create_port(INTERFACE_SYSTEM, system_srv_conn, syssrv,
585 &port);
586 if (rc != EOK)
587 goto error;
588
589 rc = loc_server_register(NAME, &srv);
590 if (rc != EOK) {
591 log_msg(LOG_DEFAULT, LVL_ERROR,
592 "Failed registering server: %s.", str_error(rc));
593 rc = EEXIST;
594 goto error;
595 }
596
597 rc = loc_service_register(srv, SYSTEM_DEFAULT, port, &sid);
598 if (rc != EOK) {
599 log_msg(LOG_DEFAULT, LVL_ERROR,
600 "Failed registering service: %s.", str_error(rc));
601 rc = EEXIST;
602 goto error;
603 }
604
605 return EOK;
606error:
607 if (sid != 0)
608 loc_service_unregister(srv, sid);
609 if (srv != NULL)
610 loc_server_unregister(srv);
611 if (port != 0)
612 async_port_destroy(port);
613 return rc;
614}
615
616/** Handle connection to system server. */
617static void system_srv_conn(ipc_call_t *icall, void *arg)
618{
619 sys_srv_t *syssrv = (sys_srv_t *)arg;
620
621 /* Set up protocol structure */
622 system_srv_initialize(&syssrv->srv);
623 syssrv->srv.ops = &system_srv_ops;
624 syssrv->srv.arg = syssrv;
625
626 /* Handle connection */
627 system_conn(icall, &syssrv->srv);
628}
629
630/** System poweroff request.
631 *
632 * @param arg Argument (sys_srv_t *)
633 */
634static errno_t system_srv_poweroff(void *arg)
635{
636 sys_srv_t *syssrv = (sys_srv_t *)arg;
637 errno_t rc;
638
639 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_poweroff");
640
641 rc = system_sys_shutdown();
642 if (rc != EOK) {
643 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_poweroff failed");
644 system_srv_shutdown_failed(&syssrv->srv);
645 }
646
647 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_poweroff complete");
648 system_srv_shutdown_complete(&syssrv->srv);
649 return EOK;
650}
651
652/** System restart request.
653 *
654 * @param arg Argument (sys_srv_t *)
655 */
656static errno_t system_srv_restart(void *arg)
657{
658 sys_srv_t *syssrv = (sys_srv_t *)arg;
659 errno_t rc;
660
661 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_restart");
662
663 rc = system_sys_shutdown();
664 if (rc != EOK) {
665 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_restart failed");
666 system_srv_shutdown_failed(&syssrv->srv);
667 }
668
669 /* Quiesce the device tree. */
670
671 log_msg(LOG_DEFAULT, LVL_NOTE, "Quiescing devices.");
672
673 rc = devman_quiesce_devices("/hw");
674 if (rc != EOK) {
675 log_msg(LOG_DEFAULT, LVL_ERROR,
676 "Failed to quiesce device tree.");
677 return rc;
678 }
679
680 sys_reboot();
681
682 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_restart complete");
683 system_srv_shutdown_complete(&syssrv->srv);
684 return EOK;
685}
686
687int main(int argc, char *argv[])
688{
689 errno_t rc;
690 sys_srv_t srv;
691
692 info_print();
693
694 if (log_init(NAME) != EOK) {
695 printf(NAME ": Failed to initialize logging.\n");
696 return 1;
697 }
698
699 /* Perform startup tasks. */
700 rc = system_startup();
701 if (rc != EOK)
702 return 1;
703
704 rc = system_srv_init(&srv);
705 if (rc != EOK)
706 return 1;
707
708 printf(NAME ": Accepting connections.\n");
709 task_retval(0);
710 async_manager();
711
712 return 0;
713}
714
715/** @}
716 */
Note: See TracBrowser for help on using the repository browser.