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