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