linux-old/kernel/fork.c
<<
>>
Prefs
   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
  27int nr_tasks=1;
  28int nr_running=1;
  29long last_pid=0;
  30
  31static int find_empty_process(void)
  32{
  33        int free_task;
  34        int i, tasks_free;
  35        int this_user_tasks;
  36
  37repeat:
  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
  63static struct file * copy_fd(struct file * old_file)
  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
  85static int dup_mmap(struct task_struct * tsk)
  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 */
 119static void copy_files(unsigned long clone_flags, struct task_struct * p)
 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 */
 139static int copy_mm(unsigned long clone_flags, struct task_struct * p)
 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
 154static void copy_fs(unsigned long clone_flags, struct task_struct * p)
 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 */
 167int do_fork(unsigned long clone_flags, unsigned long usp, struct pt_regs *regs)
 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->p_pptr = p->p_opptr = current;
 196        p->p_cptr = NULL;
 197        p->signal = 0;
 198        p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
 199        p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
 200        p->leader = 0;          /* process leadership doesn't inherit */
 201        p->tty_old_pgrp = 0;
 202        p->utime = p->stime = 0;
 203        p->cutime = p->cstime = 0;
 204        p->start_time = jiffies;
 205        p->mm->swappable = 0;   /* don't try to swap it out before it's set up */
 206        task[nr] = p;
 207        SET_LINKS(p);
 208        nr_tasks++;
 209
 210        /* copy all the process information */
 211        copy_thread(nr, clone_flags, usp, p, regs);
 212        if (copy_mm(clone_flags, p))
 213                goto bad_fork_cleanup;
 214        p->semundo = NULL;
 215        copy_files(clone_flags, p);
 216        copy_fs(clone_flags, p);
 217
 218        /* ok, now we should be set up.. */
 219        p->mm->swappable = 1;
 220        p->exit_signal = clone_flags & CSIGNAL;
 221        p->counter = current->counter >> 1;
 222        p->state = TASK_RUNNING;        /* do this last, just in case */
 223        return p->pid;
 224bad_fork_cleanup:
 225        task[nr] = NULL;
 226        REMOVE_LINKS(p);
 227        nr_tasks--;
 228bad_fork_free:
 229        free_page(new_stack);
 230        free_page((long) p);
 231bad_fork:
 232        return -EAGAIN;
 233}
 234
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.