Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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