root/kernel/fork.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. find_empty_process
  2. copy_fd
  3. dup_mmap
  4. sys_fork

   1 /*
   2  *  linux/kernel/fork.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  *  'fork.c' contains the help-routines for the 'fork' system call
   9  * (see also system_call.s).
  10  * Fork is rather simple, once you get the hang of it, but the memory
  11  * management can be a bitch. See 'mm/mm.c': 'copy_page_tables()'
  12  */
  13 
  14 #include <linux/errno.h>
  15 #include <linux/sched.h>
  16 #include <linux/kernel.h>
  17 #include <linux/mm.h>
  18 #include <linux/stddef.h>
  19 #include <linux/unistd.h>
  20 #include <linux/segment.h>
  21 #include <linux/ptrace.h>
  22 
  23 #include <asm/segment.h>
  24 #include <asm/system.h>
  25 
  26 extern "C" void lcall7(void);
  27 extern "C" void ret_from_sys_call(void) __asm__("ret_from_sys_call");
  28 
  29 #define MAX_TASKS_PER_USER (NR_TASKS/2)
  30 
  31 extern int shm_fork(struct task_struct *, struct task_struct *);
  32 long last_pid=0;
  33 
  34 static int find_empty_process(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36         int i, task_nr;
  37         int this_user_tasks;
  38 
  39 repeat:
  40         if ((++last_pid) & 0xffff8000)
  41                 last_pid=1;
  42         this_user_tasks = 0;
  43         for(i=0 ; i < NR_TASKS ; i++) {
  44                 if (!task[i])
  45                         continue;
  46                 if (task[i]->uid == current->uid)
  47                         this_user_tasks++;
  48                 if (task[i]->pid == last_pid || task[i]->pgrp == last_pid)
  49                         goto repeat;
  50         }
  51         if (this_user_tasks > MAX_TASKS_PER_USER && current->uid)
  52                 return -EAGAIN;
  53 /* Only the super-user can fill the last available slot */
  54         task_nr = 0;
  55         for(i=1 ; i<NR_TASKS ; i++)
  56                 if (!task[i])
  57                         if (task_nr)
  58                                 return task_nr;
  59                         else
  60                                 task_nr = i;
  61         if (task_nr && suser())
  62                 return task_nr;
  63         return -EAGAIN;
  64 }
  65 
  66 static struct file * copy_fd(struct file * old_file)
     /* [previous][next][first][last][top][bottom][index][help] */
  67 {
  68         struct file * new_file = get_empty_filp();
  69         int error;
  70 
  71         if (new_file) {
  72                 memcpy(new_file,old_file,sizeof(struct file));
  73                 new_file->f_count = 1;
  74                 if (new_file->f_inode)
  75                         new_file->f_inode->i_count++;
  76                 if (new_file->f_op && new_file->f_op->open) {
  77                         error = new_file->f_op->open(new_file->f_inode,new_file);
  78                         if (error) {
  79                                 iput(new_file->f_inode);
  80                                 new_file->f_count = 0;
  81                                 new_file = NULL;
  82                         }
  83                 }
  84         }
  85         return new_file;
  86 }
  87 
  88 int dup_mmap(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90         struct vm_area_struct * mpnt, **p, *tmp;
  91 
  92         tsk->mmap = NULL;
  93         p = &tsk->mmap;
  94         for (mpnt = current->mmap ; mpnt ; mpnt = mpnt->vm_next) {
  95                 tmp = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
  96                 if (!tmp)
  97                         return -ENOMEM;
  98                 *tmp = *mpnt;
  99                 tmp->vm_task = tsk;
 100                 tmp->vm_next = NULL;
 101                 if (tmp->vm_inode)
 102                         tmp->vm_inode->i_count++;
 103                 *p = tmp;
 104                 p = &tmp->vm_next;
 105         }
 106         return 0;
 107 }
 108 
 109 #define IS_CLONE (regs.orig_eax == __NR_clone)
 110 #define copy_vm(p) ((clone_flags & COPYVM)?copy_page_tables(p):clone_page_tables(p))
 111 
 112 /*
 113  *  Ok, this is the main fork-routine. It copies the system process
 114  * information (task[nr]) and sets up the necessary registers. It
 115  * also copies the data segment in it's entirety.
 116  */
 117 extern "C" int sys_fork(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 118 {
 119         struct pt_regs * childregs;
 120         struct task_struct *p;
 121         int i,nr;
 122         struct file *f;
 123         unsigned long clone_flags = COPYVM | SIGCHLD;
 124 
 125         p = (struct task_struct *) __get_free_page(GFP_KERNEL);
 126         if (!p)
 127                 goto bad_fork;
 128         nr = find_empty_process();
 129         if (nr < 0)
 130                 goto bad_fork_free;
 131         task[nr] = p;
 132         *p = *current;
 133         p->kernel_stack_page = 0;
 134         p->state = TASK_UNINTERRUPTIBLE;
 135         p->flags &= ~(PF_PTRACED|PF_TRACESYS);
 136         p->pid = last_pid;
 137         p->swappable = 1;
 138         p->p_pptr = p->p_opptr = current;
 139         p->p_cptr = NULL;
 140         SET_LINKS(p);
 141         p->signal = 0;
 142         p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
 143         p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
 144         p->leader = 0;          /* process leadership doesn't inherit */
 145         p->utime = p->stime = 0;
 146         p->cutime = p->cstime = 0;
 147         p->min_flt = p->maj_flt = 0;
 148         p->cmin_flt = p->cmaj_flt = 0;
 149         p->start_time = jiffies;
 150 /*
 151  * set up new TSS and kernel stack
 152  */
 153         p->kernel_stack_page = __get_free_page(GFP_KERNEL);
 154         if (!p->kernel_stack_page)
 155                 goto bad_fork_cleanup;
 156         p->tss.es = KERNEL_DS;
 157         p->tss.cs = KERNEL_CS;
 158         p->tss.ss = KERNEL_DS;
 159         p->tss.ds = KERNEL_DS;
 160         p->tss.fs = KERNEL_DS;
 161         p->tss.gs = KERNEL_DS;
 162         p->tss.ss0 = KERNEL_DS;
 163         p->tss.esp0 = p->kernel_stack_page + PAGE_SIZE;
 164         p->tss.tr = _TSS(nr);
 165         childregs = ((struct pt_regs *) (p->kernel_stack_page + PAGE_SIZE)) - 1;
 166         p->tss.esp = (unsigned long) childregs;
 167         p->tss.eip = (unsigned long) ret_from_sys_call;
 168         *childregs = regs;
 169         childregs->eax = 0;
 170         p->tss.back_link = 0;
 171         p->tss.eflags = regs.eflags & 0xffffcfff;       /* iopl is always 0 for a new process */
 172         if (IS_CLONE) {
 173                 if (regs.ebx)
 174                         childregs->esp = regs.ebx;
 175                 clone_flags = regs.ecx;
 176                 if (childregs->esp == regs.esp)
 177                         clone_flags |= COPYVM;
 178         }
 179         p->exit_signal = clone_flags & CSIGNAL;
 180         p->tss.ldt = _LDT(nr);
 181         p->tss.bitmap = offsetof(struct tss_struct,io_bitmap);
 182         set_call_gate(p->ldt+0,lcall7);
 183         for (i = 0; i < IO_BITMAP_SIZE+1 ; i++) /* IO bitmap is actually SIZE+1 */
 184                 p->tss.io_bitmap[i] = ~0;
 185         if (last_task_used_math == current)
 186                 __asm__("clts ; fnsave %0 ; frstor %0":"=m" (p->tss.i387));
 187         p->semun = NULL; p->shm = NULL;
 188         if (copy_vm(p) || shm_fork(current, p))
 189                 goto bad_fork_cleanup;
 190         if (clone_flags & COPYFD) {
 191                 for (i=0; i<NR_OPEN;i++)
 192                         if ((f = p->filp[i]) != NULL)
 193                                 p->filp[i] = copy_fd(f);
 194         } else {
 195                 for (i=0; i<NR_OPEN;i++)
 196                         if ((f = p->filp[i]) != NULL)
 197                                 f->f_count++;
 198         }
 199         if (current->pwd)
 200                 current->pwd->i_count++;
 201         if (current->root)
 202                 current->root->i_count++;
 203         if (current->executable)
 204                 current->executable->i_count++;
 205         dup_mmap(p);
 206         set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
 207         set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
 208         p->counter = current->counter >> 1;
 209         p->state = TASK_RUNNING;        /* do this last, just in case */
 210         return p->pid;
 211 bad_fork_cleanup:
 212         task[nr] = NULL;
 213         REMOVE_LINKS(p);
 214         free_page(p->kernel_stack_page);
 215 bad_fork_free:
 216         free_page((long) p);
 217 bad_fork:
 218         return -EAGAIN;
 219 }

/* [previous][next][first][last][top][bottom][index][help] */