Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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