1
2
3
4
5
6
7#include <linux/config.h>
8#include <linux/mm.h>
9#include <linux/slab.h>
10#include <linux/interrupt.h>
11#include <linux/smp_lock.h>
12#include <linux/module.h>
13#include <linux/completion.h>
14#include <linux/personality.h>
15#include <linux/tty.h>
16#include <linux/namespace.h>
17#include <linux/key.h>
18#include <linux/security.h>
19#include <linux/cpu.h>
20#include <linux/acct.h>
21#include <linux/file.h>
22#include <linux/binfmts.h>
23#include <linux/ptrace.h>
24#include <linux/profile.h>
25#include <linux/mount.h>
26#include <linux/proc_fs.h>
27#include <linux/mempolicy.h>
28#include <linux/audit.h>
29#include <linux/task_io_accounting_ops.h>
30
31#include <asm/uaccess.h>
32#include <asm/unistd.h>
33#include <asm/pgtable.h>
34#include <asm/mmu_context.h>
35
36extern void sem_exit (void);
37extern struct task_struct *child_reaper;
38
39int getrusage(struct task_struct *, int, struct rusage __user *);
40
41static void __unhash_process(struct task_struct *p)
42{
43 nr_threads--;
44 detach_pid(p, PIDTYPE_PID);
45 detach_pid(p, PIDTYPE_TGID);
46 if (thread_group_leader(p)) {
47 detach_pid(p, PIDTYPE_PGID);
48 detach_pid(p, PIDTYPE_SID);
49 if (p->pid)
50 __get_cpu_var(process_counts)--;
51 }
52
53 REMOVE_LINKS(p);
54}
55
56void release_task(struct task_struct * p)
57{
58 int zap_leader;
59 task_t *leader;
60 struct dentry *proc_dentry;
61
62repeat:
63 atomic_dec(&p->user->processes);
64 spin_lock(&p->proc_lock);
65 proc_dentry = proc_pid_unhash(p);
66 write_lock_irq(&tasklist_lock);
67 if (unlikely(p->ptrace))
68 __ptrace_unlink(p);
69 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
70 __exit_signal(p);
71 __exit_sighand(p);
72 __unhash_process(p);
73
74
75
76
77
78
79 zap_leader = 0;
80 leader = p->group_leader;
81 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
82 BUG_ON(leader->exit_signal == -1);
83 do_notify_parent(leader, leader->exit_signal);
84
85
86
87
88
89
90
91
92 zap_leader = (leader->exit_signal == -1);
93 }
94
95 sched_exit(p);
96 write_unlock_irq(&tasklist_lock);
97 spin_unlock(&p->proc_lock);
98 proc_pid_flush(proc_dentry);
99 release_thread(p);
100 put_task_struct(p);
101
102 p = leader;
103 if (unlikely(zap_leader))
104 goto repeat;
105}
106
107
108
109void unhash_process(struct task_struct *p)
110{
111 struct dentry *proc_dentry;
112
113 spin_lock(&p->proc_lock);
114 proc_dentry = proc_pid_unhash(p);
115 write_lock_irq(&tasklist_lock);
116 __unhash_process(p);
117 write_unlock_irq(&tasklist_lock);
118 spin_unlock(&p->proc_lock);
119 proc_pid_flush(proc_dentry);
120}
121
122
123
124
125
126
127int session_of_pgrp(int pgrp)
128{
129 struct task_struct *p;
130 int sid = -1;
131
132 read_lock(&tasklist_lock);
133 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
134 if (p->signal->session > 0) {
135 sid = p->signal->session;
136 goto out;
137 }
138 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
139 p = find_task_by_pid(pgrp);
140 if (p)
141 sid = p->signal->session;
142out:
143 read_unlock(&tasklist_lock);
144
145 return sid;
146}
147
148
149
150
151
152
153
154
155
156static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
157{
158 struct task_struct *p;
159 int ret = 1;
160
161 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
162 if (p == ignored_task
163 || p->exit_state >= EXIT_ZOMBIE
164 || p->real_parent->pid == 1)
165 continue;
166 if (process_group(p->real_parent) != pgrp
167 && p->real_parent->signal->session == p->signal->session) {
168 ret = 0;
169 break;
170 }
171 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
172 return ret;
173}
174
175int is_orphaned_pgrp(int pgrp)
176{
177 int retval;
178
179 read_lock(&tasklist_lock);
180 retval = will_become_orphaned_pgrp(pgrp, NULL);
181 read_unlock(&tasklist_lock);
182
183 return retval;
184}
185
186static inline int has_stopped_jobs(int pgrp)
187{
188 int retval = 0;
189 struct task_struct *p;
190
191 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
192 if (p->state != TASK_STOPPED)
193 continue;
194
195
196
197
198 if (unlikely (p->ptrace)
199 && p->exit_code != SIGSTOP
200 && p->exit_code != SIGTSTP
201 && p->exit_code != SIGTTOU
202 && p->exit_code != SIGTTIN)
203 continue;
204
205 retval = 1;
206 break;
207 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
208 return retval;
209}
210
211
212
213
214
215
216
217
218
219
220
221
222
223void reparent_to_init(void)
224{
225 write_lock_irq(&tasklist_lock);
226
227 ptrace_unlink(current);
228
229 REMOVE_LINKS(current);
230 current->parent = child_reaper;
231 current->real_parent = child_reaper;
232 SET_LINKS(current);
233
234
235 current->exit_signal = SIGCHLD;
236
237 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
238 set_user_nice(current, 0);
239
240
241
242 security_task_reparent_to_init(current);
243 memcpy(current->rlim, init_task.rlim, sizeof(*(current->rlim)));
244 atomic_inc(&(INIT_USER->__count));
245 switch_uid(INIT_USER);
246
247 write_unlock_irq(&tasklist_lock);
248}
249
250void __set_special_pids(pid_t session, pid_t pgrp)
251{
252 struct task_struct *curr = current;
253
254 if (curr->signal->session != session) {
255 detach_pid(curr, PIDTYPE_SID);
256 curr->signal->session = session;
257 attach_pid(curr, PIDTYPE_SID, session);
258 }
259 if (process_group(curr) != pgrp) {
260 detach_pid(curr, PIDTYPE_PGID);
261 curr->signal->pgrp = pgrp;
262 attach_pid(curr, PIDTYPE_PGID, pgrp);
263 }
264}
265
266void set_special_pids(pid_t session, pid_t pgrp)
267{
268 write_lock_irq(&tasklist_lock);
269 __set_special_pids(session, pgrp);
270 write_unlock_irq(&tasklist_lock);
271}
272
273
274
275
276
277
278int allow_signal(int sig)
279{
280 if (sig < 1 || sig > _NSIG)
281 return -EINVAL;
282
283 spin_lock_irq(¤t->sighand->siglock);
284 sigdelset(¤t->blocked, sig);
285 if (!current->mm) {
286
287
288
289
290 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
291 }
292 recalc_sigpending();
293 spin_unlock_irq(¤t->sighand->siglock);
294 return 0;
295}
296
297EXPORT_SYMBOL(allow_signal);
298
299int disallow_signal(int sig)
300{
301 if (sig < 1 || sig > _NSIG)
302 return -EINVAL;
303
304 spin_lock_irq(¤t->sighand->siglock);
305 sigaddset(¤t->blocked, sig);
306 recalc_sigpending();
307 spin_unlock_irq(¤t->sighand->siglock);
308 return 0;
309}
310
311EXPORT_SYMBOL(disallow_signal);
312
313
314
315
316
317
318void daemonize(const char *name, ...)
319{
320 va_list args;
321 struct fs_struct *fs;
322 sigset_t blocked;
323
324 va_start(args, name);
325 vsnprintf(current->comm, sizeof(current->comm), name, args);
326 va_end(args);
327
328
329
330
331
332
333 exit_mm(current);
334
335 set_special_pids(1, 1);
336 down(&tty_sem);
337 current->signal->tty = NULL;
338 up(&tty_sem);
339
340
341 sigfillset(&blocked);
342 sigprocmask(SIG_BLOCK, &blocked, NULL);
343 flush_signals(current);
344
345
346
347 exit_fs(current);
348 fs = init_task.fs;
349 current->fs = fs;
350 atomic_inc(&fs->count);
351 exit_files(current);
352 current->files = init_task.files;
353 atomic_inc(¤t->files->count);
354
355 reparent_to_init();
356}
357
358EXPORT_SYMBOL(daemonize);
359
360static inline void close_files(struct files_struct * files)
361{
362 int i, j;
363
364 j = 0;
365 for (;;) {
366 unsigned long set;
367 i = j * __NFDBITS;
368 if (i >= files->max_fdset || i >= files->max_fds)
369 break;
370 set = files->open_fds->fds_bits[j++];
371 while (set) {
372 if (set & 1) {
373 struct file * file = xchg(&files->fd[i], NULL);
374 if (file) {
375 filp_close(file, files);
376 cond_resched();
377 }
378 }
379 i++;
380 set >>= 1;
381 }
382 }
383}
384
385struct files_struct *get_files_struct(struct task_struct *task)
386{
387 struct files_struct *files;
388
389 task_lock(task);
390 files = task->files;
391 if (files)
392 atomic_inc(&files->count);
393 task_unlock(task);
394
395 return files;
396}
397
398void fastcall put_files_struct(struct files_struct *files)
399{
400 if (atomic_dec_and_test(&files->count)) {
401 close_files(files);
402
403
404
405 if (files->fd != &files->fd_array[0])
406 free_fd_array(files->fd, files->max_fds);
407 if (files->max_fdset > __FD_SETSIZE) {
408 free_fdset(files->open_fds, files->max_fdset);
409 free_fdset(files->close_on_exec, files->max_fdset);
410 }
411 kmem_cache_free(files_cachep, files);
412 }
413}
414
415EXPORT_SYMBOL(put_files_struct);
416
417void reset_files_struct(struct task_struct *tsk, struct files_struct *files)
418{
419 struct files_struct *old;
420
421 old = tsk->files;
422 task_lock(tsk);
423 tsk->files = files;
424 task_unlock(tsk);
425 put_files_struct(old);
426}
427
428EXPORT_SYMBOL(reset_files_struct);
429
430static inline void __exit_files(struct task_struct *tsk)
431{
432 struct files_struct * files = tsk->files;
433
434 if (files) {
435 task_lock(tsk);
436 tsk->files = NULL;
437 task_unlock(tsk);
438 put_files_struct(files);
439 }
440}
441
442void exit_files(struct task_struct *tsk)
443{
444 __exit_files(tsk);
445}
446
447static inline void __put_fs_struct(struct fs_struct *fs)
448{
449
450 if (atomic_dec_and_test(&fs->count)) {
451 dput(fs->root);
452 mntput(fs->rootmnt);
453 dput(fs->pwd);
454 mntput(fs->pwdmnt);
455 if (fs->altroot) {
456 dput(fs->altroot);
457 mntput(fs->altrootmnt);
458 }
459 kmem_cache_free(fs_cachep, fs);
460 }
461}
462
463void put_fs_struct(struct fs_struct *fs)
464{
465 __put_fs_struct(fs);
466}
467
468static inline void __exit_fs(struct task_struct *tsk)
469{
470 struct fs_struct * fs = tsk->fs;
471
472 if (fs) {
473 task_lock(tsk);
474 tsk->fs = NULL;
475 task_unlock(tsk);
476 __put_fs_struct(fs);
477 }
478}
479
480void exit_fs(struct task_struct *tsk)
481{
482 __exit_fs(tsk);
483}
484
485EXPORT_SYMBOL_GPL(exit_fs);
486
487
488
489
490
491static inline void __exit_mm(struct task_struct * tsk)
492{
493 struct mm_struct *mm = tsk->mm;
494
495 mm_release(tsk, mm);
496 if (!mm)
497 return;
498
499
500
501
502
503
504
505 down_read(&mm->mmap_sem);
506 if (mm->core_waiters) {
507 up_read(&mm->mmap_sem);
508 down_write(&mm->mmap_sem);
509 if (!--mm->core_waiters)
510 complete(mm->core_startup_done);
511 up_write(&mm->mmap_sem);
512
513 wait_for_completion(&mm->core_done);
514 down_read(&mm->mmap_sem);
515 }
516 atomic_inc(&mm->mm_count);
517 if (mm != tsk->active_mm) BUG();
518
519 task_lock(tsk);
520 tsk->mm = NULL;
521 up_read(&mm->mmap_sem);
522 enter_lazy_tlb(mm, current);
523 task_unlock(tsk);
524 mmput(mm);
525}
526
527void exit_mm(struct task_struct *tsk)
528{
529 __exit_mm(tsk);
530}
531
532static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
533{
534
535
536
537
538 BUG_ON(p == reaper || reaper->state >= EXIT_ZOMBIE || reaper->exit_state >= EXIT_ZOMBIE);
539 p->real_parent = reaper;
540}
541
542static inline void reparent_thread(task_t *p, task_t *father, int traced)
543{
544 if (p->pdeath_signal)
545
546 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
547
548
549 if (unlikely(traced)) {
550
551 list_del_init(&p->ptrace_list);
552 if (p->parent != p->real_parent)
553 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
554 } else {
555
556
557
558 p->ptrace = 0;
559 list_del_init(&p->sibling);
560 p->parent = p->real_parent;
561 list_add_tail(&p->sibling, &p->parent->children);
562
563 if (p->state == TASK_TRACED) {
564
565
566
567
568
569 ptrace_untrace(p);
570 }
571 }
572
573
574
575
576 if (p->real_parent->group_leader == father->group_leader)
577 return;
578
579
580 if (p->exit_signal != -1)
581 p->exit_signal = SIGCHLD;
582
583
584
585
586 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
587 thread_group_empty(p))
588 do_notify_parent(p, p->exit_signal);
589
590
591
592
593
594
595
596 if ((process_group(p) != process_group(father)) &&
597 (p->signal->session == father->signal->session)) {
598 int pgrp = process_group(p);
599
600 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
601 __kill_pg_info(SIGHUP, (void *)1, pgrp);
602 __kill_pg_info(SIGCONT, (void *)1, pgrp);
603 }
604 }
605}
606
607
608
609
610
611
612
613static inline void forget_original_parent(struct task_struct * father,
614 struct list_head *to_release)
615{
616 struct task_struct *p, *reaper = father;
617 struct list_head *_p, *_n;
618
619 do {
620 reaper = next_thread(reaper);
621 if (reaper == father) {
622 reaper = child_reaper;
623 break;
624 }
625 } while (reaper->exit_state >= EXIT_ZOMBIE);
626
627
628
629
630
631
632
633
634
635 list_for_each_safe(_p, _n, &father->children) {
636 int ptrace;
637 p = list_entry(_p,struct task_struct,sibling);
638
639
640
641 if (p->first_time_slice == father->pid)
642 p->first_time_slice = 0;
643
644 ptrace = p->ptrace;
645
646
647 BUG_ON(father != p->real_parent && !ptrace);
648
649 if (father == p->real_parent) {
650
651 choose_new_parent(p, reaper, child_reaper);
652 reparent_thread(p, father, 0);
653 } else {
654
655 __ptrace_unlink (p);
656 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
657 thread_group_empty(p))
658 do_notify_parent(p, p->exit_signal);
659 }
660
661
662
663
664
665
666
667 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
668 list_add(&p->ptrace_list, to_release);
669 }
670 list_for_each_safe(_p, _n, &father->ptrace_children) {
671 p = list_entry(_p,struct task_struct,ptrace_list);
672
673 if (p->first_time_slice == father->pid)
674 p->first_time_slice = 0;
675
676 choose_new_parent(p, reaper, child_reaper);
677 reparent_thread(p, father, 1);
678 }
679}
680
681
682
683
684
685static void exit_notify(struct task_struct *tsk)
686{
687 int state;
688 struct task_struct *t;
689 struct list_head ptrace_dead, *_p, *_n;
690
691 if (signal_pending(tsk) && !tsk->signal->group_exit
692 && !thread_group_empty(tsk)) {
693
694
695
696
697
698
699
700
701
702 read_lock(&tasklist_lock);
703 spin_lock_irq(&tsk->sighand->siglock);
704 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
705 if (!signal_pending(t) && !(t->flags & PF_EXITING))
706 recalc_sigpending_and_wake(t);
707 spin_unlock_irq(&tsk->sighand->siglock);
708 read_unlock(&tasklist_lock);
709 }
710
711 write_lock_irq(&tasklist_lock);
712
713
714
715
716
717
718
719
720
721
722 INIT_LIST_HEAD(&ptrace_dead);
723 forget_original_parent(tsk, &ptrace_dead);
724 BUG_ON(!list_empty(&tsk->children));
725 BUG_ON(!list_empty(&tsk->ptrace_children));
726
727
728
729
730
731
732
733
734
735
736
737 t = tsk->real_parent;
738
739 if ((process_group(t) != process_group(tsk)) &&
740 (t->signal->session == tsk->signal->session) &&
741 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
742 has_stopped_jobs(process_group(tsk))) {
743 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
744 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
745 }
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
764 (tsk->parent_exec_id != t->self_exec_id ||
765 tsk->self_exec_id != tsk->parent_exec_id))
766 tsk->exit_signal = SIGCHLD;
767
768
769
770
771
772
773 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
774 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
775 do_notify_parent(tsk, signal);
776 } else if (tsk->ptrace) {
777 do_notify_parent(tsk, SIGCHLD);
778 }
779
780 state = EXIT_ZOMBIE;
781 if (tsk->exit_signal == -1 &&
782 (likely(tsk->ptrace == 0) ||
783 unlikely(tsk->parent->signal->group_exit)))
784 state = EXIT_DEAD;
785 tsk->exit_state = state;
786
787
788
789
790
791 tsk->it_virt_value = 0;
792 tsk->it_prof_value = 0;
793 tsk->rlim[RLIMIT_CPU].rlim_cur = RLIM_INFINITY;
794
795 write_unlock_irq(&tasklist_lock);
796
797 list_for_each_safe(_p, _n, &ptrace_dead) {
798 list_del_init(_p);
799 t = list_entry(_p,struct task_struct,ptrace_list);
800 release_task(t);
801 }
802
803
804 if (state == EXIT_DEAD)
805 release_task(tsk);
806
807
808 preempt_disable();
809 tsk->flags |= PF_DEAD;
810}
811
812asmlinkage NORET_TYPE void do_exit(long code)
813{
814 struct task_struct *tsk = current;
815 int group_dead;
816
817 profile_task_exit(tsk);
818
819 if (unlikely(in_interrupt()))
820 panic("Aiee, killing interrupt handler!");
821 if (unlikely(!tsk->pid))
822 panic("Attempted to kill the idle task!");
823 if (unlikely(tsk->pid == 1))
824 panic("Attempted to kill init!");
825 if (tsk->io_context)
826 exit_io_context();
827
828 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
829 current->ptrace_message = code;
830 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
831 }
832
833 tsk->flags |= PF_EXITING;
834 del_timer_sync(&tsk->real_timer);
835
836 if (unlikely(in_atomic()))
837 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
838 current->comm, current->pid,
839 preempt_count());
840
841 group_dead = atomic_dec_and_test(&tsk->signal->live);
842 if (group_dead)
843 exit_itimers(tsk->signal);
844 acct_process(code);
845
846 if (current->tux_info) {
847#ifdef CONFIG_TUX_DEBUG
848 printk("Possibly unexpected TUX-thread exit(%ld) at %p?\n",
849 code, __builtin_return_address(0));
850#endif
851 current->tux_exit();
852 }
853
854 if (unlikely(tsk->audit_context))
855 audit_free(tsk);
856 __exit_mm(tsk);
857
858 exit_sem(tsk);
859 __exit_files(tsk);
860 __exit_fs(tsk);
861 exit_namespace(tsk);
862 exit_thread();
863 exit_keys(tsk);
864
865 if (group_dead && tsk->signal->leader)
866 disassociate_ctty(1);
867
868 module_put(tsk->thread_info->exec_domain->module);
869 if (tsk->binfmt)
870 module_put(tsk->binfmt->module);
871
872 tsk->exit_code = code;
873 exit_notify(tsk);
874#ifdef CONFIG_NUMA
875 mpol_free(tsk->mempolicy);
876 tsk->mempolicy = NULL;
877#endif
878
879 BUG_ON(!(current->flags & PF_DEAD));
880 schedule();
881 BUG();
882
883 for (;;) ;
884}
885
886NORET_TYPE void complete_and_exit(struct completion *comp, long code)
887{
888 if (comp)
889 complete(comp);
890
891 do_exit(code);
892}
893
894EXPORT_SYMBOL(complete_and_exit);
895
896asmlinkage long sys_exit(int error_code)
897{
898 do_exit((error_code&0xff)<<8);
899}
900
901task_t fastcall *next_thread(const task_t *p)
902{
903#ifdef CONFIG_SMP
904 if (!p->sighand)
905 BUG();
906 if (!spin_is_locked(&p->sighand->siglock) &&
907 !rwlock_is_locked(&tasklist_lock))
908 BUG();
909#endif
910 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
911}
912
913EXPORT_SYMBOL(next_thread);
914
915
916
917
918
919NORET_TYPE void
920do_group_exit(int exit_code)
921{
922 BUG_ON(exit_code & 0x80);
923
924 if (current->signal->group_exit)
925 exit_code = current->signal->group_exit_code;
926 else if (!thread_group_empty(current)) {
927 struct signal_struct *const sig = current->signal;
928 struct sighand_struct *const sighand = current->sighand;
929 read_lock(&tasklist_lock);
930 spin_lock_irq(&sighand->siglock);
931 if (sig->group_exit)
932
933 exit_code = sig->group_exit_code;
934 else {
935 sig->group_exit = 1;
936 sig->group_exit_code = exit_code;
937 zap_other_threads(current);
938 }
939 spin_unlock_irq(&sighand->siglock);
940 read_unlock(&tasklist_lock);
941 }
942
943 do_exit(exit_code);
944
945}
946
947
948
949
950
951
952asmlinkage void sys_exit_group(int error_code)
953{
954 do_group_exit((error_code & 0xff) << 8);
955}
956
957static int eligible_child(pid_t pid, int options, task_t *p)
958{
959 if (pid > 0) {
960 if (p->pid != pid)
961 return 0;
962 } else if (!pid) {
963 if (process_group(p) != process_group(current))
964 return 0;
965 } else if (pid != -1) {
966 if (process_group(p) != -pid)
967 return 0;
968 }
969
970
971
972
973
974 if (p->exit_signal == -1 && !p->ptrace)
975 return 0;
976
977
978
979
980
981
982 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
983 && !(options & __WALL))
984 return 0;
985
986
987
988
989 if (delay_group_leader(p))
990 return 2;
991
992 if (security_task_wait(p))
993 return 0;
994
995 return 1;
996}
997
998static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
999 int why, int status,
1000 struct siginfo __user *infop,
1001 struct rusage __user *rusagep)
1002{
1003 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
1004 put_task_struct(p);
1005 if (!retval)
1006 retval = put_user(SIGCHLD, &infop->si_signo);
1007 if (!retval)
1008 retval = put_user(0, &infop->si_errno);
1009 if (!retval)
1010 retval = put_user((short)why, &infop->si_code);
1011 if (!retval)
1012 retval = put_user(pid, &infop->si_pid);
1013 if (!retval)
1014 retval = put_user(uid, &infop->si_uid);
1015 if (!retval)
1016 retval = put_user(status, &infop->si_status);
1017 if (!retval)
1018 retval = pid;
1019 return retval;
1020}
1021
1022
1023
1024
1025
1026
1027
1028static int wait_task_zombie(task_t *p, int noreap,
1029 struct siginfo __user *infop,
1030 int __user *stat_addr, struct rusage __user *ru)
1031{
1032 unsigned long state;
1033 int retval;
1034 int status;
1035
1036 if (unlikely(noreap)) {
1037 pid_t pid = p->pid;
1038 uid_t uid = p->uid;
1039 int exit_code = p->exit_code;
1040 int why, status;
1041
1042 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1043 return 0;
1044 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1045 return 0;
1046 get_task_struct(p);
1047 read_unlock(&tasklist_lock);
1048 if ((exit_code & 0x7f) == 0) {
1049 why = CLD_EXITED;
1050 status = exit_code >> 8;
1051 } else {
1052 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1053 status = exit_code & 0x7f;
1054 }
1055 return wait_noreap_copyout(p, pid, uid, why,
1056 status, infop, ru);
1057 }
1058
1059
1060
1061
1062
1063 state = xchg(&p->exit_state, EXIT_DEAD);
1064 if (state != EXIT_ZOMBIE) {
1065 BUG_ON(state != EXIT_DEAD);
1066 return 0;
1067 }
1068 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1069
1070
1071
1072
1073 return 0;
1074 }
1075
1076 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092 spin_lock_irq(&p->parent->sighand->siglock);
1093 p->parent->signal->cutime +=
1094 p->utime + p->signal->utime + p->signal->cutime;
1095 p->parent->signal->cstime +=
1096 p->stime + p->signal->stime + p->signal->cstime;
1097 p->parent->signal->cmin_flt +=
1098 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1099 p->parent->signal->cmaj_flt +=
1100 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1101 p->parent->signal->cnvcsw +=
1102 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1103 p->parent->signal->cnivcsw +=
1104 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1105 p->parent->signal->cinblock +=
1106 task_io_get_inblock(p) +
1107 p->signal->inblock + p->signal->cinblock;
1108 p->parent->signal->coublock +=
1109 task_io_get_oublock(p) +
1110 p->signal->oublock + p->signal->coublock;
1111 spin_unlock_irq(&p->parent->sighand->siglock);
1112 }
1113
1114
1115
1116
1117
1118 read_unlock(&tasklist_lock);
1119
1120 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1121 status = p->signal->group_exit
1122 ? p->signal->group_exit_code : p->exit_code;
1123 if (!retval && stat_addr)
1124 retval = put_user(status, stat_addr);
1125 if (!retval && infop)
1126 retval = put_user(SIGCHLD, &infop->si_signo);
1127 if (!retval && infop)
1128 retval = put_user(0, &infop->si_errno);
1129 if (!retval && infop) {
1130 int why;
1131
1132 if ((status & 0x7f) == 0) {
1133 why = CLD_EXITED;
1134 status >>= 8;
1135 } else {
1136 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1137 status &= 0x7f;
1138 }
1139 retval = put_user((short)why, &infop->si_code);
1140 if (!retval)
1141 retval = put_user(status, &infop->si_status);
1142 }
1143 if (!retval && infop)
1144 retval = put_user(p->pid, &infop->si_pid);
1145 if (!retval && infop)
1146 retval = put_user(p->uid, &infop->si_uid);
1147 if (retval) {
1148
1149 p->exit_state = EXIT_ZOMBIE;
1150 return retval;
1151 }
1152 retval = p->pid;
1153 if (p->real_parent != p->parent) {
1154 write_lock_irq(&tasklist_lock);
1155
1156 if (p->real_parent != p->parent) {
1157 __ptrace_unlink(p);
1158
1159 p->exit_state = EXIT_ZOMBIE;
1160
1161
1162
1163
1164
1165 if (p->exit_signal != -1) {
1166 do_notify_parent(p, p->exit_signal);
1167 if (p->exit_signal != -1)
1168 p = NULL;
1169 }
1170 }
1171 write_unlock_irq(&tasklist_lock);
1172 }
1173 if (p != NULL)
1174 release_task(p);
1175 BUG_ON(!retval);
1176 return retval;
1177}
1178
1179
1180
1181
1182
1183
1184
1185static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1186 struct siginfo __user *infop,
1187 int __user *stat_addr, struct rusage __user *ru)
1188{
1189 int retval, exit_code;
1190
1191 if (!p->exit_code)
1192 return 0;
1193 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1194 p->signal && p->signal->group_stop_count > 0)
1195
1196
1197
1198
1199 return 0;
1200
1201
1202
1203
1204
1205
1206
1207
1208 get_task_struct(p);
1209 read_unlock(&tasklist_lock);
1210
1211 if (unlikely(noreap)) {
1212 pid_t pid = p->pid;
1213 uid_t uid = p->uid;
1214 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1215
1216 exit_code = p->exit_code;
1217 if (unlikely(!exit_code) || unlikely(p->exit_state))
1218 goto bail_ref;
1219 return wait_noreap_copyout(p, pid, uid,
1220 why, (exit_code << 8) | 0x7f,
1221 infop, ru);
1222 }
1223
1224 write_lock_irq(&tasklist_lock);
1225
1226
1227
1228
1229
1230
1231 exit_code = xchg(&p->exit_code, 0);
1232 if (unlikely(p->exit_state >= EXIT_ZOMBIE)) {
1233
1234
1235
1236
1237
1238
1239
1240 p->exit_code = exit_code;
1241 exit_code = 0;
1242 }
1243 if (unlikely(exit_code == 0)) {
1244
1245
1246
1247
1248 write_unlock_irq(&tasklist_lock);
1249bail_ref:
1250 put_task_struct(p);
1251
1252
1253
1254
1255
1256
1257
1258
1259 return -EAGAIN;
1260 }
1261
1262
1263 remove_parent(p);
1264 add_parent(p, p->parent);
1265
1266 write_unlock_irq(&tasklist_lock);
1267
1268 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1269 if (!retval && stat_addr)
1270 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1271 if (!retval && infop)
1272 retval = put_user(SIGCHLD, &infop->si_signo);
1273 if (!retval && infop)
1274 retval = put_user(0, &infop->si_errno);
1275 if (!retval && infop)
1276 retval = put_user((short)((p->ptrace & PT_PTRACED)
1277 ? CLD_TRAPPED : CLD_STOPPED),
1278 &infop->si_code);
1279 if (!retval && infop)
1280 retval = put_user(exit_code, &infop->si_status);
1281 if (!retval && infop)
1282 retval = put_user(p->pid, &infop->si_pid);
1283 if (!retval && infop)
1284 retval = put_user(p->uid, &infop->si_uid);
1285 if (!retval)
1286 retval = p->pid;
1287 put_task_struct(p);
1288
1289 BUG_ON(!retval);
1290 return retval;
1291}
1292
1293
1294
1295
1296
1297
1298
1299static int wait_task_continued(task_t *p, int noreap,
1300 struct siginfo __user *infop,
1301 int __user *stat_addr, struct rusage __user *ru)
1302{
1303 int retval;
1304 pid_t pid;
1305 uid_t uid;
1306
1307 if (unlikely(!p->signal))
1308 return 0;
1309
1310 if (p->signal->stop_state >= 0)
1311 return 0;
1312
1313 spin_lock_irq(&p->sighand->siglock);
1314 if (p->signal->stop_state >= 0) {
1315 spin_unlock_irq(&p->sighand->siglock);
1316 return 0;
1317 }
1318 if (!noreap)
1319 p->signal->stop_state = 0;
1320 spin_unlock_irq(&p->sighand->siglock);
1321
1322 pid = p->pid;
1323 uid = p->uid;
1324 get_task_struct(p);
1325 read_unlock(&tasklist_lock);
1326
1327 if (!infop) {
1328 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1329 put_task_struct(p);
1330 if (!retval && stat_addr)
1331 retval = put_user(0xffff, stat_addr);
1332 if (!retval)
1333 retval = p->pid;
1334 } else {
1335 retval = wait_noreap_copyout(p, pid, uid,
1336 CLD_CONTINUED, SIGCONT,
1337 infop, ru);
1338 BUG_ON(retval == 0);
1339 }
1340
1341 return retval;
1342}
1343
1344
1345static inline int my_ptrace_child(struct task_struct *p)
1346{
1347 if (!(p->ptrace & PT_PTRACED))
1348 return 0;
1349 if (!(p->ptrace & PT_ATTACHED))
1350 return 1;
1351
1352
1353
1354
1355
1356
1357
1358 return (p->parent != p->real_parent);
1359}
1360
1361static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1362 int __user *stat_addr, struct rusage __user *ru)
1363{
1364 DECLARE_WAITQUEUE(wait, current);
1365 struct task_struct *tsk;
1366 int flag, retval;
1367
1368 add_wait_queue(¤t->wait_chldexit,&wait);
1369repeat:
1370
1371
1372
1373
1374 flag = 0;
1375 current->state = TASK_INTERRUPTIBLE;
1376 read_lock(&tasklist_lock);
1377 tsk = current;
1378 do {
1379 struct task_struct *p;
1380 struct list_head *_p;
1381 int ret;
1382
1383 list_for_each(_p,&tsk->children) {
1384 p = list_entry(_p,struct task_struct,sibling);
1385
1386 ret = eligible_child(pid, options, p);
1387 if (!ret)
1388 continue;
1389
1390 switch (p->state) {
1391 case TASK_TRACED:
1392
1393
1394
1395
1396
1397
1398
1399
1400 flag = 1;
1401 if (!my_ptrace_child(p))
1402 continue;
1403
1404 case TASK_STOPPED:
1405
1406
1407
1408
1409 flag = 1;
1410 if (!(options & WUNTRACED) &&
1411 !my_ptrace_child(p))
1412 continue;
1413 retval = wait_task_stopped(p, ret == 2,
1414 (options & WNOWAIT),
1415 infop,
1416 stat_addr, ru);
1417 if (retval == -EAGAIN)
1418 goto repeat;
1419 if (retval != 0)
1420 goto end;
1421 break;
1422 default:
1423
1424 if (p->exit_state == EXIT_DEAD)
1425 continue;
1426
1427 if (p->exit_state == EXIT_ZOMBIE) {
1428
1429
1430
1431
1432 if (ret == 2)
1433 goto check_continued;
1434 if (!likely(options & WEXITED))
1435 continue;
1436 retval = wait_task_zombie(
1437 p, (options & WNOWAIT),
1438 infop, stat_addr, ru);
1439
1440 if (retval != 0)
1441 goto end;
1442 break;
1443 }
1444check_continued:
1445
1446
1447
1448
1449 flag = 1;
1450 if (!unlikely(options & WCONTINUED))
1451 continue;
1452 retval = wait_task_continued(
1453 p, (options & WNOWAIT),
1454 infop, stat_addr, ru);
1455 if (retval != 0)
1456 goto end;
1457 break;
1458 }
1459 }
1460 if (!flag) {
1461 list_for_each(_p, &tsk->ptrace_children) {
1462 p = list_entry(_p, struct task_struct,
1463 ptrace_list);
1464 if (!eligible_child(pid, options, p))
1465 continue;
1466 flag = 1;
1467 break;
1468 }
1469 }
1470 if (options & __WNOTHREAD)
1471 break;
1472 tsk = next_thread(tsk);
1473 if (tsk->signal != current->signal)
1474 BUG();
1475 } while (tsk != current);
1476
1477 read_unlock(&tasklist_lock);
1478 if (flag) {
1479 retval = 0;
1480 if (options & WNOHANG)
1481 goto end;
1482 retval = -ERESTARTSYS;
1483 if (signal_pending(current))
1484 goto end;
1485 schedule();
1486 goto repeat;
1487 }
1488 retval = -ECHILD;
1489end:
1490 current->state = TASK_RUNNING;
1491 remove_wait_queue(¤t->wait_chldexit,&wait);
1492 if (infop) {
1493 if (retval > 0)
1494 retval = 0;
1495 else {
1496
1497
1498
1499
1500
1501 if (!retval)
1502 retval = put_user(0, &infop->si_signo);
1503 if (!retval)
1504 retval = put_user(0, &infop->si_errno);
1505 if (!retval)
1506 retval = put_user(0, &infop->si_code);
1507 if (!retval)
1508 retval = put_user(0, &infop->si_pid);
1509 if (!retval)
1510 retval = put_user(0, &infop->si_uid);
1511 if (!retval)
1512 retval = put_user(0, &infop->si_status);
1513 }
1514 }
1515 return retval;
1516}
1517
1518asmlinkage long sys_waitid(int which, pid_t pid,
1519 struct siginfo __user *infop, int options,
1520 struct rusage __user *ru)
1521{
1522 long ret;
1523
1524 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1525 return -EINVAL;
1526 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1527 return -EINVAL;
1528
1529 switch (which) {
1530 case P_ALL:
1531 pid = -1;
1532 break;
1533 case P_PID:
1534 if (pid <= 0)
1535 return -EINVAL;
1536 break;
1537 case P_PGID:
1538 if (pid <= 0)
1539 return -EINVAL;
1540 pid = -pid;
1541 break;
1542 default:
1543 return -EINVAL;
1544 }
1545
1546 ret = do_wait(pid, options, infop, NULL, ru);
1547
1548
1549 prevent_tail_call(ret);
1550 return ret;
1551}
1552
1553asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1554 int options, struct rusage __user *ru)
1555{
1556 long ret;
1557
1558 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1559 __WNOTHREAD|__WCLONE|__WALL))
1560 return -EINVAL;
1561 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1562
1563
1564 prevent_tail_call(ret);
1565 return ret;
1566}
1567
1568#ifdef __ARCH_WANT_SYS_WAITPID
1569
1570
1571
1572
1573
1574asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1575{
1576 return sys_wait4(pid, stat_addr, options, NULL);
1577}
1578
1579#endif
1580