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

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