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

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