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