source: mainline/uspace/app/init/init.c@ f8cb1bc5

Last change on this file since f8cb1bc5 was 04e520e, checked in by Jiri Svoboda <jiri@…>, 19 months ago

Config file persistence

Copy /cfg to /w/cfg when installing and when starting live image
Move futil to separate library
Wait for /w to be mounted while booting from disk
Change taskbar and taskbar-cfg to work with /w/cfg/taskbar.sif

  • Property mode set to 100644
File size: 12.7 KB
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
3 * Copyright (c) 2005 Martin Decky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup init
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <fibril.h>
38#include <futil.h>
39#include <stdio.h>
40#include <stdarg.h>
41#include <vfs/vfs.h>
42#include <stdbool.h>
43#include <errno.h>
44#include <task.h>
45#include <stdlib.h>
46#include <macros.h>
47#include <str.h>
48#include <loc.h>
49#include <str_error.h>
50#include <config.h>
51#include <io/logctl.h>
52#include <vfs/vfs.h>
53#include <vol.h>
54#include "untar.h"
55#include "init.h"
56
57#define BANNER_LEFT "######> "
58#define BANNER_RIGHT " <######"
59
60#define ROOT_DEVICE "bd/initrd"
61#define ROOT_MOUNT_POINT "/"
62
63#define LOCFS_FS_TYPE "locfs"
64#define LOCFS_MOUNT_POINT "/loc"
65
66#define TMPFS_FS_TYPE "tmpfs"
67#define TMPFS_MOUNT_POINT "/tmp"
68
69#define SRV_CONSOLE "/srv/hid/console"
70#define APP_GETTERM "/app/getterm"
71
72#define SRV_DISPLAY "/srv/hid/display"
73
74#define HID_INPUT "hid/input"
75#define HID_OUTPUT "hid/output"
76
77#define srv_start(path, ...) \
78 srv_startl(path, path, ##__VA_ARGS__, NULL)
79
80static const char *sys_dirs[] = {
81 "/w/cfg",
82 "/w/data"
83};
84
85/** Print banner */
86static void info_print(void)
87{
88 printf("%s: HelenOS init\n", NAME);
89}
90
91static void oom_check(errno_t rc, const char *path)
92{
93 if (rc == ENOMEM) {
94 printf("%sOut-of-memory condition detected%s\n", BANNER_LEFT,
95 BANNER_RIGHT);
96 printf("%sBailing out of the boot process after %s%s\n",
97 BANNER_LEFT, path, BANNER_RIGHT);
98 printf("%sMore physical memory is required%s\n", BANNER_LEFT,
99 BANNER_RIGHT);
100 exit(ENOMEM);
101 }
102}
103
104/** Report mount operation success */
105static bool mount_report(const char *desc, const char *mntpt,
106 const char *fstype, const char *dev, errno_t rc)
107{
108 switch (rc) {
109 case EOK:
110 if ((dev != NULL) && (str_cmp(dev, "") != 0))
111 printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
112 fstype, dev);
113 else
114 printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
115 break;
116 case EBUSY:
117 printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
118 return false;
119 case ELIMIT:
120 printf("%s: %s limit exceeded\n", NAME, desc);
121 return false;
122 case ENOENT:
123 printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
124 return false;
125 default:
126 printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
127 str_error(rc));
128 return false;
129 }
130
131 return true;
132}
133
134/** Mount root file system
135 *
136 * The operation blocks until the root file system
137 * server is ready for mounting.
138 *
139 * @param[in] fstype Root file system type.
140 *
141 * @return True on success.
142 * @return False on failure.
143 *
144 */
145static bool mount_root(const char *fstype)
146{
147 const char *root_device = "";
148
149 if (str_cmp(fstype, "tmpfs") != 0)
150 root_device = ROOT_DEVICE;
151
152 errno_t rc = vfs_mount_path(ROOT_MOUNT_POINT, fstype, root_device, "",
153 IPC_FLAG_BLOCKING, 0);
154 if (rc == EOK)
155 logctl_set_root();
156
157 bool ret = mount_report("Root file system", ROOT_MOUNT_POINT, fstype,
158 root_device, rc);
159
160 rc = vfs_cwd_set(ROOT_MOUNT_POINT);
161 if (rc != EOK) {
162 printf("%s: Unable to set current directory to %s (%s)\n",
163 NAME, ROOT_MOUNT_POINT, str_error(ret));
164 return false;
165 }
166
167 if ((ret) && (str_cmp(fstype, "tmpfs") == 0)) {
168 printf("%s: Extracting root file system archive\n", NAME);
169 ret = bd_untar(ROOT_DEVICE);
170 }
171
172 return ret;
173}
174
175/** Mount locfs file system
176 *
177 * The operation blocks until the locfs file system
178 * server is ready for mounting.
179 *
180 * @return True on success.
181 * @return False on failure.
182 *
183 */
184static bool mount_locfs(void)
185{
186 errno_t rc = vfs_mount_path(LOCFS_MOUNT_POINT, LOCFS_FS_TYPE, "", "",
187 IPC_FLAG_BLOCKING, 0);
188 return mount_report("Location service file system", LOCFS_MOUNT_POINT,
189 LOCFS_FS_TYPE, NULL, rc);
190}
191
192static errno_t srv_startl(const char *path, ...)
193{
194 vfs_stat_t s;
195 if (vfs_stat_path(path, &s) != EOK) {
196 printf("%s: Unable to stat %s\n", NAME, path);
197 return ENOENT;
198 }
199
200 printf("%s: Starting %s\n", NAME, path);
201
202 va_list ap;
203 const char *arg;
204 int cnt = 0;
205
206 va_start(ap, path);
207 do {
208 arg = va_arg(ap, const char *);
209 cnt++;
210 } while (arg != NULL);
211 va_end(ap);
212
213 va_start(ap, path);
214 task_id_t id;
215 task_wait_t wait;
216 errno_t rc = task_spawn(&id, &wait, path, cnt, ap);
217 va_end(ap);
218
219 if (rc != EOK) {
220 oom_check(rc, path);
221 printf("%s: Error spawning %s (%s)\n", NAME, path,
222 str_error(rc));
223 return rc;
224 }
225
226 if (!id) {
227 printf("%s: Error spawning %s (invalid task id)\n", NAME,
228 path);
229 return EINVAL;
230 }
231
232 task_exit_t texit;
233 int retval;
234 rc = task_wait(&wait, &texit, &retval);
235 if (rc != EOK) {
236 printf("%s: Error waiting for %s (%s)\n", NAME, path,
237 str_error(rc));
238 return rc;
239 }
240
241 if (texit != TASK_EXIT_NORMAL) {
242 printf("%s: Server %s failed to start (unexpectedly "
243 "terminated)\n", NAME, path);
244 return EINVAL;
245 }
246
247 if (retval != 0)
248 printf("%s: Server %s failed to start (exit code %d)\n", NAME,
249 path, retval);
250
251 return retval == 0 ? EOK : EPARTY;
252}
253
254static errno_t console(const char *isvc, const char *osvc)
255{
256 /* Wait for the input service to be ready */
257 service_id_t service_id;
258 errno_t rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
259 if (rc != EOK) {
260 printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
261 str_error(rc));
262 return rc;
263 }
264
265 /* Wait for the output service to be ready */
266 rc = loc_service_get_id(osvc, &service_id, IPC_FLAG_BLOCKING);
267 if (rc != EOK) {
268 printf("%s: Error waiting on %s (%s)\n", NAME, osvc,
269 str_error(rc));
270 return rc;
271 }
272
273 return srv_start(SRV_CONSOLE, isvc, osvc);
274}
275
276#ifdef CONFIG_WINSYS
277
278static errno_t display_server(void)
279{
280 return srv_start(SRV_DISPLAY);
281}
282
283static int app_start(const char *app, const char *arg)
284{
285 printf("%s: Spawning %s\n", NAME, app);
286
287 task_id_t id;
288 task_wait_t wait;
289 errno_t rc = task_spawnl(&id, &wait, app, app, arg, NULL);
290 if (rc != EOK) {
291 oom_check(rc, app);
292 printf("%s: Error spawning %s (%s)\n", NAME, app,
293 str_error(rc));
294 return -1;
295 }
296
297 task_exit_t texit;
298 int retval;
299 rc = task_wait(&wait, &texit, &retval);
300 if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
301 printf("%s: Error retrieving retval from %s (%s)\n", NAME,
302 app, str_error(rc));
303 return rc;
304 }
305
306 return retval;
307}
308
309#endif
310
311static void getterm(const char *svc, const char *app, bool msg)
312{
313 if (msg) {
314 printf("%s: Spawning %s %s %s --msg --wait -- %s\n", NAME,
315 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
316
317 errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
318 LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
319 if (rc != EOK) {
320 oom_check(rc, APP_GETTERM);
321 printf("%s: Error spawning %s %s %s --msg --wait -- %s\n",
322 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
323 }
324 } else {
325 printf("%s: Spawning %s %s %s --wait -- %s\n", NAME,
326 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
327
328 errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
329 LOCFS_MOUNT_POINT, "--wait", "--", app, NULL);
330 if (rc != EOK) {
331 oom_check(rc, APP_GETTERM);
332 printf("%s: Error spawning %s %s %s --wait -- %s\n",
333 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
334 }
335 }
336}
337
338static bool mount_tmpfs(void)
339{
340 errno_t rc = vfs_mount_path(TMPFS_MOUNT_POINT, TMPFS_FS_TYPE, "", "", 0, 0);
341 return mount_report("Temporary file system", TMPFS_MOUNT_POINT,
342 TMPFS_FS_TYPE, NULL, rc);
343}
344
345/** Init system volume.
346 *
347 * See if system volume is configured. If so, try to wait for it to become
348 * available. If not, create basic directories for live image omde.
349 */
350static errno_t init_sysvol(void)
351{
352 vol_t *vol = NULL;
353 vol_info_t vinfo;
354 volume_id_t *volume_ids = NULL;
355 service_id_t *part_ids = NULL;
356 vol_part_info_t pinfo;
357 size_t nvols;
358 size_t nparts;
359 bool sv_mounted;
360 size_t i;
361 errno_t rc;
362 bool found_cfg;
363 const char **cp;
364
365 rc = vol_create(&vol);
366 if (rc != EOK) {
367 printf("Error contacting volume service.\n");
368 goto error;
369 }
370
371 rc = vol_get_volumes(vol, &volume_ids, &nvols);
372 if (rc != EOK) {
373 printf("Error getting list of volumes.\n");
374 goto error;
375 }
376
377 /* XXX This could be handled more efficiently by volsrv itself */
378 found_cfg = false;
379 for (i = 0; i < nvols; i++) {
380 rc = vol_info(vol, volume_ids[i], &vinfo);
381 if (rc != EOK) {
382 printf("Error getting volume information.\n");
383 rc = EIO;
384 goto error;
385 }
386
387 if (str_cmp(vinfo.path, "/w") == 0) {
388 found_cfg = true;
389 break;
390 }
391 }
392
393 free(volume_ids);
394 volume_ids = NULL;
395
396 if (!found_cfg) {
397 /* Prepare directory structure for live image mode */
398 printf("%s: Creating live image directory structure.\n", NAME);
399 cp = sys_dirs;
400 while (*cp != NULL) {
401 rc = vfs_link_path(*cp, KIND_DIRECTORY, NULL);
402 if (rc != EOK) {
403 printf("%s: Error creating directory '%s'.\n",
404 NAME, *cp);
405 goto error;
406 }
407
408 ++cp;
409 }
410
411 /* Copy initial configuration files */
412 rc = futil_rcopy_contents("/cfg", "/w/cfg");
413 if (rc != EOK)
414 goto error;
415 } else {
416 printf("%s: System volume is configured.\n", NAME);
417
418 /* Wait until system volume is mounted */
419 sv_mounted = false;
420
421 while (true) {
422 rc = vol_get_parts(vol, &part_ids, &nparts);
423 if (rc != EOK) {
424 printf("Error getting list of volumes.\n");
425 goto error;
426 }
427
428 for (i = 0; i < nparts; i++) {
429 rc = vol_part_info(vol, part_ids[i], &pinfo);
430 if (rc != EOK) {
431 printf("Error getting partition "
432 "information.\n");
433 rc = EIO;
434 goto error;
435 }
436
437 if (str_cmp(pinfo.cur_mp, "/w") == 0) {
438 sv_mounted = true;
439 break;
440 }
441 }
442
443 if (sv_mounted)
444 break;
445
446 free(part_ids);
447 part_ids = NULL;
448
449 fibril_sleep(1);
450 printf("Sleeping(1) for system volume.\n");
451 }
452 }
453
454 vol_destroy(vol);
455 return EOK;
456error:
457 vol_destroy(vol);
458 if (volume_ids != NULL)
459 free(volume_ids);
460 if (part_ids != NULL)
461 free(part_ids);
462
463 return rc;
464}
465
466int main(int argc, char *argv[])
467{
468 errno_t rc;
469
470 info_print();
471
472 if (!mount_root(STRING(RDFMT))) {
473 printf("%s: Exiting\n", NAME);
474 return 1;
475 }
476
477 /* Make sure file systems are running. */
478 if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
479 srv_start("/srv/fs/tmpfs");
480 if (str_cmp(STRING(RDFMT), "exfat") != 0)
481 srv_start("/srv/fs/exfat");
482 if (str_cmp(STRING(RDFMT), "fat") != 0)
483 srv_start("/srv/fs/fat");
484 srv_start("/srv/fs/cdfs");
485 srv_start("/srv/fs/mfs");
486
487 srv_start("/srv/klog");
488 srv_start("/srv/fs/locfs");
489 srv_start("/srv/taskmon");
490
491 if (!mount_locfs()) {
492 printf("%s: Exiting\n", NAME);
493 return 2;
494 }
495
496 mount_tmpfs();
497
498 srv_start("/srv/devman");
499 srv_start("/srv/hid/s3c24xx_uart");
500 srv_start("/srv/hid/s3c24xx_ts");
501
502 srv_start("/srv/bd/vbd");
503 srv_start("/srv/volsrv");
504
505 srv_start("/srv/net/loopip");
506 srv_start("/srv/net/ethip");
507 srv_start("/srv/net/inetsrv");
508 srv_start("/srv/net/tcp");
509 srv_start("/srv/net/udp");
510 srv_start("/srv/net/dnsrsrv");
511 srv_start("/srv/net/dhcp");
512 srv_start("/srv/net/nconfsrv");
513
514 srv_start("/srv/clipboard");
515 srv_start("/srv/hid/remcons");
516
517 srv_start("/srv/hid/input", HID_INPUT);
518 srv_start("/srv/hid/output", HID_OUTPUT);
519 srv_start("/srv/audio/hound");
520
521 init_sysvol();
522
523#ifdef CONFIG_WINSYS
524 if (!config_key_exists("console")) {
525 rc = display_server();
526 if (rc == EOK) {
527 app_start("/app/taskbar", NULL);
528 app_start("/app/terminal", "-topleft");
529 }
530 }
531#endif
532 rc = console(HID_INPUT, HID_OUTPUT);
533 if (rc == EOK) {
534 getterm("term/vc0", "/app/bdsh", true);
535 getterm("term/vc1", "/app/bdsh", false);
536 getterm("term/vc2", "/app/bdsh", false);
537 getterm("term/vc3", "/app/bdsh", false);
538 getterm("term/vc4", "/app/bdsh", false);
539 getterm("term/vc5", "/app/bdsh", false);
540 }
541
542 return 0;
543}
544
545/** @}
546 */
Note: See TracBrowser for help on using the repository browser.