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