Home | History | Annotate | Line # | Download | only in kern
init_main.c revision 1.279
      1 /*	$NetBSD: init_main.c,v 1.279 2006/10/08 04:28:44 thorpej 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.279 2006/10/08 04:28:44 thorpej 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_fileassoc.h"
     85 
     86 #include "rnd.h"
     87 #include "veriexec.h"
     88 
     89 #include <sys/param.h>
     90 #include <sys/acct.h>
     91 #include <sys/filedesc.h>
     92 #include <sys/file.h>
     93 #include <sys/errno.h>
     94 #include <sys/callout.h>
     95 #include <sys/kernel.h>
     96 #include <sys/kcont.h>
     97 #include <sys/kmem.h>
     98 #include <sys/mount.h>
     99 #include <sys/proc.h>
    100 #include <sys/kthread.h>
    101 #include <sys/resourcevar.h>
    102 #include <sys/signalvar.h>
    103 #include <sys/systm.h>
    104 #include <sys/vnode.h>
    105 #include <sys/tty.h>
    106 #include <sys/conf.h>
    107 #include <sys/disklabel.h>
    108 #include <sys/buf.h>
    109 #include <sys/device.h>
    110 #include <sys/exec.h>
    111 #include <sys/socketvar.h>
    112 #include <sys/protosw.h>
    113 #include <sys/reboot.h>
    114 #include <sys/user.h>
    115 #include <sys/sysctl.h>
    116 #include <sys/event.h>
    117 #include <sys/mbuf.h>
    118 #ifdef FAST_IPSEC
    119 #include <netipsec/ipsec.h>
    120 #endif
    121 #ifdef SYSVSHM
    122 #include <sys/shm.h>
    123 #endif
    124 #ifdef SYSVSEM
    125 #include <sys/sem.h>
    126 #endif
    127 #ifdef SYSVMSG
    128 #include <sys/msg.h>
    129 #endif
    130 #ifdef P1003_1B_SEMAPHORE
    131 #include <sys/ksem.h>
    132 #endif
    133 #include <sys/domain.h>
    134 #include <sys/namei.h>
    135 #if NRND > 0
    136 #include <sys/rnd.h>
    137 #endif
    138 #ifndef PIPE_SOCKETPAIR
    139 #include <sys/pipe.h>
    140 #endif
    141 #ifdef LKM
    142 #include <sys/lkm.h>
    143 #endif
    144 #if NVERIEXEC > 0
    145 #include <sys/verified_exec.h>
    146 #endif /* NVERIEXEC > 0 */
    147 #include <sys/kauth.h>
    148 #include <net80211/ieee80211_netbsd.h>
    149 
    150 #include <sys/syscall.h>
    151 #include <sys/sa.h>
    152 #include <sys/syscallargs.h>
    153 
    154 #ifdef FILEASSOC
    155 #include <sys/fileassoc.h>
    156 #endif /* FILEASSOC */
    157 
    158 #include <ufs/ufs/quota.h>
    159 
    160 #include <miscfs/genfs/genfs.h>
    161 #include <miscfs/syncfs/syncfs.h>
    162 
    163 #include <machine/cpu.h>
    164 
    165 #include <uvm/uvm.h>
    166 
    167 #include <dev/cons.h>
    168 
    169 #include <net/if.h>
    170 #include <net/raw_cb.h>
    171 
    172 #include <secmodel/secmodel.h>
    173 
    174 extern struct proc proc0;
    175 extern struct lwp lwp0;
    176 extern struct cwdinfo cwdi0;
    177 
    178 #ifndef curlwp
    179 struct	lwp *curlwp = &lwp0;
    180 #endif
    181 struct	proc *initproc;
    182 
    183 struct	vnode *rootvp, *swapdev_vp;
    184 int	boothowto;
    185 int	cold = 1;			/* still working on startup */
    186 struct timeval boottime;	        /* time at system startup - will only follow settime deltas */
    187 time_t	rootfstime;			/* recorded root fs time, if known */
    188 
    189 volatile int start_init_exec;		/* semaphore for start_init() */
    190 
    191 static void check_console(struct lwp *l);
    192 static void start_init(void *);
    193 void main(void);
    194 
    195 /*
    196  * System startup; initialize the world, create process 0, mount root
    197  * filesystem, and fork to create init and pagedaemon.  Most of the
    198  * hard work is done in the lower-level initialization routines including
    199  * startup(), which does memory initialization and autoconfiguration.
    200  */
    201 void
    202 main(void)
    203 {
    204 #ifdef __HAVE_TIMECOUNTER
    205 	struct timeval time;
    206 #endif
    207 	struct lwp *l;
    208 	struct proc *p;
    209 	struct pdevinit *pdev;
    210 	int s, error;
    211 	extern struct pdevinit pdevinit[];
    212 	extern void schedcpu(void *);
    213 #ifdef NVNODE_IMPLICIT
    214 	int usevnodes;
    215 #endif
    216 
    217 	/*
    218 	 * Initialize the current LWP pointer (curlwp) before
    219 	 * any possible traps/probes to simplify trap processing.
    220 	 */
    221 	l = &lwp0;
    222 	curlwp = l;
    223 	l->l_cpu = curcpu();
    224 	l->l_proc = &proc0;
    225 	l->l_lid = 1;
    226 
    227 	/*
    228 	 * Attempt to find console and initialize
    229 	 * in case of early panic or other messages.
    230 	 */
    231 	consinit();
    232 
    233 	KERNEL_LOCK_INIT();
    234 
    235 	uvm_init();
    236 
    237 	kmem_init();
    238 
    239 	/* Do machine-dependent initialization. */
    240 	cpu_startup();
    241 
    242 	/* Initialize callouts. */
    243 	callout_startup();
    244 
    245 	/* Initialize the buffer cache */
    246 	bufinit();
    247 
    248 	/*
    249 	 * Initialize mbuf's.  Do this now because we might attempt to
    250 	 * allocate mbufs or mbuf clusters during autoconfiguration.
    251 	 */
    252 	mbinit();
    253 
    254 	/* Initialize sockets. */
    255 	soinit();
    256 
    257 #ifdef KCONT
    258 	/* Initialize kcont. */
    259         kcont_init();
    260 #endif
    261 
    262 	/*
    263 	 * The following things must be done before autoconfiguration.
    264 	 */
    265 	evcnt_init();		/* initialize event counters */
    266 #if NRND > 0
    267 	rnd_init();		/* initialize RNG */
    268 #endif
    269 	/* Initialize the sysctl subsystem. */
    270 	sysctl_init();
    271 
    272 	/* Initialize process and pgrp structures. */
    273 	procinit();
    274 	lwpinit();
    275 
    276 	/* Initialize signal-related data structures. */
    277 	signal_init();
    278 
    279 	/* Create process 0 (the swapper). */
    280 	proc0_init();
    281 
    282 	/*
    283 	 * Charge root for one process.
    284 	 */
    285 	(void)chgproccnt(0, 1);
    286 
    287 	rqinit();
    288 
    289 	/* Initialize the file systems. */
    290 #ifdef NVNODE_IMPLICIT
    291 	/*
    292 	 * If maximum number of vnodes in namei vnode cache is not explicitly
    293 	 * defined in kernel config, adjust the number such as we use roughly
    294 	 * 1.0% of memory for vnode cache (but not less than NVNODE vnodes).
    295 	 */
    296 	usevnodes = (ptoa((unsigned)physmem) / 100) / sizeof(struct vnode);
    297 	if (usevnodes > desiredvnodes)
    298 		desiredvnodes = usevnodes;
    299 #endif
    300 	vfsinit();
    301 
    302 
    303 #ifdef __HAVE_TIMECOUNTER
    304 	inittimecounter();
    305 	ntp_init();
    306 #endif /* __HAVE_TIMECOUNTER */
    307 
    308 	/* Initialize kauth. */
    309 	kauth_init();
    310 
    311 	/* Configure the system hardware.  This will enable interrupts. */
    312 	configure();
    313 
    314 	ubc_init();		/* must be after autoconfig */
    315 
    316 	/* Lock the kernel on behalf of proc0. */
    317 	KERNEL_PROC_LOCK(l);
    318 
    319 #ifdef SYSVSHM
    320 	/* Initialize System V style shared memory. */
    321 	shminit();
    322 #endif
    323 
    324 #ifdef SYSVSEM
    325 	/* Initialize System V style semaphores. */
    326 	seminit();
    327 #endif
    328 
    329 #ifdef SYSVMSG
    330 	/* Initialize System V style message queues. */
    331 	msginit();
    332 #endif
    333 
    334 #ifdef P1003_1B_SEMAPHORE
    335 	/* Initialize posix semaphores */
    336 	ksem_init();
    337 #endif
    338 
    339 	/* Initialize default security model. */
    340 	secmodel_start();
    341 
    342 #ifdef FILEASSOC
    343 	fileassoc_init();
    344 #endif /* FILEASSOC */
    345 
    346 #if NVERIEXEC > 0
    347 	  /*
    348 	   * Initialise the fingerprint operations vectors before
    349 	   * fingerprints can be loaded.
    350 	   */
    351 	veriexec_init_fp_ops();
    352 #endif /* NVERIEXEC > 0 */
    353 
    354 	/* Attach pseudo-devices. */
    355 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
    356 		(*pdev->pdev_attach)(pdev->pdev_count);
    357 
    358 #ifdef	FAST_IPSEC
    359 	/* Attach network crypto subsystem */
    360 	ipsec_attach();
    361 #endif
    362 
    363 	/*
    364 	 * Initialize protocols.  Block reception of incoming packets
    365 	 * until everything is ready.
    366 	 */
    367 	s = splnet();
    368 	ifinit();
    369 	domaininit();
    370 	if_attachdomain();
    371 	splx(s);
    372 
    373 #ifdef GPROF
    374 	/* Initialize kernel profiling. */
    375 	kmstartup();
    376 #endif
    377 
    378 	/* Initialize system accouting. */
    379 	acct_init();
    380 
    381 	/* Kick off timeout driven events by calling first time. */
    382 	schedcpu(NULL);
    383 
    384 	/*
    385 	 * Create process 1 (init(8)).  We do this now, as Unix has
    386 	 * historically had init be process 1, and changing this would
    387 	 * probably upset a lot of people.
    388 	 *
    389 	 * Note that process 1 won't immediately exec init(8), but will
    390 	 * wait for us to inform it that the root file system has been
    391 	 * mounted.
    392 	 */
    393 	if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
    394 		panic("fork init");
    395 
    396 	/*
    397 	 * Create any kernel threads who's creation was deferred because
    398 	 * initproc had not yet been created.
    399 	 */
    400 	kthread_run_deferred_queue();
    401 
    402 	/*
    403 	 * Now that device driver threads have been created, wait for
    404 	 * them to finish any deferred autoconfiguration.  Note we don't
    405 	 * need to lock this semaphore, since we haven't booted any
    406 	 * secondary processors, yet.
    407 	 */
    408 	while (config_pending)
    409 		(void) tsleep(&config_pending, PWAIT, "cfpend", 0);
    410 
    411 	/*
    412 	 * Finalize configuration now that all real devices have been
    413 	 * found.  This needs to be done before the root device is
    414 	 * selected, since finalization may create the root device.
    415 	 */
    416 	config_finalize();
    417 
    418 	/*
    419 	 * Now that autoconfiguration has completed, we can determine
    420 	 * the root and dump devices.
    421 	 */
    422 	cpu_rootconf();
    423 	cpu_dumpconf();
    424 
    425 	/* Mount the root file system. */
    426 	do {
    427 		domountroothook();
    428 		if ((error = vfs_mountroot())) {
    429 			printf("cannot mount root, error = %d\n", error);
    430 			boothowto |= RB_ASKNAME;
    431 			setroot(root_device,
    432 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
    433 		}
    434 	} while (error != 0);
    435 	mountroothook_destroy();
    436 
    437 	/*
    438 	 * Initialise the time-of-day clock, passing the time recorded
    439 	 * in the root filesystem (if any) for use by systems that
    440 	 * don't have a non-volatile time-of-day device.
    441 	 */
    442 	inittodr(rootfstime);
    443 
    444 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
    445 	CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
    446 
    447 	/*
    448 	 * Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to
    449 	 * reference it.
    450 	 */
    451 	error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
    452 	if (error)
    453 		panic("cannot find root vnode, error=%d", error);
    454 	cwdi0.cwdi_cdir = rootvnode;
    455 	VREF(cwdi0.cwdi_cdir);
    456 	VOP_UNLOCK(rootvnode, 0);
    457 	cwdi0.cwdi_rdir = NULL;
    458 
    459 	/*
    460 	 * Now that root is mounted, we can fixup initproc's CWD
    461 	 * info.  All other processes are kthreads, which merely
    462 	 * share proc0's CWD info.
    463 	 */
    464 	initproc->p_cwdi->cwdi_cdir = rootvnode;
    465 	VREF(initproc->p_cwdi->cwdi_cdir);
    466 	initproc->p_cwdi->cwdi_rdir = NULL;
    467 
    468 	/*
    469 	 * Now can look at time, having had a chance to verify the time
    470 	 * from the file system.  Reset p->p_rtime as it may have been
    471 	 * munched in mi_switch() after the time got set.
    472 	 */
    473 	proclist_lock_read();
    474 	s = splsched();
    475 #ifdef __HAVE_TIMECOUNTER
    476 	getmicrotime(&time);
    477 #else
    478 	mono_time = time;
    479 #endif
    480 	boottime = time;
    481 	LIST_FOREACH(p, &allproc, p_list) {
    482 		KASSERT((p->p_flag & P_MARKER) == 0);
    483 		p->p_stats->p_start = time;
    484 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    485 			if (l->l_cpu != NULL)
    486 				l->l_cpu->ci_schedstate.spc_runtime = time;
    487 		}
    488 		p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
    489 	}
    490 	splx(s);
    491 	proclist_unlock_read();
    492 
    493 	/* Create the pageout daemon kernel thread. */
    494 	uvm_swap_init();
    495 	if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
    496 		panic("fork pagedaemon");
    497 
    498 	/* Create the filesystem syncer kernel thread. */
    499 	if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
    500 		panic("fork syncer");
    501 
    502 	/* Create the aiodone daemon kernel thread. */
    503 	if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc,
    504 	    "aiodoned"))
    505 		panic("fork aiodoned");
    506 
    507 #if defined(MULTIPROCESSOR)
    508 	/* Boot the secondary processors. */
    509 	cpu_boot_secondary_processors();
    510 #endif
    511 
    512 	/* Initialize exec structures */
    513 	exec_init(1);
    514 
    515 	/*
    516 	 * Okay, now we can let init(8) exec!  It's off to userland!
    517 	 */
    518 	start_init_exec = 1;
    519 	wakeup(&start_init_exec);
    520 
    521 	/* The scheduler is an infinite loop. */
    522 	uvm_scheduler();
    523 	/* NOTREACHED */
    524 }
    525 
    526 void
    527 setrootfstime(time_t t)
    528 {
    529 	rootfstime = t;
    530 }
    531 
    532 static void
    533 check_console(struct lwp *l)
    534 {
    535 	struct nameidata nd;
    536 	int error;
    537 
    538 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", l);
    539 	error = namei(&nd);
    540 	if (error == 0)
    541 		vrele(nd.ni_vp);
    542 	else if (error == ENOENT)
    543 		printf("warning: no /dev/console\n");
    544 	else
    545 		printf("warning: lookup /dev/console: error %d\n", error);
    546 }
    547 
    548 /*
    549  * List of paths to try when searching for "init".
    550  */
    551 static const char *initpaths[] = {
    552 	"/sbin/init",
    553 	"/sbin/oinit",
    554 	"/sbin/init.bak",
    555 	NULL,
    556 };
    557 
    558 /*
    559  * Start the initial user process; try exec'ing each pathname in "initpaths".
    560  * The program is invoked with one argument containing the boot flags.
    561  */
    562 static void
    563 start_init(void *arg)
    564 {
    565 	struct lwp *l = arg;
    566 	struct proc *p = l->l_proc;
    567 	vaddr_t addr;
    568 	struct sys_execve_args /* {
    569 		syscallarg(const char *) path;
    570 		syscallarg(char * const *) argp;
    571 		syscallarg(char * const *) envp;
    572 	} */ args;
    573 	int options, i, error;
    574 	register_t retval[2];
    575 	char flags[4], *flagsp;
    576 	const char *path, *slash;
    577 	char *ucp, **uap, *arg0, *arg1 = NULL;
    578 	char ipath[129];
    579 	int ipx, len;
    580 
    581 	/*
    582 	 * Now in process 1.
    583 	 */
    584 	strncpy(p->p_comm, "init", MAXCOMLEN);
    585 
    586 	/*
    587 	 * Wait for main() to tell us that it's safe to exec.
    588 	 */
    589 	while (start_init_exec == 0)
    590 		(void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
    591 
    592 	/*
    593 	 * This is not the right way to do this.  We really should
    594 	 * hand-craft a descriptor onto /dev/console to hand to init,
    595 	 * but that's a _lot_ more work, and the benefit from this easy
    596 	 * hack makes up for the "good is the enemy of the best" effect.
    597 	 */
    598 	check_console(l);
    599 
    600 	/*
    601 	 * Need just enough stack to hold the faked-up "execve()" arguments.
    602 	 */
    603 	addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
    604 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
    605                     NULL, UVM_UNKNOWN_OFFSET, 0,
    606                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    607 		    UVM_ADV_NORMAL,
    608                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
    609 		panic("init: couldn't allocate argument space");
    610 	p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE);
    611 
    612 	ipx = 0;
    613 	while (1) {
    614 		if (boothowto & RB_ASKNAME) {
    615 			printf("init path");
    616 			if (initpaths[ipx])
    617 				printf(" (default %s)", initpaths[ipx]);
    618 			printf(": ");
    619 			len = cngetsn(ipath, sizeof(ipath)-1);
    620 			if (len == 0) {
    621 				if (initpaths[ipx])
    622 					path = initpaths[ipx++];
    623 				else
    624 					continue;
    625 			} else {
    626 				ipath[len] = '\0';
    627 				path = ipath;
    628 			}
    629 		} else {
    630 			if ((path = initpaths[ipx++]) == NULL)
    631 				break;
    632 		}
    633 
    634 		ucp = (char *)USRSTACK;
    635 
    636 		/*
    637 		 * Construct the boot flag argument.
    638 		 */
    639 		flagsp = flags;
    640 		*flagsp++ = '-';
    641 		options = 0;
    642 
    643 		if (boothowto & RB_SINGLE) {
    644 			*flagsp++ = 's';
    645 			options = 1;
    646 		}
    647 #ifdef notyet
    648 		if (boothowto & RB_FASTBOOT) {
    649 			*flagsp++ = 'f';
    650 			options = 1;
    651 		}
    652 #endif
    653 
    654 		/*
    655 		 * Move out the flags (arg 1), if necessary.
    656 		 */
    657 		if (options != 0) {
    658 			*flagsp++ = '\0';
    659 			i = flagsp - flags;
    660 #ifdef DEBUG
    661 			printf("init: copying out flags `%s' %d\n", flags, i);
    662 #endif
    663 			arg1 = STACK_ALLOC(ucp, i);
    664 			ucp = STACK_MAX(arg1, i);
    665 			(void)copyout((caddr_t)flags, arg1, i);
    666 		}
    667 
    668 		/*
    669 		 * Move out the file name (also arg 0).
    670 		 */
    671 		i = strlen(path) + 1;
    672 #ifdef DEBUG
    673 		printf("init: copying out path `%s' %d\n", path, i);
    674 #else
    675 		if (boothowto & RB_ASKNAME || path != initpaths[0])
    676 			printf("init: trying %s\n", path);
    677 #endif
    678 		arg0 = STACK_ALLOC(ucp, i);
    679 		ucp = STACK_MAX(arg0, i);
    680 		(void)copyout(path, arg0, i);
    681 
    682 		/*
    683 		 * Move out the arg pointers.
    684 		 */
    685 		ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES);
    686 		uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
    687 		SCARG(&args, path) = arg0;
    688 		SCARG(&args, argp) = uap;
    689 		SCARG(&args, envp) = NULL;
    690 		slash = strrchr(path, '/');
    691 		if (slash)
    692 			(void)suword((caddr_t)uap++,
    693 			    (long)arg0 + (slash + 1 - path));
    694 		else
    695 			(void)suword((caddr_t)uap++, (long)arg0);
    696 		if (options != 0)
    697 			(void)suword((caddr_t)uap++, (long)arg1);
    698 		(void)suword((caddr_t)uap++, 0);	/* terminator */
    699 
    700 		/*
    701 		 * Now try to exec the program.  If can't for any reason
    702 		 * other than it doesn't exist, complain.
    703 		 */
    704 		error = sys_execve(l, &args, retval);
    705 		if (error == 0 || error == EJUSTRETURN) {
    706 			KERNEL_PROC_UNLOCK(l);
    707 			return;
    708 		}
    709 		printf("exec %s: error %d\n", path, error);
    710 	}
    711 	printf("init: not found\n");
    712 	panic("no init");
    713 }
    714