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