root/kernel/fork.c

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

DEFINITIONS

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

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