4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/module.h>
9 #include <linux/utsname.h>
10 #include <linux/mman.h>
11 #include <linux/smp_lock.h>
12 #include <linux/notifier.h>
13 #include <linux/reboot.h>
14 #include <linux/prctl.h>
15 #include <linux/highuid.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>
39 #include <asm/unistd.h>
41 #ifndef SET_UNALIGN_CTL
42 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
44 #ifndef GET_UNALIGN_CTL
45 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
48 # define SET_FPEMU_CTL(a,b) (-EINVAL)
51 # define GET_FPEMU_CTL(a,b) (-EINVAL)
54 # define SET_FPEXC_CTL(a,b) (-EINVAL)
57 # define GET_FPEXC_CTL(a,b) (-EINVAL)
60 # define GET_ENDIAN(a,b) (-EINVAL)
63 # define SET_ENDIAN(a,b) (-EINVAL)
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;
75 EXPORT_SYMBOL(overflowuid);
76 EXPORT_SYMBOL(overflowgid);
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
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
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)
120 rcu_assign_pointer(*nl, n);
124 static int notifier_chain_unregister(struct notifier_block **nl,
125 struct notifier_block *n)
127 while ((*nl) != NULL) {
129 rcu_assign_pointer(*nl, n->next);
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);
166 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
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)
195 spin_lock_irqsave(&nh->lock, flags);
196 ret = notifier_chain_register(&nh->head, n);
197 spin_unlock_irqrestore(&nh->lock, flags);
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)
218 spin_lock_irqsave(&nh->lock, flags);
219 ret = notifier_chain_unregister(&nh->head, n);
220 spin_unlock_irqrestore(&nh->lock, flags);
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)
254 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
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)
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);
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)
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);
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,
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)
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);
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)
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);
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)
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);
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)
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
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)
633 if (p->uid != current->euid &&
634 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
638 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
642 no_nice = security_task_setnice(p, niceval);
649 set_user_nice(p, niceval);
654 asmlinkage long sys_setpriority(int which, int who, int niceval)
656 struct task_struct *g, *p;
657 struct user_struct *user;
661 if (which > 2 || which < 0)
664 /* normalize: avoid signed division (rounding problems) */
671 read_lock(&tasklist_lock);
675 p = find_task_by_pid(who);
679 error = set_one_prio(p, niceval, error);
683 pgrp = find_pid(who);
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);
691 user = current->user;
695 if ((who != current->uid) && !(user = find_user(who)))
696 goto out_unlock; /* No processes for this user */
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() */
707 read_unlock(&tasklist_lock);
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;
725 if (which > 2 || which < 0)
728 read_lock(&tasklist_lock);
732 p = find_task_by_pid(who);
736 niceval = 20 - task_nice(p);
737 if (niceval > retval)
743 pgrp = find_pid(who);
745 pgrp = task_pgrp(current);
746 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
747 niceval = 20 - task_nice(p);
748 if (niceval > retval)
750 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
753 user = current->user;
757 if ((who != current->uid) && !(user = find_user(who)))
758 goto out_unlock; /* No processes for this user */
762 niceval = 20 - task_nice(p);
763 if (niceval > retval)
766 while_each_thread(g, p);
767 if (who != current->uid)
768 free_uid(user); /* for find_user() */
772 read_unlock(&tasklist_lock);
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;
799 * kernel_restart - reboot the system
800 * @cmd: pointer to buffer containing command to execute for restart
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);
810 printk(KERN_EMERG "Restarting system.\n");
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)
826 struct kimage *image;
827 image = xchg(&kexec_image, NULL);
830 kernel_restart_prepare(NULL);
831 printk(KERN_EMERG "Starting new kernel\n");
833 machine_kexec(image);
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;
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");
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");
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)
882 /* We only trust the superuser with rebooting the system. */
883 if (!capable(CAP_SYS_BOOT))
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))
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;
902 case LINUX_REBOOT_CMD_RESTART:
903 kernel_restart(NULL);
906 case LINUX_REBOOT_CMD_CAD_ON:
910 case LINUX_REBOOT_CMD_CAD_OFF:
914 case LINUX_REBOOT_CMD_HALT:
920 case LINUX_REBOOT_CMD_POWER_OFF:
926 case LINUX_REBOOT_CMD_RESTART2:
927 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
931 buffer[sizeof(buffer) - 1] = '\0';
933 kernel_restart(buffer);
936 case LINUX_REBOOT_CMD_KEXEC:
941 #ifdef CONFIG_SOFTWARE_SUSPEND
942 case LINUX_REBOOT_CMD_SW_SUSPEND:
944 int ret = hibernate();
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);
973 schedule_work(&cad_work);
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;
1004 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
1008 if (rgid != (gid_t) -1) {
1009 if ((old_rgid == rgid) ||
1010 (current->egid==rgid) ||
1011 capable(CAP_SETGID))
1016 if (egid != (gid_t) -1) {
1017 if ((old_rgid == egid) ||
1018 (current->egid == egid) ||
1019 (current->sgid == egid) ||
1020 capable(CAP_SETGID))
1025 if (new_egid != old_egid) {
1026 current->mm->dumpable = suid_dumpable;
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);
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;
1050 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
1054 if (capable(CAP_SETGID)) {
1055 if (old_egid != gid) {
1056 current->mm->dumpable = suid_dumpable;
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;
1065 current->egid = current->fsgid = gid;
1070 key_fsgid_changed(current);
1071 proc_id_connector(current, PROC_EVENT_GID);
1075 static int set_user(uid_t new_ruid, int dumpclear)
1077 struct user_struct *new_user;
1079 new_user = alloc_uid(new_ruid);
1083 if (atomic_read(&new_user->processes) >=
1084 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
1085 new_user != &root_user) {
1090 switch_uid(new_user);
1093 current->mm->dumpable = suid_dumpable;
1096 current->uid = new_ruid;
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;
1120 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
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) {
1130 if ((old_ruid != ruid) &&
1131 (current->euid != ruid) &&
1132 !capable(CAP_SETUID))
1136 if (euid != (uid_t) -1) {
1138 if ((old_ruid != euid) &&
1139 (current->euid != euid) &&
1140 (current->suid != euid) &&
1141 !capable(CAP_SETUID))
1145 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
1148 if (new_euid != old_euid) {
1149 current->mm->dumpable = suid_dumpable;
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;
1183 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
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)
1195 } else if ((uid != current->uid) && (uid != new_suid))
1198 if (old_euid != uid) {
1199 current->mm->dumpable = suid_dumpable;
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;
1223 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1227 if (!capable(CAP_SETUID)) {
1228 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1229 (ruid != current->euid) && (ruid != current->suid))
1231 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1232 (euid != current->euid) && (euid != current->suid))
1234 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1235 (suid != current->euid) && (suid != current->suid))
1238 if (ruid != (uid_t) -1) {
1239 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1242 if (euid != (uid_t) -1) {
1243 if (euid != current->euid) {
1244 current->mm->dumpable = suid_dumpable;
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)
1263 if (!(retval = put_user(current->uid, ruid)) &&
1264 !(retval = put_user(current->euid, euid)))
1265 retval = put_user(current->suid, suid);
1271 * Same as above, but for rgid, egid, sgid.
1273 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1277 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1281 if (!capable(CAP_SETGID)) {
1282 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1283 (rgid != current->egid) && (rgid != current->sgid))
1285 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1286 (egid != current->egid) && (egid != current->sgid))
1288 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1289 (sgid != current->egid) && (sgid != current->sgid))
1292 if (egid != (gid_t) -1) {
1293 if (egid != current->egid) {
1294 current->mm->dumpable = suid_dumpable;
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);
1310 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1314 if (!(retval = put_user(current->gid, rgid)) &&
1315 !(retval = put_user(current->egid, egid)))
1316 retval = put_user(current->sgid, sgid);
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)
1332 old_fsuid = current->fsuid;
1333 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
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;
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);
1355 * Samma på svenska..
1357 asmlinkage long sys_setfsgid(gid_t gid)
1361 old_fsgid = current->fsgid;
1362 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
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;
1372 current->fsgid = gid;
1373 key_fsgid_changed(current);
1374 proc_id_connector(current, PROC_EVENT_GID);
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.
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;
1398 utime = cputime_add(utime, t->utime);
1399 stime = cputime_add(stime, t->stime);
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)))
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.
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;
1437 pid = group_leader->pid;
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);
1449 p = find_task_by_pid(pid);
1454 if (!thread_group_leader(p))
1457 if (p->real_parent == group_leader) {
1459 if (task_session(p) != task_session(group_leader))
1466 if (p != group_leader)
1471 if (p->signal->leader)
1475 struct task_struct *g =
1476 find_task_by_pid_type(PIDTYPE_PGID, pgid);
1478 if (!g || task_session(g) != task_session(group_leader))
1482 err = security_task_setpgid(p, pgid);
1486 if (process_group(p) != pgid) {
1487 detach_pid(p, PIDTYPE_PGID);
1488 p->signal->pgrp = pgid;
1489 attach_pid(p, PIDTYPE_PGID, pgid);
1494 /* All paths lead to here, thus we are safe. -DaveM */
1495 write_unlock_irq(&tasklist_lock);
1499 asmlinkage long sys_getpgid(pid_t pid)
1502 return process_group(current);
1505 struct task_struct *p;
1507 read_lock(&tasklist_lock);
1508 p = find_task_by_pid(pid);
1512 retval = security_task_getpgid(p);
1514 retval = process_group(p);
1516 read_unlock(&tasklist_lock);
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);
1531 asmlinkage long sys_getsid(pid_t pid)
1534 return process_session(current);
1537 struct task_struct *p;
1539 read_lock(&tasklist_lock);
1540 p = find_task_by_pid(pid);
1544 retval = security_task_getsid(p);
1546 retval = process_session(p);
1548 read_unlock(&tasklist_lock);
1553 asmlinkage long sys_setsid(void)
1555 struct task_struct *group_leader = current->group_leader;
1559 write_lock_irq(&tasklist_lock);
1561 /* Fail if I am already a session leader */
1562 if (group_leader->signal->leader)
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))
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);
1585 write_unlock_irq(&tasklist_lock);
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;
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);
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;
1615 for (i = 0; i < nblocks; i++) {
1617 b = (void *)__get_free_page(GFP_USER);
1619 goto out_undo_partial_alloc;
1620 group_info->blocks[i] = b;
1625 out_undo_partial_alloc:
1627 free_page((unsigned long)group_info->blocks[i]);
1633 EXPORT_SYMBOL(groups_alloc);
1635 void groups_free(struct group_info *group_info)
1637 if (group_info->blocks[0] != group_info->small_block) {
1639 for (i = 0; i < group_info->nblocks; i++)
1640 free_page((unsigned long)group_info->blocks[i]);
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)
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))
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)
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))
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)
1698 max = gidsetsize - stride;
1699 for (base = 0; base < max; 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);
1710 GROUP_AT(group_info, right) = tmp;
1716 /* a simple bsearch */
1717 int groups_search(struct group_info *group_info, gid_t grp)
1719 unsigned int left, right;
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);
1739 /* validate and set current->group_info */
1740 int set_current_groups(struct group_info *group_info)
1743 struct group_info *old_info;
1745 retval = security_task_setgroups(group_info);
1749 groups_sort(group_info);
1750 get_group_info(group_info);
1753 old_info = current->group_info;
1754 current->group_info = group_info;
1755 task_unlock(current);
1757 put_group_info(old_info);
1762 EXPORT_SYMBOL(set_current_groups);
1764 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1769 * SMP: Nobody else can change our grouplist. Thus we are
1776 /* no need to grab task_lock here; it cannot change */
1777 i = current->group_info->ngroups;
1779 if (i > gidsetsize) {
1783 if (groups_to_user(grouplist, current->group_info)) {
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;
1802 if (!capable(CAP_SETGID))
1804 if ((unsigned)gidsetsize > NGROUPS_MAX)
1807 group_info = groups_alloc(gidsetsize);
1810 retval = groups_from_user(group_info, grouplist);
1812 put_group_info(group_info);
1816 retval = set_current_groups(group_info);
1817 put_group_info(group_info);
1823 * Check whether we're fsgid/egid or in the supplemental group..
1825 int in_group_p(gid_t grp)
1828 if (grp != current->fsgid)
1829 retval = groups_search(current->group_info, grp);
1833 EXPORT_SYMBOL(in_group_p);
1835 int in_egroup_p(gid_t grp)
1838 if (grp != current->egid)
1839 retval = groups_search(current->group_info, grp);
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)
1853 down_read(&uts_sem);
1854 if (copy_to_user(name, utsname(), sizeof *name))
1860 asmlinkage long sys_sethostname(char __user *name, int len)
1863 char tmp[__NEW_UTS_LEN];
1865 if (!capable(CAP_SYS_ADMIN))
1867 if (len < 0 || len > __NEW_UTS_LEN)
1869 down_write(&uts_sem);
1871 if (!copy_from_user(tmp, name, len)) {
1872 memcpy(utsname()->nodename, tmp, len);
1873 utsname()->nodename[len] = 0;
1880 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1882 asmlinkage long sys_gethostname(char __user *name, int len)
1888 down_read(&uts_sem);
1889 i = 1 + strlen(utsname()->nodename);
1893 if (copy_to_user(name, utsname()->nodename, i))
1902 * Only setdomainname; getdomainname can be implemented by calling
1905 asmlinkage long sys_setdomainname(char __user *name, int len)
1908 char tmp[__NEW_UTS_LEN];
1910 if (!capable(CAP_SYS_ADMIN))
1912 if (len < 0 || len > __NEW_UTS_LEN)
1915 down_write(&uts_sem);
1917 if (!copy_from_user(tmp, name, len)) {
1918 memcpy(utsname()->domainname, tmp, len);
1919 utsname()->domainname[len] = 0;
1926 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1928 if (resource >= RLIM_NLIMITS)
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)
1948 if (resource >= RLIM_NLIMITS)
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;
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;
1969 if (resource >= RLIM_NLIMITS)
1971 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1973 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1975 old_rlim = current->signal->rlim + resource;
1976 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1977 !capable(CAP_SYS_RESOURCE))
1979 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1982 retval = security_task_setrlimit(resource, &new_rlim);
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
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)
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)
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;
2017 cputime = secs_to_cputime(rlim_cur);
2018 read_lock(&tasklist_lock);
2019 spin_lock_irq(¤t->sighand->siglock);
2020 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
2021 spin_unlock_irq(¤t->sighand->siglock);
2022 read_unlock(&tasklist_lock);
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.
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
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;
2071 if (!lock_task_sighand(p, &flags)) {
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)
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;
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;
2112 unlock_task_sighand(p, &flags);
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)
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)
2130 return getrusage(current, who, ru);
2133 asmlinkage long sys_umask(int mask)
2135 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
2139 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
2140 unsigned long arg4, unsigned long arg5)
2144 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2149 case PR_SET_PDEATHSIG:
2150 if (!valid_signal(arg2)) {
2154 current->pdeath_signal = arg2;
2156 case PR_GET_PDEATHSIG:
2157 error = put_user(current->pdeath_signal, (int __user *)arg2);
2159 case PR_GET_DUMPABLE:
2160 error = current->mm->dumpable;
2162 case PR_SET_DUMPABLE:
2163 if (arg2 < 0 || arg2 > 1) {
2167 current->mm->dumpable = arg2;
2170 case PR_SET_UNALIGN:
2171 error = SET_UNALIGN_CTL(current, arg2);
2173 case PR_GET_UNALIGN:
2174 error = GET_UNALIGN_CTL(current, arg2);
2177 error = SET_FPEMU_CTL(current, arg2);
2180 error = GET_FPEMU_CTL(current, arg2);
2183 error = SET_FPEXC_CTL(current, arg2);
2186 error = GET_FPEXC_CTL(current, arg2);
2189 error = PR_TIMING_STATISTICAL;
2192 if (arg2 == PR_TIMING_STATISTICAL)
2198 case PR_GET_KEEPCAPS:
2199 if (current->keep_capabilities)
2202 case PR_SET_KEEPCAPS:
2203 if (arg2 != 0 && arg2 != 1) {
2207 current->keep_capabilities = arg2;
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)
2217 set_task_comm(me, ncomm);
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)))
2230 error = GET_ENDIAN(current, arg2);
2233 error = SET_ENDIAN(current, arg2);
2243 asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2244 struct getcpu_cache __user *cache)
2247 int cpu = raw_smp_processor_id();
2249 err |= put_user(cpu, cpup);
2251 err |= put_user(cpu_to_node(cpu), nodep);
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
2262 unsigned long t0, t1;
2263 get_user(t0, &cache->blob[0]);
2264 get_user(t1, &cache->blob[1]);
2267 put_user(t0, &cache->blob[0]);
2268 put_user(t1, &cache->blob[1]);
2270 return err ? -EFAULT : 0;