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. 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 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->min_flt = p->mm->maj_flt = 0;
 141                 p->mm->cmin_flt = p->mm->cmaj_flt = 0;
 142                 if (copy_page_tables(p))
 143                         return 1;
 144                 return dup_mmap(p);
 145         } else {
 146                 if (clone_page_tables(p))
 147                         return 1;
 148                 return dup_mmap(p);             /* wrong.. */
 149         }
 150 }
 151 
 152 static void copy_fs(unsigned long clone_flags, struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 153 {
 154         if (current->fs->pwd)
 155                 current->fs->pwd->i_count++;
 156         if (current->fs->root)
 157                 current->fs->root->i_count++;
 158 }
 159 
 160 /*
 161  *  Ok, this is the main fork-routine. It copies the system process
 162  * information (task[nr]) and sets up the necessary registers. It
 163  * also copies the data segment in its entirety.
 164  */
 165 int do_fork(unsigned long clone_flags, unsigned long usp, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 166 {
 167         int nr;
 168         unsigned long new_stack;
 169         struct task_struct *p;
 170 
 171         if(!(p = (struct task_struct*)__get_free_page(GFP_KERNEL)))
 172                 goto bad_fork;
 173         new_stack = get_free_page(GFP_KERNEL);
 174         if (!new_stack)
 175                 goto bad_fork_free;
 176         nr = find_empty_process();
 177         if (nr < 0)
 178                 goto bad_fork_free;
 179 
 180         *p = *current;
 181 
 182         if (p->exec_domain && p->exec_domain->use_count)
 183                 (*p->exec_domain->use_count)++;
 184         if (p->binfmt && p->binfmt->use_count)
 185                 (*p->binfmt->use_count)++;
 186 
 187         p->did_exec = 0;
 188         p->kernel_stack_page = new_stack;
 189         *(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
 190         p->state = TASK_UNINTERRUPTIBLE;
 191         p->flags &= ~(PF_PTRACED|PF_TRACESYS);
 192         p->pid = last_pid;
 193         p->p_pptr = p->p_opptr = current;
 194         p->p_cptr = NULL;
 195         p->signal = 0;
 196         p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
 197         p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
 198         p->leader = 0;          /* process leadership doesn't inherit */
 199         p->tty_old_pgrp = 0;
 200         p->utime = p->stime = 0;
 201         p->cutime = p->cstime = 0;
 202         p->start_time = jiffies;
 203         p->mm->swappable = 0;   /* don't try to swap it out before it's set up */
 204         task[nr] = p;
 205         SET_LINKS(p);
 206 
 207         /* copy all the process information */
 208         copy_thread(nr, clone_flags, usp, p, regs);
 209         if (copy_mm(clone_flags, p))
 210                 goto bad_fork_cleanup;
 211         p->semundo = NULL;
 212         copy_files(clone_flags, p);
 213         copy_fs(clone_flags, p);
 214 
 215         /* ok, now we should be set up.. */
 216         p->mm->swappable = 1;
 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] */