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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f47c2dc1 was 8fefd8b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Preserve srv directory structure

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