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/servers/pm/exec.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 /* This file handles the EXEC system call.  It performs the work as follows:
    2  *    - see if the permissions allow the file to be executed
    3  *    - read the header and extract the sizes
    4  *    - fetch the initial args and environment from the user space
    5  *    - allocate the memory for the new process
    6  *    - copy the initial stack from PM to the process
    7  *    - read in the text and data segments and copy to the process
    8  *    - take care of setuid and setgid bits
    9  *    - fix up 'mproc' table
   10  *    - tell kernel about EXEC
   11  *    - save offset to initial argc (for ps)
   12  *
   13  * The entry points into this file are:
   14  *   do_exec:    perform the EXEC system call
   15  *   rw_seg:     read or write a segment from or to a file
   16  *   find_share: find a process whose text segment can be shared
   17  */
   18 
   19 #include "pm.h"
   20 #include <sys/stat.h>
   21 #include <minix/callnr.h>
   22 #include <minix/com.h>
   23 #include <a.out.h>
   24 #include <signal.h>
   25 #include <string.h>
   26 #include "mproc.h"
   27 #include "param.h"
   28 
   29 FORWARD _PROTOTYPE( int new_mem, (struct mproc *sh_mp, vir_bytes text_bytes,
   30                 vir_bytes data_bytes, vir_bytes bss_bytes,
   31                 vir_bytes stk_bytes, phys_bytes tot_bytes)              );
   32 FORWARD _PROTOTYPE( void patch_ptr, (char stack[ARG_MAX], vir_bytes base) );
   33 FORWARD _PROTOTYPE( int insert_arg, (char stack[ARG_MAX],
   34                 vir_bytes *stk_bytes, char *arg, int replace)           );
   35 FORWARD _PROTOTYPE( char *patch_stack, (int fd, char stack[ARG_MAX],
   36                 vir_bytes *stk_bytes, char *script)                     );
   37 FORWARD _PROTOTYPE( int read_header, (int fd, int *ft, vir_bytes *text_bytes,
   38                 vir_bytes *data_bytes, vir_bytes *bss_bytes,
   39                 phys_bytes *tot_bytes, long *sym_bytes, vir_clicks sc,
   40                 vir_bytes *pc)                                          );
   41 
   42 #define ESCRIPT (-2000) /* Returned by read_header for a #! script. */
   43 #define PTRSIZE sizeof(char *) /* Size of pointers in argv[] and envp[]. */
   44 
   45 /*===========================================================================*
   46  *                              do_exec                                      *
   47  *===========================================================================*/
   48 PUBLIC int do_exec()
   49 {
   50 /* Perform the execve(name, argv, envp) call.  The user library builds a
   51  * complete stack image, including pointers, args, environ, etc.  The stack
   52  * is copied to a buffer inside PM, and then to the new core image.
   53  */
   54   register struct mproc *rmp;
   55   struct mproc *sh_mp;
   56   int m, r, fd, ft, sn;
   57   static char mbuf[ARG_MAX];    /* buffer for stack and zeroes */
   58   static char name_buf[PATH_MAX]; /* the name of the file to exec */
   59   char *new_sp, *name, *basename;
   60   vir_bytes src, dst, text_bytes, data_bytes, bss_bytes, stk_bytes, vsp;
   61   phys_bytes tot_bytes;         /* total space for program, including gap */
   62   long sym_bytes;
   63   vir_clicks sc;
   64   struct stat s_buf[2], *s_p;
   65   vir_bytes pc;
   66 
   67   /* Do some validity checks. */
   68   rmp = mp;
   69   stk_bytes = (vir_bytes) m_in.stack_bytes;
   70   if (stk_bytes > ARG_MAX) return(ENOMEM);      /* stack too big */
   71   if (m_in.exec_len <= 0 || m_in.exec_len > PATH_MAX) return(EINVAL);
   72 
   73   /* Get the exec file name and see if the file is executable. */
   74   src = (vir_bytes) m_in.exec_name;
   75   dst = (vir_bytes) name_buf;
   76   r = sys_datacopy(who, (vir_bytes) src,
   77                 PM_PROC_NR, (vir_bytes) dst, (phys_bytes) m_in.exec_len);
   78   if (r != OK) return(r);       /* file name not in user data segment */
   79 
   80   /* Fetch the stack from the user before destroying the old core image. */
   81   src = (vir_bytes) m_in.stack_ptr;
   82   dst = (vir_bytes) mbuf;
   83   r = sys_datacopy(who, (vir_bytes) src,
   84                         PM_PROC_NR, (vir_bytes) dst, (phys_bytes)stk_bytes);
   85   /* can't fetch stack (e.g. bad virtual addr) */
   86   if (r != OK) return(EACCES);  
   87 
   88   r = 0;        /* r = 0 (first attempt), or 1 (interpreted script) */
   89   name = name_buf;      /* name of file to exec. */
   90   do {
   91         s_p = &s_buf[r];
   92         tell_fs(CHDIR, who, FALSE, 0);  /* switch to the user's FS environ */
   93         fd = allowed(name, s_p, X_BIT); /* is file executable? */
   94         if (fd < 0)  return(fd);                /* file was not executable */
   95 
   96         /* Read the file header and extract the segment sizes. */
   97         sc = (stk_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
   98 
   99         m = read_header(fd, &ft, &text_bytes, &data_bytes, &bss_bytes, 
  100                                         &tot_bytes, &sym_bytes, sc, &pc);
  101         if (m != ESCRIPT || ++r > 1) break;
  102   } while ((name = patch_stack(fd, mbuf, &stk_bytes, name_buf)) != NULL);
  103 
  104   if (m < 0) {
  105         close(fd);              /* something wrong with header */
  106         return(stk_bytes > ARG_MAX ? ENOMEM : ENOEXEC);
  107   }
  108 
  109   /* Can the process' text be shared with that of one already running? */
  110   sh_mp = find_share(rmp, s_p->st_ino, s_p->st_dev, s_p->st_ctime);
  111 
  112   /* Allocate new memory and release old memory.  Fix map and tell kernel. */
  113   r = new_mem(sh_mp, text_bytes, data_bytes, bss_bytes, stk_bytes, tot_bytes);
  114   if (r != OK) {
  115         close(fd);              /* insufficient core or program too big */
  116         return(r);
  117   }
  118 
  119   /* Save file identification to allow it to be shared. */
  120   rmp->mp_ino = s_p->st_ino;
  121   rmp->mp_dev = s_p->st_dev;
  122   rmp->mp_ctime = s_p->st_ctime;
  123 
  124   /* Patch up stack and copy it from PM to new core image. */
  125   vsp = (vir_bytes) rmp->mp_seg[S].mem_vir << CLICK_SHIFT;
  126   vsp += (vir_bytes) rmp->mp_seg[S].mem_len << CLICK_SHIFT;
  127   vsp -= stk_bytes;
  128   patch_ptr(mbuf, vsp);
  129   src = (vir_bytes) mbuf;
  130   r = sys_datacopy(PM_PROC_NR, (vir_bytes) src,
  131                         who, (vir_bytes) vsp, (phys_bytes)stk_bytes);
  132   if (r != OK) panic(__FILE__,"do_exec stack copy err on", who);
  133 
  134   /* Read in text and data segments. */
  135   if (sh_mp != NULL) {
  136         lseek(fd, (off_t) text_bytes, SEEK_CUR);  /* shared: skip text */
  137   } else {
  138         rw_seg(0, fd, who, T, text_bytes);
  139   }
  140   rw_seg(0, fd, who, D, data_bytes);
  141 
  142   close(fd);                    /* don't need exec file any more */
  143 
  144   /* Take care of setuid/setgid bits. */
  145   if ((rmp->mp_flags & TRACED) == 0) { /* suppress if tracing */
  146         if (s_buf[0].st_mode & I_SET_UID_BIT) {
  147                 rmp->mp_effuid = s_buf[0].st_uid;
  148                 tell_fs(SETUID,who, (int)rmp->mp_realuid, (int)rmp->mp_effuid);
  149         }
  150         if (s_buf[0].st_mode & I_SET_GID_BIT) {
  151                 rmp->mp_effgid = s_buf[0].st_gid;
  152                 tell_fs(SETGID,who, (int)rmp->mp_realgid, (int)rmp->mp_effgid);
  153         }
  154   }
  155 
  156   /* Save offset to initial argc (for ps) */
  157   rmp->mp_procargs = vsp;
  158 
  159   /* Fix 'mproc' fields, tell kernel that exec is done,  reset caught sigs. */
  160   for (sn = 1; sn <= _NSIG; sn++) {
  161         if (sigismember(&rmp->mp_catch, sn)) {
  162                 sigdelset(&rmp->mp_catch, sn);
  163                 rmp->mp_sigact[sn].sa_handler = SIG_DFL;
  164                 sigemptyset(&rmp->mp_sigact[sn].sa_mask);
  165         }
  166   }
  167 
  168   rmp->mp_flags &= ~SEPARATE;   /* turn off SEPARATE bit */
  169   rmp->mp_flags |= ft;          /* turn it on for separate I & D files */
  170   new_sp = (char *) vsp;
  171 
  172   tell_fs(EXEC, who, 0, 0);     /* allow FS to handle FD_CLOEXEC files */
  173 
  174   /* System will save command line for debugging, ps(1) output, etc. */
  175   basename = strrchr(name, '/');
  176   if (basename == NULL) basename = name; else basename++;
  177   strncpy(rmp->mp_name, basename, PROC_NAME_LEN-1);
  178   rmp->mp_name[PROC_NAME_LEN] = '\0';
  179   sys_exec(who, new_sp, basename, pc);
  180 
  181   /* Cause a signal if this process is traced. */
  182   if (rmp->mp_flags & TRACED) check_sig(rmp->mp_pid, SIGTRAP);
  183 
  184   return(SUSPEND);              /* no reply, new program just runs */
  185 }
  186 
  187 /*===========================================================================*
  188  *                              read_header                                  *
  189  *===========================================================================*/
  190 PRIVATE int read_header(fd, ft, text_bytes, data_bytes, bss_bytes, 
  191                                                 tot_bytes, sym_bytes, sc, pc)
  192 int fd;                         /* file descriptor for reading exec file */
  193 int *ft;                        /* place to return ft number */
  194 vir_bytes *text_bytes;          /* place to return text size */
  195 vir_bytes *data_bytes;          /* place to return initialized data size */
  196 vir_bytes *bss_bytes;           /* place to return bss size */
  197 phys_bytes *tot_bytes;          /* place to return total size */
  198 long *sym_bytes;                /* place to return symbol table size */
  199 vir_clicks sc;                  /* stack size in clicks */
  200 vir_bytes *pc;                  /* program entry point (initial PC) */
  201 {
  202 /* Read the header and extract the text, data, bss and total sizes from it. */
  203 
  204   int m, ct;
  205   vir_clicks tc, dc, s_vir, dvir;
  206   phys_clicks totc;
  207   struct exec hdr;              /* a.out header is read in here */
  208 
  209   /* Read the header and check the magic number.  The standard MINIX header 
  210    * is defined in <a.out.h>.  It consists of 8 chars followed by 6 longs.
  211    * Then come 4 more longs that are not used here.
  212    *    Byte 0: magic number 0x01
  213    *    Byte 1: magic number 0x03
  214    *    Byte 2: normal = 0x10 (not checked, 0 is OK), separate I/D = 0x20
  215    *    Byte 3: CPU type, Intel 16 bit = 0x04, Intel 32 bit = 0x10, 
  216    *            Motorola = 0x0B, Sun SPARC = 0x17
  217    *    Byte 4: Header length = 0x20
  218    *    Bytes 5-7 are not used.
  219    *
  220    *    Now come the 6 longs
  221    *    Bytes  8-11: size of text segments in bytes
  222    *    Bytes 12-15: size of initialized data segment in bytes
  223    *    Bytes 16-19: size of bss in bytes
  224    *    Bytes 20-23: program entry point
  225    *    Bytes 24-27: total memory allocated to program (text, data + stack)
  226    *    Bytes 28-31: size of symbol table in bytes
  227    * The longs are represented in a machine dependent order,
  228    * little-endian on the 8088, big-endian on the 68000.
  229    * The header is followed directly by the text and data segments, and the 
  230    * symbol table (if any). The sizes are given in the header. Only the 
  231    * text and data segments are copied into memory by exec. The header is 
  232    * used here only. The symbol table is for the benefit of a debugger and 
  233    * is ignored here.
  234    */
  235 
  236   if ((m= read(fd, &hdr, A_MINHDR)) < 2) return(ENOEXEC);
  237 
  238   /* Interpreted script? */
  239   if (((char *) &hdr)[0] == '#' && ((char *) &hdr)[1] == '!') return(ESCRIPT);
  240 
  241   if (m != A_MINHDR) return(ENOEXEC);
  242 
  243   /* Check magic number, cpu type, and flags. */
  244   if (BADMAG(hdr)) return(ENOEXEC);
  245 #if (CHIP == INTEL && _WORD_SIZE == 2)
  246   if (hdr.a_cpu != A_I8086) return(ENOEXEC);
  247 #endif
  248 #if (CHIP == INTEL && _WORD_SIZE == 4)
  249   if (hdr.a_cpu != A_I80386) return(ENOEXEC);
  250 #endif
  251   if ((hdr.a_flags & ~(A_NSYM | A_EXEC | A_SEP)) != 0) return(ENOEXEC);
  252 
  253   *ft = ( (hdr.a_flags & A_SEP) ? SEPARATE : 0);    /* separate I & D or not */
  254 
  255   /* Get text and data sizes. */
  256   *text_bytes = (vir_bytes) hdr.a_text; /* text size in bytes */
  257   *data_bytes = (vir_bytes) hdr.a_data; /* data size in bytes */
  258   *bss_bytes  = (vir_bytes) hdr.a_bss;  /* bss size in bytes */
  259   *tot_bytes  = hdr.a_total;            /* total bytes to allocate for prog */
  260   *sym_bytes  = hdr.a_syms;             /* symbol table size in bytes */
  261   if (*tot_bytes == 0) return(ENOEXEC);
  262 
  263   if (*ft != SEPARATE) {
  264         /* If I & D space is not separated, it is all considered data. Text=0*/
  265         *data_bytes += *text_bytes;
  266         *text_bytes = 0;
  267   }
  268   *pc = hdr.a_entry;    /* initial address to start execution */
  269 
  270   /* Check to see if segment sizes are feasible. */
  271   tc = ((unsigned long) *text_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
  272   dc = (*data_bytes + *bss_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
  273   totc = (*tot_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
  274   if (dc >= totc) return(ENOEXEC);      /* stack must be at least 1 click */
  275   dvir = (*ft == SEPARATE ? 0 : tc);
  276   s_vir = dvir + (totc - sc);
  277 #if (CHIP == INTEL && _WORD_SIZE == 2)
  278   m = size_ok(*ft, tc, dc, sc, dvir, s_vir);
  279 #else
  280   m = (dvir + dc > s_vir) ? ENOMEM : OK;
  281 #endif
  282   ct = hdr.a_hdrlen & BYTE;             /* header length */
  283   if (ct > A_MINHDR) lseek(fd, (off_t) ct, SEEK_SET); /* skip unused hdr */
  284   return(m);
  285 }
  286 
  287 /*===========================================================================*
  288  *                              new_mem                                      *
  289  *===========================================================================*/
  290 PRIVATE int new_mem(sh_mp, text_bytes, data_bytes,
  291         bss_bytes,stk_bytes,tot_bytes)
  292 struct mproc *sh_mp;            /* text can be shared with this process */
  293 vir_bytes text_bytes;           /* text segment size in bytes */
  294 vir_bytes data_bytes;           /* size of initialized data in bytes */
  295 vir_bytes bss_bytes;            /* size of bss in bytes */
  296 vir_bytes stk_bytes;            /* size of initial stack segment in bytes */
  297 phys_bytes tot_bytes;           /* total memory to allocate, including gap */
  298 {
  299 /* Allocate new memory and release the old memory.  Change the map and report
  300  * the new map to the kernel.  Zero the new core image's bss, gap and stack.
  301  */
  302 
  303   register struct mproc *rmp = mp;
  304   vir_clicks text_clicks, data_clicks, gap_clicks, stack_clicks, tot_clicks;
  305   phys_clicks new_base;
  306   phys_bytes bytes, base, bss_offset;
  307   int s;
  308 
  309   /* No need to allocate text if it can be shared. */
  310   if (sh_mp != NULL) text_bytes = 0;
  311 
  312   /* Allow the old data to be swapped out to make room.  (Which is really a
  313    * waste of time, because we are going to throw it away anyway.)
  314    */
  315   rmp->mp_flags |= WAITING;
  316 
  317   /* Acquire the new memory.  Each of the 4 parts: text, (data+bss), gap,
  318    * and stack occupies an integral number of clicks, starting at click
  319    * boundary.  The data and bss parts are run together with no space.
  320    */
  321   text_clicks = ((unsigned long) text_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
  322   data_clicks = (data_bytes + bss_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
  323   stack_clicks = (stk_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
  324   tot_clicks = (tot_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
  325   gap_clicks = tot_clicks - data_clicks - stack_clicks;
  326   if ( (int) gap_clicks < 0) return(ENOMEM);
  327 
  328   /* Try to allocate memory for the new process. */
  329   new_base = alloc_mem(text_clicks + tot_clicks);
  330   if (new_base == NO_MEM) return(ENOMEM);
  331 
  332   /* We've got memory for the new core image.  Release the old one. */
  333   rmp = mp;
  334 
  335   if (find_share(rmp, rmp->mp_ino, rmp->mp_dev, rmp->mp_ctime) == NULL) {
  336         /* No other process shares the text segment, so free it. */
  337         free_mem(rmp->mp_seg[T].mem_phys, rmp->mp_seg[T].mem_len);
  338   }
  339   /* Free the data and stack segments. */
  340   free_mem(rmp->mp_seg[D].mem_phys,
  341    rmp->mp_seg[S].mem_vir + rmp->mp_seg[S].mem_len - rmp->mp_seg[D].mem_vir);
  342 
  343   /* We have now passed the point of no return.  The old core image has been
  344    * forever lost, memory for a new core image has been allocated.  Set up
  345    * and report new map.
  346    */
  347   if (sh_mp != NULL) {
  348         /* Share the text segment. */
  349         rmp->mp_seg[T] = sh_mp->mp_seg[T];
  350   } else {
  351         rmp->mp_seg[T].mem_phys = new_base;
  352         rmp->mp_seg[T].mem_vir = 0;
  353         rmp->mp_seg[T].mem_len = text_clicks;
  354   }
  355   rmp->mp_seg[D].mem_phys = new_base + text_clicks;
  356   rmp->mp_seg[D].mem_vir = 0;
  357   rmp->mp_seg[D].mem_len = data_clicks;
  358   rmp->mp_seg[S].mem_phys = rmp->mp_seg[D].mem_phys + data_clicks + gap_clicks;
  359   rmp->mp_seg[S].mem_vir = rmp->mp_seg[D].mem_vir + data_clicks + gap_clicks;
  360   rmp->mp_seg[S].mem_len = stack_clicks;
  361 
  362 #if (CHIP == M68000)
  363   rmp->mp_seg[T].mem_vir = 0;
  364   rmp->mp_seg[D].mem_vir = rmp->mp_seg[T].mem_len;
  365   rmp->mp_seg[S].mem_vir = rmp->mp_seg[D].mem_vir 
  366         + rmp->mp_seg[D].mem_len + gap_clicks;
  367 #endif
  368 
  369   sys_newmap(who, rmp->mp_seg);   /* report new map to the kernel */
  370 
  371   /* The old memory may have been swapped out, but the new memory is real. */
  372   rmp->mp_flags &= ~(WAITING|ONSWAP|SWAPIN);
  373 
  374   /* Zero the bss, gap, and stack segment. */
  375   bytes = (phys_bytes)(data_clicks + gap_clicks + stack_clicks) << CLICK_SHIFT;
  376   base = (phys_bytes) rmp->mp_seg[D].mem_phys << CLICK_SHIFT;
  377   bss_offset = (data_bytes >> CLICK_SHIFT) << CLICK_SHIFT;
  378   base += bss_offset;
  379   bytes -= bss_offset;
  380 
  381   if ((s=sys_memset(0, base, bytes)) != OK) {
  382         panic(__FILE__,"new_mem can't zero", s);
  383   }
  384 
  385   return(OK);
  386 }
  387 
  388 /*===========================================================================*
  389  *                              patch_ptr                                    *
  390  *===========================================================================*/
  391 PRIVATE void patch_ptr(stack, base)
  392 char stack[ARG_MAX];            /* pointer to stack image within PM */
  393 vir_bytes base;                 /* virtual address of stack base inside user */
  394 {
  395 /* When doing an exec(name, argv, envp) call, the user builds up a stack
  396  * image with arg and env pointers relative to the start of the stack.  Now
  397  * these pointers must be relocated, since the stack is not positioned at
  398  * address 0 in the user's address space.
  399  */
  400 
  401   char **ap, flag;
  402   vir_bytes v;
  403 
  404   flag = 0;                     /* counts number of 0-pointers seen */
  405   ap = (char **) stack;         /* points initially to 'nargs' */
  406   ap++;                         /* now points to argv[0] */
  407   while (flag < 2) {
  408         if (ap >= (char **) &stack[ARG_MAX]) return;    /* too bad */
  409         if (*ap != NULL) {
  410                 v = (vir_bytes) *ap;    /* v is relative pointer */
  411                 v += base;              /* relocate it */
  412                 *ap = (char *) v;       /* put it back */
  413         } else {
  414                 flag++;
  415         }
  416         ap++;
  417   }
  418 }
  419 
  420 /*===========================================================================*
  421  *                              insert_arg                                   *
  422  *===========================================================================*/
  423 PRIVATE int insert_arg(stack, stk_bytes, arg, replace)
  424 char stack[ARG_MAX];            /* pointer to stack image within PM */
  425 vir_bytes *stk_bytes;           /* size of initial stack */
  426 char *arg;                      /* argument to prepend/replace as new argv[0] */
  427 int replace;
  428 {
  429 /* Patch the stack so that arg will become argv[0].  Be careful, the stack may
  430  * be filled with garbage, although it normally looks like this:
  431  *      nargs argv[0] ... argv[nargs-1] NULL envp[0] ... NULL
  432  * followed by the strings "pointed" to by the argv[i] and the envp[i].  The
  433  * pointers are really offsets from the start of stack.
  434  * Return true iff the operation succeeded.
  435  */
  436   int offset, a0, a1, old_bytes = *stk_bytes;
  437 
  438   /* Prepending arg adds at least one string and a zero byte. */
  439   offset = strlen(arg) + 1;
  440 
  441   a0 = (int) ((char **) stack)[1];      /* argv[0] */
  442   if (a0 < 4 * PTRSIZE || a0 >= old_bytes) return(FALSE);
  443 
  444   a1 = a0;                      /* a1 will point to the strings to be moved */
  445   if (replace) {
  446         /* Move a1 to the end of argv[0][] (argv[1] if nargs > 1). */
  447         do {
  448                 if (a1 == old_bytes) return(FALSE);
  449                 --offset;
  450         } while (stack[a1++] != 0);
  451   } else {
  452         offset += PTRSIZE;      /* new argv[0] needs new pointer in argv[] */
  453         a0 += PTRSIZE;          /* location of new argv[0][]. */
  454   }
  455 
  456   /* stack will grow by offset bytes (or shrink by -offset bytes) */
  457   if ((*stk_bytes += offset) > ARG_MAX) return(FALSE);
  458 
  459   /* Reposition the strings by offset bytes */
  460   memmove(stack + a1 + offset, stack + a1, old_bytes - a1);
  461 
  462   strcpy(stack + a0, arg);      /* Put arg in the new space. */
  463 
  464   if (!replace) {
  465         /* Make space for a new argv[0]. */
  466         memmove(stack + 2 * PTRSIZE, stack + 1 * PTRSIZE, a0 - 2 * PTRSIZE);
  467 
  468         ((char **) stack)[0]++; /* nargs++; */
  469   }
  470   /* Now patch up argv[] and envp[] by offset. */
  471   patch_ptr(stack, (vir_bytes) offset);
  472   ((char **) stack)[1] = (char *) a0;   /* set argv[0] correctly */
  473   return(TRUE);
  474 }
  475 
  476 /*===========================================================================*
  477  *                              patch_stack                                  *
  478  *===========================================================================*/
  479 PRIVATE char *patch_stack(fd, stack, stk_bytes, script)
  480 int fd;                         /* file descriptor to open script file */
  481 char stack[ARG_MAX];            /* pointer to stack image within PM */
  482 vir_bytes *stk_bytes;           /* size of initial stack */
  483 char *script;                   /* name of script to interpret */
  484 {
  485 /* Patch the argument vector to include the path name of the script to be
  486  * interpreted, and all strings on the #! line.  Returns the path name of
  487  * the interpreter.
  488  */
  489   char *sp, *interp = NULL;
  490   int n;
  491   enum { INSERT=FALSE, REPLACE=TRUE };
  492 
  493   /* Make script[] the new argv[0]. */
  494   if (!insert_arg(stack, stk_bytes, script, REPLACE)) return(NULL);
  495 
  496   if (lseek(fd, 2L, 0) == -1                    /* just behind the #! */
  497     || (n= read(fd, script, PATH_MAX)) < 0      /* read line one */
  498     || (sp= memchr(script, '\n', n)) == NULL)   /* must be a proper line */
  499         return(NULL);
  500 
  501   /* Move sp backwards through script[], prepending each string to stack. */
  502   for (;;) {
  503         /* skip spaces behind argument. */
  504         while (sp > script && (*--sp == ' ' || *sp == '\t')) {}
  505         if (sp == script) break;
  506 
  507         sp[1] = 0;
  508         /* Move to the start of the argument. */
  509         while (sp > script && sp[-1] != ' ' && sp[-1] != '\t') --sp;
  510 
  511         interp = sp;
  512         if (!insert_arg(stack, stk_bytes, sp, INSERT)) return(NULL);
  513   }
  514 
  515   /* Round *stk_bytes up to the size of a pointer for alignment contraints. */
  516   *stk_bytes= ((*stk_bytes + PTRSIZE - 1) / PTRSIZE) * PTRSIZE;
  517 
  518   close(fd);
  519   return(interp);
  520 }
  521 
  522 /*===========================================================================*
  523  *                              rw_seg                                       *
  524  *===========================================================================*/
  525 PUBLIC void rw_seg(rw, fd, proc, seg, seg_bytes0)
  526 int rw;                         /* 0 = read, 1 = write */
  527 int fd;                         /* file descriptor to read from / write to */
  528 int proc;                       /* process number */
  529 int seg;                        /* T, D, or S */
  530 phys_bytes seg_bytes0;          /* how much is to be transferred? */
  531 {
  532 /* Transfer text or data from/to a file and copy to/from a process segment.
  533  * This procedure is a little bit tricky.  The logical way to transfer a
  534  * segment would be block by block and copying each block to/from the user
  535  * space one at a time.  This is too slow, so we do something dirty here,
  536  * namely send the user space and virtual address to the file system in the
  537  * upper 10 bits of the file descriptor, and pass it the user virtual address
  538  * instead of a PM address.  The file system extracts these parameters when 
  539  * gets a read or write call from the process manager, which is the only 
  540  * process that is permitted to use this trick.  The file system then copies 
  541  * the whole segment directly to/from user space, bypassing PM completely.
  542  *
  543  * The byte count on read is usually smaller than the segment count, because
  544  * a segment is padded out to a click multiple, and the data segment is only
  545  * partially initialized.
  546  */
  547 
  548   int new_fd, bytes, r;
  549   char *ubuf_ptr;
  550   struct mem_map *sp = &mproc[proc].mp_seg[seg];
  551   phys_bytes seg_bytes = seg_bytes0;
  552 
  553   new_fd = (proc << 7) | (seg << 5) | fd;
  554   ubuf_ptr = (char *) ((vir_bytes) sp->mem_vir << CLICK_SHIFT);
  555 
  556   while (seg_bytes != 0) {
  557 #define PM_CHUNK_SIZE 8192
  558         bytes = MIN((INT_MAX / PM_CHUNK_SIZE) * PM_CHUNK_SIZE, seg_bytes);
  559         if (rw == 0) {
  560                 r = read(new_fd, ubuf_ptr, bytes);
  561         } else {
  562                 r = write(new_fd, ubuf_ptr, bytes);
  563         }
  564         if (r != bytes) break;
  565         ubuf_ptr += bytes;
  566         seg_bytes -= bytes;
  567   }
  568 }
  569 
  570 /*===========================================================================*
  571  *                              find_share                                   *
  572  *===========================================================================*/
  573 PUBLIC struct mproc *find_share(mp_ign, ino, dev, ctime)
  574 struct mproc *mp_ign;           /* process that should not be looked at */
  575 ino_t ino;                      /* parameters that uniquely identify a file */
  576 dev_t dev;
  577 time_t ctime;
  578 {
  579 /* Look for a process that is the file <ino, dev, ctime> in execution.  Don't
  580  * accidentally "find" mp_ign, because it is the process on whose behalf this
  581  * call is made.
  582  */
  583   struct mproc *sh_mp;
  584   for (sh_mp = &mproc[0]; sh_mp < &mproc[NR_PROCS]; sh_mp++) {
  585 
  586         if (!(sh_mp->mp_flags & SEPARATE)) continue;
  587         if (sh_mp == mp_ign) continue;
  588         if (sh_mp->mp_ino != ino) continue;
  589         if (sh_mp->mp_dev != dev) continue;
  590         if (sh_mp->mp_ctime != ctime) continue;
  591         return sh_mp;
  592   }
  593   return(NULL);
  594 }

Cache object: cc38c44102cb5409ddb9fe200710d11b


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