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