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

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