This source file includes following definitions.
- generate
- send_sig
- notify_parent
- release
- bad_task_ptr
- audit_ptree
- session_of_pgrp
- kill_pg
- kill_sl
- kill_proc
- sys_kill
- is_orphaned_pgrp
- has_stopped_jobs
- forget_original_parent
- exit_files
- exit_fs
- do_exit
- sys_exit
- sys_wait4
- sys_waitpid
1
2
3
4
5
6
7 #undef DEBUG_PROC_TREE
8
9 #include <linux/wait.h>
10 #include <linux/errno.h>
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/resource.h>
15 #include <linux/mm.h>
16 #include <linux/tty.h>
17 #include <linux/malloc.h>
18
19 #include <asm/segment.h>
20 extern void sem_exit (void);
21
22 int getrusage(struct task_struct *, int, struct rusage *);
23
24 static int generate(unsigned long sig, struct task_struct * p)
25 {
26 unsigned long mask = 1 << (sig-1);
27 struct sigaction * sa = sig + p->sigaction - 1;
28
29
30 if (!(p->flags & PF_PTRACED)) {
31
32 if (sa->sa_handler == SIG_IGN && sig != SIGCHLD)
33 return 0;
34
35 if ((sa->sa_handler == SIG_DFL) &&
36 (sig == SIGCONT || sig == SIGCHLD || sig == SIGWINCH))
37 return 0;
38 }
39 p->signal |= mask;
40 if (p->state == TASK_INTERRUPTIBLE && (p->signal & ~p->blocked))
41 wake_up_process(p);
42 return 1;
43 }
44
45 int send_sig(unsigned long sig,struct task_struct * p,int priv)
46 {
47 if (!p || sig > 32)
48 return -EINVAL;
49 if (!priv && ((sig != SIGCONT) || (current->session != p->session)) &&
50 (current->euid != p->euid) && (current->euid != p->uid) && !suser())
51 return -EPERM;
52 if (!sig)
53 return 0;
54
55
56
57 if (p->state == TASK_ZOMBIE)
58 return 0;
59 if ((sig == SIGKILL) || (sig == SIGCONT)) {
60 if (p->state == TASK_STOPPED)
61 wake_up_process(p);
62 p->exit_code = 0;
63 p->signal &= ~( (1<<(SIGSTOP-1)) | (1<<(SIGTSTP-1)) |
64 (1<<(SIGTTIN-1)) | (1<<(SIGTTOU-1)) );
65 }
66
67 if ((sig >= SIGSTOP) && (sig <= SIGTTOU))
68 p->signal &= ~(1<<(SIGCONT-1));
69
70 generate(sig,p);
71 return 0;
72 }
73
74 void notify_parent(struct task_struct * tsk)
75 {
76 if (tsk->p_pptr == task[1])
77 tsk->exit_signal = SIGCHLD;
78 send_sig(tsk->exit_signal, tsk->p_pptr, 1);
79 wake_up_interruptible(&tsk->p_pptr->wait_chldexit);
80 }
81
82 void release(struct task_struct * p)
83 {
84 int i;
85
86 if (!p)
87 return;
88 if (p == current) {
89 printk("task releasing itself\n");
90 return;
91 }
92 for (i=1 ; i<NR_TASKS ; i++)
93 if (task[i] == p) {
94 nr_tasks--;
95 task[i] = NULL;
96 REMOVE_LINKS(p);
97 if (STACK_MAGIC != *(unsigned long *)p->kernel_stack_page)
98 printk(KERN_ALERT "release: %s kernel stack corruption. Aiee\n", p->comm);
99 free_page(p->kernel_stack_page);
100 free_page((long) p);
101 return;
102 }
103 panic("trying to release non-existent task");
104 }
105
106 #ifdef DEBUG_PROC_TREE
107
108
109
110
111 int bad_task_ptr(struct task_struct *p)
112 {
113 int i;
114
115 if (!p)
116 return 0;
117 for (i=0 ; i<NR_TASKS ; i++)
118 if (task[i] == p)
119 return 0;
120 return 1;
121 }
122
123
124
125
126
127
128
129
130
131
132 void audit_ptree(void)
133 {
134 int i;
135
136 for (i=1 ; i<NR_TASKS ; i++) {
137 if (!task[i])
138 continue;
139 if (bad_task_ptr(task[i]->p_pptr))
140 printk("Warning, pid %d's parent link is bad\n",
141 task[i]->pid);
142 if (bad_task_ptr(task[i]->p_cptr))
143 printk("Warning, pid %d's child link is bad\n",
144 task[i]->pid);
145 if (bad_task_ptr(task[i]->p_ysptr))
146 printk("Warning, pid %d's ys link is bad\n",
147 task[i]->pid);
148 if (bad_task_ptr(task[i]->p_osptr))
149 printk("Warning, pid %d's os link is bad\n",
150 task[i]->pid);
151 if (task[i]->p_pptr == task[i])
152 printk("Warning, pid %d parent link points to self\n",
153 task[i]->pid);
154 if (task[i]->p_cptr == task[i])
155 printk("Warning, pid %d child link points to self\n",
156 task[i]->pid);
157 if (task[i]->p_ysptr == task[i])
158 printk("Warning, pid %d ys link points to self\n",
159 task[i]->pid);
160 if (task[i]->p_osptr == task[i])
161 printk("Warning, pid %d os link points to self\n",
162 task[i]->pid);
163 if (task[i]->p_osptr) {
164 if (task[i]->p_pptr != task[i]->p_osptr->p_pptr)
165 printk(
166 "Warning, pid %d older sibling %d parent is %d\n",
167 task[i]->pid, task[i]->p_osptr->pid,
168 task[i]->p_osptr->p_pptr->pid);
169 if (task[i]->p_osptr->p_ysptr != task[i])
170 printk(
171 "Warning, pid %d older sibling %d has mismatched ys link\n",
172 task[i]->pid, task[i]->p_osptr->pid);
173 }
174 if (task[i]->p_ysptr) {
175 if (task[i]->p_pptr != task[i]->p_ysptr->p_pptr)
176 printk(
177 "Warning, pid %d younger sibling %d parent is %d\n",
178 task[i]->pid, task[i]->p_osptr->pid,
179 task[i]->p_osptr->p_pptr->pid);
180 if (task[i]->p_ysptr->p_osptr != task[i])
181 printk(
182 "Warning, pid %d younger sibling %d has mismatched os link\n",
183 task[i]->pid, task[i]->p_ysptr->pid);
184 }
185 if (task[i]->p_cptr) {
186 if (task[i]->p_cptr->p_pptr != task[i])
187 printk(
188 "Warning, pid %d youngest child %d has mismatched parent link\n",
189 task[i]->pid, task[i]->p_cptr->pid);
190 if (task[i]->p_cptr->p_ysptr)
191 printk(
192 "Warning, pid %d youngest child %d has non-NULL ys link\n",
193 task[i]->pid, task[i]->p_cptr->pid);
194 }
195 }
196 }
197 #endif
198
199
200
201
202
203
204 int session_of_pgrp(int pgrp)
205 {
206 struct task_struct *p;
207 int fallback;
208
209 fallback = -1;
210 for_each_task(p) {
211 if (p->session <= 0)
212 continue;
213 if (p->pgrp == pgrp)
214 return p->session;
215 if (p->pid == pgrp)
216 fallback = p->session;
217 }
218 return fallback;
219 }
220
221
222
223
224
225 int kill_pg(int pgrp, int sig, int priv)
226 {
227 struct task_struct *p;
228 int err,retval = -ESRCH;
229 int found = 0;
230
231 if (sig<0 || sig>32 || pgrp<=0)
232 return -EINVAL;
233 for_each_task(p) {
234 if (p->pgrp == pgrp) {
235 if ((err = send_sig(sig,p,priv)) != 0)
236 retval = err;
237 else
238 found++;
239 }
240 }
241 return(found ? 0 : retval);
242 }
243
244
245
246
247
248
249 int kill_sl(int sess, int sig, int priv)
250 {
251 struct task_struct *p;
252 int err,retval = -ESRCH;
253 int found = 0;
254
255 if (sig<0 || sig>32 || sess<=0)
256 return -EINVAL;
257 for_each_task(p) {
258 if (p->session == sess && p->leader) {
259 if ((err = send_sig(sig,p,priv)) != 0)
260 retval = err;
261 else
262 found++;
263 }
264 }
265 return(found ? 0 : retval);
266 }
267
268 int kill_proc(int pid, int sig, int priv)
269 {
270 struct task_struct *p;
271
272 if (sig<0 || sig>32)
273 return -EINVAL;
274 for_each_task(p) {
275 if (p && p->pid == pid)
276 return send_sig(sig,p,priv);
277 }
278 return(-ESRCH);
279 }
280
281
282
283
284
285 asmlinkage int sys_kill(int pid,int sig)
286 {
287 int err, retval = 0, count = 0;
288
289 if (!pid)
290 return(kill_pg(current->pgrp,sig,0));
291 if (pid == -1) {
292 struct task_struct * p;
293 for_each_task(p) {
294 if (p->pid > 1 && p != current) {
295 ++count;
296 if ((err = send_sig(sig,p,0)) != -EPERM)
297 retval = err;
298 }
299 }
300 return(count ? retval : -ESRCH);
301 }
302 if (pid < 0)
303 return(kill_pg(-pid,sig,0));
304
305 return(kill_proc(pid,sig,0));
306 }
307
308
309
310
311
312
313
314
315
316 int is_orphaned_pgrp(int pgrp)
317 {
318 struct task_struct *p;
319
320 for_each_task(p) {
321 if ((p->pgrp != pgrp) ||
322 (p->state == TASK_ZOMBIE) ||
323 (p->p_pptr->pid == 1))
324 continue;
325 if ((p->p_pptr->pgrp != pgrp) &&
326 (p->p_pptr->session == p->session))
327 return 0;
328 }
329 return(1);
330 }
331
332 static int has_stopped_jobs(int pgrp)
333 {
334 struct task_struct * p;
335
336 for_each_task(p) {
337 if (p->pgrp != pgrp)
338 continue;
339 if (p->state == TASK_STOPPED)
340 return(1);
341 }
342 return(0);
343 }
344
345 static void forget_original_parent(struct task_struct * father)
346 {
347 struct task_struct * p;
348
349 for_each_task(p) {
350 if (p->p_opptr == father)
351 if (task[1])
352 p->p_opptr = task[1];
353 else
354 p->p_opptr = task[0];
355 }
356 }
357
358 static void exit_files(void)
359 {
360 int i;
361
362 for (i=0 ; i<NR_OPEN ; i++)
363 if (current->files->fd[i])
364 sys_close(i);
365 }
366
367 static void exit_fs(void)
368 {
369 iput(current->fs->pwd);
370 current->fs->pwd = NULL;
371 iput(current->fs->root);
372 current->fs->root = NULL;
373 }
374
375 NORET_TYPE void do_exit(long code)
376 {
377 struct task_struct *p;
378
379 if (intr_count) {
380 printk("Aiee, killing interrupt handler\n");
381 intr_count = 0;
382 }
383 fake_volatile:
384 current->flags |= PF_EXITING;
385 del_timer(¤t->real_timer);
386 sem_exit();
387 exit_mmap(current);
388 free_page_tables(current);
389 exit_files();
390 exit_fs();
391 exit_thread();
392 forget_original_parent(current);
393
394
395
396
397
398
399
400
401
402 if ((current->p_pptr->pgrp != current->pgrp) &&
403 (current->p_pptr->session == current->session) &&
404 is_orphaned_pgrp(current->pgrp) &&
405 has_stopped_jobs(current->pgrp)) {
406 kill_pg(current->pgrp,SIGHUP,1);
407 kill_pg(current->pgrp,SIGCONT,1);
408 }
409
410 notify_parent(current);
411
412
413
414
415
416
417
418
419
420 while ((p = current->p_cptr) != NULL) {
421 current->p_cptr = p->p_osptr;
422 p->p_ysptr = NULL;
423 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
424 if (task[1] && task[1] != current)
425 p->p_pptr = task[1];
426 else
427 p->p_pptr = task[0];
428 p->p_osptr = p->p_pptr->p_cptr;
429 p->p_osptr->p_ysptr = p;
430 p->p_pptr->p_cptr = p;
431 if (p->state == TASK_ZOMBIE)
432 notify_parent(p);
433
434
435
436
437
438
439 if ((p->pgrp != current->pgrp) &&
440 (p->session == current->session) &&
441 is_orphaned_pgrp(p->pgrp) &&
442 has_stopped_jobs(p->pgrp)) {
443 kill_pg(p->pgrp,SIGHUP,1);
444 kill_pg(p->pgrp,SIGCONT,1);
445 }
446 }
447 if (current->leader)
448 disassociate_ctty(1);
449 if (last_task_used_math == current)
450 last_task_used_math = NULL;
451 current->state = TASK_ZOMBIE;
452 current->exit_code = code;
453 current->mm->rss = 0;
454 #ifdef DEBUG_PROC_TREE
455 audit_ptree();
456 #endif
457 if (current->exec_domain && current->exec_domain->use_count)
458 (*current->exec_domain->use_count)--;
459 if (current->binfmt && current->binfmt->use_count)
460 (*current->binfmt->use_count)--;
461 schedule();
462
463
464
465
466
467
468
469
470
471
472
473
474
475 goto fake_volatile;
476 }
477
478 asmlinkage int sys_exit(int error_code)
479 {
480 do_exit((error_code&0xff)<<8);
481 }
482
483 asmlinkage int sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
484 {
485 int flag, retval;
486 struct wait_queue wait = { current, NULL };
487 struct task_struct *p;
488
489 if (stat_addr) {
490 flag = verify_area(VERIFY_WRITE, stat_addr, sizeof(*stat_addr));
491 if (flag)
492 return flag;
493 }
494 add_wait_queue(¤t->wait_chldexit,&wait);
495 repeat:
496 flag=0;
497 for (p = current->p_cptr ; p ; p = p->p_osptr) {
498 if (pid>0) {
499 if (p->pid != pid)
500 continue;
501 } else if (!pid) {
502 if (p->pgrp != current->pgrp)
503 continue;
504 } else if (pid != -1) {
505 if (p->pgrp != -pid)
506 continue;
507 }
508
509 if ((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
510 continue;
511 flag = 1;
512 switch (p->state) {
513 case TASK_STOPPED:
514 if (!p->exit_code)
515 continue;
516 if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED))
517 continue;
518 if (stat_addr)
519 put_user((p->exit_code << 8) | 0x7f,
520 stat_addr);
521 p->exit_code = 0;
522 if (ru != NULL)
523 getrusage(p, RUSAGE_BOTH, ru);
524 retval = p->pid;
525 goto end_wait4;
526 case TASK_ZOMBIE:
527 current->cutime += p->utime + p->cutime;
528 current->cstime += p->stime + p->cstime;
529 current->mm->cmin_flt += p->mm->min_flt + p->mm->cmin_flt;
530 current->mm->cmaj_flt += p->mm->maj_flt + p->mm->cmaj_flt;
531 if (ru != NULL)
532 getrusage(p, RUSAGE_BOTH, ru);
533 flag = p->pid;
534 if (stat_addr)
535 put_user(p->exit_code, stat_addr);
536 if (p->p_opptr != p->p_pptr) {
537 REMOVE_LINKS(p);
538 p->p_pptr = p->p_opptr;
539 SET_LINKS(p);
540 notify_parent(p);
541 } else
542 release(p);
543 #ifdef DEBUG_PROC_TREE
544 audit_ptree();
545 #endif
546 retval = flag;
547 goto end_wait4;
548 default:
549 continue;
550 }
551 }
552 if (flag) {
553 retval = 0;
554 if (options & WNOHANG)
555 goto end_wait4;
556 current->state=TASK_INTERRUPTIBLE;
557 schedule();
558 current->signal &= ~(1<<(SIGCHLD-1));
559 retval = -ERESTARTSYS;
560 if (current->signal & ~current->blocked)
561 goto end_wait4;
562 goto repeat;
563 }
564 retval = -ECHILD;
565 end_wait4:
566 remove_wait_queue(¤t->wait_chldexit,&wait);
567 return retval;
568 }
569
570
571
572
573
574 asmlinkage int sys_waitpid(pid_t pid,unsigned int * stat_addr, int options)
575 {
576 return sys_wait4(pid, stat_addr, options, NULL);
577 }