blob: 4d141ae3e8029d13deaaee39d3245e5c42766157 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/mm.h>
9#include <linux/utsname.h>
10#include <linux/mman.h>
11#include <linux/smp_lock.h>
12#include <linux/notifier.h>
13#include <linux/reboot.h>
14#include <linux/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/highuid.h>
16#include <linux/fs.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <linux/compat.h>
37#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080038#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070039#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/uaccess.h>
42#include <asm/io.h>
43#include <asm/unistd.h>
44
45#ifndef SET_UNALIGN_CTL
46# define SET_UNALIGN_CTL(a,b) (-EINVAL)
47#endif
48#ifndef GET_UNALIGN_CTL
49# define GET_UNALIGN_CTL(a,b) (-EINVAL)
50#endif
51#ifndef SET_FPEMU_CTL
52# define SET_FPEMU_CTL(a,b) (-EINVAL)
53#endif
54#ifndef GET_FPEMU_CTL
55# define GET_FPEMU_CTL(a,b) (-EINVAL)
56#endif
57#ifndef SET_FPEXC_CTL
58# define SET_FPEXC_CTL(a,b) (-EINVAL)
59#endif
60#ifndef GET_FPEXC_CTL
61# define GET_FPEXC_CTL(a,b) (-EINVAL)
62#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100063#ifndef GET_ENDIAN
64# define GET_ENDIAN(a,b) (-EINVAL)
65#endif
66#ifndef SET_ENDIAN
67# define SET_ENDIAN(a,b) (-EINVAL)
68#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * this is where the system-wide overflow UID and GID are defined, for
72 * architectures that now have 32-bit UID/GID but didn't in the past
73 */
74
75int overflowuid = DEFAULT_OVERFLOWUID;
76int overflowgid = DEFAULT_OVERFLOWGID;
77
78#ifdef CONFIG_UID16
79EXPORT_SYMBOL(overflowuid);
80EXPORT_SYMBOL(overflowgid);
81#endif
82
83/*
84 * the same as above, but for filesystems which can only store a 16-bit
85 * UID and GID. as such, this is needed on all architectures
86 */
87
88int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
89int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
90
91EXPORT_SYMBOL(fs_overflowuid);
92EXPORT_SYMBOL(fs_overflowgid);
93
94/*
95 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
96 */
97
98int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -070099struct pid *cad_pid;
100EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
103 * Notifier list for kernel code which wants to be called
104 * at shutdown. This is used to stop any idling DMA operations
105 * and the like.
106 */
107
Alan Sterne041c682006-03-27 01:16:30 -0800108static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Alan Sterne041c682006-03-27 01:16:30 -0800110/*
111 * Notifier chain core routines. The exported routines below
112 * are layered on top of these, with appropriate locking added.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
Alan Sterne041c682006-03-27 01:16:30 -0800114
115static int notifier_chain_register(struct notifier_block **nl,
116 struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Alan Sterne041c682006-03-27 01:16:30 -0800118 while ((*nl) != NULL) {
119 if (n->priority > (*nl)->priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 break;
Alan Sterne041c682006-03-27 01:16:30 -0800121 nl = &((*nl)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Alan Sterne041c682006-03-27 01:16:30 -0800123 n->next = *nl;
124 rcu_assign_pointer(*nl, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126}
127
Alan Sterne041c682006-03-27 01:16:30 -0800128static int notifier_chain_unregister(struct notifier_block **nl,
129 struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Alan Sterne041c682006-03-27 01:16:30 -0800131 while ((*nl) != NULL) {
132 if ((*nl) == n) {
133 rcu_assign_pointer(*nl, n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 0;
135 }
Alan Sterne041c682006-03-27 01:16:30 -0800136 nl = &((*nl)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -ENOENT;
139}
140
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700141/**
142 * notifier_call_chain - Informs the registered notifiers about an event.
143 * @nl: Pointer to head of the blocking notifier chain
144 * @val: Value passed unmodified to notifier function
145 * @v: Pointer passed unmodified to notifier function
146 * @nr_to_call: Number of notifier functions to be called. Don't care
147 * value of this parameter is -1.
148 * @nr_calls: Records the number of notifications sent. Don't care
149 * value of this field is NULL.
150 * @returns: notifier_call_chain returns the value returned by the
151 * last notifier function called.
152 */
153
Alan Sterne041c682006-03-27 01:16:30 -0800154static int __kprobes notifier_call_chain(struct notifier_block **nl,
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700155 unsigned long val, void *v,
156 int nr_to_call, int *nr_calls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Alan Sterne041c682006-03-27 01:16:30 -0800158 int ret = NOTIFY_DONE;
Alan Sternbbb17472006-06-25 05:47:15 -0700159 struct notifier_block *nb, *next_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Alan Sterne041c682006-03-27 01:16:30 -0800161 nb = rcu_dereference(*nl);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700162
163 while (nb && nr_to_call) {
Alan Sternbbb17472006-06-25 05:47:15 -0700164 next_nb = rcu_dereference(nb->next);
Alan Sterne041c682006-03-27 01:16:30 -0800165 ret = nb->notifier_call(nb, val, v);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700166
167 if (nr_calls)
168 (*nr_calls)++;
169
Alan Sterne041c682006-03-27 01:16:30 -0800170 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
171 break;
Alan Sternbbb17472006-06-25 05:47:15 -0700172 nb = next_nb;
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700173 nr_to_call--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 return ret;
176}
177
Alan Sterne041c682006-03-27 01:16:30 -0800178/*
179 * Atomic notifier chain routines. Registration and unregistration
Alan Sterneabc0692006-10-04 02:17:04 -0700180 * use a spinlock, and call_chain is synchronized by RCU (no locks).
Alan Sterne041c682006-03-27 01:16:30 -0800181 */
182
183/**
184 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
185 * @nh: Pointer to head of the atomic notifier chain
186 * @n: New entry in notifier chain
187 *
188 * Adds a notifier to an atomic notifier chain.
189 *
190 * Currently always returns zero.
191 */
192
193int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
194 struct notifier_block *n)
195{
196 unsigned long flags;
197 int ret;
198
199 spin_lock_irqsave(&nh->lock, flags);
200 ret = notifier_chain_register(&nh->head, n);
201 spin_unlock_irqrestore(&nh->lock, flags);
202 return ret;
203}
204
205EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
206
207/**
208 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
209 * @nh: Pointer to head of the atomic notifier chain
210 * @n: Entry to remove from notifier chain
211 *
212 * Removes a notifier from an atomic notifier chain.
213 *
214 * Returns zero on success or %-ENOENT on failure.
215 */
216int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
217 struct notifier_block *n)
218{
219 unsigned long flags;
220 int ret;
221
222 spin_lock_irqsave(&nh->lock, flags);
223 ret = notifier_chain_unregister(&nh->head, n);
224 spin_unlock_irqrestore(&nh->lock, flags);
225 synchronize_rcu();
226 return ret;
227}
228
229EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
230
231/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700232 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
Alan Sterne041c682006-03-27 01:16:30 -0800233 * @nh: Pointer to head of the atomic notifier chain
234 * @val: Value passed unmodified to notifier function
235 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700236 * @nr_to_call: See the comment for notifier_call_chain.
237 * @nr_calls: See the comment for notifier_call_chain.
Alan Sterne041c682006-03-27 01:16:30 -0800238 *
239 * Calls each function in a notifier chain in turn. The functions
240 * run in an atomic context, so they must not block.
241 * This routine uses RCU to synchronize with changes to the chain.
242 *
243 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800244 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800245 * will return immediately, with the return value of
246 * the notifier function which halted execution.
247 * Otherwise the return value is the return value
248 * of the last notifier function called.
249 */
250
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700251int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
252 unsigned long val, void *v,
253 int nr_to_call, int *nr_calls)
Alan Sterne041c682006-03-27 01:16:30 -0800254{
255 int ret;
256
257 rcu_read_lock();
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700258 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
Alan Sterne041c682006-03-27 01:16:30 -0800259 rcu_read_unlock();
260 return ret;
261}
262
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700263EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
Alan Sterne041c682006-03-27 01:16:30 -0800264
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700265int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
266 unsigned long val, void *v)
267{
268 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
269}
270
271EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
Alan Sterne041c682006-03-27 01:16:30 -0800272/*
273 * Blocking notifier chain routines. All access to the chain is
274 * synchronized by an rwsem.
275 */
276
277/**
278 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
279 * @nh: Pointer to head of the blocking notifier chain
280 * @n: New entry in notifier chain
281 *
282 * Adds a notifier to a blocking notifier chain.
283 * Must be called in process context.
284 *
285 * Currently always returns zero.
286 */
287
288int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
289 struct notifier_block *n)
290{
291 int ret;
292
293 /*
294 * This code gets used during boot-up, when task switching is
295 * not yet working and interrupts must remain disabled. At
296 * such times we must not call down_write().
297 */
298 if (unlikely(system_state == SYSTEM_BOOTING))
299 return notifier_chain_register(&nh->head, n);
300
301 down_write(&nh->rwsem);
302 ret = notifier_chain_register(&nh->head, n);
303 up_write(&nh->rwsem);
304 return ret;
305}
306
307EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
308
309/**
310 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
311 * @nh: Pointer to head of the blocking notifier chain
312 * @n: Entry to remove from notifier chain
313 *
314 * Removes a notifier from a blocking notifier chain.
315 * Must be called from process context.
316 *
317 * Returns zero on success or %-ENOENT on failure.
318 */
319int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
320 struct notifier_block *n)
321{
322 int ret;
323
324 /*
325 * This code gets used during boot-up, when task switching is
326 * not yet working and interrupts must remain disabled. At
327 * such times we must not call down_write().
328 */
329 if (unlikely(system_state == SYSTEM_BOOTING))
330 return notifier_chain_unregister(&nh->head, n);
331
332 down_write(&nh->rwsem);
333 ret = notifier_chain_unregister(&nh->head, n);
334 up_write(&nh->rwsem);
335 return ret;
336}
337
338EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
339
340/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700341 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
Alan Sterne041c682006-03-27 01:16:30 -0800342 * @nh: Pointer to head of the blocking notifier chain
343 * @val: Value passed unmodified to notifier function
344 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700345 * @nr_to_call: See comment for notifier_call_chain.
346 * @nr_calls: See comment for notifier_call_chain.
Alan Sterne041c682006-03-27 01:16:30 -0800347 *
348 * Calls each function in a notifier chain in turn. The functions
349 * run in a process context, so they are allowed to block.
350 *
351 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800352 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800353 * will return immediately, with the return value of
354 * the notifier function which halted execution.
355 * Otherwise the return value is the return value
356 * of the last notifier function called.
357 */
358
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700359int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
360 unsigned long val, void *v,
361 int nr_to_call, int *nr_calls)
Alan Sterne041c682006-03-27 01:16:30 -0800362{
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100363 int ret = NOTIFY_DONE;
Alan Sterne041c682006-03-27 01:16:30 -0800364
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100365 /*
366 * We check the head outside the lock, but if this access is
367 * racy then it does not matter what the result of the test
368 * is, we re-check the list after having taken the lock anyway:
369 */
370 if (rcu_dereference(nh->head)) {
371 down_read(&nh->rwsem);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700372 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
373 nr_calls);
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100374 up_read(&nh->rwsem);
375 }
Alan Sterne041c682006-03-27 01:16:30 -0800376 return ret;
377}
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700378EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
Alan Sterne041c682006-03-27 01:16:30 -0800379
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700380int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
381 unsigned long val, void *v)
382{
383 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
384}
Alan Sterne041c682006-03-27 01:16:30 -0800385EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
386
387/*
388 * Raw notifier chain routines. There is no protection;
389 * the caller must provide it. Use at your own risk!
390 */
391
392/**
393 * raw_notifier_chain_register - Add notifier to a raw notifier chain
394 * @nh: Pointer to head of the raw notifier chain
395 * @n: New entry in notifier chain
396 *
397 * Adds a notifier to a raw notifier chain.
398 * All locking must be provided by the caller.
399 *
400 * Currently always returns zero.
401 */
402
403int raw_notifier_chain_register(struct raw_notifier_head *nh,
404 struct notifier_block *n)
405{
406 return notifier_chain_register(&nh->head, n);
407}
408
409EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
410
411/**
412 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
413 * @nh: Pointer to head of the raw notifier chain
414 * @n: Entry to remove from notifier chain
415 *
416 * Removes a notifier from a raw notifier chain.
417 * All locking must be provided by the caller.
418 *
419 * Returns zero on success or %-ENOENT on failure.
420 */
421int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
422 struct notifier_block *n)
423{
424 return notifier_chain_unregister(&nh->head, n);
425}
426
427EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
428
429/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700430 * __raw_notifier_call_chain - Call functions in a raw notifier chain
Alan Sterne041c682006-03-27 01:16:30 -0800431 * @nh: Pointer to head of the raw notifier chain
432 * @val: Value passed unmodified to notifier function
433 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700434 * @nr_to_call: See comment for notifier_call_chain.
435 * @nr_calls: See comment for notifier_call_chain
Alan Sterne041c682006-03-27 01:16:30 -0800436 *
437 * Calls each function in a notifier chain in turn. The functions
438 * run in an undefined context.
439 * All locking must be provided by the caller.
440 *
441 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800442 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800443 * will return immediately, with the return value of
444 * the notifier function which halted execution.
445 * Otherwise the return value is the return value
446 * of the last notifier function called.
447 */
448
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700449int __raw_notifier_call_chain(struct raw_notifier_head *nh,
450 unsigned long val, void *v,
451 int nr_to_call, int *nr_calls)
452{
453 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
454}
455
456EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
457
Alan Sterne041c682006-03-27 01:16:30 -0800458int raw_notifier_call_chain(struct raw_notifier_head *nh,
459 unsigned long val, void *v)
460{
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700461 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
Alan Sterne041c682006-03-27 01:16:30 -0800462}
463
464EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Alan Sterneabc0692006-10-04 02:17:04 -0700466/*
467 * SRCU notifier chain routines. Registration and unregistration
468 * use a mutex, and call_chain is synchronized by SRCU (no locks).
469 */
470
471/**
472 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
473 * @nh: Pointer to head of the SRCU notifier chain
474 * @n: New entry in notifier chain
475 *
476 * Adds a notifier to an SRCU notifier chain.
477 * Must be called in process context.
478 *
479 * Currently always returns zero.
480 */
481
482int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
483 struct notifier_block *n)
484{
485 int ret;
486
487 /*
488 * This code gets used during boot-up, when task switching is
489 * not yet working and interrupts must remain disabled. At
490 * such times we must not call mutex_lock().
491 */
492 if (unlikely(system_state == SYSTEM_BOOTING))
493 return notifier_chain_register(&nh->head, n);
494
495 mutex_lock(&nh->mutex);
496 ret = notifier_chain_register(&nh->head, n);
497 mutex_unlock(&nh->mutex);
498 return ret;
499}
500
501EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
502
503/**
504 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
505 * @nh: Pointer to head of the SRCU notifier chain
506 * @n: Entry to remove from notifier chain
507 *
508 * Removes a notifier from an SRCU notifier chain.
509 * Must be called from process context.
510 *
511 * Returns zero on success or %-ENOENT on failure.
512 */
513int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
514 struct notifier_block *n)
515{
516 int ret;
517
518 /*
519 * This code gets used during boot-up, when task switching is
520 * not yet working and interrupts must remain disabled. At
521 * such times we must not call mutex_lock().
522 */
523 if (unlikely(system_state == SYSTEM_BOOTING))
524 return notifier_chain_unregister(&nh->head, n);
525
526 mutex_lock(&nh->mutex);
527 ret = notifier_chain_unregister(&nh->head, n);
528 mutex_unlock(&nh->mutex);
529 synchronize_srcu(&nh->srcu);
530 return ret;
531}
532
533EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
534
535/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700536 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
Alan Sterneabc0692006-10-04 02:17:04 -0700537 * @nh: Pointer to head of the SRCU notifier chain
538 * @val: Value passed unmodified to notifier function
539 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700540 * @nr_to_call: See comment for notifier_call_chain.
541 * @nr_calls: See comment for notifier_call_chain
Alan Sterneabc0692006-10-04 02:17:04 -0700542 *
543 * Calls each function in a notifier chain in turn. The functions
544 * run in a process context, so they are allowed to block.
545 *
546 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800547 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
Alan Sterneabc0692006-10-04 02:17:04 -0700548 * will return immediately, with the return value of
549 * the notifier function which halted execution.
550 * Otherwise the return value is the return value
551 * of the last notifier function called.
552 */
553
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700554int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
555 unsigned long val, void *v,
556 int nr_to_call, int *nr_calls)
Alan Sterneabc0692006-10-04 02:17:04 -0700557{
558 int ret;
559 int idx;
560
561 idx = srcu_read_lock(&nh->srcu);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700562 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
Alan Sterneabc0692006-10-04 02:17:04 -0700563 srcu_read_unlock(&nh->srcu, idx);
564 return ret;
565}
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700566EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
Alan Sterneabc0692006-10-04 02:17:04 -0700567
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700568int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
569 unsigned long val, void *v)
570{
571 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
572}
Alan Sterneabc0692006-10-04 02:17:04 -0700573EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
574
575/**
576 * srcu_init_notifier_head - Initialize an SRCU notifier head
577 * @nh: Pointer to head of the srcu notifier chain
578 *
579 * Unlike other sorts of notifier heads, SRCU notifier heads require
580 * dynamic initialization. Be sure to call this routine before
581 * calling any of the other SRCU notifier routines for this head.
582 *
583 * If an SRCU notifier head is deallocated, it must first be cleaned
584 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
585 * per-cpu data (used by the SRCU mechanism) will leak.
586 */
587
588void srcu_init_notifier_head(struct srcu_notifier_head *nh)
589{
590 mutex_init(&nh->mutex);
Alan Sterne6a92012006-10-04 02:17:05 -0700591 if (init_srcu_struct(&nh->srcu) < 0)
592 BUG();
Alan Sterneabc0692006-10-04 02:17:04 -0700593 nh->head = NULL;
594}
595
596EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/**
599 * register_reboot_notifier - Register function to be called at reboot time
600 * @nb: Info about notifier function to be called
601 *
602 * Registers a function with the list of functions
603 * to be called at reboot time.
604 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800605 * Currently always returns zero, as blocking_notifier_chain_register()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * always returns zero.
607 */
608
609int register_reboot_notifier(struct notifier_block * nb)
610{
Alan Sterne041c682006-03-27 01:16:30 -0800611 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614EXPORT_SYMBOL(register_reboot_notifier);
615
616/**
617 * unregister_reboot_notifier - Unregister previously registered reboot notifier
618 * @nb: Hook to be unregistered
619 *
620 * Unregisters a previously registered reboot
621 * notifier function.
622 *
623 * Returns zero on success, or %-ENOENT on failure.
624 */
625
626int unregister_reboot_notifier(struct notifier_block * nb)
627{
Alan Sterne041c682006-03-27 01:16:30 -0800628 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631EXPORT_SYMBOL(unregister_reboot_notifier);
632
633static int set_one_prio(struct task_struct *p, int niceval, int error)
634{
635 int no_nice;
636
637 if (p->uid != current->euid &&
638 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
639 error = -EPERM;
640 goto out;
641 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700642 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 error = -EACCES;
644 goto out;
645 }
646 no_nice = security_task_setnice(p, niceval);
647 if (no_nice) {
648 error = no_nice;
649 goto out;
650 }
651 if (error == -ESRCH)
652 error = 0;
653 set_user_nice(p, niceval);
654out:
655 return error;
656}
657
658asmlinkage long sys_setpriority(int which, int who, int niceval)
659{
660 struct task_struct *g, *p;
661 struct user_struct *user;
662 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800663 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Daniel Walker3e88c552007-05-10 22:22:53 -0700665 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 goto out;
667
668 /* normalize: avoid signed division (rounding problems) */
669 error = -ESRCH;
670 if (niceval < -20)
671 niceval = -20;
672 if (niceval > 19)
673 niceval = 19;
674
675 read_lock(&tasklist_lock);
676 switch (which) {
677 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800678 if (who)
679 p = find_task_by_pid(who);
680 else
681 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (p)
683 error = set_one_prio(p, niceval, error);
684 break;
685 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800686 if (who)
687 pgrp = find_pid(who);
688 else
689 pgrp = task_pgrp(current);
690 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 error = set_one_prio(p, niceval, error);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800692 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 break;
694 case PRIO_USER:
695 user = current->user;
696 if (!who)
697 who = current->uid;
698 else
699 if ((who != current->uid) && !(user = find_user(who)))
700 goto out_unlock; /* No processes for this user */
701
702 do_each_thread(g, p)
703 if (p->uid == who)
704 error = set_one_prio(p, niceval, error);
705 while_each_thread(g, p);
706 if (who != current->uid)
707 free_uid(user); /* For find_user() */
708 break;
709 }
710out_unlock:
711 read_unlock(&tasklist_lock);
712out:
713 return error;
714}
715
716/*
717 * Ugh. To avoid negative return values, "getpriority()" will
718 * not return the normal nice-value, but a negated value that
719 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
720 * to stay compatible.
721 */
722asmlinkage long sys_getpriority(int which, int who)
723{
724 struct task_struct *g, *p;
725 struct user_struct *user;
726 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800727 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Daniel Walker3e88c552007-05-10 22:22:53 -0700729 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -EINVAL;
731
732 read_lock(&tasklist_lock);
733 switch (which) {
734 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800735 if (who)
736 p = find_task_by_pid(who);
737 else
738 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (p) {
740 niceval = 20 - task_nice(p);
741 if (niceval > retval)
742 retval = niceval;
743 }
744 break;
745 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800746 if (who)
747 pgrp = find_pid(who);
748 else
749 pgrp = task_pgrp(current);
750 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 niceval = 20 - task_nice(p);
752 if (niceval > retval)
753 retval = niceval;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800754 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
756 case PRIO_USER:
757 user = current->user;
758 if (!who)
759 who = current->uid;
760 else
761 if ((who != current->uid) && !(user = find_user(who)))
762 goto out_unlock; /* No processes for this user */
763
764 do_each_thread(g, p)
765 if (p->uid == who) {
766 niceval = 20 - task_nice(p);
767 if (niceval > retval)
768 retval = niceval;
769 }
770 while_each_thread(g, p);
771 if (who != current->uid)
772 free_uid(user); /* for find_user() */
773 break;
774 }
775out_unlock:
776 read_unlock(&tasklist_lock);
777
778 return retval;
779}
780
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700781/**
782 * emergency_restart - reboot the system
783 *
784 * Without shutting down any hardware or taking any locks
785 * reboot the system. This is called when we know we are in
786 * trouble so this is our best effort to reboot. This is
787 * safe to call in interrupt context.
788 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600789void emergency_restart(void)
790{
791 machine_emergency_restart();
792}
793EXPORT_SYMBOL_GPL(emergency_restart);
794
Adrian Bunk83cc5ed2006-06-25 05:47:41 -0700795static void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600796{
Alan Sterne041c682006-03-27 01:16:30 -0800797 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600798 system_state = SYSTEM_RESTART;
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600799 device_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700800}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800801
802/**
803 * kernel_restart - reboot the system
804 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800805 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800806 *
807 * Shutdown everything and perform a clean reboot.
808 * This is not safe to call in interrupt context.
809 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700810void kernel_restart(char *cmd)
811{
812 kernel_restart_prepare(cmd);
Cal Peake756184b2006-09-30 23:27:24 -0700813 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600814 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700815 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600816 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600817 machine_restart(cmd);
818}
819EXPORT_SYMBOL_GPL(kernel_restart);
820
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700821/**
822 * kernel_kexec - reboot the system
823 *
824 * Move into place and start executing a preloaded standalone
825 * executable. If nothing was preloaded return an error.
826 */
Adrian Bunk83cc5ed2006-06-25 05:47:41 -0700827static void kernel_kexec(void)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600828{
829#ifdef CONFIG_KEXEC
830 struct kimage *image;
Al Viro4bb80892006-02-01 05:57:32 -0500831 image = xchg(&kexec_image, NULL);
Cal Peake756184b2006-09-30 23:27:24 -0700832 if (!image)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600833 return;
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700834 kernel_restart_prepare(NULL);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600835 printk(KERN_EMERG "Starting new kernel\n");
836 machine_shutdown();
837 machine_kexec(image);
838#endif
839}
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600840
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500841void kernel_shutdown_prepare(enum system_states state)
842{
Alan Sterne041c682006-03-27 01:16:30 -0800843 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500844 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
845 system_state = state;
846 device_shutdown();
847}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700848/**
849 * kernel_halt - halt the system
850 *
851 * Shutdown everything and perform a clean system halt.
852 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700853void kernel_halt(void)
854{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500855 kernel_shutdown_prepare(SYSTEM_HALT);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600856 printk(KERN_EMERG "System halted.\n");
857 machine_halt();
858}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500859
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600860EXPORT_SYMBOL_GPL(kernel_halt);
861
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700862/**
863 * kernel_power_off - power_off the system
864 *
865 * Shutdown everything and perform a clean system power_off.
866 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700867void kernel_power_off(void)
868{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500869 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600870 printk(KERN_EMERG "Power down.\n");
871 machine_power_off();
872}
873EXPORT_SYMBOL_GPL(kernel_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * Reboot system call: for obvious reasons only root may call it,
876 * and even root needs to set up some magic numbers in the registers
877 * so that some mistake won't make this reboot the whole machine.
878 * You can also set the meaning of the ctrl-alt-del-key here.
879 *
880 * reboot doesn't sync: do that yourself before calling this.
881 */
882asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
883{
884 char buffer[256];
885
886 /* We only trust the superuser with rebooting the system. */
887 if (!capable(CAP_SYS_BOOT))
888 return -EPERM;
889
890 /* For safety, we require "magic" arguments. */
891 if (magic1 != LINUX_REBOOT_MAGIC1 ||
892 (magic2 != LINUX_REBOOT_MAGIC2 &&
893 magic2 != LINUX_REBOOT_MAGIC2A &&
894 magic2 != LINUX_REBOOT_MAGIC2B &&
895 magic2 != LINUX_REBOOT_MAGIC2C))
896 return -EINVAL;
897
Eric W. Biederman5e382912006-01-08 01:03:46 -0800898 /* Instead of trying to make the power_off code look like
899 * halt when pm_power_off is not set do it the easy way.
900 */
901 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
902 cmd = LINUX_REBOOT_CMD_HALT;
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 lock_kernel();
905 switch (cmd) {
906 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600907 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 break;
909
910 case LINUX_REBOOT_CMD_CAD_ON:
911 C_A_D = 1;
912 break;
913
914 case LINUX_REBOOT_CMD_CAD_OFF:
915 C_A_D = 0;
916 break;
917
918 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600919 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 unlock_kernel();
921 do_exit(0);
922 break;
923
924 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600925 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 unlock_kernel();
927 do_exit(0);
928 break;
929
930 case LINUX_REBOOT_CMD_RESTART2:
931 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
932 unlock_kernel();
933 return -EFAULT;
934 }
935 buffer[sizeof(buffer) - 1] = '\0';
936
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600937 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 break;
939
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700940 case LINUX_REBOOT_CMD_KEXEC:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600941 kernel_kexec();
942 unlock_kernel();
943 return -EINVAL;
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945#ifdef CONFIG_SOFTWARE_SUSPEND
946 case LINUX_REBOOT_CMD_SW_SUSPEND:
947 {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700948 int ret = hibernate();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 unlock_kernel();
950 return ret;
951 }
952#endif
953
954 default:
955 unlock_kernel();
956 return -EINVAL;
957 }
958 unlock_kernel();
959 return 0;
960}
961
David Howells65f27f32006-11-22 14:55:48 +0000962static void deferred_cad(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600964 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967/*
968 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
969 * As it's called within an interrupt, it may NOT sync: the only choice
970 * is whether to reboot at once, or just ignore the ctrl-alt-del.
971 */
972void ctrl_alt_del(void)
973{
David Howells65f27f32006-11-22 14:55:48 +0000974 static DECLARE_WORK(cad_work, deferred_cad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (C_A_D)
977 schedule_work(&cad_work);
978 else
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700979 kill_cad_pid(SIGINT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/*
983 * Unprivileged users may change the real gid to the effective gid
984 * or vice versa. (BSD-style)
985 *
986 * If you set the real gid at all, or set the effective gid to a value not
987 * equal to the real gid, then the saved gid is set to the new effective gid.
988 *
989 * This makes it possible for a setgid program to completely drop its
990 * privileges, which is often a useful assertion to make when you are doing
991 * a security audit over a program.
992 *
993 * The general idea is that a program which uses just setregid() will be
994 * 100% compatible with BSD. A program which uses just setgid() will be
995 * 100% compatible with POSIX with saved IDs.
996 *
997 * SMP: There are not races, the GIDs are checked only by filesystem
998 * operations (as far as semantic preservation is concerned).
999 */
1000asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
1001{
1002 int old_rgid = current->gid;
1003 int old_egid = current->egid;
1004 int new_rgid = old_rgid;
1005 int new_egid = old_egid;
1006 int retval;
1007
1008 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
1009 if (retval)
1010 return retval;
1011
1012 if (rgid != (gid_t) -1) {
1013 if ((old_rgid == rgid) ||
1014 (current->egid==rgid) ||
1015 capable(CAP_SETGID))
1016 new_rgid = rgid;
1017 else
1018 return -EPERM;
1019 }
1020 if (egid != (gid_t) -1) {
1021 if ((old_rgid == egid) ||
1022 (current->egid == egid) ||
1023 (current->sgid == egid) ||
1024 capable(CAP_SETGID))
1025 new_egid = egid;
Cal Peake756184b2006-09-30 23:27:24 -07001026 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Cal Peake756184b2006-09-30 23:27:24 -07001029 if (new_egid != old_egid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001030 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001031 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033 if (rgid != (gid_t) -1 ||
1034 (egid != (gid_t) -1 && egid != old_rgid))
1035 current->sgid = new_egid;
1036 current->fsgid = new_egid;
1037 current->egid = new_egid;
1038 current->gid = new_rgid;
1039 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001040 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return 0;
1042}
1043
1044/*
1045 * setgid() is implemented like SysV w/ SAVED_IDS
1046 *
1047 * SMP: Same implicit races as above.
1048 */
1049asmlinkage long sys_setgid(gid_t gid)
1050{
1051 int old_egid = current->egid;
1052 int retval;
1053
1054 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
1055 if (retval)
1056 return retval;
1057
Cal Peake756184b2006-09-30 23:27:24 -07001058 if (capable(CAP_SETGID)) {
1059 if (old_egid != gid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001060 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001061 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 current->gid = current->egid = current->sgid = current->fsgid = gid;
Cal Peake756184b2006-09-30 23:27:24 -07001064 } else if ((gid == current->gid) || (gid == current->sgid)) {
1065 if (old_egid != gid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001066 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001067 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 current->egid = current->fsgid = gid;
1070 }
1071 else
1072 return -EPERM;
1073
1074 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001075 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return 0;
1077}
1078
1079static int set_user(uid_t new_ruid, int dumpclear)
1080{
1081 struct user_struct *new_user;
1082
Cedric Le Goateracce2922007-07-15 23:40:59 -07001083 new_user = alloc_uid(current->nsproxy->user_ns, new_ruid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!new_user)
1085 return -EAGAIN;
1086
1087 if (atomic_read(&new_user->processes) >=
1088 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
Cedric Le Goateracce2922007-07-15 23:40:59 -07001089 new_user != current->nsproxy->user_ns->root_user) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 free_uid(new_user);
1091 return -EAGAIN;
1092 }
1093
1094 switch_uid(new_user);
1095
Cal Peake756184b2006-09-30 23:27:24 -07001096 if (dumpclear) {
Alan Coxd6e71142005-06-23 00:09:43 -07001097 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001098 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 current->uid = new_ruid;
1101 return 0;
1102}
1103
1104/*
1105 * Unprivileged users may change the real uid to the effective uid
1106 * or vice versa. (BSD-style)
1107 *
1108 * If you set the real uid at all, or set the effective uid to a value not
1109 * equal to the real uid, then the saved uid is set to the new effective uid.
1110 *
1111 * This makes it possible for a setuid program to completely drop its
1112 * privileges, which is often a useful assertion to make when you are doing
1113 * a security audit over a program.
1114 *
1115 * The general idea is that a program which uses just setreuid() will be
1116 * 100% compatible with BSD. A program which uses just setuid() will be
1117 * 100% compatible with POSIX with saved IDs.
1118 */
1119asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
1120{
1121 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
1122 int retval;
1123
1124 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
1125 if (retval)
1126 return retval;
1127
1128 new_ruid = old_ruid = current->uid;
1129 new_euid = old_euid = current->euid;
1130 old_suid = current->suid;
1131
1132 if (ruid != (uid_t) -1) {
1133 new_ruid = ruid;
1134 if ((old_ruid != ruid) &&
1135 (current->euid != ruid) &&
1136 !capable(CAP_SETUID))
1137 return -EPERM;
1138 }
1139
1140 if (euid != (uid_t) -1) {
1141 new_euid = euid;
1142 if ((old_ruid != euid) &&
1143 (current->euid != euid) &&
1144 (current->suid != euid) &&
1145 !capable(CAP_SETUID))
1146 return -EPERM;
1147 }
1148
1149 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
1150 return -EAGAIN;
1151
Cal Peake756184b2006-09-30 23:27:24 -07001152 if (new_euid != old_euid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001153 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001154 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 current->fsuid = current->euid = new_euid;
1157 if (ruid != (uid_t) -1 ||
1158 (euid != (uid_t) -1 && euid != old_ruid))
1159 current->suid = current->euid;
1160 current->fsuid = current->euid;
1161
1162 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001163 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
1166}
1167
1168
1169
1170/*
1171 * setuid() is implemented like SysV with SAVED_IDS
1172 *
1173 * Note that SAVED_ID's is deficient in that a setuid root program
1174 * like sendmail, for example, cannot set its uid to be a normal
1175 * user and then switch back, because if you're root, setuid() sets
1176 * the saved uid too. If you don't like this, blame the bright people
1177 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
1178 * will allow a root program to temporarily drop privileges and be able to
1179 * regain them by swapping the real and effective uid.
1180 */
1181asmlinkage long sys_setuid(uid_t uid)
1182{
1183 int old_euid = current->euid;
David Rientjesa09c17a2006-12-06 20:40:18 -08001184 int old_ruid, old_suid, new_suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int retval;
1186
1187 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
1188 if (retval)
1189 return retval;
1190
David Rientjesa09c17a2006-12-06 20:40:18 -08001191 old_ruid = current->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 old_suid = current->suid;
1193 new_suid = old_suid;
1194
1195 if (capable(CAP_SETUID)) {
1196 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1197 return -EAGAIN;
1198 new_suid = uid;
1199 } else if ((uid != current->uid) && (uid != new_suid))
1200 return -EPERM;
1201
Cal Peake756184b2006-09-30 23:27:24 -07001202 if (old_euid != uid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001203 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001204 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 current->fsuid = current->euid = uid;
1207 current->suid = new_suid;
1208
1209 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001210 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1213}
1214
1215
1216/*
1217 * This function implements a generic ability to update ruid, euid,
1218 * and suid. This allows you to implement the 4.4 compatible seteuid().
1219 */
1220asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1221{
1222 int old_ruid = current->uid;
1223 int old_euid = current->euid;
1224 int old_suid = current->suid;
1225 int retval;
1226
1227 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1228 if (retval)
1229 return retval;
1230
1231 if (!capable(CAP_SETUID)) {
1232 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1233 (ruid != current->euid) && (ruid != current->suid))
1234 return -EPERM;
1235 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1236 (euid != current->euid) && (euid != current->suid))
1237 return -EPERM;
1238 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1239 (suid != current->euid) && (suid != current->suid))
1240 return -EPERM;
1241 }
1242 if (ruid != (uid_t) -1) {
1243 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1244 return -EAGAIN;
1245 }
1246 if (euid != (uid_t) -1) {
Cal Peake756184b2006-09-30 23:27:24 -07001247 if (euid != current->euid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001248 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001249 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 current->euid = euid;
1252 }
1253 current->fsuid = current->euid;
1254 if (suid != (uid_t) -1)
1255 current->suid = suid;
1256
1257 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001258 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1261}
1262
1263asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1264{
1265 int retval;
1266
1267 if (!(retval = put_user(current->uid, ruid)) &&
1268 !(retval = put_user(current->euid, euid)))
1269 retval = put_user(current->suid, suid);
1270
1271 return retval;
1272}
1273
1274/*
1275 * Same as above, but for rgid, egid, sgid.
1276 */
1277asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1278{
1279 int retval;
1280
1281 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1282 if (retval)
1283 return retval;
1284
1285 if (!capable(CAP_SETGID)) {
1286 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1287 (rgid != current->egid) && (rgid != current->sgid))
1288 return -EPERM;
1289 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1290 (egid != current->egid) && (egid != current->sgid))
1291 return -EPERM;
1292 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1293 (sgid != current->egid) && (sgid != current->sgid))
1294 return -EPERM;
1295 }
1296 if (egid != (gid_t) -1) {
Cal Peake756184b2006-09-30 23:27:24 -07001297 if (egid != current->egid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001298 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001299 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 current->egid = egid;
1302 }
1303 current->fsgid = current->egid;
1304 if (rgid != (gid_t) -1)
1305 current->gid = rgid;
1306 if (sgid != (gid_t) -1)
1307 current->sgid = sgid;
1308
1309 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001310 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return 0;
1312}
1313
1314asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1315{
1316 int retval;
1317
1318 if (!(retval = put_user(current->gid, rgid)) &&
1319 !(retval = put_user(current->egid, egid)))
1320 retval = put_user(current->sgid, sgid);
1321
1322 return retval;
1323}
1324
1325
1326/*
1327 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1328 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1329 * whatever uid it wants to). It normally shadows "euid", except when
1330 * explicitly set by setfsuid() or for access..
1331 */
1332asmlinkage long sys_setfsuid(uid_t uid)
1333{
1334 int old_fsuid;
1335
1336 old_fsuid = current->fsuid;
1337 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1338 return old_fsuid;
1339
1340 if (uid == current->uid || uid == current->euid ||
1341 uid == current->suid || uid == current->fsuid ||
Cal Peake756184b2006-09-30 23:27:24 -07001342 capable(CAP_SETUID)) {
1343 if (uid != old_fsuid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001344 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001345 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347 current->fsuid = uid;
1348 }
1349
1350 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001351 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1354
1355 return old_fsuid;
1356}
1357
1358/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +02001359 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
1361asmlinkage long sys_setfsgid(gid_t gid)
1362{
1363 int old_fsgid;
1364
1365 old_fsgid = current->fsgid;
1366 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1367 return old_fsgid;
1368
1369 if (gid == current->gid || gid == current->egid ||
1370 gid == current->sgid || gid == current->fsgid ||
Cal Peake756184b2006-09-30 23:27:24 -07001371 capable(CAP_SETGID)) {
1372 if (gid != old_fsgid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001373 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001374 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376 current->fsgid = gid;
1377 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001378 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 return old_fsgid;
1381}
1382
1383asmlinkage long sys_times(struct tms __user * tbuf)
1384{
1385 /*
1386 * In the SMP world we might just be unlucky and have one of
1387 * the times increment as we use it. Since the value is an
1388 * atomically safe type this is just fine. Conceptually its
1389 * as if the syscall took an instant longer to occur.
1390 */
1391 if (tbuf) {
1392 struct tms tmp;
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001393 struct task_struct *tsk = current;
1394 struct task_struct *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 cputime_t utime, stime, cutime, cstime;
1396
Oleg Nesterov7d7185c2006-03-28 16:11:21 -08001397 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001398 utime = tsk->signal->utime;
1399 stime = tsk->signal->stime;
1400 t = tsk;
1401 do {
1402 utime = cputime_add(utime, t->utime);
1403 stime = cputime_add(stime, t->stime);
1404 t = next_thread(t);
1405 } while (t != tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001407 cutime = tsk->signal->cutime;
1408 cstime = tsk->signal->cstime;
1409 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 tmp.tms_utime = cputime_to_clock_t(utime);
1412 tmp.tms_stime = cputime_to_clock_t(stime);
1413 tmp.tms_cutime = cputime_to_clock_t(cutime);
1414 tmp.tms_cstime = cputime_to_clock_t(cstime);
1415 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1416 return -EFAULT;
1417 }
1418 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1419}
1420
1421/*
1422 * This needs some heavy checking ...
1423 * I just haven't the stomach for it. I also don't fully
1424 * understand sessions/pgrp etc. Let somebody who does explain it.
1425 *
1426 * OK, I think I have the protection semantics right.... this is really
1427 * only important on a multi-user system anyway, to make sure one user
1428 * can't send a signal to a process owned by another. -TYT, 12/12/91
1429 *
1430 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1431 * LBT 04.03.94
1432 */
1433
1434asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1435{
1436 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001437 struct task_struct *group_leader = current->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int err = -EINVAL;
1439
1440 if (!pid)
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001441 pid = group_leader->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (!pgid)
1443 pgid = pid;
1444 if (pgid < 0)
1445 return -EINVAL;
1446
1447 /* From this point forward we keep holding onto the tasklist lock
1448 * so that our parent does not change from under us. -DaveM
1449 */
1450 write_lock_irq(&tasklist_lock);
1451
1452 err = -ESRCH;
1453 p = find_task_by_pid(pid);
1454 if (!p)
1455 goto out;
1456
1457 err = -EINVAL;
1458 if (!thread_group_leader(p))
1459 goto out;
1460
Oleg Nesterovf7dd7952006-01-08 01:03:59 -08001461 if (p->real_parent == group_leader) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -08001463 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto out;
1465 err = -EACCES;
1466 if (p->did_exec)
1467 goto out;
1468 } else {
1469 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001470 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 goto out;
1472 }
1473
1474 err = -EPERM;
1475 if (p->signal->leader)
1476 goto out;
1477
1478 if (pgid != pid) {
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001479 struct task_struct *g =
1480 find_task_by_pid_type(PIDTYPE_PGID, pgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Eric W. Biederman41487c62007-02-12 00:53:01 -08001482 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 err = security_task_setpgid(p, pgid);
1487 if (err)
1488 goto out;
1489
1490 if (process_group(p) != pgid) {
1491 detach_pid(p, PIDTYPE_PGID);
1492 p->signal->pgrp = pgid;
Sukadev Bhattiprolue713d0d2007-05-10 22:22:58 -07001493 attach_pid(p, PIDTYPE_PGID, find_pid(pgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495
1496 err = 0;
1497out:
1498 /* All paths lead to here, thus we are safe. -DaveM */
1499 write_unlock_irq(&tasklist_lock);
1500 return err;
1501}
1502
1503asmlinkage long sys_getpgid(pid_t pid)
1504{
Cal Peake756184b2006-09-30 23:27:24 -07001505 if (!pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return process_group(current);
Cal Peake756184b2006-09-30 23:27:24 -07001507 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 int retval;
1509 struct task_struct *p;
1510
1511 read_lock(&tasklist_lock);
1512 p = find_task_by_pid(pid);
1513
1514 retval = -ESRCH;
1515 if (p) {
1516 retval = security_task_getpgid(p);
1517 if (!retval)
1518 retval = process_group(p);
1519 }
1520 read_unlock(&tasklist_lock);
1521 return retval;
1522 }
1523}
1524
1525#ifdef __ARCH_WANT_SYS_GETPGRP
1526
1527asmlinkage long sys_getpgrp(void)
1528{
1529 /* SMP - assuming writes are word atomic this is fine */
1530 return process_group(current);
1531}
1532
1533#endif
1534
1535asmlinkage long sys_getsid(pid_t pid)
1536{
Cal Peake756184b2006-09-30 23:27:24 -07001537 if (!pid)
Cedric Le Goater937949d2006-12-08 02:37:54 -08001538 return process_session(current);
Cal Peake756184b2006-09-30 23:27:24 -07001539 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int retval;
1541 struct task_struct *p;
1542
1543 read_lock(&tasklist_lock);
1544 p = find_task_by_pid(pid);
1545
1546 retval = -ESRCH;
Cal Peake756184b2006-09-30 23:27:24 -07001547 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 retval = security_task_getsid(p);
1549 if (!retval)
Cedric Le Goater937949d2006-12-08 02:37:54 -08001550 retval = process_session(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 read_unlock(&tasklist_lock);
1553 return retval;
1554 }
1555}
1556
1557asmlinkage long sys_setsid(void)
1558{
Oren Laadane19f2472006-01-08 01:03:58 -08001559 struct task_struct *group_leader = current->group_leader;
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001560 pid_t session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int err = -EPERM;
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 write_lock_irq(&tasklist_lock);
1564
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001565 /* Fail if I am already a session leader */
1566 if (group_leader->signal->leader)
1567 goto out;
1568
1569 session = group_leader->pid;
1570 /* Fail if a process group id already exists that equals the
1571 * proposed session id.
1572 *
1573 * Don't check if session id == 1 because kernel threads use this
1574 * session id and so the check will always fail and make it so
1575 * init cannot successfully call setsid.
1576 */
1577 if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 goto out;
1579
Oren Laadane19f2472006-01-08 01:03:58 -08001580 group_leader->signal->leader = 1;
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001581 __set_special_pids(session, session);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001582
1583 spin_lock(&group_leader->sighand->siglock);
Oren Laadane19f2472006-01-08 01:03:58 -08001584 group_leader->signal->tty = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001585 spin_unlock(&group_leader->sighand->siglock);
1586
Oren Laadane19f2472006-01-08 01:03:58 -08001587 err = process_group(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588out:
1589 write_unlock_irq(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return err;
1591}
1592
1593/*
1594 * Supplementary group IDs
1595 */
1596
1597/* init to 2 - one for init_task, one to ensure it is never freed */
1598struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1599
1600struct group_info *groups_alloc(int gidsetsize)
1601{
1602 struct group_info *group_info;
1603 int nblocks;
1604 int i;
1605
1606 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1607 /* Make sure we always allocate at least one indirect block pointer */
1608 nblocks = nblocks ? : 1;
1609 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1610 if (!group_info)
1611 return NULL;
1612 group_info->ngroups = gidsetsize;
1613 group_info->nblocks = nblocks;
1614 atomic_set(&group_info->usage, 1);
1615
Cal Peake756184b2006-09-30 23:27:24 -07001616 if (gidsetsize <= NGROUPS_SMALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 group_info->blocks[0] = group_info->small_block;
Cal Peake756184b2006-09-30 23:27:24 -07001618 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 for (i = 0; i < nblocks; i++) {
1620 gid_t *b;
1621 b = (void *)__get_free_page(GFP_USER);
1622 if (!b)
1623 goto out_undo_partial_alloc;
1624 group_info->blocks[i] = b;
1625 }
1626 }
1627 return group_info;
1628
1629out_undo_partial_alloc:
1630 while (--i >= 0) {
1631 free_page((unsigned long)group_info->blocks[i]);
1632 }
1633 kfree(group_info);
1634 return NULL;
1635}
1636
1637EXPORT_SYMBOL(groups_alloc);
1638
1639void groups_free(struct group_info *group_info)
1640{
1641 if (group_info->blocks[0] != group_info->small_block) {
1642 int i;
1643 for (i = 0; i < group_info->nblocks; i++)
1644 free_page((unsigned long)group_info->blocks[i]);
1645 }
1646 kfree(group_info);
1647}
1648
1649EXPORT_SYMBOL(groups_free);
1650
1651/* export the group_info to a user-space array */
1652static int groups_to_user(gid_t __user *grouplist,
1653 struct group_info *group_info)
1654{
1655 int i;
1656 int count = group_info->ngroups;
1657
1658 for (i = 0; i < group_info->nblocks; i++) {
1659 int cp_count = min(NGROUPS_PER_BLOCK, count);
1660 int off = i * NGROUPS_PER_BLOCK;
1661 int len = cp_count * sizeof(*grouplist);
1662
1663 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1664 return -EFAULT;
1665
1666 count -= cp_count;
1667 }
1668 return 0;
1669}
1670
1671/* fill a group_info from a user-space array - it must be allocated already */
1672static int groups_from_user(struct group_info *group_info,
1673 gid_t __user *grouplist)
Cal Peake756184b2006-09-30 23:27:24 -07001674{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int i;
1676 int count = group_info->ngroups;
1677
1678 for (i = 0; i < group_info->nblocks; i++) {
1679 int cp_count = min(NGROUPS_PER_BLOCK, count);
1680 int off = i * NGROUPS_PER_BLOCK;
1681 int len = cp_count * sizeof(*grouplist);
1682
1683 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1684 return -EFAULT;
1685
1686 count -= cp_count;
1687 }
1688 return 0;
1689}
1690
Domen Puncerebe8b542005-05-05 16:16:19 -07001691/* a simple Shell sort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692static void groups_sort(struct group_info *group_info)
1693{
1694 int base, max, stride;
1695 int gidsetsize = group_info->ngroups;
1696
1697 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1698 ; /* nothing */
1699 stride /= 3;
1700
1701 while (stride) {
1702 max = gidsetsize - stride;
1703 for (base = 0; base < max; base++) {
1704 int left = base;
1705 int right = left + stride;
1706 gid_t tmp = GROUP_AT(group_info, right);
1707
1708 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1709 GROUP_AT(group_info, right) =
1710 GROUP_AT(group_info, left);
1711 right = left;
1712 left -= stride;
1713 }
1714 GROUP_AT(group_info, right) = tmp;
1715 }
1716 stride /= 3;
1717 }
1718}
1719
1720/* a simple bsearch */
David Howells3e301482005-06-23 22:00:56 -07001721int groups_search(struct group_info *group_info, gid_t grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001723 unsigned int left, right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 if (!group_info)
1726 return 0;
1727
1728 left = 0;
1729 right = group_info->ngroups;
1730 while (left < right) {
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001731 unsigned int mid = (left+right)/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 int cmp = grp - GROUP_AT(group_info, mid);
1733 if (cmp > 0)
1734 left = mid + 1;
1735 else if (cmp < 0)
1736 right = mid;
1737 else
1738 return 1;
1739 }
1740 return 0;
1741}
1742
1743/* validate and set current->group_info */
1744int set_current_groups(struct group_info *group_info)
1745{
1746 int retval;
1747 struct group_info *old_info;
1748
1749 retval = security_task_setgroups(group_info);
1750 if (retval)
1751 return retval;
1752
1753 groups_sort(group_info);
1754 get_group_info(group_info);
1755
1756 task_lock(current);
1757 old_info = current->group_info;
1758 current->group_info = group_info;
1759 task_unlock(current);
1760
1761 put_group_info(old_info);
1762
1763 return 0;
1764}
1765
1766EXPORT_SYMBOL(set_current_groups);
1767
1768asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1769{
1770 int i = 0;
1771
1772 /*
1773 * SMP: Nobody else can change our grouplist. Thus we are
1774 * safe.
1775 */
1776
1777 if (gidsetsize < 0)
1778 return -EINVAL;
1779
1780 /* no need to grab task_lock here; it cannot change */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 i = current->group_info->ngroups;
1782 if (gidsetsize) {
1783 if (i > gidsetsize) {
1784 i = -EINVAL;
1785 goto out;
1786 }
1787 if (groups_to_user(grouplist, current->group_info)) {
1788 i = -EFAULT;
1789 goto out;
1790 }
1791 }
1792out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return i;
1794}
1795
1796/*
1797 * SMP: Our groups are copy-on-write. We can set them safely
1798 * without another task interfering.
1799 */
1800
1801asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1802{
1803 struct group_info *group_info;
1804 int retval;
1805
1806 if (!capable(CAP_SETGID))
1807 return -EPERM;
1808 if ((unsigned)gidsetsize > NGROUPS_MAX)
1809 return -EINVAL;
1810
1811 group_info = groups_alloc(gidsetsize);
1812 if (!group_info)
1813 return -ENOMEM;
1814 retval = groups_from_user(group_info, grouplist);
1815 if (retval) {
1816 put_group_info(group_info);
1817 return retval;
1818 }
1819
1820 retval = set_current_groups(group_info);
1821 put_group_info(group_info);
1822
1823 return retval;
1824}
1825
1826/*
1827 * Check whether we're fsgid/egid or in the supplemental group..
1828 */
1829int in_group_p(gid_t grp)
1830{
1831 int retval = 1;
Cal Peake756184b2006-09-30 23:27:24 -07001832 if (grp != current->fsgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 retval = groups_search(current->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return retval;
1835}
1836
1837EXPORT_SYMBOL(in_group_p);
1838
1839int in_egroup_p(gid_t grp)
1840{
1841 int retval = 1;
Cal Peake756184b2006-09-30 23:27:24 -07001842 if (grp != current->egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 retval = groups_search(current->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return retval;
1845}
1846
1847EXPORT_SYMBOL(in_egroup_p);
1848
1849DECLARE_RWSEM(uts_sem);
1850
David S. Miller393b0722005-11-10 12:47:50 -08001851EXPORT_SYMBOL(uts_sem);
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853asmlinkage long sys_newuname(struct new_utsname __user * name)
1854{
1855 int errno = 0;
1856
1857 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001858 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 errno = -EFAULT;
1860 up_read(&uts_sem);
1861 return errno;
1862}
1863
1864asmlinkage long sys_sethostname(char __user *name, int len)
1865{
1866 int errno;
1867 char tmp[__NEW_UTS_LEN];
1868
1869 if (!capable(CAP_SYS_ADMIN))
1870 return -EPERM;
1871 if (len < 0 || len > __NEW_UTS_LEN)
1872 return -EINVAL;
1873 down_write(&uts_sem);
1874 errno = -EFAULT;
1875 if (!copy_from_user(tmp, name, len)) {
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001876 memcpy(utsname()->nodename, tmp, len);
1877 utsname()->nodename[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 errno = 0;
1879 }
1880 up_write(&uts_sem);
1881 return errno;
1882}
1883
1884#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1885
1886asmlinkage long sys_gethostname(char __user *name, int len)
1887{
1888 int i, errno;
1889
1890 if (len < 0)
1891 return -EINVAL;
1892 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001893 i = 1 + strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (i > len)
1895 i = len;
1896 errno = 0;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001897 if (copy_to_user(name, utsname()->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 errno = -EFAULT;
1899 up_read(&uts_sem);
1900 return errno;
1901}
1902
1903#endif
1904
1905/*
1906 * Only setdomainname; getdomainname can be implemented by calling
1907 * uname()
1908 */
1909asmlinkage long sys_setdomainname(char __user *name, int len)
1910{
1911 int errno;
1912 char tmp[__NEW_UTS_LEN];
1913
1914 if (!capable(CAP_SYS_ADMIN))
1915 return -EPERM;
1916 if (len < 0 || len > __NEW_UTS_LEN)
1917 return -EINVAL;
1918
1919 down_write(&uts_sem);
1920 errno = -EFAULT;
1921 if (!copy_from_user(tmp, name, len)) {
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001922 memcpy(utsname()->domainname, tmp, len);
1923 utsname()->domainname[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 errno = 0;
1925 }
1926 up_write(&uts_sem);
1927 return errno;
1928}
1929
1930asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1931{
1932 if (resource >= RLIM_NLIMITS)
1933 return -EINVAL;
1934 else {
1935 struct rlimit value;
1936 task_lock(current->group_leader);
1937 value = current->signal->rlim[resource];
1938 task_unlock(current->group_leader);
1939 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1940 }
1941}
1942
1943#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1944
1945/*
1946 * Back compatibility for getrlimit. Needed for some apps.
1947 */
1948
1949asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1950{
1951 struct rlimit x;
1952 if (resource >= RLIM_NLIMITS)
1953 return -EINVAL;
1954
1955 task_lock(current->group_leader);
1956 x = current->signal->rlim[resource];
1957 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001958 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001960 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 x.rlim_max = 0x7FFFFFFF;
1962 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1963}
1964
1965#endif
1966
1967asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1968{
1969 struct rlimit new_rlim, *old_rlim;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001970 unsigned long it_prof_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 int retval;
1972
1973 if (resource >= RLIM_NLIMITS)
1974 return -EINVAL;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001975 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return -EFAULT;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001977 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1978 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 old_rlim = current->signal->rlim + resource;
1980 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1981 !capable(CAP_SYS_RESOURCE))
1982 return -EPERM;
1983 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001984 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 retval = security_task_setrlimit(resource, &new_rlim);
1987 if (retval)
1988 return retval;
1989
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001990 if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
1991 /*
1992 * The caller is asking for an immediate RLIMIT_CPU
1993 * expiry. But we use the zero value to mean "it was
1994 * never set". So let's cheat and make it one second
1995 * instead
1996 */
1997 new_rlim.rlim_cur = 1;
1998 }
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 task_lock(current->group_leader);
2001 *old_rlim = new_rlim;
2002 task_unlock(current->group_leader);
2003
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002004 if (resource != RLIMIT_CPU)
2005 goto out;
Andrew Mortond3561f72006-03-24 03:18:36 -08002006
2007 /*
2008 * RLIMIT_CPU handling. Note that the kernel fails to return an error
2009 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
2010 * very long-standing error, and fixing it now risks breakage of
2011 * applications, so we live with it
2012 */
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002013 if (new_rlim.rlim_cur == RLIM_INFINITY)
2014 goto out;
2015
2016 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
2017 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
Andrew Mortone0661112006-03-24 03:18:35 -08002018 unsigned long rlim_cur = new_rlim.rlim_cur;
2019 cputime_t cputime;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002020
Andrew Mortone0661112006-03-24 03:18:35 -08002021 cputime = secs_to_cputime(rlim_cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 read_lock(&tasklist_lock);
2023 spin_lock_irq(&current->sighand->siglock);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002024 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 spin_unlock_irq(&current->sighand->siglock);
2026 read_unlock(&tasklist_lock);
2027 }
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002028out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return 0;
2030}
2031
2032/*
2033 * It would make sense to put struct rusage in the task_struct,
2034 * except that would make the task_struct be *really big*. After
2035 * task_struct gets moved into malloc'ed memory, it would
2036 * make sense to do this. It will make moving the rest of the information
2037 * a lot simpler! (Which we're not doing right now because we're not
2038 * measuring them yet).
2039 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
2041 * races with threads incrementing their own counters. But since word
2042 * reads are atomic, we either get new values or old values and we don't
2043 * care which for the sums. We always take the siglock to protect reading
2044 * the c* fields from p->signal from races with exit.c updating those
2045 * fields when reaping, so a sample either gets all the additions of a
2046 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08002047 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002048 * Locking:
2049 * We need to take the siglock for CHILDEREN, SELF and BOTH
2050 * for the cases current multithreaded, non-current single threaded
2051 * non-current multithreaded. Thread traversal is now safe with
2052 * the siglock held.
2053 * Strictly speaking, we donot need to take the siglock if we are current and
2054 * single threaded, as no one else can take our signal_struct away, no one
2055 * else can reap the children to update signal->c* counters, and no one else
2056 * can race with the signal-> fields. If we do not take any lock, the
2057 * signal-> fields could be read out of order while another thread was just
2058 * exiting. So we should place a read memory barrier when we avoid the lock.
2059 * On the writer side, write memory barrier is implied in __exit_signal
2060 * as __exit_signal releases the siglock spinlock after updating the signal->
2061 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08002062 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
2064
2065static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
2066{
2067 struct task_struct *t;
2068 unsigned long flags;
2069 cputime_t utime, stime;
2070
2071 memset((char *) r, 0, sizeof *r);
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002072 utime = stime = cputime_zero;
2073
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002074 rcu_read_lock();
2075 if (!lock_task_sighand(p, &flags)) {
2076 rcu_read_unlock();
2077 return;
2078 }
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002081 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 utime = p->signal->cutime;
2084 stime = p->signal->cstime;
2085 r->ru_nvcsw = p->signal->cnvcsw;
2086 r->ru_nivcsw = p->signal->cnivcsw;
2087 r->ru_minflt = p->signal->cmin_flt;
2088 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07002089 r->ru_inblock = p->signal->cinblock;
2090 r->ru_oublock = p->signal->coublock;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002091
2092 if (who == RUSAGE_CHILDREN)
2093 break;
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 case RUSAGE_SELF:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 utime = cputime_add(utime, p->signal->utime);
2097 stime = cputime_add(stime, p->signal->stime);
2098 r->ru_nvcsw += p->signal->nvcsw;
2099 r->ru_nivcsw += p->signal->nivcsw;
2100 r->ru_minflt += p->signal->min_flt;
2101 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07002102 r->ru_inblock += p->signal->inblock;
2103 r->ru_oublock += p->signal->oublock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 t = p;
2105 do {
2106 utime = cputime_add(utime, t->utime);
2107 stime = cputime_add(stime, t->stime);
2108 r->ru_nvcsw += t->nvcsw;
2109 r->ru_nivcsw += t->nivcsw;
2110 r->ru_minflt += t->min_flt;
2111 r->ru_majflt += t->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07002112 r->ru_inblock += task_io_get_inblock(t);
2113 r->ru_oublock += task_io_get_oublock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 t = next_thread(t);
2115 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 default:
2119 BUG();
2120 }
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002121
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002122 unlock_task_sighand(p, &flags);
2123 rcu_read_unlock();
2124
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002125 cputime_to_timeval(utime, &r->ru_utime);
2126 cputime_to_timeval(stime, &r->ru_stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
2130{
2131 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
2134}
2135
2136asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
2137{
2138 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
2139 return -EINVAL;
2140 return getrusage(current, who, ru);
2141}
2142
2143asmlinkage long sys_umask(int mask)
2144{
2145 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
2146 return mask;
2147}
2148
2149asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
2150 unsigned long arg4, unsigned long arg5)
2151{
2152 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2155 if (error)
2156 return error;
2157
2158 switch (option) {
2159 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07002160 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 error = -EINVAL;
2162 break;
2163 }
Jesper Juhl0730ded2005-09-06 15:17:37 -07002164 current->pdeath_signal = arg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 break;
2166 case PR_GET_PDEATHSIG:
2167 error = put_user(current->pdeath_signal, (int __user *)arg2);
2168 break;
2169 case PR_GET_DUMPABLE:
Michael Kerrisk2030c0f2005-09-16 19:28:02 -07002170 error = current->mm->dumpable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 break;
2172 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02002173 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 error = -EINVAL;
2175 break;
2176 }
2177 current->mm->dumpable = arg2;
2178 break;
2179
2180 case PR_SET_UNALIGN:
2181 error = SET_UNALIGN_CTL(current, arg2);
2182 break;
2183 case PR_GET_UNALIGN:
2184 error = GET_UNALIGN_CTL(current, arg2);
2185 break;
2186 case PR_SET_FPEMU:
2187 error = SET_FPEMU_CTL(current, arg2);
2188 break;
2189 case PR_GET_FPEMU:
2190 error = GET_FPEMU_CTL(current, arg2);
2191 break;
2192 case PR_SET_FPEXC:
2193 error = SET_FPEXC_CTL(current, arg2);
2194 break;
2195 case PR_GET_FPEXC:
2196 error = GET_FPEXC_CTL(current, arg2);
2197 break;
2198 case PR_GET_TIMING:
2199 error = PR_TIMING_STATISTICAL;
2200 break;
2201 case PR_SET_TIMING:
2202 if (arg2 == PR_TIMING_STATISTICAL)
2203 error = 0;
2204 else
2205 error = -EINVAL;
2206 break;
2207
2208 case PR_GET_KEEPCAPS:
2209 if (current->keep_capabilities)
2210 error = 1;
2211 break;
2212 case PR_SET_KEEPCAPS:
2213 if (arg2 != 0 && arg2 != 1) {
2214 error = -EINVAL;
2215 break;
2216 }
2217 current->keep_capabilities = arg2;
2218 break;
2219 case PR_SET_NAME: {
2220 struct task_struct *me = current;
2221 unsigned char ncomm[sizeof(me->comm)];
2222
2223 ncomm[sizeof(me->comm)-1] = 0;
2224 if (strncpy_from_user(ncomm, (char __user *)arg2,
2225 sizeof(me->comm)-1) < 0)
2226 return -EFAULT;
2227 set_task_comm(me, ncomm);
2228 return 0;
2229 }
2230 case PR_GET_NAME: {
2231 struct task_struct *me = current;
2232 unsigned char tcomm[sizeof(me->comm)];
2233
2234 get_task_comm(tcomm, me);
2235 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2236 return -EFAULT;
2237 return 0;
2238 }
Anton Blanchard651d7652006-06-07 16:10:19 +10002239 case PR_GET_ENDIAN:
2240 error = GET_ENDIAN(current, arg2);
2241 break;
2242 case PR_SET_ENDIAN:
2243 error = SET_ENDIAN(current, arg2);
2244 break;
2245
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -07002246 case PR_GET_SECCOMP:
2247 error = prctl_get_seccomp();
2248 break;
2249 case PR_SET_SECCOMP:
2250 error = prctl_set_seccomp(arg2);
2251 break;
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 default:
2254 error = -EINVAL;
2255 break;
2256 }
2257 return error;
2258}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002259
2260asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2261 struct getcpu_cache __user *cache)
2262{
2263 int err = 0;
2264 int cpu = raw_smp_processor_id();
2265 if (cpup)
2266 err |= put_user(cpu, cpup);
2267 if (nodep)
2268 err |= put_user(cpu_to_node(cpu), nodep);
2269 if (cache) {
2270 /*
2271 * The cache is not needed for this implementation,
2272 * but make sure user programs pass something
2273 * valid. vsyscall implementations can instead make
2274 * good use of the cache. Only use t0 and t1 because
2275 * these are available in both 32bit and 64bit ABI (no
2276 * need for a compat_getcpu). 32bit has enough
2277 * padding
2278 */
2279 unsigned long t0, t1;
Andi Kleen34596dc2006-09-30 01:47:55 +02002280 get_user(t0, &cache->blob[0]);
2281 get_user(t1, &cache->blob[1]);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002282 t0++;
2283 t1++;
Andi Kleen34596dc2006-09-30 01:47:55 +02002284 put_user(t0, &cache->blob[0]);
2285 put_user(t1, &cache->blob[1]);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002286 }
2287 return err ? -EFAULT : 0;
2288}