source: mainline/uspace/app/init/init.c@ 04e520e

Last change on this file since 04e520e was 04e520e, checked in by Jiri Svoboda <jiri@…>, 20 months ago

Config file persistence

Copy /cfg to /w/cfg when installing and when starting live image
Move futil to separate library
Wait for /w to be mounted while booting from disk
Change taskbar and taskbar-cfg to work with /w/cfg/taskbar.sif

  • Property mode set to 100644
File size: 12.7 KB
RevLine 
[3eddaff]1/*
[04e520e]2 * Copyright (c) 2024 Jiri Svoboda
[df4ed85]3 * Copyright (c) 2005 Martin Decky
[3eddaff]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
[a63966d]30/** @addtogroup init
[b2951e2]31 * @{
[c91c9fb]32 */
[b2951e2]33/**
34 * @file
35 */
36
[0b63dc2]37#include <fibril.h>
[04e520e]38#include <futil.h>
[00c4994]39#include <stdio.h>
[6d5e378]40#include <stdarg.h>
[860271d4]41#include <vfs/vfs.h>
[3e6a98c5]42#include <stdbool.h>
[860271d4]43#include <errno.h>
44#include <task.h>
[38d150e]45#include <stdlib.h>
[c91c9fb]46#include <macros.h>
[19f857a]47#include <str.h>
[15f3c3f]48#include <loc.h>
[d9fae235]49#include <str_error.h>
[73d8600]50#include <config.h>
[8e9b2534]51#include <io/logctl.h>
[0b63dc2]52#include <vfs/vfs.h>
[63c1dd5]53#include <vol.h>
[75701004]54#include "untar.h"
[860271d4]55#include "init.h"
56
[51da086]57#define BANNER_LEFT "######> "
58#define BANNER_RIGHT " <######"
59
[d9fae235]60#define ROOT_DEVICE "bd/initrd"
61#define ROOT_MOUNT_POINT "/"
62
[15f3c3f]63#define LOCFS_FS_TYPE "locfs"
64#define LOCFS_MOUNT_POINT "/loc"
[47a350f]65
[1ab0852]66#define TMPFS_FS_TYPE "tmpfs"
67#define TMPFS_MOUNT_POINT "/tmp"
[d9fae235]68
[8fefd8b]69#define SRV_CONSOLE "/srv/hid/console"
[df747bd8]70#define APP_GETTERM "/app/getterm"
[47a350f]71
[0b63dc2]72#define SRV_DISPLAY "/srv/hid/display"
[6d5e378]73
74#define HID_INPUT "hid/input"
75#define HID_OUTPUT "hid/output"
76
77#define srv_start(path, ...) \
78 srv_startl(path, path, ##__VA_ARGS__, NULL)
79
[63c1dd5]80static const char *sys_dirs[] = {
81 "/w/cfg",
82 "/w/data"
83};
84
[53d6ac3d]85/** Print banner */
[36b8100a]86static void info_print(void)
87{
[d9fae235]88 printf("%s: HelenOS init\n", NAME);
[36b8100a]89}
90
[51da086]91static void oom_check(errno_t rc, const char *path)
92{
93 if (rc == ENOMEM) {
94 printf("%sOut-of-memory condition detected%s\n", BANNER_LEFT,
95 BANNER_RIGHT);
96 printf("%sBailing out of the boot process after %s%s\n",
97 BANNER_LEFT, path, BANNER_RIGHT);
98 printf("%sMore physical memory is required%s\n", BANNER_LEFT,
99 BANNER_RIGHT);
100 exit(ENOMEM);
101 }
102}
103
[53d6ac3d]104/** Report mount operation success */
[d9fae235]105static bool mount_report(const char *desc, const char *mntpt,
[b7fd2a0]106 const char *fstype, const char *dev, errno_t rc)
[860271d4]107{
[f49cf64]108 switch (rc) {
109 case EOK:
[75701004]110 if ((dev != NULL) && (str_cmp(dev, "") != 0))
[d9fae235]111 printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
112 fstype, dev);
113 else
114 printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
[f49cf64]115 break;
116 case EBUSY:
[d9fae235]117 printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
[f49cf64]118 return false;
119 case ELIMIT:
[d9fae235]120 printf("%s: %s limit exceeded\n", NAME, desc);
[f49cf64]121 return false;
122 case ENOENT:
[d9fae235]123 printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
[f49cf64]124 return false;
125 default:
[d9fae235]126 printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
127 str_error(rc));
[f49cf64]128 return false;
[860271d4]129 }
[a35b458]130
[860271d4]131 return true;
132}
133
[75701004]134/** Mount root file system
[53d6ac3d]135 *
[75701004]136 * The operation blocks until the root file system
[53d6ac3d]137 * server is ready for mounting.
138 *
[75701004]139 * @param[in] fstype Root file system type.
[53d6ac3d]140 *
141 * @return True on success.
142 * @return False on failure.
143 *
144 */
[d9fae235]145static bool mount_root(const char *fstype)
[a095d20]146{
[75701004]147 const char *root_device = "";
[a35b458]148
[75701004]149 if (str_cmp(fstype, "tmpfs") != 0)
150 root_device = ROOT_DEVICE;
[a35b458]151
[75701004]152 errno_t rc = vfs_mount_path(ROOT_MOUNT_POINT, fstype, root_device, "",
[4979403]153 IPC_FLAG_BLOCKING, 0);
[8e9b2534]154 if (rc == EOK)
155 logctl_set_root();
[75701004]156
157 bool ret = mount_report("Root file system", ROOT_MOUNT_POINT, fstype,
158 root_device, rc);
159
160 rc = vfs_cwd_set(ROOT_MOUNT_POINT);
161 if (rc != EOK) {
162 printf("%s: Unable to set current directory to %s (%s)\n",
163 NAME, ROOT_MOUNT_POINT, str_error(ret));
164 return false;
165 }
166
167 if ((ret) && (str_cmp(fstype, "tmpfs") == 0)) {
168 printf("%s: Extracting root file system archive\n", NAME);
169 ret = bd_untar(ROOT_DEVICE);
170 }
171
172 return ret;
[d9fae235]173}
174
[75701004]175/** Mount locfs file system
[53d6ac3d]176 *
[75701004]177 * The operation blocks until the locfs file system
[53d6ac3d]178 * server is ready for mounting.
179 *
180 * @return True on success.
181 * @return False on failure.
182 *
183 */
[15f3c3f]184static bool mount_locfs(void)
[d9fae235]185{
[b7fd2a0]186 errno_t rc = vfs_mount_path(LOCFS_MOUNT_POINT, LOCFS_FS_TYPE, "", "",
[4979403]187 IPC_FLAG_BLOCKING, 0);
[75701004]188 return mount_report("Location service file system", LOCFS_MOUNT_POINT,
[15f3c3f]189 LOCFS_FS_TYPE, NULL, rc);
[a095d20]190}
191
[b7fd2a0]192static errno_t srv_startl(const char *path, ...)
[860271d4]193{
[39330200]194 vfs_stat_t s;
[23a0368]195 if (vfs_stat_path(path, &s) != EOK) {
[6d5e378]196 printf("%s: Unable to stat %s\n", NAME, path);
197 return ENOENT;
198 }
[a35b458]199
[6d5e378]200 printf("%s: Starting %s\n", NAME, path);
[a35b458]201
[6d5e378]202 va_list ap;
203 const char *arg;
204 int cnt = 0;
[a35b458]205
[6d5e378]206 va_start(ap, path);
207 do {
208 arg = va_arg(ap, const char *);
209 cnt++;
210 } while (arg != NULL);
211 va_end(ap);
[a35b458]212
[6d5e378]213 va_start(ap, path);
214 task_id_t id;
[1c635d6]215 task_wait_t wait;
[b7fd2a0]216 errno_t rc = task_spawn(&id, &wait, path, cnt, ap);
[6d5e378]217 va_end(ap);
[a35b458]218
[0485135]219 if (rc != EOK) {
[51da086]220 oom_check(rc, path);
[6d5e378]221 printf("%s: Error spawning %s (%s)\n", NAME, path,
[0485135]222 str_error(rc));
[6d5e378]223 return rc;
[0485135]224 }
[a35b458]225
[95bc57c]226 if (!id) {
[6d5e378]227 printf("%s: Error spawning %s (invalid task id)\n", NAME,
228 path);
229 return EINVAL;
[95bc57c]230 }
[a35b458]231
[6d5e378]232 task_exit_t texit;
233 int retval;
[1c635d6]234 rc = task_wait(&wait, &texit, &retval);
[95bc57c]235 if (rc != EOK) {
[6d5e378]236 printf("%s: Error waiting for %s (%s)\n", NAME, path,
[0485135]237 str_error(rc));
[6d5e378]238 return rc;
[95bc57c]239 }
[a35b458]240
[0485135]241 if (texit != TASK_EXIT_NORMAL) {
242 printf("%s: Server %s failed to start (unexpectedly "
[6d5e378]243 "terminated)\n", NAME, path);
244 return EINVAL;
[0485135]245 }
[a35b458]246
[6d5e378]247 if (retval != 0)
[0485135]248 printf("%s: Server %s failed to start (exit code %d)\n", NAME,
[6d5e378]249 path, retval);
[a35b458]250
[1569a9b]251 return retval == 0 ? EOK : EPARTY;
[95bc57c]252}
253
[b7fd2a0]254static errno_t console(const char *isvc, const char *osvc)
[47a350f]255{
[15f3c3f]256 /* Wait for the input service to be ready */
257 service_id_t service_id;
[b7fd2a0]258 errno_t rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
[0485135]259 if (rc != EOK) {
[7c014d1]260 printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
[d9fae235]261 str_error(rc));
[6d5e378]262 return rc;
[0485135]263 }
[a35b458]264
[6d5e378]265 /* Wait for the output service to be ready */
266 rc = loc_service_get_id(osvc, &service_id, IPC_FLAG_BLOCKING);
[0485135]267 if (rc != EOK) {
[6d5e378]268 printf("%s: Error waiting on %s (%s)\n", NAME, osvc,
[7c014d1]269 str_error(rc));
[6d5e378]270 return rc;
271 }
[a35b458]272
[6d5e378]273 return srv_start(SRV_CONSOLE, isvc, osvc);
274}
275
[1382446]276#ifdef CONFIG_WINSYS
277
[0b63dc2]278static errno_t display_server(void)
[6d5e378]279{
[0b63dc2]280 return srv_start(SRV_DISPLAY);
[6d5e378]281}
282
[06d0c81]283static int app_start(const char *app, const char *arg)
[6d5e378]284{
[0b63dc2]285 printf("%s: Spawning %s\n", NAME, app);
[a35b458]286
[6d5e378]287 task_id_t id;
[1c635d6]288 task_wait_t wait;
[06d0c81]289 errno_t rc = task_spawnl(&id, &wait, app, app, arg, NULL);
[7c014d1]290 if (rc != EOK) {
[51da086]291 oom_check(rc, app);
[0b63dc2]292 printf("%s: Error spawning %s (%s)\n", NAME, app,
293 str_error(rc));
[6d5e378]294 return -1;
[0485135]295 }
[a35b458]296
[6d5e378]297 task_exit_t texit;
298 int retval;
[1c635d6]299 rc = task_wait(&wait, &texit, &retval);
[6d5e378]300 if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
301 printf("%s: Error retrieving retval from %s (%s)\n", NAME,
302 app, str_error(rc));
[51da086]303 return rc;
[0485135]304 }
[a35b458]305
[6d5e378]306 return retval;
[47a350f]307}
308
[1382446]309#endif
310
[593e023]311static void getterm(const char *svc, const char *app, bool msg)
[36b8100a]312{
[593e023]313 if (msg) {
314 printf("%s: Spawning %s %s %s --msg --wait -- %s\n", NAME,
315 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
[a35b458]316
[b7fd2a0]317 errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
[593e023]318 LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
[51da086]319 if (rc != EOK) {
320 oom_check(rc, APP_GETTERM);
[593e023]321 printf("%s: Error spawning %s %s %s --msg --wait -- %s\n",
322 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
[51da086]323 }
[4deb8b5]324 } else {
[593e023]325 printf("%s: Spawning %s %s %s --wait -- %s\n", NAME,
326 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
[a35b458]327
[b7fd2a0]328 errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
[593e023]329 LOCFS_MOUNT_POINT, "--wait", "--", app, NULL);
[51da086]330 if (rc != EOK) {
331 oom_check(rc, APP_GETTERM);
[593e023]332 printf("%s: Error spawning %s %s %s --wait -- %s\n",
333 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
[51da086]334 }
[0485135]335 }
[36b8100a]336}
337
[1ab0852]338static bool mount_tmpfs(void)
[03333bc]339{
[b7fd2a0]340 errno_t rc = vfs_mount_path(TMPFS_MOUNT_POINT, TMPFS_FS_TYPE, "", "", 0, 0);
[75701004]341 return mount_report("Temporary file system", TMPFS_MOUNT_POINT,
[1ab0852]342 TMPFS_FS_TYPE, NULL, rc);
[03333bc]343}
344
[63c1dd5]345/** Init system volume.
346 *
347 * See if system volume is configured. If so, try to wait for it to become
348 * available. If not, create basic directories for live image omde.
349 */
350static errno_t init_sysvol(void)
351{
352 vol_t *vol = NULL;
353 vol_info_t vinfo;
354 volume_id_t *volume_ids = NULL;
[04e520e]355 service_id_t *part_ids = NULL;
356 vol_part_info_t pinfo;
[63c1dd5]357 size_t nvols;
[04e520e]358 size_t nparts;
359 bool sv_mounted;
[63c1dd5]360 size_t i;
361 errno_t rc;
362 bool found_cfg;
363 const char **cp;
364
365 rc = vol_create(&vol);
366 if (rc != EOK) {
367 printf("Error contacting volume service.\n");
368 goto error;
369 }
370
371 rc = vol_get_volumes(vol, &volume_ids, &nvols);
372 if (rc != EOK) {
373 printf("Error getting list of volumes.\n");
374 goto error;
375 }
376
377 /* XXX This could be handled more efficiently by volsrv itself */
378 found_cfg = false;
379 for (i = 0; i < nvols; i++) {
380 rc = vol_info(vol, volume_ids[i], &vinfo);
381 if (rc != EOK) {
382 printf("Error getting volume information.\n");
383 rc = EIO;
384 goto error;
385 }
386
387 if (str_cmp(vinfo.path, "/w") == 0) {
388 found_cfg = true;
389 break;
390 }
391 }
392
393 free(volume_ids);
[04e520e]394 volume_ids = NULL;
[63c1dd5]395
396 if (!found_cfg) {
397 /* Prepare directory structure for live image mode */
398 printf("%s: Creating live image directory structure.\n", NAME);
399 cp = sys_dirs;
400 while (*cp != NULL) {
401 rc = vfs_link_path(*cp, KIND_DIRECTORY, NULL);
402 if (rc != EOK) {
403 printf("%s: Error creating directory '%s'.\n",
404 NAME, *cp);
[04e520e]405 goto error;
[63c1dd5]406 }
407
408 ++cp;
409 }
[04e520e]410
411 /* Copy initial configuration files */
412 rc = futil_rcopy_contents("/cfg", "/w/cfg");
413 if (rc != EOK)
414 goto error;
[63c1dd5]415 } else {
416 printf("%s: System volume is configured.\n", NAME);
[04e520e]417
418 /* Wait until system volume is mounted */
419 sv_mounted = false;
420
421 while (true) {
422 rc = vol_get_parts(vol, &part_ids, &nparts);
423 if (rc != EOK) {
424 printf("Error getting list of volumes.\n");
425 goto error;
426 }
427
428 for (i = 0; i < nparts; i++) {
429 rc = vol_part_info(vol, part_ids[i], &pinfo);
430 if (rc != EOK) {
431 printf("Error getting partition "
432 "information.\n");
433 rc = EIO;
434 goto error;
435 }
436
437 if (str_cmp(pinfo.cur_mp, "/w") == 0) {
438 sv_mounted = true;
439 break;
440 }
441 }
442
443 if (sv_mounted)
444 break;
445
446 free(part_ids);
447 part_ids = NULL;
448
449 fibril_sleep(1);
450 printf("Sleeping(1) for system volume.\n");
451 }
[63c1dd5]452 }
453
[04e520e]454 vol_destroy(vol);
[63c1dd5]455 return EOK;
456error:
457 vol_destroy(vol);
458 if (volume_ids != NULL)
459 free(volume_ids);
[04e520e]460 if (part_ids != NULL)
461 free(part_ids);
[63c1dd5]462
463 return rc;
464}
465
[5106e98]466int main(int argc, char *argv[])
467{
[b7fd2a0]468 errno_t rc;
[73d8600]469
[860271d4]470 info_print();
[a35b458]471
[36b8100a]472 if (!mount_root(STRING(RDFMT))) {
[d9fae235]473 printf("%s: Exiting\n", NAME);
[6d5e378]474 return 1;
[860271d4]475 }
[a35b458]476
[395df52]477 /* Make sure file systems are running. */
[6d5e378]478 if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
[8fefd8b]479 srv_start("/srv/fs/tmpfs");
[395df52]480 if (str_cmp(STRING(RDFMT), "exfat") != 0)
[8fefd8b]481 srv_start("/srv/fs/exfat");
[395df52]482 if (str_cmp(STRING(RDFMT), "fat") != 0)
[8fefd8b]483 srv_start("/srv/fs/fat");
484 srv_start("/srv/fs/cdfs");
485 srv_start("/srv/fs/mfs");
[a35b458]486
[ca05e9b]487 srv_start("/srv/klog");
[8fefd8b]488 srv_start("/srv/fs/locfs");
[5cd1eb9a]489 srv_start("/srv/taskmon");
[a35b458]490
[15f3c3f]491 if (!mount_locfs()) {
[d9fae235]492 printf("%s: Exiting\n", NAME);
[6d5e378]493 return 2;
[a095d20]494 }
[a35b458]495
[1ab0852]496 mount_tmpfs();
[a35b458]497
[6d5e378]498 srv_start("/srv/devman");
[8fefd8b]499 srv_start("/srv/hid/s3c24xx_uart");
500 srv_start("/srv/hid/s3c24xx_ts");
[a35b458]501
[8fefd8b]502 srv_start("/srv/bd/vbd");
[dc2d582]503 srv_start("/srv/volsrv");
[a35b458]504
[8fefd8b]505 srv_start("/srv/net/loopip");
506 srv_start("/srv/net/ethip");
507 srv_start("/srv/net/inetsrv");
508 srv_start("/srv/net/tcp");
509 srv_start("/srv/net/udp");
510 srv_start("/srv/net/dnsrsrv");
511 srv_start("/srv/net/dhcp");
512 srv_start("/srv/net/nconfsrv");
[a35b458]513
[6d5e378]514 srv_start("/srv/clipboard");
[8fefd8b]515 srv_start("/srv/hid/remcons");
[a35b458]516
[8fefd8b]517 srv_start("/srv/hid/input", HID_INPUT);
518 srv_start("/srv/hid/output", HID_OUTPUT);
519 srv_start("/srv/audio/hound");
[a35b458]520
[63c1dd5]521 init_sysvol();
522
[1382446]523#ifdef CONFIG_WINSYS
[73d8600]524 if (!config_key_exists("console")) {
[0b63dc2]525 rc = display_server();
[73d8600]526 if (rc == EOK) {
[a5c7b865]527 app_start("/app/taskbar", NULL);
[06d0c81]528 app_start("/app/terminal", "-topleft");
[73d8600]529 }
[593e023]530 }
[1382446]531#endif
[593e023]532 rc = console(HID_INPUT, HID_OUTPUT);
533 if (rc == EOK) {
534 getterm("term/vc0", "/app/bdsh", true);
535 getterm("term/vc1", "/app/bdsh", false);
536 getterm("term/vc2", "/app/bdsh", false);
537 getterm("term/vc3", "/app/bdsh", false);
538 getterm("term/vc4", "/app/bdsh", false);
539 getterm("term/vc5", "/app/bdsh", false);
[6d5e378]540 }
[a35b458]541
[3eddaff]542 return 0;
543}
[b2951e2]544
545/** @}
546 */
Note: See TracBrowser for help on using the repository browser.