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

Last change on this file since eebecdc was eebecdc, checked in by Miroslav Cimerman <mc@…>, 5 months ago

merge upstream/master into helenraid

  • 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 srv_start("/srv/bd/hr");
470
471 init_sysvol();
472
473 srv_start("/srv/taskmon");
474
475 srv_start("/srv/net/loopip");
476 srv_start("/srv/net/ethip");
477 srv_start("/srv/net/dhcp");
478 srv_start("/srv/net/inetsrv");
479 srv_start("/srv/net/tcp");
480 srv_start("/srv/net/udp");
481 srv_start("/srv/net/dnsrsrv");
482
483 srv_start("/srv/clipboard");
484 srv_start("/srv/hid/remcons");
485
486 srv_start("/srv/hid/input", HID_INPUT);
487 srv_start("/srv/hid/output", HID_OUTPUT);
488 srv_start("/srv/audio/hound");
489
490#ifdef CONFIG_WINSYS
491 if (!config_key_exists("console")) {
492 rc = display_server();
493 if (rc == EOK) {
494 app_start("/app/taskbar", NULL);
495 app_start("/app/terminal", "-topleft");
496 }
497 }
498#endif
499 rc = console(HID_INPUT, HID_OUTPUT);
500 if (rc == EOK) {
501 getterm("term/vc0", "/app/bdsh", true);
502 getterm("term/vc1", "/app/bdsh", false);
503 getterm("term/vc2", "/app/bdsh", false);
504 getterm("term/vc3", "/app/bdsh", false);
505 getterm("term/vc4", "/app/bdsh", false);
506 getterm("term/vc5", "/app/bdsh", false);
507 }
508
509 return EOK;
510}
511
512/** Perform sytem shutdown tasks.
513 *
514 * @return EOK on success or an error code
515 */
516static errno_t system_sys_shutdown(void)
517{
518 vol_t *vol = NULL;
519 service_id_t *part_ids = NULL;
520 size_t nparts;
521 size_t i;
522 errno_t rc;
523
524 /* Eject all volumes. */
525
526 log_msg(LOG_DEFAULT, LVL_NOTE, "Ejecting volumes.");
527
528 rc = vol_create(&vol);
529 if (rc != EOK) {
530 log_msg(LOG_DEFAULT, LVL_ERROR, "Error contacting volume "
531 "service.");
532 goto error;
533 }
534
535 rc = vol_get_parts(vol, &part_ids, &nparts);
536 if (rc != EOK) {
537 log_msg(LOG_DEFAULT, LVL_ERROR, "Error getting volume list.");
538 goto error;
539 }
540
541 for (i = 0; i < nparts; i++) {
542 rc = vol_part_eject(vol, part_ids[i], vef_none);
543 if (rc != EOK) {
544 log_msg(LOG_DEFAULT, LVL_ERROR, "Error ejecting "
545 "volume %zu", (size_t)part_ids[i]);
546 goto error;
547 }
548 }
549
550 free(part_ids);
551 vol_destroy(vol);
552
553 return EOK;
554error:
555 if (part_ids != NULL)
556 free(part_ids);
557 if (vol != NULL)
558 vol_destroy(vol);
559 return rc;
560}
561
562/** Initialize system control service. */
563static errno_t system_srv_init(sys_srv_t *syssrv)
564{
565 port_id_t port;
566 loc_srv_t *srv = NULL;
567 service_id_t sid = 0;
568 errno_t rc;
569
570 (void)system;
571
572 log_msg(LOG_DEFAULT, LVL_DEBUG, "system_srv_init()");
573
574 rc = async_create_port(INTERFACE_SYSTEM, system_srv_conn, syssrv,
575 &port);
576 if (rc != EOK)
577 goto error;
578
579 rc = loc_server_register(NAME, &srv);
580 if (rc != EOK) {
581 log_msg(LOG_DEFAULT, LVL_ERROR,
582 "Failed registering server: %s.", str_error(rc));
583 rc = EEXIST;
584 goto error;
585 }
586
587 rc = loc_service_register(srv, SYSTEM_DEFAULT, &sid);
588 if (rc != EOK) {
589 log_msg(LOG_DEFAULT, LVL_ERROR,
590 "Failed registering service: %s.", str_error(rc));
591 rc = EEXIST;
592 goto error;
593 }
594
595 return EOK;
596error:
597 if (sid != 0)
598 loc_service_unregister(srv, sid);
599 if (srv != NULL)
600 loc_server_unregister(srv);
601 // XXX destroy port
602 return rc;
603}
604
605/** Handle connection to system server. */
606static void system_srv_conn(ipc_call_t *icall, void *arg)
607{
608 sys_srv_t *syssrv = (sys_srv_t *)arg;
609
610 /* Set up protocol structure */
611 system_srv_initialize(&syssrv->srv);
612 syssrv->srv.ops = &system_srv_ops;
613 syssrv->srv.arg = syssrv;
614
615 /* Handle connection */
616 system_conn(icall, &syssrv->srv);
617}
618
619/** System poweroff request.
620 *
621 * @param arg Argument (sys_srv_t *)
622 */
623static errno_t system_srv_poweroff(void *arg)
624{
625 sys_srv_t *syssrv = (sys_srv_t *)arg;
626 errno_t rc;
627
628 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_poweroff");
629
630 rc = system_sys_shutdown();
631 if (rc != EOK) {
632 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_poweroff failed");
633 system_srv_shutdown_failed(&syssrv->srv);
634 }
635
636 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_poweroff complete");
637 system_srv_shutdown_complete(&syssrv->srv);
638 return EOK;
639}
640
641/** System restart request.
642 *
643 * @param arg Argument (sys_srv_t *)
644 */
645static errno_t system_srv_restart(void *arg)
646{
647 sys_srv_t *syssrv = (sys_srv_t *)arg;
648 errno_t rc;
649
650 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_restart");
651
652 rc = system_sys_shutdown();
653 if (rc != EOK) {
654 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_restart failed");
655 system_srv_shutdown_failed(&syssrv->srv);
656 }
657
658 /* Quiesce the device tree. */
659
660 log_msg(LOG_DEFAULT, LVL_NOTE, "Quiescing devices.");
661
662 rc = devman_quiesce_devices("/hw");
663 if (rc != EOK) {
664 log_msg(LOG_DEFAULT, LVL_ERROR,
665 "Failed to quiesce device tree.");
666 return rc;
667 }
668
669 sys_reboot();
670
671 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_restart complete");
672 system_srv_shutdown_complete(&syssrv->srv);
673 return EOK;
674}
675
676int main(int argc, char *argv[])
677{
678 errno_t rc;
679 sys_srv_t srv;
680
681 info_print();
682
683 if (log_init(NAME) != EOK) {
684 printf(NAME ": Failed to initialize logging.\n");
685 return 1;
686 }
687
688 /* Perform startup tasks. */
689 rc = system_startup();
690 if (rc != EOK)
691 return 1;
692
693 rc = system_srv_init(&srv);
694 if (rc != EOK)
695 return 1;
696
697 printf(NAME ": Accepting connections.\n");
698 task_retval(0);
699 async_manager();
700
701 return 0;
702}
703
704/** @}
705 */
Note: See TracBrowser for help on using the repository browser.