root/kernel/fork.c

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

DEFINITIONS

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

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