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