4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 #pragma ident "%Z%%M% %I% %E% SMI"
45 #include <sys/types.h>
47 #include <sys/resource.h>
48 #include <sys/procset.h>
49 #include <sys/priocntl.h>
55 prio_to_idtype(int which
)
108 getpriority(int which
, id_t who
)
114 if ((idtype
= prio_to_idtype(which
)) == -1) {
120 if (old_idtype(which
)) {
123 } else if (who
!= P_MYID
) {
130 * The POSIX standard requires that a 0 value for the who argument
131 * should specify the current process, process group, or user.
132 * For all other id types we can treat zero as normal id value.
134 if (who
== 0 && old_idtype(which
))
140 pcnice
.pc_op
= PC_GETNICE
;
142 if (priocntl(idtype
, id
, PC_DONICE
, &pcnice
) == -1)
145 return (pcnice
.pc_val
);
149 setpriority(int which
, id_t who
, int prio
)
156 if ((idtype
= prio_to_idtype(which
)) == -1) {
162 if (old_idtype(which
)) {
165 } else if (who
!= P_MYID
) {
171 if (who
== 0 && old_idtype(which
))
176 if (prio
> NZERO
- 1)
178 else if (prio
< -NZERO
)
181 pcnice
.pc_val
= prio
;
182 pcnice
.pc_op
= PC_SETNICE
;
184 ret
= priocntl(idtype
, id
, PC_DONICE
, &pcnice
);
186 if (ret
!= 0 && errno
== EPERM
) {
187 pcnice_t gpcnice
= { 0, PC_GETNICE
};
188 priv_set_t
*pset
= NULL
;
191 * The priocntl PC_DONICE subcommand returns EPERM if we lack
192 * sufficient privileges to carry out the operation, but
193 * setpriority(3C) may need to return EACCES. We can't just
194 * change EPERM to EACCES, because there are other conditions
195 * which legitimately cause EPERM (such as an euid/ruid mismatch
196 * between the current process and the target.).
198 * setpriority(3C) must return EACCES if we lack the privilege
199 * checked for below and we are trying to increase the process
200 * priority (by lowering the numeric value of its priority).
202 if (priocntl(idtype
, id
, PC_DONICE
, &gpcnice
) == 0 &&
203 prio
< gpcnice
.pc_val
) {
204 if ((pset
= priv_allocset()) != NULL
&&
205 getppriv(PRIV_EFFECTIVE
, pset
) == 0 &&
206 !priv_ismember(pset
, "proc_priocntl"))