root/kernel/fork.c

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

DEFINITIONS

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

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