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 <ipc/ipc.h>
|
---|
40 | #include <vfs/vfs.h>
|
---|
41 | #include <bool.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <fcntl.h>
|
---|
44 | #include <sys/stat.h>
|
---|
45 | #include <task.h>
|
---|
46 | #include <malloc.h>
|
---|
47 | #include <macros.h>
|
---|
48 | #include <string.h>
|
---|
49 | #include <devmap.h>
|
---|
50 | #include "init.h"
|
---|
51 |
|
---|
52 | #define DEVFS_MOUNT_POINT "/dev"
|
---|
53 |
|
---|
54 | #define SRV_CONSOLE "/srv/console"
|
---|
55 | #define APP_GETTERM "/app/getterm"
|
---|
56 |
|
---|
57 | static void info_print(void)
|
---|
58 | {
|
---|
59 | printf(NAME ": HelenOS init\n");
|
---|
60 | }
|
---|
61 |
|
---|
62 | static bool mount_root(const char *fstype)
|
---|
63 | {
|
---|
64 | char *opts = "";
|
---|
65 | const char *root_dev = "bd/initrd";
|
---|
66 |
|
---|
67 | if (str_cmp(fstype, "tmpfs") == 0)
|
---|
68 | opts = "restore";
|
---|
69 |
|
---|
70 | int rc = mount(fstype, "/", root_dev, opts, IPC_FLAG_BLOCKING);
|
---|
71 |
|
---|
72 | switch (rc) {
|
---|
73 | case EOK:
|
---|
74 | printf(NAME ": Root filesystem mounted, %s at %s\n",
|
---|
75 | fstype, root_dev);
|
---|
76 | break;
|
---|
77 | case EBUSY:
|
---|
78 | printf(NAME ": Root filesystem already mounted\n");
|
---|
79 | return false;
|
---|
80 | case ELIMIT:
|
---|
81 | printf(NAME ": Unable to mount root filesystem\n");
|
---|
82 | return false;
|
---|
83 | case ENOENT:
|
---|
84 | printf(NAME ": Unknown filesystem type (%s)\n", fstype);
|
---|
85 | return false;
|
---|
86 | default:
|
---|
87 | printf(NAME ": Error mounting root filesystem (%d)\n", rc);
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 |
|
---|
94 | static bool mount_devfs(void)
|
---|
95 | {
|
---|
96 | char null[MAX_DEVICE_NAME];
|
---|
97 | int null_id = devmap_null_create();
|
---|
98 |
|
---|
99 | if (null_id == -1) {
|
---|
100 | printf(NAME ": Unable to create null device\n");
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 |
|
---|
104 | snprintf(null, MAX_DEVICE_NAME, "null/%d", null_id);
|
---|
105 | int rc = mount("devfs", DEVFS_MOUNT_POINT, null, "", IPC_FLAG_BLOCKING);
|
---|
106 |
|
---|
107 | switch (rc) {
|
---|
108 | case EOK:
|
---|
109 | printf(NAME ": Device filesystem mounted\n");
|
---|
110 | break;
|
---|
111 | case EBUSY:
|
---|
112 | printf(NAME ": Device filesystem already mounted\n");
|
---|
113 | devmap_null_destroy(null_id);
|
---|
114 | return false;
|
---|
115 | case ELIMIT:
|
---|
116 | printf(NAME ": Unable to mount device filesystem\n");
|
---|
117 | devmap_null_destroy(null_id);
|
---|
118 | return false;
|
---|
119 | case ENOENT:
|
---|
120 | printf(NAME ": Unknown filesystem type (devfs)\n");
|
---|
121 | devmap_null_destroy(null_id);
|
---|
122 | return false;
|
---|
123 | default:
|
---|
124 | printf(NAME ": Error mounting device filesystem (%d)\n", rc);
|
---|
125 | devmap_null_destroy(null_id);
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 |
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | static void spawn(char *fname)
|
---|
133 | {
|
---|
134 | char *argv[2];
|
---|
135 | struct stat s;
|
---|
136 |
|
---|
137 | if (stat(fname, &s) == ENOENT)
|
---|
138 | return;
|
---|
139 |
|
---|
140 | printf(NAME ": Spawning %s\n", fname);
|
---|
141 |
|
---|
142 | argv[0] = fname;
|
---|
143 | argv[1] = NULL;
|
---|
144 |
|
---|
145 | if (!task_spawn(fname, argv))
|
---|
146 | printf(NAME ": Error spawning %s\n", fname);
|
---|
147 | }
|
---|
148 |
|
---|
149 | static void srv_start(char *fname)
|
---|
150 | {
|
---|
151 | char *argv[2];
|
---|
152 | task_id_t id;
|
---|
153 | task_exit_t texit;
|
---|
154 | int rc, retval;
|
---|
155 | struct stat s;
|
---|
156 |
|
---|
157 | if (stat(fname, &s) == ENOENT)
|
---|
158 | return;
|
---|
159 |
|
---|
160 | printf(NAME ": Starting %s\n", fname);
|
---|
161 |
|
---|
162 | argv[0] = fname;
|
---|
163 | argv[1] = NULL;
|
---|
164 |
|
---|
165 | id = task_spawn(fname, argv);
|
---|
166 | if (!id) {
|
---|
167 | printf(NAME ": Error spawning %s\n", fname);
|
---|
168 | return;
|
---|
169 | }
|
---|
170 |
|
---|
171 | rc = task_wait(id, &texit, &retval);
|
---|
172 | if (rc != EOK) {
|
---|
173 | printf(NAME ": Error waiting for %s\n", fname);
|
---|
174 | return;
|
---|
175 | }
|
---|
176 |
|
---|
177 | if ((texit != TASK_EXIT_NORMAL) || (retval != 0)) {
|
---|
178 | printf(NAME ": Server %s failed to start (returned %d)\n",
|
---|
179 | fname, retval);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | static void console(char *dev)
|
---|
184 | {
|
---|
185 | char *argv[3];
|
---|
186 | char hid_in[MAX_DEVICE_NAME];
|
---|
187 | int rc;
|
---|
188 |
|
---|
189 | snprintf(hid_in, MAX_DEVICE_NAME, "%s/%s", DEVFS_MOUNT_POINT, dev);
|
---|
190 |
|
---|
191 | printf(NAME ": Spawning %s with %s\n", SRV_CONSOLE, hid_in);
|
---|
192 |
|
---|
193 | /* Wait for the input device to be ready */
|
---|
194 | dev_handle_t handle;
|
---|
195 | rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING);
|
---|
196 |
|
---|
197 | if (rc == EOK) {
|
---|
198 | argv[0] = SRV_CONSOLE;
|
---|
199 | argv[1] = hid_in;
|
---|
200 | argv[2] = NULL;
|
---|
201 |
|
---|
202 | if (!task_spawn(SRV_CONSOLE, argv))
|
---|
203 | printf(NAME ": Error spawning %s with %s\n", SRV_CONSOLE, hid_in);
|
---|
204 | } else
|
---|
205 | printf(NAME ": Error waiting on %s\n", hid_in);
|
---|
206 | }
|
---|
207 |
|
---|
208 | static void getterm(char *dev, char *app)
|
---|
209 | {
|
---|
210 | char *argv[4];
|
---|
211 | char term[MAX_DEVICE_NAME];
|
---|
212 | int rc;
|
---|
213 |
|
---|
214 | snprintf(term, MAX_DEVICE_NAME, "%s/%s", DEVFS_MOUNT_POINT, dev);
|
---|
215 |
|
---|
216 | printf(NAME ": Spawning %s with %s %s\n", APP_GETTERM, term, app);
|
---|
217 |
|
---|
218 | /* Wait for the terminal device to be ready */
|
---|
219 | dev_handle_t handle;
|
---|
220 | rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING);
|
---|
221 |
|
---|
222 | if (rc == EOK) {
|
---|
223 | argv[0] = APP_GETTERM;
|
---|
224 | argv[1] = term;
|
---|
225 | argv[2] = app;
|
---|
226 | argv[3] = NULL;
|
---|
227 |
|
---|
228 | if (!task_spawn(APP_GETTERM, argv))
|
---|
229 | printf(NAME ": Error spawning %s with %s %s\n", APP_GETTERM,
|
---|
230 | term, app);
|
---|
231 | } else
|
---|
232 | printf(NAME ": Error waiting on %s\n", term);
|
---|
233 | }
|
---|
234 |
|
---|
235 | static void mount_scratch(void)
|
---|
236 | {
|
---|
237 | int rc;
|
---|
238 |
|
---|
239 | printf("Trying to mount null/0 on /scratch... ");
|
---|
240 | fflush(stdout);
|
---|
241 |
|
---|
242 | rc = mount("tmpfs", "/scratch", "null/0", "", 0);
|
---|
243 | if (rc == EOK)
|
---|
244 | printf("OK\n");
|
---|
245 | else
|
---|
246 | printf("Failed\n");
|
---|
247 | }
|
---|
248 |
|
---|
249 | static void mount_data(void)
|
---|
250 | {
|
---|
251 | int rc;
|
---|
252 |
|
---|
253 | printf("Trying to mount bd/disk0 on /data... ");
|
---|
254 | fflush(stdout);
|
---|
255 |
|
---|
256 | rc = mount("fat", "/data", "bd/disk0", "wtcache", 0);
|
---|
257 | if (rc == EOK)
|
---|
258 | printf("OK\n");
|
---|
259 | else
|
---|
260 | printf("Failed\n");
|
---|
261 | }
|
---|
262 |
|
---|
263 | int main(int argc, char *argv[])
|
---|
264 | {
|
---|
265 | info_print();
|
---|
266 |
|
---|
267 | if (!mount_root(STRING(RDFMT))) {
|
---|
268 | printf(NAME ": Exiting\n");
|
---|
269 | return -1;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /* Make sure tmpfs is running. */
|
---|
273 | if (str_cmp(STRING(RDFMT), "tmpfs") != 0) {
|
---|
274 | spawn("/srv/tmpfs");
|
---|
275 | }
|
---|
276 |
|
---|
277 | spawn("/srv/devfs");
|
---|
278 | spawn("/srv/taskmon");
|
---|
279 |
|
---|
280 | if (!mount_devfs()) {
|
---|
281 | printf(NAME ": Exiting\n");
|
---|
282 | return -2;
|
---|
283 | }
|
---|
284 |
|
---|
285 | mount_scratch();
|
---|
286 |
|
---|
287 | spawn("/srv/fhc");
|
---|
288 | spawn("/srv/obio");
|
---|
289 | srv_start("/srv/cuda_adb");
|
---|
290 | srv_start("/srv/i8042");
|
---|
291 | srv_start("/srv/adb_ms");
|
---|
292 | srv_start("/srv/char_ms");
|
---|
293 |
|
---|
294 | spawn("/srv/fb");
|
---|
295 | spawn("/srv/kbd");
|
---|
296 | console("hid_in/kbd");
|
---|
297 |
|
---|
298 | spawn("/srv/clip");
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * Start these synchronously so that mount_data() can be
|
---|
302 | * non-blocking.
|
---|
303 | */
|
---|
304 | #ifdef CONFIG_START_BD
|
---|
305 | srv_start("/srv/ata_bd");
|
---|
306 | srv_start("/srv/gxe_bd");
|
---|
307 | #else
|
---|
308 | (void) srv_start;
|
---|
309 | #endif
|
---|
310 |
|
---|
311 | #ifdef CONFIG_MOUNT_DATA
|
---|
312 | mount_data();
|
---|
313 | #else
|
---|
314 | (void) mount_data;
|
---|
315 | #endif
|
---|
316 |
|
---|
317 | getterm("term/vc0", "/app/bdsh");
|
---|
318 | getterm("term/vc1", "/app/bdsh");
|
---|
319 | getterm("term/vc2", "/app/bdsh");
|
---|
320 | getterm("term/vc3", "/app/bdsh");
|
---|
321 | getterm("term/vc4", "/app/bdsh");
|
---|
322 | getterm("term/vc5", "/app/bdsh");
|
---|
323 | getterm("term/vc6", "/app/klog");
|
---|
324 |
|
---|
325 | return 0;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /** @}
|
---|
329 | */
|
---|