source: mainline/uspace/app/init/init.c@ 9c4cf0d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9c4cf0d was b19e892, checked in by Jakub Jermar <jakub@…>, 9 years ago

Merge open() with posix_open() and provide vfs_lookup_open() instead

vfs_lookup_open() is really just a convenience wrapper around
vfs_lookup() and vfs_open().

  • Property mode set to 100644
File size: 9.5 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 Init
30 * @brief Init process for user space environment configuration.
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <stdio.h>
38#include <unistd.h>
39#include <stdarg.h>
40#include <vfs/vfs.h>
41#include <stdbool.h>
42#include <errno.h>
43#include <task.h>
44#include <malloc.h>
45#include <macros.h>
46#include <str.h>
47#include <loc.h>
48#include <str_error.h>
49#include <config.h>
50#include <io/logctl.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/console"
63#define APP_GETTERM "/app/getterm"
64
65#define SRV_COMPOSITOR "/srv/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
74/** Print banner */
75static void info_print(void)
76{
77 printf("%s: HelenOS init\n", NAME);
78}
79
80/** Report mount operation success */
81static bool mount_report(const char *desc, const char *mntpt,
82 const char *fstype, const char *dev, int rc)
83{
84 switch (rc) {
85 case EOK:
86 if (dev != NULL)
87 printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
88 fstype, dev);
89 else
90 printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
91 break;
92 case EBUSY:
93 printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
94 return false;
95 case ELIMIT:
96 printf("%s: %s limit exceeded\n", NAME, desc);
97 return false;
98 case ENOENT:
99 printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
100 return false;
101 default:
102 printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
103 str_error(rc));
104 return false;
105 }
106
107 return true;
108}
109
110/** Mount root filesystem
111 *
112 * The operation blocks until the root filesystem
113 * server is ready for mounting.
114 *
115 * @param[in] fstype Root filesystem type.
116 *
117 * @return True on success.
118 * @return False on failure.
119 *
120 */
121static bool mount_root(const char *fstype)
122{
123 const char *opts = "";
124
125 if (str_cmp(fstype, "tmpfs") == 0)
126 opts = "restore";
127
128 int rc = vfs_mount_path(ROOT_MOUNT_POINT, fstype, ROOT_DEVICE, opts,
129 IPC_FLAG_BLOCKING, 0);
130 if (rc == EOK)
131 logctl_set_root();
132 return mount_report("Root filesystem", ROOT_MOUNT_POINT, fstype,
133 ROOT_DEVICE, rc);
134}
135
136/** Mount locfs filesystem
137 *
138 * The operation blocks until the locfs filesystem
139 * server is ready for mounting.
140 *
141 * @return True on success.
142 * @return False on failure.
143 *
144 */
145static bool mount_locfs(void)
146{
147 int rc = vfs_mount_path(LOCFS_MOUNT_POINT, LOCFS_FS_TYPE, "", "",
148 IPC_FLAG_BLOCKING, 0);
149 return mount_report("Location service filesystem", LOCFS_MOUNT_POINT,
150 LOCFS_FS_TYPE, NULL, rc);
151}
152
153static int srv_startl(const char *path, ...)
154{
155 struct stat s;
156 if (vfs_stat_path(path, &s) != EOK) {
157 printf("%s: Unable to stat %s\n", NAME, path);
158 return ENOENT;
159 }
160
161 printf("%s: Starting %s\n", NAME, path);
162
163 va_list ap;
164 const char *arg;
165 int cnt = 0;
166
167 va_start(ap, path);
168 do {
169 arg = va_arg(ap, const char *);
170 cnt++;
171 } while (arg != NULL);
172 va_end(ap);
173
174 va_start(ap, path);
175 task_id_t id;
176 task_wait_t wait;
177 int rc = task_spawn(&id, &wait, path, cnt, ap);
178 va_end(ap);
179
180 if (rc != EOK) {
181 printf("%s: Error spawning %s (%s)\n", NAME, path,
182 str_error(rc));
183 return rc;
184 }
185
186 if (!id) {
187 printf("%s: Error spawning %s (invalid task id)\n", NAME,
188 path);
189 return EINVAL;
190 }
191
192 task_exit_t texit;
193 int retval;
194 rc = task_wait(&wait, &texit, &retval);
195 if (rc != EOK) {
196 printf("%s: Error waiting for %s (%s)\n", NAME, path,
197 str_error(rc));
198 return rc;
199 }
200
201 if (texit != TASK_EXIT_NORMAL) {
202 printf("%s: Server %s failed to start (unexpectedly "
203 "terminated)\n", NAME, path);
204 return EINVAL;
205 }
206
207 if (retval != 0)
208 printf("%s: Server %s failed to start (exit code %d)\n", NAME,
209 path, retval);
210
211 return retval;
212}
213
214static int console(const char *isvc, const char *osvc)
215{
216 /* Wait for the input service to be ready */
217 service_id_t service_id;
218 int rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
219 if (rc != EOK) {
220 printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
221 str_error(rc));
222 return rc;
223 }
224
225 /* Wait for the output service to be ready */
226 rc = loc_service_get_id(osvc, &service_id, IPC_FLAG_BLOCKING);
227 if (rc != EOK) {
228 printf("%s: Error waiting on %s (%s)\n", NAME, osvc,
229 str_error(rc));
230 return rc;
231 }
232
233 return srv_start(SRV_CONSOLE, isvc, osvc);
234}
235
236static int compositor(const char *isvc, const char *name)
237{
238 /* Wait for the input service to be ready */
239 service_id_t service_id;
240 int rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
241 if (rc != EOK) {
242 printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
243 str_error(rc));
244 return rc;
245 }
246
247 return srv_start(SRV_COMPOSITOR, isvc, name);
248}
249
250static int gui_start(const char *app, const char *srv_name)
251{
252 char winreg[50];
253 snprintf(winreg, sizeof(winreg), "%s%s%s", "comp", srv_name, "/winreg");
254
255 printf("%s: Spawning %s %s\n", NAME, app, winreg);
256
257 task_id_t id;
258 task_wait_t wait;
259 int rc = task_spawnl(&id, &wait, app, app, winreg, NULL);
260 if (rc != EOK) {
261 printf("%s: Error spawning %s %s (%s)\n", NAME, app,
262 winreg, str_error(rc));
263 return -1;
264 }
265
266 task_exit_t texit;
267 int retval;
268 rc = task_wait(&wait, &texit, &retval);
269 if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
270 printf("%s: Error retrieving retval from %s (%s)\n", NAME,
271 app, str_error(rc));
272 return -1;
273 }
274
275 return retval;
276}
277
278static void getterm(const char *svc, const char *app, bool msg)
279{
280 if (msg) {
281 printf("%s: Spawning %s %s %s --msg --wait -- %s\n", NAME,
282 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
283
284 int rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
285 LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
286 if (rc != EOK)
287 printf("%s: Error spawning %s %s %s --msg --wait -- %s\n",
288 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
289 } else {
290 printf("%s: Spawning %s %s %s --wait -- %s\n", NAME,
291 APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
292
293 int rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
294 LOCFS_MOUNT_POINT, "--wait", "--", app, NULL);
295 if (rc != EOK)
296 printf("%s: Error spawning %s %s %s --wait -- %s\n",
297 NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
298 }
299}
300
301static bool mount_tmpfs(void)
302{
303 int rc = vfs_mount_path(TMPFS_MOUNT_POINT, TMPFS_FS_TYPE, "", "", 0, 0);
304 return mount_report("Temporary filesystem", TMPFS_MOUNT_POINT,
305 TMPFS_FS_TYPE, NULL, rc);
306}
307
308int main(int argc, char *argv[])
309{
310 int rc;
311
312 info_print();
313
314 if (!mount_root(STRING(RDFMT))) {
315 printf("%s: Exiting\n", NAME);
316 return 1;
317 }
318
319 /* Make sure tmpfs is running. */
320 if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
321 srv_start("/srv/tmpfs");
322
323 srv_start("/srv/klog");
324 srv_start("/srv/locfs");
325 srv_start("/srv/taskmon");
326
327 if (!mount_locfs()) {
328 printf("%s: Exiting\n", NAME);
329 return 2;
330 }
331
332 mount_tmpfs();
333
334 srv_start("/srv/devman");
335 srv_start("/srv/apic");
336 srv_start("/srv/i8259");
337 srv_start("/srv/icp-ic");
338 srv_start("/srv/obio");
339 srv_start("/srv/cuda_adb");
340 srv_start("/srv/s3c24xx_uart");
341 srv_start("/srv/s3c24xx_ts");
342
343 srv_start("/srv/vbd");
344 srv_start("/srv/volsrv");
345
346 srv_start("/srv/loopip");
347 srv_start("/srv/ethip");
348 srv_start("/srv/inetsrv");
349 srv_start("/srv/tcp");
350 srv_start("/srv/udp");
351 srv_start("/srv/dnsrsrv");
352 srv_start("/srv/dhcp");
353 srv_start("/srv/nconfsrv");
354
355 srv_start("/srv/clipboard");
356 srv_start("/srv/remcons");
357
358 srv_start("/srv/input", HID_INPUT);
359 srv_start("/srv/output", HID_OUTPUT);
360 srv_start("/srv/hound");
361
362 if (!config_key_exists("console")) {
363 rc = compositor(HID_INPUT, HID_COMPOSITOR_SERVER);
364 if (rc == EOK) {
365 gui_start("/app/barber", HID_COMPOSITOR_SERVER);
366 gui_start("/app/vlaunch", HID_COMPOSITOR_SERVER);
367 gui_start("/app/vterm", HID_COMPOSITOR_SERVER);
368 }
369 }
370
371 rc = console(HID_INPUT, HID_OUTPUT);
372 if (rc == EOK) {
373 getterm("term/vc0", "/app/bdsh", true);
374 getterm("term/vc1", "/app/bdsh", false);
375 getterm("term/vc2", "/app/bdsh", false);
376 getterm("term/vc3", "/app/bdsh", false);
377 getterm("term/vc4", "/app/bdsh", false);
378 getterm("term/vc5", "/app/bdsh", false);
379 }
380
381 return 0;
382}
383
384/** @}
385 */
Note: See TracBrowser for help on using the repository browser.