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 return -ENOMEM;
98 *tmp = *mpnt;
99 tmp->vm_task = tsk;
100 tmp->vm_next = NULL;
101 if (tmp->vm_inode) {
102 tmp->vm_inode->i_count++;
103
104 tmp->vm_next_share->vm_prev_share = tmp;
105 mpnt->vm_next_share = tmp;
106 tmp->vm_prev_share = mpnt;
107 }
108 if (tmp->vm_ops && tmp->vm_ops->open)
109 tmp->vm_ops->open(tmp);
110 *p = tmp;
111 p = &tmp->vm_next;
112 }
113 return 0;
114 }
115
116
117
118
119 static void copy_files(unsigned long clone_flags, struct task_struct * p)
120 {
121 int i;
122 struct file * f;
123
124 if (clone_flags & COPYFD) {
125 for (i=0; i<NR_OPEN;i++)
126 if ((f = p->files->fd[i]) != NULL)
127 p->files->fd[i] = copy_fd(f);
128 } else {
129 for (i=0; i<NR_OPEN;i++)
130 if ((f = p->files->fd[i]) != NULL)
131 f->f_count++;
132 }
133 }
134
135
136
137
138
139 static int copy_mm(unsigned long clone_flags, struct task_struct * p)
140 {
141 if (clone_flags & COPYVM) {
142 p->mm->swappable = 1;
143 p->mm->min_flt = p->mm->maj_flt = 0;
144 p->mm->cmin_flt = p->mm->cmaj_flt = 0;
145 if (copy_page_tables(p))
146 return 1;
147 return dup_mmap(p);
148 } else {
149 if (clone_page_tables(p))
150 return 1;
151 return dup_mmap(p);
152 }
153 }
154
155 static void copy_fs(unsigned long clone_flags, struct task_struct * p)
156 {
157 if (current->fs->pwd)
158 current->fs->pwd->i_count++;
159 if (current->fs->root)
160 current->fs->root->i_count++;
161 }
162
163
164
165
166
167
168 asmlinkage int sys_fork(struct pt_regs regs)
169 {
170 int nr;
171 struct task_struct *p;
172 unsigned long new_stack;
173 unsigned long clone_flags = COPYVM | SIGCHLD;
174
175 if(!(p = (struct task_struct*)__get_free_page(GFP_KERNEL)))
176 goto bad_fork;
177 new_stack = get_free_page(GFP_KERNEL);
178 if (!new_stack)
179 goto bad_fork_free;
180 nr = find_empty_process();
181 if (nr < 0)
182 goto bad_fork_free;
183
184 *p = *current;
185
186 if (p->exec_domain && p->exec_domain->use_count)
187 (*p->exec_domain->use_count)++;
188 if (p->binfmt && p->binfmt->use_count)
189 (*p->binfmt->use_count)++;
190
191 p->did_exec = 0;
192 p->kernel_stack_page = new_stack;
193 *(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
194 p->state = TASK_UNINTERRUPTIBLE;
195 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
196 p->pid = last_pid;
197 p->p_pptr = p->p_opptr = current;
198 p->p_cptr = NULL;
199 SET_LINKS(p);
200 p->signal = 0;
201 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
202 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
203 p->leader = 0;
204 p->tty_old_pgrp = 0;
205 p->utime = p->stime = 0;
206 p->cutime = p->cstime = 0;
207 p->start_time = jiffies;
208 task[nr] = p;
209
210
211 clone_flags = copy_thread(nr, COPYVM | SIGCHLD, p, ®s);
212 if (copy_mm(clone_flags, p))
213 goto bad_fork_cleanup;
214 p->semundo = NULL;
215 copy_files(clone_flags, p);
216 copy_fs(clone_flags, p);
217
218
219 p->exit_signal = clone_flags & CSIGNAL;
220 p->counter = current->counter >> 1;
221 p->state = TASK_RUNNING;
222 return p->pid;
223 bad_fork_cleanup:
224 task[nr] = NULL;
225 REMOVE_LINKS(p);
226 bad_fork_free:
227 free_page(new_stack);
228 free_page((long) p);
229 bad_fork:
230 return -EAGAIN;
231 }