root/kernel/sys.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. proc_sel
  2. sys_setpriority
  3. sys_getpriority
  4. sys_profil
  5. sys_ftime
  6. sys_break
  7. sys_stty
  8. sys_gtty
  9. sys_prof
  10. save_v86_state
  11. mark_screen_rdonly
  12. sys_vm86
  13. sys_reboot
  14. ctrl_alt_del
  15. sys_setregid
  16. sys_setgid
  17. sys_acct
  18. sys_phys
  19. sys_lock
  20. sys_mpx
  21. sys_ulimit
  22. sys_old_syscall
  23. sys_time
  24. sys_setreuid
  25. sys_setuid
  26. sys_stime
  27. sys_times
  28. sys_brk
  29. sys_setpgid
  30. sys_getpgrp
  31. sys_setsid
  32. sys_getgroups
  33. sys_setgroups
  34. in_group_p
  35. sys_newuname
  36. sys_uname
  37. sys_olduname
  38. sys_sethostname
  39. sys_setdomainname
  40. sys_getrlimit
  41. sys_setrlimit
  42. getrusage
  43. sys_getrusage
  44. do_gettimeofday
  45. sys_gettimeofday
  46. sys_settimeofday
  47. adjust_clock
  48. sys_umask

   1 /*
   2  *  linux/kernel/sys.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <linux/errno.h>
   8 #include <linux/sched.h>
   9 #include <linux/tty.h>
  10 #include <linux/kernel.h>
  11 #include <linux/config.h>
  12 #include <linux/times.h>
  13 #include <linux/utsname.h>
  14 #include <linux/param.h>
  15 #include <linux/resource.h>
  16 #include <linux/signal.h>
  17 #include <linux/string.h>
  18 #include <linux/ptrace.h>
  19 #include <linux/stat.h>
  20 
  21 #include <asm/segment.h>
  22 #include <asm/io.h>
  23 
  24 /*
  25  * this indicates wether you can reboot with ctrl-alt-del: the default is yes
  26  */
  27 static int C_A_D = 1;
  28 
  29 /* 
  30  * The timezone where the local system is located.  Used as a default by some
  31  * programs who obtain this value by using gettimeofday.
  32  */
  33 struct timezone sys_tz = { 0, 0};
  34 
  35 extern int session_of_pgrp(int pgrp);
  36 extern void adjust_clock(void);
  37 
  38 #define PZERO   15
  39 
  40 static int proc_sel(struct task_struct *p, int which, int who)
     /* [previous][next][first][last][top][bottom][index][help] */
  41 {
  42         switch (which) {
  43                 case PRIO_PROCESS:
  44                         if (!who && p == current)
  45                                 return 1;
  46                         return(p->pid == who);
  47                 case PRIO_PGRP:
  48                         if (!who)
  49                                 who = current->pgrp;
  50                         return(p->pgrp == who);
  51                 case PRIO_USER:
  52                         if (!who)
  53                                 who = current->uid;
  54                         return(p->uid == who);
  55         }
  56         return 0;
  57 }
  58 
  59 asmlinkage int sys_setpriority(int which, int who, int niceval)
     /* [previous][next][first][last][top][bottom][index][help] */
  60 {
  61         struct task_struct **p;
  62         int error = ESRCH;
  63         int priority;
  64 
  65         if (which > 2 || which < 0)
  66                 return -EINVAL;
  67 
  68         if ((priority = PZERO - niceval) <= 0)
  69                 priority = 1;
  70 
  71         for(p = &LAST_TASK; p > &FIRST_TASK; --p) {
  72                 if (!*p || !proc_sel(*p, which, who))
  73                         continue;
  74                 if ((*p)->uid != current->euid &&
  75                         (*p)->uid != current->uid && !suser()) {
  76                         error = EPERM;
  77                         continue;
  78                 }
  79                 if (error == ESRCH)
  80                         error = 0;
  81                 if (priority > (*p)->priority && !suser())
  82                         error = EACCES;
  83                 else
  84                         (*p)->priority = priority;
  85         }
  86         return -error;
  87 }
  88 
  89 asmlinkage int sys_getpriority(int which, int who)
     /* [previous][next][first][last][top][bottom][index][help] */
  90 {
  91         struct task_struct **p;
  92         int max_prio = 0;
  93 
  94         if (which > 2 || which < 0)
  95                 return -EINVAL;
  96 
  97         for(p = &LAST_TASK; p > &FIRST_TASK; --p) {
  98                 if (!*p || !proc_sel(*p, which, who))
  99                         continue;
 100                 if ((*p)->priority > max_prio)
 101                         max_prio = (*p)->priority;
 102         }
 103         return(max_prio ? max_prio : -ESRCH);
 104 }
 105 
 106 asmlinkage int sys_profil(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108         return -ENOSYS;
 109 }
 110 
 111 asmlinkage int sys_ftime(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 112 {
 113         return -ENOSYS;
 114 }
 115 
 116 asmlinkage int sys_break(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118         return -ENOSYS;
 119 }
 120 
 121 asmlinkage int sys_stty(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 122 {
 123         return -ENOSYS;
 124 }
 125 
 126 asmlinkage int sys_gtty(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 127 {
 128         return -ENOSYS;
 129 }
 130 
 131 asmlinkage int sys_prof(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133         return -ENOSYS;
 134 }
 135 
 136 asmlinkage unsigned long save_v86_state(struct vm86_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 137 {
 138         unsigned long stack;
 139 
 140         if (!current->vm86_info) {
 141                 printk("no vm86_info: BAD\n");
 142                 do_exit(SIGSEGV);
 143         }
 144         memcpy_tofs(&(current->vm86_info->regs),regs,sizeof(*regs));
 145         put_fs_long(current->screen_bitmap,&(current->vm86_info->screen_bitmap));
 146         stack = current->tss.esp0;
 147         current->tss.esp0 = current->saved_kernel_stack;
 148         current->saved_kernel_stack = 0;
 149         return stack;
 150 }
 151 
 152 static void mark_screen_rdonly(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 153 {
 154         unsigned long tmp;
 155         unsigned long *pg_table;
 156 
 157         if ((tmp = tsk->tss.cr3) != 0) {
 158                 tmp = *(unsigned long *) tmp;
 159                 if (tmp & PAGE_PRESENT) {
 160                         tmp &= PAGE_MASK;
 161                         pg_table = (0xA0000 >> PAGE_SHIFT) + (unsigned long *) tmp;
 162                         tmp = 32;
 163                         while (tmp--) {
 164                                 if (PAGE_PRESENT & *pg_table)
 165                                         *pg_table &= ~PAGE_RW;
 166                                 pg_table++;
 167                         }
 168                 }
 169         }
 170 }
 171 
 172 asmlinkage int sys_vm86(struct vm86_struct * v86)
     /* [previous][next][first][last][top][bottom][index][help] */
 173 {
 174         struct vm86_struct info;
 175         struct pt_regs * pt_regs = (struct pt_regs *) &v86;
 176 
 177         if (current->saved_kernel_stack)
 178                 return -EPERM;
 179         memcpy_fromfs(&info,v86,sizeof(info));
 180 /*
 181  * make sure the vm86() system call doesn't try to do anything silly
 182  */
 183         info.regs.__null_ds = 0;
 184         info.regs.__null_es = 0;
 185         info.regs.__null_fs = 0;
 186         info.regs.__null_gs = 0;
 187 /*
 188  * The eflags register is also special: we cannot trust that the user
 189  * has set it up safely, so this makes sure interrupt etc flags are
 190  * inherited from protected mode.
 191  */
 192         info.regs.eflags &= 0x00000dd5;
 193         info.regs.eflags |= ~0x00000dd5 & pt_regs->eflags;
 194         info.regs.eflags |= VM_MASK;
 195         current->saved_kernel_stack = current->tss.esp0;
 196         current->tss.esp0 = (unsigned long) pt_regs;
 197         current->vm86_info = v86;
 198         current->screen_bitmap = info.screen_bitmap;
 199         if (info.flags & VM86_SCREEN_BITMAP)
 200                 mark_screen_rdonly(current);
 201         __asm__ __volatile__("movl %0,%%esp\n\t"
 202                 "pushl $ret_from_sys_call\n\t"
 203                 "ret"
 204                 : /* no outputs */
 205                 :"g" ((long) &(info.regs)),"a" (info.regs.eax));
 206         return 0;
 207 }
 208 
 209 extern void hard_reset_now(void);
 210 
 211 /*
 212  * Reboot system call: for obvious reasons only root may call it,
 213  * and even root needs to set up some magic numbers in the registers
 214  * so that some mistake won't make this reboot the whole machine.
 215  * You can also set the meaning of the ctrl-alt-del-key here.
 216  *
 217  * reboot doesn't sync: do that yourself before calling this.
 218  */
 219 asmlinkage int sys_reboot(int magic, int magic_too, int flag)
     /* [previous][next][first][last][top][bottom][index][help] */
 220 {
 221         if (!suser())
 222                 return -EPERM;
 223         if (magic != 0xfee1dead || magic_too != 672274793)
 224                 return -EINVAL;
 225         if (flag == 0x01234567)
 226                 hard_reset_now();
 227         else if (flag == 0x89ABCDEF)
 228                 C_A_D = 1;
 229         else if (!flag)
 230                 C_A_D = 0;
 231         else
 232                 return -EINVAL;
 233         return (0);
 234 }
 235 
 236 /*
 237  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
 238  * As it's called within an interrupt, it may NOT sync: the only choice
 239  * is wether to reboot at once, or just ignore the ctrl-alt-del.
 240  */
 241 void ctrl_alt_del(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 242 {
 243         if (C_A_D)
 244                 hard_reset_now();
 245         else
 246                 send_sig(SIGINT,task[1],1);
 247 }
 248         
 249 
 250 /*
 251  * This is done BSD-style, with no consideration of the saved gid, except
 252  * that if you set the effective gid, it sets the saved gid too.  This 
 253  * makes it possible for a setgid program to completely drop its privileges,
 254  * which is often a useful assertion to make when you are doing a security
 255  * audit over a program.
 256  *
 257  * The general idea is that a program which uses just setregid() will be
 258  * 100% compatible with BSD.  A program which uses just setgid() will be
 259  * 100% compatible with POSIX w/ Saved ID's. 
 260  */
 261 asmlinkage int sys_setregid(gid_t rgid, gid_t egid)
     /* [previous][next][first][last][top][bottom][index][help] */
 262 {
 263         int old_rgid = current->gid;
 264 
 265         if (rgid != (gid_t) -1) {
 266                 if ((current->egid==rgid) ||
 267                     (old_rgid == rgid) || 
 268                     suser())
 269                         current->gid = rgid;
 270                 else
 271                         return(-EPERM);
 272         }
 273         if (egid != (gid_t) -1) {
 274                 if ((old_rgid == egid) ||
 275                     (current->egid == egid) ||
 276                     suser()) {
 277                         current->egid = egid;
 278                         current->sgid = egid;
 279                 } else {
 280                         current->gid = old_rgid;
 281                         return(-EPERM);
 282                 }
 283         }
 284         return 0;
 285 }
 286 
 287 /*
 288  * setgid() is implemeneted like SysV w/ SAVED_IDS 
 289  */
 290 asmlinkage int sys_setgid(gid_t gid)
     /* [previous][next][first][last][top][bottom][index][help] */
 291 {
 292         if (suser())
 293                 current->gid = current->egid = current->sgid = gid;
 294         else if ((gid == current->gid) || (gid == current->sgid))
 295                 current->egid = gid;
 296         else
 297                 return -EPERM;
 298         return 0;
 299 }
 300 
 301 asmlinkage int sys_acct(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 302 {
 303         return -ENOSYS;
 304 }
 305 
 306 asmlinkage int sys_phys(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 307 {
 308         return -ENOSYS;
 309 }
 310 
 311 asmlinkage int sys_lock(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 312 {
 313         return -ENOSYS;
 314 }
 315 
 316 asmlinkage int sys_mpx(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 317 {
 318         return -ENOSYS;
 319 }
 320 
 321 asmlinkage int sys_ulimit(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 322 {
 323         return -ENOSYS;
 324 }
 325 
 326 asmlinkage int sys_old_syscall(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 327 {
 328         return -ENOSYS;
 329 }
 330 
 331 asmlinkage int sys_time(long * tloc)
     /* [previous][next][first][last][top][bottom][index][help] */
 332 {
 333         int i, error;
 334 
 335         i = CURRENT_TIME;
 336         if (tloc) {
 337                 error = verify_area(VERIFY_WRITE, tloc, 4);
 338                 if (error)
 339                         return error;
 340                 put_fs_long(i,(unsigned long *)tloc);
 341         }
 342         return i;
 343 }
 344 
 345 /*
 346  * Unprivileged users may change the real user id to the effective uid
 347  * or vice versa.  (BSD-style)
 348  *
 349  * When you set the effective uid, it sets the saved uid too.  This 
 350  * makes it possible for a setuid program to completely drop its privileges,
 351  * which is often a useful assertion to make when you are doing a security
 352  * audit over a program.
 353  *
 354  * The general idea is that a program which uses just setreuid() will be
 355  * 100% compatible with BSD.  A program which uses just setuid() will be
 356  * 100% compatible with POSIX w/ Saved ID's. 
 357  */
 358 asmlinkage int sys_setreuid(uid_t ruid, uid_t euid)
     /* [previous][next][first][last][top][bottom][index][help] */
 359 {
 360         int old_ruid = current->uid;
 361         
 362         if (ruid != (uid_t) -1) {
 363                 if ((current->euid==ruid) ||
 364                     (old_ruid == ruid) ||
 365                     suser())
 366                         current->uid = ruid;
 367                 else
 368                         return(-EPERM);
 369         }
 370         if (euid != (uid_t) -1) {
 371                 if ((old_ruid == euid) ||
 372                     (current->euid == euid) ||
 373                     suser()) {
 374                         current->euid = euid;
 375                         current->suid = euid;
 376                 } else {
 377                         current->uid = old_ruid;
 378                         return(-EPERM);
 379                 }
 380         }
 381         return 0;
 382 }
 383 
 384 /*
 385  * setuid() is implemeneted like SysV w/ SAVED_IDS 
 386  * 
 387  * Note that SAVED_ID's is deficient in that a setuid root program
 388  * like sendmail, for example, cannot set its uid to be a normal 
 389  * user and then switch back, because if you're root, setuid() sets
 390  * the saved uid too.  If you don't like this, blame the bright people
 391  * in the POSIX commmittee and/or USG.  Note that the BSD-style setreuid()
 392  * will allow a root program to temporarily drop privileges and be able to
 393  * regain them by swapping the real and effective uid.  
 394  */
 395 asmlinkage int sys_setuid(uid_t uid)
     /* [previous][next][first][last][top][bottom][index][help] */
 396 {
 397         if (suser())
 398                 current->uid = current->euid = current->suid = uid;
 399         else if ((uid == current->uid) || (uid == current->suid))
 400                 current->euid = uid;
 401         else
 402                 return -EPERM;
 403         return(0);
 404 }
 405 
 406 asmlinkage int sys_stime(long * tptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 407 {
 408         if (!suser())
 409                 return -EPERM;
 410         startup_time = get_fs_long((unsigned long *)tptr) - jiffies/HZ;
 411         jiffies_offset = 0;
 412         return 0;
 413 }
 414 
 415 asmlinkage int sys_times(struct tms * tbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 416 {
 417         if (tbuf) {
 418                 int error = verify_area(VERIFY_WRITE,tbuf,sizeof *tbuf);
 419                 if (error)
 420                         return error;
 421                 put_fs_long(current->utime,(unsigned long *)&tbuf->tms_utime);
 422                 put_fs_long(current->stime,(unsigned long *)&tbuf->tms_stime);
 423                 put_fs_long(current->cutime,(unsigned long *)&tbuf->tms_cutime);
 424                 put_fs_long(current->cstime,(unsigned long *)&tbuf->tms_cstime);
 425         }
 426         return jiffies;
 427 }
 428 
 429 asmlinkage int sys_brk(unsigned long brk)
     /* [previous][next][first][last][top][bottom][index][help] */
 430 {
 431         int freepages;
 432         unsigned long rlim;
 433         unsigned long newbrk, oldbrk;
 434 
 435         if (brk < current->end_code)
 436                 return current->brk;
 437         newbrk = PAGE_ALIGN(brk);
 438         oldbrk = PAGE_ALIGN(current->brk);
 439         /*
 440          * Always allow shrinking brk
 441          */
 442         if (brk <= current->brk) {
 443                 current->brk = brk;
 444                 unmap_page_range(newbrk, oldbrk-newbrk);
 445                 return brk;
 446         }
 447         /*
 448          * Check against rlimit and stack..
 449          */
 450         rlim = current->rlim[RLIMIT_DATA].rlim_cur;
 451         if (rlim >= RLIM_INFINITY)
 452                 rlim = ~0;
 453         if (brk - current->end_code > rlim || brk >= current->start_stack - 16384)
 454                 return current->brk;
 455         /*
 456          * stupid algorithm to decide if we have enough memory: while
 457          * simple, it hopefully works in most obvious cases.. Easy to
 458          * fool it, but this should catch most mistakes.
 459          */
 460         freepages = buffermem >> 12;
 461         freepages += nr_free_pages;
 462         freepages += nr_swap_pages;
 463         freepages -= (high_memory - 0x100000) >> 16;
 464         freepages -= (newbrk-oldbrk) >> 12;
 465         if (freepages < 0)
 466                 return current->brk;
 467 #if 0
 468         freepages += current->rss;
 469         freepages -= oldbrk >> 12;
 470         if (freepages < 0)
 471                 return current->brk;
 472 #endif
 473         /*
 474          * Ok, we have probably got enough memory - let it rip.
 475          */
 476         current->brk = brk;
 477         zeromap_page_range(oldbrk, newbrk-oldbrk, PAGE_COPY);
 478         return brk;
 479 }
 480 
 481 /*
 482  * This needs some heave checking ...
 483  * I just haven't get the stomach for it. I also don't fully
 484  * understand sessions/pgrp etc. Let somebody who does explain it.
 485  *
 486  * OK, I think I have the protection semantics right.... this is really
 487  * only important on a multi-user system anyway, to make sure one user
 488  * can't send a signal to a process owned by another.  -TYT, 12/12/91
 489  */
 490 asmlinkage int sys_setpgid(pid_t pid, pid_t pgid)
     /* [previous][next][first][last][top][bottom][index][help] */
 491 {
 492         int i; 
 493 
 494         if (!pid)
 495                 pid = current->pid;
 496         if (!pgid)
 497                 pgid = current->pid;
 498         if (pgid < 0)
 499                 return -EINVAL;
 500         for (i=0 ; i<NR_TASKS ; i++)
 501                 if (task[i] && (task[i]->pid == pid) &&
 502                     ((task[i]->p_pptr == current) || 
 503                      (task[i] == current))) {
 504                         if (task[i]->leader)
 505                                 return -EPERM;
 506                         if ((task[i]->session != current->session) ||
 507                             ((pgid != pid) && 
 508                              (session_of_pgrp(pgid) != current->session)))
 509                                 return -EPERM;
 510                         task[i]->pgrp = pgid;
 511                         return 0;
 512                 }
 513         return -ESRCH;
 514 }
 515 
 516 asmlinkage int sys_getpgrp(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 517 {
 518         return current->pgrp;
 519 }
 520 
 521 asmlinkage int sys_setsid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 522 {
 523         if (current->leader && !suser())
 524                 return -EPERM;
 525         current->leader = 1;
 526         current->session = current->pgrp = current->pid;
 527         current->tty = -1;
 528         return current->pgrp;
 529 }
 530 
 531 /*
 532  * Supplementary group ID's
 533  */
 534 asmlinkage int sys_getgroups(int gidsetsize, gid_t *grouplist)
     /* [previous][next][first][last][top][bottom][index][help] */
 535 {
 536         int i;
 537 
 538         if (gidsetsize) {
 539                 i = verify_area(VERIFY_WRITE, grouplist, sizeof(gid_t) * gidsetsize);
 540                 if (i)
 541                         return i;
 542         }
 543         for (i = 0 ; (i < NGROUPS) && (current->groups[i] != NOGROUP) ; i++) {
 544                 if (!gidsetsize)
 545                         continue;
 546                 if (i >= gidsetsize)
 547                         break;
 548                 put_fs_word(current->groups[i], (short *) grouplist);
 549                 grouplist++;
 550         }
 551         return(i);
 552 }
 553 
 554 asmlinkage int sys_setgroups(int gidsetsize, gid_t *grouplist)
     /* [previous][next][first][last][top][bottom][index][help] */
 555 {
 556         int     i;
 557 
 558         if (!suser())
 559                 return -EPERM;
 560         if (gidsetsize > NGROUPS)
 561                 return -EINVAL;
 562         for (i = 0; i < gidsetsize; i++, grouplist++) {
 563                 current->groups[i] = get_fs_word((unsigned short *) grouplist);
 564         }
 565         if (i < NGROUPS)
 566                 current->groups[i] = NOGROUP;
 567         return 0;
 568 }
 569 
 570 int in_group_p(gid_t grp)
     /* [previous][next][first][last][top][bottom][index][help] */
 571 {
 572         int     i;
 573 
 574         if (grp == current->egid)
 575                 return 1;
 576 
 577         for (i = 0; i < NGROUPS; i++) {
 578                 if (current->groups[i] == NOGROUP)
 579                         break;
 580                 if (current->groups[i] == grp)
 581                         return 1;
 582         }
 583         return 0;
 584 }
 585 
 586 asmlinkage int sys_newuname(struct new_utsname * name)
     /* [previous][next][first][last][top][bottom][index][help] */
 587 {
 588         int error;
 589 
 590         if (!name)
 591                 return -EFAULT;
 592         error = verify_area(VERIFY_WRITE, name, sizeof *name);
 593         if (!error)
 594                 memcpy_tofs(name,&system_utsname,sizeof *name);
 595         return error;
 596 }
 597 
 598 asmlinkage int sys_uname(struct old_utsname * name)
     /* [previous][next][first][last][top][bottom][index][help] */
 599 {
 600         int error;
 601         if (!name)
 602                 return -EFAULT;
 603         error = verify_area(VERIFY_WRITE, name,sizeof *name);
 604         if (error)
 605                 return error;
 606         memcpy_tofs(&name->sysname,&system_utsname.sysname,
 607                 sizeof (system_utsname.sysname));
 608         memcpy_tofs(&name->nodename,&system_utsname.nodename,
 609                 sizeof (system_utsname.nodename));
 610         memcpy_tofs(&name->release,&system_utsname.release,
 611                 sizeof (system_utsname.release));
 612         memcpy_tofs(&name->version,&system_utsname.version,
 613                 sizeof (system_utsname.version));
 614         memcpy_tofs(&name->machine,&system_utsname.machine,
 615                 sizeof (system_utsname.machine));
 616         return 0;
 617 }
 618 
 619 asmlinkage int sys_olduname(struct oldold_utsname * name)
     /* [previous][next][first][last][top][bottom][index][help] */
 620 {
 621         int error;
 622         if (!name)
 623                 return -EFAULT;
 624         error = verify_area(VERIFY_WRITE, name,sizeof *name);
 625         if (error)
 626                 return error;
 627         memcpy_tofs(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
 628         put_fs_byte(0,name->sysname+__OLD_UTS_LEN);
 629         memcpy_tofs(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
 630         put_fs_byte(0,name->nodename+__OLD_UTS_LEN);
 631         memcpy_tofs(&name->release,&system_utsname.release,__OLD_UTS_LEN);
 632         put_fs_byte(0,name->release+__OLD_UTS_LEN);
 633         memcpy_tofs(&name->version,&system_utsname.version,__OLD_UTS_LEN);
 634         put_fs_byte(0,name->version+__OLD_UTS_LEN);
 635         memcpy_tofs(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
 636         put_fs_byte(0,name->machine+__OLD_UTS_LEN);
 637         return 0;
 638 }
 639 
 640 /*
 641  * Only sethostname; gethostname can be implemented by calling uname()
 642  */
 643 asmlinkage int sys_sethostname(char *name, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 644 {
 645         int     i;
 646         
 647         if (!suser())
 648                 return -EPERM;
 649         if (len > __NEW_UTS_LEN)
 650                 return -EINVAL;
 651         for (i=0; i < len; i++) {
 652                 if ((system_utsname.nodename[i] = get_fs_byte(name+i)) == 0)
 653                         return 0;
 654         }
 655         system_utsname.nodename[i] = 0;
 656         return 0;
 657 }
 658 
 659 /*
 660  * Only setdomainname; getdomainname can be implemented by calling
 661  * uname()
 662  */
 663 asmlinkage int sys_setdomainname(char *name, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 664 {
 665         int     i;
 666         
 667         if (!suser())
 668                 return -EPERM;
 669         if (len > __NEW_UTS_LEN)
 670                 return -EINVAL;
 671         for (i=0; i < len; i++) {
 672                 if ((system_utsname.domainname[i] = get_fs_byte(name+i)) == 0)
 673                         return 0;
 674         }
 675         system_utsname.domainname[i] = 0;
 676         return 0;
 677 }
 678 
 679 asmlinkage int sys_getrlimit(unsigned int resource, struct rlimit *rlim)
     /* [previous][next][first][last][top][bottom][index][help] */
 680 {
 681         int error;
 682 
 683         if (resource >= RLIM_NLIMITS)
 684                 return -EINVAL;
 685         error = verify_area(VERIFY_WRITE,rlim,sizeof *rlim);
 686         if (error)
 687                 return error;
 688         put_fs_long(current->rlim[resource].rlim_cur, 
 689                     (unsigned long *) rlim);
 690         put_fs_long(current->rlim[resource].rlim_max, 
 691                     ((unsigned long *) rlim)+1);
 692         return 0;       
 693 }
 694 
 695 asmlinkage int sys_setrlimit(unsigned int resource, struct rlimit *rlim)
     /* [previous][next][first][last][top][bottom][index][help] */
 696 {
 697         struct rlimit new_rlim, *old_rlim;
 698 
 699         if (resource >= RLIM_NLIMITS)
 700                 return -EINVAL;
 701         old_rlim = current->rlim + resource;
 702         new_rlim.rlim_cur = get_fs_long((unsigned long *) rlim);
 703         new_rlim.rlim_max = get_fs_long(((unsigned long *) rlim)+1);
 704         if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
 705              (new_rlim.rlim_max > old_rlim->rlim_max)) &&
 706             !suser())
 707                 return -EPERM;
 708         *old_rlim = new_rlim;
 709         return 0;
 710 }
 711 
 712 /*
 713  * It would make sense to put struct rusuage in the task_struct,
 714  * except that would make the task_struct be *really big*.  After
 715  * task_struct gets moved into malloc'ed memory, it would
 716  * make sense to do this.  It will make moving the rest of the information
 717  * a lot simpler!  (Which we're not doing right now because we're not
 718  * measuring them yet).
 719  */
 720 int getrusage(struct task_struct *p, int who, struct rusage *ru)
     /* [previous][next][first][last][top][bottom][index][help] */
 721 {
 722         int error;
 723         struct rusage r;
 724         unsigned long   *lp, *lpend, *dest;
 725 
 726         error = verify_area(VERIFY_WRITE, ru, sizeof *ru);
 727         if (error)
 728                 return error;
 729         memset((char *) &r, 0, sizeof(r));
 730         switch (who) {
 731                 case RUSAGE_SELF:
 732                         r.ru_utime.tv_sec = CT_TO_SECS(p->utime);
 733                         r.ru_utime.tv_usec = CT_TO_USECS(p->utime);
 734                         r.ru_stime.tv_sec = CT_TO_SECS(p->stime);
 735                         r.ru_stime.tv_usec = CT_TO_USECS(p->stime);
 736                         r.ru_minflt = p->min_flt;
 737                         r.ru_majflt = p->maj_flt;
 738                         break;
 739                 case RUSAGE_CHILDREN:
 740                         r.ru_utime.tv_sec = CT_TO_SECS(p->cutime);
 741                         r.ru_utime.tv_usec = CT_TO_USECS(p->cutime);
 742                         r.ru_stime.tv_sec = CT_TO_SECS(p->cstime);
 743                         r.ru_stime.tv_usec = CT_TO_USECS(p->cstime);
 744                         r.ru_minflt = p->cmin_flt;
 745                         r.ru_majflt = p->cmaj_flt;
 746                         break;
 747                 default:
 748                         r.ru_utime.tv_sec = CT_TO_SECS(p->utime + p->cutime);
 749                         r.ru_utime.tv_usec = CT_TO_USECS(p->utime + p->cutime);
 750                         r.ru_stime.tv_sec = CT_TO_SECS(p->stime + p->cstime);
 751                         r.ru_stime.tv_usec = CT_TO_USECS(p->stime + p->cstime);
 752                         r.ru_minflt = p->min_flt + p->cmin_flt;
 753                         r.ru_majflt = p->maj_flt + p->cmaj_flt;
 754                         break;
 755         }
 756         lp = (unsigned long *) &r;
 757         lpend = (unsigned long *) (&r+1);
 758         dest = (unsigned long *) ru;
 759         for (; lp < lpend; lp++, dest++) 
 760                 put_fs_long(*lp, dest);
 761         return 0;
 762 }
 763 
 764 asmlinkage int sys_getrusage(int who, struct rusage *ru)
     /* [previous][next][first][last][top][bottom][index][help] */
 765 {
 766         if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
 767                 return -EINVAL;
 768         return getrusage(current, who, ru);
 769 }
 770 
 771 #define LATCH ((1193180 + HZ/2)/HZ)
 772 
 773 /*
 774  * This version of gettimeofday has near microsecond resolution.
 775  * It was inspired by Steve McCanne's microtime-i386 for BSD.  -- jrs
 776  */
 777 static inline void do_gettimeofday(struct timeval *tv)
     /* [previous][next][first][last][top][bottom][index][help] */
 778 {
 779         unsigned long nowtime;
 780         long count;
 781 
 782 #ifdef __i386__
 783         cli();
 784         /* timer count may underflow right here */
 785         outb_p(0x00, 0x43);     /* latch the count ASAP */
 786         nowtime = jiffies;      /* must be saved inside cli/sti */
 787         count = inb_p(0x40);    /* read the latched count */
 788         count |= inb_p(0x40) << 8;
 789         /* we know probability of underflow is always MUCH less than 1% */
 790         if (count < (LATCH - LATCH/100))
 791                 sti();
 792         else {
 793                 /* check for pending timer interrupt */
 794                 outb_p(0x0a, 0x20);
 795                 if (inb(0x20) & 1)
 796                         nowtime++;
 797                 sti();
 798         }
 799         nowtime += jiffies_offset;
 800         tv->tv_sec = startup_time + CT_TO_SECS(nowtime);
 801         /* the correction term is always in the range [0, 1) clocktick */
 802         tv->tv_usec = CT_TO_USECS(nowtime)
 803                 + ((LATCH - 1) - count)*(1000000/HZ)/LATCH;
 804 #else /* not __i386__ */
 805         nowtime = jiffies + jiffes_offset;
 806         tv->tv_sec = startup_time + CT_TO_SECS(nowtime);
 807         tv->tv_usec = CT_TO_USECS(nowtime);
 808 #endif /* not __i386__ */
 809 }
 810 
 811 asmlinkage int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
     /* [previous][next][first][last][top][bottom][index][help] */
 812 {
 813         int error;
 814 
 815         if (tv) {
 816                 struct timeval ktv;
 817                 error = verify_area(VERIFY_WRITE, tv, sizeof *tv);
 818                 if (error)
 819                         return error;
 820                 do_gettimeofday(&ktv);
 821                 put_fs_long(ktv.tv_sec, (unsigned long *) &tv->tv_sec);
 822                 put_fs_long(ktv.tv_usec, (unsigned long *) &tv->tv_usec);
 823         }
 824         if (tz) {
 825                 error = verify_area(VERIFY_WRITE, tz, sizeof *tz);
 826                 if (error)
 827                         return error;
 828                 put_fs_long(sys_tz.tz_minuteswest, (unsigned long *) tz);
 829                 put_fs_long(sys_tz.tz_dsttime, ((unsigned long *) tz)+1);
 830         }
 831         return 0;
 832 }
 833 
 834 /*
 835  * The first time we set the timezone, we will warp the clock so that
 836  * it is ticking GMT time instead of local time.  Presumably, 
 837  * if someone is setting the timezone then we are running in an
 838  * environment where the programs understand about timezones.
 839  * This should be done at boot time in the /etc/rc script, as
 840  * soon as possible, so that the clock can be set right.  Otherwise,
 841  * various programs will get confused when the clock gets warped.
 842  */
 843 asmlinkage int sys_settimeofday(struct timeval *tv, struct timezone *tz)
     /* [previous][next][first][last][top][bottom][index][help] */
 844 {
 845         static int      firsttime = 1;
 846 
 847         if (!suser())
 848                 return -EPERM;
 849         if (tz) {
 850                 sys_tz.tz_minuteswest = get_fs_long((unsigned long *) tz);
 851                 sys_tz.tz_dsttime = get_fs_long(((unsigned long *) tz)+1);
 852                 if (firsttime) {
 853                         firsttime = 0;
 854                         if (!tv)
 855                                 adjust_clock();
 856                 }
 857         }
 858         if (tv) {
 859                 int sec, usec;
 860 
 861                 sec = get_fs_long((unsigned long *)tv);
 862                 usec = get_fs_long(((unsigned long *)tv)+1);
 863         
 864                 startup_time = sec - jiffies/HZ;
 865                 jiffies_offset = usec * HZ / 1000000 - jiffies%HZ;
 866         }
 867         return 0;
 868 }
 869 
 870 /*
 871  * Adjust the time obtained from the CMOS to be GMT time instead of
 872  * local time.
 873  * 
 874  * This is ugly, but preferable to the alternatives.  Otherwise we
 875  * would either need to write a program to do it in /etc/rc (and risk
 876  * confusion if the program gets run more than once; it would also be 
 877  * hard to make the program warp the clock precisely n hours)  or
 878  * compile in the timezone information into the kernel.  Bad, bad....
 879  *
 880  * XXX Currently does not adjust for daylight savings time.  May not
 881  * need to do anything, depending on how smart (dumb?) the BIOS
 882  * is.  Blast it all.... the best thing to do not depend on the CMOS
 883  * clock at all, but get the time via NTP or timed if you're on a 
 884  * network....                          - TYT, 1/1/92
 885  */
 886 void adjust_clock(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 887 {
 888         startup_time += sys_tz.tz_minuteswest*60;
 889 }
 890 
 891 asmlinkage int sys_umask(int mask)
     /* [previous][next][first][last][top][bottom][index][help] */
 892 {
 893         int old = current->umask;
 894 
 895         current->umask = mask & S_IRWXUGO;
 896         return (old);
 897 }

/* [previous][next][first][last][top][bottom][index][help] */