[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>
|
---|
[6d5e378] | 39 | #include <stdarg.h>
|
---|
[860271d4] | 40 | #include <vfs/vfs.h>
|
---|
[3e6a98c5] | 41 | #include <stdbool.h>
|
---|
[860271d4] | 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>
|
---|
[15f3c3f] | 49 | #include <loc.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 |
|
---|
[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 |
|
---|
[47a350f] | 62 | #define SRV_CONSOLE "/srv/console"
|
---|
[df747bd8] | 63 | #define APP_GETTERM "/app/getterm"
|
---|
[47a350f] | 64 |
|
---|
[6d5e378] | 65 | #define SRV_COMPOSITOR "/srv/compositor"
|
---|
| 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 |
|
---|
[53d6ac3d] | 74 | /** Print banner */
|
---|
[36b8100a] | 75 | static void info_print(void)
|
---|
| 76 | {
|
---|
[d9fae235] | 77 | printf("%s: HelenOS init\n", NAME);
|
---|
[36b8100a] | 78 | }
|
---|
| 79 |
|
---|
[53d6ac3d] | 80 | /** Report mount operation success */
|
---|
[d9fae235] | 81 | static bool mount_report(const char *desc, const char *mntpt,
|
---|
| 82 | const char *fstype, const char *dev, int rc)
|
---|
[860271d4] | 83 | {
|
---|
[f49cf64] | 84 | switch (rc) {
|
---|
| 85 | case EOK:
|
---|
[d9fae235] | 86 | if (dev != NULL)
|
---|
| 87 | printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
|
---|
| 88 | fstype, dev);
|
---|
| 89 | else
|
---|
| 90 | printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
|
---|
[f49cf64] | 91 | break;
|
---|
| 92 | case EBUSY:
|
---|
[d9fae235] | 93 | printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
|
---|
[f49cf64] | 94 | return false;
|
---|
| 95 | case ELIMIT:
|
---|
[d9fae235] | 96 | printf("%s: %s limit exceeded\n", NAME, desc);
|
---|
[f49cf64] | 97 | return false;
|
---|
| 98 | case ENOENT:
|
---|
[d9fae235] | 99 | printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
|
---|
[f49cf64] | 100 | return false;
|
---|
| 101 | default:
|
---|
[d9fae235] | 102 | printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
|
---|
| 103 | str_error(rc));
|
---|
[f49cf64] | 104 | return false;
|
---|
[860271d4] | 105 | }
|
---|
| 106 |
|
---|
| 107 | return true;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[53d6ac3d] | 110 | /** Mount root filesystem
|
---|
| 111 | *
|
---|
| 112 | * The operation blocks until the root filesystem
|
---|
| 113 | * server is ready for mounting.
|
---|
| 114 | *
|
---|
| 115 | * @param[in] fstype Root filesystem type.
|
---|
| 116 | *
|
---|
| 117 | * @return True on success.
|
---|
| 118 | * @return False on failure.
|
---|
| 119 | *
|
---|
| 120 | */
|
---|
[d9fae235] | 121 | static bool mount_root(const char *fstype)
|
---|
[a095d20] | 122 | {
|
---|
[d9fae235] | 123 | const char *opts = "";
|
---|
[f49cf64] | 124 |
|
---|
[d9fae235] | 125 | if (str_cmp(fstype, "tmpfs") == 0)
|
---|
| 126 | opts = "restore";
|
---|
[a095d20] | 127 |
|
---|
[d9fae235] | 128 | int rc = mount(fstype, ROOT_MOUNT_POINT, ROOT_DEVICE, opts,
|
---|
[4979403] | 129 | IPC_FLAG_BLOCKING, 0);
|
---|
[d9fae235] | 130 | return mount_report("Root filesystem", ROOT_MOUNT_POINT, fstype,
|
---|
| 131 | ROOT_DEVICE, rc);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[53d6ac3d] | 134 | /** Mount locfs filesystem
|
---|
| 135 | *
|
---|
| 136 | * The operation blocks until the locfs filesystem
|
---|
| 137 | * server is ready for mounting.
|
---|
| 138 | *
|
---|
| 139 | * @return True on success.
|
---|
| 140 | * @return False on failure.
|
---|
| 141 | *
|
---|
| 142 | */
|
---|
[15f3c3f] | 143 | static bool mount_locfs(void)
|
---|
[d9fae235] | 144 | {
|
---|
[15f3c3f] | 145 | int rc = mount(LOCFS_FS_TYPE, LOCFS_MOUNT_POINT, "", "",
|
---|
[4979403] | 146 | IPC_FLAG_BLOCKING, 0);
|
---|
[15f3c3f] | 147 | return mount_report("Location service filesystem", LOCFS_MOUNT_POINT,
|
---|
| 148 | LOCFS_FS_TYPE, NULL, rc);
|
---|
[a095d20] | 149 | }
|
---|
| 150 |
|
---|
[6d5e378] | 151 | static int srv_startl(const char *path, ...)
|
---|
[860271d4] | 152 | {
|
---|
[1757ffce] | 153 | struct stat s;
|
---|
[6d5e378] | 154 | if (stat(path, &s) == ENOENT) {
|
---|
| 155 | printf("%s: Unable to stat %s\n", NAME, path);
|
---|
| 156 | return ENOENT;
|
---|
| 157 | }
|
---|
[1757ffce] | 158 |
|
---|
[6d5e378] | 159 | printf("%s: Starting %s\n", NAME, path);
|
---|
| 160 |
|
---|
| 161 | va_list ap;
|
---|
| 162 | const char *arg;
|
---|
| 163 | int cnt = 0;
|
---|
| 164 |
|
---|
| 165 | va_start(ap, path);
|
---|
| 166 | do {
|
---|
| 167 | arg = va_arg(ap, const char *);
|
---|
| 168 | cnt++;
|
---|
| 169 | } while (arg != NULL);
|
---|
| 170 | va_end(ap);
|
---|
| 171 |
|
---|
| 172 | va_start(ap, path);
|
---|
| 173 | task_id_t id;
|
---|
[1c635d6] | 174 | task_wait_t wait;
|
---|
| 175 | int rc = task_spawn(&id, &wait, path, cnt, ap);
|
---|
[6d5e378] | 176 | va_end(ap);
|
---|
[c91c9fb] | 177 |
|
---|
[0485135] | 178 | if (rc != EOK) {
|
---|
[6d5e378] | 179 | printf("%s: Error spawning %s (%s)\n", NAME, path,
|
---|
[0485135] | 180 | str_error(rc));
|
---|
[6d5e378] | 181 | return rc;
|
---|
[0485135] | 182 | }
|
---|
[95bc57c] | 183 |
|
---|
| 184 | if (!id) {
|
---|
[6d5e378] | 185 | printf("%s: Error spawning %s (invalid task id)\n", NAME,
|
---|
| 186 | path);
|
---|
| 187 | return EINVAL;
|
---|
[95bc57c] | 188 | }
|
---|
[d9fae235] | 189 |
|
---|
[6d5e378] | 190 | task_exit_t texit;
|
---|
| 191 | int retval;
|
---|
[1c635d6] | 192 | rc = task_wait(&wait, &texit, &retval);
|
---|
[95bc57c] | 193 | if (rc != EOK) {
|
---|
[6d5e378] | 194 | printf("%s: Error waiting for %s (%s)\n", NAME, path,
|
---|
[0485135] | 195 | str_error(rc));
|
---|
[6d5e378] | 196 | return rc;
|
---|
[95bc57c] | 197 | }
|
---|
[d9fae235] | 198 |
|
---|
[0485135] | 199 | if (texit != TASK_EXIT_NORMAL) {
|
---|
| 200 | printf("%s: Server %s failed to start (unexpectedly "
|
---|
[6d5e378] | 201 | "terminated)\n", NAME, path);
|
---|
| 202 | return EINVAL;
|
---|
[0485135] | 203 | }
|
---|
[6d5e378] | 204 |
|
---|
| 205 | if (retval != 0)
|
---|
[0485135] | 206 | printf("%s: Server %s failed to start (exit code %d)\n", NAME,
|
---|
[6d5e378] | 207 | path, retval);
|
---|
| 208 |
|
---|
| 209 | return retval;
|
---|
[95bc57c] | 210 | }
|
---|
| 211 |
|
---|
[6d5e378] | 212 | static int console(const char *isvc, const char *osvc)
|
---|
[47a350f] | 213 | {
|
---|
[15f3c3f] | 214 | /* Wait for the input service to be ready */
|
---|
| 215 | service_id_t service_id;
|
---|
[7c014d1] | 216 | int rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
|
---|
[0485135] | 217 | if (rc != EOK) {
|
---|
[7c014d1] | 218 | printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
|
---|
[d9fae235] | 219 | str_error(rc));
|
---|
[6d5e378] | 220 | return rc;
|
---|
[0485135] | 221 | }
|
---|
| 222 |
|
---|
[6d5e378] | 223 | /* Wait for the output service to be ready */
|
---|
| 224 | rc = loc_service_get_id(osvc, &service_id, IPC_FLAG_BLOCKING);
|
---|
[0485135] | 225 | if (rc != EOK) {
|
---|
[6d5e378] | 226 | printf("%s: Error waiting on %s (%s)\n", NAME, osvc,
|
---|
[7c014d1] | 227 | str_error(rc));
|
---|
[6d5e378] | 228 | return rc;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | return srv_start(SRV_CONSOLE, isvc, osvc);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | static int compositor(const char *isvc, const char *name)
|
---|
| 235 | {
|
---|
| 236 | /* Wait for the input service to be ready */
|
---|
| 237 | service_id_t service_id;
|
---|
| 238 | int rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
|
---|
| 239 | if (rc != EOK) {
|
---|
| 240 | printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
|
---|
| 241 | str_error(rc));
|
---|
| 242 | return rc;
|
---|
[7c014d1] | 243 | }
|
---|
| 244 |
|
---|
[6d5e378] | 245 | return srv_start(SRV_COMPOSITOR, isvc, name);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | static int gui_start(const char *app, const char *srv_name)
|
---|
| 249 | {
|
---|
| 250 | char winreg[50];
|
---|
| 251 | snprintf(winreg, sizeof(winreg), "%s%s%s", "comp", srv_name, "/winreg");
|
---|
| 252 |
|
---|
| 253 | printf("%s: Spawning %s %s\n", NAME, app, winreg);
|
---|
| 254 |
|
---|
| 255 | task_id_t id;
|
---|
[1c635d6] | 256 | task_wait_t wait;
|
---|
| 257 | int rc = task_spawnl(&id, &wait, app, app, winreg, NULL);
|
---|
[7c014d1] | 258 | if (rc != EOK) {
|
---|
[6d5e378] | 259 | printf("%s: Error spawning %s %s (%s)\n", NAME, app,
|
---|
| 260 | winreg, str_error(rc));
|
---|
| 261 | return -1;
|
---|
[0485135] | 262 | }
|
---|
[6d5e378] | 263 |
|
---|
| 264 | task_exit_t texit;
|
---|
| 265 | int retval;
|
---|
[1c635d6] | 266 | rc = task_wait(&wait, &texit, &retval);
|
---|
[6d5e378] | 267 | if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
|
---|
| 268 | printf("%s: Error retrieving retval from %s (%s)\n", NAME,
|
---|
| 269 | app, str_error(rc));
|
---|
| 270 | return -1;
|
---|
[0485135] | 271 | }
|
---|
[6d5e378] | 272 |
|
---|
| 273 | return retval;
|
---|
[47a350f] | 274 | }
|
---|
| 275 |
|
---|
[593e023] | 276 | static void getterm(const char *svc, const char *app, bool msg)
|
---|
[36b8100a] | 277 | {
|
---|
[593e023] | 278 | if (msg) {
|
---|
| 279 | printf("%s: Spawning %s %s %s --msg --wait -- %s\n", NAME,
|
---|
| 280 | APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
| 281 |
|
---|
[1c635d6] | 282 | int rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
|
---|
[593e023] | 283 | LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
|
---|
| 284 | if (rc != EOK)
|
---|
| 285 | printf("%s: Error spawning %s %s %s --msg --wait -- %s\n",
|
---|
| 286 | NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
[4deb8b5] | 287 | } else {
|
---|
[593e023] | 288 | printf("%s: Spawning %s %s %s --wait -- %s\n", NAME,
|
---|
| 289 | APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
| 290 |
|
---|
[1c635d6] | 291 | int rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
|
---|
[593e023] | 292 | LOCFS_MOUNT_POINT, "--wait", "--", app, NULL);
|
---|
| 293 | if (rc != EOK)
|
---|
| 294 | printf("%s: Error spawning %s %s %s --wait -- %s\n",
|
---|
| 295 | NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
[0485135] | 296 | }
|
---|
[36b8100a] | 297 | }
|
---|
| 298 |
|
---|
[1ab0852] | 299 | static bool mount_tmpfs(void)
|
---|
[03333bc] | 300 | {
|
---|
[4979403] | 301 | int rc = mount(TMPFS_FS_TYPE, TMPFS_MOUNT_POINT, "", "", 0, 0);
|
---|
[1ab0852] | 302 | return mount_report("Temporary filesystem", TMPFS_MOUNT_POINT,
|
---|
| 303 | TMPFS_FS_TYPE, NULL, rc);
|
---|
[03333bc] | 304 | }
|
---|
| 305 |
|
---|
[5106e98] | 306 | int main(int argc, char *argv[])
|
---|
| 307 | {
|
---|
[860271d4] | 308 | info_print();
|
---|
| 309 |
|
---|
[36b8100a] | 310 | if (!mount_root(STRING(RDFMT))) {
|
---|
[d9fae235] | 311 | printf("%s: Exiting\n", NAME);
|
---|
[6d5e378] | 312 | return 1;
|
---|
[860271d4] | 313 | }
|
---|
[d9fae235] | 314 |
|
---|
[03333bc] | 315 | /* Make sure tmpfs is running. */
|
---|
[6d5e378] | 316 | if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
|
---|
| 317 | srv_start("/srv/tmpfs");
|
---|
[860271d4] | 318 |
|
---|
[ca05e9b] | 319 | srv_start("/srv/klog");
|
---|
[6d5e378] | 320 | srv_start("/srv/locfs");
|
---|
| 321 | srv_start("/srv/taskmon");
|
---|
[a095d20] | 322 |
|
---|
[15f3c3f] | 323 | if (!mount_locfs()) {
|
---|
[d9fae235] | 324 | printf("%s: Exiting\n", NAME);
|
---|
[6d5e378] | 325 | return 2;
|
---|
[a095d20] | 326 | }
|
---|
[d9fae235] | 327 |
|
---|
[1ab0852] | 328 | mount_tmpfs();
|
---|
[a095d20] | 329 |
|
---|
[6d5e378] | 330 | srv_start("/srv/devman");
|
---|
| 331 | srv_start("/srv/apic");
|
---|
| 332 | srv_start("/srv/i8259");
|
---|
[75fe97b] | 333 | srv_start("/srv/icp-ic");
|
---|
[6d5e378] | 334 | srv_start("/srv/obio");
|
---|
[3a2f8aa] | 335 | srv_start("/srv/cuda_adb");
|
---|
[f7cbc6f] | 336 | srv_start("/srv/s3c24xx_uart");
|
---|
| 337 | srv_start("/srv/s3c24xx_ts");
|
---|
[d9fae235] | 338 |
|
---|
[6d5e378] | 339 | srv_start("/srv/loopip");
|
---|
| 340 | srv_start("/srv/ethip");
|
---|
| 341 | srv_start("/srv/inetsrv");
|
---|
| 342 | srv_start("/srv/tcp");
|
---|
| 343 | srv_start("/srv/udp");
|
---|
[31e9fe0] | 344 | srv_start("/srv/dnsrsrv");
|
---|
[bd88bee] | 345 | srv_start("/srv/dhcp");
|
---|
| 346 | srv_start("/srv/nconfsrv");
|
---|
[00d7e1b] | 347 |
|
---|
[6d5e378] | 348 | srv_start("/srv/clipboard");
|
---|
| 349 | srv_start("/srv/remcons");
|
---|
[d9fae235] | 350 |
|
---|
[6d5e378] | 351 | srv_start("/srv/input", HID_INPUT);
|
---|
| 352 | srv_start("/srv/output", HID_OUTPUT);
|
---|
[2e2c18a1] | 353 | srv_start("/srv/hound");
|
---|
[6d5e378] | 354 |
|
---|
| 355 | int rc = compositor(HID_INPUT, HID_COMPOSITOR_SERVER);
|
---|
| 356 | if (rc == EOK) {
|
---|
| 357 | gui_start("/app/vlaunch", HID_COMPOSITOR_SERVER);
|
---|
| 358 | gui_start("/app/vterm", HID_COMPOSITOR_SERVER);
|
---|
[593e023] | 359 | }
|
---|
| 360 |
|
---|
| 361 | rc = console(HID_INPUT, HID_OUTPUT);
|
---|
| 362 | if (rc == EOK) {
|
---|
| 363 | getterm("term/vc0", "/app/bdsh", true);
|
---|
| 364 | getterm("term/vc1", "/app/bdsh", false);
|
---|
| 365 | getterm("term/vc2", "/app/bdsh", false);
|
---|
| 366 | getterm("term/vc3", "/app/bdsh", false);
|
---|
| 367 | getterm("term/vc4", "/app/bdsh", false);
|
---|
| 368 | getterm("term/vc5", "/app/bdsh", false);
|
---|
[6d5e378] | 369 | }
|
---|
[d9fae235] | 370 |
|
---|
[3eddaff] | 371 | return 0;
|
---|
| 372 | }
|
---|
[b2951e2] | 373 |
|
---|
| 374 | /** @}
|
---|
| 375 | */
|
---|