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

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