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 long last_pid=0;
  28 
  29 static int find_empty_process(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  30 {
  31         int free_task;
  32         int i, tasks_free;
  33         int this_user_tasks;
  34 
  35 repeat:
  36         if ((++last_pid) & 0xffff8000)
  37                 last_pid=1;
  38         this_user_tasks = 0;
  39         tasks_free = 0;
  40         free_task = -EAGAIN;
  41         i = NR_TASKS;
  42         while (--i > 0) {
  43                 if (!task[i]) {
  44                         free_task = i;
  45                         tasks_free++;
  46                         continue;
  47                 }
  48                 if (task[i]->uid == current->uid)
  49                         this_user_tasks++;
  50                 if (task[i]->pid == last_pid || task[i]->pgrp == last_pid ||
  51                     task[i]->session == last_pid)
  52                         goto repeat;
  53         }
  54         if (tasks_free <= MIN_TASKS_LEFT_FOR_ROOT ||
  55             this_user_tasks > current->rlim[RLIMIT_NPROC].rlim_cur)
  56                 if (current->uid)
  57                         return -EAGAIN;
  58         return free_task;
  59 }
  60 
  61 static struct file * copy_fd(struct file * old_file)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63         struct file * new_file = get_empty_filp();
  64         int error;
  65 
  66         if (new_file) {
  67                 memcpy(new_file,old_file,sizeof(struct file));
  68                 new_file->f_count = 1;
  69                 if (new_file->f_inode)
  70                         new_file->f_inode->i_count++;
  71                 if (new_file->f_op && new_file->f_op->open) {
  72                         error = new_file->f_op->open(new_file->f_inode,new_file);
  73                         if (error) {
  74                                 iput(new_file->f_inode);
  75                                 new_file->f_count = 0;
  76                                 new_file = NULL;
  77                         }
  78                 }
  79         }
  80         return new_file;
  81 }
  82 
  83 static int dup_mmap(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
  84 {
  85         struct vm_area_struct * mpnt, **p, *tmp;
  86 
  87         tsk->mm->mmap = NULL;
  88         p = &tsk->mm->mmap;
  89         for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
  90                 tmp = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
  91                 if (!tmp) {
  92                         exit_mmap(tsk);
  93                         return -ENOMEM;
  94                 }
  95                 *tmp = *mpnt;
  96                 tmp->vm_task = tsk;
  97                 tmp->vm_next = NULL;
  98                 if (tmp->vm_inode) {
  99                         tmp->vm_inode->i_count++;
 100                         /* insert tmp into the share list, just after mpnt */
 101                         tmp->vm_next_share->vm_prev_share = tmp;
 102                         mpnt->vm_next_share = tmp;
 103                         tmp->vm_prev_share = mpnt;
 104                 }
 105                 if (tmp->vm_ops && tmp->vm_ops->open)
 106                         tmp->vm_ops->open(tmp);
 107                 *p = tmp;
 108                 p = &tmp->vm_next;
 109         }
 110         build_mmap_avl(tsk);
 111         return 0;
 112 }
 113 
 114 /*
 115  * SHAREFD not yet implemented..
 116  */
 117 static void copy_files(unsigned long clone_flags, struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 118 {
 119         int i;
 120         struct file * f;
 121 
 122         if (clone_flags & COPYFD) {
 123                 for (i=0; i<NR_OPEN;i++)
 124                         if ((f = p->files->fd[i]) != NULL)
 125                                 p->files->fd[i] = copy_fd(f);
 126         } else {
 127                 for (i=0; i<NR_OPEN;i++)
 128                         if ((f = p->files->fd[i]) != NULL)
 129                                 f->f_count++;
 130         }
 131 }
 132 
 133 /*
 134  * CLONEVM not yet correctly implemented: needs to clone the mmap
 135  * instead of duplicating it..
 136  */
 137 static int copy_mm(unsigned long clone_flags, struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139         if (clone_flags & COPYVM) {
 140                 p->mm->swappable = 1;
 141                 p->mm->min_flt = p->mm->maj_flt = 0;
 142                 p->mm->cmin_flt = p->mm->cmaj_flt = 0;
 143                 if (copy_page_tables(p))
 144                         return 1;
 145                 return dup_mmap(p);
 146         } else {
 147                 if (clone_page_tables(p))
 148                         return 1;
 149                 return dup_mmap(p);             /* wrong.. */
 150         }
 151 }
 152 
 153 static void copy_fs(unsigned long clone_flags, struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 154 {
 155         if (current->fs->pwd)
 156                 current->fs->pwd->i_count++;
 157         if (current->fs->root)
 158                 current->fs->root->i_count++;
 159 }
 160 
 161 /*
 162  *  Ok, this is the main fork-routine. It copies the system process
 163  * information (task[nr]) and sets up the necessary registers. It
 164  * also copies the data segment in its entirety.
 165  */
 166 asmlinkage int sys_fork(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168         int nr;
 169         struct task_struct *p;
 170         unsigned long new_stack;
 171         unsigned long clone_flags = COPYVM | SIGCHLD;
 172 
 173         if(!(p = (struct task_struct*)__get_free_page(GFP_KERNEL)))
 174                 goto bad_fork;
 175         new_stack = get_free_page(GFP_KERNEL);
 176         if (!new_stack)
 177                 goto bad_fork_free;
 178         nr = find_empty_process();
 179         if (nr < 0)
 180                 goto bad_fork_free;
 181 
 182         *p = *current;
 183 
 184         if (p->exec_domain && p->exec_domain->use_count)
 185                 (*p->exec_domain->use_count)++;
 186         if (p->binfmt && p->binfmt->use_count)
 187                 (*p->binfmt->use_count)++;
 188 
 189         p->did_exec = 0;
 190         p->kernel_stack_page = new_stack;
 191         *(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
 192         p->state = TASK_UNINTERRUPTIBLE;
 193         p->flags &= ~(PF_PTRACED|PF_TRACESYS);
 194         p->pid = last_pid;
 195         p->p_pptr = p->p_opptr = current;
 196         p->p_cptr = NULL;
 197         SET_LINKS(p);
 198         p->signal = 0;
 199         p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
 200         p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
 201         p->leader = 0;          /* process leadership doesn't inherit */
 202         p->tty_old_pgrp = 0;
 203         p->utime = p->stime = 0;
 204         p->cutime = p->cstime = 0;
 205         p->start_time = jiffies;
 206         task[nr] = p;
 207 
 208         /* copy all the process information */
 209         clone_flags = copy_thread(nr, COPYVM | SIGCHLD, p, &regs);
 210         if (copy_mm(clone_flags, p))
 211                 goto bad_fork_cleanup;
 212         p->semundo = NULL;
 213         copy_files(clone_flags, p);
 214         copy_fs(clone_flags, p);
 215 
 216         /* ok, now we should be set up.. */
 217         p->exit_signal = clone_flags & CSIGNAL;
 218         p->counter = current->counter >> 1;
 219         p->state = TASK_RUNNING;        /* do this last, just in case */
 220         return p->pid;
 221 bad_fork_cleanup:
 222         task[nr] = NULL;
 223         REMOVE_LINKS(p);
 224 bad_fork_free:
 225         free_page(new_stack);
 226         free_page((long) p);
 227 bad_fork:
 228         return -EAGAIN;
 229 }

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