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/kernel/system/do_fork.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 /* The kernel call implemented in this file:
    2  *   m_type:    SYS_FORK
    3  *
    4  * The parameters for this kernel call are:
    5  *    m1_i1:    PR_PROC_NR      (child's process table slot)    
    6  *    m1_i2:    PR_PPROC_NR     (parent, process that forked)   
    7  */
    8 
    9 #include "../system.h"
   10 #include <signal.h>
   11 #if (CHIP == INTEL)
   12 #include "../protect.h"
   13 #endif
   14 
   15 #if USE_FORK
   16 
   17 /*===========================================================================*
   18  *                              do_fork                                      *
   19  *===========================================================================*/
   20 PUBLIC int do_fork(m_ptr)
   21 register message *m_ptr;        /* pointer to request message */
   22 {
   23 /* Handle sys_fork().  PR_PPROC_NR has forked.  The child is PR_PROC_NR. */
   24 #if (CHIP == INTEL)
   25   reg_t old_ldt_sel;
   26 #endif
   27   register struct proc *rpc;            /* child process pointer */
   28   struct proc *rpp;                     /* parent process pointer */
   29   int i;
   30 
   31   rpp = proc_addr(m_ptr->PR_PPROC_NR);
   32   rpc = proc_addr(m_ptr->PR_PROC_NR);
   33   if (isemptyp(rpp) || ! isemptyp(rpc)) return(EINVAL);
   34 
   35   /* Copy parent 'proc' struct to child. And reinitialize some fields. */
   36 #if (CHIP == INTEL)
   37   old_ldt_sel = rpc->p_ldt_sel;         /* backup local descriptors */
   38   *rpc = *rpp;                          /* copy 'proc' struct */
   39   rpc->p_ldt_sel = old_ldt_sel;         /* restore descriptors */
   40 #else
   41   *rpc = *rpp;                          /* copy 'proc' struct */
   42 #endif
   43   rpc->p_nr = m_ptr->PR_PROC_NR;        /* this was obliterated by copy */
   44 
   45   /* Only one in group should have SIGNALED, child doesn't inherit tracing. */
   46   rpc->p_rts_flags |= NO_MAP;           /* inhibit process from running */
   47   rpc->p_rts_flags &= ~(SIGNALED | SIG_PENDING | P_STOP);
   48   sigemptyset(&rpc->p_pending);
   49 
   50   rpc->p_reg.retreg = 0;        /* child sees pid = 0 to know it is child */
   51   rpc->p_user_time = 0;         /* set all the accounting times to 0 */
   52   rpc->p_sys_time = 0;
   53 
   54   /* Parent and child have to share the quantum that the forked process had,
   55    * so that queued processes do not have to wait longer because of the fork.
   56    * If the time left is odd, the child gets an extra tick.
   57    */
   58   rpc->p_ticks_left = (rpc->p_ticks_left + 1) / 2;
   59   rpp->p_ticks_left =  rpp->p_ticks_left / 2;   
   60 
   61   /* If the parent is a privileged process, take away the privileges from the 
   62    * child process and inhibit it from running by setting the NO_PRIV flag.
   63    * The caller should explicitely set the new privileges before executing.
   64    */
   65   if (priv(rpp)->s_flags & SYS_PROC) {
   66       rpc->p_priv = priv_addr(USER_PRIV_ID);
   67       rpc->p_rts_flags |= NO_PRIV;
   68   }
   69   return(OK);
   70 }
   71 
   72 #endif /* USE_FORK */
   73 

Cache object: fd63bca8c6a299d250d1c82da7e97d89


[ 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.