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/ptrace.h>
21 #include <linux/malloc.h>
22 #include <linux/ldt.h>
23
24 #include <asm/segment.h>
25 #include <asm/system.h>
26
27
28
29 #define MAX_TASKS_PER_USER (NR_TASKS/2)
30 #define MIN_TASKS_LEFT_FOR_ROOT 4
31
32 long last_pid=0;
33
34 static int find_empty_process(void)
35 {
36 int free_task;
37 int i, tasks_free;
38 int this_user_tasks;
39
40 repeat:
41 if ((++last_pid) & 0xffff8000)
42 last_pid=1;
43 this_user_tasks = 0;
44 tasks_free = 0;
45 free_task = -EAGAIN;
46 i = NR_TASKS;
47 while (--i > 0) {
48 if (!task[i]) {
49 free_task = i;
50 tasks_free++;
51 continue;
52 }
53 if (task[i]->uid == current->uid)
54 this_user_tasks++;
55 if (task[i]->pid == last_pid || task[i]->pgrp == last_pid ||
56 task[i]->session == last_pid)
57 goto repeat;
58 }
59 if (tasks_free <= MIN_TASKS_LEFT_FOR_ROOT ||
60 this_user_tasks > MAX_TASKS_PER_USER)
61 if (current->uid)
62 return -EAGAIN;
63 return free_task;
64 }
65
66 static struct file * copy_fd(struct file * old_file)
67 {
68 struct file * new_file = get_empty_filp();
69 int error;
70
71 if (new_file) {
72 memcpy(new_file,old_file,sizeof(struct file));
73 new_file->f_count = 1;
74 if (new_file->f_inode)
75 new_file->f_inode->i_count++;
76 if (new_file->f_op && new_file->f_op->open) {
77 error = new_file->f_op->open(new_file->f_inode,new_file);
78 if (error) {
79 iput(new_file->f_inode);
80 new_file->f_count = 0;
81 new_file = NULL;
82 }
83 }
84 }
85 return new_file;
86 }
87
88 static int dup_mmap(struct task_struct * tsk)
89 {
90 struct vm_area_struct * mpnt, **p, *tmp;
91
92 tsk->mm->mmap = NULL;
93 p = &tsk->mm->mmap;
94 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
95 tmp = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
96 if (!tmp) {
97 exit_mmap(tsk);
98 return -ENOMEM;
99 }
100 *tmp = *mpnt;
101 tmp->vm_task = tsk;
102 tmp->vm_next = NULL;
103 if (tmp->vm_inode) {
104 tmp->vm_inode->i_count++;
105
106 tmp->vm_next_share->vm_prev_share = tmp;
107 mpnt->vm_next_share = tmp;
108 tmp->vm_prev_share = mpnt;
109 }
110 if (tmp->vm_ops && tmp->vm_ops->open)
111 tmp->vm_ops->open(tmp);
112 *p = tmp;
113 p = &tmp->vm_next;
114 }
115 build_mmap_avl(tsk);
116 return 0;
117 }
118
119
120
121
122 static void copy_files(unsigned long clone_flags, struct task_struct * p)
123 {
124 int i;
125 struct file * f;
126
127 if (clone_flags & COPYFD) {
128 for (i=0; i<NR_OPEN;i++)
129 if ((f = p->files->fd[i]) != NULL)
130 p->files->fd[i] = copy_fd(f);
131 } else {
132 for (i=0; i<NR_OPEN;i++)
133 if ((f = p->files->fd[i]) != NULL)
134 f->f_count++;
135 }
136 }
137
138
139
140
141
142 static int copy_mm(unsigned long clone_flags, struct task_struct * p)
143 {
144 if (clone_flags & COPYVM) {
145 p->mm->swappable = 1;
146 p->mm->min_flt = p->mm->maj_flt = 0;
147 p->mm->cmin_flt = p->mm->cmaj_flt = 0;
148 if (copy_page_tables(p))
149 return 1;
150 return dup_mmap(p);
151 } else {
152 if (clone_page_tables(p))
153 return 1;
154 return dup_mmap(p);
155 }
156 }
157
158 static void copy_fs(unsigned long clone_flags, struct task_struct * p)
159 {
160 if (current->fs->pwd)
161 current->fs->pwd->i_count++;
162 if (current->fs->root)
163 current->fs->root->i_count++;
164 }
165
166
167
168
169
170
171 asmlinkage int sys_fork(struct pt_regs regs)
172 {
173 int nr;
174 struct task_struct *p;
175 unsigned long new_stack;
176 unsigned long clone_flags = COPYVM | SIGCHLD;
177
178 if(!(p = (struct task_struct*)__get_free_page(GFP_KERNEL)))
179 goto bad_fork;
180 new_stack = get_free_page(GFP_KERNEL);
181 if (!new_stack)
182 goto bad_fork_free;
183 nr = find_empty_process();
184 if (nr < 0)
185 goto bad_fork_free;
186
187 *p = *current;
188
189 if (p->exec_domain && p->exec_domain->use_count)
190 (*p->exec_domain->use_count)++;
191 if (p->binfmt && p->binfmt->use_count)
192 (*p->binfmt->use_count)++;
193
194 p->did_exec = 0;
195 p->kernel_stack_page = new_stack;
196 *(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
197 p->state = TASK_UNINTERRUPTIBLE;
198 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
199 p->pid = last_pid;
200 p->p_pptr = p->p_opptr = current;
201 p->p_cptr = NULL;
202 SET_LINKS(p);
203 p->signal = 0;
204 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
205 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
206 p->leader = 0;
207 p->tty_old_pgrp = 0;
208 p->utime = p->stime = 0;
209 p->cutime = p->cstime = 0;
210 p->start_time = jiffies;
211 task[nr] = p;
212
213
214 clone_flags = copy_thread(nr, COPYVM | SIGCHLD, p, ®s);
215 if (copy_mm(clone_flags, p))
216 goto bad_fork_cleanup;
217 p->semundo = NULL;
218 copy_files(clone_flags, p);
219 copy_fs(clone_flags, p);
220
221
222 p->exit_signal = clone_flags & CSIGNAL;
223 p->counter = current->counter >> 1;
224 p->state = TASK_RUNNING;
225 return p->pid;
226 bad_fork_cleanup:
227 task[nr] = NULL;
228 REMOVE_LINKS(p);
229 bad_fork_free:
230 free_page(new_stack);
231 free_page((long) p);
232 bad_fork:
233 return -EAGAIN;
234 }