Merge branch 'merge'
[pandora-kernel.git] / kernel / sys.c
1 /*
2  *  linux/kernel/sys.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/config.h>
8 #include <linux/module.h>
9 #include <linux/mm.h>
10 #include <linux/utsname.h>
11 #include <linux/mman.h>
12 #include <linux/smp_lock.h>
13 #include <linux/notifier.h>
14 #include <linux/reboot.h>
15 #include <linux/prctl.h>
16 #include <linux/init.h>
17 #include <linux/highuid.h>
18 #include <linux/fs.h>
19 #include <linux/kernel.h>
20 #include <linux/kexec.h>
21 #include <linux/workqueue.h>
22 #include <linux/capability.h>
23 #include <linux/device.h>
24 #include <linux/key.h>
25 #include <linux/times.h>
26 #include <linux/posix-timers.h>
27 #include <linux/security.h>
28 #include <linux/dcookies.h>
29 #include <linux/suspend.h>
30 #include <linux/tty.h>
31 #include <linux/signal.h>
32 #include <linux/cn_proc.h>
33
34 #include <linux/compat.h>
35 #include <linux/syscalls.h>
36 #include <linux/kprobes.h>
37
38 #include <asm/uaccess.h>
39 #include <asm/io.h>
40 #include <asm/unistd.h>
41
42 #ifndef SET_UNALIGN_CTL
43 # define SET_UNALIGN_CTL(a,b)   (-EINVAL)
44 #endif
45 #ifndef GET_UNALIGN_CTL
46 # define GET_UNALIGN_CTL(a,b)   (-EINVAL)
47 #endif
48 #ifndef SET_FPEMU_CTL
49 # define SET_FPEMU_CTL(a,b)     (-EINVAL)
50 #endif
51 #ifndef GET_FPEMU_CTL
52 # define GET_FPEMU_CTL(a,b)     (-EINVAL)
53 #endif
54 #ifndef SET_FPEXC_CTL
55 # define SET_FPEXC_CTL(a,b)     (-EINVAL)
56 #endif
57 #ifndef GET_FPEXC_CTL
58 # define GET_FPEXC_CTL(a,b)     (-EINVAL)
59 #endif
60 #ifndef GET_ENDIAN
61 # define GET_ENDIAN(a,b)        (-EINVAL)
62 #endif
63 #ifndef SET_ENDIAN
64 # define SET_ENDIAN(a,b)        (-EINVAL)
65 #endif
66
67 /*
68  * this is where the system-wide overflow UID and GID are defined, for
69  * architectures that now have 32-bit UID/GID but didn't in the past
70  */
71
72 int overflowuid = DEFAULT_OVERFLOWUID;
73 int overflowgid = DEFAULT_OVERFLOWGID;
74
75 #ifdef CONFIG_UID16
76 EXPORT_SYMBOL(overflowuid);
77 EXPORT_SYMBOL(overflowgid);
78 #endif
79
80 /*
81  * the same as above, but for filesystems which can only store a 16-bit
82  * UID and GID. as such, this is needed on all architectures
83  */
84
85 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
86 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
87
88 EXPORT_SYMBOL(fs_overflowuid);
89 EXPORT_SYMBOL(fs_overflowgid);
90
91 /*
92  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
93  */
94
95 int C_A_D = 1;
96 int cad_pid = 1;
97
98 /*
99  *      Notifier list for kernel code which wants to be called
100  *      at shutdown. This is used to stop any idling DMA operations
101  *      and the like. 
102  */
103
104 static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
105
106 /*
107  *      Notifier chain core routines.  The exported routines below
108  *      are layered on top of these, with appropriate locking added.
109  */
110
111 static int notifier_chain_register(struct notifier_block **nl,
112                 struct notifier_block *n)
113 {
114         while ((*nl) != NULL) {
115                 if (n->priority > (*nl)->priority)
116                         break;
117                 nl = &((*nl)->next);
118         }
119         n->next = *nl;
120         rcu_assign_pointer(*nl, n);
121         return 0;
122 }
123
124 static int notifier_chain_unregister(struct notifier_block **nl,
125                 struct notifier_block *n)
126 {
127         while ((*nl) != NULL) {
128                 if ((*nl) == n) {
129                         rcu_assign_pointer(*nl, n->next);
130                         return 0;
131                 }
132                 nl = &((*nl)->next);
133         }
134         return -ENOENT;
135 }
136
137 static int __kprobes notifier_call_chain(struct notifier_block **nl,
138                 unsigned long val, void *v)
139 {
140         int ret = NOTIFY_DONE;
141         struct notifier_block *nb;
142
143         nb = rcu_dereference(*nl);
144         while (nb) {
145                 ret = nb->notifier_call(nb, val, v);
146                 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
147                         break;
148                 nb = rcu_dereference(nb->next);
149         }
150         return ret;
151 }
152
153 /*
154  *      Atomic notifier chain routines.  Registration and unregistration
155  *      use a mutex, and call_chain is synchronized by RCU (no locks).
156  */
157
158 /**
159  *      atomic_notifier_chain_register - Add notifier to an atomic notifier chain
160  *      @nh: Pointer to head of the atomic notifier chain
161  *      @n: New entry in notifier chain
162  *
163  *      Adds a notifier to an atomic notifier chain.
164  *
165  *      Currently always returns zero.
166  */
167
168 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
169                 struct notifier_block *n)
170 {
171         unsigned long flags;
172         int ret;
173
174         spin_lock_irqsave(&nh->lock, flags);
175         ret = notifier_chain_register(&nh->head, n);
176         spin_unlock_irqrestore(&nh->lock, flags);
177         return ret;
178 }
179
180 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
181
182 /**
183  *      atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
184  *      @nh: Pointer to head of the atomic notifier chain
185  *      @n: Entry to remove from notifier chain
186  *
187  *      Removes a notifier from an atomic notifier chain.
188  *
189  *      Returns zero on success or %-ENOENT on failure.
190  */
191 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
192                 struct notifier_block *n)
193 {
194         unsigned long flags;
195         int ret;
196
197         spin_lock_irqsave(&nh->lock, flags);
198         ret = notifier_chain_unregister(&nh->head, n);
199         spin_unlock_irqrestore(&nh->lock, flags);
200         synchronize_rcu();
201         return ret;
202 }
203
204 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
205
206 /**
207  *      atomic_notifier_call_chain - Call functions in an atomic notifier chain
208  *      @nh: Pointer to head of the atomic notifier chain
209  *      @val: Value passed unmodified to notifier function
210  *      @v: Pointer passed unmodified to notifier function
211  *
212  *      Calls each function in a notifier chain in turn.  The functions
213  *      run in an atomic context, so they must not block.
214  *      This routine uses RCU to synchronize with changes to the chain.
215  *
216  *      If the return value of the notifier can be and'ed
217  *      with %NOTIFY_STOP_MASK then atomic_notifier_call_chain
218  *      will return immediately, with the return value of
219  *      the notifier function which halted execution.
220  *      Otherwise the return value is the return value
221  *      of the last notifier function called.
222  */
223  
224 int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
225                 unsigned long val, void *v)
226 {
227         int ret;
228
229         rcu_read_lock();
230         ret = notifier_call_chain(&nh->head, val, v);
231         rcu_read_unlock();
232         return ret;
233 }
234
235 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
236
237 /*
238  *      Blocking notifier chain routines.  All access to the chain is
239  *      synchronized by an rwsem.
240  */
241
242 /**
243  *      blocking_notifier_chain_register - Add notifier to a blocking notifier chain
244  *      @nh: Pointer to head of the blocking notifier chain
245  *      @n: New entry in notifier chain
246  *
247  *      Adds a notifier to a blocking notifier chain.
248  *      Must be called in process context.
249  *
250  *      Currently always returns zero.
251  */
252  
253 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
254                 struct notifier_block *n)
255 {
256         int ret;
257
258         /*
259          * This code gets used during boot-up, when task switching is
260          * not yet working and interrupts must remain disabled.  At
261          * such times we must not call down_write().
262          */
263         if (unlikely(system_state == SYSTEM_BOOTING))
264                 return notifier_chain_register(&nh->head, n);
265
266         down_write(&nh->rwsem);
267         ret = notifier_chain_register(&nh->head, n);
268         up_write(&nh->rwsem);
269         return ret;
270 }
271
272 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
273
274 /**
275  *      blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
276  *      @nh: Pointer to head of the blocking notifier chain
277  *      @n: Entry to remove from notifier chain
278  *
279  *      Removes a notifier from a blocking notifier chain.
280  *      Must be called from process context.
281  *
282  *      Returns zero on success or %-ENOENT on failure.
283  */
284 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
285                 struct notifier_block *n)
286 {
287         int ret;
288
289         /*
290          * This code gets used during boot-up, when task switching is
291          * not yet working and interrupts must remain disabled.  At
292          * such times we must not call down_write().
293          */
294         if (unlikely(system_state == SYSTEM_BOOTING))
295                 return notifier_chain_unregister(&nh->head, n);
296
297         down_write(&nh->rwsem);
298         ret = notifier_chain_unregister(&nh->head, n);
299         up_write(&nh->rwsem);
300         return ret;
301 }
302
303 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
304
305 /**
306  *      blocking_notifier_call_chain - Call functions in a blocking notifier chain
307  *      @nh: Pointer to head of the blocking notifier chain
308  *      @val: Value passed unmodified to notifier function
309  *      @v: Pointer passed unmodified to notifier function
310  *
311  *      Calls each function in a notifier chain in turn.  The functions
312  *      run in a process context, so they are allowed to block.
313  *
314  *      If the return value of the notifier can be and'ed
315  *      with %NOTIFY_STOP_MASK then blocking_notifier_call_chain
316  *      will return immediately, with the return value of
317  *      the notifier function which halted execution.
318  *      Otherwise the return value is the return value
319  *      of the last notifier function called.
320  */
321  
322 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
323                 unsigned long val, void *v)
324 {
325         int ret;
326
327         down_read(&nh->rwsem);
328         ret = notifier_call_chain(&nh->head, val, v);
329         up_read(&nh->rwsem);
330         return ret;
331 }
332
333 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
334
335 /*
336  *      Raw notifier chain routines.  There is no protection;
337  *      the caller must provide it.  Use at your own risk!
338  */
339
340 /**
341  *      raw_notifier_chain_register - Add notifier to a raw notifier chain
342  *      @nh: Pointer to head of the raw notifier chain
343  *      @n: New entry in notifier chain
344  *
345  *      Adds a notifier to a raw notifier chain.
346  *      All locking must be provided by the caller.
347  *
348  *      Currently always returns zero.
349  */
350
351 int raw_notifier_chain_register(struct raw_notifier_head *nh,
352                 struct notifier_block *n)
353 {
354         return notifier_chain_register(&nh->head, n);
355 }
356
357 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
358
359 /**
360  *      raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
361  *      @nh: Pointer to head of the raw notifier chain
362  *      @n: Entry to remove from notifier chain
363  *
364  *      Removes a notifier from a raw notifier chain.
365  *      All locking must be provided by the caller.
366  *
367  *      Returns zero on success or %-ENOENT on failure.
368  */
369 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
370                 struct notifier_block *n)
371 {
372         return notifier_chain_unregister(&nh->head, n);
373 }
374
375 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
376
377 /**
378  *      raw_notifier_call_chain - Call functions in a raw notifier chain
379  *      @nh: Pointer to head of the raw notifier chain
380  *      @val: Value passed unmodified to notifier function
381  *      @v: Pointer passed unmodified to notifier function
382  *
383  *      Calls each function in a notifier chain in turn.  The functions
384  *      run in an undefined context.
385  *      All locking must be provided by the caller.
386  *
387  *      If the return value of the notifier can be and'ed
388  *      with %NOTIFY_STOP_MASK then raw_notifier_call_chain
389  *      will return immediately, with the return value of
390  *      the notifier function which halted execution.
391  *      Otherwise the return value is the return value
392  *      of the last notifier function called.
393  */
394
395 int raw_notifier_call_chain(struct raw_notifier_head *nh,
396                 unsigned long val, void *v)
397 {
398         return notifier_call_chain(&nh->head, val, v);
399 }
400
401 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
402
403 /**
404  *      register_reboot_notifier - Register function to be called at reboot time
405  *      @nb: Info about notifier function to be called
406  *
407  *      Registers a function with the list of functions
408  *      to be called at reboot time.
409  *
410  *      Currently always returns zero, as blocking_notifier_chain_register
411  *      always returns zero.
412  */
413  
414 int register_reboot_notifier(struct notifier_block * nb)
415 {
416         return blocking_notifier_chain_register(&reboot_notifier_list, nb);
417 }
418
419 EXPORT_SYMBOL(register_reboot_notifier);
420
421 /**
422  *      unregister_reboot_notifier - Unregister previously registered reboot notifier
423  *      @nb: Hook to be unregistered
424  *
425  *      Unregisters a previously registered reboot
426  *      notifier function.
427  *
428  *      Returns zero on success, or %-ENOENT on failure.
429  */
430  
431 int unregister_reboot_notifier(struct notifier_block * nb)
432 {
433         return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
434 }
435
436 EXPORT_SYMBOL(unregister_reboot_notifier);
437
438 static int set_one_prio(struct task_struct *p, int niceval, int error)
439 {
440         int no_nice;
441
442         if (p->uid != current->euid &&
443                 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
444                 error = -EPERM;
445                 goto out;
446         }
447         if (niceval < task_nice(p) && !can_nice(p, niceval)) {
448                 error = -EACCES;
449                 goto out;
450         }
451         no_nice = security_task_setnice(p, niceval);
452         if (no_nice) {
453                 error = no_nice;
454                 goto out;
455         }
456         if (error == -ESRCH)
457                 error = 0;
458         set_user_nice(p, niceval);
459 out:
460         return error;
461 }
462
463 asmlinkage long sys_setpriority(int which, int who, int niceval)
464 {
465         struct task_struct *g, *p;
466         struct user_struct *user;
467         int error = -EINVAL;
468
469         if (which > 2 || which < 0)
470                 goto out;
471
472         /* normalize: avoid signed division (rounding problems) */
473         error = -ESRCH;
474         if (niceval < -20)
475                 niceval = -20;
476         if (niceval > 19)
477                 niceval = 19;
478
479         read_lock(&tasklist_lock);
480         switch (which) {
481                 case PRIO_PROCESS:
482                         if (!who)
483                                 who = current->pid;
484                         p = find_task_by_pid(who);
485                         if (p)
486                                 error = set_one_prio(p, niceval, error);
487                         break;
488                 case PRIO_PGRP:
489                         if (!who)
490                                 who = process_group(current);
491                         do_each_task_pid(who, PIDTYPE_PGID, p) {
492                                 error = set_one_prio(p, niceval, error);
493                         } while_each_task_pid(who, PIDTYPE_PGID, p);
494                         break;
495                 case PRIO_USER:
496                         user = current->user;
497                         if (!who)
498                                 who = current->uid;
499                         else
500                                 if ((who != current->uid) && !(user = find_user(who)))
501                                         goto out_unlock;        /* No processes for this user */
502
503                         do_each_thread(g, p)
504                                 if (p->uid == who)
505                                         error = set_one_prio(p, niceval, error);
506                         while_each_thread(g, p);
507                         if (who != current->uid)
508                                 free_uid(user);         /* For find_user() */
509                         break;
510         }
511 out_unlock:
512         read_unlock(&tasklist_lock);
513 out:
514         return error;
515 }
516
517 /*
518  * Ugh. To avoid negative return values, "getpriority()" will
519  * not return the normal nice-value, but a negated value that
520  * has been offset by 20 (ie it returns 40..1 instead of -20..19)
521  * to stay compatible.
522  */
523 asmlinkage long sys_getpriority(int which, int who)
524 {
525         struct task_struct *g, *p;
526         struct user_struct *user;
527         long niceval, retval = -ESRCH;
528
529         if (which > 2 || which < 0)
530                 return -EINVAL;
531
532         read_lock(&tasklist_lock);
533         switch (which) {
534                 case PRIO_PROCESS:
535                         if (!who)
536                                 who = current->pid;
537                         p = find_task_by_pid(who);
538                         if (p) {
539                                 niceval = 20 - task_nice(p);
540                                 if (niceval > retval)
541                                         retval = niceval;
542                         }
543                         break;
544                 case PRIO_PGRP:
545                         if (!who)
546                                 who = process_group(current);
547                         do_each_task_pid(who, PIDTYPE_PGID, p) {
548                                 niceval = 20 - task_nice(p);
549                                 if (niceval > retval)
550                                         retval = niceval;
551                         } while_each_task_pid(who, PIDTYPE_PGID, p);
552                         break;
553                 case PRIO_USER:
554                         user = current->user;
555                         if (!who)
556                                 who = current->uid;
557                         else
558                                 if ((who != current->uid) && !(user = find_user(who)))
559                                         goto out_unlock;        /* No processes for this user */
560
561                         do_each_thread(g, p)
562                                 if (p->uid == who) {
563                                         niceval = 20 - task_nice(p);
564                                         if (niceval > retval)
565                                                 retval = niceval;
566                                 }
567                         while_each_thread(g, p);
568                         if (who != current->uid)
569                                 free_uid(user);         /* for find_user() */
570                         break;
571         }
572 out_unlock:
573         read_unlock(&tasklist_lock);
574
575         return retval;
576 }
577
578 /**
579  *      emergency_restart - reboot the system
580  *
581  *      Without shutting down any hardware or taking any locks
582  *      reboot the system.  This is called when we know we are in
583  *      trouble so this is our best effort to reboot.  This is
584  *      safe to call in interrupt context.
585  */
586 void emergency_restart(void)
587 {
588         machine_emergency_restart();
589 }
590 EXPORT_SYMBOL_GPL(emergency_restart);
591
592 void kernel_restart_prepare(char *cmd)
593 {
594         blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
595         system_state = SYSTEM_RESTART;
596         device_shutdown();
597 }
598
599 /**
600  *      kernel_restart - reboot the system
601  *      @cmd: pointer to buffer containing command to execute for restart
602  *              or %NULL
603  *
604  *      Shutdown everything and perform a clean reboot.
605  *      This is not safe to call in interrupt context.
606  */
607 void kernel_restart(char *cmd)
608 {
609         kernel_restart_prepare(cmd);
610         if (!cmd) {
611                 printk(KERN_EMERG "Restarting system.\n");
612         } else {
613                 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
614         }
615         printk(".\n");
616         machine_restart(cmd);
617 }
618 EXPORT_SYMBOL_GPL(kernel_restart);
619
620 /**
621  *      kernel_kexec - reboot the system
622  *
623  *      Move into place and start executing a preloaded standalone
624  *      executable.  If nothing was preloaded return an error.
625  */
626 void kernel_kexec(void)
627 {
628 #ifdef CONFIG_KEXEC
629         struct kimage *image;
630         image = xchg(&kexec_image, NULL);
631         if (!image) {
632                 return;
633         }
634         kernel_restart_prepare(NULL);
635         printk(KERN_EMERG "Starting new kernel\n");
636         machine_shutdown();
637         machine_kexec(image);
638 #endif
639 }
640 EXPORT_SYMBOL_GPL(kernel_kexec);
641
642 void kernel_shutdown_prepare(enum system_states state)
643 {
644         blocking_notifier_call_chain(&reboot_notifier_list,
645                 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
646         system_state = state;
647         device_shutdown();
648 }
649 /**
650  *      kernel_halt - halt the system
651  *
652  *      Shutdown everything and perform a clean system halt.
653  */
654 void kernel_halt(void)
655 {
656         kernel_shutdown_prepare(SYSTEM_HALT);
657         printk(KERN_EMERG "System halted.\n");
658         machine_halt();
659 }
660
661 EXPORT_SYMBOL_GPL(kernel_halt);
662
663 /**
664  *      kernel_power_off - power_off the system
665  *
666  *      Shutdown everything and perform a clean system power_off.
667  */
668 void kernel_power_off(void)
669 {
670         kernel_shutdown_prepare(SYSTEM_POWER_OFF);
671         printk(KERN_EMERG "Power down.\n");
672         machine_power_off();
673 }
674 EXPORT_SYMBOL_GPL(kernel_power_off);
675 /*
676  * Reboot system call: for obvious reasons only root may call it,
677  * and even root needs to set up some magic numbers in the registers
678  * so that some mistake won't make this reboot the whole machine.
679  * You can also set the meaning of the ctrl-alt-del-key here.
680  *
681  * reboot doesn't sync: do that yourself before calling this.
682  */
683 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
684 {
685         char buffer[256];
686
687         /* We only trust the superuser with rebooting the system. */
688         if (!capable(CAP_SYS_BOOT))
689                 return -EPERM;
690
691         /* For safety, we require "magic" arguments. */
692         if (magic1 != LINUX_REBOOT_MAGIC1 ||
693             (magic2 != LINUX_REBOOT_MAGIC2 &&
694                         magic2 != LINUX_REBOOT_MAGIC2A &&
695                         magic2 != LINUX_REBOOT_MAGIC2B &&
696                         magic2 != LINUX_REBOOT_MAGIC2C))
697                 return -EINVAL;
698
699         /* Instead of trying to make the power_off code look like
700          * halt when pm_power_off is not set do it the easy way.
701          */
702         if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
703                 cmd = LINUX_REBOOT_CMD_HALT;
704
705         lock_kernel();
706         switch (cmd) {
707         case LINUX_REBOOT_CMD_RESTART:
708                 kernel_restart(NULL);
709                 break;
710
711         case LINUX_REBOOT_CMD_CAD_ON:
712                 C_A_D = 1;
713                 break;
714
715         case LINUX_REBOOT_CMD_CAD_OFF:
716                 C_A_D = 0;
717                 break;
718
719         case LINUX_REBOOT_CMD_HALT:
720                 kernel_halt();
721                 unlock_kernel();
722                 do_exit(0);
723                 break;
724
725         case LINUX_REBOOT_CMD_POWER_OFF:
726                 kernel_power_off();
727                 unlock_kernel();
728                 do_exit(0);
729                 break;
730
731         case LINUX_REBOOT_CMD_RESTART2:
732                 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
733                         unlock_kernel();
734                         return -EFAULT;
735                 }
736                 buffer[sizeof(buffer) - 1] = '\0';
737
738                 kernel_restart(buffer);
739                 break;
740
741         case LINUX_REBOOT_CMD_KEXEC:
742                 kernel_kexec();
743                 unlock_kernel();
744                 return -EINVAL;
745
746 #ifdef CONFIG_SOFTWARE_SUSPEND
747         case LINUX_REBOOT_CMD_SW_SUSPEND:
748                 {
749                         int ret = software_suspend();
750                         unlock_kernel();
751                         return ret;
752                 }
753 #endif
754
755         default:
756                 unlock_kernel();
757                 return -EINVAL;
758         }
759         unlock_kernel();
760         return 0;
761 }
762
763 static void deferred_cad(void *dummy)
764 {
765         kernel_restart(NULL);
766 }
767
768 /*
769  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
770  * As it's called within an interrupt, it may NOT sync: the only choice
771  * is whether to reboot at once, or just ignore the ctrl-alt-del.
772  */
773 void ctrl_alt_del(void)
774 {
775         static DECLARE_WORK(cad_work, deferred_cad, NULL);
776
777         if (C_A_D)
778                 schedule_work(&cad_work);
779         else
780                 kill_proc(cad_pid, SIGINT, 1);
781 }
782         
783
784 /*
785  * Unprivileged users may change the real gid to the effective gid
786  * or vice versa.  (BSD-style)
787  *
788  * If you set the real gid at all, or set the effective gid to a value not
789  * equal to the real gid, then the saved gid is set to the new effective gid.
790  *
791  * This makes it possible for a setgid program to completely drop its
792  * privileges, which is often a useful assertion to make when you are doing
793  * a security audit over a program.
794  *
795  * The general idea is that a program which uses just setregid() will be
796  * 100% compatible with BSD.  A program which uses just setgid() will be
797  * 100% compatible with POSIX with saved IDs. 
798  *
799  * SMP: There are not races, the GIDs are checked only by filesystem
800  *      operations (as far as semantic preservation is concerned).
801  */
802 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
803 {
804         int old_rgid = current->gid;
805         int old_egid = current->egid;
806         int new_rgid = old_rgid;
807         int new_egid = old_egid;
808         int retval;
809
810         retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
811         if (retval)
812                 return retval;
813
814         if (rgid != (gid_t) -1) {
815                 if ((old_rgid == rgid) ||
816                     (current->egid==rgid) ||
817                     capable(CAP_SETGID))
818                         new_rgid = rgid;
819                 else
820                         return -EPERM;
821         }
822         if (egid != (gid_t) -1) {
823                 if ((old_rgid == egid) ||
824                     (current->egid == egid) ||
825                     (current->sgid == egid) ||
826                     capable(CAP_SETGID))
827                         new_egid = egid;
828                 else {
829                         return -EPERM;
830                 }
831         }
832         if (new_egid != old_egid)
833         {
834                 current->mm->dumpable = suid_dumpable;
835                 smp_wmb();
836         }
837         if (rgid != (gid_t) -1 ||
838             (egid != (gid_t) -1 && egid != old_rgid))
839                 current->sgid = new_egid;
840         current->fsgid = new_egid;
841         current->egid = new_egid;
842         current->gid = new_rgid;
843         key_fsgid_changed(current);
844         proc_id_connector(current, PROC_EVENT_GID);
845         return 0;
846 }
847
848 /*
849  * setgid() is implemented like SysV w/ SAVED_IDS 
850  *
851  * SMP: Same implicit races as above.
852  */
853 asmlinkage long sys_setgid(gid_t gid)
854 {
855         int old_egid = current->egid;
856         int retval;
857
858         retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
859         if (retval)
860                 return retval;
861
862         if (capable(CAP_SETGID))
863         {
864                 if(old_egid != gid)
865                 {
866                         current->mm->dumpable = suid_dumpable;
867                         smp_wmb();
868                 }
869                 current->gid = current->egid = current->sgid = current->fsgid = gid;
870         }
871         else if ((gid == current->gid) || (gid == current->sgid))
872         {
873                 if(old_egid != gid)
874                 {
875                         current->mm->dumpable = suid_dumpable;
876                         smp_wmb();
877                 }
878                 current->egid = current->fsgid = gid;
879         }
880         else
881                 return -EPERM;
882
883         key_fsgid_changed(current);
884         proc_id_connector(current, PROC_EVENT_GID);
885         return 0;
886 }
887   
888 static int set_user(uid_t new_ruid, int dumpclear)
889 {
890         struct user_struct *new_user;
891
892         new_user = alloc_uid(new_ruid);
893         if (!new_user)
894                 return -EAGAIN;
895
896         if (atomic_read(&new_user->processes) >=
897                                 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
898                         new_user != &root_user) {
899                 free_uid(new_user);
900                 return -EAGAIN;
901         }
902
903         switch_uid(new_user);
904
905         if(dumpclear)
906         {
907                 current->mm->dumpable = suid_dumpable;
908                 smp_wmb();
909         }
910         current->uid = new_ruid;
911         return 0;
912 }
913
914 /*
915  * Unprivileged users may change the real uid to the effective uid
916  * or vice versa.  (BSD-style)
917  *
918  * If you set the real uid at all, or set the effective uid to a value not
919  * equal to the real uid, then the saved uid is set to the new effective uid.
920  *
921  * This makes it possible for a setuid program to completely drop its
922  * privileges, which is often a useful assertion to make when you are doing
923  * a security audit over a program.
924  *
925  * The general idea is that a program which uses just setreuid() will be
926  * 100% compatible with BSD.  A program which uses just setuid() will be
927  * 100% compatible with POSIX with saved IDs. 
928  */
929 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
930 {
931         int old_ruid, old_euid, old_suid, new_ruid, new_euid;
932         int retval;
933
934         retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
935         if (retval)
936                 return retval;
937
938         new_ruid = old_ruid = current->uid;
939         new_euid = old_euid = current->euid;
940         old_suid = current->suid;
941
942         if (ruid != (uid_t) -1) {
943                 new_ruid = ruid;
944                 if ((old_ruid != ruid) &&
945                     (current->euid != ruid) &&
946                     !capable(CAP_SETUID))
947                         return -EPERM;
948         }
949
950         if (euid != (uid_t) -1) {
951                 new_euid = euid;
952                 if ((old_ruid != euid) &&
953                     (current->euid != euid) &&
954                     (current->suid != euid) &&
955                     !capable(CAP_SETUID))
956                         return -EPERM;
957         }
958
959         if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
960                 return -EAGAIN;
961
962         if (new_euid != old_euid)
963         {
964                 current->mm->dumpable = suid_dumpable;
965                 smp_wmb();
966         }
967         current->fsuid = current->euid = new_euid;
968         if (ruid != (uid_t) -1 ||
969             (euid != (uid_t) -1 && euid != old_ruid))
970                 current->suid = current->euid;
971         current->fsuid = current->euid;
972
973         key_fsuid_changed(current);
974         proc_id_connector(current, PROC_EVENT_UID);
975
976         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
977 }
978
979
980                 
981 /*
982  * setuid() is implemented like SysV with SAVED_IDS 
983  * 
984  * Note that SAVED_ID's is deficient in that a setuid root program
985  * like sendmail, for example, cannot set its uid to be a normal 
986  * user and then switch back, because if you're root, setuid() sets
987  * the saved uid too.  If you don't like this, blame the bright people
988  * in the POSIX committee and/or USG.  Note that the BSD-style setreuid()
989  * will allow a root program to temporarily drop privileges and be able to
990  * regain them by swapping the real and effective uid.  
991  */
992 asmlinkage long sys_setuid(uid_t uid)
993 {
994         int old_euid = current->euid;
995         int old_ruid, old_suid, new_ruid, new_suid;
996         int retval;
997
998         retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
999         if (retval)
1000                 return retval;
1001
1002         old_ruid = new_ruid = current->uid;
1003         old_suid = current->suid;
1004         new_suid = old_suid;
1005         
1006         if (capable(CAP_SETUID)) {
1007                 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1008                         return -EAGAIN;
1009                 new_suid = uid;
1010         } else if ((uid != current->uid) && (uid != new_suid))
1011                 return -EPERM;
1012
1013         if (old_euid != uid)
1014         {
1015                 current->mm->dumpable = suid_dumpable;
1016                 smp_wmb();
1017         }
1018         current->fsuid = current->euid = uid;
1019         current->suid = new_suid;
1020
1021         key_fsuid_changed(current);
1022         proc_id_connector(current, PROC_EVENT_UID);
1023
1024         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1025 }
1026
1027
1028 /*
1029  * This function implements a generic ability to update ruid, euid,
1030  * and suid.  This allows you to implement the 4.4 compatible seteuid().
1031  */
1032 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1033 {
1034         int old_ruid = current->uid;
1035         int old_euid = current->euid;
1036         int old_suid = current->suid;
1037         int retval;
1038
1039         retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1040         if (retval)
1041                 return retval;
1042
1043         if (!capable(CAP_SETUID)) {
1044                 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1045                     (ruid != current->euid) && (ruid != current->suid))
1046                         return -EPERM;
1047                 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1048                     (euid != current->euid) && (euid != current->suid))
1049                         return -EPERM;
1050                 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1051                     (suid != current->euid) && (suid != current->suid))
1052                         return -EPERM;
1053         }
1054         if (ruid != (uid_t) -1) {
1055                 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1056                         return -EAGAIN;
1057         }
1058         if (euid != (uid_t) -1) {
1059                 if (euid != current->euid)
1060                 {
1061                         current->mm->dumpable = suid_dumpable;
1062                         smp_wmb();
1063                 }
1064                 current->euid = euid;
1065         }
1066         current->fsuid = current->euid;
1067         if (suid != (uid_t) -1)
1068                 current->suid = suid;
1069
1070         key_fsuid_changed(current);
1071         proc_id_connector(current, PROC_EVENT_UID);
1072
1073         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1074 }
1075
1076 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1077 {
1078         int retval;
1079
1080         if (!(retval = put_user(current->uid, ruid)) &&
1081             !(retval = put_user(current->euid, euid)))
1082                 retval = put_user(current->suid, suid);
1083
1084         return retval;
1085 }
1086
1087 /*
1088  * Same as above, but for rgid, egid, sgid.
1089  */
1090 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1091 {
1092         int retval;
1093
1094         retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1095         if (retval)
1096                 return retval;
1097
1098         if (!capable(CAP_SETGID)) {
1099                 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1100                     (rgid != current->egid) && (rgid != current->sgid))
1101                         return -EPERM;
1102                 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1103                     (egid != current->egid) && (egid != current->sgid))
1104                         return -EPERM;
1105                 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1106                     (sgid != current->egid) && (sgid != current->sgid))
1107                         return -EPERM;
1108         }
1109         if (egid != (gid_t) -1) {
1110                 if (egid != current->egid)
1111                 {
1112                         current->mm->dumpable = suid_dumpable;
1113                         smp_wmb();
1114                 }
1115                 current->egid = egid;
1116         }
1117         current->fsgid = current->egid;
1118         if (rgid != (gid_t) -1)
1119                 current->gid = rgid;
1120         if (sgid != (gid_t) -1)
1121                 current->sgid = sgid;
1122
1123         key_fsgid_changed(current);
1124         proc_id_connector(current, PROC_EVENT_GID);
1125         return 0;
1126 }
1127
1128 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1129 {
1130         int retval;
1131
1132         if (!(retval = put_user(current->gid, rgid)) &&
1133             !(retval = put_user(current->egid, egid)))
1134                 retval = put_user(current->sgid, sgid);
1135
1136         return retval;
1137 }
1138
1139
1140 /*
1141  * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1142  * is used for "access()" and for the NFS daemon (letting nfsd stay at
1143  * whatever uid it wants to). It normally shadows "euid", except when
1144  * explicitly set by setfsuid() or for access..
1145  */
1146 asmlinkage long sys_setfsuid(uid_t uid)
1147 {
1148         int old_fsuid;
1149
1150         old_fsuid = current->fsuid;
1151         if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1152                 return old_fsuid;
1153
1154         if (uid == current->uid || uid == current->euid ||
1155             uid == current->suid || uid == current->fsuid || 
1156             capable(CAP_SETUID))
1157         {
1158                 if (uid != old_fsuid)
1159                 {
1160                         current->mm->dumpable = suid_dumpable;
1161                         smp_wmb();
1162                 }
1163                 current->fsuid = uid;
1164         }
1165
1166         key_fsuid_changed(current);
1167         proc_id_connector(current, PROC_EVENT_UID);
1168
1169         security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1170
1171         return old_fsuid;
1172 }
1173
1174 /*
1175  * Samma pÃ¥ svenska..
1176  */
1177 asmlinkage long sys_setfsgid(gid_t gid)
1178 {
1179         int old_fsgid;
1180
1181         old_fsgid = current->fsgid;
1182         if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1183                 return old_fsgid;
1184
1185         if (gid == current->gid || gid == current->egid ||
1186             gid == current->sgid || gid == current->fsgid || 
1187             capable(CAP_SETGID))
1188         {
1189                 if (gid != old_fsgid)
1190                 {
1191                         current->mm->dumpable = suid_dumpable;
1192                         smp_wmb();
1193                 }
1194                 current->fsgid = gid;
1195                 key_fsgid_changed(current);
1196                 proc_id_connector(current, PROC_EVENT_GID);
1197         }
1198         return old_fsgid;
1199 }
1200
1201 asmlinkage long sys_times(struct tms __user * tbuf)
1202 {
1203         /*
1204          *      In the SMP world we might just be unlucky and have one of
1205          *      the times increment as we use it. Since the value is an
1206          *      atomically safe type this is just fine. Conceptually its
1207          *      as if the syscall took an instant longer to occur.
1208          */
1209         if (tbuf) {
1210                 struct tms tmp;
1211                 struct task_struct *tsk = current;
1212                 struct task_struct *t;
1213                 cputime_t utime, stime, cutime, cstime;
1214
1215                 spin_lock_irq(&tsk->sighand->siglock);
1216                 utime = tsk->signal->utime;
1217                 stime = tsk->signal->stime;
1218                 t = tsk;
1219                 do {
1220                         utime = cputime_add(utime, t->utime);
1221                         stime = cputime_add(stime, t->stime);
1222                         t = next_thread(t);
1223                 } while (t != tsk);
1224
1225                 cutime = tsk->signal->cutime;
1226                 cstime = tsk->signal->cstime;
1227                 spin_unlock_irq(&tsk->sighand->siglock);
1228
1229                 tmp.tms_utime = cputime_to_clock_t(utime);
1230                 tmp.tms_stime = cputime_to_clock_t(stime);
1231                 tmp.tms_cutime = cputime_to_clock_t(cutime);
1232                 tmp.tms_cstime = cputime_to_clock_t(cstime);
1233                 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1234                         return -EFAULT;
1235         }
1236         return (long) jiffies_64_to_clock_t(get_jiffies_64());
1237 }
1238
1239 /*
1240  * This needs some heavy checking ...
1241  * I just haven't the stomach for it. I also don't fully
1242  * understand sessions/pgrp etc. Let somebody who does explain it.
1243  *
1244  * OK, I think I have the protection semantics right.... this is really
1245  * only important on a multi-user system anyway, to make sure one user
1246  * can't send a signal to a process owned by another.  -TYT, 12/12/91
1247  *
1248  * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1249  * LBT 04.03.94
1250  */
1251
1252 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1253 {
1254         struct task_struct *p;
1255         struct task_struct *group_leader = current->group_leader;
1256         int err = -EINVAL;
1257
1258         if (!pid)
1259                 pid = group_leader->pid;
1260         if (!pgid)
1261                 pgid = pid;
1262         if (pgid < 0)
1263                 return -EINVAL;
1264
1265         /* From this point forward we keep holding onto the tasklist lock
1266          * so that our parent does not change from under us. -DaveM
1267          */
1268         write_lock_irq(&tasklist_lock);
1269
1270         err = -ESRCH;
1271         p = find_task_by_pid(pid);
1272         if (!p)
1273                 goto out;
1274
1275         err = -EINVAL;
1276         if (!thread_group_leader(p))
1277                 goto out;
1278
1279         if (p->real_parent == group_leader) {
1280                 err = -EPERM;
1281                 if (p->signal->session != group_leader->signal->session)
1282                         goto out;
1283                 err = -EACCES;
1284                 if (p->did_exec)
1285                         goto out;
1286         } else {
1287                 err = -ESRCH;
1288                 if (p != group_leader)
1289                         goto out;
1290         }
1291
1292         err = -EPERM;
1293         if (p->signal->leader)
1294                 goto out;
1295
1296         if (pgid != pid) {
1297                 struct task_struct *p;
1298
1299                 do_each_task_pid(pgid, PIDTYPE_PGID, p) {
1300                         if (p->signal->session == group_leader->signal->session)
1301                                 goto ok_pgid;
1302                 } while_each_task_pid(pgid, PIDTYPE_PGID, p);
1303                 goto out;
1304         }
1305
1306 ok_pgid:
1307         err = security_task_setpgid(p, pgid);
1308         if (err)
1309                 goto out;
1310
1311         if (process_group(p) != pgid) {
1312                 detach_pid(p, PIDTYPE_PGID);
1313                 p->signal->pgrp = pgid;
1314                 attach_pid(p, PIDTYPE_PGID, pgid);
1315         }
1316
1317         err = 0;
1318 out:
1319         /* All paths lead to here, thus we are safe. -DaveM */
1320         write_unlock_irq(&tasklist_lock);
1321         return err;
1322 }
1323
1324 asmlinkage long sys_getpgid(pid_t pid)
1325 {
1326         if (!pid) {
1327                 return process_group(current);
1328         } else {
1329                 int retval;
1330                 struct task_struct *p;
1331
1332                 read_lock(&tasklist_lock);
1333                 p = find_task_by_pid(pid);
1334
1335                 retval = -ESRCH;
1336                 if (p) {
1337                         retval = security_task_getpgid(p);
1338                         if (!retval)
1339                                 retval = process_group(p);
1340                 }
1341                 read_unlock(&tasklist_lock);
1342                 return retval;
1343         }
1344 }
1345
1346 #ifdef __ARCH_WANT_SYS_GETPGRP
1347
1348 asmlinkage long sys_getpgrp(void)
1349 {
1350         /* SMP - assuming writes are word atomic this is fine */
1351         return process_group(current);
1352 }
1353
1354 #endif
1355
1356 asmlinkage long sys_getsid(pid_t pid)
1357 {
1358         if (!pid) {
1359                 return current->signal->session;
1360         } else {
1361                 int retval;
1362                 struct task_struct *p;
1363
1364                 read_lock(&tasklist_lock);
1365                 p = find_task_by_pid(pid);
1366
1367                 retval = -ESRCH;
1368                 if(p) {
1369                         retval = security_task_getsid(p);
1370                         if (!retval)
1371                                 retval = p->signal->session;
1372                 }
1373                 read_unlock(&tasklist_lock);
1374                 return retval;
1375         }
1376 }
1377
1378 asmlinkage long sys_setsid(void)
1379 {
1380         struct task_struct *group_leader = current->group_leader;
1381         pid_t session;
1382         int err = -EPERM;
1383
1384         mutex_lock(&tty_mutex);
1385         write_lock_irq(&tasklist_lock);
1386
1387         /* Fail if I am already a session leader */
1388         if (group_leader->signal->leader)
1389                 goto out;
1390
1391         session = group_leader->pid;
1392         /* Fail if a process group id already exists that equals the
1393          * proposed session id.
1394          *
1395          * Don't check if session id == 1 because kernel threads use this
1396          * session id and so the check will always fail and make it so
1397          * init cannot successfully call setsid.
1398          */
1399         if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
1400                 goto out;
1401
1402         group_leader->signal->leader = 1;
1403         __set_special_pids(session, session);
1404         group_leader->signal->tty = NULL;
1405         group_leader->signal->tty_old_pgrp = 0;
1406         err = process_group(group_leader);
1407 out:
1408         write_unlock_irq(&tasklist_lock);
1409         mutex_unlock(&tty_mutex);
1410         return err;
1411 }
1412
1413 /*
1414  * Supplementary group IDs
1415  */
1416
1417 /* init to 2 - one for init_task, one to ensure it is never freed */
1418 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1419
1420 struct group_info *groups_alloc(int gidsetsize)
1421 {
1422         struct group_info *group_info;
1423         int nblocks;
1424         int i;
1425
1426         nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1427         /* Make sure we always allocate at least one indirect block pointer */
1428         nblocks = nblocks ? : 1;
1429         group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1430         if (!group_info)
1431                 return NULL;
1432         group_info->ngroups = gidsetsize;
1433         group_info->nblocks = nblocks;
1434         atomic_set(&group_info->usage, 1);
1435
1436         if (gidsetsize <= NGROUPS_SMALL) {
1437                 group_info->blocks[0] = group_info->small_block;
1438         } else {
1439                 for (i = 0; i < nblocks; i++) {
1440                         gid_t *b;
1441                         b = (void *)__get_free_page(GFP_USER);
1442                         if (!b)
1443                                 goto out_undo_partial_alloc;
1444                         group_info->blocks[i] = b;
1445                 }
1446         }
1447         return group_info;
1448
1449 out_undo_partial_alloc:
1450         while (--i >= 0) {
1451                 free_page((unsigned long)group_info->blocks[i]);
1452         }
1453         kfree(group_info);
1454         return NULL;
1455 }
1456
1457 EXPORT_SYMBOL(groups_alloc);
1458
1459 void groups_free(struct group_info *group_info)
1460 {
1461         if (group_info->blocks[0] != group_info->small_block) {
1462                 int i;
1463                 for (i = 0; i < group_info->nblocks; i++)
1464                         free_page((unsigned long)group_info->blocks[i]);
1465         }
1466         kfree(group_info);
1467 }
1468
1469 EXPORT_SYMBOL(groups_free);
1470
1471 /* export the group_info to a user-space array */
1472 static int groups_to_user(gid_t __user *grouplist,
1473     struct group_info *group_info)
1474 {
1475         int i;
1476         int count = group_info->ngroups;
1477
1478         for (i = 0; i < group_info->nblocks; i++) {
1479                 int cp_count = min(NGROUPS_PER_BLOCK, count);
1480                 int off = i * NGROUPS_PER_BLOCK;
1481                 int len = cp_count * sizeof(*grouplist);
1482
1483                 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1484                         return -EFAULT;
1485
1486                 count -= cp_count;
1487         }
1488         return 0;
1489 }
1490
1491 /* fill a group_info from a user-space array - it must be allocated already */
1492 static int groups_from_user(struct group_info *group_info,
1493     gid_t __user *grouplist)
1494  {
1495         int i;
1496         int count = group_info->ngroups;
1497
1498         for (i = 0; i < group_info->nblocks; i++) {
1499                 int cp_count = min(NGROUPS_PER_BLOCK, count);
1500                 int off = i * NGROUPS_PER_BLOCK;
1501                 int len = cp_count * sizeof(*grouplist);
1502
1503                 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1504                         return -EFAULT;
1505
1506                 count -= cp_count;
1507         }
1508         return 0;
1509 }
1510
1511 /* a simple Shell sort */
1512 static void groups_sort(struct group_info *group_info)
1513 {
1514         int base, max, stride;
1515         int gidsetsize = group_info->ngroups;
1516
1517         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1518                 ; /* nothing */
1519         stride /= 3;
1520
1521         while (stride) {
1522                 max = gidsetsize - stride;
1523                 for (base = 0; base < max; base++) {
1524                         int left = base;
1525                         int right = left + stride;
1526                         gid_t tmp = GROUP_AT(group_info, right);
1527
1528                         while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1529                                 GROUP_AT(group_info, right) =
1530                                     GROUP_AT(group_info, left);
1531                                 right = left;
1532                                 left -= stride;
1533                         }
1534                         GROUP_AT(group_info, right) = tmp;
1535                 }
1536                 stride /= 3;
1537         }
1538 }
1539
1540 /* a simple bsearch */
1541 int groups_search(struct group_info *group_info, gid_t grp)
1542 {
1543         unsigned int left, right;
1544
1545         if (!group_info)
1546                 return 0;
1547
1548         left = 0;
1549         right = group_info->ngroups;
1550         while (left < right) {
1551                 unsigned int mid = (left+right)/2;
1552                 int cmp = grp - GROUP_AT(group_info, mid);
1553                 if (cmp > 0)
1554                         left = mid + 1;
1555                 else if (cmp < 0)
1556                         right = mid;
1557                 else
1558                         return 1;
1559         }
1560         return 0;
1561 }
1562
1563 /* validate and set current->group_info */
1564 int set_current_groups(struct group_info *group_info)
1565 {
1566         int retval;
1567         struct group_info *old_info;
1568
1569         retval = security_task_setgroups(group_info);
1570         if (retval)
1571                 return retval;
1572
1573         groups_sort(group_info);
1574         get_group_info(group_info);
1575
1576         task_lock(current);
1577         old_info = current->group_info;
1578         current->group_info = group_info;
1579         task_unlock(current);
1580
1581         put_group_info(old_info);
1582
1583         return 0;
1584 }
1585
1586 EXPORT_SYMBOL(set_current_groups);
1587
1588 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1589 {
1590         int i = 0;
1591
1592         /*
1593          *      SMP: Nobody else can change our grouplist. Thus we are
1594          *      safe.
1595          */
1596
1597         if (gidsetsize < 0)
1598                 return -EINVAL;
1599
1600         /* no need to grab task_lock here; it cannot change */
1601         i = current->group_info->ngroups;
1602         if (gidsetsize) {
1603                 if (i > gidsetsize) {
1604                         i = -EINVAL;
1605                         goto out;
1606                 }
1607                 if (groups_to_user(grouplist, current->group_info)) {
1608                         i = -EFAULT;
1609                         goto out;
1610                 }
1611         }
1612 out:
1613         return i;
1614 }
1615
1616 /*
1617  *      SMP: Our groups are copy-on-write. We can set them safely
1618  *      without another task interfering.
1619  */
1620  
1621 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1622 {
1623         struct group_info *group_info;
1624         int retval;
1625
1626         if (!capable(CAP_SETGID))
1627                 return -EPERM;
1628         if ((unsigned)gidsetsize > NGROUPS_MAX)
1629                 return -EINVAL;
1630
1631         group_info = groups_alloc(gidsetsize);
1632         if (!group_info)
1633                 return -ENOMEM;
1634         retval = groups_from_user(group_info, grouplist);
1635         if (retval) {
1636                 put_group_info(group_info);
1637                 return retval;
1638         }
1639
1640         retval = set_current_groups(group_info);
1641         put_group_info(group_info);
1642
1643         return retval;
1644 }
1645
1646 /*
1647  * Check whether we're fsgid/egid or in the supplemental group..
1648  */
1649 int in_group_p(gid_t grp)
1650 {
1651         int retval = 1;
1652         if (grp != current->fsgid) {
1653                 retval = groups_search(current->group_info, grp);
1654         }
1655         return retval;
1656 }
1657
1658 EXPORT_SYMBOL(in_group_p);
1659
1660 int in_egroup_p(gid_t grp)
1661 {
1662         int retval = 1;
1663         if (grp != current->egid) {
1664                 retval = groups_search(current->group_info, grp);
1665         }
1666         return retval;
1667 }
1668
1669 EXPORT_SYMBOL(in_egroup_p);
1670
1671 DECLARE_RWSEM(uts_sem);
1672
1673 EXPORT_SYMBOL(uts_sem);
1674
1675 asmlinkage long sys_newuname(struct new_utsname __user * name)
1676 {
1677         int errno = 0;
1678
1679         down_read(&uts_sem);
1680         if (copy_to_user(name,&system_utsname,sizeof *name))
1681                 errno = -EFAULT;
1682         up_read(&uts_sem);
1683         return errno;
1684 }
1685
1686 asmlinkage long sys_sethostname(char __user *name, int len)
1687 {
1688         int errno;
1689         char tmp[__NEW_UTS_LEN];
1690
1691         if (!capable(CAP_SYS_ADMIN))
1692                 return -EPERM;
1693         if (len < 0 || len > __NEW_UTS_LEN)
1694                 return -EINVAL;
1695         down_write(&uts_sem);
1696         errno = -EFAULT;
1697         if (!copy_from_user(tmp, name, len)) {
1698                 memcpy(system_utsname.nodename, tmp, len);
1699                 system_utsname.nodename[len] = 0;
1700                 errno = 0;
1701         }
1702         up_write(&uts_sem);
1703         return errno;
1704 }
1705
1706 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1707
1708 asmlinkage long sys_gethostname(char __user *name, int len)
1709 {
1710         int i, errno;
1711
1712         if (len < 0)
1713                 return -EINVAL;
1714         down_read(&uts_sem);
1715         i = 1 + strlen(system_utsname.nodename);
1716         if (i > len)
1717                 i = len;
1718         errno = 0;
1719         if (copy_to_user(name, system_utsname.nodename, i))
1720                 errno = -EFAULT;
1721         up_read(&uts_sem);
1722         return errno;
1723 }
1724
1725 #endif
1726
1727 /*
1728  * Only setdomainname; getdomainname can be implemented by calling
1729  * uname()
1730  */
1731 asmlinkage long sys_setdomainname(char __user *name, int len)
1732 {
1733         int errno;
1734         char tmp[__NEW_UTS_LEN];
1735
1736         if (!capable(CAP_SYS_ADMIN))
1737                 return -EPERM;
1738         if (len < 0 || len > __NEW_UTS_LEN)
1739                 return -EINVAL;
1740
1741         down_write(&uts_sem);
1742         errno = -EFAULT;
1743         if (!copy_from_user(tmp, name, len)) {
1744                 memcpy(system_utsname.domainname, tmp, len);
1745                 system_utsname.domainname[len] = 0;
1746                 errno = 0;
1747         }
1748         up_write(&uts_sem);
1749         return errno;
1750 }
1751
1752 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1753 {
1754         if (resource >= RLIM_NLIMITS)
1755                 return -EINVAL;
1756         else {
1757                 struct rlimit value;
1758                 task_lock(current->group_leader);
1759                 value = current->signal->rlim[resource];
1760                 task_unlock(current->group_leader);
1761                 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1762         }
1763 }
1764
1765 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1766
1767 /*
1768  *      Back compatibility for getrlimit. Needed for some apps.
1769  */
1770  
1771 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1772 {
1773         struct rlimit x;
1774         if (resource >= RLIM_NLIMITS)
1775                 return -EINVAL;
1776
1777         task_lock(current->group_leader);
1778         x = current->signal->rlim[resource];
1779         task_unlock(current->group_leader);
1780         if(x.rlim_cur > 0x7FFFFFFF)
1781                 x.rlim_cur = 0x7FFFFFFF;
1782         if(x.rlim_max > 0x7FFFFFFF)
1783                 x.rlim_max = 0x7FFFFFFF;
1784         return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1785 }
1786
1787 #endif
1788
1789 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1790 {
1791         struct rlimit new_rlim, *old_rlim;
1792         unsigned long it_prof_secs;
1793         int retval;
1794
1795         if (resource >= RLIM_NLIMITS)
1796                 return -EINVAL;
1797         if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1798                 return -EFAULT;
1799         if (new_rlim.rlim_cur > new_rlim.rlim_max)
1800                 return -EINVAL;
1801         old_rlim = current->signal->rlim + resource;
1802         if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1803             !capable(CAP_SYS_RESOURCE))
1804                 return -EPERM;
1805         if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1806                 return -EPERM;
1807
1808         retval = security_task_setrlimit(resource, &new_rlim);
1809         if (retval)
1810                 return retval;
1811
1812         task_lock(current->group_leader);
1813         *old_rlim = new_rlim;
1814         task_unlock(current->group_leader);
1815
1816         if (resource != RLIMIT_CPU)
1817                 goto out;
1818
1819         /*
1820          * RLIMIT_CPU handling.   Note that the kernel fails to return an error
1821          * code if it rejected the user's attempt to set RLIMIT_CPU.  This is a
1822          * very long-standing error, and fixing it now risks breakage of
1823          * applications, so we live with it
1824          */
1825         if (new_rlim.rlim_cur == RLIM_INFINITY)
1826                 goto out;
1827
1828         it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
1829         if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
1830                 unsigned long rlim_cur = new_rlim.rlim_cur;
1831                 cputime_t cputime;
1832
1833                 if (rlim_cur == 0) {
1834                         /*
1835                          * The caller is asking for an immediate RLIMIT_CPU
1836                          * expiry.  But we use the zero value to mean "it was
1837                          * never set".  So let's cheat and make it one second
1838                          * instead
1839                          */
1840                         rlim_cur = 1;
1841                 }
1842                 cputime = secs_to_cputime(rlim_cur);
1843                 read_lock(&tasklist_lock);
1844                 spin_lock_irq(&current->sighand->siglock);
1845                 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
1846                 spin_unlock_irq(&current->sighand->siglock);
1847                 read_unlock(&tasklist_lock);
1848         }
1849 out:
1850         return 0;
1851 }
1852
1853 /*
1854  * It would make sense to put struct rusage in the task_struct,
1855  * except that would make the task_struct be *really big*.  After
1856  * task_struct gets moved into malloc'ed memory, it would
1857  * make sense to do this.  It will make moving the rest of the information
1858  * a lot simpler!  (Which we're not doing right now because we're not
1859  * measuring them yet).
1860  *
1861  * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1862  * races with threads incrementing their own counters.  But since word
1863  * reads are atomic, we either get new values or old values and we don't
1864  * care which for the sums.  We always take the siglock to protect reading
1865  * the c* fields from p->signal from races with exit.c updating those
1866  * fields when reaping, so a sample either gets all the additions of a
1867  * given child after it's reaped, or none so this sample is before reaping.
1868  *
1869  * tasklist_lock locking optimisation:
1870  * If we are current and single threaded, we do not need to take the tasklist
1871  * lock or the siglock.  No one else can take our signal_struct away,
1872  * no one else can reap the children to update signal->c* counters, and
1873  * no one else can race with the signal-> fields.
1874  * If we do not take the tasklist_lock, the signal-> fields could be read
1875  * out of order while another thread was just exiting. So we place a
1876  * read memory barrier when we avoid the lock.  On the writer side,
1877  * write memory barrier is implied in  __exit_signal as __exit_signal releases
1878  * the siglock spinlock after updating the signal-> fields.
1879  *
1880  * We don't really need the siglock when we access the non c* fields
1881  * of the signal_struct (for RUSAGE_SELF) even in multithreaded
1882  * case, since we take the tasklist lock for read and the non c* signal->
1883  * fields are updated only in __exit_signal, which is called with
1884  * tasklist_lock taken for write, hence these two threads cannot execute
1885  * concurrently.
1886  *
1887  */
1888
1889 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1890 {
1891         struct task_struct *t;
1892         unsigned long flags;
1893         cputime_t utime, stime;
1894         int need_lock = 0;
1895
1896         memset((char *) r, 0, sizeof *r);
1897         utime = stime = cputime_zero;
1898
1899         if (p != current || !thread_group_empty(p))
1900                 need_lock = 1;
1901
1902         if (need_lock) {
1903                 read_lock(&tasklist_lock);
1904                 if (unlikely(!p->signal)) {
1905                         read_unlock(&tasklist_lock);
1906                         return;
1907                 }
1908         } else
1909                 /* See locking comments above */
1910                 smp_rmb();
1911
1912         switch (who) {
1913                 case RUSAGE_BOTH:
1914                 case RUSAGE_CHILDREN:
1915                         spin_lock_irqsave(&p->sighand->siglock, flags);
1916                         utime = p->signal->cutime;
1917                         stime = p->signal->cstime;
1918                         r->ru_nvcsw = p->signal->cnvcsw;
1919                         r->ru_nivcsw = p->signal->cnivcsw;
1920                         r->ru_minflt = p->signal->cmin_flt;
1921                         r->ru_majflt = p->signal->cmaj_flt;
1922                         spin_unlock_irqrestore(&p->sighand->siglock, flags);
1923
1924                         if (who == RUSAGE_CHILDREN)
1925                                 break;
1926
1927                 case RUSAGE_SELF:
1928                         utime = cputime_add(utime, p->signal->utime);
1929                         stime = cputime_add(stime, p->signal->stime);
1930                         r->ru_nvcsw += p->signal->nvcsw;
1931                         r->ru_nivcsw += p->signal->nivcsw;
1932                         r->ru_minflt += p->signal->min_flt;
1933                         r->ru_majflt += p->signal->maj_flt;
1934                         t = p;
1935                         do {
1936                                 utime = cputime_add(utime, t->utime);
1937                                 stime = cputime_add(stime, t->stime);
1938                                 r->ru_nvcsw += t->nvcsw;
1939                                 r->ru_nivcsw += t->nivcsw;
1940                                 r->ru_minflt += t->min_flt;
1941                                 r->ru_majflt += t->maj_flt;
1942                                 t = next_thread(t);
1943                         } while (t != p);
1944                         break;
1945
1946                 default:
1947                         BUG();
1948         }
1949
1950         if (need_lock)
1951                 read_unlock(&tasklist_lock);
1952         cputime_to_timeval(utime, &r->ru_utime);
1953         cputime_to_timeval(stime, &r->ru_stime);
1954 }
1955
1956 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1957 {
1958         struct rusage r;
1959         k_getrusage(p, who, &r);
1960         return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1961 }
1962
1963 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1964 {
1965         if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1966                 return -EINVAL;
1967         return getrusage(current, who, ru);
1968 }
1969
1970 asmlinkage long sys_umask(int mask)
1971 {
1972         mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1973         return mask;
1974 }
1975     
1976 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1977                           unsigned long arg4, unsigned long arg5)
1978 {
1979         long error;
1980
1981         error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1982         if (error)
1983                 return error;
1984
1985         switch (option) {
1986                 case PR_SET_PDEATHSIG:
1987                         if (!valid_signal(arg2)) {
1988                                 error = -EINVAL;
1989                                 break;
1990                         }
1991                         current->pdeath_signal = arg2;
1992                         break;
1993                 case PR_GET_PDEATHSIG:
1994                         error = put_user(current->pdeath_signal, (int __user *)arg2);
1995                         break;
1996                 case PR_GET_DUMPABLE:
1997                         error = current->mm->dumpable;
1998                         break;
1999                 case PR_SET_DUMPABLE:
2000                         if (arg2 < 0 || arg2 > 2) {
2001                                 error = -EINVAL;
2002                                 break;
2003                         }
2004                         current->mm->dumpable = arg2;
2005                         break;
2006
2007                 case PR_SET_UNALIGN:
2008                         error = SET_UNALIGN_CTL(current, arg2);
2009                         break;
2010                 case PR_GET_UNALIGN:
2011                         error = GET_UNALIGN_CTL(current, arg2);
2012                         break;
2013                 case PR_SET_FPEMU:
2014                         error = SET_FPEMU_CTL(current, arg2);
2015                         break;
2016                 case PR_GET_FPEMU:
2017                         error = GET_FPEMU_CTL(current, arg2);
2018                         break;
2019                 case PR_SET_FPEXC:
2020                         error = SET_FPEXC_CTL(current, arg2);
2021                         break;
2022                 case PR_GET_FPEXC:
2023                         error = GET_FPEXC_CTL(current, arg2);
2024                         break;
2025                 case PR_GET_TIMING:
2026                         error = PR_TIMING_STATISTICAL;
2027                         break;
2028                 case PR_SET_TIMING:
2029                         if (arg2 == PR_TIMING_STATISTICAL)
2030                                 error = 0;
2031                         else
2032                                 error = -EINVAL;
2033                         break;
2034
2035                 case PR_GET_KEEPCAPS:
2036                         if (current->keep_capabilities)
2037                                 error = 1;
2038                         break;
2039                 case PR_SET_KEEPCAPS:
2040                         if (arg2 != 0 && arg2 != 1) {
2041                                 error = -EINVAL;
2042                                 break;
2043                         }
2044                         current->keep_capabilities = arg2;
2045                         break;
2046                 case PR_SET_NAME: {
2047                         struct task_struct *me = current;
2048                         unsigned char ncomm[sizeof(me->comm)];
2049
2050                         ncomm[sizeof(me->comm)-1] = 0;
2051                         if (strncpy_from_user(ncomm, (char __user *)arg2,
2052                                                 sizeof(me->comm)-1) < 0)
2053                                 return -EFAULT;
2054                         set_task_comm(me, ncomm);
2055                         return 0;
2056                 }
2057                 case PR_GET_NAME: {
2058                         struct task_struct *me = current;
2059                         unsigned char tcomm[sizeof(me->comm)];
2060
2061                         get_task_comm(tcomm, me);
2062                         if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2063                                 return -EFAULT;
2064                         return 0;
2065                 }
2066                 case PR_GET_ENDIAN:
2067                         error = GET_ENDIAN(current, arg2);
2068                         break;
2069                 case PR_SET_ENDIAN:
2070                         error = SET_ENDIAN(current, arg2);
2071                         break;
2072
2073                 default:
2074                         error = -EINVAL;
2075                         break;
2076         }
2077         return error;
2078 }