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

Last change on this file since edc89bd8 was 7bf29e5, checked in by Miroslav Cimerman <mc@…>, 9 months ago

Merge 'upstream/master' into helenraid-para

  • Property mode set to 100644
File size: 14.9 KB
Line 
1/*
2 * Copyright (c) 2024 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 /* Wait until system volume is mounted */
385 sv_mounted = false;
386
387 while (true) {
388 rc = vol_get_parts(vol, &part_ids, &nparts);
389 if (rc != EOK) {
390 printf("Error getting list of volumes.\n");
391 goto error;
392 }
393
394 for (i = 0; i < nparts; i++) {
395 rc = vol_part_info(vol, part_ids[i], &pinfo);
396 if (rc != EOK) {
397 printf("Error getting partition "
398 "information.\n");
399 rc = EIO;
400 goto error;
401 }
402
403 if (str_cmp(pinfo.cur_mp, "/w") == 0) {
404 sv_mounted = true;
405 break;
406 }
407 }
408
409 if (sv_mounted)
410 break;
411
412 free(part_ids);
413 part_ids = NULL;
414
415 fibril_sleep(1);
416 printf("Sleeping(1) for system volume.\n");
417 }
418 }
419
420 vol_destroy(vol);
421 return EOK;
422error:
423 vol_destroy(vol);
424 if (volume_ids != NULL)
425 free(volume_ids);
426 if (part_ids != NULL)
427 free(part_ids);
428
429 return rc;
430}
431
432/** Perform sytem startup tasks.
433 *
434 * @return EOK on success or an error code
435 */
436static errno_t system_startup(void)
437{
438 errno_t rc;
439
440 /* Make sure file systems are running. */
441 if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
442 srv_start("/srv/fs/tmpfs");
443 if (str_cmp(STRING(RDFMT), "exfat") != 0)
444 srv_start("/srv/fs/exfat");
445 if (str_cmp(STRING(RDFMT), "fat") != 0)
446 srv_start("/srv/fs/fat");
447 srv_start("/srv/fs/cdfs");
448 srv_start("/srv/fs/mfs");
449
450 srv_start("/srv/klog");
451 srv_start("/srv/fs/locfs");
452
453 if (!mount_locfs()) {
454 printf("%s: Exiting\n", NAME);
455 return EIO;
456 }
457
458 mount_tmpfs();
459
460 srv_start("/srv/devman");
461 srv_start("/srv/hid/s3c24xx_uart");
462 srv_start("/srv/hid/s3c24xx_ts");
463
464 srv_start("/srv/bd/vbd");
465 srv_start("/srv/volsrv");
466 srv_start("/srv/bd/hr");
467
468 init_sysvol();
469
470 srv_start("/srv/taskmon");
471
472 srv_start("/srv/net/loopip");
473 srv_start("/srv/net/ethip");
474 srv_start("/srv/net/dhcp");
475 srv_start("/srv/net/inetsrv");
476 srv_start("/srv/net/tcp");
477 srv_start("/srv/net/udp");
478 srv_start("/srv/net/dnsrsrv");
479
480 srv_start("/srv/clipboard");
481 srv_start("/srv/hid/remcons");
482
483 srv_start("/srv/hid/input", HID_INPUT);
484 srv_start("/srv/hid/output", HID_OUTPUT);
485 srv_start("/srv/audio/hound");
486
487#ifdef CONFIG_WINSYS
488 if (!config_key_exists("console")) {
489 rc = display_server();
490 if (rc == EOK) {
491 app_start("/app/taskbar", NULL);
492 app_start("/app/terminal", "-topleft");
493 }
494 }
495#endif
496 rc = console(HID_INPUT, HID_OUTPUT);
497 if (rc == EOK) {
498 getterm("term/vc0", "/app/bdsh", true);
499 getterm("term/vc1", "/app/bdsh", false);
500 getterm("term/vc2", "/app/bdsh", false);
501 getterm("term/vc3", "/app/bdsh", false);
502 getterm("term/vc4", "/app/bdsh", false);
503 getterm("term/vc5", "/app/bdsh", false);
504 }
505
506 return EOK;
507}
508
509/** Perform sytem shutdown tasks.
510 *
511 * @return EOK on success or an error code
512 */
513static errno_t system_sys_shutdown(void)
514{
515 vol_t *vol = NULL;
516 service_id_t *part_ids = NULL;
517 size_t nparts;
518 size_t i;
519 errno_t rc;
520
521 /* Eject all volumes. */
522
523 rc = vol_create(&vol);
524 if (rc != EOK) {
525 log_msg(LOG_DEFAULT, LVL_ERROR, "Error contacting volume "
526 "service.");
527 goto error;
528 }
529
530 rc = vol_get_parts(vol, &part_ids, &nparts);
531 if (rc != EOK) {
532 log_msg(LOG_DEFAULT, LVL_ERROR, "Error getting volume list.");
533 goto error;
534 }
535
536 for (i = 0; i < nparts; i++) {
537 rc = vol_part_eject(vol, part_ids[i]);
538 if (rc != EOK) {
539 log_msg(LOG_DEFAULT, LVL_ERROR, "Error ejecting "
540 "volume %zu", (size_t)part_ids[i]);
541 goto error;
542 }
543 }
544
545 free(part_ids);
546 vol_destroy(vol);
547 return EOK;
548error:
549 if (part_ids != NULL)
550 free(part_ids);
551 if (vol != NULL)
552 vol_destroy(vol);
553 return rc;
554}
555
556/** Initialize system control service. */
557static errno_t system_srv_init(sys_srv_t *syssrv)
558{
559 port_id_t port;
560 loc_srv_t *srv = NULL;
561 service_id_t sid = 0;
562 errno_t rc;
563
564 (void)system;
565
566 log_msg(LOG_DEFAULT, LVL_DEBUG, "system_srv_init()");
567
568 rc = async_create_port(INTERFACE_SYSTEM, system_srv_conn, syssrv,
569 &port);
570 if (rc != EOK)
571 goto error;
572
573 rc = loc_server_register(NAME, &srv);
574 if (rc != EOK) {
575 log_msg(LOG_DEFAULT, LVL_ERROR,
576 "Failed registering server: %s.", str_error(rc));
577 rc = EEXIST;
578 goto error;
579 }
580
581 rc = loc_service_register(srv, SYSTEM_DEFAULT, &sid);
582 if (rc != EOK) {
583 log_msg(LOG_DEFAULT, LVL_ERROR,
584 "Failed registering service: %s.", str_error(rc));
585 rc = EEXIST;
586 goto error;
587 }
588
589 return EOK;
590error:
591 if (sid != 0)
592 loc_service_unregister(srv, sid);
593 if (srv != NULL)
594 loc_server_unregister(srv);
595 // XXX destroy port
596 return rc;
597}
598
599/** Handle connection to system server. */
600static void system_srv_conn(ipc_call_t *icall, void *arg)
601{
602 sys_srv_t *syssrv = (sys_srv_t *)arg;
603
604 /* Set up protocol structure */
605 system_srv_initialize(&syssrv->srv);
606 syssrv->srv.ops = &system_srv_ops;
607 syssrv->srv.arg = syssrv;
608
609 /* Handle connection */
610 system_conn(icall, &syssrv->srv);
611}
612
613/** System shutdown request.
614 *
615 * @param arg Argument (sys_srv_t *)
616 */
617static errno_t system_srv_shutdown(void *arg)
618{
619 sys_srv_t *syssrv = (sys_srv_t *)arg;
620 errno_t rc;
621
622 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_shutdown");
623
624 rc = system_sys_shutdown();
625 if (rc != EOK) {
626 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_shutdown failed");
627 system_srv_shutdown_failed(&syssrv->srv);
628 }
629
630 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_shutdown complete");
631 system_srv_shutdown_complete(&syssrv->srv);
632 return EOK;
633}
634
635int main(int argc, char *argv[])
636{
637 errno_t rc;
638 sys_srv_t srv;
639
640 info_print();
641
642 if (log_init(NAME) != EOK) {
643 printf(NAME ": Failed to initialize logging.\n");
644 return 1;
645 }
646
647 /* Perform startup tasks. */
648 rc = system_startup();
649 if (rc != EOK)
650 return 1;
651
652 rc = system_srv_init(&srv);
653 if (rc != EOK)
654 return 1;
655
656 printf(NAME ": Accepting connections.\n");
657 task_retval(0);
658 async_manager();
659
660 return 0;
661}
662
663/** @}
664 */
Note: See TracBrowser for help on using the repository browser.