ia64/linux-2.6.18-xen.hg

annotate kernel/exit.c @ 798:b02a90bf5bbc

ACPI: Backport missing part for T-State MSR support

Part of below kernel commit was missed while packporting T-State
support.

commit f79f06ab9f86d7203006d2ec8992ac80df36a34e
Author: Zhao Yakui <yakui.zhao@intel.com>
Date: Thu Nov 15 17:06:36 2007 +0800

ACPI: Enable MSR (FixedHW) support for T-States

Add throttling control via MSR when T-states uses
the FixHW Control Status registers.

Signed-off-by: Zhao Yakui <yakui.zhao@intel.com>
Signed-off-by: Li Shaohua <shaohua.li@intel.com>
Signed-off-by: Len Brown <len.brown@intel.com>

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