4 * Copyright (C) 1991, 1992 Linus Torvalds 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()' 14 #include <linux/errno.h> 15 #include <linux/sched.h> 16 #include <linux/kernel.h> 18 #include <linux/stddef.h> 19 #include <linux/unistd.h> 20 #include <linux/ptrace.h> 21 #include <linux/malloc.h> 22 #include <linux/ldt.h> 23 #include <linux/smp.h> 25 #include <asm/segment.h> 26 #include <asm/system.h> 27 #include <asm/pgtable.h> 31 unsigned long int total_forks
=0;/* Handle normal Linux uptimes. */ 34 staticinlineintfind_empty_process(void) 38 if(nr_tasks
>= NR_TASKS
- MIN_TASKS_LEFT_FOR_ROOT
) { 43 long max_tasks
= current
->rlim
[RLIMIT_NPROC
].rlim_cur
; 45 max_tasks
--;/* count the new process.. */ 46 if(max_tasks
< nr_tasks
) { 47 struct task_struct
*p
; 49 if(p
->uid
== current
->uid
) 55 for(i
=0; i
< NR_TASKS
; i
++) { 62 static intget_pid(unsigned long flags
) 64 struct task_struct
*p
; 69 if((++last_pid
) &0xffff8000) 72 if(p
->pid
== last_pid
|| 73 p
->pgrp
== last_pid
|| 74 p
->session
== last_pid
) 80 staticinlineintdup_mmap(struct mm_struct
* mm
) 82 struct vm_area_struct
* mpnt
, **p
, *tmp
; 86 for(mpnt
= current
->mm
->mmap
; mpnt
; mpnt
= mpnt
->vm_next
) { 87 tmp
= (struct vm_area_struct
*)kmalloc(sizeof(struct vm_area_struct
), GFP_KERNEL
); 93 tmp
->vm_flags
&= ~VM_LOCKED
; 97 tmp
->vm_inode
->i_count
++; 98 /* insert tmp into the share list, just after mpnt */ 99 tmp
->vm_next_share
->vm_prev_share
= tmp
; 100 mpnt
->vm_next_share
= tmp
; 101 tmp
->vm_prev_share
= mpnt
; 103 if(tmp
->vm_ops
&& tmp
->vm_ops
->open
) 104 tmp
->vm_ops
->open(tmp
); 105 if(copy_page_range(mm
, current
->mm
, tmp
)) { 116 staticinlineintcopy_mm(unsigned long clone_flags
,struct task_struct
* tsk
) 118 if(!(clone_flags
& CLONE_VM
)) { 119 struct mm_struct
* mm
=kmalloc(sizeof(*tsk
->mm
), GFP_KERNEL
); 126 tsk
->min_flt
= tsk
->maj_flt
=0; 127 tsk
->cmin_flt
= tsk
->cmaj_flt
=0; 128 tsk
->nswap
= tsk
->cnswap
=0; 129 if(new_page_tables(tsk
)) 132 free_page_tables(mm
); 137 SET_PAGE_DIR(tsk
, current
->mm
->pgd
); 138 current
->mm
->count
++; 142 staticinlineintcopy_fs(unsigned long clone_flags
,struct task_struct
* tsk
) 144 if(clone_flags
& CLONE_FS
) { 145 current
->fs
->count
++; 148 tsk
->fs
=kmalloc(sizeof(*tsk
->fs
), GFP_KERNEL
); 152 tsk
->fs
->umask
= current
->fs
->umask
; 153 if((tsk
->fs
->root
= current
->fs
->root
)) 154 tsk
->fs
->root
->i_count
++; 155 if((tsk
->fs
->pwd
= current
->fs
->pwd
)) 156 tsk
->fs
->pwd
->i_count
++; 160 staticinlineintcopy_files(unsigned long clone_flags
,struct task_struct
* tsk
) 163 struct files_struct
*oldf
, *newf
; 164 struct file
**old_fds
, **new_fds
; 166 oldf
= current
->files
; 167 if(clone_flags
& CLONE_FILES
) { 172 newf
=kmalloc(sizeof(*newf
), GFP_KERNEL
); 178 newf
->close_on_exec
= oldf
->close_on_exec
; 179 newf
->open_fds
= oldf
->open_fds
; 183 for(i
= NR_OPEN
; i
!=0; i
--) { 184 struct file
* f
= *old_fds
; 194 staticinlineintcopy_sighand(unsigned long clone_flags
,struct task_struct
* tsk
) 196 if(clone_flags
& CLONE_SIGHAND
) { 197 current
->sig
->count
++; 200 tsk
->sig
=kmalloc(sizeof(*tsk
->sig
), GFP_KERNEL
); 204 memcpy(tsk
->sig
->action
, current
->sig
->action
,sizeof(tsk
->sig
->action
)); 209 * Ok, this is the main fork-routine. It copies the system process 210 * information (task[nr]) and sets up the necessary registers. It 211 * also copies the data segment in its entirety. 213 intdo_fork(unsigned long clone_flags
,unsigned long usp
,struct pt_regs
*regs
) 217 unsigned long new_stack
; 218 struct task_struct
*p
; 220 p
= (struct task_struct
*)kmalloc(sizeof(*p
), GFP_KERNEL
); 223 new_stack
=alloc_kernel_stack(); 225 goto bad_fork_free_p
; 227 nr
=find_empty_process(); 229 goto bad_fork_free_stack
; 233 if(p
->exec_domain
&& p
->exec_domain
->use_count
) 234 (*p
->exec_domain
->use_count
)++; 235 if(p
->binfmt
&& p
->binfmt
->use_count
) 236 (*p
->binfmt
->use_count
)++; 240 p
->kernel_stack_page
= new_stack
; 241 *(unsigned long*) p
->kernel_stack_page
= STACK_MAGIC
; 242 p
->state
= TASK_UNINTERRUPTIBLE
; 243 p
->flags
&= ~(PF_PTRACED
|PF_TRACESYS
|PF_SUPERPRIV
); 244 p
->flags
|= PF_FORKNOEXEC
; 245 p
->pid
=get_pid(clone_flags
); 248 p
->p_pptr
= p
->p_opptr
= current
; 251 p
->it_real_value
= p
->it_virt_value
= p
->it_prof_value
=0; 252 p
->it_real_incr
= p
->it_virt_incr
= p
->it_prof_incr
=0; 253 init_timer(&p
->real_timer
); 254 p
->real_timer
.data
= (unsigned long) p
; 255 p
->leader
=0;/* process leadership doesn't inherit */ 257 p
->utime
= p
->stime
=0; 258 p
->cutime
= p
->cstime
=0; 260 p
->processor
= NO_PROC_ID
; 263 p
->start_time
= jiffies
; 269 /* copy all the process information */ 270 if(copy_files(clone_flags
, p
)) 271 goto bad_fork_cleanup
; 272 if(copy_fs(clone_flags
, p
)) 273 goto bad_fork_cleanup_files
; 274 if(copy_sighand(clone_flags
, p
)) 275 goto bad_fork_cleanup_fs
; 276 if(copy_mm(clone_flags
, p
)) 277 goto bad_fork_cleanup_sighand
; 278 copy_thread(nr
, clone_flags
, usp
, p
, regs
); 281 /* ok, now we should be set up.. */ 283 p
->exit_signal
= clone_flags
& CSIGNAL
; 284 p
->counter
= current
->counter
>>1; 285 wake_up_process(p
);/* do this last, just in case */ 289 bad_fork_cleanup_sighand
: 293 bad_fork_cleanup_files
: 296 if(p
->exec_domain
&& p
->exec_domain
->use_count
) 297 (*p
->exec_domain
->use_count
)--; 298 if(p
->binfmt
&& p
->binfmt
->use_count
) 299 (*p
->binfmt
->use_count
)--; 304 free_kernel_stack(new_stack
);