init_main.c revision 1.304 1 /* $NetBSD: init_main.c,v 1.304 2007/06/17 13:34:43 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
37 */
38
39 /*
40 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.304 2007/06/17 13:34:43 yamt Exp $");
75
76 #include "opt_ipsec.h"
77 #include "opt_kcont.h"
78 #include "opt_multiprocessor.h"
79 #include "opt_ntp.h"
80 #include "opt_pipe.h"
81 #include "opt_posix.h"
82 #include "opt_syscall_debug.h"
83 #include "opt_sysv.h"
84 #include "opt_systrace.h"
85 #include "opt_fileassoc.h"
86 #include "opt_ktrace.h"
87 #include "opt_pax.h"
88
89 #include "rnd.h"
90 #include "veriexec.h"
91
92 #include <sys/param.h>
93 #include <sys/acct.h>
94 #include <sys/filedesc.h>
95 #include <sys/file.h>
96 #include <sys/errno.h>
97 #include <sys/callout.h>
98 #include <sys/cpu.h>
99 #include <sys/kernel.h>
100 #include <sys/kcont.h>
101 #include <sys/kmem.h>
102 #include <sys/mount.h>
103 #include <sys/proc.h>
104 #include <sys/kthread.h>
105 #include <sys/resourcevar.h>
106 #include <sys/signalvar.h>
107 #include <sys/systm.h>
108 #include <sys/vnode.h>
109 #include <sys/fstrans.h>
110 #include <sys/tty.h>
111 #include <sys/conf.h>
112 #include <sys/disklabel.h>
113 #include <sys/buf.h>
114 #include <sys/device.h>
115 #include <sys/exec.h>
116 #include <sys/socketvar.h>
117 #include <sys/protosw.h>
118 #include <sys/reboot.h>
119 #include <sys/user.h>
120 #include <sys/sysctl.h>
121 #include <sys/event.h>
122 #include <sys/mbuf.h>
123 #include <sys/sched.h>
124 #include <sys/sleepq.h>
125 #include <sys/iostat.h>
126 #include <sys/vmem.h>
127 #ifdef FAST_IPSEC
128 #include <netipsec/ipsec.h>
129 #endif
130 #ifdef SYSVSHM
131 #include <sys/shm.h>
132 #endif
133 #ifdef SYSVSEM
134 #include <sys/sem.h>
135 #endif
136 #ifdef SYSVMSG
137 #include <sys/msg.h>
138 #endif
139 #ifdef SYSTRACE
140 #include <sys/systrace.h>
141 #endif
142 #ifdef P1003_1B_SEMAPHORE
143 #include <sys/ksem.h>
144 #endif
145 #include <sys/domain.h>
146 #include <sys/namei.h>
147 #if NRND > 0
148 #include <sys/rnd.h>
149 #endif
150 #include <sys/pipe.h>
151 #ifdef LKM
152 #include <sys/lkm.h>
153 #endif
154 #if NVERIEXEC > 0
155 #include <sys/verified_exec.h>
156 #endif /* NVERIEXEC > 0 */
157 #ifdef KTRACE
158 #include <sys/ktrace.h>
159 #endif
160 #include <sys/debug.h>
161 #include <sys/kauth.h>
162 #include <net80211/ieee80211_netbsd.h>
163
164 #include <sys/syscall.h>
165 #include <sys/syscallargs.h>
166
167 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD)
168 #include <sys/pax.h>
169 #endif /* PAX_MPROTECT || PAX_SEGVGUARD */
170 #include <ufs/ufs/quota.h>
171
172 #include <miscfs/genfs/genfs.h>
173 #include <miscfs/syncfs/syncfs.h>
174
175 #include <machine/cpu.h>
176
177 #include <uvm/uvm.h>
178
179 #include <dev/cons.h>
180
181 #include <net/if.h>
182 #include <net/raw_cb.h>
183
184 #include <secmodel/secmodel.h>
185
186 extern struct proc proc0;
187 extern struct lwp lwp0;
188 extern struct cwdinfo cwdi0;
189
190 #ifndef curlwp
191 struct lwp *curlwp = &lwp0;
192 #endif
193 struct proc *initproc;
194
195 struct vnode *rootvp, *swapdev_vp;
196 int boothowto;
197 int cold = 1; /* still working on startup */
198 struct timeval boottime; /* time at system startup - will only follow settime deltas */
199 time_t rootfstime; /* recorded root fs time, if known */
200 int ncpu = 0; /* number of CPUs configured, assume 1 */
201
202 volatile int start_init_exec; /* semaphore for start_init() */
203
204 static void check_console(struct lwp *l);
205 static void start_init(void *);
206 void main(void);
207
208 #if defined(__SSP__) || defined(__SSP_ALL__)
209 long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
210 void __stack_chk_fail(void);
211
212 void
213 __stack_chk_fail(void)
214 {
215 panic("stack overflow detected; terminated");
216 }
217 #endif
218
219 void __secmodel_none(void);
220 __weak_alias(secmodel_start,__secmodel_none);
221 void
222 __secmodel_none(void)
223 {
224 return;
225 }
226
227 /*
228 * System startup; initialize the world, create process 0, mount root
229 * filesystem, and fork to create init and pagedaemon. Most of the
230 * hard work is done in the lower-level initialization routines including
231 * startup(), which does memory initialization and autoconfiguration.
232 */
233 void
234 main(void)
235 {
236 #ifdef __HAVE_TIMECOUNTER
237 struct timeval time;
238 #endif
239 struct lwp *l;
240 struct proc *p;
241 struct pdevinit *pdev;
242 int s, error;
243 extern struct pdevinit pdevinit[];
244 #ifdef NVNODE_IMPLICIT
245 int usevnodes;
246 #endif
247
248 /*
249 * Initialize the current LWP pointer (curlwp) before
250 * any possible traps/probes to simplify trap processing.
251 */
252 l = &lwp0;
253 curlwp = l;
254 l->l_cpu = curcpu();
255 l->l_proc = &proc0;
256 l->l_lid = 1;
257
258 /*
259 * Attempt to find console and initialize
260 * in case of early panic or other messages.
261 */
262 consinit();
263
264 KERNEL_LOCK_INIT();
265
266 uvm_init();
267
268 #ifdef DEBUG
269 debug_init();
270 #endif
271
272 kmem_init();
273
274 /* Do machine-dependent initialization. */
275 cpu_startup();
276
277 /* Initialize callouts. */
278 callout_startup();
279
280 /*
281 * Initialize the kernel authorization subsystem and start the
282 * default security model, if any. We need to do this early
283 * enough so that subsystems relying on any of the aforementioned
284 * can work properly. Since the security model may dictate the
285 * credential inheritance policy, it is needed at least before
286 * any process is created, specifically proc0.
287 */
288 kauth_init();
289 secmodel_start();
290
291 /* Initialize the buffer cache */
292 bufinit();
293
294 /*
295 * Initialize mbuf's. Do this now because we might attempt to
296 * allocate mbufs or mbuf clusters during autoconfiguration.
297 */
298 mbinit();
299
300 /* Initialize sockets. */
301 soinit();
302
303 #ifdef KCONT
304 /* Initialize kcont. */
305 kcont_init();
306 #endif
307
308 /*
309 * The following things must be done before autoconfiguration.
310 */
311 evcnt_init(); /* initialize event counters */
312 #if NRND > 0
313 rnd_init(); /* initialize RNG */
314 #endif
315
316 /* Initialize process and pgrp structures. */
317 procinit();
318 lwpinit();
319
320 /* Initialize signal-related data structures. */
321 signal_init();
322
323 /* Create process 0 (the swapper). */
324 proc0_init();
325
326 /*
327 * Charge root for one process.
328 */
329 (void)chgproccnt(0, 1);
330
331 /* Initialize the run queues, turnstiles and sleep queues. */
332 sched_rqinit();
333 turnstile_init();
334 sleeptab_init(&sleeptab);
335
336 /* MI initialization of the boot cpu */
337 error = mi_cpu_attach(curcpu());
338 KASSERT(error == 0);
339
340 /* Initialize the sysctl subsystem. */
341 sysctl_init();
342
343 /* Initialize I/O statistics. */
344 iostat_init();
345
346 /* Initialize the file systems. */
347 #ifdef NVNODE_IMPLICIT
348 /*
349 * If maximum number of vnodes in namei vnode cache is not explicitly
350 * defined in kernel config, adjust the number such as we use roughly
351 * 1.0% of memory for vnode cache (but not less than NVNODE vnodes).
352 */
353 usevnodes = (ptoa((unsigned)physmem) / 100) / sizeof(struct vnode);
354 if (usevnodes > desiredvnodes)
355 desiredvnodes = usevnodes;
356 #endif
357 vfsinit();
358
359 /* Initialize fstrans. */
360 fstrans_init();
361
362 /* Initialize asynchronous I/O. */
363 aio_sysinit();
364
365 #ifdef __HAVE_TIMECOUNTER
366 inittimecounter();
367 ntp_init();
368 #endif /* __HAVE_TIMECOUNTER */
369
370 /* Configure the system hardware. This will enable interrupts. */
371 configure();
372
373 #if defined(__SSP__) || defined(__SSP_ALL__)
374 {
375 #ifdef DIAGNOSTIC
376 printf("Initializing SSP:");
377 #endif
378 /*
379 * We initialize ssp here carefully:
380 * 1. after we got some entropy
381 * 2. without calling a function
382 */
383 size_t i;
384 long guard[__arraycount(__stack_chk_guard)];
385
386 arc4randbytes(guard, sizeof(guard));
387 for (i = 0; i < __arraycount(guard); i++)
388 __stack_chk_guard[i] = guard[i];
389 #ifdef DIAGNOSTIC
390 for (i = 0; i < __arraycount(guard); i++)
391 printf("%lx ", guard[i]);
392 printf("\n");
393 #endif
394 }
395 #endif
396 ubc_init(); /* must be after autoconfig */
397
398 /* Lock the kernel on behalf of proc0. */
399 KERNEL_LOCK(1, l);
400
401 #ifdef SYSTRACE
402 systrace_init();
403 #endif
404
405 #ifdef SYSVSHM
406 /* Initialize System V style shared memory. */
407 shminit();
408 #endif
409
410 #ifdef SYSVSEM
411 /* Initialize System V style semaphores. */
412 seminit();
413 #endif
414
415 #ifdef SYSVMSG
416 /* Initialize System V style message queues. */
417 msginit();
418 #endif
419
420 #ifdef P1003_1B_SEMAPHORE
421 /* Initialize posix semaphores */
422 ksem_init();
423 #endif
424
425 #if NVERIEXEC > 0
426 /*
427 * Initialise the Veriexec subsystem.
428 */
429 veriexec_init();
430 #endif /* NVERIEXEC > 0 */
431
432 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD)
433 pax_init();
434 #endif /* PAX_MPROTECT || PAX_SEGVGUARD */
435
436 /* Attach pseudo-devices. */
437 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
438 (*pdev->pdev_attach)(pdev->pdev_count);
439
440 #ifdef FAST_IPSEC
441 /* Attach network crypto subsystem */
442 ipsec_attach();
443 #endif
444
445 /*
446 * Initialize protocols. Block reception of incoming packets
447 * until everything is ready.
448 */
449 s = splnet();
450 ifinit();
451 domaininit();
452 if_attachdomain();
453 splx(s);
454
455 #ifdef GPROF
456 /* Initialize kernel profiling. */
457 kmstartup();
458 #endif
459
460 /* Initialize system accouting. */
461 acct_init();
462
463 #ifndef PIPE_SOCKETPAIR
464 /* Initialize pipes. */
465 pipe_init();
466 #endif
467
468 /* Setup the scheduler */
469 sched_setup();
470
471 #ifdef KTRACE
472 /* Initialize ktrace. */
473 ktrinit();
474 #endif
475
476 /*
477 * Create process 1 (init(8)). We do this now, as Unix has
478 * historically had init be process 1, and changing this would
479 * probably upset a lot of people.
480 *
481 * Note that process 1 won't immediately exec init(8), but will
482 * wait for us to inform it that the root file system has been
483 * mounted.
484 */
485 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
486 panic("fork init");
487
488 /*
489 * Create any kernel threads who's creation was deferred because
490 * initproc had not yet been created.
491 */
492 kthread_run_deferred_queue();
493
494 /*
495 * Now that device driver threads have been created, wait for
496 * them to finish any deferred autoconfiguration. Note we don't
497 * need to lock this semaphore, since we haven't booted any
498 * secondary processors, yet.
499 */
500 while (config_pending)
501 (void) tsleep(&config_pending, PWAIT, "cfpend", 0);
502
503 /*
504 * Finalize configuration now that all real devices have been
505 * found. This needs to be done before the root device is
506 * selected, since finalization may create the root device.
507 */
508 config_finalize();
509
510 /*
511 * Now that autoconfiguration has completed, we can determine
512 * the root and dump devices.
513 */
514 cpu_rootconf();
515 cpu_dumpconf();
516
517 /* Mount the root file system. */
518 do {
519 domountroothook();
520 if ((error = vfs_mountroot())) {
521 printf("cannot mount root, error = %d\n", error);
522 boothowto |= RB_ASKNAME;
523 setroot(root_device,
524 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
525 }
526 } while (error != 0);
527 mountroothook_destroy();
528
529 /*
530 * Initialise the time-of-day clock, passing the time recorded
531 * in the root filesystem (if any) for use by systems that
532 * don't have a non-volatile time-of-day device.
533 */
534 inittodr(rootfstime);
535
536 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
537 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
538
539 /*
540 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
541 * reference it.
542 */
543 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
544 if (error)
545 panic("cannot find root vnode, error=%d", error);
546 cwdi0.cwdi_cdir = rootvnode;
547 VREF(cwdi0.cwdi_cdir);
548 VOP_UNLOCK(rootvnode, 0);
549 cwdi0.cwdi_rdir = NULL;
550
551 /*
552 * Now that root is mounted, we can fixup initproc's CWD
553 * info. All other processes are kthreads, which merely
554 * share proc0's CWD info.
555 */
556 initproc->p_cwdi->cwdi_cdir = rootvnode;
557 VREF(initproc->p_cwdi->cwdi_cdir);
558 initproc->p_cwdi->cwdi_rdir = NULL;
559
560 /*
561 * Now can look at time, having had a chance to verify the time
562 * from the file system. Reset l->l_rtime as it may have been
563 * munched in mi_switch() after the time got set.
564 */
565 #ifdef __HAVE_TIMECOUNTER
566 getmicrotime(&time);
567 #else
568 mono_time = time;
569 #endif
570 boottime = time;
571 mutex_enter(&proclist_lock);
572 LIST_FOREACH(p, &allproc, p_list) {
573 KASSERT((p->p_flag & PK_MARKER) == 0);
574 mutex_enter(&p->p_smutex);
575 p->p_stats->p_start = time;
576 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
577 lwp_lock(l);
578 l->l_cpu->ci_schedstate.spc_runtime = time;
579 l->l_rtime.tv_sec = l->l_rtime.tv_usec = 0;
580 lwp_unlock(l);
581 }
582 mutex_exit(&p->p_smutex);
583 }
584 mutex_exit(&proclist_lock);
585
586 /* Create the pageout daemon kernel thread. */
587 uvm_swap_init();
588 if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
589 panic("fork pagedaemon");
590
591 /* Create the filesystem syncer kernel thread. */
592 if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
593 panic("fork syncer");
594
595 /* Create the aiodone daemon kernel thread. */
596 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
597 uvm_aiodone_worker, NULL, PVM, IPL_BIO, 0))
598 panic("fork aiodoned");
599
600 vmem_rehash_start();
601
602 #if defined(MULTIPROCESSOR)
603 /* Boot the secondary processors. */
604 cpu_boot_secondary_processors();
605 #endif
606
607 /* Initialize exec structures */
608 exec_init(1);
609
610 /*
611 * Okay, now we can let init(8) exec! It's off to userland!
612 */
613 start_init_exec = 1;
614 wakeup(&start_init_exec);
615
616 /* The scheduler is an infinite loop. */
617 uvm_scheduler();
618 /* NOTREACHED */
619 }
620
621 void
622 setrootfstime(time_t t)
623 {
624 rootfstime = t;
625 }
626
627 static void
628 check_console(struct lwp *l)
629 {
630 struct nameidata nd;
631 int error;
632
633 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", l);
634 error = namei(&nd);
635 if (error == 0)
636 vrele(nd.ni_vp);
637 else if (error == ENOENT)
638 printf("warning: no /dev/console\n");
639 else
640 printf("warning: lookup /dev/console: error %d\n", error);
641 }
642
643 /*
644 * List of paths to try when searching for "init".
645 */
646 static const char *initpaths[] = {
647 "/sbin/init",
648 "/sbin/oinit",
649 "/sbin/init.bak",
650 NULL,
651 };
652
653 /*
654 * Start the initial user process; try exec'ing each pathname in "initpaths".
655 * The program is invoked with one argument containing the boot flags.
656 */
657 static void
658 start_init(void *arg)
659 {
660 struct lwp *l = arg;
661 struct proc *p = l->l_proc;
662 vaddr_t addr;
663 struct sys_execve_args /* {
664 syscallarg(const char *) path;
665 syscallarg(char * const *) argp;
666 syscallarg(char * const *) envp;
667 } */ args;
668 int options, i, error;
669 register_t retval[2];
670 char flags[4], *flagsp;
671 const char *path, *slash;
672 char *ucp, **uap, *arg0, *arg1 = NULL;
673 char ipath[129];
674 int ipx, len;
675
676 /*
677 * Now in process 1.
678 */
679 strncpy(p->p_comm, "init", MAXCOMLEN);
680
681 /*
682 * Wait for main() to tell us that it's safe to exec.
683 */
684 while (start_init_exec == 0)
685 (void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
686
687 /*
688 * This is not the right way to do this. We really should
689 * hand-craft a descriptor onto /dev/console to hand to init,
690 * but that's a _lot_ more work, and the benefit from this easy
691 * hack makes up for the "good is the enemy of the best" effect.
692 */
693 check_console(l);
694
695 /*
696 * Need just enough stack to hold the faked-up "execve()" arguments.
697 */
698 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
699 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
700 NULL, UVM_UNKNOWN_OFFSET, 0,
701 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
702 UVM_ADV_NORMAL,
703 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
704 panic("init: couldn't allocate argument space");
705 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
706
707 ipx = 0;
708 while (1) {
709 if (boothowto & RB_ASKNAME) {
710 printf("init path");
711 if (initpaths[ipx])
712 printf(" (default %s)", initpaths[ipx]);
713 printf(": ");
714 len = cngetsn(ipath, sizeof(ipath)-1);
715 if (len == 0) {
716 if (initpaths[ipx])
717 path = initpaths[ipx++];
718 else
719 continue;
720 } else {
721 ipath[len] = '\0';
722 path = ipath;
723 }
724 } else {
725 if ((path = initpaths[ipx++]) == NULL)
726 break;
727 }
728
729 ucp = (char *)USRSTACK;
730
731 /*
732 * Construct the boot flag argument.
733 */
734 flagsp = flags;
735 *flagsp++ = '-';
736 options = 0;
737
738 if (boothowto & RB_SINGLE) {
739 *flagsp++ = 's';
740 options = 1;
741 }
742 #ifdef notyet
743 if (boothowto & RB_FASTBOOT) {
744 *flagsp++ = 'f';
745 options = 1;
746 }
747 #endif
748
749 /*
750 * Move out the flags (arg 1), if necessary.
751 */
752 if (options != 0) {
753 *flagsp++ = '\0';
754 i = flagsp - flags;
755 #ifdef DEBUG
756 printf("init: copying out flags `%s' %d\n", flags, i);
757 #endif
758 arg1 = STACK_ALLOC(ucp, i);
759 ucp = STACK_MAX(arg1, i);
760 (void)copyout((void *)flags, arg1, i);
761 }
762
763 /*
764 * Move out the file name (also arg 0).
765 */
766 i = strlen(path) + 1;
767 #ifdef DEBUG
768 printf("init: copying out path `%s' %d\n", path, i);
769 #else
770 if (boothowto & RB_ASKNAME || path != initpaths[0])
771 printf("init: trying %s\n", path);
772 #endif
773 arg0 = STACK_ALLOC(ucp, i);
774 ucp = STACK_MAX(arg0, i);
775 (void)copyout(path, arg0, i);
776
777 /*
778 * Move out the arg pointers.
779 */
780 ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
781 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
782 SCARG(&args, path) = arg0;
783 SCARG(&args, argp) = uap;
784 SCARG(&args, envp) = NULL;
785 slash = strrchr(path, '/');
786 if (slash)
787 (void)suword((void *)uap++,
788 (long)arg0 + (slash + 1 - path));
789 else
790 (void)suword((void *)uap++, (long)arg0);
791 if (options != 0)
792 (void)suword((void *)uap++, (long)arg1);
793 (void)suword((void *)uap++, 0); /* terminator */
794
795 /*
796 * Now try to exec the program. If can't for any reason
797 * other than it doesn't exist, complain.
798 */
799 error = sys_execve(l, &args, retval);
800 if (error == 0 || error == EJUSTRETURN) {
801 KERNEL_UNLOCK_LAST(l);
802 return;
803 }
804 printf("exec %s: error %d\n", path, error);
805 }
806 printf("init: not found\n");
807 panic("no init");
808 }
809