source: mainline/uspace/app/init/init.c@ 901b302

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 901b302 was a5c7b865, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Start task bar by default

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