home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / exec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-09  |  22.2 KB  |  887 lines

  1. /*
  2.  *  linux/fs/exec.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  * This file is subject to the terms and conditions of the GNU General Public
  7.  * License.  See the file README.legal in the main directory of this archive
  8.  * for more details.
  9.  */
  10.  
  11. /*
  12.  * #!-checking implemented by tytso.
  13.  */
  14.  
  15. /*
  16.  * Demand-loading implemented 01.12.91 - no need to read anything but
  17.  * the header into memory. The inode of the executable is put into
  18.  * "current->executable", and page faults do the actual loading. Clean.
  19.  *
  20.  * Once more I can proudly say that linux stood up to being changed: it
  21.  * was less than 2 hours work to get demand-loading completely implemented.
  22.  *
  23.  * Demand loading changed July 1993 by Eric Youngdale.     Use mmap instead,
  24.  * current->executable is only used by the procfs.  This allows a dispatch
  25.  * table to check for several different types  of binary formats.  We keep
  26.  * trying until we recognize the file or we run out of supported binary
  27.  * formats.
  28.  */
  29.  
  30. #include <linux/fs.h>
  31. #include <linux/sched.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mm.h>
  34. #include <linux/mman.h>
  35. #include <linux/traps.h>
  36. #include <linux/a.out.h>
  37. #include <linux/errno.h>
  38. #include <linux/signal.h>
  39. #include <linux/string.h>
  40. #include <linux/stat.h>
  41. #include <linux/fcntl.h>
  42. #include <linux/ptrace.h>
  43. #include <linux/user.h>
  44. #include <linux/malloc.h>
  45.  
  46.  
  47. #include <linux/binfmts.h>
  48.  
  49. #include <asm/system.h>
  50. #include <asm/segment.h>
  51.  
  52. asmlinkage int sys_exit(int exit_code);
  53. asmlinkage int sys_close(unsigned fd);
  54. asmlinkage int sys_open(const char *, int, int);
  55. asmlinkage int sys_brk(unsigned long);
  56.  
  57. extern void shm_exit (void);
  58.  
  59. int open_inode(struct inode * inode, int mode)
  60. {
  61.     int error, fd;
  62.     struct file *f, **fpp;
  63.  
  64.     if (!inode->i_op || !inode->i_op->default_file_ops)
  65.         return -EINVAL;
  66.     f = get_empty_filp();
  67.     if (!f)
  68.         return -EMFILE;
  69.     fd = 0;
  70.     fpp = current->filp;
  71.     for (;;) {
  72.         if (!*fpp)
  73.             break;
  74.         if (++fd > NR_OPEN) {
  75.             f->f_count--;
  76.             return -ENFILE;
  77.         }
  78.         fpp++;
  79.     }
  80.     *fpp = f;
  81.     f->f_flags = mode;
  82.     f->f_mode = (mode+1) & O_ACCMODE;
  83.     f->f_inode = inode;
  84.     f->f_pos = 0;
  85.     f->f_reada = 0;
  86.     f->f_op = inode->i_op->default_file_ops;
  87.     if (f->f_op->open) {
  88.         error = f->f_op->open(inode,f);
  89.         if (error) {
  90.             *fpp = NULL;
  91.             f->f_count--;
  92.             return error;
  93.         }
  94.     }
  95.     inode->i_count++;
  96.     return fd;
  97. }
  98.  
  99. /*
  100.  * These are the only things you should do on a core-file: use only these
  101.  * macros to write out all the necessary info.
  102.  */
  103. #define DUMP_WRITE(addr,nr) \
  104. while (file.f_op->write(inode,&file,(char *)(addr),(nr)) != (nr)) goto close_coredump
  105.  
  106. #define DUMP_SEEK(offset) \
  107. if (file.f_op->lseek) { \
  108.     if (file.f_op->lseek(inode,&file,(offset),0) != (offset)) \
  109.         goto close_coredump; \
  110. } else file.f_pos = (offset)
  111.  
  112. /*
  113.  * Routine writes a core dump image in the current directory.
  114.  * Currently only a stub-function.
  115.  *
  116.  * Note that setuid/setgid files won't make a core-dump if the uid/gid
  117.  * changed due to the set[u|g]id. It's enforced by the "current->dumpable"
  118.  * field, which also makes sure the core-dumps won't be recursive if the
  119.  * dumping of the process results in another error..
  120.  */
  121. extern void arch_core_dump (struct user *up, long signr,
  122.                struct pt_regs *regs);
  123.  
  124. int core_dump(long signr, struct pt_regs * regs)
  125. {
  126.     struct inode * inode = NULL;
  127.     struct file file;
  128.     unsigned short fs;
  129.     int has_dumped = 0;
  130.     char corefile[6+sizeof(current->comm)];
  131.     register int dump_start, dump_size;
  132.     struct user dump;
  133.  
  134.     if (!current->dumpable)
  135.         return 0;
  136.     current->dumpable = 0;
  137.  
  138. /* See if we have enough room to write the upage.  */
  139.     if (current->rlim[RLIMIT_CORE].rlim_cur < PAGE_SIZE)
  140.         return 0;
  141.     fs = get_fs();
  142.     set_fs(KERNEL_DS);
  143.     memcpy(corefile,"core.",5);
  144.     memcpy(corefile+5,current->comm,sizeof(current->comm));
  145.     if (open_namei(corefile,O_CREAT | 2 | O_TRUNC,0600,&inode,NULL)) {
  146.         inode = NULL;
  147.         goto end_coredump;
  148.     }
  149.     if (!S_ISREG(inode->i_mode))
  150.         goto end_coredump;
  151.     if (!inode->i_op || !inode->i_op->default_file_ops)
  152.         goto end_coredump;
  153.     file.f_mode = 3;
  154.     file.f_flags = 0;
  155.     file.f_count = 1;
  156.     file.f_inode = inode;
  157.     file.f_pos = 0;
  158.     file.f_reada = 0;
  159.     file.f_op = inode->i_op->default_file_ops;
  160.     if (file.f_op->open)
  161.         if (file.f_op->open(inode,&file))
  162.             goto end_coredump;
  163.     if (!file.f_op->write)
  164.         goto close_coredump;
  165.     has_dumped = 1;
  166.     /* architecture specific stuff */
  167.     arch_core_dump (&dump, signr, regs);
  168. /* changed the size calculations - should hopefully work better. lbt */
  169.     set_fs(KERNEL_DS);
  170. /* struct user */
  171.     DUMP_WRITE(&dump,sizeof(dump));
  172. /* Now dump all of the user data.  Include malloced stuff as well */
  173.     DUMP_SEEK(PAGE_SIZE);
  174. /* now we start writing out the user space info */
  175.     set_fs(USER_DS);
  176. /* Dump the data area */
  177.     if (dump.u_dsize != 0) {
  178.         dump_start = dump.u_tsize << 12;
  179.         dump_size = dump.u_dsize << 12;
  180.         DUMP_WRITE(dump_start,dump_size);
  181.     };
  182. /* Now prepare to dump the stack area */
  183.     if (dump.u_ssize != 0) {
  184.         dump_start = dump.start_stack;
  185.         dump_size = dump.u_ssize << 12;
  186.         DUMP_WRITE(dump_start,dump_size);
  187.     };
  188. /* Finally dump the task struct.  Not be used by gdb, but could be useful */
  189.     set_fs(KERNEL_DS);
  190.     DUMP_WRITE(current,sizeof(*current));
  191. close_coredump:
  192.     if (file.f_op->release)
  193.         file.f_op->release(inode,&file);
  194. end_coredump:
  195.     set_fs(fs);
  196.     iput(inode);
  197.     return has_dumped;
  198. }
  199.  
  200. /*
  201.  * Note that a shared library must be both readable and executable due to
  202.  * security reasons.
  203.  *
  204.  * Also note that we take the address to load from from the file itself.
  205.  */
  206. asmlinkage int sys_uselib(const char * library)
  207. {
  208.     int fd, retval;
  209.     struct file * file;
  210.     struct linux_binfmt * fmt;
  211.  
  212.     fd = sys_open(library, 0, 0);
  213.     if (fd < 0)
  214.         return fd;
  215.     file = current->filp[fd];
  216.     retval = -ENOEXEC;
  217.     if (file && file->f_inode && file->f_op && file->f_op->read) {
  218.         fmt = formats;
  219.         do {
  220.             int (*fn)(int) = fmt->load_shlib;
  221.             if (!fn)
  222.                 break;
  223.             retval = fn(fd);
  224.             fmt++;
  225.         } while (retval == -ENOEXEC);
  226.     }
  227.     sys_close(fd);
  228.     return retval;
  229. }
  230.  
  231. /*
  232.  * create_tables() parses the env- and arg-strings in new user
  233.  * memory and creates the pointer tables from them, and puts their
  234.  * addresses on the "stack", returning the new stack pointer value.
  235.  */
  236. unsigned long * create_tables(char * p,int argc,int envc)
  237. {
  238.     unsigned long *argv,*envp;
  239.     unsigned long * sp;
  240.     struct vm_area_struct *mpnt;
  241.  
  242.     mpnt = (struct vm_area_struct *)kmalloc(sizeof(*mpnt), GFP_KERNEL);
  243.     if (mpnt) {
  244.         mpnt->vm_task = current;
  245.         mpnt->vm_start = PAGE_MASK & (unsigned long) p;
  246.         mpnt->vm_end = TASK_SIZE;
  247.         mpnt->vm_page_prot = PAGE_PRIVATE|PAGE_DIRTY;
  248.         mpnt->vm_share = NULL;
  249.         mpnt->vm_inode = NULL;
  250.         mpnt->vm_offset = 0;
  251.         mpnt->vm_ops = NULL;
  252.         insert_vm_struct(current, mpnt);
  253.         current->stk_vma = mpnt;
  254.     }
  255.     sp = (unsigned long *) (0xfffffffc & (unsigned long) p);
  256.     sp -= envc+1;
  257.     envp = sp;
  258.     sp -= argc+1;
  259.     argv = sp;
  260.     put_fs_long((unsigned long)envp,--sp);
  261.     put_fs_long((unsigned long)argv,--sp);
  262.     put_fs_long((unsigned long)argc,--sp);
  263.     current->arg_start = (unsigned long) p;
  264.     while (argc-->0) {
  265.         put_fs_long((unsigned long) p,argv++);
  266.         while (get_fs_byte(p++)) /* nothing */ ;
  267.     }
  268.     put_fs_long(0,argv);
  269.     current->arg_end = current->env_start = (unsigned long) p;
  270.     while (envc-->0) {
  271.         put_fs_long((unsigned long) p,envp++);
  272.         while (get_fs_byte(p++)) /* nothing */ ;
  273.     }
  274.     put_fs_long(0,envp);
  275.     current->env_end = (unsigned long) p;
  276.     return sp;
  277. }
  278.  
  279. /*
  280.  * count() counts the number of arguments/envelopes
  281.  */
  282. static int count(char ** argv)
  283. {
  284.     int i=0;
  285.     char ** tmp;
  286.  
  287.     if ((tmp = argv) != 0)
  288.         while (get_fs_long((unsigned long *) (tmp++)))
  289.             i++;
  290.  
  291.     return i;
  292. }
  293.  
  294. /*
  295.  * 'copy_string()' copies argument/envelope strings from user
  296.  * memory to free pages in kernel mem. These are in a format ready
  297.  * to be put directly into the top of new user memory.
  298.  *
  299.  * Modified by TYT, 11/24/91 to add the from_kmem argument, which specifies
  300.  * whether the string and the string array are from user or kernel segments:
  301.  *
  302.  * from_kmem     argv *        argv **
  303.  *    0      user space    user space
  304.  *    1      kernel space  user space
  305.  *    2      kernel space  kernel space
  306.  *
  307.  * We do this by playing games with the fs segment register.  Since it
  308.  * it is expensive to load a segment register, we try to avoid calling
  309.  * set_fs() unless we absolutely have to.
  310.  */
  311. unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
  312.         unsigned long p, int from_kmem)
  313. {
  314.     char *tmp, *pag = NULL;
  315.     int len, offset = 0;
  316.     unsigned long old_fs, new_fs;
  317.  
  318.     if (!p)
  319.         return 0;    /* bullet-proofing */
  320.     new_fs = get_ds();
  321.     old_fs = get_fs();
  322.     if (from_kmem==2)
  323.         set_fs(new_fs);
  324.     while (argc-- > 0) {
  325.         if (from_kmem == 1)
  326.             set_fs(new_fs);
  327.         if (!(tmp = (char *)get_fs_long(((unsigned long *)argv)+argc)))
  328.             panic("VFS: argc is wrong");
  329.         if (from_kmem == 1)
  330.             set_fs(old_fs);
  331.         len=0;        /* remember zero-padding */
  332.         do {
  333.             len++;
  334.         } while (get_fs_byte(tmp++));
  335.         if (p < len) {  /* this shouldn't happen - 128kB */
  336.             set_fs(old_fs);
  337.             return 0;
  338.         }
  339.         while (len) {
  340.             --p; --tmp; --len;
  341.             if (--offset < 0) {
  342.                 offset = p % PAGE_SIZE;
  343.                 if (from_kmem==2)
  344.                     set_fs(old_fs);
  345.                 if (!(pag = (char *) page[p/PAGE_SIZE]) &&
  346.                     !(pag = (char *) page[p/PAGE_SIZE] =
  347.                       (unsigned long *) get_free_page(GFP_USER)))
  348.                     return 0;
  349.                 if (from_kmem==2)
  350.                     set_fs(new_fs);
  351.  
  352.             }
  353.             *(pag + offset) = get_fs_byte(tmp);
  354.         }
  355.     }
  356.     if (from_kmem !=0)
  357.         set_fs(old_fs);
  358.     return p;
  359. }
  360.  
  361. unsigned long change_ldt(unsigned long text_size,unsigned long * page)
  362. {
  363.     unsigned long code_limit,data_limit,code_base,data_base;
  364.     int i;
  365.  
  366.     code_limit = TASK_SIZE;
  367.     data_limit = TASK_SIZE;
  368.     code_base = data_base = 0;
  369.     current->start_code = code_base;
  370.     data_base += data_limit;
  371.     for (i=MAX_ARG_PAGES-1 ; i>=0 ; i--) {
  372.         data_base -= PAGE_SIZE;
  373.         if (page[i]) {
  374.             current->rss++;
  375.             put_dirty_page(current,page[i],data_base);
  376.         }
  377.     }
  378.     return data_limit;
  379. }
  380.  
  381. /*
  382.  * Read in the complete executable. This is used for "-N" files
  383.  * that aren't on a block boundary, and for files on filesystems
  384.  * without bmap support.
  385.  */
  386. int read_exec(struct inode *inode, unsigned long offset,
  387.     char * addr, unsigned long count)
  388. {
  389.     struct file file;
  390.     int result = -ENOEXEC;
  391.  
  392.     if (!inode->i_op || !inode->i_op->default_file_ops)
  393.         goto end_readexec;
  394.     file.f_mode = 1;
  395.     file.f_flags = 0;
  396.     file.f_count = 1;
  397.     file.f_inode = inode;
  398.     file.f_pos = 0;
  399.     file.f_reada = 0;
  400.     file.f_op = inode->i_op->default_file_ops;
  401.     if (file.f_op->open)
  402.         if (file.f_op->open(inode,&file))
  403.             goto end_readexec;
  404.     if (!file.f_op || !file.f_op->read)
  405.         goto close_readexec;
  406.     if (file.f_op->lseek) {
  407.         if (file.f_op->lseek(inode,&file,offset,0) != offset)
  408.             goto close_readexec;
  409.     } else
  410.         file.f_pos = offset;
  411.     if (get_fs() == USER_DS) {
  412.         result = verify_area(VERIFY_WRITE, addr, count);
  413.         if (result)
  414.             goto close_readexec;
  415.     }
  416.     result = file.f_op->read(inode, &file, addr, count);
  417.     cache_push_v ((unsigned long)addr, count);        /* MA */
  418. close_readexec:
  419.     if (file.f_op->release)
  420.         file.f_op->release(inode,&file);
  421. end_readexec:
  422.     return result;
  423. }
  424.  
  425.  
  426. /*
  427.  * This function flushes out all traces of the currently running executable so
  428.  * that a new one can be started
  429.  */
  430.  
  431. void flush_old_exec(struct linux_binprm * bprm)
  432. {
  433.     int i;
  434.     int ch;
  435.     char * name;
  436.     struct vm_area_struct * mpnt, *mpnt1;
  437.  
  438.     current->dumpable = 1;
  439.     name = bprm->filename;
  440.     for (i=0; (ch = *(name++)) != '\0';) {
  441.         if (ch == '/')
  442.             i = 0;
  443.         else
  444.             if (i < 15)
  445.                 current->comm[i++] = ch;
  446.     }
  447.     current->comm[i] = '\0';
  448.     if (current->shm)
  449.         shm_exit();
  450.     if (current->executable) {
  451.         iput(current->executable);
  452.         current->executable = NULL;
  453.     }
  454.     /* Release all of the old mmap stuff. */
  455.  
  456.     mpnt = current->mmap;
  457.     current->mmap = NULL;
  458.     current->stk_vma = NULL;
  459.     while (mpnt) {
  460.         mpnt1 = mpnt->vm_next;
  461.         if (mpnt->vm_ops && mpnt->vm_ops->close)
  462.             mpnt->vm_ops->close(mpnt);
  463.         kfree(mpnt);
  464.         mpnt = mpnt1;
  465.     }
  466.  
  467. #ifdef __i386__
  468.     /* Flush the old ldt stuff... */
  469.     if (current->ldt) {
  470.         free_page((unsigned long) current->ldt);
  471.         current->ldt = NULL;
  472.         for (i=1 ; i<NR_TASKS ; i++) {
  473.             if (task[i] == current)  {
  474.                 set_ldt_desc(gdt+(i<<1)+
  475.                          FIRST_LDT_ENTRY,&default_ldt, 1);
  476.                 load_ldt(i);
  477.             }
  478.         }
  479.     }
  480. #endif
  481.  
  482.     if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
  483.         !permission(bprm->inode,MAY_READ))
  484.         current->dumpable = 0;
  485.     current->signal = 0;
  486.     for (i=0 ; i<32 ; i++) {
  487.         current->sigaction[i].sa_mask = 0;
  488.         current->sigaction[i].sa_flags = 0;
  489.         if (current->sigaction[i].sa_handler != SIG_IGN)
  490.             current->sigaction[i].sa_handler = NULL;
  491.     }
  492.     for (i=0 ; i<NR_OPEN ; i++)
  493.         if (FD_ISSET(i,¤t->close_on_exec))
  494.             sys_close(i);
  495.     FD_ZERO(¤t->close_on_exec);
  496.     clear_page_tables(current);
  497.     if (last_task_used_math == current)
  498.         last_task_used_math = NULL;
  499.     current->used_math = 0;
  500.     current->elf_executable = 0;
  501. }
  502.  
  503. /*
  504.  * sys_execve() executes a new program.
  505.  */
  506. static int do_execve(char *filename, char ** argv, char **envp, struct pt_regs *regs)
  507. {
  508.     struct linux_binprm bprm;
  509.     struct linux_binfmt * fmt;
  510.     unsigned long old_fs;
  511.     int i;
  512.     int retval;
  513.     int sh_bang = 0;
  514.     if (regs->sr & PS_S)
  515.         return -EINVAL;
  516.  
  517.     bprm.p = PAGE_SIZE*MAX_ARG_PAGES-4;
  518.     for (i=0 ; i<MAX_ARG_PAGES ; i++)   /* clear page-table */
  519.         bprm.page[i] = 0;
  520.     retval = open_namei(filename, 0, 0, &bprm.inode, NULL);
  521.     if (retval)
  522.         return retval;
  523.     bprm.filename = filename;
  524.     bprm.argc = count(argv);
  525.     bprm.envc = count(envp);
  526.  
  527.   restart_interp:
  528.     if (!S_ISREG(bprm.inode->i_mode)) {      /* must be regular file */
  529.         retval = -EACCES;
  530.         goto exec_error2;
  531.     }
  532.     if (IS_NOEXEC(bprm.inode)) {             /* FS mustn't be mounted noexec */
  533.         retval = -EPERM;
  534.         goto exec_error2;
  535.     }
  536.     if (!bprm.inode->i_sb) {
  537.         retval = -EACCES;
  538.         goto exec_error2;
  539.     }
  540.     i = bprm.inode->i_mode;
  541.     if (IS_NOSUID(bprm.inode) && (((i & S_ISUID) && bprm.inode->i_uid != current->
  542.         euid) || ((i & S_ISGID) && !in_group_p(bprm.inode->i_gid))) &&
  543.         !suser()) {
  544.         retval = -EPERM;
  545.         goto exec_error2;
  546.     }
  547.     /* make sure we don't let suid, sgid files be ptraced. */
  548.     if (current->flags & PF_PTRACED) {
  549.         bprm.e_uid = current->euid;
  550.         bprm.e_gid = current->egid;
  551.     } else {
  552.         bprm.e_uid = (i & S_ISUID) ? bprm.inode->i_uid : current->euid;
  553.         bprm.e_gid = (i & S_ISGID) ? bprm.inode->i_gid : current->egid;
  554.     }
  555.     if (current->euid == bprm.inode->i_uid)
  556.         i >>= 6;
  557.     else if (in_group_p(bprm.inode->i_gid))
  558.         i >>= 3;
  559.     if (!(i & 1) &&
  560.         !((bprm.inode->i_mode & 0111) && suser())) {
  561.         retval = -EACCES;
  562.         goto exec_error2;
  563.     }
  564.     memset(bprm.buf,0,sizeof(bprm.buf));
  565.     old_fs = get_fs();
  566.     set_fs(get_ds());
  567.     retval = read_exec(bprm.inode,0,bprm.buf,128);
  568.     set_fs(old_fs);
  569.     if (retval < 0)
  570.         goto exec_error2;
  571.     if ((bprm.buf[0] == '#') && (bprm.buf[1] == '!') && (!sh_bang)) {
  572.         /*
  573.          * This section does the #! interpretation.
  574.          * Sorta complicated, but hopefully it will work.  -TYT
  575.          */
  576.  
  577.         char *cp, *interp, *i_name, *i_arg;
  578.  
  579.         iput(bprm.inode);
  580.         bprm.buf[127] = '\0';
  581.         if ((cp = strchr(bprm.buf, '\n')) == NULL)
  582.             cp = bprm.buf+127;
  583.         *cp = '\0';
  584.         while (cp > bprm.buf) {
  585.             cp--;
  586.             if ((*cp == ' ') || (*cp == '\t'))
  587.                 *cp = '\0';
  588.             else
  589.                 break;
  590.         }
  591.         for (cp = bprm.buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
  592.         if (!cp || *cp == '\0') {
  593.             retval = -ENOEXEC; /* No interpreter name found */
  594.             goto exec_error1;
  595.         }
  596.         interp = i_name = cp;
  597.         i_arg = 0;
  598.         for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
  599.             if (*cp == '/')
  600.                 i_name = cp+1;
  601.         }
  602.         while ((*cp == ' ') || (*cp == '\t'))
  603.             *cp++ = '\0';
  604.         if (*cp)
  605.             i_arg = cp;
  606.         /*
  607.          * OK, we've parsed out the interpreter name and
  608.          * (optional) argument.
  609.          */
  610.         if (sh_bang++ == 0) {
  611.             bprm.p = copy_strings(bprm.envc, envp, bprm.page, bprm.p, 0);
  612.             bprm.p = copy_strings(--bprm.argc, argv+1, bprm.page, bprm.p, 0);
  613.         }
  614.         /*
  615.          * Splice in (1) the interpreter's name for argv[0]
  616.          *         (2) (optional) argument to interpreter
  617.          *         (3) filename of shell script
  618.          *
  619.          * This is done in reverse order, because of how the
  620.          * user environment and arguments are stored.
  621.          */
  622.         bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
  623.         bprm.argc++;
  624.         if (i_arg) {
  625.             bprm.p = copy_strings(1, &i_arg, bprm.page, bprm.p, 2);
  626.             bprm.argc++;
  627.         }
  628.         bprm.p = copy_strings(1, &i_name, bprm.page, bprm.p, 2);
  629.         bprm.argc++;
  630.         if (!bprm.p) {
  631.             retval = -E2BIG;
  632.             goto exec_error1;
  633.         }
  634.         /*
  635.          * OK, now restart the process with the interpreter's inode.
  636.          * Note that we use open_namei() as the name is now in kernel
  637.          * space, and we don't need to copy it.
  638.          */
  639.         retval = open_namei(interp,0,0,&bprm.inode,NULL);
  640.         if (retval)
  641.             goto exec_error1;
  642.         goto restart_interp;
  643.     }
  644.     if (!sh_bang) {
  645.         bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p,0);
  646.         bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p,0);
  647.         if (!bprm.p) {
  648.             retval = -E2BIG;
  649.             goto exec_error2;
  650.         }
  651.     }
  652.     bprm.sh_bang = sh_bang;
  653.     fmt = formats;
  654.     do {
  655.         int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  656.         if (!fn)
  657.             break;
  658.         retval = fn(&bprm, regs);
  659.         if (retval == 0) {
  660.             iput(bprm.inode);
  661.             return 0;
  662.         }
  663.         fmt++;
  664.     } while (retval == -ENOEXEC);
  665. exec_error2:
  666.     iput(bprm.inode);
  667. exec_error1:
  668.     for (i=0 ; i<MAX_ARG_PAGES ; i++)
  669.         free_page(bprm.page[i]);
  670.         
  671.     return(retval);
  672. }
  673.  
  674. /*
  675.  * sys_execve() executes a new program.
  676.  */
  677. asmlinkage int sys_execve(struct pt_regs regs)
  678. {
  679.     int error;
  680.     char * filename;
  681.  
  682.     error = getname((char *) regs.d1, &filename);
  683.     if (error)
  684.         return error;
  685.     error = do_execve(filename, (char **) regs.d2, (char **) regs.d3, ®s);
  686.     putname(filename);
  687.     return error;
  688. }
  689.  
  690. /*
  691.  * These are  the prototypes for the  functions in the    dispatch table, as
  692.  * well as the    dispatch  table itself.
  693.  */
  694.  
  695. extern int load_aout_binary(struct linux_binprm *,
  696.                 struct pt_regs * regs);
  697. extern int load_aout_library(int fd);
  698.  
  699. extern int load_elf_binary(struct linux_binprm *,
  700.                 struct pt_regs * regs);
  701. extern int load_elf_library(int fd);
  702.  
  703. /* Here are the actual binaries that will be accepted  */
  704. struct linux_binfmt formats[] = {
  705.     {load_aout_binary, load_aout_library},
  706. #ifdef CONFIG_BINFMT_ELF
  707.     {load_elf_binary, load_elf_library},
  708. #endif
  709.     {NULL, NULL}
  710. };
  711.  
  712. /*
  713.  * These are the functions used to load a.out style executables and shared
  714.  * libraries.  There is no binary dependent code anywhere else.
  715.  */
  716.  
  717. int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
  718. {
  719.     struct exec ex;
  720.     struct file * file;
  721.     int fd, error;
  722.     unsigned long p = bprm->p;
  723.  
  724.     ex = *((struct exec *) bprm->buf);              /* exec-header */
  725.     if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
  726.          N_MAGIC(ex) != QMAGIC) ||
  727.         ex.a_trsize || ex.a_drsize ||
  728.         bprm->inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
  729.         return -ENOEXEC;
  730.     }
  731.  
  732.     if (N_MAGIC(ex) == ZMAGIC &&
  733.         (N_TXTOFF(ex) < bprm->inode->i_sb->s_blocksize)) {
  734.         printk("N_TXTOFF < BLOCK_SIZE. Please convert binary.");
  735.         return -ENOEXEC;
  736.     }
  737.  
  738.     if (N_TXTOFF(ex) != BLOCK_SIZE && N_MAGIC(ex) == ZMAGIC) {
  739.         printk("N_TXTOFF != BLOCK_SIZE. See a.out.h.");
  740.         return -ENOEXEC;
  741.     }
  742.  
  743.  
  744.     /* OK, This is the point of no return */
  745.     flush_old_exec(bprm);
  746.  
  747.     current->end_code = N_TXTADDR(ex) + ex.a_text;
  748.     current->end_data = ex.a_data + current->end_code;
  749.     current->start_brk = current->brk = current->end_data;
  750.     current->start_code += N_TXTADDR(ex);
  751.     current->rss = 0;
  752.     current->suid = current->euid = bprm->e_uid;
  753.     current->mmap = NULL;
  754.     current->executable = NULL; /* for OMAGIC files */
  755.     current->sgid = current->egid = bprm->e_gid;
  756.     if (N_MAGIC(ex) == OMAGIC) {
  757.         do_mmap(NULL, 0, ex.a_text+ex.a_data,
  758.             PROT_READ|PROT_WRITE|PROT_EXEC,
  759.             MAP_FIXED|MAP_PRIVATE, 0);
  760.         read_exec(bprm->inode, 32, (char *) 0, ex.a_text+ex.a_data);
  761.     } else {
  762.         if (ex.a_text & 0xfff || ex.a_data & 0xfff)
  763.             printk("%s: executable not page aligned\n", current->comm);
  764.  
  765.         fd = open_inode(bprm->inode, O_RDONLY);
  766.  
  767.         if (fd < 0)
  768.             return fd;
  769.         file = current->filp[fd];
  770.         if (!file->f_op || !file->f_op->mmap) {
  771.             sys_close(fd);
  772.             do_mmap(NULL, 0, ex.a_text+ex.a_data,
  773.                 PROT_READ|PROT_WRITE|PROT_EXEC,
  774.                 MAP_FIXED|MAP_PRIVATE, 0);
  775.             read_exec(bprm->inode, N_TXTOFF(ex),
  776.                   (char *) N_TXTADDR(ex), ex.a_text+ex.a_data);
  777.             goto beyond_if;
  778.         }
  779.         error = do_mmap(file, N_TXTADDR(ex), ex.a_text,
  780.                 PROT_READ | PROT_EXEC,
  781.                 MAP_FIXED | MAP_SHARED, N_TXTOFF(ex));
  782.  
  783.         if (error != N_TXTADDR(ex)) {
  784.             sys_close(fd);
  785.             send_sig(SIGSEGV, current, 0);
  786.             return 0;
  787.         };
  788.  
  789.         error = do_mmap(file, N_TXTADDR(ex) + ex.a_text, ex.a_data,
  790.                 PROT_READ | PROT_WRITE | PROT_EXEC,
  791.                 MAP_FIXED | MAP_PRIVATE, N_TXTOFF(ex) + ex.a_text);
  792.         sys_close(fd);
  793.         if (error != N_TXTADDR(ex) + ex.a_text) {
  794.             send_sig(SIGSEGV, current, 0);
  795.             return 0;
  796.         };
  797.         current->executable = bprm->inode;
  798.         bprm->inode->i_count++;
  799.     }
  800. beyond_if:
  801.     sys_brk(current->brk+ex.a_bss);
  802.  
  803.     p += change_ldt(ex.a_text,bprm->page);
  804.     p -= MAX_ARG_PAGES*PAGE_SIZE;
  805.     p = (unsigned long) create_tables((char *)p,bprm->argc,bprm->envc);
  806.     current->start_stack = p;
  807.  
  808.     /* clear registers */
  809.     regs->d1  = 0;
  810.     regs->d2  = 0;
  811.     regs->d3  = 0;
  812.     regs->d4  = 0;
  813.     regs->d5  = 0;
  814.     regs->d6  = 0;
  815.     regs->d7  = 0;
  816.     regs->a0  = 0;
  817.     regs->a1  = 0;
  818.     regs->a2  = 0;
  819.     regs->a3  = 0;
  820.     regs->a4  = 0;
  821.     regs->a5  = 0;
  822.     regs->a6  = 0;
  823.  
  824.     /* clear floating point state */
  825.     clear_fpu();
  826.  
  827.     regs->pc  = ex.a_entry;     /* pc, magic happens :-) */
  828.     regs->usp = p;            /* stack pointer */
  829.     if (current->flags & PF_PTRACED)
  830.         send_sig(SIGTRAP, current, 0);
  831.     return 0;
  832. }
  833.  
  834. int load_aout_library(int fd)
  835. {
  836.     struct file * file;
  837.     struct exec ex;
  838.     struct    inode * inode;
  839.     unsigned int len;
  840.     unsigned int bss;
  841.     unsigned int start_addr;
  842.     int error;
  843.  
  844.     file = current->filp[fd];
  845.     inode = file->f_inode;
  846.  
  847.     set_fs(KERNEL_DS);
  848.     if (file->f_op->read(inode, file, (char *) &ex, sizeof(ex)) != sizeof(ex)) {
  849.         set_fs(USER_DS);
  850.         return -EACCES;
  851.     }
  852.     set_fs(USER_DS);
  853.  
  854.     /* We come in here for the regular a.out style of shared libraries */
  855.     if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || ex.a_trsize ||
  856.         ex.a_drsize || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  857.         inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
  858.         return -ENOEXEC;
  859.     }
  860.     if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
  861.         (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
  862.         printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
  863.         return -ENOEXEC;
  864.     }
  865.  
  866.     if (N_FLAGS(ex)) return -ENOEXEC;
  867.  
  868.     /* For    QMAGIC, the starting address is 0x20 into the page.  We mask
  869.        this off to get the starting address for the page */
  870.  
  871.     start_addr =  ex.a_entry & 0xfffff000;
  872.  
  873.     /* Now use mmap to map the library into memory. */
  874.     error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
  875.             PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED | MAP_PRIVATE,
  876.             N_TXTOFF(ex));
  877.     if (error != start_addr)
  878.         return error;
  879.     len = PAGE_ALIGN(ex.a_text + ex.a_data);
  880.     bss = ex.a_text + ex.a_data + ex.a_bss;
  881.     if (bss > len)
  882.         do_mmap(NULL, start_addr + len, bss-len,
  883.             PROT_READ|PROT_WRITE|PROT_EXEC,
  884.             MAP_PRIVATE|MAP_FIXED, 0);
  885.     return 0;
  886. }
  887.