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
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <fibril.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdarg.h>
|
---|
39 | #include <vfs/vfs.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <task.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <macros.h>
|
---|
45 | #include <str.h>
|
---|
46 | #include <loc.h>
|
---|
47 | #include <str_error.h>
|
---|
48 | #include <config.h>
|
---|
49 | #include <io/logctl.h>
|
---|
50 | #include <vfs/vfs.h>
|
---|
51 | #include <vol.h>
|
---|
52 | #include "untar.h"
|
---|
53 | #include "init.h"
|
---|
54 |
|
---|
55 | #define ROOT_DEVICE "bd/initrd"
|
---|
56 | #define ROOT_MOUNT_POINT "/"
|
---|
57 |
|
---|
58 | #define LOCFS_FS_TYPE "locfs"
|
---|
59 | #define LOCFS_MOUNT_POINT "/loc"
|
---|
60 |
|
---|
61 | #define TMPFS_FS_TYPE "tmpfs"
|
---|
62 | #define TMPFS_MOUNT_POINT "/tmp"
|
---|
63 |
|
---|
64 | #define SRV_CONSOLE "/srv/hid/console"
|
---|
65 | #define APP_GETTERM "/app/getterm"
|
---|
66 |
|
---|
67 | #define SRV_DISPLAY "/srv/hid/display"
|
---|
68 |
|
---|
69 | #define HID_INPUT "hid/input"
|
---|
70 | #define HID_OUTPUT "hid/output"
|
---|
71 | #define HID_COMPOSITOR_SERVER ":0"
|
---|
72 |
|
---|
73 | #define srv_start(path, ...) \
|
---|
74 | srv_startl(path, path, ##__VA_ARGS__, NULL)
|
---|
75 |
|
---|
76 | static const char *sys_dirs[] = {
|
---|
77 | "/w/cfg",
|
---|
78 | "/w/data"
|
---|
79 | };
|
---|
80 |
|
---|
81 | /** Print banner */
|
---|
82 | static void info_print(void)
|
---|
83 | {
|
---|
84 | printf("%s: HelenOS init\n", NAME);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Report mount operation success */
|
---|
88 | static bool mount_report(const char *desc, const char *mntpt,
|
---|
89 | const char *fstype, const char *dev, errno_t rc)
|
---|
90 | {
|
---|
91 | switch (rc) {
|
---|
92 | case EOK:
|
---|
93 | if ((dev != NULL) && (str_cmp(dev, "") != 0))
|
---|
94 | printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
|
---|
95 | fstype, dev);
|
---|
96 | else
|
---|
97 | printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
|
---|
98 | break;
|
---|
99 | case EBUSY:
|
---|
100 | printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
|
---|
101 | return false;
|
---|
102 | case ELIMIT:
|
---|
103 | printf("%s: %s limit exceeded\n", NAME, desc);
|
---|
104 | return false;
|
---|
105 | case ENOENT:
|
---|
106 | printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
|
---|
107 | return false;
|
---|
108 | default:
|
---|
109 | printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
|
---|
110 | str_error(rc));
|
---|
111 | return false;
|
---|
112 | }
|
---|
113 |
|
---|
114 | return true;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** Mount root file system
|
---|
118 | *
|
---|
119 | * The operation blocks until the root file system
|
---|
120 | * server is ready for mounting.
|
---|
121 | *
|
---|
122 | * @param[in] fstype Root file system type.
|
---|
123 | *
|
---|
124 | * @return True on success.
|
---|
125 | * @return False on failure.
|
---|
126 | *
|
---|
127 | */
|
---|
128 | static bool mount_root(const char *fstype)
|
---|
129 | {
|
---|
130 | const char *root_device = "";
|
---|
131 |
|
---|
132 | if (str_cmp(fstype, "tmpfs") != 0)
|
---|
133 | root_device = ROOT_DEVICE;
|
---|
134 |
|
---|
135 | errno_t rc = vfs_mount_path(ROOT_MOUNT_POINT, fstype, root_device, "",
|
---|
136 | IPC_FLAG_BLOCKING, 0);
|
---|
137 | if (rc == EOK)
|
---|
138 | logctl_set_root();
|
---|
139 |
|
---|
140 | bool ret = mount_report("Root file system", ROOT_MOUNT_POINT, fstype,
|
---|
141 | root_device, rc);
|
---|
142 |
|
---|
143 | rc = vfs_cwd_set(ROOT_MOUNT_POINT);
|
---|
144 | if (rc != EOK) {
|
---|
145 | printf("%s: Unable to set current directory to %s (%s)\n",
|
---|
146 | NAME, ROOT_MOUNT_POINT, str_error(ret));
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if ((ret) && (str_cmp(fstype, "tmpfs") == 0)) {
|
---|
151 | printf("%s: Extracting root file system archive\n", NAME);
|
---|
152 | ret = bd_untar(ROOT_DEVICE);
|
---|
153 | }
|
---|
154 |
|
---|
155 | return ret;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Mount locfs file system
|
---|
159 | *
|
---|
160 | * The operation blocks until the locfs file system
|
---|
161 | * server is ready for mounting.
|
---|
162 | *
|
---|
163 | * @return True on success.
|
---|
164 | * @return False on failure.
|
---|
165 | *
|
---|
166 | */
|
---|
167 | static bool mount_locfs(void)
|
---|
168 | {
|
---|
169 | errno_t rc = vfs_mount_path(LOCFS_MOUNT_POINT, LOCFS_FS_TYPE, "", "",
|
---|
170 | IPC_FLAG_BLOCKING, 0);
|
---|
171 | return mount_report("Location service file system", LOCFS_MOUNT_POINT,
|
---|
172 | LOCFS_FS_TYPE, NULL, rc);
|
---|
173 | }
|
---|
174 |
|
---|
175 | static errno_t srv_startl(const char *path, ...)
|
---|
176 | {
|
---|
177 | vfs_stat_t s;
|
---|
178 | if (vfs_stat_path(path, &s) != EOK) {
|
---|
179 | printf("%s: Unable to stat %s\n", NAME, path);
|
---|
180 | return ENOENT;
|
---|
181 | }
|
---|
182 |
|
---|
183 | printf("%s: Starting %s\n", NAME, path);
|
---|
184 |
|
---|
185 | va_list ap;
|
---|
186 | const char *arg;
|
---|
187 | int cnt = 0;
|
---|
188 |
|
---|
189 | va_start(ap, path);
|
---|
190 | do {
|
---|
191 | arg = va_arg(ap, const char *);
|
---|
192 | cnt++;
|
---|
193 | } while (arg != NULL);
|
---|
194 | va_end(ap);
|
---|
195 |
|
---|
196 | va_start(ap, path);
|
---|
197 | task_id_t id;
|
---|
198 | task_wait_t wait;
|
---|
199 | errno_t rc = task_spawn(&id, &wait, path, cnt, ap);
|
---|
200 | va_end(ap);
|
---|
201 |
|
---|
202 | if (rc != EOK) {
|
---|
203 | printf("%s: Error spawning %s (%s)\n", NAME, path,
|
---|
204 | str_error(rc));
|
---|
205 | return rc;
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (!id) {
|
---|
209 | printf("%s: Error spawning %s (invalid task id)\n", NAME,
|
---|
210 | path);
|
---|
211 | return EINVAL;
|
---|
212 | }
|
---|
213 |
|
---|
214 | task_exit_t texit;
|
---|
215 | int retval;
|
---|
216 | rc = task_wait(&wait, &texit, &retval);
|
---|
217 | if (rc != EOK) {
|
---|
218 | printf("%s: Error waiting for %s (%s)\n", NAME, path,
|
---|
219 | str_error(rc));
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (texit != TASK_EXIT_NORMAL) {
|
---|
224 | printf("%s: Server %s failed to start (unexpectedly "
|
---|
225 | "terminated)\n", NAME, path);
|
---|
226 | return EINVAL;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (retval != 0)
|
---|
230 | printf("%s: Server %s failed to start (exit code %d)\n", NAME,
|
---|
231 | path, retval);
|
---|
232 |
|
---|
233 | return retval == 0 ? EOK : EPARTY;
|
---|
234 | }
|
---|
235 |
|
---|
236 | static errno_t console(const char *isvc, const char *osvc)
|
---|
237 | {
|
---|
238 | /* Wait for the input service to be ready */
|
---|
239 | service_id_t service_id;
|
---|
240 | errno_t rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
|
---|
241 | if (rc != EOK) {
|
---|
242 | printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
|
---|
243 | str_error(rc));
|
---|
244 | return rc;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /* Wait for the output service to be ready */
|
---|
248 | rc = loc_service_get_id(osvc, &service_id, IPC_FLAG_BLOCKING);
|
---|
249 | if (rc != EOK) {
|
---|
250 | printf("%s: Error waiting on %s (%s)\n", NAME, osvc,
|
---|
251 | str_error(rc));
|
---|
252 | return rc;
|
---|
253 | }
|
---|
254 |
|
---|
255 | return srv_start(SRV_CONSOLE, isvc, osvc);
|
---|
256 | }
|
---|
257 |
|
---|
258 | static errno_t display_server(void)
|
---|
259 | {
|
---|
260 | return srv_start(SRV_DISPLAY);
|
---|
261 | }
|
---|
262 |
|
---|
263 | static int gui_start(const char *app)
|
---|
264 | {
|
---|
265 | printf("%s: Spawning %s\n", NAME, app);
|
---|
266 |
|
---|
267 | task_id_t id;
|
---|
268 | task_wait_t wait;
|
---|
269 | errno_t rc = task_spawnl(&id, &wait, app, app, NULL);
|
---|
270 | if (rc != EOK) {
|
---|
271 | printf("%s: Error spawning %s (%s)\n", NAME, app,
|
---|
272 | str_error(rc));
|
---|
273 | return -1;
|
---|
274 | }
|
---|
275 |
|
---|
276 | task_exit_t texit;
|
---|
277 | int retval;
|
---|
278 | rc = task_wait(&wait, &texit, &retval);
|
---|
279 | if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
|
---|
280 | printf("%s: Error retrieving retval from %s (%s)\n", NAME,
|
---|
281 | app, str_error(rc));
|
---|
282 | return -1;
|
---|
283 | }
|
---|
284 |
|
---|
285 | return retval;
|
---|
286 | }
|
---|
287 |
|
---|
288 | static void getterm(const char *svc, const char *app, bool msg)
|
---|
289 | {
|
---|
290 | if (msg) {
|
---|
291 | printf("%s: Spawning %s %s %s --msg --wait -- %s\n", NAME,
|
---|
292 | APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
293 |
|
---|
294 | errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
|
---|
295 | LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
|
---|
296 | if (rc != EOK)
|
---|
297 | printf("%s: Error spawning %s %s %s --msg --wait -- %s\n",
|
---|
298 | NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
299 | } else {
|
---|
300 | printf("%s: Spawning %s %s %s --wait -- %s\n", NAME,
|
---|
301 | APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
302 |
|
---|
303 | errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
|
---|
304 | LOCFS_MOUNT_POINT, "--wait", "--", app, NULL);
|
---|
305 | if (rc != EOK)
|
---|
306 | printf("%s: Error spawning %s %s %s --wait -- %s\n",
|
---|
307 | NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | static bool mount_tmpfs(void)
|
---|
312 | {
|
---|
313 | errno_t rc = vfs_mount_path(TMPFS_MOUNT_POINT, TMPFS_FS_TYPE, "", "", 0, 0);
|
---|
314 | return mount_report("Temporary file system", TMPFS_MOUNT_POINT,
|
---|
315 | TMPFS_FS_TYPE, NULL, rc);
|
---|
316 | }
|
---|
317 |
|
---|
318 | /** Init system volume.
|
---|
319 | *
|
---|
320 | * See if system volume is configured. If so, try to wait for it to become
|
---|
321 | * available. If not, create basic directories for live image omde.
|
---|
322 | */
|
---|
323 | static errno_t init_sysvol(void)
|
---|
324 | {
|
---|
325 | vol_t *vol = NULL;
|
---|
326 | vol_info_t vinfo;
|
---|
327 | volume_id_t *volume_ids = NULL;
|
---|
328 | size_t nvols;
|
---|
329 | size_t i;
|
---|
330 | errno_t rc;
|
---|
331 | bool found_cfg;
|
---|
332 | const char **cp;
|
---|
333 |
|
---|
334 | rc = vol_create(&vol);
|
---|
335 | if (rc != EOK) {
|
---|
336 | printf("Error contacting volume service.\n");
|
---|
337 | goto error;
|
---|
338 | }
|
---|
339 |
|
---|
340 | rc = vol_get_volumes(vol, &volume_ids, &nvols);
|
---|
341 | if (rc != EOK) {
|
---|
342 | printf("Error getting list of volumes.\n");
|
---|
343 | goto error;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /* XXX This could be handled more efficiently by volsrv itself */
|
---|
347 | found_cfg = false;
|
---|
348 | for (i = 0; i < nvols; i++) {
|
---|
349 | rc = vol_info(vol, volume_ids[i], &vinfo);
|
---|
350 | if (rc != EOK) {
|
---|
351 | printf("Error getting volume information.\n");
|
---|
352 | rc = EIO;
|
---|
353 | goto error;
|
---|
354 | }
|
---|
355 |
|
---|
356 | if (str_cmp(vinfo.path, "/w") == 0) {
|
---|
357 | found_cfg = true;
|
---|
358 | break;
|
---|
359 | }
|
---|
360 | }
|
---|
361 |
|
---|
362 | vol_destroy(vol);
|
---|
363 | free(volume_ids);
|
---|
364 |
|
---|
365 | if (!found_cfg) {
|
---|
366 | /* Prepare directory structure for live image mode */
|
---|
367 | printf("%s: Creating live image directory structure.\n", NAME);
|
---|
368 | cp = sys_dirs;
|
---|
369 | while (*cp != NULL) {
|
---|
370 | rc = vfs_link_path(*cp, KIND_DIRECTORY, NULL);
|
---|
371 | if (rc != EOK) {
|
---|
372 | printf("%s: Error creating directory '%s'.\n",
|
---|
373 | NAME, *cp);
|
---|
374 | return rc;
|
---|
375 | }
|
---|
376 |
|
---|
377 | ++cp;
|
---|
378 | }
|
---|
379 | } else {
|
---|
380 | printf("%s: System volume is configured.\n", NAME);
|
---|
381 | }
|
---|
382 |
|
---|
383 | return EOK;
|
---|
384 | error:
|
---|
385 | vol_destroy(vol);
|
---|
386 | if (volume_ids != NULL)
|
---|
387 | free(volume_ids);
|
---|
388 |
|
---|
389 | return rc;
|
---|
390 | }
|
---|
391 |
|
---|
392 | int main(int argc, char *argv[])
|
---|
393 | {
|
---|
394 | errno_t rc;
|
---|
395 |
|
---|
396 | info_print();
|
---|
397 |
|
---|
398 | if (!mount_root(STRING(RDFMT))) {
|
---|
399 | printf("%s: Exiting\n", NAME);
|
---|
400 | return 1;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /* Make sure file systems are running. */
|
---|
404 | if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
|
---|
405 | srv_start("/srv/fs/tmpfs");
|
---|
406 | if (str_cmp(STRING(RDFMT), "exfat") != 0)
|
---|
407 | srv_start("/srv/fs/exfat");
|
---|
408 | if (str_cmp(STRING(RDFMT), "fat") != 0)
|
---|
409 | srv_start("/srv/fs/fat");
|
---|
410 | srv_start("/srv/fs/cdfs");
|
---|
411 | srv_start("/srv/fs/mfs");
|
---|
412 |
|
---|
413 | srv_start("/srv/klog");
|
---|
414 | srv_start("/srv/fs/locfs");
|
---|
415 | srv_start("/srv/taskmon");
|
---|
416 |
|
---|
417 | if (!mount_locfs()) {
|
---|
418 | printf("%s: Exiting\n", NAME);
|
---|
419 | return 2;
|
---|
420 | }
|
---|
421 |
|
---|
422 | mount_tmpfs();
|
---|
423 |
|
---|
424 | srv_start("/srv/devman");
|
---|
425 | srv_start("/srv/hid/s3c24xx_uart");
|
---|
426 | srv_start("/srv/hid/s3c24xx_ts");
|
---|
427 |
|
---|
428 | srv_start("/srv/bd/vbd");
|
---|
429 | srv_start("/srv/volsrv");
|
---|
430 |
|
---|
431 | srv_start("/srv/net/loopip");
|
---|
432 | srv_start("/srv/net/ethip");
|
---|
433 | srv_start("/srv/net/inetsrv");
|
---|
434 | srv_start("/srv/net/tcp");
|
---|
435 | srv_start("/srv/net/udp");
|
---|
436 | srv_start("/srv/net/dnsrsrv");
|
---|
437 | srv_start("/srv/net/dhcp");
|
---|
438 | srv_start("/srv/net/nconfsrv");
|
---|
439 |
|
---|
440 | srv_start("/srv/clipboard");
|
---|
441 | srv_start("/srv/hid/remcons");
|
---|
442 |
|
---|
443 | srv_start("/srv/hid/input", HID_INPUT);
|
---|
444 | srv_start("/srv/hid/output", HID_OUTPUT);
|
---|
445 | srv_start("/srv/audio/hound");
|
---|
446 |
|
---|
447 | init_sysvol();
|
---|
448 |
|
---|
449 | if (!config_key_exists("console")) {
|
---|
450 | rc = display_server();
|
---|
451 | if (rc == EOK) {
|
---|
452 | fibril_sleep(1);
|
---|
453 | gui_start("/app/terminal");
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 | rc = console(HID_INPUT, HID_OUTPUT);
|
---|
458 | if (rc == EOK) {
|
---|
459 | getterm("term/vc0", "/app/bdsh", true);
|
---|
460 | getterm("term/vc1", "/app/bdsh", false);
|
---|
461 | getterm("term/vc2", "/app/bdsh", false);
|
---|
462 | getterm("term/vc3", "/app/bdsh", false);
|
---|
463 | getterm("term/vc4", "/app/bdsh", false);
|
---|
464 | getterm("term/vc5", "/app/bdsh", false);
|
---|
465 | }
|
---|
466 |
|
---|
467 | return 0;
|
---|
468 | }
|
---|
469 |
|
---|
470 | /** @}
|
---|
471 | */
|
---|