[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>
|
---|
[c91c9fb] | 39 | #include <ipc/ipc.h>
|
---|
[860271d4] | 40 | #include <vfs/vfs.h>
|
---|
| 41 | #include <bool.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <fcntl.h>
|
---|
[1757ffce] | 44 | #include <sys/stat.h>
|
---|
[860271d4] | 45 | #include <task.h>
|
---|
| 46 | #include <malloc.h>
|
---|
[c91c9fb] | 47 | #include <macros.h>
|
---|
[19f857a] | 48 | #include <str.h>
|
---|
[36b8100a] | 49 | #include <devmap.h>
|
---|
[d9fae235] | 50 | #include <str_error.h>
|
---|
[860271d4] | 51 | #include "init.h"
|
---|
| 52 |
|
---|
[d9fae235] | 53 | #define ROOT_DEVICE "bd/initrd"
|
---|
| 54 | #define ROOT_MOUNT_POINT "/"
|
---|
| 55 |
|
---|
| 56 | #define DEVFS_FS_TYPE "devfs"
|
---|
[47a350f] | 57 | #define DEVFS_MOUNT_POINT "/dev"
|
---|
| 58 |
|
---|
[d9fae235] | 59 | #define SCRATCH_FS_TYPE "tmpfs"
|
---|
| 60 | #define SCRATCH_MOUNT_POINT "/scratch"
|
---|
| 61 |
|
---|
| 62 | #define DATA_FS_TYPE "fat"
|
---|
| 63 | #define DATA_DEVICE "bd/disk0"
|
---|
| 64 | #define DATA_MOUNT_POINT "/data"
|
---|
| 65 |
|
---|
[47a350f] | 66 | #define SRV_CONSOLE "/srv/console"
|
---|
[df747bd8] | 67 | #define APP_GETTERM "/app/getterm"
|
---|
[47a350f] | 68 |
|
---|
[36b8100a] | 69 | static void info_print(void)
|
---|
| 70 | {
|
---|
[d9fae235] | 71 | printf("%s: HelenOS init\n", NAME);
|
---|
[36b8100a] | 72 | }
|
---|
| 73 |
|
---|
[d9fae235] | 74 | static bool mount_report(const char *desc, const char *mntpt,
|
---|
| 75 | const char *fstype, const char *dev, int rc)
|
---|
[860271d4] | 76 | {
|
---|
[f49cf64] | 77 | switch (rc) {
|
---|
| 78 | case EOK:
|
---|
[d9fae235] | 79 | if (dev != NULL)
|
---|
| 80 | printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
|
---|
| 81 | fstype, dev);
|
---|
| 82 | else
|
---|
| 83 | printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
|
---|
[f49cf64] | 84 | break;
|
---|
| 85 | case EBUSY:
|
---|
[d9fae235] | 86 | printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
|
---|
[f49cf64] | 87 | return false;
|
---|
| 88 | case ELIMIT:
|
---|
[d9fae235] | 89 | printf("%s: %s limit exceeded\n", NAME, desc);
|
---|
[f49cf64] | 90 | return false;
|
---|
| 91 | case ENOENT:
|
---|
[d9fae235] | 92 | printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
|
---|
[f49cf64] | 93 | return false;
|
---|
| 94 | default:
|
---|
[d9fae235] | 95 | printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
|
---|
| 96 | str_error(rc));
|
---|
[f49cf64] | 97 | return false;
|
---|
[860271d4] | 98 | }
|
---|
| 99 |
|
---|
| 100 | return true;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[d9fae235] | 103 | static bool mount_root(const char *fstype)
|
---|
[a095d20] | 104 | {
|
---|
[d9fae235] | 105 | const char *opts = "";
|
---|
[f49cf64] | 106 |
|
---|
[d9fae235] | 107 | if (str_cmp(fstype, "tmpfs") == 0)
|
---|
| 108 | opts = "restore";
|
---|
[a095d20] | 109 |
|
---|
[d9fae235] | 110 | int rc = mount(fstype, ROOT_MOUNT_POINT, ROOT_DEVICE, opts,
|
---|
| 111 | IPC_FLAG_BLOCKING);
|
---|
| 112 | return mount_report("Root filesystem", ROOT_MOUNT_POINT, fstype,
|
---|
| 113 | ROOT_DEVICE, rc);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | static bool mount_devfs(void)
|
---|
| 117 | {
|
---|
| 118 | int rc = mount(DEVFS_FS_TYPE, DEVFS_MOUNT_POINT, "", "",
|
---|
| 119 | IPC_FLAG_BLOCKING);
|
---|
| 120 | return mount_report("Device filesystem", DEVFS_MOUNT_POINT, DEVFS_FS_TYPE,
|
---|
| 121 | NULL, rc);
|
---|
[a095d20] | 122 | }
|
---|
| 123 |
|
---|
[a000878c] | 124 | static void spawn(const char *fname)
|
---|
[860271d4] | 125 | {
|
---|
[0485135] | 126 | int rc;
|
---|
[1757ffce] | 127 | struct stat s;
|
---|
| 128 |
|
---|
| 129 | if (stat(fname, &s) == ENOENT)
|
---|
| 130 | return;
|
---|
[c91c9fb] | 131 |
|
---|
[d9fae235] | 132 | printf("%s: Spawning %s\n", NAME, fname);
|
---|
[0485135] | 133 | rc = task_spawnl(NULL, fname, fname, NULL);
|
---|
| 134 | if (rc != EOK) {
|
---|
[d9fae235] | 135 | printf("%s: Error spawning %s (%s)\n", NAME, fname,
|
---|
[0485135] | 136 | str_error(rc));
|
---|
| 137 | }
|
---|
[b27a97bb] | 138 | }
|
---|
| 139 |
|
---|
[a000878c] | 140 | static void srv_start(const char *fname)
|
---|
[95bc57c] | 141 | {
|
---|
| 142 | task_id_t id;
|
---|
| 143 | task_exit_t texit;
|
---|
| 144 | int rc, retval;
|
---|
[1757ffce] | 145 | struct stat s;
|
---|
| 146 |
|
---|
| 147 | if (stat(fname, &s) == ENOENT)
|
---|
| 148 | return;
|
---|
[95bc57c] | 149 |
|
---|
[d9fae235] | 150 | printf("%s: Starting %s\n", NAME, fname);
|
---|
[0485135] | 151 | rc = task_spawnl(&id, fname, fname, NULL);
|
---|
[95bc57c] | 152 | if (!id) {
|
---|
[d9fae235] | 153 | printf("%s: Error spawning %s (%s)\n", NAME, fname,
|
---|
[0485135] | 154 | str_error(rc));
|
---|
[95bc57c] | 155 | return;
|
---|
| 156 | }
|
---|
[d9fae235] | 157 |
|
---|
[95bc57c] | 158 | rc = task_wait(id, &texit, &retval);
|
---|
| 159 | if (rc != EOK) {
|
---|
[d9fae235] | 160 | printf("%s: Error waiting for %s (%s(\n", NAME, fname,
|
---|
[0485135] | 161 | str_error(rc));
|
---|
[95bc57c] | 162 | return;
|
---|
| 163 | }
|
---|
[d9fae235] | 164 |
|
---|
[0485135] | 165 | if (texit != TASK_EXIT_NORMAL) {
|
---|
| 166 | printf("%s: Server %s failed to start (unexpectedly "
|
---|
| 167 | "terminated)\n", NAME, fname);
|
---|
| 168 | return;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | if (retval != 0) {
|
---|
| 172 | printf("%s: Server %s failed to start (exit code %d)\n", NAME,
|
---|
| 173 | fname, retval);
|
---|
[95bc57c] | 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[a000878c] | 177 | static void console(const char *dev)
|
---|
[47a350f] | 178 | {
|
---|
[28be7fa] | 179 | char hid_in[DEVMAP_NAME_MAXLEN];
|
---|
[47a350f] | 180 | int rc;
|
---|
| 181 |
|
---|
[28be7fa] | 182 | snprintf(hid_in, DEVMAP_NAME_MAXLEN, "%s/%s", DEVFS_MOUNT_POINT, dev);
|
---|
[47a350f] | 183 |
|
---|
[d9fae235] | 184 | printf("%s: Spawning %s %s\n", NAME, SRV_CONSOLE, hid_in);
|
---|
[47a350f] | 185 |
|
---|
| 186 | /* Wait for the input device to be ready */
|
---|
| 187 | dev_handle_t handle;
|
---|
| 188 | rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING);
|
---|
[0485135] | 189 | if (rc != EOK) {
|
---|
[d9fae235] | 190 | printf("%s: Error waiting on %s (%s)\n", NAME, hid_in,
|
---|
| 191 | str_error(rc));
|
---|
[0485135] | 192 | return;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | rc = task_spawnl(NULL, SRV_CONSOLE, SRV_CONSOLE, hid_in, NULL);
|
---|
| 196 | if (rc != EOK) {
|
---|
| 197 | printf("%s: Error spawning %s %s (%s)\n", NAME, SRV_CONSOLE,
|
---|
| 198 | hid_in, str_error(rc));
|
---|
| 199 | }
|
---|
[47a350f] | 200 | }
|
---|
| 201 |
|
---|
[4deb8b5] | 202 | static void getterm(const char *dev, const char *app, bool wmsg)
|
---|
[36b8100a] | 203 | {
|
---|
[28be7fa] | 204 | char term[DEVMAP_NAME_MAXLEN];
|
---|
[62140db] | 205 | int rc;
|
---|
[36b8100a] | 206 |
|
---|
[28be7fa] | 207 | snprintf(term, DEVMAP_NAME_MAXLEN, "%s/%s", DEVFS_MOUNT_POINT, dev);
|
---|
[36b8100a] | 208 |
|
---|
[d9fae235] | 209 | printf("%s: Spawning %s %s %s\n", NAME, APP_GETTERM, term, app);
|
---|
[36b8100a] | 210 |
|
---|
[47a350f] | 211 | /* Wait for the terminal device to be ready */
|
---|
[36b8100a] | 212 | dev_handle_t handle;
|
---|
[62140db] | 213 | rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING);
|
---|
[0485135] | 214 | if (rc != EOK) {
|
---|
[d9fae235] | 215 | printf("%s: Error waiting on %s (%s)\n", NAME, term,
|
---|
| 216 | str_error(rc));
|
---|
[0485135] | 217 | return;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[4deb8b5] | 220 | if (wmsg) {
|
---|
| 221 | rc = task_spawnl(NULL, APP_GETTERM, APP_GETTERM, "-w", term,
|
---|
| 222 | app, NULL);
|
---|
| 223 | if (rc != EOK) {
|
---|
| 224 | printf("%s: Error spawning %s -w %s %s (%s)\n", NAME,
|
---|
| 225 | APP_GETTERM, term, app, str_error(rc));
|
---|
| 226 | }
|
---|
| 227 | } else {
|
---|
| 228 | rc = task_spawnl(NULL, APP_GETTERM, APP_GETTERM, term, app,
|
---|
| 229 | NULL);
|
---|
| 230 | if (rc != EOK) {
|
---|
| 231 | printf("%s: Error spawning %s %s %s (%s)\n", NAME,
|
---|
| 232 | APP_GETTERM, term, app, str_error(rc));
|
---|
| 233 | }
|
---|
[0485135] | 234 | }
|
---|
[36b8100a] | 235 | }
|
---|
| 236 |
|
---|
[d9fae235] | 237 | static bool mount_scratch(void)
|
---|
[03333bc] | 238 | {
|
---|
[d9fae235] | 239 | int rc = mount(SCRATCH_FS_TYPE, SCRATCH_MOUNT_POINT, "", "", 0);
|
---|
| 240 | return mount_report("Scratch filesystem", SCRATCH_MOUNT_POINT,
|
---|
| 241 | SCRATCH_FS_TYPE, NULL, rc);
|
---|
[03333bc] | 242 | }
|
---|
| 243 |
|
---|
[d9fae235] | 244 | static bool mount_data(void)
|
---|
[00fe6bb] | 245 | {
|
---|
[d9fae235] | 246 | int rc = mount(DATA_FS_TYPE, DATA_MOUNT_POINT, DATA_DEVICE, "wtcache", 0);
|
---|
| 247 | return mount_report("Data filesystem", DATA_MOUNT_POINT, DATA_FS_TYPE,
|
---|
| 248 | DATA_DEVICE, rc);
|
---|
[00fe6bb] | 249 | }
|
---|
| 250 |
|
---|
[5106e98] | 251 | int main(int argc, char *argv[])
|
---|
| 252 | {
|
---|
[860271d4] | 253 | info_print();
|
---|
| 254 |
|
---|
[36b8100a] | 255 | if (!mount_root(STRING(RDFMT))) {
|
---|
[d9fae235] | 256 | printf("%s: Exiting\n", NAME);
|
---|
[860271d4] | 257 | return -1;
|
---|
| 258 | }
|
---|
[d9fae235] | 259 |
|
---|
[03333bc] | 260 | /* Make sure tmpfs is running. */
|
---|
| 261 | if (str_cmp(STRING(RDFMT), "tmpfs") != 0) {
|
---|
| 262 | spawn("/srv/tmpfs");
|
---|
| 263 | }
|
---|
[860271d4] | 264 |
|
---|
[a095d20] | 265 | spawn("/srv/devfs");
|
---|
[a074b4f] | 266 | spawn("/srv/taskmon");
|
---|
[a095d20] | 267 |
|
---|
| 268 | if (!mount_devfs()) {
|
---|
[d9fae235] | 269 | printf("%s: Exiting\n", NAME);
|
---|
[a095d20] | 270 | return -2;
|
---|
| 271 | }
|
---|
[d9fae235] | 272 |
|
---|
[03333bc] | 273 | mount_scratch();
|
---|
[a095d20] | 274 |
|
---|
[bb2dbf8] | 275 | spawn("/srv/fhc");
|
---|
| 276 | spawn("/srv/obio");
|
---|
[3a2f8aa] | 277 | srv_start("/srv/cuda_adb");
|
---|
[9f51afc] | 278 | srv_start("/srv/i8042");
|
---|
[a9b5b5f] | 279 | srv_start("/srv/s3c24ser");
|
---|
[b73c26d] | 280 | srv_start("/srv/adb_ms");
|
---|
| 281 | srv_start("/srv/char_ms");
|
---|
[527298a] | 282 | srv_start("/srv/s3c24ts");
|
---|
[d9fae235] | 283 |
|
---|
[de9c5cb] | 284 | spawn("/srv/fb");
|
---|
| 285 | spawn("/srv/kbd");
|
---|
[47a350f] | 286 | console("hid_in/kbd");
|
---|
| 287 |
|
---|
[fb623e2] | 288 | spawn("/srv/clip");
|
---|
[d9fae235] | 289 |
|
---|
[95bc57c] | 290 | /*
|
---|
| 291 | * Start these synchronously so that mount_data() can be
|
---|
| 292 | * non-blocking.
|
---|
| 293 | */
|
---|
[1641eb0] | 294 | #ifdef CONFIG_START_BD
|
---|
[95bc57c] | 295 | srv_start("/srv/ata_bd");
|
---|
| 296 | srv_start("/srv/gxe_bd");
|
---|
[f019cc07] | 297 | #else
|
---|
| 298 | (void) srv_start;
|
---|
| 299 | #endif
|
---|
[d9fae235] | 300 |
|
---|
[1641eb0] | 301 | #ifdef CONFIG_MOUNT_DATA
|
---|
[f49cf64] | 302 | mount_data();
|
---|
[f019cc07] | 303 | #else
|
---|
| 304 | (void) mount_data;
|
---|
[1641eb0] | 305 | #endif
|
---|
[d9fae235] | 306 |
|
---|
[4deb8b5] | 307 | getterm("term/vc0", "/app/bdsh", true);
|
---|
| 308 | getterm("term/vc1", "/app/bdsh", false);
|
---|
| 309 | getterm("term/vc2", "/app/bdsh", false);
|
---|
| 310 | getterm("term/vc3", "/app/bdsh", false);
|
---|
| 311 | getterm("term/vc4", "/app/bdsh", false);
|
---|
| 312 | getterm("term/vc5", "/app/bdsh", false);
|
---|
| 313 | getterm("term/vc6", "/app/klog", false);
|
---|
[d9fae235] | 314 |
|
---|
[3eddaff] | 315 | return 0;
|
---|
| 316 | }
|
---|
[b2951e2] | 317 |
|
---|
| 318 | /** @}
|
---|
| 319 | */
|
---|