blob: 8b71cef3bf1a3c195c6094b98d8c85f453fbb41a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/sys.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Paul Gortmaker9984de12011-05-23 14:51:41 -04007#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
9#include <linux/utsname.h>
10#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/reboot.h>
12#include <linux/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/highuid.h>
14#include <linux/fs.h>
Paul Gortmaker74da1ff2011-05-26 12:48:41 -040015#include <linux/kmod.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020016#include <linux/perf_event.h>
Daniel Walker3e88c552007-05-10 22:22:53 -070017#include <linux/resource.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070018#include <linux/kernel.h>
19#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070030#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080031#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020032#include <linux/getcpu.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070033#include <linux/task_io_accounting_ops.h>
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -070034#include <linux/seccomp.h>
Mark Lord40477272007-10-01 01:20:10 -070035#include <linux/cpu.h>
Christoph Hellwige28cbf22010-03-10 15:21:19 -080036#include <linux/personality.h>
Paul Mackerrase3d5a272009-01-06 14:41:02 -080037#include <linux/ptrace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040038#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010040#include <linux/syscore_ops.h>
Andi Kleenbe274252011-08-19 16:15:10 -070041#include <linux/version.h>
42#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <linux/compat.h>
45#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080046#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070047#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Seiji Aguchi04c68622011-01-12 16:59:30 -080049#include <linux/kmsg_dump.h>
Andi Kleenbe274252011-08-19 16:15:10 -070050/* Move somewhere else to avoid recompiling? */
51#include <generated/utsrelease.h>
Seiji Aguchi04c68622011-01-12 16:59:30 -080052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/uaccess.h>
54#include <asm/io.h>
55#include <asm/unistd.h>
56
57#ifndef SET_UNALIGN_CTL
58# define SET_UNALIGN_CTL(a,b) (-EINVAL)
59#endif
60#ifndef GET_UNALIGN_CTL
61# define GET_UNALIGN_CTL(a,b) (-EINVAL)
62#endif
63#ifndef SET_FPEMU_CTL
64# define SET_FPEMU_CTL(a,b) (-EINVAL)
65#endif
66#ifndef GET_FPEMU_CTL
67# define GET_FPEMU_CTL(a,b) (-EINVAL)
68#endif
69#ifndef SET_FPEXC_CTL
70# define SET_FPEXC_CTL(a,b) (-EINVAL)
71#endif
72#ifndef GET_FPEXC_CTL
73# define GET_FPEXC_CTL(a,b) (-EINVAL)
74#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100075#ifndef GET_ENDIAN
76# define GET_ENDIAN(a,b) (-EINVAL)
77#endif
78#ifndef SET_ENDIAN
79# define SET_ENDIAN(a,b) (-EINVAL)
80#endif
Erik Bosman8fb402b2008-04-11 18:54:17 +020081#ifndef GET_TSC_CTL
82# define GET_TSC_CTL(a) (-EINVAL)
83#endif
84#ifndef SET_TSC_CTL
85# define SET_TSC_CTL(a) (-EINVAL)
86#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/*
89 * this is where the system-wide overflow UID and GID are defined, for
90 * architectures that now have 32-bit UID/GID but didn't in the past
91 */
92
93int overflowuid = DEFAULT_OVERFLOWUID;
94int overflowgid = DEFAULT_OVERFLOWGID;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096EXPORT_SYMBOL(overflowuid);
97EXPORT_SYMBOL(overflowgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
100 * the same as above, but for filesystems which can only store a 16-bit
101 * UID and GID. as such, this is needed on all architectures
102 */
103
104int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
105int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
106
107EXPORT_SYMBOL(fs_overflowuid);
108EXPORT_SYMBOL(fs_overflowgid);
109
110/*
111 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
112 */
113
114int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700115struct pid *cad_pid;
116EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/*
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700119 * If set, this is used for preparing the system to power off.
120 */
121
122void (*pm_power_off_prepare)(void);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700123
David Howellsc69e8d92008-11-14 10:39:19 +1100124/*
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700125 * Returns true if current's euid is same as p's uid or euid,
126 * or has CAP_SYS_NICE to p's user_ns.
127 *
128 * Called with rcu_read_lock, creds are safe
129 */
130static bool set_one_prio_perm(struct task_struct *p)
131{
132 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
133
Eric W. Biederman5af66202012-03-03 20:21:47 -0800134 if (uid_eq(pcred->uid, cred->euid) ||
135 uid_eq(pcred->euid, cred->euid))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700136 return true;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800137 if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700138 return true;
139 return false;
140}
141
142/*
David Howellsc69e8d92008-11-14 10:39:19 +1100143 * set the priority of a task
144 * - the caller must hold the RCU read lock
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static int set_one_prio(struct task_struct *p, int niceval, int error)
147{
148 int no_nice;
149
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700150 if (!set_one_prio_perm(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 error = -EPERM;
152 goto out;
153 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700154 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 error = -EACCES;
156 goto out;
157 }
158 no_nice = security_task_setnice(p, niceval);
159 if (no_nice) {
160 error = no_nice;
161 goto out;
162 }
163 if (error == -ESRCH)
164 error = 0;
165 set_user_nice(p, niceval);
166out:
167 return error;
168}
169
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100170SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 struct task_struct *g, *p;
173 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100174 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800176 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800177 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Daniel Walker3e88c552007-05-10 22:22:53 -0700179 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto out;
181
182 /* normalize: avoid signed division (rounding problems) */
183 error = -ESRCH;
184 if (niceval < -20)
185 niceval = -20;
186 if (niceval > 19)
187 niceval = 19;
188
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000189 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 read_lock(&tasklist_lock);
191 switch (which) {
192 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800193 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700194 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800195 else
196 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (p)
198 error = set_one_prio(p, niceval, error);
199 break;
200 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800201 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700202 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800203 else
204 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700205 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 error = set_one_prio(p, niceval, error);
Ken Chen2d70b682008-08-20 14:09:17 -0700207 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
209 case PRIO_USER:
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800210 uid = make_kuid(cred->user_ns, who);
Eric W. Biederman74ba5082012-03-03 18:58:11 -0800211 user = cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!who)
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800213 uid = cred->uid;
214 else if (!uid_eq(uid, cred->uid) &&
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800215 !(user = find_user(uid)))
David Howells86a264a2008-11-14 10:39:18 +1100216 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800218 do_each_thread(g, p) {
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800219 if (uid_eq(task_uid(p), uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 error = set_one_prio(p, niceval, error);
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800221 } while_each_thread(g, p);
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800222 if (!uid_eq(uid, cred->uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 free_uid(user); /* For find_user() */
224 break;
225 }
226out_unlock:
227 read_unlock(&tasklist_lock);
Thomas Gleixnerd4581a22009-12-10 00:52:51 +0000228 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229out:
230 return error;
231}
232
233/*
234 * Ugh. To avoid negative return values, "getpriority()" will
235 * not return the normal nice-value, but a negated value that
236 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
237 * to stay compatible.
238 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100239SYSCALL_DEFINE2(getpriority, int, which, int, who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 struct task_struct *g, *p;
242 struct user_struct *user;
David Howells86a264a2008-11-14 10:39:18 +1100243 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800245 struct pid *pgrp;
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800246 kuid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Daniel Walker3e88c552007-05-10 22:22:53 -0700248 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -EINVAL;
250
Tetsuo Handa70118832010-02-22 12:44:16 -0800251 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 read_lock(&tasklist_lock);
253 switch (which) {
254 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800255 if (who)
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700256 p = find_task_by_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800257 else
258 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (p) {
260 niceval = 20 - task_nice(p);
261 if (niceval > retval)
262 retval = niceval;
263 }
264 break;
265 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800266 if (who)
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700267 pgrp = find_vpid(who);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800268 else
269 pgrp = task_pgrp(current);
Ken Chen2d70b682008-08-20 14:09:17 -0700270 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 niceval = 20 - task_nice(p);
272 if (niceval > retval)
273 retval = niceval;
Ken Chen2d70b682008-08-20 14:09:17 -0700274 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276 case PRIO_USER:
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800277 uid = make_kuid(cred->user_ns, who);
Eric W. Biederman74ba5082012-03-03 18:58:11 -0800278 user = cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!who)
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800280 uid = cred->uid;
281 else if (!uid_eq(uid, cred->uid) &&
Eric W. Biederman7b44ab92011-11-16 23:20:58 -0800282 !(user = find_user(uid)))
David Howells86a264a2008-11-14 10:39:18 +1100283 goto out_unlock; /* No processes for this user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800285 do_each_thread(g, p) {
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800286 if (uid_eq(task_uid(p), uid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 niceval = 20 - task_nice(p);
288 if (niceval > retval)
289 retval = niceval;
290 }
H Hartley Sweetendfc6a732009-12-14 18:00:22 -0800291 } while_each_thread(g, p);
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800292 if (!uid_eq(uid, cred->uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 free_uid(user); /* for find_user() */
294 break;
295 }
296out_unlock:
297 read_unlock(&tasklist_lock);
Tetsuo Handa70118832010-02-22 12:44:16 -0800298 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 return retval;
301}
302
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700303/**
304 * emergency_restart - reboot the system
305 *
306 * Without shutting down any hardware or taking any locks
307 * reboot the system. This is called when we know we are in
308 * trouble so this is our best effort to reboot. This is
309 * safe to call in interrupt context.
310 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600311void emergency_restart(void)
312{
Seiji Aguchi04c68622011-01-12 16:59:30 -0800313 kmsg_dump(KMSG_DUMP_EMERG);
Eric W. Biederman7c903472005-07-26 11:29:55 -0600314 machine_emergency_restart();
315}
316EXPORT_SYMBOL_GPL(emergency_restart);
317
Huang Yingca195b72008-08-15 00:40:24 -0700318void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600319{
Alan Sterne041c682006-03-27 01:16:30 -0800320 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600321 system_state = SYSTEM_RESTART;
Kay Sieversb50fa7c2011-05-05 13:32:05 +0200322 usermodehelper_disable();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600323 device_shutdown();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100324 syscore_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700325}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800326
327/**
Amerigo Wangc5f41752011-07-25 17:13:10 -0700328 * register_reboot_notifier - Register function to be called at reboot time
329 * @nb: Info about notifier function to be called
330 *
331 * Registers a function with the list of functions
332 * to be called at reboot time.
333 *
334 * Currently always returns zero, as blocking_notifier_chain_register()
335 * always returns zero.
336 */
337int register_reboot_notifier(struct notifier_block *nb)
338{
339 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
340}
341EXPORT_SYMBOL(register_reboot_notifier);
342
343/**
344 * unregister_reboot_notifier - Unregister previously registered reboot notifier
345 * @nb: Hook to be unregistered
346 *
347 * Unregisters a previously registered reboot
348 * notifier function.
349 *
350 * Returns zero on success, or %-ENOENT on failure.
351 */
352int unregister_reboot_notifier(struct notifier_block *nb)
353{
354 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
355}
356EXPORT_SYMBOL(unregister_reboot_notifier);
357
358/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800359 * kernel_restart - reboot the system
360 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800361 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800362 *
363 * Shutdown everything and perform a clean reboot.
364 * This is not safe to call in interrupt context.
365 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700366void kernel_restart(char *cmd)
367{
368 kernel_restart_prepare(cmd);
Cal Peake756184b2006-09-30 23:27:24 -0700369 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600370 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700371 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600372 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Seiji Aguchi04c68622011-01-12 16:59:30 -0800373 kmsg_dump(KMSG_DUMP_RESTART);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600374 machine_restart(cmd);
375}
376EXPORT_SYMBOL_GPL(kernel_restart);
377
Adrian Bunk4ef72292008-02-04 22:30:06 -0800378static void kernel_shutdown_prepare(enum system_states state)
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500379{
Alan Sterne041c682006-03-27 01:16:30 -0800380 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500381 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
382 system_state = state;
Kay Sieversb50fa7c2011-05-05 13:32:05 +0200383 usermodehelper_disable();
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500384 device_shutdown();
385}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700386/**
387 * kernel_halt - halt the system
388 *
389 * Shutdown everything and perform a clean system halt.
390 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700391void kernel_halt(void)
392{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500393 kernel_shutdown_prepare(SYSTEM_HALT);
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100394 syscore_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600395 printk(KERN_EMERG "System halted.\n");
Seiji Aguchi04c68622011-01-12 16:59:30 -0800396 kmsg_dump(KMSG_DUMP_HALT);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600397 machine_halt();
398}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500399
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600400EXPORT_SYMBOL_GPL(kernel_halt);
401
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700402/**
403 * kernel_power_off - power_off the system
404 *
405 * Shutdown everything and perform a clean system power_off.
406 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700407void kernel_power_off(void)
408{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500409 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Rafael J. Wysockibd804eb2007-07-19 01:47:40 -0700410 if (pm_power_off_prepare)
411 pm_power_off_prepare();
Mark Lord40477272007-10-01 01:20:10 -0700412 disable_nonboot_cpus();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100413 syscore_shutdown();
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600414 printk(KERN_EMERG "Power down.\n");
Seiji Aguchi04c68622011-01-12 16:59:30 -0800415 kmsg_dump(KMSG_DUMP_POWEROFF);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600416 machine_power_off();
417}
418EXPORT_SYMBOL_GPL(kernel_power_off);
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200419
420static DEFINE_MUTEX(reboot_mutex);
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/*
423 * Reboot system call: for obvious reasons only root may call it,
424 * and even root needs to set up some magic numbers in the registers
425 * so that some mistake won't make this reboot the whole machine.
426 * You can also set the meaning of the ctrl-alt-del-key here.
427 *
428 * reboot doesn't sync: do that yourself before calling this.
429 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +0100430SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
431 void __user *, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 char buffer[256];
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700434 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* We only trust the superuser with rebooting the system. */
437 if (!capable(CAP_SYS_BOOT))
438 return -EPERM;
439
440 /* For safety, we require "magic" arguments. */
441 if (magic1 != LINUX_REBOOT_MAGIC1 ||
442 (magic2 != LINUX_REBOOT_MAGIC2 &&
443 magic2 != LINUX_REBOOT_MAGIC2A &&
444 magic2 != LINUX_REBOOT_MAGIC2B &&
445 magic2 != LINUX_REBOOT_MAGIC2C))
446 return -EINVAL;
447
Daniel Lezcanocf3f8922012-03-28 14:42:51 -0700448 /*
449 * If pid namespaces are enabled and the current task is in a child
450 * pid_namespace, the command is handled by reboot_pid_ns() which will
451 * call do_exit().
452 */
453 ret = reboot_pid_ns(task_active_pid_ns(current), cmd);
454 if (ret)
455 return ret;
456
Eric W. Biederman5e382912006-01-08 01:03:46 -0800457 /* Instead of trying to make the power_off code look like
458 * halt when pm_power_off is not set do it the easy way.
459 */
460 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
461 cmd = LINUX_REBOOT_CMD_HALT;
462
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200463 mutex_lock(&reboot_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 switch (cmd) {
465 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600466 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468
469 case LINUX_REBOOT_CMD_CAD_ON:
470 C_A_D = 1;
471 break;
472
473 case LINUX_REBOOT_CMD_CAD_OFF:
474 C_A_D = 0;
475 break;
476
477 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600478 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 do_exit(0);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700480 panic("cannot halt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600483 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 do_exit(0);
485 break;
486
487 case LINUX_REBOOT_CMD_RESTART2:
488 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200489 ret = -EFAULT;
490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 buffer[sizeof(buffer) - 1] = '\0';
493
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600494 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496
Huang Ying3ab83522008-07-25 19:45:07 -0700497#ifdef CONFIG_KEXEC
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700498 case LINUX_REBOOT_CMD_KEXEC:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700499 ret = kernel_kexec();
500 break;
Huang Ying3ab83522008-07-25 19:45:07 -0700501#endif
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600502
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200503#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 case LINUX_REBOOT_CMD_SW_SUSPEND:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700505 ret = hibernate();
506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507#endif
508
509 default:
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700510 ret = -EINVAL;
511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Thomas Gleixner6f15fa52009-10-09 20:31:33 +0200513 mutex_unlock(&reboot_mutex);
Andi Kleen3d26dcf2009-04-13 14:40:08 -0700514 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
David Howells65f27f32006-11-22 14:55:48 +0000517static void deferred_cad(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600519 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/*
523 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
524 * As it's called within an interrupt, it may NOT sync: the only choice
525 * is whether to reboot at once, or just ignore the ctrl-alt-del.
526 */
527void ctrl_alt_del(void)
528{
David Howells65f27f32006-11-22 14:55:48 +0000529 static DECLARE_WORK(cad_work, deferred_cad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (C_A_D)
532 schedule_work(&cad_work);
533 else
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700534 kill_cad_pid(SIGINT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/*
538 * Unprivileged users may change the real gid to the effective gid
539 * or vice versa. (BSD-style)
540 *
541 * If you set the real gid at all, or set the effective gid to a value not
542 * equal to the real gid, then the saved gid is set to the new effective gid.
543 *
544 * This makes it possible for a setgid program to completely drop its
545 * privileges, which is often a useful assertion to make when you are doing
546 * a security audit over a program.
547 *
548 * The general idea is that a program which uses just setregid() will be
549 * 100% compatible with BSD. A program which uses just setgid() will be
550 * 100% compatible with POSIX with saved IDs.
551 *
552 * SMP: There are not races, the GIDs are checked only by filesystem
553 * operations (as far as semantic preservation is concerned).
554 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100555SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800557 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100558 const struct cred *old;
559 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800561 kgid_t krgid, kegid;
562
563 krgid = make_kgid(ns, rgid);
564 kegid = make_kgid(ns, egid);
565
566 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
567 return -EINVAL;
568 if ((egid != (gid_t) -1) && !gid_valid(kegid))
569 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
David Howellsd84f4f92008-11-14 10:39:23 +1100571 new = prepare_creds();
572 if (!new)
573 return -ENOMEM;
574 old = current_cred();
575
David Howellsd84f4f92008-11-14 10:39:23 +1100576 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (rgid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800578 if (gid_eq(old->gid, krgid) ||
579 gid_eq(old->egid, krgid) ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700580 nsown_capable(CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800581 new->gid = krgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 else
David Howellsd84f4f92008-11-14 10:39:23 +1100583 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 if (egid != (gid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800586 if (gid_eq(old->gid, kegid) ||
587 gid_eq(old->egid, kegid) ||
588 gid_eq(old->sgid, kegid) ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700589 nsown_capable(CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800590 new->egid = kegid;
Cal Peake756184b2006-09-30 23:27:24 -0700591 else
David Howellsd84f4f92008-11-14 10:39:23 +1100592 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
David Howellsd84f4f92008-11-14 10:39:23 +1100594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (rgid != (gid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800596 (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100597 new->sgid = new->egid;
598 new->fsgid = new->egid;
599
600 return commit_creds(new);
601
602error:
603 abort_creds(new);
604 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607/*
608 * setgid() is implemented like SysV w/ SAVED_IDS
609 *
610 * SMP: Same implicit races as above.
611 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100612SYSCALL_DEFINE1(setgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800614 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100615 const struct cred *old;
616 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800618 kgid_t kgid;
619
620 kgid = make_kgid(ns, gid);
621 if (!gid_valid(kgid))
622 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
David Howellsd84f4f92008-11-14 10:39:23 +1100624 new = prepare_creds();
625 if (!new)
626 return -ENOMEM;
627 old = current_cred();
628
David Howellsd84f4f92008-11-14 10:39:23 +1100629 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700630 if (nsown_capable(CAP_SETGID))
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800631 new->gid = new->egid = new->sgid = new->fsgid = kgid;
632 else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
633 new->egid = new->fsgid = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 else
David Howellsd84f4f92008-11-14 10:39:23 +1100635 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
David Howellsd84f4f92008-11-14 10:39:23 +1100637 return commit_creds(new);
638
639error:
640 abort_creds(new);
641 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
Dhaval Giani54e99122009-02-27 15:13:54 +0530643
David Howellsd84f4f92008-11-14 10:39:23 +1100644/*
645 * change the user struct in a credentials set to match the new UID
646 */
647static int set_user(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct user_struct *new_user;
650
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800651 new_user = alloc_uid(new->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!new_user)
653 return -EAGAIN;
654
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400655 /*
656 * We don't fail in case of NPROC limit excess here because too many
657 * poorly written programs don't check set*uid() return code, assuming
658 * it never fails if called by root. We may still enforce NPROC limit
659 * for programs doing set*uid()+execve() by harmlessly deferring the
660 * failure to the execve() stage.
661 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800662 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400663 new_user != INIT_USER)
664 current->flags |= PF_NPROC_EXCEEDED;
665 else
666 current->flags &= ~PF_NPROC_EXCEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
David Howellsd84f4f92008-11-14 10:39:23 +1100668 free_uid(new->user);
669 new->user = new_user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 0;
671}
672
673/*
674 * Unprivileged users may change the real uid to the effective uid
675 * or vice versa. (BSD-style)
676 *
677 * If you set the real uid at all, or set the effective uid to a value not
678 * equal to the real uid, then the saved uid is set to the new effective uid.
679 *
680 * This makes it possible for a setuid program to completely drop its
681 * privileges, which is often a useful assertion to make when you are doing
682 * a security audit over a program.
683 *
684 * The general idea is that a program which uses just setreuid() will be
685 * 100% compatible with BSD. A program which uses just setuid() will be
686 * 100% compatible with POSIX with saved IDs.
687 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100688SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800690 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100691 const struct cred *old;
692 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800694 kuid_t kruid, keuid;
695
696 kruid = make_kuid(ns, ruid);
697 keuid = make_kuid(ns, euid);
698
699 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
700 return -EINVAL;
701 if ((euid != (uid_t) -1) && !uid_valid(keuid))
702 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David Howellsd84f4f92008-11-14 10:39:23 +1100704 new = prepare_creds();
705 if (!new)
706 return -ENOMEM;
707 old = current_cred();
708
David Howellsd84f4f92008-11-14 10:39:23 +1100709 retval = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800711 new->uid = kruid;
712 if (!uid_eq(old->uid, kruid) &&
713 !uid_eq(old->euid, kruid) &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700714 !nsown_capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100715 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
718 if (euid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800719 new->euid = keuid;
720 if (!uid_eq(old->uid, keuid) &&
721 !uid_eq(old->euid, keuid) &&
722 !uid_eq(old->suid, keuid) &&
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700723 !nsown_capable(CAP_SETUID))
David Howellsd84f4f92008-11-14 10:39:23 +1100724 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800727 if (!uid_eq(new->uid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530728 retval = set_user(new);
729 if (retval < 0)
730 goto error;
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (ruid != (uid_t) -1 ||
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800733 (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
David Howellsd84f4f92008-11-14 10:39:23 +1100734 new->suid = new->euid;
735 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
David Howellsd84f4f92008-11-14 10:39:23 +1100737 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
738 if (retval < 0)
739 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
David Howellsd84f4f92008-11-14 10:39:23 +1100741 return commit_creds(new);
742
743error:
744 abort_creds(new);
745 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748/*
749 * setuid() is implemented like SysV with SAVED_IDS
750 *
751 * Note that SAVED_ID's is deficient in that a setuid root program
752 * like sendmail, for example, cannot set its uid to be a normal
753 * user and then switch back, because if you're root, setuid() sets
754 * the saved uid too. If you don't like this, blame the bright people
755 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
756 * will allow a root program to temporarily drop privileges and be able to
757 * regain them by swapping the real and effective uid.
758 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100759SYSCALL_DEFINE1(setuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800761 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100762 const struct cred *old;
763 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800765 kuid_t kuid;
766
767 kuid = make_kuid(ns, uid);
768 if (!uid_valid(kuid))
769 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
David Howellsd84f4f92008-11-14 10:39:23 +1100771 new = prepare_creds();
772 if (!new)
773 return -ENOMEM;
774 old = current_cred();
775
David Howellsd84f4f92008-11-14 10:39:23 +1100776 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700777 if (nsown_capable(CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800778 new->suid = new->uid = kuid;
779 if (!uid_eq(kuid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530780 retval = set_user(new);
781 if (retval < 0)
782 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +1100783 }
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800784 } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100785 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800788 new->fsuid = new->euid = kuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
David Howellsd84f4f92008-11-14 10:39:23 +1100790 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
791 if (retval < 0)
792 goto error;
793
794 return commit_creds(new);
795
796error:
797 abort_creds(new);
798 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
801
802/*
803 * This function implements a generic ability to update ruid, euid,
804 * and suid. This allows you to implement the 4.4 compatible seteuid().
805 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100806SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800808 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100809 const struct cred *old;
810 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800812 kuid_t kruid, keuid, ksuid;
813
814 kruid = make_kuid(ns, ruid);
815 keuid = make_kuid(ns, euid);
816 ksuid = make_kuid(ns, suid);
817
818 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
819 return -EINVAL;
820
821 if ((euid != (uid_t) -1) && !uid_valid(keuid))
822 return -EINVAL;
823
824 if ((suid != (uid_t) -1) && !uid_valid(ksuid))
825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
David Howellsd84f4f92008-11-14 10:39:23 +1100827 new = prepare_creds();
828 if (!new)
829 return -ENOMEM;
830
David Howellsd84f4f92008-11-14 10:39:23 +1100831 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
David Howellsd84f4f92008-11-14 10:39:23 +1100833 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700834 if (!nsown_capable(CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800835 if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
836 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100837 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800838 if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
839 !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100840 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800841 if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
842 !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
David Howellsd84f4f92008-11-14 10:39:23 +1100843 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
David Howellsd84f4f92008-11-14 10:39:23 +1100845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (ruid != (uid_t) -1) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800847 new->uid = kruid;
848 if (!uid_eq(kruid, old->uid)) {
Dhaval Giani54e99122009-02-27 15:13:54 +0530849 retval = set_user(new);
850 if (retval < 0)
851 goto error;
852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
David Howellsd84f4f92008-11-14 10:39:23 +1100854 if (euid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800855 new->euid = keuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (suid != (uid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800857 new->suid = ksuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100858 new->fsuid = new->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
David Howellsd84f4f92008-11-14 10:39:23 +1100860 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
861 if (retval < 0)
862 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
David Howellsd84f4f92008-11-14 10:39:23 +1100864 return commit_creds(new);
865
866error:
867 abort_creds(new);
868 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800871SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
David Howells86a264a2008-11-14 10:39:18 +1100873 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800875 uid_t ruid, euid, suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800877 ruid = from_kuid_munged(cred->user_ns, cred->uid);
878 euid = from_kuid_munged(cred->user_ns, cred->euid);
879 suid = from_kuid_munged(cred->user_ns, cred->suid);
880
881 if (!(retval = put_user(ruid, ruidp)) &&
882 !(retval = put_user(euid, euidp)))
883 retval = put_user(suid, suidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 return retval;
886}
887
888/*
889 * Same as above, but for rgid, egid, sgid.
890 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100891SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800893 struct user_namespace *ns = current_user_ns();
David Howellsd84f4f92008-11-14 10:39:23 +1100894 const struct cred *old;
895 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800897 kgid_t krgid, kegid, ksgid;
898
899 krgid = make_kgid(ns, rgid);
900 kegid = make_kgid(ns, egid);
901 ksgid = make_kgid(ns, sgid);
902
903 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
904 return -EINVAL;
905 if ((egid != (gid_t) -1) && !gid_valid(kegid))
906 return -EINVAL;
907 if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
908 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
David Howellsd84f4f92008-11-14 10:39:23 +1100910 new = prepare_creds();
911 if (!new)
912 return -ENOMEM;
913 old = current_cred();
914
David Howellsd84f4f92008-11-14 10:39:23 +1100915 retval = -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700916 if (!nsown_capable(CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800917 if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
918 !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100919 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800920 if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
921 !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100922 goto error;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800923 if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
924 !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100925 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
David Howellsd84f4f92008-11-14 10:39:23 +1100928 if (rgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800929 new->gid = krgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100930 if (egid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800931 new->egid = kegid;
David Howellsd84f4f92008-11-14 10:39:23 +1100932 if (sgid != (gid_t) -1)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800933 new->sgid = ksgid;
David Howellsd84f4f92008-11-14 10:39:23 +1100934 new->fsgid = new->egid;
935
936 return commit_creds(new);
937
938error:
939 abort_creds(new);
940 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800943SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
David Howells86a264a2008-11-14 10:39:18 +1100945 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800947 gid_t rgid, egid, sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800949 rgid = from_kgid_munged(cred->user_ns, cred->gid);
950 egid = from_kgid_munged(cred->user_ns, cred->egid);
951 sgid = from_kgid_munged(cred->user_ns, cred->sgid);
952
953 if (!(retval = put_user(rgid, rgidp)) &&
954 !(retval = put_user(egid, egidp)))
955 retval = put_user(sgid, sgidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 return retval;
958}
959
960
961/*
962 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
963 * is used for "access()" and for the NFS daemon (letting nfsd stay at
964 * whatever uid it wants to). It normally shadows "euid", except when
965 * explicitly set by setfsuid() or for access..
966 */
Heiko Carstensae1251a2009-01-14 14:14:05 +0100967SYSCALL_DEFINE1(setfsuid, uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
David Howellsd84f4f92008-11-14 10:39:23 +1100969 const struct cred *old;
970 struct cred *new;
971 uid_t old_fsuid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800972 kuid_t kuid;
973
974 old = current_cred();
975 old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
976
977 kuid = make_kuid(old->user_ns, uid);
978 if (!uid_valid(kuid))
979 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
David Howellsd84f4f92008-11-14 10:39:23 +1100981 new = prepare_creds();
982 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800983 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800985 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
986 uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -0700987 nsown_capable(CAP_SETUID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800988 if (!uid_eq(kuid, old->fsuid)) {
989 new->fsuid = kuid;
David Howellsd84f4f92008-11-14 10:39:23 +1100990 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
991 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
David Howellsd84f4f92008-11-14 10:39:23 +1100995 abort_creds(new);
996 return old_fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
David Howellsd84f4f92008-11-14 10:39:23 +1100998change_okay:
999 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return old_fsuid;
1001}
1002
1003/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +02001004 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Heiko Carstensae1251a2009-01-14 14:14:05 +01001006SYSCALL_DEFINE1(setfsgid, gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
David Howellsd84f4f92008-11-14 10:39:23 +11001008 const struct cred *old;
1009 struct cred *new;
1010 gid_t old_fsgid;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001011 kgid_t kgid;
1012
1013 old = current_cred();
1014 old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
1015
1016 kgid = make_kgid(old->user_ns, gid);
1017 if (!gid_valid(kgid))
1018 return old_fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
David Howellsd84f4f92008-11-14 10:39:23 +11001020 new = prepare_creds();
1021 if (!new)
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001022 return old_fsgid;
David Howellsd84f4f92008-11-14 10:39:23 +11001023
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001024 if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
1025 gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001026 nsown_capable(CAP_SETGID)) {
Eric W. Biedermana29c33f2012-02-07 18:51:01 -08001027 if (!gid_eq(kgid, old->fsgid)) {
1028 new->fsgid = kgid;
David Howellsd84f4f92008-11-14 10:39:23 +11001029 goto change_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
David Howellsd84f4f92008-11-14 10:39:23 +11001032
David Howellsd84f4f92008-11-14 10:39:23 +11001033 abort_creds(new);
1034 return old_fsgid;
1035
1036change_okay:
1037 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return old_fsgid;
1039}
1040
Frank Mayharf06febc2008-09-12 09:54:39 -07001041void do_sys_times(struct tms *tms)
1042{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001043 cputime_t tgutime, tgstime, cutime, cstime;
Frank Mayharf06febc2008-09-12 09:54:39 -07001044
Oleg Nesterov2b5fe6d2008-11-17 15:40:08 +01001045 spin_lock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001046 thread_group_times(current, &tgutime, &tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001047 cutime = current->signal->cutime;
1048 cstime = current->signal->cstime;
1049 spin_unlock_irq(&current->sighand->siglock);
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001050 tms->tms_utime = cputime_to_clock_t(tgutime);
1051 tms->tms_stime = cputime_to_clock_t(tgstime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001052 tms->tms_cutime = cputime_to_clock_t(cutime);
1053 tms->tms_cstime = cputime_to_clock_t(cstime);
1054}
1055
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001056SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (tbuf) {
1059 struct tms tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Frank Mayharf06febc2008-09-12 09:54:39 -07001061 do_sys_times(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1063 return -EFAULT;
1064 }
Paul Mackerrase3d5a272009-01-06 14:41:02 -08001065 force_successful_syscall_return();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1067}
1068
1069/*
1070 * This needs some heavy checking ...
1071 * I just haven't the stomach for it. I also don't fully
1072 * understand sessions/pgrp etc. Let somebody who does explain it.
1073 *
1074 * OK, I think I have the protection semantics right.... this is really
1075 * only important on a multi-user system anyway, to make sure one user
1076 * can't send a signal to a process owned by another. -TYT, 12/12/91
1077 *
1078 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1079 * LBT 04.03.94
1080 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001081SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001084 struct task_struct *group_leader = current->group_leader;
Oleg Nesterov4e021302008-02-08 04:19:08 -08001085 struct pid *pgrp;
1086 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (!pid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001089 pid = task_pid_vnr(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (!pgid)
1091 pgid = pid;
1092 if (pgid < 0)
1093 return -EINVAL;
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001094 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 /* From this point forward we keep holding onto the tasklist lock
1097 * so that our parent does not change from under us. -DaveM
1098 */
1099 write_lock_irq(&tasklist_lock);
1100
1101 err = -ESRCH;
Oleg Nesterov4e021302008-02-08 04:19:08 -08001102 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (!p)
1104 goto out;
1105
1106 err = -EINVAL;
1107 if (!thread_group_leader(p))
1108 goto out;
1109
Oleg Nesterov4e021302008-02-08 04:19:08 -08001110 if (same_thread_group(p->real_parent, group_leader)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -08001112 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 goto out;
1114 err = -EACCES;
1115 if (p->did_exec)
1116 goto out;
1117 } else {
1118 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001119 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto out;
1121 }
1122
1123 err = -EPERM;
1124 if (p->signal->leader)
1125 goto out;
1126
Oleg Nesterov4e021302008-02-08 04:19:08 -08001127 pgrp = task_pid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (pgid != pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001129 struct task_struct *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Oleg Nesterov4e021302008-02-08 04:19:08 -08001131 pgrp = find_vpid(pgid);
1132 g = pid_task(pgrp, PIDTYPE_PGID);
Eric W. Biederman41487c62007-02-12 00:53:01 -08001133 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001134 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 err = security_task_setpgid(p, pgid);
1138 if (err)
1139 goto out;
1140
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07001141 if (task_pgrp(p) != pgrp)
Oleg Nesterov83beaf32008-04-30 00:54:27 -07001142 change_pid(p, PIDTYPE_PGID, pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 err = 0;
1145out:
1146 /* All paths lead to here, thus we are safe. -DaveM */
1147 write_unlock_irq(&tasklist_lock);
Paul E. McKenney950eaac2010-08-31 17:00:18 -07001148 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return err;
1150}
1151
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001152SYSCALL_DEFINE1(getpgid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001154 struct task_struct *p;
1155 struct pid *grp;
1156 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001158 rcu_read_lock();
1159 if (!pid)
1160 grp = task_pgrp(current);
1161 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 retval = -ESRCH;
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001163 p = find_task_by_vpid(pid);
1164 if (!p)
1165 goto out;
1166 grp = task_pgrp(p);
1167 if (!grp)
1168 goto out;
1169
1170 retval = security_task_getpgid(p);
1171 if (retval)
1172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001174 retval = pid_vnr(grp);
1175out:
1176 rcu_read_unlock();
1177 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180#ifdef __ARCH_WANT_SYS_GETPGRP
1181
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001182SYSCALL_DEFINE0(getpgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Oleg Nesterov12a3de0a2008-04-30 00:54:29 -07001184 return sys_getpgid(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187#endif
1188
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001189SYSCALL_DEFINE1(getsid, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001191 struct task_struct *p;
1192 struct pid *sid;
1193 int retval;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001194
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001195 rcu_read_lock();
1196 if (!pid)
1197 sid = task_session(current);
1198 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 retval = -ESRCH;
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001200 p = find_task_by_vpid(pid);
1201 if (!p)
1202 goto out;
1203 sid = task_session(p);
1204 if (!sid)
1205 goto out;
1206
1207 retval = security_task_getsid(p);
1208 if (retval)
1209 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
Oleg Nesterov1dd768c02008-04-30 00:54:28 -07001211 retval = pid_vnr(sid);
1212out:
1213 rcu_read_unlock();
1214 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Heiko Carstensb290ebe2009-01-14 14:14:06 +01001217SYSCALL_DEFINE0(setsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Oren Laadane19f2472006-01-08 01:03:58 -08001219 struct task_struct *group_leader = current->group_leader;
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001220 struct pid *sid = task_pid(group_leader);
1221 pid_t session = pid_vnr(sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 int err = -EPERM;
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 write_lock_irq(&tasklist_lock);
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001225 /* Fail if I am already a session leader */
1226 if (group_leader->signal->leader)
1227 goto out;
1228
Oleg Nesterov430c6232008-02-08 04:19:11 -08001229 /* Fail if a process group id already exists that equals the
1230 * proposed session id.
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001231 */
Oleg Nesterov6806aac2008-02-08 04:19:12 -08001232 if (pid_task(sid, PIDTYPE_PGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 goto out;
1234
Oren Laadane19f2472006-01-08 01:03:58 -08001235 group_leader->signal->leader = 1;
Oleg Nesterov8520d7c2008-02-08 04:19:09 -08001236 __set_special_pids(sid);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001237
Alan Cox9c9f4de2008-10-13 10:37:26 +01001238 proc_clear_tty(group_leader);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001239
Oleg Nesterove4cc0a92008-02-08 04:19:09 -08001240 err = session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241out:
1242 write_unlock_irq(&tasklist_lock);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001243 if (err > 0) {
Christian Borntraeger0d0df592009-10-26 16:49:34 -07001244 proc_sid_connector(group_leader);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001245 sched_autogroup_create_attach(group_leader);
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return err;
1248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250DECLARE_RWSEM(uts_sem);
1251
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001252#ifdef COMPAT_UTS_MACHINE
1253#define override_architecture(name) \
Andreas Schwab46da2762010-04-23 13:17:44 -04001254 (personality(current->personality) == PER_LINUX32 && \
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001255 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1256 sizeof(COMPAT_UTS_MACHINE)))
1257#else
1258#define override_architecture(name) 0
1259#endif
1260
Andi Kleenbe274252011-08-19 16:15:10 -07001261/*
1262 * Work around broken programs that cannot handle "Linux 3.0".
1263 * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1264 */
1265static int override_release(char __user *release, int len)
1266{
1267 int ret = 0;
Linus Torvaldsa84a79e2011-10-17 08:24:24 -07001268 char buf[65];
Andi Kleenbe274252011-08-19 16:15:10 -07001269
1270 if (current->personality & UNAME26) {
1271 char *rest = UTS_RELEASE;
1272 int ndots = 0;
1273 unsigned v;
1274
1275 while (*rest) {
1276 if (*rest == '.' && ++ndots >= 3)
1277 break;
1278 if (!isdigit(*rest) && *rest != '.')
1279 break;
1280 rest++;
1281 }
1282 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40;
1283 snprintf(buf, len, "2.6.%u%s", v, rest);
1284 ret = copy_to_user(release, buf, len);
1285 }
1286 return ret;
1287}
1288
Heiko Carstense48fbb62009-01-14 14:14:26 +01001289SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 int errno = 0;
1292
1293 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001294 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 errno = -EFAULT;
1296 up_read(&uts_sem);
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001297
Andi Kleenbe274252011-08-19 16:15:10 -07001298 if (!errno && override_release(name->release, sizeof(name->release)))
1299 errno = -EFAULT;
Christoph Hellwige28cbf22010-03-10 15:21:19 -08001300 if (!errno && override_architecture(name))
1301 errno = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return errno;
1303}
1304
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001305#ifdef __ARCH_WANT_SYS_OLD_UNAME
1306/*
1307 * Old cruft
1308 */
1309SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1310{
1311 int error = 0;
1312
1313 if (!name)
1314 return -EFAULT;
1315
1316 down_read(&uts_sem);
1317 if (copy_to_user(name, utsname(), sizeof(*name)))
1318 error = -EFAULT;
1319 up_read(&uts_sem);
1320
Andi Kleenbe274252011-08-19 16:15:10 -07001321 if (!error && override_release(name->release, sizeof(name->release)))
1322 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001323 if (!error && override_architecture(name))
1324 error = -EFAULT;
1325 return error;
1326}
1327
1328SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1329{
1330 int error;
1331
1332 if (!name)
1333 return -EFAULT;
1334 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1335 return -EFAULT;
1336
1337 down_read(&uts_sem);
1338 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1339 __OLD_UTS_LEN);
1340 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1341 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1342 __OLD_UTS_LEN);
1343 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1344 error |= __copy_to_user(&name->release, &utsname()->release,
1345 __OLD_UTS_LEN);
1346 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1347 error |= __copy_to_user(&name->version, &utsname()->version,
1348 __OLD_UTS_LEN);
1349 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1350 error |= __copy_to_user(&name->machine, &utsname()->machine,
1351 __OLD_UTS_LEN);
1352 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1353 up_read(&uts_sem);
1354
1355 if (!error && override_architecture(name))
1356 error = -EFAULT;
Andi Kleenbe274252011-08-19 16:15:10 -07001357 if (!error && override_release(name->release, sizeof(name->release)))
1358 error = -EFAULT;
Christoph Hellwig5cacdb42010-03-10 15:21:21 -08001359 return error ? -EFAULT : 0;
1360}
1361#endif
1362
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001363SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 int errno;
1366 char tmp[__NEW_UTS_LEN];
1367
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -07001368 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EPERM;
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (len < 0 || len > __NEW_UTS_LEN)
1372 return -EINVAL;
1373 down_write(&uts_sem);
1374 errno = -EFAULT;
1375 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001376 struct new_utsname *u = utsname();
1377
1378 memcpy(u->nodename, tmp, len);
1379 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001381 uts_proc_notify(UTS_PROC_HOSTNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383 up_write(&uts_sem);
1384 return errno;
1385}
1386
1387#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1388
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001389SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 int i, errno;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001392 struct new_utsname *u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 if (len < 0)
1395 return -EINVAL;
1396 down_read(&uts_sem);
Andrew Morton9679e4d2008-10-15 22:01:51 -07001397 u = utsname();
1398 i = 1 + strlen(u->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (i > len)
1400 i = len;
1401 errno = 0;
Andrew Morton9679e4d2008-10-15 22:01:51 -07001402 if (copy_to_user(name, u->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 errno = -EFAULT;
1404 up_read(&uts_sem);
1405 return errno;
1406}
1407
1408#endif
1409
1410/*
1411 * Only setdomainname; getdomainname can be implemented by calling
1412 * uname()
1413 */
Heiko Carstens5a8a82b2009-01-14 14:14:25 +01001414SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 int errno;
1417 char tmp[__NEW_UTS_LEN];
1418
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001419 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EPERM;
1421 if (len < 0 || len > __NEW_UTS_LEN)
1422 return -EINVAL;
1423
1424 down_write(&uts_sem);
1425 errno = -EFAULT;
1426 if (!copy_from_user(tmp, name, len)) {
Andrew Morton9679e4d2008-10-15 22:01:51 -07001427 struct new_utsname *u = utsname();
1428
1429 memcpy(u->domainname, tmp, len);
1430 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 errno = 0;
Sasikantha babu499eea62012-05-31 16:26:07 -07001432 uts_proc_notify(UTS_PROC_DOMAINNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 up_write(&uts_sem);
1435 return errno;
1436}
1437
Heiko Carstense48fbb62009-01-14 14:14:26 +01001438SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Jiri Slabyb9518342010-05-04 11:28:25 +02001440 struct rlimit value;
1441 int ret;
1442
1443 ret = do_prlimit(current, resource, NULL, &value);
1444 if (!ret)
1445 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1446
1447 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
1450#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1451
1452/*
1453 * Back compatibility for getrlimit. Needed for some apps.
1454 */
1455
Heiko Carstense48fbb62009-01-14 14:14:26 +01001456SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1457 struct rlimit __user *, rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct rlimit x;
1460 if (resource >= RLIM_NLIMITS)
1461 return -EINVAL;
1462
1463 task_lock(current->group_leader);
1464 x = current->signal->rlim[resource];
1465 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001466 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001468 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 x.rlim_max = 0x7FFFFFFF;
1470 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1471}
1472
1473#endif
1474
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001475static inline bool rlim64_is_infinity(__u64 rlim64)
1476{
1477#if BITS_PER_LONG < 64
1478 return rlim64 >= ULONG_MAX;
1479#else
1480 return rlim64 == RLIM64_INFINITY;
1481#endif
1482}
1483
1484static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1485{
1486 if (rlim->rlim_cur == RLIM_INFINITY)
1487 rlim64->rlim_cur = RLIM64_INFINITY;
1488 else
1489 rlim64->rlim_cur = rlim->rlim_cur;
1490 if (rlim->rlim_max == RLIM_INFINITY)
1491 rlim64->rlim_max = RLIM64_INFINITY;
1492 else
1493 rlim64->rlim_max = rlim->rlim_max;
1494}
1495
1496static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1497{
1498 if (rlim64_is_infinity(rlim64->rlim_cur))
1499 rlim->rlim_cur = RLIM_INFINITY;
1500 else
1501 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1502 if (rlim64_is_infinity(rlim64->rlim_max))
1503 rlim->rlim_max = RLIM_INFINITY;
1504 else
1505 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1506}
1507
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001508/* make sure you are allowed to change @tsk limits before calling this */
Jiri Slaby5b415352010-03-24 16:11:29 +01001509int do_prlimit(struct task_struct *tsk, unsigned int resource,
1510 struct rlimit *new_rlim, struct rlimit *old_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Jiri Slaby5b415352010-03-24 16:11:29 +01001512 struct rlimit *rlim;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001513 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 if (resource >= RLIM_NLIMITS)
1516 return -EINVAL;
Jiri Slaby5b415352010-03-24 16:11:29 +01001517 if (new_rlim) {
1518 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1519 return -EINVAL;
1520 if (resource == RLIMIT_NOFILE &&
1521 new_rlim->rlim_max > sysctl_nr_open)
1522 return -EPERM;
1523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001525 /* protect tsk->signal and tsk->sighand from disappearing */
1526 read_lock(&tasklist_lock);
1527 if (!tsk->sighand) {
1528 retval = -ESRCH;
1529 goto out;
1530 }
1531
Jiri Slaby5b415352010-03-24 16:11:29 +01001532 rlim = tsk->signal->rlim + resource;
Jiri Slaby86f162f2009-11-14 17:37:04 +01001533 task_lock(tsk->group_leader);
Jiri Slaby5b415352010-03-24 16:11:29 +01001534 if (new_rlim) {
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001535 /* Keep the capable check against init_user_ns until
1536 cgroups can contain all limits */
Jiri Slaby5b415352010-03-24 16:11:29 +01001537 if (new_rlim->rlim_max > rlim->rlim_max &&
1538 !capable(CAP_SYS_RESOURCE))
1539 retval = -EPERM;
1540 if (!retval)
1541 retval = security_task_setrlimit(tsk->group_leader,
1542 resource, new_rlim);
1543 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1544 /*
1545 * The caller is asking for an immediate RLIMIT_CPU
1546 * expiry. But we use the zero value to mean "it was
1547 * never set". So let's cheat and make it one second
1548 * instead
1549 */
1550 new_rlim->rlim_cur = 1;
1551 }
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001552 }
Jiri Slaby5b415352010-03-24 16:11:29 +01001553 if (!retval) {
1554 if (old_rlim)
1555 *old_rlim = *rlim;
1556 if (new_rlim)
1557 *rlim = *new_rlim;
1558 }
Jiri Slaby7855c352009-08-26 23:45:34 +02001559 task_unlock(tsk->group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Andrew Mortond3561f72006-03-24 03:18:36 -08001561 /*
1562 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1563 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1564 * very long-standing error, and fixing it now risks breakage of
1565 * applications, so we live with it
1566 */
Jiri Slaby5b415352010-03-24 16:11:29 +01001567 if (!retval && new_rlim && resource == RLIMIT_CPU &&
1568 new_rlim->rlim_cur != RLIM_INFINITY)
1569 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001570out:
Jiri Slaby1c1e6182009-08-28 14:08:17 +02001571 read_unlock(&tasklist_lock);
Oleg Nesterov2fb9d262009-09-03 19:21:45 +02001572 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001575/* rcu lock must be held */
1576static int check_prlimit_permission(struct task_struct *task)
1577{
1578 const struct cred *cred = current_cred(), *tcred;
1579
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001580 if (current == task)
1581 return 0;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001582
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001583 tcred = __task_cred(task);
Eric W. Biederman5af66202012-03-03 20:21:47 -08001584 if (uid_eq(cred->uid, tcred->euid) &&
1585 uid_eq(cred->uid, tcred->suid) &&
1586 uid_eq(cred->uid, tcred->uid) &&
1587 gid_eq(cred->gid, tcred->egid) &&
1588 gid_eq(cred->gid, tcred->sgid) &&
1589 gid_eq(cred->gid, tcred->gid))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001590 return 0;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -08001591 if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
Serge E. Hallynfc832ad2011-03-23 16:43:22 -07001592 return 0;
1593
1594 return -EPERM;
Jiri Slabyc022a0a2010-05-04 18:03:50 +02001595}
1596
1597SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1598 const struct rlimit64 __user *, new_rlim,
1599 struct rlimit64 __user *, old_rlim)
1600{
1601 struct rlimit64 old64, new64;
1602 struct rlimit old, new;
1603 struct task_struct *tsk;
1604 int ret;
1605
1606 if (new_rlim) {
1607 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1608 return -EFAULT;
1609 rlim64_to_rlim(&new64, &new);
1610 }
1611
1612 rcu_read_lock();
1613 tsk = pid ? find_task_by_vpid(pid) : current;
1614 if (!tsk) {
1615 rcu_read_unlock();
1616 return -ESRCH;
1617 }
1618 ret = check_prlimit_permission(tsk);
1619 if (ret) {
1620 rcu_read_unlock();
1621 return ret;
1622 }
1623 get_task_struct(tsk);
1624 rcu_read_unlock();
1625
1626 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1627 old_rlim ? &old : NULL);
1628
1629 if (!ret && old_rlim) {
1630 rlim_to_rlim64(&old, &old64);
1631 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1632 ret = -EFAULT;
1633 }
1634
1635 put_task_struct(tsk);
1636 return ret;
1637}
1638
Jiri Slaby7855c352009-08-26 23:45:34 +02001639SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1640{
1641 struct rlimit new_rlim;
1642
1643 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1644 return -EFAULT;
Jiri Slaby5b415352010-03-24 16:11:29 +01001645 return do_prlimit(current, resource, &new_rlim, NULL);
Jiri Slaby7855c352009-08-26 23:45:34 +02001646}
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648/*
1649 * It would make sense to put struct rusage in the task_struct,
1650 * except that would make the task_struct be *really big*. After
1651 * task_struct gets moved into malloc'ed memory, it would
1652 * make sense to do this. It will make moving the rest of the information
1653 * a lot simpler! (Which we're not doing right now because we're not
1654 * measuring them yet).
1655 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1657 * races with threads incrementing their own counters. But since word
1658 * reads are atomic, we either get new values or old values and we don't
1659 * care which for the sums. We always take the siglock to protect reading
1660 * the c* fields from p->signal from races with exit.c updating those
1661 * fields when reaping, so a sample either gets all the additions of a
1662 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001663 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001664 * Locking:
1665 * We need to take the siglock for CHILDEREN, SELF and BOTH
1666 * for the cases current multithreaded, non-current single threaded
1667 * non-current multithreaded. Thread traversal is now safe with
1668 * the siglock held.
1669 * Strictly speaking, we donot need to take the siglock if we are current and
1670 * single threaded, as no one else can take our signal_struct away, no one
1671 * else can reap the children to update signal->c* counters, and no one else
1672 * can race with the signal-> fields. If we do not take any lock, the
1673 * signal-> fields could be read out of order while another thread was just
1674 * exiting. So we should place a read memory barrier when we avoid the lock.
1675 * On the writer side, write memory barrier is implied in __exit_signal
1676 * as __exit_signal releases the siglock spinlock after updating the signal->
1677 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001678 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 */
1680
Frank Mayharf06febc2008-09-12 09:54:39 -07001681static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001682{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001683 r->ru_nvcsw += t->nvcsw;
1684 r->ru_nivcsw += t->nivcsw;
1685 r->ru_minflt += t->min_flt;
1686 r->ru_majflt += t->maj_flt;
1687 r->ru_inblock += task_io_get_inblock(t);
1688 r->ru_oublock += task_io_get_oublock(t);
1689}
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1692{
1693 struct task_struct *t;
1694 unsigned long flags;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001695 cputime_t tgutime, tgstime, utime, stime;
Jiri Pirko1f102062009-09-22 16:44:10 -07001696 unsigned long maxrss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 memset((char *) r, 0, sizeof *r);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001699 utime = stime = 0;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001700
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001701 if (who == RUSAGE_THREAD) {
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09001702 task_times(current, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001703 accumulate_thread_rusage(p, r);
Jiri Pirko1f102062009-09-22 16:44:10 -07001704 maxrss = p->signal->maxrss;
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001705 goto out;
1706 }
1707
Oleg Nesterovd6cf7232008-04-30 00:52:38 -07001708 if (!lock_task_sighand(p, &flags))
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001709 return;
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001712 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 utime = p->signal->cutime;
1715 stime = p->signal->cstime;
1716 r->ru_nvcsw = p->signal->cnvcsw;
1717 r->ru_nivcsw = p->signal->cnivcsw;
1718 r->ru_minflt = p->signal->cmin_flt;
1719 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001720 r->ru_inblock = p->signal->cinblock;
1721 r->ru_oublock = p->signal->coublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001722 maxrss = p->signal->cmaxrss;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001723
1724 if (who == RUSAGE_CHILDREN)
1725 break;
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 case RUSAGE_SELF:
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09001728 thread_group_times(p, &tgutime, &tgstime);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001729 utime += tgutime;
1730 stime += tgstime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 r->ru_nvcsw += p->signal->nvcsw;
1732 r->ru_nivcsw += p->signal->nivcsw;
1733 r->ru_minflt += p->signal->min_flt;
1734 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07001735 r->ru_inblock += p->signal->inblock;
1736 r->ru_oublock += p->signal->oublock;
Jiri Pirko1f102062009-09-22 16:44:10 -07001737 if (maxrss < p->signal->maxrss)
1738 maxrss = p->signal->maxrss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 t = p;
1740 do {
Frank Mayharf06febc2008-09-12 09:54:39 -07001741 accumulate_thread_rusage(t, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 t = next_thread(t);
1743 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 default:
1747 BUG();
1748 }
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001749 unlock_task_sighand(p, &flags);
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001750
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001751out:
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001752 cputime_to_timeval(utime, &r->ru_utime);
1753 cputime_to_timeval(stime, &r->ru_stime);
Jiri Pirko1f102062009-09-22 16:44:10 -07001754
1755 if (who != RUSAGE_CHILDREN) {
1756 struct mm_struct *mm = get_task_mm(p);
1757 if (mm) {
1758 setmax_mm_hiwater_rss(&maxrss, mm);
1759 mmput(mm);
1760 }
1761 }
1762 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
1765int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1766{
1767 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1770}
1771
Heiko Carstense48fbb62009-01-14 14:14:26 +01001772SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Sripathi Kodi679c9cd2008-04-29 00:58:42 -07001774 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1775 who != RUSAGE_THREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return -EINVAL;
1777 return getrusage(current, who, ru);
1778}
1779
Heiko Carstense48fbb62009-01-14 14:14:26 +01001780SYSCALL_DEFINE1(umask, int, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1783 return mask;
1784}
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -08001785
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001786#ifdef CONFIG_CHECKPOINT_RESTORE
1787static int prctl_set_mm(int opt, unsigned long addr,
1788 unsigned long arg4, unsigned long arg5)
1789{
1790 unsigned long rlim = rlimit(RLIMIT_DATA);
1791 unsigned long vm_req_flags;
1792 unsigned long vm_bad_flags;
1793 struct vm_area_struct *vma;
1794 int error = 0;
1795 struct mm_struct *mm = current->mm;
1796
1797 if (arg4 | arg5)
1798 return -EINVAL;
1799
Cyrill Gorcunov79f07132012-03-15 15:17:10 -07001800 if (!capable(CAP_SYS_RESOURCE))
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08001801 return -EPERM;
1802
1803 if (addr >= TASK_SIZE)
1804 return -EINVAL;
1805
1806 down_read(&mm->mmap_sem);
1807 vma = find_vma(mm, addr);
1808
1809 if (opt != PR_SET_MM_START_BRK && opt != PR_SET_MM_BRK) {
1810 /* It must be existing VMA */
1811 if (!vma || vma->vm_start > addr)
1812 goto out;
1813 }
1814
1815 error = -EINVAL;
1816 switch (opt) {
1817 case PR_SET_MM_START_CODE:
1818 case PR_SET_MM_END_CODE:
1819 vm_req_flags = VM_READ | VM_EXEC;
1820 vm_bad_flags = VM_WRITE | VM_MAYSHARE;
1821
1822 if ((vma->vm_flags & vm_req_flags) != vm_req_flags ||
1823 (vma->vm_flags & vm_bad_flags))
1824 goto out;
1825
1826 if (opt == PR_SET_MM_START_CODE)
1827 mm->start_code = addr;
1828 else
1829 mm->end_code = addr;
1830 break;
1831
1832 case PR_SET_MM_START_DATA:
1833 case PR_SET_MM_END_DATA:
1834 vm_req_flags = VM_READ | VM_WRITE;
1835 vm_bad_flags = VM_EXEC | VM_MAYSHARE;
1836
1837 if ((vma->vm_flags & vm_req_flags) != vm_req_flags ||
1838 (vma->vm_flags & vm_bad_flags))
1839 goto out;
1840
1841 if (opt == PR_SET_MM_START_DATA)
1842 mm->start_data = addr;
1843 else
1844 mm->end_data = addr;
1845 break;
1846
1847 case PR_SET_MM_START_STACK:
1848
1849#ifdef CONFIG_STACK_GROWSUP
1850 vm_req_flags = VM_READ | VM_WRITE | VM_GROWSUP;
1851#else
1852 vm_req_flags = VM_READ | VM_WRITE | VM_GROWSDOWN;
1853#endif
1854 if ((vma->vm_flags & vm_req_flags) != vm_req_flags)
1855 goto out;
1856
1857 mm->start_stack = addr;
1858 break;
1859
1860 case PR_SET_MM_START_BRK:
1861 if (addr <= mm->end_data)
1862 goto out;
1863
1864 if (rlim < RLIM_INFINITY &&
1865 (mm->brk - addr) +
1866 (mm->end_data - mm->start_data) > rlim)
1867 goto out;
1868
1869 mm->start_brk = addr;
1870 break;
1871
1872 case PR_SET_MM_BRK:
1873 if (addr <= mm->end_data)
1874 goto out;
1875
1876 if (rlim < RLIM_INFINITY &&
1877 (addr - mm->start_brk) +
1878 (mm->end_data - mm->start_data) > rlim)
1879 goto out;
1880
1881 mm->brk = addr;
1882 break;
1883
1884 default:
1885 error = -EINVAL;
1886 goto out;
1887 }
1888
1889 error = 0;
1890
1891out:
1892 up_read(&mm->mmap_sem);
1893
1894 return error;
1895}
1896#else /* CONFIG_CHECKPOINT_RESTORE */
1897static int prctl_set_mm(int opt, unsigned long addr,
1898 unsigned long arg4, unsigned long arg5)
1899{
1900 return -EINVAL;
1901}
1902#endif
1903
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001904SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
1905 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
David Howellsb6dff3e2008-11-14 10:39:16 +11001907 struct task_struct *me = current;
1908 unsigned char comm[sizeof(me->comm)];
1909 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
David Howellsd84f4f92008-11-14 10:39:23 +11001911 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1912 if (error != -ENOSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return error;
1914
David Howellsd84f4f92008-11-14 10:39:23 +11001915 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 switch (option) {
1917 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07001918 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 error = -EINVAL;
1920 break;
1921 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001922 me->pdeath_signal = arg2;
1923 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 break;
1925 case PR_GET_PDEATHSIG:
David Howellsb6dff3e2008-11-14 10:39:16 +11001926 error = put_user(me->pdeath_signal, (int __user *)arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 break;
1928 case PR_GET_DUMPABLE:
David Howellsb6dff3e2008-11-14 10:39:16 +11001929 error = get_dumpable(me->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
1931 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02001932 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 error = -EINVAL;
1934 break;
1935 }
David Howellsb6dff3e2008-11-14 10:39:16 +11001936 set_dumpable(me->mm, arg2);
1937 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 break;
1939
1940 case PR_SET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001941 error = SET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 break;
1943 case PR_GET_UNALIGN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001944 error = GET_UNALIGN_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 break;
1946 case PR_SET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001947 error = SET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 break;
1949 case PR_GET_FPEMU:
David Howellsb6dff3e2008-11-14 10:39:16 +11001950 error = GET_FPEMU_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 break;
1952 case PR_SET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001953 error = SET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 break;
1955 case PR_GET_FPEXC:
David Howellsb6dff3e2008-11-14 10:39:16 +11001956 error = GET_FPEXC_CTL(me, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 break;
1958 case PR_GET_TIMING:
1959 error = PR_TIMING_STATISTICAL;
1960 break;
1961 case PR_SET_TIMING:
Shi Weihua7b266552008-05-23 13:04:59 -07001962 if (arg2 != PR_TIMING_STATISTICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 error = -EINVAL;
David Howellsb6dff3e2008-11-14 10:39:16 +11001964 else
1965 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 break;
1967
David Howellsb6dff3e2008-11-14 10:39:16 +11001968 case PR_SET_NAME:
1969 comm[sizeof(me->comm)-1] = 0;
1970 if (strncpy_from_user(comm, (char __user *)arg2,
1971 sizeof(me->comm) - 1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return -EFAULT;
David Howellsb6dff3e2008-11-14 10:39:16 +11001973 set_task_comm(me, comm);
Vladimir Zapolskiyf786ecb2011-09-21 09:26:44 +00001974 proc_comm_connector(me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return 0;
David Howellsb6dff3e2008-11-14 10:39:16 +11001976 case PR_GET_NAME:
1977 get_task_comm(comm, me);
1978 if (copy_to_user((char __user *)arg2, comm,
1979 sizeof(comm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return -EFAULT;
1981 return 0;
Anton Blanchard651d7652006-06-07 16:10:19 +10001982 case PR_GET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001983 error = GET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001984 break;
1985 case PR_SET_ENDIAN:
David Howellsb6dff3e2008-11-14 10:39:16 +11001986 error = SET_ENDIAN(me, arg2);
Anton Blanchard651d7652006-06-07 16:10:19 +10001987 break;
1988
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -07001989 case PR_GET_SECCOMP:
1990 error = prctl_get_seccomp();
1991 break;
1992 case PR_SET_SECCOMP:
Will Drewrye2cfabdf2012-04-12 16:47:57 -05001993 error = prctl_set_seccomp(arg2, (char __user *)arg3);
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -07001994 break;
Erik Bosman8fb402b2008-04-11 18:54:17 +02001995 case PR_GET_TSC:
1996 error = GET_TSC_CTL(arg2);
1997 break;
1998 case PR_SET_TSC:
1999 error = SET_TSC_CTL(arg2);
2000 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002001 case PR_TASK_PERF_EVENTS_DISABLE:
2002 error = perf_event_task_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01002003 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004 case PR_TASK_PERF_EVENTS_ENABLE:
2005 error = perf_event_task_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01002006 break;
Arjan van de Ven69766752008-09-01 15:52:40 -07002007 case PR_GET_TIMERSLACK:
2008 error = current->timer_slack_ns;
2009 break;
2010 case PR_SET_TIMERSLACK:
2011 if (arg2 <= 0)
2012 current->timer_slack_ns =
2013 current->default_timer_slack_ns;
2014 else
2015 current->timer_slack_ns = arg2;
David Howellsb6dff3e2008-11-14 10:39:16 +11002016 error = 0;
Arjan van de Ven69766752008-09-01 15:52:40 -07002017 break;
Andi Kleen4db96cf2009-09-16 11:50:14 +02002018 case PR_MCE_KILL:
2019 if (arg4 | arg5)
2020 return -EINVAL;
2021 switch (arg2) {
Andi Kleen1087e9b2009-10-04 02:20:11 +02002022 case PR_MCE_KILL_CLEAR:
Andi Kleen4db96cf2009-09-16 11:50:14 +02002023 if (arg3 != 0)
2024 return -EINVAL;
2025 current->flags &= ~PF_MCE_PROCESS;
2026 break;
Andi Kleen1087e9b2009-10-04 02:20:11 +02002027 case PR_MCE_KILL_SET:
Andi Kleen4db96cf2009-09-16 11:50:14 +02002028 current->flags |= PF_MCE_PROCESS;
Andi Kleen1087e9b2009-10-04 02:20:11 +02002029 if (arg3 == PR_MCE_KILL_EARLY)
Andi Kleen4db96cf2009-09-16 11:50:14 +02002030 current->flags |= PF_MCE_EARLY;
Andi Kleen1087e9b2009-10-04 02:20:11 +02002031 else if (arg3 == PR_MCE_KILL_LATE)
Andi Kleen4db96cf2009-09-16 11:50:14 +02002032 current->flags &= ~PF_MCE_EARLY;
Andi Kleen1087e9b2009-10-04 02:20:11 +02002033 else if (arg3 == PR_MCE_KILL_DEFAULT)
2034 current->flags &=
2035 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2036 else
2037 return -EINVAL;
Andi Kleen4db96cf2009-09-16 11:50:14 +02002038 break;
2039 default:
2040 return -EINVAL;
2041 }
2042 error = 0;
2043 break;
Andi Kleen1087e9b2009-10-04 02:20:11 +02002044 case PR_MCE_KILL_GET:
2045 if (arg2 | arg3 | arg4 | arg5)
2046 return -EINVAL;
2047 if (current->flags & PF_MCE_PROCESS)
2048 error = (current->flags & PF_MCE_EARLY) ?
2049 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2050 else
2051 error = PR_MCE_KILL_DEFAULT;
2052 break;
Cyrill Gorcunov028ee4b2012-01-12 17:20:55 -08002053 case PR_SET_MM:
2054 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2055 break;
Lennart Poetteringebec18a2012-03-23 15:01:54 -07002056 case PR_SET_CHILD_SUBREAPER:
2057 me->signal->is_child_subreaper = !!arg2;
2058 error = 0;
2059 break;
2060 case PR_GET_CHILD_SUBREAPER:
2061 error = put_user(me->signal->is_child_subreaper,
2062 (int __user *) arg2);
2063 break;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002064 case PR_SET_NO_NEW_PRIVS:
2065 if (arg2 != 1 || arg3 || arg4 || arg5)
2066 return -EINVAL;
2067
2068 current->no_new_privs = 1;
2069 break;
2070 case PR_GET_NO_NEW_PRIVS:
2071 if (arg2 || arg3 || arg4 || arg5)
2072 return -EINVAL;
2073 return current->no_new_privs ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 default:
2075 error = -EINVAL;
2076 break;
2077 }
2078 return error;
2079}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002080
Heiko Carstens836f92a2009-01-14 14:14:33 +01002081SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2082 struct getcpu_cache __user *, unused)
Andi Kleen3cfc3482006-09-26 10:52:28 +02002083{
2084 int err = 0;
2085 int cpu = raw_smp_processor_id();
2086 if (cpup)
2087 err |= put_user(cpu, cpup);
2088 if (nodep)
2089 err |= put_user(cpu_to_node(cpu), nodep);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002090 return err ? -EFAULT : 0;
2091}
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002092
2093char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
2094
Neil Hormana06a4dc2010-05-26 14:42:58 -07002095static void argv_cleanup(struct subprocess_info *info)
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002096{
Neil Hormana06a4dc2010-05-26 14:42:58 -07002097 argv_free(info->argv);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002098}
2099
2100/**
2101 * orderly_poweroff - Trigger an orderly system poweroff
2102 * @force: force poweroff if command execution fails
2103 *
2104 * This may be called from any context to trigger a system shutdown.
2105 * If the orderly shutdown fails, it will force an immediate shutdown.
2106 */
2107int orderly_poweroff(bool force)
2108{
2109 int argc;
2110 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
2111 static char *envp[] = {
2112 "HOME=/",
2113 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
2114 NULL
2115 };
2116 int ret = -ENOMEM;
2117 struct subprocess_info *info;
2118
2119 if (argv == NULL) {
2120 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
2121 __func__, poweroff_cmd);
2122 goto out;
2123 }
2124
KOSAKI Motohiroac331d12008-07-25 01:45:38 -07002125 info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002126 if (info == NULL) {
2127 argv_free(argv);
2128 goto out;
2129 }
2130
Neil Hormana06a4dc2010-05-26 14:42:58 -07002131 call_usermodehelper_setfns(info, NULL, argv_cleanup, NULL);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002132
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -07002133 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -07002134
2135 out:
2136 if (ret && force) {
2137 printk(KERN_WARNING "Failed to start orderly shutdown: "
2138 "forcing the issue\n");
2139
2140 /* I guess this should try to kick off some daemon to
2141 sync and poweroff asap. Or not even bother syncing
2142 if we're doing an emergency shutdown? */
2143 emergency_sync();
2144 kernel_power_off();
2145 }
2146
2147 return ret;
2148}
2149EXPORT_SYMBOL_GPL(orderly_poweroff);