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 i;
35 int this_user_tasks;
36 struct task_struct *p;
37
38 if (nr_tasks >= NR_TASKS - MIN_TASKS_LEFT_FOR_ROOT) {
39 if (current->uid)
40 return -EAGAIN;
41 }
42 repeat:
43 if ((++last_pid) & 0xffff8000)
44 last_pid=1;
45 this_user_tasks = 0;
46 for_each_task (p) {
47 if (p->uid == current->uid)
48 this_user_tasks++;
49 if (p->pid == last_pid ||
50 p->pgrp == last_pid ||
51 p->session == last_pid)
52 goto repeat;
53 }
54 if (this_user_tasks > current->rlim[RLIMIT_NPROC].rlim_cur)
55 if (current->uid)
56 return -EAGAIN;
57 for (i = 0 ; i < NR_TASKS ; i++) {
58 if (!task[i])
59 return i;
60 }
61 return -EAGAIN;
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->min_flt = tsk->maj_flt = 0;
112 tsk->cmin_flt = tsk->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->swappable = 0;
209 p->kernel_stack_page = new_stack;
210 *(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
211 p->state = TASK_UNINTERRUPTIBLE;
212 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
213 p->pid = last_pid;
214 p->next_run = NULL;
215 p->prev_run = NULL;
216 p->p_pptr = p->p_opptr = current;
217 p->p_cptr = NULL;
218 p->signal = 0;
219 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
220 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
221 init_timer(&p->real_timer);
222 p->real_timer.data = (unsigned long) p;
223 p->leader = 0;
224 p->tty_old_pgrp = 0;
225 p->utime = p->stime = 0;
226 p->cutime = p->cstime = 0;
227 p->start_time = jiffies;
228 task[nr] = p;
229 SET_LINKS(p);
230 nr_tasks++;
231
232 error = -ENOMEM;
233
234 if (copy_files(clone_flags, p))
235 goto bad_fork_cleanup;
236 if (copy_fs(clone_flags, p))
237 goto bad_fork_cleanup_files;
238 if (copy_sighand(clone_flags, p))
239 goto bad_fork_cleanup_fs;
240 if (copy_mm(clone_flags, p))
241 goto bad_fork_cleanup_sighand;
242 copy_thread(nr, clone_flags, usp, p, regs);
243 p->semundo = NULL;
244
245
246 p->swappable = 1;
247 p->exit_signal = clone_flags & CSIGNAL;
248 p->counter = current->counter >> 1;
249 wake_up_process(p);
250 return p->pid;
251
252 bad_fork_cleanup_sighand:
253 exit_sighand(p);
254 bad_fork_cleanup_fs:
255 exit_fs(p);
256 bad_fork_cleanup_files:
257 exit_files(p);
258 bad_fork_cleanup:
259 if (p->exec_domain && p->exec_domain->use_count)
260 (*p->exec_domain->use_count)--;
261 if (p->binfmt && p->binfmt->use_count)
262 (*p->binfmt->use_count)--;
263 task[nr] = NULL;
264 REMOVE_LINKS(p);
265 nr_tasks--;
266 bad_fork_free:
267 free_page(new_stack);
268 kfree(p);
269 bad_fork:
270 return error;
271 }