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

Last change on this file since dbae3b6 was dbae3b6, checked in by Jiri Svoboda <jiri@…>, 10 months ago

Persistently store taskmon configuration.

  • 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 NULL,
84};
85
86/** Print banner */
87static void info_print(void)
88{
89 printf("%s: HelenOS init\n", NAME);
90}
91
92static void oom_check(errno_t rc, const char *path)
93{
94 if (rc == ENOMEM) {
95 printf("%sOut-of-memory condition detected%s\n", BANNER_LEFT,
96 BANNER_RIGHT);
97 printf("%sBailing out of the boot process after %s%s\n",
98 BANNER_LEFT, path, BANNER_RIGHT);
99 printf("%sMore physical memory is required%s\n", BANNER_LEFT,
100 BANNER_RIGHT);
101 exit(ENOMEM);
102 }
103}
104
105/** Report mount operation success */
106static bool mount_report(const char *desc, const char *mntpt,
107 const char *fstype, const char *dev, errno_t rc)
108{
109 switch (rc) {
110 case EOK:
111 if ((dev != NULL) && (str_cmp(dev, "") != 0))
112 printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
113 fstype, dev);
114 else
115 printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
116 break;
117 case EBUSY:
118 printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
119 return false;
120 case ELIMIT:
121 printf("%s: %s limit exceeded\n", NAME, desc);
122 return false;
123 case ENOENT:
124 printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
125 return false;
126 default:
127 printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
128 str_error(rc));
129 return false;
130 }
131
132 return true;
133}
134
135/** Mount root file system
136 *
137 * The operation blocks until the root file system
138 * server is ready for mounting.
139 *
140 * @param[in] fstype Root file system type.
141 *
142 * @return True on success.
143 * @return False on failure.
144 *
145 */
146static bool mount_root(const char *fstype)
147{
148 const char *root_device = "";
149
150 if (str_cmp(fstype, "tmpfs") != 0)
151 root_device = ROOT_DEVICE;
152
153 errno_t rc = vfs_mount_path(ROOT_MOUNT_POINT, fstype, root_device, "",
154 IPC_FLAG_BLOCKING, 0);
155 if (rc == EOK)
156 logctl_set_root();
157
158 bool ret = mount_report("Root file system", ROOT_MOUNT_POINT, fstype,
159 root_device, rc);
160
161 rc = vfs_cwd_set(ROOT_MOUNT_POINT);
162 if (rc != EOK) {
163 printf("%s: Unable to set current directory to %s (%s)\n",
164 NAME, ROOT_MOUNT_POINT, str_error(ret));
165 return false;
166 }
167
168 if ((ret) && (str_cmp(fstype, "tmpfs") == 0)) {
169 printf("%s: Extracting root file system archive\n", NAME);
170 ret = bd_untar(ROOT_DEVICE);
171 }
172
173 return ret;
174}
175
176/** Mount locfs file system
177 *
178 * The operation blocks until the locfs file system
179 * server is ready for mounting.
180 *
181 * @return True on success.
182 * @return False on failure.
183 *
184 */
185static bool mount_locfs(void)
186{
187 errno_t rc = vfs_mount_path(LOCFS_MOUNT_POINT, LOCFS_FS_TYPE, "", "",
188 IPC_FLAG_BLOCKING, 0);
189 return mount_report("Location service file system", LOCFS_MOUNT_POINT,
190 LOCFS_FS_TYPE, NULL, rc);
191}
192
193static errno_t srv_startl(const char *path, ...)
194{
195 vfs_stat_t s;
196 if (vfs_stat_path(path, &s) != EOK) {
197 printf("%s: Unable to stat %s\n", NAME, path);
198 return ENOENT;
199 }
200
201 printf("%s: Starting %s\n", NAME, path);
202
203 va_list ap;
204 const char *arg;
205 int cnt = 0;
206
207 va_start(ap, path);
208 do {
209 arg = va_arg(ap, const char *);
210 cnt++;
211 } while (arg != NULL);
212 va_end(ap);
213
214 va_start(ap, path);
215 task_id_t id;
216 task_wait_t wait;
217 errno_t rc = task_spawn(&id, &wait, path, cnt, ap);
218 va_end(ap);
219
220 if (rc != EOK) {
221 oom_check(rc, path);
222 printf("%s: Error spawning %s (%s)\n", NAME, path,
223 str_error(rc));
224 return rc;
225 }
226
227 if (!id) {
228 printf("%s: Error spawning %s (invalid task id)\n", NAME,
229 path);
230 return EINVAL;
231 }
232
233 task_exit_t texit;
234 int retval;
235 rc = task_wait(&wait, &texit, &retval);
236 if (rc != EOK) {
237 printf("%s: Error waiting for %s (%s)\n", NAME, path,
238 str_error(rc));
239 return rc;
240 }
241
242 if (texit != TASK_EXIT_NORMAL) {
243 printf("%s: Server %s failed to start (unexpectedly "
244 "terminated)\n", NAME, path);
245 return EINVAL;
246 }
247
248 if (retval != 0)
249 printf("%s: Server %s failed to start (exit code %d)\n", NAME,
250 path, retval);
251
252 return retval == 0 ? EOK : EPARTY;
253}
254
255static errno_t console(const char *isvc, const char *osvc)
256{
257 /* Wait for the input service to be ready */
258 service_id_t service_id;
259 errno_t rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
260 if (rc != EOK) {
261 printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
262 str_error(rc));
263 return rc;
264 }
265
266 /* Wait for the output service to be ready */
267 rc = loc_service_get_id(osvc, &service_id, IPC_FLAG_BLOCKING);
268 if (rc != EOK) {
269 printf("%s: Error waiting on %s (%s)\n", NAME, osvc,
270 str_error(rc));
271 return rc;
272 }
273
274 return srv_start(SRV_CONSOLE, isvc, osvc);
275}
276
277#ifdef CONFIG_WINSYS
278
279static errno_t display_server(void)
280{
281 return srv_start(SRV_DISPLAY);
282}
283
284static int app_start(const char *app, const char *arg)
285{
286 printf("%s: Spawning %s\n", NAME, app);
287
288 task_id_t id;
289 task_wait_t wait;
290 errno_t rc = task_spawnl(&id, &wait, app, app, arg, NULL);
291 if (rc != EOK) {
292 oom_check(rc, app);
293 printf("%s: Error spawning %s (%s)\n", NAME, app,
294 str_error(rc));
295 return -1;
296 }
297
298 task_exit_t texit;
299 int retval;
300 rc = task_wait(&wait, &texit, &retval);
301 if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
302 printf("%s: Error retrieving retval from %s (%s)\n", NAME,
303 app, str_error(rc));
304 return rc;
305 }
306
307 return retval;
308}
309
310#endif
311
312static void getterm(const char *svc, const char *app, bool msg)
313{
314 if (msg) {
315 printf("%s: Spawning %s %s %s --msg --wait -- %s\n", NAME,
316 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
317
318 errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
319 LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
320 if (rc != EOK) {
321 oom_check(rc, APP_GETTERM);
322 printf("%s: Error spawning %s %s %s --msg --wait -- %s\n",
323 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
324 }
325 } else {
326 printf("%s: Spawning %s %s %s --wait -- %s\n", NAME,
327 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
328
329 errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
330 LOCFS_MOUNT_POINT, "--wait", "--", app, NULL);
331 if (rc != EOK) {
332 oom_check(rc, APP_GETTERM);
333 printf("%s: Error spawning %s %s %s --wait -- %s\n",
334 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
335 }
336 }
337}
338
339static bool mount_tmpfs(void)
340{
341 errno_t rc = vfs_mount_path(TMPFS_MOUNT_POINT, TMPFS_FS_TYPE, "", "", 0, 0);
342 return mount_report("Temporary file system", TMPFS_MOUNT_POINT,
343 TMPFS_FS_TYPE, NULL, rc);
344}
345
346/** Init system volume.
347 *
348 * See if system volume is configured. If so, try to wait for it to become
349 * available. If not, create basic directories for live image omde.
350 */
351static errno_t init_sysvol(void)
352{
353 vol_t *vol = NULL;
354 vol_info_t vinfo;
355 volume_id_t *volume_ids = NULL;
356 service_id_t *part_ids = NULL;
357 vol_part_info_t pinfo;
358 size_t nvols;
359 size_t nparts;
360 bool sv_mounted;
361 size_t i;
362 errno_t rc;
363 bool found_cfg;
364 const char **cp;
365
366 rc = vol_create(&vol);
367 if (rc != EOK) {
368 printf("Error contacting volume service.\n");
369 goto error;
370 }
371
372 rc = vol_get_volumes(vol, &volume_ids, &nvols);
373 if (rc != EOK) {
374 printf("Error getting list of volumes.\n");
375 goto error;
376 }
377
378 /* XXX This could be handled more efficiently by volsrv itself */
379 found_cfg = false;
380 for (i = 0; i < nvols; i++) {
381 rc = vol_info(vol, volume_ids[i], &vinfo);
382 if (rc != EOK) {
383 printf("Error getting volume information.\n");
384 rc = EIO;
385 goto error;
386 }
387
388 if (str_cmp(vinfo.path, "/w") == 0) {
389 found_cfg = true;
390 break;
391 }
392 }
393
394 free(volume_ids);
395 volume_ids = NULL;
396
397 if (!found_cfg) {
398 /* Prepare directory structure for live image mode */
399 printf("%s: Creating live image directory structure.\n", NAME);
400 cp = sys_dirs;
401 while (*cp != NULL) {
402 rc = vfs_link_path(*cp, KIND_DIRECTORY, NULL);
403 if (rc != EOK) {
404 printf("%s: Error creating directory '%s'.\n",
405 NAME, *cp);
406 goto error;
407 }
408
409 ++cp;
410 }
411
412 /* Copy initial configuration files */
413 rc = futil_rcopy_contents("/cfg", "/w/cfg");
414 if (rc != EOK)
415 goto error;
416 } else {
417 printf("%s: System volume is configured.\n", NAME);
418
419 /* Wait until system volume is mounted */
420 sv_mounted = false;
421
422 while (true) {
423 rc = vol_get_parts(vol, &part_ids, &nparts);
424 if (rc != EOK) {
425 printf("Error getting list of volumes.\n");
426 goto error;
427 }
428
429 for (i = 0; i < nparts; i++) {
430 rc = vol_part_info(vol, part_ids[i], &pinfo);
431 if (rc != EOK) {
432 printf("Error getting partition "
433 "information.\n");
434 rc = EIO;
435 goto error;
436 }
437
438 if (str_cmp(pinfo.cur_mp, "/w") == 0) {
439 sv_mounted = true;
440 break;
441 }
442 }
443
444 if (sv_mounted)
445 break;
446
447 free(part_ids);
448 part_ids = NULL;
449
450 fibril_sleep(1);
451 printf("Sleeping(1) for system volume.\n");
452 }
453 }
454
455 vol_destroy(vol);
456 return EOK;
457error:
458 vol_destroy(vol);
459 if (volume_ids != NULL)
460 free(volume_ids);
461 if (part_ids != NULL)
462 free(part_ids);
463
464 return rc;
465}
466
467int main(int argc, char *argv[])
468{
469 errno_t rc;
470
471 info_print();
472
473 if (!mount_root(STRING(RDFMT))) {
474 printf("%s: Exiting\n", NAME);
475 return 1;
476 }
477
478 /* Make sure file systems are running. */
479 if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
480 srv_start("/srv/fs/tmpfs");
481 if (str_cmp(STRING(RDFMT), "exfat") != 0)
482 srv_start("/srv/fs/exfat");
483 if (str_cmp(STRING(RDFMT), "fat") != 0)
484 srv_start("/srv/fs/fat");
485 srv_start("/srv/fs/cdfs");
486 srv_start("/srv/fs/mfs");
487
488 srv_start("/srv/klog");
489 srv_start("/srv/fs/locfs");
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 init_sysvol();
506
507 srv_start("/srv/taskmon");
508
509 srv_start("/srv/net/loopip");
510 srv_start("/srv/net/ethip");
511 srv_start("/srv/net/inetsrv");
512 srv_start("/srv/net/tcp");
513 srv_start("/srv/net/udp");
514 srv_start("/srv/net/dnsrsrv");
515 srv_start("/srv/net/dhcp");
516 srv_start("/srv/net/nconfsrv");
517
518 srv_start("/srv/clipboard");
519 srv_start("/srv/hid/remcons");
520
521 srv_start("/srv/hid/input", HID_INPUT);
522 srv_start("/srv/hid/output", HID_OUTPUT);
523 srv_start("/srv/audio/hound");
524
525#ifdef CONFIG_WINSYS
526 if (!config_key_exists("console")) {
527 rc = display_server();
528 if (rc == EOK) {
529 app_start("/app/taskbar", NULL);
530 app_start("/app/terminal", "-topleft");
531 }
532 }
533#endif
534 rc = console(HID_INPUT, HID_OUTPUT);
535 if (rc == EOK) {
536 getterm("term/vc0", "/app/bdsh", true);
537 getterm("term/vc1", "/app/bdsh", false);
538 getterm("term/vc2", "/app/bdsh", false);
539 getterm("term/vc3", "/app/bdsh", false);
540 getterm("term/vc4", "/app/bdsh", false);
541 getterm("term/vc5", "/app/bdsh", false);
542 }
543
544 return 0;
545}
546
547/** @}
548 */
Note: See TracBrowser for help on using the repository browser.