blob: b88806c66244b2df825f6bb43bbb20b251cce21c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/compat.h>
34#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080035#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/uaccess.h>
38#include <asm/io.h>
39#include <asm/unistd.h>
40
41#ifndef SET_UNALIGN_CTL
42# define SET_UNALIGN_CTL(a,b) (-EINVAL)
43#endif
44#ifndef GET_UNALIGN_CTL
45# define GET_UNALIGN_CTL(a,b) (-EINVAL)
46#endif
47#ifndef SET_FPEMU_CTL
48# define SET_FPEMU_CTL(a,b) (-EINVAL)
49#endif
50#ifndef GET_FPEMU_CTL
51# define GET_FPEMU_CTL(a,b) (-EINVAL)
52#endif
53#ifndef SET_FPEXC_CTL
54# define SET_FPEXC_CTL(a,b) (-EINVAL)
55#endif
56#ifndef GET_FPEXC_CTL
57# define GET_FPEXC_CTL(a,b) (-EINVAL)
58#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100059#ifndef GET_ENDIAN
60# define GET_ENDIAN(a,b) (-EINVAL)
61#endif
62#ifndef SET_ENDIAN
63# define SET_ENDIAN(a,b) (-EINVAL)
64#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * this is where the system-wide overflow UID and GID are defined, for
68 * architectures that now have 32-bit UID/GID but didn't in the past
69 */
70
71int overflowuid = DEFAULT_OVERFLOWUID;
72int overflowgid = DEFAULT_OVERFLOWGID;
73
74#ifdef CONFIG_UID16
75EXPORT_SYMBOL(overflowuid);
76EXPORT_SYMBOL(overflowgid);
77#endif
78
79/*
80 * the same as above, but for filesystems which can only store a 16-bit
81 * UID and GID. as such, this is needed on all architectures
82 */
83
84int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
85int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
86
87EXPORT_SYMBOL(fs_overflowuid);
88EXPORT_SYMBOL(fs_overflowgid);
89
90/*
91 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
92 */
93
94int C_A_D = 1;
95int cad_pid = 1;
96
97/*
98 * Notifier list for kernel code which wants to be called
99 * at shutdown. This is used to stop any idling DMA operations
100 * and the like.
101 */
102
Alan Sterne041c682006-03-27 01:16:30 -0800103static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Alan Sterne041c682006-03-27 01:16:30 -0800105/*
106 * Notifier chain core routines. The exported routines below
107 * are layered on top of these, with appropriate locking added.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Alan Sterne041c682006-03-27 01:16:30 -0800109
110static int notifier_chain_register(struct notifier_block **nl,
111 struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Alan Sterne041c682006-03-27 01:16:30 -0800113 while ((*nl) != NULL) {
114 if (n->priority > (*nl)->priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
Alan Sterne041c682006-03-27 01:16:30 -0800116 nl = &((*nl)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Alan Sterne041c682006-03-27 01:16:30 -0800118 n->next = *nl;
119 rcu_assign_pointer(*nl, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121}
122
Alan Sterne041c682006-03-27 01:16:30 -0800123static int notifier_chain_unregister(struct notifier_block **nl,
124 struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Alan Sterne041c682006-03-27 01:16:30 -0800126 while ((*nl) != NULL) {
127 if ((*nl) == n) {
128 rcu_assign_pointer(*nl, n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
130 }
Alan Sterne041c682006-03-27 01:16:30 -0800131 nl = &((*nl)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return -ENOENT;
134}
135
Alan Sterne041c682006-03-27 01:16:30 -0800136static int __kprobes notifier_call_chain(struct notifier_block **nl,
137 unsigned long val, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Alan Sterne041c682006-03-27 01:16:30 -0800139 int ret = NOTIFY_DONE;
Alan Sternbbb17472006-06-25 05:47:15 -0700140 struct notifier_block *nb, *next_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Alan Sterne041c682006-03-27 01:16:30 -0800142 nb = rcu_dereference(*nl);
143 while (nb) {
Alan Sternbbb17472006-06-25 05:47:15 -0700144 next_nb = rcu_dereference(nb->next);
Alan Sterne041c682006-03-27 01:16:30 -0800145 ret = nb->notifier_call(nb, val, v);
146 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
147 break;
Alan Sternbbb17472006-06-25 05:47:15 -0700148 nb = next_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 return ret;
151}
152
Alan Sterne041c682006-03-27 01:16:30 -0800153/*
154 * Atomic notifier chain routines. Registration and unregistration
155 * use a mutex, and call_chain is synchronized by RCU (no locks).
156 */
157
158/**
159 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
160 * @nh: Pointer to head of the atomic notifier chain
161 * @n: New entry in notifier chain
162 *
163 * Adds a notifier to an atomic notifier chain.
164 *
165 * Currently always returns zero.
166 */
167
168int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
169 struct notifier_block *n)
170{
171 unsigned long flags;
172 int ret;
173
174 spin_lock_irqsave(&nh->lock, flags);
175 ret = notifier_chain_register(&nh->head, n);
176 spin_unlock_irqrestore(&nh->lock, flags);
177 return ret;
178}
179
180EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
181
182/**
183 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
184 * @nh: Pointer to head of the atomic notifier chain
185 * @n: Entry to remove from notifier chain
186 *
187 * Removes a notifier from an atomic notifier chain.
188 *
189 * Returns zero on success or %-ENOENT on failure.
190 */
191int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
192 struct notifier_block *n)
193{
194 unsigned long flags;
195 int ret;
196
197 spin_lock_irqsave(&nh->lock, flags);
198 ret = notifier_chain_unregister(&nh->head, n);
199 spin_unlock_irqrestore(&nh->lock, flags);
200 synchronize_rcu();
201 return ret;
202}
203
204EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
205
206/**
207 * atomic_notifier_call_chain - Call functions in an atomic notifier chain
208 * @nh: Pointer to head of the atomic notifier chain
209 * @val: Value passed unmodified to notifier function
210 * @v: Pointer passed unmodified to notifier function
211 *
212 * Calls each function in a notifier chain in turn. The functions
213 * run in an atomic context, so they must not block.
214 * This routine uses RCU to synchronize with changes to the chain.
215 *
216 * If the return value of the notifier can be and'ed
217 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain
218 * will return immediately, with the return value of
219 * the notifier function which halted execution.
220 * Otherwise the return value is the return value
221 * of the last notifier function called.
222 */
223
224int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
225 unsigned long val, void *v)
226{
227 int ret;
228
229 rcu_read_lock();
230 ret = notifier_call_chain(&nh->head, val, v);
231 rcu_read_unlock();
232 return ret;
233}
234
235EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
236
237/*
238 * Blocking notifier chain routines. All access to the chain is
239 * synchronized by an rwsem.
240 */
241
242/**
243 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
244 * @nh: Pointer to head of the blocking notifier chain
245 * @n: New entry in notifier chain
246 *
247 * Adds a notifier to a blocking notifier chain.
248 * Must be called in process context.
249 *
250 * Currently always returns zero.
251 */
252
253int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
254 struct notifier_block *n)
255{
256 int ret;
257
258 /*
259 * This code gets used during boot-up, when task switching is
260 * not yet working and interrupts must remain disabled. At
261 * such times we must not call down_write().
262 */
263 if (unlikely(system_state == SYSTEM_BOOTING))
264 return notifier_chain_register(&nh->head, n);
265
266 down_write(&nh->rwsem);
267 ret = notifier_chain_register(&nh->head, n);
268 up_write(&nh->rwsem);
269 return ret;
270}
271
272EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
273
274/**
275 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
276 * @nh: Pointer to head of the blocking notifier chain
277 * @n: Entry to remove from notifier chain
278 *
279 * Removes a notifier from a blocking notifier chain.
280 * Must be called from process context.
281 *
282 * Returns zero on success or %-ENOENT on failure.
283 */
284int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
285 struct notifier_block *n)
286{
287 int ret;
288
289 /*
290 * This code gets used during boot-up, when task switching is
291 * not yet working and interrupts must remain disabled. At
292 * such times we must not call down_write().
293 */
294 if (unlikely(system_state == SYSTEM_BOOTING))
295 return notifier_chain_unregister(&nh->head, n);
296
297 down_write(&nh->rwsem);
298 ret = notifier_chain_unregister(&nh->head, n);
299 up_write(&nh->rwsem);
300 return ret;
301}
302
303EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
304
305/**
306 * blocking_notifier_call_chain - Call functions in a blocking notifier chain
307 * @nh: Pointer to head of the blocking notifier chain
308 * @val: Value passed unmodified to notifier function
309 * @v: Pointer passed unmodified to notifier function
310 *
311 * Calls each function in a notifier chain in turn. The functions
312 * run in a process context, so they are allowed to block.
313 *
314 * If the return value of the notifier can be and'ed
315 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain
316 * will return immediately, with the return value of
317 * the notifier function which halted execution.
318 * Otherwise the return value is the return value
319 * of the last notifier function called.
320 */
321
322int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
323 unsigned long val, void *v)
324{
325 int ret;
326
327 down_read(&nh->rwsem);
328 ret = notifier_call_chain(&nh->head, val, v);
329 up_read(&nh->rwsem);
330 return ret;
331}
332
333EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
334
335/*
336 * Raw notifier chain routines. There is no protection;
337 * the caller must provide it. Use at your own risk!
338 */
339
340/**
341 * raw_notifier_chain_register - Add notifier to a raw notifier chain
342 * @nh: Pointer to head of the raw notifier chain
343 * @n: New entry in notifier chain
344 *
345 * Adds a notifier to a raw notifier chain.
346 * All locking must be provided by the caller.
347 *
348 * Currently always returns zero.
349 */
350
351int raw_notifier_chain_register(struct raw_notifier_head *nh,
352 struct notifier_block *n)
353{
354 return notifier_chain_register(&nh->head, n);
355}
356
357EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
358
359/**
360 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
361 * @nh: Pointer to head of the raw notifier chain
362 * @n: Entry to remove from notifier chain
363 *
364 * Removes a notifier from a raw notifier chain.
365 * All locking must be provided by the caller.
366 *
367 * Returns zero on success or %-ENOENT on failure.
368 */
369int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
370 struct notifier_block *n)
371{
372 return notifier_chain_unregister(&nh->head, n);
373}
374
375EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
376
377/**
378 * raw_notifier_call_chain - Call functions in a raw notifier chain
379 * @nh: Pointer to head of the raw notifier chain
380 * @val: Value passed unmodified to notifier function
381 * @v: Pointer passed unmodified to notifier function
382 *
383 * Calls each function in a notifier chain in turn. The functions
384 * run in an undefined context.
385 * All locking must be provided by the caller.
386 *
387 * If the return value of the notifier can be and'ed
388 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain
389 * will return immediately, with the return value of
390 * the notifier function which halted execution.
391 * Otherwise the return value is the return value
392 * of the last notifier function called.
393 */
394
395int raw_notifier_call_chain(struct raw_notifier_head *nh,
396 unsigned long val, void *v)
397{
398 return notifier_call_chain(&nh->head, val, v);
399}
400
401EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403/**
404 * register_reboot_notifier - Register function to be called at reboot time
405 * @nb: Info about notifier function to be called
406 *
407 * Registers a function with the list of functions
408 * to be called at reboot time.
409 *
Alan Sterne041c682006-03-27 01:16:30 -0800410 * Currently always returns zero, as blocking_notifier_chain_register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * always returns zero.
412 */
413
414int register_reboot_notifier(struct notifier_block * nb)
415{
Alan Sterne041c682006-03-27 01:16:30 -0800416 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419EXPORT_SYMBOL(register_reboot_notifier);
420
421/**
422 * unregister_reboot_notifier - Unregister previously registered reboot notifier
423 * @nb: Hook to be unregistered
424 *
425 * Unregisters a previously registered reboot
426 * notifier function.
427 *
428 * Returns zero on success, or %-ENOENT on failure.
429 */
430
431int unregister_reboot_notifier(struct notifier_block * nb)
432{
Alan Sterne041c682006-03-27 01:16:30 -0800433 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436EXPORT_SYMBOL(unregister_reboot_notifier);
437
438static int set_one_prio(struct task_struct *p, int niceval, int error)
439{
440 int no_nice;
441
442 if (p->uid != current->euid &&
443 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
444 error = -EPERM;
445 goto out;
446 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700447 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 error = -EACCES;
449 goto out;
450 }
451 no_nice = security_task_setnice(p, niceval);
452 if (no_nice) {
453 error = no_nice;
454 goto out;
455 }
456 if (error == -ESRCH)
457 error = 0;
458 set_user_nice(p, niceval);
459out:
460 return error;
461}
462
463asmlinkage long sys_setpriority(int which, int who, int niceval)
464{
465 struct task_struct *g, *p;
466 struct user_struct *user;
467 int error = -EINVAL;
468
469 if (which > 2 || which < 0)
470 goto out;
471
472 /* normalize: avoid signed division (rounding problems) */
473 error = -ESRCH;
474 if (niceval < -20)
475 niceval = -20;
476 if (niceval > 19)
477 niceval = 19;
478
479 read_lock(&tasklist_lock);
480 switch (which) {
481 case PRIO_PROCESS:
482 if (!who)
483 who = current->pid;
484 p = find_task_by_pid(who);
485 if (p)
486 error = set_one_prio(p, niceval, error);
487 break;
488 case PRIO_PGRP:
489 if (!who)
490 who = process_group(current);
491 do_each_task_pid(who, PIDTYPE_PGID, p) {
492 error = set_one_prio(p, niceval, error);
493 } while_each_task_pid(who, PIDTYPE_PGID, p);
494 break;
495 case PRIO_USER:
496 user = current->user;
497 if (!who)
498 who = current->uid;
499 else
500 if ((who != current->uid) && !(user = find_user(who)))
501 goto out_unlock; /* No processes for this user */
502
503 do_each_thread(g, p)
504 if (p->uid == who)
505 error = set_one_prio(p, niceval, error);
506 while_each_thread(g, p);
507 if (who != current->uid)
508 free_uid(user); /* For find_user() */
509 break;
510 }
511out_unlock:
512 read_unlock(&tasklist_lock);
513out:
514 return error;
515}
516
517/*
518 * Ugh. To avoid negative return values, "getpriority()" will
519 * not return the normal nice-value, but a negated value that
520 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
521 * to stay compatible.
522 */
523asmlinkage long sys_getpriority(int which, int who)
524{
525 struct task_struct *g, *p;
526 struct user_struct *user;
527 long niceval, retval = -ESRCH;
528
529 if (which > 2 || which < 0)
530 return -EINVAL;
531
532 read_lock(&tasklist_lock);
533 switch (which) {
534 case PRIO_PROCESS:
535 if (!who)
536 who = current->pid;
537 p = find_task_by_pid(who);
538 if (p) {
539 niceval = 20 - task_nice(p);
540 if (niceval > retval)
541 retval = niceval;
542 }
543 break;
544 case PRIO_PGRP:
545 if (!who)
546 who = process_group(current);
547 do_each_task_pid(who, PIDTYPE_PGID, p) {
548 niceval = 20 - task_nice(p);
549 if (niceval > retval)
550 retval = niceval;
551 } while_each_task_pid(who, PIDTYPE_PGID, p);
552 break;
553 case PRIO_USER:
554 user = current->user;
555 if (!who)
556 who = current->uid;
557 else
558 if ((who != current->uid) && !(user = find_user(who)))
559 goto out_unlock; /* No processes for this user */
560
561 do_each_thread(g, p)
562 if (p->uid == who) {
563 niceval = 20 - task_nice(p);
564 if (niceval > retval)
565 retval = niceval;
566 }
567 while_each_thread(g, p);
568 if (who != current->uid)
569 free_uid(user); /* for find_user() */
570 break;
571 }
572out_unlock:
573 read_unlock(&tasklist_lock);
574
575 return retval;
576}
577
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700578/**
579 * emergency_restart - reboot the system
580 *
581 * Without shutting down any hardware or taking any locks
582 * reboot the system. This is called when we know we are in
583 * trouble so this is our best effort to reboot. This is
584 * safe to call in interrupt context.
585 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600586void emergency_restart(void)
587{
588 machine_emergency_restart();
589}
590EXPORT_SYMBOL_GPL(emergency_restart);
591
Adrian Bunk83cc5ed2006-06-25 05:47:41 -0700592static void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600593{
Alan Sterne041c682006-03-27 01:16:30 -0800594 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600595 system_state = SYSTEM_RESTART;
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600596 device_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700597}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800598
599/**
600 * kernel_restart - reboot the system
601 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800602 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800603 *
604 * Shutdown everything and perform a clean reboot.
605 * This is not safe to call in interrupt context.
606 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700607void kernel_restart(char *cmd)
608{
609 kernel_restart_prepare(cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600610 if (!cmd) {
611 printk(KERN_EMERG "Restarting system.\n");
612 } else {
613 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
614 }
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600615 machine_restart(cmd);
616}
617EXPORT_SYMBOL_GPL(kernel_restart);
618
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700619/**
620 * kernel_kexec - reboot the system
621 *
622 * Move into place and start executing a preloaded standalone
623 * executable. If nothing was preloaded return an error.
624 */
Adrian Bunk83cc5ed2006-06-25 05:47:41 -0700625static void kernel_kexec(void)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600626{
627#ifdef CONFIG_KEXEC
628 struct kimage *image;
Al Viro4bb80892006-02-01 05:57:32 -0500629 image = xchg(&kexec_image, NULL);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600630 if (!image) {
631 return;
632 }
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700633 kernel_restart_prepare(NULL);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600634 printk(KERN_EMERG "Starting new kernel\n");
635 machine_shutdown();
636 machine_kexec(image);
637#endif
638}
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600639
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500640void kernel_shutdown_prepare(enum system_states state)
641{
Alan Sterne041c682006-03-27 01:16:30 -0800642 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500643 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
644 system_state = state;
645 device_shutdown();
646}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700647/**
648 * kernel_halt - halt the system
649 *
650 * Shutdown everything and perform a clean system halt.
651 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700652void kernel_halt(void)
653{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500654 kernel_shutdown_prepare(SYSTEM_HALT);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600655 printk(KERN_EMERG "System halted.\n");
656 machine_halt();
657}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500658
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600659EXPORT_SYMBOL_GPL(kernel_halt);
660
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700661/**
662 * kernel_power_off - power_off the system
663 *
664 * Shutdown everything and perform a clean system power_off.
665 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700666void kernel_power_off(void)
667{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500668 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600669 printk(KERN_EMERG "Power down.\n");
670 machine_power_off();
671}
672EXPORT_SYMBOL_GPL(kernel_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673/*
674 * Reboot system call: for obvious reasons only root may call it,
675 * and even root needs to set up some magic numbers in the registers
676 * so that some mistake won't make this reboot the whole machine.
677 * You can also set the meaning of the ctrl-alt-del-key here.
678 *
679 * reboot doesn't sync: do that yourself before calling this.
680 */
681asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
682{
683 char buffer[256];
684
685 /* We only trust the superuser with rebooting the system. */
686 if (!capable(CAP_SYS_BOOT))
687 return -EPERM;
688
689 /* For safety, we require "magic" arguments. */
690 if (magic1 != LINUX_REBOOT_MAGIC1 ||
691 (magic2 != LINUX_REBOOT_MAGIC2 &&
692 magic2 != LINUX_REBOOT_MAGIC2A &&
693 magic2 != LINUX_REBOOT_MAGIC2B &&
694 magic2 != LINUX_REBOOT_MAGIC2C))
695 return -EINVAL;
696
Eric W. Biederman5e382912006-01-08 01:03:46 -0800697 /* Instead of trying to make the power_off code look like
698 * halt when pm_power_off is not set do it the easy way.
699 */
700 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
701 cmd = LINUX_REBOOT_CMD_HALT;
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 lock_kernel();
704 switch (cmd) {
705 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600706 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
708
709 case LINUX_REBOOT_CMD_CAD_ON:
710 C_A_D = 1;
711 break;
712
713 case LINUX_REBOOT_CMD_CAD_OFF:
714 C_A_D = 0;
715 break;
716
717 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600718 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unlock_kernel();
720 do_exit(0);
721 break;
722
723 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600724 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 unlock_kernel();
726 do_exit(0);
727 break;
728
729 case LINUX_REBOOT_CMD_RESTART2:
730 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
731 unlock_kernel();
732 return -EFAULT;
733 }
734 buffer[sizeof(buffer) - 1] = '\0';
735
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600736 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
738
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700739 case LINUX_REBOOT_CMD_KEXEC:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600740 kernel_kexec();
741 unlock_kernel();
742 return -EINVAL;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744#ifdef CONFIG_SOFTWARE_SUSPEND
745 case LINUX_REBOOT_CMD_SW_SUSPEND:
746 {
747 int ret = software_suspend();
748 unlock_kernel();
749 return ret;
750 }
751#endif
752
753 default:
754 unlock_kernel();
755 return -EINVAL;
756 }
757 unlock_kernel();
758 return 0;
759}
760
761static void deferred_cad(void *dummy)
762{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600763 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/*
767 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
768 * As it's called within an interrupt, it may NOT sync: the only choice
769 * is whether to reboot at once, or just ignore the ctrl-alt-del.
770 */
771void ctrl_alt_del(void)
772{
773 static DECLARE_WORK(cad_work, deferred_cad, NULL);
774
775 if (C_A_D)
776 schedule_work(&cad_work);
777 else
778 kill_proc(cad_pid, SIGINT, 1);
779}
780
781
782/*
783 * Unprivileged users may change the real gid to the effective gid
784 * or vice versa. (BSD-style)
785 *
786 * If you set the real gid at all, or set the effective gid to a value not
787 * equal to the real gid, then the saved gid is set to the new effective gid.
788 *
789 * This makes it possible for a setgid program to completely drop its
790 * privileges, which is often a useful assertion to make when you are doing
791 * a security audit over a program.
792 *
793 * The general idea is that a program which uses just setregid() will be
794 * 100% compatible with BSD. A program which uses just setgid() will be
795 * 100% compatible with POSIX with saved IDs.
796 *
797 * SMP: There are not races, the GIDs are checked only by filesystem
798 * operations (as far as semantic preservation is concerned).
799 */
800asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
801{
802 int old_rgid = current->gid;
803 int old_egid = current->egid;
804 int new_rgid = old_rgid;
805 int new_egid = old_egid;
806 int retval;
807
808 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
809 if (retval)
810 return retval;
811
812 if (rgid != (gid_t) -1) {
813 if ((old_rgid == rgid) ||
814 (current->egid==rgid) ||
815 capable(CAP_SETGID))
816 new_rgid = rgid;
817 else
818 return -EPERM;
819 }
820 if (egid != (gid_t) -1) {
821 if ((old_rgid == egid) ||
822 (current->egid == egid) ||
823 (current->sgid == egid) ||
824 capable(CAP_SETGID))
825 new_egid = egid;
826 else {
827 return -EPERM;
828 }
829 }
830 if (new_egid != old_egid)
831 {
Alan Coxd6e71142005-06-23 00:09:43 -0700832 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700833 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 if (rgid != (gid_t) -1 ||
836 (egid != (gid_t) -1 && egid != old_rgid))
837 current->sgid = new_egid;
838 current->fsgid = new_egid;
839 current->egid = new_egid;
840 current->gid = new_rgid;
841 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800842 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return 0;
844}
845
846/*
847 * setgid() is implemented like SysV w/ SAVED_IDS
848 *
849 * SMP: Same implicit races as above.
850 */
851asmlinkage long sys_setgid(gid_t gid)
852{
853 int old_egid = current->egid;
854 int retval;
855
856 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
857 if (retval)
858 return retval;
859
860 if (capable(CAP_SETGID))
861 {
862 if(old_egid != gid)
863 {
Alan Coxd6e71142005-06-23 00:09:43 -0700864 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700865 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 current->gid = current->egid = current->sgid = current->fsgid = gid;
868 }
869 else if ((gid == current->gid) || (gid == current->sgid))
870 {
871 if(old_egid != gid)
872 {
Alan Coxd6e71142005-06-23 00:09:43 -0700873 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700874 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 current->egid = current->fsgid = gid;
877 }
878 else
879 return -EPERM;
880
881 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800882 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return 0;
884}
885
886static int set_user(uid_t new_ruid, int dumpclear)
887{
888 struct user_struct *new_user;
889
890 new_user = alloc_uid(new_ruid);
891 if (!new_user)
892 return -EAGAIN;
893
894 if (atomic_read(&new_user->processes) >=
895 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
896 new_user != &root_user) {
897 free_uid(new_user);
898 return -EAGAIN;
899 }
900
901 switch_uid(new_user);
902
903 if(dumpclear)
904 {
Alan Coxd6e71142005-06-23 00:09:43 -0700905 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700906 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908 current->uid = new_ruid;
909 return 0;
910}
911
912/*
913 * Unprivileged users may change the real uid to the effective uid
914 * or vice versa. (BSD-style)
915 *
916 * If you set the real uid at all, or set the effective uid to a value not
917 * equal to the real uid, then the saved uid is set to the new effective uid.
918 *
919 * This makes it possible for a setuid program to completely drop its
920 * privileges, which is often a useful assertion to make when you are doing
921 * a security audit over a program.
922 *
923 * The general idea is that a program which uses just setreuid() will be
924 * 100% compatible with BSD. A program which uses just setuid() will be
925 * 100% compatible with POSIX with saved IDs.
926 */
927asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
928{
929 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
930 int retval;
931
932 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
933 if (retval)
934 return retval;
935
936 new_ruid = old_ruid = current->uid;
937 new_euid = old_euid = current->euid;
938 old_suid = current->suid;
939
940 if (ruid != (uid_t) -1) {
941 new_ruid = ruid;
942 if ((old_ruid != ruid) &&
943 (current->euid != ruid) &&
944 !capable(CAP_SETUID))
945 return -EPERM;
946 }
947
948 if (euid != (uid_t) -1) {
949 new_euid = euid;
950 if ((old_ruid != euid) &&
951 (current->euid != euid) &&
952 (current->suid != euid) &&
953 !capable(CAP_SETUID))
954 return -EPERM;
955 }
956
957 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
958 return -EAGAIN;
959
960 if (new_euid != old_euid)
961 {
Alan Coxd6e71142005-06-23 00:09:43 -0700962 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700963 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965 current->fsuid = current->euid = new_euid;
966 if (ruid != (uid_t) -1 ||
967 (euid != (uid_t) -1 && euid != old_ruid))
968 current->suid = current->euid;
969 current->fsuid = current->euid;
970
971 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800972 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
975}
976
977
978
979/*
980 * setuid() is implemented like SysV with SAVED_IDS
981 *
982 * Note that SAVED_ID's is deficient in that a setuid root program
983 * like sendmail, for example, cannot set its uid to be a normal
984 * user and then switch back, because if you're root, setuid() sets
985 * the saved uid too. If you don't like this, blame the bright people
986 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
987 * will allow a root program to temporarily drop privileges and be able to
988 * regain them by swapping the real and effective uid.
989 */
990asmlinkage long sys_setuid(uid_t uid)
991{
992 int old_euid = current->euid;
993 int old_ruid, old_suid, new_ruid, new_suid;
994 int retval;
995
996 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
997 if (retval)
998 return retval;
999
1000 old_ruid = new_ruid = current->uid;
1001 old_suid = current->suid;
1002 new_suid = old_suid;
1003
1004 if (capable(CAP_SETUID)) {
1005 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1006 return -EAGAIN;
1007 new_suid = uid;
1008 } else if ((uid != current->uid) && (uid != new_suid))
1009 return -EPERM;
1010
1011 if (old_euid != uid)
1012 {
Alan Coxd6e71142005-06-23 00:09:43 -07001013 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001014 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 current->fsuid = current->euid = uid;
1017 current->suid = new_suid;
1018
1019 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001020 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1023}
1024
1025
1026/*
1027 * This function implements a generic ability to update ruid, euid,
1028 * and suid. This allows you to implement the 4.4 compatible seteuid().
1029 */
1030asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1031{
1032 int old_ruid = current->uid;
1033 int old_euid = current->euid;
1034 int old_suid = current->suid;
1035 int retval;
1036
1037 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1038 if (retval)
1039 return retval;
1040
1041 if (!capable(CAP_SETUID)) {
1042 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1043 (ruid != current->euid) && (ruid != current->suid))
1044 return -EPERM;
1045 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1046 (euid != current->euid) && (euid != current->suid))
1047 return -EPERM;
1048 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1049 (suid != current->euid) && (suid != current->suid))
1050 return -EPERM;
1051 }
1052 if (ruid != (uid_t) -1) {
1053 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1054 return -EAGAIN;
1055 }
1056 if (euid != (uid_t) -1) {
1057 if (euid != current->euid)
1058 {
Alan Coxd6e71142005-06-23 00:09:43 -07001059 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001060 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062 current->euid = euid;
1063 }
1064 current->fsuid = current->euid;
1065 if (suid != (uid_t) -1)
1066 current->suid = suid;
1067
1068 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001069 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1072}
1073
1074asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1075{
1076 int retval;
1077
1078 if (!(retval = put_user(current->uid, ruid)) &&
1079 !(retval = put_user(current->euid, euid)))
1080 retval = put_user(current->suid, suid);
1081
1082 return retval;
1083}
1084
1085/*
1086 * Same as above, but for rgid, egid, sgid.
1087 */
1088asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1089{
1090 int retval;
1091
1092 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1093 if (retval)
1094 return retval;
1095
1096 if (!capable(CAP_SETGID)) {
1097 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1098 (rgid != current->egid) && (rgid != current->sgid))
1099 return -EPERM;
1100 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1101 (egid != current->egid) && (egid != current->sgid))
1102 return -EPERM;
1103 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1104 (sgid != current->egid) && (sgid != current->sgid))
1105 return -EPERM;
1106 }
1107 if (egid != (gid_t) -1) {
1108 if (egid != current->egid)
1109 {
Alan Coxd6e71142005-06-23 00:09:43 -07001110 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001111 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113 current->egid = egid;
1114 }
1115 current->fsgid = current->egid;
1116 if (rgid != (gid_t) -1)
1117 current->gid = rgid;
1118 if (sgid != (gid_t) -1)
1119 current->sgid = sgid;
1120
1121 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001122 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return 0;
1124}
1125
1126asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1127{
1128 int retval;
1129
1130 if (!(retval = put_user(current->gid, rgid)) &&
1131 !(retval = put_user(current->egid, egid)))
1132 retval = put_user(current->sgid, sgid);
1133
1134 return retval;
1135}
1136
1137
1138/*
1139 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1140 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1141 * whatever uid it wants to). It normally shadows "euid", except when
1142 * explicitly set by setfsuid() or for access..
1143 */
1144asmlinkage long sys_setfsuid(uid_t uid)
1145{
1146 int old_fsuid;
1147
1148 old_fsuid = current->fsuid;
1149 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1150 return old_fsuid;
1151
1152 if (uid == current->uid || uid == current->euid ||
1153 uid == current->suid || uid == current->fsuid ||
1154 capable(CAP_SETUID))
1155 {
1156 if (uid != old_fsuid)
1157 {
Alan Coxd6e71142005-06-23 00:09:43 -07001158 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001159 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161 current->fsuid = uid;
1162 }
1163
1164 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001165 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1168
1169 return old_fsuid;
1170}
1171
1172/*
1173 * Samma på svenska..
1174 */
1175asmlinkage long sys_setfsgid(gid_t gid)
1176{
1177 int old_fsgid;
1178
1179 old_fsgid = current->fsgid;
1180 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1181 return old_fsgid;
1182
1183 if (gid == current->gid || gid == current->egid ||
1184 gid == current->sgid || gid == current->fsgid ||
1185 capable(CAP_SETGID))
1186 {
1187 if (gid != old_fsgid)
1188 {
Alan Coxd6e71142005-06-23 00:09:43 -07001189 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001190 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192 current->fsgid = gid;
1193 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001194 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 return old_fsgid;
1197}
1198
1199asmlinkage long sys_times(struct tms __user * tbuf)
1200{
1201 /*
1202 * In the SMP world we might just be unlucky and have one of
1203 * the times increment as we use it. Since the value is an
1204 * atomically safe type this is just fine. Conceptually its
1205 * as if the syscall took an instant longer to occur.
1206 */
1207 if (tbuf) {
1208 struct tms tmp;
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001209 struct task_struct *tsk = current;
1210 struct task_struct *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 cputime_t utime, stime, cutime, cstime;
1212
Oleg Nesterov7d7185c2006-03-28 16:11:21 -08001213 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001214 utime = tsk->signal->utime;
1215 stime = tsk->signal->stime;
1216 t = tsk;
1217 do {
1218 utime = cputime_add(utime, t->utime);
1219 stime = cputime_add(stime, t->stime);
1220 t = next_thread(t);
1221 } while (t != tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001223 cutime = tsk->signal->cutime;
1224 cstime = tsk->signal->cstime;
1225 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 tmp.tms_utime = cputime_to_clock_t(utime);
1228 tmp.tms_stime = cputime_to_clock_t(stime);
1229 tmp.tms_cutime = cputime_to_clock_t(cutime);
1230 tmp.tms_cstime = cputime_to_clock_t(cstime);
1231 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1232 return -EFAULT;
1233 }
1234 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1235}
1236
1237/*
1238 * This needs some heavy checking ...
1239 * I just haven't the stomach for it. I also don't fully
1240 * understand sessions/pgrp etc. Let somebody who does explain it.
1241 *
1242 * OK, I think I have the protection semantics right.... this is really
1243 * only important on a multi-user system anyway, to make sure one user
1244 * can't send a signal to a process owned by another. -TYT, 12/12/91
1245 *
1246 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1247 * LBT 04.03.94
1248 */
1249
1250asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1251{
1252 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001253 struct task_struct *group_leader = current->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int err = -EINVAL;
1255
1256 if (!pid)
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001257 pid = group_leader->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (!pgid)
1259 pgid = pid;
1260 if (pgid < 0)
1261 return -EINVAL;
1262
1263 /* From this point forward we keep holding onto the tasklist lock
1264 * so that our parent does not change from under us. -DaveM
1265 */
1266 write_lock_irq(&tasklist_lock);
1267
1268 err = -ESRCH;
1269 p = find_task_by_pid(pid);
1270 if (!p)
1271 goto out;
1272
1273 err = -EINVAL;
1274 if (!thread_group_leader(p))
1275 goto out;
1276
Oleg Nesterovf7dd7952006-01-08 01:03:59 -08001277 if (p->real_parent == group_leader) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 err = -EPERM;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001279 if (p->signal->session != group_leader->signal->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 goto out;
1281 err = -EACCES;
1282 if (p->did_exec)
1283 goto out;
1284 } else {
1285 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001286 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 goto out;
1288 }
1289
1290 err = -EPERM;
1291 if (p->signal->leader)
1292 goto out;
1293
1294 if (pgid != pid) {
1295 struct task_struct *p;
1296
1297 do_each_task_pid(pgid, PIDTYPE_PGID, p) {
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001298 if (p->signal->session == group_leader->signal->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 goto ok_pgid;
1300 } while_each_task_pid(pgid, PIDTYPE_PGID, p);
1301 goto out;
1302 }
1303
1304ok_pgid:
1305 err = security_task_setpgid(p, pgid);
1306 if (err)
1307 goto out;
1308
1309 if (process_group(p) != pgid) {
1310 detach_pid(p, PIDTYPE_PGID);
1311 p->signal->pgrp = pgid;
1312 attach_pid(p, PIDTYPE_PGID, pgid);
1313 }
1314
1315 err = 0;
1316out:
1317 /* All paths lead to here, thus we are safe. -DaveM */
1318 write_unlock_irq(&tasklist_lock);
1319 return err;
1320}
1321
1322asmlinkage long sys_getpgid(pid_t pid)
1323{
1324 if (!pid) {
1325 return process_group(current);
1326 } else {
1327 int retval;
1328 struct task_struct *p;
1329
1330 read_lock(&tasklist_lock);
1331 p = find_task_by_pid(pid);
1332
1333 retval = -ESRCH;
1334 if (p) {
1335 retval = security_task_getpgid(p);
1336 if (!retval)
1337 retval = process_group(p);
1338 }
1339 read_unlock(&tasklist_lock);
1340 return retval;
1341 }
1342}
1343
1344#ifdef __ARCH_WANT_SYS_GETPGRP
1345
1346asmlinkage long sys_getpgrp(void)
1347{
1348 /* SMP - assuming writes are word atomic this is fine */
1349 return process_group(current);
1350}
1351
1352#endif
1353
1354asmlinkage long sys_getsid(pid_t pid)
1355{
1356 if (!pid) {
1357 return current->signal->session;
1358 } else {
1359 int retval;
1360 struct task_struct *p;
1361
1362 read_lock(&tasklist_lock);
1363 p = find_task_by_pid(pid);
1364
1365 retval = -ESRCH;
1366 if(p) {
1367 retval = security_task_getsid(p);
1368 if (!retval)
1369 retval = p->signal->session;
1370 }
1371 read_unlock(&tasklist_lock);
1372 return retval;
1373 }
1374}
1375
1376asmlinkage long sys_setsid(void)
1377{
Oren Laadane19f2472006-01-08 01:03:58 -08001378 struct task_struct *group_leader = current->group_leader;
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001379 pid_t session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int err = -EPERM;
1381
Ingo Molnar70522e12006-03-23 03:00:31 -08001382 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 write_lock_irq(&tasklist_lock);
1384
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001385 /* Fail if I am already a session leader */
1386 if (group_leader->signal->leader)
1387 goto out;
1388
1389 session = group_leader->pid;
1390 /* Fail if a process group id already exists that equals the
1391 * proposed session id.
1392 *
1393 * Don't check if session id == 1 because kernel threads use this
1394 * session id and so the check will always fail and make it so
1395 * init cannot successfully call setsid.
1396 */
1397 if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 goto out;
1399
Oren Laadane19f2472006-01-08 01:03:58 -08001400 group_leader->signal->leader = 1;
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001401 __set_special_pids(session, session);
Oren Laadane19f2472006-01-08 01:03:58 -08001402 group_leader->signal->tty = NULL;
1403 group_leader->signal->tty_old_pgrp = 0;
1404 err = process_group(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405out:
1406 write_unlock_irq(&tasklist_lock);
Ingo Molnar70522e12006-03-23 03:00:31 -08001407 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return err;
1409}
1410
1411/*
1412 * Supplementary group IDs
1413 */
1414
1415/* init to 2 - one for init_task, one to ensure it is never freed */
1416struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1417
1418struct group_info *groups_alloc(int gidsetsize)
1419{
1420 struct group_info *group_info;
1421 int nblocks;
1422 int i;
1423
1424 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1425 /* Make sure we always allocate at least one indirect block pointer */
1426 nblocks = nblocks ? : 1;
1427 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1428 if (!group_info)
1429 return NULL;
1430 group_info->ngroups = gidsetsize;
1431 group_info->nblocks = nblocks;
1432 atomic_set(&group_info->usage, 1);
1433
1434 if (gidsetsize <= NGROUPS_SMALL) {
1435 group_info->blocks[0] = group_info->small_block;
1436 } else {
1437 for (i = 0; i < nblocks; i++) {
1438 gid_t *b;
1439 b = (void *)__get_free_page(GFP_USER);
1440 if (!b)
1441 goto out_undo_partial_alloc;
1442 group_info->blocks[i] = b;
1443 }
1444 }
1445 return group_info;
1446
1447out_undo_partial_alloc:
1448 while (--i >= 0) {
1449 free_page((unsigned long)group_info->blocks[i]);
1450 }
1451 kfree(group_info);
1452 return NULL;
1453}
1454
1455EXPORT_SYMBOL(groups_alloc);
1456
1457void groups_free(struct group_info *group_info)
1458{
1459 if (group_info->blocks[0] != group_info->small_block) {
1460 int i;
1461 for (i = 0; i < group_info->nblocks; i++)
1462 free_page((unsigned long)group_info->blocks[i]);
1463 }
1464 kfree(group_info);
1465}
1466
1467EXPORT_SYMBOL(groups_free);
1468
1469/* export the group_info to a user-space array */
1470static int groups_to_user(gid_t __user *grouplist,
1471 struct group_info *group_info)
1472{
1473 int i;
1474 int count = group_info->ngroups;
1475
1476 for (i = 0; i < group_info->nblocks; i++) {
1477 int cp_count = min(NGROUPS_PER_BLOCK, count);
1478 int off = i * NGROUPS_PER_BLOCK;
1479 int len = cp_count * sizeof(*grouplist);
1480
1481 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1482 return -EFAULT;
1483
1484 count -= cp_count;
1485 }
1486 return 0;
1487}
1488
1489/* fill a group_info from a user-space array - it must be allocated already */
1490static int groups_from_user(struct group_info *group_info,
1491 gid_t __user *grouplist)
1492 {
1493 int i;
1494 int count = group_info->ngroups;
1495
1496 for (i = 0; i < group_info->nblocks; i++) {
1497 int cp_count = min(NGROUPS_PER_BLOCK, count);
1498 int off = i * NGROUPS_PER_BLOCK;
1499 int len = cp_count * sizeof(*grouplist);
1500
1501 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1502 return -EFAULT;
1503
1504 count -= cp_count;
1505 }
1506 return 0;
1507}
1508
Domen Puncerebe8b542005-05-05 16:16:19 -07001509/* a simple Shell sort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510static void groups_sort(struct group_info *group_info)
1511{
1512 int base, max, stride;
1513 int gidsetsize = group_info->ngroups;
1514
1515 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1516 ; /* nothing */
1517 stride /= 3;
1518
1519 while (stride) {
1520 max = gidsetsize - stride;
1521 for (base = 0; base < max; base++) {
1522 int left = base;
1523 int right = left + stride;
1524 gid_t tmp = GROUP_AT(group_info, right);
1525
1526 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1527 GROUP_AT(group_info, right) =
1528 GROUP_AT(group_info, left);
1529 right = left;
1530 left -= stride;
1531 }
1532 GROUP_AT(group_info, right) = tmp;
1533 }
1534 stride /= 3;
1535 }
1536}
1537
1538/* a simple bsearch */
David Howells3e301482005-06-23 22:00:56 -07001539int groups_search(struct group_info *group_info, gid_t grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001541 unsigned int left, right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 if (!group_info)
1544 return 0;
1545
1546 left = 0;
1547 right = group_info->ngroups;
1548 while (left < right) {
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001549 unsigned int mid = (left+right)/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 int cmp = grp - GROUP_AT(group_info, mid);
1551 if (cmp > 0)
1552 left = mid + 1;
1553 else if (cmp < 0)
1554 right = mid;
1555 else
1556 return 1;
1557 }
1558 return 0;
1559}
1560
1561/* validate and set current->group_info */
1562int set_current_groups(struct group_info *group_info)
1563{
1564 int retval;
1565 struct group_info *old_info;
1566
1567 retval = security_task_setgroups(group_info);
1568 if (retval)
1569 return retval;
1570
1571 groups_sort(group_info);
1572 get_group_info(group_info);
1573
1574 task_lock(current);
1575 old_info = current->group_info;
1576 current->group_info = group_info;
1577 task_unlock(current);
1578
1579 put_group_info(old_info);
1580
1581 return 0;
1582}
1583
1584EXPORT_SYMBOL(set_current_groups);
1585
1586asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1587{
1588 int i = 0;
1589
1590 /*
1591 * SMP: Nobody else can change our grouplist. Thus we are
1592 * safe.
1593 */
1594
1595 if (gidsetsize < 0)
1596 return -EINVAL;
1597
1598 /* no need to grab task_lock here; it cannot change */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 i = current->group_info->ngroups;
1600 if (gidsetsize) {
1601 if (i > gidsetsize) {
1602 i = -EINVAL;
1603 goto out;
1604 }
1605 if (groups_to_user(grouplist, current->group_info)) {
1606 i = -EFAULT;
1607 goto out;
1608 }
1609 }
1610out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return i;
1612}
1613
1614/*
1615 * SMP: Our groups are copy-on-write. We can set them safely
1616 * without another task interfering.
1617 */
1618
1619asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1620{
1621 struct group_info *group_info;
1622 int retval;
1623
1624 if (!capable(CAP_SETGID))
1625 return -EPERM;
1626 if ((unsigned)gidsetsize > NGROUPS_MAX)
1627 return -EINVAL;
1628
1629 group_info = groups_alloc(gidsetsize);
1630 if (!group_info)
1631 return -ENOMEM;
1632 retval = groups_from_user(group_info, grouplist);
1633 if (retval) {
1634 put_group_info(group_info);
1635 return retval;
1636 }
1637
1638 retval = set_current_groups(group_info);
1639 put_group_info(group_info);
1640
1641 return retval;
1642}
1643
1644/*
1645 * Check whether we're fsgid/egid or in the supplemental group..
1646 */
1647int in_group_p(gid_t grp)
1648{
1649 int retval = 1;
1650 if (grp != current->fsgid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 retval = groups_search(current->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653 return retval;
1654}
1655
1656EXPORT_SYMBOL(in_group_p);
1657
1658int in_egroup_p(gid_t grp)
1659{
1660 int retval = 1;
1661 if (grp != current->egid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 retval = groups_search(current->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664 return retval;
1665}
1666
1667EXPORT_SYMBOL(in_egroup_p);
1668
1669DECLARE_RWSEM(uts_sem);
1670
David S. Miller393b0722005-11-10 12:47:50 -08001671EXPORT_SYMBOL(uts_sem);
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673asmlinkage long sys_newuname(struct new_utsname __user * name)
1674{
1675 int errno = 0;
1676
1677 down_read(&uts_sem);
1678 if (copy_to_user(name,&system_utsname,sizeof *name))
1679 errno = -EFAULT;
1680 up_read(&uts_sem);
1681 return errno;
1682}
1683
1684asmlinkage long sys_sethostname(char __user *name, int len)
1685{
1686 int errno;
1687 char tmp[__NEW_UTS_LEN];
1688
1689 if (!capable(CAP_SYS_ADMIN))
1690 return -EPERM;
1691 if (len < 0 || len > __NEW_UTS_LEN)
1692 return -EINVAL;
1693 down_write(&uts_sem);
1694 errno = -EFAULT;
1695 if (!copy_from_user(tmp, name, len)) {
1696 memcpy(system_utsname.nodename, tmp, len);
1697 system_utsname.nodename[len] = 0;
1698 errno = 0;
1699 }
1700 up_write(&uts_sem);
1701 return errno;
1702}
1703
1704#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1705
1706asmlinkage long sys_gethostname(char __user *name, int len)
1707{
1708 int i, errno;
1709
1710 if (len < 0)
1711 return -EINVAL;
1712 down_read(&uts_sem);
1713 i = 1 + strlen(system_utsname.nodename);
1714 if (i > len)
1715 i = len;
1716 errno = 0;
1717 if (copy_to_user(name, system_utsname.nodename, i))
1718 errno = -EFAULT;
1719 up_read(&uts_sem);
1720 return errno;
1721}
1722
1723#endif
1724
1725/*
1726 * Only setdomainname; getdomainname can be implemented by calling
1727 * uname()
1728 */
1729asmlinkage long sys_setdomainname(char __user *name, int len)
1730{
1731 int errno;
1732 char tmp[__NEW_UTS_LEN];
1733
1734 if (!capable(CAP_SYS_ADMIN))
1735 return -EPERM;
1736 if (len < 0 || len > __NEW_UTS_LEN)
1737 return -EINVAL;
1738
1739 down_write(&uts_sem);
1740 errno = -EFAULT;
1741 if (!copy_from_user(tmp, name, len)) {
1742 memcpy(system_utsname.domainname, tmp, len);
1743 system_utsname.domainname[len] = 0;
1744 errno = 0;
1745 }
1746 up_write(&uts_sem);
1747 return errno;
1748}
1749
1750asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1751{
1752 if (resource >= RLIM_NLIMITS)
1753 return -EINVAL;
1754 else {
1755 struct rlimit value;
1756 task_lock(current->group_leader);
1757 value = current->signal->rlim[resource];
1758 task_unlock(current->group_leader);
1759 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1760 }
1761}
1762
1763#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1764
1765/*
1766 * Back compatibility for getrlimit. Needed for some apps.
1767 */
1768
1769asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1770{
1771 struct rlimit x;
1772 if (resource >= RLIM_NLIMITS)
1773 return -EINVAL;
1774
1775 task_lock(current->group_leader);
1776 x = current->signal->rlim[resource];
1777 task_unlock(current->group_leader);
1778 if(x.rlim_cur > 0x7FFFFFFF)
1779 x.rlim_cur = 0x7FFFFFFF;
1780 if(x.rlim_max > 0x7FFFFFFF)
1781 x.rlim_max = 0x7FFFFFFF;
1782 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1783}
1784
1785#endif
1786
1787asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1788{
1789 struct rlimit new_rlim, *old_rlim;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001790 unsigned long it_prof_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int retval;
1792
1793 if (resource >= RLIM_NLIMITS)
1794 return -EINVAL;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001795 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return -EFAULT;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001797 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1798 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 old_rlim = current->signal->rlim + resource;
1800 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1801 !capable(CAP_SYS_RESOURCE))
1802 return -EPERM;
1803 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001804 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 retval = security_task_setrlimit(resource, &new_rlim);
1807 if (retval)
1808 return retval;
1809
1810 task_lock(current->group_leader);
1811 *old_rlim = new_rlim;
1812 task_unlock(current->group_leader);
1813
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001814 if (resource != RLIMIT_CPU)
1815 goto out;
Andrew Mortond3561f72006-03-24 03:18:36 -08001816
1817 /*
1818 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1819 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1820 * very long-standing error, and fixing it now risks breakage of
1821 * applications, so we live with it
1822 */
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001823 if (new_rlim.rlim_cur == RLIM_INFINITY)
1824 goto out;
1825
1826 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
1827 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
Andrew Mortone0661112006-03-24 03:18:35 -08001828 unsigned long rlim_cur = new_rlim.rlim_cur;
1829 cputime_t cputime;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001830
Andrew Mortone0661112006-03-24 03:18:35 -08001831 if (rlim_cur == 0) {
1832 /*
1833 * The caller is asking for an immediate RLIMIT_CPU
1834 * expiry. But we use the zero value to mean "it was
1835 * never set". So let's cheat and make it one second
1836 * instead
1837 */
1838 rlim_cur = 1;
1839 }
1840 cputime = secs_to_cputime(rlim_cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 read_lock(&tasklist_lock);
1842 spin_lock_irq(&current->sighand->siglock);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001843 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 spin_unlock_irq(&current->sighand->siglock);
1845 read_unlock(&tasklist_lock);
1846 }
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001847out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return 0;
1849}
1850
1851/*
1852 * It would make sense to put struct rusage in the task_struct,
1853 * except that would make the task_struct be *really big*. After
1854 * task_struct gets moved into malloc'ed memory, it would
1855 * make sense to do this. It will make moving the rest of the information
1856 * a lot simpler! (Which we're not doing right now because we're not
1857 * measuring them yet).
1858 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1860 * races with threads incrementing their own counters. But since word
1861 * reads are atomic, we either get new values or old values and we don't
1862 * care which for the sums. We always take the siglock to protect reading
1863 * the c* fields from p->signal from races with exit.c updating those
1864 * fields when reaping, so a sample either gets all the additions of a
1865 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001866 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001867 * Locking:
1868 * We need to take the siglock for CHILDEREN, SELF and BOTH
1869 * for the cases current multithreaded, non-current single threaded
1870 * non-current multithreaded. Thread traversal is now safe with
1871 * the siglock held.
1872 * Strictly speaking, we donot need to take the siglock if we are current and
1873 * single threaded, as no one else can take our signal_struct away, no one
1874 * else can reap the children to update signal->c* counters, and no one else
1875 * can race with the signal-> fields. If we do not take any lock, the
1876 * signal-> fields could be read out of order while another thread was just
1877 * exiting. So we should place a read memory barrier when we avoid the lock.
1878 * On the writer side, write memory barrier is implied in __exit_signal
1879 * as __exit_signal releases the siglock spinlock after updating the signal->
1880 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001881 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 */
1883
1884static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1885{
1886 struct task_struct *t;
1887 unsigned long flags;
1888 cputime_t utime, stime;
1889
1890 memset((char *) r, 0, sizeof *r);
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001891 utime = stime = cputime_zero;
1892
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001893 rcu_read_lock();
1894 if (!lock_task_sighand(p, &flags)) {
1895 rcu_read_unlock();
1896 return;
1897 }
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001900 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 utime = p->signal->cutime;
1903 stime = p->signal->cstime;
1904 r->ru_nvcsw = p->signal->cnvcsw;
1905 r->ru_nivcsw = p->signal->cnivcsw;
1906 r->ru_minflt = p->signal->cmin_flt;
1907 r->ru_majflt = p->signal->cmaj_flt;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001908
1909 if (who == RUSAGE_CHILDREN)
1910 break;
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 case RUSAGE_SELF:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 utime = cputime_add(utime, p->signal->utime);
1914 stime = cputime_add(stime, p->signal->stime);
1915 r->ru_nvcsw += p->signal->nvcsw;
1916 r->ru_nivcsw += p->signal->nivcsw;
1917 r->ru_minflt += p->signal->min_flt;
1918 r->ru_majflt += p->signal->maj_flt;
1919 t = p;
1920 do {
1921 utime = cputime_add(utime, t->utime);
1922 stime = cputime_add(stime, t->stime);
1923 r->ru_nvcsw += t->nvcsw;
1924 r->ru_nivcsw += t->nivcsw;
1925 r->ru_minflt += t->min_flt;
1926 r->ru_majflt += t->maj_flt;
1927 t = next_thread(t);
1928 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 default:
1932 BUG();
1933 }
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001934
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001935 unlock_task_sighand(p, &flags);
1936 rcu_read_unlock();
1937
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001938 cputime_to_timeval(utime, &r->ru_utime);
1939 cputime_to_timeval(stime, &r->ru_stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
1942int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1943{
1944 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1947}
1948
1949asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1950{
1951 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1952 return -EINVAL;
1953 return getrusage(current, who, ru);
1954}
1955
1956asmlinkage long sys_umask(int mask)
1957{
1958 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1959 return mask;
1960}
1961
1962asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1963 unsigned long arg4, unsigned long arg5)
1964{
1965 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1968 if (error)
1969 return error;
1970
1971 switch (option) {
1972 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07001973 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 error = -EINVAL;
1975 break;
1976 }
Jesper Juhl0730ded2005-09-06 15:17:37 -07001977 current->pdeath_signal = arg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 break;
1979 case PR_GET_PDEATHSIG:
1980 error = put_user(current->pdeath_signal, (int __user *)arg2);
1981 break;
1982 case PR_GET_DUMPABLE:
Michael Kerrisk2030c0f2005-09-16 19:28:02 -07001983 error = current->mm->dumpable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 break;
1985 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02001986 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 error = -EINVAL;
1988 break;
1989 }
1990 current->mm->dumpable = arg2;
1991 break;
1992
1993 case PR_SET_UNALIGN:
1994 error = SET_UNALIGN_CTL(current, arg2);
1995 break;
1996 case PR_GET_UNALIGN:
1997 error = GET_UNALIGN_CTL(current, arg2);
1998 break;
1999 case PR_SET_FPEMU:
2000 error = SET_FPEMU_CTL(current, arg2);
2001 break;
2002 case PR_GET_FPEMU:
2003 error = GET_FPEMU_CTL(current, arg2);
2004 break;
2005 case PR_SET_FPEXC:
2006 error = SET_FPEXC_CTL(current, arg2);
2007 break;
2008 case PR_GET_FPEXC:
2009 error = GET_FPEXC_CTL(current, arg2);
2010 break;
2011 case PR_GET_TIMING:
2012 error = PR_TIMING_STATISTICAL;
2013 break;
2014 case PR_SET_TIMING:
2015 if (arg2 == PR_TIMING_STATISTICAL)
2016 error = 0;
2017 else
2018 error = -EINVAL;
2019 break;
2020
2021 case PR_GET_KEEPCAPS:
2022 if (current->keep_capabilities)
2023 error = 1;
2024 break;
2025 case PR_SET_KEEPCAPS:
2026 if (arg2 != 0 && arg2 != 1) {
2027 error = -EINVAL;
2028 break;
2029 }
2030 current->keep_capabilities = arg2;
2031 break;
2032 case PR_SET_NAME: {
2033 struct task_struct *me = current;
2034 unsigned char ncomm[sizeof(me->comm)];
2035
2036 ncomm[sizeof(me->comm)-1] = 0;
2037 if (strncpy_from_user(ncomm, (char __user *)arg2,
2038 sizeof(me->comm)-1) < 0)
2039 return -EFAULT;
2040 set_task_comm(me, ncomm);
2041 return 0;
2042 }
2043 case PR_GET_NAME: {
2044 struct task_struct *me = current;
2045 unsigned char tcomm[sizeof(me->comm)];
2046
2047 get_task_comm(tcomm, me);
2048 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2049 return -EFAULT;
2050 return 0;
2051 }
Anton Blanchard651d7652006-06-07 16:10:19 +10002052 case PR_GET_ENDIAN:
2053 error = GET_ENDIAN(current, arg2);
2054 break;
2055 case PR_SET_ENDIAN:
2056 error = SET_ENDIAN(current, arg2);
2057 break;
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 default:
2060 error = -EINVAL;
2061 break;
2062 }
2063 return error;
2064}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002065
2066asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2067 struct getcpu_cache __user *cache)
2068{
2069 int err = 0;
2070 int cpu = raw_smp_processor_id();
2071 if (cpup)
2072 err |= put_user(cpu, cpup);
2073 if (nodep)
2074 err |= put_user(cpu_to_node(cpu), nodep);
2075 if (cache) {
2076 /*
2077 * The cache is not needed for this implementation,
2078 * but make sure user programs pass something
2079 * valid. vsyscall implementations can instead make
2080 * good use of the cache. Only use t0 and t1 because
2081 * these are available in both 32bit and 64bit ABI (no
2082 * need for a compat_getcpu). 32bit has enough
2083 * padding
2084 */
2085 unsigned long t0, t1;
Andi Kleen34596dc2006-09-30 01:47:55 +02002086 get_user(t0, &cache->blob[0]);
2087 get_user(t1, &cache->blob[1]);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002088 t0++;
2089 t1++;
Andi Kleen34596dc2006-09-30 01:47:55 +02002090 put_user(t0, &cache->blob[0]);
2091 put_user(t1, &cache->blob[1]);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002092 }
2093 return err ? -EFAULT : 0;
2094}