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