#include "lint.h"
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/procset.h>
#include <sys/priocntl.h>
#include <limits.h>
#include <errno.h>
#include <priv.h>
static idtype_t
prio_to_idtype(int which)
{
switch (which) {
case PRIO_PROCESS:
return (P_PID);
case PRIO_PGRP:
return (P_PGID);
case PRIO_USER:
return (P_UID);
case PRIO_GROUP:
return (P_GID);
case PRIO_SESSION:
return (P_SID);
case PRIO_LWP:
return (P_LWPID);
case PRIO_TASK:
return (P_TASKID);
case PRIO_PROJECT:
return (P_PROJID);
case PRIO_ZONE:
return (P_ZONEID);
case PRIO_CONTRACT:
return (P_CTID);
default:
return (-1);
}
}
static int
old_idtype(int which)
{
switch (which) {
case PRIO_PROCESS:
case PRIO_PGRP:
case PRIO_USER:
return (1);
default:
return (0);
}
}
int
getpriority(int which, id_t who)
{
id_t id;
idtype_t idtype;
pcnice_t pcnice;
if ((idtype = prio_to_idtype(which)) == -1) {
errno = EINVAL;
return (-1);
}
if (who < 0) {
if (old_idtype(which)) {
errno = EINVAL;
return (-1);
} else if (who != P_MYID) {
errno = EINVAL;
return (-1);
}
}
if (who == 0 && old_idtype(which))
id = P_MYID;
else
id = who;
pcnice.pc_val = 0;
pcnice.pc_op = PC_GETNICE;
if (priocntl(idtype, id, PC_DONICE, &pcnice) == -1)
return (-1);
else
return (pcnice.pc_val);
}
int
setpriority(int which, id_t who, int prio)
{
id_t id;
idtype_t idtype;
pcnice_t pcnice;
int ret;
if ((idtype = prio_to_idtype(which)) == -1) {
errno = EINVAL;
return (-1);
}
if (who < 0) {
if (old_idtype(which)) {
errno = EINVAL;
return (-1);
} else if (who != P_MYID) {
errno = EINVAL;
return (-1);
}
}
if (who == 0 && old_idtype(which))
id = P_MYID;
else
id = who;
if (prio > NZERO - 1)
prio = NZERO - 1;
else if (prio < -NZERO)
prio = -NZERO;
pcnice.pc_val = prio;
pcnice.pc_op = PC_SETNICE;
ret = priocntl(idtype, id, PC_DONICE, &pcnice);
if (ret != 0 && errno == EPERM) {
pcnice_t gpcnice = { 0, PC_GETNICE };
priv_set_t *pset = NULL;
if (priocntl(idtype, id, PC_DONICE, &gpcnice) == 0 &&
prio < gpcnice.pc_val) {
if ((pset = priv_allocset()) != NULL &&
getppriv(PRIV_EFFECTIVE, pset) == 0 &&
!priv_ismember(pset, "proc_priocntl"))
errno = EACCES;
if (pset != NULL)
priv_freeset(pset);
}
}
return (ret);
}