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

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