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