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

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