The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/init_main.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 1995 Terrence R. Lambert
    3  * All rights reserved.
    4  *
    5  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
    6  *      The Regents of the University of California.  All rights reserved.
    7  * (c) UNIX System Laboratories, Inc.
    8  * All or some portions of this file are derived from material licensed
    9  * to the University of California by American Telephone and Telegraph
   10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   11  * the permission of UNIX System Laboratories, Inc.
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  * 3. All advertising materials mentioning features or use of this software
   22  *    must display the following acknowledgement:
   23  *      This product includes software developed by the University of
   24  *      California, Berkeley and its contributors.
   25  * 4. Neither the name of the University nor the names of its contributors
   26  *    may be used to endorse or promote products derived from this software
   27  *    without specific prior written permission.
   28  *
   29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   39  * SUCH DAMAGE.
   40  *
   41  *      @(#)init_main.c 8.9 (Berkeley) 1/21/94
   42  * $FreeBSD$
   43  */
   44 
   45 #include "opt_devfs.h"
   46 #include "opt_init_path.h"
   47 
   48 #include <sys/param.h>
   49 #include <sys/file.h>
   50 #include <sys/filedesc.h>
   51 #include <sys/kernel.h>
   52 #include <sys/mount.h>
   53 #include <sys/sysctl.h>
   54 #include <sys/proc.h>
   55 #include <sys/resourcevar.h>
   56 #include <sys/signalvar.h>
   57 #include <sys/systm.h>
   58 #include <sys/vnode.h>
   59 #include <sys/sysent.h>
   60 #include <sys/reboot.h>
   61 #include <sys/sysproto.h>
   62 #include <sys/vmmeter.h>
   63 #include <sys/unistd.h>
   64 #include <sys/malloc.h>
   65 
   66 #include <machine/cpu.h>
   67 
   68 #include <vm/vm.h>
   69 #include <vm/vm_param.h>
   70 #include <vm/vm_prot.h>
   71 #include <sys/lock.h>
   72 #include <vm/pmap.h>
   73 #include <vm/vm_map.h>
   74 #include <sys/user.h>
   75 #include <sys/copyright.h>
   76 
   77 extern struct linker_set        sysinit_set;    /* XXX */
   78 
   79 extern void __main __P((void));
   80 extern void main __P((void *framep));
   81 
   82 /* Components of the first process -- never freed. */
   83 static struct session session0;
   84 static struct pgrp pgrp0;
   85 struct  proc proc0;
   86 static struct pcred cred0;
   87 static struct procsig procsig0;
   88 static struct filedesc0 filedesc0;
   89 static struct plimit limit0;
   90 static struct vmspace vmspace0;
   91 struct  proc *initproc;
   92 
   93 int cmask = CMASK;
   94 extern  struct user *proc0paddr;
   95 
   96 struct  vnode *rootvp;
   97 int     boothowto = 0;          /* initialized so that it can be patched */
   98 
   99 struct  timeval boottime;
  100 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime,
  101         CTLFLAG_RD, &boottime, timeval, "");
  102 
  103 /*
  104  * Promiscuous argument pass for start_init()
  105  *
  106  * This is a kludge because we use a return from main() rather than a call
  107  * to a new routine in locore.s to kick the kernel alive from locore.s.
  108  */
  109 static void     *init_framep;
  110 
  111 
  112 #if __GNUC__ >= 2
  113 void __main() {}
  114 #endif
  115 
  116 
  117 /*
  118  * This ensures that there is at least one entry so that the sysinit_set
  119  * symbol is not undefined.  A sybsystem ID of SI_SUB_DUMMY is never
  120  * executed.
  121  */
  122 SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL)
  123 
  124 /*
  125  * The sysinit table itself.  Items are checked off as the are run.
  126  * If we want to register new sysinit types, add them to newsysinit.
  127  */
  128 struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items;
  129 struct sysinit **newsysinit;
  130 
  131 /*
  132  * Merge a new sysinit set into the current set, reallocating it if
  133  * necessary.  This can only be called after malloc is running.
  134  */
  135 void
  136 sysinit_add(set)
  137         struct sysinit **set;
  138 {
  139         struct sysinit **newset;
  140         struct sysinit **sipp;
  141         struct sysinit **xipp;
  142         int count = 0;
  143 
  144         if (newsysinit)
  145                 for (sipp = newsysinit; *sipp; sipp++)
  146                         count++;
  147         else
  148                 for (sipp = sysinit; *sipp; sipp++)
  149                         count++;
  150         for (sipp = set; *sipp; sipp++)
  151                 count++;
  152         count++;                /* Trailing NULL */
  153         newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT);
  154         if (newset == NULL)
  155                 panic("cannot malloc for sysinit");
  156         xipp = newset;
  157         if (newsysinit)
  158                 for (sipp = newsysinit; *sipp; sipp++)
  159                         *xipp++ = *sipp;
  160         else
  161                 for (sipp = sysinit; *sipp; sipp++)
  162                         *xipp++ = *sipp;
  163         for (sipp = set; *sipp; sipp++)
  164                 *xipp++ = *sipp;
  165         *xipp = NULL;
  166         if (newsysinit)
  167                 free(newsysinit, M_TEMP);
  168         newsysinit = newset;
  169 }
  170 
  171 /*
  172  * System startup; initialize the world, create process 0, mount root
  173  * filesystem, and fork to create init and pagedaemon.  Most of the
  174  * hard work is done in the lower-level initialization routines including
  175  * startup(), which does memory initialization and autoconfiguration.
  176  *
  177  * This allows simple addition of new kernel subsystems that require
  178  * boot time initialization.  It also allows substitution of subsystem
  179  * (for instance, a scheduler, kernel profiler, or VM system) by object
  180  * module.  Finally, it allows for optional "kernel threads".
  181  */
  182 void
  183 main(framep)
  184         void *framep;
  185 {
  186 
  187         register struct sysinit **sipp;         /* system initialization*/
  188         register struct sysinit **xipp;         /* interior loop of sort*/
  189         register struct sysinit *save;          /* bubble*/
  190 
  191         /*
  192          * Copy the locore.s frame pointer for proc0, this is forked into
  193          * all other processes.
  194          */
  195         init_framep = framep;
  196 
  197 restart:
  198         /*
  199          * Perform a bubble sort of the system initialization objects by
  200          * their subsystem (primary key) and order (secondary key).
  201          */
  202         for (sipp = sysinit; *sipp; sipp++) {
  203                 for (xipp = sipp + 1; *xipp; xipp++) {
  204                         if ((*sipp)->subsystem < (*xipp)->subsystem ||
  205                              ((*sipp)->subsystem == (*xipp)->subsystem &&
  206                               (*sipp)->order < (*xipp)->order))
  207                                 continue;       /* skip*/
  208                         save = *sipp;
  209                         *sipp = *xipp;
  210                         *xipp = save;
  211                 }
  212         }
  213 
  214         /*
  215          * Traverse the (now) ordered list of system initialization tasks.
  216          * Perform each task, and continue on to the next task.
  217          *
  218          * The last item on the list is expected to be the scheduler,
  219          * which will not return.
  220          */
  221         for (sipp = sysinit; *sipp; sipp++) {
  222 
  223                 if ((*sipp)->subsystem == SI_SUB_DUMMY)
  224                         continue;       /* skip dummy task(s)*/
  225 
  226                 if ((*sipp)->subsystem == SI_SUB_DONE)
  227                         continue;
  228 
  229                 switch( (*sipp)->type) {
  230                 case SI_TYPE_DEFAULT:
  231                         /* no special processing*/
  232                         (*((*sipp)->func))((*sipp)->udata);
  233                         break;
  234 
  235                 case SI_TYPE_KTHREAD:
  236 #if !defined(SMP)
  237                         /* kernel thread*/
  238                         if (fork1(&proc0, RFMEM|RFFDG|RFPROC))
  239                                 panic("fork kernel thread");
  240                         cpu_set_fork_handler(pfind(proc0.p_retval[0]),
  241                             (*sipp)->func, (*sipp)->udata);
  242                         break;
  243 #endif
  244 
  245                 case SI_TYPE_KPROCESS:
  246                         if (fork1(&proc0, RFFDG|RFPROC))
  247                                 panic("fork kernel process");
  248                         cpu_set_fork_handler(pfind(proc0.p_retval[0]),
  249                             (*sipp)->func, (*sipp)->udata);
  250                         break;
  251 
  252                 default:
  253                         panic("init_main: unrecognized init type");
  254                 }
  255 
  256                 /* Check off the one we're just done */
  257                 (*sipp)->subsystem = SI_SUB_DONE;
  258 
  259                 /* Check if we've installed more sysinit items via KLD */
  260                 if (newsysinit != NULL) {
  261                         if (sysinit != (struct sysinit **)sysinit_set.ls_items)
  262                                 free(sysinit, M_TEMP);
  263                         sysinit = newsysinit;
  264                         newsysinit = NULL;
  265                         goto restart;
  266                 }
  267         }
  268 
  269         panic("Shouldn't get here!");
  270         /* NOTREACHED*/
  271 }
  272 
  273 
  274 /*
  275  * Start a kernel process.  This is called after a fork() call in
  276  * main() in the file kern/init_main.c.
  277  *
  278  * This function is used to start "internal" daemons.
  279  */
  280 /* ARGSUSED*/
  281 void
  282 kproc_start(udata)
  283         void *udata;
  284 {
  285         struct kproc_desc       *kp = udata;
  286         struct proc             *p = curproc;
  287 
  288 #ifdef DIAGNOSTIC
  289         printf("Start pid=%d <%s>\n",p->p_pid, kp->arg0);
  290 #endif
  291 
  292         /* save a global descriptor, if desired*/
  293         if( kp->global_procpp != NULL)
  294                 *kp->global_procpp      = p;
  295 
  296         /* this is a non-swapped system process*/
  297         p->p_flag |= P_INMEM | P_SYSTEM;
  298 
  299         /* set up arg0 for 'ps', et al*/
  300         strcpy( p->p_comm, kp->arg0);
  301 
  302         /* call the processes' main()...*/
  303         (*kp->func)();
  304 
  305         /* NOTREACHED */
  306         panic("kproc_start: %s", kp->arg0);
  307 }
  308 
  309 
  310 /*
  311  ***************************************************************************
  312  ****
  313  **** The following SYSINIT's belong elsewhere, but have not yet
  314  **** been moved.
  315  ****
  316  ***************************************************************************
  317  */
  318 #ifdef OMIT
  319 /*
  320  * Handled by vfs_mountroot (bad idea) at this time... should be
  321  * done the same as 4.4Lite2.
  322  */
  323 SYSINIT(swapinit, SI_SUB_SWAP, SI_ORDER_FIRST, swapinit, NULL)
  324 #endif  /* OMIT*/
  325 
  326 static void print_caddr_t __P((void *data));
  327 static void
  328 print_caddr_t(data)
  329         void *data;
  330 {
  331         printf("%s", (char *)data);
  332 }
  333 SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright)
  334 
  335 
  336 /*
  337  ***************************************************************************
  338  ****
  339  **** The two following SYSINT's are proc0 specific glue code.  I am not
  340  **** convinced that they can not be safely combined, but their order of
  341  **** operation has been maintained as the same as the original init_main.c
  342  **** for right now.
  343  ****
  344  **** These probably belong in init_proc.c or kern_proc.c, since they
  345  **** deal with proc0 (the fork template process).
  346  ****
  347  ***************************************************************************
  348  */
  349 /* ARGSUSED*/
  350 static void proc0_init __P((void *dummy));
  351 static void
  352 proc0_init(dummy)
  353         void *dummy;
  354 {
  355         register struct proc            *p;
  356         register struct filedesc0       *fdp;
  357         register unsigned i;
  358 
  359         /*
  360          * Initialize the current process pointer (curproc) before
  361          * any possible traps/probes to simplify trap processing.
  362          */
  363         p = &proc0;
  364         curproc = p;                    /* XXX redundant*/
  365 
  366         /*
  367          * Initialize process and pgrp structures.
  368          */
  369         procinit();
  370 
  371         /*
  372          * Initialize sleep queue hash table
  373          */
  374         sleepinit();
  375 
  376         /*
  377          * additional VM structures
  378          */
  379         vm_init2();
  380 
  381         /*
  382          * Create process 0 (the swapper).
  383          */
  384         LIST_INSERT_HEAD(&allproc, p, p_list);
  385         p->p_pgrp = &pgrp0;
  386         LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
  387         LIST_INIT(&pgrp0.pg_members);
  388         LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
  389 
  390         pgrp0.pg_session = &session0;
  391         session0.s_count = 1;
  392         session0.s_leader = p;
  393 
  394         p->p_sysent = &aout_sysvec;
  395 
  396         p->p_flag = P_INMEM | P_SYSTEM;
  397         p->p_stat = SRUN;
  398         p->p_nice = NZERO;
  399         p->p_rtprio.type = RTP_PRIO_NORMAL;
  400         p->p_rtprio.prio = 0;
  401 
  402 /*
  403  * Link for kernel based threads
  404  */
  405         p->p_peers = 0;
  406         p->p_leader = p;
  407 
  408         bcopy("swapper", p->p_comm, sizeof ("swapper"));
  409 
  410         /* Create credentials. */
  411         cred0.p_refcnt = 1;
  412         p->p_cred = &cred0;
  413         p->p_ucred = crget();
  414         p->p_ucred->cr_ngroups = 1;     /* group 0 */
  415 
  416         /* Create procsig. */
  417         p->p_procsig = &procsig0;
  418         p->p_procsig->ps_refcnt = 2;
  419 
  420         /* Create the file descriptor table. */
  421         fdp = &filedesc0;
  422         p->p_fd = &fdp->fd_fd;
  423         fdp->fd_fd.fd_refcnt = 1;
  424         fdp->fd_fd.fd_cmask = cmask;
  425         fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
  426         fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
  427         fdp->fd_fd.fd_nfiles = NDFILE;
  428 
  429         /* Create the limits structures. */
  430         p->p_limit = &limit0;
  431         for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
  432                 limit0.pl_rlimit[i].rlim_cur =
  433                     limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
  434         limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
  435             limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
  436         limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
  437             limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
  438         i = ptoa(cnt.v_free_count);
  439         limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
  440         limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
  441         limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
  442         limit0.p_cpulimit = RLIM_INFINITY;
  443         limit0.p_refcnt = 1;
  444 
  445 
  446         /* Allocate a prototype map so we have something to fork. */
  447         pmap_pinit0(&vmspace0.vm_pmap);
  448         p->p_vmspace = &vmspace0;
  449         vmspace0.vm_refcnt = 1;
  450         vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS),
  451             trunc_page(VM_MAXUSER_ADDRESS));
  452         vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
  453         p->p_addr = proc0paddr;                         /* XXX */
  454 
  455 #ifndef __alpha__               /* XXX what is this? */
  456 #define INCOMPAT_LITES2
  457 #ifdef INCOMPAT_LITES2
  458         /*
  459          * proc0 needs to have a coherent frame base in its stack.
  460          */
  461         cpu_set_init_frame(p, init_framep);                     /* XXX! */
  462 #endif  /* INCOMPAT_LITES2*/
  463 #endif
  464 
  465         /*
  466          * We continue to place resource usage info and signal
  467          * actions in the user struct so they're pageable.
  468          */
  469         p->p_stats = &p->p_addr->u_stats;
  470         p->p_sigacts = &p->p_addr->u_sigacts;
  471 
  472         /*
  473          * Charge root for one process.
  474          */
  475         (void)chgproccnt(0, 1);
  476 
  477         /*
  478          * Initialize the procfs flags (to 0, of course)
  479          */
  480         p->p_stops = p->p_stype = p->p_step = 0;
  481 
  482 }
  483 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL)
  484 
  485 /* ARGSUSED*/
  486 static void proc0_post __P((void *dummy));
  487 static void
  488 proc0_post(dummy)
  489         void *dummy;
  490 {
  491         struct timespec ts;
  492 
  493         /*
  494          * Now we can look at the time, having had a chance to verify the
  495          * time from the file system.  Pretend that proc0 started now.
  496          */
  497         microtime(&proc0.p_stats->p_start);
  498         proc0.p_runtime = 0;
  499         microuptime(&proc0.p_switchtime);
  500         switchticks = ticks;
  501 
  502         /*
  503          * Give the ``random'' number generator a thump.
  504          * XXX: Does read_random() contain enough bits to be used here ?
  505          */
  506         nanotime(&ts);
  507         srandom(ts.tv_sec ^ ts.tv_nsec);
  508 
  509         /* Initialize signal state for process 0. */
  510         siginit(&proc0);
  511 }
  512 SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL)
  513 
  514 
  515 
  516 
  517 /*
  518  ***************************************************************************
  519  ****
  520  **** The following SYSINIT's and glue code should be moved to the
  521  **** respective files on a per subsystem basis.
  522  ****
  523  ***************************************************************************
  524  */
  525 
  526 /* ARGSUSED */
  527 static void root_conf __P((void *dummy));
  528 static void
  529 root_conf(dummy)
  530         void *dummy;
  531 {
  532         cpu_rootconf();
  533 }
  534 SYSINIT(root_conf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, root_conf, NULL)
  535 
  536 /* ARGSUSED*/
  537 static void xxx_vfs_root_fdtab __P((void *dummy));
  538 static void
  539 xxx_vfs_root_fdtab(dummy)
  540         void *dummy;
  541 {
  542         register struct filedesc0       *fdp = &filedesc0;
  543 
  544         /* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
  545         if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
  546                 panic("cannot find root vnode");
  547         fdp->fd_fd.fd_cdir = rootvnode;
  548         VREF(fdp->fd_fd.fd_cdir);
  549         VOP_UNLOCK(rootvnode, 0, &proc0);
  550         fdp->fd_fd.fd_rdir = rootvnode;
  551 }
  552 SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL)
  553 
  554 
  555 /*
  556  ***************************************************************************
  557  ****
  558  **** The following code probably belongs in another file, like
  559  **** kern/init_init.c.  It is here for two reasons only:
  560  ****
  561  ****   1)      This code returns to startup the system; this is
  562  ****           abnormal for a kernel thread.
  563  ****   2)      This code promiscuously uses init_frame
  564  ****
  565  ***************************************************************************
  566  */
  567 
  568 static void kthread_init __P((void *dummy));
  569 SYSINIT_KP(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kthread_init, NULL)
  570 
  571 
  572 extern void prepare_usermode __P((void));
  573 static void start_init __P((struct proc *p));
  574 
  575 /* ARGSUSED*/
  576 static void
  577 kthread_init(dummy)
  578         void *dummy;
  579 {
  580         /* Create process 1 (init(8)). */
  581         start_init(curproc);
  582 
  583         prepare_usermode();
  584 
  585         /*
  586          * This returns to the fork trampoline, then to user mode.
  587          */
  588         return; 
  589 }
  590 
  591 
  592 /*
  593  * List of paths to try when searching for "init".
  594  */
  595 static char init_path[MAXPATHLEN] =
  596 #ifdef  INIT_PATH
  597     __XSTRING(INIT_PATH);
  598 #else
  599     "/sbin/init:/sbin/oinit:/sbin/init.bak:/stand/sysinstall";
  600 #endif
  601 SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, "");
  602 
  603 /*
  604  * Start the initial user process; try exec'ing each pathname in init_path.
  605  * The program is invoked with one argument containing the boot flags.
  606  */
  607 static void
  608 start_init(p)
  609         struct proc *p;
  610 {
  611         vm_offset_t addr;
  612         struct execve_args args;
  613         int options, error;
  614         char *var, *path, *next, *s;
  615         char *ucp, **uap, *arg0, *arg1;
  616 
  617         initproc = p;
  618 
  619         /*
  620          * Need just enough stack to hold the faked-up "execve()" arguments.
  621          */
  622         addr = trunc_page(USRSTACK - PAGE_SIZE);
  623         if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE,
  624                         FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0)
  625                 panic("init: couldn't allocate argument space");
  626         p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
  627         p->p_vmspace->vm_ssize = 1;
  628 
  629         if ((var = getenv("init_path")) != NULL) {
  630                 strncpy(init_path, var, sizeof init_path);
  631                 init_path[sizeof init_path - 1] = 0;
  632         }
  633         
  634         for (path = init_path; *path != '\0'; path = next) {
  635                 while (*path == ':')
  636                         path++;
  637                 if (*path == '\0')
  638                         break;
  639                 for (next = path; *next != '\0' && *next != ':'; next++)
  640                         /* nothing */ ;
  641                 if (bootverbose)
  642                         printf("start_init: trying %.*s\n", next-path, path);
  643                         
  644                 /*
  645                  * Move out the boot flag argument.
  646                  */
  647                 options = 0;
  648                 ucp = (char *)USRSTACK;
  649                 (void)subyte(--ucp, 0);         /* trailing zero */
  650                 if (boothowto & RB_SINGLE) {
  651                         (void)subyte(--ucp, 's');
  652                         options = 1;
  653                 }
  654 #ifdef notyet
  655                 if (boothowto & RB_FASTBOOT) {
  656                         (void)subyte(--ucp, 'f');
  657                         options = 1;
  658                 }
  659 #endif
  660 
  661 #ifdef BOOTCDROM
  662                 (void)subyte(--ucp, 'C');
  663                 options = 1;
  664 #endif
  665                 if (options == 0)
  666                         (void)subyte(--ucp, '-');
  667                 (void)subyte(--ucp, '-');               /* leading hyphen */
  668                 arg1 = ucp;
  669 
  670                 /*
  671                  * Move out the file name (also arg 0).
  672                  */
  673                 (void)subyte(--ucp, 0);
  674                 for (s = next - 1; s >= path; s--)
  675                         (void)subyte(--ucp, *s);
  676                 arg0 = ucp;
  677 
  678                 /*
  679                  * Move out the arg pointers.
  680                  */
  681                 uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1));
  682                 (void)suword((caddr_t)--uap, (long)0);  /* terminator */
  683                 (void)suword((caddr_t)--uap, (long)(intptr_t)arg1);
  684                 (void)suword((caddr_t)--uap, (long)(intptr_t)arg0);
  685 
  686                 /*
  687                  * Point at the arguments.
  688                  */
  689                 args.fname = arg0;
  690                 args.argv = uap;
  691                 args.envv = NULL;
  692 
  693                 /*
  694                  * Now try to exec the program.  If can't for any reason
  695                  * other than it doesn't exist, complain.
  696                  *
  697                  * Otherwise return to main() which returns to btext
  698                  * which completes the system startup.
  699                  */
  700                 if ((error = execve(p, &args)) == 0)
  701                         return;
  702                 if (error != ENOENT)
  703                         printf("exec %.*s: error %d\n", next-path, path, error);
  704         }
  705         printf("init: not found\n");
  706         panic("no init");
  707 }

Cache object: ed909a1228f02e1ed066732d3e8e79a1


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.