17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
59acbbeafSnn35248 * Common Development and Distribution License (the "License").
69acbbeafSnn35248 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21d4204c85Sraf
227c478bd9Sstevel@tonic-gate /*
23d4204c85Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate * The Regents of the University of California
337c478bd9Sstevel@tonic-gate * All Rights Reserved
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate * contributors.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
40*7257d1b4Sraf #include "lint.h"
417c478bd9Sstevel@tonic-gate #include <string.h>
42d4204c85Sraf #include <limits.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/time.h>
457c478bd9Sstevel@tonic-gate #include <sys/resource.h>
467c478bd9Sstevel@tonic-gate #include <sys/procset.h>
477c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
489acbbeafSnn35248 #include <limits.h>
497c478bd9Sstevel@tonic-gate #include <errno.h>
509acbbeafSnn35248 #include <priv.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate static idtype_t
prio_to_idtype(int which)537c478bd9Sstevel@tonic-gate prio_to_idtype(int which)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate switch (which) {
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate case PRIO_PROCESS:
587c478bd9Sstevel@tonic-gate return (P_PID);
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate case PRIO_PGRP:
617c478bd9Sstevel@tonic-gate return (P_PGID);
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate case PRIO_USER:
647c478bd9Sstevel@tonic-gate return (P_UID);
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate case PRIO_GROUP:
677c478bd9Sstevel@tonic-gate return (P_GID);
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate case PRIO_SESSION:
707c478bd9Sstevel@tonic-gate return (P_SID);
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate case PRIO_LWP:
737c478bd9Sstevel@tonic-gate return (P_LWPID);
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate case PRIO_TASK:
767c478bd9Sstevel@tonic-gate return (P_TASKID);
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate case PRIO_PROJECT:
797c478bd9Sstevel@tonic-gate return (P_PROJID);
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate case PRIO_ZONE:
827c478bd9Sstevel@tonic-gate return (P_ZONEID);
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate case PRIO_CONTRACT:
857c478bd9Sstevel@tonic-gate return (P_CTID);
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate default:
887c478bd9Sstevel@tonic-gate return (-1);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate static int
old_idtype(int which)937c478bd9Sstevel@tonic-gate old_idtype(int which)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate switch (which) {
967c478bd9Sstevel@tonic-gate case PRIO_PROCESS:
977c478bd9Sstevel@tonic-gate case PRIO_PGRP:
987c478bd9Sstevel@tonic-gate case PRIO_USER:
997c478bd9Sstevel@tonic-gate return (1);
1007c478bd9Sstevel@tonic-gate default:
1017c478bd9Sstevel@tonic-gate return (0);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate int
getpriority(int which,id_t who)1067c478bd9Sstevel@tonic-gate getpriority(int which, id_t who)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate id_t id;
1097c478bd9Sstevel@tonic-gate idtype_t idtype;
1107c478bd9Sstevel@tonic-gate pcnice_t pcnice;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate if ((idtype = prio_to_idtype(which)) == -1) {
1137c478bd9Sstevel@tonic-gate errno = EINVAL;
1147c478bd9Sstevel@tonic-gate return (-1);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate if (who < 0) {
1187c478bd9Sstevel@tonic-gate if (old_idtype(which)) {
1197c478bd9Sstevel@tonic-gate errno = EINVAL;
1207c478bd9Sstevel@tonic-gate return (-1);
1217c478bd9Sstevel@tonic-gate } else if (who != P_MYID) {
1227c478bd9Sstevel@tonic-gate errno = EINVAL;
1237c478bd9Sstevel@tonic-gate return (-1);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate * The POSIX standard requires that a 0 value for the who argument
1297c478bd9Sstevel@tonic-gate * should specify the current process, process group, or user.
1307c478bd9Sstevel@tonic-gate * For all other id types we can treat zero as normal id value.
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate if (who == 0 && old_idtype(which))
1337c478bd9Sstevel@tonic-gate id = P_MYID;
1347c478bd9Sstevel@tonic-gate else
1357c478bd9Sstevel@tonic-gate id = who;
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate pcnice.pc_val = 0;
1387c478bd9Sstevel@tonic-gate pcnice.pc_op = PC_GETNICE;
1397c478bd9Sstevel@tonic-gate
140d4204c85Sraf if (priocntl(idtype, id, PC_DONICE, &pcnice) == -1)
1417c478bd9Sstevel@tonic-gate return (-1);
1427c478bd9Sstevel@tonic-gate else
1437c478bd9Sstevel@tonic-gate return (pcnice.pc_val);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate int
setpriority(int which,id_t who,int prio)1477c478bd9Sstevel@tonic-gate setpriority(int which, id_t who, int prio)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate id_t id;
1507c478bd9Sstevel@tonic-gate idtype_t idtype;
1517c478bd9Sstevel@tonic-gate pcnice_t pcnice;
1529acbbeafSnn35248 int ret;
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate if ((idtype = prio_to_idtype(which)) == -1) {
1557c478bd9Sstevel@tonic-gate errno = EINVAL;
1567c478bd9Sstevel@tonic-gate return (-1);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate if (who < 0) {
1607c478bd9Sstevel@tonic-gate if (old_idtype(which)) {
1617c478bd9Sstevel@tonic-gate errno = EINVAL;
1627c478bd9Sstevel@tonic-gate return (-1);
1637c478bd9Sstevel@tonic-gate } else if (who != P_MYID) {
1647c478bd9Sstevel@tonic-gate errno = EINVAL;
1657c478bd9Sstevel@tonic-gate return (-1);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate if (who == 0 && old_idtype(which))
1707c478bd9Sstevel@tonic-gate id = P_MYID;
1717c478bd9Sstevel@tonic-gate else
1727c478bd9Sstevel@tonic-gate id = who;
1737c478bd9Sstevel@tonic-gate
174d4204c85Sraf if (prio > NZERO - 1)
175d4204c85Sraf prio = NZERO - 1;
176d4204c85Sraf else if (prio < -NZERO)
177d4204c85Sraf prio = -NZERO;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate pcnice.pc_val = prio;
1807c478bd9Sstevel@tonic-gate pcnice.pc_op = PC_SETNICE;
1817c478bd9Sstevel@tonic-gate
182d4204c85Sraf ret = priocntl(idtype, id, PC_DONICE, &pcnice);
1839acbbeafSnn35248
1849acbbeafSnn35248 if (ret != 0 && errno == EPERM) {
1859acbbeafSnn35248 pcnice_t gpcnice = { 0, PC_GETNICE };
186d4204c85Sraf priv_set_t *pset = NULL;
1879acbbeafSnn35248
1889acbbeafSnn35248 /*
1899acbbeafSnn35248 * The priocntl PC_DONICE subcommand returns EPERM if we lack
1909acbbeafSnn35248 * sufficient privileges to carry out the operation, but
191d4204c85Sraf * setpriority(3C) may need to return EACCES. We can't just
192d4204c85Sraf * change EPERM to EACCES, because there are other conditions
193d4204c85Sraf * which legitimately cause EPERM (such as an euid/ruid mismatch
1949acbbeafSnn35248 * between the current process and the target.).
195d4204c85Sraf *
1969acbbeafSnn35248 * setpriority(3C) must return EACCES if we lack the privilege
1979acbbeafSnn35248 * checked for below and we are trying to increase the process
1989acbbeafSnn35248 * priority (by lowering the numeric value of its priority).
1999acbbeafSnn35248 */
200d4204c85Sraf if (priocntl(idtype, id, PC_DONICE, &gpcnice) == 0 &&
201d4204c85Sraf prio < gpcnice.pc_val) {
202d4204c85Sraf if ((pset = priv_allocset()) != NULL &&
203d4204c85Sraf getppriv(PRIV_EFFECTIVE, pset) == 0 &&
2049acbbeafSnn35248 !priv_ismember(pset, "proc_priocntl"))
2059acbbeafSnn35248 errno = EACCES;
206d4204c85Sraf if (pset != NULL)
2079acbbeafSnn35248 priv_freeset(pset);
2089acbbeafSnn35248 }
209d4204c85Sraf }
2109acbbeafSnn35248
2119acbbeafSnn35248 return (ret);
2127c478bd9Sstevel@tonic-gate }
213