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