source: mainline/uspace/srv/system/system.c@ 4285f384

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

Allow physically ejecting CD-ROM using vol eject -s

  • Property mode set to 100644
File size: 14.8 KB
Line 
1/*
2 * Copyright (c) 2025 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 <io/log.h>
40#include <stdio.h>
41#include <stdarg.h>
42#include <vfs/vfs.h>
43#include <stdbool.h>
44#include <errno.h>
45#include <task.h>
46#include <stdlib.h>
47#include <macros.h>
48#include <str.h>
49#include <loc.h>
50#include <str_error.h>
51#include <config.h>
52#include <io/logctl.h>
53#include <vfs/vfs.h>
54#include <vol.h>
55#include <system.h>
56#include <system_srv.h>
57#include "system.h"
58
59#define BANNER_LEFT "######> "
60#define BANNER_RIGHT " <######"
61
62#define LOCFS_FS_TYPE "locfs"
63#define LOCFS_MOUNT_POINT "/loc"
64
65#define TMPFS_FS_TYPE "tmpfs"
66#define TMPFS_MOUNT_POINT "/tmp"
67
68#define SRV_CONSOLE "/srv/hid/console"
69#define APP_GETTERM "/app/getterm"
70
71#define SRV_DISPLAY "/srv/hid/display"
72
73#define HID_INPUT "hid/input"
74#define HID_OUTPUT "hid/output"
75
76#define srv_start(path, ...) \
77 srv_startl(path, path, ##__VA_ARGS__, NULL)
78
79static const char *sys_dirs[] = {
80 "/w/cfg",
81 "/w/data",
82 NULL,
83};
84
85static void system_srv_conn(ipc_call_t *, void *);
86static errno_t system_srv_shutdown(void *);
87
88system_ops_t system_srv_ops = {
89 .shutdown = system_srv_shutdown
90};
91
92/** Print banner */
93static void info_print(void)
94{
95 printf("%s: HelenOS system server\n", NAME);
96}
97
98static void oom_check(errno_t rc, const char *path)
99{
100 if (rc == ENOMEM) {
101 printf("%sOut-of-memory condition detected%s\n", BANNER_LEFT,
102 BANNER_RIGHT);
103 printf("%sBailing out of the boot process after %s%s\n",
104 BANNER_LEFT, path, BANNER_RIGHT);
105 printf("%sMore physical memory is required%s\n", BANNER_LEFT,
106 BANNER_RIGHT);
107 exit(ENOMEM);
108 }
109}
110
111/** Report mount operation success */
112static bool mount_report(const char *desc, const char *mntpt,
113 const char *fstype, const char *dev, errno_t rc)
114{
115 switch (rc) {
116 case EOK:
117 if ((dev != NULL) && (str_cmp(dev, "") != 0))
118 printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
119 fstype, dev);
120 else
121 printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
122 break;
123 case EBUSY:
124 printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
125 return false;
126 case ELIMIT:
127 printf("%s: %s limit exceeded\n", NAME, desc);
128 return false;
129 case ENOENT:
130 printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
131 return false;
132 default:
133 printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
134 str_error(rc));
135 return false;
136 }
137
138 return true;
139}
140
141/** Mount locfs file system
142 *
143 * The operation blocks until the locfs file system
144 * server is ready for mounting.
145 *
146 * @return True on success.
147 * @return False on failure.
148 *
149 */
150static bool mount_locfs(void)
151{
152 errno_t rc = vfs_mount_path(LOCFS_MOUNT_POINT, LOCFS_FS_TYPE, "", "",
153 IPC_FLAG_BLOCKING, 0);
154 return mount_report("Location service file system", LOCFS_MOUNT_POINT,
155 LOCFS_FS_TYPE, NULL, rc);
156}
157
158static errno_t srv_startl(const char *path, ...)
159{
160 vfs_stat_t s;
161 if (vfs_stat_path(path, &s) != EOK) {
162 printf("%s: Unable to stat %s\n", NAME, path);
163 return ENOENT;
164 }
165
166 printf("%s: Starting %s\n", NAME, path);
167
168 va_list ap;
169 const char *arg;
170 int cnt = 0;
171
172 va_start(ap, path);
173 do {
174 arg = va_arg(ap, const char *);
175 cnt++;
176 } while (arg != NULL);
177 va_end(ap);
178
179 va_start(ap, path);
180 task_id_t id;
181 task_wait_t wait;
182 errno_t rc = task_spawn(&id, &wait, path, cnt, ap);
183 va_end(ap);
184
185 if (rc != EOK) {
186 oom_check(rc, path);
187 printf("%s: Error spawning %s (%s)\n", NAME, path,
188 str_error(rc));
189 return rc;
190 }
191
192 if (!id) {
193 printf("%s: Error spawning %s (invalid task id)\n", NAME,
194 path);
195 return EINVAL;
196 }
197
198 task_exit_t texit;
199 int retval;
200 rc = task_wait(&wait, &texit, &retval);
201 if (rc != EOK) {
202 printf("%s: Error waiting for %s (%s)\n", NAME, path,
203 str_error(rc));
204 return rc;
205 }
206
207 if (texit != TASK_EXIT_NORMAL) {
208 printf("%s: Server %s failed to start (unexpectedly "
209 "terminated)\n", NAME, path);
210 return EINVAL;
211 }
212
213 if (retval != 0)
214 printf("%s: Server %s failed to start (exit code %d)\n", NAME,
215 path, retval);
216
217 return retval == 0 ? EOK : EPARTY;
218}
219
220static errno_t console(const char *isvc, const char *osvc)
221{
222 /* Wait for the input service to be ready */
223 service_id_t service_id;
224 errno_t rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
225 if (rc != EOK) {
226 printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
227 str_error(rc));
228 return rc;
229 }
230
231 /* Wait for the output service to be ready */
232 rc = loc_service_get_id(osvc, &service_id, IPC_FLAG_BLOCKING);
233 if (rc != EOK) {
234 printf("%s: Error waiting on %s (%s)\n", NAME, osvc,
235 str_error(rc));
236 return rc;
237 }
238
239 return srv_start(SRV_CONSOLE, isvc, osvc);
240}
241
242#ifdef CONFIG_WINSYS
243
244static errno_t display_server(void)
245{
246 return srv_start(SRV_DISPLAY);
247}
248
249static int app_start(const char *app, const char *arg)
250{
251 printf("%s: Spawning %s\n", NAME, app);
252
253 task_id_t id;
254 task_wait_t wait;
255 errno_t rc = task_spawnl(&id, &wait, app, app, arg, NULL);
256 if (rc != EOK) {
257 oom_check(rc, app);
258 printf("%s: Error spawning %s (%s)\n", NAME, app,
259 str_error(rc));
260 return -1;
261 }
262
263 task_exit_t texit;
264 int retval;
265 rc = task_wait(&wait, &texit, &retval);
266 if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
267 printf("%s: Error retrieving retval from %s (%s)\n", NAME,
268 app, str_error(rc));
269 return rc;
270 }
271
272 return retval;
273}
274
275#endif
276
277static void getterm(const char *svc, const char *app, bool msg)
278{
279 if (msg) {
280 printf("%s: Spawning %s %s %s --msg --wait -- %s\n", NAME,
281 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
282
283 errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
284 LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
285 if (rc != EOK) {
286 oom_check(rc, APP_GETTERM);
287 printf("%s: Error spawning %s %s %s --msg --wait -- %s\n",
288 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
289 }
290 } else {
291 printf("%s: Spawning %s %s %s --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, "--wait", "--", app, NULL);
296 if (rc != EOK) {
297 oom_check(rc, APP_GETTERM);
298 printf("%s: Error spawning %s %s %s --wait -- %s\n",
299 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
300 }
301 }
302}
303
304static bool mount_tmpfs(void)
305{
306 errno_t rc = vfs_mount_path(TMPFS_MOUNT_POINT, TMPFS_FS_TYPE, "", "", 0, 0);
307 return mount_report("Temporary file system", TMPFS_MOUNT_POINT,
308 TMPFS_FS_TYPE, NULL, rc);
309}
310
311/** Init system volume.
312 *
313 * See if system volume is configured. If so, try to wait for it to become
314 * available. If not, create basic directories for live image omde.
315 */
316static errno_t init_sysvol(void)
317{
318 vol_t *vol = NULL;
319 vol_info_t vinfo;
320 volume_id_t *volume_ids = NULL;
321 service_id_t *part_ids = NULL;
322 vol_part_info_t pinfo;
323 size_t nvols;
324 size_t nparts;
325 bool sv_mounted;
326 size_t i;
327 errno_t rc;
328 bool found_cfg;
329 const char **cp;
330
331 rc = vol_create(&vol);
332 if (rc != EOK) {
333 printf("Error contacting volume service.\n");
334 goto error;
335 }
336
337 rc = vol_get_volumes(vol, &volume_ids, &nvols);
338 if (rc != EOK) {
339 printf("Error getting list of volumes.\n");
340 goto error;
341 }
342
343 /* XXX This could be handled more efficiently by volsrv itself */
344 found_cfg = false;
345 for (i = 0; i < nvols; i++) {
346 rc = vol_info(vol, volume_ids[i], &vinfo);
347 if (rc != EOK) {
348 printf("Error getting volume information.\n");
349 rc = EIO;
350 goto error;
351 }
352
353 if (str_cmp(vinfo.path, "/w") == 0) {
354 found_cfg = true;
355 break;
356 }
357 }
358
359 free(volume_ids);
360 volume_ids = NULL;
361
362 if (!found_cfg) {
363 /* Prepare directory structure for live image mode */
364 printf("%s: Creating live image directory structure.\n", NAME);
365 cp = sys_dirs;
366 while (*cp != NULL) {
367 rc = vfs_link_path(*cp, KIND_DIRECTORY, NULL);
368 if (rc != EOK) {
369 printf("%s: Error creating directory '%s'.\n",
370 NAME, *cp);
371 goto error;
372 }
373
374 ++cp;
375 }
376
377 /* Copy initial configuration files */
378 rc = futil_rcopy_contents("/cfg", "/w/cfg");
379 if (rc != EOK)
380 goto error;
381 } else {
382 printf("%s: System volume is configured.\n", NAME);
383
384 /* Verify that system volume is mounted */
385 sv_mounted = false;
386
387 rc = vol_get_parts(vol, &part_ids, &nparts);
388 if (rc != EOK) {
389 printf("Error getting list of volumes.\n");
390 goto error;
391 }
392
393 for (i = 0; i < nparts; i++) {
394 rc = vol_part_info(vol, part_ids[i], &pinfo);
395 if (rc != EOK) {
396 printf("Error getting partition "
397 "information.\n");
398 rc = EIO;
399 goto error;
400 }
401
402 if (str_cmp(pinfo.cur_mp, "/w") == 0) {
403 sv_mounted = true;
404 break;
405 }
406 }
407
408 if (sv_mounted == false) {
409 printf("System volume not found.\n");
410 rc = EIO;
411 goto error;
412 }
413
414 free(part_ids);
415 part_ids = NULL;
416
417 }
418
419 vol_destroy(vol);
420 return EOK;
421error:
422 vol_destroy(vol);
423 if (volume_ids != NULL)
424 free(volume_ids);
425 if (part_ids != NULL)
426 free(part_ids);
427
428 return rc;
429}
430
431/** Perform sytem startup tasks.
432 *
433 * @return EOK on success or an error code
434 */
435static errno_t system_startup(void)
436{
437 errno_t rc;
438
439 /* Make sure file systems are running. */
440 if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
441 srv_start("/srv/fs/tmpfs");
442 if (str_cmp(STRING(RDFMT), "exfat") != 0)
443 srv_start("/srv/fs/exfat");
444 if (str_cmp(STRING(RDFMT), "fat") != 0)
445 srv_start("/srv/fs/fat");
446 srv_start("/srv/fs/cdfs");
447 srv_start("/srv/fs/mfs");
448
449 srv_start("/srv/klog");
450 srv_start("/srv/fs/locfs");
451
452 if (!mount_locfs()) {
453 printf("%s: Exiting\n", NAME);
454 return EIO;
455 }
456
457 mount_tmpfs();
458
459 srv_start("/srv/devman");
460 srv_start("/srv/hid/s3c24xx_uart");
461 srv_start("/srv/hid/s3c24xx_ts");
462
463 srv_start("/srv/bd/vbd");
464 srv_start("/srv/volsrv");
465
466 init_sysvol();
467
468 srv_start("/srv/taskmon");
469
470 srv_start("/srv/net/loopip");
471 srv_start("/srv/net/ethip");
472 srv_start("/srv/net/dhcp");
473 srv_start("/srv/net/inetsrv");
474 srv_start("/srv/net/tcp");
475 srv_start("/srv/net/udp");
476 srv_start("/srv/net/dnsrsrv");
477
478 srv_start("/srv/clipboard");
479 srv_start("/srv/hid/remcons");
480
481 srv_start("/srv/hid/input", HID_INPUT);
482 srv_start("/srv/hid/output", HID_OUTPUT);
483 srv_start("/srv/audio/hound");
484
485#ifdef CONFIG_WINSYS
486 if (!config_key_exists("console")) {
487 rc = display_server();
488 if (rc == EOK) {
489 app_start("/app/taskbar", NULL);
490 app_start("/app/terminal", "-topleft");
491 }
492 }
493#endif
494 rc = console(HID_INPUT, HID_OUTPUT);
495 if (rc == EOK) {
496 getterm("term/vc0", "/app/bdsh", true);
497 getterm("term/vc1", "/app/bdsh", false);
498 getterm("term/vc2", "/app/bdsh", false);
499 getterm("term/vc3", "/app/bdsh", false);
500 getterm("term/vc4", "/app/bdsh", false);
501 getterm("term/vc5", "/app/bdsh", false);
502 }
503
504 return EOK;
505}
506
507/** Perform sytem shutdown tasks.
508 *
509 * @return EOK on success or an error code
510 */
511static errno_t system_sys_shutdown(void)
512{
513 vol_t *vol = NULL;
514 service_id_t *part_ids = NULL;
515 size_t nparts;
516 size_t i;
517 errno_t rc;
518
519 /* Eject all volumes. */
520
521 rc = vol_create(&vol);
522 if (rc != EOK) {
523 log_msg(LOG_DEFAULT, LVL_ERROR, "Error contacting volume "
524 "service.");
525 goto error;
526 }
527
528 rc = vol_get_parts(vol, &part_ids, &nparts);
529 if (rc != EOK) {
530 log_msg(LOG_DEFAULT, LVL_ERROR, "Error getting volume list.");
531 goto error;
532 }
533
534 for (i = 0; i < nparts; i++) {
535 rc = vol_part_eject(vol, part_ids[i], vef_none);
536 if (rc != EOK) {
537 log_msg(LOG_DEFAULT, LVL_ERROR, "Error ejecting "
538 "volume %zu", (size_t)part_ids[i]);
539 goto error;
540 }
541 }
542
543 free(part_ids);
544 vol_destroy(vol);
545 return EOK;
546error:
547 if (part_ids != NULL)
548 free(part_ids);
549 if (vol != NULL)
550 vol_destroy(vol);
551 return rc;
552}
553
554/** Initialize system control service. */
555static errno_t system_srv_init(sys_srv_t *syssrv)
556{
557 port_id_t port;
558 loc_srv_t *srv = NULL;
559 service_id_t sid = 0;
560 errno_t rc;
561
562 (void)system;
563
564 log_msg(LOG_DEFAULT, LVL_DEBUG, "system_srv_init()");
565
566 rc = async_create_port(INTERFACE_SYSTEM, system_srv_conn, syssrv,
567 &port);
568 if (rc != EOK)
569 goto error;
570
571 rc = loc_server_register(NAME, &srv);
572 if (rc != EOK) {
573 log_msg(LOG_DEFAULT, LVL_ERROR,
574 "Failed registering server: %s.", str_error(rc));
575 rc = EEXIST;
576 goto error;
577 }
578
579 rc = loc_service_register(srv, SYSTEM_DEFAULT, &sid);
580 if (rc != EOK) {
581 log_msg(LOG_DEFAULT, LVL_ERROR,
582 "Failed registering service: %s.", str_error(rc));
583 rc = EEXIST;
584 goto error;
585 }
586
587 return EOK;
588error:
589 if (sid != 0)
590 loc_service_unregister(srv, sid);
591 if (srv != NULL)
592 loc_server_unregister(srv);
593 // XXX destroy port
594 return rc;
595}
596
597/** Handle connection to system server. */
598static void system_srv_conn(ipc_call_t *icall, void *arg)
599{
600 sys_srv_t *syssrv = (sys_srv_t *)arg;
601
602 /* Set up protocol structure */
603 system_srv_initialize(&syssrv->srv);
604 syssrv->srv.ops = &system_srv_ops;
605 syssrv->srv.arg = syssrv;
606
607 /* Handle connection */
608 system_conn(icall, &syssrv->srv);
609}
610
611/** System shutdown request.
612 *
613 * @param arg Argument (sys_srv_t *)
614 */
615static errno_t system_srv_shutdown(void *arg)
616{
617 sys_srv_t *syssrv = (sys_srv_t *)arg;
618 errno_t rc;
619
620 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_shutdown");
621
622 rc = system_sys_shutdown();
623 if (rc != EOK) {
624 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_shutdown failed");
625 system_srv_shutdown_failed(&syssrv->srv);
626 }
627
628 log_msg(LOG_DEFAULT, LVL_NOTE, "system_srv_shutdown complete");
629 system_srv_shutdown_complete(&syssrv->srv);
630 return EOK;
631}
632
633int main(int argc, char *argv[])
634{
635 errno_t rc;
636 sys_srv_t srv;
637
638 info_print();
639
640 if (log_init(NAME) != EOK) {
641 printf(NAME ": Failed to initialize logging.\n");
642 return 1;
643 }
644
645 /* Perform startup tasks. */
646 rc = system_startup();
647 if (rc != EOK)
648 return 1;
649
650 rc = system_srv_init(&srv);
651 if (rc != EOK)
652 return 1;
653
654 printf(NAME ": Accepting connections.\n");
655 task_retval(0);
656 async_manager();
657
658 return 0;
659}
660
661/** @}
662 */
Note: See TracBrowser for help on using the repository browser.