root/kernel/fork.c

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

DEFINITIONS

This source file includes following definitions.
  1. find_empty_process
  2. dup_mmap
  3. copy_mm
  4. copy_fs
  5. copy_files
  6. copy_sighand
  7. do_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/config.h>
  15 #include <linux/errno.h>
  16 #include <linux/sched.h>
  17 #include <linux/kernel.h>
  18 #include <linux/mm.h>
  19 #include <linux/stddef.h>
  20 #include <linux/unistd.h>
  21 #include <linux/ptrace.h>
  22 #include <linux/malloc.h>
  23 #include <linux/ldt.h>
  24 #include <linux/smp.h>
  25 
  26 #include <asm/segment.h>
  27 #include <asm/system.h>
  28 #include <asm/pgtable.h>
  29 
  30 int nr_tasks=1;
  31 int nr_running=1;
  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;
  37         int this_user_tasks;
  38         struct task_struct *p;
  39 
  40         if (nr_tasks >= NR_TASKS - MIN_TASKS_LEFT_FOR_ROOT) {
  41                 if (current->uid)
  42                         return -EAGAIN;
  43         }
  44 repeat:
  45         if(smp_threads_ready) {
  46                 if ((++last_pid) & 0xffff8000)
  47                         last_pid=1;
  48         }
  49         this_user_tasks = 0;
  50         for_each_task (p) {
  51                 if (p->uid == current->uid)
  52                         this_user_tasks++;
  53                 if (smp_threads_ready && (p->pid == last_pid ||
  54                     p->pgrp == last_pid ||
  55                     p->session == last_pid))
  56                         goto repeat;
  57         }
  58         if (this_user_tasks > current->rlim[RLIMIT_NPROC].rlim_cur)
  59                 if (current->uid)
  60                         return -EAGAIN;
  61         for (i = 0 ; i < NR_TASKS ; i++) {
  62                 if (!task[i])
  63                         return i;
  64         }
  65         return -EAGAIN;
  66 }
  67 
  68 static int dup_mmap(struct mm_struct * mm)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70         struct vm_area_struct * mpnt, **p, *tmp;
  71 
  72         mm->mmap = NULL;
  73         p = &mm->mmap;
  74         for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
  75                 tmp = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
  76                 if (!tmp) {
  77                         exit_mmap(mm);
  78                         return -ENOMEM;
  79                 }
  80                 *tmp = *mpnt;
  81                 tmp->vm_mm = mm;
  82                 tmp->vm_next = NULL;
  83                 if (tmp->vm_inode) {
  84                         tmp->vm_inode->i_count++;
  85                         /* insert tmp into the share list, just after mpnt */
  86                         tmp->vm_next_share->vm_prev_share = tmp;
  87                         mpnt->vm_next_share = tmp;
  88                         tmp->vm_prev_share = mpnt;
  89                 }
  90                 if (tmp->vm_ops && tmp->vm_ops->open)
  91                         tmp->vm_ops->open(tmp);
  92                 if (copy_page_range(mm, current->mm, tmp)) {
  93                         exit_mmap(mm);
  94                         return -ENOMEM;
  95                 }
  96                 *p = tmp;
  97                 p = &tmp->vm_next;
  98         }
  99         build_mmap_avl(mm);
 100         return 0;
 101 }
 102 
 103 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 104 {
 105         if (clone_flags & CLONE_VM) {
 106                 SET_PAGE_DIR(tsk, current->mm->pgd);
 107                 current->mm->count++;
 108                 return 0;
 109         }
 110         tsk->mm = kmalloc(sizeof(*tsk->mm), GFP_KERNEL);
 111         if (!tsk->mm)
 112                 return -1;
 113         *tsk->mm = *current->mm;
 114         tsk->mm->count = 1;
 115         tsk->min_flt = tsk->maj_flt = 0;
 116         tsk->cmin_flt = tsk->cmaj_flt = 0;
 117         if (new_page_tables(tsk))
 118                 return -1;
 119         if (dup_mmap(tsk->mm)) {
 120                 free_page_tables(tsk);
 121                 return -1;
 122         }
 123         return 0;
 124 }
 125 
 126 static int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 127 {
 128         if (clone_flags & CLONE_FS) {
 129                 current->fs->count++;
 130                 return 0;
 131         }
 132         tsk->fs = kmalloc(sizeof(*tsk->fs), GFP_KERNEL);
 133         if (!tsk->fs)
 134                 return -1;
 135         tsk->fs->count = 1;
 136         tsk->fs->umask = current->fs->umask;
 137         if ((tsk->fs->root = current->fs->root))
 138                 tsk->fs->root->i_count++;
 139         if ((tsk->fs->pwd = current->fs->pwd))
 140                 tsk->fs->pwd->i_count++;
 141         return 0;
 142 }
 143 
 144 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 145 {
 146         int i;
 147 
 148         if (clone_flags & CLONE_FILES) {
 149                 current->files->count++;
 150                 return 0;
 151         }
 152         tsk->files = kmalloc(sizeof(*tsk->files), GFP_KERNEL);
 153         if (!tsk->files)
 154                 return -1;
 155         tsk->files->count = 1;
 156         memcpy(&tsk->files->close_on_exec, &current->files->close_on_exec,
 157                 sizeof(tsk->files->close_on_exec));
 158         for (i = 0; i < NR_OPEN; i++) {
 159                 struct file * f = current->files->fd[i];
 160                 if (f)
 161                         f->f_count++;
 162                 tsk->files->fd[i] = f;
 163         }
 164         return 0;
 165 }
 166 
 167 static int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 168 {
 169         if (clone_flags & CLONE_SIGHAND) {
 170                 current->sig->count++;
 171                 return 0;
 172         }
 173         tsk->sig = kmalloc(sizeof(*tsk->sig), GFP_KERNEL);
 174         if (!tsk->sig)
 175                 return -1;
 176         tsk->sig->count = 1;
 177         memcpy(tsk->sig->action, current->sig->action, sizeof(tsk->sig->action));
 178         return 0;
 179 }
 180 
 181 /*
 182  *  Ok, this is the main fork-routine. It copies the system process
 183  * information (task[nr]) and sets up the necessary registers. It
 184  * also copies the data segment in its entirety.
 185  */
 186 int do_fork(unsigned long clone_flags, unsigned long usp, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188         int nr;
 189         int error = -ENOMEM;
 190         unsigned long new_stack;
 191         struct task_struct *p;
 192 
 193         p = (struct task_struct *) kmalloc(sizeof(*p), GFP_KERNEL);
 194         if (!p)
 195                 goto bad_fork;
 196         new_stack = get_free_page(GFP_KERNEL);
 197         if (!new_stack)
 198                 goto bad_fork_free_p;
 199         error = -EAGAIN;
 200         nr = find_empty_process();
 201         if (nr < 0)
 202                 goto bad_fork_free_stack;
 203 
 204         *p = *current;
 205 
 206         if (p->exec_domain && p->exec_domain->use_count)
 207                 (*p->exec_domain->use_count)++;
 208         if (p->binfmt && p->binfmt->use_count)
 209                 (*p->binfmt->use_count)++;
 210 
 211         p->did_exec = 0;
 212         p->swappable = 0;
 213         p->kernel_stack_page = new_stack;
 214         *(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
 215         p->state = TASK_UNINTERRUPTIBLE;
 216         p->flags &= ~(PF_PTRACED|PF_TRACESYS);
 217         p->pid = last_pid;
 218         p->next_run = NULL;
 219         p->prev_run = NULL;
 220         p->p_pptr = p->p_opptr = current;
 221         p->p_cptr = NULL;
 222         p->signal = 0;
 223         p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
 224         p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
 225         init_timer(&p->real_timer);
 226         p->real_timer.data = (unsigned long) p;
 227         p->leader = 0;          /* process leadership doesn't inherit */
 228         p->tty_old_pgrp = 0;
 229         p->utime = p->stime = 0;
 230         p->cutime = p->cstime = 0;
 231 #ifdef CONFIG_SMP
 232         p->processor = NO_PROC_ID;
 233         p->lock_depth = 1;
 234 #endif
 235         p->start_time = jiffies;
 236         task[nr] = p;
 237         SET_LINKS(p);
 238         nr_tasks++;
 239 
 240         error = -ENOMEM;
 241         /* copy all the process information */
 242         if (copy_files(clone_flags, p))
 243                 goto bad_fork_cleanup;
 244         if (copy_fs(clone_flags, p))
 245                 goto bad_fork_cleanup_files;
 246         if (copy_sighand(clone_flags, p))
 247                 goto bad_fork_cleanup_fs;
 248         if (copy_mm(clone_flags, p))
 249                 goto bad_fork_cleanup_sighand;
 250         copy_thread(nr, clone_flags, usp, p, regs);
 251         p->semundo = NULL;
 252 
 253         /* ok, now we should be set up.. */
 254         p->swappable = 1;
 255         p->exit_signal = clone_flags & CSIGNAL;
 256         p->counter = current->counter >> 1;
 257         wake_up_process(p);                     /* do this last, just in case */
 258         return p->pid;
 259 
 260 bad_fork_cleanup_sighand:
 261         exit_sighand(p);
 262 bad_fork_cleanup_fs:
 263         exit_fs(p);
 264 bad_fork_cleanup_files:
 265         exit_files(p);
 266 bad_fork_cleanup:
 267         if (p->exec_domain && p->exec_domain->use_count)
 268                 (*p->exec_domain->use_count)--;
 269         if (p->binfmt && p->binfmt->use_count)
 270                 (*p->binfmt->use_count)--;
 271         task[nr] = NULL;
 272         REMOVE_LINKS(p);
 273         nr_tasks--;
 274 bad_fork_free_stack:
 275         free_page(new_stack);
 276 bad_fork_free_p:
 277         kfree(p);
 278 bad_fork:
 279         return error;
 280 }

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