[PATCH] make bio->bi_end_io() optional
[linux-2.6/history.git] / kernel / sys.c
blob3c2992ac68f2fa9f9649405e473de476dd6ffcb3
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>
25 #include <asm/uaccess.h>
26 #include <asm/io.h>
27 #include <asm/unistd.h>
29 #ifndef SET_UNALIGN_CTL
30 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
31 #endif
32 #ifndef GET_UNALIGN_CTL
33 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
34 #endif
35 #ifndef SET_FPEMU_CTL
36 # define SET_FPEMU_CTL(a,b) (-EINVAL)
37 #endif
38 #ifndef GET_FPEMU_CTL
39 # define GET_FPEMU_CTL(a,b) (-EINVAL)
40 #endif
41 #ifndef SET_FPEXC_CTL
42 # define SET_FPEXC_CTL(a,b) (-EINVAL)
43 #endif
44 #ifndef GET_FPEXC_CTL
45 # define GET_FPEXC_CTL(a,b) (-EINVAL)
46 #endif
49 * this is where the system-wide overflow UID and GID are defined, for
50 * architectures that now have 32-bit UID/GID but didn't in the past
53 int overflowuid = DEFAULT_OVERFLOWUID;
54 int overflowgid = DEFAULT_OVERFLOWGID;
57 * the same as above, but for filesystems which can only store a 16-bit
58 * UID and GID. as such, this is needed on all architectures
61 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
62 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
65 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
68 int C_A_D = 1;
69 int cad_pid = 1;
71 extern int system_running;
74 * Notifier list for kernel code which wants to be called
75 * at shutdown. This is used to stop any idling DMA operations
76 * and the like.
79 static struct notifier_block *reboot_notifier_list;
80 rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
82 /**
83 * notifier_chain_register - Add notifier to a notifier chain
84 * @list: Pointer to root list pointer
85 * @n: New entry in notifier chain
87 * Adds a notifier to a notifier chain.
89 * Currently always returns zero.
92 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
94 write_lock(&notifier_lock);
95 while(*list)
97 if(n->priority > (*list)->priority)
98 break;
99 list= &((*list)->next);
101 n->next = *list;
102 *list=n;
103 write_unlock(&notifier_lock);
104 return 0;
108 * notifier_chain_unregister - Remove notifier from a notifier chain
109 * @nl: Pointer to root list pointer
110 * @n: New entry in notifier chain
112 * Removes a notifier from a notifier chain.
114 * Returns zero on success, or %-ENOENT on failure.
117 int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
119 write_lock(&notifier_lock);
120 while((*nl)!=NULL)
122 if((*nl)==n)
124 *nl=n->next;
125 write_unlock(&notifier_lock);
126 return 0;
128 nl=&((*nl)->next);
130 write_unlock(&notifier_lock);
131 return -ENOENT;
135 * notifier_call_chain - Call functions in a notifier chain
136 * @n: Pointer to root pointer of notifier chain
137 * @val: Value passed unmodified to notifier function
138 * @v: Pointer passed unmodified to notifier function
140 * Calls each function in a notifier chain in turn.
142 * If the return value of the notifier can be and'd
143 * with %NOTIFY_STOP_MASK, then notifier_call_chain
144 * will return immediately, with the return value of
145 * the notifier function which halted execution.
146 * Otherwise, the return value is the return value
147 * of the last notifier function called.
150 int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
152 int ret=NOTIFY_DONE;
153 struct notifier_block *nb = *n;
155 while(nb)
157 ret=nb->notifier_call(nb,val,v);
158 if(ret&NOTIFY_STOP_MASK)
160 return ret;
162 nb=nb->next;
164 return ret;
168 * register_reboot_notifier - Register function to be called at reboot time
169 * @nb: Info about notifier function to be called
171 * Registers a function with the list of functions
172 * to be called at reboot time.
174 * Currently always returns zero, as notifier_chain_register
175 * always returns zero.
178 int register_reboot_notifier(struct notifier_block * nb)
180 return notifier_chain_register(&reboot_notifier_list, nb);
184 * unregister_reboot_notifier - Unregister previously registered reboot notifier
185 * @nb: Hook to be unregistered
187 * Unregisters a previously registered reboot
188 * notifier function.
190 * Returns zero on success, or %-ENOENT on failure.
193 int unregister_reboot_notifier(struct notifier_block * nb)
195 return notifier_chain_unregister(&reboot_notifier_list, nb);
198 asmlinkage long sys_ni_syscall(void)
200 return -ENOSYS;
203 cond_syscall(sys_nfsservctl)
204 cond_syscall(sys_quotactl)
205 cond_syscall(sys_acct)
206 cond_syscall(sys_lookup_dcookie)
208 static int set_one_prio(struct task_struct *p, int niceval, int error)
210 if (p->uid != current->euid &&
211 p->uid != current->uid && !capable(CAP_SYS_NICE)) {
212 error = -EPERM;
213 goto out;
216 if (error == -ESRCH)
217 error = 0;
218 if (niceval < task_nice(p) && !capable(CAP_SYS_NICE))
219 error = -EACCES;
220 else
221 set_user_nice(p, niceval);
222 out:
223 return error;
226 asmlinkage long sys_setpriority(int which, int who, int niceval)
228 struct task_struct *g, *p;
229 struct user_struct *user;
230 struct pid *pid;
231 struct list_head *l;
232 int error = -EINVAL;
234 if (which > 2 || which < 0)
235 goto out;
237 /* normalize: avoid signed division (rounding problems) */
238 error = -ESRCH;
239 if (niceval < -20)
240 niceval = -20;
241 if (niceval > 19)
242 niceval = 19;
244 read_lock(&tasklist_lock);
245 switch (which) {
246 case PRIO_PROCESS:
247 if (!who)
248 who = current->pid;
249 p = find_task_by_pid(who);
250 if (p)
251 error = set_one_prio(p, niceval, error);
252 break;
253 case PRIO_PGRP:
254 if (!who)
255 who = current->pgrp;
256 for_each_task_pid(who, PIDTYPE_PGID, p, l, pid)
257 error = set_one_prio(p, niceval, error);
258 break;
259 case PRIO_USER:
260 if (!who)
261 user = current->user;
262 else
263 user = find_user(who);
265 if (!user)
266 goto out_unlock;
268 do_each_thread(g, p)
269 if (p->uid == who)
270 error = set_one_prio(p, niceval, error);
271 while_each_thread(g, p);
272 break;
274 out_unlock:
275 read_unlock(&tasklist_lock);
276 out:
277 return error;
281 * Ugh. To avoid negative return values, "getpriority()" will
282 * not return the normal nice-value, but a negated value that
283 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
284 * to stay compatible.
286 asmlinkage long sys_getpriority(int which, int who)
288 struct task_struct *g, *p;
289 struct list_head *l;
290 struct pid *pid;
291 struct user_struct *user;
292 long niceval, retval = -ESRCH;
294 if (which > 2 || which < 0)
295 return -EINVAL;
297 read_lock(&tasklist_lock);
298 switch (which) {
299 case PRIO_PROCESS:
300 if (!who)
301 who = current->pid;
302 p = find_task_by_pid(who);
303 if (p) {
304 niceval = 20 - task_nice(p);
305 if (niceval > retval)
306 retval = niceval;
308 break;
309 case PRIO_PGRP:
310 if (!who)
311 who = current->pgrp;
312 for_each_task_pid(who, PIDTYPE_PGID, p, l, pid) {
313 niceval = 20 - task_nice(p);
314 if (niceval > retval)
315 retval = niceval;
317 break;
318 case PRIO_USER:
319 if (!who)
320 user = current->user;
321 else
322 user = find_user(who);
324 if (!user)
325 goto out_unlock;
327 do_each_thread(g, p)
328 if (p->uid == who) {
329 niceval = 20 - task_nice(p);
330 if (niceval > retval)
331 retval = niceval;
333 while_each_thread(g, p);
334 break;
336 out_unlock:
337 read_unlock(&tasklist_lock);
339 return retval;
344 * Reboot system call: for obvious reasons only root may call it,
345 * and even root needs to set up some magic numbers in the registers
346 * so that some mistake won't make this reboot the whole machine.
347 * You can also set the meaning of the ctrl-alt-del-key here.
349 * reboot doesn't sync: do that yourself before calling this.
351 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void * arg)
353 char buffer[256];
355 /* We only trust the superuser with rebooting the system. */
356 if (!capable(CAP_SYS_BOOT))
357 return -EPERM;
359 /* For safety, we require "magic" arguments. */
360 if (magic1 != LINUX_REBOOT_MAGIC1 ||
361 (magic2 != LINUX_REBOOT_MAGIC2 && magic2 != LINUX_REBOOT_MAGIC2A &&
362 magic2 != LINUX_REBOOT_MAGIC2B))
363 return -EINVAL;
365 lock_kernel();
366 switch (cmd) {
367 case LINUX_REBOOT_CMD_RESTART:
368 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
369 system_running = 0;
370 device_shutdown();
371 printk(KERN_EMERG "Restarting system.\n");
372 machine_restart(NULL);
373 break;
375 case LINUX_REBOOT_CMD_CAD_ON:
376 C_A_D = 1;
377 break;
379 case LINUX_REBOOT_CMD_CAD_OFF:
380 C_A_D = 0;
381 break;
383 case LINUX_REBOOT_CMD_HALT:
384 notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
385 system_running = 0;
386 device_shutdown();
387 printk(KERN_EMERG "System halted.\n");
388 machine_halt();
389 do_exit(0);
390 break;
392 case LINUX_REBOOT_CMD_POWER_OFF:
393 notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
394 system_running = 0;
395 device_shutdown();
396 printk(KERN_EMERG "Power down.\n");
397 machine_power_off();
398 do_exit(0);
399 break;
401 case LINUX_REBOOT_CMD_RESTART2:
402 if (strncpy_from_user(&buffer[0], (char *)arg, sizeof(buffer) - 1) < 0) {
403 unlock_kernel();
404 return -EFAULT;
406 buffer[sizeof(buffer) - 1] = '\0';
408 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, buffer);
409 system_running = 0;
410 device_shutdown();
411 printk(KERN_EMERG "Restarting system with command '%s'.\n", buffer);
412 machine_restart(buffer);
413 break;
415 #ifdef CONFIG_SOFTWARE_SUSPEND
416 case LINUX_REBOOT_CMD_SW_SUSPEND:
417 if (!software_suspend_enabled) {
418 unlock_kernel();
419 return -EAGAIN;
421 software_suspend();
422 do_exit(0);
423 break;
424 #endif
426 default:
427 unlock_kernel();
428 return -EINVAL;
430 unlock_kernel();
431 return 0;
434 static void deferred_cad(void *dummy)
436 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
437 machine_restart(NULL);
441 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
442 * As it's called within an interrupt, it may NOT sync: the only choice
443 * is whether to reboot at once, or just ignore the ctrl-alt-del.
445 void ctrl_alt_del(void)
447 static DECLARE_WORK(cad_work, deferred_cad, NULL);
449 if (C_A_D)
450 schedule_work(&cad_work);
451 else
452 kill_proc(cad_pid, SIGINT, 1);
457 * Unprivileged users may change the real gid to the effective gid
458 * or vice versa. (BSD-style)
460 * If you set the real gid at all, or set the effective gid to a value not
461 * equal to the real gid, then the saved gid is set to the new effective gid.
463 * This makes it possible for a setgid program to completely drop its
464 * privileges, which is often a useful assertion to make when you are doing
465 * a security audit over a program.
467 * The general idea is that a program which uses just setregid() will be
468 * 100% compatible with BSD. A program which uses just setgid() will be
469 * 100% compatible with POSIX with saved IDs.
471 * SMP: There are not races, the GIDs are checked only by filesystem
472 * operations (as far as semantic preservation is concerned).
474 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
476 int old_rgid = current->gid;
477 int old_egid = current->egid;
478 int new_rgid = old_rgid;
479 int new_egid = old_egid;
480 int retval;
482 retval = security_ops->task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
483 if (retval)
484 return retval;
486 if (rgid != (gid_t) -1) {
487 if ((old_rgid == rgid) ||
488 (current->egid==rgid) ||
489 capable(CAP_SETGID))
490 new_rgid = rgid;
491 else
492 return -EPERM;
494 if (egid != (gid_t) -1) {
495 if ((old_rgid == egid) ||
496 (current->egid == egid) ||
497 (current->sgid == egid) ||
498 capable(CAP_SETGID))
499 new_egid = egid;
500 else {
501 return -EPERM;
504 if (new_egid != old_egid)
506 current->mm->dumpable = 0;
507 wmb();
509 if (rgid != (gid_t) -1 ||
510 (egid != (gid_t) -1 && egid != old_rgid))
511 current->sgid = new_egid;
512 current->fsgid = new_egid;
513 current->egid = new_egid;
514 current->gid = new_rgid;
515 return 0;
519 * setgid() is implemented like SysV w/ SAVED_IDS
521 * SMP: Same implicit races as above.
523 asmlinkage long sys_setgid(gid_t gid)
525 int old_egid = current->egid;
526 int retval;
528 retval = security_ops->task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
529 if (retval)
530 return retval;
532 if (capable(CAP_SETGID))
534 if(old_egid != gid)
536 current->mm->dumpable=0;
537 wmb();
539 current->gid = current->egid = current->sgid = current->fsgid = gid;
541 else if ((gid == current->gid) || (gid == current->sgid))
543 if(old_egid != gid)
545 current->mm->dumpable=0;
546 wmb();
548 current->egid = current->fsgid = gid;
550 else
551 return -EPERM;
552 return 0;
555 static int set_user(uid_t new_ruid, int dumpclear)
557 struct user_struct *new_user, *old_user;
559 /* What if a process setreuid()'s and this brings the
560 * new uid over his NPROC rlimit? We can check this now
561 * cheaply with the new uid cache, so if it matters
562 * we should be checking for it. -DaveM
564 new_user = alloc_uid(new_ruid);
565 if (!new_user)
566 return -EAGAIN;
567 old_user = current->user;
568 atomic_dec(&old_user->processes);
569 atomic_inc(&new_user->processes);
571 if(dumpclear)
573 current->mm->dumpable = 0;
574 wmb();
576 current->uid = new_ruid;
577 current->user = new_user;
578 free_uid(old_user);
579 return 0;
583 * Unprivileged users may change the real uid to the effective uid
584 * or vice versa. (BSD-style)
586 * If you set the real uid at all, or set the effective uid to a value not
587 * equal to the real uid, then the saved uid is set to the new effective uid.
589 * This makes it possible for a setuid program to completely drop its
590 * privileges, which is often a useful assertion to make when you are doing
591 * a security audit over a program.
593 * The general idea is that a program which uses just setreuid() will be
594 * 100% compatible with BSD. A program which uses just setuid() will be
595 * 100% compatible with POSIX with saved IDs.
597 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
599 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
600 int retval;
602 retval = security_ops->task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
603 if (retval)
604 return retval;
606 new_ruid = old_ruid = current->uid;
607 new_euid = old_euid = current->euid;
608 old_suid = current->suid;
610 if (ruid != (uid_t) -1) {
611 new_ruid = ruid;
612 if ((old_ruid != ruid) &&
613 (current->euid != ruid) &&
614 !capable(CAP_SETUID))
615 return -EPERM;
618 if (euid != (uid_t) -1) {
619 new_euid = euid;
620 if ((old_ruid != euid) &&
621 (current->euid != euid) &&
622 (current->suid != euid) &&
623 !capable(CAP_SETUID))
624 return -EPERM;
627 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
628 return -EAGAIN;
630 if (new_euid != old_euid)
632 current->mm->dumpable=0;
633 wmb();
635 current->fsuid = current->euid = new_euid;
636 if (ruid != (uid_t) -1 ||
637 (euid != (uid_t) -1 && euid != old_ruid))
638 current->suid = current->euid;
639 current->fsuid = current->euid;
641 return security_ops->task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
647 * setuid() is implemented like SysV with SAVED_IDS
649 * Note that SAVED_ID's is deficient in that a setuid root program
650 * like sendmail, for example, cannot set its uid to be a normal
651 * user and then switch back, because if you're root, setuid() sets
652 * the saved uid too. If you don't like this, blame the bright people
653 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
654 * will allow a root program to temporarily drop privileges and be able to
655 * regain them by swapping the real and effective uid.
657 asmlinkage long sys_setuid(uid_t uid)
659 int old_euid = current->euid;
660 int old_ruid, old_suid, new_ruid, new_suid;
661 int retval;
663 retval = security_ops->task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
664 if (retval)
665 return retval;
667 old_ruid = new_ruid = current->uid;
668 old_suid = current->suid;
669 new_suid = old_suid;
671 if (capable(CAP_SETUID)) {
672 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
673 return -EAGAIN;
674 new_suid = uid;
675 } else if ((uid != current->uid) && (uid != new_suid))
676 return -EPERM;
678 if (old_euid != uid)
680 current->mm->dumpable = 0;
681 wmb();
683 current->fsuid = current->euid = uid;
684 current->suid = new_suid;
686 return security_ops->task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
691 * This function implements a generic ability to update ruid, euid,
692 * and suid. This allows you to implement the 4.4 compatible seteuid().
694 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
696 int old_ruid = current->uid;
697 int old_euid = current->euid;
698 int old_suid = current->suid;
699 int retval;
701 retval = security_ops->task_setuid(ruid, euid, suid, LSM_SETID_RES);
702 if (retval)
703 return retval;
705 if (!capable(CAP_SETUID)) {
706 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
707 (ruid != current->euid) && (ruid != current->suid))
708 return -EPERM;
709 if ((euid != (uid_t) -1) && (euid != current->uid) &&
710 (euid != current->euid) && (euid != current->suid))
711 return -EPERM;
712 if ((suid != (uid_t) -1) && (suid != current->uid) &&
713 (suid != current->euid) && (suid != current->suid))
714 return -EPERM;
716 if (ruid != (uid_t) -1) {
717 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
718 return -EAGAIN;
720 if (euid != (uid_t) -1) {
721 if (euid != current->euid)
723 current->mm->dumpable = 0;
724 wmb();
726 current->euid = euid;
728 current->fsuid = current->euid;
729 if (suid != (uid_t) -1)
730 current->suid = suid;
732 return security_ops->task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
735 asmlinkage long sys_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
737 int retval;
739 if (!(retval = put_user(current->uid, ruid)) &&
740 !(retval = put_user(current->euid, euid)))
741 retval = put_user(current->suid, suid);
743 return retval;
747 * Same as above, but for rgid, egid, sgid.
749 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
751 int retval;
753 retval = security_ops->task_setgid(rgid, egid, sgid, LSM_SETID_RES);
754 if (retval)
755 return retval;
757 if (!capable(CAP_SETGID)) {
758 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
759 (rgid != current->egid) && (rgid != current->sgid))
760 return -EPERM;
761 if ((egid != (gid_t) -1) && (egid != current->gid) &&
762 (egid != current->egid) && (egid != current->sgid))
763 return -EPERM;
764 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
765 (sgid != current->egid) && (sgid != current->sgid))
766 return -EPERM;
768 if (egid != (gid_t) -1) {
769 if (egid != current->egid)
771 current->mm->dumpable = 0;
772 wmb();
774 current->egid = egid;
776 current->fsgid = current->egid;
777 if (rgid != (gid_t) -1)
778 current->gid = rgid;
779 if (sgid != (gid_t) -1)
780 current->sgid = sgid;
781 return 0;
784 asmlinkage long sys_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
786 int retval;
788 if (!(retval = put_user(current->gid, rgid)) &&
789 !(retval = put_user(current->egid, egid)))
790 retval = put_user(current->sgid, sgid);
792 return retval;
797 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
798 * is used for "access()" and for the NFS daemon (letting nfsd stay at
799 * whatever uid it wants to). It normally shadows "euid", except when
800 * explicitly set by setfsuid() or for access..
802 asmlinkage long sys_setfsuid(uid_t uid)
804 int old_fsuid;
805 int retval;
807 retval = security_ops->task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
808 if (retval)
809 return retval;
811 old_fsuid = current->fsuid;
812 if (uid == current->uid || uid == current->euid ||
813 uid == current->suid || uid == current->fsuid ||
814 capable(CAP_SETUID))
816 if (uid != old_fsuid)
818 current->mm->dumpable = 0;
819 wmb();
821 current->fsuid = uid;
824 retval = security_ops->task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
825 if (retval)
826 return retval;
828 return old_fsuid;
832 * Samma på svenska..
834 asmlinkage long sys_setfsgid(gid_t gid)
836 int old_fsgid;
837 int retval;
839 retval = security_ops->task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS);
840 if (retval)
841 return retval;
843 old_fsgid = current->fsgid;
844 if (gid == current->gid || gid == current->egid ||
845 gid == current->sgid || gid == current->fsgid ||
846 capable(CAP_SETGID))
848 if (gid != old_fsgid)
850 current->mm->dumpable = 0;
851 wmb();
853 current->fsgid = gid;
855 return old_fsgid;
858 asmlinkage long sys_times(struct tms * tbuf)
861 * In the SMP world we might just be unlucky and have one of
862 * the times increment as we use it. Since the value is an
863 * atomically safe type this is just fine. Conceptually its
864 * as if the syscall took an instant longer to occur.
866 if (tbuf) {
867 struct tms tmp;
868 tmp.tms_utime = jiffies_to_clock_t(current->utime);
869 tmp.tms_stime = jiffies_to_clock_t(current->stime);
870 tmp.tms_cutime = jiffies_to_clock_t(current->cutime);
871 tmp.tms_cstime = jiffies_to_clock_t(current->cstime);
872 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
873 return -EFAULT;
875 return jiffies_to_clock_t(jiffies);
879 * This needs some heavy checking ...
880 * I just haven't the stomach for it. I also don't fully
881 * understand sessions/pgrp etc. Let somebody who does explain it.
883 * OK, I think I have the protection semantics right.... this is really
884 * only important on a multi-user system anyway, to make sure one user
885 * can't send a signal to a process owned by another. -TYT, 12/12/91
887 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
888 * LBT 04.03.94
891 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
893 struct task_struct *p;
894 int err = -EINVAL;
896 if (!pid)
897 pid = current->pid;
898 if (!pgid)
899 pgid = pid;
900 if (pgid < 0)
901 return -EINVAL;
903 /* From this point forward we keep holding onto the tasklist lock
904 * so that our parent does not change from under us. -DaveM
906 write_lock_irq(&tasklist_lock);
908 err = -ESRCH;
909 p = find_task_by_pid(pid);
910 if (!p)
911 goto out;
912 err = -EINVAL;
913 if (!thread_group_leader(p))
914 goto out;
916 if (p->parent == current || p->real_parent == current) {
917 err = -EPERM;
918 if (p->session != current->session)
919 goto out;
920 err = -EACCES;
921 if (p->did_exec)
922 goto out;
923 } else if (p != current)
924 goto out;
925 err = -EPERM;
926 if (p->leader)
927 goto out;
928 if (pgid != pid) {
929 struct task_struct *p;
930 struct pid *pid;
931 struct list_head *l;
933 for_each_task_pid(pgid, PIDTYPE_PGID, p, l, pid)
934 if (p->session == current->session)
935 goto ok_pgid;
936 goto out;
939 ok_pgid:
940 if (p->pgrp != pgid) {
941 detach_pid(p, PIDTYPE_PGID);
942 p->pgrp = pgid;
943 attach_pid(p, PIDTYPE_PGID, pgid);
945 err = 0;
946 out:
947 /* All paths lead to here, thus we are safe. -DaveM */
948 write_unlock_irq(&tasklist_lock);
949 return err;
952 asmlinkage long sys_getpgid(pid_t pid)
954 if (!pid) {
955 return current->pgrp;
956 } else {
957 int retval;
958 struct task_struct *p;
960 read_lock(&tasklist_lock);
961 p = find_task_by_pid(pid);
963 retval = -ESRCH;
964 if (p) {
965 retval = security_ops->task_getpgid(p);
966 if (!retval)
967 retval = p->pgrp;
969 read_unlock(&tasklist_lock);
970 return retval;
974 asmlinkage long sys_getpgrp(void)
976 /* SMP - assuming writes are word atomic this is fine */
977 return current->pgrp;
980 asmlinkage long sys_getsid(pid_t pid)
982 if (!pid) {
983 return current->session;
984 } else {
985 int retval;
986 struct task_struct *p;
988 read_lock(&tasklist_lock);
989 p = find_task_by_pid(pid);
991 retval = -ESRCH;
992 if(p) {
993 retval = security_ops->task_getsid(p);
994 if (!retval)
995 retval = p->session;
997 read_unlock(&tasklist_lock);
998 return retval;
1002 asmlinkage long sys_setsid(void)
1004 struct pid *pid;
1005 int err = -EPERM;
1007 if (!thread_group_leader(current))
1008 return -EINVAL;
1010 write_lock_irq(&tasklist_lock);
1012 pid = find_pid(PIDTYPE_PGID, current->pid);
1013 if (pid)
1014 goto out;
1016 current->leader = 1;
1017 if (current->session != current->pid) {
1018 detach_pid(current, PIDTYPE_SID);
1019 current->session = current->pid;
1020 attach_pid(current, PIDTYPE_SID, current->pid);
1022 if (current->pgrp != current->pid) {
1023 detach_pid(current, PIDTYPE_PGID);
1024 current->pgrp = current->pid;
1025 attach_pid(current, PIDTYPE_PGID, current->pid);
1027 current->tty = NULL;
1028 current->tty_old_pgrp = 0;
1029 err = current->pgrp;
1030 out:
1031 write_unlock_irq(&tasklist_lock);
1032 return err;
1036 * Supplementary group IDs
1038 asmlinkage long sys_getgroups(int gidsetsize, gid_t *grouplist)
1040 int i;
1043 * SMP: Nobody else can change our grouplist. Thus we are
1044 * safe.
1047 if (gidsetsize < 0)
1048 return -EINVAL;
1049 i = current->ngroups;
1050 if (gidsetsize) {
1051 if (i > gidsetsize)
1052 return -EINVAL;
1053 if (copy_to_user(grouplist, current->groups, sizeof(gid_t)*i))
1054 return -EFAULT;
1056 return i;
1060 * SMP: Our groups are not shared. We can copy to/from them safely
1061 * without another task interfering.
1064 asmlinkage long sys_setgroups(int gidsetsize, gid_t *grouplist)
1066 gid_t groups[NGROUPS];
1067 int retval;
1069 if (!capable(CAP_SETGID))
1070 return -EPERM;
1071 if ((unsigned) gidsetsize > NGROUPS)
1072 return -EINVAL;
1073 if(copy_from_user(groups, grouplist, gidsetsize * sizeof(gid_t)))
1074 return -EFAULT;
1075 retval = security_ops->task_setgroups(gidsetsize, groups);
1076 if (retval)
1077 return retval;
1078 memcpy(current->groups, groups, gidsetsize * sizeof(gid_t));
1079 current->ngroups = gidsetsize;
1080 return 0;
1083 static int supplemental_group_member(gid_t grp)
1085 int i = current->ngroups;
1087 if (i) {
1088 gid_t *groups = current->groups;
1089 do {
1090 if (*groups == grp)
1091 return 1;
1092 groups++;
1093 i--;
1094 } while (i);
1096 return 0;
1100 * Check whether we're fsgid/egid or in the supplemental group..
1102 int in_group_p(gid_t grp)
1104 int retval = 1;
1105 if (grp != current->fsgid)
1106 retval = supplemental_group_member(grp);
1107 return retval;
1110 int in_egroup_p(gid_t grp)
1112 int retval = 1;
1113 if (grp != current->egid)
1114 retval = supplemental_group_member(grp);
1115 return retval;
1118 DECLARE_RWSEM(uts_sem);
1120 asmlinkage long sys_newuname(struct new_utsname * name)
1122 int errno = 0;
1124 down_read(&uts_sem);
1125 if (copy_to_user(name,&system_utsname,sizeof *name))
1126 errno = -EFAULT;
1127 up_read(&uts_sem);
1128 return errno;
1131 asmlinkage long sys_sethostname(char *name, int len)
1133 int errno;
1135 if (!capable(CAP_SYS_ADMIN))
1136 return -EPERM;
1137 if (len < 0 || len > __NEW_UTS_LEN)
1138 return -EINVAL;
1139 down_write(&uts_sem);
1140 errno = -EFAULT;
1141 if (!copy_from_user(system_utsname.nodename, name, len)) {
1142 system_utsname.nodename[len] = 0;
1143 errno = 0;
1145 up_write(&uts_sem);
1146 return errno;
1149 asmlinkage long sys_gethostname(char *name, int len)
1151 int i, errno;
1153 if (len < 0)
1154 return -EINVAL;
1155 down_read(&uts_sem);
1156 i = 1 + strlen(system_utsname.nodename);
1157 if (i > len)
1158 i = len;
1159 errno = 0;
1160 if (copy_to_user(name, system_utsname.nodename, i))
1161 errno = -EFAULT;
1162 up_read(&uts_sem);
1163 return errno;
1167 * Only setdomainname; getdomainname can be implemented by calling
1168 * uname()
1170 asmlinkage long sys_setdomainname(char *name, int len)
1172 int errno;
1174 if (!capable(CAP_SYS_ADMIN))
1175 return -EPERM;
1176 if (len < 0 || len > __NEW_UTS_LEN)
1177 return -EINVAL;
1179 down_write(&uts_sem);
1180 errno = -EFAULT;
1181 if (!copy_from_user(system_utsname.domainname, name, len)) {
1182 errno = 0;
1183 system_utsname.domainname[len] = 0;
1185 up_write(&uts_sem);
1186 return errno;
1189 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit *rlim)
1191 if (resource >= RLIM_NLIMITS)
1192 return -EINVAL;
1193 else
1194 return copy_to_user(rlim, current->rlim + resource, sizeof(*rlim))
1195 ? -EFAULT : 0;
1198 #if !defined(__ia64__)
1201 * Back compatibility for getrlimit. Needed for some apps.
1204 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit *rlim)
1206 struct rlimit x;
1207 if (resource >= RLIM_NLIMITS)
1208 return -EINVAL;
1210 memcpy(&x, current->rlim + resource, sizeof(*rlim));
1211 if(x.rlim_cur > 0x7FFFFFFF)
1212 x.rlim_cur = 0x7FFFFFFF;
1213 if(x.rlim_max > 0x7FFFFFFF)
1214 x.rlim_max = 0x7FFFFFFF;
1215 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1218 #endif
1220 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit *rlim)
1222 struct rlimit new_rlim, *old_rlim;
1223 int retval;
1225 if (resource >= RLIM_NLIMITS)
1226 return -EINVAL;
1227 if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1228 return -EFAULT;
1229 old_rlim = current->rlim + resource;
1230 if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
1231 (new_rlim.rlim_max > old_rlim->rlim_max)) &&
1232 !capable(CAP_SYS_RESOURCE))
1233 return -EPERM;
1234 if (resource == RLIMIT_NOFILE) {
1235 if (new_rlim.rlim_cur > NR_OPEN || new_rlim.rlim_max > NR_OPEN)
1236 return -EPERM;
1239 retval = security_ops->task_setrlimit(resource, &new_rlim);
1240 if (retval)
1241 return retval;
1243 *old_rlim = new_rlim;
1244 return 0;
1248 * It would make sense to put struct rusage in the task_struct,
1249 * except that would make the task_struct be *really big*. After
1250 * task_struct gets moved into malloc'ed memory, it would
1251 * make sense to do this. It will make moving the rest of the information
1252 * a lot simpler! (Which we're not doing right now because we're not
1253 * measuring them yet).
1255 * This is SMP safe. Either we are called from sys_getrusage on ourselves
1256 * below (we know we aren't going to exit/disappear and only we change our
1257 * rusage counters), or we are called from wait4() on a process which is
1258 * either stopped or zombied. In the zombied case the task won't get
1259 * reaped till shortly after the call to getrusage(), in both cases the
1260 * task being examined is in a frozen state so the counters won't change.
1262 * FIXME! Get the fault counts properly!
1264 int getrusage(struct task_struct *p, int who, struct rusage *ru)
1266 struct rusage r;
1268 memset((char *) &r, 0, sizeof(r));
1269 switch (who) {
1270 case RUSAGE_SELF:
1271 jiffies_to_timeval(p->utime, &r.ru_utime);
1272 jiffies_to_timeval(p->stime, &r.ru_stime);
1273 r.ru_minflt = p->min_flt;
1274 r.ru_majflt = p->maj_flt;
1275 r.ru_nswap = p->nswap;
1276 break;
1277 case RUSAGE_CHILDREN:
1278 jiffies_to_timeval(p->cutime, &r.ru_utime);
1279 jiffies_to_timeval(p->cstime, &r.ru_stime);
1280 r.ru_minflt = p->cmin_flt;
1281 r.ru_majflt = p->cmaj_flt;
1282 r.ru_nswap = p->cnswap;
1283 break;
1284 default:
1285 jiffies_to_timeval(p->utime + p->cutime, &r.ru_utime);
1286 jiffies_to_timeval(p->stime + p->cstime, &r.ru_stime);
1287 r.ru_minflt = p->min_flt + p->cmin_flt;
1288 r.ru_majflt = p->maj_flt + p->cmaj_flt;
1289 r.ru_nswap = p->nswap + p->cnswap;
1290 break;
1292 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1295 asmlinkage long sys_getrusage(int who, struct rusage *ru)
1297 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1298 return -EINVAL;
1299 return getrusage(current, who, ru);
1302 asmlinkage long sys_umask(int mask)
1304 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1305 return mask;
1308 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1309 unsigned long arg4, unsigned long arg5)
1311 int error = 0;
1312 int sig;
1314 error = security_ops->task_prctl(option, arg2, arg3, arg4, arg5);
1315 if (error)
1316 return error;
1318 switch (option) {
1319 case PR_SET_PDEATHSIG:
1320 sig = arg2;
1321 if (sig < 0 || sig > _NSIG) {
1322 error = -EINVAL;
1323 break;
1325 current->pdeath_signal = sig;
1326 break;
1327 case PR_GET_PDEATHSIG:
1328 error = put_user(current->pdeath_signal, (int *)arg2);
1329 break;
1330 case PR_GET_DUMPABLE:
1331 if (current->mm->dumpable)
1332 error = 1;
1333 break;
1334 case PR_SET_DUMPABLE:
1335 if (arg2 != 0 && arg2 != 1) {
1336 error = -EINVAL;
1337 break;
1339 current->mm->dumpable = arg2;
1340 break;
1342 case PR_SET_UNALIGN:
1343 error = SET_UNALIGN_CTL(current, arg2);
1344 break;
1345 case PR_GET_UNALIGN:
1346 error = GET_UNALIGN_CTL(current, arg2);
1347 break;
1348 case PR_SET_FPEMU:
1349 error = SET_FPEMU_CTL(current, arg2);
1350 break;
1351 case PR_GET_FPEMU:
1352 error = GET_FPEMU_CTL(current, arg2);
1353 break;
1354 case PR_SET_FPEXC:
1355 error = SET_FPEXC_CTL(current, arg2);
1356 break;
1357 case PR_GET_FPEXC:
1358 error = GET_FPEXC_CTL(current, arg2);
1359 break;
1362 case PR_GET_KEEPCAPS:
1363 if (current->keep_capabilities)
1364 error = 1;
1365 break;
1366 case PR_SET_KEEPCAPS:
1367 if (arg2 != 0 && arg2 != 1) {
1368 error = -EINVAL;
1369 break;
1371 current->keep_capabilities = arg2;
1372 break;
1373 default:
1374 error = -EINVAL;
1375 break;
1377 return error;
1380 EXPORT_SYMBOL(notifier_chain_register);
1381 EXPORT_SYMBOL(notifier_chain_unregister);
1382 EXPORT_SYMBOL(notifier_call_chain);
1383 EXPORT_SYMBOL(register_reboot_notifier);
1384 EXPORT_SYMBOL(unregister_reboot_notifier);
1385 EXPORT_SYMBOL(in_group_p);
1386 EXPORT_SYMBOL(in_egroup_p);