xref: /titanic_50/usr/src/cmd/dispadmin/subr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <unistd.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
39*7c478bd9Sstevel@tonic-gate #include <limits.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include "dispadmin.h"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * Utility functions for dispadmin command.
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate void
fatalerr(const char * format,...)50*7c478bd9Sstevel@tonic-gate fatalerr(const char *format, ...)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	va_list ap;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	(void) va_start(ap, format);
55*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
56*7c478bd9Sstevel@tonic-gate 	va_end(ap);
57*7c478bd9Sstevel@tonic-gate 	exit(1);
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * hrtconvert() returns the interval specified by htp as a single
63*7c478bd9Sstevel@tonic-gate  * value in resolution htp->hrt_res.  Returns -1 on overflow.
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate long
hrtconvert(hrtimer_t * htp)66*7c478bd9Sstevel@tonic-gate hrtconvert(hrtimer_t *htp)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	long	sum;
69*7c478bd9Sstevel@tonic-gate 	long	product;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	product = htp->hrt_secs * htp->hrt_res;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if (product / htp->hrt_res == htp->hrt_secs) {
74*7c478bd9Sstevel@tonic-gate 		sum = product + htp->hrt_rem;
75*7c478bd9Sstevel@tonic-gate 		if (sum - htp->hrt_rem == product) {
76*7c478bd9Sstevel@tonic-gate 			return (sum);
77*7c478bd9Sstevel@tonic-gate 		}
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	return (-1);
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate /*
83*7c478bd9Sstevel@tonic-gate  * The following routine was removed from libc (libc/port/gen/hrtnewres.c).
84*7c478bd9Sstevel@tonic-gate  * It has also been added to priocntl, so if you fix it here, you should
85*7c478bd9Sstevel@tonic-gate  * also probably fix it there. In the long term, this should be recoded to
86*7c478bd9Sstevel@tonic-gate  * not be hrt'ish.
87*7c478bd9Sstevel@tonic-gate  */
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate /*
90*7c478bd9Sstevel@tonic-gate  *	Convert interval expressed in htp->hrt_res to new_res.
91*7c478bd9Sstevel@tonic-gate  *
92*7c478bd9Sstevel@tonic-gate  *	Calculate: (interval * new_res) / htp->hrt_res  rounding off as
93*7c478bd9Sstevel@tonic-gate  *		specified by round.
94*7c478bd9Sstevel@tonic-gate  *
95*7c478bd9Sstevel@tonic-gate  *	Note:	All args are assumed to be positive.  If
96*7c478bd9Sstevel@tonic-gate  *	the last divide results in something bigger than
97*7c478bd9Sstevel@tonic-gate  *	a long, then -1 is returned instead.
98*7c478bd9Sstevel@tonic-gate  */
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate int
_hrtnewres(hrtimer_t * htp,ulong_t new_res,long round)101*7c478bd9Sstevel@tonic-gate _hrtnewres(hrtimer_t *htp, ulong_t new_res, long round)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	long		interval;
104*7c478bd9Sstevel@tonic-gate 	longlong_t	dint;
105*7c478bd9Sstevel@tonic-gate 	longlong_t	dto_res;
106*7c478bd9Sstevel@tonic-gate 	longlong_t	drem;
107*7c478bd9Sstevel@tonic-gate 	longlong_t	dfrom_res;
108*7c478bd9Sstevel@tonic-gate 	longlong_t	prod;
109*7c478bd9Sstevel@tonic-gate 	longlong_t	quot;
110*7c478bd9Sstevel@tonic-gate 	long		numerator;
111*7c478bd9Sstevel@tonic-gate 	long		result;
112*7c478bd9Sstevel@tonic-gate 	ulong_t		modulus;
113*7c478bd9Sstevel@tonic-gate 	ulong_t		twomodulus;
114*7c478bd9Sstevel@tonic-gate 	long		temp;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	if (htp->hrt_res == 0 || new_res == 0 ||
117*7c478bd9Sstevel@tonic-gate 	    new_res > NANOSEC || htp->hrt_rem < 0)
118*7c478bd9Sstevel@tonic-gate 		return (-1);
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	if (htp->hrt_rem >= htp->hrt_res) {
121*7c478bd9Sstevel@tonic-gate 		htp->hrt_secs += htp->hrt_rem / htp->hrt_res;
122*7c478bd9Sstevel@tonic-gate 		htp->hrt_rem = htp->hrt_rem % htp->hrt_res;
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	interval = htp->hrt_rem;
126*7c478bd9Sstevel@tonic-gate 	if (interval == 0) {
127*7c478bd9Sstevel@tonic-gate 		htp->hrt_res = new_res;
128*7c478bd9Sstevel@tonic-gate 		return (0);
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	/*
132*7c478bd9Sstevel@tonic-gate 	 *	Try to do the calculations in single precision first
133*7c478bd9Sstevel@tonic-gate 	 *	(for speed).  If they overflow, use double precision.
134*7c478bd9Sstevel@tonic-gate 	 *	What we want to compute is:
135*7c478bd9Sstevel@tonic-gate 	 *
136*7c478bd9Sstevel@tonic-gate 	 *		(interval * new_res) / hrt->hrt_res
137*7c478bd9Sstevel@tonic-gate 	 */
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	numerator = interval * new_res;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	if (numerator / new_res  ==  interval) {
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 		/*
144*7c478bd9Sstevel@tonic-gate 		 *	The above multiply didn't give overflow since
145*7c478bd9Sstevel@tonic-gate 		 *	the division got back the original number.  Go
146*7c478bd9Sstevel@tonic-gate 		 *	ahead and compute the result.
147*7c478bd9Sstevel@tonic-gate 		 */
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		result = numerator / htp->hrt_res;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 		/*
152*7c478bd9Sstevel@tonic-gate 		 *	For HRT_RND, compute the value of:
153*7c478bd9Sstevel@tonic-gate 		 *
154*7c478bd9Sstevel@tonic-gate 		 *		(interval * new_res) % htp->hrt_res
155*7c478bd9Sstevel@tonic-gate 		 *
156*7c478bd9Sstevel@tonic-gate 		 *	If it is greater than half of the htp->hrt_res,
157*7c478bd9Sstevel@tonic-gate 		 *	then rounding increases the result by 1.
158*7c478bd9Sstevel@tonic-gate 		 *
159*7c478bd9Sstevel@tonic-gate 		 *	For HRT_RNDUP, we increase the result by 1 if:
160*7c478bd9Sstevel@tonic-gate 		 *
161*7c478bd9Sstevel@tonic-gate 		 *		result * htp->hrt_res != numerator
162*7c478bd9Sstevel@tonic-gate 		 *
163*7c478bd9Sstevel@tonic-gate 		 *	because this tells us we truncated when calculating
164*7c478bd9Sstevel@tonic-gate 		 *	result above.
165*7c478bd9Sstevel@tonic-gate 		 *
166*7c478bd9Sstevel@tonic-gate 		 *	We also check for overflow when incrementing result
167*7c478bd9Sstevel@tonic-gate 		 *	although this is extremely rare.
168*7c478bd9Sstevel@tonic-gate 		 */
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 		if (round == HRT_RND) {
171*7c478bd9Sstevel@tonic-gate 			modulus = numerator - result * htp->hrt_res;
172*7c478bd9Sstevel@tonic-gate 			if ((twomodulus = 2 * modulus) / 2 == modulus) {
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 				/*
175*7c478bd9Sstevel@tonic-gate 				 * No overflow (if we overflow in calculation
176*7c478bd9Sstevel@tonic-gate 				 * of twomodulus we fall through and use
177*7c478bd9Sstevel@tonic-gate 				 * double precision).
178*7c478bd9Sstevel@tonic-gate 				 */
179*7c478bd9Sstevel@tonic-gate 				if (twomodulus >= htp->hrt_res) {
180*7c478bd9Sstevel@tonic-gate 					temp = result + 1;
181*7c478bd9Sstevel@tonic-gate 					if (temp - 1 == result)
182*7c478bd9Sstevel@tonic-gate 						result++;
183*7c478bd9Sstevel@tonic-gate 					else
184*7c478bd9Sstevel@tonic-gate 						return (-1);
185*7c478bd9Sstevel@tonic-gate 				}
186*7c478bd9Sstevel@tonic-gate 				htp->hrt_res = new_res;
187*7c478bd9Sstevel@tonic-gate 				htp->hrt_rem = result;
188*7c478bd9Sstevel@tonic-gate 				return (0);
189*7c478bd9Sstevel@tonic-gate 			}
190*7c478bd9Sstevel@tonic-gate 		} else if (round == HRT_RNDUP) {
191*7c478bd9Sstevel@tonic-gate 			if (result * htp->hrt_res != numerator) {
192*7c478bd9Sstevel@tonic-gate 				temp = result + 1;
193*7c478bd9Sstevel@tonic-gate 				if (temp - 1 == result)
194*7c478bd9Sstevel@tonic-gate 					result++;
195*7c478bd9Sstevel@tonic-gate 				else
196*7c478bd9Sstevel@tonic-gate 					return (-1);
197*7c478bd9Sstevel@tonic-gate 			}
198*7c478bd9Sstevel@tonic-gate 			htp->hrt_res = new_res;
199*7c478bd9Sstevel@tonic-gate 			htp->hrt_rem = result;
200*7c478bd9Sstevel@tonic-gate 			return (0);
201*7c478bd9Sstevel@tonic-gate 		} else {	/* round == HRT_TRUNC */
202*7c478bd9Sstevel@tonic-gate 			htp->hrt_res = new_res;
203*7c478bd9Sstevel@tonic-gate 			htp->hrt_rem = result;
204*7c478bd9Sstevel@tonic-gate 			return (0);
205*7c478bd9Sstevel@tonic-gate 		}
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	/*
209*7c478bd9Sstevel@tonic-gate 	 *	We would get overflow doing the calculation is
210*7c478bd9Sstevel@tonic-gate 	 *	single precision so do it the slow but careful way.
211*7c478bd9Sstevel@tonic-gate 	 *
212*7c478bd9Sstevel@tonic-gate 	 *	Compute the interval times the resolution we are
213*7c478bd9Sstevel@tonic-gate 	 *	going to.
214*7c478bd9Sstevel@tonic-gate 	 */
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	dint = interval;
217*7c478bd9Sstevel@tonic-gate 	dto_res = new_res;
218*7c478bd9Sstevel@tonic-gate 	prod = dint * dto_res;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	/*
221*7c478bd9Sstevel@tonic-gate 	 *	For HRT_RND the result will be equal to:
222*7c478bd9Sstevel@tonic-gate 	 *
223*7c478bd9Sstevel@tonic-gate 	 *		((interval * new_res) + htp->hrt_res / 2) / htp->hrt_res
224*7c478bd9Sstevel@tonic-gate 	 *
225*7c478bd9Sstevel@tonic-gate 	 *	and for HRT_RNDUP we use:
226*7c478bd9Sstevel@tonic-gate 	 *
227*7c478bd9Sstevel@tonic-gate 	 *		((interval * new_res) + htp->hrt_res - 1) / htp->hrt_res
228*7c478bd9Sstevel@tonic-gate 	 *
229*7c478bd9Sstevel@tonic-gate 	 * 	This is a different but equivalent way of rounding.
230*7c478bd9Sstevel@tonic-gate 	 */
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	if (round == HRT_RND) {
233*7c478bd9Sstevel@tonic-gate 		drem = htp->hrt_res / 2;
234*7c478bd9Sstevel@tonic-gate 		prod = prod + drem;
235*7c478bd9Sstevel@tonic-gate 	} else if (round == HRT_RNDUP) {
236*7c478bd9Sstevel@tonic-gate 		drem = htp->hrt_res - 1;
237*7c478bd9Sstevel@tonic-gate 		prod = prod + drem;
238*7c478bd9Sstevel@tonic-gate 	}
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	dfrom_res = htp->hrt_res;
241*7c478bd9Sstevel@tonic-gate 	quot = prod / dfrom_res;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	/*
244*7c478bd9Sstevel@tonic-gate 	 *	If the quotient won't fit in a long, then we have
245*7c478bd9Sstevel@tonic-gate 	 *	overflow.  Otherwise, return the result.
246*7c478bd9Sstevel@tonic-gate 	 */
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	if (quot > UINT_MAX) {
249*7c478bd9Sstevel@tonic-gate 		return (-1);
250*7c478bd9Sstevel@tonic-gate 	} else {
251*7c478bd9Sstevel@tonic-gate 		htp->hrt_res = new_res;
252*7c478bd9Sstevel@tonic-gate 		htp->hrt_rem = (int)quot;
253*7c478bd9Sstevel@tonic-gate 		return (0);
254*7c478bd9Sstevel@tonic-gate 	}
255*7c478bd9Sstevel@tonic-gate }
256