[PATCH] choose_new_parent: remove unused arg, sanitize exit_state check
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / exit.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/exit.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/config.h>
8#include <linux/mm.h>
9#include <linux/slab.h>
10#include <linux/interrupt.h>
11#include <linux/smp_lock.h>
12#include <linux/module.h>
c59ede7b 13#include <linux/capability.h>
1da177e4
LT
14#include <linux/completion.h>
15#include <linux/personality.h>
16#include <linux/tty.h>
17#include <linux/namespace.h>
18#include <linux/key.h>
19#include <linux/security.h>
20#include <linux/cpu.h>
21#include <linux/acct.h>
22#include <linux/file.h>
23#include <linux/binfmts.h>
24#include <linux/ptrace.h>
25#include <linux/profile.h>
26#include <linux/mount.h>
27#include <linux/proc_fs.h>
28#include <linux/mempolicy.h>
29#include <linux/cpuset.h>
30#include <linux/syscalls.h>
7ed20e1a 31#include <linux/signal.h>
9f46080c 32#include <linux/cn_proc.h>
de5097c2 33#include <linux/mutex.h>
0771dfef 34#include <linux/futex.h>
34f192c6 35#include <linux/compat.h>
1da177e4
LT
36
37#include <asm/uaccess.h>
38#include <asm/unistd.h>
39#include <asm/pgtable.h>
40#include <asm/mmu_context.h>
41
42extern void sem_exit (void);
43extern struct task_struct *child_reaper;
44
45int getrusage(struct task_struct *, int, struct rusage __user *);
46
408b664a
AB
47static void exit_mm(struct task_struct * tsk);
48
1da177e4
LT
49static void __unhash_process(struct task_struct *p)
50{
51 nr_threads--;
52 detach_pid(p, PIDTYPE_PID);
53 detach_pid(p, PIDTYPE_TGID);
54 if (thread_group_leader(p)) {
55 detach_pid(p, PIDTYPE_PGID);
56 detach_pid(p, PIDTYPE_SID);
57 if (p->pid)
58 __get_cpu_var(process_counts)--;
59 }
60
61 REMOVE_LINKS(p);
62}
63
64void release_task(struct task_struct * p)
65{
66 int zap_leader;
67 task_t *leader;
68 struct dentry *proc_dentry;
69
70repeat:
71 atomic_dec(&p->user->processes);
72 spin_lock(&p->proc_lock);
73 proc_dentry = proc_pid_unhash(p);
74 write_lock_irq(&tasklist_lock);
75 if (unlikely(p->ptrace))
76 __ptrace_unlink(p);
77 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
78 __exit_signal(p);
71a2224d
CL
79 /*
80 * Note that the fastpath in sys_times depends on __exit_signal having
81 * updated the counters before a task is removed from the tasklist of
82 * the process by __unhash_process.
83 */
1da177e4
LT
84 __unhash_process(p);
85
86 /*
87 * If we are the last non-leader member of the thread
88 * group, and the leader is zombie, then notify the
89 * group leader's parent process. (if it wants notification.)
90 */
91 zap_leader = 0;
92 leader = p->group_leader;
93 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
94 BUG_ON(leader->exit_signal == -1);
95 do_notify_parent(leader, leader->exit_signal);
96 /*
97 * If we were the last child thread and the leader has
98 * exited already, and the leader's parent ignores SIGCHLD,
99 * then we are the one who should release the leader.
100 *
101 * do_notify_parent() will have marked it self-reaping in
102 * that case.
103 */
104 zap_leader = (leader->exit_signal == -1);
105 }
106
107 sched_exit(p);
108 write_unlock_irq(&tasklist_lock);
109 spin_unlock(&p->proc_lock);
110 proc_pid_flush(proc_dentry);
111 release_thread(p);
112 put_task_struct(p);
113
114 p = leader;
115 if (unlikely(zap_leader))
116 goto repeat;
117}
118
119/* we are using it only for SMP init */
120
121void unhash_process(struct task_struct *p)
122{
123 struct dentry *proc_dentry;
124
125 spin_lock(&p->proc_lock);
126 proc_dentry = proc_pid_unhash(p);
127 write_lock_irq(&tasklist_lock);
128 __unhash_process(p);
129 write_unlock_irq(&tasklist_lock);
130 spin_unlock(&p->proc_lock);
131 proc_pid_flush(proc_dentry);
132}
133
134/*
135 * This checks not only the pgrp, but falls back on the pid if no
136 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
137 * without this...
138 */
139int session_of_pgrp(int pgrp)
140{
141 struct task_struct *p;
142 int sid = -1;
143
144 read_lock(&tasklist_lock);
145 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
146 if (p->signal->session > 0) {
147 sid = p->signal->session;
148 goto out;
149 }
150 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
151 p = find_task_by_pid(pgrp);
152 if (p)
153 sid = p->signal->session;
154out:
155 read_unlock(&tasklist_lock);
156
157 return sid;
158}
159
160/*
161 * Determine if a process group is "orphaned", according to the POSIX
162 * definition in 2.2.2.52. Orphaned process groups are not to be affected
163 * by terminal-generated stop signals. Newly orphaned process groups are
164 * to receive a SIGHUP and a SIGCONT.
165 *
166 * "I ask you, have you ever known what it is to be an orphan?"
167 */
168static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
169{
170 struct task_struct *p;
171 int ret = 1;
172
173 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
174 if (p == ignored_task
175 || p->exit_state
176 || p->real_parent->pid == 1)
177 continue;
178 if (process_group(p->real_parent) != pgrp
179 && p->real_parent->signal->session == p->signal->session) {
180 ret = 0;
181 break;
182 }
183 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
184 return ret; /* (sighing) "Often!" */
185}
186
187int is_orphaned_pgrp(int pgrp)
188{
189 int retval;
190
191 read_lock(&tasklist_lock);
192 retval = will_become_orphaned_pgrp(pgrp, NULL);
193 read_unlock(&tasklist_lock);
194
195 return retval;
196}
197
858119e1 198static int has_stopped_jobs(int pgrp)
1da177e4
LT
199{
200 int retval = 0;
201 struct task_struct *p;
202
203 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
204 if (p->state != TASK_STOPPED)
205 continue;
206
207 /* If p is stopped by a debugger on a signal that won't
208 stop it, then don't count p as stopped. This isn't
209 perfect but it's a good approximation. */
210 if (unlikely (p->ptrace)
211 && p->exit_code != SIGSTOP
212 && p->exit_code != SIGTSTP
213 && p->exit_code != SIGTTOU
214 && p->exit_code != SIGTTIN)
215 continue;
216
217 retval = 1;
218 break;
219 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
220 return retval;
221}
222
223/**
4dc3b16b 224 * reparent_to_init - Reparent the calling kernel thread to the init task.
1da177e4
LT
225 *
226 * If a kernel thread is launched as a result of a system call, or if
227 * it ever exits, it should generally reparent itself to init so that
228 * it is correctly cleaned up on exit.
229 *
230 * The various task state such as scheduling policy and priority may have
231 * been inherited from a user process, so we reset them to sane values here.
232 *
233 * NOTE that reparent_to_init() gives the caller full capabilities.
234 */
858119e1 235static void reparent_to_init(void)
1da177e4
LT
236{
237 write_lock_irq(&tasklist_lock);
238
239 ptrace_unlink(current);
240 /* Reparent to init */
241 REMOVE_LINKS(current);
242 current->parent = child_reaper;
243 current->real_parent = child_reaper;
244 SET_LINKS(current);
245
246 /* Set the exit signal to SIGCHLD so we signal init on exit */
247 current->exit_signal = SIGCHLD;
248
b0a9499c
IM
249 if ((current->policy == SCHED_NORMAL ||
250 current->policy == SCHED_BATCH)
251 && (task_nice(current) < 0))
1da177e4
LT
252 set_user_nice(current, 0);
253 /* cpus_allowed? */
254 /* rt_priority? */
255 /* signals? */
256 security_task_reparent_to_init(current);
257 memcpy(current->signal->rlim, init_task.signal->rlim,
258 sizeof(current->signal->rlim));
259 atomic_inc(&(INIT_USER->__count));
260 write_unlock_irq(&tasklist_lock);
261 switch_uid(INIT_USER);
262}
263
264void __set_special_pids(pid_t session, pid_t pgrp)
265{
e19f247a 266 struct task_struct *curr = current->group_leader;
1da177e4
LT
267
268 if (curr->signal->session != session) {
269 detach_pid(curr, PIDTYPE_SID);
270 curr->signal->session = session;
271 attach_pid(curr, PIDTYPE_SID, session);
272 }
273 if (process_group(curr) != pgrp) {
274 detach_pid(curr, PIDTYPE_PGID);
275 curr->signal->pgrp = pgrp;
276 attach_pid(curr, PIDTYPE_PGID, pgrp);
277 }
278}
279
280void set_special_pids(pid_t session, pid_t pgrp)
281{
282 write_lock_irq(&tasklist_lock);
283 __set_special_pids(session, pgrp);
284 write_unlock_irq(&tasklist_lock);
285}
286
287/*
288 * Let kernel threads use this to say that they
289 * allow a certain signal (since daemonize() will
290 * have disabled all of them by default).
291 */
292int allow_signal(int sig)
293{
7ed20e1a 294 if (!valid_signal(sig) || sig < 1)
1da177e4
LT
295 return -EINVAL;
296
297 spin_lock_irq(&current->sighand->siglock);
298 sigdelset(&current->blocked, sig);
299 if (!current->mm) {
300 /* Kernel threads handle their own signals.
301 Let the signal code know it'll be handled, so
302 that they don't get converted to SIGKILL or
303 just silently dropped */
304 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
305 }
306 recalc_sigpending();
307 spin_unlock_irq(&current->sighand->siglock);
308 return 0;
309}
310
311EXPORT_SYMBOL(allow_signal);
312
313int disallow_signal(int sig)
314{
7ed20e1a 315 if (!valid_signal(sig) || sig < 1)
1da177e4
LT
316 return -EINVAL;
317
318 spin_lock_irq(&current->sighand->siglock);
319 sigaddset(&current->blocked, sig);
320 recalc_sigpending();
321 spin_unlock_irq(&current->sighand->siglock);
322 return 0;
323}
324
325EXPORT_SYMBOL(disallow_signal);
326
327/*
328 * Put all the gunge required to become a kernel thread without
329 * attached user resources in one place where it belongs.
330 */
331
332void daemonize(const char *name, ...)
333{
334 va_list args;
335 struct fs_struct *fs;
336 sigset_t blocked;
337
338 va_start(args, name);
339 vsnprintf(current->comm, sizeof(current->comm), name, args);
340 va_end(args);
341
342 /*
343 * If we were started as result of loading a module, close all of the
344 * user space pages. We don't need them, and if we didn't close them
345 * they would be locked into memory.
346 */
347 exit_mm(current);
348
349 set_special_pids(1, 1);
70522e12 350 mutex_lock(&tty_mutex);
1da177e4 351 current->signal->tty = NULL;
70522e12 352 mutex_unlock(&tty_mutex);
1da177e4
LT
353
354 /* Block and flush all signals */
355 sigfillset(&blocked);
356 sigprocmask(SIG_BLOCK, &blocked, NULL);
357 flush_signals(current);
358
359 /* Become as one with the init task */
360
361 exit_fs(current); /* current->fs->count--; */
362 fs = init_task.fs;
363 current->fs = fs;
364 atomic_inc(&fs->count);
5914811a
BS
365 exit_namespace(current);
366 current->namespace = init_task.namespace;
367 get_namespace(current->namespace);
1da177e4
LT
368 exit_files(current);
369 current->files = init_task.files;
370 atomic_inc(&current->files->count);
371
372 reparent_to_init();
373}
374
375EXPORT_SYMBOL(daemonize);
376
858119e1 377static void close_files(struct files_struct * files)
1da177e4
LT
378{
379 int i, j;
badf1662 380 struct fdtable *fdt;
1da177e4
LT
381
382 j = 0;
4fb3a538
DS
383
384 /*
385 * It is safe to dereference the fd table without RCU or
386 * ->file_lock because this is the last reference to the
387 * files structure.
388 */
badf1662 389 fdt = files_fdtable(files);
1da177e4
LT
390 for (;;) {
391 unsigned long set;
392 i = j * __NFDBITS;
badf1662 393 if (i >= fdt->max_fdset || i >= fdt->max_fds)
1da177e4 394 break;
badf1662 395 set = fdt->open_fds->fds_bits[j++];
1da177e4
LT
396 while (set) {
397 if (set & 1) {
badf1662 398 struct file * file = xchg(&fdt->fd[i], NULL);
1da177e4
LT
399 if (file)
400 filp_close(file, files);
401 }
402 i++;
403 set >>= 1;
404 }
405 }
406}
407
408struct files_struct *get_files_struct(struct task_struct *task)
409{
410 struct files_struct *files;
411
412 task_lock(task);
413 files = task->files;
414 if (files)
415 atomic_inc(&files->count);
416 task_unlock(task);
417
418 return files;
419}
420
421void fastcall put_files_struct(struct files_struct *files)
422{
badf1662
DS
423 struct fdtable *fdt;
424
1da177e4
LT
425 if (atomic_dec_and_test(&files->count)) {
426 close_files(files);
427 /*
428 * Free the fd and fdset arrays if we expanded them.
ab2af1f5
DS
429 * If the fdtable was embedded, pass files for freeing
430 * at the end of the RCU grace period. Otherwise,
431 * you can free files immediately.
1da177e4 432 */
badf1662 433 fdt = files_fdtable(files);
ab2af1f5
DS
434 if (fdt == &files->fdtab)
435 fdt->free_files = files;
436 else
437 kmem_cache_free(files_cachep, files);
438 free_fdtable(fdt);
1da177e4
LT
439 }
440}
441
442EXPORT_SYMBOL(put_files_struct);
443
444static inline void __exit_files(struct task_struct *tsk)
445{
446 struct files_struct * files = tsk->files;
447
448 if (files) {
449 task_lock(tsk);
450 tsk->files = NULL;
451 task_unlock(tsk);
452 put_files_struct(files);
453 }
454}
455
456void exit_files(struct task_struct *tsk)
457{
458 __exit_files(tsk);
459}
460
461static inline void __put_fs_struct(struct fs_struct *fs)
462{
463 /* No need to hold fs->lock if we are killing it */
464 if (atomic_dec_and_test(&fs->count)) {
465 dput(fs->root);
466 mntput(fs->rootmnt);
467 dput(fs->pwd);
468 mntput(fs->pwdmnt);
469 if (fs->altroot) {
470 dput(fs->altroot);
471 mntput(fs->altrootmnt);
472 }
473 kmem_cache_free(fs_cachep, fs);
474 }
475}
476
477void put_fs_struct(struct fs_struct *fs)
478{
479 __put_fs_struct(fs);
480}
481
482static inline void __exit_fs(struct task_struct *tsk)
483{
484 struct fs_struct * fs = tsk->fs;
485
486 if (fs) {
487 task_lock(tsk);
488 tsk->fs = NULL;
489 task_unlock(tsk);
490 __put_fs_struct(fs);
491 }
492}
493
494void exit_fs(struct task_struct *tsk)
495{
496 __exit_fs(tsk);
497}
498
499EXPORT_SYMBOL_GPL(exit_fs);
500
501/*
502 * Turn us into a lazy TLB process if we
503 * aren't already..
504 */
408b664a 505static void exit_mm(struct task_struct * tsk)
1da177e4
LT
506{
507 struct mm_struct *mm = tsk->mm;
508
509 mm_release(tsk, mm);
510 if (!mm)
511 return;
512 /*
513 * Serialize with any possible pending coredump.
514 * We must hold mmap_sem around checking core_waiters
515 * and clearing tsk->mm. The core-inducing thread
516 * will increment core_waiters for each thread in the
517 * group with ->mm != NULL.
518 */
519 down_read(&mm->mmap_sem);
520 if (mm->core_waiters) {
521 up_read(&mm->mmap_sem);
522 down_write(&mm->mmap_sem);
523 if (!--mm->core_waiters)
524 complete(mm->core_startup_done);
525 up_write(&mm->mmap_sem);
526
527 wait_for_completion(&mm->core_done);
528 down_read(&mm->mmap_sem);
529 }
530 atomic_inc(&mm->mm_count);
531 if (mm != tsk->active_mm) BUG();
532 /* more a memory barrier than a real lock */
533 task_lock(tsk);
534 tsk->mm = NULL;
535 up_read(&mm->mmap_sem);
536 enter_lazy_tlb(mm, current);
537 task_unlock(tsk);
538 mmput(mm);
539}
540
d799f035 541static inline void choose_new_parent(task_t *p, task_t *reaper)
1da177e4
LT
542{
543 /*
544 * Make sure we're not reparenting to ourselves and that
545 * the parent is not a zombie.
546 */
d799f035 547 BUG_ON(p == reaper || reaper->exit_state);
1da177e4 548 p->real_parent = reaper;
1da177e4
LT
549}
550
858119e1 551static void reparent_thread(task_t *p, task_t *father, int traced)
1da177e4
LT
552{
553 /* We don't want people slaying init. */
554 if (p->exit_signal != -1)
555 p->exit_signal = SIGCHLD;
556
557 if (p->pdeath_signal)
558 /* We already hold the tasklist_lock here. */
b67a1b9e 559 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
1da177e4
LT
560
561 /* Move the child from its dying parent to the new one. */
562 if (unlikely(traced)) {
563 /* Preserve ptrace links if someone else is tracing this child. */
564 list_del_init(&p->ptrace_list);
565 if (p->parent != p->real_parent)
566 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
567 } else {
568 /* If this child is being traced, then we're the one tracing it
569 * anyway, so let go of it.
570 */
571 p->ptrace = 0;
572 list_del_init(&p->sibling);
573 p->parent = p->real_parent;
574 list_add_tail(&p->sibling, &p->parent->children);
575
576 /* If we'd notified the old parent about this child's death,
577 * also notify the new parent.
578 */
579 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
580 thread_group_empty(p))
581 do_notify_parent(p, p->exit_signal);
582 else if (p->state == TASK_TRACED) {
583 /*
584 * If it was at a trace stop, turn it into
585 * a normal stop since it's no longer being
586 * traced.
587 */
588 ptrace_untrace(p);
589 }
590 }
591
592 /*
593 * process group orphan check
594 * Case ii: Our child is in a different pgrp
595 * than we are, and it was the only connection
596 * outside, so the child pgrp is now orphaned.
597 */
598 if ((process_group(p) != process_group(father)) &&
599 (p->signal->session == father->signal->session)) {
600 int pgrp = process_group(p);
601
602 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
b67a1b9e
ON
603 __kill_pg_info(SIGHUP, SEND_SIG_PRIV, pgrp);
604 __kill_pg_info(SIGCONT, SEND_SIG_PRIV, pgrp);
1da177e4
LT
605 }
606 }
607}
608
609/*
610 * When we die, we re-parent all our children.
611 * Try to give them to another thread in our thread
612 * group, and if no such member exists, give it to
613 * the global child reaper process (ie "init")
614 */
858119e1 615static void forget_original_parent(struct task_struct * father,
1da177e4
LT
616 struct list_head *to_release)
617{
618 struct task_struct *p, *reaper = father;
619 struct list_head *_p, *_n;
620
621 do {
622 reaper = next_thread(reaper);
623 if (reaper == father) {
624 reaper = child_reaper;
625 break;
626 }
627 } while (reaper->exit_state);
628
629 /*
630 * There are only two places where our children can be:
631 *
632 * - in our child list
633 * - in our ptraced child list
634 *
635 * Search them and reparent children.
636 */
637 list_for_each_safe(_p, _n, &father->children) {
638 int ptrace;
639 p = list_entry(_p,struct task_struct,sibling);
640
641 ptrace = p->ptrace;
642
643 /* if father isn't the real parent, then ptrace must be enabled */
644 BUG_ON(father != p->real_parent && !ptrace);
645
646 if (father == p->real_parent) {
647 /* reparent with a reaper, real father it's us */
d799f035 648 choose_new_parent(p, reaper);
1da177e4
LT
649 reparent_thread(p, father, 0);
650 } else {
651 /* reparent ptraced task to its real parent */
652 __ptrace_unlink (p);
653 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
654 thread_group_empty(p))
655 do_notify_parent(p, p->exit_signal);
656 }
657
658 /*
659 * if the ptraced child is a zombie with exit_signal == -1
660 * we must collect it before we exit, or it will remain
661 * zombie forever since we prevented it from self-reap itself
662 * while it was being traced by us, to be able to see it in wait4.
663 */
664 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
665 list_add(&p->ptrace_list, to_release);
666 }
667 list_for_each_safe(_p, _n, &father->ptrace_children) {
668 p = list_entry(_p,struct task_struct,ptrace_list);
d799f035 669 choose_new_parent(p, reaper);
1da177e4
LT
670 reparent_thread(p, father, 1);
671 }
672}
673
674/*
675 * Send signals to all our closest relatives so that they know
676 * to properly mourn us..
677 */
678static void exit_notify(struct task_struct *tsk)
679{
680 int state;
681 struct task_struct *t;
682 struct list_head ptrace_dead, *_p, *_n;
683
684 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
685 && !thread_group_empty(tsk)) {
686 /*
687 * This occurs when there was a race between our exit
688 * syscall and a group signal choosing us as the one to
689 * wake up. It could be that we are the only thread
690 * alerted to check for pending signals, but another thread
691 * should be woken now to take the signal since we will not.
692 * Now we'll wake all the threads in the group just to make
693 * sure someone gets all the pending signals.
694 */
695 read_lock(&tasklist_lock);
696 spin_lock_irq(&tsk->sighand->siglock);
697 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
698 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
699 recalc_sigpending_tsk(t);
700 if (signal_pending(t))
701 signal_wake_up(t, 0);
702 }
703 spin_unlock_irq(&tsk->sighand->siglock);
704 read_unlock(&tasklist_lock);
705 }
706
707 write_lock_irq(&tasklist_lock);
708
709 /*
710 * This does two things:
711 *
712 * A. Make init inherit all the child processes
713 * B. Check to see if any process groups have become orphaned
714 * as a result of our exiting, and if they have any stopped
715 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
716 */
717
718 INIT_LIST_HEAD(&ptrace_dead);
719 forget_original_parent(tsk, &ptrace_dead);
720 BUG_ON(!list_empty(&tsk->children));
721 BUG_ON(!list_empty(&tsk->ptrace_children));
722
723 /*
724 * Check to see if any process groups have become orphaned
725 * as a result of our exiting, and if they have any stopped
726 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
727 *
728 * Case i: Our father is in a different pgrp than we are
729 * and we were the only connection outside, so our pgrp
730 * is about to become orphaned.
731 */
732
733 t = tsk->real_parent;
734
735 if ((process_group(t) != process_group(tsk)) &&
736 (t->signal->session == tsk->signal->session) &&
737 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
738 has_stopped_jobs(process_group(tsk))) {
b67a1b9e
ON
739 __kill_pg_info(SIGHUP, SEND_SIG_PRIV, process_group(tsk));
740 __kill_pg_info(SIGCONT, SEND_SIG_PRIV, process_group(tsk));
1da177e4
LT
741 }
742
743 /* Let father know we died
744 *
745 * Thread signals are configurable, but you aren't going to use
746 * that to send signals to arbitary processes.
747 * That stops right now.
748 *
749 * If the parent exec id doesn't match the exec id we saved
750 * when we started then we know the parent has changed security
751 * domain.
752 *
753 * If our self_exec id doesn't match our parent_exec_id then
754 * we have changed execution domain as these two values started
755 * the same after a fork.
756 *
757 */
758
759 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
760 ( tsk->parent_exec_id != t->self_exec_id ||
761 tsk->self_exec_id != tsk->parent_exec_id)
762 && !capable(CAP_KILL))
763 tsk->exit_signal = SIGCHLD;
764
765
766 /* If something other than our normal parent is ptracing us, then
767 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
768 * only has special meaning to our real parent.
769 */
770 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
771 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
772 do_notify_parent(tsk, signal);
773 } else if (tsk->ptrace) {
774 do_notify_parent(tsk, SIGCHLD);
775 }
776
777 state = EXIT_ZOMBIE;
778 if (tsk->exit_signal == -1 &&
779 (likely(tsk->ptrace == 0) ||
780 unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
781 state = EXIT_DEAD;
782 tsk->exit_state = state;
783
784 write_unlock_irq(&tasklist_lock);
785
786 list_for_each_safe(_p, _n, &ptrace_dead) {
787 list_del_init(_p);
788 t = list_entry(_p,struct task_struct,ptrace_list);
789 release_task(t);
790 }
791
792 /* If the process is dead, release it - nobody will wait for it */
793 if (state == EXIT_DEAD)
794 release_task(tsk);
1da177e4
LT
795}
796
797fastcall NORET_TYPE void do_exit(long code)
798{
799 struct task_struct *tsk = current;
800 int group_dead;
801
802 profile_task_exit(tsk);
803
22e2c507
JA
804 WARN_ON(atomic_read(&tsk->fs_excl));
805
1da177e4
LT
806 if (unlikely(in_interrupt()))
807 panic("Aiee, killing interrupt handler!");
808 if (unlikely(!tsk->pid))
809 panic("Attempted to kill the idle task!");
fef23e7f 810 if (unlikely(tsk == child_reaper))
1da177e4 811 panic("Attempted to kill init!");
1da177e4
LT
812
813 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
814 current->ptrace_message = code;
815 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
816 }
817
df164db5
AN
818 /*
819 * We're taking recursive faults here in do_exit. Safest is to just
820 * leave this task alone and wait for reboot.
821 */
822 if (unlikely(tsk->flags & PF_EXITING)) {
823 printk(KERN_ALERT
824 "Fixing recursive fault but reboot is needed!\n");
afc847b7
AV
825 if (tsk->io_context)
826 exit_io_context();
df164db5
AN
827 set_current_state(TASK_UNINTERRUPTIBLE);
828 schedule();
829 }
830
1da177e4
LT
831 tsk->flags |= PF_EXITING;
832
a362f463
LT
833 /*
834 * Make sure we don't try to process any timer firings
835 * while we are already exiting.
836 */
837 tsk->it_virt_expires = cputime_zero;
838 tsk->it_prof_expires = cputime_zero;
839 tsk->it_sched_expires = 0;
840
1da177e4
LT
841 if (unlikely(in_atomic()))
842 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
843 current->comm, current->pid,
844 preempt_count());
845
846 acct_update_integrals(tsk);
365e9c87
HD
847 if (tsk->mm) {
848 update_hiwater_rss(tsk->mm);
849 update_hiwater_vm(tsk->mm);
850 }
1da177e4 851 group_dead = atomic_dec_and_test(&tsk->signal->live);
c3068951 852 if (group_dead) {
2ff678b8 853 hrtimer_cancel(&tsk->signal->real_timer);
25f407f0 854 exit_itimers(tsk->signal);
1da177e4 855 acct_process(code);
c3068951 856 }
0771dfef
IM
857 if (unlikely(tsk->robust_list))
858 exit_robust_list(tsk);
34f192c6
IM
859#ifdef CONFIG_COMPAT
860 if (unlikely(tsk->compat_robust_list))
861 compat_exit_robust_list(tsk);
862#endif
1da177e4
LT
863 exit_mm(tsk);
864
865 exit_sem(tsk);
866 __exit_files(tsk);
867 __exit_fs(tsk);
868 exit_namespace(tsk);
869 exit_thread();
870 cpuset_exit(tsk);
871 exit_keys(tsk);
872
873 if (group_dead && tsk->signal->leader)
874 disassociate_ctty(1);
875
a1261f54 876 module_put(task_thread_info(tsk)->exec_domain->module);
1da177e4
LT
877 if (tsk->binfmt)
878 module_put(tsk->binfmt->module);
879
880 tsk->exit_code = code;
9f46080c 881 proc_exit_connector(tsk);
1da177e4
LT
882 exit_notify(tsk);
883#ifdef CONFIG_NUMA
884 mpol_free(tsk->mempolicy);
885 tsk->mempolicy = NULL;
886#endif
de5097c2
IM
887 /*
888 * If DEBUG_MUTEXES is on, make sure we are holding no locks:
889 */
890 mutex_debug_check_no_locks_held(tsk);
1da177e4 891
afc847b7
AV
892 if (tsk->io_context)
893 exit_io_context();
894
7407251a
CQH
895 /* PF_DEAD causes final put_task_struct after we schedule. */
896 preempt_disable();
897 BUG_ON(tsk->flags & PF_DEAD);
898 tsk->flags |= PF_DEAD;
899
1da177e4
LT
900 schedule();
901 BUG();
902 /* Avoid "noreturn function does return". */
903 for (;;) ;
904}
905
012914da
RA
906EXPORT_SYMBOL_GPL(do_exit);
907
1da177e4
LT
908NORET_TYPE void complete_and_exit(struct completion *comp, long code)
909{
910 if (comp)
911 complete(comp);
912
913 do_exit(code);
914}
915
916EXPORT_SYMBOL(complete_and_exit);
917
918asmlinkage long sys_exit(int error_code)
919{
920 do_exit((error_code&0xff)<<8);
921}
922
923task_t fastcall *next_thread(const task_t *p)
924{
925 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
926}
927
928EXPORT_SYMBOL(next_thread);
929
930/*
931 * Take down every thread in the group. This is called by fatal signals
932 * as well as by sys_exit_group (below).
933 */
934NORET_TYPE void
935do_group_exit(int exit_code)
936{
937 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
938
939 if (current->signal->flags & SIGNAL_GROUP_EXIT)
940 exit_code = current->signal->group_exit_code;
941 else if (!thread_group_empty(current)) {
942 struct signal_struct *const sig = current->signal;
943 struct sighand_struct *const sighand = current->sighand;
944 read_lock(&tasklist_lock);
945 spin_lock_irq(&sighand->siglock);
946 if (sig->flags & SIGNAL_GROUP_EXIT)
947 /* Another thread got here before we took the lock. */
948 exit_code = sig->group_exit_code;
949 else {
1da177e4
LT
950 sig->group_exit_code = exit_code;
951 zap_other_threads(current);
952 }
953 spin_unlock_irq(&sighand->siglock);
954 read_unlock(&tasklist_lock);
955 }
956
957 do_exit(exit_code);
958 /* NOTREACHED */
959}
960
961/*
962 * this kills every thread in the thread group. Note that any externally
963 * wait4()-ing process will get the correct exit code - even if this
964 * thread is not the thread group leader.
965 */
966asmlinkage void sys_exit_group(int error_code)
967{
968 do_group_exit((error_code & 0xff) << 8);
969}
970
971static int eligible_child(pid_t pid, int options, task_t *p)
972{
973 if (pid > 0) {
974 if (p->pid != pid)
975 return 0;
976 } else if (!pid) {
977 if (process_group(p) != process_group(current))
978 return 0;
979 } else if (pid != -1) {
980 if (process_group(p) != -pid)
981 return 0;
982 }
983
984 /*
985 * Do not consider detached threads that are
986 * not ptraced:
987 */
988 if (p->exit_signal == -1 && !p->ptrace)
989 return 0;
990
991 /* Wait for all children (clone and not) if __WALL is set;
992 * otherwise, wait for clone children *only* if __WCLONE is
993 * set; otherwise, wait for non-clone children *only*. (Note:
994 * A "clone" child here is one that reports to its parent
995 * using a signal other than SIGCHLD.) */
996 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
997 && !(options & __WALL))
998 return 0;
999 /*
1000 * Do not consider thread group leaders that are
1001 * in a non-empty thread group:
1002 */
1003 if (current->tgid != p->tgid && delay_group_leader(p))
1004 return 2;
1005
1006 if (security_task_wait(p))
1007 return 0;
1008
1009 return 1;
1010}
1011
1012static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
1013 int why, int status,
1014 struct siginfo __user *infop,
1015 struct rusage __user *rusagep)
1016{
1017 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
1018 put_task_struct(p);
1019 if (!retval)
1020 retval = put_user(SIGCHLD, &infop->si_signo);
1021 if (!retval)
1022 retval = put_user(0, &infop->si_errno);
1023 if (!retval)
1024 retval = put_user((short)why, &infop->si_code);
1025 if (!retval)
1026 retval = put_user(pid, &infop->si_pid);
1027 if (!retval)
1028 retval = put_user(uid, &infop->si_uid);
1029 if (!retval)
1030 retval = put_user(status, &infop->si_status);
1031 if (!retval)
1032 retval = pid;
1033 return retval;
1034}
1035
1036/*
1037 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1038 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1039 * the lock and this task is uninteresting. If we return nonzero, we have
1040 * released the lock and the system call should return.
1041 */
1042static int wait_task_zombie(task_t *p, int noreap,
1043 struct siginfo __user *infop,
1044 int __user *stat_addr, struct rusage __user *ru)
1045{
1046 unsigned long state;
1047 int retval;
1048 int status;
1049
1050 if (unlikely(noreap)) {
1051 pid_t pid = p->pid;
1052 uid_t uid = p->uid;
1053 int exit_code = p->exit_code;
1054 int why, status;
1055
1056 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1057 return 0;
1058 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1059 return 0;
1060 get_task_struct(p);
1061 read_unlock(&tasklist_lock);
1062 if ((exit_code & 0x7f) == 0) {
1063 why = CLD_EXITED;
1064 status = exit_code >> 8;
1065 } else {
1066 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1067 status = exit_code & 0x7f;
1068 }
1069 return wait_noreap_copyout(p, pid, uid, why,
1070 status, infop, ru);
1071 }
1072
1073 /*
1074 * Try to move the task's state to DEAD
1075 * only one thread is allowed to do this:
1076 */
1077 state = xchg(&p->exit_state, EXIT_DEAD);
1078 if (state != EXIT_ZOMBIE) {
1079 BUG_ON(state != EXIT_DEAD);
1080 return 0;
1081 }
1082 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1083 /*
1084 * This can only happen in a race with a ptraced thread
1085 * dying on another processor.
1086 */
1087 return 0;
1088 }
1089
1090 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
3795e161
JJ
1091 struct signal_struct *psig;
1092 struct signal_struct *sig;
1093
1da177e4
LT
1094 /*
1095 * The resource counters for the group leader are in its
1096 * own task_struct. Those for dead threads in the group
1097 * are in its signal_struct, as are those for the child
1098 * processes it has previously reaped. All these
1099 * accumulate in the parent's signal_struct c* fields.
1100 *
1101 * We don't bother to take a lock here to protect these
1102 * p->signal fields, because they are only touched by
1103 * __exit_signal, which runs with tasklist_lock
1104 * write-locked anyway, and so is excluded here. We do
1105 * need to protect the access to p->parent->signal fields,
1106 * as other threads in the parent group can be right
1107 * here reaping other children at the same time.
1108 */
1109 spin_lock_irq(&p->parent->sighand->siglock);
3795e161
JJ
1110 psig = p->parent->signal;
1111 sig = p->signal;
1112 psig->cutime =
1113 cputime_add(psig->cutime,
1da177e4 1114 cputime_add(p->utime,
3795e161
JJ
1115 cputime_add(sig->utime,
1116 sig->cutime)));
1117 psig->cstime =
1118 cputime_add(psig->cstime,
1da177e4 1119 cputime_add(p->stime,
3795e161
JJ
1120 cputime_add(sig->stime,
1121 sig->cstime)));
1122 psig->cmin_flt +=
1123 p->min_flt + sig->min_flt + sig->cmin_flt;
1124 psig->cmaj_flt +=
1125 p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1126 psig->cnvcsw +=
1127 p->nvcsw + sig->nvcsw + sig->cnvcsw;
1128 psig->cnivcsw +=
1129 p->nivcsw + sig->nivcsw + sig->cnivcsw;
1da177e4
LT
1130 spin_unlock_irq(&p->parent->sighand->siglock);
1131 }
1132
1133 /*
1134 * Now we are sure this task is interesting, and no other
1135 * thread can reap it because we set its state to EXIT_DEAD.
1136 */
1137 read_unlock(&tasklist_lock);
1138
1139 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1140 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1141 ? p->signal->group_exit_code : p->exit_code;
1142 if (!retval && stat_addr)
1143 retval = put_user(status, stat_addr);
1144 if (!retval && infop)
1145 retval = put_user(SIGCHLD, &infop->si_signo);
1146 if (!retval && infop)
1147 retval = put_user(0, &infop->si_errno);
1148 if (!retval && infop) {
1149 int why;
1150
1151 if ((status & 0x7f) == 0) {
1152 why = CLD_EXITED;
1153 status >>= 8;
1154 } else {
1155 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1156 status &= 0x7f;
1157 }
1158 retval = put_user((short)why, &infop->si_code);
1159 if (!retval)
1160 retval = put_user(status, &infop->si_status);
1161 }
1162 if (!retval && infop)
1163 retval = put_user(p->pid, &infop->si_pid);
1164 if (!retval && infop)
1165 retval = put_user(p->uid, &infop->si_uid);
1166 if (retval) {
1167 // TODO: is this safe?
1168 p->exit_state = EXIT_ZOMBIE;
1169 return retval;
1170 }
1171 retval = p->pid;
1172 if (p->real_parent != p->parent) {
1173 write_lock_irq(&tasklist_lock);
1174 /* Double-check with lock held. */
1175 if (p->real_parent != p->parent) {
1176 __ptrace_unlink(p);
1177 // TODO: is this safe?
1178 p->exit_state = EXIT_ZOMBIE;
1179 /*
1180 * If this is not a detached task, notify the parent.
1181 * If it's still not detached after that, don't release
1182 * it now.
1183 */
1184 if (p->exit_signal != -1) {
1185 do_notify_parent(p, p->exit_signal);
1186 if (p->exit_signal != -1)
1187 p = NULL;
1188 }
1189 }
1190 write_unlock_irq(&tasklist_lock);
1191 }
1192 if (p != NULL)
1193 release_task(p);
1194 BUG_ON(!retval);
1195 return retval;
1196}
1197
1198/*
1199 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1200 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1201 * the lock and this task is uninteresting. If we return nonzero, we have
1202 * released the lock and the system call should return.
1203 */
1204static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1205 struct siginfo __user *infop,
1206 int __user *stat_addr, struct rusage __user *ru)
1207{
1208 int retval, exit_code;
1209
1210 if (!p->exit_code)
1211 return 0;
1212 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1213 p->signal && p->signal->group_stop_count > 0)
1214 /*
1215 * A group stop is in progress and this is the group leader.
1216 * We won't report until all threads have stopped.
1217 */
1218 return 0;
1219
1220 /*
1221 * Now we are pretty sure this task is interesting.
1222 * Make sure it doesn't get reaped out from under us while we
1223 * give up the lock and then examine it below. We don't want to
1224 * keep holding onto the tasklist_lock while we call getrusage and
1225 * possibly take page faults for user memory.
1226 */
1227 get_task_struct(p);
1228 read_unlock(&tasklist_lock);
1229
1230 if (unlikely(noreap)) {
1231 pid_t pid = p->pid;
1232 uid_t uid = p->uid;
1233 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1234
1235 exit_code = p->exit_code;
1236 if (unlikely(!exit_code) ||
14bf01bb 1237 unlikely(p->state & TASK_TRACED))
1da177e4
LT
1238 goto bail_ref;
1239 return wait_noreap_copyout(p, pid, uid,
1240 why, (exit_code << 8) | 0x7f,
1241 infop, ru);
1242 }
1243
1244 write_lock_irq(&tasklist_lock);
1245
1246 /*
1247 * This uses xchg to be atomic with the thread resuming and setting
1248 * it. It must also be done with the write lock held to prevent a
1249 * race with the EXIT_ZOMBIE case.
1250 */
1251 exit_code = xchg(&p->exit_code, 0);
1252 if (unlikely(p->exit_state)) {
1253 /*
1254 * The task resumed and then died. Let the next iteration
1255 * catch it in EXIT_ZOMBIE. Note that exit_code might
1256 * already be zero here if it resumed and did _exit(0).
1257 * The task itself is dead and won't touch exit_code again;
1258 * other processors in this function are locked out.
1259 */
1260 p->exit_code = exit_code;
1261 exit_code = 0;
1262 }
1263 if (unlikely(exit_code == 0)) {
1264 /*
1265 * Another thread in this function got to it first, or it
1266 * resumed, or it resumed and then died.
1267 */
1268 write_unlock_irq(&tasklist_lock);
1269bail_ref:
1270 put_task_struct(p);
1271 /*
1272 * We are returning to the wait loop without having successfully
1273 * removed the process and having released the lock. We cannot
1274 * continue, since the "p" task pointer is potentially stale.
1275 *
1276 * Return -EAGAIN, and do_wait() will restart the loop from the
1277 * beginning. Do _not_ re-acquire the lock.
1278 */
1279 return -EAGAIN;
1280 }
1281
1282 /* move to end of parent's list to avoid starvation */
1283 remove_parent(p);
1284 add_parent(p, p->parent);
1285
1286 write_unlock_irq(&tasklist_lock);
1287
1288 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1289 if (!retval && stat_addr)
1290 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1291 if (!retval && infop)
1292 retval = put_user(SIGCHLD, &infop->si_signo);
1293 if (!retval && infop)
1294 retval = put_user(0, &infop->si_errno);
1295 if (!retval && infop)
1296 retval = put_user((short)((p->ptrace & PT_PTRACED)
1297 ? CLD_TRAPPED : CLD_STOPPED),
1298 &infop->si_code);
1299 if (!retval && infop)
1300 retval = put_user(exit_code, &infop->si_status);
1301 if (!retval && infop)
1302 retval = put_user(p->pid, &infop->si_pid);
1303 if (!retval && infop)
1304 retval = put_user(p->uid, &infop->si_uid);
1305 if (!retval)
1306 retval = p->pid;
1307 put_task_struct(p);
1308
1309 BUG_ON(!retval);
1310 return retval;
1311}
1312
1313/*
1314 * Handle do_wait work for one task in a live, non-stopped state.
1315 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1316 * the lock and this task is uninteresting. If we return nonzero, we have
1317 * released the lock and the system call should return.
1318 */
1319static int wait_task_continued(task_t *p, int noreap,
1320 struct siginfo __user *infop,
1321 int __user *stat_addr, struct rusage __user *ru)
1322{
1323 int retval;
1324 pid_t pid;
1325 uid_t uid;
1326
1327 if (unlikely(!p->signal))
1328 return 0;
1329
1330 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1331 return 0;
1332
1333 spin_lock_irq(&p->sighand->siglock);
1334 /* Re-check with the lock held. */
1335 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1336 spin_unlock_irq(&p->sighand->siglock);
1337 return 0;
1338 }
1339 if (!noreap)
1340 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1341 spin_unlock_irq(&p->sighand->siglock);
1342
1343 pid = p->pid;
1344 uid = p->uid;
1345 get_task_struct(p);
1346 read_unlock(&tasklist_lock);
1347
1348 if (!infop) {
1349 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1350 put_task_struct(p);
1351 if (!retval && stat_addr)
1352 retval = put_user(0xffff, stat_addr);
1353 if (!retval)
1354 retval = p->pid;
1355 } else {
1356 retval = wait_noreap_copyout(p, pid, uid,
1357 CLD_CONTINUED, SIGCONT,
1358 infop, ru);
1359 BUG_ON(retval == 0);
1360 }
1361
1362 return retval;
1363}
1364
1365
1366static inline int my_ptrace_child(struct task_struct *p)
1367{
1368 if (!(p->ptrace & PT_PTRACED))
1369 return 0;
1370 if (!(p->ptrace & PT_ATTACHED))
1371 return 1;
1372 /*
1373 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1374 * we are the attacher. If we are the real parent, this is a race
1375 * inside ptrace_attach. It is waiting for the tasklist_lock,
1376 * which we have to switch the parent links, but has already set
1377 * the flags in p->ptrace.
1378 */
1379 return (p->parent != p->real_parent);
1380}
1381
1382static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1383 int __user *stat_addr, struct rusage __user *ru)
1384{
1385 DECLARE_WAITQUEUE(wait, current);
1386 struct task_struct *tsk;
1387 int flag, retval;
1388
1389 add_wait_queue(&current->signal->wait_chldexit,&wait);
1390repeat:
1391 /*
1392 * We will set this flag if we see any child that might later
1393 * match our criteria, even if we are not able to reap it yet.
1394 */
1395 flag = 0;
1396 current->state = TASK_INTERRUPTIBLE;
1397 read_lock(&tasklist_lock);
1398 tsk = current;
1399 do {
1400 struct task_struct *p;
1401 struct list_head *_p;
1402 int ret;
1403
1404 list_for_each(_p,&tsk->children) {
1405 p = list_entry(_p,struct task_struct,sibling);
1406
1407 ret = eligible_child(pid, options, p);
1408 if (!ret)
1409 continue;
1410
1411 switch (p->state) {
1412 case TASK_TRACED:
7f2a5255
RM
1413 /*
1414 * When we hit the race with PTRACE_ATTACH,
1415 * we will not report this child. But the
1416 * race means it has not yet been moved to
1417 * our ptrace_children list, so we need to
1418 * set the flag here to avoid a spurious ECHILD
1419 * when the race happens with the only child.
1420 */
1421 flag = 1;
1da177e4
LT
1422 if (!my_ptrace_child(p))
1423 continue;
1424 /*FALLTHROUGH*/
1425 case TASK_STOPPED:
1426 /*
1427 * It's stopped now, so it might later
1428 * continue, exit, or stop again.
1429 */
1430 flag = 1;
1431 if (!(options & WUNTRACED) &&
1432 !my_ptrace_child(p))
1433 continue;
1434 retval = wait_task_stopped(p, ret == 2,
1435 (options & WNOWAIT),
1436 infop,
1437 stat_addr, ru);
1438 if (retval == -EAGAIN)
1439 goto repeat;
1440 if (retval != 0) /* He released the lock. */
1441 goto end;
1442 break;
1443 default:
1444 // case EXIT_DEAD:
1445 if (p->exit_state == EXIT_DEAD)
1446 continue;
1447 // case EXIT_ZOMBIE:
1448 if (p->exit_state == EXIT_ZOMBIE) {
1449 /*
1450 * Eligible but we cannot release
1451 * it yet:
1452 */
1453 if (ret == 2)
1454 goto check_continued;
1455 if (!likely(options & WEXITED))
1456 continue;
1457 retval = wait_task_zombie(
1458 p, (options & WNOWAIT),
1459 infop, stat_addr, ru);
1460 /* He released the lock. */
1461 if (retval != 0)
1462 goto end;
1463 break;
1464 }
1465check_continued:
1466 /*
1467 * It's running now, so it might later
1468 * exit, stop, or stop and then continue.
1469 */
1470 flag = 1;
1471 if (!unlikely(options & WCONTINUED))
1472 continue;
1473 retval = wait_task_continued(
1474 p, (options & WNOWAIT),
1475 infop, stat_addr, ru);
1476 if (retval != 0) /* He released the lock. */
1477 goto end;
1478 break;
1479 }
1480 }
1481 if (!flag) {
1482 list_for_each(_p, &tsk->ptrace_children) {
1483 p = list_entry(_p, struct task_struct,
1484 ptrace_list);
1485 if (!eligible_child(pid, options, p))
1486 continue;
1487 flag = 1;
1488 break;
1489 }
1490 }
1491 if (options & __WNOTHREAD)
1492 break;
1493 tsk = next_thread(tsk);
1494 if (tsk->signal != current->signal)
1495 BUG();
1496 } while (tsk != current);
1497
1498 read_unlock(&tasklist_lock);
1499 if (flag) {
1500 retval = 0;
1501 if (options & WNOHANG)
1502 goto end;
1503 retval = -ERESTARTSYS;
1504 if (signal_pending(current))
1505 goto end;
1506 schedule();
1507 goto repeat;
1508 }
1509 retval = -ECHILD;
1510end:
1511 current->state = TASK_RUNNING;
1512 remove_wait_queue(&current->signal->wait_chldexit,&wait);
1513 if (infop) {
1514 if (retval > 0)
1515 retval = 0;
1516 else {
1517 /*
1518 * For a WNOHANG return, clear out all the fields
1519 * we would set so the user can easily tell the
1520 * difference.
1521 */
1522 if (!retval)
1523 retval = put_user(0, &infop->si_signo);
1524 if (!retval)
1525 retval = put_user(0, &infop->si_errno);
1526 if (!retval)
1527 retval = put_user(0, &infop->si_code);
1528 if (!retval)
1529 retval = put_user(0, &infop->si_pid);
1530 if (!retval)
1531 retval = put_user(0, &infop->si_uid);
1532 if (!retval)
1533 retval = put_user(0, &infop->si_status);
1534 }
1535 }
1536 return retval;
1537}
1538
1539asmlinkage long sys_waitid(int which, pid_t pid,
1540 struct siginfo __user *infop, int options,
1541 struct rusage __user *ru)
1542{
1543 long ret;
1544
1545 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1546 return -EINVAL;
1547 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1548 return -EINVAL;
1549
1550 switch (which) {
1551 case P_ALL:
1552 pid = -1;
1553 break;
1554 case P_PID:
1555 if (pid <= 0)
1556 return -EINVAL;
1557 break;
1558 case P_PGID:
1559 if (pid <= 0)
1560 return -EINVAL;
1561 pid = -pid;
1562 break;
1563 default:
1564 return -EINVAL;
1565 }
1566
1567 ret = do_wait(pid, options, infop, NULL, ru);
1568
1569 /* avoid REGPARM breakage on x86: */
1570 prevent_tail_call(ret);
1571 return ret;
1572}
1573
1574asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1575 int options, struct rusage __user *ru)
1576{
1577 long ret;
1578
1579 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1580 __WNOTHREAD|__WCLONE|__WALL))
1581 return -EINVAL;
1582 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1583
1584 /* avoid REGPARM breakage on x86: */
1585 prevent_tail_call(ret);
1586 return ret;
1587}
1588
1589#ifdef __ARCH_WANT_SYS_WAITPID
1590
1591/*
1592 * sys_waitpid() remains for compatibility. waitpid() should be
1593 * implemented by calling sys_wait4() from libc.a.
1594 */
1595asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1596{
1597 return sys_wait4(pid, stat_addr, options, NULL);
1598}
1599
1600#endif