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

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