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