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