blob: ec319bbb0bd4ac7c6644f00f26383017ca54ffd7 [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>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070017#include <linux/kernel.h>
18#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/device.h>
22#include <linux/key.h>
23#include <linux/times.h>
24#include <linux/posix-timers.h>
25#include <linux/security.h>
26#include <linux/dcookies.h>
27#include <linux/suspend.h>
28#include <linux/tty.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070029#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080030#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020031#include <linux/getcpu.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070032#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <linux/compat.h>
35#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080036#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/uaccess.h>
39#include <asm/io.h>
40#include <asm/unistd.h>
41
42#ifndef SET_UNALIGN_CTL
43# define SET_UNALIGN_CTL(a,b) (-EINVAL)
44#endif
45#ifndef GET_UNALIGN_CTL
46# define GET_UNALIGN_CTL(a,b) (-EINVAL)
47#endif
48#ifndef SET_FPEMU_CTL
49# define SET_FPEMU_CTL(a,b) (-EINVAL)
50#endif
51#ifndef GET_FPEMU_CTL
52# define GET_FPEMU_CTL(a,b) (-EINVAL)
53#endif
54#ifndef SET_FPEXC_CTL
55# define SET_FPEXC_CTL(a,b) (-EINVAL)
56#endif
57#ifndef GET_FPEXC_CTL
58# define GET_FPEXC_CTL(a,b) (-EINVAL)
59#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100060#ifndef GET_ENDIAN
61# define GET_ENDIAN(a,b) (-EINVAL)
62#endif
63#ifndef SET_ENDIAN
64# define SET_ENDIAN(a,b) (-EINVAL)
65#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
68 * this is where the system-wide overflow UID and GID are defined, for
69 * architectures that now have 32-bit UID/GID but didn't in the past
70 */
71
72int overflowuid = DEFAULT_OVERFLOWUID;
73int overflowgid = DEFAULT_OVERFLOWGID;
74
75#ifdef CONFIG_UID16
76EXPORT_SYMBOL(overflowuid);
77EXPORT_SYMBOL(overflowgid);
78#endif
79
80/*
81 * the same as above, but for filesystems which can only store a 16-bit
82 * UID and GID. as such, this is needed on all architectures
83 */
84
85int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
86int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
87
88EXPORT_SYMBOL(fs_overflowuid);
89EXPORT_SYMBOL(fs_overflowgid);
90
91/*
92 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
93 */
94
95int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -070096struct pid *cad_pid;
97EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
100 * Notifier list for kernel code which wants to be called
101 * at shutdown. This is used to stop any idling DMA operations
102 * and the like.
103 */
104
Alan Sterne041c682006-03-27 01:16:30 -0800105static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Alan Sterne041c682006-03-27 01:16:30 -0800107/*
108 * Notifier chain core routines. The exported routines below
109 * are layered on top of these, with appropriate locking added.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
Alan Sterne041c682006-03-27 01:16:30 -0800111
112static int notifier_chain_register(struct notifier_block **nl,
113 struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Alan Sterne041c682006-03-27 01:16:30 -0800115 while ((*nl) != NULL) {
116 if (n->priority > (*nl)->priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 break;
Alan Sterne041c682006-03-27 01:16:30 -0800118 nl = &((*nl)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Alan Sterne041c682006-03-27 01:16:30 -0800120 n->next = *nl;
121 rcu_assign_pointer(*nl, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 0;
123}
124
Alan Sterne041c682006-03-27 01:16:30 -0800125static int notifier_chain_unregister(struct notifier_block **nl,
126 struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Alan Sterne041c682006-03-27 01:16:30 -0800128 while ((*nl) != NULL) {
129 if ((*nl) == n) {
130 rcu_assign_pointer(*nl, n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return 0;
132 }
Alan Sterne041c682006-03-27 01:16:30 -0800133 nl = &((*nl)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -ENOENT;
136}
137
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700138/**
139 * notifier_call_chain - Informs the registered notifiers about an event.
140 * @nl: Pointer to head of the blocking notifier chain
141 * @val: Value passed unmodified to notifier function
142 * @v: Pointer passed unmodified to notifier function
143 * @nr_to_call: Number of notifier functions to be called. Don't care
144 * value of this parameter is -1.
145 * @nr_calls: Records the number of notifications sent. Don't care
146 * value of this field is NULL.
147 * @returns: notifier_call_chain returns the value returned by the
148 * last notifier function called.
149 */
150
Alan Sterne041c682006-03-27 01:16:30 -0800151static int __kprobes notifier_call_chain(struct notifier_block **nl,
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700152 unsigned long val, void *v,
153 int nr_to_call, int *nr_calls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Alan Sterne041c682006-03-27 01:16:30 -0800155 int ret = NOTIFY_DONE;
Alan Sternbbb17472006-06-25 05:47:15 -0700156 struct notifier_block *nb, *next_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Alan Sterne041c682006-03-27 01:16:30 -0800158 nb = rcu_dereference(*nl);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700159
160 while (nb && nr_to_call) {
Alan Sternbbb17472006-06-25 05:47:15 -0700161 next_nb = rcu_dereference(nb->next);
Alan Sterne041c682006-03-27 01:16:30 -0800162 ret = nb->notifier_call(nb, val, v);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700163
164 if (nr_calls)
165 (*nr_calls)++;
166
Alan Sterne041c682006-03-27 01:16:30 -0800167 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
168 break;
Alan Sternbbb17472006-06-25 05:47:15 -0700169 nb = next_nb;
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700170 nr_to_call--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 return ret;
173}
174
Alan Sterne041c682006-03-27 01:16:30 -0800175/*
176 * Atomic notifier chain routines. Registration and unregistration
Alan Sterneabc0692006-10-04 02:17:04 -0700177 * use a spinlock, and call_chain is synchronized by RCU (no locks).
Alan Sterne041c682006-03-27 01:16:30 -0800178 */
179
180/**
181 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
182 * @nh: Pointer to head of the atomic notifier chain
183 * @n: New entry in notifier chain
184 *
185 * Adds a notifier to an atomic notifier chain.
186 *
187 * Currently always returns zero.
188 */
189
190int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
191 struct notifier_block *n)
192{
193 unsigned long flags;
194 int ret;
195
196 spin_lock_irqsave(&nh->lock, flags);
197 ret = notifier_chain_register(&nh->head, n);
198 spin_unlock_irqrestore(&nh->lock, flags);
199 return ret;
200}
201
202EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
203
204/**
205 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
206 * @nh: Pointer to head of the atomic notifier chain
207 * @n: Entry to remove from notifier chain
208 *
209 * Removes a notifier from an atomic notifier chain.
210 *
211 * Returns zero on success or %-ENOENT on failure.
212 */
213int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
214 struct notifier_block *n)
215{
216 unsigned long flags;
217 int ret;
218
219 spin_lock_irqsave(&nh->lock, flags);
220 ret = notifier_chain_unregister(&nh->head, n);
221 spin_unlock_irqrestore(&nh->lock, flags);
222 synchronize_rcu();
223 return ret;
224}
225
226EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
227
228/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700229 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
Alan Sterne041c682006-03-27 01:16:30 -0800230 * @nh: Pointer to head of the atomic notifier chain
231 * @val: Value passed unmodified to notifier function
232 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700233 * @nr_to_call: See the comment for notifier_call_chain.
234 * @nr_calls: See the comment for notifier_call_chain.
Alan Sterne041c682006-03-27 01:16:30 -0800235 *
236 * Calls each function in a notifier chain in turn. The functions
237 * run in an atomic context, so they must not block.
238 * This routine uses RCU to synchronize with changes to the chain.
239 *
240 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800241 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800242 * will return immediately, with the return value of
243 * the notifier function which halted execution.
244 * Otherwise the return value is the return value
245 * of the last notifier function called.
246 */
247
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700248int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
249 unsigned long val, void *v,
250 int nr_to_call, int *nr_calls)
Alan Sterne041c682006-03-27 01:16:30 -0800251{
252 int ret;
253
254 rcu_read_lock();
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700255 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
Alan Sterne041c682006-03-27 01:16:30 -0800256 rcu_read_unlock();
257 return ret;
258}
259
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700260EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
Alan Sterne041c682006-03-27 01:16:30 -0800261
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700262int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
263 unsigned long val, void *v)
264{
265 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
266}
267
268EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
Alan Sterne041c682006-03-27 01:16:30 -0800269/*
270 * Blocking notifier chain routines. All access to the chain is
271 * synchronized by an rwsem.
272 */
273
274/**
275 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
276 * @nh: Pointer to head of the blocking notifier chain
277 * @n: New entry in notifier chain
278 *
279 * Adds a notifier to a blocking notifier chain.
280 * Must be called in process context.
281 *
282 * Currently always returns zero.
283 */
284
285int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
286 struct notifier_block *n)
287{
288 int ret;
289
290 /*
291 * This code gets used during boot-up, when task switching is
292 * not yet working and interrupts must remain disabled. At
293 * such times we must not call down_write().
294 */
295 if (unlikely(system_state == SYSTEM_BOOTING))
296 return notifier_chain_register(&nh->head, n);
297
298 down_write(&nh->rwsem);
299 ret = notifier_chain_register(&nh->head, n);
300 up_write(&nh->rwsem);
301 return ret;
302}
303
304EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
305
306/**
307 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
308 * @nh: Pointer to head of the blocking notifier chain
309 * @n: Entry to remove from notifier chain
310 *
311 * Removes a notifier from a blocking notifier chain.
312 * Must be called from process context.
313 *
314 * Returns zero on success or %-ENOENT on failure.
315 */
316int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
317 struct notifier_block *n)
318{
319 int ret;
320
321 /*
322 * This code gets used during boot-up, when task switching is
323 * not yet working and interrupts must remain disabled. At
324 * such times we must not call down_write().
325 */
326 if (unlikely(system_state == SYSTEM_BOOTING))
327 return notifier_chain_unregister(&nh->head, n);
328
329 down_write(&nh->rwsem);
330 ret = notifier_chain_unregister(&nh->head, n);
331 up_write(&nh->rwsem);
332 return ret;
333}
334
335EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
336
337/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700338 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
Alan Sterne041c682006-03-27 01:16:30 -0800339 * @nh: Pointer to head of the blocking notifier chain
340 * @val: Value passed unmodified to notifier function
341 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700342 * @nr_to_call: See comment for notifier_call_chain.
343 * @nr_calls: See comment for notifier_call_chain.
Alan Sterne041c682006-03-27 01:16:30 -0800344 *
345 * Calls each function in a notifier chain in turn. The functions
346 * run in a process context, so they are allowed to block.
347 *
348 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800349 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800350 * will return immediately, with the return value of
351 * the notifier function which halted execution.
352 * Otherwise the return value is the return value
353 * of the last notifier function called.
354 */
355
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700356int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
357 unsigned long val, void *v,
358 int nr_to_call, int *nr_calls)
Alan Sterne041c682006-03-27 01:16:30 -0800359{
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100360 int ret = NOTIFY_DONE;
Alan Sterne041c682006-03-27 01:16:30 -0800361
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100362 /*
363 * We check the head outside the lock, but if this access is
364 * racy then it does not matter what the result of the test
365 * is, we re-check the list after having taken the lock anyway:
366 */
367 if (rcu_dereference(nh->head)) {
368 down_read(&nh->rwsem);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700369 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
370 nr_calls);
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100371 up_read(&nh->rwsem);
372 }
Alan Sterne041c682006-03-27 01:16:30 -0800373 return ret;
374}
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700375EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
Alan Sterne041c682006-03-27 01:16:30 -0800376
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700377int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
378 unsigned long val, void *v)
379{
380 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
381}
Alan Sterne041c682006-03-27 01:16:30 -0800382EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
383
384/*
385 * Raw notifier chain routines. There is no protection;
386 * the caller must provide it. Use at your own risk!
387 */
388
389/**
390 * raw_notifier_chain_register - Add notifier to a raw notifier chain
391 * @nh: Pointer to head of the raw notifier chain
392 * @n: New entry in notifier chain
393 *
394 * Adds a notifier to a raw notifier chain.
395 * All locking must be provided by the caller.
396 *
397 * Currently always returns zero.
398 */
399
400int raw_notifier_chain_register(struct raw_notifier_head *nh,
401 struct notifier_block *n)
402{
403 return notifier_chain_register(&nh->head, n);
404}
405
406EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
407
408/**
409 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
410 * @nh: Pointer to head of the raw notifier chain
411 * @n: Entry to remove from notifier chain
412 *
413 * Removes a notifier from a raw notifier chain.
414 * All locking must be provided by the caller.
415 *
416 * Returns zero on success or %-ENOENT on failure.
417 */
418int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
419 struct notifier_block *n)
420{
421 return notifier_chain_unregister(&nh->head, n);
422}
423
424EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
425
426/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700427 * __raw_notifier_call_chain - Call functions in a raw notifier chain
Alan Sterne041c682006-03-27 01:16:30 -0800428 * @nh: Pointer to head of the raw notifier chain
429 * @val: Value passed unmodified to notifier function
430 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700431 * @nr_to_call: See comment for notifier_call_chain.
432 * @nr_calls: See comment for notifier_call_chain
Alan Sterne041c682006-03-27 01:16:30 -0800433 *
434 * Calls each function in a notifier chain in turn. The functions
435 * run in an undefined context.
436 * All locking must be provided by the caller.
437 *
438 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800439 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800440 * will return immediately, with the return value of
441 * the notifier function which halted execution.
442 * Otherwise the return value is the return value
443 * of the last notifier function called.
444 */
445
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700446int __raw_notifier_call_chain(struct raw_notifier_head *nh,
447 unsigned long val, void *v,
448 int nr_to_call, int *nr_calls)
449{
450 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
451}
452
453EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
454
Alan Sterne041c682006-03-27 01:16:30 -0800455int raw_notifier_call_chain(struct raw_notifier_head *nh,
456 unsigned long val, void *v)
457{
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700458 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
Alan Sterne041c682006-03-27 01:16:30 -0800459}
460
461EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Alan Sterneabc0692006-10-04 02:17:04 -0700463/*
464 * SRCU notifier chain routines. Registration and unregistration
465 * use a mutex, and call_chain is synchronized by SRCU (no locks).
466 */
467
468/**
469 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
470 * @nh: Pointer to head of the SRCU notifier chain
471 * @n: New entry in notifier chain
472 *
473 * Adds a notifier to an SRCU notifier chain.
474 * Must be called in process context.
475 *
476 * Currently always returns zero.
477 */
478
479int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
480 struct notifier_block *n)
481{
482 int ret;
483
484 /*
485 * This code gets used during boot-up, when task switching is
486 * not yet working and interrupts must remain disabled. At
487 * such times we must not call mutex_lock().
488 */
489 if (unlikely(system_state == SYSTEM_BOOTING))
490 return notifier_chain_register(&nh->head, n);
491
492 mutex_lock(&nh->mutex);
493 ret = notifier_chain_register(&nh->head, n);
494 mutex_unlock(&nh->mutex);
495 return ret;
496}
497
498EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
499
500/**
501 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
502 * @nh: Pointer to head of the SRCU notifier chain
503 * @n: Entry to remove from notifier chain
504 *
505 * Removes a notifier from an SRCU notifier chain.
506 * Must be called from process context.
507 *
508 * Returns zero on success or %-ENOENT on failure.
509 */
510int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
511 struct notifier_block *n)
512{
513 int ret;
514
515 /*
516 * This code gets used during boot-up, when task switching is
517 * not yet working and interrupts must remain disabled. At
518 * such times we must not call mutex_lock().
519 */
520 if (unlikely(system_state == SYSTEM_BOOTING))
521 return notifier_chain_unregister(&nh->head, n);
522
523 mutex_lock(&nh->mutex);
524 ret = notifier_chain_unregister(&nh->head, n);
525 mutex_unlock(&nh->mutex);
526 synchronize_srcu(&nh->srcu);
527 return ret;
528}
529
530EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
531
532/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700533 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
Alan Sterneabc0692006-10-04 02:17:04 -0700534 * @nh: Pointer to head of the SRCU notifier chain
535 * @val: Value passed unmodified to notifier function
536 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700537 * @nr_to_call: See comment for notifier_call_chain.
538 * @nr_calls: See comment for notifier_call_chain
Alan Sterneabc0692006-10-04 02:17:04 -0700539 *
540 * Calls each function in a notifier chain in turn. The functions
541 * run in a process context, so they are allowed to block.
542 *
543 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800544 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
Alan Sterneabc0692006-10-04 02:17:04 -0700545 * will return immediately, with the return value of
546 * the notifier function which halted execution.
547 * Otherwise the return value is the return value
548 * of the last notifier function called.
549 */
550
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700551int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
552 unsigned long val, void *v,
553 int nr_to_call, int *nr_calls)
Alan Sterneabc0692006-10-04 02:17:04 -0700554{
555 int ret;
556 int idx;
557
558 idx = srcu_read_lock(&nh->srcu);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700559 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
Alan Sterneabc0692006-10-04 02:17:04 -0700560 srcu_read_unlock(&nh->srcu, idx);
561 return ret;
562}
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700563EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
Alan Sterneabc0692006-10-04 02:17:04 -0700564
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700565int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
566 unsigned long val, void *v)
567{
568 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
569}
Alan Sterneabc0692006-10-04 02:17:04 -0700570EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
571
572/**
573 * srcu_init_notifier_head - Initialize an SRCU notifier head
574 * @nh: Pointer to head of the srcu notifier chain
575 *
576 * Unlike other sorts of notifier heads, SRCU notifier heads require
577 * dynamic initialization. Be sure to call this routine before
578 * calling any of the other SRCU notifier routines for this head.
579 *
580 * If an SRCU notifier head is deallocated, it must first be cleaned
581 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
582 * per-cpu data (used by the SRCU mechanism) will leak.
583 */
584
585void srcu_init_notifier_head(struct srcu_notifier_head *nh)
586{
587 mutex_init(&nh->mutex);
Alan Sterne6a92012006-10-04 02:17:05 -0700588 if (init_srcu_struct(&nh->srcu) < 0)
589 BUG();
Alan Sterneabc0692006-10-04 02:17:04 -0700590 nh->head = NULL;
591}
592
593EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/**
596 * register_reboot_notifier - Register function to be called at reboot time
597 * @nb: Info about notifier function to be called
598 *
599 * Registers a function with the list of functions
600 * to be called at reboot time.
601 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800602 * Currently always returns zero, as blocking_notifier_chain_register()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * always returns zero.
604 */
605
606int register_reboot_notifier(struct notifier_block * nb)
607{
Alan Sterne041c682006-03-27 01:16:30 -0800608 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611EXPORT_SYMBOL(register_reboot_notifier);
612
613/**
614 * unregister_reboot_notifier - Unregister previously registered reboot notifier
615 * @nb: Hook to be unregistered
616 *
617 * Unregisters a previously registered reboot
618 * notifier function.
619 *
620 * Returns zero on success, or %-ENOENT on failure.
621 */
622
623int unregister_reboot_notifier(struct notifier_block * nb)
624{
Alan Sterne041c682006-03-27 01:16:30 -0800625 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628EXPORT_SYMBOL(unregister_reboot_notifier);
629
630static int set_one_prio(struct task_struct *p, int niceval, int error)
631{
632 int no_nice;
633
634 if (p->uid != current->euid &&
635 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
636 error = -EPERM;
637 goto out;
638 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700639 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 error = -EACCES;
641 goto out;
642 }
643 no_nice = security_task_setnice(p, niceval);
644 if (no_nice) {
645 error = no_nice;
646 goto out;
647 }
648 if (error == -ESRCH)
649 error = 0;
650 set_user_nice(p, niceval);
651out:
652 return error;
653}
654
655asmlinkage long sys_setpriority(int which, int who, int niceval)
656{
657 struct task_struct *g, *p;
658 struct user_struct *user;
659 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800660 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (which > 2 || which < 0)
663 goto out;
664
665 /* normalize: avoid signed division (rounding problems) */
666 error = -ESRCH;
667 if (niceval < -20)
668 niceval = -20;
669 if (niceval > 19)
670 niceval = 19;
671
672 read_lock(&tasklist_lock);
673 switch (which) {
674 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800675 if (who)
676 p = find_task_by_pid(who);
677 else
678 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (p)
680 error = set_one_prio(p, niceval, error);
681 break;
682 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800683 if (who)
684 pgrp = find_pid(who);
685 else
686 pgrp = task_pgrp(current);
687 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 error = set_one_prio(p, niceval, error);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800689 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691 case PRIO_USER:
692 user = current->user;
693 if (!who)
694 who = current->uid;
695 else
696 if ((who != current->uid) && !(user = find_user(who)))
697 goto out_unlock; /* No processes for this user */
698
699 do_each_thread(g, p)
700 if (p->uid == who)
701 error = set_one_prio(p, niceval, error);
702 while_each_thread(g, p);
703 if (who != current->uid)
704 free_uid(user); /* For find_user() */
705 break;
706 }
707out_unlock:
708 read_unlock(&tasklist_lock);
709out:
710 return error;
711}
712
713/*
714 * Ugh. To avoid negative return values, "getpriority()" will
715 * not return the normal nice-value, but a negated value that
716 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
717 * to stay compatible.
718 */
719asmlinkage long sys_getpriority(int which, int who)
720{
721 struct task_struct *g, *p;
722 struct user_struct *user;
723 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800724 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (which > 2 || which < 0)
727 return -EINVAL;
728
729 read_lock(&tasklist_lock);
730 switch (which) {
731 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800732 if (who)
733 p = find_task_by_pid(who);
734 else
735 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (p) {
737 niceval = 20 - task_nice(p);
738 if (niceval > retval)
739 retval = niceval;
740 }
741 break;
742 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800743 if (who)
744 pgrp = find_pid(who);
745 else
746 pgrp = task_pgrp(current);
747 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 niceval = 20 - task_nice(p);
749 if (niceval > retval)
750 retval = niceval;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800751 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
753 case PRIO_USER:
754 user = current->user;
755 if (!who)
756 who = current->uid;
757 else
758 if ((who != current->uid) && !(user = find_user(who)))
759 goto out_unlock; /* No processes for this user */
760
761 do_each_thread(g, p)
762 if (p->uid == who) {
763 niceval = 20 - task_nice(p);
764 if (niceval > retval)
765 retval = niceval;
766 }
767 while_each_thread(g, p);
768 if (who != current->uid)
769 free_uid(user); /* for find_user() */
770 break;
771 }
772out_unlock:
773 read_unlock(&tasklist_lock);
774
775 return retval;
776}
777
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700778/**
779 * emergency_restart - reboot the system
780 *
781 * Without shutting down any hardware or taking any locks
782 * reboot the system. This is called when we know we are in
783 * trouble so this is our best effort to reboot. This is
784 * safe to call in interrupt context.
785 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600786void emergency_restart(void)
787{
788 machine_emergency_restart();
789}
790EXPORT_SYMBOL_GPL(emergency_restart);
791
Adrian Bunk83cc5ed2006-06-25 05:47:41 -0700792static void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600793{
Alan Sterne041c682006-03-27 01:16:30 -0800794 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600795 system_state = SYSTEM_RESTART;
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600796 device_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700797}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800798
799/**
800 * kernel_restart - reboot the system
801 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800802 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800803 *
804 * Shutdown everything and perform a clean reboot.
805 * This is not safe to call in interrupt context.
806 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700807void kernel_restart(char *cmd)
808{
809 kernel_restart_prepare(cmd);
Cal Peake756184b2006-09-30 23:27:24 -0700810 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600811 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700812 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600813 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600814 machine_restart(cmd);
815}
816EXPORT_SYMBOL_GPL(kernel_restart);
817
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700818/**
819 * kernel_kexec - reboot the system
820 *
821 * Move into place and start executing a preloaded standalone
822 * executable. If nothing was preloaded return an error.
823 */
Adrian Bunk83cc5ed2006-06-25 05:47:41 -0700824static void kernel_kexec(void)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600825{
826#ifdef CONFIG_KEXEC
827 struct kimage *image;
Al Viro4bb80892006-02-01 05:57:32 -0500828 image = xchg(&kexec_image, NULL);
Cal Peake756184b2006-09-30 23:27:24 -0700829 if (!image)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600830 return;
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700831 kernel_restart_prepare(NULL);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600832 printk(KERN_EMERG "Starting new kernel\n");
833 machine_shutdown();
834 machine_kexec(image);
835#endif
836}
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600837
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500838void kernel_shutdown_prepare(enum system_states state)
839{
Alan Sterne041c682006-03-27 01:16:30 -0800840 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500841 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
842 system_state = state;
843 device_shutdown();
844}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700845/**
846 * kernel_halt - halt the system
847 *
848 * Shutdown everything and perform a clean system halt.
849 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700850void kernel_halt(void)
851{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500852 kernel_shutdown_prepare(SYSTEM_HALT);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600853 printk(KERN_EMERG "System halted.\n");
854 machine_halt();
855}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500856
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600857EXPORT_SYMBOL_GPL(kernel_halt);
858
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700859/**
860 * kernel_power_off - power_off the system
861 *
862 * Shutdown everything and perform a clean system power_off.
863 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700864void kernel_power_off(void)
865{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500866 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600867 printk(KERN_EMERG "Power down.\n");
868 machine_power_off();
869}
870EXPORT_SYMBOL_GPL(kernel_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
872 * Reboot system call: for obvious reasons only root may call it,
873 * and even root needs to set up some magic numbers in the registers
874 * so that some mistake won't make this reboot the whole machine.
875 * You can also set the meaning of the ctrl-alt-del-key here.
876 *
877 * reboot doesn't sync: do that yourself before calling this.
878 */
879asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
880{
881 char buffer[256];
882
883 /* We only trust the superuser with rebooting the system. */
884 if (!capable(CAP_SYS_BOOT))
885 return -EPERM;
886
887 /* For safety, we require "magic" arguments. */
888 if (magic1 != LINUX_REBOOT_MAGIC1 ||
889 (magic2 != LINUX_REBOOT_MAGIC2 &&
890 magic2 != LINUX_REBOOT_MAGIC2A &&
891 magic2 != LINUX_REBOOT_MAGIC2B &&
892 magic2 != LINUX_REBOOT_MAGIC2C))
893 return -EINVAL;
894
Eric W. Biederman5e382912006-01-08 01:03:46 -0800895 /* Instead of trying to make the power_off code look like
896 * halt when pm_power_off is not set do it the easy way.
897 */
898 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
899 cmd = LINUX_REBOOT_CMD_HALT;
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 lock_kernel();
902 switch (cmd) {
903 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600904 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 break;
906
907 case LINUX_REBOOT_CMD_CAD_ON:
908 C_A_D = 1;
909 break;
910
911 case LINUX_REBOOT_CMD_CAD_OFF:
912 C_A_D = 0;
913 break;
914
915 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600916 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 unlock_kernel();
918 do_exit(0);
919 break;
920
921 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600922 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unlock_kernel();
924 do_exit(0);
925 break;
926
927 case LINUX_REBOOT_CMD_RESTART2:
928 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
929 unlock_kernel();
930 return -EFAULT;
931 }
932 buffer[sizeof(buffer) - 1] = '\0';
933
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600934 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
936
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700937 case LINUX_REBOOT_CMD_KEXEC:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600938 kernel_kexec();
939 unlock_kernel();
940 return -EINVAL;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942#ifdef CONFIG_SOFTWARE_SUSPEND
943 case LINUX_REBOOT_CMD_SW_SUSPEND:
944 {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700945 int ret = hibernate();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 unlock_kernel();
947 return ret;
948 }
949#endif
950
951 default:
952 unlock_kernel();
953 return -EINVAL;
954 }
955 unlock_kernel();
956 return 0;
957}
958
David Howells65f27f32006-11-22 14:55:48 +0000959static void deferred_cad(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600961 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964/*
965 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
966 * As it's called within an interrupt, it may NOT sync: the only choice
967 * is whether to reboot at once, or just ignore the ctrl-alt-del.
968 */
969void ctrl_alt_del(void)
970{
David Howells65f27f32006-11-22 14:55:48 +0000971 static DECLARE_WORK(cad_work, deferred_cad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 if (C_A_D)
974 schedule_work(&cad_work);
975 else
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700976 kill_cad_pid(SIGINT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/*
980 * Unprivileged users may change the real gid to the effective gid
981 * or vice versa. (BSD-style)
982 *
983 * If you set the real gid at all, or set the effective gid to a value not
984 * equal to the real gid, then the saved gid is set to the new effective gid.
985 *
986 * This makes it possible for a setgid program to completely drop its
987 * privileges, which is often a useful assertion to make when you are doing
988 * a security audit over a program.
989 *
990 * The general idea is that a program which uses just setregid() will be
991 * 100% compatible with BSD. A program which uses just setgid() will be
992 * 100% compatible with POSIX with saved IDs.
993 *
994 * SMP: There are not races, the GIDs are checked only by filesystem
995 * operations (as far as semantic preservation is concerned).
996 */
997asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
998{
999 int old_rgid = current->gid;
1000 int old_egid = current->egid;
1001 int new_rgid = old_rgid;
1002 int new_egid = old_egid;
1003 int retval;
1004
1005 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
1006 if (retval)
1007 return retval;
1008
1009 if (rgid != (gid_t) -1) {
1010 if ((old_rgid == rgid) ||
1011 (current->egid==rgid) ||
1012 capable(CAP_SETGID))
1013 new_rgid = rgid;
1014 else
1015 return -EPERM;
1016 }
1017 if (egid != (gid_t) -1) {
1018 if ((old_rgid == egid) ||
1019 (current->egid == egid) ||
1020 (current->sgid == egid) ||
1021 capable(CAP_SETGID))
1022 new_egid = egid;
Cal Peake756184b2006-09-30 23:27:24 -07001023 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Cal Peake756184b2006-09-30 23:27:24 -07001026 if (new_egid != old_egid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001027 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001028 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030 if (rgid != (gid_t) -1 ||
1031 (egid != (gid_t) -1 && egid != old_rgid))
1032 current->sgid = new_egid;
1033 current->fsgid = new_egid;
1034 current->egid = new_egid;
1035 current->gid = new_rgid;
1036 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001037 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return 0;
1039}
1040
1041/*
1042 * setgid() is implemented like SysV w/ SAVED_IDS
1043 *
1044 * SMP: Same implicit races as above.
1045 */
1046asmlinkage long sys_setgid(gid_t gid)
1047{
1048 int old_egid = current->egid;
1049 int retval;
1050
1051 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
1052 if (retval)
1053 return retval;
1054
Cal Peake756184b2006-09-30 23:27:24 -07001055 if (capable(CAP_SETGID)) {
1056 if (old_egid != gid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001057 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001058 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 current->gid = current->egid = current->sgid = current->fsgid = gid;
Cal Peake756184b2006-09-30 23:27:24 -07001061 } else if ((gid == current->gid) || (gid == current->sgid)) {
1062 if (old_egid != gid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001063 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001064 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 current->egid = current->fsgid = gid;
1067 }
1068 else
1069 return -EPERM;
1070
1071 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001072 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return 0;
1074}
1075
1076static int set_user(uid_t new_ruid, int dumpclear)
1077{
1078 struct user_struct *new_user;
1079
1080 new_user = alloc_uid(new_ruid);
1081 if (!new_user)
1082 return -EAGAIN;
1083
1084 if (atomic_read(&new_user->processes) >=
1085 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
1086 new_user != &root_user) {
1087 free_uid(new_user);
1088 return -EAGAIN;
1089 }
1090
1091 switch_uid(new_user);
1092
Cal Peake756184b2006-09-30 23:27:24 -07001093 if (dumpclear) {
Alan Coxd6e71142005-06-23 00:09:43 -07001094 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001095 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 current->uid = new_ruid;
1098 return 0;
1099}
1100
1101/*
1102 * Unprivileged users may change the real uid to the effective uid
1103 * or vice versa. (BSD-style)
1104 *
1105 * If you set the real uid at all, or set the effective uid to a value not
1106 * equal to the real uid, then the saved uid is set to the new effective uid.
1107 *
1108 * This makes it possible for a setuid program to completely drop its
1109 * privileges, which is often a useful assertion to make when you are doing
1110 * a security audit over a program.
1111 *
1112 * The general idea is that a program which uses just setreuid() will be
1113 * 100% compatible with BSD. A program which uses just setuid() will be
1114 * 100% compatible with POSIX with saved IDs.
1115 */
1116asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
1117{
1118 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
1119 int retval;
1120
1121 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
1122 if (retval)
1123 return retval;
1124
1125 new_ruid = old_ruid = current->uid;
1126 new_euid = old_euid = current->euid;
1127 old_suid = current->suid;
1128
1129 if (ruid != (uid_t) -1) {
1130 new_ruid = ruid;
1131 if ((old_ruid != ruid) &&
1132 (current->euid != ruid) &&
1133 !capable(CAP_SETUID))
1134 return -EPERM;
1135 }
1136
1137 if (euid != (uid_t) -1) {
1138 new_euid = euid;
1139 if ((old_ruid != euid) &&
1140 (current->euid != euid) &&
1141 (current->suid != euid) &&
1142 !capable(CAP_SETUID))
1143 return -EPERM;
1144 }
1145
1146 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
1147 return -EAGAIN;
1148
Cal Peake756184b2006-09-30 23:27:24 -07001149 if (new_euid != old_euid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001150 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001151 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153 current->fsuid = current->euid = new_euid;
1154 if (ruid != (uid_t) -1 ||
1155 (euid != (uid_t) -1 && euid != old_ruid))
1156 current->suid = current->euid;
1157 current->fsuid = current->euid;
1158
1159 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001160 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
1163}
1164
1165
1166
1167/*
1168 * setuid() is implemented like SysV with SAVED_IDS
1169 *
1170 * Note that SAVED_ID's is deficient in that a setuid root program
1171 * like sendmail, for example, cannot set its uid to be a normal
1172 * user and then switch back, because if you're root, setuid() sets
1173 * the saved uid too. If you don't like this, blame the bright people
1174 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
1175 * will allow a root program to temporarily drop privileges and be able to
1176 * regain them by swapping the real and effective uid.
1177 */
1178asmlinkage long sys_setuid(uid_t uid)
1179{
1180 int old_euid = current->euid;
David Rientjesa09c17a2006-12-06 20:40:18 -08001181 int old_ruid, old_suid, new_suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int retval;
1183
1184 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
1185 if (retval)
1186 return retval;
1187
David Rientjesa09c17a2006-12-06 20:40:18 -08001188 old_ruid = current->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 old_suid = current->suid;
1190 new_suid = old_suid;
1191
1192 if (capable(CAP_SETUID)) {
1193 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1194 return -EAGAIN;
1195 new_suid = uid;
1196 } else if ((uid != current->uid) && (uid != new_suid))
1197 return -EPERM;
1198
Cal Peake756184b2006-09-30 23:27:24 -07001199 if (old_euid != uid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001200 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001201 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203 current->fsuid = current->euid = uid;
1204 current->suid = new_suid;
1205
1206 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001207 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1210}
1211
1212
1213/*
1214 * This function implements a generic ability to update ruid, euid,
1215 * and suid. This allows you to implement the 4.4 compatible seteuid().
1216 */
1217asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1218{
1219 int old_ruid = current->uid;
1220 int old_euid = current->euid;
1221 int old_suid = current->suid;
1222 int retval;
1223
1224 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1225 if (retval)
1226 return retval;
1227
1228 if (!capable(CAP_SETUID)) {
1229 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1230 (ruid != current->euid) && (ruid != current->suid))
1231 return -EPERM;
1232 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1233 (euid != current->euid) && (euid != current->suid))
1234 return -EPERM;
1235 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1236 (suid != current->euid) && (suid != current->suid))
1237 return -EPERM;
1238 }
1239 if (ruid != (uid_t) -1) {
1240 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1241 return -EAGAIN;
1242 }
1243 if (euid != (uid_t) -1) {
Cal Peake756184b2006-09-30 23:27:24 -07001244 if (euid != current->euid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001245 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001246 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248 current->euid = euid;
1249 }
1250 current->fsuid = current->euid;
1251 if (suid != (uid_t) -1)
1252 current->suid = suid;
1253
1254 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001255 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1258}
1259
1260asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1261{
1262 int retval;
1263
1264 if (!(retval = put_user(current->uid, ruid)) &&
1265 !(retval = put_user(current->euid, euid)))
1266 retval = put_user(current->suid, suid);
1267
1268 return retval;
1269}
1270
1271/*
1272 * Same as above, but for rgid, egid, sgid.
1273 */
1274asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1275{
1276 int retval;
1277
1278 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1279 if (retval)
1280 return retval;
1281
1282 if (!capable(CAP_SETGID)) {
1283 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1284 (rgid != current->egid) && (rgid != current->sgid))
1285 return -EPERM;
1286 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1287 (egid != current->egid) && (egid != current->sgid))
1288 return -EPERM;
1289 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1290 (sgid != current->egid) && (sgid != current->sgid))
1291 return -EPERM;
1292 }
1293 if (egid != (gid_t) -1) {
Cal Peake756184b2006-09-30 23:27:24 -07001294 if (egid != current->egid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001295 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001296 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 current->egid = egid;
1299 }
1300 current->fsgid = current->egid;
1301 if (rgid != (gid_t) -1)
1302 current->gid = rgid;
1303 if (sgid != (gid_t) -1)
1304 current->sgid = sgid;
1305
1306 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001307 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309}
1310
1311asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1312{
1313 int retval;
1314
1315 if (!(retval = put_user(current->gid, rgid)) &&
1316 !(retval = put_user(current->egid, egid)))
1317 retval = put_user(current->sgid, sgid);
1318
1319 return retval;
1320}
1321
1322
1323/*
1324 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1325 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1326 * whatever uid it wants to). It normally shadows "euid", except when
1327 * explicitly set by setfsuid() or for access..
1328 */
1329asmlinkage long sys_setfsuid(uid_t uid)
1330{
1331 int old_fsuid;
1332
1333 old_fsuid = current->fsuid;
1334 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1335 return old_fsuid;
1336
1337 if (uid == current->uid || uid == current->euid ||
1338 uid == current->suid || uid == current->fsuid ||
Cal Peake756184b2006-09-30 23:27:24 -07001339 capable(CAP_SETUID)) {
1340 if (uid != old_fsuid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001341 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001342 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 current->fsuid = uid;
1345 }
1346
1347 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001348 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1351
1352 return old_fsuid;
1353}
1354
1355/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +02001356 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
1358asmlinkage long sys_setfsgid(gid_t gid)
1359{
1360 int old_fsgid;
1361
1362 old_fsgid = current->fsgid;
1363 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1364 return old_fsgid;
1365
1366 if (gid == current->gid || gid == current->egid ||
1367 gid == current->sgid || gid == current->fsgid ||
Cal Peake756184b2006-09-30 23:27:24 -07001368 capable(CAP_SETGID)) {
1369 if (gid != old_fsgid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001370 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001371 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373 current->fsgid = gid;
1374 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001375 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 return old_fsgid;
1378}
1379
1380asmlinkage long sys_times(struct tms __user * tbuf)
1381{
1382 /*
1383 * In the SMP world we might just be unlucky and have one of
1384 * the times increment as we use it. Since the value is an
1385 * atomically safe type this is just fine. Conceptually its
1386 * as if the syscall took an instant longer to occur.
1387 */
1388 if (tbuf) {
1389 struct tms tmp;
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001390 struct task_struct *tsk = current;
1391 struct task_struct *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 cputime_t utime, stime, cutime, cstime;
1393
Oleg Nesterov7d7185c2006-03-28 16:11:21 -08001394 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001395 utime = tsk->signal->utime;
1396 stime = tsk->signal->stime;
1397 t = tsk;
1398 do {
1399 utime = cputime_add(utime, t->utime);
1400 stime = cputime_add(stime, t->stime);
1401 t = next_thread(t);
1402 } while (t != tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001404 cutime = tsk->signal->cutime;
1405 cstime = tsk->signal->cstime;
1406 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 tmp.tms_utime = cputime_to_clock_t(utime);
1409 tmp.tms_stime = cputime_to_clock_t(stime);
1410 tmp.tms_cutime = cputime_to_clock_t(cutime);
1411 tmp.tms_cstime = cputime_to_clock_t(cstime);
1412 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1413 return -EFAULT;
1414 }
1415 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1416}
1417
1418/*
1419 * This needs some heavy checking ...
1420 * I just haven't the stomach for it. I also don't fully
1421 * understand sessions/pgrp etc. Let somebody who does explain it.
1422 *
1423 * OK, I think I have the protection semantics right.... this is really
1424 * only important on a multi-user system anyway, to make sure one user
1425 * can't send a signal to a process owned by another. -TYT, 12/12/91
1426 *
1427 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1428 * LBT 04.03.94
1429 */
1430
1431asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1432{
1433 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001434 struct task_struct *group_leader = current->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int err = -EINVAL;
1436
1437 if (!pid)
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001438 pid = group_leader->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (!pgid)
1440 pgid = pid;
1441 if (pgid < 0)
1442 return -EINVAL;
1443
1444 /* From this point forward we keep holding onto the tasklist lock
1445 * so that our parent does not change from under us. -DaveM
1446 */
1447 write_lock_irq(&tasklist_lock);
1448
1449 err = -ESRCH;
1450 p = find_task_by_pid(pid);
1451 if (!p)
1452 goto out;
1453
1454 err = -EINVAL;
1455 if (!thread_group_leader(p))
1456 goto out;
1457
Oleg Nesterovf7dd7952006-01-08 01:03:59 -08001458 if (p->real_parent == group_leader) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -08001460 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 goto out;
1462 err = -EACCES;
1463 if (p->did_exec)
1464 goto out;
1465 } else {
1466 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001467 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 goto out;
1469 }
1470
1471 err = -EPERM;
1472 if (p->signal->leader)
1473 goto out;
1474
1475 if (pgid != pid) {
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001476 struct task_struct *g =
1477 find_task_by_pid_type(PIDTYPE_PGID, pgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Eric W. Biederman41487c62007-02-12 00:53:01 -08001479 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001480 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 err = security_task_setpgid(p, pgid);
1484 if (err)
1485 goto out;
1486
1487 if (process_group(p) != pgid) {
1488 detach_pid(p, PIDTYPE_PGID);
1489 p->signal->pgrp = pgid;
1490 attach_pid(p, PIDTYPE_PGID, pgid);
1491 }
1492
1493 err = 0;
1494out:
1495 /* All paths lead to here, thus we are safe. -DaveM */
1496 write_unlock_irq(&tasklist_lock);
1497 return err;
1498}
1499
1500asmlinkage long sys_getpgid(pid_t pid)
1501{
Cal Peake756184b2006-09-30 23:27:24 -07001502 if (!pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return process_group(current);
Cal Peake756184b2006-09-30 23:27:24 -07001504 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 int retval;
1506 struct task_struct *p;
1507
1508 read_lock(&tasklist_lock);
1509 p = find_task_by_pid(pid);
1510
1511 retval = -ESRCH;
1512 if (p) {
1513 retval = security_task_getpgid(p);
1514 if (!retval)
1515 retval = process_group(p);
1516 }
1517 read_unlock(&tasklist_lock);
1518 return retval;
1519 }
1520}
1521
1522#ifdef __ARCH_WANT_SYS_GETPGRP
1523
1524asmlinkage long sys_getpgrp(void)
1525{
1526 /* SMP - assuming writes are word atomic this is fine */
1527 return process_group(current);
1528}
1529
1530#endif
1531
1532asmlinkage long sys_getsid(pid_t pid)
1533{
Cal Peake756184b2006-09-30 23:27:24 -07001534 if (!pid)
Cedric Le Goater937949d2006-12-08 02:37:54 -08001535 return process_session(current);
Cal Peake756184b2006-09-30 23:27:24 -07001536 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int retval;
1538 struct task_struct *p;
1539
1540 read_lock(&tasklist_lock);
1541 p = find_task_by_pid(pid);
1542
1543 retval = -ESRCH;
Cal Peake756184b2006-09-30 23:27:24 -07001544 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 retval = security_task_getsid(p);
1546 if (!retval)
Cedric Le Goater937949d2006-12-08 02:37:54 -08001547 retval = process_session(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549 read_unlock(&tasklist_lock);
1550 return retval;
1551 }
1552}
1553
1554asmlinkage long sys_setsid(void)
1555{
Oren Laadane19f2472006-01-08 01:03:58 -08001556 struct task_struct *group_leader = current->group_leader;
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001557 pid_t session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int err = -EPERM;
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 write_lock_irq(&tasklist_lock);
1561
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001562 /* Fail if I am already a session leader */
1563 if (group_leader->signal->leader)
1564 goto out;
1565
1566 session = group_leader->pid;
1567 /* Fail if a process group id already exists that equals the
1568 * proposed session id.
1569 *
1570 * Don't check if session id == 1 because kernel threads use this
1571 * session id and so the check will always fail and make it so
1572 * init cannot successfully call setsid.
1573 */
1574 if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 goto out;
1576
Oren Laadane19f2472006-01-08 01:03:58 -08001577 group_leader->signal->leader = 1;
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001578 __set_special_pids(session, session);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001579
1580 spin_lock(&group_leader->sighand->siglock);
Oren Laadane19f2472006-01-08 01:03:58 -08001581 group_leader->signal->tty = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001582 spin_unlock(&group_leader->sighand->siglock);
1583
Oren Laadane19f2472006-01-08 01:03:58 -08001584 err = process_group(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585out:
1586 write_unlock_irq(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return err;
1588}
1589
1590/*
1591 * Supplementary group IDs
1592 */
1593
1594/* init to 2 - one for init_task, one to ensure it is never freed */
1595struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1596
1597struct group_info *groups_alloc(int gidsetsize)
1598{
1599 struct group_info *group_info;
1600 int nblocks;
1601 int i;
1602
1603 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1604 /* Make sure we always allocate at least one indirect block pointer */
1605 nblocks = nblocks ? : 1;
1606 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1607 if (!group_info)
1608 return NULL;
1609 group_info->ngroups = gidsetsize;
1610 group_info->nblocks = nblocks;
1611 atomic_set(&group_info->usage, 1);
1612
Cal Peake756184b2006-09-30 23:27:24 -07001613 if (gidsetsize <= NGROUPS_SMALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 group_info->blocks[0] = group_info->small_block;
Cal Peake756184b2006-09-30 23:27:24 -07001615 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 for (i = 0; i < nblocks; i++) {
1617 gid_t *b;
1618 b = (void *)__get_free_page(GFP_USER);
1619 if (!b)
1620 goto out_undo_partial_alloc;
1621 group_info->blocks[i] = b;
1622 }
1623 }
1624 return group_info;
1625
1626out_undo_partial_alloc:
1627 while (--i >= 0) {
1628 free_page((unsigned long)group_info->blocks[i]);
1629 }
1630 kfree(group_info);
1631 return NULL;
1632}
1633
1634EXPORT_SYMBOL(groups_alloc);
1635
1636void groups_free(struct group_info *group_info)
1637{
1638 if (group_info->blocks[0] != group_info->small_block) {
1639 int i;
1640 for (i = 0; i < group_info->nblocks; i++)
1641 free_page((unsigned long)group_info->blocks[i]);
1642 }
1643 kfree(group_info);
1644}
1645
1646EXPORT_SYMBOL(groups_free);
1647
1648/* export the group_info to a user-space array */
1649static int groups_to_user(gid_t __user *grouplist,
1650 struct group_info *group_info)
1651{
1652 int i;
1653 int count = group_info->ngroups;
1654
1655 for (i = 0; i < group_info->nblocks; i++) {
1656 int cp_count = min(NGROUPS_PER_BLOCK, count);
1657 int off = i * NGROUPS_PER_BLOCK;
1658 int len = cp_count * sizeof(*grouplist);
1659
1660 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1661 return -EFAULT;
1662
1663 count -= cp_count;
1664 }
1665 return 0;
1666}
1667
1668/* fill a group_info from a user-space array - it must be allocated already */
1669static int groups_from_user(struct group_info *group_info,
1670 gid_t __user *grouplist)
Cal Peake756184b2006-09-30 23:27:24 -07001671{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 int i;
1673 int count = group_info->ngroups;
1674
1675 for (i = 0; i < group_info->nblocks; i++) {
1676 int cp_count = min(NGROUPS_PER_BLOCK, count);
1677 int off = i * NGROUPS_PER_BLOCK;
1678 int len = cp_count * sizeof(*grouplist);
1679
1680 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1681 return -EFAULT;
1682
1683 count -= cp_count;
1684 }
1685 return 0;
1686}
1687
Domen Puncerebe8b542005-05-05 16:16:19 -07001688/* a simple Shell sort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689static void groups_sort(struct group_info *group_info)
1690{
1691 int base, max, stride;
1692 int gidsetsize = group_info->ngroups;
1693
1694 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1695 ; /* nothing */
1696 stride /= 3;
1697
1698 while (stride) {
1699 max = gidsetsize - stride;
1700 for (base = 0; base < max; base++) {
1701 int left = base;
1702 int right = left + stride;
1703 gid_t tmp = GROUP_AT(group_info, right);
1704
1705 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1706 GROUP_AT(group_info, right) =
1707 GROUP_AT(group_info, left);
1708 right = left;
1709 left -= stride;
1710 }
1711 GROUP_AT(group_info, right) = tmp;
1712 }
1713 stride /= 3;
1714 }
1715}
1716
1717/* a simple bsearch */
David Howells3e301482005-06-23 22:00:56 -07001718int groups_search(struct group_info *group_info, gid_t grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001720 unsigned int left, right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 if (!group_info)
1723 return 0;
1724
1725 left = 0;
1726 right = group_info->ngroups;
1727 while (left < right) {
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001728 unsigned int mid = (left+right)/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 int cmp = grp - GROUP_AT(group_info, mid);
1730 if (cmp > 0)
1731 left = mid + 1;
1732 else if (cmp < 0)
1733 right = mid;
1734 else
1735 return 1;
1736 }
1737 return 0;
1738}
1739
1740/* validate and set current->group_info */
1741int set_current_groups(struct group_info *group_info)
1742{
1743 int retval;
1744 struct group_info *old_info;
1745
1746 retval = security_task_setgroups(group_info);
1747 if (retval)
1748 return retval;
1749
1750 groups_sort(group_info);
1751 get_group_info(group_info);
1752
1753 task_lock(current);
1754 old_info = current->group_info;
1755 current->group_info = group_info;
1756 task_unlock(current);
1757
1758 put_group_info(old_info);
1759
1760 return 0;
1761}
1762
1763EXPORT_SYMBOL(set_current_groups);
1764
1765asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1766{
1767 int i = 0;
1768
1769 /*
1770 * SMP: Nobody else can change our grouplist. Thus we are
1771 * safe.
1772 */
1773
1774 if (gidsetsize < 0)
1775 return -EINVAL;
1776
1777 /* no need to grab task_lock here; it cannot change */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 i = current->group_info->ngroups;
1779 if (gidsetsize) {
1780 if (i > gidsetsize) {
1781 i = -EINVAL;
1782 goto out;
1783 }
1784 if (groups_to_user(grouplist, current->group_info)) {
1785 i = -EFAULT;
1786 goto out;
1787 }
1788 }
1789out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return i;
1791}
1792
1793/*
1794 * SMP: Our groups are copy-on-write. We can set them safely
1795 * without another task interfering.
1796 */
1797
1798asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1799{
1800 struct group_info *group_info;
1801 int retval;
1802
1803 if (!capable(CAP_SETGID))
1804 return -EPERM;
1805 if ((unsigned)gidsetsize > NGROUPS_MAX)
1806 return -EINVAL;
1807
1808 group_info = groups_alloc(gidsetsize);
1809 if (!group_info)
1810 return -ENOMEM;
1811 retval = groups_from_user(group_info, grouplist);
1812 if (retval) {
1813 put_group_info(group_info);
1814 return retval;
1815 }
1816
1817 retval = set_current_groups(group_info);
1818 put_group_info(group_info);
1819
1820 return retval;
1821}
1822
1823/*
1824 * Check whether we're fsgid/egid or in the supplemental group..
1825 */
1826int in_group_p(gid_t grp)
1827{
1828 int retval = 1;
Cal Peake756184b2006-09-30 23:27:24 -07001829 if (grp != current->fsgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 retval = groups_search(current->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return retval;
1832}
1833
1834EXPORT_SYMBOL(in_group_p);
1835
1836int in_egroup_p(gid_t grp)
1837{
1838 int retval = 1;
Cal Peake756184b2006-09-30 23:27:24 -07001839 if (grp != current->egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 retval = groups_search(current->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return retval;
1842}
1843
1844EXPORT_SYMBOL(in_egroup_p);
1845
1846DECLARE_RWSEM(uts_sem);
1847
David S. Miller393b0722005-11-10 12:47:50 -08001848EXPORT_SYMBOL(uts_sem);
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850asmlinkage long sys_newuname(struct new_utsname __user * name)
1851{
1852 int errno = 0;
1853
1854 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001855 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 errno = -EFAULT;
1857 up_read(&uts_sem);
1858 return errno;
1859}
1860
1861asmlinkage long sys_sethostname(char __user *name, int len)
1862{
1863 int errno;
1864 char tmp[__NEW_UTS_LEN];
1865
1866 if (!capable(CAP_SYS_ADMIN))
1867 return -EPERM;
1868 if (len < 0 || len > __NEW_UTS_LEN)
1869 return -EINVAL;
1870 down_write(&uts_sem);
1871 errno = -EFAULT;
1872 if (!copy_from_user(tmp, name, len)) {
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001873 memcpy(utsname()->nodename, tmp, len);
1874 utsname()->nodename[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 errno = 0;
1876 }
1877 up_write(&uts_sem);
1878 return errno;
1879}
1880
1881#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1882
1883asmlinkage long sys_gethostname(char __user *name, int len)
1884{
1885 int i, errno;
1886
1887 if (len < 0)
1888 return -EINVAL;
1889 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001890 i = 1 + strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (i > len)
1892 i = len;
1893 errno = 0;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001894 if (copy_to_user(name, utsname()->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 errno = -EFAULT;
1896 up_read(&uts_sem);
1897 return errno;
1898}
1899
1900#endif
1901
1902/*
1903 * Only setdomainname; getdomainname can be implemented by calling
1904 * uname()
1905 */
1906asmlinkage long sys_setdomainname(char __user *name, int len)
1907{
1908 int errno;
1909 char tmp[__NEW_UTS_LEN];
1910
1911 if (!capable(CAP_SYS_ADMIN))
1912 return -EPERM;
1913 if (len < 0 || len > __NEW_UTS_LEN)
1914 return -EINVAL;
1915
1916 down_write(&uts_sem);
1917 errno = -EFAULT;
1918 if (!copy_from_user(tmp, name, len)) {
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001919 memcpy(utsname()->domainname, tmp, len);
1920 utsname()->domainname[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 errno = 0;
1922 }
1923 up_write(&uts_sem);
1924 return errno;
1925}
1926
1927asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1928{
1929 if (resource >= RLIM_NLIMITS)
1930 return -EINVAL;
1931 else {
1932 struct rlimit value;
1933 task_lock(current->group_leader);
1934 value = current->signal->rlim[resource];
1935 task_unlock(current->group_leader);
1936 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1937 }
1938}
1939
1940#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1941
1942/*
1943 * Back compatibility for getrlimit. Needed for some apps.
1944 */
1945
1946asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1947{
1948 struct rlimit x;
1949 if (resource >= RLIM_NLIMITS)
1950 return -EINVAL;
1951
1952 task_lock(current->group_leader);
1953 x = current->signal->rlim[resource];
1954 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001955 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001957 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 x.rlim_max = 0x7FFFFFFF;
1959 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1960}
1961
1962#endif
1963
1964asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1965{
1966 struct rlimit new_rlim, *old_rlim;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001967 unsigned long it_prof_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 int retval;
1969
1970 if (resource >= RLIM_NLIMITS)
1971 return -EINVAL;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001972 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return -EFAULT;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001974 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1975 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 old_rlim = current->signal->rlim + resource;
1977 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1978 !capable(CAP_SYS_RESOURCE))
1979 return -EPERM;
1980 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001981 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 retval = security_task_setrlimit(resource, &new_rlim);
1984 if (retval)
1985 return retval;
1986
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001987 if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
1988 /*
1989 * The caller is asking for an immediate RLIMIT_CPU
1990 * expiry. But we use the zero value to mean "it was
1991 * never set". So let's cheat and make it one second
1992 * instead
1993 */
1994 new_rlim.rlim_cur = 1;
1995 }
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 task_lock(current->group_leader);
1998 *old_rlim = new_rlim;
1999 task_unlock(current->group_leader);
2000
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002001 if (resource != RLIMIT_CPU)
2002 goto out;
Andrew Mortond3561f72006-03-24 03:18:36 -08002003
2004 /*
2005 * RLIMIT_CPU handling. Note that the kernel fails to return an error
2006 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
2007 * very long-standing error, and fixing it now risks breakage of
2008 * applications, so we live with it
2009 */
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002010 if (new_rlim.rlim_cur == RLIM_INFINITY)
2011 goto out;
2012
2013 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
2014 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
Andrew Mortone0661112006-03-24 03:18:35 -08002015 unsigned long rlim_cur = new_rlim.rlim_cur;
2016 cputime_t cputime;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002017
Andrew Mortone0661112006-03-24 03:18:35 -08002018 cputime = secs_to_cputime(rlim_cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 read_lock(&tasklist_lock);
2020 spin_lock_irq(&current->sighand->siglock);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002021 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 spin_unlock_irq(&current->sighand->siglock);
2023 read_unlock(&tasklist_lock);
2024 }
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002025out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 return 0;
2027}
2028
2029/*
2030 * It would make sense to put struct rusage in the task_struct,
2031 * except that would make the task_struct be *really big*. After
2032 * task_struct gets moved into malloc'ed memory, it would
2033 * make sense to do this. It will make moving the rest of the information
2034 * a lot simpler! (Which we're not doing right now because we're not
2035 * measuring them yet).
2036 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
2038 * races with threads incrementing their own counters. But since word
2039 * reads are atomic, we either get new values or old values and we don't
2040 * care which for the sums. We always take the siglock to protect reading
2041 * the c* fields from p->signal from races with exit.c updating those
2042 * fields when reaping, so a sample either gets all the additions of a
2043 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08002044 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002045 * Locking:
2046 * We need to take the siglock for CHILDEREN, SELF and BOTH
2047 * for the cases current multithreaded, non-current single threaded
2048 * non-current multithreaded. Thread traversal is now safe with
2049 * the siglock held.
2050 * Strictly speaking, we donot need to take the siglock if we are current and
2051 * single threaded, as no one else can take our signal_struct away, no one
2052 * else can reap the children to update signal->c* counters, and no one else
2053 * can race with the signal-> fields. If we do not take any lock, the
2054 * signal-> fields could be read out of order while another thread was just
2055 * exiting. So we should place a read memory barrier when we avoid the lock.
2056 * On the writer side, write memory barrier is implied in __exit_signal
2057 * as __exit_signal releases the siglock spinlock after updating the signal->
2058 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08002059 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 */
2061
2062static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
2063{
2064 struct task_struct *t;
2065 unsigned long flags;
2066 cputime_t utime, stime;
2067
2068 memset((char *) r, 0, sizeof *r);
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002069 utime = stime = cputime_zero;
2070
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002071 rcu_read_lock();
2072 if (!lock_task_sighand(p, &flags)) {
2073 rcu_read_unlock();
2074 return;
2075 }
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002078 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 utime = p->signal->cutime;
2081 stime = p->signal->cstime;
2082 r->ru_nvcsw = p->signal->cnvcsw;
2083 r->ru_nivcsw = p->signal->cnivcsw;
2084 r->ru_minflt = p->signal->cmin_flt;
2085 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07002086 r->ru_inblock = p->signal->cinblock;
2087 r->ru_oublock = p->signal->coublock;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002088
2089 if (who == RUSAGE_CHILDREN)
2090 break;
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 case RUSAGE_SELF:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 utime = cputime_add(utime, p->signal->utime);
2094 stime = cputime_add(stime, p->signal->stime);
2095 r->ru_nvcsw += p->signal->nvcsw;
2096 r->ru_nivcsw += p->signal->nivcsw;
2097 r->ru_minflt += p->signal->min_flt;
2098 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07002099 r->ru_inblock += p->signal->inblock;
2100 r->ru_oublock += p->signal->oublock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 t = p;
2102 do {
2103 utime = cputime_add(utime, t->utime);
2104 stime = cputime_add(stime, t->stime);
2105 r->ru_nvcsw += t->nvcsw;
2106 r->ru_nivcsw += t->nivcsw;
2107 r->ru_minflt += t->min_flt;
2108 r->ru_majflt += t->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07002109 r->ru_inblock += task_io_get_inblock(t);
2110 r->ru_oublock += task_io_get_oublock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 t = next_thread(t);
2112 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 default:
2116 BUG();
2117 }
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002118
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002119 unlock_task_sighand(p, &flags);
2120 rcu_read_unlock();
2121
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002122 cputime_to_timeval(utime, &r->ru_utime);
2123 cputime_to_timeval(stime, &r->ru_stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
2126int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
2127{
2128 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
2131}
2132
2133asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
2134{
2135 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
2136 return -EINVAL;
2137 return getrusage(current, who, ru);
2138}
2139
2140asmlinkage long sys_umask(int mask)
2141{
2142 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
2143 return mask;
2144}
2145
2146asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
2147 unsigned long arg4, unsigned long arg5)
2148{
2149 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2152 if (error)
2153 return error;
2154
2155 switch (option) {
2156 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07002157 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 error = -EINVAL;
2159 break;
2160 }
Jesper Juhl0730ded2005-09-06 15:17:37 -07002161 current->pdeath_signal = arg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 break;
2163 case PR_GET_PDEATHSIG:
2164 error = put_user(current->pdeath_signal, (int __user *)arg2);
2165 break;
2166 case PR_GET_DUMPABLE:
Michael Kerrisk2030c0f2005-09-16 19:28:02 -07002167 error = current->mm->dumpable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 break;
2169 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02002170 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 error = -EINVAL;
2172 break;
2173 }
2174 current->mm->dumpable = arg2;
2175 break;
2176
2177 case PR_SET_UNALIGN:
2178 error = SET_UNALIGN_CTL(current, arg2);
2179 break;
2180 case PR_GET_UNALIGN:
2181 error = GET_UNALIGN_CTL(current, arg2);
2182 break;
2183 case PR_SET_FPEMU:
2184 error = SET_FPEMU_CTL(current, arg2);
2185 break;
2186 case PR_GET_FPEMU:
2187 error = GET_FPEMU_CTL(current, arg2);
2188 break;
2189 case PR_SET_FPEXC:
2190 error = SET_FPEXC_CTL(current, arg2);
2191 break;
2192 case PR_GET_FPEXC:
2193 error = GET_FPEXC_CTL(current, arg2);
2194 break;
2195 case PR_GET_TIMING:
2196 error = PR_TIMING_STATISTICAL;
2197 break;
2198 case PR_SET_TIMING:
2199 if (arg2 == PR_TIMING_STATISTICAL)
2200 error = 0;
2201 else
2202 error = -EINVAL;
2203 break;
2204
2205 case PR_GET_KEEPCAPS:
2206 if (current->keep_capabilities)
2207 error = 1;
2208 break;
2209 case PR_SET_KEEPCAPS:
2210 if (arg2 != 0 && arg2 != 1) {
2211 error = -EINVAL;
2212 break;
2213 }
2214 current->keep_capabilities = arg2;
2215 break;
2216 case PR_SET_NAME: {
2217 struct task_struct *me = current;
2218 unsigned char ncomm[sizeof(me->comm)];
2219
2220 ncomm[sizeof(me->comm)-1] = 0;
2221 if (strncpy_from_user(ncomm, (char __user *)arg2,
2222 sizeof(me->comm)-1) < 0)
2223 return -EFAULT;
2224 set_task_comm(me, ncomm);
2225 return 0;
2226 }
2227 case PR_GET_NAME: {
2228 struct task_struct *me = current;
2229 unsigned char tcomm[sizeof(me->comm)];
2230
2231 get_task_comm(tcomm, me);
2232 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2233 return -EFAULT;
2234 return 0;
2235 }
Anton Blanchard651d7652006-06-07 16:10:19 +10002236 case PR_GET_ENDIAN:
2237 error = GET_ENDIAN(current, arg2);
2238 break;
2239 case PR_SET_ENDIAN:
2240 error = SET_ENDIAN(current, arg2);
2241 break;
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 default:
2244 error = -EINVAL;
2245 break;
2246 }
2247 return error;
2248}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002249
2250asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2251 struct getcpu_cache __user *cache)
2252{
2253 int err = 0;
2254 int cpu = raw_smp_processor_id();
2255 if (cpup)
2256 err |= put_user(cpu, cpup);
2257 if (nodep)
2258 err |= put_user(cpu_to_node(cpu), nodep);
2259 if (cache) {
2260 /*
2261 * The cache is not needed for this implementation,
2262 * but make sure user programs pass something
2263 * valid. vsyscall implementations can instead make
2264 * good use of the cache. Only use t0 and t1 because
2265 * these are available in both 32bit and 64bit ABI (no
2266 * need for a compat_getcpu). 32bit has enough
2267 * padding
2268 */
2269 unsigned long t0, t1;
Andi Kleen34596dc2006-09-30 01:47:55 +02002270 get_user(t0, &cache->blob[0]);
2271 get_user(t1, &cache->blob[1]);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002272 t0++;
2273 t1++;
Andi Kleen34596dc2006-09-30 01:47:55 +02002274 put_user(t0, &cache->blob[0]);
2275 put_user(t1, &cache->blob[1]);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002276 }
2277 return err ? -EFAULT : 0;
2278}