xref: /onnv-gate/usr/src/cmd/priocntl/subr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
30*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include	<stdio.h>
34*0Sstevel@tonic-gate #include	<string.h>
35*0Sstevel@tonic-gate #include	<strings.h>
36*0Sstevel@tonic-gate #include	<stdlib.h>
37*0Sstevel@tonic-gate #include	<unistd.h>
38*0Sstevel@tonic-gate #include	<sys/types.h>
39*0Sstevel@tonic-gate #include	<limits.h>
40*0Sstevel@tonic-gate #include	<dirent.h>
41*0Sstevel@tonic-gate #include	<fcntl.h>
42*0Sstevel@tonic-gate #include	<sys/time.h>
43*0Sstevel@tonic-gate #include	<sys/procset.h>
44*0Sstevel@tonic-gate #include	<sys/priocntl.h>
45*0Sstevel@tonic-gate #include	<sys/task.h>
46*0Sstevel@tonic-gate #include	<procfs.h>
47*0Sstevel@tonic-gate #include	<project.h>
48*0Sstevel@tonic-gate #include	<errno.h>
49*0Sstevel@tonic-gate #include	<zone.h>
50*0Sstevel@tonic-gate #include	<libcontract_priv.h>
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #include "priocntl.h"
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /*LINTLIBRARY*/
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate  * Utility functions for priocntl command.
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static char	*procdir = "/proc";
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*PRINTFLIKE1*/
63*0Sstevel@tonic-gate void
fatalerr(format,a1,a2,a3,a4,a5)64*0Sstevel@tonic-gate fatalerr(format, a1, a2, a3, a4, a5)
65*0Sstevel@tonic-gate char	*format;
66*0Sstevel@tonic-gate int	a1, a2, a3, a4, a5;
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	(void) fprintf(stderr, format, a1, a2, a3, a4, a5);
69*0Sstevel@tonic-gate 	exit(1);
70*0Sstevel@tonic-gate }
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * Structure defining idtypes known to the priocntl command
75*0Sstevel@tonic-gate  * along with the corresponding names and a liberal guess
76*0Sstevel@tonic-gate  * of the max number of procs sharing any given ID of that type.
77*0Sstevel@tonic-gate  * The idtype values themselves are defined in <sys/procset.h>.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate static struct idtypes {
80*0Sstevel@tonic-gate 	idtype_t	idtype;
81*0Sstevel@tonic-gate 	char		*idtypnm;
82*0Sstevel@tonic-gate } idtypes [] = {
83*0Sstevel@tonic-gate 	{ P_PID,	"pid"	},
84*0Sstevel@tonic-gate 	{ P_PPID,	"ppid"	},
85*0Sstevel@tonic-gate 	{ P_PGID,	"pgid"	},
86*0Sstevel@tonic-gate 	{ P_SID,	"sid"	},
87*0Sstevel@tonic-gate 	{ P_CID,	"class"	},
88*0Sstevel@tonic-gate 	{ P_UID,	"uid"	},
89*0Sstevel@tonic-gate 	{ P_GID,	"gid"	},
90*0Sstevel@tonic-gate 	{ P_PROJID,	"projid" },
91*0Sstevel@tonic-gate 	{ P_TASKID,	"taskid" },
92*0Sstevel@tonic-gate 	{ P_ZONEID,	"zoneid" },
93*0Sstevel@tonic-gate 	{ P_CTID,	"ctid" },
94*0Sstevel@tonic-gate 	{ P_ALL,	"all"	}
95*0Sstevel@tonic-gate };
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate #define	IDCNT	(sizeof (idtypes) / sizeof (struct idtypes))
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate int
str2idtyp(idtypnm,idtypep)101*0Sstevel@tonic-gate str2idtyp(idtypnm, idtypep)
102*0Sstevel@tonic-gate char		*idtypnm;
103*0Sstevel@tonic-gate idtype_t	*idtypep;
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	register struct idtypes	*curp;
106*0Sstevel@tonic-gate 	register struct idtypes	*endp;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	for (curp = idtypes, endp = &idtypes[IDCNT]; curp < endp; curp++) {
109*0Sstevel@tonic-gate 		if (strcmp(curp->idtypnm, idtypnm) == 0) {
110*0Sstevel@tonic-gate 			*idtypep = curp->idtype;
111*0Sstevel@tonic-gate 			return (0);
112*0Sstevel@tonic-gate 		}
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 	return (-1);
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate int
idtyp2str(idtype,idtypnm)119*0Sstevel@tonic-gate idtyp2str(idtype, idtypnm)
120*0Sstevel@tonic-gate idtype_t	idtype;
121*0Sstevel@tonic-gate char		*idtypnm;
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	register struct idtypes	*curp;
124*0Sstevel@tonic-gate 	register struct idtypes	*endp;
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	for (curp = idtypes, endp = &idtypes[IDCNT]; curp < endp; curp++) {
127*0Sstevel@tonic-gate 		if (idtype == curp->idtype) {
128*0Sstevel@tonic-gate 			(void) strncpy(idtypnm, curp->idtypnm, PC_IDTYPNMSZ);
129*0Sstevel@tonic-gate 			return (0);
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 	return (-1);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate /*
137*0Sstevel@tonic-gate  * Compare two IDs for equality.
138*0Sstevel@tonic-gate  */
139*0Sstevel@tonic-gate int
idcompar(id1p,id2p)140*0Sstevel@tonic-gate idcompar(id1p, id2p)
141*0Sstevel@tonic-gate id_t	*id1p;
142*0Sstevel@tonic-gate id_t	*id2p;
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	if (*id1p == *id2p)
145*0Sstevel@tonic-gate 		return (0);
146*0Sstevel@tonic-gate 	else
147*0Sstevel@tonic-gate 		return (-1);
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate id_t
clname2cid(clname)152*0Sstevel@tonic-gate clname2cid(clname)
153*0Sstevel@tonic-gate char	*clname;
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	pcinfo_t	pcinfo;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	(void) strncpy(pcinfo.pc_clname, clname, PC_CLNMSZ);
158*0Sstevel@tonic-gate 	if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
159*0Sstevel@tonic-gate 		return ((id_t)-1);
160*0Sstevel@tonic-gate 	return (pcinfo.pc_cid);
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate int
getmyid(idtype,idptr)165*0Sstevel@tonic-gate getmyid(idtype, idptr)
166*0Sstevel@tonic-gate idtype_t	idtype;
167*0Sstevel@tonic-gate id_t		*idptr;
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	pcinfo_t	pcinfo;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	switch (idtype) {
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	case P_PID:
174*0Sstevel@tonic-gate 		*idptr = (id_t)getpid();
175*0Sstevel@tonic-gate 		break;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	case P_PPID:
178*0Sstevel@tonic-gate 		*idptr = (id_t)getppid();
179*0Sstevel@tonic-gate 		break;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	case P_PGID:
182*0Sstevel@tonic-gate 		*idptr = (id_t)getpgrp();
183*0Sstevel@tonic-gate 		break;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	case P_SID:
186*0Sstevel@tonic-gate 		*idptr = (id_t)getsid(getpid());
187*0Sstevel@tonic-gate 		break;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	case P_CID:
190*0Sstevel@tonic-gate 		if (priocntl(P_PID, P_MYID, PC_GETXPARMS, NULL,
191*0Sstevel@tonic-gate 		    PC_KY_CLNAME, pcinfo.pc_clname, 0) == -1 ||
192*0Sstevel@tonic-gate 		    priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
193*0Sstevel@tonic-gate 			return (-1);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 		*idptr = pcinfo.pc_cid;
196*0Sstevel@tonic-gate 		break;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	case P_UID:
199*0Sstevel@tonic-gate 		*idptr = (id_t)getuid();
200*0Sstevel@tonic-gate 		break;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	case P_GID:
203*0Sstevel@tonic-gate 		*idptr = (id_t)getgid();
204*0Sstevel@tonic-gate 		break;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	case P_PROJID:
207*0Sstevel@tonic-gate 		*idptr = (id_t)getprojid();
208*0Sstevel@tonic-gate 		break;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	case P_TASKID:
211*0Sstevel@tonic-gate 		*idptr = (id_t)gettaskid();
212*0Sstevel@tonic-gate 		break;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	case P_ZONEID:
215*0Sstevel@tonic-gate 		*idptr = (id_t)getzoneid();
216*0Sstevel@tonic-gate 		break;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	case P_CTID: {
219*0Sstevel@tonic-gate 		ctid_t id = getctid();
220*0Sstevel@tonic-gate 		if (id == -1)
221*0Sstevel@tonic-gate 			return (-1);
222*0Sstevel@tonic-gate 		*idptr = id;
223*0Sstevel@tonic-gate 		break;
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	default:
227*0Sstevel@tonic-gate 		return (-1);
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 	return (0);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate int
getmyidstr(idtype,idstr)234*0Sstevel@tonic-gate getmyidstr(idtype, idstr)
235*0Sstevel@tonic-gate idtype_t	idtype;
236*0Sstevel@tonic-gate char		*idstr;
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	char		clname[PC_CLNMSZ];
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	switch (idtype) {
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	case P_PID:
243*0Sstevel@tonic-gate 		itoa((long)getpid(), idstr);
244*0Sstevel@tonic-gate 		break;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	case P_PPID:
247*0Sstevel@tonic-gate 		itoa((long)getppid(), idstr);
248*0Sstevel@tonic-gate 		break;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	case P_PGID:
251*0Sstevel@tonic-gate 		itoa((long)getpgrp(), idstr);
252*0Sstevel@tonic-gate 		break;
253*0Sstevel@tonic-gate 	case P_SID:
254*0Sstevel@tonic-gate 		itoa((long)getsid(getpid()), idstr);
255*0Sstevel@tonic-gate 		break;
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	case P_CID:
258*0Sstevel@tonic-gate 		if (priocntl(P_PID, P_MYID, PC_GETXPARMS, NULL,
259*0Sstevel@tonic-gate 		    PC_KY_CLNAME, clname, 0) == -1)
260*0Sstevel@tonic-gate 			return (-1);
261*0Sstevel@tonic-gate 		(void) strncpy(idstr, clname, PC_CLNMSZ);
262*0Sstevel@tonic-gate 		break;
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	case P_UID:
265*0Sstevel@tonic-gate 		itoa((long)getuid(), idstr);
266*0Sstevel@tonic-gate 		break;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	case P_GID:
269*0Sstevel@tonic-gate 		itoa((long)getgid(), idstr);
270*0Sstevel@tonic-gate 		break;
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	case P_PROJID:
273*0Sstevel@tonic-gate 		itoa((long)getprojid(), idstr);
274*0Sstevel@tonic-gate 		break;
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	case P_TASKID:
277*0Sstevel@tonic-gate 		itoa((long)gettaskid(), idstr);
278*0Sstevel@tonic-gate 		break;
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	case P_ZONEID:
281*0Sstevel@tonic-gate 		itoa((long)getzoneid(), idstr);
282*0Sstevel@tonic-gate 		break;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	case P_CTID: {
285*0Sstevel@tonic-gate 		id_t id;
286*0Sstevel@tonic-gate 		if ((id = getctid()) == -1)
287*0Sstevel@tonic-gate 			return (-1);
288*0Sstevel@tonic-gate 		itoa((long)id, idstr);
289*0Sstevel@tonic-gate 		break;
290*0Sstevel@tonic-gate 	}
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	default:
293*0Sstevel@tonic-gate 		return (-1);
294*0Sstevel@tonic-gate 	}
295*0Sstevel@tonic-gate 	return (0);
296*0Sstevel@tonic-gate }
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate /*
299*0Sstevel@tonic-gate  * Look for pids with "upri > uprilim" in the set specified by idtype/id.
300*0Sstevel@tonic-gate  * If upri exceeds uprilim then print a warning.
301*0Sstevel@tonic-gate  */
302*0Sstevel@tonic-gate int
verifyupri(idtype_t idtype,id_t id,char * clname,int key,pri_t upri,char * basenm)303*0Sstevel@tonic-gate verifyupri(idtype_t idtype, id_t id, char *clname, int key,
304*0Sstevel@tonic-gate 	pri_t upri, char *basenm)
305*0Sstevel@tonic-gate {
306*0Sstevel@tonic-gate 	psinfo_t		prinfo;
307*0Sstevel@tonic-gate 	prcred_t		prcred;
308*0Sstevel@tonic-gate 	DIR			*dirp;
309*0Sstevel@tonic-gate 	struct dirent		*dentp;
310*0Sstevel@tonic-gate 	char			pname[MAXNAMLEN];
311*0Sstevel@tonic-gate 	char			*fname;
312*0Sstevel@tonic-gate 	int			procfd;
313*0Sstevel@tonic-gate 	int			saverr;
314*0Sstevel@tonic-gate 	pri_t			uprilim;
315*0Sstevel@tonic-gate 	int			verify;
316*0Sstevel@tonic-gate 	int			error = 0;
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	if (idtype == P_PID) {
319*0Sstevel@tonic-gate 		if (priocntl(P_PID, id, PC_GETXPARMS, clname, key,
320*0Sstevel@tonic-gate 		    &uprilim, 0) == -1)
321*0Sstevel@tonic-gate 			error = -1;
322*0Sstevel@tonic-gate 		else if (upri > uprilim)
323*0Sstevel@tonic-gate 			(void) fprintf(stderr,
324*0Sstevel@tonic-gate 			    "%s: Specified user priority %d exceeds"
325*0Sstevel@tonic-gate 			    " limit %d; set to %d (pid %d)\n",
326*0Sstevel@tonic-gate 			    basenm, upri, uprilim, uprilim, (int)id);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		return (error);
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	/*
332*0Sstevel@tonic-gate 	 * Look for the processes in the set specified by idtype/id.
333*0Sstevel@tonic-gate 	 * We read the /proc/<pid>/psinfo file to get the necessary
334*0Sstevel@tonic-gate 	 * process information.
335*0Sstevel@tonic-gate 	 */
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	if ((dirp = opendir(procdir)) == NULL)
338*0Sstevel@tonic-gate 		fatalerr("%s: Can't open PROC directory %s\n",
339*0Sstevel@tonic-gate 		    basenm, procdir);
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	while ((dentp = readdir(dirp)) != NULL) {
342*0Sstevel@tonic-gate 		if (dentp->d_name[0] == '.')	/* skip . and .. */
343*0Sstevel@tonic-gate 			continue;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 		(void) snprintf(pname, MAXNAMLEN, "%s/%s/",
346*0Sstevel@tonic-gate 		    procdir, dentp->d_name);
347*0Sstevel@tonic-gate 		fname = pname + strlen(pname);
348*0Sstevel@tonic-gate retry:
349*0Sstevel@tonic-gate 		(void) strncpy(fname, "psinfo", strlen("psinfo") + 1);
350*0Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) < 0)
351*0Sstevel@tonic-gate 			continue;
352*0Sstevel@tonic-gate 		if (read(procfd, &prinfo, sizeof (prinfo)) != sizeof (prinfo)) {
353*0Sstevel@tonic-gate 			saverr = errno;
354*0Sstevel@tonic-gate 			(void) close(procfd);
355*0Sstevel@tonic-gate 			if (saverr == EAGAIN)
356*0Sstevel@tonic-gate 				goto retry;
357*0Sstevel@tonic-gate 			continue;
358*0Sstevel@tonic-gate 		}
359*0Sstevel@tonic-gate 		(void) close(procfd);
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 		if (idtype == P_UID || idtype == P_GID) {
362*0Sstevel@tonic-gate 			(void) strncpy(fname, "cred", strlen("cred") + 1);
363*0Sstevel@tonic-gate 			if ((procfd = open(pname, O_RDONLY)) < 0 ||
364*0Sstevel@tonic-gate 			    read(procfd, &prcred, sizeof (prcred)) !=
365*0Sstevel@tonic-gate 			    sizeof (prcred)) {
366*0Sstevel@tonic-gate 				saverr = errno;
367*0Sstevel@tonic-gate 				(void) close(procfd);
368*0Sstevel@tonic-gate 				if (saverr == EAGAIN)
369*0Sstevel@tonic-gate 					goto retry;
370*0Sstevel@tonic-gate 				continue;
371*0Sstevel@tonic-gate 			}
372*0Sstevel@tonic-gate 			(void) close(procfd);
373*0Sstevel@tonic-gate 		}
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 		if (prinfo.pr_lwp.pr_state == 0 || prinfo.pr_nlwp == 0)
376*0Sstevel@tonic-gate 			continue;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 		/*
379*0Sstevel@tonic-gate 		 * The lwp must be in the correct class.
380*0Sstevel@tonic-gate 		 */
381*0Sstevel@tonic-gate 		if (strncmp(clname, prinfo.pr_lwp.pr_clname, PC_CLNMSZ) != 0)
382*0Sstevel@tonic-gate 			continue;
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 		verify = 0;
385*0Sstevel@tonic-gate 		switch (idtype) {
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 		case P_PPID:
388*0Sstevel@tonic-gate 			if (id == (id_t)prinfo.pr_ppid)
389*0Sstevel@tonic-gate 				verify++;
390*0Sstevel@tonic-gate 			break;
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 		case P_PGID:
393*0Sstevel@tonic-gate 			if (id == (id_t)prinfo.pr_pgid)
394*0Sstevel@tonic-gate 				verify++;
395*0Sstevel@tonic-gate 			break;
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 		case P_SID:
398*0Sstevel@tonic-gate 			if (id == (id_t)prinfo.pr_sid)
399*0Sstevel@tonic-gate 				verify++;
400*0Sstevel@tonic-gate 			break;
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 		case P_UID:
403*0Sstevel@tonic-gate 			if (id == (id_t)prcred.pr_euid)
404*0Sstevel@tonic-gate 				verify++;
405*0Sstevel@tonic-gate 			break;
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 		case P_GID:
408*0Sstevel@tonic-gate 			if (id == (id_t)prcred.pr_egid)
409*0Sstevel@tonic-gate 				verify++;
410*0Sstevel@tonic-gate 			break;
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 		case P_PROJID:
413*0Sstevel@tonic-gate 			if (id == (id_t)prinfo.pr_projid)
414*0Sstevel@tonic-gate 				verify++;
415*0Sstevel@tonic-gate 			break;
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 		case P_TASKID:
418*0Sstevel@tonic-gate 			if (id == (id_t)prinfo.pr_taskid)
419*0Sstevel@tonic-gate 				verify++;
420*0Sstevel@tonic-gate 			break;
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 		case P_ZONEID:
423*0Sstevel@tonic-gate 			if (id == (id_t)prinfo.pr_zoneid)
424*0Sstevel@tonic-gate 				verify++;
425*0Sstevel@tonic-gate 			break;
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 		case P_CTID:
428*0Sstevel@tonic-gate 			if (id == (id_t)prinfo.pr_contract)
429*0Sstevel@tonic-gate 				verify++;
430*0Sstevel@tonic-gate 			break;
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 		case P_CID:
433*0Sstevel@tonic-gate 		case P_ALL:
434*0Sstevel@tonic-gate 			verify++;
435*0Sstevel@tonic-gate 			break;
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 		default:
438*0Sstevel@tonic-gate 			fatalerr("%s: Bad idtype %d in verifyupri()\n",
439*0Sstevel@tonic-gate 			    basenm, idtype);
440*0Sstevel@tonic-gate 		}
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 		if (verify) {
443*0Sstevel@tonic-gate 			if (priocntl(P_PID, prinfo.pr_pid, PC_GETXPARMS,
444*0Sstevel@tonic-gate 			    clname, key, &uprilim, 0) == -1)
445*0Sstevel@tonic-gate 				error = -1;
446*0Sstevel@tonic-gate 			else if (upri > uprilim)
447*0Sstevel@tonic-gate 				(void) fprintf(stderr,
448*0Sstevel@tonic-gate 				    "%s: Specified user priority %d exceeds"
449*0Sstevel@tonic-gate 				    " limit %d; set to %d (pid %d)\n",
450*0Sstevel@tonic-gate 				    basenm, upri, uprilim, uprilim,
451*0Sstevel@tonic-gate 				    (int)prinfo.pr_pid);
452*0Sstevel@tonic-gate 		}
453*0Sstevel@tonic-gate 	}
454*0Sstevel@tonic-gate 	(void) closedir(dirp);
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	return (error);
457*0Sstevel@tonic-gate }
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate /*
461*0Sstevel@tonic-gate  * Read a list of pids from a stream.
462*0Sstevel@tonic-gate  */
463*0Sstevel@tonic-gate pid_t *
read_pidlist(size_t * npidsp,FILE * filep)464*0Sstevel@tonic-gate read_pidlist(size_t *npidsp, FILE *filep)
465*0Sstevel@tonic-gate {
466*0Sstevel@tonic-gate 	size_t	nitems;
467*0Sstevel@tonic-gate 	pid_t	*pidlist = NULL;
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	*npidsp = 0;
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	do {
472*0Sstevel@tonic-gate 		if ((pidlist = (pid_t *)realloc(pidlist,
473*0Sstevel@tonic-gate 		    (*npidsp + NPIDS) * sizeof (pid_t))) == NULL)
474*0Sstevel@tonic-gate 			return (NULL);
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 		nitems = fread(pidlist + *npidsp, sizeof (pid_t), NPIDS, filep);
477*0Sstevel@tonic-gate 		if (ferror(filep))
478*0Sstevel@tonic-gate 			return (NULL);
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 		*npidsp += nitems;
481*0Sstevel@tonic-gate 	} while (nitems == NPIDS);
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	return (pidlist);
484*0Sstevel@tonic-gate }
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate void
free_pidlist(pid_t * pidlist)488*0Sstevel@tonic-gate free_pidlist(pid_t *pidlist)
489*0Sstevel@tonic-gate {
490*0Sstevel@tonic-gate 	free(pidlist);
491*0Sstevel@tonic-gate }
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate long
str2num(char * p,long min,long max)495*0Sstevel@tonic-gate str2num(char *p, long min, long max)
496*0Sstevel@tonic-gate {
497*0Sstevel@tonic-gate 	long val;
498*0Sstevel@tonic-gate 	char *q;
499*0Sstevel@tonic-gate 	errno = 0;
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	val = strtol(p, &q, 10);
502*0Sstevel@tonic-gate 	if (errno != 0 || q == p || *q != '\0' || val < min || val > max)
503*0Sstevel@tonic-gate 		errno = EINVAL;
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	return (val);
506*0Sstevel@tonic-gate }
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate /*
510*0Sstevel@tonic-gate  * itoa() and reverse() taken almost verbatim from K & R Chapter 3.
511*0Sstevel@tonic-gate  */
512*0Sstevel@tonic-gate static void	reverse();
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate /*
515*0Sstevel@tonic-gate  * itoa(): Convert n to characters in s.
516*0Sstevel@tonic-gate  */
517*0Sstevel@tonic-gate void
itoa(n,s)518*0Sstevel@tonic-gate itoa(n, s)
519*0Sstevel@tonic-gate long	n;
520*0Sstevel@tonic-gate char	*s;
521*0Sstevel@tonic-gate {
522*0Sstevel@tonic-gate 	long	i, sign;
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate 	if ((sign = n) < 0)	/* record sign */
525*0Sstevel@tonic-gate 		n = -n;		/* make sign positive */
526*0Sstevel@tonic-gate 	i = 0;
527*0Sstevel@tonic-gate 	do {	/* generate digits in reverse order */
528*0Sstevel@tonic-gate 		s[i++] = n % 10 + '0';	/* get next digit */
529*0Sstevel@tonic-gate 	} while ((n /= 10) > 0);	/* delete it */
530*0Sstevel@tonic-gate 	if (sign < 0)
531*0Sstevel@tonic-gate 		s[i++] = '-';
532*0Sstevel@tonic-gate 	s[i] = '\0';
533*0Sstevel@tonic-gate 	reverse(s);
534*0Sstevel@tonic-gate }
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate /*
538*0Sstevel@tonic-gate  * reverse(): Reverse string s in place.
539*0Sstevel@tonic-gate  */
540*0Sstevel@tonic-gate static void
reverse(s)541*0Sstevel@tonic-gate reverse(s)
542*0Sstevel@tonic-gate char	*s;
543*0Sstevel@tonic-gate {
544*0Sstevel@tonic-gate 	int	c, i, j;
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
547*0Sstevel@tonic-gate 		c = s[i];
548*0Sstevel@tonic-gate 		s[i] = s[j];
549*0Sstevel@tonic-gate 		s[j] = (char)c;
550*0Sstevel@tonic-gate 	}
551*0Sstevel@tonic-gate }
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate /*
555*0Sstevel@tonic-gate  * The following routine was removed from libc (libc/port/gen/hrtnewres.c).
556*0Sstevel@tonic-gate  * It has also been added to disadmin, so if you fix it here, you should
557*0Sstevel@tonic-gate  * also probably fix it there. In the long term, this should be recoded to
558*0Sstevel@tonic-gate  * not be hrt'ish.
559*0Sstevel@tonic-gate  */
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate /*
562*0Sstevel@tonic-gate  *	Convert interval expressed in htp->hrt_res to new_res.
563*0Sstevel@tonic-gate  *
564*0Sstevel@tonic-gate  *	Calculate: (interval * new_res) / htp->hrt_res  rounding off as
565*0Sstevel@tonic-gate  *		specified by round.
566*0Sstevel@tonic-gate  *
567*0Sstevel@tonic-gate  *	Note:	All args are assumed to be positive.  If
568*0Sstevel@tonic-gate  *	the last divide results in something bigger than
569*0Sstevel@tonic-gate  *	a long, then -1 is returned instead.
570*0Sstevel@tonic-gate  */
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate int
_hrtnewres(htp,new_res,round)573*0Sstevel@tonic-gate _hrtnewres(htp, new_res, round)
574*0Sstevel@tonic-gate register hrtimer_t *htp;
575*0Sstevel@tonic-gate register ulong_t new_res;
576*0Sstevel@tonic-gate long round;
577*0Sstevel@tonic-gate {
578*0Sstevel@tonic-gate 	register long  interval;
579*0Sstevel@tonic-gate 	longlong_t	dint;
580*0Sstevel@tonic-gate 	longlong_t	dto_res;
581*0Sstevel@tonic-gate 	longlong_t	drem;
582*0Sstevel@tonic-gate 	longlong_t	dfrom_res;
583*0Sstevel@tonic-gate 	longlong_t	prod;
584*0Sstevel@tonic-gate 	longlong_t	quot;
585*0Sstevel@tonic-gate 	register long	numerator;
586*0Sstevel@tonic-gate 	register long	result;
587*0Sstevel@tonic-gate 	ulong_t		modulus;
588*0Sstevel@tonic-gate 	ulong_t		twomodulus;
589*0Sstevel@tonic-gate 	long		temp;
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate 	if (new_res > NANOSEC || htp->hrt_rem < 0)
592*0Sstevel@tonic-gate 		return (-1);
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	if (htp->hrt_rem >= htp->hrt_res) {
595*0Sstevel@tonic-gate 		htp->hrt_secs += htp->hrt_rem / htp->hrt_res;
596*0Sstevel@tonic-gate 		htp->hrt_rem = htp->hrt_rem % htp->hrt_res;
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	interval = htp->hrt_rem;
600*0Sstevel@tonic-gate 	if (interval == 0) {
601*0Sstevel@tonic-gate 		htp->hrt_res = new_res;
602*0Sstevel@tonic-gate 		return (0);
603*0Sstevel@tonic-gate 	}
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	/*
606*0Sstevel@tonic-gate 	 *	Try to do the calculations in single precision first
607*0Sstevel@tonic-gate 	 *	(for speed).  If they overflow, use double precision.
608*0Sstevel@tonic-gate 	 *	What we want to compute is:
609*0Sstevel@tonic-gate 	 *
610*0Sstevel@tonic-gate 	 *		(interval * new_res) / hrt->hrt_res
611*0Sstevel@tonic-gate 	 */
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	numerator = interval * new_res;
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	if (numerator / new_res  ==  interval) {
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 		/*
618*0Sstevel@tonic-gate 		 *	The above multiply didn't give overflow since
619*0Sstevel@tonic-gate 		 *	the division got back the original number.  Go
620*0Sstevel@tonic-gate 		 *	ahead and compute the result.
621*0Sstevel@tonic-gate 		 */
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 		result = numerator / htp->hrt_res;
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 		/*
626*0Sstevel@tonic-gate 		 *	For HRT_RND, compute the value of:
627*0Sstevel@tonic-gate 		 *
628*0Sstevel@tonic-gate 		 *		(interval * new_res) % htp->hrt_res
629*0Sstevel@tonic-gate 		 *
630*0Sstevel@tonic-gate 		 *	If it is greater than half of the htp->hrt_res,
631*0Sstevel@tonic-gate 		 *	then rounding increases the result by 1.
632*0Sstevel@tonic-gate 		 *
633*0Sstevel@tonic-gate 		 *	For HRT_RNDUP, we increase the result by 1 if:
634*0Sstevel@tonic-gate 		 *
635*0Sstevel@tonic-gate 		 *		result * htp->hrt_res != numerator
636*0Sstevel@tonic-gate 		 *
637*0Sstevel@tonic-gate 		 *	because this tells us we truncated when calculating
638*0Sstevel@tonic-gate 		 *	result above.
639*0Sstevel@tonic-gate 		 *
640*0Sstevel@tonic-gate 		 *	We also check for overflow when incrementing result
641*0Sstevel@tonic-gate 		 *	although this is extremely rare.
642*0Sstevel@tonic-gate 		 */
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 		if (round == HRT_RND) {
645*0Sstevel@tonic-gate 			modulus = numerator - result * htp->hrt_res;
646*0Sstevel@tonic-gate 			if ((twomodulus = 2 * modulus) / 2 == modulus) {
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate 				/*
649*0Sstevel@tonic-gate 				 * No overflow (if we overflow in calculation
650*0Sstevel@tonic-gate 				 * of twomodulus we fall through and use
651*0Sstevel@tonic-gate 				 * double precision).
652*0Sstevel@tonic-gate 				 */
653*0Sstevel@tonic-gate 				if (twomodulus >= htp->hrt_res) {
654*0Sstevel@tonic-gate 					temp = result + 1;
655*0Sstevel@tonic-gate 					if (temp - 1 == result)
656*0Sstevel@tonic-gate 						result++;
657*0Sstevel@tonic-gate 					else
658*0Sstevel@tonic-gate 						return (-1);
659*0Sstevel@tonic-gate 				}
660*0Sstevel@tonic-gate 				htp->hrt_res = new_res;
661*0Sstevel@tonic-gate 				htp->hrt_rem = result;
662*0Sstevel@tonic-gate 				return (0);
663*0Sstevel@tonic-gate 			}
664*0Sstevel@tonic-gate 		} else if (round == HRT_RNDUP) {
665*0Sstevel@tonic-gate 			if (result * htp->hrt_res != numerator) {
666*0Sstevel@tonic-gate 				temp = result + 1;
667*0Sstevel@tonic-gate 				if (temp - 1 == result)
668*0Sstevel@tonic-gate 					result++;
669*0Sstevel@tonic-gate 				else
670*0Sstevel@tonic-gate 					return (-1);
671*0Sstevel@tonic-gate 			}
672*0Sstevel@tonic-gate 			htp->hrt_res = new_res;
673*0Sstevel@tonic-gate 			htp->hrt_rem = result;
674*0Sstevel@tonic-gate 			return (0);
675*0Sstevel@tonic-gate 		} else {	/* round == HRT_TRUNC */
676*0Sstevel@tonic-gate 			htp->hrt_res = new_res;
677*0Sstevel@tonic-gate 			htp->hrt_rem = result;
678*0Sstevel@tonic-gate 			return (0);
679*0Sstevel@tonic-gate 		}
680*0Sstevel@tonic-gate 	}
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 	/*
683*0Sstevel@tonic-gate 	 *	We would get overflow doing the calculation is
684*0Sstevel@tonic-gate 	 *	single precision so do it the slow but careful way.
685*0Sstevel@tonic-gate 	 *
686*0Sstevel@tonic-gate 	 *	Compute the interval times the resolution we are
687*0Sstevel@tonic-gate 	 *	going to.
688*0Sstevel@tonic-gate 	 */
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 	dint = interval;
691*0Sstevel@tonic-gate 	dto_res = new_res;
692*0Sstevel@tonic-gate 	prod = dint * dto_res;
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 	/*
695*0Sstevel@tonic-gate 	 *	For HRT_RND the result will be equal to:
696*0Sstevel@tonic-gate 	 *
697*0Sstevel@tonic-gate 	 *		((interval * new_res) + htp->hrt_res / 2) / htp->hrt_res
698*0Sstevel@tonic-gate 	 *
699*0Sstevel@tonic-gate 	 *	and for HRT_RNDUP we use:
700*0Sstevel@tonic-gate 	 *
701*0Sstevel@tonic-gate 	 *		((interval * new_res) + htp->hrt_res - 1) / htp->hrt_res
702*0Sstevel@tonic-gate 	 *
703*0Sstevel@tonic-gate 	 * 	This is a different but equivalent way of rounding.
704*0Sstevel@tonic-gate 	 */
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 	if (round == HRT_RND) {
707*0Sstevel@tonic-gate 		drem = htp->hrt_res / 2;
708*0Sstevel@tonic-gate 		prod = prod + drem;
709*0Sstevel@tonic-gate 	} else if (round == HRT_RNDUP) {
710*0Sstevel@tonic-gate 		drem = htp->hrt_res - 1;
711*0Sstevel@tonic-gate 		prod = prod + drem;
712*0Sstevel@tonic-gate 	}
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 	dfrom_res = htp->hrt_res;
715*0Sstevel@tonic-gate 	quot = prod / dfrom_res;
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	/*
718*0Sstevel@tonic-gate 	 *	If the quotient won't fit in a long, then we have
719*0Sstevel@tonic-gate 	 *	overflow.  Otherwise, return the result.
720*0Sstevel@tonic-gate 	 */
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 	if (quot > UINT_MAX) {
723*0Sstevel@tonic-gate 		return (-1);
724*0Sstevel@tonic-gate 	} else {
725*0Sstevel@tonic-gate 		htp->hrt_res = new_res;
726*0Sstevel@tonic-gate 		htp->hrt_rem = (int)quot;
727*0Sstevel@tonic-gate 		return (0);
728*0Sstevel@tonic-gate 	}
729*0Sstevel@tonic-gate }
730