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