xref: /illumos-gate/usr/src/lib/libc/port/gen/setpriority.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include "synonyms.h"
43 
44 #include <string.h>
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/resource.h>
48 #include <sys/procset.h>
49 #include <sys/priocntl.h>
50 #include <errno.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, (caddr_t)&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 
153 	if ((idtype = prio_to_idtype(which)) == -1) {
154 		errno = EINVAL;
155 		return (-1);
156 	}
157 
158 	if (who < 0) {
159 		if (old_idtype(which)) {
160 			errno = EINVAL;
161 			return (-1);
162 		} else if (who != P_MYID) {
163 			errno = EINVAL;
164 			return (-1);
165 		}
166 	}
167 
168 	if (who == 0 && old_idtype(which))
169 		id = P_MYID;
170 	else
171 		id = who;
172 
173 	if (prio > 19)
174 		prio = 19;
175 	else if (prio < -20)
176 		prio = -20;
177 
178 	pcnice.pc_val = prio;
179 	pcnice.pc_op = PC_SETNICE;
180 
181 	return (priocntl(idtype, id, PC_DONICE, (caddr_t)&pcnice));
182 }
183