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