source: mainline/uspace/srv/system/system.c@ 10005fd

Last change on this file since 10005fd was d30e067, checked in by Miroslav Cimerman <mc@…>, 7 months ago

Merge 'upstream/master' into helenraid-para

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