This source file includes following definitions.
- find_empty_process
- get_pid
- dup_mmap
- copy_mm
- copy_fs
- copy_files
- copy_sighand
- do_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/ptrace.h>
21 #include <linux/malloc.h>
22 #include <linux/ldt.h>
23 #include <linux/smp.h>
24
25 #include <asm/segment.h>
26 #include <asm/system.h>
27 #include <asm/pgtable.h>
28
29 int nr_tasks=1;
30 int nr_running=1;
31
32 static int find_empty_process(void)
33 {
34 int i;
35 struct task_struct *p;
36
37 if (nr_tasks >= NR_TASKS - MIN_TASKS_LEFT_FOR_ROOT) {
38 if (current->uid)
39 return -EAGAIN;
40 }
41 if (current->uid) {
42 long max_tasks = current->rlim[RLIMIT_NPROC].rlim_cur;
43
44 if (max_tasks < nr_tasks) {
45 for_each_task (p) {
46 if (p->uid == current->uid)
47 if (--max_tasks < 0)
48 return -EAGAIN;
49 }
50 }
51 }
52 for (i = 0 ; i < NR_TASKS ; i++) {
53 if (!task[i])
54 return i;
55 }
56 return -EAGAIN;
57 }
58
59 static int get_pid(unsigned long flags)
60 {
61 static int last_pid = 0;
62 struct task_struct *p;
63
64 if (flags & CLONE_PID)
65 return current->pid;
66 repeat:
67 if ((++last_pid) & 0xffff8000)
68 last_pid=1;
69 for_each_task (p) {
70 if (p->pid == last_pid ||
71 p->pgrp == last_pid ||
72 p->session == last_pid)
73 goto repeat;
74 }
75 return last_pid;
76 }
77
78 static int dup_mmap(struct mm_struct * mm)
79 {
80 struct vm_area_struct * mpnt, **p, *tmp;
81
82 mm->mmap = NULL;
83 p = &mm->mmap;
84 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
85 tmp = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
86 if (!tmp) {
87 exit_mmap(mm);
88 return -ENOMEM;
89 }
90 *tmp = *mpnt;
91 tmp->vm_mm = mm;
92 tmp->vm_next = NULL;
93 if (tmp->vm_inode) {
94 tmp->vm_inode->i_count++;
95
96 tmp->vm_next_share->vm_prev_share = tmp;
97 mpnt->vm_next_share = tmp;
98 tmp->vm_prev_share = mpnt;
99 }
100 if (tmp->vm_ops && tmp->vm_ops->open)
101 tmp->vm_ops->open(tmp);
102 if (copy_page_range(mm, current->mm, tmp)) {
103 exit_mmap(mm);
104 return -ENOMEM;
105 }
106 *p = tmp;
107 p = &tmp->vm_next;
108 }
109 build_mmap_avl(mm);
110 return 0;
111 }
112
113 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
114 {
115 if (clone_flags & CLONE_VM) {
116 SET_PAGE_DIR(tsk, current->mm->pgd);
117 current->mm->count++;
118 return 0;
119 }
120 tsk->mm = kmalloc(sizeof(*tsk->mm), GFP_KERNEL);
121 if (!tsk->mm)
122 return -1;
123 *tsk->mm = *current->mm;
124 tsk->mm->count = 1;
125 tsk->min_flt = tsk->maj_flt = 0;
126 tsk->cmin_flt = tsk->cmaj_flt = 0;
127 tsk->nswap = tsk->cnswap = 0;
128 if (new_page_tables(tsk))
129 return -1;
130 if (dup_mmap(tsk->mm)) {
131 free_page_tables(tsk);
132 return -1;
133 }
134 return 0;
135 }
136
137 static int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
138 {
139 if (clone_flags & CLONE_FS) {
140 current->fs->count++;
141 return 0;
142 }
143 tsk->fs = kmalloc(sizeof(*tsk->fs), GFP_KERNEL);
144 if (!tsk->fs)
145 return -1;
146 tsk->fs->count = 1;
147 tsk->fs->umask = current->fs->umask;
148 if ((tsk->fs->root = current->fs->root))
149 tsk->fs->root->i_count++;
150 if ((tsk->fs->pwd = current->fs->pwd))
151 tsk->fs->pwd->i_count++;
152 return 0;
153 }
154
155 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
156 {
157 int i;
158
159 if (clone_flags & CLONE_FILES) {
160 current->files->count++;
161 return 0;
162 }
163 tsk->files = kmalloc(sizeof(*tsk->files), GFP_KERNEL);
164 if (!tsk->files)
165 return -1;
166 tsk->files->count = 1;
167 memcpy(&tsk->files->close_on_exec, ¤t->files->close_on_exec,
168 sizeof(tsk->files->close_on_exec));
169 for (i = 0; i < NR_OPEN; i++) {
170 struct file * f = current->files->fd[i];
171 if (f)
172 f->f_count++;
173 tsk->files->fd[i] = f;
174 }
175 return 0;
176 }
177
178 static int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
179 {
180 if (clone_flags & CLONE_SIGHAND) {
181 current->sig->count++;
182 return 0;
183 }
184 tsk->sig = kmalloc(sizeof(*tsk->sig), GFP_KERNEL);
185 if (!tsk->sig)
186 return -1;
187 tsk->sig->count = 1;
188 memcpy(tsk->sig->action, current->sig->action, sizeof(tsk->sig->action));
189 return 0;
190 }
191
192
193
194
195
196
197 int do_fork(unsigned long clone_flags, unsigned long usp, struct pt_regs *regs)
198 {
199 int nr;
200 int error = -ENOMEM;
201 unsigned long new_stack;
202 struct task_struct *p;
203
204 p = (struct task_struct *) kmalloc(sizeof(*p), GFP_KERNEL);
205 if (!p)
206 goto bad_fork;
207 new_stack = get_free_page(GFP_KERNEL);
208 if (!new_stack)
209 goto bad_fork_free_p;
210 error = -EAGAIN;
211 nr = find_empty_process();
212 if (nr < 0)
213 goto bad_fork_free_stack;
214
215 *p = *current;
216
217 if (p->exec_domain && p->exec_domain->use_count)
218 (*p->exec_domain->use_count)++;
219 if (p->binfmt && p->binfmt->use_count)
220 (*p->binfmt->use_count)++;
221
222 p->did_exec = 0;
223 p->swappable = 0;
224 p->kernel_stack_page = new_stack;
225 *(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
226 p->state = TASK_UNINTERRUPTIBLE;
227 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
228 p->pid = get_pid(clone_flags);
229 p->next_run = NULL;
230 p->prev_run = NULL;
231 p->p_pptr = p->p_opptr = current;
232 p->p_cptr = NULL;
233 p->signal = 0;
234 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
235 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
236 init_timer(&p->real_timer);
237 p->real_timer.data = (unsigned long) p;
238 p->leader = 0;
239 p->tty_old_pgrp = 0;
240 p->utime = p->stime = 0;
241 p->cutime = p->cstime = 0;
242 #ifdef __SMP__
243 p->processor = NO_PROC_ID;
244 p->lock_depth = 1;
245 #endif
246 p->start_time = jiffies;
247 task[nr] = p;
248 SET_LINKS(p);
249 nr_tasks++;
250
251 error = -ENOMEM;
252
253 if (copy_files(clone_flags, p))
254 goto bad_fork_cleanup;
255 if (copy_fs(clone_flags, p))
256 goto bad_fork_cleanup_files;
257 if (copy_sighand(clone_flags, p))
258 goto bad_fork_cleanup_fs;
259 if (copy_mm(clone_flags, p))
260 goto bad_fork_cleanup_sighand;
261 copy_thread(nr, clone_flags, usp, p, regs);
262 p->semundo = NULL;
263
264
265 p->swappable = 1;
266 p->exit_signal = clone_flags & CSIGNAL;
267 p->counter = current->counter >> 1;
268 wake_up_process(p);
269 return p->pid;
270
271 bad_fork_cleanup_sighand:
272 exit_sighand(p);
273 bad_fork_cleanup_fs:
274 exit_fs(p);
275 bad_fork_cleanup_files:
276 exit_files(p);
277 bad_fork_cleanup:
278 if (p->exec_domain && p->exec_domain->use_count)
279 (*p->exec_domain->use_count)--;
280 if (p->binfmt && p->binfmt->use_count)
281 (*p->binfmt->use_count)--;
282 task[nr] = NULL;
283 REMOVE_LINKS(p);
284 nr_tasks--;
285 bad_fork_free_stack:
286 free_page(new_stack);
287 bad_fork_free_p:
288 kfree(p);
289 bad_fork:
290 return error;
291 }