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/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 /*
  28  * This is how a process data structure is allocated. In most
  29  * cases, the "tsk" pointers point to the same allocation unit
  30  * substructures, but if the new process shares part (or all)
  31  * of the sub-units with the parent process, the tsk pointers
  32  * may point to the parent instead.
  33  *
  34  * Regardless, we always allocate the full allocation unit, as
  35  * the normal fork() semantics require all of them and doing
  36  * suballocations would be wasteful.
  37  */
  38 struct allocation_struct {
  39         struct task_struct tsk;
  40         struct sigaction sigaction[32];
  41         struct fs_struct fs;
  42         struct files_struct files;
  43         struct mm_struct mm;
  44 };
  45 
  46 int nr_tasks=1;
  47 int nr_running=1;
  48 long last_pid=0;
  49 
  50 static int find_empty_process(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52         int free_task;
  53         int i, tasks_free;
  54         int this_user_tasks;
  55 
  56 repeat:
  57         if ((++last_pid) & 0xffff8000)
  58                 last_pid=1;
  59         this_user_tasks = 0;
  60         tasks_free = 0;
  61         free_task = -EAGAIN;
  62         i = NR_TASKS;
  63         while (--i > 0) {
  64                 if (!task[i]) {
  65                         free_task = i;
  66                         tasks_free++;
  67                         continue;
  68                 }
  69                 if (task[i]->uid == current->uid)
  70                         this_user_tasks++;
  71                 if (task[i]->pid == last_pid || task[i]->pgrp == last_pid ||
  72                     task[i]->session == last_pid)
  73                         goto repeat;
  74         }
  75         if (tasks_free <= MIN_TASKS_LEFT_FOR_ROOT ||
  76             this_user_tasks > current->rlim[RLIMIT_NPROC].rlim_cur)
  77                 if (current->uid)
  78                         return -EAGAIN;
  79         return free_task;
  80 }
  81 
  82 static int dup_mmap(struct mm_struct * mm)
     /* [previous][next][first][last][top][bottom][index][help] */
  83 {
  84         struct vm_area_struct * mpnt, **p, *tmp;
  85 
  86         mm->mmap = NULL;
  87         p = &mm->mmap;
  88         for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
  89                 tmp = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
  90                 if (!tmp) {
  91                         exit_mmap(mm);
  92                         return -ENOMEM;
  93                 }
  94                 *tmp = *mpnt;
  95                 tmp->vm_mm = mm;
  96                 tmp->vm_next = NULL;
  97                 if (tmp->vm_inode) {
  98                         tmp->vm_inode->i_count++;
  99                         /* insert tmp into the share list, just after mpnt */
 100                         tmp->vm_next_share->vm_prev_share = tmp;
 101                         mpnt->vm_next_share = tmp;
 102                         tmp->vm_prev_share = mpnt;
 103                 }
 104                 if (tmp->vm_ops && tmp->vm_ops->open)
 105                         tmp->vm_ops->open(tmp);
 106                 *p = tmp;
 107                 p = &tmp->vm_next;
 108         }
 109         build_mmap_avl(mm);
 110         return 0;
 111 }
 112 
 113 static int copy_mm(unsigned long clone_flags, struct allocation_struct * u)
     /* [previous][next][first][last][top][bottom][index][help] */
 114 {
 115         if (clone_flags & CLONE_VM) {
 116                 if (clone_page_tables(&u->tsk))
 117                         return -1;
 118                 current->mm->count++;
 119                 mem_map[MAP_NR(current->mm)]++;
 120                 return 0;
 121         }
 122         u->tsk.mm = &u->mm;
 123         u->mm = *current->mm;
 124         u->mm.count = 1;
 125         u->mm.min_flt = u->mm.maj_flt = 0;
 126         u->mm.cmin_flt = u->mm.cmaj_flt = 0;
 127         if (copy_page_tables(&u->tsk))
 128                 return -1;
 129         if (dup_mmap(&u->mm))
 130                 return -1;
 131         mem_map[MAP_NR(u)]++;
 132         return 0;
 133 }
 134 
 135 static void copy_fs(unsigned long clone_flags, struct allocation_struct * u)
     /* [previous][next][first][last][top][bottom][index][help] */
 136 {
 137         if (clone_flags & CLONE_FS) {
 138                 current->fs->count++;
 139                 mem_map[MAP_NR(current->fs)]++;
 140                 return;
 141         }
 142         u->tsk.fs = &u->fs;
 143         u->fs = *current->fs;
 144         u->fs.count = 1;
 145         if (u->fs.pwd)
 146                 u->fs.pwd->i_count++;
 147         if (u->fs.root)
 148                 u->fs.root->i_count++;
 149         mem_map[MAP_NR(u)]++;
 150 }
 151 
 152 static void copy_files(unsigned long clone_flags, struct allocation_struct * u)
     /* [previous][next][first][last][top][bottom][index][help] */
 153 {
 154         int i;
 155 
 156         if (clone_flags & CLONE_FILES) {
 157                 current->files->count++;
 158                 mem_map[MAP_NR(current->files)]++;
 159                 return;
 160         }
 161         u->tsk.files = &u->files;
 162         u->files = *current->files;
 163         u->files.count = 1;
 164         for (i = 0; i < NR_OPEN; i++) {
 165                 struct file * f = u->files.fd[i];
 166                 if (f)
 167                         f->f_count++;
 168         }
 169         mem_map[MAP_NR(u)]++;
 170 }
 171 
 172 static void copy_sighand(unsigned long clone_flags, struct allocation_struct * u)
     /* [previous][next][first][last][top][bottom][index][help] */
 173 {
 174         if (clone_flags & CLONE_SIGHAND) {
 175                 mem_map[MAP_NR(current->sigaction)]++;
 176                 return;
 177         }
 178         u->tsk.sigaction = u->sigaction;
 179         memcpy(u->sigaction, current->sigaction, sizeof(u->sigaction));
 180         mem_map[MAP_NR(u)]++;
 181 }
 182 
 183 /*
 184  *  Ok, this is the main fork-routine. It copies the system process
 185  * information (task[nr]) and sets up the necessary registers. It
 186  * also copies the data segment in its entirety.
 187  */
 188 int do_fork(unsigned long clone_flags, unsigned long usp, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 189 {
 190         int nr;
 191         int error = -ENOMEM;
 192         unsigned long new_stack;
 193         struct task_struct *p;
 194         struct allocation_struct *alloc;
 195 
 196         alloc = (struct allocation_struct *) __get_free_page(GFP_KERNEL);
 197         if (!alloc)
 198                 goto bad_fork;
 199         p = &alloc->tsk;
 200         new_stack = get_free_page(GFP_KERNEL);
 201         if (!new_stack)
 202                 goto bad_fork_free;
 203         error = -EAGAIN;
 204         nr = find_empty_process();
 205         if (nr < 0)
 206                 goto bad_fork_free;
 207 
 208         *p = *current;
 209 
 210         if (p->exec_domain && p->exec_domain->use_count)
 211                 (*p->exec_domain->use_count)++;
 212         if (p->binfmt && p->binfmt->use_count)
 213                 (*p->binfmt->use_count)++;
 214 
 215         p->did_exec = 0;
 216         p->kernel_stack_page = new_stack;
 217         *(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
 218         p->state = TASK_UNINTERRUPTIBLE;
 219         p->flags &= ~(PF_PTRACED|PF_TRACESYS);
 220         p->pid = last_pid;
 221         p->next_run = NULL;
 222         p->prev_run = NULL;
 223         p->p_pptr = p->p_opptr = current;
 224         p->p_cptr = NULL;
 225         p->signal = 0;
 226         p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
 227         p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
 228         init_timer(&p->real_timer);
 229         p->real_timer.data = (unsigned long) p;
 230         p->leader = 0;          /* process leadership doesn't inherit */
 231         p->tty_old_pgrp = 0;
 232         p->utime = p->stime = 0;
 233         p->cutime = p->cstime = 0;
 234         p->start_time = jiffies;
 235         task[nr] = p;
 236         SET_LINKS(p);
 237         nr_tasks++;
 238 
 239         error = -ENOMEM;
 240         /* copy all the process information */
 241         copy_thread(nr, clone_flags, usp, p, regs);
 242         if (copy_mm(clone_flags, alloc))
 243                 goto bad_fork_cleanup;
 244         p->semundo = NULL;
 245         copy_files(clone_flags, alloc);
 246         copy_fs(clone_flags, alloc);
 247         copy_sighand(clone_flags, alloc);
 248 
 249         /* ok, now we should be set up.. */
 250         p->mm->swappable = 1;
 251         p->exit_signal = clone_flags & CSIGNAL;
 252         p->counter = current->counter >> 1;
 253         wake_up_process(p);                     /* do this last, just in case */
 254         return p->pid;
 255 
 256 bad_fork_cleanup:
 257         if (p->exec_domain && p->exec_domain->use_count)
 258                 (*p->exec_domain->use_count)--;
 259         if (p->binfmt && p->binfmt->use_count)
 260                 (*p->binfmt->use_count)--;
 261         task[nr] = NULL;
 262         REMOVE_LINKS(p);
 263         nr_tasks--;
 264 bad_fork_free:
 265         free_page(new_stack);
 266         free_page((long) p);
 267 bad_fork:
 268         return error;
 269 }

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