source: mainline/uspace/app/init/init.c@ 36b16bc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 36b16bc was 53d6ac3d, checked in by Martin Decky <martin@…>, 14 years ago

add some comments
fix a typo

  • Property mode set to 100644
File size: 8.1 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
[b2951e2]29/** @addtogroup init Init
[c91c9fb]30 * @brief Init process for user space environment configuration.
[b2951e2]31 * @{
[c91c9fb]32 */
[b2951e2]33/**
34 * @file
35 */
36
[00c4994]37#include <stdio.h>
[860271d4]38#include <unistd.h>
39#include <vfs/vfs.h>
40#include <bool.h>
41#include <errno.h>
42#include <fcntl.h>
[1757ffce]43#include <sys/stat.h>
[860271d4]44#include <task.h>
45#include <malloc.h>
[c91c9fb]46#include <macros.h>
[19f857a]47#include <str.h>
[15f3c3f]48#include <loc.h>
[d9fae235]49#include <str_error.h>
[860271d4]50#include "init.h"
51
[d9fae235]52#define ROOT_DEVICE "bd/initrd"
53#define ROOT_MOUNT_POINT "/"
54
[15f3c3f]55#define LOCFS_FS_TYPE "locfs"
56#define LOCFS_MOUNT_POINT "/loc"
[47a350f]57
[1ab0852]58#define TMPFS_FS_TYPE "tmpfs"
59#define TMPFS_MOUNT_POINT "/tmp"
[d9fae235]60
61#define DATA_FS_TYPE "fat"
[e092dc5]62#define DATA_DEVICE "bd/ata1disk0"
[d9fae235]63#define DATA_MOUNT_POINT "/data"
64
[47a350f]65#define SRV_CONSOLE "/srv/console"
[df747bd8]66#define APP_GETTERM "/app/getterm"
[47a350f]67
[53d6ac3d]68/** Print banner */
[36b8100a]69static void info_print(void)
70{
[d9fae235]71 printf("%s: HelenOS init\n", NAME);
[36b8100a]72}
73
[53d6ac3d]74/** Report mount operation success */
[d9fae235]75static bool mount_report(const char *desc, const char *mntpt,
76 const char *fstype, const char *dev, int rc)
[860271d4]77{
[f49cf64]78 switch (rc) {
79 case EOK:
[d9fae235]80 if (dev != NULL)
81 printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
82 fstype, dev);
83 else
84 printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
[f49cf64]85 break;
86 case EBUSY:
[d9fae235]87 printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
[f49cf64]88 return false;
89 case ELIMIT:
[d9fae235]90 printf("%s: %s limit exceeded\n", NAME, desc);
[f49cf64]91 return false;
92 case ENOENT:
[d9fae235]93 printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
[f49cf64]94 return false;
95 default:
[d9fae235]96 printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
97 str_error(rc));
[f49cf64]98 return false;
[860271d4]99 }
100
101 return true;
102}
103
[53d6ac3d]104/** Mount root filesystem
105 *
106 * The operation blocks until the root filesystem
107 * server is ready for mounting.
108 *
109 * @param[in] fstype Root filesystem type.
110 *
111 * @return True on success.
112 * @return False on failure.
113 *
114 */
[d9fae235]115static bool mount_root(const char *fstype)
[a095d20]116{
[d9fae235]117 const char *opts = "";
[f49cf64]118
[d9fae235]119 if (str_cmp(fstype, "tmpfs") == 0)
120 opts = "restore";
[a095d20]121
[d9fae235]122 int rc = mount(fstype, ROOT_MOUNT_POINT, ROOT_DEVICE, opts,
123 IPC_FLAG_BLOCKING);
124 return mount_report("Root filesystem", ROOT_MOUNT_POINT, fstype,
125 ROOT_DEVICE, rc);
126}
127
[53d6ac3d]128/** Mount locfs filesystem
129 *
130 * The operation blocks until the locfs filesystem
131 * server is ready for mounting.
132 *
133 * @return True on success.
134 * @return False on failure.
135 *
136 */
[15f3c3f]137static bool mount_locfs(void)
[d9fae235]138{
[15f3c3f]139 int rc = mount(LOCFS_FS_TYPE, LOCFS_MOUNT_POINT, "", "",
[d9fae235]140 IPC_FLAG_BLOCKING);
[15f3c3f]141 return mount_report("Location service filesystem", LOCFS_MOUNT_POINT,
142 LOCFS_FS_TYPE, NULL, rc);
[a095d20]143}
144
[a000878c]145static void spawn(const char *fname)
[860271d4]146{
[0485135]147 int rc;
[1757ffce]148 struct stat s;
149
150 if (stat(fname, &s) == ENOENT)
151 return;
[c91c9fb]152
[d9fae235]153 printf("%s: Spawning %s\n", NAME, fname);
[0485135]154 rc = task_spawnl(NULL, fname, fname, NULL);
155 if (rc != EOK) {
[d9fae235]156 printf("%s: Error spawning %s (%s)\n", NAME, fname,
[0485135]157 str_error(rc));
158 }
[b27a97bb]159}
160
[a000878c]161static void srv_start(const char *fname)
[95bc57c]162{
163 task_id_t id;
164 task_exit_t texit;
165 int rc, retval;
[1757ffce]166 struct stat s;
167
168 if (stat(fname, &s) == ENOENT)
169 return;
[95bc57c]170
[d9fae235]171 printf("%s: Starting %s\n", NAME, fname);
[0485135]172 rc = task_spawnl(&id, fname, fname, NULL);
[95bc57c]173 if (!id) {
[d9fae235]174 printf("%s: Error spawning %s (%s)\n", NAME, fname,
[0485135]175 str_error(rc));
[95bc57c]176 return;
177 }
[d9fae235]178
[95bc57c]179 rc = task_wait(id, &texit, &retval);
180 if (rc != EOK) {
[53d6ac3d]181 printf("%s: Error waiting for %s (%s)\n", NAME, fname,
[0485135]182 str_error(rc));
[95bc57c]183 return;
184 }
[d9fae235]185
[0485135]186 if (texit != TASK_EXIT_NORMAL) {
187 printf("%s: Server %s failed to start (unexpectedly "
188 "terminated)\n", NAME, fname);
189 return;
190 }
191
192 if (retval != 0) {
193 printf("%s: Server %s failed to start (exit code %d)\n", NAME,
194 fname, retval);
[95bc57c]195 }
196}
197
[15f3c3f]198static void console(const char *svc)
[47a350f]199{
[15f3c3f]200 printf("%s: Spawning %s %s\n", NAME, SRV_CONSOLE, svc);
[47a350f]201
[15f3c3f]202 /* Wait for the input service to be ready */
203 service_id_t service_id;
204 int rc = loc_service_get_id(svc, &service_id, IPC_FLAG_BLOCKING);
[0485135]205 if (rc != EOK) {
[15f3c3f]206 printf("%s: Error waiting on %s (%s)\n", NAME, svc,
[d9fae235]207 str_error(rc));
[0485135]208 return;
209 }
210
[15f3c3f]211 rc = task_spawnl(NULL, SRV_CONSOLE, SRV_CONSOLE, svc, NULL);
[0485135]212 if (rc != EOK) {
213 printf("%s: Error spawning %s %s (%s)\n", NAME, SRV_CONSOLE,
[15f3c3f]214 svc, str_error(rc));
[0485135]215 }
[47a350f]216}
217
[15f3c3f]218static void getterm(const char *svc, const char *app, bool wmsg)
[36b8100a]219{
[15f3c3f]220 char term[LOC_NAME_MAXLEN];
[62140db]221 int rc;
[36b8100a]222
[15f3c3f]223 snprintf(term, LOC_NAME_MAXLEN, "%s/%s", LOCFS_MOUNT_POINT, svc);
[36b8100a]224
[d9fae235]225 printf("%s: Spawning %s %s %s\n", NAME, APP_GETTERM, term, app);
[36b8100a]226
[15f3c3f]227 /* Wait for the terminal service to be ready */
228 service_id_t service_id;
229 rc = loc_service_get_id(svc, &service_id, IPC_FLAG_BLOCKING);
[0485135]230 if (rc != EOK) {
[d9fae235]231 printf("%s: Error waiting on %s (%s)\n", NAME, term,
232 str_error(rc));
[0485135]233 return;
234 }
235
[4deb8b5]236 if (wmsg) {
237 rc = task_spawnl(NULL, APP_GETTERM, APP_GETTERM, "-w", term,
238 app, NULL);
239 if (rc != EOK) {
240 printf("%s: Error spawning %s -w %s %s (%s)\n", NAME,
241 APP_GETTERM, term, app, str_error(rc));
242 }
243 } else {
244 rc = task_spawnl(NULL, APP_GETTERM, APP_GETTERM, term, app,
245 NULL);
246 if (rc != EOK) {
247 printf("%s: Error spawning %s %s %s (%s)\n", NAME,
248 APP_GETTERM, term, app, str_error(rc));
249 }
[0485135]250 }
[36b8100a]251}
252
[1ab0852]253static bool mount_tmpfs(void)
[03333bc]254{
[1ab0852]255 int rc = mount(TMPFS_FS_TYPE, TMPFS_MOUNT_POINT, "", "", 0);
256 return mount_report("Temporary filesystem", TMPFS_MOUNT_POINT,
257 TMPFS_FS_TYPE, NULL, rc);
[03333bc]258}
259
[d9fae235]260static bool mount_data(void)
[00fe6bb]261{
[d9fae235]262 int rc = mount(DATA_FS_TYPE, DATA_MOUNT_POINT, DATA_DEVICE, "wtcache", 0);
263 return mount_report("Data filesystem", DATA_MOUNT_POINT, DATA_FS_TYPE,
264 DATA_DEVICE, rc);
[00fe6bb]265}
266
[5106e98]267int main(int argc, char *argv[])
268{
[860271d4]269 info_print();
270
[36b8100a]271 if (!mount_root(STRING(RDFMT))) {
[d9fae235]272 printf("%s: Exiting\n", NAME);
[860271d4]273 return -1;
274 }
[d9fae235]275
[03333bc]276 /* Make sure tmpfs is running. */
277 if (str_cmp(STRING(RDFMT), "tmpfs") != 0) {
278 spawn("/srv/tmpfs");
279 }
[860271d4]280
[15f3c3f]281 spawn("/srv/locfs");
[a074b4f]282 spawn("/srv/taskmon");
[a095d20]283
[15f3c3f]284 if (!mount_locfs()) {
[d9fae235]285 printf("%s: Exiting\n", NAME);
[a095d20]286 return -2;
287 }
[d9fae235]288
[1ab0852]289 mount_tmpfs();
[a095d20]290
[3acb285a]291 spawn("/srv/devman");
[acc7ce4]292 spawn("/srv/apic");
293 spawn("/srv/i8259");
[bb2dbf8]294 spawn("/srv/obio");
[3a2f8aa]295 srv_start("/srv/cuda_adb");
[9f51afc]296 srv_start("/srv/i8042");
[a9b5b5f]297 srv_start("/srv/s3c24ser");
[527298a]298 srv_start("/srv/s3c24ts");
[d9fae235]299
[de9c5cb]300 spawn("/srv/fb");
[5f88293]301 spawn("/srv/input");
[1875a0c]302 console("hid/input");
[47a350f]303
[fb623e2]304 spawn("/srv/clip");
[d9fae235]305
[95bc57c]306 /*
307 * Start these synchronously so that mount_data() can be
308 * non-blocking.
309 */
[1641eb0]310#ifdef CONFIG_START_BD
[95bc57c]311 srv_start("/srv/ata_bd");
312 srv_start("/srv/gxe_bd");
[f019cc07]313#else
314 (void) srv_start;
315#endif
[d9fae235]316
[1641eb0]317#ifdef CONFIG_MOUNT_DATA
[1bfae13]318 /* Make sure fat is running. */
319 if (str_cmp(STRING(RDFMT), "fat") != 0) {
320 srv_start("/srv/fat");
321 }
[f49cf64]322 mount_data();
[f019cc07]323#else
324 (void) mount_data;
[1641eb0]325#endif
[d9fae235]326
[4deb8b5]327 getterm("term/vc0", "/app/bdsh", true);
328 getterm("term/vc1", "/app/bdsh", false);
329 getterm("term/vc2", "/app/bdsh", false);
330 getterm("term/vc3", "/app/bdsh", false);
331 getterm("term/vc4", "/app/bdsh", false);
332 getterm("term/vc5", "/app/bdsh", false);
333 getterm("term/vc6", "/app/klog", false);
[d9fae235]334
[3eddaff]335 return 0;
336}
[b2951e2]337
338/** @}
339 */
Note: See TracBrowser for help on using the repository browser.