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