do not panic after modified data found on the freelist
[unix-history] / usr / src / sys / kern / kern_resource.c
index 20275db..47cb6f4 100644 (file)
@@ -1,26 +1,31 @@
-/*
- * Copyright (c) 1982, 1986 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+/*-
+ * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)kern_resource.c     7.4 (Berkeley) %G%
+ *     @(#)kern_resource.c     7.13 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
-#include "dir.h"
-#include "user.h"
+#include "resourcevar.h"
+#include "malloc.h"
 #include "proc.h"
 
 #include "proc.h"
 
+#include "vm/vm.h"
+
 /*
  * Resource controls and accounting.
  */
 
 /*
  * Resource controls and accounting.
  */
 
-getpriority()
-{
-       register struct a {
+getpriority(curp, uap, retval)
+       struct proc *curp;
+       register struct args {
                int     which;
                int     who;
                int     which;
                int     who;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        register struct proc *p;
        register int low = PRIO_MAX + 1;
 
        register struct proc *p;
        register int low = PRIO_MAX + 1;
 
@@ -28,7 +33,7 @@ getpriority()
 
        case PRIO_PROCESS:
                if (uap->who == 0)
 
        case PRIO_PROCESS:
                if (uap->who == 0)
-                       p = u.u_procp;
+                       p = curp;
                else
                        p = pfind(uap->who);
                if (p == 0)
                else
                        p = pfind(uap->who);
                if (p == 0)
@@ -40,7 +45,7 @@ getpriority()
                register struct pgrp *pg;
 
                if (uap->who == 0)
                register struct pgrp *pg;
 
                if (uap->who == 0)
-                       pg = u.u_procp->p_pgrp;
+                       pg = curp->p_pgrp;
                else if ((pg = pgfind(uap->who)) == NULL)
                        break;
                for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) {
                else if ((pg = pgfind(uap->who)) == NULL)
                        break;
                for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) {
@@ -52,45 +57,46 @@ getpriority()
 
        case PRIO_USER:
                if (uap->who == 0)
 
        case PRIO_USER:
                if (uap->who == 0)
-                       uap->who = u.u_uid;
+                       uap->who = curp->p_ucred->cr_uid;
                for (p = allproc; p != NULL; p = p->p_nxt) {
                for (p = allproc; p != NULL; p = p->p_nxt) {
-                       if (p->p_uid == uap->who &&
+                       if (p->p_ucred->cr_uid == uap->who &&
                            p->p_nice < low)
                                low = p->p_nice;
                }
                break;
 
        default:
                            p->p_nice < low)
                                low = p->p_nice;
                }
                break;
 
        default:
-               u.u_error = EINVAL;
-               return;
+               return (EINVAL);
        }
        }
-       if (low == PRIO_MAX + 1) {
-               u.u_error = ESRCH;
-               return;
-       }
-       u.u_r.r_val1 = low;
+       if (low == PRIO_MAX + 1)
+               return (ESRCH);
+       *retval = low;
+       return (0);
 }
 
 }
 
-setpriority()
-{
-       register struct a {
+/* ARGSUSED */
+setpriority(curp, uap, retval)
+       struct proc *curp;
+       register struct args {
                int     which;
                int     who;
                int     prio;
                int     which;
                int     who;
                int     prio;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        register struct proc *p;
        register struct proc *p;
-       int found = 0;
+       int found = 0, error = 0;
 
        switch (uap->which) {
 
        case PRIO_PROCESS:
                if (uap->who == 0)
 
        switch (uap->which) {
 
        case PRIO_PROCESS:
                if (uap->who == 0)
-                       p = u.u_procp;
+                       p = curp;
                else
                        p = pfind(uap->who);
                if (p == 0)
                        break;
                else
                        p = pfind(uap->who);
                if (p == 0)
                        break;
-               donice(p, uap->prio);
+               error = donice(curp, p, uap->prio);
                found++;
                break;
 
                found++;
                break;
 
@@ -98,11 +104,11 @@ setpriority()
                register struct pgrp *pg;
                 
                if (uap->who == 0)
                register struct pgrp *pg;
                 
                if (uap->who == 0)
-                       pg = u.u_procp->p_pgrp;
+                       pg = curp->p_pgrp;
                else if ((pg = pgfind(uap->who)) == NULL)
                        break;
                for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) {
                else if ((pg = pgfind(uap->who)) == NULL)
                        break;
                for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) {
-                       donice(p, uap->prio);
+                       error = donice(curp, p, uap->prio);
                        found++;
                }
                break;
                        found++;
                }
                break;
@@ -110,66 +116,72 @@ setpriority()
 
        case PRIO_USER:
                if (uap->who == 0)
 
        case PRIO_USER:
                if (uap->who == 0)
-                       uap->who = u.u_uid;
+                       uap->who = curp->p_ucred->cr_uid;
                for (p = allproc; p != NULL; p = p->p_nxt)
                for (p = allproc; p != NULL; p = p->p_nxt)
-                       if (p->p_uid == uap->who) {
-                               donice(p, uap->prio);
+                       if (p->p_ucred->cr_uid == uap->who) {
+                               error = donice(curp, p, uap->prio);
                                found++;
                        }
                break;
 
        default:
                                found++;
                        }
                break;
 
        default:
-               u.u_error = EINVAL;
-               return;
+               return (EINVAL);
        }
        if (found == 0)
        }
        if (found == 0)
-               u.u_error = ESRCH;
+               return (ESRCH);
+       return (0);
 }
 
 }
 
-donice(p, n)
-       register struct proc *p;
+donice(curp, chgp, n)
+       register struct proc *curp, *chgp;
        register int n;
 {
        register int n;
 {
+       register struct pcred *pcred = curp->p_cred;
 
 
-       if (u.u_uid && u.u_ruid &&
-           u.u_uid != p->p_uid && u.u_ruid != p->p_uid) {
-               u.u_error = EPERM;
-               return;
-       }
+       if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
+           pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
+           pcred->p_ruid != chgp->p_ucred->cr_uid)
+               return (EPERM);
        if (n > PRIO_MAX)
                n = PRIO_MAX;
        if (n < PRIO_MIN)
                n = PRIO_MIN;
        if (n > PRIO_MAX)
                n = PRIO_MAX;
        if (n < PRIO_MIN)
                n = PRIO_MIN;
-       if (n < p->p_nice && suser(u.u_cred, &u.u_acflag)) {
-               u.u_error = EACCES;
-               return;
-       }
-       p->p_nice = n;
-       (void) setpri(p);
+       if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
+               return (EACCES);
+       chgp->p_nice = n;
+       (void) setpri(chgp);
+       return (0);
 }
 
 }
 
-setrlimit()
-{
-       register struct a {
+/* ARGSUSED */
+setrlimit(p, uap, retval)
+       struct proc *p;
+       register struct args {
                u_int   which;
                struct  rlimit *lim;
                u_int   which;
                struct  rlimit *lim;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct rlimit alim;
        register struct rlimit *alimp;
        extern unsigned maxdmap;
        struct rlimit alim;
        register struct rlimit *alimp;
        extern unsigned maxdmap;
+       int error;
 
 
-       if (uap->which >= RLIM_NLIMITS) {
-               u.u_error = EINVAL;
-               return;
-       }
-       alimp = &u.u_rlimit[uap->which];
-       u.u_error = copyin((caddr_t)uap->lim, (caddr_t)&alim,
-               sizeof (struct rlimit));
-       if (u.u_error)
-               return;
+       if (uap->which >= RLIM_NLIMITS)
+               return (EINVAL);
+       alimp = &p->p_rlimit[uap->which];
+       if (error =
+           copyin((caddr_t)uap->lim, (caddr_t)&alim, sizeof (struct rlimit)))
+               return (error);
        if (alim.rlim_cur > alimp->rlim_max || alim.rlim_max > alimp->rlim_max)
        if (alim.rlim_cur > alimp->rlim_max || alim.rlim_max > alimp->rlim_max)
-               if (u.u_error = suser(u.u_cred, &u.u_acflag))
-                       return;
+               if (error = suser(p->p_ucred, &p->p_acflag))
+                       return (error);
+       if (p->p_limit->p_refcnt > 1 &&
+           (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
+               p->p_limit->p_refcnt--;
+               p->p_limit = limcopy(p->p_limit);
+       }
+
        switch (uap->which) {
 
        case RLIMIT_DATA:
        switch (uap->which) {
 
        case RLIMIT_DATA:
@@ -184,52 +196,85 @@ setrlimit()
                        alim.rlim_cur = maxdmap;
                if (alim.rlim_max > maxdmap)
                        alim.rlim_max = maxdmap;
                        alim.rlim_cur = maxdmap;
                if (alim.rlim_max > maxdmap)
                        alim.rlim_max = maxdmap;
+               /*
+                * Stack is allocated to the max at exec time with only
+                * "rlim_cur" bytes accessible.  If stack limit is going
+                * up make more accessible, if going down make inaccessible.
+                */
+               if (alim.rlim_cur != alimp->rlim_cur) {
+                       vm_offset_t addr;
+                       vm_size_t size;
+                       vm_prot_t prot;
+
+                       if (alim.rlim_cur > alimp->rlim_cur) {
+                               prot = VM_PROT_ALL;
+                               size = alim.rlim_cur - alimp->rlim_cur;
+                               addr = USRSTACK - alim.rlim_cur;
+                       } else {
+                               prot = VM_PROT_NONE;
+                               size = alimp->rlim_cur - alim.rlim_cur;
+                               addr = USRSTACK - alimp->rlim_cur;
+                       }
+                       addr = trunc_page(addr);
+                       size = round_page(size);
+                       (void) vm_map_protect(&p->p_vmspace->vm_map,
+                                             addr, addr+size, prot, FALSE);
+               }
                break;
        }
                break;
        }
-       *alimp = alim;
-       if (uap->which == RLIMIT_RSS)
-               u.u_procp->p_maxrss = alim.rlim_cur/NBPG;
+       p->p_rlimit[uap->which] = alim;
+       return (0);
 }
 
 }
 
-getrlimit()
-{
-       register struct a {
+/* ARGSUSED */
+getrlimit(p, uap, retval)
+       struct proc *p;
+       register struct args {
                u_int   which;
                struct  rlimit *rlp;
                u_int   which;
                struct  rlimit *rlp;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
 
 
-       if (uap->which >= RLIM_NLIMITS) {
-               u.u_error = EINVAL;
-               return;
-       }
-       u.u_error = copyout((caddr_t)&u.u_rlimit[uap->which], (caddr_t)uap->rlp,
-           sizeof (struct rlimit));
+       if (uap->which >= RLIM_NLIMITS)
+               return (EINVAL);
+       return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
+           sizeof (struct rlimit)));
 }
 
 }
 
-getrusage()
-{
-       register struct a {
+/* ARGSUSED */
+getrusage(p, uap, retval)
+       register struct proc *p;
+       register struct args {
                int     who;
                struct  rusage *rusage;
                int     who;
                struct  rusage *rusage;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        register struct rusage *rup;
 
        switch (uap->who) {
 
        register struct rusage *rup;
 
        switch (uap->who) {
 
-       case RUSAGE_SELF:
-               rup = &u.u_ru;
+       case RUSAGE_SELF: {
+               int s;
+
+               rup = &p->p_stats->p_ru;
+               s = splclock();
+               rup->ru_stime = p->p_stime;
+               rup->ru_utime = p->p_utime;
+               splx(s);
                break;
                break;
+       }
 
        case RUSAGE_CHILDREN:
 
        case RUSAGE_CHILDREN:
-               rup = &u.u_cru;
+               rup = &p->p_stats->p_cru;
                break;
 
        default:
                break;
 
        default:
-               u.u_error = EINVAL;
-               return;
+               return (EINVAL);
        }
        }
-       u.u_error = copyout((caddr_t)rup, (caddr_t)uap->rusage,
-           sizeof (struct rusage));
+       return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
+           sizeof (struct rusage)));
 }
 
 ruadd(ru, ru2)
 }
 
 ruadd(ru, ru2)
@@ -246,3 +291,23 @@ ruadd(ru, ru2)
        for (i = &ru->ru_last - &ru->ru_first; i > 0; i--)
                *ip++ += *ip2++;
 }
        for (i = &ru->ru_last - &ru->ru_first; i > 0; i--)
                *ip++ += *ip2++;
 }
+
+/*
+ * Make a copy of the plimit structure.
+ * We share these structures copy-on-write after fork,
+ * and copy when a limit is changed.
+ */
+struct plimit *
+limcopy(lim)
+       struct plimit *lim;
+{
+       register struct plimit *copy;
+
+       MALLOC(copy, struct plimit *, sizeof(struct plimit),
+           M_SUBPROC, M_WAITOK);
+       bcopy(lim->pl_rlimit, copy->pl_rlimit,
+           sizeof(struct rlimit) * RLIM_NLIMITS);
+       copy->p_lflags = 0;
+       copy->p_refcnt = 1;
+       return (copy);
+}