This source file includes following definitions.
- find_empty_process
- copy_fd
- dup_mmap
- copy_files
- copy_mm
- copy_fs
- sys_fork
1
2
3
4
5
6
7
8
9
10
11
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 #include <linux/segment.h>
21 #include <linux/ptrace.h>
22 #include <linux/malloc.h>
23 #include <linux/ldt.h>
24
25 #include <asm/segment.h>
26 #include <asm/system.h>
27
28 asmlinkage void ret_from_sys_call(void) __asm__("ret_from_sys_call");
29
30
31
32 #define MAX_TASKS_PER_USER (NR_TASKS/2)
33 #define MIN_TASKS_LEFT_FOR_ROOT 4
34
35 long last_pid=0;
36
37 static int find_empty_process(void)
38 {
39 int free_task;
40 int i, tasks_free;
41 int this_user_tasks;
42
43 repeat:
44 if ((++last_pid) & 0xffff8000)
45 last_pid=1;
46 this_user_tasks = 0;
47 tasks_free = 0;
48 free_task = -EAGAIN;
49 i = NR_TASKS;
50 while (--i > 0) {
51 if (!task[i]) {
52 free_task = i;
53 tasks_free++;
54 continue;
55 }
56 if (task[i]->uid == current->uid)
57 this_user_tasks++;
58 if (task[i]->pid == last_pid || task[i]->pgrp == last_pid ||
59 task[i]->session == last_pid)
60 goto repeat;
61 }
62 if (tasks_free <= MIN_TASKS_LEFT_FOR_ROOT ||
63 this_user_tasks > MAX_TASKS_PER_USER)
64 if (current->uid)
65 return -EAGAIN;
66 return free_task;
67 }
68
69 static struct file * copy_fd(struct file * old_file)
70 {
71 struct file * new_file = get_empty_filp();
72 int error;
73
74 if (new_file) {
75 memcpy(new_file,old_file,sizeof(struct file));
76 new_file->f_count = 1;
77 if (new_file->f_inode)
78 new_file->f_inode->i_count++;
79 if (new_file->f_op && new_file->f_op->open) {
80 error = new_file->f_op->open(new_file->f_inode,new_file);
81 if (error) {
82 iput(new_file->f_inode);
83 new_file->f_count = 0;
84 new_file = NULL;
85 }
86 }
87 }
88 return new_file;
89 }
90
91 static int dup_mmap(struct task_struct * tsk)
92 {
93 struct vm_area_struct * mpnt, **p, *tmp;
94
95 tsk->mm->mmap = NULL;
96 p = &tsk->mm->mmap;
97 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
98 tmp = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
99 if (!tmp)
100 return -ENOMEM;
101 *tmp = *mpnt;
102 tmp->vm_task = tsk;
103 tmp->vm_next = NULL;
104 if (tmp->vm_inode)
105 tmp->vm_inode->i_count++;
106 if (tmp->vm_ops && tmp->vm_ops->open)
107 tmp->vm_ops->open(tmp);
108 *p = tmp;
109 p = &tmp->vm_next;
110 }
111 return 0;
112 }
113
114
115
116
117 static void copy_files(unsigned long clone_flags, struct task_struct * p)
118 {
119 int i;
120 struct file * f;
121
122 if (clone_flags & COPYFD) {
123 for (i=0; i<NR_OPEN;i++)
124 if ((f = p->files->fd[i]) != NULL)
125 p->files->fd[i] = copy_fd(f);
126 } else {
127 for (i=0; i<NR_OPEN;i++)
128 if ((f = p->files->fd[i]) != NULL)
129 f->f_count++;
130 }
131 }
132
133
134
135
136
137 static int copy_mm(unsigned long clone_flags, struct task_struct * p)
138 {
139 if (clone_flags & COPYVM) {
140 p->mm->swappable = 1;
141 p->mm->min_flt = p->mm->maj_flt = 0;
142 p->mm->cmin_flt = p->mm->cmaj_flt = 0;
143 if (copy_page_tables(p))
144 return 1;
145 return dup_mmap(p);
146 } else {
147 if (clone_page_tables(p))
148 return 1;
149 return dup_mmap(p);
150 }
151 }
152
153 static void copy_fs(unsigned long clone_flags, struct task_struct * p)
154 {
155 if (current->fs->pwd)
156 current->fs->pwd->i_count++;
157 if (current->fs->root)
158 current->fs->root->i_count++;
159 }
160
161 #define IS_CLONE (regs.orig_eax == __NR_clone)
162
163
164
165
166
167
168 asmlinkage int sys_fork(struct pt_regs regs)
169 {
170 struct pt_regs * childregs;
171 struct task_struct *p;
172 int i,nr;
173 unsigned long clone_flags = COPYVM | SIGCHLD;
174
175 if(!(p = (struct task_struct*)__get_free_page(GFP_KERNEL)))
176 goto bad_fork;
177 nr = find_empty_process();
178 if (nr < 0)
179 goto bad_fork_free;
180 task[nr] = p;
181 *p = *current;
182
183 if (p->exec_domain && p->exec_domain->use_count)
184 (*p->exec_domain->use_count)++;
185 if (p->binfmt && p->binfmt->use_count)
186 (*p->binfmt->use_count)++;
187
188 p->did_exec = 0;
189 p->kernel_stack_page = 0;
190 p->state = TASK_UNINTERRUPTIBLE;
191 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
192 p->pid = last_pid;
193 p->p_pptr = p->p_opptr = current;
194 p->p_cptr = NULL;
195 SET_LINKS(p);
196 p->signal = 0;
197 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
198 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
199 p->leader = 0;
200 p->utime = p->stime = 0;
201 p->cutime = p->cstime = 0;
202 p->start_time = jiffies;
203
204
205
206 if (!(p->kernel_stack_page = get_free_page(GFP_KERNEL)))
207 goto bad_fork_cleanup;
208 *(unsigned long *)p->kernel_stack_page = STACK_MAGIC;
209 p->tss.es = KERNEL_DS;
210 p->tss.cs = KERNEL_CS;
211 p->tss.ss = KERNEL_DS;
212 p->tss.ds = KERNEL_DS;
213 p->tss.fs = USER_DS;
214 p->tss.gs = KERNEL_DS;
215 p->tss.ss0 = KERNEL_DS;
216 p->tss.esp0 = p->kernel_stack_page + PAGE_SIZE;
217 p->tss.tr = _TSS(nr);
218 childregs = ((struct pt_regs *) (p->kernel_stack_page + PAGE_SIZE)) - 1;
219 p->tss.esp = (unsigned long) childregs;
220 p->tss.eip = (unsigned long) ret_from_sys_call;
221 *childregs = regs;
222 childregs->eax = 0;
223 p->tss.back_link = 0;
224 p->tss.eflags = regs.eflags & 0xffffcfff;
225 if (IS_CLONE) {
226 if (regs.ebx)
227 childregs->esp = regs.ebx;
228 clone_flags = regs.ecx;
229 if (childregs->esp == regs.esp)
230 clone_flags |= COPYVM;
231 }
232 p->exit_signal = clone_flags & CSIGNAL;
233 p->tss.ldt = _LDT(nr);
234 if (p->ldt) {
235 p->ldt = (struct desc_struct*) vmalloc(LDT_ENTRIES*LDT_ENTRY_SIZE);
236 if (p->ldt != NULL)
237 memcpy(p->ldt, current->ldt, LDT_ENTRIES*LDT_ENTRY_SIZE);
238 }
239 p->tss.bitmap = offsetof(struct tss_struct,io_bitmap);
240 for (i = 0; i < IO_BITMAP_SIZE+1 ; i++)
241 p->tss.io_bitmap[i] = ~0;
242 if (last_task_used_math == current)
243 __asm__("clts ; fnsave %0 ; frstor %0":"=m" (p->tss.i387));
244 if (copy_mm(clone_flags, p))
245 goto bad_fork_cleanup;
246 p->semundo = NULL;
247 copy_files(clone_flags, p);
248 copy_fs(clone_flags, p);
249 set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
250 if (p->ldt)
251 set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,p->ldt, 512);
252 else
253 set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&default_ldt, 1);
254
255 p->counter = current->counter >> 1;
256 p->state = TASK_RUNNING;
257 return p->pid;
258 bad_fork_cleanup:
259 task[nr] = NULL;
260 REMOVE_LINKS(p);
261 free_page(p->kernel_stack_page);
262 bad_fork_free:
263 free_page((long) p);
264 bad_fork:
265 return -EAGAIN;
266 }