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

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