AFS: write support fixes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / sys.c
blobcdb7e9457ba6596336b2618282f418bf13626e30
1 /*
2 * linux/kernel/sys.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/module.h>
8 #include <linux/mm.h>
9 #include <linux/utsname.h>
10 #include <linux/mman.h>
11 #include <linux/smp_lock.h>
12 #include <linux/notifier.h>
13 #include <linux/reboot.h>
14 #include <linux/prctl.h>
15 #include <linux/highuid.h>
16 #include <linux/fs.h>
17 #include <linux/kernel.h>
18 #include <linux/kexec.h>
19 #include <linux/workqueue.h>
20 #include <linux/capability.h>
21 #include <linux/device.h>
22 #include <linux/key.h>
23 #include <linux/times.h>
24 #include <linux/posix-timers.h>
25 #include <linux/security.h>
26 #include <linux/dcookies.h>
27 #include <linux/suspend.h>
28 #include <linux/tty.h>
29 #include <linux/signal.h>
30 #include <linux/cn_proc.h>
31 #include <linux/getcpu.h>
33 #include <linux/compat.h>
34 #include <linux/syscalls.h>
35 #include <linux/kprobes.h>
37 #include <asm/uaccess.h>
38 #include <asm/io.h>
39 #include <asm/unistd.h>
41 #ifndef SET_UNALIGN_CTL
42 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
43 #endif
44 #ifndef GET_UNALIGN_CTL
45 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
46 #endif
47 #ifndef SET_FPEMU_CTL
48 # define SET_FPEMU_CTL(a,b) (-EINVAL)
49 #endif
50 #ifndef GET_FPEMU_CTL
51 # define GET_FPEMU_CTL(a,b) (-EINVAL)
52 #endif
53 #ifndef SET_FPEXC_CTL
54 # define SET_FPEXC_CTL(a,b) (-EINVAL)
55 #endif
56 #ifndef GET_FPEXC_CTL
57 # define GET_FPEXC_CTL(a,b) (-EINVAL)
58 #endif
59 #ifndef GET_ENDIAN
60 # define GET_ENDIAN(a,b) (-EINVAL)
61 #endif
62 #ifndef SET_ENDIAN
63 # define SET_ENDIAN(a,b) (-EINVAL)
64 #endif
67 * this is where the system-wide overflow UID and GID are defined, for
68 * architectures that now have 32-bit UID/GID but didn't in the past
71 int overflowuid = DEFAULT_OVERFLOWUID;
72 int overflowgid = DEFAULT_OVERFLOWGID;
74 #ifdef CONFIG_UID16
75 EXPORT_SYMBOL(overflowuid);
76 EXPORT_SYMBOL(overflowgid);
77 #endif
80 * the same as above, but for filesystems which can only store a 16-bit
81 * UID and GID. as such, this is needed on all architectures
84 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
85 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
87 EXPORT_SYMBOL(fs_overflowuid);
88 EXPORT_SYMBOL(fs_overflowgid);
91 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
94 int C_A_D = 1;
95 struct pid *cad_pid;
96 EXPORT_SYMBOL(cad_pid);
99 * Notifier list for kernel code which wants to be called
100 * at shutdown. This is used to stop any idling DMA operations
101 * and the like.
104 static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
107 * Notifier chain core routines. The exported routines below
108 * are layered on top of these, with appropriate locking added.
111 static int notifier_chain_register(struct notifier_block **nl,
112 struct notifier_block *n)
114 while ((*nl) != NULL) {
115 if (n->priority > (*nl)->priority)
116 break;
117 nl = &((*nl)->next);
119 n->next = *nl;
120 rcu_assign_pointer(*nl, n);
121 return 0;
124 static int notifier_chain_unregister(struct notifier_block **nl,
125 struct notifier_block *n)
127 while ((*nl) != NULL) {
128 if ((*nl) == n) {
129 rcu_assign_pointer(*nl, n->next);
130 return 0;
132 nl = &((*nl)->next);
134 return -ENOENT;
138 * notifier_call_chain - Informs the registered notifiers about an event.
139 * @nl: Pointer to head of the blocking notifier chain
140 * @val: Value passed unmodified to notifier function
141 * @v: Pointer passed unmodified to notifier function
142 * @nr_to_call: Number of notifier functions to be called. Don't care
143 * value of this parameter is -1.
144 * @nr_calls: Records the number of notifications sent. Don't care
145 * value of this field is NULL.
146 * @returns: notifier_call_chain returns the value returned by the
147 * last notifier function called.
150 static int __kprobes notifier_call_chain(struct notifier_block **nl,
151 unsigned long val, void *v,
152 int nr_to_call, int *nr_calls)
154 int ret = NOTIFY_DONE;
155 struct notifier_block *nb, *next_nb;
157 nb = rcu_dereference(*nl);
159 while (nb && nr_to_call) {
160 next_nb = rcu_dereference(nb->next);
161 ret = nb->notifier_call(nb, val, v);
163 if (nr_calls)
164 (*nr_calls)++;
166 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
167 break;
168 nb = next_nb;
169 nr_to_call--;
171 return ret;
175 * Atomic notifier chain routines. Registration and unregistration
176 * use a spinlock, and call_chain is synchronized by RCU (no locks).
180 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
181 * @nh: Pointer to head of the atomic notifier chain
182 * @n: New entry in notifier chain
184 * Adds a notifier to an atomic notifier chain.
186 * Currently always returns zero.
189 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
190 struct notifier_block *n)
192 unsigned long flags;
193 int ret;
195 spin_lock_irqsave(&nh->lock, flags);
196 ret = notifier_chain_register(&nh->head, n);
197 spin_unlock_irqrestore(&nh->lock, flags);
198 return ret;
201 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
204 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
205 * @nh: Pointer to head of the atomic notifier chain
206 * @n: Entry to remove from notifier chain
208 * Removes a notifier from an atomic notifier chain.
210 * Returns zero on success or %-ENOENT on failure.
212 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
213 struct notifier_block *n)
215 unsigned long flags;
216 int ret;
218 spin_lock_irqsave(&nh->lock, flags);
219 ret = notifier_chain_unregister(&nh->head, n);
220 spin_unlock_irqrestore(&nh->lock, flags);
221 synchronize_rcu();
222 return ret;
225 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
228 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
229 * @nh: Pointer to head of the atomic notifier chain
230 * @val: Value passed unmodified to notifier function
231 * @v: Pointer passed unmodified to notifier function
232 * @nr_to_call: See the comment for notifier_call_chain.
233 * @nr_calls: See the comment for notifier_call_chain.
235 * Calls each function in a notifier chain in turn. The functions
236 * run in an atomic context, so they must not block.
237 * This routine uses RCU to synchronize with changes to the chain.
239 * If the return value of the notifier can be and'ed
240 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
241 * will return immediately, with the return value of
242 * the notifier function which halted execution.
243 * Otherwise the return value is the return value
244 * of the last notifier function called.
247 int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
248 unsigned long val, void *v,
249 int nr_to_call, int *nr_calls)
251 int ret;
253 rcu_read_lock();
254 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
255 rcu_read_unlock();
256 return ret;
259 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
261 int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
262 unsigned long val, void *v)
264 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
267 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
269 * Blocking notifier chain routines. All access to the chain is
270 * synchronized by an rwsem.
274 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
275 * @nh: Pointer to head of the blocking notifier chain
276 * @n: New entry in notifier chain
278 * Adds a notifier to a blocking notifier chain.
279 * Must be called in process context.
281 * Currently always returns zero.
284 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
285 struct notifier_block *n)
287 int ret;
290 * This code gets used during boot-up, when task switching is
291 * not yet working and interrupts must remain disabled. At
292 * such times we must not call down_write().
294 if (unlikely(system_state == SYSTEM_BOOTING))
295 return notifier_chain_register(&nh->head, n);
297 down_write(&nh->rwsem);
298 ret = notifier_chain_register(&nh->head, n);
299 up_write(&nh->rwsem);
300 return ret;
303 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
306 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
307 * @nh: Pointer to head of the blocking notifier chain
308 * @n: Entry to remove from notifier chain
310 * Removes a notifier from a blocking notifier chain.
311 * Must be called from process context.
313 * Returns zero on success or %-ENOENT on failure.
315 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
316 struct notifier_block *n)
318 int ret;
321 * This code gets used during boot-up, when task switching is
322 * not yet working and interrupts must remain disabled. At
323 * such times we must not call down_write().
325 if (unlikely(system_state == SYSTEM_BOOTING))
326 return notifier_chain_unregister(&nh->head, n);
328 down_write(&nh->rwsem);
329 ret = notifier_chain_unregister(&nh->head, n);
330 up_write(&nh->rwsem);
331 return ret;
334 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
337 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
338 * @nh: Pointer to head of the blocking notifier chain
339 * @val: Value passed unmodified to notifier function
340 * @v: Pointer passed unmodified to notifier function
341 * @nr_to_call: See comment for notifier_call_chain.
342 * @nr_calls: See comment for notifier_call_chain.
344 * Calls each function in a notifier chain in turn. The functions
345 * run in a process context, so they are allowed to block.
347 * If the return value of the notifier can be and'ed
348 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
349 * will return immediately, with the return value of
350 * the notifier function which halted execution.
351 * Otherwise the return value is the return value
352 * of the last notifier function called.
355 int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
356 unsigned long val, void *v,
357 int nr_to_call, int *nr_calls)
359 int ret = NOTIFY_DONE;
362 * We check the head outside the lock, but if this access is
363 * racy then it does not matter what the result of the test
364 * is, we re-check the list after having taken the lock anyway:
366 if (rcu_dereference(nh->head)) {
367 down_read(&nh->rwsem);
368 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
369 nr_calls);
370 up_read(&nh->rwsem);
372 return ret;
374 EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
376 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
377 unsigned long val, void *v)
379 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
381 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
384 * Raw notifier chain routines. There is no protection;
385 * the caller must provide it. Use at your own risk!
389 * raw_notifier_chain_register - Add notifier to a raw notifier chain
390 * @nh: Pointer to head of the raw notifier chain
391 * @n: New entry in notifier chain
393 * Adds a notifier to a raw notifier chain.
394 * All locking must be provided by the caller.
396 * Currently always returns zero.
399 int raw_notifier_chain_register(struct raw_notifier_head *nh,
400 struct notifier_block *n)
402 return notifier_chain_register(&nh->head, n);
405 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
408 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
409 * @nh: Pointer to head of the raw notifier chain
410 * @n: Entry to remove from notifier chain
412 * Removes a notifier from a raw notifier chain.
413 * All locking must be provided by the caller.
415 * Returns zero on success or %-ENOENT on failure.
417 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
418 struct notifier_block *n)
420 return notifier_chain_unregister(&nh->head, n);
423 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
426 * __raw_notifier_call_chain - Call functions in a raw notifier chain
427 * @nh: Pointer to head of the raw notifier chain
428 * @val: Value passed unmodified to notifier function
429 * @v: Pointer passed unmodified to notifier function
430 * @nr_to_call: See comment for notifier_call_chain.
431 * @nr_calls: See comment for notifier_call_chain
433 * Calls each function in a notifier chain in turn. The functions
434 * run in an undefined context.
435 * All locking must be provided by the caller.
437 * If the return value of the notifier can be and'ed
438 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
439 * will return immediately, with the return value of
440 * the notifier function which halted execution.
441 * Otherwise the return value is the return value
442 * of the last notifier function called.
445 int __raw_notifier_call_chain(struct raw_notifier_head *nh,
446 unsigned long val, void *v,
447 int nr_to_call, int *nr_calls)
449 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
452 EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
454 int raw_notifier_call_chain(struct raw_notifier_head *nh,
455 unsigned long val, void *v)
457 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
460 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
463 * SRCU notifier chain routines. Registration and unregistration
464 * use a mutex, and call_chain is synchronized by SRCU (no locks).
468 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
469 * @nh: Pointer to head of the SRCU notifier chain
470 * @n: New entry in notifier chain
472 * Adds a notifier to an SRCU notifier chain.
473 * Must be called in process context.
475 * Currently always returns zero.
478 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
479 struct notifier_block *n)
481 int ret;
484 * This code gets used during boot-up, when task switching is
485 * not yet working and interrupts must remain disabled. At
486 * such times we must not call mutex_lock().
488 if (unlikely(system_state == SYSTEM_BOOTING))
489 return notifier_chain_register(&nh->head, n);
491 mutex_lock(&nh->mutex);
492 ret = notifier_chain_register(&nh->head, n);
493 mutex_unlock(&nh->mutex);
494 return ret;
497 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
500 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
501 * @nh: Pointer to head of the SRCU notifier chain
502 * @n: Entry to remove from notifier chain
504 * Removes a notifier from an SRCU notifier chain.
505 * Must be called from process context.
507 * Returns zero on success or %-ENOENT on failure.
509 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
510 struct notifier_block *n)
512 int ret;
515 * This code gets used during boot-up, when task switching is
516 * not yet working and interrupts must remain disabled. At
517 * such times we must not call mutex_lock().
519 if (unlikely(system_state == SYSTEM_BOOTING))
520 return notifier_chain_unregister(&nh->head, n);
522 mutex_lock(&nh->mutex);
523 ret = notifier_chain_unregister(&nh->head, n);
524 mutex_unlock(&nh->mutex);
525 synchronize_srcu(&nh->srcu);
526 return ret;
529 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
532 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
533 * @nh: Pointer to head of the SRCU notifier chain
534 * @val: Value passed unmodified to notifier function
535 * @v: Pointer passed unmodified to notifier function
536 * @nr_to_call: See comment for notifier_call_chain.
537 * @nr_calls: See comment for notifier_call_chain
539 * Calls each function in a notifier chain in turn. The functions
540 * run in a process context, so they are allowed to block.
542 * If the return value of the notifier can be and'ed
543 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
544 * will return immediately, with the return value of
545 * the notifier function which halted execution.
546 * Otherwise the return value is the return value
547 * of the last notifier function called.
550 int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
551 unsigned long val, void *v,
552 int nr_to_call, int *nr_calls)
554 int ret;
555 int idx;
557 idx = srcu_read_lock(&nh->srcu);
558 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
559 srcu_read_unlock(&nh->srcu, idx);
560 return ret;
562 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
564 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
565 unsigned long val, void *v)
567 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
569 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
572 * srcu_init_notifier_head - Initialize an SRCU notifier head
573 * @nh: Pointer to head of the srcu notifier chain
575 * Unlike other sorts of notifier heads, SRCU notifier heads require
576 * dynamic initialization. Be sure to call this routine before
577 * calling any of the other SRCU notifier routines for this head.
579 * If an SRCU notifier head is deallocated, it must first be cleaned
580 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
581 * per-cpu data (used by the SRCU mechanism) will leak.
584 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
586 mutex_init(&nh->mutex);
587 if (init_srcu_struct(&nh->srcu) < 0)
588 BUG();
589 nh->head = NULL;
592 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
595 * register_reboot_notifier - Register function to be called at reboot time
596 * @nb: Info about notifier function to be called
598 * Registers a function with the list of functions
599 * to be called at reboot time.
601 * Currently always returns zero, as blocking_notifier_chain_register()
602 * always returns zero.
605 int register_reboot_notifier(struct notifier_block * nb)
607 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
610 EXPORT_SYMBOL(register_reboot_notifier);
613 * unregister_reboot_notifier - Unregister previously registered reboot notifier
614 * @nb: Hook to be unregistered
616 * Unregisters a previously registered reboot
617 * notifier function.
619 * Returns zero on success, or %-ENOENT on failure.
622 int unregister_reboot_notifier(struct notifier_block * nb)
624 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
627 EXPORT_SYMBOL(unregister_reboot_notifier);
629 static int set_one_prio(struct task_struct *p, int niceval, int error)
631 int no_nice;
633 if (p->uid != current->euid &&
634 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
635 error = -EPERM;
636 goto out;
638 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
639 error = -EACCES;
640 goto out;
642 no_nice = security_task_setnice(p, niceval);
643 if (no_nice) {
644 error = no_nice;
645 goto out;
647 if (error == -ESRCH)
648 error = 0;
649 set_user_nice(p, niceval);
650 out:
651 return error;
654 asmlinkage long sys_setpriority(int which, int who, int niceval)
656 struct task_struct *g, *p;
657 struct user_struct *user;
658 int error = -EINVAL;
659 struct pid *pgrp;
661 if (which > 2 || which < 0)
662 goto out;
664 /* normalize: avoid signed division (rounding problems) */
665 error = -ESRCH;
666 if (niceval < -20)
667 niceval = -20;
668 if (niceval > 19)
669 niceval = 19;
671 read_lock(&tasklist_lock);
672 switch (which) {
673 case PRIO_PROCESS:
674 if (who)
675 p = find_task_by_pid(who);
676 else
677 p = current;
678 if (p)
679 error = set_one_prio(p, niceval, error);
680 break;
681 case PRIO_PGRP:
682 if (who)
683 pgrp = find_pid(who);
684 else
685 pgrp = task_pgrp(current);
686 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
687 error = set_one_prio(p, niceval, error);
688 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
689 break;
690 case PRIO_USER:
691 user = current->user;
692 if (!who)
693 who = current->uid;
694 else
695 if ((who != current->uid) && !(user = find_user(who)))
696 goto out_unlock; /* No processes for this user */
698 do_each_thread(g, p)
699 if (p->uid == who)
700 error = set_one_prio(p, niceval, error);
701 while_each_thread(g, p);
702 if (who != current->uid)
703 free_uid(user); /* For find_user() */
704 break;
706 out_unlock:
707 read_unlock(&tasklist_lock);
708 out:
709 return error;
713 * Ugh. To avoid negative return values, "getpriority()" will
714 * not return the normal nice-value, but a negated value that
715 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
716 * to stay compatible.
718 asmlinkage long sys_getpriority(int which, int who)
720 struct task_struct *g, *p;
721 struct user_struct *user;
722 long niceval, retval = -ESRCH;
723 struct pid *pgrp;
725 if (which > 2 || which < 0)
726 return -EINVAL;
728 read_lock(&tasklist_lock);
729 switch (which) {
730 case PRIO_PROCESS:
731 if (who)
732 p = find_task_by_pid(who);
733 else
734 p = current;
735 if (p) {
736 niceval = 20 - task_nice(p);
737 if (niceval > retval)
738 retval = niceval;
740 break;
741 case PRIO_PGRP:
742 if (who)
743 pgrp = find_pid(who);
744 else
745 pgrp = task_pgrp(current);
746 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
747 niceval = 20 - task_nice(p);
748 if (niceval > retval)
749 retval = niceval;
750 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
751 break;
752 case PRIO_USER:
753 user = current->user;
754 if (!who)
755 who = current->uid;
756 else
757 if ((who != current->uid) && !(user = find_user(who)))
758 goto out_unlock; /* No processes for this user */
760 do_each_thread(g, p)
761 if (p->uid == who) {
762 niceval = 20 - task_nice(p);
763 if (niceval > retval)
764 retval = niceval;
766 while_each_thread(g, p);
767 if (who != current->uid)
768 free_uid(user); /* for find_user() */
769 break;
771 out_unlock:
772 read_unlock(&tasklist_lock);
774 return retval;
778 * emergency_restart - reboot the system
780 * Without shutting down any hardware or taking any locks
781 * reboot the system. This is called when we know we are in
782 * trouble so this is our best effort to reboot. This is
783 * safe to call in interrupt context.
785 void emergency_restart(void)
787 machine_emergency_restart();
789 EXPORT_SYMBOL_GPL(emergency_restart);
791 static void kernel_restart_prepare(char *cmd)
793 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
794 system_state = SYSTEM_RESTART;
795 device_shutdown();
799 * kernel_restart - reboot the system
800 * @cmd: pointer to buffer containing command to execute for restart
801 * or %NULL
803 * Shutdown everything and perform a clean reboot.
804 * This is not safe to call in interrupt context.
806 void kernel_restart(char *cmd)
808 kernel_restart_prepare(cmd);
809 if (!cmd)
810 printk(KERN_EMERG "Restarting system.\n");
811 else
812 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
813 machine_restart(cmd);
815 EXPORT_SYMBOL_GPL(kernel_restart);
818 * kernel_kexec - reboot the system
820 * Move into place and start executing a preloaded standalone
821 * executable. If nothing was preloaded return an error.
823 static void kernel_kexec(void)
825 #ifdef CONFIG_KEXEC
826 struct kimage *image;
827 image = xchg(&kexec_image, NULL);
828 if (!image)
829 return;
830 kernel_restart_prepare(NULL);
831 printk(KERN_EMERG "Starting new kernel\n");
832 machine_shutdown();
833 machine_kexec(image);
834 #endif
837 void kernel_shutdown_prepare(enum system_states state)
839 blocking_notifier_call_chain(&reboot_notifier_list,
840 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
841 system_state = state;
842 device_shutdown();
845 * kernel_halt - halt the system
847 * Shutdown everything and perform a clean system halt.
849 void kernel_halt(void)
851 kernel_shutdown_prepare(SYSTEM_HALT);
852 printk(KERN_EMERG "System halted.\n");
853 machine_halt();
856 EXPORT_SYMBOL_GPL(kernel_halt);
859 * kernel_power_off - power_off the system
861 * Shutdown everything and perform a clean system power_off.
863 void kernel_power_off(void)
865 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
866 printk(KERN_EMERG "Power down.\n");
867 machine_power_off();
869 EXPORT_SYMBOL_GPL(kernel_power_off);
871 * Reboot system call: for obvious reasons only root may call it,
872 * and even root needs to set up some magic numbers in the registers
873 * so that some mistake won't make this reboot the whole machine.
874 * You can also set the meaning of the ctrl-alt-del-key here.
876 * reboot doesn't sync: do that yourself before calling this.
878 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
880 char buffer[256];
882 /* We only trust the superuser with rebooting the system. */
883 if (!capable(CAP_SYS_BOOT))
884 return -EPERM;
886 /* For safety, we require "magic" arguments. */
887 if (magic1 != LINUX_REBOOT_MAGIC1 ||
888 (magic2 != LINUX_REBOOT_MAGIC2 &&
889 magic2 != LINUX_REBOOT_MAGIC2A &&
890 magic2 != LINUX_REBOOT_MAGIC2B &&
891 magic2 != LINUX_REBOOT_MAGIC2C))
892 return -EINVAL;
894 /* Instead of trying to make the power_off code look like
895 * halt when pm_power_off is not set do it the easy way.
897 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
898 cmd = LINUX_REBOOT_CMD_HALT;
900 lock_kernel();
901 switch (cmd) {
902 case LINUX_REBOOT_CMD_RESTART:
903 kernel_restart(NULL);
904 break;
906 case LINUX_REBOOT_CMD_CAD_ON:
907 C_A_D = 1;
908 break;
910 case LINUX_REBOOT_CMD_CAD_OFF:
911 C_A_D = 0;
912 break;
914 case LINUX_REBOOT_CMD_HALT:
915 kernel_halt();
916 unlock_kernel();
917 do_exit(0);
918 break;
920 case LINUX_REBOOT_CMD_POWER_OFF:
921 kernel_power_off();
922 unlock_kernel();
923 do_exit(0);
924 break;
926 case LINUX_REBOOT_CMD_RESTART2:
927 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
928 unlock_kernel();
929 return -EFAULT;
931 buffer[sizeof(buffer) - 1] = '\0';
933 kernel_restart(buffer);
934 break;
936 case LINUX_REBOOT_CMD_KEXEC:
937 kernel_kexec();
938 unlock_kernel();
939 return -EINVAL;
941 #ifdef CONFIG_SOFTWARE_SUSPEND
942 case LINUX_REBOOT_CMD_SW_SUSPEND:
944 int ret = hibernate();
945 unlock_kernel();
946 return ret;
948 #endif
950 default:
951 unlock_kernel();
952 return -EINVAL;
954 unlock_kernel();
955 return 0;
958 static void deferred_cad(struct work_struct *dummy)
960 kernel_restart(NULL);
964 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
965 * As it's called within an interrupt, it may NOT sync: the only choice
966 * is whether to reboot at once, or just ignore the ctrl-alt-del.
968 void ctrl_alt_del(void)
970 static DECLARE_WORK(cad_work, deferred_cad);
972 if (C_A_D)
973 schedule_work(&cad_work);
974 else
975 kill_cad_pid(SIGINT, 1);
979 * Unprivileged users may change the real gid to the effective gid
980 * or vice versa. (BSD-style)
982 * If you set the real gid at all, or set the effective gid to a value not
983 * equal to the real gid, then the saved gid is set to the new effective gid.
985 * This makes it possible for a setgid program to completely drop its
986 * privileges, which is often a useful assertion to make when you are doing
987 * a security audit over a program.
989 * The general idea is that a program which uses just setregid() will be
990 * 100% compatible with BSD. A program which uses just setgid() will be
991 * 100% compatible with POSIX with saved IDs.
993 * SMP: There are not races, the GIDs are checked only by filesystem
994 * operations (as far as semantic preservation is concerned).
996 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
998 int old_rgid = current->gid;
999 int old_egid = current->egid;
1000 int new_rgid = old_rgid;
1001 int new_egid = old_egid;
1002 int retval;
1004 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
1005 if (retval)
1006 return retval;
1008 if (rgid != (gid_t) -1) {
1009 if ((old_rgid == rgid) ||
1010 (current->egid==rgid) ||
1011 capable(CAP_SETGID))
1012 new_rgid = rgid;
1013 else
1014 return -EPERM;
1016 if (egid != (gid_t) -1) {
1017 if ((old_rgid == egid) ||
1018 (current->egid == egid) ||
1019 (current->sgid == egid) ||
1020 capable(CAP_SETGID))
1021 new_egid = egid;
1022 else
1023 return -EPERM;
1025 if (new_egid != old_egid) {
1026 current->mm->dumpable = suid_dumpable;
1027 smp_wmb();
1029 if (rgid != (gid_t) -1 ||
1030 (egid != (gid_t) -1 && egid != old_rgid))
1031 current->sgid = new_egid;
1032 current->fsgid = new_egid;
1033 current->egid = new_egid;
1034 current->gid = new_rgid;
1035 key_fsgid_changed(current);
1036 proc_id_connector(current, PROC_EVENT_GID);
1037 return 0;
1041 * setgid() is implemented like SysV w/ SAVED_IDS
1043 * SMP: Same implicit races as above.
1045 asmlinkage long sys_setgid(gid_t gid)
1047 int old_egid = current->egid;
1048 int retval;
1050 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
1051 if (retval)
1052 return retval;
1054 if (capable(CAP_SETGID)) {
1055 if (old_egid != gid) {
1056 current->mm->dumpable = suid_dumpable;
1057 smp_wmb();
1059 current->gid = current->egid = current->sgid = current->fsgid = gid;
1060 } else if ((gid == current->gid) || (gid == current->sgid)) {
1061 if (old_egid != gid) {
1062 current->mm->dumpable = suid_dumpable;
1063 smp_wmb();
1065 current->egid = current->fsgid = gid;
1067 else
1068 return -EPERM;
1070 key_fsgid_changed(current);
1071 proc_id_connector(current, PROC_EVENT_GID);
1072 return 0;
1075 static int set_user(uid_t new_ruid, int dumpclear)
1077 struct user_struct *new_user;
1079 new_user = alloc_uid(new_ruid);
1080 if (!new_user)
1081 return -EAGAIN;
1083 if (atomic_read(&new_user->processes) >=
1084 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
1085 new_user != &root_user) {
1086 free_uid(new_user);
1087 return -EAGAIN;
1090 switch_uid(new_user);
1092 if (dumpclear) {
1093 current->mm->dumpable = suid_dumpable;
1094 smp_wmb();
1096 current->uid = new_ruid;
1097 return 0;
1101 * Unprivileged users may change the real uid to the effective uid
1102 * or vice versa. (BSD-style)
1104 * If you set the real uid at all, or set the effective uid to a value not
1105 * equal to the real uid, then the saved uid is set to the new effective uid.
1107 * This makes it possible for a setuid program to completely drop its
1108 * privileges, which is often a useful assertion to make when you are doing
1109 * a security audit over a program.
1111 * The general idea is that a program which uses just setreuid() will be
1112 * 100% compatible with BSD. A program which uses just setuid() will be
1113 * 100% compatible with POSIX with saved IDs.
1115 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
1117 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
1118 int retval;
1120 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
1121 if (retval)
1122 return retval;
1124 new_ruid = old_ruid = current->uid;
1125 new_euid = old_euid = current->euid;
1126 old_suid = current->suid;
1128 if (ruid != (uid_t) -1) {
1129 new_ruid = ruid;
1130 if ((old_ruid != ruid) &&
1131 (current->euid != ruid) &&
1132 !capable(CAP_SETUID))
1133 return -EPERM;
1136 if (euid != (uid_t) -1) {
1137 new_euid = euid;
1138 if ((old_ruid != euid) &&
1139 (current->euid != euid) &&
1140 (current->suid != euid) &&
1141 !capable(CAP_SETUID))
1142 return -EPERM;
1145 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
1146 return -EAGAIN;
1148 if (new_euid != old_euid) {
1149 current->mm->dumpable = suid_dumpable;
1150 smp_wmb();
1152 current->fsuid = current->euid = new_euid;
1153 if (ruid != (uid_t) -1 ||
1154 (euid != (uid_t) -1 && euid != old_ruid))
1155 current->suid = current->euid;
1156 current->fsuid = current->euid;
1158 key_fsuid_changed(current);
1159 proc_id_connector(current, PROC_EVENT_UID);
1161 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
1167 * setuid() is implemented like SysV with SAVED_IDS
1169 * Note that SAVED_ID's is deficient in that a setuid root program
1170 * like sendmail, for example, cannot set its uid to be a normal
1171 * user and then switch back, because if you're root, setuid() sets
1172 * the saved uid too. If you don't like this, blame the bright people
1173 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
1174 * will allow a root program to temporarily drop privileges and be able to
1175 * regain them by swapping the real and effective uid.
1177 asmlinkage long sys_setuid(uid_t uid)
1179 int old_euid = current->euid;
1180 int old_ruid, old_suid, new_suid;
1181 int retval;
1183 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
1184 if (retval)
1185 return retval;
1187 old_ruid = current->uid;
1188 old_suid = current->suid;
1189 new_suid = old_suid;
1191 if (capable(CAP_SETUID)) {
1192 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1193 return -EAGAIN;
1194 new_suid = uid;
1195 } else if ((uid != current->uid) && (uid != new_suid))
1196 return -EPERM;
1198 if (old_euid != uid) {
1199 current->mm->dumpable = suid_dumpable;
1200 smp_wmb();
1202 current->fsuid = current->euid = uid;
1203 current->suid = new_suid;
1205 key_fsuid_changed(current);
1206 proc_id_connector(current, PROC_EVENT_UID);
1208 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1213 * This function implements a generic ability to update ruid, euid,
1214 * and suid. This allows you to implement the 4.4 compatible seteuid().
1216 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1218 int old_ruid = current->uid;
1219 int old_euid = current->euid;
1220 int old_suid = current->suid;
1221 int retval;
1223 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1224 if (retval)
1225 return retval;
1227 if (!capable(CAP_SETUID)) {
1228 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1229 (ruid != current->euid) && (ruid != current->suid))
1230 return -EPERM;
1231 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1232 (euid != current->euid) && (euid != current->suid))
1233 return -EPERM;
1234 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1235 (suid != current->euid) && (suid != current->suid))
1236 return -EPERM;
1238 if (ruid != (uid_t) -1) {
1239 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1240 return -EAGAIN;
1242 if (euid != (uid_t) -1) {
1243 if (euid != current->euid) {
1244 current->mm->dumpable = suid_dumpable;
1245 smp_wmb();
1247 current->euid = euid;
1249 current->fsuid = current->euid;
1250 if (suid != (uid_t) -1)
1251 current->suid = suid;
1253 key_fsuid_changed(current);
1254 proc_id_connector(current, PROC_EVENT_UID);
1256 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1259 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1261 int retval;
1263 if (!(retval = put_user(current->uid, ruid)) &&
1264 !(retval = put_user(current->euid, euid)))
1265 retval = put_user(current->suid, suid);
1267 return retval;
1271 * Same as above, but for rgid, egid, sgid.
1273 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1275 int retval;
1277 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1278 if (retval)
1279 return retval;
1281 if (!capable(CAP_SETGID)) {
1282 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1283 (rgid != current->egid) && (rgid != current->sgid))
1284 return -EPERM;
1285 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1286 (egid != current->egid) && (egid != current->sgid))
1287 return -EPERM;
1288 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1289 (sgid != current->egid) && (sgid != current->sgid))
1290 return -EPERM;
1292 if (egid != (gid_t) -1) {
1293 if (egid != current->egid) {
1294 current->mm->dumpable = suid_dumpable;
1295 smp_wmb();
1297 current->egid = egid;
1299 current->fsgid = current->egid;
1300 if (rgid != (gid_t) -1)
1301 current->gid = rgid;
1302 if (sgid != (gid_t) -1)
1303 current->sgid = sgid;
1305 key_fsgid_changed(current);
1306 proc_id_connector(current, PROC_EVENT_GID);
1307 return 0;
1310 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1312 int retval;
1314 if (!(retval = put_user(current->gid, rgid)) &&
1315 !(retval = put_user(current->egid, egid)))
1316 retval = put_user(current->sgid, sgid);
1318 return retval;
1323 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1324 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1325 * whatever uid it wants to). It normally shadows "euid", except when
1326 * explicitly set by setfsuid() or for access..
1328 asmlinkage long sys_setfsuid(uid_t uid)
1330 int old_fsuid;
1332 old_fsuid = current->fsuid;
1333 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1334 return old_fsuid;
1336 if (uid == current->uid || uid == current->euid ||
1337 uid == current->suid || uid == current->fsuid ||
1338 capable(CAP_SETUID)) {
1339 if (uid != old_fsuid) {
1340 current->mm->dumpable = suid_dumpable;
1341 smp_wmb();
1343 current->fsuid = uid;
1346 key_fsuid_changed(current);
1347 proc_id_connector(current, PROC_EVENT_UID);
1349 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1351 return old_fsuid;
1355 * Samma på svenska..
1357 asmlinkage long sys_setfsgid(gid_t gid)
1359 int old_fsgid;
1361 old_fsgid = current->fsgid;
1362 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1363 return old_fsgid;
1365 if (gid == current->gid || gid == current->egid ||
1366 gid == current->sgid || gid == current->fsgid ||
1367 capable(CAP_SETGID)) {
1368 if (gid != old_fsgid) {
1369 current->mm->dumpable = suid_dumpable;
1370 smp_wmb();
1372 current->fsgid = gid;
1373 key_fsgid_changed(current);
1374 proc_id_connector(current, PROC_EVENT_GID);
1376 return old_fsgid;
1379 asmlinkage long sys_times(struct tms __user * tbuf)
1382 * In the SMP world we might just be unlucky and have one of
1383 * the times increment as we use it. Since the value is an
1384 * atomically safe type this is just fine. Conceptually its
1385 * as if the syscall took an instant longer to occur.
1387 if (tbuf) {
1388 struct tms tmp;
1389 struct task_struct *tsk = current;
1390 struct task_struct *t;
1391 cputime_t utime, stime, cutime, cstime;
1393 spin_lock_irq(&tsk->sighand->siglock);
1394 utime = tsk->signal->utime;
1395 stime = tsk->signal->stime;
1396 t = tsk;
1397 do {
1398 utime = cputime_add(utime, t->utime);
1399 stime = cputime_add(stime, t->stime);
1400 t = next_thread(t);
1401 } while (t != tsk);
1403 cutime = tsk->signal->cutime;
1404 cstime = tsk->signal->cstime;
1405 spin_unlock_irq(&tsk->sighand->siglock);
1407 tmp.tms_utime = cputime_to_clock_t(utime);
1408 tmp.tms_stime = cputime_to_clock_t(stime);
1409 tmp.tms_cutime = cputime_to_clock_t(cutime);
1410 tmp.tms_cstime = cputime_to_clock_t(cstime);
1411 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1412 return -EFAULT;
1414 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1418 * This needs some heavy checking ...
1419 * I just haven't the stomach for it. I also don't fully
1420 * understand sessions/pgrp etc. Let somebody who does explain it.
1422 * OK, I think I have the protection semantics right.... this is really
1423 * only important on a multi-user system anyway, to make sure one user
1424 * can't send a signal to a process owned by another. -TYT, 12/12/91
1426 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1427 * LBT 04.03.94
1430 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1432 struct task_struct *p;
1433 struct task_struct *group_leader = current->group_leader;
1434 int err = -EINVAL;
1436 if (!pid)
1437 pid = group_leader->pid;
1438 if (!pgid)
1439 pgid = pid;
1440 if (pgid < 0)
1441 return -EINVAL;
1443 /* From this point forward we keep holding onto the tasklist lock
1444 * so that our parent does not change from under us. -DaveM
1446 write_lock_irq(&tasklist_lock);
1448 err = -ESRCH;
1449 p = find_task_by_pid(pid);
1450 if (!p)
1451 goto out;
1453 err = -EINVAL;
1454 if (!thread_group_leader(p))
1455 goto out;
1457 if (p->real_parent == group_leader) {
1458 err = -EPERM;
1459 if (task_session(p) != task_session(group_leader))
1460 goto out;
1461 err = -EACCES;
1462 if (p->did_exec)
1463 goto out;
1464 } else {
1465 err = -ESRCH;
1466 if (p != group_leader)
1467 goto out;
1470 err = -EPERM;
1471 if (p->signal->leader)
1472 goto out;
1474 if (pgid != pid) {
1475 struct task_struct *g =
1476 find_task_by_pid_type(PIDTYPE_PGID, pgid);
1478 if (!g || task_session(g) != task_session(group_leader))
1479 goto out;
1482 err = security_task_setpgid(p, pgid);
1483 if (err)
1484 goto out;
1486 if (process_group(p) != pgid) {
1487 detach_pid(p, PIDTYPE_PGID);
1488 p->signal->pgrp = pgid;
1489 attach_pid(p, PIDTYPE_PGID, pgid);
1492 err = 0;
1493 out:
1494 /* All paths lead to here, thus we are safe. -DaveM */
1495 write_unlock_irq(&tasklist_lock);
1496 return err;
1499 asmlinkage long sys_getpgid(pid_t pid)
1501 if (!pid)
1502 return process_group(current);
1503 else {
1504 int retval;
1505 struct task_struct *p;
1507 read_lock(&tasklist_lock);
1508 p = find_task_by_pid(pid);
1510 retval = -ESRCH;
1511 if (p) {
1512 retval = security_task_getpgid(p);
1513 if (!retval)
1514 retval = process_group(p);
1516 read_unlock(&tasklist_lock);
1517 return retval;
1521 #ifdef __ARCH_WANT_SYS_GETPGRP
1523 asmlinkage long sys_getpgrp(void)
1525 /* SMP - assuming writes are word atomic this is fine */
1526 return process_group(current);
1529 #endif
1531 asmlinkage long sys_getsid(pid_t pid)
1533 if (!pid)
1534 return process_session(current);
1535 else {
1536 int retval;
1537 struct task_struct *p;
1539 read_lock(&tasklist_lock);
1540 p = find_task_by_pid(pid);
1542 retval = -ESRCH;
1543 if (p) {
1544 retval = security_task_getsid(p);
1545 if (!retval)
1546 retval = process_session(p);
1548 read_unlock(&tasklist_lock);
1549 return retval;
1553 asmlinkage long sys_setsid(void)
1555 struct task_struct *group_leader = current->group_leader;
1556 pid_t session;
1557 int err = -EPERM;
1559 write_lock_irq(&tasklist_lock);
1561 /* Fail if I am already a session leader */
1562 if (group_leader->signal->leader)
1563 goto out;
1565 session = group_leader->pid;
1566 /* Fail if a process group id already exists that equals the
1567 * proposed session id.
1569 * Don't check if session id == 1 because kernel threads use this
1570 * session id and so the check will always fail and make it so
1571 * init cannot successfully call setsid.
1573 if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
1574 goto out;
1576 group_leader->signal->leader = 1;
1577 __set_special_pids(session, session);
1579 spin_lock(&group_leader->sighand->siglock);
1580 group_leader->signal->tty = NULL;
1581 spin_unlock(&group_leader->sighand->siglock);
1583 err = process_group(group_leader);
1584 out:
1585 write_unlock_irq(&tasklist_lock);
1586 return err;
1590 * Supplementary group IDs
1593 /* init to 2 - one for init_task, one to ensure it is never freed */
1594 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1596 struct group_info *groups_alloc(int gidsetsize)
1598 struct group_info *group_info;
1599 int nblocks;
1600 int i;
1602 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1603 /* Make sure we always allocate at least one indirect block pointer */
1604 nblocks = nblocks ? : 1;
1605 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1606 if (!group_info)
1607 return NULL;
1608 group_info->ngroups = gidsetsize;
1609 group_info->nblocks = nblocks;
1610 atomic_set(&group_info->usage, 1);
1612 if (gidsetsize <= NGROUPS_SMALL)
1613 group_info->blocks[0] = group_info->small_block;
1614 else {
1615 for (i = 0; i < nblocks; i++) {
1616 gid_t *b;
1617 b = (void *)__get_free_page(GFP_USER);
1618 if (!b)
1619 goto out_undo_partial_alloc;
1620 group_info->blocks[i] = b;
1623 return group_info;
1625 out_undo_partial_alloc:
1626 while (--i >= 0) {
1627 free_page((unsigned long)group_info->blocks[i]);
1629 kfree(group_info);
1630 return NULL;
1633 EXPORT_SYMBOL(groups_alloc);
1635 void groups_free(struct group_info *group_info)
1637 if (group_info->blocks[0] != group_info->small_block) {
1638 int i;
1639 for (i = 0; i < group_info->nblocks; i++)
1640 free_page((unsigned long)group_info->blocks[i]);
1642 kfree(group_info);
1645 EXPORT_SYMBOL(groups_free);
1647 /* export the group_info to a user-space array */
1648 static int groups_to_user(gid_t __user *grouplist,
1649 struct group_info *group_info)
1651 int i;
1652 int count = group_info->ngroups;
1654 for (i = 0; i < group_info->nblocks; i++) {
1655 int cp_count = min(NGROUPS_PER_BLOCK, count);
1656 int off = i * NGROUPS_PER_BLOCK;
1657 int len = cp_count * sizeof(*grouplist);
1659 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1660 return -EFAULT;
1662 count -= cp_count;
1664 return 0;
1667 /* fill a group_info from a user-space array - it must be allocated already */
1668 static int groups_from_user(struct group_info *group_info,
1669 gid_t __user *grouplist)
1671 int i;
1672 int count = group_info->ngroups;
1674 for (i = 0; i < group_info->nblocks; i++) {
1675 int cp_count = min(NGROUPS_PER_BLOCK, count);
1676 int off = i * NGROUPS_PER_BLOCK;
1677 int len = cp_count * sizeof(*grouplist);
1679 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1680 return -EFAULT;
1682 count -= cp_count;
1684 return 0;
1687 /* a simple Shell sort */
1688 static void groups_sort(struct group_info *group_info)
1690 int base, max, stride;
1691 int gidsetsize = group_info->ngroups;
1693 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1694 ; /* nothing */
1695 stride /= 3;
1697 while (stride) {
1698 max = gidsetsize - stride;
1699 for (base = 0; base < max; base++) {
1700 int left = base;
1701 int right = left + stride;
1702 gid_t tmp = GROUP_AT(group_info, right);
1704 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1705 GROUP_AT(group_info, right) =
1706 GROUP_AT(group_info, left);
1707 right = left;
1708 left -= stride;
1710 GROUP_AT(group_info, right) = tmp;
1712 stride /= 3;
1716 /* a simple bsearch */
1717 int groups_search(struct group_info *group_info, gid_t grp)
1719 unsigned int left, right;
1721 if (!group_info)
1722 return 0;
1724 left = 0;
1725 right = group_info->ngroups;
1726 while (left < right) {
1727 unsigned int mid = (left+right)/2;
1728 int cmp = grp - GROUP_AT(group_info, mid);
1729 if (cmp > 0)
1730 left = mid + 1;
1731 else if (cmp < 0)
1732 right = mid;
1733 else
1734 return 1;
1736 return 0;
1739 /* validate and set current->group_info */
1740 int set_current_groups(struct group_info *group_info)
1742 int retval;
1743 struct group_info *old_info;
1745 retval = security_task_setgroups(group_info);
1746 if (retval)
1747 return retval;
1749 groups_sort(group_info);
1750 get_group_info(group_info);
1752 task_lock(current);
1753 old_info = current->group_info;
1754 current->group_info = group_info;
1755 task_unlock(current);
1757 put_group_info(old_info);
1759 return 0;
1762 EXPORT_SYMBOL(set_current_groups);
1764 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1766 int i = 0;
1769 * SMP: Nobody else can change our grouplist. Thus we are
1770 * safe.
1773 if (gidsetsize < 0)
1774 return -EINVAL;
1776 /* no need to grab task_lock here; it cannot change */
1777 i = current->group_info->ngroups;
1778 if (gidsetsize) {
1779 if (i > gidsetsize) {
1780 i = -EINVAL;
1781 goto out;
1783 if (groups_to_user(grouplist, current->group_info)) {
1784 i = -EFAULT;
1785 goto out;
1788 out:
1789 return i;
1793 * SMP: Our groups are copy-on-write. We can set them safely
1794 * without another task interfering.
1797 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1799 struct group_info *group_info;
1800 int retval;
1802 if (!capable(CAP_SETGID))
1803 return -EPERM;
1804 if ((unsigned)gidsetsize > NGROUPS_MAX)
1805 return -EINVAL;
1807 group_info = groups_alloc(gidsetsize);
1808 if (!group_info)
1809 return -ENOMEM;
1810 retval = groups_from_user(group_info, grouplist);
1811 if (retval) {
1812 put_group_info(group_info);
1813 return retval;
1816 retval = set_current_groups(group_info);
1817 put_group_info(group_info);
1819 return retval;
1823 * Check whether we're fsgid/egid or in the supplemental group..
1825 int in_group_p(gid_t grp)
1827 int retval = 1;
1828 if (grp != current->fsgid)
1829 retval = groups_search(current->group_info, grp);
1830 return retval;
1833 EXPORT_SYMBOL(in_group_p);
1835 int in_egroup_p(gid_t grp)
1837 int retval = 1;
1838 if (grp != current->egid)
1839 retval = groups_search(current->group_info, grp);
1840 return retval;
1843 EXPORT_SYMBOL(in_egroup_p);
1845 DECLARE_RWSEM(uts_sem);
1847 EXPORT_SYMBOL(uts_sem);
1849 asmlinkage long sys_newuname(struct new_utsname __user * name)
1851 int errno = 0;
1853 down_read(&uts_sem);
1854 if (copy_to_user(name, utsname(), sizeof *name))
1855 errno = -EFAULT;
1856 up_read(&uts_sem);
1857 return errno;
1860 asmlinkage long sys_sethostname(char __user *name, int len)
1862 int errno;
1863 char tmp[__NEW_UTS_LEN];
1865 if (!capable(CAP_SYS_ADMIN))
1866 return -EPERM;
1867 if (len < 0 || len > __NEW_UTS_LEN)
1868 return -EINVAL;
1869 down_write(&uts_sem);
1870 errno = -EFAULT;
1871 if (!copy_from_user(tmp, name, len)) {
1872 memcpy(utsname()->nodename, tmp, len);
1873 utsname()->nodename[len] = 0;
1874 errno = 0;
1876 up_write(&uts_sem);
1877 return errno;
1880 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1882 asmlinkage long sys_gethostname(char __user *name, int len)
1884 int i, errno;
1886 if (len < 0)
1887 return -EINVAL;
1888 down_read(&uts_sem);
1889 i = 1 + strlen(utsname()->nodename);
1890 if (i > len)
1891 i = len;
1892 errno = 0;
1893 if (copy_to_user(name, utsname()->nodename, i))
1894 errno = -EFAULT;
1895 up_read(&uts_sem);
1896 return errno;
1899 #endif
1902 * Only setdomainname; getdomainname can be implemented by calling
1903 * uname()
1905 asmlinkage long sys_setdomainname(char __user *name, int len)
1907 int errno;
1908 char tmp[__NEW_UTS_LEN];
1910 if (!capable(CAP_SYS_ADMIN))
1911 return -EPERM;
1912 if (len < 0 || len > __NEW_UTS_LEN)
1913 return -EINVAL;
1915 down_write(&uts_sem);
1916 errno = -EFAULT;
1917 if (!copy_from_user(tmp, name, len)) {
1918 memcpy(utsname()->domainname, tmp, len);
1919 utsname()->domainname[len] = 0;
1920 errno = 0;
1922 up_write(&uts_sem);
1923 return errno;
1926 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1928 if (resource >= RLIM_NLIMITS)
1929 return -EINVAL;
1930 else {
1931 struct rlimit value;
1932 task_lock(current->group_leader);
1933 value = current->signal->rlim[resource];
1934 task_unlock(current->group_leader);
1935 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1939 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1942 * Back compatibility for getrlimit. Needed for some apps.
1945 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1947 struct rlimit x;
1948 if (resource >= RLIM_NLIMITS)
1949 return -EINVAL;
1951 task_lock(current->group_leader);
1952 x = current->signal->rlim[resource];
1953 task_unlock(current->group_leader);
1954 if (x.rlim_cur > 0x7FFFFFFF)
1955 x.rlim_cur = 0x7FFFFFFF;
1956 if (x.rlim_max > 0x7FFFFFFF)
1957 x.rlim_max = 0x7FFFFFFF;
1958 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1961 #endif
1963 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1965 struct rlimit new_rlim, *old_rlim;
1966 unsigned long it_prof_secs;
1967 int retval;
1969 if (resource >= RLIM_NLIMITS)
1970 return -EINVAL;
1971 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1972 return -EFAULT;
1973 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1974 return -EINVAL;
1975 old_rlim = current->signal->rlim + resource;
1976 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1977 !capable(CAP_SYS_RESOURCE))
1978 return -EPERM;
1979 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1980 return -EPERM;
1982 retval = security_task_setrlimit(resource, &new_rlim);
1983 if (retval)
1984 return retval;
1986 if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
1988 * The caller is asking for an immediate RLIMIT_CPU
1989 * expiry. But we use the zero value to mean "it was
1990 * never set". So let's cheat and make it one second
1991 * instead
1993 new_rlim.rlim_cur = 1;
1996 task_lock(current->group_leader);
1997 *old_rlim = new_rlim;
1998 task_unlock(current->group_leader);
2000 if (resource != RLIMIT_CPU)
2001 goto out;
2004 * RLIMIT_CPU handling. Note that the kernel fails to return an error
2005 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
2006 * very long-standing error, and fixing it now risks breakage of
2007 * applications, so we live with it
2009 if (new_rlim.rlim_cur == RLIM_INFINITY)
2010 goto out;
2012 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
2013 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
2014 unsigned long rlim_cur = new_rlim.rlim_cur;
2015 cputime_t cputime;
2017 cputime = secs_to_cputime(rlim_cur);
2018 read_lock(&tasklist_lock);
2019 spin_lock_irq(&current->sighand->siglock);
2020 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
2021 spin_unlock_irq(&current->sighand->siglock);
2022 read_unlock(&tasklist_lock);
2024 out:
2025 return 0;
2029 * It would make sense to put struct rusage in the task_struct,
2030 * except that would make the task_struct be *really big*. After
2031 * task_struct gets moved into malloc'ed memory, it would
2032 * make sense to do this. It will make moving the rest of the information
2033 * a lot simpler! (Which we're not doing right now because we're not
2034 * measuring them yet).
2036 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
2037 * races with threads incrementing their own counters. But since word
2038 * reads are atomic, we either get new values or old values and we don't
2039 * care which for the sums. We always take the siglock to protect reading
2040 * the c* fields from p->signal from races with exit.c updating those
2041 * fields when reaping, so a sample either gets all the additions of a
2042 * given child after it's reaped, or none so this sample is before reaping.
2044 * Locking:
2045 * We need to take the siglock for CHILDEREN, SELF and BOTH
2046 * for the cases current multithreaded, non-current single threaded
2047 * non-current multithreaded. Thread traversal is now safe with
2048 * the siglock held.
2049 * Strictly speaking, we donot need to take the siglock if we are current and
2050 * single threaded, as no one else can take our signal_struct away, no one
2051 * else can reap the children to update signal->c* counters, and no one else
2052 * can race with the signal-> fields. If we do not take any lock, the
2053 * signal-> fields could be read out of order while another thread was just
2054 * exiting. So we should place a read memory barrier when we avoid the lock.
2055 * On the writer side, write memory barrier is implied in __exit_signal
2056 * as __exit_signal releases the siglock spinlock after updating the signal->
2057 * fields. But we don't do this yet to keep things simple.
2061 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
2063 struct task_struct *t;
2064 unsigned long flags;
2065 cputime_t utime, stime;
2067 memset((char *) r, 0, sizeof *r);
2068 utime = stime = cputime_zero;
2070 rcu_read_lock();
2071 if (!lock_task_sighand(p, &flags)) {
2072 rcu_read_unlock();
2073 return;
2076 switch (who) {
2077 case RUSAGE_BOTH:
2078 case RUSAGE_CHILDREN:
2079 utime = p->signal->cutime;
2080 stime = p->signal->cstime;
2081 r->ru_nvcsw = p->signal->cnvcsw;
2082 r->ru_nivcsw = p->signal->cnivcsw;
2083 r->ru_minflt = p->signal->cmin_flt;
2084 r->ru_majflt = p->signal->cmaj_flt;
2086 if (who == RUSAGE_CHILDREN)
2087 break;
2089 case RUSAGE_SELF:
2090 utime = cputime_add(utime, p->signal->utime);
2091 stime = cputime_add(stime, p->signal->stime);
2092 r->ru_nvcsw += p->signal->nvcsw;
2093 r->ru_nivcsw += p->signal->nivcsw;
2094 r->ru_minflt += p->signal->min_flt;
2095 r->ru_majflt += p->signal->maj_flt;
2096 t = p;
2097 do {
2098 utime = cputime_add(utime, t->utime);
2099 stime = cputime_add(stime, t->stime);
2100 r->ru_nvcsw += t->nvcsw;
2101 r->ru_nivcsw += t->nivcsw;
2102 r->ru_minflt += t->min_flt;
2103 r->ru_majflt += t->maj_flt;
2104 t = next_thread(t);
2105 } while (t != p);
2106 break;
2108 default:
2109 BUG();
2112 unlock_task_sighand(p, &flags);
2113 rcu_read_unlock();
2115 cputime_to_timeval(utime, &r->ru_utime);
2116 cputime_to_timeval(stime, &r->ru_stime);
2119 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
2121 struct rusage r;
2122 k_getrusage(p, who, &r);
2123 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
2126 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
2128 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
2129 return -EINVAL;
2130 return getrusage(current, who, ru);
2133 asmlinkage long sys_umask(int mask)
2135 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
2136 return mask;
2139 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
2140 unsigned long arg4, unsigned long arg5)
2142 long error;
2144 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2145 if (error)
2146 return error;
2148 switch (option) {
2149 case PR_SET_PDEATHSIG:
2150 if (!valid_signal(arg2)) {
2151 error = -EINVAL;
2152 break;
2154 current->pdeath_signal = arg2;
2155 break;
2156 case PR_GET_PDEATHSIG:
2157 error = put_user(current->pdeath_signal, (int __user *)arg2);
2158 break;
2159 case PR_GET_DUMPABLE:
2160 error = current->mm->dumpable;
2161 break;
2162 case PR_SET_DUMPABLE:
2163 if (arg2 < 0 || arg2 > 1) {
2164 error = -EINVAL;
2165 break;
2167 current->mm->dumpable = arg2;
2168 break;
2170 case PR_SET_UNALIGN:
2171 error = SET_UNALIGN_CTL(current, arg2);
2172 break;
2173 case PR_GET_UNALIGN:
2174 error = GET_UNALIGN_CTL(current, arg2);
2175 break;
2176 case PR_SET_FPEMU:
2177 error = SET_FPEMU_CTL(current, arg2);
2178 break;
2179 case PR_GET_FPEMU:
2180 error = GET_FPEMU_CTL(current, arg2);
2181 break;
2182 case PR_SET_FPEXC:
2183 error = SET_FPEXC_CTL(current, arg2);
2184 break;
2185 case PR_GET_FPEXC:
2186 error = GET_FPEXC_CTL(current, arg2);
2187 break;
2188 case PR_GET_TIMING:
2189 error = PR_TIMING_STATISTICAL;
2190 break;
2191 case PR_SET_TIMING:
2192 if (arg2 == PR_TIMING_STATISTICAL)
2193 error = 0;
2194 else
2195 error = -EINVAL;
2196 break;
2198 case PR_GET_KEEPCAPS:
2199 if (current->keep_capabilities)
2200 error = 1;
2201 break;
2202 case PR_SET_KEEPCAPS:
2203 if (arg2 != 0 && arg2 != 1) {
2204 error = -EINVAL;
2205 break;
2207 current->keep_capabilities = arg2;
2208 break;
2209 case PR_SET_NAME: {
2210 struct task_struct *me = current;
2211 unsigned char ncomm[sizeof(me->comm)];
2213 ncomm[sizeof(me->comm)-1] = 0;
2214 if (strncpy_from_user(ncomm, (char __user *)arg2,
2215 sizeof(me->comm)-1) < 0)
2216 return -EFAULT;
2217 set_task_comm(me, ncomm);
2218 return 0;
2220 case PR_GET_NAME: {
2221 struct task_struct *me = current;
2222 unsigned char tcomm[sizeof(me->comm)];
2224 get_task_comm(tcomm, me);
2225 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2226 return -EFAULT;
2227 return 0;
2229 case PR_GET_ENDIAN:
2230 error = GET_ENDIAN(current, arg2);
2231 break;
2232 case PR_SET_ENDIAN:
2233 error = SET_ENDIAN(current, arg2);
2234 break;
2236 default:
2237 error = -EINVAL;
2238 break;
2240 return error;
2243 asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2244 struct getcpu_cache __user *cache)
2246 int err = 0;
2247 int cpu = raw_smp_processor_id();
2248 if (cpup)
2249 err |= put_user(cpu, cpup);
2250 if (nodep)
2251 err |= put_user(cpu_to_node(cpu), nodep);
2252 if (cache) {
2254 * The cache is not needed for this implementation,
2255 * but make sure user programs pass something
2256 * valid. vsyscall implementations can instead make
2257 * good use of the cache. Only use t0 and t1 because
2258 * these are available in both 32bit and 64bit ABI (no
2259 * need for a compat_getcpu). 32bit has enough
2260 * padding
2262 unsigned long t0, t1;
2263 get_user(t0, &cache->blob[0]);
2264 get_user(t1, &cache->blob[1]);
2265 t0++;
2266 t1++;
2267 put_user(t0, &cache->blob[0]);
2268 put_user(t1, &cache->blob[1]);
2270 return err ? -EFAULT : 0;