| 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 <vfs/vfs.h>
|
|---|
| 40 | #include <bool.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <fcntl.h>
|
|---|
| 43 | #include <sys/stat.h>
|
|---|
| 44 | #include <task.h>
|
|---|
| 45 | #include <malloc.h>
|
|---|
| 46 | #include <macros.h>
|
|---|
| 47 | #include <str.h>
|
|---|
| 48 | #include <loc.h>
|
|---|
| 49 | #include <str_error.h>
|
|---|
| 50 | #include "init.h"
|
|---|
| 51 |
|
|---|
| 52 | #define ROOT_DEVICE "bd/initrd"
|
|---|
| 53 | #define ROOT_MOUNT_POINT "/"
|
|---|
| 54 |
|
|---|
| 55 | #define LOCFS_FS_TYPE "locfs"
|
|---|
| 56 | #define LOCFS_MOUNT_POINT "/loc"
|
|---|
| 57 |
|
|---|
| 58 | #define TMPFS_FS_TYPE "tmpfs"
|
|---|
| 59 | #define TMPFS_MOUNT_POINT "/tmp"
|
|---|
| 60 |
|
|---|
| 61 | #define DATA_FS_TYPE "fat"
|
|---|
| 62 | #define DATA_DEVICE "bd/ata1disk0"
|
|---|
| 63 | #define DATA_MOUNT_POINT "/data"
|
|---|
| 64 |
|
|---|
| 65 | #define SRV_CONSOLE "/srv/console"
|
|---|
| 66 | #define APP_GETTERM "/app/getterm"
|
|---|
| 67 |
|
|---|
| 68 | /** Print banner */
|
|---|
| 69 | static void info_print(void)
|
|---|
| 70 | {
|
|---|
| 71 | printf("%s: HelenOS init\n", NAME);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /** Report mount operation success */
|
|---|
| 75 | static bool mount_report(const char *desc, const char *mntpt,
|
|---|
| 76 | const char *fstype, const char *dev, int rc)
|
|---|
| 77 | {
|
|---|
| 78 | switch (rc) {
|
|---|
| 79 | case EOK:
|
|---|
| 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);
|
|---|
| 85 | break;
|
|---|
| 86 | case EBUSY:
|
|---|
| 87 | printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
|
|---|
| 88 | return false;
|
|---|
| 89 | case ELIMIT:
|
|---|
| 90 | printf("%s: %s limit exceeded\n", NAME, desc);
|
|---|
| 91 | return false;
|
|---|
| 92 | case ENOENT:
|
|---|
| 93 | printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
|
|---|
| 94 | return false;
|
|---|
| 95 | default:
|
|---|
| 96 | printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
|
|---|
| 97 | str_error(rc));
|
|---|
| 98 | return false;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | return true;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 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 | */
|
|---|
| 115 | static bool mount_root(const char *fstype)
|
|---|
| 116 | {
|
|---|
| 117 | const char *opts = "";
|
|---|
| 118 |
|
|---|
| 119 | if (str_cmp(fstype, "tmpfs") == 0)
|
|---|
| 120 | opts = "restore";
|
|---|
| 121 |
|
|---|
| 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 |
|
|---|
| 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 | */
|
|---|
| 137 | static bool mount_locfs(void)
|
|---|
| 138 | {
|
|---|
| 139 | int rc = mount(LOCFS_FS_TYPE, LOCFS_MOUNT_POINT, "", "",
|
|---|
| 140 | IPC_FLAG_BLOCKING);
|
|---|
| 141 | return mount_report("Location service filesystem", LOCFS_MOUNT_POINT,
|
|---|
| 142 | LOCFS_FS_TYPE, NULL, rc);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | static void spawn(const char *fname)
|
|---|
| 146 | {
|
|---|
| 147 | int rc;
|
|---|
| 148 | struct stat s;
|
|---|
| 149 |
|
|---|
| 150 | if (stat(fname, &s) == ENOENT)
|
|---|
| 151 | return;
|
|---|
| 152 |
|
|---|
| 153 | printf("%s: Spawning %s\n", NAME, fname);
|
|---|
| 154 | rc = task_spawnl(NULL, fname, fname, NULL);
|
|---|
| 155 | if (rc != EOK) {
|
|---|
| 156 | printf("%s: Error spawning %s (%s)\n", NAME, fname,
|
|---|
| 157 | str_error(rc));
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | static void srv_start(const char *fname)
|
|---|
| 162 | {
|
|---|
| 163 | task_id_t id;
|
|---|
| 164 | task_exit_t texit;
|
|---|
| 165 | int rc, retval;
|
|---|
| 166 | struct stat s;
|
|---|
| 167 |
|
|---|
| 168 | if (stat(fname, &s) == ENOENT)
|
|---|
| 169 | return;
|
|---|
| 170 |
|
|---|
| 171 | printf("%s: Starting %s\n", NAME, fname);
|
|---|
| 172 | rc = task_spawnl(&id, fname, fname, NULL);
|
|---|
| 173 | if (!id) {
|
|---|
| 174 | printf("%s: Error spawning %s (%s)\n", NAME, fname,
|
|---|
| 175 | str_error(rc));
|
|---|
| 176 | return;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | rc = task_wait(id, &texit, &retval);
|
|---|
| 180 | if (rc != EOK) {
|
|---|
| 181 | printf("%s: Error waiting for %s (%s)\n", NAME, fname,
|
|---|
| 182 | str_error(rc));
|
|---|
| 183 | return;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 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);
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | static void console(const char *svc)
|
|---|
| 199 | {
|
|---|
| 200 | printf("%s: Spawning %s %s\n", NAME, SRV_CONSOLE, svc);
|
|---|
| 201 |
|
|---|
| 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);
|
|---|
| 205 | if (rc != EOK) {
|
|---|
| 206 | printf("%s: Error waiting on %s (%s)\n", NAME, svc,
|
|---|
| 207 | str_error(rc));
|
|---|
| 208 | return;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | rc = task_spawnl(NULL, SRV_CONSOLE, SRV_CONSOLE, svc, NULL);
|
|---|
| 212 | if (rc != EOK) {
|
|---|
| 213 | printf("%s: Error spawning %s %s (%s)\n", NAME, SRV_CONSOLE,
|
|---|
| 214 | svc, str_error(rc));
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | static void getterm(const char *svc, const char *app, bool wmsg)
|
|---|
| 219 | {
|
|---|
| 220 | char term[LOC_NAME_MAXLEN];
|
|---|
| 221 | int rc;
|
|---|
| 222 |
|
|---|
| 223 | snprintf(term, LOC_NAME_MAXLEN, "%s/%s", LOCFS_MOUNT_POINT, svc);
|
|---|
| 224 |
|
|---|
| 225 | printf("%s: Spawning %s %s %s\n", NAME, APP_GETTERM, term, app);
|
|---|
| 226 |
|
|---|
| 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);
|
|---|
| 230 | if (rc != EOK) {
|
|---|
| 231 | printf("%s: Error waiting on %s (%s)\n", NAME, term,
|
|---|
| 232 | str_error(rc));
|
|---|
| 233 | return;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 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 | }
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | static bool mount_tmpfs(void)
|
|---|
| 254 | {
|
|---|
| 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);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | static bool mount_data(void)
|
|---|
| 261 | {
|
|---|
| 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);
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | int main(int argc, char *argv[])
|
|---|
| 268 | {
|
|---|
| 269 | info_print();
|
|---|
| 270 |
|
|---|
| 271 | if (!mount_root(STRING(RDFMT))) {
|
|---|
| 272 | printf("%s: Exiting\n", NAME);
|
|---|
| 273 | return -1;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | /* Make sure tmpfs is running. */
|
|---|
| 277 | if (str_cmp(STRING(RDFMT), "tmpfs") != 0) {
|
|---|
| 278 | spawn("/srv/tmpfs");
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | spawn("/srv/locfs");
|
|---|
| 282 | spawn("/srv/taskmon");
|
|---|
| 283 |
|
|---|
| 284 | if (!mount_locfs()) {
|
|---|
| 285 | printf("%s: Exiting\n", NAME);
|
|---|
| 286 | return -2;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | mount_tmpfs();
|
|---|
| 290 |
|
|---|
| 291 | spawn("/srv/devman");
|
|---|
| 292 | spawn("/srv/apic");
|
|---|
| 293 | spawn("/srv/i8259");
|
|---|
| 294 | spawn("/srv/obio");
|
|---|
| 295 | srv_start("/srv/cuda_adb");
|
|---|
| 296 | srv_start("/srv/i8042");
|
|---|
| 297 | srv_start("/srv/s3c24ser");
|
|---|
| 298 | srv_start("/srv/s3c24ts");
|
|---|
| 299 |
|
|---|
| 300 | spawn("/srv/fb");
|
|---|
| 301 | spawn("/srv/input");
|
|---|
| 302 | console("hid/input");
|
|---|
| 303 |
|
|---|
| 304 | spawn("/srv/clip");
|
|---|
| 305 |
|
|---|
| 306 | /*
|
|---|
| 307 | * Start these synchronously so that mount_data() can be
|
|---|
| 308 | * non-blocking.
|
|---|
| 309 | */
|
|---|
| 310 | #ifdef CONFIG_START_BD
|
|---|
| 311 | srv_start("/srv/ata_bd");
|
|---|
| 312 | srv_start("/srv/gxe_bd");
|
|---|
| 313 | #else
|
|---|
| 314 | (void) srv_start;
|
|---|
| 315 | #endif
|
|---|
| 316 |
|
|---|
| 317 | #ifdef CONFIG_MOUNT_DATA
|
|---|
| 318 | /* Make sure fat is running. */
|
|---|
| 319 | if (str_cmp(STRING(RDFMT), "fat") != 0) {
|
|---|
| 320 | srv_start("/srv/fat");
|
|---|
| 321 | }
|
|---|
| 322 | mount_data();
|
|---|
| 323 | #else
|
|---|
| 324 | (void) mount_data;
|
|---|
| 325 | #endif
|
|---|
| 326 |
|
|---|
| 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);
|
|---|
| 334 |
|
|---|
| 335 | return 0;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | /** @}
|
|---|
| 339 | */
|
|---|