V4L/DVB (3436): move config byte from tuner_params to tuner_range struct.
[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/config.h>
8 #include <linux/module.h>
9 #include <linux/mm.h>
10 #include <linux/utsname.h>
11 #include <linux/mman.h>
12 #include <linux/smp_lock.h>
13 #include <linux/notifier.h>
14 #include <linux/reboot.h>
15 #include <linux/prctl.h>
16 #include <linux/init.h>
17 #include <linux/highuid.h>
18 #include <linux/fs.h>
19 #include <linux/kernel.h>
20 #include <linux/kexec.h>
21 #include <linux/workqueue.h>
22 #include <linux/capability.h>
23 #include <linux/device.h>
24 #include <linux/key.h>
25 #include <linux/times.h>
26 #include <linux/posix-timers.h>
27 #include <linux/security.h>
28 #include <linux/dcookies.h>
29 #include <linux/suspend.h>
30 #include <linux/tty.h>
31 #include <linux/signal.h>
32 #include <linux/cn_proc.h>
33
34 #include <linux/compat.h>
35 #include <linux/syscalls.h>
36 #include <linux/kprobes.h>
37
38 #include <asm/uaccess.h>
39 #include <asm/io.h>
40 #include <asm/unistd.h>
41
42 #ifndef SET_UNALIGN_CTL
43 # define SET_UNALIGN_CTL(a,b)   (-EINVAL)
44 #endif
45 #ifndef GET_UNALIGN_CTL
46 # define GET_UNALIGN_CTL(a,b)   (-EINVAL)
47 #endif
48 #ifndef SET_FPEMU_CTL
49 # define SET_FPEMU_CTL(a,b)     (-EINVAL)
50 #endif
51 #ifndef GET_FPEMU_CTL
52 # define GET_FPEMU_CTL(a,b)     (-EINVAL)
53 #endif
54 #ifndef SET_FPEXC_CTL
55 # define SET_FPEXC_CTL(a,b)     (-EINVAL)
56 #endif
57 #ifndef GET_FPEXC_CTL
58 # define GET_FPEXC_CTL(a,b)     (-EINVAL)
59 #endif
60
61 /*
62  * this is where the system-wide overflow UID and GID are defined, for
63  * architectures that now have 32-bit UID/GID but didn't in the past
64  */
65
66 int overflowuid = DEFAULT_OVERFLOWUID;
67 int overflowgid = DEFAULT_OVERFLOWGID;
68
69 #ifdef CONFIG_UID16
70 EXPORT_SYMBOL(overflowuid);
71 EXPORT_SYMBOL(overflowgid);
72 #endif
73
74 /*
75  * the same as above, but for filesystems which can only store a 16-bit
76  * UID and GID. as such, this is needed on all architectures
77  */
78
79 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
80 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
81
82 EXPORT_SYMBOL(fs_overflowuid);
83 EXPORT_SYMBOL(fs_overflowgid);
84
85 /*
86  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
87  */
88
89 int C_A_D = 1;
90 int cad_pid = 1;
91
92 /*
93  *      Notifier list for kernel code which wants to be called
94  *      at shutdown. This is used to stop any idling DMA operations
95  *      and the like. 
96  */
97
98 static struct notifier_block *reboot_notifier_list;
99 static DEFINE_RWLOCK(notifier_lock);
100
101 /**
102  *      notifier_chain_register - Add notifier to a notifier chain
103  *      @list: Pointer to root list pointer
104  *      @n: New entry in notifier chain
105  *
106  *      Adds a notifier to a notifier chain.
107  *
108  *      Currently always returns zero.
109  */
110  
111 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
112 {
113         write_lock(&notifier_lock);
114         while(*list)
115         {
116                 if(n->priority > (*list)->priority)
117                         break;
118                 list= &((*list)->next);
119         }
120         n->next = *list;
121         *list=n;
122         write_unlock(&notifier_lock);
123         return 0;
124 }
125
126 EXPORT_SYMBOL(notifier_chain_register);
127
128 /**
129  *      notifier_chain_unregister - Remove notifier from a notifier chain
130  *      @nl: Pointer to root list pointer
131  *      @n: New entry in notifier chain
132  *
133  *      Removes a notifier from a notifier chain.
134  *
135  *      Returns zero on success, or %-ENOENT on failure.
136  */
137  
138 int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
139 {
140         write_lock(&notifier_lock);
141         while((*nl)!=NULL)
142         {
143                 if((*nl)==n)
144                 {
145                         *nl=n->next;
146                         write_unlock(&notifier_lock);
147                         return 0;
148                 }
149                 nl=&((*nl)->next);
150         }
151         write_unlock(&notifier_lock);
152         return -ENOENT;
153 }
154
155 EXPORT_SYMBOL(notifier_chain_unregister);
156
157 /**
158  *      notifier_call_chain - Call functions in a notifier chain
159  *      @n: Pointer to root pointer of notifier chain
160  *      @val: Value passed unmodified to notifier function
161  *      @v: Pointer passed unmodified to notifier function
162  *
163  *      Calls each function in a notifier chain in turn.
164  *
165  *      If the return value of the notifier can be and'd
166  *      with %NOTIFY_STOP_MASK, then notifier_call_chain
167  *      will return immediately, with the return value of
168  *      the notifier function which halted execution.
169  *      Otherwise, the return value is the return value
170  *      of the last notifier function called.
171  */
172  
173 int __kprobes notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
174 {
175         int ret=NOTIFY_DONE;
176         struct notifier_block *nb = *n;
177
178         while(nb)
179         {
180                 ret=nb->notifier_call(nb,val,v);
181                 if(ret&NOTIFY_STOP_MASK)
182                 {
183                         return ret;
184                 }
185                 nb=nb->next;
186         }
187         return ret;
188 }
189
190 EXPORT_SYMBOL(notifier_call_chain);
191
192 /**
193  *      register_reboot_notifier - Register function to be called at reboot time
194  *      @nb: Info about notifier function to be called
195  *
196  *      Registers a function with the list of functions
197  *      to be called at reboot time.
198  *
199  *      Currently always returns zero, as notifier_chain_register
200  *      always returns zero.
201  */
202  
203 int register_reboot_notifier(struct notifier_block * nb)
204 {
205         return notifier_chain_register(&reboot_notifier_list, nb);
206 }
207
208 EXPORT_SYMBOL(register_reboot_notifier);
209
210 /**
211  *      unregister_reboot_notifier - Unregister previously registered reboot notifier
212  *      @nb: Hook to be unregistered
213  *
214  *      Unregisters a previously registered reboot
215  *      notifier function.
216  *
217  *      Returns zero on success, or %-ENOENT on failure.
218  */
219  
220 int unregister_reboot_notifier(struct notifier_block * nb)
221 {
222         return notifier_chain_unregister(&reboot_notifier_list, nb);
223 }
224
225 EXPORT_SYMBOL(unregister_reboot_notifier);
226
227 #ifndef CONFIG_SECURITY
228 int capable(int cap)
229 {
230         if (cap_raised(current->cap_effective, cap)) {
231                current->flags |= PF_SUPERPRIV;
232                return 1;
233         }
234         return 0;
235 }
236 EXPORT_SYMBOL(capable);
237 #endif
238
239 static int set_one_prio(struct task_struct *p, int niceval, int error)
240 {
241         int no_nice;
242
243         if (p->uid != current->euid &&
244                 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
245                 error = -EPERM;
246                 goto out;
247         }
248         if (niceval < task_nice(p) && !can_nice(p, niceval)) {
249                 error = -EACCES;
250                 goto out;
251         }
252         no_nice = security_task_setnice(p, niceval);
253         if (no_nice) {
254                 error = no_nice;
255                 goto out;
256         }
257         if (error == -ESRCH)
258                 error = 0;
259         set_user_nice(p, niceval);
260 out:
261         return error;
262 }
263
264 asmlinkage long sys_setpriority(int which, int who, int niceval)
265 {
266         struct task_struct *g, *p;
267         struct user_struct *user;
268         int error = -EINVAL;
269
270         if (which > 2 || which < 0)
271                 goto out;
272
273         /* normalize: avoid signed division (rounding problems) */
274         error = -ESRCH;
275         if (niceval < -20)
276                 niceval = -20;
277         if (niceval > 19)
278                 niceval = 19;
279
280         read_lock(&tasklist_lock);
281         switch (which) {
282                 case PRIO_PROCESS:
283                         if (!who)
284                                 who = current->pid;
285                         p = find_task_by_pid(who);
286                         if (p)
287                                 error = set_one_prio(p, niceval, error);
288                         break;
289                 case PRIO_PGRP:
290                         if (!who)
291                                 who = process_group(current);
292                         do_each_task_pid(who, PIDTYPE_PGID, p) {
293                                 error = set_one_prio(p, niceval, error);
294                         } while_each_task_pid(who, PIDTYPE_PGID, p);
295                         break;
296                 case PRIO_USER:
297                         user = current->user;
298                         if (!who)
299                                 who = current->uid;
300                         else
301                                 if ((who != current->uid) && !(user = find_user(who)))
302                                         goto out_unlock;        /* No processes for this user */
303
304                         do_each_thread(g, p)
305                                 if (p->uid == who)
306                                         error = set_one_prio(p, niceval, error);
307                         while_each_thread(g, p);
308                         if (who != current->uid)
309                                 free_uid(user);         /* For find_user() */
310                         break;
311         }
312 out_unlock:
313         read_unlock(&tasklist_lock);
314 out:
315         return error;
316 }
317
318 /*
319  * Ugh. To avoid negative return values, "getpriority()" will
320  * not return the normal nice-value, but a negated value that
321  * has been offset by 20 (ie it returns 40..1 instead of -20..19)
322  * to stay compatible.
323  */
324 asmlinkage long sys_getpriority(int which, int who)
325 {
326         struct task_struct *g, *p;
327         struct user_struct *user;
328         long niceval, retval = -ESRCH;
329
330         if (which > 2 || which < 0)
331                 return -EINVAL;
332
333         read_lock(&tasklist_lock);
334         switch (which) {
335                 case PRIO_PROCESS:
336                         if (!who)
337                                 who = current->pid;
338                         p = find_task_by_pid(who);
339                         if (p) {
340                                 niceval = 20 - task_nice(p);
341                                 if (niceval > retval)
342                                         retval = niceval;
343                         }
344                         break;
345                 case PRIO_PGRP:
346                         if (!who)
347                                 who = process_group(current);
348                         do_each_task_pid(who, PIDTYPE_PGID, p) {
349                                 niceval = 20 - task_nice(p);
350                                 if (niceval > retval)
351                                         retval = niceval;
352                         } while_each_task_pid(who, PIDTYPE_PGID, p);
353                         break;
354                 case PRIO_USER:
355                         user = current->user;
356                         if (!who)
357                                 who = current->uid;
358                         else
359                                 if ((who != current->uid) && !(user = find_user(who)))
360                                         goto out_unlock;        /* No processes for this user */
361
362                         do_each_thread(g, p)
363                                 if (p->uid == who) {
364                                         niceval = 20 - task_nice(p);
365                                         if (niceval > retval)
366                                                 retval = niceval;
367                                 }
368                         while_each_thread(g, p);
369                         if (who != current->uid)
370                                 free_uid(user);         /* for find_user() */
371                         break;
372         }
373 out_unlock:
374         read_unlock(&tasklist_lock);
375
376         return retval;
377 }
378
379 /**
380  *      emergency_restart - reboot the system
381  *
382  *      Without shutting down any hardware or taking any locks
383  *      reboot the system.  This is called when we know we are in
384  *      trouble so this is our best effort to reboot.  This is
385  *      safe to call in interrupt context.
386  */
387 void emergency_restart(void)
388 {
389         machine_emergency_restart();
390 }
391 EXPORT_SYMBOL_GPL(emergency_restart);
392
393 void kernel_restart_prepare(char *cmd)
394 {
395         notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
396         system_state = SYSTEM_RESTART;
397         device_shutdown();
398 }
399
400 /**
401  *      kernel_restart - reboot the system
402  *      @cmd: pointer to buffer containing command to execute for restart
403  *              or %NULL
404  *
405  *      Shutdown everything and perform a clean reboot.
406  *      This is not safe to call in interrupt context.
407  */
408 void kernel_restart(char *cmd)
409 {
410         kernel_restart_prepare(cmd);
411         if (!cmd) {
412                 printk(KERN_EMERG "Restarting system.\n");
413         } else {
414                 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
415         }
416         printk(".\n");
417         machine_restart(cmd);
418 }
419 EXPORT_SYMBOL_GPL(kernel_restart);
420
421 /**
422  *      kernel_kexec - reboot the system
423  *
424  *      Move into place and start executing a preloaded standalone
425  *      executable.  If nothing was preloaded return an error.
426  */
427 void kernel_kexec(void)
428 {
429 #ifdef CONFIG_KEXEC
430         struct kimage *image;
431         image = xchg(&kexec_image, 0);
432         if (!image) {
433                 return;
434         }
435         kernel_restart_prepare(NULL);
436         printk(KERN_EMERG "Starting new kernel\n");
437         machine_shutdown();
438         machine_kexec(image);
439 #endif
440 }
441 EXPORT_SYMBOL_GPL(kernel_kexec);
442
443 /**
444  *      kernel_halt - halt the system
445  *
446  *      Shutdown everything and perform a clean system halt.
447  */
448 void kernel_halt_prepare(void)
449 {
450         notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
451         system_state = SYSTEM_HALT;
452         device_shutdown();
453 }
454 void kernel_halt(void)
455 {
456         kernel_halt_prepare();
457         printk(KERN_EMERG "System halted.\n");
458         machine_halt();
459 }
460 EXPORT_SYMBOL_GPL(kernel_halt);
461
462 /**
463  *      kernel_power_off - power_off the system
464  *
465  *      Shutdown everything and perform a clean system power_off.
466  */
467 void kernel_power_off_prepare(void)
468 {
469         notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
470         system_state = SYSTEM_POWER_OFF;
471         device_shutdown();
472 }
473 void kernel_power_off(void)
474 {
475         kernel_power_off_prepare();
476         printk(KERN_EMERG "Power down.\n");
477         machine_power_off();
478 }
479 EXPORT_SYMBOL_GPL(kernel_power_off);
480
481 /*
482  * Reboot system call: for obvious reasons only root may call it,
483  * and even root needs to set up some magic numbers in the registers
484  * so that some mistake won't make this reboot the whole machine.
485  * You can also set the meaning of the ctrl-alt-del-key here.
486  *
487  * reboot doesn't sync: do that yourself before calling this.
488  */
489 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
490 {
491         char buffer[256];
492
493         /* We only trust the superuser with rebooting the system. */
494         if (!capable(CAP_SYS_BOOT))
495                 return -EPERM;
496
497         /* For safety, we require "magic" arguments. */
498         if (magic1 != LINUX_REBOOT_MAGIC1 ||
499             (magic2 != LINUX_REBOOT_MAGIC2 &&
500                         magic2 != LINUX_REBOOT_MAGIC2A &&
501                         magic2 != LINUX_REBOOT_MAGIC2B &&
502                         magic2 != LINUX_REBOOT_MAGIC2C))
503                 return -EINVAL;
504
505         /* Instead of trying to make the power_off code look like
506          * halt when pm_power_off is not set do it the easy way.
507          */
508         if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
509                 cmd = LINUX_REBOOT_CMD_HALT;
510
511         lock_kernel();
512         switch (cmd) {
513         case LINUX_REBOOT_CMD_RESTART:
514                 kernel_restart(NULL);
515                 break;
516
517         case LINUX_REBOOT_CMD_CAD_ON:
518                 C_A_D = 1;
519                 break;
520
521         case LINUX_REBOOT_CMD_CAD_OFF:
522                 C_A_D = 0;
523                 break;
524
525         case LINUX_REBOOT_CMD_HALT:
526                 kernel_halt();
527                 unlock_kernel();
528                 do_exit(0);
529                 break;
530
531         case LINUX_REBOOT_CMD_POWER_OFF:
532                 kernel_power_off();
533                 unlock_kernel();
534                 do_exit(0);
535                 break;
536
537         case LINUX_REBOOT_CMD_RESTART2:
538                 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
539                         unlock_kernel();
540                         return -EFAULT;
541                 }
542                 buffer[sizeof(buffer) - 1] = '\0';
543
544                 kernel_restart(buffer);
545                 break;
546
547         case LINUX_REBOOT_CMD_KEXEC:
548                 kernel_kexec();
549                 unlock_kernel();
550                 return -EINVAL;
551
552 #ifdef CONFIG_SOFTWARE_SUSPEND
553         case LINUX_REBOOT_CMD_SW_SUSPEND:
554                 {
555                         int ret = software_suspend();
556                         unlock_kernel();
557                         return ret;
558                 }
559 #endif
560
561         default:
562                 unlock_kernel();
563                 return -EINVAL;
564         }
565         unlock_kernel();
566         return 0;
567 }
568
569 static void deferred_cad(void *dummy)
570 {
571         kernel_restart(NULL);
572 }
573
574 /*
575  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
576  * As it's called within an interrupt, it may NOT sync: the only choice
577  * is whether to reboot at once, or just ignore the ctrl-alt-del.
578  */
579 void ctrl_alt_del(void)
580 {
581         static DECLARE_WORK(cad_work, deferred_cad, NULL);
582
583         if (C_A_D)
584                 schedule_work(&cad_work);
585         else
586                 kill_proc(cad_pid, SIGINT, 1);
587 }
588         
589
590 /*
591  * Unprivileged users may change the real gid to the effective gid
592  * or vice versa.  (BSD-style)
593  *
594  * If you set the real gid at all, or set the effective gid to a value not
595  * equal to the real gid, then the saved gid is set to the new effective gid.
596  *
597  * This makes it possible for a setgid program to completely drop its
598  * privileges, which is often a useful assertion to make when you are doing
599  * a security audit over a program.
600  *
601  * The general idea is that a program which uses just setregid() will be
602  * 100% compatible with BSD.  A program which uses just setgid() will be
603  * 100% compatible with POSIX with saved IDs. 
604  *
605  * SMP: There are not races, the GIDs are checked only by filesystem
606  *      operations (as far as semantic preservation is concerned).
607  */
608 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
609 {
610         int old_rgid = current->gid;
611         int old_egid = current->egid;
612         int new_rgid = old_rgid;
613         int new_egid = old_egid;
614         int retval;
615
616         retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
617         if (retval)
618                 return retval;
619
620         if (rgid != (gid_t) -1) {
621                 if ((old_rgid == rgid) ||
622                     (current->egid==rgid) ||
623                     capable(CAP_SETGID))
624                         new_rgid = rgid;
625                 else
626                         return -EPERM;
627         }
628         if (egid != (gid_t) -1) {
629                 if ((old_rgid == egid) ||
630                     (current->egid == egid) ||
631                     (current->sgid == egid) ||
632                     capable(CAP_SETGID))
633                         new_egid = egid;
634                 else {
635                         return -EPERM;
636                 }
637         }
638         if (new_egid != old_egid)
639         {
640                 current->mm->dumpable = suid_dumpable;
641                 smp_wmb();
642         }
643         if (rgid != (gid_t) -1 ||
644             (egid != (gid_t) -1 && egid != old_rgid))
645                 current->sgid = new_egid;
646         current->fsgid = new_egid;
647         current->egid = new_egid;
648         current->gid = new_rgid;
649         key_fsgid_changed(current);
650         proc_id_connector(current, PROC_EVENT_GID);
651         return 0;
652 }
653
654 /*
655  * setgid() is implemented like SysV w/ SAVED_IDS 
656  *
657  * SMP: Same implicit races as above.
658  */
659 asmlinkage long sys_setgid(gid_t gid)
660 {
661         int old_egid = current->egid;
662         int retval;
663
664         retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
665         if (retval)
666                 return retval;
667
668         if (capable(CAP_SETGID))
669         {
670                 if(old_egid != gid)
671                 {
672                         current->mm->dumpable = suid_dumpable;
673                         smp_wmb();
674                 }
675                 current->gid = current->egid = current->sgid = current->fsgid = gid;
676         }
677         else if ((gid == current->gid) || (gid == current->sgid))
678         {
679                 if(old_egid != gid)
680                 {
681                         current->mm->dumpable = suid_dumpable;
682                         smp_wmb();
683                 }
684                 current->egid = current->fsgid = gid;
685         }
686         else
687                 return -EPERM;
688
689         key_fsgid_changed(current);
690         proc_id_connector(current, PROC_EVENT_GID);
691         return 0;
692 }
693   
694 static int set_user(uid_t new_ruid, int dumpclear)
695 {
696         struct user_struct *new_user;
697
698         new_user = alloc_uid(new_ruid);
699         if (!new_user)
700                 return -EAGAIN;
701
702         if (atomic_read(&new_user->processes) >=
703                                 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
704                         new_user != &root_user) {
705                 free_uid(new_user);
706                 return -EAGAIN;
707         }
708
709         switch_uid(new_user);
710
711         if(dumpclear)
712         {
713                 current->mm->dumpable = suid_dumpable;
714                 smp_wmb();
715         }
716         current->uid = new_ruid;
717         return 0;
718 }
719
720 /*
721  * Unprivileged users may change the real uid to the effective uid
722  * or vice versa.  (BSD-style)
723  *
724  * If you set the real uid at all, or set the effective uid to a value not
725  * equal to the real uid, then the saved uid is set to the new effective uid.
726  *
727  * This makes it possible for a setuid program to completely drop its
728  * privileges, which is often a useful assertion to make when you are doing
729  * a security audit over a program.
730  *
731  * The general idea is that a program which uses just setreuid() will be
732  * 100% compatible with BSD.  A program which uses just setuid() will be
733  * 100% compatible with POSIX with saved IDs. 
734  */
735 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
736 {
737         int old_ruid, old_euid, old_suid, new_ruid, new_euid;
738         int retval;
739
740         retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
741         if (retval)
742                 return retval;
743
744         new_ruid = old_ruid = current->uid;
745         new_euid = old_euid = current->euid;
746         old_suid = current->suid;
747
748         if (ruid != (uid_t) -1) {
749                 new_ruid = ruid;
750                 if ((old_ruid != ruid) &&
751                     (current->euid != ruid) &&
752                     !capable(CAP_SETUID))
753                         return -EPERM;
754         }
755
756         if (euid != (uid_t) -1) {
757                 new_euid = euid;
758                 if ((old_ruid != euid) &&
759                     (current->euid != euid) &&
760                     (current->suid != euid) &&
761                     !capable(CAP_SETUID))
762                         return -EPERM;
763         }
764
765         if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
766                 return -EAGAIN;
767
768         if (new_euid != old_euid)
769         {
770                 current->mm->dumpable = suid_dumpable;
771                 smp_wmb();
772         }
773         current->fsuid = current->euid = new_euid;
774         if (ruid != (uid_t) -1 ||
775             (euid != (uid_t) -1 && euid != old_ruid))
776                 current->suid = current->euid;
777         current->fsuid = current->euid;
778
779         key_fsuid_changed(current);
780         proc_id_connector(current, PROC_EVENT_UID);
781
782         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
783 }
784
785
786                 
787 /*
788  * setuid() is implemented like SysV with SAVED_IDS 
789  * 
790  * Note that SAVED_ID's is deficient in that a setuid root program
791  * like sendmail, for example, cannot set its uid to be a normal 
792  * user and then switch back, because if you're root, setuid() sets
793  * the saved uid too.  If you don't like this, blame the bright people
794  * in the POSIX committee and/or USG.  Note that the BSD-style setreuid()
795  * will allow a root program to temporarily drop privileges and be able to
796  * regain them by swapping the real and effective uid.  
797  */
798 asmlinkage long sys_setuid(uid_t uid)
799 {
800         int old_euid = current->euid;
801         int old_ruid, old_suid, new_ruid, new_suid;
802         int retval;
803
804         retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
805         if (retval)
806                 return retval;
807
808         old_ruid = new_ruid = current->uid;
809         old_suid = current->suid;
810         new_suid = old_suid;
811         
812         if (capable(CAP_SETUID)) {
813                 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
814                         return -EAGAIN;
815                 new_suid = uid;
816         } else if ((uid != current->uid) && (uid != new_suid))
817                 return -EPERM;
818
819         if (old_euid != uid)
820         {
821                 current->mm->dumpable = suid_dumpable;
822                 smp_wmb();
823         }
824         current->fsuid = current->euid = uid;
825         current->suid = new_suid;
826
827         key_fsuid_changed(current);
828         proc_id_connector(current, PROC_EVENT_UID);
829
830         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
831 }
832
833
834 /*
835  * This function implements a generic ability to update ruid, euid,
836  * and suid.  This allows you to implement the 4.4 compatible seteuid().
837  */
838 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
839 {
840         int old_ruid = current->uid;
841         int old_euid = current->euid;
842         int old_suid = current->suid;
843         int retval;
844
845         retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
846         if (retval)
847                 return retval;
848
849         if (!capable(CAP_SETUID)) {
850                 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
851                     (ruid != current->euid) && (ruid != current->suid))
852                         return -EPERM;
853                 if ((euid != (uid_t) -1) && (euid != current->uid) &&
854                     (euid != current->euid) && (euid != current->suid))
855                         return -EPERM;
856                 if ((suid != (uid_t) -1) && (suid != current->uid) &&
857                     (suid != current->euid) && (suid != current->suid))
858                         return -EPERM;
859         }
860         if (ruid != (uid_t) -1) {
861                 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
862                         return -EAGAIN;
863         }
864         if (euid != (uid_t) -1) {
865                 if (euid != current->euid)
866                 {
867                         current->mm->dumpable = suid_dumpable;
868                         smp_wmb();
869                 }
870                 current->euid = euid;
871         }
872         current->fsuid = current->euid;
873         if (suid != (uid_t) -1)
874                 current->suid = suid;
875
876         key_fsuid_changed(current);
877         proc_id_connector(current, PROC_EVENT_UID);
878
879         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
880 }
881
882 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
883 {
884         int retval;
885
886         if (!(retval = put_user(current->uid, ruid)) &&
887             !(retval = put_user(current->euid, euid)))
888                 retval = put_user(current->suid, suid);
889
890         return retval;
891 }
892
893 /*
894  * Same as above, but for rgid, egid, sgid.
895  */
896 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
897 {
898         int retval;
899
900         retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
901         if (retval)
902                 return retval;
903
904         if (!capable(CAP_SETGID)) {
905                 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
906                     (rgid != current->egid) && (rgid != current->sgid))
907                         return -EPERM;
908                 if ((egid != (gid_t) -1) && (egid != current->gid) &&
909                     (egid != current->egid) && (egid != current->sgid))
910                         return -EPERM;
911                 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
912                     (sgid != current->egid) && (sgid != current->sgid))
913                         return -EPERM;
914         }
915         if (egid != (gid_t) -1) {
916                 if (egid != current->egid)
917                 {
918                         current->mm->dumpable = suid_dumpable;
919                         smp_wmb();
920                 }
921                 current->egid = egid;
922         }
923         current->fsgid = current->egid;
924         if (rgid != (gid_t) -1)
925                 current->gid = rgid;
926         if (sgid != (gid_t) -1)
927                 current->sgid = sgid;
928
929         key_fsgid_changed(current);
930         proc_id_connector(current, PROC_EVENT_GID);
931         return 0;
932 }
933
934 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
935 {
936         int retval;
937
938         if (!(retval = put_user(current->gid, rgid)) &&
939             !(retval = put_user(current->egid, egid)))
940                 retval = put_user(current->sgid, sgid);
941
942         return retval;
943 }
944
945
946 /*
947  * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
948  * is used for "access()" and for the NFS daemon (letting nfsd stay at
949  * whatever uid it wants to). It normally shadows "euid", except when
950  * explicitly set by setfsuid() or for access..
951  */
952 asmlinkage long sys_setfsuid(uid_t uid)
953 {
954         int old_fsuid;
955
956         old_fsuid = current->fsuid;
957         if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
958                 return old_fsuid;
959
960         if (uid == current->uid || uid == current->euid ||
961             uid == current->suid || uid == current->fsuid || 
962             capable(CAP_SETUID))
963         {
964                 if (uid != old_fsuid)
965                 {
966                         current->mm->dumpable = suid_dumpable;
967                         smp_wmb();
968                 }
969                 current->fsuid = uid;
970         }
971
972         key_fsuid_changed(current);
973         proc_id_connector(current, PROC_EVENT_UID);
974
975         security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
976
977         return old_fsuid;
978 }
979
980 /*
981  * Samma pÃ¥ svenska..
982  */
983 asmlinkage long sys_setfsgid(gid_t gid)
984 {
985         int old_fsgid;
986
987         old_fsgid = current->fsgid;
988         if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
989                 return old_fsgid;
990
991         if (gid == current->gid || gid == current->egid ||
992             gid == current->sgid || gid == current->fsgid || 
993             capable(CAP_SETGID))
994         {
995                 if (gid != old_fsgid)
996                 {
997                         current->mm->dumpable = suid_dumpable;
998                         smp_wmb();
999                 }
1000                 current->fsgid = gid;
1001                 key_fsgid_changed(current);
1002                 proc_id_connector(current, PROC_EVENT_GID);
1003         }
1004         return old_fsgid;
1005 }
1006
1007 asmlinkage long sys_times(struct tms __user * tbuf)
1008 {
1009         /*
1010          *      In the SMP world we might just be unlucky and have one of
1011          *      the times increment as we use it. Since the value is an
1012          *      atomically safe type this is just fine. Conceptually its
1013          *      as if the syscall took an instant longer to occur.
1014          */
1015         if (tbuf) {
1016                 struct tms tmp;
1017                 cputime_t utime, stime, cutime, cstime;
1018
1019 #ifdef CONFIG_SMP
1020                 if (thread_group_empty(current)) {
1021                         /*
1022                          * Single thread case without the use of any locks.
1023                          *
1024                          * We may race with release_task if two threads are
1025                          * executing. However, release task first adds up the
1026                          * counters (__exit_signal) before  removing the task
1027                          * from the process tasklist (__unhash_process).
1028                          * __exit_signal also acquires and releases the
1029                          * siglock which results in the proper memory ordering
1030                          * so that the list modifications are always visible
1031                          * after the counters have been updated.
1032                          *
1033                          * If the counters have been updated by the second thread
1034                          * but the thread has not yet been removed from the list
1035                          * then the other branch will be executing which will
1036                          * block on tasklist_lock until the exit handling of the
1037                          * other task is finished.
1038                          *
1039                          * This also implies that the sighand->siglock cannot
1040                          * be held by another processor. So we can also
1041                          * skip acquiring that lock.
1042                          */
1043                         utime = cputime_add(current->signal->utime, current->utime);
1044                         stime = cputime_add(current->signal->utime, current->stime);
1045                         cutime = current->signal->cutime;
1046                         cstime = current->signal->cstime;
1047                 } else
1048 #endif
1049                 {
1050
1051                         /* Process with multiple threads */
1052                         struct task_struct *tsk = current;
1053                         struct task_struct *t;
1054
1055                         read_lock(&tasklist_lock);
1056                         utime = tsk->signal->utime;
1057                         stime = tsk->signal->stime;
1058                         t = tsk;
1059                         do {
1060                                 utime = cputime_add(utime, t->utime);
1061                                 stime = cputime_add(stime, t->stime);
1062                                 t = next_thread(t);
1063                         } while (t != tsk);
1064
1065                         /*
1066                          * While we have tasklist_lock read-locked, no dying thread
1067                          * can be updating current->signal->[us]time.  Instead,
1068                          * we got their counts included in the live thread loop.
1069                          * However, another thread can come in right now and
1070                          * do a wait call that updates current->signal->c[us]time.
1071                          * To make sure we always see that pair updated atomically,
1072                          * we take the siglock around fetching them.
1073                          */
1074                         spin_lock_irq(&tsk->sighand->siglock);
1075                         cutime = tsk->signal->cutime;
1076                         cstime = tsk->signal->cstime;
1077                         spin_unlock_irq(&tsk->sighand->siglock);
1078                         read_unlock(&tasklist_lock);
1079                 }
1080                 tmp.tms_utime = cputime_to_clock_t(utime);
1081                 tmp.tms_stime = cputime_to_clock_t(stime);
1082                 tmp.tms_cutime = cputime_to_clock_t(cutime);
1083                 tmp.tms_cstime = cputime_to_clock_t(cstime);
1084                 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1085                         return -EFAULT;
1086         }
1087         return (long) jiffies_64_to_clock_t(get_jiffies_64());
1088 }
1089
1090 /*
1091  * This needs some heavy checking ...
1092  * I just haven't the stomach for it. I also don't fully
1093  * understand sessions/pgrp etc. Let somebody who does explain it.
1094  *
1095  * OK, I think I have the protection semantics right.... this is really
1096  * only important on a multi-user system anyway, to make sure one user
1097  * can't send a signal to a process owned by another.  -TYT, 12/12/91
1098  *
1099  * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1100  * LBT 04.03.94
1101  */
1102
1103 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1104 {
1105         struct task_struct *p;
1106         struct task_struct *group_leader = current->group_leader;
1107         int err = -EINVAL;
1108
1109         if (!pid)
1110                 pid = group_leader->pid;
1111         if (!pgid)
1112                 pgid = pid;
1113         if (pgid < 0)
1114                 return -EINVAL;
1115
1116         /* From this point forward we keep holding onto the tasklist lock
1117          * so that our parent does not change from under us. -DaveM
1118          */
1119         write_lock_irq(&tasklist_lock);
1120
1121         err = -ESRCH;
1122         p = find_task_by_pid(pid);
1123         if (!p)
1124                 goto out;
1125
1126         err = -EINVAL;
1127         if (!thread_group_leader(p))
1128                 goto out;
1129
1130         if (p->real_parent == group_leader) {
1131                 err = -EPERM;
1132                 if (p->signal->session != group_leader->signal->session)
1133                         goto out;
1134                 err = -EACCES;
1135                 if (p->did_exec)
1136                         goto out;
1137         } else {
1138                 err = -ESRCH;
1139                 if (p != group_leader)
1140                         goto out;
1141         }
1142
1143         err = -EPERM;
1144         if (p->signal->leader)
1145                 goto out;
1146
1147         if (pgid != pid) {
1148                 struct task_struct *p;
1149
1150                 do_each_task_pid(pgid, PIDTYPE_PGID, p) {
1151                         if (p->signal->session == group_leader->signal->session)
1152                                 goto ok_pgid;
1153                 } while_each_task_pid(pgid, PIDTYPE_PGID, p);
1154                 goto out;
1155         }
1156
1157 ok_pgid:
1158         err = security_task_setpgid(p, pgid);
1159         if (err)
1160                 goto out;
1161
1162         if (process_group(p) != pgid) {
1163                 detach_pid(p, PIDTYPE_PGID);
1164                 p->signal->pgrp = pgid;
1165                 attach_pid(p, PIDTYPE_PGID, pgid);
1166         }
1167
1168         err = 0;
1169 out:
1170         /* All paths lead to here, thus we are safe. -DaveM */
1171         write_unlock_irq(&tasklist_lock);
1172         return err;
1173 }
1174
1175 asmlinkage long sys_getpgid(pid_t pid)
1176 {
1177         if (!pid) {
1178                 return process_group(current);
1179         } else {
1180                 int retval;
1181                 struct task_struct *p;
1182
1183                 read_lock(&tasklist_lock);
1184                 p = find_task_by_pid(pid);
1185
1186                 retval = -ESRCH;
1187                 if (p) {
1188                         retval = security_task_getpgid(p);
1189                         if (!retval)
1190                                 retval = process_group(p);
1191                 }
1192                 read_unlock(&tasklist_lock);
1193                 return retval;
1194         }
1195 }
1196
1197 #ifdef __ARCH_WANT_SYS_GETPGRP
1198
1199 asmlinkage long sys_getpgrp(void)
1200 {
1201         /* SMP - assuming writes are word atomic this is fine */
1202         return process_group(current);
1203 }
1204
1205 #endif
1206
1207 asmlinkage long sys_getsid(pid_t pid)
1208 {
1209         if (!pid) {
1210                 return current->signal->session;
1211         } else {
1212                 int retval;
1213                 struct task_struct *p;
1214
1215                 read_lock(&tasklist_lock);
1216                 p = find_task_by_pid(pid);
1217
1218                 retval = -ESRCH;
1219                 if(p) {
1220                         retval = security_task_getsid(p);
1221                         if (!retval)
1222                                 retval = p->signal->session;
1223                 }
1224                 read_unlock(&tasklist_lock);
1225                 return retval;
1226         }
1227 }
1228
1229 asmlinkage long sys_setsid(void)
1230 {
1231         struct task_struct *group_leader = current->group_leader;
1232         struct pid *pid;
1233         int err = -EPERM;
1234
1235         down(&tty_sem);
1236         write_lock_irq(&tasklist_lock);
1237
1238         pid = find_pid(PIDTYPE_PGID, group_leader->pid);
1239         if (pid)
1240                 goto out;
1241
1242         group_leader->signal->leader = 1;
1243         __set_special_pids(group_leader->pid, group_leader->pid);
1244         group_leader->signal->tty = NULL;
1245         group_leader->signal->tty_old_pgrp = 0;
1246         err = process_group(group_leader);
1247 out:
1248         write_unlock_irq(&tasklist_lock);
1249         up(&tty_sem);
1250         return err;
1251 }
1252
1253 /*
1254  * Supplementary group IDs
1255  */
1256
1257 /* init to 2 - one for init_task, one to ensure it is never freed */
1258 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1259
1260 struct group_info *groups_alloc(int gidsetsize)
1261 {
1262         struct group_info *group_info;
1263         int nblocks;
1264         int i;
1265
1266         nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1267         /* Make sure we always allocate at least one indirect block pointer */
1268         nblocks = nblocks ? : 1;
1269         group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1270         if (!group_info)
1271                 return NULL;
1272         group_info->ngroups = gidsetsize;
1273         group_info->nblocks = nblocks;
1274         atomic_set(&group_info->usage, 1);
1275
1276         if (gidsetsize <= NGROUPS_SMALL) {
1277                 group_info->blocks[0] = group_info->small_block;
1278         } else {
1279                 for (i = 0; i < nblocks; i++) {
1280                         gid_t *b;
1281                         b = (void *)__get_free_page(GFP_USER);
1282                         if (!b)
1283                                 goto out_undo_partial_alloc;
1284                         group_info->blocks[i] = b;
1285                 }
1286         }
1287         return group_info;
1288
1289 out_undo_partial_alloc:
1290         while (--i >= 0) {
1291                 free_page((unsigned long)group_info->blocks[i]);
1292         }
1293         kfree(group_info);
1294         return NULL;
1295 }
1296
1297 EXPORT_SYMBOL(groups_alloc);
1298
1299 void groups_free(struct group_info *group_info)
1300 {
1301         if (group_info->blocks[0] != group_info->small_block) {
1302                 int i;
1303                 for (i = 0; i < group_info->nblocks; i++)
1304                         free_page((unsigned long)group_info->blocks[i]);
1305         }
1306         kfree(group_info);
1307 }
1308
1309 EXPORT_SYMBOL(groups_free);
1310
1311 /* export the group_info to a user-space array */
1312 static int groups_to_user(gid_t __user *grouplist,
1313     struct group_info *group_info)
1314 {
1315         int i;
1316         int count = group_info->ngroups;
1317
1318         for (i = 0; i < group_info->nblocks; i++) {
1319                 int cp_count = min(NGROUPS_PER_BLOCK, count);
1320                 int off = i * NGROUPS_PER_BLOCK;
1321                 int len = cp_count * sizeof(*grouplist);
1322
1323                 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1324                         return -EFAULT;
1325
1326                 count -= cp_count;
1327         }
1328         return 0;
1329 }
1330
1331 /* fill a group_info from a user-space array - it must be allocated already */
1332 static int groups_from_user(struct group_info *group_info,
1333     gid_t __user *grouplist)
1334  {
1335         int i;
1336         int count = group_info->ngroups;
1337
1338         for (i = 0; i < group_info->nblocks; i++) {
1339                 int cp_count = min(NGROUPS_PER_BLOCK, count);
1340                 int off = i * NGROUPS_PER_BLOCK;
1341                 int len = cp_count * sizeof(*grouplist);
1342
1343                 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1344                         return -EFAULT;
1345
1346                 count -= cp_count;
1347         }
1348         return 0;
1349 }
1350
1351 /* a simple Shell sort */
1352 static void groups_sort(struct group_info *group_info)
1353 {
1354         int base, max, stride;
1355         int gidsetsize = group_info->ngroups;
1356
1357         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1358                 ; /* nothing */
1359         stride /= 3;
1360
1361         while (stride) {
1362                 max = gidsetsize - stride;
1363                 for (base = 0; base < max; base++) {
1364                         int left = base;
1365                         int right = left + stride;
1366                         gid_t tmp = GROUP_AT(group_info, right);
1367
1368                         while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1369                                 GROUP_AT(group_info, right) =
1370                                     GROUP_AT(group_info, left);
1371                                 right = left;
1372                                 left -= stride;
1373                         }
1374                         GROUP_AT(group_info, right) = tmp;
1375                 }
1376                 stride /= 3;
1377         }
1378 }
1379
1380 /* a simple bsearch */
1381 int groups_search(struct group_info *group_info, gid_t grp)
1382 {
1383         int left, right;
1384
1385         if (!group_info)
1386                 return 0;
1387
1388         left = 0;
1389         right = group_info->ngroups;
1390         while (left < right) {
1391                 int mid = (left+right)/2;
1392                 int cmp = grp - GROUP_AT(group_info, mid);
1393                 if (cmp > 0)
1394                         left = mid + 1;
1395                 else if (cmp < 0)
1396                         right = mid;
1397                 else
1398                         return 1;
1399         }
1400         return 0;
1401 }
1402
1403 /* validate and set current->group_info */
1404 int set_current_groups(struct group_info *group_info)
1405 {
1406         int retval;
1407         struct group_info *old_info;
1408
1409         retval = security_task_setgroups(group_info);
1410         if (retval)
1411                 return retval;
1412
1413         groups_sort(group_info);
1414         get_group_info(group_info);
1415
1416         task_lock(current);
1417         old_info = current->group_info;
1418         current->group_info = group_info;
1419         task_unlock(current);
1420
1421         put_group_info(old_info);
1422
1423         return 0;
1424 }
1425
1426 EXPORT_SYMBOL(set_current_groups);
1427
1428 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1429 {
1430         int i = 0;
1431
1432         /*
1433          *      SMP: Nobody else can change our grouplist. Thus we are
1434          *      safe.
1435          */
1436
1437         if (gidsetsize < 0)
1438                 return -EINVAL;
1439
1440         /* no need to grab task_lock here; it cannot change */
1441         get_group_info(current->group_info);
1442         i = current->group_info->ngroups;
1443         if (gidsetsize) {
1444                 if (i > gidsetsize) {
1445                         i = -EINVAL;
1446                         goto out;
1447                 }
1448                 if (groups_to_user(grouplist, current->group_info)) {
1449                         i = -EFAULT;
1450                         goto out;
1451                 }
1452         }
1453 out:
1454         put_group_info(current->group_info);
1455         return i;
1456 }
1457
1458 /*
1459  *      SMP: Our groups are copy-on-write. We can set them safely
1460  *      without another task interfering.
1461  */
1462  
1463 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1464 {
1465         struct group_info *group_info;
1466         int retval;
1467
1468         if (!capable(CAP_SETGID))
1469                 return -EPERM;
1470         if ((unsigned)gidsetsize > NGROUPS_MAX)
1471                 return -EINVAL;
1472
1473         group_info = groups_alloc(gidsetsize);
1474         if (!group_info)
1475                 return -ENOMEM;
1476         retval = groups_from_user(group_info, grouplist);
1477         if (retval) {
1478                 put_group_info(group_info);
1479                 return retval;
1480         }
1481
1482         retval = set_current_groups(group_info);
1483         put_group_info(group_info);
1484
1485         return retval;
1486 }
1487
1488 /*
1489  * Check whether we're fsgid/egid or in the supplemental group..
1490  */
1491 int in_group_p(gid_t grp)
1492 {
1493         int retval = 1;
1494         if (grp != current->fsgid) {
1495                 get_group_info(current->group_info);
1496                 retval = groups_search(current->group_info, grp);
1497                 put_group_info(current->group_info);
1498         }
1499         return retval;
1500 }
1501
1502 EXPORT_SYMBOL(in_group_p);
1503
1504 int in_egroup_p(gid_t grp)
1505 {
1506         int retval = 1;
1507         if (grp != current->egid) {
1508                 get_group_info(current->group_info);
1509                 retval = groups_search(current->group_info, grp);
1510                 put_group_info(current->group_info);
1511         }
1512         return retval;
1513 }
1514
1515 EXPORT_SYMBOL(in_egroup_p);
1516
1517 DECLARE_RWSEM(uts_sem);
1518
1519 EXPORT_SYMBOL(uts_sem);
1520
1521 asmlinkage long sys_newuname(struct new_utsname __user * name)
1522 {
1523         int errno = 0;
1524
1525         down_read(&uts_sem);
1526         if (copy_to_user(name,&system_utsname,sizeof *name))
1527                 errno = -EFAULT;
1528         up_read(&uts_sem);
1529         return errno;
1530 }
1531
1532 asmlinkage long sys_sethostname(char __user *name, int len)
1533 {
1534         int errno;
1535         char tmp[__NEW_UTS_LEN];
1536
1537         if (!capable(CAP_SYS_ADMIN))
1538                 return -EPERM;
1539         if (len < 0 || len > __NEW_UTS_LEN)
1540                 return -EINVAL;
1541         down_write(&uts_sem);
1542         errno = -EFAULT;
1543         if (!copy_from_user(tmp, name, len)) {
1544                 memcpy(system_utsname.nodename, tmp, len);
1545                 system_utsname.nodename[len] = 0;
1546                 errno = 0;
1547         }
1548         up_write(&uts_sem);
1549         return errno;
1550 }
1551
1552 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1553
1554 asmlinkage long sys_gethostname(char __user *name, int len)
1555 {
1556         int i, errno;
1557
1558         if (len < 0)
1559                 return -EINVAL;
1560         down_read(&uts_sem);
1561         i = 1 + strlen(system_utsname.nodename);
1562         if (i > len)
1563                 i = len;
1564         errno = 0;
1565         if (copy_to_user(name, system_utsname.nodename, i))
1566                 errno = -EFAULT;
1567         up_read(&uts_sem);
1568         return errno;
1569 }
1570
1571 #endif
1572
1573 /*
1574  * Only setdomainname; getdomainname can be implemented by calling
1575  * uname()
1576  */
1577 asmlinkage long sys_setdomainname(char __user *name, int len)
1578 {
1579         int errno;
1580         char tmp[__NEW_UTS_LEN];
1581
1582         if (!capable(CAP_SYS_ADMIN))
1583                 return -EPERM;
1584         if (len < 0 || len > __NEW_UTS_LEN)
1585                 return -EINVAL;
1586
1587         down_write(&uts_sem);
1588         errno = -EFAULT;
1589         if (!copy_from_user(tmp, name, len)) {
1590                 memcpy(system_utsname.domainname, tmp, len);
1591                 system_utsname.domainname[len] = 0;
1592                 errno = 0;
1593         }
1594         up_write(&uts_sem);
1595         return errno;
1596 }
1597
1598 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1599 {
1600         if (resource >= RLIM_NLIMITS)
1601                 return -EINVAL;
1602         else {
1603                 struct rlimit value;
1604                 task_lock(current->group_leader);
1605                 value = current->signal->rlim[resource];
1606                 task_unlock(current->group_leader);
1607                 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1608         }
1609 }
1610
1611 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1612
1613 /*
1614  *      Back compatibility for getrlimit. Needed for some apps.
1615  */
1616  
1617 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1618 {
1619         struct rlimit x;
1620         if (resource >= RLIM_NLIMITS)
1621                 return -EINVAL;
1622
1623         task_lock(current->group_leader);
1624         x = current->signal->rlim[resource];
1625         task_unlock(current->group_leader);
1626         if(x.rlim_cur > 0x7FFFFFFF)
1627                 x.rlim_cur = 0x7FFFFFFF;
1628         if(x.rlim_max > 0x7FFFFFFF)
1629                 x.rlim_max = 0x7FFFFFFF;
1630         return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1631 }
1632
1633 #endif
1634
1635 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1636 {
1637         struct rlimit new_rlim, *old_rlim;
1638         int retval;
1639
1640         if (resource >= RLIM_NLIMITS)
1641                 return -EINVAL;
1642         if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1643                 return -EFAULT;
1644        if (new_rlim.rlim_cur > new_rlim.rlim_max)
1645                return -EINVAL;
1646         old_rlim = current->signal->rlim + resource;
1647         if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1648             !capable(CAP_SYS_RESOURCE))
1649                 return -EPERM;
1650         if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1651                         return -EPERM;
1652
1653         retval = security_task_setrlimit(resource, &new_rlim);
1654         if (retval)
1655                 return retval;
1656
1657         task_lock(current->group_leader);
1658         *old_rlim = new_rlim;
1659         task_unlock(current->group_leader);
1660
1661         if (resource == RLIMIT_CPU && new_rlim.rlim_cur != RLIM_INFINITY &&
1662             (cputime_eq(current->signal->it_prof_expires, cputime_zero) ||
1663              new_rlim.rlim_cur <= cputime_to_secs(
1664                      current->signal->it_prof_expires))) {
1665                 cputime_t cputime = secs_to_cputime(new_rlim.rlim_cur);
1666                 read_lock(&tasklist_lock);
1667                 spin_lock_irq(&current->sighand->siglock);
1668                 set_process_cpu_timer(current, CPUCLOCK_PROF,
1669                                       &cputime, NULL);
1670                 spin_unlock_irq(&current->sighand->siglock);
1671                 read_unlock(&tasklist_lock);
1672         }
1673
1674         return 0;
1675 }
1676
1677 /*
1678  * It would make sense to put struct rusage in the task_struct,
1679  * except that would make the task_struct be *really big*.  After
1680  * task_struct gets moved into malloc'ed memory, it would
1681  * make sense to do this.  It will make moving the rest of the information
1682  * a lot simpler!  (Which we're not doing right now because we're not
1683  * measuring them yet).
1684  *
1685  * This expects to be called with tasklist_lock read-locked or better,
1686  * and the siglock not locked.  It may momentarily take the siglock.
1687  *
1688  * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1689  * races with threads incrementing their own counters.  But since word
1690  * reads are atomic, we either get new values or old values and we don't
1691  * care which for the sums.  We always take the siglock to protect reading
1692  * the c* fields from p->signal from races with exit.c updating those
1693  * fields when reaping, so a sample either gets all the additions of a
1694  * given child after it's reaped, or none so this sample is before reaping.
1695  */
1696
1697 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1698 {
1699         struct task_struct *t;
1700         unsigned long flags;
1701         cputime_t utime, stime;
1702
1703         memset((char *) r, 0, sizeof *r);
1704
1705         if (unlikely(!p->signal))
1706                 return;
1707
1708         utime = stime = cputime_zero;
1709
1710         switch (who) {
1711                 case RUSAGE_BOTH:
1712                 case RUSAGE_CHILDREN:
1713                         spin_lock_irqsave(&p->sighand->siglock, flags);
1714                         utime = p->signal->cutime;
1715                         stime = p->signal->cstime;
1716                         r->ru_nvcsw = p->signal->cnvcsw;
1717                         r->ru_nivcsw = p->signal->cnivcsw;
1718                         r->ru_minflt = p->signal->cmin_flt;
1719                         r->ru_majflt = p->signal->cmaj_flt;
1720                         spin_unlock_irqrestore(&p->sighand->siglock, flags);
1721
1722                         if (who == RUSAGE_CHILDREN)
1723                                 break;
1724
1725                 case RUSAGE_SELF:
1726                         utime = cputime_add(utime, p->signal->utime);
1727                         stime = cputime_add(stime, p->signal->stime);
1728                         r->ru_nvcsw += p->signal->nvcsw;
1729                         r->ru_nivcsw += p->signal->nivcsw;
1730                         r->ru_minflt += p->signal->min_flt;
1731                         r->ru_majflt += p->signal->maj_flt;
1732                         t = p;
1733                         do {
1734                                 utime = cputime_add(utime, t->utime);
1735                                 stime = cputime_add(stime, t->stime);
1736                                 r->ru_nvcsw += t->nvcsw;
1737                                 r->ru_nivcsw += t->nivcsw;
1738                                 r->ru_minflt += t->min_flt;
1739                                 r->ru_majflt += t->maj_flt;
1740                                 t = next_thread(t);
1741                         } while (t != p);
1742                         break;
1743
1744                 default:
1745                         BUG();
1746         }
1747
1748         cputime_to_timeval(utime, &r->ru_utime);
1749         cputime_to_timeval(stime, &r->ru_stime);
1750 }
1751
1752 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1753 {
1754         struct rusage r;
1755         read_lock(&tasklist_lock);
1756         k_getrusage(p, who, &r);
1757         read_unlock(&tasklist_lock);
1758         return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1759 }
1760
1761 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1762 {
1763         if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1764                 return -EINVAL;
1765         return getrusage(current, who, ru);
1766 }
1767
1768 asmlinkage long sys_umask(int mask)
1769 {
1770         mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1771         return mask;
1772 }
1773     
1774 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1775                           unsigned long arg4, unsigned long arg5)
1776 {
1777         long error;
1778
1779         error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1780         if (error)
1781                 return error;
1782
1783         switch (option) {
1784                 case PR_SET_PDEATHSIG:
1785                         if (!valid_signal(arg2)) {
1786                                 error = -EINVAL;
1787                                 break;
1788                         }
1789                         current->pdeath_signal = arg2;
1790                         break;
1791                 case PR_GET_PDEATHSIG:
1792                         error = put_user(current->pdeath_signal, (int __user *)arg2);
1793                         break;
1794                 case PR_GET_DUMPABLE:
1795                         error = current->mm->dumpable;
1796                         break;
1797                 case PR_SET_DUMPABLE:
1798                         if (arg2 < 0 || arg2 > 2) {
1799                                 error = -EINVAL;
1800                                 break;
1801                         }
1802                         current->mm->dumpable = arg2;
1803                         break;
1804
1805                 case PR_SET_UNALIGN:
1806                         error = SET_UNALIGN_CTL(current, arg2);
1807                         break;
1808                 case PR_GET_UNALIGN:
1809                         error = GET_UNALIGN_CTL(current, arg2);
1810                         break;
1811                 case PR_SET_FPEMU:
1812                         error = SET_FPEMU_CTL(current, arg2);
1813                         break;
1814                 case PR_GET_FPEMU:
1815                         error = GET_FPEMU_CTL(current, arg2);
1816                         break;
1817                 case PR_SET_FPEXC:
1818                         error = SET_FPEXC_CTL(current, arg2);
1819                         break;
1820                 case PR_GET_FPEXC:
1821                         error = GET_FPEXC_CTL(current, arg2);
1822                         break;
1823                 case PR_GET_TIMING:
1824                         error = PR_TIMING_STATISTICAL;
1825                         break;
1826                 case PR_SET_TIMING:
1827                         if (arg2 == PR_TIMING_STATISTICAL)
1828                                 error = 0;
1829                         else
1830                                 error = -EINVAL;
1831                         break;
1832
1833                 case PR_GET_KEEPCAPS:
1834                         if (current->keep_capabilities)
1835                                 error = 1;
1836                         break;
1837                 case PR_SET_KEEPCAPS:
1838                         if (arg2 != 0 && arg2 != 1) {
1839                                 error = -EINVAL;
1840                                 break;
1841                         }
1842                         current->keep_capabilities = arg2;
1843                         break;
1844                 case PR_SET_NAME: {
1845                         struct task_struct *me = current;
1846                         unsigned char ncomm[sizeof(me->comm)];
1847
1848                         ncomm[sizeof(me->comm)-1] = 0;
1849                         if (strncpy_from_user(ncomm, (char __user *)arg2,
1850                                                 sizeof(me->comm)-1) < 0)
1851                                 return -EFAULT;
1852                         set_task_comm(me, ncomm);
1853                         return 0;
1854                 }
1855                 case PR_GET_NAME: {
1856                         struct task_struct *me = current;
1857                         unsigned char tcomm[sizeof(me->comm)];
1858
1859                         get_task_comm(tcomm, me);
1860                         if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
1861                                 return -EFAULT;
1862                         return 0;
1863                 }
1864                 default:
1865                         error = -EINVAL;
1866                         break;
1867         }
1868         return error;
1869 }