user namespace: add the framework
[safe/jmp/linux-2.6] / kernel / sys.c
1 /*
2  *  linux/kernel/sys.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #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>
15 #include <linux/highuid.h>
16 #include <linux/fs.h>
17 #include <linux/resource.h>
18 #include <linux/kernel.h>
19 #include <linux/kexec.h>
20 #include <linux/workqueue.h>
21 #include <linux/capability.h>
22 #include <linux/device.h>
23 #include <linux/key.h>
24 #include <linux/times.h>
25 #include <linux/posix-timers.h>
26 #include <linux/security.h>
27 #include <linux/dcookies.h>
28 #include <linux/suspend.h>
29 #include <linux/tty.h>
30 #include <linux/signal.h>
31 #include <linux/cn_proc.h>
32 #include <linux/getcpu.h>
33 #include <linux/task_io_accounting_ops.h>
34
35 #include <linux/compat.h>
36 #include <linux/syscalls.h>
37 #include <linux/kprobes.h>
38 #include <linux/user_namespace.h>
39
40 #include <asm/uaccess.h>
41 #include <asm/io.h>
42 #include <asm/unistd.h>
43
44 #ifndef SET_UNALIGN_CTL
45 # define SET_UNALIGN_CTL(a,b)   (-EINVAL)
46 #endif
47 #ifndef GET_UNALIGN_CTL
48 # define GET_UNALIGN_CTL(a,b)   (-EINVAL)
49 #endif
50 #ifndef SET_FPEMU_CTL
51 # define SET_FPEMU_CTL(a,b)     (-EINVAL)
52 #endif
53 #ifndef GET_FPEMU_CTL
54 # define GET_FPEMU_CTL(a,b)     (-EINVAL)
55 #endif
56 #ifndef SET_FPEXC_CTL
57 # define SET_FPEXC_CTL(a,b)     (-EINVAL)
58 #endif
59 #ifndef GET_FPEXC_CTL
60 # define GET_FPEXC_CTL(a,b)     (-EINVAL)
61 #endif
62 #ifndef GET_ENDIAN
63 # define GET_ENDIAN(a,b)        (-EINVAL)
64 #endif
65 #ifndef SET_ENDIAN
66 # define SET_ENDIAN(a,b)        (-EINVAL)
67 #endif
68
69 /*
70  * this is where the system-wide overflow UID and GID are defined, for
71  * architectures that now have 32-bit UID/GID but didn't in the past
72  */
73
74 int overflowuid = DEFAULT_OVERFLOWUID;
75 int overflowgid = DEFAULT_OVERFLOWGID;
76
77 #ifdef CONFIG_UID16
78 EXPORT_SYMBOL(overflowuid);
79 EXPORT_SYMBOL(overflowgid);
80 #endif
81
82 /*
83  * the same as above, but for filesystems which can only store a 16-bit
84  * UID and GID. as such, this is needed on all architectures
85  */
86
87 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
88 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
89
90 EXPORT_SYMBOL(fs_overflowuid);
91 EXPORT_SYMBOL(fs_overflowgid);
92
93 /*
94  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
95  */
96
97 int C_A_D = 1;
98 struct pid *cad_pid;
99 EXPORT_SYMBOL(cad_pid);
100
101 /*
102  *      Notifier list for kernel code which wants to be called
103  *      at shutdown. This is used to stop any idling DMA operations
104  *      and the like. 
105  */
106
107 static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
108
109 /*
110  *      Notifier chain core routines.  The exported routines below
111  *      are layered on top of these, with appropriate locking added.
112  */
113
114 static int notifier_chain_register(struct notifier_block **nl,
115                 struct notifier_block *n)
116 {
117         while ((*nl) != NULL) {
118                 if (n->priority > (*nl)->priority)
119                         break;
120                 nl = &((*nl)->next);
121         }
122         n->next = *nl;
123         rcu_assign_pointer(*nl, n);
124         return 0;
125 }
126
127 static int notifier_chain_unregister(struct notifier_block **nl,
128                 struct notifier_block *n)
129 {
130         while ((*nl) != NULL) {
131                 if ((*nl) == n) {
132                         rcu_assign_pointer(*nl, n->next);
133                         return 0;
134                 }
135                 nl = &((*nl)->next);
136         }
137         return -ENOENT;
138 }
139
140 /**
141  * notifier_call_chain - Informs the registered notifiers about an event.
142  *      @nl:            Pointer to head of the blocking notifier chain
143  *      @val:           Value passed unmodified to notifier function
144  *      @v:             Pointer passed unmodified to notifier function
145  *      @nr_to_call:    Number of notifier functions to be called. Don't care
146  *                      value of this parameter is -1.
147  *      @nr_calls:      Records the number of notifications sent. Don't care
148  *                      value of this field is NULL.
149  *      @returns:       notifier_call_chain returns the value returned by the
150  *                      last notifier function called.
151  */
152
153 static int __kprobes notifier_call_chain(struct notifier_block **nl,
154                                         unsigned long val, void *v,
155                                         int nr_to_call, int *nr_calls)
156 {
157         int ret = NOTIFY_DONE;
158         struct notifier_block *nb, *next_nb;
159
160         nb = rcu_dereference(*nl);
161
162         while (nb && nr_to_call) {
163                 next_nb = rcu_dereference(nb->next);
164                 ret = nb->notifier_call(nb, val, v);
165
166                 if (nr_calls)
167                         (*nr_calls)++;
168
169                 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
170                         break;
171                 nb = next_nb;
172                 nr_to_call--;
173         }
174         return ret;
175 }
176
177 /*
178  *      Atomic notifier chain routines.  Registration and unregistration
179  *      use a spinlock, and call_chain is synchronized by RCU (no locks).
180  */
181
182 /**
183  *      atomic_notifier_chain_register - Add notifier to an atomic notifier chain
184  *      @nh: Pointer to head of the atomic notifier chain
185  *      @n: New entry in notifier chain
186  *
187  *      Adds a notifier to an atomic notifier chain.
188  *
189  *      Currently always returns zero.
190  */
191
192 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
193                 struct notifier_block *n)
194 {
195         unsigned long flags;
196         int ret;
197
198         spin_lock_irqsave(&nh->lock, flags);
199         ret = notifier_chain_register(&nh->head, n);
200         spin_unlock_irqrestore(&nh->lock, flags);
201         return ret;
202 }
203
204 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
205
206 /**
207  *      atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
208  *      @nh: Pointer to head of the atomic notifier chain
209  *      @n: Entry to remove from notifier chain
210  *
211  *      Removes a notifier from an atomic notifier chain.
212  *
213  *      Returns zero on success or %-ENOENT on failure.
214  */
215 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
216                 struct notifier_block *n)
217 {
218         unsigned long flags;
219         int ret;
220
221         spin_lock_irqsave(&nh->lock, flags);
222         ret = notifier_chain_unregister(&nh->head, n);
223         spin_unlock_irqrestore(&nh->lock, flags);
224         synchronize_rcu();
225         return ret;
226 }
227
228 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
229
230 /**
231  *      __atomic_notifier_call_chain - Call functions in an atomic notifier chain
232  *      @nh: Pointer to head of the atomic notifier chain
233  *      @val: Value passed unmodified to notifier function
234  *      @v: Pointer passed unmodified to notifier function
235  *      @nr_to_call: See the comment for notifier_call_chain.
236  *      @nr_calls: See the comment for notifier_call_chain.
237  *
238  *      Calls each function in a notifier chain in turn.  The functions
239  *      run in an atomic context, so they must not block.
240  *      This routine uses RCU to synchronize with changes to the chain.
241  *
242  *      If the return value of the notifier can be and'ed
243  *      with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
244  *      will return immediately, with the return value of
245  *      the notifier function which halted execution.
246  *      Otherwise the return value is the return value
247  *      of the last notifier function called.
248  */
249  
250 int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
251                                         unsigned long val, void *v,
252                                         int nr_to_call, int *nr_calls)
253 {
254         int ret;
255
256         rcu_read_lock();
257         ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
258         rcu_read_unlock();
259         return ret;
260 }
261
262 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
263
264 int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
265                 unsigned long val, void *v)
266 {
267         return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
268 }
269
270 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
271 /*
272  *      Blocking notifier chain routines.  All access to the chain is
273  *      synchronized by an rwsem.
274  */
275
276 /**
277  *      blocking_notifier_chain_register - Add notifier to a blocking notifier chain
278  *      @nh: Pointer to head of the blocking notifier chain
279  *      @n: New entry in notifier chain
280  *
281  *      Adds a notifier to a blocking notifier chain.
282  *      Must be called in process context.
283  *
284  *      Currently always returns zero.
285  */
286  
287 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
288                 struct notifier_block *n)
289 {
290         int ret;
291
292         /*
293          * This code gets used during boot-up, when task switching is
294          * not yet working and interrupts must remain disabled.  At
295          * such times we must not call down_write().
296          */
297         if (unlikely(system_state == SYSTEM_BOOTING))
298                 return notifier_chain_register(&nh->head, n);
299
300         down_write(&nh->rwsem);
301         ret = notifier_chain_register(&nh->head, n);
302         up_write(&nh->rwsem);
303         return ret;
304 }
305
306 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
307
308 /**
309  *      blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
310  *      @nh: Pointer to head of the blocking notifier chain
311  *      @n: Entry to remove from notifier chain
312  *
313  *      Removes a notifier from a blocking notifier chain.
314  *      Must be called from process context.
315  *
316  *      Returns zero on success or %-ENOENT on failure.
317  */
318 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
319                 struct notifier_block *n)
320 {
321         int ret;
322
323         /*
324          * This code gets used during boot-up, when task switching is
325          * not yet working and interrupts must remain disabled.  At
326          * such times we must not call down_write().
327          */
328         if (unlikely(system_state == SYSTEM_BOOTING))
329                 return notifier_chain_unregister(&nh->head, n);
330
331         down_write(&nh->rwsem);
332         ret = notifier_chain_unregister(&nh->head, n);
333         up_write(&nh->rwsem);
334         return ret;
335 }
336
337 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
338
339 /**
340  *      __blocking_notifier_call_chain - Call functions in a blocking notifier chain
341  *      @nh: Pointer to head of the blocking notifier chain
342  *      @val: Value passed unmodified to notifier function
343  *      @v: Pointer passed unmodified to notifier function
344  *      @nr_to_call: See comment for notifier_call_chain.
345  *      @nr_calls: See comment for notifier_call_chain.
346  *
347  *      Calls each function in a notifier chain in turn.  The functions
348  *      run in a process context, so they are allowed to block.
349  *
350  *      If the return value of the notifier can be and'ed
351  *      with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
352  *      will return immediately, with the return value of
353  *      the notifier function which halted execution.
354  *      Otherwise the return value is the return value
355  *      of the last notifier function called.
356  */
357  
358 int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
359                                    unsigned long val, void *v,
360                                    int nr_to_call, int *nr_calls)
361 {
362         int ret = NOTIFY_DONE;
363
364         /*
365          * We check the head outside the lock, but if this access is
366          * racy then it does not matter what the result of the test
367          * is, we re-check the list after having taken the lock anyway:
368          */
369         if (rcu_dereference(nh->head)) {
370                 down_read(&nh->rwsem);
371                 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
372                                         nr_calls);
373                 up_read(&nh->rwsem);
374         }
375         return ret;
376 }
377 EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
378
379 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
380                 unsigned long val, void *v)
381 {
382         return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
383 }
384 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
385
386 /*
387  *      Raw notifier chain routines.  There is no protection;
388  *      the caller must provide it.  Use at your own risk!
389  */
390
391 /**
392  *      raw_notifier_chain_register - Add notifier to a raw notifier chain
393  *      @nh: Pointer to head of the raw notifier chain
394  *      @n: New entry in notifier chain
395  *
396  *      Adds a notifier to a raw notifier chain.
397  *      All locking must be provided by the caller.
398  *
399  *      Currently always returns zero.
400  */
401
402 int raw_notifier_chain_register(struct raw_notifier_head *nh,
403                 struct notifier_block *n)
404 {
405         return notifier_chain_register(&nh->head, n);
406 }
407
408 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
409
410 /**
411  *      raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
412  *      @nh: Pointer to head of the raw notifier chain
413  *      @n: Entry to remove from notifier chain
414  *
415  *      Removes a notifier from a raw notifier chain.
416  *      All locking must be provided by the caller.
417  *
418  *      Returns zero on success or %-ENOENT on failure.
419  */
420 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
421                 struct notifier_block *n)
422 {
423         return notifier_chain_unregister(&nh->head, n);
424 }
425
426 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
427
428 /**
429  *      __raw_notifier_call_chain - Call functions in a raw notifier chain
430  *      @nh: Pointer to head of the raw notifier chain
431  *      @val: Value passed unmodified to notifier function
432  *      @v: Pointer passed unmodified to notifier function
433  *      @nr_to_call: See comment for notifier_call_chain.
434  *      @nr_calls: See comment for notifier_call_chain
435  *
436  *      Calls each function in a notifier chain in turn.  The functions
437  *      run in an undefined context.
438  *      All locking must be provided by the caller.
439  *
440  *      If the return value of the notifier can be and'ed
441  *      with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
442  *      will return immediately, with the return value of
443  *      the notifier function which halted execution.
444  *      Otherwise the return value is the return value
445  *      of the last notifier function called.
446  */
447
448 int __raw_notifier_call_chain(struct raw_notifier_head *nh,
449                               unsigned long val, void *v,
450                               int nr_to_call, int *nr_calls)
451 {
452         return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
453 }
454
455 EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
456
457 int raw_notifier_call_chain(struct raw_notifier_head *nh,
458                 unsigned long val, void *v)
459 {
460         return __raw_notifier_call_chain(nh, val, v, -1, NULL);
461 }
462
463 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
464
465 /*
466  *      SRCU notifier chain routines.    Registration and unregistration
467  *      use a mutex, and call_chain is synchronized by SRCU (no locks).
468  */
469
470 /**
471  *      srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
472  *      @nh: Pointer to head of the SRCU notifier chain
473  *      @n: New entry in notifier chain
474  *
475  *      Adds a notifier to an SRCU notifier chain.
476  *      Must be called in process context.
477  *
478  *      Currently always returns zero.
479  */
480
481 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
482                 struct notifier_block *n)
483 {
484         int ret;
485
486         /*
487          * This code gets used during boot-up, when task switching is
488          * not yet working and interrupts must remain disabled.  At
489          * such times we must not call mutex_lock().
490          */
491         if (unlikely(system_state == SYSTEM_BOOTING))
492                 return notifier_chain_register(&nh->head, n);
493
494         mutex_lock(&nh->mutex);
495         ret = notifier_chain_register(&nh->head, n);
496         mutex_unlock(&nh->mutex);
497         return ret;
498 }
499
500 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
501
502 /**
503  *      srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
504  *      @nh: Pointer to head of the SRCU notifier chain
505  *      @n: Entry to remove from notifier chain
506  *
507  *      Removes a notifier from an SRCU notifier chain.
508  *      Must be called from process context.
509  *
510  *      Returns zero on success or %-ENOENT on failure.
511  */
512 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
513                 struct notifier_block *n)
514 {
515         int ret;
516
517         /*
518          * This code gets used during boot-up, when task switching is
519          * not yet working and interrupts must remain disabled.  At
520          * such times we must not call mutex_lock().
521          */
522         if (unlikely(system_state == SYSTEM_BOOTING))
523                 return notifier_chain_unregister(&nh->head, n);
524
525         mutex_lock(&nh->mutex);
526         ret = notifier_chain_unregister(&nh->head, n);
527         mutex_unlock(&nh->mutex);
528         synchronize_srcu(&nh->srcu);
529         return ret;
530 }
531
532 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
533
534 /**
535  *      __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
536  *      @nh: Pointer to head of the SRCU notifier chain
537  *      @val: Value passed unmodified to notifier function
538  *      @v: Pointer passed unmodified to notifier function
539  *      @nr_to_call: See comment for notifier_call_chain.
540  *      @nr_calls: See comment for notifier_call_chain
541  *
542  *      Calls each function in a notifier chain in turn.  The functions
543  *      run in a process context, so they are allowed to block.
544  *
545  *      If the return value of the notifier can be and'ed
546  *      with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
547  *      will return immediately, with the return value of
548  *      the notifier function which halted execution.
549  *      Otherwise the return value is the return value
550  *      of the last notifier function called.
551  */
552
553 int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
554                                unsigned long val, void *v,
555                                int nr_to_call, int *nr_calls)
556 {
557         int ret;
558         int idx;
559
560         idx = srcu_read_lock(&nh->srcu);
561         ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
562         srcu_read_unlock(&nh->srcu, idx);
563         return ret;
564 }
565 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
566
567 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
568                 unsigned long val, void *v)
569 {
570         return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
571 }
572 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
573
574 /**
575  *      srcu_init_notifier_head - Initialize an SRCU notifier head
576  *      @nh: Pointer to head of the srcu notifier chain
577  *
578  *      Unlike other sorts of notifier heads, SRCU notifier heads require
579  *      dynamic initialization.  Be sure to call this routine before
580  *      calling any of the other SRCU notifier routines for this head.
581  *
582  *      If an SRCU notifier head is deallocated, it must first be cleaned
583  *      up by calling srcu_cleanup_notifier_head().  Otherwise the head's
584  *      per-cpu data (used by the SRCU mechanism) will leak.
585  */
586
587 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
588 {
589         mutex_init(&nh->mutex);
590         if (init_srcu_struct(&nh->srcu) < 0)
591                 BUG();
592         nh->head = NULL;
593 }
594
595 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
596
597 /**
598  *      register_reboot_notifier - Register function to be called at reboot time
599  *      @nb: Info about notifier function to be called
600  *
601  *      Registers a function with the list of functions
602  *      to be called at reboot time.
603  *
604  *      Currently always returns zero, as blocking_notifier_chain_register()
605  *      always returns zero.
606  */
607  
608 int register_reboot_notifier(struct notifier_block * nb)
609 {
610         return blocking_notifier_chain_register(&reboot_notifier_list, nb);
611 }
612
613 EXPORT_SYMBOL(register_reboot_notifier);
614
615 /**
616  *      unregister_reboot_notifier - Unregister previously registered reboot notifier
617  *      @nb: Hook to be unregistered
618  *
619  *      Unregisters a previously registered reboot
620  *      notifier function.
621  *
622  *      Returns zero on success, or %-ENOENT on failure.
623  */
624  
625 int unregister_reboot_notifier(struct notifier_block * nb)
626 {
627         return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
628 }
629
630 EXPORT_SYMBOL(unregister_reboot_notifier);
631
632 static int set_one_prio(struct task_struct *p, int niceval, int error)
633 {
634         int no_nice;
635
636         if (p->uid != current->euid &&
637                 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
638                 error = -EPERM;
639                 goto out;
640         }
641         if (niceval < task_nice(p) && !can_nice(p, niceval)) {
642                 error = -EACCES;
643                 goto out;
644         }
645         no_nice = security_task_setnice(p, niceval);
646         if (no_nice) {
647                 error = no_nice;
648                 goto out;
649         }
650         if (error == -ESRCH)
651                 error = 0;
652         set_user_nice(p, niceval);
653 out:
654         return error;
655 }
656
657 asmlinkage long sys_setpriority(int which, int who, int niceval)
658 {
659         struct task_struct *g, *p;
660         struct user_struct *user;
661         int error = -EINVAL;
662         struct pid *pgrp;
663
664         if (which > PRIO_USER || which < PRIO_PROCESS)
665                 goto out;
666
667         /* normalize: avoid signed division (rounding problems) */
668         error = -ESRCH;
669         if (niceval < -20)
670                 niceval = -20;
671         if (niceval > 19)
672                 niceval = 19;
673
674         read_lock(&tasklist_lock);
675         switch (which) {
676                 case PRIO_PROCESS:
677                         if (who)
678                                 p = find_task_by_pid(who);
679                         else
680                                 p = current;
681                         if (p)
682                                 error = set_one_prio(p, niceval, error);
683                         break;
684                 case PRIO_PGRP:
685                         if (who)
686                                 pgrp = find_pid(who);
687                         else
688                                 pgrp = task_pgrp(current);
689                         do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
690                                 error = set_one_prio(p, niceval, error);
691                         } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
692                         break;
693                 case PRIO_USER:
694                         user = current->user;
695                         if (!who)
696                                 who = current->uid;
697                         else
698                                 if ((who != current->uid) && !(user = find_user(who)))
699                                         goto out_unlock;        /* No processes for this user */
700
701                         do_each_thread(g, p)
702                                 if (p->uid == who)
703                                         error = set_one_prio(p, niceval, error);
704                         while_each_thread(g, p);
705                         if (who != current->uid)
706                                 free_uid(user);         /* For find_user() */
707                         break;
708         }
709 out_unlock:
710         read_unlock(&tasklist_lock);
711 out:
712         return error;
713 }
714
715 /*
716  * Ugh. To avoid negative return values, "getpriority()" will
717  * not return the normal nice-value, but a negated value that
718  * has been offset by 20 (ie it returns 40..1 instead of -20..19)
719  * to stay compatible.
720  */
721 asmlinkage long sys_getpriority(int which, int who)
722 {
723         struct task_struct *g, *p;
724         struct user_struct *user;
725         long niceval, retval = -ESRCH;
726         struct pid *pgrp;
727
728         if (which > PRIO_USER || which < PRIO_PROCESS)
729                 return -EINVAL;
730
731         read_lock(&tasklist_lock);
732         switch (which) {
733                 case PRIO_PROCESS:
734                         if (who)
735                                 p = find_task_by_pid(who);
736                         else
737                                 p = current;
738                         if (p) {
739                                 niceval = 20 - task_nice(p);
740                                 if (niceval > retval)
741                                         retval = niceval;
742                         }
743                         break;
744                 case PRIO_PGRP:
745                         if (who)
746                                 pgrp = find_pid(who);
747                         else
748                                 pgrp = task_pgrp(current);
749                         do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
750                                 niceval = 20 - task_nice(p);
751                                 if (niceval > retval)
752                                         retval = niceval;
753                         } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
754                         break;
755                 case PRIO_USER:
756                         user = current->user;
757                         if (!who)
758                                 who = current->uid;
759                         else
760                                 if ((who != current->uid) && !(user = find_user(who)))
761                                         goto out_unlock;        /* No processes for this user */
762
763                         do_each_thread(g, p)
764                                 if (p->uid == who) {
765                                         niceval = 20 - task_nice(p);
766                                         if (niceval > retval)
767                                                 retval = niceval;
768                                 }
769                         while_each_thread(g, p);
770                         if (who != current->uid)
771                                 free_uid(user);         /* for find_user() */
772                         break;
773         }
774 out_unlock:
775         read_unlock(&tasklist_lock);
776
777         return retval;
778 }
779
780 /**
781  *      emergency_restart - reboot the system
782  *
783  *      Without shutting down any hardware or taking any locks
784  *      reboot the system.  This is called when we know we are in
785  *      trouble so this is our best effort to reboot.  This is
786  *      safe to call in interrupt context.
787  */
788 void emergency_restart(void)
789 {
790         machine_emergency_restart();
791 }
792 EXPORT_SYMBOL_GPL(emergency_restart);
793
794 static void kernel_restart_prepare(char *cmd)
795 {
796         blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
797         system_state = SYSTEM_RESTART;
798         device_shutdown();
799 }
800
801 /**
802  *      kernel_restart - reboot the system
803  *      @cmd: pointer to buffer containing command to execute for restart
804  *              or %NULL
805  *
806  *      Shutdown everything and perform a clean reboot.
807  *      This is not safe to call in interrupt context.
808  */
809 void kernel_restart(char *cmd)
810 {
811         kernel_restart_prepare(cmd);
812         if (!cmd)
813                 printk(KERN_EMERG "Restarting system.\n");
814         else
815                 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
816         machine_restart(cmd);
817 }
818 EXPORT_SYMBOL_GPL(kernel_restart);
819
820 /**
821  *      kernel_kexec - reboot the system
822  *
823  *      Move into place and start executing a preloaded standalone
824  *      executable.  If nothing was preloaded return an error.
825  */
826 static void kernel_kexec(void)
827 {
828 #ifdef CONFIG_KEXEC
829         struct kimage *image;
830         image = xchg(&kexec_image, NULL);
831         if (!image)
832                 return;
833         kernel_restart_prepare(NULL);
834         printk(KERN_EMERG "Starting new kernel\n");
835         machine_shutdown();
836         machine_kexec(image);
837 #endif
838 }
839
840 void kernel_shutdown_prepare(enum system_states state)
841 {
842         blocking_notifier_call_chain(&reboot_notifier_list,
843                 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
844         system_state = state;
845         device_shutdown();
846 }
847 /**
848  *      kernel_halt - halt the system
849  *
850  *      Shutdown everything and perform a clean system halt.
851  */
852 void kernel_halt(void)
853 {
854         kernel_shutdown_prepare(SYSTEM_HALT);
855         printk(KERN_EMERG "System halted.\n");
856         machine_halt();
857 }
858
859 EXPORT_SYMBOL_GPL(kernel_halt);
860
861 /**
862  *      kernel_power_off - power_off the system
863  *
864  *      Shutdown everything and perform a clean system power_off.
865  */
866 void kernel_power_off(void)
867 {
868         kernel_shutdown_prepare(SYSTEM_POWER_OFF);
869         printk(KERN_EMERG "Power down.\n");
870         machine_power_off();
871 }
872 EXPORT_SYMBOL_GPL(kernel_power_off);
873 /*
874  * Reboot system call: for obvious reasons only root may call it,
875  * and even root needs to set up some magic numbers in the registers
876  * so that some mistake won't make this reboot the whole machine.
877  * You can also set the meaning of the ctrl-alt-del-key here.
878  *
879  * reboot doesn't sync: do that yourself before calling this.
880  */
881 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
882 {
883         char buffer[256];
884
885         /* We only trust the superuser with rebooting the system. */
886         if (!capable(CAP_SYS_BOOT))
887                 return -EPERM;
888
889         /* For safety, we require "magic" arguments. */
890         if (magic1 != LINUX_REBOOT_MAGIC1 ||
891             (magic2 != LINUX_REBOOT_MAGIC2 &&
892                         magic2 != LINUX_REBOOT_MAGIC2A &&
893                         magic2 != LINUX_REBOOT_MAGIC2B &&
894                         magic2 != LINUX_REBOOT_MAGIC2C))
895                 return -EINVAL;
896
897         /* Instead of trying to make the power_off code look like
898          * halt when pm_power_off is not set do it the easy way.
899          */
900         if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
901                 cmd = LINUX_REBOOT_CMD_HALT;
902
903         lock_kernel();
904         switch (cmd) {
905         case LINUX_REBOOT_CMD_RESTART:
906                 kernel_restart(NULL);
907                 break;
908
909         case LINUX_REBOOT_CMD_CAD_ON:
910                 C_A_D = 1;
911                 break;
912
913         case LINUX_REBOOT_CMD_CAD_OFF:
914                 C_A_D = 0;
915                 break;
916
917         case LINUX_REBOOT_CMD_HALT:
918                 kernel_halt();
919                 unlock_kernel();
920                 do_exit(0);
921                 break;
922
923         case LINUX_REBOOT_CMD_POWER_OFF:
924                 kernel_power_off();
925                 unlock_kernel();
926                 do_exit(0);
927                 break;
928
929         case LINUX_REBOOT_CMD_RESTART2:
930                 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
931                         unlock_kernel();
932                         return -EFAULT;
933                 }
934                 buffer[sizeof(buffer) - 1] = '\0';
935
936                 kernel_restart(buffer);
937                 break;
938
939         case LINUX_REBOOT_CMD_KEXEC:
940                 kernel_kexec();
941                 unlock_kernel();
942                 return -EINVAL;
943
944 #ifdef CONFIG_SOFTWARE_SUSPEND
945         case LINUX_REBOOT_CMD_SW_SUSPEND:
946                 {
947                         int ret = hibernate();
948                         unlock_kernel();
949                         return ret;
950                 }
951 #endif
952
953         default:
954                 unlock_kernel();
955                 return -EINVAL;
956         }
957         unlock_kernel();
958         return 0;
959 }
960
961 static void deferred_cad(struct work_struct *dummy)
962 {
963         kernel_restart(NULL);
964 }
965
966 /*
967  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
968  * As it's called within an interrupt, it may NOT sync: the only choice
969  * is whether to reboot at once, or just ignore the ctrl-alt-del.
970  */
971 void ctrl_alt_del(void)
972 {
973         static DECLARE_WORK(cad_work, deferred_cad);
974
975         if (C_A_D)
976                 schedule_work(&cad_work);
977         else
978                 kill_cad_pid(SIGINT, 1);
979 }
980         
981 /*
982  * Unprivileged users may change the real gid to the effective gid
983  * or vice versa.  (BSD-style)
984  *
985  * If you set the real gid at all, or set the effective gid to a value not
986  * equal to the real gid, then the saved gid is set to the new effective gid.
987  *
988  * This makes it possible for a setgid program to completely drop its
989  * privileges, which is often a useful assertion to make when you are doing
990  * a security audit over a program.
991  *
992  * The general idea is that a program which uses just setregid() will be
993  * 100% compatible with BSD.  A program which uses just setgid() will be
994  * 100% compatible with POSIX with saved IDs. 
995  *
996  * SMP: There are not races, the GIDs are checked only by filesystem
997  *      operations (as far as semantic preservation is concerned).
998  */
999 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
1000 {
1001         int old_rgid = current->gid;
1002         int old_egid = current->egid;
1003         int new_rgid = old_rgid;
1004         int new_egid = old_egid;
1005         int retval;
1006
1007         retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
1008         if (retval)
1009                 return retval;
1010
1011         if (rgid != (gid_t) -1) {
1012                 if ((old_rgid == rgid) ||
1013                     (current->egid==rgid) ||
1014                     capable(CAP_SETGID))
1015                         new_rgid = rgid;
1016                 else
1017                         return -EPERM;
1018         }
1019         if (egid != (gid_t) -1) {
1020                 if ((old_rgid == egid) ||
1021                     (current->egid == egid) ||
1022                     (current->sgid == egid) ||
1023                     capable(CAP_SETGID))
1024                         new_egid = egid;
1025                 else
1026                         return -EPERM;
1027         }
1028         if (new_egid != old_egid) {
1029                 current->mm->dumpable = suid_dumpable;
1030                 smp_wmb();
1031         }
1032         if (rgid != (gid_t) -1 ||
1033             (egid != (gid_t) -1 && egid != old_rgid))
1034                 current->sgid = new_egid;
1035         current->fsgid = new_egid;
1036         current->egid = new_egid;
1037         current->gid = new_rgid;
1038         key_fsgid_changed(current);
1039         proc_id_connector(current, PROC_EVENT_GID);
1040         return 0;
1041 }
1042
1043 /*
1044  * setgid() is implemented like SysV w/ SAVED_IDS 
1045  *
1046  * SMP: Same implicit races as above.
1047  */
1048 asmlinkage long sys_setgid(gid_t gid)
1049 {
1050         int old_egid = current->egid;
1051         int retval;
1052
1053         retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
1054         if (retval)
1055                 return retval;
1056
1057         if (capable(CAP_SETGID)) {
1058                 if (old_egid != gid) {
1059                         current->mm->dumpable = suid_dumpable;
1060                         smp_wmb();
1061                 }
1062                 current->gid = current->egid = current->sgid = current->fsgid = gid;
1063         } else if ((gid == current->gid) || (gid == current->sgid)) {
1064                 if (old_egid != gid) {
1065                         current->mm->dumpable = suid_dumpable;
1066                         smp_wmb();
1067                 }
1068                 current->egid = current->fsgid = gid;
1069         }
1070         else
1071                 return -EPERM;
1072
1073         key_fsgid_changed(current);
1074         proc_id_connector(current, PROC_EVENT_GID);
1075         return 0;
1076 }
1077   
1078 static int set_user(uid_t new_ruid, int dumpclear)
1079 {
1080         struct user_struct *new_user;
1081
1082         new_user = alloc_uid(current->nsproxy->user_ns, new_ruid);
1083         if (!new_user)
1084                 return -EAGAIN;
1085
1086         if (atomic_read(&new_user->processes) >=
1087                                 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
1088                         new_user != current->nsproxy->user_ns->root_user) {
1089                 free_uid(new_user);
1090                 return -EAGAIN;
1091         }
1092
1093         switch_uid(new_user);
1094
1095         if (dumpclear) {
1096                 current->mm->dumpable = suid_dumpable;
1097                 smp_wmb();
1098         }
1099         current->uid = new_ruid;
1100         return 0;
1101 }
1102
1103 /*
1104  * Unprivileged users may change the real uid to the effective uid
1105  * or vice versa.  (BSD-style)
1106  *
1107  * If you set the real uid at all, or set the effective uid to a value not
1108  * equal to the real uid, then the saved uid is set to the new effective uid.
1109  *
1110  * This makes it possible for a setuid program to completely drop its
1111  * privileges, which is often a useful assertion to make when you are doing
1112  * a security audit over a program.
1113  *
1114  * The general idea is that a program which uses just setreuid() will be
1115  * 100% compatible with BSD.  A program which uses just setuid() will be
1116  * 100% compatible with POSIX with saved IDs. 
1117  */
1118 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
1119 {
1120         int old_ruid, old_euid, old_suid, new_ruid, new_euid;
1121         int retval;
1122
1123         retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
1124         if (retval)
1125                 return retval;
1126
1127         new_ruid = old_ruid = current->uid;
1128         new_euid = old_euid = current->euid;
1129         old_suid = current->suid;
1130
1131         if (ruid != (uid_t) -1) {
1132                 new_ruid = ruid;
1133                 if ((old_ruid != ruid) &&
1134                     (current->euid != ruid) &&
1135                     !capable(CAP_SETUID))
1136                         return -EPERM;
1137         }
1138
1139         if (euid != (uid_t) -1) {
1140                 new_euid = euid;
1141                 if ((old_ruid != euid) &&
1142                     (current->euid != euid) &&
1143                     (current->suid != euid) &&
1144                     !capable(CAP_SETUID))
1145                         return -EPERM;
1146         }
1147
1148         if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
1149                 return -EAGAIN;
1150
1151         if (new_euid != old_euid) {
1152                 current->mm->dumpable = suid_dumpable;
1153                 smp_wmb();
1154         }
1155         current->fsuid = current->euid = new_euid;
1156         if (ruid != (uid_t) -1 ||
1157             (euid != (uid_t) -1 && euid != old_ruid))
1158                 current->suid = current->euid;
1159         current->fsuid = current->euid;
1160
1161         key_fsuid_changed(current);
1162         proc_id_connector(current, PROC_EVENT_UID);
1163
1164         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
1165 }
1166
1167
1168                 
1169 /*
1170  * setuid() is implemented like SysV with SAVED_IDS 
1171  * 
1172  * Note that SAVED_ID's is deficient in that a setuid root program
1173  * like sendmail, for example, cannot set its uid to be a normal 
1174  * user and then switch back, because if you're root, setuid() sets
1175  * the saved uid too.  If you don't like this, blame the bright people
1176  * in the POSIX committee and/or USG.  Note that the BSD-style setreuid()
1177  * will allow a root program to temporarily drop privileges and be able to
1178  * regain them by swapping the real and effective uid.  
1179  */
1180 asmlinkage long sys_setuid(uid_t uid)
1181 {
1182         int old_euid = current->euid;
1183         int old_ruid, old_suid, new_suid;
1184         int retval;
1185
1186         retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
1187         if (retval)
1188                 return retval;
1189
1190         old_ruid = current->uid;
1191         old_suid = current->suid;
1192         new_suid = old_suid;
1193         
1194         if (capable(CAP_SETUID)) {
1195                 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1196                         return -EAGAIN;
1197                 new_suid = uid;
1198         } else if ((uid != current->uid) && (uid != new_suid))
1199                 return -EPERM;
1200
1201         if (old_euid != uid) {
1202                 current->mm->dumpable = suid_dumpable;
1203                 smp_wmb();
1204         }
1205         current->fsuid = current->euid = uid;
1206         current->suid = new_suid;
1207
1208         key_fsuid_changed(current);
1209         proc_id_connector(current, PROC_EVENT_UID);
1210
1211         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1212 }
1213
1214
1215 /*
1216  * This function implements a generic ability to update ruid, euid,
1217  * and suid.  This allows you to implement the 4.4 compatible seteuid().
1218  */
1219 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1220 {
1221         int old_ruid = current->uid;
1222         int old_euid = current->euid;
1223         int old_suid = current->suid;
1224         int retval;
1225
1226         retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1227         if (retval)
1228                 return retval;
1229
1230         if (!capable(CAP_SETUID)) {
1231                 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1232                     (ruid != current->euid) && (ruid != current->suid))
1233                         return -EPERM;
1234                 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1235                     (euid != current->euid) && (euid != current->suid))
1236                         return -EPERM;
1237                 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1238                     (suid != current->euid) && (suid != current->suid))
1239                         return -EPERM;
1240         }
1241         if (ruid != (uid_t) -1) {
1242                 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1243                         return -EAGAIN;
1244         }
1245         if (euid != (uid_t) -1) {
1246                 if (euid != current->euid) {
1247                         current->mm->dumpable = suid_dumpable;
1248                         smp_wmb();
1249                 }
1250                 current->euid = euid;
1251         }
1252         current->fsuid = current->euid;
1253         if (suid != (uid_t) -1)
1254                 current->suid = suid;
1255
1256         key_fsuid_changed(current);
1257         proc_id_connector(current, PROC_EVENT_UID);
1258
1259         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1260 }
1261
1262 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1263 {
1264         int retval;
1265
1266         if (!(retval = put_user(current->uid, ruid)) &&
1267             !(retval = put_user(current->euid, euid)))
1268                 retval = put_user(current->suid, suid);
1269
1270         return retval;
1271 }
1272
1273 /*
1274  * Same as above, but for rgid, egid, sgid.
1275  */
1276 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1277 {
1278         int retval;
1279
1280         retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1281         if (retval)
1282                 return retval;
1283
1284         if (!capable(CAP_SETGID)) {
1285                 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1286                     (rgid != current->egid) && (rgid != current->sgid))
1287                         return -EPERM;
1288                 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1289                     (egid != current->egid) && (egid != current->sgid))
1290                         return -EPERM;
1291                 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1292                     (sgid != current->egid) && (sgid != current->sgid))
1293                         return -EPERM;
1294         }
1295         if (egid != (gid_t) -1) {
1296                 if (egid != current->egid) {
1297                         current->mm->dumpable = suid_dumpable;
1298                         smp_wmb();
1299                 }
1300                 current->egid = egid;
1301         }
1302         current->fsgid = current->egid;
1303         if (rgid != (gid_t) -1)
1304                 current->gid = rgid;
1305         if (sgid != (gid_t) -1)
1306                 current->sgid = sgid;
1307
1308         key_fsgid_changed(current);
1309         proc_id_connector(current, PROC_EVENT_GID);
1310         return 0;
1311 }
1312
1313 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1314 {
1315         int retval;
1316
1317         if (!(retval = put_user(current->gid, rgid)) &&
1318             !(retval = put_user(current->egid, egid)))
1319                 retval = put_user(current->sgid, sgid);
1320
1321         return retval;
1322 }
1323
1324
1325 /*
1326  * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1327  * is used for "access()" and for the NFS daemon (letting nfsd stay at
1328  * whatever uid it wants to). It normally shadows "euid", except when
1329  * explicitly set by setfsuid() or for access..
1330  */
1331 asmlinkage long sys_setfsuid(uid_t uid)
1332 {
1333         int old_fsuid;
1334
1335         old_fsuid = current->fsuid;
1336         if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1337                 return old_fsuid;
1338
1339         if (uid == current->uid || uid == current->euid ||
1340             uid == current->suid || uid == current->fsuid || 
1341             capable(CAP_SETUID)) {
1342                 if (uid != old_fsuid) {
1343                         current->mm->dumpable = suid_dumpable;
1344                         smp_wmb();
1345                 }
1346                 current->fsuid = uid;
1347         }
1348
1349         key_fsuid_changed(current);
1350         proc_id_connector(current, PROC_EVENT_UID);
1351
1352         security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1353
1354         return old_fsuid;
1355 }
1356
1357 /*
1358  * Samma pÃ¥ svenska..
1359  */
1360 asmlinkage long sys_setfsgid(gid_t gid)
1361 {
1362         int old_fsgid;
1363
1364         old_fsgid = current->fsgid;
1365         if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1366                 return old_fsgid;
1367
1368         if (gid == current->gid || gid == current->egid ||
1369             gid == current->sgid || gid == current->fsgid || 
1370             capable(CAP_SETGID)) {
1371                 if (gid != old_fsgid) {
1372                         current->mm->dumpable = suid_dumpable;
1373                         smp_wmb();
1374                 }
1375                 current->fsgid = gid;
1376                 key_fsgid_changed(current);
1377                 proc_id_connector(current, PROC_EVENT_GID);
1378         }
1379         return old_fsgid;
1380 }
1381
1382 asmlinkage long sys_times(struct tms __user * tbuf)
1383 {
1384         /*
1385          *      In the SMP world we might just be unlucky and have one of
1386          *      the times increment as we use it. Since the value is an
1387          *      atomically safe type this is just fine. Conceptually its
1388          *      as if the syscall took an instant longer to occur.
1389          */
1390         if (tbuf) {
1391                 struct tms tmp;
1392                 struct task_struct *tsk = current;
1393                 struct task_struct *t;
1394                 cputime_t utime, stime, cutime, cstime;
1395
1396                 spin_lock_irq(&tsk->sighand->siglock);
1397                 utime = tsk->signal->utime;
1398                 stime = tsk->signal->stime;
1399                 t = tsk;
1400                 do {
1401                         utime = cputime_add(utime, t->utime);
1402                         stime = cputime_add(stime, t->stime);
1403                         t = next_thread(t);
1404                 } while (t != tsk);
1405
1406                 cutime = tsk->signal->cutime;
1407                 cstime = tsk->signal->cstime;
1408                 spin_unlock_irq(&tsk->sighand->siglock);
1409
1410                 tmp.tms_utime = cputime_to_clock_t(utime);
1411                 tmp.tms_stime = cputime_to_clock_t(stime);
1412                 tmp.tms_cutime = cputime_to_clock_t(cutime);
1413                 tmp.tms_cstime = cputime_to_clock_t(cstime);
1414                 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1415                         return -EFAULT;
1416         }
1417         return (long) jiffies_64_to_clock_t(get_jiffies_64());
1418 }
1419
1420 /*
1421  * This needs some heavy checking ...
1422  * I just haven't the stomach for it. I also don't fully
1423  * understand sessions/pgrp etc. Let somebody who does explain it.
1424  *
1425  * OK, I think I have the protection semantics right.... this is really
1426  * only important on a multi-user system anyway, to make sure one user
1427  * can't send a signal to a process owned by another.  -TYT, 12/12/91
1428  *
1429  * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1430  * LBT 04.03.94
1431  */
1432
1433 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1434 {
1435         struct task_struct *p;
1436         struct task_struct *group_leader = current->group_leader;
1437         int err = -EINVAL;
1438
1439         if (!pid)
1440                 pid = group_leader->pid;
1441         if (!pgid)
1442                 pgid = pid;
1443         if (pgid < 0)
1444                 return -EINVAL;
1445
1446         /* From this point forward we keep holding onto the tasklist lock
1447          * so that our parent does not change from under us. -DaveM
1448          */
1449         write_lock_irq(&tasklist_lock);
1450
1451         err = -ESRCH;
1452         p = find_task_by_pid(pid);
1453         if (!p)
1454                 goto out;
1455
1456         err = -EINVAL;
1457         if (!thread_group_leader(p))
1458                 goto out;
1459
1460         if (p->real_parent == group_leader) {
1461                 err = -EPERM;
1462                 if (task_session(p) != task_session(group_leader))
1463                         goto out;
1464                 err = -EACCES;
1465                 if (p->did_exec)
1466                         goto out;
1467         } else {
1468                 err = -ESRCH;
1469                 if (p != group_leader)
1470                         goto out;
1471         }
1472
1473         err = -EPERM;
1474         if (p->signal->leader)
1475                 goto out;
1476
1477         if (pgid != pid) {
1478                 struct task_struct *g =
1479                         find_task_by_pid_type(PIDTYPE_PGID, pgid);
1480
1481                 if (!g || task_session(g) != task_session(group_leader))
1482                         goto out;
1483         }
1484
1485         err = security_task_setpgid(p, pgid);
1486         if (err)
1487                 goto out;
1488
1489         if (process_group(p) != pgid) {
1490                 detach_pid(p, PIDTYPE_PGID);
1491                 p->signal->pgrp = pgid;
1492                 attach_pid(p, PIDTYPE_PGID, find_pid(pgid));
1493         }
1494
1495         err = 0;
1496 out:
1497         /* All paths lead to here, thus we are safe. -DaveM */
1498         write_unlock_irq(&tasklist_lock);
1499         return err;
1500 }
1501
1502 asmlinkage long sys_getpgid(pid_t pid)
1503 {
1504         if (!pid)
1505                 return process_group(current);
1506         else {
1507                 int retval;
1508                 struct task_struct *p;
1509
1510                 read_lock(&tasklist_lock);
1511                 p = find_task_by_pid(pid);
1512
1513                 retval = -ESRCH;
1514                 if (p) {
1515                         retval = security_task_getpgid(p);
1516                         if (!retval)
1517                                 retval = process_group(p);
1518                 }
1519                 read_unlock(&tasklist_lock);
1520                 return retval;
1521         }
1522 }
1523
1524 #ifdef __ARCH_WANT_SYS_GETPGRP
1525
1526 asmlinkage long sys_getpgrp(void)
1527 {
1528         /* SMP - assuming writes are word atomic this is fine */
1529         return process_group(current);
1530 }
1531
1532 #endif
1533
1534 asmlinkage long sys_getsid(pid_t pid)
1535 {
1536         if (!pid)
1537                 return process_session(current);
1538         else {
1539                 int retval;
1540                 struct task_struct *p;
1541
1542                 read_lock(&tasklist_lock);
1543                 p = find_task_by_pid(pid);
1544
1545                 retval = -ESRCH;
1546                 if (p) {
1547                         retval = security_task_getsid(p);
1548                         if (!retval)
1549                                 retval = process_session(p);
1550                 }
1551                 read_unlock(&tasklist_lock);
1552                 return retval;
1553         }
1554 }
1555
1556 asmlinkage long sys_setsid(void)
1557 {
1558         struct task_struct *group_leader = current->group_leader;
1559         pid_t session;
1560         int err = -EPERM;
1561
1562         write_lock_irq(&tasklist_lock);
1563
1564         /* Fail if I am already a session leader */
1565         if (group_leader->signal->leader)
1566                 goto out;
1567
1568         session = group_leader->pid;
1569         /* Fail if a process group id already exists that equals the
1570          * proposed session id.
1571          *
1572          * Don't check if session id == 1 because kernel threads use this
1573          * session id and so the check will always fail and make it so
1574          * init cannot successfully call setsid.
1575          */
1576         if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
1577                 goto out;
1578
1579         group_leader->signal->leader = 1;
1580         __set_special_pids(session, session);
1581
1582         spin_lock(&group_leader->sighand->siglock);
1583         group_leader->signal->tty = NULL;
1584         spin_unlock(&group_leader->sighand->siglock);
1585
1586         err = process_group(group_leader);
1587 out:
1588         write_unlock_irq(&tasklist_lock);
1589         return err;
1590 }
1591
1592 /*
1593  * Supplementary group IDs
1594  */
1595
1596 /* init to 2 - one for init_task, one to ensure it is never freed */
1597 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1598
1599 struct group_info *groups_alloc(int gidsetsize)
1600 {
1601         struct group_info *group_info;
1602         int nblocks;
1603         int i;
1604
1605         nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1606         /* Make sure we always allocate at least one indirect block pointer */
1607         nblocks = nblocks ? : 1;
1608         group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1609         if (!group_info)
1610                 return NULL;
1611         group_info->ngroups = gidsetsize;
1612         group_info->nblocks = nblocks;
1613         atomic_set(&group_info->usage, 1);
1614
1615         if (gidsetsize <= NGROUPS_SMALL)
1616                 group_info->blocks[0] = group_info->small_block;
1617         else {
1618                 for (i = 0; i < nblocks; i++) {
1619                         gid_t *b;
1620                         b = (void *)__get_free_page(GFP_USER);
1621                         if (!b)
1622                                 goto out_undo_partial_alloc;
1623                         group_info->blocks[i] = b;
1624                 }
1625         }
1626         return group_info;
1627
1628 out_undo_partial_alloc:
1629         while (--i >= 0) {
1630                 free_page((unsigned long)group_info->blocks[i]);
1631         }
1632         kfree(group_info);
1633         return NULL;
1634 }
1635
1636 EXPORT_SYMBOL(groups_alloc);
1637
1638 void groups_free(struct group_info *group_info)
1639 {
1640         if (group_info->blocks[0] != group_info->small_block) {
1641                 int i;
1642                 for (i = 0; i < group_info->nblocks; i++)
1643                         free_page((unsigned long)group_info->blocks[i]);
1644         }
1645         kfree(group_info);
1646 }
1647
1648 EXPORT_SYMBOL(groups_free);
1649
1650 /* export the group_info to a user-space array */
1651 static int groups_to_user(gid_t __user *grouplist,
1652     struct group_info *group_info)
1653 {
1654         int i;
1655         int count = group_info->ngroups;
1656
1657         for (i = 0; i < group_info->nblocks; i++) {
1658                 int cp_count = min(NGROUPS_PER_BLOCK, count);
1659                 int off = i * NGROUPS_PER_BLOCK;
1660                 int len = cp_count * sizeof(*grouplist);
1661
1662                 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1663                         return -EFAULT;
1664
1665                 count -= cp_count;
1666         }
1667         return 0;
1668 }
1669
1670 /* fill a group_info from a user-space array - it must be allocated already */
1671 static int groups_from_user(struct group_info *group_info,
1672     gid_t __user *grouplist)
1673 {
1674         int i;
1675         int count = group_info->ngroups;
1676
1677         for (i = 0; i < group_info->nblocks; i++) {
1678                 int cp_count = min(NGROUPS_PER_BLOCK, count);
1679                 int off = i * NGROUPS_PER_BLOCK;
1680                 int len = cp_count * sizeof(*grouplist);
1681
1682                 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1683                         return -EFAULT;
1684
1685                 count -= cp_count;
1686         }
1687         return 0;
1688 }
1689
1690 /* a simple Shell sort */
1691 static void groups_sort(struct group_info *group_info)
1692 {
1693         int base, max, stride;
1694         int gidsetsize = group_info->ngroups;
1695
1696         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1697                 ; /* nothing */
1698         stride /= 3;
1699
1700         while (stride) {
1701                 max = gidsetsize - stride;
1702                 for (base = 0; base < max; base++) {
1703                         int left = base;
1704                         int right = left + stride;
1705                         gid_t tmp = GROUP_AT(group_info, right);
1706
1707                         while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1708                                 GROUP_AT(group_info, right) =
1709                                     GROUP_AT(group_info, left);
1710                                 right = left;
1711                                 left -= stride;
1712                         }
1713                         GROUP_AT(group_info, right) = tmp;
1714                 }
1715                 stride /= 3;
1716         }
1717 }
1718
1719 /* a simple bsearch */
1720 int groups_search(struct group_info *group_info, gid_t grp)
1721 {
1722         unsigned int left, right;
1723
1724         if (!group_info)
1725                 return 0;
1726
1727         left = 0;
1728         right = group_info->ngroups;
1729         while (left < right) {
1730                 unsigned int mid = (left+right)/2;
1731                 int cmp = grp - GROUP_AT(group_info, mid);
1732                 if (cmp > 0)
1733                         left = mid + 1;
1734                 else if (cmp < 0)
1735                         right = mid;
1736                 else
1737                         return 1;
1738         }
1739         return 0;
1740 }
1741
1742 /* validate and set current->group_info */
1743 int set_current_groups(struct group_info *group_info)
1744 {
1745         int retval;
1746         struct group_info *old_info;
1747
1748         retval = security_task_setgroups(group_info);
1749         if (retval)
1750                 return retval;
1751
1752         groups_sort(group_info);
1753         get_group_info(group_info);
1754
1755         task_lock(current);
1756         old_info = current->group_info;
1757         current->group_info = group_info;
1758         task_unlock(current);
1759
1760         put_group_info(old_info);
1761
1762         return 0;
1763 }
1764
1765 EXPORT_SYMBOL(set_current_groups);
1766
1767 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1768 {
1769         int i = 0;
1770
1771         /*
1772          *      SMP: Nobody else can change our grouplist. Thus we are
1773          *      safe.
1774          */
1775
1776         if (gidsetsize < 0)
1777                 return -EINVAL;
1778
1779         /* no need to grab task_lock here; it cannot change */
1780         i = current->group_info->ngroups;
1781         if (gidsetsize) {
1782                 if (i > gidsetsize) {
1783                         i = -EINVAL;
1784                         goto out;
1785                 }
1786                 if (groups_to_user(grouplist, current->group_info)) {
1787                         i = -EFAULT;
1788                         goto out;
1789                 }
1790         }
1791 out:
1792         return i;
1793 }
1794
1795 /*
1796  *      SMP: Our groups are copy-on-write. We can set them safely
1797  *      without another task interfering.
1798  */
1799  
1800 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1801 {
1802         struct group_info *group_info;
1803         int retval;
1804
1805         if (!capable(CAP_SETGID))
1806                 return -EPERM;
1807         if ((unsigned)gidsetsize > NGROUPS_MAX)
1808                 return -EINVAL;
1809
1810         group_info = groups_alloc(gidsetsize);
1811         if (!group_info)
1812                 return -ENOMEM;
1813         retval = groups_from_user(group_info, grouplist);
1814         if (retval) {
1815                 put_group_info(group_info);
1816                 return retval;
1817         }
1818
1819         retval = set_current_groups(group_info);
1820         put_group_info(group_info);
1821
1822         return retval;
1823 }
1824
1825 /*
1826  * Check whether we're fsgid/egid or in the supplemental group..
1827  */
1828 int in_group_p(gid_t grp)
1829 {
1830         int retval = 1;
1831         if (grp != current->fsgid)
1832                 retval = groups_search(current->group_info, grp);
1833         return retval;
1834 }
1835
1836 EXPORT_SYMBOL(in_group_p);
1837
1838 int in_egroup_p(gid_t grp)
1839 {
1840         int retval = 1;
1841         if (grp != current->egid)
1842                 retval = groups_search(current->group_info, grp);
1843         return retval;
1844 }
1845
1846 EXPORT_SYMBOL(in_egroup_p);
1847
1848 DECLARE_RWSEM(uts_sem);
1849
1850 EXPORT_SYMBOL(uts_sem);
1851
1852 asmlinkage long sys_newuname(struct new_utsname __user * name)
1853 {
1854         int errno = 0;
1855
1856         down_read(&uts_sem);
1857         if (copy_to_user(name, utsname(), sizeof *name))
1858                 errno = -EFAULT;
1859         up_read(&uts_sem);
1860         return errno;
1861 }
1862
1863 asmlinkage long sys_sethostname(char __user *name, int len)
1864 {
1865         int errno;
1866         char tmp[__NEW_UTS_LEN];
1867
1868         if (!capable(CAP_SYS_ADMIN))
1869                 return -EPERM;
1870         if (len < 0 || len > __NEW_UTS_LEN)
1871                 return -EINVAL;
1872         down_write(&uts_sem);
1873         errno = -EFAULT;
1874         if (!copy_from_user(tmp, name, len)) {
1875                 memcpy(utsname()->nodename, tmp, len);
1876                 utsname()->nodename[len] = 0;
1877                 errno = 0;
1878         }
1879         up_write(&uts_sem);
1880         return errno;
1881 }
1882
1883 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1884
1885 asmlinkage long sys_gethostname(char __user *name, int len)
1886 {
1887         int i, errno;
1888
1889         if (len < 0)
1890                 return -EINVAL;
1891         down_read(&uts_sem);
1892         i = 1 + strlen(utsname()->nodename);
1893         if (i > len)
1894                 i = len;
1895         errno = 0;
1896         if (copy_to_user(name, utsname()->nodename, i))
1897                 errno = -EFAULT;
1898         up_read(&uts_sem);
1899         return errno;
1900 }
1901
1902 #endif
1903
1904 /*
1905  * Only setdomainname; getdomainname can be implemented by calling
1906  * uname()
1907  */
1908 asmlinkage long sys_setdomainname(char __user *name, int len)
1909 {
1910         int errno;
1911         char tmp[__NEW_UTS_LEN];
1912
1913         if (!capable(CAP_SYS_ADMIN))
1914                 return -EPERM;
1915         if (len < 0 || len > __NEW_UTS_LEN)
1916                 return -EINVAL;
1917
1918         down_write(&uts_sem);
1919         errno = -EFAULT;
1920         if (!copy_from_user(tmp, name, len)) {
1921                 memcpy(utsname()->domainname, tmp, len);
1922                 utsname()->domainname[len] = 0;
1923                 errno = 0;
1924         }
1925         up_write(&uts_sem);
1926         return errno;
1927 }
1928
1929 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1930 {
1931         if (resource >= RLIM_NLIMITS)
1932                 return -EINVAL;
1933         else {
1934                 struct rlimit value;
1935                 task_lock(current->group_leader);
1936                 value = current->signal->rlim[resource];
1937                 task_unlock(current->group_leader);
1938                 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1939         }
1940 }
1941
1942 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1943
1944 /*
1945  *      Back compatibility for getrlimit. Needed for some apps.
1946  */
1947  
1948 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1949 {
1950         struct rlimit x;
1951         if (resource >= RLIM_NLIMITS)
1952                 return -EINVAL;
1953
1954         task_lock(current->group_leader);
1955         x = current->signal->rlim[resource];
1956         task_unlock(current->group_leader);
1957         if (x.rlim_cur > 0x7FFFFFFF)
1958                 x.rlim_cur = 0x7FFFFFFF;
1959         if (x.rlim_max > 0x7FFFFFFF)
1960                 x.rlim_max = 0x7FFFFFFF;
1961         return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1962 }
1963
1964 #endif
1965
1966 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1967 {
1968         struct rlimit new_rlim, *old_rlim;
1969         unsigned long it_prof_secs;
1970         int retval;
1971
1972         if (resource >= RLIM_NLIMITS)
1973                 return -EINVAL;
1974         if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1975                 return -EFAULT;
1976         if (new_rlim.rlim_cur > new_rlim.rlim_max)
1977                 return -EINVAL;
1978         old_rlim = current->signal->rlim + resource;
1979         if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1980             !capable(CAP_SYS_RESOURCE))
1981                 return -EPERM;
1982         if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1983                 return -EPERM;
1984
1985         retval = security_task_setrlimit(resource, &new_rlim);
1986         if (retval)
1987                 return retval;
1988
1989         if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
1990                 /*
1991                  * The caller is asking for an immediate RLIMIT_CPU
1992                  * expiry.  But we use the zero value to mean "it was
1993                  * never set".  So let's cheat and make it one second
1994                  * instead
1995                  */
1996                 new_rlim.rlim_cur = 1;
1997         }
1998
1999         task_lock(current->group_leader);
2000         *old_rlim = new_rlim;
2001         task_unlock(current->group_leader);
2002
2003         if (resource != RLIMIT_CPU)
2004                 goto out;
2005
2006         /*
2007          * RLIMIT_CPU handling.   Note that the kernel fails to return an error
2008          * code if it rejected the user's attempt to set RLIMIT_CPU.  This is a
2009          * very long-standing error, and fixing it now risks breakage of
2010          * applications, so we live with it
2011          */
2012         if (new_rlim.rlim_cur == RLIM_INFINITY)
2013                 goto out;
2014
2015         it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
2016         if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
2017                 unsigned long rlim_cur = new_rlim.rlim_cur;
2018                 cputime_t cputime;
2019
2020                 cputime = secs_to_cputime(rlim_cur);
2021                 read_lock(&tasklist_lock);
2022                 spin_lock_irq(&current->sighand->siglock);
2023                 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
2024                 spin_unlock_irq(&current->sighand->siglock);
2025                 read_unlock(&tasklist_lock);
2026         }
2027 out:
2028         return 0;
2029 }
2030
2031 /*
2032  * It would make sense to put struct rusage in the task_struct,
2033  * except that would make the task_struct be *really big*.  After
2034  * task_struct gets moved into malloc'ed memory, it would
2035  * make sense to do this.  It will make moving the rest of the information
2036  * a lot simpler!  (Which we're not doing right now because we're not
2037  * measuring them yet).
2038  *
2039  * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
2040  * races with threads incrementing their own counters.  But since word
2041  * reads are atomic, we either get new values or old values and we don't
2042  * care which for the sums.  We always take the siglock to protect reading
2043  * the c* fields from p->signal from races with exit.c updating those
2044  * fields when reaping, so a sample either gets all the additions of a
2045  * given child after it's reaped, or none so this sample is before reaping.
2046  *
2047  * Locking:
2048  * We need to take the siglock for CHILDEREN, SELF and BOTH
2049  * for  the cases current multithreaded, non-current single threaded
2050  * non-current multithreaded.  Thread traversal is now safe with
2051  * the siglock held.
2052  * Strictly speaking, we donot need to take the siglock if we are current and
2053  * single threaded,  as no one else can take our signal_struct away, no one
2054  * else can  reap the  children to update signal->c* counters, and no one else
2055  * can race with the signal-> fields. If we do not take any lock, the
2056  * signal-> fields could be read out of order while another thread was just
2057  * exiting. So we should  place a read memory barrier when we avoid the lock.
2058  * On the writer side,  write memory barrier is implied in  __exit_signal
2059  * as __exit_signal releases  the siglock spinlock after updating the signal->
2060  * fields. But we don't do this yet to keep things simple.
2061  *
2062  */
2063
2064 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
2065 {
2066         struct task_struct *t;
2067         unsigned long flags;
2068         cputime_t utime, stime;
2069
2070         memset((char *) r, 0, sizeof *r);
2071         utime = stime = cputime_zero;
2072
2073         rcu_read_lock();
2074         if (!lock_task_sighand(p, &flags)) {
2075                 rcu_read_unlock();
2076                 return;
2077         }
2078
2079         switch (who) {
2080                 case RUSAGE_BOTH:
2081                 case RUSAGE_CHILDREN:
2082                         utime = p->signal->cutime;
2083                         stime = p->signal->cstime;
2084                         r->ru_nvcsw = p->signal->cnvcsw;
2085                         r->ru_nivcsw = p->signal->cnivcsw;
2086                         r->ru_minflt = p->signal->cmin_flt;
2087                         r->ru_majflt = p->signal->cmaj_flt;
2088                         r->ru_inblock = p->signal->cinblock;
2089                         r->ru_oublock = p->signal->coublock;
2090
2091                         if (who == RUSAGE_CHILDREN)
2092                                 break;
2093
2094                 case RUSAGE_SELF:
2095                         utime = cputime_add(utime, p->signal->utime);
2096                         stime = cputime_add(stime, p->signal->stime);
2097                         r->ru_nvcsw += p->signal->nvcsw;
2098                         r->ru_nivcsw += p->signal->nivcsw;
2099                         r->ru_minflt += p->signal->min_flt;
2100                         r->ru_majflt += p->signal->maj_flt;
2101                         r->ru_inblock += p->signal->inblock;
2102                         r->ru_oublock += p->signal->oublock;
2103                         t = p;
2104                         do {
2105                                 utime = cputime_add(utime, t->utime);
2106                                 stime = cputime_add(stime, t->stime);
2107                                 r->ru_nvcsw += t->nvcsw;
2108                                 r->ru_nivcsw += t->nivcsw;
2109                                 r->ru_minflt += t->min_flt;
2110                                 r->ru_majflt += t->maj_flt;
2111                                 r->ru_inblock += task_io_get_inblock(t);
2112                                 r->ru_oublock += task_io_get_oublock(t);
2113                                 t = next_thread(t);
2114                         } while (t != p);
2115                         break;
2116
2117                 default:
2118                         BUG();
2119         }
2120
2121         unlock_task_sighand(p, &flags);
2122         rcu_read_unlock();
2123
2124         cputime_to_timeval(utime, &r->ru_utime);
2125         cputime_to_timeval(stime, &r->ru_stime);
2126 }
2127
2128 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
2129 {
2130         struct rusage r;
2131         k_getrusage(p, who, &r);
2132         return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
2133 }
2134
2135 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
2136 {
2137         if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
2138                 return -EINVAL;
2139         return getrusage(current, who, ru);
2140 }
2141
2142 asmlinkage long sys_umask(int mask)
2143 {
2144         mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
2145         return mask;
2146 }
2147     
2148 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
2149                           unsigned long arg4, unsigned long arg5)
2150 {
2151         long error;
2152
2153         error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2154         if (error)
2155                 return error;
2156
2157         switch (option) {
2158                 case PR_SET_PDEATHSIG:
2159                         if (!valid_signal(arg2)) {
2160                                 error = -EINVAL;
2161                                 break;
2162                         }
2163                         current->pdeath_signal = arg2;
2164                         break;
2165                 case PR_GET_PDEATHSIG:
2166                         error = put_user(current->pdeath_signal, (int __user *)arg2);
2167                         break;
2168                 case PR_GET_DUMPABLE:
2169                         error = current->mm->dumpable;
2170                         break;
2171                 case PR_SET_DUMPABLE:
2172                         if (arg2 < 0 || arg2 > 1) {
2173                                 error = -EINVAL;
2174                                 break;
2175                         }
2176                         current->mm->dumpable = arg2;
2177                         break;
2178
2179                 case PR_SET_UNALIGN:
2180                         error = SET_UNALIGN_CTL(current, arg2);
2181                         break;
2182                 case PR_GET_UNALIGN:
2183                         error = GET_UNALIGN_CTL(current, arg2);
2184                         break;
2185                 case PR_SET_FPEMU:
2186                         error = SET_FPEMU_CTL(current, arg2);
2187                         break;
2188                 case PR_GET_FPEMU:
2189                         error = GET_FPEMU_CTL(current, arg2);
2190                         break;
2191                 case PR_SET_FPEXC:
2192                         error = SET_FPEXC_CTL(current, arg2);
2193                         break;
2194                 case PR_GET_FPEXC:
2195                         error = GET_FPEXC_CTL(current, arg2);
2196                         break;
2197                 case PR_GET_TIMING:
2198                         error = PR_TIMING_STATISTICAL;
2199                         break;
2200                 case PR_SET_TIMING:
2201                         if (arg2 == PR_TIMING_STATISTICAL)
2202                                 error = 0;
2203                         else
2204                                 error = -EINVAL;
2205                         break;
2206
2207                 case PR_GET_KEEPCAPS:
2208                         if (current->keep_capabilities)
2209                                 error = 1;
2210                         break;
2211                 case PR_SET_KEEPCAPS:
2212                         if (arg2 != 0 && arg2 != 1) {
2213                                 error = -EINVAL;
2214                                 break;
2215                         }
2216                         current->keep_capabilities = arg2;
2217                         break;
2218                 case PR_SET_NAME: {
2219                         struct task_struct *me = current;
2220                         unsigned char ncomm[sizeof(me->comm)];
2221
2222                         ncomm[sizeof(me->comm)-1] = 0;
2223                         if (strncpy_from_user(ncomm, (char __user *)arg2,
2224                                                 sizeof(me->comm)-1) < 0)
2225                                 return -EFAULT;
2226                         set_task_comm(me, ncomm);
2227                         return 0;
2228                 }
2229                 case PR_GET_NAME: {
2230                         struct task_struct *me = current;
2231                         unsigned char tcomm[sizeof(me->comm)];
2232
2233                         get_task_comm(tcomm, me);
2234                         if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2235                                 return -EFAULT;
2236                         return 0;
2237                 }
2238                 case PR_GET_ENDIAN:
2239                         error = GET_ENDIAN(current, arg2);
2240                         break;
2241                 case PR_SET_ENDIAN:
2242                         error = SET_ENDIAN(current, arg2);
2243                         break;
2244
2245                 default:
2246                         error = -EINVAL;
2247                         break;
2248         }
2249         return error;
2250 }
2251
2252 asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2253                            struct getcpu_cache __user *cache)
2254 {
2255         int err = 0;
2256         int cpu = raw_smp_processor_id();
2257         if (cpup)
2258                 err |= put_user(cpu, cpup);
2259         if (nodep)
2260                 err |= put_user(cpu_to_node(cpu), nodep);
2261         if (cache) {
2262                 /*
2263                  * The cache is not needed for this implementation,
2264                  * but make sure user programs pass something
2265                  * valid. vsyscall implementations can instead make
2266                  * good use of the cache. Only use t0 and t1 because
2267                  * these are available in both 32bit and 64bit ABI (no
2268                  * need for a compat_getcpu). 32bit has enough
2269                  * padding
2270                  */
2271                 unsigned long t0, t1;
2272                 get_user(t0, &cache->blob[0]);
2273                 get_user(t1, &cache->blob[1]);
2274                 t0++;
2275                 t1++;
2276                 put_user(t0, &cache->blob[0]);
2277                 put_user(t1, &cache->blob[1]);
2278         }
2279         return err ? -EFAULT : 0;
2280 }