ckrm_E16rc1 mem controller version 1
[linux-2.6.git] / kernel / exit.c.orig
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/security.h>
18 #include <linux/acct.h>
19 #include <linux/file.h>
20 #include <linux/binfmts.h>
21 #include <linux/ptrace.h>
22 #include <linux/profile.h>
23 #include <linux/mount.h>
24 #include <linux/proc_fs.h>
25 #include <linux/mempolicy.h>
26 #include <linux/ckrm.h>
27 #include <linux/ckrm_tsk.h>
28
29 #include <asm/uaccess.h>
30 #include <asm/unistd.h>
31 #include <asm/pgtable.h>
32 #include <asm/mmu_context.h>
33
34 extern void sem_exit (void);
35 extern struct task_struct *child_reaper;
36
37 int getrusage(struct task_struct *, int, struct rusage __user *);
38
39 static void __unhash_process(struct task_struct *p)
40 {
41         nr_threads--;
42         detach_pid(p, PIDTYPE_PID);
43         detach_pid(p, PIDTYPE_TGID);
44         if (thread_group_leader(p)) {
45                 detach_pid(p, PIDTYPE_PGID);
46                 detach_pid(p, PIDTYPE_SID);
47                 if (p->pid)
48                         __get_cpu_var(process_counts)--;
49         }
50
51         REMOVE_LINKS(p);
52 }
53
54 void release_task(struct task_struct * p)
55 {
56         int zap_leader;
57         task_t *leader;
58         struct dentry *proc_dentry;
59
60 repeat: 
61         BUG_ON(p->state < TASK_ZOMBIE);
62  
63         atomic_dec(&p->user->processes);
64         spin_lock(&p->proc_lock);
65         proc_dentry = proc_pid_unhash(p);
66         write_lock_irq(&tasklist_lock);
67         if (unlikely(p->ptrace))
68                 __ptrace_unlink(p);
69         BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
70         __exit_signal(p);
71         __exit_sighand(p);
72         __unhash_process(p);
73
74         /*
75          * If we are the last non-leader member of the thread
76          * group, and the leader is zombie, then notify the
77          * group leader's parent process. (if it wants notification.)
78          */
79         zap_leader = 0;
80         leader = p->group_leader;
81         if (leader != p && thread_group_empty(leader) && leader->state == TASK_ZOMBIE) {
82                 BUG_ON(leader->exit_signal == -1);
83                 do_notify_parent(leader, leader->exit_signal);
84                 /*
85                  * If we were the last child thread and the leader has
86                  * exited already, and the leader's parent ignores SIGCHLD,
87                  * then we are the one who should release the leader.
88                  *
89                  * do_notify_parent() will have marked it self-reaping in
90                  * that case.
91                  */
92                 zap_leader = (leader->exit_signal == -1);
93         }
94
95         p->parent->cutime += p->utime + p->cutime;
96         p->parent->cstime += p->stime + p->cstime;
97         p->parent->cmin_flt += p->min_flt + p->cmin_flt;
98         p->parent->cmaj_flt += p->maj_flt + p->cmaj_flt;
99         p->parent->cnvcsw += p->nvcsw + p->cnvcsw;
100         p->parent->cnivcsw += p->nivcsw + p->cnivcsw;
101         sched_exit(p);
102         write_unlock_irq(&tasklist_lock);
103         spin_unlock(&p->proc_lock);
104         proc_pid_flush(proc_dentry);
105         release_thread(p);
106         put_task_struct(p);
107
108         p = leader;
109         if (unlikely(zap_leader))
110                 goto repeat;
111 }
112
113 /* we are using it only for SMP init */
114
115 void unhash_process(struct task_struct *p)
116 {
117         struct dentry *proc_dentry;
118
119         spin_lock(&p->proc_lock);
120         proc_dentry = proc_pid_unhash(p);
121         write_lock_irq(&tasklist_lock);
122         __unhash_process(p);
123         write_unlock_irq(&tasklist_lock);
124         spin_unlock(&p->proc_lock);
125         proc_pid_flush(proc_dentry);
126 }
127
128 /*
129  * This checks not only the pgrp, but falls back on the pid if no
130  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
131  * without this...
132  */
133 int session_of_pgrp(int pgrp)
134 {
135         struct task_struct *p;
136         struct list_head *l;
137         struct pid *pid;
138         int sid = -1;
139
140         read_lock(&tasklist_lock);
141         for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid)
142                 if (p->signal->session > 0) {
143                         sid = p->signal->session;
144                         goto out;
145                 }
146         p = find_task_by_pid(pgrp);
147         if (p)
148                 sid = p->signal->session;
149 out:
150         read_unlock(&tasklist_lock);
151         
152         return sid;
153 }
154
155 /*
156  * Determine if a process group is "orphaned", according to the POSIX
157  * definition in 2.2.2.52.  Orphaned process groups are not to be affected
158  * by terminal-generated stop signals.  Newly orphaned process groups are
159  * to receive a SIGHUP and a SIGCONT.
160  *
161  * "I ask you, have you ever known what it is to be an orphan?"
162  */
163 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
164 {
165         struct task_struct *p;
166         struct list_head *l;
167         struct pid *pid;
168         int ret = 1;
169
170         for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
171                 if (p == ignored_task
172                                 || p->state >= TASK_ZOMBIE 
173                                 || p->real_parent->pid == 1)
174                         continue;
175                 if (process_group(p->real_parent) != pgrp
176                             && p->real_parent->signal->session == p->signal->session) {
177                         ret = 0;
178                         break;
179                 }
180         }
181         return ret;     /* (sighing) "Often!" */
182 }
183
184 int is_orphaned_pgrp(int pgrp)
185 {
186         int retval;
187
188         read_lock(&tasklist_lock);
189         retval = will_become_orphaned_pgrp(pgrp, NULL);
190         read_unlock(&tasklist_lock);
191
192         return retval;
193 }
194
195 static inline int has_stopped_jobs(int pgrp)
196 {
197         int retval = 0;
198         struct task_struct *p;
199         struct list_head *l;
200         struct pid *pid;
201
202         for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
203                 if (p->state != TASK_STOPPED)
204                         continue;
205
206                 /* If p is stopped by a debugger on a signal that won't
207                    stop it, then don't count p as stopped.  This isn't
208                    perfect but it's a good approximation.  */
209                 if (unlikely (p->ptrace)
210                     && p->exit_code != SIGSTOP
211                     && p->exit_code != SIGTSTP
212                     && p->exit_code != SIGTTOU
213                     && p->exit_code != SIGTTIN)
214                         continue;
215
216                 retval = 1;
217                 break;
218         }
219         return retval;
220 }
221
222 /**
223  * reparent_to_init() - Reparent the calling kernel thread to the init task.
224  *
225  * If a kernel thread is launched as a result of a system call, or if
226  * it ever exits, it should generally reparent itself to init so that
227  * it is correctly cleaned up on exit.
228  *
229  * The various task state such as scheduling policy and priority may have
230  * been inherited from a user process, so we reset them to sane values here.
231  *
232  * NOTE that reparent_to_init() gives the caller full capabilities.
233  */
234 void reparent_to_init(void)
235 {
236         write_lock_irq(&tasklist_lock);
237
238         ptrace_unlink(current);
239         /* Reparent to init */
240         REMOVE_LINKS(current);
241         current->parent = child_reaper;
242         current->real_parent = child_reaper;
243         SET_LINKS(current);
244
245         /* Set the exit signal to SIGCHLD so we signal init on exit */
246         current->exit_signal = SIGCHLD;
247
248         if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
249                 set_user_nice(current, 0);
250         /* cpus_allowed? */
251         /* rt_priority? */
252         /* signals? */
253         security_task_reparent_to_init(current);
254         memcpy(current->rlim, init_task.rlim, sizeof(*(current->rlim)));
255         atomic_inc(&(INIT_USER->__count));
256         switch_uid(INIT_USER);
257
258         write_unlock_irq(&tasklist_lock);
259 }
260
261 void __set_special_pids(pid_t session, pid_t pgrp)
262 {
263         struct task_struct *curr = current;
264
265         if (curr->signal->session != session) {
266                 detach_pid(curr, PIDTYPE_SID);
267                 curr->signal->session = session;
268                 attach_pid(curr, PIDTYPE_SID, session);
269         }
270         if (process_group(curr) != pgrp) {
271                 detach_pid(curr, PIDTYPE_PGID);
272                 curr->signal->pgrp = pgrp;
273                 attach_pid(curr, PIDTYPE_PGID, pgrp);
274         }
275 }
276
277 void set_special_pids(pid_t session, pid_t pgrp)
278 {
279         write_lock_irq(&tasklist_lock);
280         __set_special_pids(session, pgrp);
281         write_unlock_irq(&tasklist_lock);
282 }
283
284 /*
285  * Let kernel threads use this to say that they
286  * allow a certain signal (since daemonize() will
287  * have disabled all of them by default).
288  */
289 int allow_signal(int sig)
290 {
291         if (sig < 1 || sig > _NSIG)
292                 return -EINVAL;
293
294         spin_lock_irq(&current->sighand->siglock);
295         sigdelset(&current->blocked, sig);
296         if (!current->mm) {
297                 /* Kernel threads handle their own signals.
298                    Let the signal code know it'll be handled, so
299                    that they don't get converted to SIGKILL or
300                    just silently dropped */
301                 current->sighand->action[(sig)-1].sa.sa_handler = (void *)2;
302         }
303         recalc_sigpending();
304         spin_unlock_irq(&current->sighand->siglock);
305         return 0;
306 }
307
308 EXPORT_SYMBOL(allow_signal);
309
310 int disallow_signal(int sig)
311 {
312         if (sig < 1 || sig > _NSIG)
313                 return -EINVAL;
314
315         spin_lock_irq(&current->sighand->siglock);
316         sigaddset(&current->blocked, sig);
317         recalc_sigpending();
318         spin_unlock_irq(&current->sighand->siglock);
319         return 0;
320 }
321
322 EXPORT_SYMBOL(disallow_signal);
323
324 /*
325  *      Put all the gunge required to become a kernel thread without
326  *      attached user resources in one place where it belongs.
327  */
328
329 void daemonize(const char *name, ...)
330 {
331         va_list args;
332         struct fs_struct *fs;
333         sigset_t blocked;
334
335         va_start(args, name);
336         vsnprintf(current->comm, sizeof(current->comm), name, args);
337         va_end(args);
338
339         /*
340          * If we were started as result of loading a module, close all of the
341          * user space pages.  We don't need them, and if we didn't close them
342          * they would be locked into memory.
343          */
344         exit_mm(current);
345
346         set_special_pids(1, 1);
347         current->signal->tty = NULL;
348
349         /* Block and flush all signals */
350         sigfillset(&blocked);
351         sigprocmask(SIG_BLOCK, &blocked, NULL);
352         flush_signals(current);
353
354         /* Become as one with the init task */
355
356         exit_fs(current);       /* current->fs->count--; */
357         fs = init_task.fs;
358         current->fs = fs;
359         atomic_inc(&fs->count);
360         exit_files(current);
361         current->files = init_task.files;
362         atomic_inc(&current->files->count);
363
364         reparent_to_init();
365 }
366
367 EXPORT_SYMBOL(daemonize);
368
369 static inline void close_files(struct files_struct * files)
370 {
371         int i, j;
372
373         j = 0;
374         for (;;) {
375                 unsigned long set;
376                 i = j * __NFDBITS;
377                 if (i >= files->max_fdset || i >= files->max_fds)
378                         break;
379                 set = files->open_fds->fds_bits[j++];
380                 while (set) {
381                         if (set & 1) {
382                                 struct file * file = xchg(&files->fd[i], NULL);
383                                 if (file)
384                                         filp_close(file, files);
385                         }
386                         i++;
387                         set >>= 1;
388                 }
389         }
390 }
391
392 struct files_struct *get_files_struct(struct task_struct *task)
393 {
394         struct files_struct *files;
395
396         task_lock(task);
397         files = task->files;
398         if (files)
399                 atomic_inc(&files->count);
400         task_unlock(task);
401
402         return files;
403 }
404
405 void fastcall put_files_struct(struct files_struct *files)
406 {
407         if (atomic_dec_and_test(&files->count)) {
408                 close_files(files);
409                 /*
410                  * Free the fd and fdset arrays if we expanded them.
411                  */
412                 if (files->fd != &files->fd_array[0])
413                         free_fd_array(files->fd, files->max_fds);
414                 if (files->max_fdset > __FD_SETSIZE) {
415                         free_fdset(files->open_fds, files->max_fdset);
416                         free_fdset(files->close_on_exec, files->max_fdset);
417                 }
418                 kmem_cache_free(files_cachep, files);
419         }
420 }
421
422 EXPORT_SYMBOL(put_files_struct);
423
424 static inline void __exit_files(struct task_struct *tsk)
425 {
426         struct files_struct * files = tsk->files;
427
428         if (files) {
429                 task_lock(tsk);
430                 tsk->files = NULL;
431                 task_unlock(tsk);
432                 put_files_struct(files);
433         }
434 }
435
436 void exit_files(struct task_struct *tsk)
437 {
438         __exit_files(tsk);
439 }
440
441 static inline void __put_fs_struct(struct fs_struct *fs)
442 {
443         /* No need to hold fs->lock if we are killing it */
444         if (atomic_dec_and_test(&fs->count)) {
445                 dput(fs->root);
446                 mntput(fs->rootmnt);
447                 dput(fs->pwd);
448                 mntput(fs->pwdmnt);
449                 if (fs->altroot) {
450                         dput(fs->altroot);
451                         mntput(fs->altrootmnt);
452                 }
453                 kmem_cache_free(fs_cachep, fs);
454         }
455 }
456
457 void put_fs_struct(struct fs_struct *fs)
458 {
459         __put_fs_struct(fs);
460 }
461
462 static inline void __exit_fs(struct task_struct *tsk)
463 {
464         struct fs_struct * fs = tsk->fs;
465
466         if (fs) {
467                 task_lock(tsk);
468                 tsk->fs = NULL;
469                 task_unlock(tsk);
470                 __put_fs_struct(fs);
471         }
472 }
473
474 void exit_fs(struct task_struct *tsk)
475 {
476         __exit_fs(tsk);
477 }
478
479 EXPORT_SYMBOL_GPL(exit_fs);
480
481 /*
482  * Turn us into a lazy TLB process if we
483  * aren't already..
484  */
485 static inline void __exit_mm(struct task_struct * tsk)
486 {
487         struct mm_struct *mm = tsk->mm;
488
489         mm_release(tsk, mm);
490         if (!mm)
491                 return;
492         /*
493          * Serialize with any possible pending coredump.
494          * We must hold mmap_sem around checking core_waiters
495          * and clearing tsk->mm.  The core-inducing thread
496          * will increment core_waiters for each thread in the
497          * group with ->mm != NULL.
498          */
499         down_read(&mm->mmap_sem);
500         if (mm->core_waiters) {
501                 up_read(&mm->mmap_sem);
502                 down_write(&mm->mmap_sem);
503                 if (!--mm->core_waiters)
504                         complete(mm->core_startup_done);
505                 up_write(&mm->mmap_sem);
506
507                 wait_for_completion(&mm->core_done);
508                 down_read(&mm->mmap_sem);
509         }
510         atomic_inc(&mm->mm_count);
511         if (mm != tsk->active_mm) BUG();
512         /* more a memory barrier than a real lock */
513         task_lock(tsk);
514         tsk->mm = NULL;
515         up_read(&mm->mmap_sem);
516         enter_lazy_tlb(mm, current);
517         task_unlock(tsk);
518         mmput(mm);
519 }
520
521 void exit_mm(struct task_struct *tsk)
522 {
523         __exit_mm(tsk);
524 }
525
526 EXPORT_SYMBOL(exit_mm);
527
528 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
529 {
530         /*
531          * Make sure we're not reparenting to ourselves and that
532          * the parent is not a zombie.
533          */
534         if (p == reaper || reaper->state >= TASK_ZOMBIE)
535                 p->real_parent = child_reaper;
536         else
537                 p->real_parent = reaper;
538         if (p->parent == p->real_parent)
539                 BUG();
540 }
541
542 static inline void reparent_thread(task_t *p, task_t *father, int traced)
543 {
544         /* We don't want people slaying init.  */
545         if (p->exit_signal != -1)
546                 p->exit_signal = SIGCHLD;
547         p->self_exec_id++;
548
549         if (p->pdeath_signal)
550                 /* We already hold the tasklist_lock here.  */
551                 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
552
553         /* Move the child from its dying parent to the new one.  */
554         if (unlikely(traced)) {
555                 /* Preserve ptrace links if someone else is tracing this child.  */
556                 list_del_init(&p->ptrace_list);
557                 if (p->parent != p->real_parent)
558                         list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
559         } else {
560                 /* If this child is being traced, then we're the one tracing it
561                  * anyway, so let go of it.
562                  */
563                 p->ptrace = 0;
564                 list_del_init(&p->sibling);
565                 p->parent = p->real_parent;
566                 list_add_tail(&p->sibling, &p->parent->children);
567
568                 /* If we'd notified the old parent about this child's death,
569                  * also notify the new parent.
570                  */
571                 if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
572                     thread_group_empty(p))
573                         do_notify_parent(p, p->exit_signal);
574         }
575
576         /*
577          * process group orphan check
578          * Case ii: Our child is in a different pgrp
579          * than we are, and it was the only connection
580          * outside, so the child pgrp is now orphaned.
581          */
582         if ((process_group(p) != process_group(father)) &&
583             (p->signal->session == father->signal->session)) {
584                 int pgrp = process_group(p);
585
586                 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
587                         __kill_pg_info(SIGHUP, (void *)1, pgrp);
588                         __kill_pg_info(SIGCONT, (void *)1, pgrp);
589                 }
590         }
591 }
592
593 /*
594  * When we die, we re-parent all our children.
595  * Try to give them to another thread in our thread
596  * group, and if no such member exists, give it to
597  * the global child reaper process (ie "init")
598  */
599 static inline void forget_original_parent(struct task_struct * father)
600 {
601         struct task_struct *p, *reaper = father;
602         struct list_head *_p, *_n;
603
604         reaper = father->group_leader;
605         if (reaper == father)
606                 reaper = child_reaper;
607
608         /*
609          * There are only two places where our children can be:
610          *
611          * - in our child list
612          * - in our ptraced child list
613          *
614          * Search them and reparent children.
615          */
616         list_for_each_safe(_p, _n, &father->children) {
617                 p = list_entry(_p,struct task_struct,sibling);
618                 if (father == p->real_parent) {
619                         choose_new_parent(p, reaper, child_reaper);
620                         reparent_thread(p, father, 0);
621                 } else {
622                         ptrace_unlink (p);
623                         if (p->state == TASK_ZOMBIE && p->exit_signal != -1 &&
624                             thread_group_empty(p))
625                                 do_notify_parent(p, p->exit_signal);
626                 }
627         }
628         list_for_each_safe(_p, _n, &father->ptrace_children) {
629                 p = list_entry(_p,struct task_struct,ptrace_list);
630                 choose_new_parent(p, reaper, child_reaper);
631                 reparent_thread(p, father, 1);
632         }
633 }
634
635 /*
636  * Send signals to all our closest relatives so that they know
637  * to properly mourn us..
638  */
639 static void exit_notify(struct task_struct *tsk)
640 {
641         int state;
642         struct task_struct *t;
643
644         ckrm_cb_exit(tsk);
645
646         if (signal_pending(tsk) && !tsk->signal->group_exit
647             && !thread_group_empty(tsk)) {
648                 /*
649                  * This occurs when there was a race between our exit
650                  * syscall and a group signal choosing us as the one to
651                  * wake up.  It could be that we are the only thread
652                  * alerted to check for pending signals, but another thread
653                  * should be woken now to take the signal since we will not.
654                  * Now we'll wake all the threads in the group just to make
655                  * sure someone gets all the pending signals.
656                  */
657                 read_lock(&tasklist_lock);
658                 spin_lock_irq(&tsk->sighand->siglock);
659                 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
660                         if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
661                                 recalc_sigpending_tsk(t);
662                                 if (signal_pending(t))
663                                         signal_wake_up(t, 0);
664                         }
665                 spin_unlock_irq(&tsk->sighand->siglock);
666                 read_unlock(&tasklist_lock);
667         }
668
669         write_lock_irq(&tasklist_lock);
670
671         /*
672          * This does two things:
673          *
674          * A.  Make init inherit all the child processes
675          * B.  Check to see if any process groups have become orphaned
676          *      as a result of our exiting, and if they have any stopped
677          *      jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
678          */
679
680         forget_original_parent(tsk);
681         BUG_ON(!list_empty(&tsk->children));
682
683         /*
684          * Check to see if any process groups have become orphaned
685          * as a result of our exiting, and if they have any stopped
686          * jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
687          *
688          * Case i: Our father is in a different pgrp than we are
689          * and we were the only connection outside, so our pgrp
690          * is about to become orphaned.
691          */
692          
693         t = tsk->real_parent;
694         
695         if ((process_group(t) != process_group(tsk)) &&
696             (t->signal->session == tsk->signal->session) &&
697             will_become_orphaned_pgrp(process_group(tsk), tsk) &&
698             has_stopped_jobs(process_group(tsk))) {
699                 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
700                 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
701         }
702
703         /* Let father know we died 
704          *
705          * Thread signals are configurable, but you aren't going to use
706          * that to send signals to arbitary processes. 
707          * That stops right now.
708          *
709          * If the parent exec id doesn't match the exec id we saved
710          * when we started then we know the parent has changed security
711          * domain.
712          *
713          * If our self_exec id doesn't match our parent_exec_id then
714          * we have changed execution domain as these two values started
715          * the same after a fork.
716          *      
717          */
718         
719         if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
720             ( tsk->parent_exec_id != t->self_exec_id  ||
721               tsk->self_exec_id != tsk->parent_exec_id)
722             && !capable(CAP_KILL))
723                 tsk->exit_signal = SIGCHLD;
724
725
726         /* If something other than our normal parent is ptracing us, then
727          * send it a SIGCHLD instead of honoring exit_signal.  exit_signal
728          * only has special meaning to our real parent.
729          */
730         if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
731                 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
732                 do_notify_parent(tsk, signal);
733         } else if (tsk->ptrace) {
734                 do_notify_parent(tsk, SIGCHLD);
735         }
736
737         state = TASK_ZOMBIE;
738         if (tsk->exit_signal == -1 && tsk->ptrace == 0)
739                 state = TASK_DEAD;
740         tsk->state = state;
741         tsk->flags |= PF_DEAD;
742
743         /*
744          * Clear these here so that update_process_times() won't try to deliver
745          * itimer, profile or rlimit signals to this task while it is in late exit.
746          */
747         tsk->it_virt_value = 0;
748         tsk->it_prof_value = 0;
749         tsk->rlim[RLIMIT_CPU].rlim_cur = RLIM_INFINITY;
750
751         /*
752          * In the preemption case it must be impossible for the task
753          * to get runnable again, so use "_raw_" unlock to keep
754          * preempt_count elevated until we schedule().
755          *
756          * To avoid deadlock on SMP, interrupts must be unmasked.  If we
757          * don't, subsequently called functions (e.g, wait_task_inactive()
758          * via release_task()) will spin, with interrupt flags
759          * unwittingly blocked, until the other task sleeps.  That task
760          * may itself be waiting for smp_call_function() to answer and
761          * complete, and with interrupts blocked that will never happen.
762          */
763         _raw_write_unlock(&tasklist_lock);
764         local_irq_enable();
765
766         /* If the process is dead, release it - nobody will wait for it */
767         if (state == TASK_DEAD)
768                 release_task(tsk);
769
770 }
771
772 asmlinkage NORET_TYPE void do_exit(long code)
773 {
774         struct task_struct *tsk = current;
775
776         if (unlikely(in_interrupt()))
777                 panic("Aiee, killing interrupt handler!");
778         if (unlikely(!tsk->pid))
779                 panic("Attempted to kill the idle task!");
780         if (unlikely(tsk->pid == 1))
781                 panic("Attempted to kill init!");
782         if (tsk->io_context)
783                 exit_io_context();
784         tsk->flags |= PF_EXITING;
785         del_timer_sync(&tsk->real_timer);
786
787         if (unlikely(in_atomic()))
788                 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
789                                 current->comm, current->pid,
790                                 preempt_count());
791
792         profile_exit_task(tsk);
793  
794         if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
795                 current->ptrace_message = code;
796                 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
797         }
798
799         acct_process(code);
800         __exit_mm(tsk);
801
802         exit_sem(tsk);
803         __exit_files(tsk);
804         __exit_fs(tsk);
805         exit_namespace(tsk);
806         exit_thread();
807 #ifdef CONFIG_NUMA
808         mpol_free(tsk->mempolicy);
809 #endif
810
811         if (tsk->signal->leader)
812                 disassociate_ctty(1);
813
814         module_put(tsk->thread_info->exec_domain->module);
815         if (tsk->binfmt)
816                 module_put(tsk->binfmt->module);
817
818         tsk->exit_code = code;
819 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
820         numtasks_put_ref(tsk->taskclass);
821 #endif
822         exit_notify(tsk);
823         schedule();
824         BUG();
825         /* Avoid "noreturn function does return".  */
826         for (;;) ;
827 }
828
829 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
830 {
831         if (comp)
832                 complete(comp);
833         
834         do_exit(code);
835 }
836
837 EXPORT_SYMBOL(complete_and_exit);
838
839 asmlinkage long sys_exit(int error_code)
840 {
841         do_exit((error_code&0xff)<<8);
842 }
843
844 task_t fastcall *next_thread(task_t *p)
845 {
846         struct pid_link *link = p->pids + PIDTYPE_TGID;
847         struct list_head *tmp, *head = &link->pidptr->task_list;
848
849 #ifdef CONFIG_SMP
850         if (!p->sighand)
851                 BUG();
852         if (!spin_is_locked(&p->sighand->siglock) &&
853                                 !rwlock_is_locked(&tasklist_lock))
854                 BUG();
855 #endif
856         tmp = link->pid_chain.next;
857         if (tmp == head)
858                 tmp = head->next;
859
860         return pid_task(tmp, PIDTYPE_TGID);
861 }
862
863 EXPORT_SYMBOL(next_thread);
864
865 /*
866  * Take down every thread in the group.  This is called by fatal signals
867  * as well as by sys_exit_group (below).
868  */
869 NORET_TYPE void
870 do_group_exit(int exit_code)
871 {
872         BUG_ON(exit_code & 0x80); /* core dumps don't get here */
873
874         if (current->signal->group_exit)
875                 exit_code = current->signal->group_exit_code;
876         else if (!thread_group_empty(current)) {
877                 struct signal_struct *const sig = current->signal;
878                 struct sighand_struct *const sighand = current->sighand;
879                 read_lock(&tasklist_lock);
880                 spin_lock_irq(&sighand->siglock);
881                 if (sig->group_exit)
882                         /* Another thread got here before we took the lock.  */
883                         exit_code = sig->group_exit_code;
884                 else {
885                         sig->group_exit = 1;
886                         sig->group_exit_code = exit_code;
887                         zap_other_threads(current);
888                 }
889                 spin_unlock_irq(&sighand->siglock);
890                 read_unlock(&tasklist_lock);
891         }
892
893         do_exit(exit_code);
894         /* NOTREACHED */
895 }
896
897 /*
898  * this kills every thread in the thread group. Note that any externally
899  * wait4()-ing process will get the correct exit code - even if this
900  * thread is not the thread group leader.
901  */
902 asmlinkage void sys_exit_group(int error_code)
903 {
904         do_group_exit((error_code & 0xff) << 8);
905 }
906
907 static int eligible_child(pid_t pid, int options, task_t *p)
908 {
909         if (pid > 0) {
910                 if (p->pid != pid)
911                         return 0;
912         } else if (!pid) {
913                 if (process_group(p) != process_group(current))
914                         return 0;
915         } else if (pid != -1) {
916                 if (process_group(p) != -pid)
917                         return 0;
918         }
919
920         /*
921          * Do not consider detached threads that are
922          * not ptraced:
923          */
924         if (p->exit_signal == -1 && !p->ptrace)
925                 return 0;
926
927         /* Wait for all children (clone and not) if __WALL is set;
928          * otherwise, wait for clone children *only* if __WCLONE is
929          * set; otherwise, wait for non-clone children *only*.  (Note:
930          * A "clone" child here is one that reports to its parent
931          * using a signal other than SIGCHLD.) */
932         if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
933             && !(options & __WALL))
934                 return 0;
935         /*
936          * Do not consider thread group leaders that are
937          * in a non-empty thread group:
938          */
939         if (current->tgid != p->tgid && delay_group_leader(p))
940                 return 2;
941
942         if (security_task_wait(p))
943                 return 0;
944
945         return 1;
946 }
947
948 /*
949  * Handle sys_wait4 work for one task in state TASK_ZOMBIE.  We hold
950  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
951  * the lock and this task is uninteresting.  If we return nonzero, we have
952  * released the lock and the system call should return.
953  */
954 static int wait_task_zombie(task_t *p, unsigned int __user *stat_addr, struct rusage __user *ru)
955 {
956         unsigned long state;
957         int retval;
958
959         /*
960          * Try to move the task's state to DEAD
961          * only one thread is allowed to do this:
962          */
963         state = xchg(&p->state, TASK_DEAD);
964         if (state != TASK_ZOMBIE) {
965                 BUG_ON(state != TASK_DEAD);
966                 return 0;
967         }
968         if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
969                 /*
970                  * This can only happen in a race with a ptraced thread
971                  * dying on another processor.
972                  */
973                 return 0;
974
975         /*
976          * Now we are sure this task is interesting, and no other
977          * thread can reap it because we set its state to TASK_DEAD.
978          */
979         read_unlock(&tasklist_lock);
980
981         retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
982         if (!retval && stat_addr) {
983                 if (p->signal->group_exit)
984                         retval = put_user(p->signal->group_exit_code, stat_addr);
985                 else
986                         retval = put_user(p->exit_code, stat_addr);
987         }
988         if (retval) {
989                 p->state = TASK_ZOMBIE;
990                 return retval;
991         }
992         retval = p->pid;
993         if (p->real_parent != p->parent) {
994                 write_lock_irq(&tasklist_lock);
995                 /* Double-check with lock held.  */
996                 if (p->real_parent != p->parent) {
997                         __ptrace_unlink(p);
998                         p->state = TASK_ZOMBIE;
999                         /* If this is a detached thread, this is where it goes away.  */
1000                         if (p->exit_signal == -1) {
1001                                 /* release_task takes the lock itself.  */
1002                                 write_unlock_irq(&tasklist_lock);
1003                                 release_task (p);
1004                         }
1005                         else {
1006                                 do_notify_parent(p, p->exit_signal);
1007                                 write_unlock_irq(&tasklist_lock);
1008                         }
1009                         p = NULL;
1010                 }
1011                 else
1012                         write_unlock_irq(&tasklist_lock);
1013         }
1014         if (p != NULL)
1015                 release_task(p);
1016         BUG_ON(!retval);
1017         return retval;
1018 }
1019
1020 /*
1021  * Handle sys_wait4 work for one task in state TASK_STOPPED.  We hold
1022  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
1023  * the lock and this task is uninteresting.  If we return nonzero, we have
1024  * released the lock and the system call should return.
1025  */
1026 static int wait_task_stopped(task_t *p, int delayed_group_leader,
1027                              unsigned int __user *stat_addr,
1028                              struct rusage __user *ru)
1029 {
1030         int retval, exit_code;
1031
1032         if (!p->exit_code)
1033                 return 0;
1034         if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1035             p->signal && p->signal->group_stop_count > 0)
1036                 /*
1037                  * A group stop is in progress and this is the group leader.
1038                  * We won't report until all threads have stopped.
1039                  */
1040                 return 0;
1041
1042         /*
1043          * Now we are pretty sure this task is interesting.
1044          * Make sure it doesn't get reaped out from under us while we
1045          * give up the lock and then examine it below.  We don't want to
1046          * keep holding onto the tasklist_lock while we call getrusage and
1047          * possibly take page faults for user memory.
1048          */
1049         get_task_struct(p);
1050         read_unlock(&tasklist_lock);
1051         write_lock_irq(&tasklist_lock);
1052
1053         /*
1054          * This uses xchg to be atomic with the thread resuming and setting
1055          * it.  It must also be done with the write lock held to prevent a
1056          * race with the TASK_ZOMBIE case.
1057          */
1058         exit_code = xchg(&p->exit_code, 0);
1059         if (unlikely(p->state > TASK_STOPPED)) {
1060                 /*
1061                  * The task resumed and then died.  Let the next iteration
1062                  * catch it in TASK_ZOMBIE.  Note that exit_code might
1063                  * already be zero here if it resumed and did _exit(0).
1064                  * The task itself is dead and won't touch exit_code again;
1065                  * other processors in this function are locked out.
1066                  */
1067                 p->exit_code = exit_code;
1068                 exit_code = 0;
1069         }
1070         if (unlikely(exit_code == 0)) {
1071                 /*
1072                  * Another thread in this function got to it first, or it
1073                  * resumed, or it resumed and then died.
1074                  */
1075                 write_unlock_irq(&tasklist_lock);
1076                 put_task_struct(p);
1077                 read_lock(&tasklist_lock);
1078                 return 0;
1079         }
1080
1081         /* move to end of parent's list to avoid starvation */
1082         remove_parent(p);
1083         add_parent(p, p->parent);
1084
1085         write_unlock_irq(&tasklist_lock);
1086
1087         retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1088         if (!retval && stat_addr)
1089                 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1090         if (!retval)
1091                 retval = p->pid;
1092         put_task_struct(p);
1093
1094         BUG_ON(!retval);
1095         return retval;
1096 }
1097
1098 asmlinkage long sys_wait4(pid_t pid,unsigned int __user *stat_addr, int options, struct rusage __user *ru)
1099 {
1100         DECLARE_WAITQUEUE(wait, current);
1101         struct task_struct *tsk;
1102         int flag, retval;
1103
1104         if (options & ~(WNOHANG|WUNTRACED|__WNOTHREAD|__WCLONE|__WALL))
1105                 return -EINVAL;
1106
1107         add_wait_queue(&current->wait_chldexit,&wait);
1108 repeat:
1109         flag = 0;
1110         current->state = TASK_INTERRUPTIBLE;
1111         read_lock(&tasklist_lock);
1112         tsk = current;
1113         do {
1114                 struct task_struct *p;
1115                 struct list_head *_p;
1116                 int ret;
1117
1118                 list_for_each(_p,&tsk->children) {
1119                         p = list_entry(_p,struct task_struct,sibling);
1120
1121                         ret = eligible_child(pid, options, p);
1122                         if (!ret)
1123                                 continue;
1124                         flag = 1;
1125
1126                         switch (p->state) {
1127                         case TASK_STOPPED:
1128                                 if (!(options & WUNTRACED) &&
1129                                     !(p->ptrace & PT_PTRACED))
1130                                         continue;
1131                                 retval = wait_task_stopped(p, ret == 2,
1132                                                            stat_addr, ru);
1133                                 if (retval != 0) /* He released the lock.  */
1134                                         goto end_wait4;
1135                                 break;
1136                         case TASK_ZOMBIE:
1137                                 /*
1138                                  * Eligible but we cannot release it yet:
1139                                  */
1140                                 if (ret == 2)
1141                                         continue;
1142                                 retval = wait_task_zombie(p, stat_addr, ru);
1143                                 if (retval != 0) /* He released the lock.  */
1144                                         goto end_wait4;
1145                                 break;
1146                         }
1147                 }
1148                 if (!flag) {
1149                         list_for_each (_p,&tsk->ptrace_children) {
1150                                 p = list_entry(_p,struct task_struct,ptrace_list);
1151                                 if (!eligible_child(pid, options, p))
1152                                         continue;
1153                                 flag = 1;
1154                                 break;
1155                         }
1156                 }
1157                 if (options & __WNOTHREAD)
1158                         break;
1159                 tsk = next_thread(tsk);
1160                 if (tsk->signal != current->signal)
1161                         BUG();
1162         } while (tsk != current);
1163         read_unlock(&tasklist_lock);
1164         if (flag) {
1165                 retval = 0;
1166                 if (options & WNOHANG)
1167                         goto end_wait4;
1168                 retval = -ERESTARTSYS;
1169                 if (signal_pending(current))
1170                         goto end_wait4;
1171                 schedule();
1172                 goto repeat;
1173         }
1174         retval = -ECHILD;
1175 end_wait4:
1176         current->state = TASK_RUNNING;
1177         remove_wait_queue(&current->wait_chldexit,&wait);
1178         return retval;
1179 }
1180
1181 #ifdef __ARCH_WANT_SYS_WAITPID
1182
1183 /*
1184  * sys_waitpid() remains for compatibility. waitpid() should be
1185  * implemented by calling sys_wait4() from libc.a.
1186  */
1187 asmlinkage long sys_waitpid(pid_t pid, unsigned __user *stat_addr, int options)
1188 {
1189         return sys_wait4(pid, stat_addr, options, NULL);
1190 }
1191
1192 #endif