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. sys_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/segment.h>
  21 #include <linux/ptrace.h>
  22 
  23 #include <asm/segment.h>
  24 #include <asm/system.h>
  25 
  26 extern "C" void ret_from_sys_call(void) __asm__("ret_from_sys_call");
  27 
  28 #define MAX_TASKS_PER_USER (NR_TASKS/2)
  29 
  30 extern int shm_fork(struct task_struct *, struct task_struct *);
  31 long last_pid=0;
  32 
  33 static int find_empty_process(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35         int i, task_nr;
  36         int this_user_tasks;
  37 
  38 repeat:
  39         if ((++last_pid) & 0xffff8000)
  40                 last_pid=1;
  41         this_user_tasks = 0;
  42         for(i=0 ; i < NR_TASKS ; i++) {
  43                 if (!task[i])
  44                         continue;
  45                 if (task[i]->uid == current->uid)
  46                         this_user_tasks++;
  47                 if (task[i]->pid == last_pid || task[i]->pgrp == last_pid)
  48                         goto repeat;
  49         }
  50         if (this_user_tasks > MAX_TASKS_PER_USER && current->uid)
  51                 return -EAGAIN;
  52 /* Only the super-user can fill the last available slot */
  53         task_nr = 0;
  54         for(i=1 ; i<NR_TASKS ; i++)
  55                 if (!task[i])
  56                         if (task_nr)
  57                                 return task_nr;
  58                         else
  59                                 task_nr = i;
  60         if (task_nr && suser())
  61                 return task_nr;
  62         return -EAGAIN;
  63 }
  64 
  65 static struct file * copy_fd(struct file * old_file)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67         struct file * new_file = get_empty_filp();
  68         int error;
  69 
  70         if (new_file) {
  71                 memcpy(new_file,old_file,sizeof(struct file));
  72                 new_file->f_count = 1;
  73                 if (new_file->f_inode)
  74                         new_file->f_inode->i_count++;
  75                 if (new_file->f_op && new_file->f_op->open) {
  76                         error = new_file->f_op->open(new_file->f_inode,new_file);
  77                         if (error) {
  78                                 iput(new_file->f_inode);
  79                                 new_file->f_count = 0;
  80                                 new_file = NULL;
  81                         }
  82                 }
  83         }
  84         return new_file;
  85 }
  86 
  87 int dup_mmap(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89         struct vm_area_struct * mpnt, **p, *tmp;
  90 
  91         tsk->mmap = NULL;
  92         p = &tsk->mmap;
  93         for (mpnt = current->mmap ; mpnt ; mpnt = mpnt->vm_next) {
  94                 tmp = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
  95                 if (!tmp)
  96                         return -ENOMEM;
  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                 *p = tmp;
 103                 p = &tmp->vm_next;
 104         }
 105         return 0;
 106 }
 107 
 108 #define IS_CLONE (regs.orig_eax == __NR_clone)
 109 #define copy_vm(p) ((clone_flags & COPYVM)?copy_page_tables(p):clone_page_tables(p))
 110 
 111 /*
 112  *  Ok, this is the main fork-routine. It copies the system process
 113  * information (task[nr]) and sets up the necessary registers. It
 114  * also copies the data segment in it's entirety.
 115  */
 116 extern "C" int sys_fork(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118         struct pt_regs * childregs;
 119         struct task_struct *p;
 120         int i,nr;
 121         struct file *f;
 122         unsigned long clone_flags = COPYVM | SIGCHLD;
 123 
 124         if(!(p = (struct task_struct*)__get_free_page(GFP_KERNEL)))
 125                 goto bad_fork;
 126         nr = find_empty_process();
 127         if (nr < 0)
 128                 goto bad_fork_free;
 129         task[nr] = p;
 130         *p = *current;
 131         p->kernel_stack_page = 0;
 132         p->state = TASK_UNINTERRUPTIBLE;
 133         p->flags &= ~(PF_PTRACED|PF_TRACESYS);
 134         p->pid = last_pid;
 135         p->swappable = 1;
 136         p->p_pptr = p->p_opptr = current;
 137         p->p_cptr = NULL;
 138         SET_LINKS(p);
 139         p->signal = 0;
 140         p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
 141         p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
 142         p->leader = 0;          /* process leadership doesn't inherit */
 143         p->utime = p->stime = 0;
 144         p->cutime = p->cstime = 0;
 145         p->min_flt = p->maj_flt = 0;
 146         p->cmin_flt = p->cmaj_flt = 0;
 147         p->start_time = jiffies;
 148 /*
 149  * set up new TSS and kernel stack
 150  */
 151         if (!(p->kernel_stack_page = __get_free_page(GFP_KERNEL)))
 152                 goto bad_fork_cleanup;
 153         p->tss.es = KERNEL_DS;
 154         p->tss.cs = KERNEL_CS;
 155         p->tss.ss = KERNEL_DS;
 156         p->tss.ds = KERNEL_DS;
 157         p->tss.fs = USER_DS;
 158         p->tss.gs = KERNEL_DS;
 159         p->tss.ss0 = KERNEL_DS;
 160         p->tss.esp0 = p->kernel_stack_page + PAGE_SIZE;
 161         p->tss.tr = _TSS(nr);
 162         childregs = ((struct pt_regs *) (p->kernel_stack_page + PAGE_SIZE)) - 1;
 163         p->tss.esp = (unsigned long) childregs;
 164         p->tss.eip = (unsigned long) ret_from_sys_call;
 165         *childregs = regs;
 166         childregs->eax = 0;
 167         p->tss.back_link = 0;
 168         p->tss.eflags = regs.eflags & 0xffffcfff;       /* iopl is always 0 for a new process */
 169         if (IS_CLONE) {
 170                 if (regs.ebx)
 171                         childregs->esp = regs.ebx;
 172                 clone_flags = regs.ecx;
 173                 if (childregs->esp == regs.esp)
 174                         clone_flags |= COPYVM;
 175         }
 176         p->exit_signal = clone_flags & CSIGNAL;
 177         p->tss.ldt = _LDT(nr);
 178         if (p->ldt) {
 179                 if (p->ldt = (struct desc_struct*) __get_free_page(GFP_KERNEL))
 180                         memcpy(p->ldt, current->ldt, PAGE_SIZE);
 181         }
 182         p->tss.bitmap = offsetof(struct tss_struct,io_bitmap);
 183         for (i = 0; i < IO_BITMAP_SIZE+1 ; i++) /* IO bitmap is actually SIZE+1 */
 184                 p->tss.io_bitmap[i] = ~0;
 185         if (last_task_used_math == current)
 186                 __asm__("clts ; fnsave %0 ; frstor %0":"=m" (p->tss.i387));
 187         p->semun = NULL; p->shm = NULL;
 188         if (copy_vm(p) || shm_fork(current, p))
 189                 goto bad_fork_cleanup;
 190         if (clone_flags & COPYFD) {
 191                 for (i=0; i<NR_OPEN;i++)
 192                         if ((f = p->filp[i]) != NULL)
 193                                 p->filp[i] = copy_fd(f);
 194         } else {
 195                 for (i=0; i<NR_OPEN;i++)
 196                         if ((f = p->filp[i]) != NULL)
 197                                 f->f_count++;
 198         }
 199         if (current->pwd)
 200                 current->pwd->i_count++;
 201         if (current->root)
 202                 current->root->i_count++;
 203         if (current->executable)
 204                 current->executable->i_count++;
 205         dup_mmap(p);
 206         set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
 207         if (p->ldt)
 208                 set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,p->ldt, 512);
 209         else
 210                 set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&default_ldt, 1);
 211 
 212         p->counter = current->counter >> 1;
 213         p->state = TASK_RUNNING;        /* do this last, just in case */
 214         return p->pid;
 215 bad_fork_cleanup:
 216         task[nr] = NULL;
 217         REMOVE_LINKS(p);
 218         free_page(p->kernel_stack_page);
 219 bad_fork_free:
 220         free_page((long) p);
 221 bad_fork:
 222         return -EAGAIN;
 223 }

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