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_sighand
- exit_mm
- exit_notify
- do_exit
- sys_exit
- sys_wait4
- sys_waitpid
1
2
3
4
5
6
7 #undef DEBUG_PROC_TREE
8
9 #include <linux/config.h>
10 #include <linux/wait.h>
11 #include <linux/errno.h>
12 #include <linux/signal.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/resource.h>
16 #include <linux/mm.h>
17 #include <linux/tty.h>
18 #include <linux/malloc.h>
19
20 #include <asm/segment.h>
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->sig->action - 1;
29
30
31 if (!(p->flags & PF_PTRACED)) {
32
33 if (sa->sa_handler == SIG_IGN && sig != SIGCHLD)
34 return 0;
35
36 if ((sa->sa_handler == SIG_DFL) &&
37 (sig == SIGCONT || sig == SIGCHLD || sig == SIGWINCH))
38 return 0;
39 }
40 p->signal |= mask;
41 if (p->state == TASK_INTERRUPTIBLE && (p->signal & ~p->blocked))
42 wake_up_process(p);
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->euid != p->uid) && !suser())
52 return -EPERM;
53 if (!sig)
54 return 0;
55
56
57
58 if (!p->sig)
59 return 0;
60 if ((sig == SIGKILL) || (sig == SIGCONT)) {
61 if (p->state == TASK_STOPPED)
62 wake_up_process(p);
63 p->exit_code = 0;
64 p->signal &= ~( (1<<(SIGSTOP-1)) | (1<<(SIGTSTP-1)) |
65 (1<<(SIGTTIN-1)) | (1<<(SIGTTOU-1)) );
66 }
67 if (sig == SIGSTOP || sig == SIGTSTP || sig == SIGTTIN || 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[smp_num_cpus])
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 release_thread(p);
98 if (STACK_MAGIC != *(unsigned long *)p->kernel_stack_page)
99 printk(KERN_ALERT "release: %s kernel stack corruption. Aiee\n", p->comm);
100 free_page(p->kernel_stack_page);
101 current->cmin_flt += p->min_flt + p->cmin_flt;
102 current->cmaj_flt += p->maj_flt + p->cmaj_flt;
103 current->cnswap += p->nswap + p->cnswap;
104 kfree(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[smp_num_cpus])
356 p->p_opptr = task[smp_num_cpus];
357 else
358 p->p_opptr = task[0];
359 }
360 }
361
362 void exit_files(struct task_struct *tsk)
363 {
364 struct files_struct * files = tsk->files;
365
366 if (files) {
367 tsk->files = NULL;
368 if (!--files->count) {
369 int i;
370 for (i=0 ; i<NR_OPEN ; i++) {
371 struct file * filp = files->fd[i];
372 if (!filp)
373 continue;
374 files->fd[i] = NULL;
375 close_fp(filp);
376 }
377 kfree(files);
378 }
379 }
380 }
381
382 void exit_fs(struct task_struct *tsk)
383 {
384 struct fs_struct * fs = tsk->fs;
385
386 if (fs) {
387 tsk->fs = NULL;
388 if (!--fs->count) {
389 iput(fs->root);
390 iput(fs->pwd);
391 kfree(fs);
392 }
393 }
394 }
395
396 void exit_sighand(struct task_struct *tsk)
397 {
398 struct signal_struct * sig = tsk->sig;
399
400 if (sig) {
401 tsk->sig = NULL;
402 if (!--sig->count) {
403 kfree(sig);
404 }
405 }
406 }
407
408 static void exit_mm(void)
409 {
410 struct mm_struct * mm = current->mm;
411
412 current->swappable = 0;
413 if (mm) {
414 if (!--mm->count) {
415 exit_mmap(mm);
416 free_page_tables(current);
417 kfree(mm);
418 }
419 current->mm = NULL;
420 }
421 }
422
423
424
425
426
427 static void exit_notify(void)
428 {
429 struct task_struct * p;
430
431 forget_original_parent(current);
432
433
434
435
436
437
438
439
440
441 if ((current->p_pptr->pgrp != current->pgrp) &&
442 (current->p_pptr->session == current->session) &&
443 is_orphaned_pgrp(current->pgrp) &&
444 has_stopped_jobs(current->pgrp)) {
445 kill_pg(current->pgrp,SIGHUP,1);
446 kill_pg(current->pgrp,SIGCONT,1);
447 }
448
449 notify_parent(current);
450
451
452
453
454
455
456
457
458
459 while ((p = current->p_cptr) != NULL) {
460 current->p_cptr = p->p_osptr;
461 p->p_ysptr = NULL;
462 p->flags &= ~(PF_PTRACED|PF_TRACESYS);
463 if (task[smp_num_cpus] && task[smp_num_cpus] != current)
464 p->p_pptr = task[smp_num_cpus];
465 else
466 p->p_pptr = task[0];
467 p->p_osptr = p->p_pptr->p_cptr;
468 p->p_osptr->p_ysptr = p;
469 p->p_pptr->p_cptr = p;
470 if (p->state == TASK_ZOMBIE)
471 notify_parent(p);
472
473
474
475
476
477
478 if ((p->pgrp != current->pgrp) &&
479 (p->session == current->session) &&
480 is_orphaned_pgrp(p->pgrp) &&
481 has_stopped_jobs(p->pgrp)) {
482 kill_pg(p->pgrp,SIGHUP,1);
483 kill_pg(p->pgrp,SIGCONT,1);
484 }
485 }
486 if (current->leader)
487 disassociate_ctty(1);
488 }
489
490 NORET_TYPE void do_exit(long code)
491 {
492 if (intr_count) {
493 printk("Aiee, killing interrupt handler\n");
494 intr_count = 0;
495 }
496 fake_volatile:
497 current->flags |= PF_EXITING;
498 del_timer(¤t->real_timer);
499 sem_exit();
500 exit_mm();
501 exit_files(current);
502 exit_fs(current);
503 exit_sighand(current);
504 exit_thread();
505 exit_notify();
506 current->state = TASK_ZOMBIE;
507 current->exit_code = code;
508 #ifdef DEBUG_PROC_TREE
509 audit_ptree();
510 #endif
511 if (current->exec_domain && current->exec_domain->use_count)
512 (*current->exec_domain->use_count)--;
513 if (current->binfmt && current->binfmt->use_count)
514 (*current->binfmt->use_count)--;
515 schedule();
516
517
518
519
520
521
522
523
524
525
526
527
528
529 goto fake_volatile;
530 }
531
532 asmlinkage int sys_exit(int error_code)
533 {
534 do_exit((error_code&0xff)<<8);
535 }
536
537 asmlinkage int sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
538 {
539 int flag, retval;
540 struct wait_queue wait = { current, NULL };
541 struct task_struct *p;
542
543 if (stat_addr) {
544 flag = verify_area(VERIFY_WRITE, stat_addr, sizeof(*stat_addr));
545 if (flag)
546 return flag;
547 }
548 add_wait_queue(¤t->wait_chldexit,&wait);
549 repeat:
550 flag=0;
551 for (p = current->p_cptr ; p ; p = p->p_osptr) {
552 if (pid>0) {
553 if (p->pid != pid)
554 continue;
555 } else if (!pid) {
556 if (p->pgrp != current->pgrp)
557 continue;
558 } else if (pid != -1) {
559 if (p->pgrp != -pid)
560 continue;
561 }
562
563 if ((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
564 continue;
565 flag = 1;
566 switch (p->state) {
567 case TASK_STOPPED:
568 if (!p->exit_code)
569 continue;
570 if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED))
571 continue;
572 if (stat_addr)
573 put_user((p->exit_code << 8) | 0x7f,
574 stat_addr);
575 p->exit_code = 0;
576 if (ru != NULL)
577 getrusage(p, RUSAGE_BOTH, ru);
578 retval = p->pid;
579 goto end_wait4;
580 case TASK_ZOMBIE:
581 current->cutime += p->utime + p->cutime;
582 current->cstime += p->stime + p->cstime;
583 if (ru != NULL)
584 getrusage(p, RUSAGE_BOTH, ru);
585 flag = p->pid;
586 if (stat_addr)
587 put_user(p->exit_code, stat_addr);
588 if (p->p_opptr != p->p_pptr) {
589 REMOVE_LINKS(p);
590 p->p_pptr = p->p_opptr;
591 SET_LINKS(p);
592 notify_parent(p);
593 } else
594 release(p);
595 #ifdef DEBUG_PROC_TREE
596 audit_ptree();
597 #endif
598 retval = flag;
599 goto end_wait4;
600 default:
601 continue;
602 }
603 }
604 if (flag) {
605 retval = 0;
606 if (options & WNOHANG)
607 goto end_wait4;
608 current->state=TASK_INTERRUPTIBLE;
609 schedule();
610 current->signal &= ~(1<<(SIGCHLD-1));
611 retval = -ERESTARTSYS;
612 if (current->signal & ~current->blocked)
613 goto end_wait4;
614 goto repeat;
615 }
616 retval = -ECHILD;
617 end_wait4:
618 remove_wait_queue(¤t->wait_chldexit,&wait);
619 return retval;
620 }
621
622
623
624
625
626 asmlinkage int sys_waitpid(pid_t pid,unsigned int * stat_addr, int options)
627 {
628 return sys_wait4(pid, stat_addr, options, NULL);
629 }