Get it to compile again ...
[linux-2.6/linux-mips.git] / kernel / sys.c
blob5c2c439ae6bc1d3f9ed7500809b0f3e37200518a
1 /*
2 * linux/kernel/sys.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/module.h>
9 #include <linux/mm.h>
10 #include <linux/utsname.h>
11 #include <linux/mman.h>
12 #include <linux/smp_lock.h>
13 #include <linux/notifier.h>
14 #include <linux/reboot.h>
15 #include <linux/prctl.h>
16 #include <linux/init.h>
17 #include <linux/highuid.h>
18 #include <linux/fs.h>
19 #include <linux/workqueue.h>
20 #include <linux/device.h>
21 #include <linux/times.h>
22 #include <linux/security.h>
23 #include <linux/dcookies.h>
24 #include <linux/suspend.h>
26 #include <asm/uaccess.h>
27 #include <asm/io.h>
28 #include <asm/unistd.h>
30 #ifndef SET_UNALIGN_CTL
31 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
32 #endif
33 #ifndef GET_UNALIGN_CTL
34 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
35 #endif
36 #ifndef SET_FPEMU_CTL
37 # define SET_FPEMU_CTL(a,b) (-EINVAL)
38 #endif
39 #ifndef GET_FPEMU_CTL
40 # define GET_FPEMU_CTL(a,b) (-EINVAL)
41 #endif
42 #ifndef SET_FPEXC_CTL
43 # define SET_FPEXC_CTL(a,b) (-EINVAL)
44 #endif
45 #ifndef GET_FPEXC_CTL
46 # define GET_FPEXC_CTL(a,b) (-EINVAL)
47 #endif
50 * this is where the system-wide overflow UID and GID are defined, for
51 * architectures that now have 32-bit UID/GID but didn't in the past
54 int overflowuid = DEFAULT_OVERFLOWUID;
55 int overflowgid = DEFAULT_OVERFLOWGID;
58 * the same as above, but for filesystems which can only store a 16-bit
59 * UID and GID. as such, this is needed on all architectures
62 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
63 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
66 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
69 int C_A_D = 1;
70 int cad_pid = 1;
72 extern int system_running;
75 * Notifier list for kernel code which wants to be called
76 * at shutdown. This is used to stop any idling DMA operations
77 * and the like.
80 static struct notifier_block *reboot_notifier_list;
81 rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
83 /**
84 * notifier_chain_register - Add notifier to a notifier chain
85 * @list: Pointer to root list pointer
86 * @n: New entry in notifier chain
88 * Adds a notifier to a notifier chain.
90 * Currently always returns zero.
93 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
95 write_lock(&notifier_lock);
96 while(*list)
98 if(n->priority > (*list)->priority)
99 break;
100 list= &((*list)->next);
102 n->next = *list;
103 *list=n;
104 write_unlock(&notifier_lock);
105 return 0;
109 * notifier_chain_unregister - Remove notifier from a notifier chain
110 * @nl: Pointer to root list pointer
111 * @n: New entry in notifier chain
113 * Removes a notifier from a notifier chain.
115 * Returns zero on success, or %-ENOENT on failure.
118 int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
120 write_lock(&notifier_lock);
121 while((*nl)!=NULL)
123 if((*nl)==n)
125 *nl=n->next;
126 write_unlock(&notifier_lock);
127 return 0;
129 nl=&((*nl)->next);
131 write_unlock(&notifier_lock);
132 return -ENOENT;
136 * notifier_call_chain - Call functions in a notifier chain
137 * @n: Pointer to root pointer of notifier chain
138 * @val: Value passed unmodified to notifier function
139 * @v: Pointer passed unmodified to notifier function
141 * Calls each function in a notifier chain in turn.
143 * If the return value of the notifier can be and'd
144 * with %NOTIFY_STOP_MASK, then notifier_call_chain
145 * will return immediately, with the return value of
146 * the notifier function which halted execution.
147 * Otherwise, the return value is the return value
148 * of the last notifier function called.
151 int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
153 int ret=NOTIFY_DONE;
154 struct notifier_block *nb = *n;
156 while(nb)
158 ret=nb->notifier_call(nb,val,v);
159 if(ret&NOTIFY_STOP_MASK)
161 return ret;
163 nb=nb->next;
165 return ret;
169 * register_reboot_notifier - Register function to be called at reboot time
170 * @nb: Info about notifier function to be called
172 * Registers a function with the list of functions
173 * to be called at reboot time.
175 * Currently always returns zero, as notifier_chain_register
176 * always returns zero.
179 int register_reboot_notifier(struct notifier_block * nb)
181 return notifier_chain_register(&reboot_notifier_list, nb);
185 * unregister_reboot_notifier - Unregister previously registered reboot notifier
186 * @nb: Hook to be unregistered
188 * Unregisters a previously registered reboot
189 * notifier function.
191 * Returns zero on success, or %-ENOENT on failure.
194 int unregister_reboot_notifier(struct notifier_block * nb)
196 return notifier_chain_unregister(&reboot_notifier_list, nb);
199 asmlinkage long sys_ni_syscall(void)
201 return -ENOSYS;
204 cond_syscall(sys_nfsservctl)
205 cond_syscall(sys_quotactl)
206 cond_syscall(sys_acct)
207 cond_syscall(sys_lookup_dcookie)
208 cond_syscall(sys_swapon)
209 cond_syscall(sys_swapoff)
210 cond_syscall(sys_init_module)
211 cond_syscall(sys_delete_module)
212 cond_syscall(sys_socketpair)
213 cond_syscall(sys_bind)
214 cond_syscall(sys_listen)
215 cond_syscall(sys_accept)
216 cond_syscall(sys_connect)
217 cond_syscall(sys_getsockname)
218 cond_syscall(sys_getpeername)
219 cond_syscall(sys_sendto)
220 cond_syscall(sys_send)
221 cond_syscall(sys_recvfrom)
222 cond_syscall(sys_recv)
223 cond_syscall(sys_setsockopt)
224 cond_syscall(sys_getsockopt)
225 cond_syscall(sys_shutdown)
226 cond_syscall(sys_sendmsg)
227 cond_syscall(sys_recvmsg)
228 cond_syscall(sys_socketcall)
229 cond_syscall(sys_futex)
230 cond_syscall(compat_sys_futex)
231 cond_syscall(sys_epoll_create)
232 cond_syscall(sys_epoll_ctl)
233 cond_syscall(sys_epoll_wait)
235 static int set_one_prio(struct task_struct *p, int niceval, int error)
237 int no_nice;
239 if (p->uid != current->euid &&
240 p->uid != current->uid && !capable(CAP_SYS_NICE)) {
241 error = -EPERM;
242 goto out;
244 if (niceval < task_nice(p) && !capable(CAP_SYS_NICE)) {
245 error = -EACCES;
246 goto out;
248 no_nice = security_task_setnice(p, niceval);
249 if (no_nice) {
250 error = no_nice;
251 goto out;
253 if (error == -ESRCH)
254 error = 0;
255 set_user_nice(p, niceval);
256 out:
257 return error;
260 asmlinkage long sys_setpriority(int which, int who, int niceval)
262 struct task_struct *g, *p;
263 struct user_struct *user;
264 struct pid *pid;
265 struct list_head *l;
266 int error = -EINVAL;
268 if (which > 2 || which < 0)
269 goto out;
271 /* normalize: avoid signed division (rounding problems) */
272 error = -ESRCH;
273 if (niceval < -20)
274 niceval = -20;
275 if (niceval > 19)
276 niceval = 19;
278 read_lock(&tasklist_lock);
279 switch (which) {
280 case PRIO_PROCESS:
281 if (!who)
282 who = current->pid;
283 p = find_task_by_pid(who);
284 if (p)
285 error = set_one_prio(p, niceval, error);
286 break;
287 case PRIO_PGRP:
288 if (!who)
289 who = current->pgrp;
290 for_each_task_pid(who, PIDTYPE_PGID, p, l, pid)
291 error = set_one_prio(p, niceval, error);
292 break;
293 case PRIO_USER:
294 if (!who)
295 user = current->user;
296 else
297 user = find_user(who);
299 if (!user)
300 goto out_unlock;
302 do_each_thread(g, p)
303 if (p->uid == who)
304 error = set_one_prio(p, niceval, error);
305 while_each_thread(g, p);
306 break;
308 out_unlock:
309 read_unlock(&tasklist_lock);
310 out:
311 return error;
315 * Ugh. To avoid negative return values, "getpriority()" will
316 * not return the normal nice-value, but a negated value that
317 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
318 * to stay compatible.
320 asmlinkage long sys_getpriority(int which, int who)
322 struct task_struct *g, *p;
323 struct list_head *l;
324 struct pid *pid;
325 struct user_struct *user;
326 long niceval, retval = -ESRCH;
328 if (which > 2 || which < 0)
329 return -EINVAL;
331 read_lock(&tasklist_lock);
332 switch (which) {
333 case PRIO_PROCESS:
334 if (!who)
335 who = current->pid;
336 p = find_task_by_pid(who);
337 if (p) {
338 niceval = 20 - task_nice(p);
339 if (niceval > retval)
340 retval = niceval;
342 break;
343 case PRIO_PGRP:
344 if (!who)
345 who = current->pgrp;
346 for_each_task_pid(who, PIDTYPE_PGID, p, l, pid) {
347 niceval = 20 - task_nice(p);
348 if (niceval > retval)
349 retval = niceval;
351 break;
352 case PRIO_USER:
353 if (!who)
354 user = current->user;
355 else
356 user = find_user(who);
358 if (!user)
359 goto out_unlock;
361 do_each_thread(g, p)
362 if (p->uid == who) {
363 niceval = 20 - task_nice(p);
364 if (niceval > retval)
365 retval = niceval;
367 while_each_thread(g, p);
368 break;
370 out_unlock:
371 read_unlock(&tasklist_lock);
373 return retval;
378 * Reboot system call: for obvious reasons only root may call it,
379 * and even root needs to set up some magic numbers in the registers
380 * so that some mistake won't make this reboot the whole machine.
381 * You can also set the meaning of the ctrl-alt-del-key here.
383 * reboot doesn't sync: do that yourself before calling this.
385 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
387 char buffer[256];
389 /* We only trust the superuser with rebooting the system. */
390 if (!capable(CAP_SYS_BOOT))
391 return -EPERM;
393 /* For safety, we require "magic" arguments. */
394 if (magic1 != LINUX_REBOOT_MAGIC1 ||
395 (magic2 != LINUX_REBOOT_MAGIC2 && magic2 != LINUX_REBOOT_MAGIC2A &&
396 magic2 != LINUX_REBOOT_MAGIC2B))
397 return -EINVAL;
399 lock_kernel();
400 switch (cmd) {
401 case LINUX_REBOOT_CMD_RESTART:
402 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
403 system_running = 0;
404 device_shutdown();
405 printk(KERN_EMERG "Restarting system.\n");
406 machine_restart(NULL);
407 break;
409 case LINUX_REBOOT_CMD_CAD_ON:
410 C_A_D = 1;
411 break;
413 case LINUX_REBOOT_CMD_CAD_OFF:
414 C_A_D = 0;
415 break;
417 case LINUX_REBOOT_CMD_HALT:
418 notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
419 system_running = 0;
420 device_shutdown();
421 printk(KERN_EMERG "System halted.\n");
422 machine_halt();
423 unlock_kernel();
424 do_exit(0);
425 break;
427 case LINUX_REBOOT_CMD_POWER_OFF:
428 notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
429 system_running = 0;
430 device_shutdown();
431 printk(KERN_EMERG "Power down.\n");
432 machine_power_off();
433 unlock_kernel();
434 do_exit(0);
435 break;
437 case LINUX_REBOOT_CMD_RESTART2:
438 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
439 unlock_kernel();
440 return -EFAULT;
442 buffer[sizeof(buffer) - 1] = '\0';
444 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, buffer);
445 system_running = 0;
446 device_shutdown();
447 printk(KERN_EMERG "Restarting system with command '%s'.\n", buffer);
448 machine_restart(buffer);
449 break;
451 #ifdef CONFIG_SOFTWARE_SUSPEND
452 case LINUX_REBOOT_CMD_SW_SUSPEND:
453 if (!software_suspend_enabled) {
454 unlock_kernel();
455 return -EAGAIN;
457 software_suspend();
458 do_exit(0);
459 break;
460 #endif
462 default:
463 unlock_kernel();
464 return -EINVAL;
466 unlock_kernel();
467 return 0;
470 static void deferred_cad(void *dummy)
472 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
473 machine_restart(NULL);
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.
481 void ctrl_alt_del(void)
483 static DECLARE_WORK(cad_work, deferred_cad, NULL);
485 if (C_A_D)
486 schedule_work(&cad_work);
487 else
488 kill_proc(cad_pid, SIGINT, 1);
493 * Unprivileged users may change the real gid to the effective gid
494 * or vice versa. (BSD-style)
496 * If you set the real gid at all, or set the effective gid to a value not
497 * equal to the real gid, then the saved gid is set to the new effective gid.
499 * This makes it possible for a setgid program to completely drop its
500 * privileges, which is often a useful assertion to make when you are doing
501 * a security audit over a program.
503 * The general idea is that a program which uses just setregid() will be
504 * 100% compatible with BSD. A program which uses just setgid() will be
505 * 100% compatible with POSIX with saved IDs.
507 * SMP: There are not races, the GIDs are checked only by filesystem
508 * operations (as far as semantic preservation is concerned).
510 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
512 int old_rgid = current->gid;
513 int old_egid = current->egid;
514 int new_rgid = old_rgid;
515 int new_egid = old_egid;
516 int retval;
518 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
519 if (retval)
520 return retval;
522 if (rgid != (gid_t) -1) {
523 if ((old_rgid == rgid) ||
524 (current->egid==rgid) ||
525 capable(CAP_SETGID))
526 new_rgid = rgid;
527 else
528 return -EPERM;
530 if (egid != (gid_t) -1) {
531 if ((old_rgid == egid) ||
532 (current->egid == egid) ||
533 (current->sgid == egid) ||
534 capable(CAP_SETGID))
535 new_egid = egid;
536 else {
537 return -EPERM;
540 if (new_egid != old_egid)
542 current->mm->dumpable = 0;
543 wmb();
545 if (rgid != (gid_t) -1 ||
546 (egid != (gid_t) -1 && egid != old_rgid))
547 current->sgid = new_egid;
548 current->fsgid = new_egid;
549 current->egid = new_egid;
550 current->gid = new_rgid;
551 return 0;
555 * setgid() is implemented like SysV w/ SAVED_IDS
557 * SMP: Same implicit races as above.
559 asmlinkage long sys_setgid(gid_t gid)
561 int old_egid = current->egid;
562 int retval;
564 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
565 if (retval)
566 return retval;
568 if (capable(CAP_SETGID))
570 if(old_egid != gid)
572 current->mm->dumpable=0;
573 wmb();
575 current->gid = current->egid = current->sgid = current->fsgid = gid;
577 else if ((gid == current->gid) || (gid == current->sgid))
579 if(old_egid != gid)
581 current->mm->dumpable=0;
582 wmb();
584 current->egid = current->fsgid = gid;
586 else
587 return -EPERM;
588 return 0;
591 static int set_user(uid_t new_ruid, int dumpclear)
593 struct user_struct *new_user;
595 new_user = alloc_uid(new_ruid);
596 if (!new_user)
597 return -EAGAIN;
598 switch_uid(new_user);
600 if(dumpclear)
602 current->mm->dumpable = 0;
603 wmb();
605 current->uid = new_ruid;
606 return 0;
610 * Unprivileged users may change the real uid to the effective uid
611 * or vice versa. (BSD-style)
613 * If you set the real uid at all, or set the effective uid to a value not
614 * equal to the real uid, then the saved uid is set to the new effective uid.
616 * This makes it possible for a setuid program to completely drop its
617 * privileges, which is often a useful assertion to make when you are doing
618 * a security audit over a program.
620 * The general idea is that a program which uses just setreuid() will be
621 * 100% compatible with BSD. A program which uses just setuid() will be
622 * 100% compatible with POSIX with saved IDs.
624 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
626 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
627 int retval;
629 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
630 if (retval)
631 return retval;
633 new_ruid = old_ruid = current->uid;
634 new_euid = old_euid = current->euid;
635 old_suid = current->suid;
637 if (ruid != (uid_t) -1) {
638 new_ruid = ruid;
639 if ((old_ruid != ruid) &&
640 (current->euid != ruid) &&
641 !capable(CAP_SETUID))
642 return -EPERM;
645 if (euid != (uid_t) -1) {
646 new_euid = euid;
647 if ((old_ruid != euid) &&
648 (current->euid != euid) &&
649 (current->suid != euid) &&
650 !capable(CAP_SETUID))
651 return -EPERM;
654 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
655 return -EAGAIN;
657 if (new_euid != old_euid)
659 current->mm->dumpable=0;
660 wmb();
662 current->fsuid = current->euid = new_euid;
663 if (ruid != (uid_t) -1 ||
664 (euid != (uid_t) -1 && euid != old_ruid))
665 current->suid = current->euid;
666 current->fsuid = current->euid;
668 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
674 * setuid() is implemented like SysV with SAVED_IDS
676 * Note that SAVED_ID's is deficient in that a setuid root program
677 * like sendmail, for example, cannot set its uid to be a normal
678 * user and then switch back, because if you're root, setuid() sets
679 * the saved uid too. If you don't like this, blame the bright people
680 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
681 * will allow a root program to temporarily drop privileges and be able to
682 * regain them by swapping the real and effective uid.
684 asmlinkage long sys_setuid(uid_t uid)
686 int old_euid = current->euid;
687 int old_ruid, old_suid, new_ruid, new_suid;
688 int retval;
690 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
691 if (retval)
692 return retval;
694 old_ruid = new_ruid = current->uid;
695 old_suid = current->suid;
696 new_suid = old_suid;
698 if (capable(CAP_SETUID)) {
699 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
700 return -EAGAIN;
701 new_suid = uid;
702 } else if ((uid != current->uid) && (uid != new_suid))
703 return -EPERM;
705 if (old_euid != uid)
707 current->mm->dumpable = 0;
708 wmb();
710 current->fsuid = current->euid = uid;
711 current->suid = new_suid;
713 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
718 * This function implements a generic ability to update ruid, euid,
719 * and suid. This allows you to implement the 4.4 compatible seteuid().
721 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
723 int old_ruid = current->uid;
724 int old_euid = current->euid;
725 int old_suid = current->suid;
726 int retval;
728 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
729 if (retval)
730 return retval;
732 if (!capable(CAP_SETUID)) {
733 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
734 (ruid != current->euid) && (ruid != current->suid))
735 return -EPERM;
736 if ((euid != (uid_t) -1) && (euid != current->uid) &&
737 (euid != current->euid) && (euid != current->suid))
738 return -EPERM;
739 if ((suid != (uid_t) -1) && (suid != current->uid) &&
740 (suid != current->euid) && (suid != current->suid))
741 return -EPERM;
743 if (ruid != (uid_t) -1) {
744 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
745 return -EAGAIN;
747 if (euid != (uid_t) -1) {
748 if (euid != current->euid)
750 current->mm->dumpable = 0;
751 wmb();
753 current->euid = euid;
755 current->fsuid = current->euid;
756 if (suid != (uid_t) -1)
757 current->suid = suid;
759 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
762 asmlinkage long sys_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
764 int retval;
766 if (!(retval = put_user(current->uid, ruid)) &&
767 !(retval = put_user(current->euid, euid)))
768 retval = put_user(current->suid, suid);
770 return retval;
774 * Same as above, but for rgid, egid, sgid.
776 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
778 int retval;
780 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
781 if (retval)
782 return retval;
784 if (!capable(CAP_SETGID)) {
785 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
786 (rgid != current->egid) && (rgid != current->sgid))
787 return -EPERM;
788 if ((egid != (gid_t) -1) && (egid != current->gid) &&
789 (egid != current->egid) && (egid != current->sgid))
790 return -EPERM;
791 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
792 (sgid != current->egid) && (sgid != current->sgid))
793 return -EPERM;
795 if (egid != (gid_t) -1) {
796 if (egid != current->egid)
798 current->mm->dumpable = 0;
799 wmb();
801 current->egid = egid;
803 current->fsgid = current->egid;
804 if (rgid != (gid_t) -1)
805 current->gid = rgid;
806 if (sgid != (gid_t) -1)
807 current->sgid = sgid;
808 return 0;
811 asmlinkage long sys_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
813 int retval;
815 if (!(retval = put_user(current->gid, rgid)) &&
816 !(retval = put_user(current->egid, egid)))
817 retval = put_user(current->sgid, sgid);
819 return retval;
824 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
825 * is used for "access()" and for the NFS daemon (letting nfsd stay at
826 * whatever uid it wants to). It normally shadows "euid", except when
827 * explicitly set by setfsuid() or for access..
829 asmlinkage long sys_setfsuid(uid_t uid)
831 int old_fsuid;
832 int retval;
834 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
835 if (retval)
836 return retval;
838 old_fsuid = current->fsuid;
839 if (uid == current->uid || uid == current->euid ||
840 uid == current->suid || uid == current->fsuid ||
841 capable(CAP_SETUID))
843 if (uid != old_fsuid)
845 current->mm->dumpable = 0;
846 wmb();
848 current->fsuid = uid;
851 retval = security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
852 if (retval)
853 return retval;
855 return old_fsuid;
859 * Samma på svenska..
861 asmlinkage long sys_setfsgid(gid_t gid)
863 int old_fsgid;
864 int retval;
866 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS);
867 if (retval)
868 return retval;
870 old_fsgid = current->fsgid;
871 if (gid == current->gid || gid == current->egid ||
872 gid == current->sgid || gid == current->fsgid ||
873 capable(CAP_SETGID))
875 if (gid != old_fsgid)
877 current->mm->dumpable = 0;
878 wmb();
880 current->fsgid = gid;
882 return old_fsgid;
885 asmlinkage long sys_times(struct tms __user * tbuf)
888 * In the SMP world we might just be unlucky and have one of
889 * the times increment as we use it. Since the value is an
890 * atomically safe type this is just fine. Conceptually its
891 * as if the syscall took an instant longer to occur.
893 if (tbuf) {
894 struct tms tmp;
895 tmp.tms_utime = jiffies_to_clock_t(current->utime);
896 tmp.tms_stime = jiffies_to_clock_t(current->stime);
897 tmp.tms_cutime = jiffies_to_clock_t(current->cutime);
898 tmp.tms_cstime = jiffies_to_clock_t(current->cstime);
899 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
900 return -EFAULT;
902 return (long) jiffies_64_to_clock_t(get_jiffies_64());
906 * This needs some heavy checking ...
907 * I just haven't the stomach for it. I also don't fully
908 * understand sessions/pgrp etc. Let somebody who does explain it.
910 * OK, I think I have the protection semantics right.... this is really
911 * only important on a multi-user system anyway, to make sure one user
912 * can't send a signal to a process owned by another. -TYT, 12/12/91
914 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
915 * LBT 04.03.94
918 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
920 struct task_struct *p;
921 int err = -EINVAL;
923 if (!pid)
924 pid = current->pid;
925 if (!pgid)
926 pgid = pid;
927 if (pgid < 0)
928 return -EINVAL;
930 /* From this point forward we keep holding onto the tasklist lock
931 * so that our parent does not change from under us. -DaveM
933 write_lock_irq(&tasklist_lock);
935 err = -ESRCH;
936 p = find_task_by_pid(pid);
937 if (!p)
938 goto out;
940 err = -EINVAL;
941 if (!thread_group_leader(p))
942 goto out;
944 if (p->parent == current || p->real_parent == current) {
945 err = -EPERM;
946 if (p->session != current->session)
947 goto out;
948 err = -EACCES;
949 if (p->did_exec)
950 goto out;
951 } else {
952 err = -ESRCH;
953 if (p != current)
954 goto out;
957 err = -EPERM;
958 if (p->leader)
959 goto out;
961 if (pgid != pid) {
962 struct task_struct *p;
963 struct pid *pid;
964 struct list_head *l;
966 for_each_task_pid(pgid, PIDTYPE_PGID, p, l, pid)
967 if (p->session == current->session)
968 goto ok_pgid;
969 goto out;
972 ok_pgid:
973 err = security_task_setpgid(p, pgid);
974 if (err)
975 goto out;
977 if (p->pgrp != pgid) {
978 detach_pid(p, PIDTYPE_PGID);
979 p->pgrp = pgid;
980 attach_pid(p, PIDTYPE_PGID, pgid);
982 err = 0;
983 out:
984 /* All paths lead to here, thus we are safe. -DaveM */
985 write_unlock_irq(&tasklist_lock);
986 return err;
989 asmlinkage long sys_getpgid(pid_t pid)
991 if (!pid) {
992 return current->pgrp;
993 } else {
994 int retval;
995 struct task_struct *p;
997 read_lock(&tasklist_lock);
998 p = find_task_by_pid(pid);
1000 retval = -ESRCH;
1001 if (p) {
1002 retval = security_task_getpgid(p);
1003 if (!retval)
1004 retval = p->pgrp;
1006 read_unlock(&tasklist_lock);
1007 return retval;
1011 asmlinkage long sys_getpgrp(void)
1013 /* SMP - assuming writes are word atomic this is fine */
1014 return current->pgrp;
1017 asmlinkage long sys_getsid(pid_t pid)
1019 if (!pid) {
1020 return current->session;
1021 } else {
1022 int retval;
1023 struct task_struct *p;
1025 read_lock(&tasklist_lock);
1026 p = find_task_by_pid(pid);
1028 retval = -ESRCH;
1029 if(p) {
1030 retval = security_task_getsid(p);
1031 if (!retval)
1032 retval = p->session;
1034 read_unlock(&tasklist_lock);
1035 return retval;
1039 asmlinkage long sys_setsid(void)
1041 struct pid *pid;
1042 int err = -EPERM;
1044 if (!thread_group_leader(current))
1045 return -EINVAL;
1047 write_lock_irq(&tasklist_lock);
1049 pid = find_pid(PIDTYPE_PGID, current->pid);
1050 if (pid)
1051 goto out;
1053 current->leader = 1;
1054 __set_special_pids(current->pid, current->pid);
1055 current->tty = NULL;
1056 current->tty_old_pgrp = 0;
1057 err = current->pgrp;
1058 out:
1059 write_unlock_irq(&tasklist_lock);
1060 return err;
1064 * Supplementary group IDs
1066 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1068 int i;
1071 * SMP: Nobody else can change our grouplist. Thus we are
1072 * safe.
1075 if (gidsetsize < 0)
1076 return -EINVAL;
1077 i = current->ngroups;
1078 if (gidsetsize) {
1079 if (i > gidsetsize)
1080 return -EINVAL;
1081 if (copy_to_user(grouplist, current->groups, sizeof(gid_t)*i))
1082 return -EFAULT;
1084 return i;
1088 * SMP: Our groups are not shared. We can copy to/from them safely
1089 * without another task interfering.
1092 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1094 gid_t groups[NGROUPS];
1095 int retval;
1097 if (!capable(CAP_SETGID))
1098 return -EPERM;
1099 if ((unsigned) gidsetsize > NGROUPS)
1100 return -EINVAL;
1101 if (copy_from_user(groups, grouplist, gidsetsize * sizeof(gid_t)))
1102 return -EFAULT;
1103 retval = security_task_setgroups(gidsetsize, groups);
1104 if (retval)
1105 return retval;
1106 memcpy(current->groups, groups, gidsetsize * sizeof(gid_t));
1107 current->ngroups = gidsetsize;
1108 return 0;
1111 static int supplemental_group_member(gid_t grp)
1113 int i = current->ngroups;
1115 if (i) {
1116 gid_t *groups = current->groups;
1117 do {
1118 if (*groups == grp)
1119 return 1;
1120 groups++;
1121 i--;
1122 } while (i);
1124 return 0;
1128 * Check whether we're fsgid/egid or in the supplemental group..
1130 int in_group_p(gid_t grp)
1132 int retval = 1;
1133 if (grp != current->fsgid)
1134 retval = supplemental_group_member(grp);
1135 return retval;
1138 int in_egroup_p(gid_t grp)
1140 int retval = 1;
1141 if (grp != current->egid)
1142 retval = supplemental_group_member(grp);
1143 return retval;
1146 DECLARE_RWSEM(uts_sem);
1148 asmlinkage long sys_newuname(struct new_utsname __user * name)
1150 int errno = 0;
1152 down_read(&uts_sem);
1153 if (copy_to_user(name,&system_utsname,sizeof *name))
1154 errno = -EFAULT;
1155 up_read(&uts_sem);
1156 return errno;
1159 asmlinkage long sys_sethostname(char __user *name, int len)
1161 int errno;
1163 if (!capable(CAP_SYS_ADMIN))
1164 return -EPERM;
1165 if (len < 0 || len > __NEW_UTS_LEN)
1166 return -EINVAL;
1167 down_write(&uts_sem);
1168 errno = -EFAULT;
1169 if (!copy_from_user(system_utsname.nodename, name, len)) {
1170 system_utsname.nodename[len] = 0;
1171 errno = 0;
1173 up_write(&uts_sem);
1174 return errno;
1177 asmlinkage long sys_gethostname(char __user *name, int len)
1179 int i, errno;
1181 if (len < 0)
1182 return -EINVAL;
1183 down_read(&uts_sem);
1184 i = 1 + strlen(system_utsname.nodename);
1185 if (i > len)
1186 i = len;
1187 errno = 0;
1188 if (copy_to_user(name, system_utsname.nodename, i))
1189 errno = -EFAULT;
1190 up_read(&uts_sem);
1191 return errno;
1195 * Only setdomainname; getdomainname can be implemented by calling
1196 * uname()
1198 asmlinkage long sys_setdomainname(char __user *name, int len)
1200 int errno;
1202 if (!capable(CAP_SYS_ADMIN))
1203 return -EPERM;
1204 if (len < 0 || len > __NEW_UTS_LEN)
1205 return -EINVAL;
1207 down_write(&uts_sem);
1208 errno = -EFAULT;
1209 if (!copy_from_user(system_utsname.domainname, name, len)) {
1210 errno = 0;
1211 system_utsname.domainname[len] = 0;
1213 up_write(&uts_sem);
1214 return errno;
1217 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1219 if (resource >= RLIM_NLIMITS)
1220 return -EINVAL;
1221 else
1222 return copy_to_user(rlim, current->rlim + resource, sizeof(*rlim))
1223 ? -EFAULT : 0;
1226 #if !defined(__ia64__) && !defined(CONFIG_V850)
1229 * Back compatibility for getrlimit. Needed for some apps.
1232 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1234 struct rlimit x;
1235 if (resource >= RLIM_NLIMITS)
1236 return -EINVAL;
1238 memcpy(&x, current->rlim + resource, sizeof(*rlim));
1239 if(x.rlim_cur > 0x7FFFFFFF)
1240 x.rlim_cur = 0x7FFFFFFF;
1241 if(x.rlim_max > 0x7FFFFFFF)
1242 x.rlim_max = 0x7FFFFFFF;
1243 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1246 #endif
1248 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1250 struct rlimit new_rlim, *old_rlim;
1251 int retval;
1253 if (resource >= RLIM_NLIMITS)
1254 return -EINVAL;
1255 if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1256 return -EFAULT;
1257 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1258 return -EINVAL;
1259 old_rlim = current->rlim + resource;
1260 if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
1261 (new_rlim.rlim_max > old_rlim->rlim_max)) &&
1262 !capable(CAP_SYS_RESOURCE))
1263 return -EPERM;
1264 if (resource == RLIMIT_NOFILE) {
1265 if (new_rlim.rlim_cur > NR_OPEN || new_rlim.rlim_max > NR_OPEN)
1266 return -EPERM;
1269 retval = security_task_setrlimit(resource, &new_rlim);
1270 if (retval)
1271 return retval;
1273 *old_rlim = new_rlim;
1274 return 0;
1278 * It would make sense to put struct rusage in the task_struct,
1279 * except that would make the task_struct be *really big*. After
1280 * task_struct gets moved into malloc'ed memory, it would
1281 * make sense to do this. It will make moving the rest of the information
1282 * a lot simpler! (Which we're not doing right now because we're not
1283 * measuring them yet).
1285 * This is SMP safe. Either we are called from sys_getrusage on ourselves
1286 * below (we know we aren't going to exit/disappear and only we change our
1287 * rusage counters), or we are called from wait4() on a process which is
1288 * either stopped or zombied. In the zombied case the task won't get
1289 * reaped till shortly after the call to getrusage(), in both cases the
1290 * task being examined is in a frozen state so the counters won't change.
1292 * FIXME! Get the fault counts properly!
1294 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1296 struct rusage r;
1298 memset((char *) &r, 0, sizeof(r));
1299 switch (who) {
1300 case RUSAGE_SELF:
1301 jiffies_to_timeval(p->utime, &r.ru_utime);
1302 jiffies_to_timeval(p->stime, &r.ru_stime);
1303 r.ru_minflt = p->min_flt;
1304 r.ru_majflt = p->maj_flt;
1305 r.ru_nswap = p->nswap;
1306 break;
1307 case RUSAGE_CHILDREN:
1308 jiffies_to_timeval(p->cutime, &r.ru_utime);
1309 jiffies_to_timeval(p->cstime, &r.ru_stime);
1310 r.ru_minflt = p->cmin_flt;
1311 r.ru_majflt = p->cmaj_flt;
1312 r.ru_nswap = p->cnswap;
1313 break;
1314 default:
1315 jiffies_to_timeval(p->utime + p->cutime, &r.ru_utime);
1316 jiffies_to_timeval(p->stime + p->cstime, &r.ru_stime);
1317 r.ru_minflt = p->min_flt + p->cmin_flt;
1318 r.ru_majflt = p->maj_flt + p->cmaj_flt;
1319 r.ru_nswap = p->nswap + p->cnswap;
1320 break;
1322 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1325 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1327 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1328 return -EINVAL;
1329 return getrusage(current, who, ru);
1332 asmlinkage long sys_umask(int mask)
1334 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1335 return mask;
1338 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1339 unsigned long arg4, unsigned long arg5)
1341 int error;
1342 int sig;
1344 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1345 if (error)
1346 return error;
1348 switch (option) {
1349 case PR_SET_PDEATHSIG:
1350 sig = arg2;
1351 if (sig < 0 || sig > _NSIG) {
1352 error = -EINVAL;
1353 break;
1355 current->pdeath_signal = sig;
1356 break;
1357 case PR_GET_PDEATHSIG:
1358 error = put_user(current->pdeath_signal, (int __user *)arg2);
1359 break;
1360 case PR_GET_DUMPABLE:
1361 if (current->mm->dumpable)
1362 error = 1;
1363 break;
1364 case PR_SET_DUMPABLE:
1365 if (arg2 != 0 && arg2 != 1) {
1366 error = -EINVAL;
1367 break;
1369 current->mm->dumpable = arg2;
1370 break;
1372 case PR_SET_UNALIGN:
1373 error = SET_UNALIGN_CTL(current, arg2);
1374 break;
1375 case PR_GET_UNALIGN:
1376 error = GET_UNALIGN_CTL(current, arg2);
1377 break;
1378 case PR_SET_FPEMU:
1379 error = SET_FPEMU_CTL(current, arg2);
1380 break;
1381 case PR_GET_FPEMU:
1382 error = GET_FPEMU_CTL(current, arg2);
1383 break;
1384 case PR_SET_FPEXC:
1385 error = SET_FPEXC_CTL(current, arg2);
1386 break;
1387 case PR_GET_FPEXC:
1388 error = GET_FPEXC_CTL(current, arg2);
1389 break;
1392 case PR_GET_KEEPCAPS:
1393 if (current->keep_capabilities)
1394 error = 1;
1395 break;
1396 case PR_SET_KEEPCAPS:
1397 if (arg2 != 0 && arg2 != 1) {
1398 error = -EINVAL;
1399 break;
1401 current->keep_capabilities = arg2;
1402 break;
1403 default:
1404 error = -EINVAL;
1405 break;
1407 return error;
1410 EXPORT_SYMBOL(notifier_chain_register);
1411 EXPORT_SYMBOL(notifier_chain_unregister);
1412 EXPORT_SYMBOL(notifier_call_chain);
1413 EXPORT_SYMBOL(register_reboot_notifier);
1414 EXPORT_SYMBOL(unregister_reboot_notifier);
1415 EXPORT_SYMBOL(in_group_p);
1416 EXPORT_SYMBOL(in_egroup_p);