1 /*
2 * linux/kernel/exit.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
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/iocontext.h>
17 #include <linux/key.h>
18 #include <linux/security.h>
19 #include <linux/cpu.h>
20 #include <linux/acct.h>
21 #include <linux/tsacct_kern.h>
22 #include <linux/file.h>
23 #include <linux/fdtable.h>
24 #include <linux/binfmts.h>
25 #include <linux/nsproxy.h>
26 #include <linux/pid_namespace.h>
27 #include <linux/ptrace.h>
28 #include <linux/profile.h>
29 #include <linux/mount.h>
30 #include <linux/proc_fs.h>
31 #include <linux/kthread.h>
32 #include <linux/mempolicy.h>
33 #include <linux/taskstats_kern.h>
34 #include <linux/delayacct.h>
35 #include <linux/freezer.h>
36 #include <linux/cgroup.h>
37 #include <linux/syscalls.h>
38 #include <linux/signal.h>
39 #include <linux/posix-timers.h>
40 #include <linux/cn_proc.h>
41 #include <linux/mutex.h>
42 #include <linux/futex.h>
43 #include <linux/pipe_fs_i.h>
44 #include <linux/audit.h> /* for audit_free() */
45 #include <linux/resource.h>
46 #include <linux/blkdev.h>
47 #include <linux/task_io_accounting_ops.h>
48 #include <linux/tracehook.h>
49 #include <linux/init_task.h>
50 #include <trace/sched.h>
51
52 #include <asm/uaccess.h>
53 #include <asm/unistd.h>
54 #include <asm/pgtable.h>
55 #include <asm/mmu_context.h>
56 #include "cred-internals.h"
57
58 DEFINE_TRACE(sched_process_free);
59 DEFINE_TRACE(sched_process_exit);
60 DEFINE_TRACE(sched_process_wait);
61
62 #ifdef CONFIG_QEMU_TRACE
63 void qemu_trace_thread_name(char *name);
64 void qemu_trace_exit(int code);
65 #endif
66
67 static void exit_mm(struct task_struct * tsk);
68
task_detached(struct task_struct * p)69 static inline int task_detached(struct task_struct *p)
70 {
71 return p->exit_signal == -1;
72 }
73
__unhash_process(struct task_struct * p)74 static void __unhash_process(struct task_struct *p)
75 {
76 nr_threads--;
77 detach_pid(p, PIDTYPE_PID);
78 if (thread_group_leader(p)) {
79 detach_pid(p, PIDTYPE_PGID);
80 detach_pid(p, PIDTYPE_SID);
81
82 list_del_rcu(&p->tasks);
83 __get_cpu_var(process_counts)--;
84 }
85 list_del_rcu(&p->thread_group);
86 list_del_init(&p->sibling);
87 }
88
89 /*
90 * This function expects the tasklist_lock write-locked.
91 */
__exit_signal(struct task_struct * tsk)92 static void __exit_signal(struct task_struct *tsk)
93 {
94 struct signal_struct *sig = tsk->signal;
95 struct sighand_struct *sighand;
96
97 BUG_ON(!sig);
98 BUG_ON(!atomic_read(&sig->count));
99
100 sighand = rcu_dereference(tsk->sighand);
101 spin_lock(&sighand->siglock);
102
103 posix_cpu_timers_exit(tsk);
104 if (atomic_dec_and_test(&sig->count))
105 posix_cpu_timers_exit_group(tsk);
106 else {
107 /*
108 * If there is any task waiting for the group exit
109 * then notify it:
110 */
111 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count)
112 wake_up_process(sig->group_exit_task);
113
114 if (tsk == sig->curr_target)
115 sig->curr_target = next_thread(tsk);
116 /*
117 * Accumulate here the counters for all threads but the
118 * group leader as they die, so they can be added into
119 * the process-wide totals when those are taken.
120 * The group leader stays around as a zombie as long
121 * as there are other threads. When it gets reaped,
122 * the exit.c code will add its counts into these totals.
123 * We won't ever get here for the group leader, since it
124 * will have been the last reference on the signal_struct.
125 */
126 sig->utime = cputime_add(sig->utime, task_utime(tsk));
127 sig->stime = cputime_add(sig->stime, task_stime(tsk));
128 sig->gtime = cputime_add(sig->gtime, task_gtime(tsk));
129 sig->min_flt += tsk->min_flt;
130 sig->maj_flt += tsk->maj_flt;
131 sig->nvcsw += tsk->nvcsw;
132 sig->nivcsw += tsk->nivcsw;
133 sig->inblock += task_io_get_inblock(tsk);
134 sig->oublock += task_io_get_oublock(tsk);
135 task_io_accounting_add(&sig->ioac, &tsk->ioac);
136 sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
137 sig = NULL; /* Marker for below. */
138 }
139
140 __unhash_process(tsk);
141
142 /*
143 * Do this under ->siglock, we can race with another thread
144 * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
145 */
146 flush_sigqueue(&tsk->pending);
147
148 tsk->signal = NULL;
149 tsk->sighand = NULL;
150 spin_unlock(&sighand->siglock);
151
152 __cleanup_sighand(sighand);
153 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
154 if (sig) {
155 flush_sigqueue(&sig->shared_pending);
156 taskstats_tgid_free(sig);
157 /*
158 * Make sure ->signal can't go away under rq->lock,
159 * see account_group_exec_runtime().
160 */
161 task_rq_unlock_wait(tsk);
162 __cleanup_signal(sig);
163 }
164 }
165
delayed_put_task_struct(struct rcu_head * rhp)166 static void delayed_put_task_struct(struct rcu_head *rhp)
167 {
168 struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
169
170 trace_sched_process_free(tsk);
171 put_task_struct(tsk);
172 }
173
174
release_task(struct task_struct * p)175 void release_task(struct task_struct * p)
176 {
177 struct task_struct *leader;
178 int zap_leader;
179 repeat:
180 tracehook_prepare_release_task(p);
181 /* don't need to get the RCU readlock here - the process is dead and
182 * can't be modifying its own credentials */
183 atomic_dec(&__task_cred(p)->user->processes);
184
185 proc_flush_task(p);
186 write_lock_irq(&tasklist_lock);
187 tracehook_finish_release_task(p);
188 __exit_signal(p);
189
190 /*
191 * If we are the last non-leader member of the thread
192 * group, and the leader is zombie, then notify the
193 * group leader's parent process. (if it wants notification.)
194 */
195 zap_leader = 0;
196 leader = p->group_leader;
197 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
198 BUG_ON(task_detached(leader));
199 do_notify_parent(leader, leader->exit_signal);
200 /*
201 * If we were the last child thread and the leader has
202 * exited already, and the leader's parent ignores SIGCHLD,
203 * then we are the one who should release the leader.
204 *
205 * do_notify_parent() will have marked it self-reaping in
206 * that case.
207 */
208 zap_leader = task_detached(leader);
209
210 /*
211 * This maintains the invariant that release_task()
212 * only runs on a task in EXIT_DEAD, just for sanity.
213 */
214 if (zap_leader)
215 leader->exit_state = EXIT_DEAD;
216 }
217
218 write_unlock_irq(&tasklist_lock);
219 release_thread(p);
220 call_rcu(&p->rcu, delayed_put_task_struct);
221
222 p = leader;
223 if (unlikely(zap_leader))
224 goto repeat;
225 }
226
227 /*
228 * This checks not only the pgrp, but falls back on the pid if no
229 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
230 * without this...
231 *
232 * The caller must hold rcu lock or the tasklist lock.
233 */
session_of_pgrp(struct pid * pgrp)234 struct pid *session_of_pgrp(struct pid *pgrp)
235 {
236 struct task_struct *p;
237 struct pid *sid = NULL;
238
239 p = pid_task(pgrp, PIDTYPE_PGID);
240 if (p == NULL)
241 p = pid_task(pgrp, PIDTYPE_PID);
242 if (p != NULL)
243 sid = task_session(p);
244
245 return sid;
246 }
247
248 /*
249 * Determine if a process group is "orphaned", according to the POSIX
250 * definition in 2.2.2.52. Orphaned process groups are not to be affected
251 * by terminal-generated stop signals. Newly orphaned process groups are
252 * to receive a SIGHUP and a SIGCONT.
253 *
254 * "I ask you, have you ever known what it is to be an orphan?"
255 */
will_become_orphaned_pgrp(struct pid * pgrp,struct task_struct * ignored_task)256 static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task)
257 {
258 struct task_struct *p;
259
260 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
261 if ((p == ignored_task) ||
262 (p->exit_state && thread_group_empty(p)) ||
263 is_global_init(p->real_parent))
264 continue;
265
266 if (task_pgrp(p->real_parent) != pgrp &&
267 task_session(p->real_parent) == task_session(p))
268 return 0;
269 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
270
271 return 1;
272 }
273
is_current_pgrp_orphaned(void)274 int is_current_pgrp_orphaned(void)
275 {
276 int retval;
277
278 read_lock(&tasklist_lock);
279 retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
280 read_unlock(&tasklist_lock);
281
282 return retval;
283 }
284
has_stopped_jobs(struct pid * pgrp)285 static int has_stopped_jobs(struct pid *pgrp)
286 {
287 int retval = 0;
288 struct task_struct *p;
289
290 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
291 if (!task_is_stopped(p))
292 continue;
293 retval = 1;
294 break;
295 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
296 return retval;
297 }
298
299 /*
300 * Check to see if any process groups have become orphaned as
301 * a result of our exiting, and if they have any stopped jobs,
302 * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
303 */
304 static void
kill_orphaned_pgrp(struct task_struct * tsk,struct task_struct * parent)305 kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
306 {
307 struct pid *pgrp = task_pgrp(tsk);
308 struct task_struct *ignored_task = tsk;
309
310 if (!parent)
311 /* exit: our father is in a different pgrp than
312 * we are and we were the only connection outside.
313 */
314 parent = tsk->real_parent;
315 else
316 /* reparent: our child is in a different pgrp than
317 * we are, and it was the only connection outside.
318 */
319 ignored_task = NULL;
320
321 if (task_pgrp(parent) != pgrp &&
322 task_session(parent) == task_session(tsk) &&
323 will_become_orphaned_pgrp(pgrp, ignored_task) &&
324 has_stopped_jobs(pgrp)) {
325 __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
326 __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
327 }
328 }
329
330 /**
331 * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd
332 *
333 * If a kernel thread is launched as a result of a system call, or if
334 * it ever exits, it should generally reparent itself to kthreadd so it
335 * isn't in the way of other processes and is correctly cleaned up on exit.
336 *
337 * The various task state such as scheduling policy and priority may have
338 * been inherited from a user process, so we reset them to sane values here.
339 *
340 * NOTE that reparent_to_kthreadd() gives the caller full capabilities.
341 */
reparent_to_kthreadd(void)342 static void reparent_to_kthreadd(void)
343 {
344 write_lock_irq(&tasklist_lock);
345
346 ptrace_unlink(current);
347 /* Reparent to init */
348 current->real_parent = current->parent = kthreadd_task;
349 list_move_tail(¤t->sibling, ¤t->real_parent->children);
350
351 /* Set the exit signal to SIGCHLD so we signal init on exit */
352 current->exit_signal = SIGCHLD;
353
354 if (task_nice(current) < 0)
355 set_user_nice(current, 0);
356 /* cpus_allowed? */
357 /* rt_priority? */
358 /* signals? */
359 memcpy(current->signal->rlim, init_task.signal->rlim,
360 sizeof(current->signal->rlim));
361
362 atomic_inc(&init_cred.usage);
363 commit_creds(&init_cred);
364 write_unlock_irq(&tasklist_lock);
365 }
366
__set_special_pids(struct pid * pid)367 void __set_special_pids(struct pid *pid)
368 {
369 struct task_struct *curr = current->group_leader;
370 pid_t nr = pid_nr(pid);
371
372 if (task_session(curr) != pid) {
373 change_pid(curr, PIDTYPE_SID, pid);
374 set_task_session(curr, nr);
375 }
376 if (task_pgrp(curr) != pid) {
377 change_pid(curr, PIDTYPE_PGID, pid);
378 set_task_pgrp(curr, nr);
379 }
380 }
381
set_special_pids(struct pid * pid)382 static void set_special_pids(struct pid *pid)
383 {
384 write_lock_irq(&tasklist_lock);
385 __set_special_pids(pid);
386 write_unlock_irq(&tasklist_lock);
387 }
388
389 /*
390 * Let kernel threads use this to say that they
391 * allow a certain signal (since daemonize() will
392 * have disabled all of them by default).
393 */
allow_signal(int sig)394 int allow_signal(int sig)
395 {
396 if (!valid_signal(sig) || sig < 1)
397 return -EINVAL;
398
399 spin_lock_irq(¤t->sighand->siglock);
400 sigdelset(¤t->blocked, sig);
401 if (!current->mm) {
402 /* Kernel threads handle their own signals.
403 Let the signal code know it'll be handled, so
404 that they don't get converted to SIGKILL or
405 just silently dropped */
406 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
407 }
408 recalc_sigpending();
409 spin_unlock_irq(¤t->sighand->siglock);
410 return 0;
411 }
412
413 EXPORT_SYMBOL(allow_signal);
414
disallow_signal(int sig)415 int disallow_signal(int sig)
416 {
417 if (!valid_signal(sig) || sig < 1)
418 return -EINVAL;
419
420 spin_lock_irq(¤t->sighand->siglock);
421 current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN;
422 recalc_sigpending();
423 spin_unlock_irq(¤t->sighand->siglock);
424 return 0;
425 }
426
427 EXPORT_SYMBOL(disallow_signal);
428
429 /*
430 * Put all the gunge required to become a kernel thread without
431 * attached user resources in one place where it belongs.
432 */
433
daemonize(const char * name,...)434 void daemonize(const char *name, ...)
435 {
436 va_list args;
437 struct fs_struct *fs;
438 sigset_t blocked;
439
440 va_start(args, name);
441 vsnprintf(current->comm, sizeof(current->comm), name, args);
442 va_end(args);
443 #ifdef CONFIG_QEMU_TRACE
444 qemu_trace_thread_name(current->comm);
445 #endif
446
447 /*
448 * If we were started as result of loading a module, close all of the
449 * user space pages. We don't need them, and if we didn't close them
450 * they would be locked into memory.
451 */
452 exit_mm(current);
453 /*
454 * We don't want to have TIF_FREEZE set if the system-wide hibernation
455 * or suspend transition begins right now.
456 */
457 current->flags |= (PF_NOFREEZE | PF_KTHREAD);
458
459 if (current->nsproxy != &init_nsproxy) {
460 get_nsproxy(&init_nsproxy);
461 switch_task_namespaces(current, &init_nsproxy);
462 }
463 set_special_pids(&init_struct_pid);
464 proc_clear_tty(current);
465
466 /* Block and flush all signals */
467 sigfillset(&blocked);
468 sigprocmask(SIG_BLOCK, &blocked, NULL);
469 flush_signals(current);
470
471 /* Become as one with the init task */
472
473 exit_fs(current); /* current->fs->count--; */
474 fs = init_task.fs;
475 current->fs = fs;
476 atomic_inc(&fs->count);
477
478 exit_files(current);
479 current->files = init_task.files;
480 atomic_inc(¤t->files->count);
481
482 reparent_to_kthreadd();
483 }
484
485 EXPORT_SYMBOL(daemonize);
486
close_files(struct files_struct * files)487 static void close_files(struct files_struct * files)
488 {
489 int i, j;
490 struct fdtable *fdt;
491
492 j = 0;
493
494 /*
495 * It is safe to dereference the fd table without RCU or
496 * ->file_lock because this is the last reference to the
497 * files structure.
498 */
499 fdt = files_fdtable(files);
500 for (;;) {
501 unsigned long set;
502 i = j * __NFDBITS;
503 if (i >= fdt->max_fds)
504 break;
505 set = fdt->open_fds->fds_bits[j++];
506 while (set) {
507 if (set & 1) {
508 struct file * file = xchg(&fdt->fd[i], NULL);
509 if (file) {
510 filp_close(file, files);
511 cond_resched();
512 }
513 }
514 i++;
515 set >>= 1;
516 }
517 }
518 }
519
get_files_struct(struct task_struct * task)520 struct files_struct *get_files_struct(struct task_struct *task)
521 {
522 struct files_struct *files;
523
524 task_lock(task);
525 files = task->files;
526 if (files)
527 atomic_inc(&files->count);
528 task_unlock(task);
529
530 return files;
531 }
532
put_files_struct(struct files_struct * files)533 void put_files_struct(struct files_struct *files)
534 {
535 struct fdtable *fdt;
536
537 if (atomic_dec_and_test(&files->count)) {
538 close_files(files);
539 /*
540 * Free the fd and fdset arrays if we expanded them.
541 * If the fdtable was embedded, pass files for freeing
542 * at the end of the RCU grace period. Otherwise,
543 * you can free files immediately.
544 */
545 fdt = files_fdtable(files);
546 if (fdt != &files->fdtab)
547 kmem_cache_free(files_cachep, files);
548 free_fdtable(fdt);
549 }
550 }
551
reset_files_struct(struct files_struct * files)552 void reset_files_struct(struct files_struct *files)
553 {
554 struct task_struct *tsk = current;
555 struct files_struct *old;
556
557 old = tsk->files;
558 task_lock(tsk);
559 tsk->files = files;
560 task_unlock(tsk);
561 put_files_struct(old);
562 }
563
exit_files(struct task_struct * tsk)564 void exit_files(struct task_struct *tsk)
565 {
566 struct files_struct * files = tsk->files;
567
568 if (files) {
569 task_lock(tsk);
570 tsk->files = NULL;
571 task_unlock(tsk);
572 put_files_struct(files);
573 }
574 }
575
put_fs_struct(struct fs_struct * fs)576 void put_fs_struct(struct fs_struct *fs)
577 {
578 /* No need to hold fs->lock if we are killing it */
579 if (atomic_dec_and_test(&fs->count)) {
580 path_put(&fs->root);
581 path_put(&fs->pwd);
582 kmem_cache_free(fs_cachep, fs);
583 }
584 }
585
exit_fs(struct task_struct * tsk)586 void exit_fs(struct task_struct *tsk)
587 {
588 struct fs_struct * fs = tsk->fs;
589
590 if (fs) {
591 task_lock(tsk);
592 tsk->fs = NULL;
593 task_unlock(tsk);
594 put_fs_struct(fs);
595 }
596 }
597
598 EXPORT_SYMBOL_GPL(exit_fs);
599
600 #ifdef CONFIG_MM_OWNER
601 /*
602 * Task p is exiting and it owned mm, lets find a new owner for it
603 */
604 static inline int
mm_need_new_owner(struct mm_struct * mm,struct task_struct * p)605 mm_need_new_owner(struct mm_struct *mm, struct task_struct *p)
606 {
607 /*
608 * If there are other users of the mm and the owner (us) is exiting
609 * we need to find a new owner to take on the responsibility.
610 */
611 if (atomic_read(&mm->mm_users) <= 1)
612 return 0;
613 if (mm->owner != p)
614 return 0;
615 return 1;
616 }
617
mm_update_next_owner(struct mm_struct * mm)618 void mm_update_next_owner(struct mm_struct *mm)
619 {
620 struct task_struct *c, *g, *p = current;
621
622 retry:
623 if (!mm_need_new_owner(mm, p))
624 return;
625
626 read_lock(&tasklist_lock);
627 /*
628 * Search in the children
629 */
630 list_for_each_entry(c, &p->children, sibling) {
631 if (c->mm == mm)
632 goto assign_new_owner;
633 }
634
635 /*
636 * Search in the siblings
637 */
638 list_for_each_entry(c, &p->parent->children, sibling) {
639 if (c->mm == mm)
640 goto assign_new_owner;
641 }
642
643 /*
644 * Search through everything else. We should not get
645 * here often
646 */
647 do_each_thread(g, c) {
648 if (c->mm == mm)
649 goto assign_new_owner;
650 } while_each_thread(g, c);
651
652 read_unlock(&tasklist_lock);
653 /*
654 * We found no owner yet mm_users > 1: this implies that we are
655 * most likely racing with swapoff (try_to_unuse()) or /proc or
656 * ptrace or page migration (get_task_mm()). Mark owner as NULL.
657 */
658 mm->owner = NULL;
659 return;
660
661 assign_new_owner:
662 BUG_ON(c == p);
663 get_task_struct(c);
664 /*
665 * The task_lock protects c->mm from changing.
666 * We always want mm->owner->mm == mm
667 */
668 task_lock(c);
669 /*
670 * Delay read_unlock() till we have the task_lock()
671 * to ensure that c does not slip away underneath us
672 */
673 read_unlock(&tasklist_lock);
674 if (c->mm != mm) {
675 task_unlock(c);
676 put_task_struct(c);
677 goto retry;
678 }
679 mm->owner = c;
680 task_unlock(c);
681 put_task_struct(c);
682 }
683 #endif /* CONFIG_MM_OWNER */
684
685 /*
686 * Turn us into a lazy TLB process if we
687 * aren't already..
688 */
exit_mm(struct task_struct * tsk)689 static void exit_mm(struct task_struct * tsk)
690 {
691 struct mm_struct *mm = tsk->mm;
692 struct core_state *core_state;
693
694 mm_release(tsk, mm);
695 if (!mm)
696 return;
697 /*
698 * Serialize with any possible pending coredump.
699 * We must hold mmap_sem around checking core_state
700 * and clearing tsk->mm. The core-inducing thread
701 * will increment ->nr_threads for each thread in the
702 * group with ->mm != NULL.
703 */
704 down_read(&mm->mmap_sem);
705 core_state = mm->core_state;
706 if (core_state) {
707 struct core_thread self;
708 up_read(&mm->mmap_sem);
709
710 self.task = tsk;
711 self.next = xchg(&core_state->dumper.next, &self);
712 /*
713 * Implies mb(), the result of xchg() must be visible
714 * to core_state->dumper.
715 */
716 if (atomic_dec_and_test(&core_state->nr_threads))
717 complete(&core_state->startup);
718
719 for (;;) {
720 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
721 if (!self.task) /* see coredump_finish() */
722 break;
723 schedule();
724 }
725 __set_task_state(tsk, TASK_RUNNING);
726 down_read(&mm->mmap_sem);
727 }
728 atomic_inc(&mm->mm_count);
729 BUG_ON(mm != tsk->active_mm);
730 /* more a memory barrier than a real lock */
731 task_lock(tsk);
732 tsk->mm = NULL;
733 up_read(&mm->mmap_sem);
734 enter_lazy_tlb(mm, current);
735 /* We don't want this task to be frozen prematurely */
736 clear_freeze_flag(tsk);
737 task_unlock(tsk);
738 mm_update_next_owner(mm);
739 mmput(mm);
740 }
741
742 /*
743 * Return nonzero if @parent's children should reap themselves.
744 *
745 * Called with write_lock_irq(&tasklist_lock) held.
746 */
ignoring_children(struct task_struct * parent)747 static int ignoring_children(struct task_struct *parent)
748 {
749 int ret;
750 struct sighand_struct *psig = parent->sighand;
751 unsigned long flags;
752 spin_lock_irqsave(&psig->siglock, flags);
753 ret = (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
754 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT));
755 spin_unlock_irqrestore(&psig->siglock, flags);
756 return ret;
757 }
758
759 /*
760 * Detach all tasks we were using ptrace on.
761 * Any that need to be release_task'd are put on the @dead list.
762 *
763 * Called with write_lock(&tasklist_lock) held.
764 */
ptrace_exit(struct task_struct * parent,struct list_head * dead)765 static void ptrace_exit(struct task_struct *parent, struct list_head *dead)
766 {
767 struct task_struct *p, *n;
768 int ign = -1;
769
770 list_for_each_entry_safe(p, n, &parent->ptraced, ptrace_entry) {
771 __ptrace_unlink(p);
772
773 if (p->exit_state != EXIT_ZOMBIE)
774 continue;
775
776 /*
777 * If it's a zombie, our attachedness prevented normal
778 * parent notification or self-reaping. Do notification
779 * now if it would have happened earlier. If it should
780 * reap itself, add it to the @dead list. We can't call
781 * release_task() here because we already hold tasklist_lock.
782 *
783 * If it's our own child, there is no notification to do.
784 * But if our normal children self-reap, then this child
785 * was prevented by ptrace and we must reap it now.
786 */
787 if (!task_detached(p) && thread_group_empty(p)) {
788 if (!same_thread_group(p->real_parent, parent))
789 do_notify_parent(p, p->exit_signal);
790 else {
791 if (ign < 0)
792 ign = ignoring_children(parent);
793 if (ign)
794 p->exit_signal = -1;
795 }
796 }
797
798 if (task_detached(p)) {
799 /*
800 * Mark it as in the process of being reaped.
801 */
802 p->exit_state = EXIT_DEAD;
803 list_add(&p->ptrace_entry, dead);
804 }
805 }
806 }
807
808 /*
809 * Finish up exit-time ptrace cleanup.
810 *
811 * Called without locks.
812 */
ptrace_exit_finish(struct task_struct * parent,struct list_head * dead)813 static void ptrace_exit_finish(struct task_struct *parent,
814 struct list_head *dead)
815 {
816 struct task_struct *p, *n;
817
818 BUG_ON(!list_empty(&parent->ptraced));
819
820 list_for_each_entry_safe(p, n, dead, ptrace_entry) {
821 list_del_init(&p->ptrace_entry);
822 release_task(p);
823 }
824 }
825
reparent_thread(struct task_struct * p,struct task_struct * father)826 static void reparent_thread(struct task_struct *p, struct task_struct *father)
827 {
828 if (p->pdeath_signal)
829 /* We already hold the tasklist_lock here. */
830 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
831
832 list_move_tail(&p->sibling, &p->real_parent->children);
833
834 /* If this is a threaded reparent there is no need to
835 * notify anyone anything has happened.
836 */
837 if (same_thread_group(p->real_parent, father))
838 return;
839
840 /* We don't want people slaying init. */
841 if (!task_detached(p))
842 p->exit_signal = SIGCHLD;
843
844 /* If we'd notified the old parent about this child's death,
845 * also notify the new parent.
846 */
847 if (!ptrace_reparented(p) &&
848 p->exit_state == EXIT_ZOMBIE &&
849 !task_detached(p) && thread_group_empty(p))
850 do_notify_parent(p, p->exit_signal);
851
852 kill_orphaned_pgrp(p, father);
853 }
854
855 /*
856 * When we die, we re-parent all our children.
857 * Try to give them to another thread in our thread
858 * group, and if no such member exists, give it to
859 * the child reaper process (ie "init") in our pid
860 * space.
861 */
find_new_reaper(struct task_struct * father)862 static struct task_struct *find_new_reaper(struct task_struct *father)
863 {
864 struct pid_namespace *pid_ns = task_active_pid_ns(father);
865 struct task_struct *thread;
866
867 thread = father;
868 while_each_thread(father, thread) {
869 if (thread->flags & PF_EXITING)
870 continue;
871 if (unlikely(pid_ns->child_reaper == father))
872 pid_ns->child_reaper = thread;
873 return thread;
874 }
875
876 if (unlikely(pid_ns->child_reaper == father)) {
877 write_unlock_irq(&tasklist_lock);
878 if (unlikely(pid_ns == &init_pid_ns))
879 panic("Attempted to kill init!");
880
881 zap_pid_ns_processes(pid_ns);
882 write_lock_irq(&tasklist_lock);
883 /*
884 * We can not clear ->child_reaper or leave it alone.
885 * There may by stealth EXIT_DEAD tasks on ->children,
886 * forget_original_parent() must move them somewhere.
887 */
888 pid_ns->child_reaper = init_pid_ns.child_reaper;
889 }
890
891 return pid_ns->child_reaper;
892 }
893
forget_original_parent(struct task_struct * father)894 static void forget_original_parent(struct task_struct *father)
895 {
896 struct task_struct *p, *n, *reaper;
897 LIST_HEAD(ptrace_dead);
898
899 write_lock_irq(&tasklist_lock);
900 reaper = find_new_reaper(father);
901 /*
902 * First clean up ptrace if we were using it.
903 */
904 ptrace_exit(father, &ptrace_dead);
905
906 list_for_each_entry_safe(p, n, &father->children, sibling) {
907 p->real_parent = reaper;
908 if (p->parent == father) {
909 BUG_ON(p->ptrace);
910 p->parent = p->real_parent;
911 }
912 reparent_thread(p, father);
913 }
914
915 write_unlock_irq(&tasklist_lock);
916 BUG_ON(!list_empty(&father->children));
917
918 ptrace_exit_finish(father, &ptrace_dead);
919 }
920
921 /*
922 * Send signals to all our closest relatives so that they know
923 * to properly mourn us..
924 */
exit_notify(struct task_struct * tsk,int group_dead)925 static void exit_notify(struct task_struct *tsk, int group_dead)
926 {
927 int signal;
928 void *cookie;
929
930 /*
931 * This does two things:
932 *
933 * A. Make init inherit all the child processes
934 * B. Check to see if any process groups have become orphaned
935 * as a result of our exiting, and if they have any stopped
936 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
937 */
938 forget_original_parent(tsk);
939 exit_task_namespaces(tsk);
940
941 write_lock_irq(&tasklist_lock);
942 if (group_dead)
943 kill_orphaned_pgrp(tsk->group_leader, NULL);
944
945 /* Let father know we died
946 *
947 * Thread signals are configurable, but you aren't going to use
948 * that to send signals to arbitary processes.
949 * That stops right now.
950 *
951 * If the parent exec id doesn't match the exec id we saved
952 * when we started then we know the parent has changed security
953 * domain.
954 *
955 * If our self_exec id doesn't match our parent_exec_id then
956 * we have changed execution domain as these two values started
957 * the same after a fork.
958 */
959 if (tsk->exit_signal != SIGCHLD && !task_detached(tsk) &&
960 (tsk->parent_exec_id != tsk->real_parent->self_exec_id ||
961 tsk->self_exec_id != tsk->parent_exec_id) &&
962 !capable(CAP_KILL))
963 tsk->exit_signal = SIGCHLD;
964
965 signal = tracehook_notify_death(tsk, &cookie, group_dead);
966 if (signal >= 0)
967 signal = do_notify_parent(tsk, signal);
968
969 tsk->exit_state = signal == DEATH_REAP ? EXIT_DEAD : EXIT_ZOMBIE;
970
971 /* mt-exec, de_thread() is waiting for us */
972 if (thread_group_leader(tsk) &&
973 tsk->signal->group_exit_task &&
974 tsk->signal->notify_count < 0)
975 wake_up_process(tsk->signal->group_exit_task);
976
977 write_unlock_irq(&tasklist_lock);
978
979 tracehook_report_death(tsk, signal, cookie, group_dead);
980
981 /* If the process is dead, release it - nobody will wait for it */
982 if (signal == DEATH_REAP)
983 release_task(tsk);
984 }
985
986 #ifdef CONFIG_DEBUG_STACK_USAGE
check_stack_usage(void)987 static void check_stack_usage(void)
988 {
989 static DEFINE_SPINLOCK(low_water_lock);
990 static int lowest_to_date = THREAD_SIZE;
991 unsigned long *n = end_of_stack(current);
992 unsigned long free;
993
994 while (*n == 0)
995 n++;
996 free = (unsigned long)n - (unsigned long)end_of_stack(current);
997
998 if (free >= lowest_to_date)
999 return;
1000
1001 spin_lock(&low_water_lock);
1002 if (free < lowest_to_date) {
1003 printk(KERN_WARNING "%s used greatest stack depth: %lu bytes "
1004 "left\n",
1005 current->comm, free);
1006 lowest_to_date = free;
1007 }
1008 spin_unlock(&low_water_lock);
1009 }
1010 #else
check_stack_usage(void)1011 static inline void check_stack_usage(void) {}
1012 #endif
1013
do_exit(long code)1014 NORET_TYPE void do_exit(long code)
1015 {
1016 struct task_struct *tsk = current;
1017 int group_dead;
1018
1019 profile_task_exit(tsk);
1020
1021 WARN_ON(atomic_read(&tsk->fs_excl));
1022
1023 if (unlikely(in_interrupt()))
1024 panic("Aiee, killing interrupt handler!");
1025 if (unlikely(!tsk->pid))
1026 panic("Attempted to kill the idle task!");
1027
1028 tracehook_report_exit(&code);
1029
1030 /*
1031 * We're taking recursive faults here in do_exit. Safest is to just
1032 * leave this task alone and wait for reboot.
1033 */
1034 if (unlikely(tsk->flags & PF_EXITING)) {
1035 printk(KERN_ALERT
1036 "Fixing recursive fault but reboot is needed!\n");
1037 /*
1038 * We can do this unlocked here. The futex code uses
1039 * this flag just to verify whether the pi state
1040 * cleanup has been done or not. In the worst case it
1041 * loops once more. We pretend that the cleanup was
1042 * done as there is no way to return. Either the
1043 * OWNER_DIED bit is set by now or we push the blocked
1044 * task into the wait for ever nirwana as well.
1045 */
1046 tsk->flags |= PF_EXITPIDONE;
1047 set_current_state(TASK_UNINTERRUPTIBLE);
1048 schedule();
1049 }
1050
1051 exit_signals(tsk); /* sets PF_EXITING */
1052 /*
1053 * tsk->flags are checked in the futex code to protect against
1054 * an exiting task cleaning up the robust pi futexes.
1055 */
1056 smp_mb();
1057 spin_unlock_wait(&tsk->pi_lock);
1058
1059 if (unlikely(in_atomic()))
1060 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
1061 current->comm, task_pid_nr(current),
1062 preempt_count());
1063
1064 acct_update_integrals(tsk);
1065
1066 group_dead = atomic_dec_and_test(&tsk->signal->live);
1067 if (group_dead) {
1068 hrtimer_cancel(&tsk->signal->real_timer);
1069 exit_itimers(tsk->signal);
1070 }
1071 acct_collect(code, group_dead);
1072 if (group_dead)
1073 tty_audit_exit();
1074 if (unlikely(tsk->audit_context))
1075 audit_free(tsk);
1076
1077 tsk->exit_code = code;
1078 taskstats_exit(tsk, group_dead);
1079
1080 exit_mm(tsk);
1081
1082 if (group_dead)
1083 acct_process();
1084 trace_sched_process_exit(tsk);
1085
1086 exit_sem(tsk);
1087 exit_files(tsk);
1088 exit_fs(tsk);
1089 check_stack_usage();
1090 exit_thread();
1091 cgroup_exit(tsk, 1);
1092
1093 if (group_dead && tsk->signal->leader)
1094 disassociate_ctty(1);
1095
1096 module_put(task_thread_info(tsk)->exec_domain->module);
1097 if (tsk->binfmt)
1098 module_put(tsk->binfmt->module);
1099
1100 proc_exit_connector(tsk);
1101 exit_notify(tsk, group_dead);
1102 #ifdef CONFIG_NUMA
1103 mpol_put(tsk->mempolicy);
1104 tsk->mempolicy = NULL;
1105 #endif
1106 #ifdef CONFIG_FUTEX
1107 /*
1108 * This must happen late, after the PID is not
1109 * hashed anymore:
1110 */
1111 if (unlikely(!list_empty(&tsk->pi_state_list)))
1112 exit_pi_state_list(tsk);
1113 if (unlikely(current->pi_state_cache))
1114 kfree(current->pi_state_cache);
1115 #endif
1116 /*
1117 * Make sure we are holding no locks:
1118 */
1119 debug_check_no_locks_held(tsk);
1120 /*
1121 * We can do this unlocked here. The futex code uses this flag
1122 * just to verify whether the pi state cleanup has been done
1123 * or not. In the worst case it loops once more.
1124 */
1125 tsk->flags |= PF_EXITPIDONE;
1126
1127 if (tsk->io_context)
1128 exit_io_context();
1129
1130 if (tsk->splice_pipe)
1131 __free_pipe_info(tsk->splice_pipe);
1132
1133 preempt_disable();
1134 /* causes final put_task_struct in finish_task_switch(). */
1135 tsk->state = TASK_DEAD;
1136
1137 #ifdef CONFIG_QEMU_TRACE
1138 /* Emit a trace record for the exit() call. */
1139 qemu_trace_exit(code);
1140 #endif
1141
1142 schedule();
1143 BUG();
1144 /* Avoid "noreturn function does return". */
1145 for (;;)
1146 cpu_relax(); /* For when BUG is null */
1147 }
1148
1149 EXPORT_SYMBOL_GPL(do_exit);
1150
complete_and_exit(struct completion * comp,long code)1151 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
1152 {
1153 if (comp)
1154 complete(comp);
1155
1156 do_exit(code);
1157 }
1158
1159 EXPORT_SYMBOL(complete_and_exit);
1160
SYSCALL_DEFINE1(exit,int,error_code)1161 SYSCALL_DEFINE1(exit, int, error_code)
1162 {
1163 do_exit((error_code&0xff)<<8);
1164 }
1165
1166 /*
1167 * Take down every thread in the group. This is called by fatal signals
1168 * as well as by sys_exit_group (below).
1169 */
1170 NORET_TYPE void
do_group_exit(int exit_code)1171 do_group_exit(int exit_code)
1172 {
1173 struct signal_struct *sig = current->signal;
1174
1175 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
1176
1177 if (signal_group_exit(sig))
1178 exit_code = sig->group_exit_code;
1179 else if (!thread_group_empty(current)) {
1180 struct sighand_struct *const sighand = current->sighand;
1181 spin_lock_irq(&sighand->siglock);
1182 if (signal_group_exit(sig))
1183 /* Another thread got here before we took the lock. */
1184 exit_code = sig->group_exit_code;
1185 else {
1186 sig->group_exit_code = exit_code;
1187 sig->flags = SIGNAL_GROUP_EXIT;
1188 zap_other_threads(current);
1189 }
1190 spin_unlock_irq(&sighand->siglock);
1191 }
1192
1193 do_exit(exit_code);
1194 /* NOTREACHED */
1195 }
1196
1197 /*
1198 * this kills every thread in the thread group. Note that any externally
1199 * wait4()-ing process will get the correct exit code - even if this
1200 * thread is not the thread group leader.
1201 */
SYSCALL_DEFINE1(exit_group,int,error_code)1202 SYSCALL_DEFINE1(exit_group, int, error_code)
1203 {
1204 do_group_exit((error_code & 0xff) << 8);
1205 /* NOTREACHED */
1206 return 0;
1207 }
1208
task_pid_type(struct task_struct * task,enum pid_type type)1209 static struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
1210 {
1211 struct pid *pid = NULL;
1212 if (type == PIDTYPE_PID)
1213 pid = task->pids[type].pid;
1214 else if (type < PIDTYPE_MAX)
1215 pid = task->group_leader->pids[type].pid;
1216 return pid;
1217 }
1218
eligible_child(enum pid_type type,struct pid * pid,int options,struct task_struct * p)1219 static int eligible_child(enum pid_type type, struct pid *pid, int options,
1220 struct task_struct *p)
1221 {
1222 int err;
1223
1224 if (type < PIDTYPE_MAX) {
1225 if (task_pid_type(p, type) != pid)
1226 return 0;
1227 }
1228
1229 /* Wait for all children (clone and not) if __WALL is set;
1230 * otherwise, wait for clone children *only* if __WCLONE is
1231 * set; otherwise, wait for non-clone children *only*. (Note:
1232 * A "clone" child here is one that reports to its parent
1233 * using a signal other than SIGCHLD.) */
1234 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
1235 && !(options & __WALL))
1236 return 0;
1237
1238 err = security_task_wait(p);
1239 if (err)
1240 return err;
1241
1242 return 1;
1243 }
1244
wait_noreap_copyout(struct task_struct * p,pid_t pid,uid_t uid,int why,int status,struct siginfo __user * infop,struct rusage __user * rusagep)1245 static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
1246 int why, int status,
1247 struct siginfo __user *infop,
1248 struct rusage __user *rusagep)
1249 {
1250 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
1251
1252 put_task_struct(p);
1253 if (!retval)
1254 retval = put_user(SIGCHLD, &infop->si_signo);
1255 if (!retval)
1256 retval = put_user(0, &infop->si_errno);
1257 if (!retval)
1258 retval = put_user((short)why, &infop->si_code);
1259 if (!retval)
1260 retval = put_user(pid, &infop->si_pid);
1261 if (!retval)
1262 retval = put_user(uid, &infop->si_uid);
1263 if (!retval)
1264 retval = put_user(status, &infop->si_status);
1265 if (!retval)
1266 retval = pid;
1267 return retval;
1268 }
1269
1270 /*
1271 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1272 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1273 * the lock and this task is uninteresting. If we return nonzero, we have
1274 * released the lock and the system call should return.
1275 */
wait_task_zombie(struct task_struct * p,int options,struct siginfo __user * infop,int __user * stat_addr,struct rusage __user * ru)1276 static int wait_task_zombie(struct task_struct *p, int options,
1277 struct siginfo __user *infop,
1278 int __user *stat_addr, struct rusage __user *ru)
1279 {
1280 unsigned long state;
1281 int retval, status, traced;
1282 pid_t pid = task_pid_vnr(p);
1283 uid_t uid = __task_cred(p)->uid;
1284
1285 if (!likely(options & WEXITED))
1286 return 0;
1287
1288 if (unlikely(options & WNOWAIT)) {
1289 int exit_code = p->exit_code;
1290 int why, status;
1291
1292 get_task_struct(p);
1293 read_unlock(&tasklist_lock);
1294 if ((exit_code & 0x7f) == 0) {
1295 why = CLD_EXITED;
1296 status = exit_code >> 8;
1297 } else {
1298 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1299 status = exit_code & 0x7f;
1300 }
1301 return wait_noreap_copyout(p, pid, uid, why,
1302 status, infop, ru);
1303 }
1304
1305 /*
1306 * Try to move the task's state to DEAD
1307 * only one thread is allowed to do this:
1308 */
1309 state = xchg(&p->exit_state, EXIT_DEAD);
1310 if (state != EXIT_ZOMBIE) {
1311 BUG_ON(state != EXIT_DEAD);
1312 return 0;
1313 }
1314
1315 traced = ptrace_reparented(p);
1316
1317 if (likely(!traced)) {
1318 struct signal_struct *psig;
1319 struct signal_struct *sig;
1320 struct task_cputime cputime;
1321
1322 /*
1323 * The resource counters for the group leader are in its
1324 * own task_struct. Those for dead threads in the group
1325 * are in its signal_struct, as are those for the child
1326 * processes it has previously reaped. All these
1327 * accumulate in the parent's signal_struct c* fields.
1328 *
1329 * We don't bother to take a lock here to protect these
1330 * p->signal fields, because they are only touched by
1331 * __exit_signal, which runs with tasklist_lock
1332 * write-locked anyway, and so is excluded here. We do
1333 * need to protect the access to p->parent->signal fields,
1334 * as other threads in the parent group can be right
1335 * here reaping other children at the same time.
1336 *
1337 * We use thread_group_cputime() to get times for the thread
1338 * group, which consolidates times for all threads in the
1339 * group including the group leader.
1340 */
1341 thread_group_cputime(p, &cputime);
1342 spin_lock_irq(&p->parent->sighand->siglock);
1343 psig = p->parent->signal;
1344 sig = p->signal;
1345 psig->cutime =
1346 cputime_add(psig->cutime,
1347 cputime_add(cputime.utime,
1348 sig->cutime));
1349 psig->cstime =
1350 cputime_add(psig->cstime,
1351 cputime_add(cputime.stime,
1352 sig->cstime));
1353 psig->cgtime =
1354 cputime_add(psig->cgtime,
1355 cputime_add(p->gtime,
1356 cputime_add(sig->gtime,
1357 sig->cgtime)));
1358 psig->cmin_flt +=
1359 p->min_flt + sig->min_flt + sig->cmin_flt;
1360 psig->cmaj_flt +=
1361 p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1362 psig->cnvcsw +=
1363 p->nvcsw + sig->nvcsw + sig->cnvcsw;
1364 psig->cnivcsw +=
1365 p->nivcsw + sig->nivcsw + sig->cnivcsw;
1366 psig->cinblock +=
1367 task_io_get_inblock(p) +
1368 sig->inblock + sig->cinblock;
1369 psig->coublock +=
1370 task_io_get_oublock(p) +
1371 sig->oublock + sig->coublock;
1372 task_io_accounting_add(&psig->ioac, &p->ioac);
1373 task_io_accounting_add(&psig->ioac, &sig->ioac);
1374 spin_unlock_irq(&p->parent->sighand->siglock);
1375 }
1376
1377 /*
1378 * Now we are sure this task is interesting, and no other
1379 * thread can reap it because we set its state to EXIT_DEAD.
1380 */
1381 read_unlock(&tasklist_lock);
1382
1383 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1384 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1385 ? p->signal->group_exit_code : p->exit_code;
1386 if (!retval && stat_addr)
1387 retval = put_user(status, stat_addr);
1388 if (!retval && infop)
1389 retval = put_user(SIGCHLD, &infop->si_signo);
1390 if (!retval && infop)
1391 retval = put_user(0, &infop->si_errno);
1392 if (!retval && infop) {
1393 int why;
1394
1395 if ((status & 0x7f) == 0) {
1396 why = CLD_EXITED;
1397 status >>= 8;
1398 } else {
1399 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1400 status &= 0x7f;
1401 }
1402 retval = put_user((short)why, &infop->si_code);
1403 if (!retval)
1404 retval = put_user(status, &infop->si_status);
1405 }
1406 if (!retval && infop)
1407 retval = put_user(pid, &infop->si_pid);
1408 if (!retval && infop)
1409 retval = put_user(uid, &infop->si_uid);
1410 if (!retval)
1411 retval = pid;
1412
1413 if (traced) {
1414 write_lock_irq(&tasklist_lock);
1415 /* We dropped tasklist, ptracer could die and untrace */
1416 ptrace_unlink(p);
1417 /*
1418 * If this is not a detached task, notify the parent.
1419 * If it's still not detached after that, don't release
1420 * it now.
1421 */
1422 if (!task_detached(p)) {
1423 do_notify_parent(p, p->exit_signal);
1424 if (!task_detached(p)) {
1425 p->exit_state = EXIT_ZOMBIE;
1426 p = NULL;
1427 }
1428 }
1429 write_unlock_irq(&tasklist_lock);
1430 }
1431 if (p != NULL)
1432 release_task(p);
1433
1434 return retval;
1435 }
1436
1437 /*
1438 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1439 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1440 * the lock and this task is uninteresting. If we return nonzero, we have
1441 * released the lock and the system call should return.
1442 */
wait_task_stopped(int ptrace,struct task_struct * p,int options,struct siginfo __user * infop,int __user * stat_addr,struct rusage __user * ru)1443 static int wait_task_stopped(int ptrace, struct task_struct *p,
1444 int options, struct siginfo __user *infop,
1445 int __user *stat_addr, struct rusage __user *ru)
1446 {
1447 int retval, exit_code, why;
1448 uid_t uid = 0; /* unneeded, required by compiler */
1449 pid_t pid;
1450
1451 if (!(options & WUNTRACED))
1452 return 0;
1453
1454 exit_code = 0;
1455 spin_lock_irq(&p->sighand->siglock);
1456
1457 if (unlikely(!task_is_stopped_or_traced(p)))
1458 goto unlock_sig;
1459
1460 if (!ptrace && p->signal->group_stop_count > 0)
1461 /*
1462 * A group stop is in progress and this is the group leader.
1463 * We won't report until all threads have stopped.
1464 */
1465 goto unlock_sig;
1466
1467 exit_code = p->exit_code;
1468 if (!exit_code)
1469 goto unlock_sig;
1470
1471 if (!unlikely(options & WNOWAIT))
1472 p->exit_code = 0;
1473
1474 /* don't need the RCU readlock here as we're holding a spinlock */
1475 uid = __task_cred(p)->uid;
1476 unlock_sig:
1477 spin_unlock_irq(&p->sighand->siglock);
1478 if (!exit_code)
1479 return 0;
1480
1481 /*
1482 * Now we are pretty sure this task is interesting.
1483 * Make sure it doesn't get reaped out from under us while we
1484 * give up the lock and then examine it below. We don't want to
1485 * keep holding onto the tasklist_lock while we call getrusage and
1486 * possibly take page faults for user memory.
1487 */
1488 get_task_struct(p);
1489 pid = task_pid_vnr(p);
1490 why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
1491 read_unlock(&tasklist_lock);
1492
1493 if (unlikely(options & WNOWAIT))
1494 return wait_noreap_copyout(p, pid, uid,
1495 why, exit_code,
1496 infop, ru);
1497
1498 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1499 if (!retval && stat_addr)
1500 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1501 if (!retval && infop)
1502 retval = put_user(SIGCHLD, &infop->si_signo);
1503 if (!retval && infop)
1504 retval = put_user(0, &infop->si_errno);
1505 if (!retval && infop)
1506 retval = put_user((short)why, &infop->si_code);
1507 if (!retval && infop)
1508 retval = put_user(exit_code, &infop->si_status);
1509 if (!retval && infop)
1510 retval = put_user(pid, &infop->si_pid);
1511 if (!retval && infop)
1512 retval = put_user(uid, &infop->si_uid);
1513 if (!retval)
1514 retval = pid;
1515 put_task_struct(p);
1516
1517 BUG_ON(!retval);
1518 return retval;
1519 }
1520
1521 /*
1522 * Handle do_wait work for one task in a live, non-stopped state.
1523 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1524 * the lock and this task is uninteresting. If we return nonzero, we have
1525 * released the lock and the system call should return.
1526 */
wait_task_continued(struct task_struct * p,int options,struct siginfo __user * infop,int __user * stat_addr,struct rusage __user * ru)1527 static int wait_task_continued(struct task_struct *p, int options,
1528 struct siginfo __user *infop,
1529 int __user *stat_addr, struct rusage __user *ru)
1530 {
1531 int retval;
1532 pid_t pid;
1533 uid_t uid;
1534
1535 if (!unlikely(options & WCONTINUED))
1536 return 0;
1537
1538 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1539 return 0;
1540
1541 spin_lock_irq(&p->sighand->siglock);
1542 /* Re-check with the lock held. */
1543 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1544 spin_unlock_irq(&p->sighand->siglock);
1545 return 0;
1546 }
1547 if (!unlikely(options & WNOWAIT))
1548 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1549 uid = __task_cred(p)->uid;
1550 spin_unlock_irq(&p->sighand->siglock);
1551
1552 pid = task_pid_vnr(p);
1553 get_task_struct(p);
1554 read_unlock(&tasklist_lock);
1555
1556 if (!infop) {
1557 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1558 put_task_struct(p);
1559 if (!retval && stat_addr)
1560 retval = put_user(0xffff, stat_addr);
1561 if (!retval)
1562 retval = pid;
1563 } else {
1564 retval = wait_noreap_copyout(p, pid, uid,
1565 CLD_CONTINUED, SIGCONT,
1566 infop, ru);
1567 BUG_ON(retval == 0);
1568 }
1569
1570 return retval;
1571 }
1572
1573 /*
1574 * Consider @p for a wait by @parent.
1575 *
1576 * -ECHILD should be in *@notask_error before the first call.
1577 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1578 * Returns zero if the search for a child should continue;
1579 * then *@notask_error is 0 if @p is an eligible child,
1580 * or another error from security_task_wait(), or still -ECHILD.
1581 */
wait_consider_task(struct task_struct * parent,int ptrace,struct task_struct * p,int * notask_error,enum pid_type type,struct pid * pid,int options,struct siginfo __user * infop,int __user * stat_addr,struct rusage __user * ru)1582 static int wait_consider_task(struct task_struct *parent, int ptrace,
1583 struct task_struct *p, int *notask_error,
1584 enum pid_type type, struct pid *pid, int options,
1585 struct siginfo __user *infop,
1586 int __user *stat_addr, struct rusage __user *ru)
1587 {
1588 int ret = eligible_child(type, pid, options, p);
1589 if (!ret)
1590 return ret;
1591
1592 if (unlikely(ret < 0)) {
1593 /*
1594 * If we have not yet seen any eligible child,
1595 * then let this error code replace -ECHILD.
1596 * A permission error will give the user a clue
1597 * to look for security policy problems, rather
1598 * than for mysterious wait bugs.
1599 */
1600 if (*notask_error)
1601 *notask_error = ret;
1602 }
1603
1604 if (likely(!ptrace) && unlikely(p->ptrace)) {
1605 /*
1606 * This child is hidden by ptrace.
1607 * We aren't allowed to see it now, but eventually we will.
1608 */
1609 *notask_error = 0;
1610 return 0;
1611 }
1612
1613 if (p->exit_state == EXIT_DEAD)
1614 return 0;
1615
1616 /*
1617 * We don't reap group leaders with subthreads.
1618 */
1619 if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p))
1620 return wait_task_zombie(p, options, infop, stat_addr, ru);
1621
1622 /*
1623 * It's stopped or running now, so it might
1624 * later continue, exit, or stop again.
1625 */
1626 *notask_error = 0;
1627
1628 if (task_is_stopped_or_traced(p))
1629 return wait_task_stopped(ptrace, p, options,
1630 infop, stat_addr, ru);
1631
1632 return wait_task_continued(p, options, infop, stat_addr, ru);
1633 }
1634
1635 /*
1636 * Do the work of do_wait() for one thread in the group, @tsk.
1637 *
1638 * -ECHILD should be in *@notask_error before the first call.
1639 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1640 * Returns zero if the search for a child should continue; then
1641 * *@notask_error is 0 if there were any eligible children,
1642 * or another error from security_task_wait(), or still -ECHILD.
1643 */
do_wait_thread(struct task_struct * tsk,int * notask_error,enum pid_type type,struct pid * pid,int options,struct siginfo __user * infop,int __user * stat_addr,struct rusage __user * ru)1644 static int do_wait_thread(struct task_struct *tsk, int *notask_error,
1645 enum pid_type type, struct pid *pid, int options,
1646 struct siginfo __user *infop, int __user *stat_addr,
1647 struct rusage __user *ru)
1648 {
1649 struct task_struct *p;
1650
1651 list_for_each_entry(p, &tsk->children, sibling) {
1652 /*
1653 * Do not consider detached threads.
1654 */
1655 if (!task_detached(p)) {
1656 int ret = wait_consider_task(tsk, 0, p, notask_error,
1657 type, pid, options,
1658 infop, stat_addr, ru);
1659 if (ret)
1660 return ret;
1661 }
1662 }
1663
1664 return 0;
1665 }
1666
ptrace_do_wait(struct task_struct * tsk,int * notask_error,enum pid_type type,struct pid * pid,int options,struct siginfo __user * infop,int __user * stat_addr,struct rusage __user * ru)1667 static int ptrace_do_wait(struct task_struct *tsk, int *notask_error,
1668 enum pid_type type, struct pid *pid, int options,
1669 struct siginfo __user *infop, int __user *stat_addr,
1670 struct rusage __user *ru)
1671 {
1672 struct task_struct *p;
1673
1674 /*
1675 * Traditionally we see ptrace'd stopped tasks regardless of options.
1676 */
1677 options |= WUNTRACED;
1678
1679 list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1680 int ret = wait_consider_task(tsk, 1, p, notask_error,
1681 type, pid, options,
1682 infop, stat_addr, ru);
1683 if (ret)
1684 return ret;
1685 }
1686
1687 return 0;
1688 }
1689
do_wait(enum pid_type type,struct pid * pid,int options,struct siginfo __user * infop,int __user * stat_addr,struct rusage __user * ru)1690 static long do_wait(enum pid_type type, struct pid *pid, int options,
1691 struct siginfo __user *infop, int __user *stat_addr,
1692 struct rusage __user *ru)
1693 {
1694 DECLARE_WAITQUEUE(wait, current);
1695 struct task_struct *tsk;
1696 int retval;
1697
1698 trace_sched_process_wait(pid);
1699
1700 add_wait_queue(¤t->signal->wait_chldexit,&wait);
1701 repeat:
1702 /*
1703 * If there is nothing that can match our critiera just get out.
1704 * We will clear @retval to zero if we see any child that might later
1705 * match our criteria, even if we are not able to reap it yet.
1706 */
1707 retval = -ECHILD;
1708 if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type])))
1709 goto end;
1710
1711 current->state = TASK_INTERRUPTIBLE;
1712 read_lock(&tasklist_lock);
1713 tsk = current;
1714 do {
1715 int tsk_result = do_wait_thread(tsk, &retval,
1716 type, pid, options,
1717 infop, stat_addr, ru);
1718 if (!tsk_result)
1719 tsk_result = ptrace_do_wait(tsk, &retval,
1720 type, pid, options,
1721 infop, stat_addr, ru);
1722 if (tsk_result) {
1723 /*
1724 * tasklist_lock is unlocked and we have a final result.
1725 */
1726 retval = tsk_result;
1727 goto end;
1728 }
1729
1730 if (options & __WNOTHREAD)
1731 break;
1732 tsk = next_thread(tsk);
1733 BUG_ON(tsk->signal != current->signal);
1734 } while (tsk != current);
1735 read_unlock(&tasklist_lock);
1736
1737 if (!retval && !(options & WNOHANG)) {
1738 retval = -ERESTARTSYS;
1739 if (!signal_pending(current)) {
1740 schedule();
1741 goto repeat;
1742 }
1743 }
1744
1745 end:
1746 current->state = TASK_RUNNING;
1747 remove_wait_queue(¤t->signal->wait_chldexit,&wait);
1748 if (infop) {
1749 if (retval > 0)
1750 retval = 0;
1751 else {
1752 /*
1753 * For a WNOHANG return, clear out all the fields
1754 * we would set so the user can easily tell the
1755 * difference.
1756 */
1757 if (!retval)
1758 retval = put_user(0, &infop->si_signo);
1759 if (!retval)
1760 retval = put_user(0, &infop->si_errno);
1761 if (!retval)
1762 retval = put_user(0, &infop->si_code);
1763 if (!retval)
1764 retval = put_user(0, &infop->si_pid);
1765 if (!retval)
1766 retval = put_user(0, &infop->si_uid);
1767 if (!retval)
1768 retval = put_user(0, &infop->si_status);
1769 }
1770 }
1771 return retval;
1772 }
1773
SYSCALL_DEFINE5(waitid,int,which,pid_t,upid,struct siginfo __user *,infop,int,options,struct rusage __user *,ru)1774 SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1775 infop, int, options, struct rusage __user *, ru)
1776 {
1777 struct pid *pid = NULL;
1778 enum pid_type type;
1779 long ret;
1780
1781 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1782 return -EINVAL;
1783 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1784 return -EINVAL;
1785
1786 switch (which) {
1787 case P_ALL:
1788 type = PIDTYPE_MAX;
1789 break;
1790 case P_PID:
1791 type = PIDTYPE_PID;
1792 if (upid <= 0)
1793 return -EINVAL;
1794 break;
1795 case P_PGID:
1796 type = PIDTYPE_PGID;
1797 if (upid <= 0)
1798 return -EINVAL;
1799 break;
1800 default:
1801 return -EINVAL;
1802 }
1803
1804 if (type < PIDTYPE_MAX)
1805 pid = find_get_pid(upid);
1806 ret = do_wait(type, pid, options, infop, NULL, ru);
1807 put_pid(pid);
1808
1809 /* avoid REGPARM breakage on x86: */
1810 asmlinkage_protect(5, ret, which, upid, infop, options, ru);
1811 return ret;
1812 }
1813
SYSCALL_DEFINE4(wait4,pid_t,upid,int __user *,stat_addr,int,options,struct rusage __user *,ru)1814 SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1815 int, options, struct rusage __user *, ru)
1816 {
1817 struct pid *pid = NULL;
1818 enum pid_type type;
1819 long ret;
1820
1821 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1822 __WNOTHREAD|__WCLONE|__WALL))
1823 return -EINVAL;
1824
1825 if (upid == -1)
1826 type = PIDTYPE_MAX;
1827 else if (upid < 0) {
1828 type = PIDTYPE_PGID;
1829 pid = find_get_pid(-upid);
1830 } else if (upid == 0) {
1831 type = PIDTYPE_PGID;
1832 pid = get_pid(task_pgrp(current));
1833 } else /* upid > 0 */ {
1834 type = PIDTYPE_PID;
1835 pid = find_get_pid(upid);
1836 }
1837
1838 ret = do_wait(type, pid, options | WEXITED, NULL, stat_addr, ru);
1839 put_pid(pid);
1840
1841 /* avoid REGPARM breakage on x86: */
1842 asmlinkage_protect(4, ret, upid, stat_addr, options, ru);
1843 return ret;
1844 }
1845
1846 #ifdef __ARCH_WANT_SYS_WAITPID
1847
1848 /*
1849 * sys_waitpid() remains for compatibility. waitpid() should be
1850 * implemented by calling sys_wait4() from libc.a.
1851 */
SYSCALL_DEFINE3(waitpid,pid_t,pid,int __user *,stat_addr,int,options)1852 SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
1853 {
1854 return sys_wait4(pid, stat_addr, options, NULL);
1855 }
1856
1857 #endif
1858