xref: /illumos-gate/usr/src/cmd/dispadmin/subr.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
37*7c478bd9Sstevel@tonic-gate #include <limits.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "dispadmin.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * Utility functions for dispadmin command.
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate void
fatalerr(const char * format,...)48*7c478bd9Sstevel@tonic-gate fatalerr(const char *format, ...)
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	va_list ap;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	(void) va_start(ap, format);
53*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
54*7c478bd9Sstevel@tonic-gate 	va_end(ap);
55*7c478bd9Sstevel@tonic-gate 	exit(1);
56*7c478bd9Sstevel@tonic-gate }
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * hrtconvert() returns the interval specified by htp as a single
61*7c478bd9Sstevel@tonic-gate  * value in resolution htp->hrt_res.  Returns -1 on overflow.
62*7c478bd9Sstevel@tonic-gate  */
63*7c478bd9Sstevel@tonic-gate long
hrtconvert(hrtimer_t * htp)64*7c478bd9Sstevel@tonic-gate hrtconvert(hrtimer_t *htp)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	long	sum;
67*7c478bd9Sstevel@tonic-gate 	long	product;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	product = htp->hrt_secs * htp->hrt_res;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	if (product / htp->hrt_res == htp->hrt_secs) {
72*7c478bd9Sstevel@tonic-gate 		sum = product + htp->hrt_rem;
73*7c478bd9Sstevel@tonic-gate 		if (sum - htp->hrt_rem == product) {
74*7c478bd9Sstevel@tonic-gate 			return (sum);
75*7c478bd9Sstevel@tonic-gate 		}
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 	return (-1);
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate /*
81*7c478bd9Sstevel@tonic-gate  * The following routine was removed from libc (libc/port/gen/hrtnewres.c).
82*7c478bd9Sstevel@tonic-gate  * It has also been added to priocntl, so if you fix it here, you should
83*7c478bd9Sstevel@tonic-gate  * also probably fix it there. In the long term, this should be recoded to
84*7c478bd9Sstevel@tonic-gate  * not be hrt'ish.
85*7c478bd9Sstevel@tonic-gate  */
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /*
88*7c478bd9Sstevel@tonic-gate  *	Convert interval expressed in htp->hrt_res to new_res.
89*7c478bd9Sstevel@tonic-gate  *
90*7c478bd9Sstevel@tonic-gate  *	Calculate: (interval * new_res) / htp->hrt_res  rounding off as
91*7c478bd9Sstevel@tonic-gate  *		specified by round.
92*7c478bd9Sstevel@tonic-gate  *
93*7c478bd9Sstevel@tonic-gate  *	Note:	All args are assumed to be positive.  If
94*7c478bd9Sstevel@tonic-gate  *	the last divide results in something bigger than
95*7c478bd9Sstevel@tonic-gate  *	a long, then -1 is returned instead.
96*7c478bd9Sstevel@tonic-gate  */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate int
_hrtnewres(hrtimer_t * htp,ulong_t new_res,long round)99*7c478bd9Sstevel@tonic-gate _hrtnewres(hrtimer_t *htp, ulong_t new_res, long round)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	long		interval;
102*7c478bd9Sstevel@tonic-gate 	longlong_t	dint;
103*7c478bd9Sstevel@tonic-gate 	longlong_t	dto_res;
104*7c478bd9Sstevel@tonic-gate 	longlong_t	drem;
105*7c478bd9Sstevel@tonic-gate 	longlong_t	dfrom_res;
106*7c478bd9Sstevel@tonic-gate 	longlong_t	prod;
107*7c478bd9Sstevel@tonic-gate 	longlong_t	quot;
108*7c478bd9Sstevel@tonic-gate 	long		numerator;
109*7c478bd9Sstevel@tonic-gate 	long		result;
110*7c478bd9Sstevel@tonic-gate 	ulong_t		modulus;
111*7c478bd9Sstevel@tonic-gate 	ulong_t		twomodulus;
112*7c478bd9Sstevel@tonic-gate 	long		temp;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (htp->hrt_res == 0 || new_res == 0 ||
115*7c478bd9Sstevel@tonic-gate 	    new_res > NANOSEC || htp->hrt_rem < 0)
116*7c478bd9Sstevel@tonic-gate 		return (-1);
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if (htp->hrt_rem >= htp->hrt_res) {
119*7c478bd9Sstevel@tonic-gate 		htp->hrt_secs += htp->hrt_rem / htp->hrt_res;
120*7c478bd9Sstevel@tonic-gate 		htp->hrt_rem = htp->hrt_rem % htp->hrt_res;
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	interval = htp->hrt_rem;
124*7c478bd9Sstevel@tonic-gate 	if (interval == 0) {
125*7c478bd9Sstevel@tonic-gate 		htp->hrt_res = new_res;
126*7c478bd9Sstevel@tonic-gate 		return (0);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	/*
130*7c478bd9Sstevel@tonic-gate 	 *	Try to do the calculations in single precision first
131*7c478bd9Sstevel@tonic-gate 	 *	(for speed).  If they overflow, use double precision.
132*7c478bd9Sstevel@tonic-gate 	 *	What we want to compute is:
133*7c478bd9Sstevel@tonic-gate 	 *
134*7c478bd9Sstevel@tonic-gate 	 *		(interval * new_res) / hrt->hrt_res
135*7c478bd9Sstevel@tonic-gate 	 */
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	numerator = interval * new_res;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	if (numerator / new_res  ==  interval) {
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 		/*
142*7c478bd9Sstevel@tonic-gate 		 *	The above multiply didn't give overflow since
143*7c478bd9Sstevel@tonic-gate 		 *	the division got back the original number.  Go
144*7c478bd9Sstevel@tonic-gate 		 *	ahead and compute the result.
145*7c478bd9Sstevel@tonic-gate 		 */
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 		result = numerator / htp->hrt_res;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		/*
150*7c478bd9Sstevel@tonic-gate 		 *	For HRT_RND, compute the value of:
151*7c478bd9Sstevel@tonic-gate 		 *
152*7c478bd9Sstevel@tonic-gate 		 *		(interval * new_res) % htp->hrt_res
153*7c478bd9Sstevel@tonic-gate 		 *
154*7c478bd9Sstevel@tonic-gate 		 *	If it is greater than half of the htp->hrt_res,
155*7c478bd9Sstevel@tonic-gate 		 *	then rounding increases the result by 1.
156*7c478bd9Sstevel@tonic-gate 		 *
157*7c478bd9Sstevel@tonic-gate 		 *	For HRT_RNDUP, we increase the result by 1 if:
158*7c478bd9Sstevel@tonic-gate 		 *
159*7c478bd9Sstevel@tonic-gate 		 *		result * htp->hrt_res != numerator
160*7c478bd9Sstevel@tonic-gate 		 *
161*7c478bd9Sstevel@tonic-gate 		 *	because this tells us we truncated when calculating
162*7c478bd9Sstevel@tonic-gate 		 *	result above.
163*7c478bd9Sstevel@tonic-gate 		 *
164*7c478bd9Sstevel@tonic-gate 		 *	We also check for overflow when incrementing result
165*7c478bd9Sstevel@tonic-gate 		 *	although this is extremely rare.
166*7c478bd9Sstevel@tonic-gate 		 */
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 		if (round == HRT_RND) {
169*7c478bd9Sstevel@tonic-gate 			modulus = numerator - result * htp->hrt_res;
170*7c478bd9Sstevel@tonic-gate 			if ((twomodulus = 2 * modulus) / 2 == modulus) {
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 				/*
173*7c478bd9Sstevel@tonic-gate 				 * No overflow (if we overflow in calculation
174*7c478bd9Sstevel@tonic-gate 				 * of twomodulus we fall through and use
175*7c478bd9Sstevel@tonic-gate 				 * double precision).
176*7c478bd9Sstevel@tonic-gate 				 */
177*7c478bd9Sstevel@tonic-gate 				if (twomodulus >= htp->hrt_res) {
178*7c478bd9Sstevel@tonic-gate 					temp = result + 1;
179*7c478bd9Sstevel@tonic-gate 					if (temp - 1 == result)
180*7c478bd9Sstevel@tonic-gate 						result++;
181*7c478bd9Sstevel@tonic-gate 					else
182*7c478bd9Sstevel@tonic-gate 						return (-1);
183*7c478bd9Sstevel@tonic-gate 				}
184*7c478bd9Sstevel@tonic-gate 				htp->hrt_res = new_res;
185*7c478bd9Sstevel@tonic-gate 				htp->hrt_rem = result;
186*7c478bd9Sstevel@tonic-gate 				return (0);
187*7c478bd9Sstevel@tonic-gate 			}
188*7c478bd9Sstevel@tonic-gate 		} else if (round == HRT_RNDUP) {
189*7c478bd9Sstevel@tonic-gate 			if (result * htp->hrt_res != numerator) {
190*7c478bd9Sstevel@tonic-gate 				temp = result + 1;
191*7c478bd9Sstevel@tonic-gate 				if (temp - 1 == result)
192*7c478bd9Sstevel@tonic-gate 					result++;
193*7c478bd9Sstevel@tonic-gate 				else
194*7c478bd9Sstevel@tonic-gate 					return (-1);
195*7c478bd9Sstevel@tonic-gate 			}
196*7c478bd9Sstevel@tonic-gate 			htp->hrt_res = new_res;
197*7c478bd9Sstevel@tonic-gate 			htp->hrt_rem = result;
198*7c478bd9Sstevel@tonic-gate 			return (0);
199*7c478bd9Sstevel@tonic-gate 		} else {	/* round == HRT_TRUNC */
200*7c478bd9Sstevel@tonic-gate 			htp->hrt_res = new_res;
201*7c478bd9Sstevel@tonic-gate 			htp->hrt_rem = result;
202*7c478bd9Sstevel@tonic-gate 			return (0);
203*7c478bd9Sstevel@tonic-gate 		}
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	/*
207*7c478bd9Sstevel@tonic-gate 	 *	We would get overflow doing the calculation is
208*7c478bd9Sstevel@tonic-gate 	 *	single precision so do it the slow but careful way.
209*7c478bd9Sstevel@tonic-gate 	 *
210*7c478bd9Sstevel@tonic-gate 	 *	Compute the interval times the resolution we are
211*7c478bd9Sstevel@tonic-gate 	 *	going to.
212*7c478bd9Sstevel@tonic-gate 	 */
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	dint = interval;
215*7c478bd9Sstevel@tonic-gate 	dto_res = new_res;
216*7c478bd9Sstevel@tonic-gate 	prod = dint * dto_res;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	/*
219*7c478bd9Sstevel@tonic-gate 	 *	For HRT_RND the result will be equal to:
220*7c478bd9Sstevel@tonic-gate 	 *
221*7c478bd9Sstevel@tonic-gate 	 *		((interval * new_res) + htp->hrt_res / 2) / htp->hrt_res
222*7c478bd9Sstevel@tonic-gate 	 *
223*7c478bd9Sstevel@tonic-gate 	 *	and for HRT_RNDUP we use:
224*7c478bd9Sstevel@tonic-gate 	 *
225*7c478bd9Sstevel@tonic-gate 	 *		((interval * new_res) + htp->hrt_res - 1) / htp->hrt_res
226*7c478bd9Sstevel@tonic-gate 	 *
227*7c478bd9Sstevel@tonic-gate 	 * 	This is a different but equivalent way of rounding.
228*7c478bd9Sstevel@tonic-gate 	 */
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	if (round == HRT_RND) {
231*7c478bd9Sstevel@tonic-gate 		drem = htp->hrt_res / 2;
232*7c478bd9Sstevel@tonic-gate 		prod = prod + drem;
233*7c478bd9Sstevel@tonic-gate 	} else if (round == HRT_RNDUP) {
234*7c478bd9Sstevel@tonic-gate 		drem = htp->hrt_res - 1;
235*7c478bd9Sstevel@tonic-gate 		prod = prod + drem;
236*7c478bd9Sstevel@tonic-gate 	}
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	dfrom_res = htp->hrt_res;
239*7c478bd9Sstevel@tonic-gate 	quot = prod / dfrom_res;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	/*
242*7c478bd9Sstevel@tonic-gate 	 *	If the quotient won't fit in a long, then we have
243*7c478bd9Sstevel@tonic-gate 	 *	overflow.  Otherwise, return the result.
244*7c478bd9Sstevel@tonic-gate 	 */
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	if (quot > UINT_MAX) {
247*7c478bd9Sstevel@tonic-gate 		return (-1);
248*7c478bd9Sstevel@tonic-gate 	} else {
249*7c478bd9Sstevel@tonic-gate 		htp->hrt_res = new_res;
250*7c478bd9Sstevel@tonic-gate 		htp->hrt_rem = (int)quot;
251*7c478bd9Sstevel@tonic-gate 		return (0);
252*7c478bd9Sstevel@tonic-gate 	}
253*7c478bd9Sstevel@tonic-gate }
254