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