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