1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/* Init that only bootstraps the hurd and runs sh.
Copyright (C) 1993, 1994 Free Software Foundation
This file is part of the GNU Hurd.
The GNU Hurd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
The GNU Hurd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the GNU Hurd; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Written by Michael I. Bushnell and Roland McGrath. */
#include <hurd.h>
#include <hurd/fs.h>
#include <hurd/fsys.h>
#include <device/device.h>
#include <stdio.h>
#include <assert.h>
#include <hurd/paths.h>
#include <sys/reboot.h>
#include <sys/file.h>
#include <unistd.h>
#include <string.h>
#include "startup_reply.h"
#include "startup_S.h"
/* host_reboot flags for when we crash. */
#define CRASH_FLAGS RB_AUTOBOOT
#define BOOT(flags) ((flags & RB_HALT) ? "halt" : "reboot")
/* This structure keeps track of each notified task. */
struct ntfy_task
{
mach_port_t notify_port;
struct ntfy_task *next;
};
/* This structure keeps track of each registered essential task. */
struct ess_task
{
struct ess_task *next;
task_t task_port;
char *name;
};
/* These are linked lists of all of the registered items. */
struct ess_task *ess_tasks;
struct ntfy_task *ntfy_tasks;
int prompt_for_servers = 0;
/* Our receive right */
mach_port_t startup;
/* Ports to the kernel */
mach_port_t host_priv, device_master;
/* Stored information for returning proc and auth startup messages. */
mach_port_t procreply, authreply;
mach_msg_type_name_t procreplytype, authreplytype;
/* Our ports to auth and proc. */
mach_port_t authserver;
mach_port_t procserver;
/* The tasks of auth and proc and the bootstrap filesystem. */
task_t authtask, prottask, fstask;
char **global_argv;
/* Read a string from stdin into BUF. */
static int
getstring (char *buf, size_t bufsize)
{
if (fgets (buf, bufsize, stdin) != NULL && buf[0] != '\0')
{
size_t len = strlen (buf);
if (buf[len - 1] == '\n' || buf[len - 1] == '\r')
buf[len - 1] = '\0';
return 1;
}
return 0;
}
/* Reboot the microkernel. */
void
reboot_mach (int flags)
{
printf ("init: %sing Mach (flags %#x)...\n", BOOT (flags), flags);
while (errno = host_reboot (host_priv, flags))
perror ("host_reboot");
for (;;);
}
void
crash_mach (void)
{
reboot_mach (CRASH_FLAGS);
}
void
reboot_system (int flags)
{
struct ntfy_task *n;
for (n = ntfy_tasks; n != NULL; n = n->next)
{
error_t err;
printf ("init: notifying %p\n", (void *) n->notify_port);
/* XXX need to time out on reply */
err = startup_dosync (n->notify_port);
if (err && err != MACH_SEND_INVALID_DEST)
printf ("init: %p complained: %s\n",
(void *) n->notify_port,
strerror (err));
}
reboot_mach (flags);
}
void
crash (void)
{
reboot_system (CRASH_FLAGS);
}
/* The RPC server demultiplexer. */
static int
request_server (mach_msg_header_t *inp,
mach_msg_header_t *outp)
{
extern int notify_server (), startup_server ();
return (notify_server (inp, outp) ||
startup_server (inp, outp));
}
/* Run SERVER, giving it INIT_PORT_MAX initial ports from PORTS. */
void
run (char *server, mach_port_t *ports)
{
error_t err;
char buf[BUFSIZ];
char *prog = server;
if (prompt_for_servers)
{
printf ("Server file name (default %s): ", server);
if (getstring (buf, sizeof (buf)))
prog = buf;
}
while (1)
{
file_t file;
file = path_lookup (prog, O_EXEC, 0);
if (file == MACH_PORT_NULL)
perror (prog);
else
{
err = file_exec (file, MACH_PORT_NULL, EXEC_NEWTASK,
NULL, 0, /* No args. */
NULL, 0, /* No env. */
NULL, MACH_MSG_TYPE_COPY_SEND, 0, /* No dtable. */
ports, MACH_MSG_TYPE_COPY_SEND, INIT_PORT_MAX,
NULL, 0, /* No info in init ints. */
NULL, 0, NULL, 0);
if (!err)
break;
#ifdef notyet
hurd_perror (prog, err);
#endif
}
printf ("File name for server %s (or nothing to reboot): ", server);
if (getstring (buf, sizeof (buf)))
prog = buf;
else
crash ();
}
printf ("started %s\n", prog);
}
int
main (int argc, char **argv, char **envp)
{
int err;
mach_port_t bootport;
int i;
mach_port_t ports[INIT_PORT_MAX];
mach_port_t consdev;
global_argv = argv;
/* Fetch a port to the bootstrap filesystem, the host priv and
master device ports, and the console */
if (task_get_bootstrap_port (mach_task_self (), &bootport)
|| fsys_getpriv (bootport, &host_priv, &device_master, &fstask)
|| device_open (device_master, D_WRITE, "console", &consdev))
crash_mach ();
stdin = mach_open_devstream (consdev, "w+");
if (stdin == NULL)
crash_mach ();
stdout = stderr = stdin;
setbuf (stdout, NULL);
/* At this point we can use assert to check for errors. */
err = mach_port_allocate (mach_task_self (),
MACH_PORT_RIGHT_RECEIVE, &startup);
assert (!err);
/* Set up the set of ports we will pass to the programs we exec. */
for (i = 0; i < INIT_PORT_MAX; i++)
switch (i)
{
case INIT_PORT_CRDIR:
ports[i] = getcrdir ();
break;
case INIT_PORT_CWDIR:
ports[i] = getcwdir ();
break;
case INIT_PORT_BOOTSTRAP:
ports[i] = startup;
break;
default:
ports[i] = MACH_PORT_NULL;
break;
}
run ("/hurd/proc", ports, &proctask);
run ("/hurd/auth", ports, &authtask);
/* Wait for messages. When both auth and proc have started, we
run launch_system which does the rest of the boot. */
while (1)
{
err = mach_msg_server (request_server, 0, startup);
assert (!err);
}
}
void
launch_system (void)
{
mach_port_t old;
mach_port_t authproc, fsproc;
/* Reply to the proc and auth servers. */
startup_procinit_reply (procreply, procreplytype, 0,
mach_task_self (), authserver,
host_priv, MACH_MSG_TYPE_COPY_SEND,
device_master, MACH_MSG_TYPE_MOVE_SEND);
device_master = 0;
proc_task2proc (procserver, authtask, &authproc);
startup_authinit_reply (authreply, authreplytype, 0, authproc,
MACH_MSG_TYPE_MOVE_SEND);
/* Give the library our auth and proc server ports. */
_hurd_port_init (&_hurd_ports[INIT_PORT_AUTH], authserver);
_hurd_port_init (&_hurd_ports[INIT_PORT_PROC], procserver);
/* Tell the proc server our msgport and where our args and
environment are. */
proc_setmsgport (procserver, startup, &old);
if (old)
mach_port_deallocate (mach_task_self (), old);
proc_setprocargs (procserver, (int) global_argv, (int) environ);
/* Give the bootstrap FS its proc and auth ports. */
{
fsys_t fsys;
if (errno = file_getcontrol (getcrdir (), &fsys))
perror ("file_getcontrol (root)");
else
{
proc_task2proc (procserver, fstask, &fsproc);
if (errno = fsys_init (fsys, fsproc, MACH_MSG_TYPE_MOVE_SEND,
authserver))
perror ("fsys_init");
mach_port_deallocate (mach_task_self (), fsys);
}
}
printf ("Init has completed.\n");
}
error_t
S_startup_procinit (startup_t server,
mach_port_t reply,
mach_msg_type_name_t reply_porttype,
process_t proc,
mach_port_t *startuptask,
auth_t *auth,
mach_port_t *priv, mach_port_t *dev)
{
if (procserver)
/* Only one proc server. */
return EPERM;
procserver = proc;
procreply = reply;
procreplytype = reply_porttype;
/* Save the reply port until we get startup_authinit. */
if (authserver)
launch_system ();
return MIG_NO_REPLY;
}
/* Called by the auth server when it starts up. */
error_t
S_startup_authinit (startup_t server,
mach_port_t reply,
mach_msg_type_name_t reply_porttype,
mach_port_t auth,
mach_port_t *proc)
{
if (authserver)
/* Only one auth server. */
return EPERM;
authserver = auth;
/* Save the reply port until we get startup_procinit. */
authreply = reply;
authreplytype = reply_porttype;
if (procserver)
launch_system ();
return MIG_NO_REPLY;
}
|