4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/module.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/init.h>
16 #include <linux/highuid.h>
18 #include <asm/uaccess.h>
21 #ifndef SET_UNALIGN_CTL
22 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
24 #ifndef GET_UNALIGN_CTL
25 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
28 # define SET_FPEMU_CTL(a,b) (-EINVAL)
31 # define GET_FPEMU_CTL(a,b) (-EINVAL)
34 # define SET_FPEXC_CTL(a,b) (-EINVAL)
37 # define GET_FPEXC_CTL(a,b) (-EINVAL)
41 * this is where the system-wide overflow UID and GID are defined, for
42 * architectures that now have 32-bit UID/GID but didn't in the past
45 int overflowuid = DEFAULT_OVERFLOWUID;
46 int overflowgid = DEFAULT_OVERFLOWGID;
49 * the same as above, but for filesystems which can only store a 16-bit
50 * UID and GID. as such, this is needed on all architectures
53 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
54 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
57 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
65 * Notifier list for kernel code which wants to be called
66 * at shutdown. This is used to stop any idling DMA operations
70 static struct notifier_block *reboot_notifier_list;
71 rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
74 * notifier_chain_register - Add notifier to a notifier chain
75 * @list: Pointer to root list pointer
76 * @n: New entry in notifier chain
78 * Adds a notifier to a notifier chain.
80 * Currently always returns zero.
83 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
85 write_lock(¬ifier_lock);
88 if(n->priority > (*list)->priority)
90 list= &((*list)->next);
94 write_unlock(¬ifier_lock);
99 * notifier_chain_unregister - Remove notifier from a notifier chain
100 * @nl: Pointer to root list pointer
101 * @n: New entry in notifier chain
103 * Removes a notifier from a notifier chain.
105 * Returns zero on success, or %-ENOENT on failure.
108 int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
110 write_lock(¬ifier_lock);
116 write_unlock(¬ifier_lock);
121 write_unlock(¬ifier_lock);
126 * notifier_call_chain - Call functions in a notifier chain
127 * @n: Pointer to root pointer of notifier chain
128 * @val: Value passed unmodified to notifier function
129 * @v: Pointer passed unmodified to notifier function
131 * Calls each function in a notifier chain in turn.
133 * If the return value of the notifier can be and'd
134 * with %NOTIFY_STOP_MASK, then notifier_call_chain
135 * will return immediately, with the return value of
136 * the notifier function which halted execution.
137 * Otherwise, the return value is the return value
138 * of the last notifier function called.
141 int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
144 struct notifier_block *nb = *n;
148 ret=nb->notifier_call(nb,val,v);
149 if(ret&NOTIFY_STOP_MASK)
159 * register_reboot_notifier - Register function to be called at reboot time
160 * @nb: Info about notifier function to be called
162 * Registers a function with the list of functions
163 * to be called at reboot time.
165 * Currently always returns zero, as notifier_chain_register
166 * always returns zero.
169 int register_reboot_notifier(struct notifier_block * nb)
171 return notifier_chain_register(&reboot_notifier_list, nb);
175 * unregister_reboot_notifier - Unregister previously registered reboot notifier
176 * @nb: Hook to be unregistered
178 * Unregisters a previously registered reboot
181 * Returns zero on success, or %-ENOENT on failure.
184 int unregister_reboot_notifier(struct notifier_block * nb)
186 return notifier_chain_unregister(&reboot_notifier_list, nb);
189 asmlinkage long sys_ni_syscall(void)
194 static int proc_sel(struct task_struct *p, int which, int who)
200 if (!who && p == current)
202 return(p->pid == who);
206 return(p->pgrp == who);
210 return(p->uid == who);
216 asmlinkage long sys_setpriority(int which, int who, int niceval)
218 struct task_struct *p;
221 if (which > 2 || which < 0)
224 /* normalize: avoid signed division (rounding problems) */
231 read_lock(&tasklist_lock);
233 if (!proc_sel(p, which, who))
235 if (p->uid != current->euid &&
236 p->uid != current->uid && !capable(CAP_SYS_NICE)) {
242 if (niceval < p->nice && !capable(CAP_SYS_NICE))
247 read_unlock(&tasklist_lock);
253 * Ugh. To avoid negative return values, "getpriority()" will
254 * not return the normal nice-value, but a negated value that
255 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
256 * to stay compatible.
258 asmlinkage long sys_getpriority(int which, int who)
260 struct task_struct *p;
261 long retval = -ESRCH;
263 if (which > 2 || which < 0)
266 read_lock(&tasklist_lock);
269 if (!proc_sel(p, which, who))
271 niceval = 20 - p->nice;
272 if (niceval > retval)
275 read_unlock(&tasklist_lock);
282 * Reboot system call: for obvious reasons only root may call it,
283 * and even root needs to set up some magic numbers in the registers
284 * so that some mistake won't make this reboot the whole machine.
285 * You can also set the meaning of the ctrl-alt-del-key here.
287 * reboot doesn't sync: do that yourself before calling this.
289 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void * arg)
293 /* We only trust the superuser with rebooting the system. */
294 if (!capable(CAP_SYS_BOOT))
297 /* For safety, we require "magic" arguments. */
298 if (magic1 != LINUX_REBOOT_MAGIC1 ||
299 (magic2 != LINUX_REBOOT_MAGIC2 && magic2 != LINUX_REBOOT_MAGIC2A &&
300 magic2 != LINUX_REBOOT_MAGIC2B))
305 case LINUX_REBOOT_CMD_RESTART:
306 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
307 printk(KERN_EMERG "Restarting system.\n");
308 machine_restart(NULL);
311 case LINUX_REBOOT_CMD_CAD_ON:
315 case LINUX_REBOOT_CMD_CAD_OFF:
319 case LINUX_REBOOT_CMD_HALT:
320 notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
321 printk(KERN_EMERG "System halted.\n");
326 case LINUX_REBOOT_CMD_POWER_OFF:
327 notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
328 printk(KERN_EMERG "Power down.\n");
333 case LINUX_REBOOT_CMD_RESTART2:
334 if (strncpy_from_user(&buffer[0], (char *)arg, sizeof(buffer) - 1) < 0) {
338 buffer[sizeof(buffer) - 1] = '\0';
340 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, buffer);
341 printk(KERN_EMERG "Restarting system with command '%s'.\n", buffer);
342 machine_restart(buffer);
353 static void deferred_cad(void *dummy)
355 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
356 machine_restart(NULL);
360 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
361 * As it's called within an interrupt, it may NOT sync: the only choice
362 * is whether to reboot at once, or just ignore the ctrl-alt-del.
364 void ctrl_alt_del(void)
366 static struct tq_struct cad_tq = {
367 routine: deferred_cad,
371 schedule_task(&cad_tq);
373 kill_proc(cad_pid, SIGINT, 1);
378 * Unprivileged users may change the real gid to the effective gid
379 * or vice versa. (BSD-style)
381 * If you set the real gid at all, or set the effective gid to a value not
382 * equal to the real gid, then the saved gid is set to the new effective gid.
384 * This makes it possible for a setgid program to completely drop its
385 * privileges, which is often a useful assertion to make when you are doing
386 * a security audit over a program.
388 * The general idea is that a program which uses just setregid() will be
389 * 100% compatible with BSD. A program which uses just setgid() will be
390 * 100% compatible with POSIX with saved IDs.
392 * SMP: There are not races, the GIDs are checked only by filesystem
393 * operations (as far as semantic preservation is concerned).
395 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
397 int old_rgid = current->gid;
398 int old_egid = current->egid;
399 int new_rgid = old_rgid;
400 int new_egid = old_egid;
402 if (rgid != (gid_t) -1) {
403 if ((old_rgid == rgid) ||
404 (current->egid==rgid) ||
410 if (egid != (gid_t) -1) {
411 if ((old_rgid == egid) ||
412 (current->egid == egid) ||
413 (current->sgid == egid) ||
420 if (new_egid != old_egid)
422 current->mm->dumpable = 0;
425 if (rgid != (gid_t) -1 ||
426 (egid != (gid_t) -1 && egid != old_rgid))
427 current->sgid = new_egid;
428 current->fsgid = new_egid;
429 current->egid = new_egid;
430 current->gid = new_rgid;
435 * setgid() is implemented like SysV w/ SAVED_IDS
437 * SMP: Same implicit races as above.
439 asmlinkage long sys_setgid(gid_t gid)
441 int old_egid = current->egid;
443 if (capable(CAP_SETGID))
447 current->mm->dumpable=0;
450 current->gid = current->egid = current->sgid = current->fsgid = gid;
452 else if ((gid == current->gid) || (gid == current->sgid))
456 current->mm->dumpable=0;
459 current->egid = current->fsgid = gid;
467 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
468 * a process after a call to setuid, setreuid, or setresuid.
470 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
471 * {r,e,s}uid != 0, the permitted and effective capabilities are
474 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
475 * capabilities of the process are cleared.
477 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
478 * capabilities are set to the permitted capabilities.
480 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
485 * cevans - New behaviour, Oct '99
486 * A process may, via prctl(), elect to keep its capabilities when it
487 * calls setuid() and switches away from uid==0. Both permitted and
488 * effective sets will be retained.
489 * Without this change, it was impossible for a daemon to drop only some
490 * of its privilege. The call to setuid(!=0) would drop all privileges!
491 * Keeping uid 0 is not an option because uid 0 owns too many vital
493 * Thanks to Olaf Kirch and Peter Benie for spotting this.
495 static inline void cap_emulate_setxuid(int old_ruid, int old_euid,
498 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
499 (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
500 !current->keep_capabilities) {
501 cap_clear(current->cap_permitted);
502 cap_clear(current->cap_effective);
504 if (old_euid == 0 && current->euid != 0) {
505 cap_clear(current->cap_effective);
507 if (old_euid != 0 && current->euid == 0) {
508 current->cap_effective = current->cap_permitted;
512 static int set_user(uid_t new_ruid, int dumpclear)
514 struct user_struct *new_user, *old_user;
516 /* What if a process setreuid()'s and this brings the
517 * new uid over his NPROC rlimit? We can check this now
518 * cheaply with the new uid cache, so if it matters
519 * we should be checking for it. -DaveM
521 new_user = alloc_uid(new_ruid);
524 old_user = current->user;
525 atomic_dec(&old_user->processes);
526 atomic_inc(&new_user->processes);
530 current->mm->dumpable = 0;
533 current->uid = new_ruid;
534 current->user = new_user;
540 * Unprivileged users may change the real uid to the effective uid
541 * or vice versa. (BSD-style)
543 * If you set the real uid at all, or set the effective uid to a value not
544 * equal to the real uid, then the saved uid is set to the new effective uid.
546 * This makes it possible for a setuid program to completely drop its
547 * privileges, which is often a useful assertion to make when you are doing
548 * a security audit over a program.
550 * The general idea is that a program which uses just setreuid() will be
551 * 100% compatible with BSD. A program which uses just setuid() will be
552 * 100% compatible with POSIX with saved IDs.
554 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
556 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
558 new_ruid = old_ruid = current->uid;
559 new_euid = old_euid = current->euid;
560 old_suid = current->suid;
562 if (ruid != (uid_t) -1) {
564 if ((old_ruid != ruid) &&
565 (current->euid != ruid) &&
566 !capable(CAP_SETUID))
570 if (euid != (uid_t) -1) {
572 if ((old_ruid != euid) &&
573 (current->euid != euid) &&
574 (current->suid != euid) &&
575 !capable(CAP_SETUID))
579 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
582 if (new_euid != old_euid)
584 current->mm->dumpable=0;
587 current->fsuid = current->euid = new_euid;
588 if (ruid != (uid_t) -1 ||
589 (euid != (uid_t) -1 && euid != old_ruid))
590 current->suid = current->euid;
591 current->fsuid = current->euid;
593 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
594 cap_emulate_setxuid(old_ruid, old_euid, old_suid);
603 * setuid() is implemented like SysV with SAVED_IDS
605 * Note that SAVED_ID's is deficient in that a setuid root program
606 * like sendmail, for example, cannot set its uid to be a normal
607 * user and then switch back, because if you're root, setuid() sets
608 * the saved uid too. If you don't like this, blame the bright people
609 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
610 * will allow a root program to temporarily drop privileges and be able to
611 * regain them by swapping the real and effective uid.
613 asmlinkage long sys_setuid(uid_t uid)
615 int old_euid = current->euid;
616 int old_ruid, old_suid, new_ruid, new_suid;
618 old_ruid = new_ruid = current->uid;
619 old_suid = current->suid;
622 if (capable(CAP_SETUID)) {
623 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
626 } else if ((uid != current->uid) && (uid != new_suid))
631 current->mm->dumpable = 0;
634 current->fsuid = current->euid = uid;
635 current->suid = new_suid;
637 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
638 cap_emulate_setxuid(old_ruid, old_euid, old_suid);
646 * This function implements a generic ability to update ruid, euid,
647 * and suid. This allows you to implement the 4.4 compatible seteuid().
649 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
651 int old_ruid = current->uid;
652 int old_euid = current->euid;
653 int old_suid = current->suid;
655 if (!capable(CAP_SETUID)) {
656 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
657 (ruid != current->euid) && (ruid != current->suid))
659 if ((euid != (uid_t) -1) && (euid != current->uid) &&
660 (euid != current->euid) && (euid != current->suid))
662 if ((suid != (uid_t) -1) && (suid != current->uid) &&
663 (suid != current->euid) && (suid != current->suid))
666 if (ruid != (uid_t) -1) {
667 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
670 if (euid != (uid_t) -1) {
671 if (euid != current->euid)
673 current->mm->dumpable = 0;
676 current->euid = euid;
678 current->fsuid = current->euid;
679 if (suid != (uid_t) -1)
680 current->suid = suid;
682 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
683 cap_emulate_setxuid(old_ruid, old_euid, old_suid);
689 asmlinkage long sys_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
693 if (!(retval = put_user(current->uid, ruid)) &&
694 !(retval = put_user(current->euid, euid)))
695 retval = put_user(current->suid, suid);
701 * Same as above, but for rgid, egid, sgid.
703 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
705 if (!capable(CAP_SETGID)) {
706 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
707 (rgid != current->egid) && (rgid != current->sgid))
709 if ((egid != (gid_t) -1) && (egid != current->gid) &&
710 (egid != current->egid) && (egid != current->sgid))
712 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
713 (sgid != current->egid) && (sgid != current->sgid))
716 if (egid != (gid_t) -1) {
717 if (egid != current->egid)
719 current->mm->dumpable = 0;
722 current->egid = egid;
724 current->fsgid = current->egid;
725 if (rgid != (gid_t) -1)
727 if (sgid != (gid_t) -1)
728 current->sgid = sgid;
732 asmlinkage long sys_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
736 if (!(retval = put_user(current->gid, rgid)) &&
737 !(retval = put_user(current->egid, egid)))
738 retval = put_user(current->sgid, sgid);
745 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
746 * is used for "access()" and for the NFS daemon (letting nfsd stay at
747 * whatever uid it wants to). It normally shadows "euid", except when
748 * explicitly set by setfsuid() or for access..
750 asmlinkage long sys_setfsuid(uid_t uid)
754 old_fsuid = current->fsuid;
755 if (uid == current->uid || uid == current->euid ||
756 uid == current->suid || uid == current->fsuid ||
759 if (uid != old_fsuid)
761 current->mm->dumpable = 0;
764 current->fsuid = uid;
767 /* We emulate fsuid by essentially doing a scaled-down version
768 * of what we did in setresuid and friends. However, we only
769 * operate on the fs-specific bits of the process' effective
772 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
773 * if not, we might be a bit too harsh here.
776 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
777 if (old_fsuid == 0 && current->fsuid != 0) {
778 cap_t(current->cap_effective) &= ~CAP_FS_MASK;
780 if (old_fsuid != 0 && current->fsuid == 0) {
781 cap_t(current->cap_effective) |=
782 (cap_t(current->cap_permitted) & CAP_FS_MASK);
790 * Samma på svenska..
792 asmlinkage long sys_setfsgid(gid_t gid)
796 old_fsgid = current->fsgid;
797 if (gid == current->gid || gid == current->egid ||
798 gid == current->sgid || gid == current->fsgid ||
801 if (gid != old_fsgid)
803 current->mm->dumpable = 0;
806 current->fsgid = gid;
811 asmlinkage long sys_times(struct tms * tbuf)
814 * In the SMP world we might just be unlucky and have one of
815 * the times increment as we use it. Since the value is an
816 * atomically safe type this is just fine. Conceptually its
817 * as if the syscall took an instant longer to occur.
820 if (copy_to_user(tbuf, ¤t->times, sizeof(struct tms)))
826 * This needs some heavy checking ...
827 * I just haven't the stomach for it. I also don't fully
828 * understand sessions/pgrp etc. Let somebody who does explain it.
830 * OK, I think I have the protection semantics right.... this is really
831 * only important on a multi-user system anyway, to make sure one user
832 * can't send a signal to a process owned by another. -TYT, 12/12/91
834 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
838 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
840 struct task_struct * p;
850 /* From this point forward we keep holding onto the tasklist lock
851 * so that our parent does not change from under us. -DaveM
853 read_lock(&tasklist_lock);
856 p = find_task_by_pid(pid);
860 if (p->p_pptr == current || p->p_opptr == current) {
862 if (p->session != current->session)
867 } else if (p != current)
873 struct task_struct * tmp;
874 for_each_task (tmp) {
875 if (tmp->pgrp == pgid &&
876 tmp->session == current->session)
886 /* All paths lead to here, thus we are safe. -DaveM */
887 read_unlock(&tasklist_lock);
891 asmlinkage long sys_getpgid(pid_t pid)
894 return current->pgrp;
897 struct task_struct *p;
899 read_lock(&tasklist_lock);
900 p = find_task_by_pid(pid);
905 read_unlock(&tasklist_lock);
910 asmlinkage long sys_getpgrp(void)
912 /* SMP - assuming writes are word atomic this is fine */
913 return current->pgrp;
916 asmlinkage long sys_getsid(pid_t pid)
919 return current->session;
922 struct task_struct *p;
924 read_lock(&tasklist_lock);
925 p = find_task_by_pid(pid);
930 read_unlock(&tasklist_lock);
935 asmlinkage long sys_setsid(void)
937 struct task_struct * p;
940 read_lock(&tasklist_lock);
942 if (p->pgrp == current->pid)
947 current->session = current->pgrp = current->pid;
949 current->tty_old_pgrp = 0;
952 read_unlock(&tasklist_lock);
957 * Supplementary group IDs
959 asmlinkage long sys_getgroups(int gidsetsize, gid_t *grouplist)
964 * SMP: Nobody else can change our grouplist. Thus we are
970 i = current->ngroups;
974 if (copy_to_user(grouplist, current->groups, sizeof(gid_t)*i))
981 * SMP: Our groups are not shared. We can copy to/from them safely
982 * without another task interfering.
985 asmlinkage long sys_setgroups(int gidsetsize, gid_t *grouplist)
987 if (!capable(CAP_SETGID))
989 if ((unsigned) gidsetsize > NGROUPS)
991 if(copy_from_user(current->groups, grouplist, gidsetsize * sizeof(gid_t)))
993 current->ngroups = gidsetsize;
997 static int supplemental_group_member(gid_t grp)
999 int i = current->ngroups;
1002 gid_t *groups = current->groups;
1014 * Check whether we're fsgid/egid or in the supplemental group..
1016 int in_group_p(gid_t grp)
1019 if (grp != current->fsgid)
1020 retval = supplemental_group_member(grp);
1024 int in_egroup_p(gid_t grp)
1027 if (grp != current->egid)
1028 retval = supplemental_group_member(grp);
1032 DECLARE_RWSEM(uts_sem);
1034 asmlinkage long sys_newuname(struct new_utsname * name)
1038 down_read(&uts_sem);
1039 if (copy_to_user(name,&system_utsname,sizeof *name))
1045 asmlinkage long sys_sethostname(char *name, int len)
1049 if (!capable(CAP_SYS_ADMIN))
1051 if (len < 0 || len > __NEW_UTS_LEN)
1053 down_write(&uts_sem);
1055 if (!copy_from_user(system_utsname.nodename, name, len)) {
1056 system_utsname.nodename[len] = 0;
1063 asmlinkage long sys_gethostname(char *name, int len)
1069 down_read(&uts_sem);
1070 i = 1 + strlen(system_utsname.nodename);
1074 if (copy_to_user(name, system_utsname.nodename, i))
1081 * Only setdomainname; getdomainname can be implemented by calling
1084 asmlinkage long sys_setdomainname(char *name, int len)
1088 if (!capable(CAP_SYS_ADMIN))
1090 if (len < 0 || len > __NEW_UTS_LEN)
1093 down_write(&uts_sem);
1095 if (!copy_from_user(system_utsname.domainname, name, len)) {
1097 system_utsname.domainname[len] = 0;
1103 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit *rlim)
1105 if (resource >= RLIM_NLIMITS)
1108 return copy_to_user(rlim, current->rlim + resource, sizeof(*rlim))
1112 #if !defined(__ia64__)
1115 * Back compatibility for getrlimit. Needed for some apps.
1118 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit *rlim)
1121 if (resource >= RLIM_NLIMITS)
1124 memcpy(&x, current->rlim + resource, sizeof(*rlim));
1125 if(x.rlim_cur > 0x7FFFFFFF)
1126 x.rlim_cur = 0x7FFFFFFF;
1127 if(x.rlim_max > 0x7FFFFFFF)
1128 x.rlim_max = 0x7FFFFFFF;
1129 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1134 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit *rlim)
1136 struct rlimit new_rlim, *old_rlim;
1138 if (resource >= RLIM_NLIMITS)
1140 if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1142 old_rlim = current->rlim + resource;
1143 if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
1144 (new_rlim.rlim_max > old_rlim->rlim_max)) &&
1145 !capable(CAP_SYS_RESOURCE))
1147 if (resource == RLIMIT_NOFILE) {
1148 if (new_rlim.rlim_cur > NR_OPEN || new_rlim.rlim_max > NR_OPEN)
1151 *old_rlim = new_rlim;
1156 * It would make sense to put struct rusage in the task_struct,
1157 * except that would make the task_struct be *really big*. After
1158 * task_struct gets moved into malloc'ed memory, it would
1159 * make sense to do this. It will make moving the rest of the information
1160 * a lot simpler! (Which we're not doing right now because we're not
1161 * measuring them yet).
1163 * This is SMP safe. Either we are called from sys_getrusage on ourselves
1164 * below (we know we aren't going to exit/disappear and only we change our
1165 * rusage counters), or we are called from wait4() on a process which is
1166 * either stopped or zombied. In the zombied case the task won't get
1167 * reaped till shortly after the call to getrusage(), in both cases the
1168 * task being examined is in a frozen state so the counters won't change.
1170 * FIXME! Get the fault counts properly!
1172 int getrusage(struct task_struct *p, int who, struct rusage *ru)
1176 memset((char *) &r, 0, sizeof(r));
1179 r.ru_utime.tv_sec = CT_TO_SECS(p->times.tms_utime);
1180 r.ru_utime.tv_usec = CT_TO_USECS(p->times.tms_utime);
1181 r.ru_stime.tv_sec = CT_TO_SECS(p->times.tms_stime);
1182 r.ru_stime.tv_usec = CT_TO_USECS(p->times.tms_stime);
1183 r.ru_minflt = p->min_flt;
1184 r.ru_majflt = p->maj_flt;
1185 r.ru_nswap = p->nswap;
1187 case RUSAGE_CHILDREN:
1188 r.ru_utime.tv_sec = CT_TO_SECS(p->times.tms_cutime);
1189 r.ru_utime.tv_usec = CT_TO_USECS(p->times.tms_cutime);
1190 r.ru_stime.tv_sec = CT_TO_SECS(p->times.tms_cstime);
1191 r.ru_stime.tv_usec = CT_TO_USECS(p->times.tms_cstime);
1192 r.ru_minflt = p->cmin_flt;
1193 r.ru_majflt = p->cmaj_flt;
1194 r.ru_nswap = p->cnswap;
1197 r.ru_utime.tv_sec = CT_TO_SECS(p->times.tms_utime + p->times.tms_cutime);
1198 r.ru_utime.tv_usec = CT_TO_USECS(p->times.tms_utime + p->times.tms_cutime);
1199 r.ru_stime.tv_sec = CT_TO_SECS(p->times.tms_stime + p->times.tms_cstime);
1200 r.ru_stime.tv_usec = CT_TO_USECS(p->times.tms_stime + p->times.tms_cstime);
1201 r.ru_minflt = p->min_flt + p->cmin_flt;
1202 r.ru_majflt = p->maj_flt + p->cmaj_flt;
1203 r.ru_nswap = p->nswap + p->cnswap;
1206 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1209 asmlinkage long sys_getrusage(int who, struct rusage *ru)
1211 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1213 return getrusage(current, who, ru);
1216 asmlinkage long sys_umask(int mask)
1218 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1222 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1223 unsigned long arg4, unsigned long arg5)
1229 case PR_SET_PDEATHSIG:
1231 if (sig < 0 || sig > _NSIG) {
1235 current->pdeath_signal = sig;
1237 case PR_GET_PDEATHSIG:
1238 error = put_user(current->pdeath_signal, (int *)arg2);
1240 case PR_GET_DUMPABLE:
1241 if (current->mm->dumpable)
1244 case PR_SET_DUMPABLE:
1245 if (arg2 != 0 && arg2 != 1) {
1249 current->mm->dumpable = arg2;
1252 case PR_SET_UNALIGN:
1253 error = SET_UNALIGN_CTL(current, arg2);
1255 case PR_GET_UNALIGN:
1256 error = GET_UNALIGN_CTL(current, arg2);
1259 error = SET_FPEMU_CTL(current, arg2);
1262 error = GET_FPEMU_CTL(current, arg2);
1265 error = SET_FPEXC_CTL(current, arg2);
1268 error = GET_FPEXC_CTL(current, arg2);
1271 case PR_GET_KEEPCAPS:
1272 if (current->keep_capabilities)
1275 case PR_SET_KEEPCAPS:
1276 if (arg2 != 0 && arg2 != 1) {
1280 current->keep_capabilities = arg2;
1283 #ifdef SET_FP_EXC_MODE
1285 error = SET_FP_EXC_MODE(current, arg2);
1288 error = GET_FP_EXC_MODE(current);
1299 EXPORT_SYMBOL(notifier_chain_register);
1300 EXPORT_SYMBOL(notifier_chain_unregister);
1301 EXPORT_SYMBOL(notifier_call_chain);
1302 EXPORT_SYMBOL(register_reboot_notifier);
1303 EXPORT_SYMBOL(unregister_reboot_notifier);
1304 EXPORT_SYMBOL(in_group_p);
1305 EXPORT_SYMBOL(in_egroup_p);