source: mainline/uspace/srv/system/system.c@ 514108e

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

Minimize /app/init, once root is mounted, go to /srv/system

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