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