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

Last change on this file was ca48672, checked in by Jiri Svoboda <jiri@…>, 8 days ago

loc_service_register() needs to take a port ID argument.

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