source: mainline/uspace/app/init/init.c@ 7b1ae09

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7b1ae09 was 8fefd8b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Preserve srv directory structure

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