Now it works.
[cbs-scheduler.git] / kernel / sys.c
blobef839ae5f600ea2ff6d7e86958014357c1077dbe
1 /*
2 * linux/kernel/sys.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
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_counter.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/hardirq.h>
37 #include <linux/cpu.h>
38 #include <linux/ptrace.h>
40 #include <linux/compat.h>
41 #include <linux/syscalls.h>
42 #include <linux/rt_lock.h>
43 #include <linux/kprobes.h>
44 #include <linux/user_namespace.h>
46 #include <asm/uaccess.h>
47 #include <asm/io.h>
48 #include <asm/unistd.h>
50 #ifndef SET_UNALIGN_CTL
51 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
52 #endif
53 #ifndef GET_UNALIGN_CTL
54 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
55 #endif
56 #ifndef SET_FPEMU_CTL
57 # define SET_FPEMU_CTL(a,b) (-EINVAL)
58 #endif
59 #ifndef GET_FPEMU_CTL
60 # define GET_FPEMU_CTL(a,b) (-EINVAL)
61 #endif
62 #ifndef SET_FPEXC_CTL
63 # define SET_FPEXC_CTL(a,b) (-EINVAL)
64 #endif
65 #ifndef GET_FPEXC_CTL
66 # define GET_FPEXC_CTL(a,b) (-EINVAL)
67 #endif
68 #ifndef GET_ENDIAN
69 # define GET_ENDIAN(a,b) (-EINVAL)
70 #endif
71 #ifndef SET_ENDIAN
72 # define SET_ENDIAN(a,b) (-EINVAL)
73 #endif
74 #ifndef GET_TSC_CTL
75 # define GET_TSC_CTL(a) (-EINVAL)
76 #endif
77 #ifndef SET_TSC_CTL
78 # define SET_TSC_CTL(a) (-EINVAL)
79 #endif
82 * this is where the system-wide overflow UID and GID are defined, for
83 * architectures that now have 32-bit UID/GID but didn't in the past
86 int overflowuid = DEFAULT_OVERFLOWUID;
87 int overflowgid = DEFAULT_OVERFLOWGID;
89 #ifdef CONFIG_UID16
90 EXPORT_SYMBOL(overflowuid);
91 EXPORT_SYMBOL(overflowgid);
92 #endif
95 * the same as above, but for filesystems which can only store a 16-bit
96 * UID and GID. as such, this is needed on all architectures
99 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
100 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
102 EXPORT_SYMBOL(fs_overflowuid);
103 EXPORT_SYMBOL(fs_overflowgid);
106 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
109 int C_A_D = 1;
110 struct pid *cad_pid;
111 EXPORT_SYMBOL(cad_pid);
114 * If set, this is used for preparing the system to power off.
117 void (*pm_power_off_prepare)(void);
120 * set the priority of a task
121 * - the caller must hold the RCU read lock
123 static int set_one_prio(struct task_struct *p, int niceval, int error)
125 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
126 int no_nice;
128 if (pcred->uid != cred->euid &&
129 pcred->euid != cred->euid && !capable(CAP_SYS_NICE)) {
130 error = -EPERM;
131 goto out;
133 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
134 error = -EACCES;
135 goto out;
137 no_nice = security_task_setnice(p, niceval);
138 if (no_nice) {
139 error = no_nice;
140 goto out;
142 if (error == -ESRCH)
143 error = 0;
144 set_user_nice(p, niceval);
145 out:
146 return error;
149 SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
151 struct task_struct *g, *p;
152 struct user_struct *user;
153 const struct cred *cred = current_cred();
154 int error = -EINVAL;
155 struct pid *pgrp;
157 if (which > PRIO_USER || which < PRIO_PROCESS)
158 goto out;
160 /* normalize: avoid signed division (rounding problems) */
161 error = -ESRCH;
162 if (niceval < -20)
163 niceval = -20;
164 if (niceval > 19)
165 niceval = 19;
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 */
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;
202 out_unlock:
203 read_unlock(&tasklist_lock);
204 out:
205 return error;
209 * Ugh. To avoid negative return values, "getpriority()" will
210 * not return the normal nice-value, but a negated value that
211 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
212 * to stay compatible.
214 SYSCALL_DEFINE2(getpriority, int, which, int, who)
216 struct task_struct *g, *p;
217 struct user_struct *user;
218 const struct cred *cred = current_cred();
219 long niceval, retval = -ESRCH;
220 struct pid *pgrp;
222 if (which > PRIO_USER || which < PRIO_PROCESS)
223 return -EINVAL;
225 read_lock(&tasklist_lock);
226 switch (which) {
227 case PRIO_PROCESS:
228 if (who)
229 p = find_task_by_vpid(who);
230 else
231 p = current;
232 if (p) {
233 niceval = 20 - task_nice(p);
234 if (niceval > retval)
235 retval = niceval;
237 break;
238 case PRIO_PGRP:
239 if (who)
240 pgrp = find_vpid(who);
241 else
242 pgrp = task_pgrp(current);
243 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
244 niceval = 20 - task_nice(p);
245 if (niceval > retval)
246 retval = niceval;
247 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
248 break;
249 case PRIO_USER:
250 user = (struct user_struct *) cred->user;
251 if (!who)
252 who = cred->uid;
253 else if ((who != cred->uid) &&
254 !(user = find_user(who)))
255 goto out_unlock; /* No processes for this user */
257 do_each_thread(g, p)
258 if (__task_cred(p)->uid == who) {
259 niceval = 20 - task_nice(p);
260 if (niceval > retval)
261 retval = niceval;
263 while_each_thread(g, p);
264 if (who != cred->uid)
265 free_uid(user); /* for find_user() */
266 break;
268 out_unlock:
269 read_unlock(&tasklist_lock);
271 return retval;
275 * emergency_restart - reboot the system
277 * Without shutting down any hardware or taking any locks
278 * reboot the system. This is called when we know we are in
279 * trouble so this is our best effort to reboot. This is
280 * safe to call in interrupt context.
282 void emergency_restart(void)
285 * Call the notifier chain if we are not in an
286 * atomic context:
288 #ifdef CONFIG_PREEMPT
289 if (!in_atomic() && !irqs_disabled())
290 blocking_notifier_call_chain(&reboot_notifier_list,
291 SYS_RESTART, NULL);
292 #endif
293 machine_emergency_restart();
295 EXPORT_SYMBOL_GPL(emergency_restart);
297 void kernel_restart_prepare(char *cmd)
299 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
300 system_state = SYSTEM_RESTART;
301 device_shutdown();
302 sysdev_shutdown();
306 * kernel_restart - reboot the system
307 * @cmd: pointer to buffer containing command to execute for restart
308 * or %NULL
310 * Shutdown everything and perform a clean reboot.
311 * This is not safe to call in interrupt context.
313 void kernel_restart(char *cmd)
315 kernel_restart_prepare(cmd);
316 if (!cmd)
317 printk(KERN_EMERG "Restarting system.\n");
318 else
319 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
320 machine_restart(cmd);
322 EXPORT_SYMBOL_GPL(kernel_restart);
324 static void kernel_shutdown_prepare(enum system_states state)
326 blocking_notifier_call_chain(&reboot_notifier_list,
327 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
328 system_state = state;
329 device_shutdown();
332 * kernel_halt - halt the system
334 * Shutdown everything and perform a clean system halt.
336 void kernel_halt(void)
338 kernel_shutdown_prepare(SYSTEM_HALT);
339 sysdev_shutdown();
340 printk(KERN_EMERG "System halted.\n");
341 machine_halt();
344 EXPORT_SYMBOL_GPL(kernel_halt);
347 * kernel_power_off - power_off the system
349 * Shutdown everything and perform a clean system power_off.
351 void kernel_power_off(void)
353 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
354 if (pm_power_off_prepare)
355 pm_power_off_prepare();
356 disable_nonboot_cpus();
357 sysdev_shutdown();
358 printk(KERN_EMERG "Power down.\n");
359 machine_power_off();
361 EXPORT_SYMBOL_GPL(kernel_power_off);
363 * Reboot system call: for obvious reasons only root may call it,
364 * and even root needs to set up some magic numbers in the registers
365 * so that some mistake won't make this reboot the whole machine.
366 * You can also set the meaning of the ctrl-alt-del-key here.
368 * reboot doesn't sync: do that yourself before calling this.
370 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
371 void __user *, arg)
373 char buffer[256];
375 /* We only trust the superuser with rebooting the system. */
376 if (!capable(CAP_SYS_BOOT))
377 return -EPERM;
379 /* For safety, we require "magic" arguments. */
380 if (magic1 != LINUX_REBOOT_MAGIC1 ||
381 (magic2 != LINUX_REBOOT_MAGIC2 &&
382 magic2 != LINUX_REBOOT_MAGIC2A &&
383 magic2 != LINUX_REBOOT_MAGIC2B &&
384 magic2 != LINUX_REBOOT_MAGIC2C))
385 return -EINVAL;
387 /* Instead of trying to make the power_off code look like
388 * halt when pm_power_off is not set do it the easy way.
390 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
391 cmd = LINUX_REBOOT_CMD_HALT;
393 lock_kernel();
394 switch (cmd) {
395 case LINUX_REBOOT_CMD_RESTART:
396 kernel_restart(NULL);
397 break;
399 case LINUX_REBOOT_CMD_CAD_ON:
400 C_A_D = 1;
401 break;
403 case LINUX_REBOOT_CMD_CAD_OFF:
404 C_A_D = 0;
405 break;
407 case LINUX_REBOOT_CMD_HALT:
408 kernel_halt();
409 unlock_kernel();
410 do_exit(0);
411 break;
413 case LINUX_REBOOT_CMD_POWER_OFF:
414 kernel_power_off();
415 unlock_kernel();
416 do_exit(0);
417 break;
419 case LINUX_REBOOT_CMD_RESTART2:
420 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
421 unlock_kernel();
422 return -EFAULT;
424 buffer[sizeof(buffer) - 1] = '\0';
426 kernel_restart(buffer);
427 break;
429 #ifdef CONFIG_KEXEC
430 case LINUX_REBOOT_CMD_KEXEC:
432 int ret;
433 ret = kernel_kexec();
434 unlock_kernel();
435 return ret;
437 #endif
439 #ifdef CONFIG_HIBERNATION
440 case LINUX_REBOOT_CMD_SW_SUSPEND:
442 int ret = hibernate();
443 unlock_kernel();
444 return ret;
446 #endif
448 default:
449 unlock_kernel();
450 return -EINVAL;
452 unlock_kernel();
453 return 0;
456 static void deferred_cad(struct work_struct *dummy)
458 kernel_restart(NULL);
462 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
463 * As it's called within an interrupt, it may NOT sync: the only choice
464 * is whether to reboot at once, or just ignore the ctrl-alt-del.
466 void ctrl_alt_del(void)
468 static DECLARE_WORK(cad_work, deferred_cad);
470 if (C_A_D)
471 schedule_work(&cad_work);
472 else
473 kill_cad_pid(SIGINT, 1);
477 * Unprivileged users may change the real gid to the effective gid
478 * or vice versa. (BSD-style)
480 * If you set the real gid at all, or set the effective gid to a value not
481 * equal to the real gid, then the saved gid is set to the new effective gid.
483 * This makes it possible for a setgid program to completely drop its
484 * privileges, which is often a useful assertion to make when you are doing
485 * a security audit over a program.
487 * The general idea is that a program which uses just setregid() will be
488 * 100% compatible with BSD. A program which uses just setgid() will be
489 * 100% compatible with POSIX with saved IDs.
491 * SMP: There are not races, the GIDs are checked only by filesystem
492 * operations (as far as semantic preservation is concerned).
494 SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
496 const struct cred *old;
497 struct cred *new;
498 int retval;
500 new = prepare_creds();
501 if (!new)
502 return -ENOMEM;
503 old = current_cred();
505 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
506 if (retval)
507 goto error;
509 retval = -EPERM;
510 if (rgid != (gid_t) -1) {
511 if (old->gid == rgid ||
512 old->egid == rgid ||
513 capable(CAP_SETGID))
514 new->gid = rgid;
515 else
516 goto error;
518 if (egid != (gid_t) -1) {
519 if (old->gid == egid ||
520 old->egid == egid ||
521 old->sgid == egid ||
522 capable(CAP_SETGID))
523 new->egid = egid;
524 else
525 goto error;
528 if (rgid != (gid_t) -1 ||
529 (egid != (gid_t) -1 && egid != old->gid))
530 new->sgid = new->egid;
531 new->fsgid = new->egid;
533 return commit_creds(new);
535 error:
536 abort_creds(new);
537 return retval;
541 * setgid() is implemented like SysV w/ SAVED_IDS
543 * SMP: Same implicit races as above.
545 SYSCALL_DEFINE1(setgid, gid_t, gid)
547 const struct cred *old;
548 struct cred *new;
549 int retval;
551 new = prepare_creds();
552 if (!new)
553 return -ENOMEM;
554 old = current_cred();
556 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
557 if (retval)
558 goto error;
560 retval = -EPERM;
561 if (capable(CAP_SETGID))
562 new->gid = new->egid = new->sgid = new->fsgid = gid;
563 else if (gid == old->gid || gid == old->sgid)
564 new->egid = new->fsgid = gid;
565 else
566 goto error;
568 return commit_creds(new);
570 error:
571 abort_creds(new);
572 return retval;
576 * change the user struct in a credentials set to match the new UID
578 static int set_user(struct cred *new)
580 struct user_struct *new_user;
582 new_user = alloc_uid(current_user_ns(), new->uid);
583 if (!new_user)
584 return -EAGAIN;
586 if (!task_can_switch_user(new_user, current)) {
587 free_uid(new_user);
588 return -EINVAL;
591 if (atomic_read(&new_user->processes) >=
592 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
593 new_user != INIT_USER) {
594 free_uid(new_user);
595 return -EAGAIN;
598 free_uid(new->user);
599 new->user = new_user;
600 return 0;
604 * Unprivileged users may change the real uid to the effective uid
605 * or vice versa. (BSD-style)
607 * If you set the real uid at all, or set the effective uid to a value not
608 * equal to the real uid, then the saved uid is set to the new effective uid.
610 * This makes it possible for a setuid program to completely drop its
611 * privileges, which is often a useful assertion to make when you are doing
612 * a security audit over a program.
614 * The general idea is that a program which uses just setreuid() will be
615 * 100% compatible with BSD. A program which uses just setuid() will be
616 * 100% compatible with POSIX with saved IDs.
618 SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
620 const struct cred *old;
621 struct cred *new;
622 int retval;
624 new = prepare_creds();
625 if (!new)
626 return -ENOMEM;
627 old = current_cred();
629 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
630 if (retval)
631 goto error;
633 retval = -EPERM;
634 if (ruid != (uid_t) -1) {
635 new->uid = ruid;
636 if (old->uid != ruid &&
637 old->euid != ruid &&
638 !capable(CAP_SETUID))
639 goto error;
642 if (euid != (uid_t) -1) {
643 new->euid = euid;
644 if (old->uid != euid &&
645 old->euid != euid &&
646 old->suid != euid &&
647 !capable(CAP_SETUID))
648 goto error;
651 if (new->uid != old->uid) {
652 retval = set_user(new);
653 if (retval < 0)
654 goto error;
656 if (ruid != (uid_t) -1 ||
657 (euid != (uid_t) -1 && euid != old->uid))
658 new->suid = new->euid;
659 new->fsuid = new->euid;
661 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
662 if (retval < 0)
663 goto error;
665 return commit_creds(new);
667 error:
668 abort_creds(new);
669 return retval;
673 * setuid() is implemented like SysV with SAVED_IDS
675 * Note that SAVED_ID's is deficient in that a setuid root program
676 * like sendmail, for example, cannot set its uid to be a normal
677 * user and then switch back, because if you're root, setuid() sets
678 * the saved uid too. If you don't like this, blame the bright people
679 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
680 * will allow a root program to temporarily drop privileges and be able to
681 * regain them by swapping the real and effective uid.
683 SYSCALL_DEFINE1(setuid, uid_t, uid)
685 const struct cred *old;
686 struct cred *new;
687 int retval;
689 new = prepare_creds();
690 if (!new)
691 return -ENOMEM;
692 old = current_cred();
694 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
695 if (retval)
696 goto error;
698 retval = -EPERM;
699 if (capable(CAP_SETUID)) {
700 new->suid = new->uid = uid;
701 if (uid != old->uid) {
702 retval = set_user(new);
703 if (retval < 0)
704 goto error;
706 } else if (uid != old->uid && uid != new->suid) {
707 goto error;
710 new->fsuid = new->euid = uid;
712 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
713 if (retval < 0)
714 goto error;
716 return commit_creds(new);
718 error:
719 abort_creds(new);
720 return retval;
725 * This function implements a generic ability to update ruid, euid,
726 * and suid. This allows you to implement the 4.4 compatible seteuid().
728 SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
730 const struct cred *old;
731 struct cred *new;
732 int retval;
734 new = prepare_creds();
735 if (!new)
736 return -ENOMEM;
738 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
739 if (retval)
740 goto error;
741 old = current_cred();
743 retval = -EPERM;
744 if (!capable(CAP_SETUID)) {
745 if (ruid != (uid_t) -1 && ruid != old->uid &&
746 ruid != old->euid && ruid != old->suid)
747 goto error;
748 if (euid != (uid_t) -1 && euid != old->uid &&
749 euid != old->euid && euid != old->suid)
750 goto error;
751 if (suid != (uid_t) -1 && suid != old->uid &&
752 suid != old->euid && suid != old->suid)
753 goto error;
756 if (ruid != (uid_t) -1) {
757 new->uid = ruid;
758 if (ruid != old->uid) {
759 retval = set_user(new);
760 if (retval < 0)
761 goto error;
764 if (euid != (uid_t) -1)
765 new->euid = euid;
766 if (suid != (uid_t) -1)
767 new->suid = suid;
768 new->fsuid = new->euid;
770 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
771 if (retval < 0)
772 goto error;
774 return commit_creds(new);
776 error:
777 abort_creds(new);
778 return retval;
781 SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
783 const struct cred *cred = current_cred();
784 int retval;
786 if (!(retval = put_user(cred->uid, ruid)) &&
787 !(retval = put_user(cred->euid, euid)))
788 retval = put_user(cred->suid, suid);
790 return retval;
794 * Same as above, but for rgid, egid, sgid.
796 SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
798 const struct cred *old;
799 struct cred *new;
800 int retval;
802 new = prepare_creds();
803 if (!new)
804 return -ENOMEM;
805 old = current_cred();
807 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
808 if (retval)
809 goto error;
811 retval = -EPERM;
812 if (!capable(CAP_SETGID)) {
813 if (rgid != (gid_t) -1 && rgid != old->gid &&
814 rgid != old->egid && rgid != old->sgid)
815 goto error;
816 if (egid != (gid_t) -1 && egid != old->gid &&
817 egid != old->egid && egid != old->sgid)
818 goto error;
819 if (sgid != (gid_t) -1 && sgid != old->gid &&
820 sgid != old->egid && sgid != old->sgid)
821 goto error;
824 if (rgid != (gid_t) -1)
825 new->gid = rgid;
826 if (egid != (gid_t) -1)
827 new->egid = egid;
828 if (sgid != (gid_t) -1)
829 new->sgid = sgid;
830 new->fsgid = new->egid;
832 return commit_creds(new);
834 error:
835 abort_creds(new);
836 return retval;
839 SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
841 const struct cred *cred = current_cred();
842 int retval;
844 if (!(retval = put_user(cred->gid, rgid)) &&
845 !(retval = put_user(cred->egid, egid)))
846 retval = put_user(cred->sgid, sgid);
848 return retval;
853 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
854 * is used for "access()" and for the NFS daemon (letting nfsd stay at
855 * whatever uid it wants to). It normally shadows "euid", except when
856 * explicitly set by setfsuid() or for access..
858 SYSCALL_DEFINE1(setfsuid, uid_t, uid)
860 const struct cred *old;
861 struct cred *new;
862 uid_t old_fsuid;
864 new = prepare_creds();
865 if (!new)
866 return current_fsuid();
867 old = current_cred();
868 old_fsuid = old->fsuid;
870 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS) < 0)
871 goto error;
873 if (uid == old->uid || uid == old->euid ||
874 uid == old->suid || uid == old->fsuid ||
875 capable(CAP_SETUID)) {
876 if (uid != old_fsuid) {
877 new->fsuid = uid;
878 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
879 goto change_okay;
883 error:
884 abort_creds(new);
885 return old_fsuid;
887 change_okay:
888 commit_creds(new);
889 return old_fsuid;
893 * Samma på svenska..
895 SYSCALL_DEFINE1(setfsgid, gid_t, gid)
897 const struct cred *old;
898 struct cred *new;
899 gid_t old_fsgid;
901 new = prepare_creds();
902 if (!new)
903 return current_fsgid();
904 old = current_cred();
905 old_fsgid = old->fsgid;
907 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
908 goto error;
910 if (gid == old->gid || gid == old->egid ||
911 gid == old->sgid || gid == old->fsgid ||
912 capable(CAP_SETGID)) {
913 if (gid != old_fsgid) {
914 new->fsgid = gid;
915 goto change_okay;
919 error:
920 abort_creds(new);
921 return old_fsgid;
923 change_okay:
924 commit_creds(new);
925 return old_fsgid;
928 void do_sys_times(struct tms *tms)
930 struct task_cputime cputime;
931 cputime_t cutime, cstime;
933 thread_group_cputime(current, &cputime);
934 spin_lock_irq(&current->sighand->siglock);
935 cutime = current->signal->cutime;
936 cstime = current->signal->cstime;
937 spin_unlock_irq(&current->sighand->siglock);
938 tms->tms_utime = cputime_to_clock_t(cputime.utime);
939 tms->tms_stime = cputime_to_clock_t(cputime.stime);
940 tms->tms_cutime = cputime_to_clock_t(cutime);
941 tms->tms_cstime = cputime_to_clock_t(cstime);
944 SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
946 if (tbuf) {
947 struct tms tmp;
949 do_sys_times(&tmp);
950 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
951 return -EFAULT;
953 force_successful_syscall_return();
954 return (long) jiffies_64_to_clock_t(get_jiffies_64());
958 * This needs some heavy checking ...
959 * I just haven't the stomach for it. I also don't fully
960 * understand sessions/pgrp etc. Let somebody who does explain it.
962 * OK, I think I have the protection semantics right.... this is really
963 * only important on a multi-user system anyway, to make sure one user
964 * can't send a signal to a process owned by another. -TYT, 12/12/91
966 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
967 * LBT 04.03.94
969 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
971 struct task_struct *p;
972 struct task_struct *group_leader = current->group_leader;
973 struct pid *pgrp;
974 int err;
976 if (!pid)
977 pid = task_pid_vnr(group_leader);
978 if (!pgid)
979 pgid = pid;
980 if (pgid < 0)
981 return -EINVAL;
983 /* From this point forward we keep holding onto the tasklist lock
984 * so that our parent does not change from under us. -DaveM
986 write_lock_irq(&tasklist_lock);
988 err = -ESRCH;
989 p = find_task_by_vpid(pid);
990 if (!p)
991 goto out;
993 err = -EINVAL;
994 if (!thread_group_leader(p))
995 goto out;
997 if (same_thread_group(p->real_parent, group_leader)) {
998 err = -EPERM;
999 if (task_session(p) != task_session(group_leader))
1000 goto out;
1001 err = -EACCES;
1002 if (p->did_exec)
1003 goto out;
1004 } else {
1005 err = -ESRCH;
1006 if (p != group_leader)
1007 goto out;
1010 err = -EPERM;
1011 if (p->signal->leader)
1012 goto out;
1014 pgrp = task_pid(p);
1015 if (pgid != pid) {
1016 struct task_struct *g;
1018 pgrp = find_vpid(pgid);
1019 g = pid_task(pgrp, PIDTYPE_PGID);
1020 if (!g || task_session(g) != task_session(group_leader))
1021 goto out;
1024 err = security_task_setpgid(p, pgid);
1025 if (err)
1026 goto out;
1028 if (task_pgrp(p) != pgrp) {
1029 change_pid(p, PIDTYPE_PGID, pgrp);
1030 set_task_pgrp(p, pid_nr(pgrp));
1033 err = 0;
1034 out:
1035 /* All paths lead to here, thus we are safe. -DaveM */
1036 write_unlock_irq(&tasklist_lock);
1037 return err;
1040 SYSCALL_DEFINE1(getpgid, pid_t, pid)
1042 struct task_struct *p;
1043 struct pid *grp;
1044 int retval;
1046 rcu_read_lock();
1047 if (!pid)
1048 grp = task_pgrp(current);
1049 else {
1050 retval = -ESRCH;
1051 p = find_task_by_vpid(pid);
1052 if (!p)
1053 goto out;
1054 grp = task_pgrp(p);
1055 if (!grp)
1056 goto out;
1058 retval = security_task_getpgid(p);
1059 if (retval)
1060 goto out;
1062 retval = pid_vnr(grp);
1063 out:
1064 rcu_read_unlock();
1065 return retval;
1068 #ifdef __ARCH_WANT_SYS_GETPGRP
1070 SYSCALL_DEFINE0(getpgrp)
1072 return sys_getpgid(0);
1075 #endif
1077 SYSCALL_DEFINE1(getsid, pid_t, pid)
1079 struct task_struct *p;
1080 struct pid *sid;
1081 int retval;
1083 rcu_read_lock();
1084 if (!pid)
1085 sid = task_session(current);
1086 else {
1087 retval = -ESRCH;
1088 p = find_task_by_vpid(pid);
1089 if (!p)
1090 goto out;
1091 sid = task_session(p);
1092 if (!sid)
1093 goto out;
1095 retval = security_task_getsid(p);
1096 if (retval)
1097 goto out;
1099 retval = pid_vnr(sid);
1100 out:
1101 rcu_read_unlock();
1102 return retval;
1105 SYSCALL_DEFINE0(setsid)
1107 struct task_struct *group_leader = current->group_leader;
1108 struct pid *sid = task_pid(group_leader);
1109 pid_t session = pid_vnr(sid);
1110 int err = -EPERM;
1112 write_lock_irq(&tasklist_lock);
1113 /* Fail if I am already a session leader */
1114 if (group_leader->signal->leader)
1115 goto out;
1117 /* Fail if a process group id already exists that equals the
1118 * proposed session id.
1120 if (pid_task(sid, PIDTYPE_PGID))
1121 goto out;
1123 group_leader->signal->leader = 1;
1124 __set_special_pids(sid);
1126 proc_clear_tty(group_leader);
1128 err = session;
1129 out:
1130 write_unlock_irq(&tasklist_lock);
1131 return err;
1135 * Supplementary group IDs
1138 /* init to 2 - one for init_task, one to ensure it is never freed */
1139 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1141 struct group_info *groups_alloc(int gidsetsize)
1143 struct group_info *group_info;
1144 int nblocks;
1145 int i;
1147 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1148 /* Make sure we always allocate at least one indirect block pointer */
1149 nblocks = nblocks ? : 1;
1150 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1151 if (!group_info)
1152 return NULL;
1153 group_info->ngroups = gidsetsize;
1154 group_info->nblocks = nblocks;
1155 atomic_set(&group_info->usage, 1);
1157 if (gidsetsize <= NGROUPS_SMALL)
1158 group_info->blocks[0] = group_info->small_block;
1159 else {
1160 for (i = 0; i < nblocks; i++) {
1161 gid_t *b;
1162 b = (void *)__get_free_page(GFP_USER);
1163 if (!b)
1164 goto out_undo_partial_alloc;
1165 group_info->blocks[i] = b;
1168 return group_info;
1170 out_undo_partial_alloc:
1171 while (--i >= 0) {
1172 free_page((unsigned long)group_info->blocks[i]);
1174 kfree(group_info);
1175 return NULL;
1178 EXPORT_SYMBOL(groups_alloc);
1180 void groups_free(struct group_info *group_info)
1182 if (group_info->blocks[0] != group_info->small_block) {
1183 int i;
1184 for (i = 0; i < group_info->nblocks; i++)
1185 free_page((unsigned long)group_info->blocks[i]);
1187 kfree(group_info);
1190 EXPORT_SYMBOL(groups_free);
1192 /* export the group_info to a user-space array */
1193 static int groups_to_user(gid_t __user *grouplist,
1194 const struct group_info *group_info)
1196 int i;
1197 unsigned int count = group_info->ngroups;
1199 for (i = 0; i < group_info->nblocks; i++) {
1200 unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
1201 unsigned int len = cp_count * sizeof(*grouplist);
1203 if (copy_to_user(grouplist, group_info->blocks[i], len))
1204 return -EFAULT;
1206 grouplist += NGROUPS_PER_BLOCK;
1207 count -= cp_count;
1209 return 0;
1212 /* fill a group_info from a user-space array - it must be allocated already */
1213 static int groups_from_user(struct group_info *group_info,
1214 gid_t __user *grouplist)
1216 int i;
1217 unsigned int count = group_info->ngroups;
1219 for (i = 0; i < group_info->nblocks; i++) {
1220 unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
1221 unsigned int len = cp_count * sizeof(*grouplist);
1223 if (copy_from_user(group_info->blocks[i], grouplist, len))
1224 return -EFAULT;
1226 grouplist += NGROUPS_PER_BLOCK;
1227 count -= cp_count;
1229 return 0;
1232 /* a simple Shell sort */
1233 static void groups_sort(struct group_info *group_info)
1235 int base, max, stride;
1236 int gidsetsize = group_info->ngroups;
1238 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1239 ; /* nothing */
1240 stride /= 3;
1242 while (stride) {
1243 max = gidsetsize - stride;
1244 for (base = 0; base < max; base++) {
1245 int left = base;
1246 int right = left + stride;
1247 gid_t tmp = GROUP_AT(group_info, right);
1249 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1250 GROUP_AT(group_info, right) =
1251 GROUP_AT(group_info, left);
1252 right = left;
1253 left -= stride;
1255 GROUP_AT(group_info, right) = tmp;
1257 stride /= 3;
1261 /* a simple bsearch */
1262 int groups_search(const struct group_info *group_info, gid_t grp)
1264 unsigned int left, right;
1266 if (!group_info)
1267 return 0;
1269 left = 0;
1270 right = group_info->ngroups;
1271 while (left < right) {
1272 unsigned int mid = (left+right)/2;
1273 int cmp = grp - GROUP_AT(group_info, mid);
1274 if (cmp > 0)
1275 left = mid + 1;
1276 else if (cmp < 0)
1277 right = mid;
1278 else
1279 return 1;
1281 return 0;
1285 * set_groups - Change a group subscription in a set of credentials
1286 * @new: The newly prepared set of credentials to alter
1287 * @group_info: The group list to install
1289 * Validate a group subscription and, if valid, insert it into a set
1290 * of credentials.
1292 int set_groups(struct cred *new, struct group_info *group_info)
1294 int retval;
1296 retval = security_task_setgroups(group_info);
1297 if (retval)
1298 return retval;
1300 put_group_info(new->group_info);
1301 groups_sort(group_info);
1302 get_group_info(group_info);
1303 new->group_info = group_info;
1304 return 0;
1307 EXPORT_SYMBOL(set_groups);
1310 * set_current_groups - Change current's group subscription
1311 * @group_info: The group list to impose
1313 * Validate a group subscription and, if valid, impose it upon current's task
1314 * security record.
1316 int set_current_groups(struct group_info *group_info)
1318 struct cred *new;
1319 int ret;
1321 new = prepare_creds();
1322 if (!new)
1323 return -ENOMEM;
1325 ret = set_groups(new, group_info);
1326 if (ret < 0) {
1327 abort_creds(new);
1328 return ret;
1331 return commit_creds(new);
1334 EXPORT_SYMBOL(set_current_groups);
1336 SYSCALL_DEFINE2(getgroups, int, gidsetsize, gid_t __user *, grouplist)
1338 const struct cred *cred = current_cred();
1339 int i;
1341 if (gidsetsize < 0)
1342 return -EINVAL;
1344 /* no need to grab task_lock here; it cannot change */
1345 i = cred->group_info->ngroups;
1346 if (gidsetsize) {
1347 if (i > gidsetsize) {
1348 i = -EINVAL;
1349 goto out;
1351 if (groups_to_user(grouplist, cred->group_info)) {
1352 i = -EFAULT;
1353 goto out;
1356 out:
1357 return i;
1361 * SMP: Our groups are copy-on-write. We can set them safely
1362 * without another task interfering.
1365 SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist)
1367 struct group_info *group_info;
1368 int retval;
1370 if (!capable(CAP_SETGID))
1371 return -EPERM;
1372 if ((unsigned)gidsetsize > NGROUPS_MAX)
1373 return -EINVAL;
1375 group_info = groups_alloc(gidsetsize);
1376 if (!group_info)
1377 return -ENOMEM;
1378 retval = groups_from_user(group_info, grouplist);
1379 if (retval) {
1380 put_group_info(group_info);
1381 return retval;
1384 retval = set_current_groups(group_info);
1385 put_group_info(group_info);
1387 return retval;
1391 * Check whether we're fsgid/egid or in the supplemental group..
1393 int in_group_p(gid_t grp)
1395 const struct cred *cred = current_cred();
1396 int retval = 1;
1398 if (grp != cred->fsgid)
1399 retval = groups_search(cred->group_info, grp);
1400 return retval;
1403 EXPORT_SYMBOL(in_group_p);
1405 int in_egroup_p(gid_t grp)
1407 const struct cred *cred = current_cred();
1408 int retval = 1;
1410 if (grp != cred->egid)
1411 retval = groups_search(cred->group_info, grp);
1412 return retval;
1415 EXPORT_SYMBOL(in_egroup_p);
1417 DECLARE_RWSEM(uts_sem);
1419 SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
1421 int errno = 0;
1423 down_read(&uts_sem);
1424 if (copy_to_user(name, utsname(), sizeof *name))
1425 errno = -EFAULT;
1426 up_read(&uts_sem);
1427 return errno;
1430 SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1432 int errno;
1433 char tmp[__NEW_UTS_LEN];
1435 if (!capable(CAP_SYS_ADMIN))
1436 return -EPERM;
1437 if (len < 0 || len > __NEW_UTS_LEN)
1438 return -EINVAL;
1439 down_write(&uts_sem);
1440 errno = -EFAULT;
1441 if (!copy_from_user(tmp, name, len)) {
1442 struct new_utsname *u = utsname();
1444 memcpy(u->nodename, tmp, len);
1445 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
1446 errno = 0;
1448 up_write(&uts_sem);
1449 return errno;
1452 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1454 SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
1456 int i, errno;
1457 struct new_utsname *u;
1459 if (len < 0)
1460 return -EINVAL;
1461 down_read(&uts_sem);
1462 u = utsname();
1463 i = 1 + strlen(u->nodename);
1464 if (i > len)
1465 i = len;
1466 errno = 0;
1467 if (copy_to_user(name, u->nodename, i))
1468 errno = -EFAULT;
1469 up_read(&uts_sem);
1470 return errno;
1473 #endif
1476 * Only setdomainname; getdomainname can be implemented by calling
1477 * uname()
1479 SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1481 int errno;
1482 char tmp[__NEW_UTS_LEN];
1484 if (!capable(CAP_SYS_ADMIN))
1485 return -EPERM;
1486 if (len < 0 || len > __NEW_UTS_LEN)
1487 return -EINVAL;
1489 down_write(&uts_sem);
1490 errno = -EFAULT;
1491 if (!copy_from_user(tmp, name, len)) {
1492 struct new_utsname *u = utsname();
1494 memcpy(u->domainname, tmp, len);
1495 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
1496 errno = 0;
1498 up_write(&uts_sem);
1499 return errno;
1502 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1504 if (resource >= RLIM_NLIMITS)
1505 return -EINVAL;
1506 else {
1507 struct rlimit value;
1508 task_lock(current->group_leader);
1509 value = current->signal->rlim[resource];
1510 task_unlock(current->group_leader);
1511 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1515 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1518 * Back compatibility for getrlimit. Needed for some apps.
1521 SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1522 struct rlimit __user *, rlim)
1524 struct rlimit x;
1525 if (resource >= RLIM_NLIMITS)
1526 return -EINVAL;
1528 task_lock(current->group_leader);
1529 x = current->signal->rlim[resource];
1530 task_unlock(current->group_leader);
1531 if (x.rlim_cur > 0x7FFFFFFF)
1532 x.rlim_cur = 0x7FFFFFFF;
1533 if (x.rlim_max > 0x7FFFFFFF)
1534 x.rlim_max = 0x7FFFFFFF;
1535 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1538 #endif
1540 SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1542 struct rlimit new_rlim, *old_rlim;
1543 int retval;
1545 if (resource >= RLIM_NLIMITS)
1546 return -EINVAL;
1547 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1548 return -EFAULT;
1549 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1550 return -EINVAL;
1551 old_rlim = current->signal->rlim + resource;
1552 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1553 !capable(CAP_SYS_RESOURCE))
1554 return -EPERM;
1555 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
1556 return -EPERM;
1558 retval = security_task_setrlimit(resource, &new_rlim);
1559 if (retval)
1560 return retval;
1562 if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
1564 * The caller is asking for an immediate RLIMIT_CPU
1565 * expiry. But we use the zero value to mean "it was
1566 * never set". So let's cheat and make it one second
1567 * instead
1569 new_rlim.rlim_cur = 1;
1572 task_lock(current->group_leader);
1573 *old_rlim = new_rlim;
1574 task_unlock(current->group_leader);
1576 if (resource != RLIMIT_CPU)
1577 goto out;
1580 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1581 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1582 * very long-standing error, and fixing it now risks breakage of
1583 * applications, so we live with it
1585 if (new_rlim.rlim_cur == RLIM_INFINITY)
1586 goto out;
1588 update_rlimit_cpu(new_rlim.rlim_cur);
1589 out:
1590 return 0;
1594 * It would make sense to put struct rusage in the task_struct,
1595 * except that would make the task_struct be *really big*. After
1596 * task_struct gets moved into malloc'ed memory, it would
1597 * make sense to do this. It will make moving the rest of the information
1598 * a lot simpler! (Which we're not doing right now because we're not
1599 * measuring them yet).
1601 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1602 * races with threads incrementing their own counters. But since word
1603 * reads are atomic, we either get new values or old values and we don't
1604 * care which for the sums. We always take the siglock to protect reading
1605 * the c* fields from p->signal from races with exit.c updating those
1606 * fields when reaping, so a sample either gets all the additions of a
1607 * given child after it's reaped, or none so this sample is before reaping.
1609 * Locking:
1610 * We need to take the siglock for CHILDEREN, SELF and BOTH
1611 * for the cases current multithreaded, non-current single threaded
1612 * non-current multithreaded. Thread traversal is now safe with
1613 * the siglock held.
1614 * Strictly speaking, we donot need to take the siglock if we are current and
1615 * single threaded, as no one else can take our signal_struct away, no one
1616 * else can reap the children to update signal->c* counters, and no one else
1617 * can race with the signal-> fields. If we do not take any lock, the
1618 * signal-> fields could be read out of order while another thread was just
1619 * exiting. So we should place a read memory barrier when we avoid the lock.
1620 * On the writer side, write memory barrier is implied in __exit_signal
1621 * as __exit_signal releases the siglock spinlock after updating the signal->
1622 * fields. But we don't do this yet to keep things simple.
1626 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1628 r->ru_nvcsw += t->nvcsw;
1629 r->ru_nivcsw += t->nivcsw;
1630 r->ru_minflt += t->min_flt;
1631 r->ru_majflt += t->maj_flt;
1632 r->ru_inblock += task_io_get_inblock(t);
1633 r->ru_oublock += task_io_get_oublock(t);
1636 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1638 struct task_struct *t;
1639 unsigned long flags;
1640 cputime_t utime, stime;
1641 struct task_cputime cputime;
1643 memset((char *) r, 0, sizeof *r);
1644 utime = stime = cputime_zero;
1646 if (who == RUSAGE_THREAD) {
1647 utime = task_utime(current);
1648 stime = task_stime(current);
1649 accumulate_thread_rusage(p, r);
1650 goto out;
1653 if (!lock_task_sighand(p, &flags))
1654 return;
1656 switch (who) {
1657 case RUSAGE_BOTH:
1658 case RUSAGE_CHILDREN:
1659 utime = p->signal->cutime;
1660 stime = p->signal->cstime;
1661 r->ru_nvcsw = p->signal->cnvcsw;
1662 r->ru_nivcsw = p->signal->cnivcsw;
1663 r->ru_minflt = p->signal->cmin_flt;
1664 r->ru_majflt = p->signal->cmaj_flt;
1665 r->ru_inblock = p->signal->cinblock;
1666 r->ru_oublock = p->signal->coublock;
1668 if (who == RUSAGE_CHILDREN)
1669 break;
1671 case RUSAGE_SELF:
1672 thread_group_cputime(p, &cputime);
1673 utime = cputime_add(utime, cputime.utime);
1674 stime = cputime_add(stime, cputime.stime);
1675 r->ru_nvcsw += p->signal->nvcsw;
1676 r->ru_nivcsw += p->signal->nivcsw;
1677 r->ru_minflt += p->signal->min_flt;
1678 r->ru_majflt += p->signal->maj_flt;
1679 r->ru_inblock += p->signal->inblock;
1680 r->ru_oublock += p->signal->oublock;
1681 t = p;
1682 do {
1683 accumulate_thread_rusage(t, r);
1684 t = next_thread(t);
1685 } while (t != p);
1686 break;
1688 default:
1689 BUG();
1691 unlock_task_sighand(p, &flags);
1693 out:
1694 cputime_to_timeval(utime, &r->ru_utime);
1695 cputime_to_timeval(stime, &r->ru_stime);
1698 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1700 struct rusage r;
1701 k_getrusage(p, who, &r);
1702 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1705 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1707 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1708 who != RUSAGE_THREAD)
1709 return -EINVAL;
1710 return getrusage(current, who, ru);
1713 SYSCALL_DEFINE1(umask, int, mask)
1715 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1716 return mask;
1719 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
1720 unsigned long, arg4, unsigned long, arg5)
1722 struct task_struct *me = current;
1723 unsigned char comm[sizeof(me->comm)];
1724 long error;
1726 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1727 if (error != -ENOSYS)
1728 return error;
1730 error = 0;
1731 switch (option) {
1732 case PR_SET_PDEATHSIG:
1733 if (!valid_signal(arg2)) {
1734 error = -EINVAL;
1735 break;
1737 me->pdeath_signal = arg2;
1738 error = 0;
1739 break;
1740 case PR_GET_PDEATHSIG:
1741 error = put_user(me->pdeath_signal, (int __user *)arg2);
1742 break;
1743 case PR_GET_DUMPABLE:
1744 error = get_dumpable(me->mm);
1745 break;
1746 case PR_SET_DUMPABLE:
1747 if (arg2 < 0 || arg2 > 1) {
1748 error = -EINVAL;
1749 break;
1751 set_dumpable(me->mm, arg2);
1752 error = 0;
1753 break;
1755 case PR_SET_UNALIGN:
1756 error = SET_UNALIGN_CTL(me, arg2);
1757 break;
1758 case PR_GET_UNALIGN:
1759 error = GET_UNALIGN_CTL(me, arg2);
1760 break;
1761 case PR_SET_FPEMU:
1762 error = SET_FPEMU_CTL(me, arg2);
1763 break;
1764 case PR_GET_FPEMU:
1765 error = GET_FPEMU_CTL(me, arg2);
1766 break;
1767 case PR_SET_FPEXC:
1768 error = SET_FPEXC_CTL(me, arg2);
1769 break;
1770 case PR_GET_FPEXC:
1771 error = GET_FPEXC_CTL(me, arg2);
1772 break;
1773 case PR_GET_TIMING:
1774 error = PR_TIMING_STATISTICAL;
1775 break;
1776 case PR_SET_TIMING:
1777 if (arg2 != PR_TIMING_STATISTICAL)
1778 error = -EINVAL;
1779 else
1780 error = 0;
1781 break;
1783 case PR_SET_NAME:
1784 comm[sizeof(me->comm)-1] = 0;
1785 if (strncpy_from_user(comm, (char __user *)arg2,
1786 sizeof(me->comm) - 1) < 0)
1787 return -EFAULT;
1788 set_task_comm(me, comm);
1789 return 0;
1790 case PR_GET_NAME:
1791 get_task_comm(comm, me);
1792 if (copy_to_user((char __user *)arg2, comm,
1793 sizeof(comm)))
1794 return -EFAULT;
1795 return 0;
1796 case PR_GET_ENDIAN:
1797 error = GET_ENDIAN(me, arg2);
1798 break;
1799 case PR_SET_ENDIAN:
1800 error = SET_ENDIAN(me, arg2);
1801 break;
1803 case PR_GET_SECCOMP:
1804 error = prctl_get_seccomp();
1805 break;
1806 case PR_SET_SECCOMP:
1807 error = prctl_set_seccomp(arg2);
1808 break;
1809 case PR_GET_TSC:
1810 error = GET_TSC_CTL(arg2);
1811 break;
1812 case PR_SET_TSC:
1813 error = SET_TSC_CTL(arg2);
1814 break;
1815 case PR_TASK_PERF_COUNTERS_DISABLE:
1816 error = perf_counter_task_disable();
1817 break;
1818 case PR_TASK_PERF_COUNTERS_ENABLE:
1819 error = perf_counter_task_enable();
1820 break;
1821 case PR_GET_TIMERSLACK:
1822 error = current->timer_slack_ns;
1823 break;
1824 case PR_SET_TIMERSLACK:
1825 if (arg2 <= 0)
1826 current->timer_slack_ns =
1827 current->default_timer_slack_ns;
1828 else
1829 current->timer_slack_ns = arg2;
1830 error = 0;
1831 break;
1832 default:
1833 error = -EINVAL;
1834 break;
1836 return error;
1839 SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
1840 struct getcpu_cache __user *, unused)
1842 int err = 0;
1843 int cpu = raw_smp_processor_id();
1844 if (cpup)
1845 err |= put_user(cpu, cpup);
1846 if (nodep)
1847 err |= put_user(cpu_to_node(cpu), nodep);
1848 return err ? -EFAULT : 0;
1851 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
1853 static void argv_cleanup(char **argv, char **envp)
1855 argv_free(argv);
1859 * orderly_poweroff - Trigger an orderly system poweroff
1860 * @force: force poweroff if command execution fails
1862 * This may be called from any context to trigger a system shutdown.
1863 * If the orderly shutdown fails, it will force an immediate shutdown.
1865 int orderly_poweroff(bool force)
1867 int argc;
1868 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
1869 static char *envp[] = {
1870 "HOME=/",
1871 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
1872 NULL
1874 int ret = -ENOMEM;
1875 struct subprocess_info *info;
1877 if (argv == NULL) {
1878 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
1879 __func__, poweroff_cmd);
1880 goto out;
1883 info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
1884 if (info == NULL) {
1885 argv_free(argv);
1886 goto out;
1889 call_usermodehelper_setcleanup(info, argv_cleanup);
1891 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
1893 out:
1894 if (ret && force) {
1895 printk(KERN_WARNING "Failed to start orderly shutdown: "
1896 "forcing the issue\n");
1898 /* I guess this should try to kick off some daemon to
1899 sync and poweroff asap. Or not even bother syncing
1900 if we're doing an emergency shutdown? */
1901 emergency_sync();
1902 kernel_power_off();
1905 return ret;
1907 EXPORT_SYMBOL_GPL(orderly_poweroff);