]> git.karo-electronics.de Git - karo-tx-linux.git/blob - kernel/sys.c
ima: remove ACPI dependency
[karo-tx-linux.git] / kernel / sys.c
1 /*
2  *  linux/kernel/sys.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/module.h>
8 #include <linux/mm.h>
9 #include <linux/utsname.h>
10 #include <linux/mman.h>
11 #include <linux/notifier.h>
12 #include <linux/reboot.h>
13 #include <linux/prctl.h>
14 #include <linux/highuid.h>
15 #include <linux/fs.h>
16 #include <linux/perf_event.h>
17 #include <linux/resource.h>
18 #include <linux/kernel.h>
19 #include <linux/kexec.h>
20 #include <linux/workqueue.h>
21 #include <linux/capability.h>
22 #include <linux/device.h>
23 #include <linux/key.h>
24 #include <linux/times.h>
25 #include <linux/posix-timers.h>
26 #include <linux/security.h>
27 #include <linux/dcookies.h>
28 #include <linux/suspend.h>
29 #include <linux/tty.h>
30 #include <linux/signal.h>
31 #include <linux/cn_proc.h>
32 #include <linux/getcpu.h>
33 #include <linux/task_io_accounting_ops.h>
34 #include <linux/seccomp.h>
35 #include <linux/cpu.h>
36 #include <linux/personality.h>
37 #include <linux/ptrace.h>
38 #include <linux/fs_struct.h>
39
40 #include <linux/compat.h>
41 #include <linux/syscalls.h>
42 #include <linux/kprobes.h>
43 #include <linux/user_namespace.h>
44
45 #include <asm/uaccess.h>
46 #include <asm/io.h>
47 #include <asm/unistd.h>
48
49 #ifndef SET_UNALIGN_CTL
50 # define SET_UNALIGN_CTL(a,b)   (-EINVAL)
51 #endif
52 #ifndef GET_UNALIGN_CTL
53 # define GET_UNALIGN_CTL(a,b)   (-EINVAL)
54 #endif
55 #ifndef SET_FPEMU_CTL
56 # define SET_FPEMU_CTL(a,b)     (-EINVAL)
57 #endif
58 #ifndef GET_FPEMU_CTL
59 # define GET_FPEMU_CTL(a,b)     (-EINVAL)
60 #endif
61 #ifndef SET_FPEXC_CTL
62 # define SET_FPEXC_CTL(a,b)     (-EINVAL)
63 #endif
64 #ifndef GET_FPEXC_CTL
65 # define GET_FPEXC_CTL(a,b)     (-EINVAL)
66 #endif
67 #ifndef GET_ENDIAN
68 # define GET_ENDIAN(a,b)        (-EINVAL)
69 #endif
70 #ifndef SET_ENDIAN
71 # define SET_ENDIAN(a,b)        (-EINVAL)
72 #endif
73 #ifndef GET_TSC_CTL
74 # define GET_TSC_CTL(a)         (-EINVAL)
75 #endif
76 #ifndef SET_TSC_CTL
77 # define SET_TSC_CTL(a)         (-EINVAL)
78 #endif
79
80 /*
81  * this is where the system-wide overflow UID and GID are defined, for
82  * architectures that now have 32-bit UID/GID but didn't in the past
83  */
84
85 int overflowuid = DEFAULT_OVERFLOWUID;
86 int overflowgid = DEFAULT_OVERFLOWGID;
87
88 #ifdef CONFIG_UID16
89 EXPORT_SYMBOL(overflowuid);
90 EXPORT_SYMBOL(overflowgid);
91 #endif
92
93 /*
94  * the same as above, but for filesystems which can only store a 16-bit
95  * UID and GID. as such, this is needed on all architectures
96  */
97
98 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
99 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
100
101 EXPORT_SYMBOL(fs_overflowuid);
102 EXPORT_SYMBOL(fs_overflowgid);
103
104 /*
105  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
106  */
107
108 int C_A_D = 1;
109 struct pid *cad_pid;
110 EXPORT_SYMBOL(cad_pid);
111
112 /*
113  * If set, this is used for preparing the system to power off.
114  */
115
116 void (*pm_power_off_prepare)(void);
117
118 /*
119  * set the priority of a task
120  * - the caller must hold the RCU read lock
121  */
122 static int set_one_prio(struct task_struct *p, int niceval, int error)
123 {
124         const struct cred *cred = current_cred(), *pcred = __task_cred(p);
125         int no_nice;
126
127         if (pcred->uid  != cred->euid &&
128             pcred->euid != cred->euid && !capable(CAP_SYS_NICE)) {
129                 error = -EPERM;
130                 goto out;
131         }
132         if (niceval < task_nice(p) && !can_nice(p, niceval)) {
133                 error = -EACCES;
134                 goto out;
135         }
136         no_nice = security_task_setnice(p, niceval);
137         if (no_nice) {
138                 error = no_nice;
139                 goto out;
140         }
141         if (error == -ESRCH)
142                 error = 0;
143         set_user_nice(p, niceval);
144 out:
145         return error;
146 }
147
148 SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
149 {
150         struct task_struct *g, *p;
151         struct user_struct *user;
152         const struct cred *cred = current_cred();
153         int error = -EINVAL;
154         struct pid *pgrp;
155
156         if (which > PRIO_USER || which < PRIO_PROCESS)
157                 goto out;
158
159         /* normalize: avoid signed division (rounding problems) */
160         error = -ESRCH;
161         if (niceval < -20)
162                 niceval = -20;
163         if (niceval > 19)
164                 niceval = 19;
165
166         rcu_read_lock();
167         read_lock(&tasklist_lock);
168         switch (which) {
169                 case PRIO_PROCESS:
170                         if (who)
171                                 p = find_task_by_vpid(who);
172                         else
173                                 p = current;
174                         if (p)
175                                 error = set_one_prio(p, niceval, error);
176                         break;
177                 case PRIO_PGRP:
178                         if (who)
179                                 pgrp = find_vpid(who);
180                         else
181                                 pgrp = task_pgrp(current);
182                         do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
183                                 error = set_one_prio(p, niceval, error);
184                         } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
185                         break;
186                 case PRIO_USER:
187                         user = (struct user_struct *) cred->user;
188                         if (!who)
189                                 who = cred->uid;
190                         else if ((who != cred->uid) &&
191                                  !(user = find_user(who)))
192                                 goto out_unlock;        /* No processes for this user */
193
194                         do_each_thread(g, p) {
195                                 if (__task_cred(p)->uid == who)
196                                         error = set_one_prio(p, niceval, error);
197                         } while_each_thread(g, p);
198                         if (who != cred->uid)
199                                 free_uid(user);         /* For find_user() */
200                         break;
201         }
202 out_unlock:
203         read_unlock(&tasklist_lock);
204         rcu_read_unlock();
205 out:
206         return error;
207 }
208
209 /*
210  * Ugh. To avoid negative return values, "getpriority()" will
211  * not return the normal nice-value, but a negated value that
212  * has been offset by 20 (ie it returns 40..1 instead of -20..19)
213  * to stay compatible.
214  */
215 SYSCALL_DEFINE2(getpriority, int, which, int, who)
216 {
217         struct task_struct *g, *p;
218         struct user_struct *user;
219         const struct cred *cred = current_cred();
220         long niceval, retval = -ESRCH;
221         struct pid *pgrp;
222
223         if (which > PRIO_USER || which < PRIO_PROCESS)
224                 return -EINVAL;
225
226         rcu_read_lock();
227         read_lock(&tasklist_lock);
228         switch (which) {
229                 case PRIO_PROCESS:
230                         if (who)
231                                 p = find_task_by_vpid(who);
232                         else
233                                 p = current;
234                         if (p) {
235                                 niceval = 20 - task_nice(p);
236                                 if (niceval > retval)
237                                         retval = niceval;
238                         }
239                         break;
240                 case PRIO_PGRP:
241                         if (who)
242                                 pgrp = find_vpid(who);
243                         else
244                                 pgrp = task_pgrp(current);
245                         do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
246                                 niceval = 20 - task_nice(p);
247                                 if (niceval > retval)
248                                         retval = niceval;
249                         } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
250                         break;
251                 case PRIO_USER:
252                         user = (struct user_struct *) cred->user;
253                         if (!who)
254                                 who = cred->uid;
255                         else if ((who != cred->uid) &&
256                                  !(user = find_user(who)))
257                                 goto out_unlock;        /* No processes for this user */
258
259                         do_each_thread(g, p) {
260                                 if (__task_cred(p)->uid == who) {
261                                         niceval = 20 - task_nice(p);
262                                         if (niceval > retval)
263                                                 retval = niceval;
264                                 }
265                         } while_each_thread(g, p);
266                         if (who != cred->uid)
267                                 free_uid(user);         /* for find_user() */
268                         break;
269         }
270 out_unlock:
271         read_unlock(&tasklist_lock);
272         rcu_read_unlock();
273
274         return retval;
275 }
276
277 /**
278  *      emergency_restart - reboot the system
279  *
280  *      Without shutting down any hardware or taking any locks
281  *      reboot the system.  This is called when we know we are in
282  *      trouble so this is our best effort to reboot.  This is
283  *      safe to call in interrupt context.
284  */
285 void emergency_restart(void)
286 {
287         machine_emergency_restart();
288 }
289 EXPORT_SYMBOL_GPL(emergency_restart);
290
291 void kernel_restart_prepare(char *cmd)
292 {
293         blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
294         system_state = SYSTEM_RESTART;
295         device_shutdown();
296         sysdev_shutdown();
297 }
298
299 /**
300  *      kernel_restart - reboot the system
301  *      @cmd: pointer to buffer containing command to execute for restart
302  *              or %NULL
303  *
304  *      Shutdown everything and perform a clean reboot.
305  *      This is not safe to call in interrupt context.
306  */
307 void kernel_restart(char *cmd)
308 {
309         kernel_restart_prepare(cmd);
310         if (!cmd)
311                 printk(KERN_EMERG "Restarting system.\n");
312         else
313                 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
314         machine_restart(cmd);
315 }
316 EXPORT_SYMBOL_GPL(kernel_restart);
317
318 static void kernel_shutdown_prepare(enum system_states state)
319 {
320         blocking_notifier_call_chain(&reboot_notifier_list,
321                 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
322         system_state = state;
323         device_shutdown();
324 }
325 /**
326  *      kernel_halt - halt the system
327  *
328  *      Shutdown everything and perform a clean system halt.
329  */
330 void kernel_halt(void)
331 {
332         kernel_shutdown_prepare(SYSTEM_HALT);
333         sysdev_shutdown();
334         printk(KERN_EMERG "System halted.\n");
335         machine_halt();
336 }
337
338 EXPORT_SYMBOL_GPL(kernel_halt);
339
340 /**
341  *      kernel_power_off - power_off the system
342  *
343  *      Shutdown everything and perform a clean system power_off.
344  */
345 void kernel_power_off(void)
346 {
347         kernel_shutdown_prepare(SYSTEM_POWER_OFF);
348         if (pm_power_off_prepare)
349                 pm_power_off_prepare();
350         disable_nonboot_cpus();
351         sysdev_shutdown();
352         printk(KERN_EMERG "Power down.\n");
353         machine_power_off();
354 }
355 EXPORT_SYMBOL_GPL(kernel_power_off);
356
357 static DEFINE_MUTEX(reboot_mutex);
358
359 /*
360  * Reboot system call: for obvious reasons only root may call it,
361  * and even root needs to set up some magic numbers in the registers
362  * so that some mistake won't make this reboot the whole machine.
363  * You can also set the meaning of the ctrl-alt-del-key here.
364  *
365  * reboot doesn't sync: do that yourself before calling this.
366  */
367 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
368                 void __user *, arg)
369 {
370         char buffer[256];
371         int ret = 0;
372
373         /* We only trust the superuser with rebooting the system. */
374         if (!capable(CAP_SYS_BOOT))
375                 return -EPERM;
376
377         /* For safety, we require "magic" arguments. */
378         if (magic1 != LINUX_REBOOT_MAGIC1 ||
379             (magic2 != LINUX_REBOOT_MAGIC2 &&
380                         magic2 != LINUX_REBOOT_MAGIC2A &&
381                         magic2 != LINUX_REBOOT_MAGIC2B &&
382                         magic2 != LINUX_REBOOT_MAGIC2C))
383                 return -EINVAL;
384
385         /* Instead of trying to make the power_off code look like
386          * halt when pm_power_off is not set do it the easy way.
387          */
388         if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
389                 cmd = LINUX_REBOOT_CMD_HALT;
390
391         mutex_lock(&reboot_mutex);
392         switch (cmd) {
393         case LINUX_REBOOT_CMD_RESTART:
394                 kernel_restart(NULL);
395                 break;
396
397         case LINUX_REBOOT_CMD_CAD_ON:
398                 C_A_D = 1;
399                 break;
400
401         case LINUX_REBOOT_CMD_CAD_OFF:
402                 C_A_D = 0;
403                 break;
404
405         case LINUX_REBOOT_CMD_HALT:
406                 kernel_halt();
407                 do_exit(0);
408                 panic("cannot halt");
409
410         case LINUX_REBOOT_CMD_POWER_OFF:
411                 kernel_power_off();
412                 do_exit(0);
413                 break;
414
415         case LINUX_REBOOT_CMD_RESTART2:
416                 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
417                         ret = -EFAULT;
418                         break;
419                 }
420                 buffer[sizeof(buffer) - 1] = '\0';
421
422                 kernel_restart(buffer);
423                 break;
424
425 #ifdef CONFIG_KEXEC
426         case LINUX_REBOOT_CMD_KEXEC:
427                 ret = kernel_kexec();
428                 break;
429 #endif
430
431 #ifdef CONFIG_HIBERNATION
432         case LINUX_REBOOT_CMD_SW_SUSPEND:
433                 ret = hibernate();
434                 break;
435 #endif
436
437         default:
438                 ret = -EINVAL;
439                 break;
440         }
441         mutex_unlock(&reboot_mutex);
442         return ret;
443 }
444
445 static void deferred_cad(struct work_struct *dummy)
446 {
447         kernel_restart(NULL);
448 }
449
450 /*
451  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
452  * As it's called within an interrupt, it may NOT sync: the only choice
453  * is whether to reboot at once, or just ignore the ctrl-alt-del.
454  */
455 void ctrl_alt_del(void)
456 {
457         static DECLARE_WORK(cad_work, deferred_cad);
458
459         if (C_A_D)
460                 schedule_work(&cad_work);
461         else
462                 kill_cad_pid(SIGINT, 1);
463 }
464         
465 /*
466  * Unprivileged users may change the real gid to the effective gid
467  * or vice versa.  (BSD-style)
468  *
469  * If you set the real gid at all, or set the effective gid to a value not
470  * equal to the real gid, then the saved gid is set to the new effective gid.
471  *
472  * This makes it possible for a setgid program to completely drop its
473  * privileges, which is often a useful assertion to make when you are doing
474  * a security audit over a program.
475  *
476  * The general idea is that a program which uses just setregid() will be
477  * 100% compatible with BSD.  A program which uses just setgid() will be
478  * 100% compatible with POSIX with saved IDs. 
479  *
480  * SMP: There are not races, the GIDs are checked only by filesystem
481  *      operations (as far as semantic preservation is concerned).
482  */
483 SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
484 {
485         const struct cred *old;
486         struct cred *new;
487         int retval;
488
489         new = prepare_creds();
490         if (!new)
491                 return -ENOMEM;
492         old = current_cred();
493
494         retval = -EPERM;
495         if (rgid != (gid_t) -1) {
496                 if (old->gid == rgid ||
497                     old->egid == rgid ||
498                     capable(CAP_SETGID))
499                         new->gid = rgid;
500                 else
501                         goto error;
502         }
503         if (egid != (gid_t) -1) {
504                 if (old->gid == egid ||
505                     old->egid == egid ||
506                     old->sgid == egid ||
507                     capable(CAP_SETGID))
508                         new->egid = egid;
509                 else
510                         goto error;
511         }
512
513         if (rgid != (gid_t) -1 ||
514             (egid != (gid_t) -1 && egid != old->gid))
515                 new->sgid = new->egid;
516         new->fsgid = new->egid;
517
518         return commit_creds(new);
519
520 error:
521         abort_creds(new);
522         return retval;
523 }
524
525 /*
526  * setgid() is implemented like SysV w/ SAVED_IDS 
527  *
528  * SMP: Same implicit races as above.
529  */
530 SYSCALL_DEFINE1(setgid, gid_t, gid)
531 {
532         const struct cred *old;
533         struct cred *new;
534         int retval;
535
536         new = prepare_creds();
537         if (!new)
538                 return -ENOMEM;
539         old = current_cred();
540
541         retval = -EPERM;
542         if (capable(CAP_SETGID))
543                 new->gid = new->egid = new->sgid = new->fsgid = gid;
544         else if (gid == old->gid || gid == old->sgid)
545                 new->egid = new->fsgid = gid;
546         else
547                 goto error;
548
549         return commit_creds(new);
550
551 error:
552         abort_creds(new);
553         return retval;
554 }
555
556 /*
557  * change the user struct in a credentials set to match the new UID
558  */
559 static int set_user(struct cred *new)
560 {
561         struct user_struct *new_user;
562
563         new_user = alloc_uid(current_user_ns(), new->uid);
564         if (!new_user)
565                 return -EAGAIN;
566
567         if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
568                         new_user != INIT_USER) {
569                 free_uid(new_user);
570                 return -EAGAIN;
571         }
572
573         free_uid(new->user);
574         new->user = new_user;
575         return 0;
576 }
577
578 /*
579  * Unprivileged users may change the real uid to the effective uid
580  * or vice versa.  (BSD-style)
581  *
582  * If you set the real uid at all, or set the effective uid to a value not
583  * equal to the real uid, then the saved uid is set to the new effective uid.
584  *
585  * This makes it possible for a setuid program to completely drop its
586  * privileges, which is often a useful assertion to make when you are doing
587  * a security audit over a program.
588  *
589  * The general idea is that a program which uses just setreuid() will be
590  * 100% compatible with BSD.  A program which uses just setuid() will be
591  * 100% compatible with POSIX with saved IDs. 
592  */
593 SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
594 {
595         const struct cred *old;
596         struct cred *new;
597         int retval;
598
599         new = prepare_creds();
600         if (!new)
601                 return -ENOMEM;
602         old = current_cred();
603
604         retval = -EPERM;
605         if (ruid != (uid_t) -1) {
606                 new->uid = ruid;
607                 if (old->uid != ruid &&
608                     old->euid != ruid &&
609                     !capable(CAP_SETUID))
610                         goto error;
611         }
612
613         if (euid != (uid_t) -1) {
614                 new->euid = euid;
615                 if (old->uid != euid &&
616                     old->euid != euid &&
617                     old->suid != euid &&
618                     !capable(CAP_SETUID))
619                         goto error;
620         }
621
622         if (new->uid != old->uid) {
623                 retval = set_user(new);
624                 if (retval < 0)
625                         goto error;
626         }
627         if (ruid != (uid_t) -1 ||
628             (euid != (uid_t) -1 && euid != old->uid))
629                 new->suid = new->euid;
630         new->fsuid = new->euid;
631
632         retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
633         if (retval < 0)
634                 goto error;
635
636         return commit_creds(new);
637
638 error:
639         abort_creds(new);
640         return retval;
641 }
642                 
643 /*
644  * setuid() is implemented like SysV with SAVED_IDS 
645  * 
646  * Note that SAVED_ID's is deficient in that a setuid root program
647  * like sendmail, for example, cannot set its uid to be a normal 
648  * user and then switch back, because if you're root, setuid() sets
649  * the saved uid too.  If you don't like this, blame the bright people
650  * in the POSIX committee and/or USG.  Note that the BSD-style setreuid()
651  * will allow a root program to temporarily drop privileges and be able to
652  * regain them by swapping the real and effective uid.  
653  */
654 SYSCALL_DEFINE1(setuid, uid_t, uid)
655 {
656         const struct cred *old;
657         struct cred *new;
658         int retval;
659
660         new = prepare_creds();
661         if (!new)
662                 return -ENOMEM;
663         old = current_cred();
664
665         retval = -EPERM;
666         if (capable(CAP_SETUID)) {
667                 new->suid = new->uid = uid;
668                 if (uid != old->uid) {
669                         retval = set_user(new);
670                         if (retval < 0)
671                                 goto error;
672                 }
673         } else if (uid != old->uid && uid != new->suid) {
674                 goto error;
675         }
676
677         new->fsuid = new->euid = uid;
678
679         retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
680         if (retval < 0)
681                 goto error;
682
683         return commit_creds(new);
684
685 error:
686         abort_creds(new);
687         return retval;
688 }
689
690
691 /*
692  * This function implements a generic ability to update ruid, euid,
693  * and suid.  This allows you to implement the 4.4 compatible seteuid().
694  */
695 SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
696 {
697         const struct cred *old;
698         struct cred *new;
699         int retval;
700
701         new = prepare_creds();
702         if (!new)
703                 return -ENOMEM;
704
705         old = current_cred();
706
707         retval = -EPERM;
708         if (!capable(CAP_SETUID)) {
709                 if (ruid != (uid_t) -1 && ruid != old->uid &&
710                     ruid != old->euid  && ruid != old->suid)
711                         goto error;
712                 if (euid != (uid_t) -1 && euid != old->uid &&
713                     euid != old->euid  && euid != old->suid)
714                         goto error;
715                 if (suid != (uid_t) -1 && suid != old->uid &&
716                     suid != old->euid  && suid != old->suid)
717                         goto error;
718         }
719
720         if (ruid != (uid_t) -1) {
721                 new->uid = ruid;
722                 if (ruid != old->uid) {
723                         retval = set_user(new);
724                         if (retval < 0)
725                                 goto error;
726                 }
727         }
728         if (euid != (uid_t) -1)
729                 new->euid = euid;
730         if (suid != (uid_t) -1)
731                 new->suid = suid;
732         new->fsuid = new->euid;
733
734         retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
735         if (retval < 0)
736                 goto error;
737
738         return commit_creds(new);
739
740 error:
741         abort_creds(new);
742         return retval;
743 }
744
745 SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
746 {
747         const struct cred *cred = current_cred();
748         int retval;
749
750         if (!(retval   = put_user(cred->uid,  ruid)) &&
751             !(retval   = put_user(cred->euid, euid)))
752                 retval = put_user(cred->suid, suid);
753
754         return retval;
755 }
756
757 /*
758  * Same as above, but for rgid, egid, sgid.
759  */
760 SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
761 {
762         const struct cred *old;
763         struct cred *new;
764         int retval;
765
766         new = prepare_creds();
767         if (!new)
768                 return -ENOMEM;
769         old = current_cred();
770
771         retval = -EPERM;
772         if (!capable(CAP_SETGID)) {
773                 if (rgid != (gid_t) -1 && rgid != old->gid &&
774                     rgid != old->egid  && rgid != old->sgid)
775                         goto error;
776                 if (egid != (gid_t) -1 && egid != old->gid &&
777                     egid != old->egid  && egid != old->sgid)
778                         goto error;
779                 if (sgid != (gid_t) -1 && sgid != old->gid &&
780                     sgid != old->egid  && sgid != old->sgid)
781                         goto error;
782         }
783
784         if (rgid != (gid_t) -1)
785                 new->gid = rgid;
786         if (egid != (gid_t) -1)
787                 new->egid = egid;
788         if (sgid != (gid_t) -1)
789                 new->sgid = sgid;
790         new->fsgid = new->egid;
791
792         return commit_creds(new);
793
794 error:
795         abort_creds(new);
796         return retval;
797 }
798
799 SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
800 {
801         const struct cred *cred = current_cred();
802         int retval;
803
804         if (!(retval   = put_user(cred->gid,  rgid)) &&
805             !(retval   = put_user(cred->egid, egid)))
806                 retval = put_user(cred->sgid, sgid);
807
808         return retval;
809 }
810
811
812 /*
813  * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
814  * is used for "access()" and for the NFS daemon (letting nfsd stay at
815  * whatever uid it wants to). It normally shadows "euid", except when
816  * explicitly set by setfsuid() or for access..
817  */
818 SYSCALL_DEFINE1(setfsuid, uid_t, uid)
819 {
820         const struct cred *old;
821         struct cred *new;
822         uid_t old_fsuid;
823
824         new = prepare_creds();
825         if (!new)
826                 return current_fsuid();
827         old = current_cred();
828         old_fsuid = old->fsuid;
829
830         if (uid == old->uid  || uid == old->euid  ||
831             uid == old->suid || uid == old->fsuid ||
832             capable(CAP_SETUID)) {
833                 if (uid != old_fsuid) {
834                         new->fsuid = uid;
835                         if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
836                                 goto change_okay;
837                 }
838         }
839
840         abort_creds(new);
841         return old_fsuid;
842
843 change_okay:
844         commit_creds(new);
845         return old_fsuid;
846 }
847
848 /*
849  * Samma pÃ¥ svenska..
850  */
851 SYSCALL_DEFINE1(setfsgid, gid_t, gid)
852 {
853         const struct cred *old;
854         struct cred *new;
855         gid_t old_fsgid;
856
857         new = prepare_creds();
858         if (!new)
859                 return current_fsgid();
860         old = current_cred();
861         old_fsgid = old->fsgid;
862
863         if (gid == old->gid  || gid == old->egid  ||
864             gid == old->sgid || gid == old->fsgid ||
865             capable(CAP_SETGID)) {
866                 if (gid != old_fsgid) {
867                         new->fsgid = gid;
868                         goto change_okay;
869                 }
870         }
871
872         abort_creds(new);
873         return old_fsgid;
874
875 change_okay:
876         commit_creds(new);
877         return old_fsgid;
878 }
879
880 void do_sys_times(struct tms *tms)
881 {
882         cputime_t tgutime, tgstime, cutime, cstime;
883
884         spin_lock_irq(&current->sighand->siglock);
885         thread_group_times(current, &tgutime, &tgstime);
886         cutime = current->signal->cutime;
887         cstime = current->signal->cstime;
888         spin_unlock_irq(&current->sighand->siglock);
889         tms->tms_utime = cputime_to_clock_t(tgutime);
890         tms->tms_stime = cputime_to_clock_t(tgstime);
891         tms->tms_cutime = cputime_to_clock_t(cutime);
892         tms->tms_cstime = cputime_to_clock_t(cstime);
893 }
894
895 SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
896 {
897         if (tbuf) {
898                 struct tms tmp;
899
900                 do_sys_times(&tmp);
901                 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
902                         return -EFAULT;
903         }
904         force_successful_syscall_return();
905         return (long) jiffies_64_to_clock_t(get_jiffies_64());
906 }
907
908 /*
909  * This needs some heavy checking ...
910  * I just haven't the stomach for it. I also don't fully
911  * understand sessions/pgrp etc. Let somebody who does explain it.
912  *
913  * OK, I think I have the protection semantics right.... this is really
914  * only important on a multi-user system anyway, to make sure one user
915  * can't send a signal to a process owned by another.  -TYT, 12/12/91
916  *
917  * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
918  * LBT 04.03.94
919  */
920 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
921 {
922         struct task_struct *p;
923         struct task_struct *group_leader = current->group_leader;
924         struct pid *pgrp;
925         int err;
926
927         if (!pid)
928                 pid = task_pid_vnr(group_leader);
929         if (!pgid)
930                 pgid = pid;
931         if (pgid < 0)
932                 return -EINVAL;
933
934         /* From this point forward we keep holding onto the tasklist lock
935          * so that our parent does not change from under us. -DaveM
936          */
937         write_lock_irq(&tasklist_lock);
938
939         err = -ESRCH;
940         p = find_task_by_vpid(pid);
941         if (!p)
942                 goto out;
943
944         err = -EINVAL;
945         if (!thread_group_leader(p))
946                 goto out;
947
948         if (same_thread_group(p->real_parent, group_leader)) {
949                 err = -EPERM;
950                 if (task_session(p) != task_session(group_leader))
951                         goto out;
952                 err = -EACCES;
953                 if (p->did_exec)
954                         goto out;
955         } else {
956                 err = -ESRCH;
957                 if (p != group_leader)
958                         goto out;
959         }
960
961         err = -EPERM;
962         if (p->signal->leader)
963                 goto out;
964
965         pgrp = task_pid(p);
966         if (pgid != pid) {
967                 struct task_struct *g;
968
969                 pgrp = find_vpid(pgid);
970                 g = pid_task(pgrp, PIDTYPE_PGID);
971                 if (!g || task_session(g) != task_session(group_leader))
972                         goto out;
973         }
974
975         err = security_task_setpgid(p, pgid);
976         if (err)
977                 goto out;
978
979         if (task_pgrp(p) != pgrp)
980                 change_pid(p, PIDTYPE_PGID, pgrp);
981
982         err = 0;
983 out:
984         /* All paths lead to here, thus we are safe. -DaveM */
985         write_unlock_irq(&tasklist_lock);
986         return err;
987 }
988
989 SYSCALL_DEFINE1(getpgid, pid_t, pid)
990 {
991         struct task_struct *p;
992         struct pid *grp;
993         int retval;
994
995         rcu_read_lock();
996         if (!pid)
997                 grp = task_pgrp(current);
998         else {
999                 retval = -ESRCH;
1000                 p = find_task_by_vpid(pid);
1001                 if (!p)
1002                         goto out;
1003                 grp = task_pgrp(p);
1004                 if (!grp)
1005                         goto out;
1006
1007                 retval = security_task_getpgid(p);
1008                 if (retval)
1009                         goto out;
1010         }
1011         retval = pid_vnr(grp);
1012 out:
1013         rcu_read_unlock();
1014         return retval;
1015 }
1016
1017 #ifdef __ARCH_WANT_SYS_GETPGRP
1018
1019 SYSCALL_DEFINE0(getpgrp)
1020 {
1021         return sys_getpgid(0);
1022 }
1023
1024 #endif
1025
1026 SYSCALL_DEFINE1(getsid, pid_t, pid)
1027 {
1028         struct task_struct *p;
1029         struct pid *sid;
1030         int retval;
1031
1032         rcu_read_lock();
1033         if (!pid)
1034                 sid = task_session(current);
1035         else {
1036                 retval = -ESRCH;
1037                 p = find_task_by_vpid(pid);
1038                 if (!p)
1039                         goto out;
1040                 sid = task_session(p);
1041                 if (!sid)
1042                         goto out;
1043
1044                 retval = security_task_getsid(p);
1045                 if (retval)
1046                         goto out;
1047         }
1048         retval = pid_vnr(sid);
1049 out:
1050         rcu_read_unlock();
1051         return retval;
1052 }
1053
1054 SYSCALL_DEFINE0(setsid)
1055 {
1056         struct task_struct *group_leader = current->group_leader;
1057         struct pid *sid = task_pid(group_leader);
1058         pid_t session = pid_vnr(sid);
1059         int err = -EPERM;
1060
1061         write_lock_irq(&tasklist_lock);
1062         /* Fail if I am already a session leader */
1063         if (group_leader->signal->leader)
1064                 goto out;
1065
1066         /* Fail if a process group id already exists that equals the
1067          * proposed session id.
1068          */
1069         if (pid_task(sid, PIDTYPE_PGID))
1070                 goto out;
1071
1072         group_leader->signal->leader = 1;
1073         __set_special_pids(sid);
1074
1075         proc_clear_tty(group_leader);
1076
1077         err = session;
1078 out:
1079         write_unlock_irq(&tasklist_lock);
1080         if (err > 0)
1081                 proc_sid_connector(group_leader);
1082         return err;
1083 }
1084
1085 DECLARE_RWSEM(uts_sem);
1086
1087 #ifdef COMPAT_UTS_MACHINE
1088 #define override_architecture(name) \
1089         (current->personality == PER_LINUX32 && \
1090          copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1091                       sizeof(COMPAT_UTS_MACHINE)))
1092 #else
1093 #define override_architecture(name)     0
1094 #endif
1095
1096 SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
1097 {
1098         int errno = 0;
1099
1100         down_read(&uts_sem);
1101         if (copy_to_user(name, utsname(), sizeof *name))
1102                 errno = -EFAULT;
1103         up_read(&uts_sem);
1104
1105         if (!errno && override_architecture(name))
1106                 errno = -EFAULT;
1107         return errno;
1108 }
1109
1110 #ifdef __ARCH_WANT_SYS_OLD_UNAME
1111 /*
1112  * Old cruft
1113  */
1114 SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1115 {
1116         int error = 0;
1117
1118         if (!name)
1119                 return -EFAULT;
1120
1121         down_read(&uts_sem);
1122         if (copy_to_user(name, utsname(), sizeof(*name)))
1123                 error = -EFAULT;
1124         up_read(&uts_sem);
1125
1126         if (!error && override_architecture(name))
1127                 error = -EFAULT;
1128         return error;
1129 }
1130
1131 SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1132 {
1133         int error;
1134
1135         if (!name)
1136                 return -EFAULT;
1137         if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1138                 return -EFAULT;
1139
1140         down_read(&uts_sem);
1141         error = __copy_to_user(&name->sysname, &utsname()->sysname,
1142                                __OLD_UTS_LEN);
1143         error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1144         error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1145                                 __OLD_UTS_LEN);
1146         error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1147         error |= __copy_to_user(&name->release, &utsname()->release,
1148                                 __OLD_UTS_LEN);
1149         error |= __put_user(0, name->release + __OLD_UTS_LEN);
1150         error |= __copy_to_user(&name->version, &utsname()->version,
1151                                 __OLD_UTS_LEN);
1152         error |= __put_user(0, name->version + __OLD_UTS_LEN);
1153         error |= __copy_to_user(&name->machine, &utsname()->machine,
1154                                 __OLD_UTS_LEN);
1155         error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1156         up_read(&uts_sem);
1157
1158         if (!error && override_architecture(name))
1159                 error = -EFAULT;
1160         return error ? -EFAULT : 0;
1161 }
1162 #endif
1163
1164 SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1165 {
1166         int errno;
1167         char tmp[__NEW_UTS_LEN];
1168
1169         if (!capable(CAP_SYS_ADMIN))
1170                 return -EPERM;
1171         if (len < 0 || len > __NEW_UTS_LEN)
1172                 return -EINVAL;
1173         down_write(&uts_sem);
1174         errno = -EFAULT;
1175         if (!copy_from_user(tmp, name, len)) {
1176                 struct new_utsname *u = utsname();
1177
1178                 memcpy(u->nodename, tmp, len);
1179                 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
1180                 errno = 0;
1181         }
1182         up_write(&uts_sem);
1183         return errno;
1184 }
1185
1186 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1187
1188 SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
1189 {
1190         int i, errno;
1191         struct new_utsname *u;
1192
1193         if (len < 0)
1194                 return -EINVAL;
1195         down_read(&uts_sem);
1196         u = utsname();
1197         i = 1 + strlen(u->nodename);
1198         if (i > len)
1199                 i = len;
1200         errno = 0;
1201         if (copy_to_user(name, u->nodename, i))
1202                 errno = -EFAULT;
1203         up_read(&uts_sem);
1204         return errno;
1205 }
1206
1207 #endif
1208
1209 /*
1210  * Only setdomainname; getdomainname can be implemented by calling
1211  * uname()
1212  */
1213 SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1214 {
1215         int errno;
1216         char tmp[__NEW_UTS_LEN];
1217
1218         if (!capable(CAP_SYS_ADMIN))
1219                 return -EPERM;
1220         if (len < 0 || len > __NEW_UTS_LEN)
1221                 return -EINVAL;
1222
1223         down_write(&uts_sem);
1224         errno = -EFAULT;
1225         if (!copy_from_user(tmp, name, len)) {
1226                 struct new_utsname *u = utsname();
1227
1228                 memcpy(u->domainname, tmp, len);
1229                 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
1230                 errno = 0;
1231         }
1232         up_write(&uts_sem);
1233         return errno;
1234 }
1235
1236 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1237 {
1238         if (resource >= RLIM_NLIMITS)
1239                 return -EINVAL;
1240         else {
1241                 struct rlimit value;
1242                 task_lock(current->group_leader);
1243                 value = current->signal->rlim[resource];
1244                 task_unlock(current->group_leader);
1245                 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1246         }
1247 }
1248
1249 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1250
1251 /*
1252  *      Back compatibility for getrlimit. Needed for some apps.
1253  */
1254  
1255 SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1256                 struct rlimit __user *, rlim)
1257 {
1258         struct rlimit x;
1259         if (resource >= RLIM_NLIMITS)
1260                 return -EINVAL;
1261
1262         task_lock(current->group_leader);
1263         x = current->signal->rlim[resource];
1264         task_unlock(current->group_leader);
1265         if (x.rlim_cur > 0x7FFFFFFF)
1266                 x.rlim_cur = 0x7FFFFFFF;
1267         if (x.rlim_max > 0x7FFFFFFF)
1268                 x.rlim_max = 0x7FFFFFFF;
1269         return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1270 }
1271
1272 #endif
1273
1274 SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1275 {
1276         struct rlimit new_rlim, *old_rlim;
1277         int retval;
1278
1279         if (resource >= RLIM_NLIMITS)
1280                 return -EINVAL;
1281         if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1282                 return -EFAULT;
1283         if (new_rlim.rlim_cur > new_rlim.rlim_max)
1284                 return -EINVAL;
1285         old_rlim = current->signal->rlim + resource;
1286         if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1287             !capable(CAP_SYS_RESOURCE))
1288                 return -EPERM;
1289         if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
1290                 return -EPERM;
1291
1292         retval = security_task_setrlimit(resource, &new_rlim);
1293         if (retval)
1294                 return retval;
1295
1296         if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
1297                 /*
1298                  * The caller is asking for an immediate RLIMIT_CPU
1299                  * expiry.  But we use the zero value to mean "it was
1300                  * never set".  So let's cheat and make it one second
1301                  * instead
1302                  */
1303                 new_rlim.rlim_cur = 1;
1304         }
1305
1306         task_lock(current->group_leader);
1307         *old_rlim = new_rlim;
1308         task_unlock(current->group_leader);
1309
1310         if (resource != RLIMIT_CPU)
1311                 goto out;
1312
1313         /*
1314          * RLIMIT_CPU handling.   Note that the kernel fails to return an error
1315          * code if it rejected the user's attempt to set RLIMIT_CPU.  This is a
1316          * very long-standing error, and fixing it now risks breakage of
1317          * applications, so we live with it
1318          */
1319         if (new_rlim.rlim_cur == RLIM_INFINITY)
1320                 goto out;
1321
1322         update_rlimit_cpu(new_rlim.rlim_cur);
1323 out:
1324         return 0;
1325 }
1326
1327 /*
1328  * It would make sense to put struct rusage in the task_struct,
1329  * except that would make the task_struct be *really big*.  After
1330  * task_struct gets moved into malloc'ed memory, it would
1331  * make sense to do this.  It will make moving the rest of the information
1332  * a lot simpler!  (Which we're not doing right now because we're not
1333  * measuring them yet).
1334  *
1335  * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1336  * races with threads incrementing their own counters.  But since word
1337  * reads are atomic, we either get new values or old values and we don't
1338  * care which for the sums.  We always take the siglock to protect reading
1339  * the c* fields from p->signal from races with exit.c updating those
1340  * fields when reaping, so a sample either gets all the additions of a
1341  * given child after it's reaped, or none so this sample is before reaping.
1342  *
1343  * Locking:
1344  * We need to take the siglock for CHILDEREN, SELF and BOTH
1345  * for  the cases current multithreaded, non-current single threaded
1346  * non-current multithreaded.  Thread traversal is now safe with
1347  * the siglock held.
1348  * Strictly speaking, we donot need to take the siglock if we are current and
1349  * single threaded,  as no one else can take our signal_struct away, no one
1350  * else can  reap the  children to update signal->c* counters, and no one else
1351  * can race with the signal-> fields. If we do not take any lock, the
1352  * signal-> fields could be read out of order while another thread was just
1353  * exiting. So we should  place a read memory barrier when we avoid the lock.
1354  * On the writer side,  write memory barrier is implied in  __exit_signal
1355  * as __exit_signal releases  the siglock spinlock after updating the signal->
1356  * fields. But we don't do this yet to keep things simple.
1357  *
1358  */
1359
1360 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1361 {
1362         r->ru_nvcsw += t->nvcsw;
1363         r->ru_nivcsw += t->nivcsw;
1364         r->ru_minflt += t->min_flt;
1365         r->ru_majflt += t->maj_flt;
1366         r->ru_inblock += task_io_get_inblock(t);
1367         r->ru_oublock += task_io_get_oublock(t);
1368 }
1369
1370 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1371 {
1372         struct task_struct *t;
1373         unsigned long flags;
1374         cputime_t tgutime, tgstime, utime, stime;
1375         unsigned long maxrss = 0;
1376
1377         memset((char *) r, 0, sizeof *r);
1378         utime = stime = cputime_zero;
1379
1380         if (who == RUSAGE_THREAD) {
1381                 task_times(current, &utime, &stime);
1382                 accumulate_thread_rusage(p, r);
1383                 maxrss = p->signal->maxrss;
1384                 goto out;
1385         }
1386
1387         if (!lock_task_sighand(p, &flags))
1388                 return;
1389
1390         switch (who) {
1391                 case RUSAGE_BOTH:
1392                 case RUSAGE_CHILDREN:
1393                         utime = p->signal->cutime;
1394                         stime = p->signal->cstime;
1395                         r->ru_nvcsw = p->signal->cnvcsw;
1396                         r->ru_nivcsw = p->signal->cnivcsw;
1397                         r->ru_minflt = p->signal->cmin_flt;
1398                         r->ru_majflt = p->signal->cmaj_flt;
1399                         r->ru_inblock = p->signal->cinblock;
1400                         r->ru_oublock = p->signal->coublock;
1401                         maxrss = p->signal->cmaxrss;
1402
1403                         if (who == RUSAGE_CHILDREN)
1404                                 break;
1405
1406                 case RUSAGE_SELF:
1407                         thread_group_times(p, &tgutime, &tgstime);
1408                         utime = cputime_add(utime, tgutime);
1409                         stime = cputime_add(stime, tgstime);
1410                         r->ru_nvcsw += p->signal->nvcsw;
1411                         r->ru_nivcsw += p->signal->nivcsw;
1412                         r->ru_minflt += p->signal->min_flt;
1413                         r->ru_majflt += p->signal->maj_flt;
1414                         r->ru_inblock += p->signal->inblock;
1415                         r->ru_oublock += p->signal->oublock;
1416                         if (maxrss < p->signal->maxrss)
1417                                 maxrss = p->signal->maxrss;
1418                         t = p;
1419                         do {
1420                                 accumulate_thread_rusage(t, r);
1421                                 t = next_thread(t);
1422                         } while (t != p);
1423                         break;
1424
1425                 default:
1426                         BUG();
1427         }
1428         unlock_task_sighand(p, &flags);
1429
1430 out:
1431         cputime_to_timeval(utime, &r->ru_utime);
1432         cputime_to_timeval(stime, &r->ru_stime);
1433
1434         if (who != RUSAGE_CHILDREN) {
1435                 struct mm_struct *mm = get_task_mm(p);
1436                 if (mm) {
1437                         setmax_mm_hiwater_rss(&maxrss, mm);
1438                         mmput(mm);
1439                 }
1440         }
1441         r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
1442 }
1443
1444 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1445 {
1446         struct rusage r;
1447         k_getrusage(p, who, &r);
1448         return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1449 }
1450
1451 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1452 {
1453         if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1454             who != RUSAGE_THREAD)
1455                 return -EINVAL;
1456         return getrusage(current, who, ru);
1457 }
1458
1459 SYSCALL_DEFINE1(umask, int, mask)
1460 {
1461         mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1462         return mask;
1463 }
1464
1465 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
1466                 unsigned long, arg4, unsigned long, arg5)
1467 {
1468         struct task_struct *me = current;
1469         unsigned char comm[sizeof(me->comm)];
1470         long error;
1471
1472         error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1473         if (error != -ENOSYS)
1474                 return error;
1475
1476         error = 0;
1477         switch (option) {
1478                 case PR_SET_PDEATHSIG:
1479                         if (!valid_signal(arg2)) {
1480                                 error = -EINVAL;
1481                                 break;
1482                         }
1483                         me->pdeath_signal = arg2;
1484                         error = 0;
1485                         break;
1486                 case PR_GET_PDEATHSIG:
1487                         error = put_user(me->pdeath_signal, (int __user *)arg2);
1488                         break;
1489                 case PR_GET_DUMPABLE:
1490                         error = get_dumpable(me->mm);
1491                         break;
1492                 case PR_SET_DUMPABLE:
1493                         if (arg2 < 0 || arg2 > 1) {
1494                                 error = -EINVAL;
1495                                 break;
1496                         }
1497                         set_dumpable(me->mm, arg2);
1498                         error = 0;
1499                         break;
1500
1501                 case PR_SET_UNALIGN:
1502                         error = SET_UNALIGN_CTL(me, arg2);
1503                         break;
1504                 case PR_GET_UNALIGN:
1505                         error = GET_UNALIGN_CTL(me, arg2);
1506                         break;
1507                 case PR_SET_FPEMU:
1508                         error = SET_FPEMU_CTL(me, arg2);
1509                         break;
1510                 case PR_GET_FPEMU:
1511                         error = GET_FPEMU_CTL(me, arg2);
1512                         break;
1513                 case PR_SET_FPEXC:
1514                         error = SET_FPEXC_CTL(me, arg2);
1515                         break;
1516                 case PR_GET_FPEXC:
1517                         error = GET_FPEXC_CTL(me, arg2);
1518                         break;
1519                 case PR_GET_TIMING:
1520                         error = PR_TIMING_STATISTICAL;
1521                         break;
1522                 case PR_SET_TIMING:
1523                         if (arg2 != PR_TIMING_STATISTICAL)
1524                                 error = -EINVAL;
1525                         else
1526                                 error = 0;
1527                         break;
1528
1529                 case PR_SET_NAME:
1530                         comm[sizeof(me->comm)-1] = 0;
1531                         if (strncpy_from_user(comm, (char __user *)arg2,
1532                                               sizeof(me->comm) - 1) < 0)
1533                                 return -EFAULT;
1534                         set_task_comm(me, comm);
1535                         return 0;
1536                 case PR_GET_NAME:
1537                         get_task_comm(comm, me);
1538                         if (copy_to_user((char __user *)arg2, comm,
1539                                          sizeof(comm)))
1540                                 return -EFAULT;
1541                         return 0;
1542                 case PR_GET_ENDIAN:
1543                         error = GET_ENDIAN(me, arg2);
1544                         break;
1545                 case PR_SET_ENDIAN:
1546                         error = SET_ENDIAN(me, arg2);
1547                         break;
1548
1549                 case PR_GET_SECCOMP:
1550                         error = prctl_get_seccomp();
1551                         break;
1552                 case PR_SET_SECCOMP:
1553                         error = prctl_set_seccomp(arg2);
1554                         break;
1555                 case PR_GET_TSC:
1556                         error = GET_TSC_CTL(arg2);
1557                         break;
1558                 case PR_SET_TSC:
1559                         error = SET_TSC_CTL(arg2);
1560                         break;
1561                 case PR_TASK_PERF_EVENTS_DISABLE:
1562                         error = perf_event_task_disable();
1563                         break;
1564                 case PR_TASK_PERF_EVENTS_ENABLE:
1565                         error = perf_event_task_enable();
1566                         break;
1567                 case PR_GET_TIMERSLACK:
1568                         error = current->timer_slack_ns;
1569                         break;
1570                 case PR_SET_TIMERSLACK:
1571                         if (arg2 <= 0)
1572                                 current->timer_slack_ns =
1573                                         current->default_timer_slack_ns;
1574                         else
1575                                 current->timer_slack_ns = arg2;
1576                         error = 0;
1577                         break;
1578                 case PR_MCE_KILL:
1579                         if (arg4 | arg5)
1580                                 return -EINVAL;
1581                         switch (arg2) {
1582                         case PR_MCE_KILL_CLEAR:
1583                                 if (arg3 != 0)
1584                                         return -EINVAL;
1585                                 current->flags &= ~PF_MCE_PROCESS;
1586                                 break;
1587                         case PR_MCE_KILL_SET:
1588                                 current->flags |= PF_MCE_PROCESS;
1589                                 if (arg3 == PR_MCE_KILL_EARLY)
1590                                         current->flags |= PF_MCE_EARLY;
1591                                 else if (arg3 == PR_MCE_KILL_LATE)
1592                                         current->flags &= ~PF_MCE_EARLY;
1593                                 else if (arg3 == PR_MCE_KILL_DEFAULT)
1594                                         current->flags &=
1595                                                 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
1596                                 else
1597                                         return -EINVAL;
1598                                 break;
1599                         default:
1600                                 return -EINVAL;
1601                         }
1602                         error = 0;
1603                         break;
1604                 case PR_MCE_KILL_GET:
1605                         if (arg2 | arg3 | arg4 | arg5)
1606                                 return -EINVAL;
1607                         if (current->flags & PF_MCE_PROCESS)
1608                                 error = (current->flags & PF_MCE_EARLY) ?
1609                                         PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
1610                         else
1611                                 error = PR_MCE_KILL_DEFAULT;
1612                         break;
1613                 default:
1614                         error = -EINVAL;
1615                         break;
1616         }
1617         return error;
1618 }
1619
1620 SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
1621                 struct getcpu_cache __user *, unused)
1622 {
1623         int err = 0;
1624         int cpu = raw_smp_processor_id();
1625         if (cpup)
1626                 err |= put_user(cpu, cpup);
1627         if (nodep)
1628                 err |= put_user(cpu_to_node(cpu), nodep);
1629         return err ? -EFAULT : 0;
1630 }
1631
1632 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
1633
1634 static void argv_cleanup(char **argv, char **envp)
1635 {
1636         argv_free(argv);
1637 }
1638
1639 /**
1640  * orderly_poweroff - Trigger an orderly system poweroff
1641  * @force: force poweroff if command execution fails
1642  *
1643  * This may be called from any context to trigger a system shutdown.
1644  * If the orderly shutdown fails, it will force an immediate shutdown.
1645  */
1646 int orderly_poweroff(bool force)
1647 {
1648         int argc;
1649         char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
1650         static char *envp[] = {
1651                 "HOME=/",
1652                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
1653                 NULL
1654         };
1655         int ret = -ENOMEM;
1656         struct subprocess_info *info;
1657
1658         if (argv == NULL) {
1659                 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
1660                        __func__, poweroff_cmd);
1661                 goto out;
1662         }
1663
1664         info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
1665         if (info == NULL) {
1666                 argv_free(argv);
1667                 goto out;
1668         }
1669
1670         call_usermodehelper_setcleanup(info, argv_cleanup);
1671
1672         ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
1673
1674   out:
1675         if (ret && force) {
1676                 printk(KERN_WARNING "Failed to start orderly shutdown: "
1677                        "forcing the issue\n");
1678
1679                 /* I guess this should try to kick off some daemon to
1680                    sync and poweroff asap.  Or not even bother syncing
1681                    if we're doing an emergency shutdown? */
1682                 emergency_sync();
1683                 kernel_power_off();
1684         }
1685
1686         return ret;
1687 }
1688 EXPORT_SYMBOL_GPL(orderly_poweroff);