init_main.c revision 1.342 1 /* $NetBSD: init_main.c,v 1.342 2008/03/09 15:39:14 rmind 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.342 2008/03/09 15:39:14 rmind 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 #ifdef FAST_IPSEC
137 #include <netipsec/ipsec.h>
138 #endif
139 #ifdef SYSVSHM
140 #include <sys/shm.h>
141 #endif
142 #ifdef SYSVSEM
143 #include <sys/sem.h>
144 #endif
145 #ifdef SYSVMSG
146 #include <sys/msg.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/kauth.h>
167 #include <net80211/ieee80211_netbsd.h>
168
169 #include <sys/syscall.h>
170 #include <sys/syscallargs.h>
171
172 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
173 #include <sys/pax.h>
174 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
175
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 struct timeval time;
250 struct lwp *l;
251 struct proc *p;
252 struct pdevinit *pdev;
253 int s, error;
254 extern struct pdevinit pdevinit[];
255 #ifdef NVNODE_IMPLICIT
256 int usevnodes;
257 #endif
258 CPU_INFO_ITERATOR cii;
259 struct cpu_info *ci;
260
261 l = &lwp0;
262 #ifndef LWP0_CPU_INFO
263 l->l_cpu = curcpu();
264 #endif
265
266 /*
267 * Attempt to find console and initialize
268 * in case of early panic or other messages.
269 */
270 consinit();
271
272 kernel_lock_init();
273
274 uvm_init();
275
276 kmem_init();
277
278 percpu_init();
279
280 /* Initialize the extent manager. */
281 extent_init();
282
283 /* Do machine-dependent initialization. */
284 cpu_startup();
285
286 /* Start module system. */
287 module_init();
288
289 /* Initialize callouts, part 1. */
290 callout_startup();
291
292 /*
293 * Initialize the kernel authorization subsystem and start the
294 * default security model, if any. We need to do this early
295 * enough so that subsystems relying on any of the aforementioned
296 * can work properly. Since the security model may dictate the
297 * credential inheritance policy, it is needed at least before
298 * any process is created, specifically proc0.
299 */
300 kauth_init();
301 secmodel_start();
302
303 /* Initialize the buffer cache */
304 bufinit();
305
306 /* Initialize sockets. */
307 soinit();
308
309 /*
310 * The following things must be done before autoconfiguration.
311 */
312 evcnt_init(); /* initialize event counters */
313 #if NRND > 0
314 rnd_init(); /* initialize RNG */
315 #endif
316
317 /* Initialize process and pgrp structures. */
318 procinit();
319 lwpinit();
320
321 /* Initialize signal-related data structures. */
322 signal_init();
323
324 /* Initialize resource management. */
325 resource_init();
326
327 /* Create process 0 (the swapper). */
328 proc0_init();
329
330 /* Initialize the UID hash table. */
331 uid_init();
332
333 /* Charge root for one process. */
334 (void)chgproccnt(0, 1);
335
336 /* Initialize timekeeping. */
337 time_init();
338
339 /* Initialize the run queues, turnstiles and sleep queues. */
340 mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
341 sched_rqinit();
342 turnstile_init();
343 sleeptab_init(&sleeptab);
344
345 /* Initialize processor-sets */
346 psets_init();
347
348 /* MI initialization of the boot cpu */
349 error = mi_cpu_attach(curcpu());
350 KASSERT(error == 0);
351
352 /*
353 * Initialize mbuf's. Do this now because we might attempt to
354 * allocate mbufs or mbuf clusters during autoconfiguration.
355 */
356 mbinit();
357
358 /* Initialize the sysctl subsystem. */
359 sysctl_init();
360
361 /* Initialize I/O statistics. */
362 iostat_init();
363
364 /* Initialize the log device. */
365 loginit();
366
367 /* Initialize the file systems. */
368 #ifdef NVNODE_IMPLICIT
369 /*
370 * If maximum number of vnodes in namei vnode cache is not explicitly
371 * defined in kernel config, adjust the number such as we use roughly
372 * 1.0% of memory for vnode cache (but not less than NVNODE vnodes).
373 */
374 usevnodes = (ptoa((unsigned)physmem) / 100) / sizeof(struct vnode);
375 if (usevnodes > desiredvnodes)
376 desiredvnodes = usevnodes;
377 #endif
378 vfsinit();
379
380 /* Initialize fstrans. */
381 fstrans_init();
382
383 /* Initialize the file descriptor system. */
384 filedesc_init();
385
386 /* Initialize the select()/poll() system calls. */
387 selsysinit();
388
389 /* Initialize asynchronous I/O. */
390 aio_sysinit();
391
392 /* Initialize message queues. */
393 mqueue_sysinit();
394
395 /* Initialize the system monitor subsystems. */
396 #if NSYSMON_TASKQ > 0
397 sysmon_task_queue_preinit();
398 #endif
399
400 #if NSYSMON_ENVSYS > 0
401 sysmon_envsys_init();
402 #endif
403
404 #if NSYSMON_POWER > 0
405 sysmon_power_init();
406 #endif
407
408 #if NSYSMON_WDOG > 0
409 sysmon_wdog_init();
410 #endif
411
412 inittimecounter();
413 ntp_init();
414
415 /* Initialize the device switch tables. */
416 devsw_init();
417
418 /* Initialize tty subsystem. */
419 tty_init();
420 ttyldisc_init();
421
422 /* Initialize the buffer cache, part 2. */
423 bufinit2();
424
425 /* Initialize the disk wedge subsystem. */
426 dkwedge_init();
427
428 /* Configure the system hardware. This will enable interrupts. */
429 configure();
430
431 #if defined(__SSP__) || defined(__SSP_ALL__)
432 {
433 #ifdef DIAGNOSTIC
434 printf("Initializing SSP:");
435 #endif
436 /*
437 * We initialize ssp here carefully:
438 * 1. after we got some entropy
439 * 2. without calling a function
440 */
441 size_t i;
442 long guard[__arraycount(__stack_chk_guard)];
443
444 arc4randbytes(guard, sizeof(guard));
445 for (i = 0; i < __arraycount(guard); i++)
446 __stack_chk_guard[i] = guard[i];
447 #ifdef DIAGNOSTIC
448 for (i = 0; i < __arraycount(guard); i++)
449 printf("%lx ", guard[i]);
450 printf("\n");
451 #endif
452 }
453 #endif
454 ubc_init(); /* must be after autoconfig */
455
456 /* Lock the kernel on behalf of proc0. */
457 KERNEL_LOCK(1, l);
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) || defined(PAX_ASLR)
487 pax_init();
488 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
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.
548 */
549 while (config_pending)
550 (void) tsleep(&config_pending, PWAIT, "cfpend", hz);
551
552 /*
553 * Load any remaining builtin modules, and hand back temporary
554 * storage to the VM system.
555 */
556 module_init_class(MODULE_CLASS_ANY);
557 module_jettison();
558
559 /*
560 * Finalize configuration now that all real devices have been
561 * found. This needs to be done before the root device is
562 * selected, since finalization may create the root device.
563 */
564 config_finalize();
565
566 /*
567 * Now that autoconfiguration has completed, we can determine
568 * the root and dump devices.
569 */
570 cpu_rootconf();
571 cpu_dumpconf();
572
573 /* Mount the root file system. */
574 do {
575 domountroothook();
576 if ((error = vfs_mountroot())) {
577 printf("cannot mount root, error = %d\n", error);
578 boothowto |= RB_ASKNAME;
579 setroot(root_device,
580 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
581 }
582 } while (error != 0);
583 mountroothook_destroy();
584
585 /*
586 * Initialise the time-of-day clock, passing the time recorded
587 * in the root filesystem (if any) for use by systems that
588 * don't have a non-volatile time-of-day device.
589 */
590 inittodr(rootfstime);
591
592 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
593 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
594
595 /*
596 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
597 * reference it.
598 */
599 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
600 if (error)
601 panic("cannot find root vnode, error=%d", error);
602 cwdi0.cwdi_cdir = rootvnode;
603 VREF(cwdi0.cwdi_cdir);
604 VOP_UNLOCK(rootvnode, 0);
605 cwdi0.cwdi_rdir = NULL;
606
607 /*
608 * Now that root is mounted, we can fixup initproc's CWD
609 * info. All other processes are kthreads, which merely
610 * share proc0's CWD info.
611 */
612 initproc->p_cwdi->cwdi_cdir = rootvnode;
613 VREF(initproc->p_cwdi->cwdi_cdir);
614 initproc->p_cwdi->cwdi_rdir = NULL;
615
616 /*
617 * Now can look at time, having had a chance to verify the time
618 * from the file system. Reset l->l_rtime as it may have been
619 * munched in mi_switch() after the time got set.
620 */
621 getmicrotime(&time);
622 boottime = time;
623 mutex_enter(&proclist_lock);
624 LIST_FOREACH(p, &allproc, p_list) {
625 KASSERT((p->p_flag & PK_MARKER) == 0);
626 mutex_enter(&p->p_smutex);
627 p->p_stats->p_start = time;
628 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
629 lwp_lock(l);
630 memset(&l->l_rtime, 0, sizeof(l->l_rtime));
631 lwp_unlock(l);
632 }
633 mutex_exit(&p->p_smutex);
634 }
635 mutex_exit(&proclist_lock);
636 binuptime(&curlwp->l_stime);
637
638 for (CPU_INFO_FOREACH(cii, ci)) {
639 ci->ci_schedstate.spc_lastmod = time_second;
640 }
641
642 /* Create the pageout daemon kernel thread. */
643 uvm_swap_init();
644 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
645 NULL, NULL, "pgdaemon"))
646 panic("fork pagedaemon");
647
648 /* Create the filesystem syncer kernel thread. */
649 if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
650 NULL, NULL, "ioflush"))
651 panic("fork syncer");
652
653 /* Create the aiodone daemon kernel thread. */
654 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
655 uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
656 panic("fork aiodoned");
657
658 vmem_rehash_start();
659
660 /* Initialize exec structures */
661 exec_init(1);
662
663 /*
664 * Okay, now we can let init(8) exec! It's off to userland!
665 */
666 start_init_exec = 1;
667 wakeup(&start_init_exec);
668
669 /* The scheduler is an infinite loop. */
670 uvm_scheduler();
671 /* NOTREACHED */
672 }
673
674 static void
675 check_console(struct lwp *l)
676 {
677 struct nameidata nd;
678 int error;
679
680 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console");
681 error = namei(&nd);
682 if (error == 0)
683 vrele(nd.ni_vp);
684 else if (error == ENOENT)
685 printf("warning: no /dev/console\n");
686 else
687 printf("warning: lookup /dev/console: error %d\n", error);
688 }
689
690 /*
691 * List of paths to try when searching for "init".
692 */
693 static const char * const initpaths[] = {
694 "/sbin/init",
695 "/sbin/oinit",
696 "/sbin/init.bak",
697 NULL,
698 };
699
700 /*
701 * Start the initial user process; try exec'ing each pathname in "initpaths".
702 * The program is invoked with one argument containing the boot flags.
703 */
704 static void
705 start_init(void *arg)
706 {
707 struct lwp *l = arg;
708 struct proc *p = l->l_proc;
709 vaddr_t addr;
710 struct sys_execve_args /* {
711 syscallarg(const char *) path;
712 syscallarg(char * const *) argp;
713 syscallarg(char * const *) envp;
714 } */ args;
715 int options, i, error;
716 register_t retval[2];
717 char flags[4], *flagsp;
718 const char *path, *slash;
719 char *ucp, **uap, *arg0, *arg1 = NULL;
720 char ipath[129];
721 int ipx, len;
722
723 /*
724 * Now in process 1.
725 */
726 strncpy(p->p_comm, "init", MAXCOMLEN);
727
728 /*
729 * Wait for main() to tell us that it's safe to exec.
730 */
731 while (start_init_exec == 0)
732 (void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
733
734 /*
735 * This is not the right way to do this. We really should
736 * hand-craft a descriptor onto /dev/console to hand to init,
737 * but that's a _lot_ more work, and the benefit from this easy
738 * hack makes up for the "good is the enemy of the best" effect.
739 */
740 check_console(l);
741
742 /*
743 * Need just enough stack to hold the faked-up "execve()" arguments.
744 */
745 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
746 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
747 NULL, UVM_UNKNOWN_OFFSET, 0,
748 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
749 UVM_ADV_NORMAL,
750 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
751 panic("init: couldn't allocate argument space");
752 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
753
754 ipx = 0;
755 while (1) {
756 if (boothowto & RB_ASKNAME) {
757 printf("init path");
758 if (initpaths[ipx])
759 printf(" (default %s)", initpaths[ipx]);
760 printf(": ");
761 len = cngetsn(ipath, sizeof(ipath)-1);
762 if (len == 0) {
763 if (initpaths[ipx])
764 path = initpaths[ipx++];
765 else
766 continue;
767 } else {
768 ipath[len] = '\0';
769 path = ipath;
770 }
771 } else {
772 if ((path = initpaths[ipx++]) == NULL)
773 break;
774 }
775
776 ucp = (char *)USRSTACK;
777
778 /*
779 * Construct the boot flag argument.
780 */
781 flagsp = flags;
782 *flagsp++ = '-';
783 options = 0;
784
785 if (boothowto & RB_SINGLE) {
786 *flagsp++ = 's';
787 options = 1;
788 }
789 #ifdef notyet
790 if (boothowto & RB_FASTBOOT) {
791 *flagsp++ = 'f';
792 options = 1;
793 }
794 #endif
795
796 /*
797 * Move out the flags (arg 1), if necessary.
798 */
799 if (options != 0) {
800 *flagsp++ = '\0';
801 i = flagsp - flags;
802 #ifdef DEBUG
803 printf("init: copying out flags `%s' %d\n", flags, i);
804 #endif
805 arg1 = STACK_ALLOC(ucp, i);
806 ucp = STACK_MAX(arg1, i);
807 (void)copyout((void *)flags, arg1, i);
808 }
809
810 /*
811 * Move out the file name (also arg 0).
812 */
813 i = strlen(path) + 1;
814 #ifdef DEBUG
815 printf("init: copying out path `%s' %d\n", path, i);
816 #else
817 if (boothowto & RB_ASKNAME || path != initpaths[0])
818 printf("init: trying %s\n", path);
819 #endif
820 arg0 = STACK_ALLOC(ucp, i);
821 ucp = STACK_MAX(arg0, i);
822 (void)copyout(path, arg0, i);
823
824 /*
825 * Move out the arg pointers.
826 */
827 ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
828 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
829 SCARG(&args, path) = arg0;
830 SCARG(&args, argp) = uap;
831 SCARG(&args, envp) = NULL;
832 slash = strrchr(path, '/');
833 if (slash)
834 (void)suword((void *)uap++,
835 (long)arg0 + (slash + 1 - path));
836 else
837 (void)suword((void *)uap++, (long)arg0);
838 if (options != 0)
839 (void)suword((void *)uap++, (long)arg1);
840 (void)suword((void *)uap++, 0); /* terminator */
841
842 /*
843 * Now try to exec the program. If can't for any reason
844 * other than it doesn't exist, complain.
845 */
846 error = sys_execve(l, &args, retval);
847 if (error == 0 || error == EJUSTRETURN) {
848 KERNEL_UNLOCK_LAST(l);
849 return;
850 }
851 printf("exec %s: error %d\n", path, error);
852 }
853 printf("init: not found\n");
854 panic("no init");
855 }
856