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. copy_files
  5. copy_mm
  6. copy_fs
  7. 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 #include <linux/malloc.h>
  23 #include <linux/ldt.h>
  24 
  25 #include <asm/segment.h>
  26 #include <asm/system.h>
  27 
  28 asmlinkage void ret_from_sys_call(void) __asm__("ret_from_sys_call");
  29 
  30 /* These should maybe be in <linux/tasks.h> */
  31 
  32 #define MAX_TASKS_PER_USER (NR_TASKS/2)
  33 #define MIN_TASKS_LEFT_FOR_ROOT 4
  34 
  35 extern int shm_fork(struct task_struct *, struct task_struct *);
  36 long last_pid=0;
  37 
  38 static int find_empty_process(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  39 {
  40         int free_task;
  41         int i, tasks_free;
  42         int this_user_tasks;
  43 
  44 repeat:
  45         if ((++last_pid) & 0xffff8000)
  46                 last_pid=1;
  47         this_user_tasks = 0;
  48         tasks_free = 0;
  49         free_task = -EAGAIN;
  50         i = NR_TASKS;
  51         while (--i > 0) {
  52                 if (!task[i]) {
  53                         free_task = i;
  54                         tasks_free++;
  55                         continue;
  56                 }
  57                 if (task[i]->uid == current->uid)
  58                         this_user_tasks++;
  59                 if (task[i]->pid == last_pid || task[i]->pgrp == last_pid ||
  60                     task[i]->session == last_pid)
  61                         goto repeat;
  62         }
  63         if (tasks_free <= MIN_TASKS_LEFT_FOR_ROOT ||
  64             this_user_tasks > MAX_TASKS_PER_USER)
  65                 if (current->uid)
  66                         return -EAGAIN;
  67         return free_task;
  68 }
  69 
  70 static struct file * copy_fd(struct file * old_file)
     /* [previous][next][first][last][top][bottom][index][help] */
  71 {
  72         struct file * new_file = get_empty_filp();
  73         int error;
  74 
  75         if (new_file) {
  76                 memcpy(new_file,old_file,sizeof(struct file));
  77                 new_file->f_count = 1;
  78                 if (new_file->f_inode)
  79                         new_file->f_inode->i_count++;
  80                 if (new_file->f_op && new_file->f_op->open) {
  81                         error = new_file->f_op->open(new_file->f_inode,new_file);
  82                         if (error) {
  83                                 iput(new_file->f_inode);
  84                                 new_file->f_count = 0;
  85                                 new_file = NULL;
  86                         }
  87                 }
  88         }
  89         return new_file;
  90 }
  91 
  92 static int dup_mmap(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94         struct vm_area_struct * mpnt, **p, *tmp;
  95 
  96         tsk->mm->mmap = NULL;
  97         tsk->mm->stk_vma = NULL;
  98         p = &tsk->mm->mmap;
  99         for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
 100                 tmp = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
 101                 if (!tmp)
 102                         return -ENOMEM;
 103                 *tmp = *mpnt;
 104                 tmp->vm_task = tsk;
 105                 tmp->vm_next = NULL;
 106                 if (tmp->vm_inode)
 107                         tmp->vm_inode->i_count++;
 108                 *p = tmp;
 109                 p = &tmp->vm_next;
 110                 if (current->mm->stk_vma == mpnt)
 111                         tsk->mm->stk_vma = tmp;
 112         }
 113         return 0;
 114 }
 115 
 116 /*
 117  * SHAREFD not yet implemented..
 118  */
 119 static void copy_files(unsigned long clone_flags, struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         int i;
 122         struct file * f;
 123 
 124         if (clone_flags & COPYFD) {
 125                 for (i=0; i<NR_OPEN;i++)
 126                         if ((f = p->files->fd[i]) != NULL)
 127                                 p->files->fd[i] = copy_fd(f);
 128         } else {
 129                 for (i=0; i<NR_OPEN;i++)
 130                         if ((f = p->files->fd[i]) != NULL)
 131                                 f->f_count++;
 132         }
 133 }
 134 
 135 /*
 136  * CLONEVM not yet correctly implemented: needs to clone the mmap
 137  * instead of duplicating it..
 138  */
 139 static int copy_mm(unsigned long clone_flags, struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141         if (clone_flags & COPYVM) {
 142                 p->mm->swappable = 1;
 143                 p->mm->min_flt = p->mm->maj_flt = 0;
 144                 p->mm->cmin_flt = p->mm->cmaj_flt = 0;
 145                 if (copy_page_tables(p))
 146                         return 1;
 147                 dup_mmap(p);
 148         } else {
 149                 if (clone_page_tables(p))
 150                         return 1;
 151                 dup_mmap(p);            /* wrong.. */
 152         }
 153         return 0;
 154 }
 155 
 156 static void copy_fs(unsigned long clone_flags, struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 157 {
 158         if (current->fs->pwd)
 159                 current->fs->pwd->i_count++;
 160         if (current->fs->root)
 161                 current->fs->root->i_count++;
 162         if (current->executable)
 163                 current->executable->i_count++;
 164 }
 165 
 166 #define IS_CLONE (regs.orig_eax == __NR_clone)
 167 
 168 /*
 169  *  Ok, this is the main fork-routine. It copies the system process
 170  * information (task[nr]) and sets up the necessary registers. It
 171  * also copies the data segment in its entirety.
 172  */
 173 asmlinkage int sys_fork(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 174 {
 175         struct pt_regs * childregs;
 176         struct task_struct *p;
 177         int i,nr;
 178         unsigned long clone_flags = COPYVM | SIGCHLD;
 179 
 180         if(!(p = (struct task_struct*)__get_free_page(GFP_KERNEL)))
 181                 goto bad_fork;
 182         nr = find_empty_process();
 183         if (nr < 0)
 184                 goto bad_fork_free;
 185         task[nr] = p;
 186         *p = *current;
 187         p->did_exec = 0;
 188         p->kernel_stack_page = 0;
 189         p->state = TASK_UNINTERRUPTIBLE;
 190         p->flags &= ~(PF_PTRACED|PF_TRACESYS);
 191         p->pid = last_pid;
 192         p->p_pptr = p->p_opptr = current;
 193         p->p_cptr = NULL;
 194         SET_LINKS(p);
 195         p->signal = 0;
 196         p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
 197         p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
 198         p->leader = 0;          /* process leadership doesn't inherit */
 199         p->utime = p->stime = 0;
 200         p->cutime = p->cstime = 0;
 201         p->start_time = jiffies;
 202 /*
 203  * set up new TSS and kernel stack
 204  */
 205         if (!(p->kernel_stack_page = get_free_page(GFP_KERNEL)))
 206                 goto bad_fork_cleanup;
 207         *(unsigned long *)p->kernel_stack_page = STACK_MAGIC;
 208         p->tss.es = KERNEL_DS;
 209         p->tss.cs = KERNEL_CS;
 210         p->tss.ss = KERNEL_DS;
 211         p->tss.ds = KERNEL_DS;
 212         p->tss.fs = USER_DS;
 213         p->tss.gs = KERNEL_DS;
 214         p->tss.ss0 = KERNEL_DS;
 215         p->tss.esp0 = p->kernel_stack_page + PAGE_SIZE;
 216         p->tss.tr = _TSS(nr);
 217         childregs = ((struct pt_regs *) (p->kernel_stack_page + PAGE_SIZE)) - 1;
 218         p->tss.esp = (unsigned long) childregs;
 219         p->tss.eip = (unsigned long) ret_from_sys_call;
 220         *childregs = regs;
 221         childregs->eax = 0;
 222         p->tss.back_link = 0;
 223         p->tss.eflags = regs.eflags & 0xffffcfff;       /* iopl is always 0 for a new process */
 224         if (IS_CLONE) {
 225                 if (regs.ebx)
 226                         childregs->esp = regs.ebx;
 227                 clone_flags = regs.ecx;
 228                 if (childregs->esp == regs.esp)
 229                         clone_flags |= COPYVM;
 230         }
 231         p->exit_signal = clone_flags & CSIGNAL;
 232         p->tss.ldt = _LDT(nr);
 233         if (p->ldt) {
 234                 p->ldt = (struct desc_struct*) vmalloc(LDT_ENTRIES*LDT_ENTRY_SIZE);
 235                 if (p->ldt != NULL)
 236                         memcpy(p->ldt, current->ldt, LDT_ENTRIES*LDT_ENTRY_SIZE);
 237         }
 238         p->tss.bitmap = offsetof(struct tss_struct,io_bitmap);
 239         for (i = 0; i < IO_BITMAP_SIZE+1 ; i++) /* IO bitmap is actually SIZE+1 */
 240                 p->tss.io_bitmap[i] = ~0;
 241         if (last_task_used_math == current)
 242                 __asm__("clts ; fnsave %0 ; frstor %0":"=m" (p->tss.i387));
 243         p->semun = NULL; p->shm = NULL;
 244         if (copy_mm(clone_flags, p) || shm_fork(current, p))
 245                 goto bad_fork_cleanup;
 246         copy_files(clone_flags, p);
 247         copy_fs(clone_flags, p);
 248         set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
 249         if (p->ldt)
 250                 set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,p->ldt, 512);
 251         else
 252                 set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&default_ldt, 1);
 253 
 254         p->counter = current->counter >> 1;
 255         p->state = TASK_RUNNING;        /* do this last, just in case */
 256         return p->pid;
 257 bad_fork_cleanup:
 258         task[nr] = NULL;
 259         REMOVE_LINKS(p);
 260         free_page(p->kernel_stack_page);
 261 bad_fork_free:
 262         free_page((long) p);
 263 bad_fork:
 264         return -EAGAIN;
 265 }

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