~kris/9p

9hist

ref: 7f152ad5cf06f28ef86f304273f48bebdadc7b8e 9hist/boot/boot.c -rw-r--r-- 5.8 KiB
7f152ad5 — David du Colombier Plan 9 from Bell Labs 1996-05-17 30 years ago
                                                                                
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <fcall.h>
#include "../boot/boot.h"

typedef struct Net	Net;
typedef struct Flavor	Flavor;

char	cputype[NAMELEN];
char	terminal[NAMELEN];
char	sys[2*NAMELEN];
char	username[NAMELEN];
char	bootfile[3*NAMELEN];
char	conffile[NAMELEN];
char 	reply[256];
int	printcol;
int	mflag;
int	fflag;
int	kflag;
int	aflag;

char	*bargv[Nbarg];
int	bargc;

static void	swapproc(void);
static void	recover(Method*);
static Method	*rootserver(char*);

static int
rconv(va_list *arg, Fconv *fp)
{
	char s[ERRLEN];

	USED(arg);

	s[0] = 0;
	errstr(s);
	strconv(s, fp);
	return 0;
}

void
boot(int argc, char *argv[])
{
	int fd;
	Method *mp;
	char cmd[64];
	char flags[6];
	int islocal, ishybrid;

	sleep(1000);

	fmtinstall('r', rconv);

	open("#c/cons", OREAD);
	open("#c/cons", OWRITE);
	open("#c/cons", OWRITE);
	bind("#c", "/dev", MAFTER);
	bind("#e", "/env", MREPL|MCREATE);

#ifdef DEBUG
	print("argc=%d\n", argc);
	for(fd = 0; fd < argc; fd++)
		print("%lux %s ", argv[fd], argv[fd]);
	print("\n");
#endif DEBUG

	ARGBEGIN{
	case 'a':
		aflag = 1;
		break;
	case 'u':
		strcpy(username, ARGF());
		break;
	case 'k':
		kflag = 1;
		break;
	case 'm':
		mflag = 1;
		break;
	case 'f':
		fflag = 1;
		break;
	}ARGEND

	readfile("#e/cputype", cputype, sizeof(cputype));
	readfile("#e/terminal", terminal, sizeof(cputype));
	getconffile(conffile, terminal);

	/*
	 *  pick a method and initialize it
	 */
	mp = rootserver(argc ? *argv : 0);
	(*mp->config)(mp);
	islocal = strcmp(mp->name, "local") == 0;
	ishybrid = strcmp(mp->name, "hybrid") == 0;

	/*
	 *  get/set key or password
	 */
	(*pword)(islocal, mp);

	switch(rfork(RFPROC|RFNAMEG|RFFDG)) {
	case -1:
		print("failed to start recover: %r\n");
		break;
	case 0:
		recover(mp);
		break;
	}

	/*
	 *  connect to the root file system
	 */
	fd = (*mp->connect)();
	if(fd < 0)
		fatal("can't connect to file server");
	nop(fd);
	if(!islocal && !ishybrid){
		if(cfs)
			fd = (*cfs)(fd);
		doauthenticate(fd, mp);
	}
	srvcreate("boot", fd);

	/*
	 *  create the name space, mount the root fs
	 */
	if(bind("/", "/", MREPL) < 0)
		fatal("bind");
	if(mount(fd, "/", MAFTER|MCREATE, "") < 0)
		fatal("mount");
	close(fd);

	/*
	 *  if a local file server exists and it's not
	 *  running, start it and mount it onto /n/kfs
	 */
	if(access("#s/kfs", 0) < 0){
		for(mp = method; mp->name; mp++){
			if(strcmp(mp->name, "local") != 0)
				continue;
			(*mp->config)(mp);
			fd = (*mp->connect)();
			if(fd < 0)
				break;
			mount(fd, "/n/kfs", MAFTER|MCREATE, "") ;
			close(fd);
			break;
		}
	}

	settime(islocal);
	swapproc();

	sprint(cmd, "/%s/init", cputype);
	sprint(flags, "-%s%s%s", cpuflag ? "c" : "t", mflag ? "m" : "", aflag ? "a" : "");
	execl(cmd, "init", flags, 0);
	fatal(cmd);
}

Method*
findmethod(char *a)
{
	Method *mp;
	int i, j;
	char *cp;

	i = strlen(a);
	cp = strchr(a, '!');
	if(cp)
		i = cp - a;
	for(mp = method; mp->name; mp++){
		j = strlen(mp->name);
		if(j > i)
			j = i;
		if(strncmp(a, mp->name, j) == 0)
			break;
	}
	if(mp->name)
		return mp;
	return 0;
}

/*
 *  ask user from whence cometh the root file system
 */
static Method*
rootserver(char *arg)
{
	char prompt[256];
	Method *mp;
	char *cp;
	int n;

	/* make list of methods */
	mp = method;
	n = sprint(prompt, "root is from (%s", mp->name);
	for(mp++; mp->name; mp++)
		n += sprint(prompt+n, ", %s", mp->name);
	sprint(prompt+n, ")");

	/* create default reply */
	readfile("#e/bootargs", reply, sizeof(reply));
	if(reply[0] == 0 && arg != 0)
		strcpy(reply, arg);
	if(reply[0]){
		mp = findmethod(reply);
		if(mp == 0)
			reply[0] = 0;
	}
	if(reply[0] == 0)
		strcpy(reply, method->name);

	/* parse replies */
	for(;;){
		outin(prompt, reply, sizeof(reply));
		mp = findmethod(reply);
		if(mp){
			for(cp = reply; bargc < Nbarg-1 && *cp;){
				while(*cp && *cp != ' ')
					cp++;
				while(*cp == ' ')
					*cp++ = 0;
				if(*cp)
					bargv[bargc++] = cp++;
			}
			cp = strchr(reply, '!');
			if(cp)
				strcpy(sys, cp+1);
			return mp;
		}
	}

	return 0;		/* not reached */
}

int
nop(int fd)
{
	int n;
	Fcall hdr;
	char buf[128];

	print("boot: nop...");
	hdr.type = Tnop;
	hdr.tag = NOTAG;
	n = convS2M(&hdr, buf);
	if(write(fd, buf, n) != n){
		fatal("write nop");
		return 0;
	}
reread:
	n = read(fd, buf, sizeof buf);
	if(n <= 0){
		fatal("read nop");
		return 0;
	}
	if(n == 2)
		goto reread;
	if(convM2S(buf, &hdr, n) == 0) {
		fatal("format nop");
		return 0;
	}
	if(hdr.type != Rnop){
		fatal("not Rnop");
		return 0;
	}
	if(hdr.tag != NOTAG){
		fatal("tag not NOTAG");
		return 0;
	}
	return 1;
}

static void
swapproc(void)
{
	int fd;

	fd = open("#c/swap", OWRITE);
	if(fd < 0){
		warning("opening #c/swap");
		return;
	}
	if(write(fd, "start", 5) <= 0)
		warning("starting swap kproc");
	close(fd);
}

void
reattach(int rec, Method *amp, char *buf)
{
	char *mp;
	int fd, n, sv[2];
	char tmp[64], *p;

	mp = strchr(buf, ' ');
	if(mp == 0)
		goto fail;
	*mp++ = '\0';

	p = strrchr(buf, '/');
	if(p == 0)
		goto fail;

	*p = '\0';

	sprint(tmp, "%s/remote", buf);
	fd = open(tmp, OREAD);
	if(fd < 0)
		goto fail;

	n = read(fd, tmp, sizeof(tmp));
	if(n < 0)
		goto fail;

	close(fd);
	tmp[n-1] = '\0';

	print("boot: Service %s!%s down, wait...\n", buf, tmp);

	p = strrchr(buf, '/');
	if(p == 0)
		goto fail;
	*p = '\0';

	while(plumb(buf, tmp, sv, 0) < 0)
		sleep(30);

	nop(sv[1]);
	doauthenticate(sv[1], amp);

	print("\nboot: Service %s Ok\n", tmp);

	n = sprint(tmp, "%d %s", sv[1], mp);
	if(write(rec, tmp, n) < 0) {
		errstr(tmp);
		print("write recover: %s\n", tmp);
	}
	exits(0);
fail:
	print("recover fail: %s\n", buf);
	exits(0);
}

static void
recover(Method *mp)
{
	int fd, n;
	char buf[256];

	fd = open("#/./recover", ORDWR);
	if(fd < 0)
		exits(0);

	for(;;) {
		n = read(fd, buf, sizeof(buf));
		if(n < 0)
			exits(0);
		buf[n] = '\0';

		if(fork() == 0)
			reattach(fd, mp, buf);
	}
}