ACPI: thinkpad-acpi: make sure DSDT TMPx readings don't return +128
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / sys.c
blob08562f419768464c867537cd278ffe8b965e6bbe
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/resource.h>
18 #include <linux/kernel.h>
19 #include <linux/kexec.h>
20 #include <linux/workqueue.h>
21 #include <linux/capability.h>
22 #include <linux/device.h>
23 #include <linux/key.h>
24 #include <linux/times.h>
25 #include <linux/posix-timers.h>
26 #include <linux/security.h>
27 #include <linux/dcookies.h>
28 #include <linux/suspend.h>
29 #include <linux/tty.h>
30 #include <linux/signal.h>
31 #include <linux/cn_proc.h>
32 #include <linux/getcpu.h>
33 #include <linux/task_io_accounting_ops.h>
34 #include <linux/seccomp.h>
36 #include <linux/compat.h>
37 #include <linux/syscalls.h>
38 #include <linux/kprobes.h>
39 #include <linux/user_namespace.h>
41 #include <asm/uaccess.h>
42 #include <asm/io.h>
43 #include <asm/unistd.h>
45 #ifndef SET_UNALIGN_CTL
46 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
47 #endif
48 #ifndef GET_UNALIGN_CTL
49 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
50 #endif
51 #ifndef SET_FPEMU_CTL
52 # define SET_FPEMU_CTL(a,b) (-EINVAL)
53 #endif
54 #ifndef GET_FPEMU_CTL
55 # define GET_FPEMU_CTL(a,b) (-EINVAL)
56 #endif
57 #ifndef SET_FPEXC_CTL
58 # define SET_FPEXC_CTL(a,b) (-EINVAL)
59 #endif
60 #ifndef GET_FPEXC_CTL
61 # define GET_FPEXC_CTL(a,b) (-EINVAL)
62 #endif
63 #ifndef GET_ENDIAN
64 # define GET_ENDIAN(a,b) (-EINVAL)
65 #endif
66 #ifndef SET_ENDIAN
67 # define SET_ENDIAN(a,b) (-EINVAL)
68 #endif
71 * this is where the system-wide overflow UID and GID are defined, for
72 * architectures that now have 32-bit UID/GID but didn't in the past
75 int overflowuid = DEFAULT_OVERFLOWUID;
76 int overflowgid = DEFAULT_OVERFLOWGID;
78 #ifdef CONFIG_UID16
79 EXPORT_SYMBOL(overflowuid);
80 EXPORT_SYMBOL(overflowgid);
81 #endif
84 * the same as above, but for filesystems which can only store a 16-bit
85 * UID and GID. as such, this is needed on all architectures
88 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
89 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
91 EXPORT_SYMBOL(fs_overflowuid);
92 EXPORT_SYMBOL(fs_overflowgid);
95 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
98 int C_A_D = 1;
99 struct pid *cad_pid;
100 EXPORT_SYMBOL(cad_pid);
103 * If set, this is used for preparing the system to power off.
106 void (*pm_power_off_prepare)(void);
107 EXPORT_SYMBOL(pm_power_off_prepare);
110 * Notifier list for kernel code which wants to be called
111 * at shutdown. This is used to stop any idling DMA operations
112 * and the like.
115 static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
118 * Notifier chain core routines. The exported routines below
119 * are layered on top of these, with appropriate locking added.
122 static int notifier_chain_register(struct notifier_block **nl,
123 struct notifier_block *n)
125 while ((*nl) != NULL) {
126 if (n->priority > (*nl)->priority)
127 break;
128 nl = &((*nl)->next);
130 n->next = *nl;
131 rcu_assign_pointer(*nl, n);
132 return 0;
135 static int notifier_chain_unregister(struct notifier_block **nl,
136 struct notifier_block *n)
138 while ((*nl) != NULL) {
139 if ((*nl) == n) {
140 rcu_assign_pointer(*nl, n->next);
141 return 0;
143 nl = &((*nl)->next);
145 return -ENOENT;
149 * notifier_call_chain - Informs the registered notifiers about an event.
150 * @nl: Pointer to head of the blocking notifier chain
151 * @val: Value passed unmodified to notifier function
152 * @v: Pointer passed unmodified to notifier function
153 * @nr_to_call: Number of notifier functions to be called. Don't care
154 * value of this parameter is -1.
155 * @nr_calls: Records the number of notifications sent. Don't care
156 * value of this field is NULL.
157 * @returns: notifier_call_chain returns the value returned by the
158 * last notifier function called.
161 static int __kprobes notifier_call_chain(struct notifier_block **nl,
162 unsigned long val, void *v,
163 int nr_to_call, int *nr_calls)
165 int ret = NOTIFY_DONE;
166 struct notifier_block *nb, *next_nb;
168 nb = rcu_dereference(*nl);
170 while (nb && nr_to_call) {
171 next_nb = rcu_dereference(nb->next);
172 ret = nb->notifier_call(nb, val, v);
174 if (nr_calls)
175 (*nr_calls)++;
177 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
178 break;
179 nb = next_nb;
180 nr_to_call--;
182 return ret;
186 * Atomic notifier chain routines. Registration and unregistration
187 * use a spinlock, and call_chain is synchronized by RCU (no locks).
191 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
192 * @nh: Pointer to head of the atomic notifier chain
193 * @n: New entry in notifier chain
195 * Adds a notifier to an atomic notifier chain.
197 * Currently always returns zero.
200 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
201 struct notifier_block *n)
203 unsigned long flags;
204 int ret;
206 spin_lock_irqsave(&nh->lock, flags);
207 ret = notifier_chain_register(&nh->head, n);
208 spin_unlock_irqrestore(&nh->lock, flags);
209 return ret;
212 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
215 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
216 * @nh: Pointer to head of the atomic notifier chain
217 * @n: Entry to remove from notifier chain
219 * Removes a notifier from an atomic notifier chain.
221 * Returns zero on success or %-ENOENT on failure.
223 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
224 struct notifier_block *n)
226 unsigned long flags;
227 int ret;
229 spin_lock_irqsave(&nh->lock, flags);
230 ret = notifier_chain_unregister(&nh->head, n);
231 spin_unlock_irqrestore(&nh->lock, flags);
232 synchronize_rcu();
233 return ret;
236 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
239 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
240 * @nh: Pointer to head of the atomic notifier chain
241 * @val: Value passed unmodified to notifier function
242 * @v: Pointer passed unmodified to notifier function
243 * @nr_to_call: See the comment for notifier_call_chain.
244 * @nr_calls: See the comment for notifier_call_chain.
246 * Calls each function in a notifier chain in turn. The functions
247 * run in an atomic context, so they must not block.
248 * This routine uses RCU to synchronize with changes to the chain.
250 * If the return value of the notifier can be and'ed
251 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
252 * will return immediately, with the return value of
253 * the notifier function which halted execution.
254 * Otherwise the return value is the return value
255 * of the last notifier function called.
258 int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
259 unsigned long val, void *v,
260 int nr_to_call, int *nr_calls)
262 int ret;
264 rcu_read_lock();
265 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
266 rcu_read_unlock();
267 return ret;
270 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
272 int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
273 unsigned long val, void *v)
275 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
278 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
280 * Blocking notifier chain routines. All access to the chain is
281 * synchronized by an rwsem.
285 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
286 * @nh: Pointer to head of the blocking notifier chain
287 * @n: New entry in notifier chain
289 * Adds a notifier to a blocking notifier chain.
290 * Must be called in process context.
292 * Currently always returns zero.
295 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
296 struct notifier_block *n)
298 int ret;
301 * This code gets used during boot-up, when task switching is
302 * not yet working and interrupts must remain disabled. At
303 * such times we must not call down_write().
305 if (unlikely(system_state == SYSTEM_BOOTING))
306 return notifier_chain_register(&nh->head, n);
308 down_write(&nh->rwsem);
309 ret = notifier_chain_register(&nh->head, n);
310 up_write(&nh->rwsem);
311 return ret;
314 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
317 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
318 * @nh: Pointer to head of the blocking notifier chain
319 * @n: Entry to remove from notifier chain
321 * Removes a notifier from a blocking notifier chain.
322 * Must be called from process context.
324 * Returns zero on success or %-ENOENT on failure.
326 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
327 struct notifier_block *n)
329 int ret;
332 * This code gets used during boot-up, when task switching is
333 * not yet working and interrupts must remain disabled. At
334 * such times we must not call down_write().
336 if (unlikely(system_state == SYSTEM_BOOTING))
337 return notifier_chain_unregister(&nh->head, n);
339 down_write(&nh->rwsem);
340 ret = notifier_chain_unregister(&nh->head, n);
341 up_write(&nh->rwsem);
342 return ret;
345 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
348 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
349 * @nh: Pointer to head of the blocking notifier chain
350 * @val: Value passed unmodified to notifier function
351 * @v: Pointer passed unmodified to notifier function
352 * @nr_to_call: See comment for notifier_call_chain.
353 * @nr_calls: See comment for notifier_call_chain.
355 * Calls each function in a notifier chain in turn. The functions
356 * run in a process context, so they are allowed to block.
358 * If the return value of the notifier can be and'ed
359 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
360 * will return immediately, with the return value of
361 * the notifier function which halted execution.
362 * Otherwise the return value is the return value
363 * of the last notifier function called.
366 int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
367 unsigned long val, void *v,
368 int nr_to_call, int *nr_calls)
370 int ret = NOTIFY_DONE;
373 * We check the head outside the lock, but if this access is
374 * racy then it does not matter what the result of the test
375 * is, we re-check the list after having taken the lock anyway:
377 if (rcu_dereference(nh->head)) {
378 down_read(&nh->rwsem);
379 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
380 nr_calls);
381 up_read(&nh->rwsem);
383 return ret;
385 EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
387 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
388 unsigned long val, void *v)
390 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
392 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
395 * Raw notifier chain routines. There is no protection;
396 * the caller must provide it. Use at your own risk!
400 * raw_notifier_chain_register - Add notifier to a raw notifier chain
401 * @nh: Pointer to head of the raw notifier chain
402 * @n: New entry in notifier chain
404 * Adds a notifier to a raw notifier chain.
405 * All locking must be provided by the caller.
407 * Currently always returns zero.
410 int raw_notifier_chain_register(struct raw_notifier_head *nh,
411 struct notifier_block *n)
413 return notifier_chain_register(&nh->head, n);
416 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
419 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
420 * @nh: Pointer to head of the raw notifier chain
421 * @n: Entry to remove from notifier chain
423 * Removes a notifier from a raw notifier chain.
424 * All locking must be provided by the caller.
426 * Returns zero on success or %-ENOENT on failure.
428 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
429 struct notifier_block *n)
431 return notifier_chain_unregister(&nh->head, n);
434 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
437 * __raw_notifier_call_chain - Call functions in a raw notifier chain
438 * @nh: Pointer to head of the raw notifier chain
439 * @val: Value passed unmodified to notifier function
440 * @v: Pointer passed unmodified to notifier function
441 * @nr_to_call: See comment for notifier_call_chain.
442 * @nr_calls: See comment for notifier_call_chain
444 * Calls each function in a notifier chain in turn. The functions
445 * run in an undefined context.
446 * All locking must be provided by the caller.
448 * If the return value of the notifier can be and'ed
449 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
450 * will return immediately, with the return value of
451 * the notifier function which halted execution.
452 * Otherwise the return value is the return value
453 * of the last notifier function called.
456 int __raw_notifier_call_chain(struct raw_notifier_head *nh,
457 unsigned long val, void *v,
458 int nr_to_call, int *nr_calls)
460 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
463 EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
465 int raw_notifier_call_chain(struct raw_notifier_head *nh,
466 unsigned long val, void *v)
468 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
471 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
474 * SRCU notifier chain routines. Registration and unregistration
475 * use a mutex, and call_chain is synchronized by SRCU (no locks).
479 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
480 * @nh: Pointer to head of the SRCU notifier chain
481 * @n: New entry in notifier chain
483 * Adds a notifier to an SRCU notifier chain.
484 * Must be called in process context.
486 * Currently always returns zero.
489 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
490 struct notifier_block *n)
492 int ret;
495 * This code gets used during boot-up, when task switching is
496 * not yet working and interrupts must remain disabled. At
497 * such times we must not call mutex_lock().
499 if (unlikely(system_state == SYSTEM_BOOTING))
500 return notifier_chain_register(&nh->head, n);
502 mutex_lock(&nh->mutex);
503 ret = notifier_chain_register(&nh->head, n);
504 mutex_unlock(&nh->mutex);
505 return ret;
508 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
511 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
512 * @nh: Pointer to head of the SRCU notifier chain
513 * @n: Entry to remove from notifier chain
515 * Removes a notifier from an SRCU notifier chain.
516 * Must be called from process context.
518 * Returns zero on success or %-ENOENT on failure.
520 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
521 struct notifier_block *n)
523 int ret;
526 * This code gets used during boot-up, when task switching is
527 * not yet working and interrupts must remain disabled. At
528 * such times we must not call mutex_lock().
530 if (unlikely(system_state == SYSTEM_BOOTING))
531 return notifier_chain_unregister(&nh->head, n);
533 mutex_lock(&nh->mutex);
534 ret = notifier_chain_unregister(&nh->head, n);
535 mutex_unlock(&nh->mutex);
536 synchronize_srcu(&nh->srcu);
537 return ret;
540 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
543 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
544 * @nh: Pointer to head of the SRCU notifier chain
545 * @val: Value passed unmodified to notifier function
546 * @v: Pointer passed unmodified to notifier function
547 * @nr_to_call: See comment for notifier_call_chain.
548 * @nr_calls: See comment for notifier_call_chain
550 * Calls each function in a notifier chain in turn. The functions
551 * run in a process context, so they are allowed to block.
553 * If the return value of the notifier can be and'ed
554 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
555 * will return immediately, with the return value of
556 * the notifier function which halted execution.
557 * Otherwise the return value is the return value
558 * of the last notifier function called.
561 int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
562 unsigned long val, void *v,
563 int nr_to_call, int *nr_calls)
565 int ret;
566 int idx;
568 idx = srcu_read_lock(&nh->srcu);
569 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
570 srcu_read_unlock(&nh->srcu, idx);
571 return ret;
573 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
575 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
576 unsigned long val, void *v)
578 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
580 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
583 * srcu_init_notifier_head - Initialize an SRCU notifier head
584 * @nh: Pointer to head of the srcu notifier chain
586 * Unlike other sorts of notifier heads, SRCU notifier heads require
587 * dynamic initialization. Be sure to call this routine before
588 * calling any of the other SRCU notifier routines for this head.
590 * If an SRCU notifier head is deallocated, it must first be cleaned
591 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
592 * per-cpu data (used by the SRCU mechanism) will leak.
595 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
597 mutex_init(&nh->mutex);
598 if (init_srcu_struct(&nh->srcu) < 0)
599 BUG();
600 nh->head = NULL;
603 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
606 * register_reboot_notifier - Register function to be called at reboot time
607 * @nb: Info about notifier function to be called
609 * Registers a function with the list of functions
610 * to be called at reboot time.
612 * Currently always returns zero, as blocking_notifier_chain_register()
613 * always returns zero.
616 int register_reboot_notifier(struct notifier_block * nb)
618 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
621 EXPORT_SYMBOL(register_reboot_notifier);
624 * unregister_reboot_notifier - Unregister previously registered reboot notifier
625 * @nb: Hook to be unregistered
627 * Unregisters a previously registered reboot
628 * notifier function.
630 * Returns zero on success, or %-ENOENT on failure.
633 int unregister_reboot_notifier(struct notifier_block * nb)
635 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
638 EXPORT_SYMBOL(unregister_reboot_notifier);
640 static int set_one_prio(struct task_struct *p, int niceval, int error)
642 int no_nice;
644 if (p->uid != current->euid &&
645 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
646 error = -EPERM;
647 goto out;
649 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
650 error = -EACCES;
651 goto out;
653 no_nice = security_task_setnice(p, niceval);
654 if (no_nice) {
655 error = no_nice;
656 goto out;
658 if (error == -ESRCH)
659 error = 0;
660 set_user_nice(p, niceval);
661 out:
662 return error;
665 asmlinkage long sys_setpriority(int which, int who, int niceval)
667 struct task_struct *g, *p;
668 struct user_struct *user;
669 int error = -EINVAL;
670 struct pid *pgrp;
672 if (which > PRIO_USER || which < PRIO_PROCESS)
673 goto out;
675 /* normalize: avoid signed division (rounding problems) */
676 error = -ESRCH;
677 if (niceval < -20)
678 niceval = -20;
679 if (niceval > 19)
680 niceval = 19;
682 read_lock(&tasklist_lock);
683 switch (which) {
684 case PRIO_PROCESS:
685 if (who)
686 p = find_task_by_pid(who);
687 else
688 p = current;
689 if (p)
690 error = set_one_prio(p, niceval, error);
691 break;
692 case PRIO_PGRP:
693 if (who)
694 pgrp = find_pid(who);
695 else
696 pgrp = task_pgrp(current);
697 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
698 error = set_one_prio(p, niceval, error);
699 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
700 break;
701 case PRIO_USER:
702 user = current->user;
703 if (!who)
704 who = current->uid;
705 else
706 if ((who != current->uid) && !(user = find_user(who)))
707 goto out_unlock; /* No processes for this user */
709 do_each_thread(g, p)
710 if (p->uid == who)
711 error = set_one_prio(p, niceval, error);
712 while_each_thread(g, p);
713 if (who != current->uid)
714 free_uid(user); /* For find_user() */
715 break;
717 out_unlock:
718 read_unlock(&tasklist_lock);
719 out:
720 return error;
724 * Ugh. To avoid negative return values, "getpriority()" will
725 * not return the normal nice-value, but a negated value that
726 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
727 * to stay compatible.
729 asmlinkage long sys_getpriority(int which, int who)
731 struct task_struct *g, *p;
732 struct user_struct *user;
733 long niceval, retval = -ESRCH;
734 struct pid *pgrp;
736 if (which > PRIO_USER || which < PRIO_PROCESS)
737 return -EINVAL;
739 read_lock(&tasklist_lock);
740 switch (which) {
741 case PRIO_PROCESS:
742 if (who)
743 p = find_task_by_pid(who);
744 else
745 p = current;
746 if (p) {
747 niceval = 20 - task_nice(p);
748 if (niceval > retval)
749 retval = niceval;
751 break;
752 case PRIO_PGRP:
753 if (who)
754 pgrp = find_pid(who);
755 else
756 pgrp = task_pgrp(current);
757 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
758 niceval = 20 - task_nice(p);
759 if (niceval > retval)
760 retval = niceval;
761 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
762 break;
763 case PRIO_USER:
764 user = current->user;
765 if (!who)
766 who = current->uid;
767 else
768 if ((who != current->uid) && !(user = find_user(who)))
769 goto out_unlock; /* No processes for this user */
771 do_each_thread(g, p)
772 if (p->uid == who) {
773 niceval = 20 - task_nice(p);
774 if (niceval > retval)
775 retval = niceval;
777 while_each_thread(g, p);
778 if (who != current->uid)
779 free_uid(user); /* for find_user() */
780 break;
782 out_unlock:
783 read_unlock(&tasklist_lock);
785 return retval;
789 * emergency_restart - reboot the system
791 * Without shutting down any hardware or taking any locks
792 * reboot the system. This is called when we know we are in
793 * trouble so this is our best effort to reboot. This is
794 * safe to call in interrupt context.
796 void emergency_restart(void)
798 machine_emergency_restart();
800 EXPORT_SYMBOL_GPL(emergency_restart);
802 static void kernel_restart_prepare(char *cmd)
804 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
805 system_state = SYSTEM_RESTART;
806 device_shutdown();
810 * kernel_restart - reboot the system
811 * @cmd: pointer to buffer containing command to execute for restart
812 * or %NULL
814 * Shutdown everything and perform a clean reboot.
815 * This is not safe to call in interrupt context.
817 void kernel_restart(char *cmd)
819 kernel_restart_prepare(cmd);
820 if (!cmd)
821 printk(KERN_EMERG "Restarting system.\n");
822 else
823 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
824 machine_restart(cmd);
826 EXPORT_SYMBOL_GPL(kernel_restart);
829 * kernel_kexec - reboot the system
831 * Move into place and start executing a preloaded standalone
832 * executable. If nothing was preloaded return an error.
834 static void kernel_kexec(void)
836 #ifdef CONFIG_KEXEC
837 struct kimage *image;
838 image = xchg(&kexec_image, NULL);
839 if (!image)
840 return;
841 kernel_restart_prepare(NULL);
842 printk(KERN_EMERG "Starting new kernel\n");
843 machine_shutdown();
844 machine_kexec(image);
845 #endif
848 void kernel_shutdown_prepare(enum system_states state)
850 blocking_notifier_call_chain(&reboot_notifier_list,
851 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
852 system_state = state;
853 device_shutdown();
856 * kernel_halt - halt the system
858 * Shutdown everything and perform a clean system halt.
860 void kernel_halt(void)
862 kernel_shutdown_prepare(SYSTEM_HALT);
863 printk(KERN_EMERG "System halted.\n");
864 machine_halt();
867 EXPORT_SYMBOL_GPL(kernel_halt);
870 * kernel_power_off - power_off the system
872 * Shutdown everything and perform a clean system power_off.
874 void kernel_power_off(void)
876 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
877 if (pm_power_off_prepare)
878 pm_power_off_prepare();
879 printk(KERN_EMERG "Power down.\n");
880 machine_power_off();
882 EXPORT_SYMBOL_GPL(kernel_power_off);
884 * Reboot system call: for obvious reasons only root may call it,
885 * and even root needs to set up some magic numbers in the registers
886 * so that some mistake won't make this reboot the whole machine.
887 * You can also set the meaning of the ctrl-alt-del-key here.
889 * reboot doesn't sync: do that yourself before calling this.
891 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
893 char buffer[256];
895 /* We only trust the superuser with rebooting the system. */
896 if (!capable(CAP_SYS_BOOT))
897 return -EPERM;
899 /* For safety, we require "magic" arguments. */
900 if (magic1 != LINUX_REBOOT_MAGIC1 ||
901 (magic2 != LINUX_REBOOT_MAGIC2 &&
902 magic2 != LINUX_REBOOT_MAGIC2A &&
903 magic2 != LINUX_REBOOT_MAGIC2B &&
904 magic2 != LINUX_REBOOT_MAGIC2C))
905 return -EINVAL;
907 /* Instead of trying to make the power_off code look like
908 * halt when pm_power_off is not set do it the easy way.
910 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
911 cmd = LINUX_REBOOT_CMD_HALT;
913 lock_kernel();
914 switch (cmd) {
915 case LINUX_REBOOT_CMD_RESTART:
916 kernel_restart(NULL);
917 break;
919 case LINUX_REBOOT_CMD_CAD_ON:
920 C_A_D = 1;
921 break;
923 case LINUX_REBOOT_CMD_CAD_OFF:
924 C_A_D = 0;
925 break;
927 case LINUX_REBOOT_CMD_HALT:
928 kernel_halt();
929 unlock_kernel();
930 do_exit(0);
931 break;
933 case LINUX_REBOOT_CMD_POWER_OFF:
934 kernel_power_off();
935 unlock_kernel();
936 do_exit(0);
937 break;
939 case LINUX_REBOOT_CMD_RESTART2:
940 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
941 unlock_kernel();
942 return -EFAULT;
944 buffer[sizeof(buffer) - 1] = '\0';
946 kernel_restart(buffer);
947 break;
949 case LINUX_REBOOT_CMD_KEXEC:
950 kernel_kexec();
951 unlock_kernel();
952 return -EINVAL;
954 #ifdef CONFIG_SOFTWARE_SUSPEND
955 case LINUX_REBOOT_CMD_SW_SUSPEND:
957 int ret = hibernate();
958 unlock_kernel();
959 return ret;
961 #endif
963 default:
964 unlock_kernel();
965 return -EINVAL;
967 unlock_kernel();
968 return 0;
971 static void deferred_cad(struct work_struct *dummy)
973 kernel_restart(NULL);
977 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
978 * As it's called within an interrupt, it may NOT sync: the only choice
979 * is whether to reboot at once, or just ignore the ctrl-alt-del.
981 void ctrl_alt_del(void)
983 static DECLARE_WORK(cad_work, deferred_cad);
985 if (C_A_D)
986 schedule_work(&cad_work);
987 else
988 kill_cad_pid(SIGINT, 1);
992 * Unprivileged users may change the real gid to the effective gid
993 * or vice versa. (BSD-style)
995 * If you set the real gid at all, or set the effective gid to a value not
996 * equal to the real gid, then the saved gid is set to the new effective gid.
998 * This makes it possible for a setgid program to completely drop its
999 * privileges, which is often a useful assertion to make when you are doing
1000 * a security audit over a program.
1002 * The general idea is that a program which uses just setregid() will be
1003 * 100% compatible with BSD. A program which uses just setgid() will be
1004 * 100% compatible with POSIX with saved IDs.
1006 * SMP: There are not races, the GIDs are checked only by filesystem
1007 * operations (as far as semantic preservation is concerned).
1009 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
1011 int old_rgid = current->gid;
1012 int old_egid = current->egid;
1013 int new_rgid = old_rgid;
1014 int new_egid = old_egid;
1015 int retval;
1017 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
1018 if (retval)
1019 return retval;
1021 if (rgid != (gid_t) -1) {
1022 if ((old_rgid == rgid) ||
1023 (current->egid==rgid) ||
1024 capable(CAP_SETGID))
1025 new_rgid = rgid;
1026 else
1027 return -EPERM;
1029 if (egid != (gid_t) -1) {
1030 if ((old_rgid == egid) ||
1031 (current->egid == egid) ||
1032 (current->sgid == egid) ||
1033 capable(CAP_SETGID))
1034 new_egid = egid;
1035 else
1036 return -EPERM;
1038 if (new_egid != old_egid) {
1039 set_dumpable(current->mm, suid_dumpable);
1040 smp_wmb();
1042 if (rgid != (gid_t) -1 ||
1043 (egid != (gid_t) -1 && egid != old_rgid))
1044 current->sgid = new_egid;
1045 current->fsgid = new_egid;
1046 current->egid = new_egid;
1047 current->gid = new_rgid;
1048 key_fsgid_changed(current);
1049 proc_id_connector(current, PROC_EVENT_GID);
1050 return 0;
1054 * setgid() is implemented like SysV w/ SAVED_IDS
1056 * SMP: Same implicit races as above.
1058 asmlinkage long sys_setgid(gid_t gid)
1060 int old_egid = current->egid;
1061 int retval;
1063 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
1064 if (retval)
1065 return retval;
1067 if (capable(CAP_SETGID)) {
1068 if (old_egid != gid) {
1069 set_dumpable(current->mm, suid_dumpable);
1070 smp_wmb();
1072 current->gid = current->egid = current->sgid = current->fsgid = gid;
1073 } else if ((gid == current->gid) || (gid == current->sgid)) {
1074 if (old_egid != gid) {
1075 set_dumpable(current->mm, suid_dumpable);
1076 smp_wmb();
1078 current->egid = current->fsgid = gid;
1080 else
1081 return -EPERM;
1083 key_fsgid_changed(current);
1084 proc_id_connector(current, PROC_EVENT_GID);
1085 return 0;
1088 static int set_user(uid_t new_ruid, int dumpclear)
1090 struct user_struct *new_user;
1092 new_user = alloc_uid(current->nsproxy->user_ns, new_ruid);
1093 if (!new_user)
1094 return -EAGAIN;
1096 if (atomic_read(&new_user->processes) >=
1097 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
1098 new_user != current->nsproxy->user_ns->root_user) {
1099 free_uid(new_user);
1100 return -EAGAIN;
1103 switch_uid(new_user);
1105 if (dumpclear) {
1106 set_dumpable(current->mm, suid_dumpable);
1107 smp_wmb();
1109 current->uid = new_ruid;
1110 return 0;
1114 * Unprivileged users may change the real uid to the effective uid
1115 * or vice versa. (BSD-style)
1117 * If you set the real uid at all, or set the effective uid to a value not
1118 * equal to the real uid, then the saved uid is set to the new effective uid.
1120 * This makes it possible for a setuid program to completely drop its
1121 * privileges, which is often a useful assertion to make when you are doing
1122 * a security audit over a program.
1124 * The general idea is that a program which uses just setreuid() will be
1125 * 100% compatible with BSD. A program which uses just setuid() will be
1126 * 100% compatible with POSIX with saved IDs.
1128 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
1130 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
1131 int retval;
1133 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
1134 if (retval)
1135 return retval;
1137 new_ruid = old_ruid = current->uid;
1138 new_euid = old_euid = current->euid;
1139 old_suid = current->suid;
1141 if (ruid != (uid_t) -1) {
1142 new_ruid = ruid;
1143 if ((old_ruid != ruid) &&
1144 (current->euid != ruid) &&
1145 !capable(CAP_SETUID))
1146 return -EPERM;
1149 if (euid != (uid_t) -1) {
1150 new_euid = euid;
1151 if ((old_ruid != euid) &&
1152 (current->euid != euid) &&
1153 (current->suid != euid) &&
1154 !capable(CAP_SETUID))
1155 return -EPERM;
1158 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
1159 return -EAGAIN;
1161 if (new_euid != old_euid) {
1162 set_dumpable(current->mm, suid_dumpable);
1163 smp_wmb();
1165 current->fsuid = current->euid = new_euid;
1166 if (ruid != (uid_t) -1 ||
1167 (euid != (uid_t) -1 && euid != old_ruid))
1168 current->suid = current->euid;
1169 current->fsuid = current->euid;
1171 key_fsuid_changed(current);
1172 proc_id_connector(current, PROC_EVENT_UID);
1174 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
1180 * setuid() is implemented like SysV with SAVED_IDS
1182 * Note that SAVED_ID's is deficient in that a setuid root program
1183 * like sendmail, for example, cannot set its uid to be a normal
1184 * user and then switch back, because if you're root, setuid() sets
1185 * the saved uid too. If you don't like this, blame the bright people
1186 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
1187 * will allow a root program to temporarily drop privileges and be able to
1188 * regain them by swapping the real and effective uid.
1190 asmlinkage long sys_setuid(uid_t uid)
1192 int old_euid = current->euid;
1193 int old_ruid, old_suid, new_suid;
1194 int retval;
1196 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
1197 if (retval)
1198 return retval;
1200 old_ruid = current->uid;
1201 old_suid = current->suid;
1202 new_suid = old_suid;
1204 if (capable(CAP_SETUID)) {
1205 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1206 return -EAGAIN;
1207 new_suid = uid;
1208 } else if ((uid != current->uid) && (uid != new_suid))
1209 return -EPERM;
1211 if (old_euid != uid) {
1212 set_dumpable(current->mm, suid_dumpable);
1213 smp_wmb();
1215 current->fsuid = current->euid = uid;
1216 current->suid = new_suid;
1218 key_fsuid_changed(current);
1219 proc_id_connector(current, PROC_EVENT_UID);
1221 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1226 * This function implements a generic ability to update ruid, euid,
1227 * and suid. This allows you to implement the 4.4 compatible seteuid().
1229 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1231 int old_ruid = current->uid;
1232 int old_euid = current->euid;
1233 int old_suid = current->suid;
1234 int retval;
1236 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1237 if (retval)
1238 return retval;
1240 if (!capable(CAP_SETUID)) {
1241 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1242 (ruid != current->euid) && (ruid != current->suid))
1243 return -EPERM;
1244 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1245 (euid != current->euid) && (euid != current->suid))
1246 return -EPERM;
1247 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1248 (suid != current->euid) && (suid != current->suid))
1249 return -EPERM;
1251 if (ruid != (uid_t) -1) {
1252 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1253 return -EAGAIN;
1255 if (euid != (uid_t) -1) {
1256 if (euid != current->euid) {
1257 set_dumpable(current->mm, suid_dumpable);
1258 smp_wmb();
1260 current->euid = euid;
1262 current->fsuid = current->euid;
1263 if (suid != (uid_t) -1)
1264 current->suid = suid;
1266 key_fsuid_changed(current);
1267 proc_id_connector(current, PROC_EVENT_UID);
1269 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1272 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1274 int retval;
1276 if (!(retval = put_user(current->uid, ruid)) &&
1277 !(retval = put_user(current->euid, euid)))
1278 retval = put_user(current->suid, suid);
1280 return retval;
1284 * Same as above, but for rgid, egid, sgid.
1286 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1288 int retval;
1290 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1291 if (retval)
1292 return retval;
1294 if (!capable(CAP_SETGID)) {
1295 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1296 (rgid != current->egid) && (rgid != current->sgid))
1297 return -EPERM;
1298 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1299 (egid != current->egid) && (egid != current->sgid))
1300 return -EPERM;
1301 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1302 (sgid != current->egid) && (sgid != current->sgid))
1303 return -EPERM;
1305 if (egid != (gid_t) -1) {
1306 if (egid != current->egid) {
1307 set_dumpable(current->mm, suid_dumpable);
1308 smp_wmb();
1310 current->egid = egid;
1312 current->fsgid = current->egid;
1313 if (rgid != (gid_t) -1)
1314 current->gid = rgid;
1315 if (sgid != (gid_t) -1)
1316 current->sgid = sgid;
1318 key_fsgid_changed(current);
1319 proc_id_connector(current, PROC_EVENT_GID);
1320 return 0;
1323 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1325 int retval;
1327 if (!(retval = put_user(current->gid, rgid)) &&
1328 !(retval = put_user(current->egid, egid)))
1329 retval = put_user(current->sgid, sgid);
1331 return retval;
1336 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1337 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1338 * whatever uid it wants to). It normally shadows "euid", except when
1339 * explicitly set by setfsuid() or for access..
1341 asmlinkage long sys_setfsuid(uid_t uid)
1343 int old_fsuid;
1345 old_fsuid = current->fsuid;
1346 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1347 return old_fsuid;
1349 if (uid == current->uid || uid == current->euid ||
1350 uid == current->suid || uid == current->fsuid ||
1351 capable(CAP_SETUID)) {
1352 if (uid != old_fsuid) {
1353 set_dumpable(current->mm, suid_dumpable);
1354 smp_wmb();
1356 current->fsuid = uid;
1359 key_fsuid_changed(current);
1360 proc_id_connector(current, PROC_EVENT_UID);
1362 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1364 return old_fsuid;
1368 * Samma på svenska..
1370 asmlinkage long sys_setfsgid(gid_t gid)
1372 int old_fsgid;
1374 old_fsgid = current->fsgid;
1375 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1376 return old_fsgid;
1378 if (gid == current->gid || gid == current->egid ||
1379 gid == current->sgid || gid == current->fsgid ||
1380 capable(CAP_SETGID)) {
1381 if (gid != old_fsgid) {
1382 set_dumpable(current->mm, suid_dumpable);
1383 smp_wmb();
1385 current->fsgid = gid;
1386 key_fsgid_changed(current);
1387 proc_id_connector(current, PROC_EVENT_GID);
1389 return old_fsgid;
1392 asmlinkage long sys_times(struct tms __user * tbuf)
1395 * In the SMP world we might just be unlucky and have one of
1396 * the times increment as we use it. Since the value is an
1397 * atomically safe type this is just fine. Conceptually its
1398 * as if the syscall took an instant longer to occur.
1400 if (tbuf) {
1401 struct tms tmp;
1402 struct task_struct *tsk = current;
1403 struct task_struct *t;
1404 cputime_t utime, stime, cutime, cstime;
1406 spin_lock_irq(&tsk->sighand->siglock);
1407 utime = tsk->signal->utime;
1408 stime = tsk->signal->stime;
1409 t = tsk;
1410 do {
1411 utime = cputime_add(utime, t->utime);
1412 stime = cputime_add(stime, t->stime);
1413 t = next_thread(t);
1414 } while (t != tsk);
1416 cutime = tsk->signal->cutime;
1417 cstime = tsk->signal->cstime;
1418 spin_unlock_irq(&tsk->sighand->siglock);
1420 tmp.tms_utime = cputime_to_clock_t(utime);
1421 tmp.tms_stime = cputime_to_clock_t(stime);
1422 tmp.tms_cutime = cputime_to_clock_t(cutime);
1423 tmp.tms_cstime = cputime_to_clock_t(cstime);
1424 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1425 return -EFAULT;
1427 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1431 * This needs some heavy checking ...
1432 * I just haven't the stomach for it. I also don't fully
1433 * understand sessions/pgrp etc. Let somebody who does explain it.
1435 * OK, I think I have the protection semantics right.... this is really
1436 * only important on a multi-user system anyway, to make sure one user
1437 * can't send a signal to a process owned by another. -TYT, 12/12/91
1439 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1440 * LBT 04.03.94
1443 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1445 struct task_struct *p;
1446 struct task_struct *group_leader = current->group_leader;
1447 int err = -EINVAL;
1449 if (!pid)
1450 pid = group_leader->pid;
1451 if (!pgid)
1452 pgid = pid;
1453 if (pgid < 0)
1454 return -EINVAL;
1456 /* From this point forward we keep holding onto the tasklist lock
1457 * so that our parent does not change from under us. -DaveM
1459 write_lock_irq(&tasklist_lock);
1461 err = -ESRCH;
1462 p = find_task_by_pid(pid);
1463 if (!p)
1464 goto out;
1466 err = -EINVAL;
1467 if (!thread_group_leader(p))
1468 goto out;
1470 if (p->real_parent == group_leader) {
1471 err = -EPERM;
1472 if (task_session(p) != task_session(group_leader))
1473 goto out;
1474 err = -EACCES;
1475 if (p->did_exec)
1476 goto out;
1477 } else {
1478 err = -ESRCH;
1479 if (p != group_leader)
1480 goto out;
1483 err = -EPERM;
1484 if (p->signal->leader)
1485 goto out;
1487 if (pgid != pid) {
1488 struct task_struct *g =
1489 find_task_by_pid_type(PIDTYPE_PGID, pgid);
1491 if (!g || task_session(g) != task_session(group_leader))
1492 goto out;
1495 err = security_task_setpgid(p, pgid);
1496 if (err)
1497 goto out;
1499 if (process_group(p) != pgid) {
1500 detach_pid(p, PIDTYPE_PGID);
1501 p->signal->pgrp = pgid;
1502 attach_pid(p, PIDTYPE_PGID, find_pid(pgid));
1505 err = 0;
1506 out:
1507 /* All paths lead to here, thus we are safe. -DaveM */
1508 write_unlock_irq(&tasklist_lock);
1509 return err;
1512 asmlinkage long sys_getpgid(pid_t pid)
1514 if (!pid)
1515 return process_group(current);
1516 else {
1517 int retval;
1518 struct task_struct *p;
1520 read_lock(&tasklist_lock);
1521 p = find_task_by_pid(pid);
1523 retval = -ESRCH;
1524 if (p) {
1525 retval = security_task_getpgid(p);
1526 if (!retval)
1527 retval = process_group(p);
1529 read_unlock(&tasklist_lock);
1530 return retval;
1534 #ifdef __ARCH_WANT_SYS_GETPGRP
1536 asmlinkage long sys_getpgrp(void)
1538 /* SMP - assuming writes are word atomic this is fine */
1539 return process_group(current);
1542 #endif
1544 asmlinkage long sys_getsid(pid_t pid)
1546 if (!pid)
1547 return process_session(current);
1548 else {
1549 int retval;
1550 struct task_struct *p;
1552 read_lock(&tasklist_lock);
1553 p = find_task_by_pid(pid);
1555 retval = -ESRCH;
1556 if (p) {
1557 retval = security_task_getsid(p);
1558 if (!retval)
1559 retval = process_session(p);
1561 read_unlock(&tasklist_lock);
1562 return retval;
1566 asmlinkage long sys_setsid(void)
1568 struct task_struct *group_leader = current->group_leader;
1569 pid_t session;
1570 int err = -EPERM;
1572 write_lock_irq(&tasklist_lock);
1574 /* Fail if I am already a session leader */
1575 if (group_leader->signal->leader)
1576 goto out;
1578 session = group_leader->pid;
1579 /* Fail if a process group id already exists that equals the
1580 * proposed session id.
1582 * Don't check if session id == 1 because kernel threads use this
1583 * session id and so the check will always fail and make it so
1584 * init cannot successfully call setsid.
1586 if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
1587 goto out;
1589 group_leader->signal->leader = 1;
1590 __set_special_pids(session, session);
1592 spin_lock(&group_leader->sighand->siglock);
1593 group_leader->signal->tty = NULL;
1594 spin_unlock(&group_leader->sighand->siglock);
1596 err = process_group(group_leader);
1597 out:
1598 write_unlock_irq(&tasklist_lock);
1599 return err;
1603 * Supplementary group IDs
1606 /* init to 2 - one for init_task, one to ensure it is never freed */
1607 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1609 struct group_info *groups_alloc(int gidsetsize)
1611 struct group_info *group_info;
1612 int nblocks;
1613 int i;
1615 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1616 /* Make sure we always allocate at least one indirect block pointer */
1617 nblocks = nblocks ? : 1;
1618 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1619 if (!group_info)
1620 return NULL;
1621 group_info->ngroups = gidsetsize;
1622 group_info->nblocks = nblocks;
1623 atomic_set(&group_info->usage, 1);
1625 if (gidsetsize <= NGROUPS_SMALL)
1626 group_info->blocks[0] = group_info->small_block;
1627 else {
1628 for (i = 0; i < nblocks; i++) {
1629 gid_t *b;
1630 b = (void *)__get_free_page(GFP_USER);
1631 if (!b)
1632 goto out_undo_partial_alloc;
1633 group_info->blocks[i] = b;
1636 return group_info;
1638 out_undo_partial_alloc:
1639 while (--i >= 0) {
1640 free_page((unsigned long)group_info->blocks[i]);
1642 kfree(group_info);
1643 return NULL;
1646 EXPORT_SYMBOL(groups_alloc);
1648 void groups_free(struct group_info *group_info)
1650 if (group_info->blocks[0] != group_info->small_block) {
1651 int i;
1652 for (i = 0; i < group_info->nblocks; i++)
1653 free_page((unsigned long)group_info->blocks[i]);
1655 kfree(group_info);
1658 EXPORT_SYMBOL(groups_free);
1660 /* export the group_info to a user-space array */
1661 static int groups_to_user(gid_t __user *grouplist,
1662 struct group_info *group_info)
1664 int i;
1665 int count = group_info->ngroups;
1667 for (i = 0; i < group_info->nblocks; i++) {
1668 int cp_count = min(NGROUPS_PER_BLOCK, count);
1669 int off = i * NGROUPS_PER_BLOCK;
1670 int len = cp_count * sizeof(*grouplist);
1672 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1673 return -EFAULT;
1675 count -= cp_count;
1677 return 0;
1680 /* fill a group_info from a user-space array - it must be allocated already */
1681 static int groups_from_user(struct group_info *group_info,
1682 gid_t __user *grouplist)
1684 int i;
1685 int count = group_info->ngroups;
1687 for (i = 0; i < group_info->nblocks; i++) {
1688 int cp_count = min(NGROUPS_PER_BLOCK, count);
1689 int off = i * NGROUPS_PER_BLOCK;
1690 int len = cp_count * sizeof(*grouplist);
1692 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1693 return -EFAULT;
1695 count -= cp_count;
1697 return 0;
1700 /* a simple Shell sort */
1701 static void groups_sort(struct group_info *group_info)
1703 int base, max, stride;
1704 int gidsetsize = group_info->ngroups;
1706 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1707 ; /* nothing */
1708 stride /= 3;
1710 while (stride) {
1711 max = gidsetsize - stride;
1712 for (base = 0; base < max; base++) {
1713 int left = base;
1714 int right = left + stride;
1715 gid_t tmp = GROUP_AT(group_info, right);
1717 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1718 GROUP_AT(group_info, right) =
1719 GROUP_AT(group_info, left);
1720 right = left;
1721 left -= stride;
1723 GROUP_AT(group_info, right) = tmp;
1725 stride /= 3;
1729 /* a simple bsearch */
1730 int groups_search(struct group_info *group_info, gid_t grp)
1732 unsigned int left, right;
1734 if (!group_info)
1735 return 0;
1737 left = 0;
1738 right = group_info->ngroups;
1739 while (left < right) {
1740 unsigned int mid = (left+right)/2;
1741 int cmp = grp - GROUP_AT(group_info, mid);
1742 if (cmp > 0)
1743 left = mid + 1;
1744 else if (cmp < 0)
1745 right = mid;
1746 else
1747 return 1;
1749 return 0;
1752 /* validate and set current->group_info */
1753 int set_current_groups(struct group_info *group_info)
1755 int retval;
1756 struct group_info *old_info;
1758 retval = security_task_setgroups(group_info);
1759 if (retval)
1760 return retval;
1762 groups_sort(group_info);
1763 get_group_info(group_info);
1765 task_lock(current);
1766 old_info = current->group_info;
1767 current->group_info = group_info;
1768 task_unlock(current);
1770 put_group_info(old_info);
1772 return 0;
1775 EXPORT_SYMBOL(set_current_groups);
1777 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1779 int i = 0;
1782 * SMP: Nobody else can change our grouplist. Thus we are
1783 * safe.
1786 if (gidsetsize < 0)
1787 return -EINVAL;
1789 /* no need to grab task_lock here; it cannot change */
1790 i = current->group_info->ngroups;
1791 if (gidsetsize) {
1792 if (i > gidsetsize) {
1793 i = -EINVAL;
1794 goto out;
1796 if (groups_to_user(grouplist, current->group_info)) {
1797 i = -EFAULT;
1798 goto out;
1801 out:
1802 return i;
1806 * SMP: Our groups are copy-on-write. We can set them safely
1807 * without another task interfering.
1810 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1812 struct group_info *group_info;
1813 int retval;
1815 if (!capable(CAP_SETGID))
1816 return -EPERM;
1817 if ((unsigned)gidsetsize > NGROUPS_MAX)
1818 return -EINVAL;
1820 group_info = groups_alloc(gidsetsize);
1821 if (!group_info)
1822 return -ENOMEM;
1823 retval = groups_from_user(group_info, grouplist);
1824 if (retval) {
1825 put_group_info(group_info);
1826 return retval;
1829 retval = set_current_groups(group_info);
1830 put_group_info(group_info);
1832 return retval;
1836 * Check whether we're fsgid/egid or in the supplemental group..
1838 int in_group_p(gid_t grp)
1840 int retval = 1;
1841 if (grp != current->fsgid)
1842 retval = groups_search(current->group_info, grp);
1843 return retval;
1846 EXPORT_SYMBOL(in_group_p);
1848 int in_egroup_p(gid_t grp)
1850 int retval = 1;
1851 if (grp != current->egid)
1852 retval = groups_search(current->group_info, grp);
1853 return retval;
1856 EXPORT_SYMBOL(in_egroup_p);
1858 DECLARE_RWSEM(uts_sem);
1860 EXPORT_SYMBOL(uts_sem);
1862 asmlinkage long sys_newuname(struct new_utsname __user * name)
1864 int errno = 0;
1866 down_read(&uts_sem);
1867 if (copy_to_user(name, utsname(), sizeof *name))
1868 errno = -EFAULT;
1869 up_read(&uts_sem);
1870 return errno;
1873 asmlinkage long sys_sethostname(char __user *name, int len)
1875 int errno;
1876 char tmp[__NEW_UTS_LEN];
1878 if (!capable(CAP_SYS_ADMIN))
1879 return -EPERM;
1880 if (len < 0 || len > __NEW_UTS_LEN)
1881 return -EINVAL;
1882 down_write(&uts_sem);
1883 errno = -EFAULT;
1884 if (!copy_from_user(tmp, name, len)) {
1885 memcpy(utsname()->nodename, tmp, len);
1886 utsname()->nodename[len] = 0;
1887 errno = 0;
1889 up_write(&uts_sem);
1890 return errno;
1893 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1895 asmlinkage long sys_gethostname(char __user *name, int len)
1897 int i, errno;
1899 if (len < 0)
1900 return -EINVAL;
1901 down_read(&uts_sem);
1902 i = 1 + strlen(utsname()->nodename);
1903 if (i > len)
1904 i = len;
1905 errno = 0;
1906 if (copy_to_user(name, utsname()->nodename, i))
1907 errno = -EFAULT;
1908 up_read(&uts_sem);
1909 return errno;
1912 #endif
1915 * Only setdomainname; getdomainname can be implemented by calling
1916 * uname()
1918 asmlinkage long sys_setdomainname(char __user *name, int len)
1920 int errno;
1921 char tmp[__NEW_UTS_LEN];
1923 if (!capable(CAP_SYS_ADMIN))
1924 return -EPERM;
1925 if (len < 0 || len > __NEW_UTS_LEN)
1926 return -EINVAL;
1928 down_write(&uts_sem);
1929 errno = -EFAULT;
1930 if (!copy_from_user(tmp, name, len)) {
1931 memcpy(utsname()->domainname, tmp, len);
1932 utsname()->domainname[len] = 0;
1933 errno = 0;
1935 up_write(&uts_sem);
1936 return errno;
1939 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1941 if (resource >= RLIM_NLIMITS)
1942 return -EINVAL;
1943 else {
1944 struct rlimit value;
1945 task_lock(current->group_leader);
1946 value = current->signal->rlim[resource];
1947 task_unlock(current->group_leader);
1948 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1952 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1955 * Back compatibility for getrlimit. Needed for some apps.
1958 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1960 struct rlimit x;
1961 if (resource >= RLIM_NLIMITS)
1962 return -EINVAL;
1964 task_lock(current->group_leader);
1965 x = current->signal->rlim[resource];
1966 task_unlock(current->group_leader);
1967 if (x.rlim_cur > 0x7FFFFFFF)
1968 x.rlim_cur = 0x7FFFFFFF;
1969 if (x.rlim_max > 0x7FFFFFFF)
1970 x.rlim_max = 0x7FFFFFFF;
1971 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1974 #endif
1976 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1978 struct rlimit new_rlim, *old_rlim;
1979 unsigned long it_prof_secs;
1980 int retval;
1982 if (resource >= RLIM_NLIMITS)
1983 return -EINVAL;
1984 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1985 return -EFAULT;
1986 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1987 return -EINVAL;
1988 old_rlim = current->signal->rlim + resource;
1989 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1990 !capable(CAP_SYS_RESOURCE))
1991 return -EPERM;
1992 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1993 return -EPERM;
1995 retval = security_task_setrlimit(resource, &new_rlim);
1996 if (retval)
1997 return retval;
1999 if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
2001 * The caller is asking for an immediate RLIMIT_CPU
2002 * expiry. But we use the zero value to mean "it was
2003 * never set". So let's cheat and make it one second
2004 * instead
2006 new_rlim.rlim_cur = 1;
2009 task_lock(current->group_leader);
2010 *old_rlim = new_rlim;
2011 task_unlock(current->group_leader);
2013 if (resource != RLIMIT_CPU)
2014 goto out;
2017 * RLIMIT_CPU handling. Note that the kernel fails to return an error
2018 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
2019 * very long-standing error, and fixing it now risks breakage of
2020 * applications, so we live with it
2022 if (new_rlim.rlim_cur == RLIM_INFINITY)
2023 goto out;
2025 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
2026 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
2027 unsigned long rlim_cur = new_rlim.rlim_cur;
2028 cputime_t cputime;
2030 cputime = secs_to_cputime(rlim_cur);
2031 read_lock(&tasklist_lock);
2032 spin_lock_irq(&current->sighand->siglock);
2033 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
2034 spin_unlock_irq(&current->sighand->siglock);
2035 read_unlock(&tasklist_lock);
2037 out:
2038 return 0;
2042 * It would make sense to put struct rusage in the task_struct,
2043 * except that would make the task_struct be *really big*. After
2044 * task_struct gets moved into malloc'ed memory, it would
2045 * make sense to do this. It will make moving the rest of the information
2046 * a lot simpler! (Which we're not doing right now because we're not
2047 * measuring them yet).
2049 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
2050 * races with threads incrementing their own counters. But since word
2051 * reads are atomic, we either get new values or old values and we don't
2052 * care which for the sums. We always take the siglock to protect reading
2053 * the c* fields from p->signal from races with exit.c updating those
2054 * fields when reaping, so a sample either gets all the additions of a
2055 * given child after it's reaped, or none so this sample is before reaping.
2057 * Locking:
2058 * We need to take the siglock for CHILDEREN, SELF and BOTH
2059 * for the cases current multithreaded, non-current single threaded
2060 * non-current multithreaded. Thread traversal is now safe with
2061 * the siglock held.
2062 * Strictly speaking, we donot need to take the siglock if we are current and
2063 * single threaded, as no one else can take our signal_struct away, no one
2064 * else can reap the children to update signal->c* counters, and no one else
2065 * can race with the signal-> fields. If we do not take any lock, the
2066 * signal-> fields could be read out of order while another thread was just
2067 * exiting. So we should place a read memory barrier when we avoid the lock.
2068 * On the writer side, write memory barrier is implied in __exit_signal
2069 * as __exit_signal releases the siglock spinlock after updating the signal->
2070 * fields. But we don't do this yet to keep things simple.
2074 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
2076 struct task_struct *t;
2077 unsigned long flags;
2078 cputime_t utime, stime;
2080 memset((char *) r, 0, sizeof *r);
2081 utime = stime = cputime_zero;
2083 rcu_read_lock();
2084 if (!lock_task_sighand(p, &flags)) {
2085 rcu_read_unlock();
2086 return;
2089 switch (who) {
2090 case RUSAGE_BOTH:
2091 case RUSAGE_CHILDREN:
2092 utime = p->signal->cutime;
2093 stime = p->signal->cstime;
2094 r->ru_nvcsw = p->signal->cnvcsw;
2095 r->ru_nivcsw = p->signal->cnivcsw;
2096 r->ru_minflt = p->signal->cmin_flt;
2097 r->ru_majflt = p->signal->cmaj_flt;
2098 r->ru_inblock = p->signal->cinblock;
2099 r->ru_oublock = p->signal->coublock;
2101 if (who == RUSAGE_CHILDREN)
2102 break;
2104 case RUSAGE_SELF:
2105 utime = cputime_add(utime, p->signal->utime);
2106 stime = cputime_add(stime, p->signal->stime);
2107 r->ru_nvcsw += p->signal->nvcsw;
2108 r->ru_nivcsw += p->signal->nivcsw;
2109 r->ru_minflt += p->signal->min_flt;
2110 r->ru_majflt += p->signal->maj_flt;
2111 r->ru_inblock += p->signal->inblock;
2112 r->ru_oublock += p->signal->oublock;
2113 t = p;
2114 do {
2115 utime = cputime_add(utime, t->utime);
2116 stime = cputime_add(stime, t->stime);
2117 r->ru_nvcsw += t->nvcsw;
2118 r->ru_nivcsw += t->nivcsw;
2119 r->ru_minflt += t->min_flt;
2120 r->ru_majflt += t->maj_flt;
2121 r->ru_inblock += task_io_get_inblock(t);
2122 r->ru_oublock += task_io_get_oublock(t);
2123 t = next_thread(t);
2124 } while (t != p);
2125 break;
2127 default:
2128 BUG();
2131 unlock_task_sighand(p, &flags);
2132 rcu_read_unlock();
2134 cputime_to_timeval(utime, &r->ru_utime);
2135 cputime_to_timeval(stime, &r->ru_stime);
2138 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
2140 struct rusage r;
2141 k_getrusage(p, who, &r);
2142 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
2145 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
2147 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
2148 return -EINVAL;
2149 return getrusage(current, who, ru);
2152 asmlinkage long sys_umask(int mask)
2154 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
2155 return mask;
2158 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
2159 unsigned long arg4, unsigned long arg5)
2161 long error;
2163 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2164 if (error)
2165 return error;
2167 switch (option) {
2168 case PR_SET_PDEATHSIG:
2169 if (!valid_signal(arg2)) {
2170 error = -EINVAL;
2171 break;
2173 current->pdeath_signal = arg2;
2174 break;
2175 case PR_GET_PDEATHSIG:
2176 error = put_user(current->pdeath_signal, (int __user *)arg2);
2177 break;
2178 case PR_GET_DUMPABLE:
2179 error = get_dumpable(current->mm);
2180 break;
2181 case PR_SET_DUMPABLE:
2182 if (arg2 < 0 || arg2 > 1) {
2183 error = -EINVAL;
2184 break;
2186 set_dumpable(current->mm, arg2);
2187 break;
2189 case PR_SET_UNALIGN:
2190 error = SET_UNALIGN_CTL(current, arg2);
2191 break;
2192 case PR_GET_UNALIGN:
2193 error = GET_UNALIGN_CTL(current, arg2);
2194 break;
2195 case PR_SET_FPEMU:
2196 error = SET_FPEMU_CTL(current, arg2);
2197 break;
2198 case PR_GET_FPEMU:
2199 error = GET_FPEMU_CTL(current, arg2);
2200 break;
2201 case PR_SET_FPEXC:
2202 error = SET_FPEXC_CTL(current, arg2);
2203 break;
2204 case PR_GET_FPEXC:
2205 error = GET_FPEXC_CTL(current, arg2);
2206 break;
2207 case PR_GET_TIMING:
2208 error = PR_TIMING_STATISTICAL;
2209 break;
2210 case PR_SET_TIMING:
2211 if (arg2 == PR_TIMING_STATISTICAL)
2212 error = 0;
2213 else
2214 error = -EINVAL;
2215 break;
2217 case PR_GET_KEEPCAPS:
2218 if (current->keep_capabilities)
2219 error = 1;
2220 break;
2221 case PR_SET_KEEPCAPS:
2222 if (arg2 != 0 && arg2 != 1) {
2223 error = -EINVAL;
2224 break;
2226 current->keep_capabilities = arg2;
2227 break;
2228 case PR_SET_NAME: {
2229 struct task_struct *me = current;
2230 unsigned char ncomm[sizeof(me->comm)];
2232 ncomm[sizeof(me->comm)-1] = 0;
2233 if (strncpy_from_user(ncomm, (char __user *)arg2,
2234 sizeof(me->comm)-1) < 0)
2235 return -EFAULT;
2236 set_task_comm(me, ncomm);
2237 return 0;
2239 case PR_GET_NAME: {
2240 struct task_struct *me = current;
2241 unsigned char tcomm[sizeof(me->comm)];
2243 get_task_comm(tcomm, me);
2244 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2245 return -EFAULT;
2246 return 0;
2248 case PR_GET_ENDIAN:
2249 error = GET_ENDIAN(current, arg2);
2250 break;
2251 case PR_SET_ENDIAN:
2252 error = SET_ENDIAN(current, arg2);
2253 break;
2255 case PR_GET_SECCOMP:
2256 error = prctl_get_seccomp();
2257 break;
2258 case PR_SET_SECCOMP:
2259 error = prctl_set_seccomp(arg2);
2260 break;
2262 default:
2263 error = -EINVAL;
2264 break;
2266 return error;
2269 asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2270 struct getcpu_cache __user *cache)
2272 int err = 0;
2273 int cpu = raw_smp_processor_id();
2274 if (cpup)
2275 err |= put_user(cpu, cpup);
2276 if (nodep)
2277 err |= put_user(cpu_to_node(cpu), nodep);
2278 if (cache) {
2280 * The cache is not needed for this implementation,
2281 * but make sure user programs pass something
2282 * valid. vsyscall implementations can instead make
2283 * good use of the cache. Only use t0 and t1 because
2284 * these are available in both 32bit and 64bit ABI (no
2285 * need for a compat_getcpu). 32bit has enough
2286 * padding
2288 unsigned long t0, t1;
2289 get_user(t0, &cache->blob[0]);
2290 get_user(t1, &cache->blob[1]);
2291 t0++;
2292 t1++;
2293 put_user(t0, &cache->blob[0]);
2294 put_user(t1, &cache->blob[1]);
2296 return err ? -EFAULT : 0;
2299 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
2301 static void argv_cleanup(char **argv, char **envp)
2303 argv_free(argv);
2307 * orderly_poweroff - Trigger an orderly system poweroff
2308 * @force: force poweroff if command execution fails
2310 * This may be called from any context to trigger a system shutdown.
2311 * If the orderly shutdown fails, it will force an immediate shutdown.
2313 int orderly_poweroff(bool force)
2315 int argc;
2316 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
2317 static char *envp[] = {
2318 "HOME=/",
2319 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
2320 NULL
2322 int ret = -ENOMEM;
2323 struct subprocess_info *info;
2325 if (argv == NULL) {
2326 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
2327 __func__, poweroff_cmd);
2328 goto out;
2331 info = call_usermodehelper_setup(argv[0], argv, envp);
2332 if (info == NULL) {
2333 argv_free(argv);
2334 goto out;
2337 call_usermodehelper_setcleanup(info, argv_cleanup);
2339 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
2341 out:
2342 if (ret && force) {
2343 printk(KERN_WARNING "Failed to start orderly shutdown: "
2344 "forcing the issue\n");
2346 /* I guess this should try to kick off some daemon to
2347 sync and poweroff asap. Or not even bother syncing
2348 if we're doing an emergency shutdown? */
2349 emergency_sync();
2350 kernel_power_off();
2353 return ret;
2355 EXPORT_SYMBOL_GPL(orderly_poweroff);