xref: /illumos-gate/usr/src/lib/libc/port/gen/setpriority.c (revision 201ceb75ab95f9bf1f42ea1dc9ab363b43ba47cf)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #include "lint.h"
41 #include <string.h>
42 #include <limits.h>
43 #include <sys/types.h>
44 #include <sys/time.h>
45 #include <sys/resource.h>
46 #include <sys/procset.h>
47 #include <sys/priocntl.h>
48 #include <limits.h>
49 #include <errno.h>
50 #include <priv.h>
51 
52 static idtype_t
53 prio_to_idtype(int which)
54 {
55 	switch (which) {
56 
57 	case PRIO_PROCESS:
58 		return (P_PID);
59 
60 	case PRIO_PGRP:
61 		return (P_PGID);
62 
63 	case PRIO_USER:
64 		return (P_UID);
65 
66 	case PRIO_GROUP:
67 		return (P_GID);
68 
69 	case PRIO_SESSION:
70 		return (P_SID);
71 
72 	case PRIO_LWP:
73 		return (P_LWPID);
74 
75 	case PRIO_TASK:
76 		return (P_TASKID);
77 
78 	case PRIO_PROJECT:
79 		return (P_PROJID);
80 
81 	case PRIO_ZONE:
82 		return (P_ZONEID);
83 
84 	case PRIO_CONTRACT:
85 		return (P_CTID);
86 
87 	default:
88 		return (-1);
89 	}
90 }
91 
92 static int
93 old_idtype(int which)
94 {
95 	switch (which) {
96 	case PRIO_PROCESS:
97 	case PRIO_PGRP:
98 	case PRIO_USER:
99 		return (1);
100 	default:
101 		return (0);
102 	}
103 }
104 
105 int
106 getpriority(int which, id_t who)
107 {
108 	id_t id;
109 	idtype_t idtype;
110 	pcnice_t pcnice;
111 
112 	if ((idtype = prio_to_idtype(which)) == -1) {
113 		errno = EINVAL;
114 		return (-1);
115 	}
116 
117 	if (who < 0) {
118 		if (old_idtype(which)) {
119 			errno = EINVAL;
120 			return (-1);
121 		} else if (who != P_MYID) {
122 			errno = EINVAL;
123 			return (-1);
124 		}
125 	}
126 
127 	/*
128 	 * The POSIX standard requires that a 0 value for the who argument
129 	 * should specify the current process, process group, or user.
130 	 * For all other id types we can treat zero as normal id value.
131 	 */
132 	if (who == 0 && old_idtype(which))
133 		id = P_MYID;
134 	else
135 		id = who;
136 
137 	pcnice.pc_val = 0;
138 	pcnice.pc_op = PC_GETNICE;
139 
140 	if (priocntl(idtype, id, PC_DONICE, &pcnice) == -1)
141 		return (-1);
142 	else
143 		return (pcnice.pc_val);
144 }
145 
146 int
147 setpriority(int which, id_t who, int prio)
148 {
149 	id_t id;
150 	idtype_t idtype;
151 	pcnice_t pcnice;
152 	int ret;
153 
154 	if ((idtype = prio_to_idtype(which)) == -1) {
155 		errno = EINVAL;
156 		return (-1);
157 	}
158 
159 	if (who < 0) {
160 		if (old_idtype(which)) {
161 			errno = EINVAL;
162 			return (-1);
163 		} else if (who != P_MYID) {
164 			errno = EINVAL;
165 			return (-1);
166 		}
167 	}
168 
169 	if (who == 0 && old_idtype(which))
170 		id = P_MYID;
171 	else
172 		id = who;
173 
174 	if (prio > NZERO - 1)
175 		prio = NZERO - 1;
176 	else if (prio < -NZERO)
177 		prio = -NZERO;
178 
179 	pcnice.pc_val = prio;
180 	pcnice.pc_op = PC_SETNICE;
181 
182 	ret = priocntl(idtype, id, PC_DONICE, &pcnice);
183 
184 	if (ret != 0 && errno == EPERM) {
185 		pcnice_t	gpcnice = { 0, PC_GETNICE };
186 		priv_set_t	*pset = NULL;
187 
188 		/*
189 		 * The priocntl PC_DONICE subcommand returns EPERM if we lack
190 		 * sufficient privileges to carry out the operation, but
191 		 * setpriority(3C) may need to return EACCES.  We can't just
192 		 * change EPERM to EACCES, because there are other conditions
193 		 * which legitimately cause EPERM (such as an euid/ruid mismatch
194 		 * between the current process and the target.).
195 		 *
196 		 * setpriority(3C) must return EACCES if we lack the privilege
197 		 * checked for below and we are trying to increase the process
198 		 * priority (by lowering the numeric value of its priority).
199 		 */
200 		if (priocntl(idtype, id, PC_DONICE, &gpcnice) == 0 &&
201 		    prio < gpcnice.pc_val) {
202 			if ((pset = priv_allocset()) != NULL &&
203 			    getppriv(PRIV_EFFECTIVE, pset) == 0 &&
204 			    !priv_ismember(pset, "proc_priocntl"))
205 				errno = EACCES;
206 			if (pset != NULL)
207 				priv_freeset(pset);
208 		}
209 	}
210 
211 	return (ret);
212 }
213