xref: /illumos-gate/usr/src/lib/libkstat/common/kstat.c (revision 7c478bd9)
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 /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
33*7c478bd9Sstevel@tonic-gate #include <ctype.h>
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <memory.h>
36*7c478bd9Sstevel@tonic-gate #include <strings.h>
37*7c478bd9Sstevel@tonic-gate #include <string.h>
38*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate #include <poll.h>
41*7c478bd9Sstevel@tonic-gate #include "kstat.h"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static void
46*7c478bd9Sstevel@tonic-gate kstat_zalloc(void **ptr, size_t size, int free_first)
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate 	if (free_first)
49*7c478bd9Sstevel@tonic-gate 		free(*ptr);
50*7c478bd9Sstevel@tonic-gate 	*ptr = calloc(size, 1);
51*7c478bd9Sstevel@tonic-gate }
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static void
54*7c478bd9Sstevel@tonic-gate kstat_chain_free(kstat_ctl_t *kc)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	kstat_t *ksp, *nksp;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	ksp = kc->kc_chain;
59*7c478bd9Sstevel@tonic-gate 	while (ksp) {
60*7c478bd9Sstevel@tonic-gate 		nksp = ksp->ks_next;
61*7c478bd9Sstevel@tonic-gate 		free(ksp->ks_data);
62*7c478bd9Sstevel@tonic-gate 		free(ksp);
63*7c478bd9Sstevel@tonic-gate 		ksp = nksp;
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 	kc->kc_chain = NULL;
66*7c478bd9Sstevel@tonic-gate 	kc->kc_chain_id = 0;
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate kstat_ctl_t *
70*7c478bd9Sstevel@tonic-gate kstat_open(void)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kc;
73*7c478bd9Sstevel@tonic-gate 	int kd;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	kd = open("/dev/kstat", O_RDONLY);
76*7c478bd9Sstevel@tonic-gate 	if (kd == -1)
77*7c478bd9Sstevel@tonic-gate 		return (NULL);
78*7c478bd9Sstevel@tonic-gate 	kstat_zalloc((void **)&kc, sizeof (kstat_ctl_t), 0);
79*7c478bd9Sstevel@tonic-gate 	if (kc == NULL)
80*7c478bd9Sstevel@tonic-gate 		return (NULL);
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	kc->kc_kd = kd;
83*7c478bd9Sstevel@tonic-gate 	kc->kc_chain = NULL;
84*7c478bd9Sstevel@tonic-gate 	kc->kc_chain_id = 0;
85*7c478bd9Sstevel@tonic-gate 	if (kstat_chain_update(kc) == -1) {
86*7c478bd9Sstevel@tonic-gate 		int saved_err = errno;
87*7c478bd9Sstevel@tonic-gate 		(void) kstat_close(kc);
88*7c478bd9Sstevel@tonic-gate 		errno = saved_err;
89*7c478bd9Sstevel@tonic-gate 		return (NULL);
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 	return (kc);
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate int
95*7c478bd9Sstevel@tonic-gate kstat_close(kstat_ctl_t *kc)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	int rc;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	kstat_chain_free(kc);
100*7c478bd9Sstevel@tonic-gate 	rc = close(kc->kc_kd);
101*7c478bd9Sstevel@tonic-gate 	free(kc);
102*7c478bd9Sstevel@tonic-gate 	return (rc);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate kid_t
106*7c478bd9Sstevel@tonic-gate kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	kid_t kcid;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	if (ksp->ks_data == NULL && ksp->ks_data_size > 0) {
111*7c478bd9Sstevel@tonic-gate 		kstat_zalloc(&ksp->ks_data, ksp->ks_data_size, 0);
112*7c478bd9Sstevel@tonic-gate 		if (ksp->ks_data == NULL)
113*7c478bd9Sstevel@tonic-gate 			return (-1);
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 	while ((kcid = (kid_t)ioctl(kc->kc_kd, KSTAT_IOC_READ, ksp)) == -1) {
116*7c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN) {
117*7c478bd9Sstevel@tonic-gate 			(void) poll(NULL, 0, 100);	/* back off a moment */
118*7c478bd9Sstevel@tonic-gate 			continue;			/* and try again */
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 		/*
121*7c478bd9Sstevel@tonic-gate 		 * Mating dance for variable-size kstats.
122*7c478bd9Sstevel@tonic-gate 		 * You start with a buffer of a certain size,
123*7c478bd9Sstevel@tonic-gate 		 * which you hope will hold all the data.
124*7c478bd9Sstevel@tonic-gate 		 * If your buffer is too small, the kstat driver
125*7c478bd9Sstevel@tonic-gate 		 * returns ENOMEM and sets ksp->ks_data_size to
126*7c478bd9Sstevel@tonic-gate 		 * the current size of the kstat's data.  You then
127*7c478bd9Sstevel@tonic-gate 		 * resize your buffer and try again.  In practice,
128*7c478bd9Sstevel@tonic-gate 		 * this almost always converges in two passes.
129*7c478bd9Sstevel@tonic-gate 		 */
130*7c478bd9Sstevel@tonic-gate 		if (errno == ENOMEM && (ksp->ks_flags & KSTAT_FLAG_VAR_SIZE)) {
131*7c478bd9Sstevel@tonic-gate 			kstat_zalloc(&ksp->ks_data, ksp->ks_data_size, 1);
132*7c478bd9Sstevel@tonic-gate 			if (ksp->ks_data != NULL)
133*7c478bd9Sstevel@tonic-gate 				continue;
134*7c478bd9Sstevel@tonic-gate 		}
135*7c478bd9Sstevel@tonic-gate 		return (-1);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 	if (data != NULL) {
138*7c478bd9Sstevel@tonic-gate 		(void) memcpy(data, ksp->ks_data, ksp->ks_data_size);
139*7c478bd9Sstevel@tonic-gate 		if (ksp->ks_type == KSTAT_TYPE_NAMED &&
140*7c478bd9Sstevel@tonic-gate 		    ksp->ks_data_size !=
141*7c478bd9Sstevel@tonic-gate 		    ksp->ks_ndata * sizeof (kstat_named_t)) {
142*7c478bd9Sstevel@tonic-gate 			/*
143*7c478bd9Sstevel@tonic-gate 			 * Has KSTAT_DATA_STRING fields. Fix the pointers.
144*7c478bd9Sstevel@tonic-gate 			 */
145*7c478bd9Sstevel@tonic-gate 			uint_t i;
146*7c478bd9Sstevel@tonic-gate 			kstat_named_t *knp = data;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < ksp->ks_ndata; i++, knp++) {
149*7c478bd9Sstevel@tonic-gate 				if (knp->data_type != KSTAT_DATA_STRING)
150*7c478bd9Sstevel@tonic-gate 					continue;
151*7c478bd9Sstevel@tonic-gate 				if (KSTAT_NAMED_STR_PTR(knp) == NULL)
152*7c478bd9Sstevel@tonic-gate 					continue;
153*7c478bd9Sstevel@tonic-gate 				/*
154*7c478bd9Sstevel@tonic-gate 				 * The offsets of the strings within the
155*7c478bd9Sstevel@tonic-gate 				 * buffers are the same, so add the offset of
156*7c478bd9Sstevel@tonic-gate 				 * the string to the beginning of 'data' to fix
157*7c478bd9Sstevel@tonic-gate 				 * the pointer so that strings in 'data' don't
158*7c478bd9Sstevel@tonic-gate 				 * point at memory in 'ksp->ks_data'.
159*7c478bd9Sstevel@tonic-gate 				 */
160*7c478bd9Sstevel@tonic-gate 				KSTAT_NAMED_STR_PTR(knp) = (char *)data +
161*7c478bd9Sstevel@tonic-gate 				    (KSTAT_NAMED_STR_PTR(knp) -
162*7c478bd9Sstevel@tonic-gate 				    (char *)ksp->ks_data);
163*7c478bd9Sstevel@tonic-gate 			}
164*7c478bd9Sstevel@tonic-gate 		}
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 	return (kcid);
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate kid_t
170*7c478bd9Sstevel@tonic-gate kstat_write(kstat_ctl_t *kc, kstat_t *ksp, void *data)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate 	kid_t kcid;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if (ksp->ks_data == NULL && ksp->ks_data_size > 0) {
175*7c478bd9Sstevel@tonic-gate 		kstat_zalloc(&ksp->ks_data, ksp->ks_data_size, 0);
176*7c478bd9Sstevel@tonic-gate 		if (ksp->ks_data == NULL)
177*7c478bd9Sstevel@tonic-gate 			return (-1);
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 	if (data != NULL) {
180*7c478bd9Sstevel@tonic-gate 		(void) memcpy(ksp->ks_data, data, ksp->ks_data_size);
181*7c478bd9Sstevel@tonic-gate 		if (ksp->ks_type == KSTAT_TYPE_NAMED) {
182*7c478bd9Sstevel@tonic-gate 			kstat_named_t *oknp = data;
183*7c478bd9Sstevel@tonic-gate 			kstat_named_t *nknp = KSTAT_NAMED_PTR(ksp);
184*7c478bd9Sstevel@tonic-gate 			uint_t i;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < ksp->ks_ndata; i++, oknp++, nknp++) {
187*7c478bd9Sstevel@tonic-gate 				if (nknp->data_type != KSTAT_DATA_STRING)
188*7c478bd9Sstevel@tonic-gate 					continue;
189*7c478bd9Sstevel@tonic-gate 				if (KSTAT_NAMED_STR_PTR(nknp) == NULL)
190*7c478bd9Sstevel@tonic-gate 					continue;
191*7c478bd9Sstevel@tonic-gate 				/*
192*7c478bd9Sstevel@tonic-gate 				 * The buffer passed in as 'data' has string
193*7c478bd9Sstevel@tonic-gate 				 * pointers that point within 'data'.  Fix the
194*7c478bd9Sstevel@tonic-gate 				 * pointers so they point into the same offset
195*7c478bd9Sstevel@tonic-gate 				 * within the newly allocated buffer.
196*7c478bd9Sstevel@tonic-gate 				 */
197*7c478bd9Sstevel@tonic-gate 				KSTAT_NAMED_STR_PTR(nknp) =
198*7c478bd9Sstevel@tonic-gate 				    (char *)ksp->ks_data +
199*7c478bd9Sstevel@tonic-gate 				    (KSTAT_NAMED_STR_PTR(oknp) - (char *)data);
200*7c478bd9Sstevel@tonic-gate 			}
201*7c478bd9Sstevel@tonic-gate 		}
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate 	while ((kcid = (kid_t)ioctl(kc->kc_kd, KSTAT_IOC_WRITE, ksp)) == -1) {
205*7c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN) {
206*7c478bd9Sstevel@tonic-gate 			(void) poll(NULL, 0, 100);	/* back off a moment */
207*7c478bd9Sstevel@tonic-gate 			continue;			/* and try again */
208*7c478bd9Sstevel@tonic-gate 		}
209*7c478bd9Sstevel@tonic-gate 		break;
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 	return (kcid);
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate /*
215*7c478bd9Sstevel@tonic-gate  * If the current KCID is the same as kc->kc_chain_id, return 0;
216*7c478bd9Sstevel@tonic-gate  * if different, update the chain and return the new KCID.
217*7c478bd9Sstevel@tonic-gate  * This operation is non-destructive for unchanged kstats.
218*7c478bd9Sstevel@tonic-gate  */
219*7c478bd9Sstevel@tonic-gate kid_t
220*7c478bd9Sstevel@tonic-gate kstat_chain_update(kstat_ctl_t *kc)
221*7c478bd9Sstevel@tonic-gate {
222*7c478bd9Sstevel@tonic-gate 	kstat_t k0, *headers, *oksp, *nksp, **okspp, *next;
223*7c478bd9Sstevel@tonic-gate 	int i;
224*7c478bd9Sstevel@tonic-gate 	kid_t kcid;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	kcid = (kid_t)ioctl(kc->kc_kd, KSTAT_IOC_CHAIN_ID, NULL);
227*7c478bd9Sstevel@tonic-gate 	if (kcid == -1)
228*7c478bd9Sstevel@tonic-gate 		return (-1);
229*7c478bd9Sstevel@tonic-gate 	if (kcid == kc->kc_chain_id)
230*7c478bd9Sstevel@tonic-gate 		return (0);
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	/*
233*7c478bd9Sstevel@tonic-gate 	 * kstat 0's data is the kstat chain, so we can get the chain
234*7c478bd9Sstevel@tonic-gate 	 * by doing a kstat_read() of this kstat.  The only fields the
235*7c478bd9Sstevel@tonic-gate 	 * kstat driver needs are ks_kid (this identifies the kstat),
236*7c478bd9Sstevel@tonic-gate 	 * ks_data (the pointer to our buffer), and ks_data_size (the
237*7c478bd9Sstevel@tonic-gate 	 * size of our buffer).  By zeroing the struct we set ks_data = NULL
238*7c478bd9Sstevel@tonic-gate 	 * and ks_data_size = 0, so that kstat_read() will automatically
239*7c478bd9Sstevel@tonic-gate 	 * determine the size and allocate space for us.  We also fill in the
240*7c478bd9Sstevel@tonic-gate 	 * name, so that truss can print something meaningful.
241*7c478bd9Sstevel@tonic-gate 	 */
242*7c478bd9Sstevel@tonic-gate 	bzero(&k0, sizeof (k0));
243*7c478bd9Sstevel@tonic-gate 	(void) strcpy(k0.ks_name, "kstat_headers");
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	kcid = kstat_read(kc, &k0, NULL);
246*7c478bd9Sstevel@tonic-gate 	if (kcid == -1) {
247*7c478bd9Sstevel@tonic-gate 		free(k0.ks_data);
248*7c478bd9Sstevel@tonic-gate 		/* errno set by kstat_read() */
249*7c478bd9Sstevel@tonic-gate 		return (-1);
250*7c478bd9Sstevel@tonic-gate 	}
251*7c478bd9Sstevel@tonic-gate 	headers = k0.ks_data;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	/*
254*7c478bd9Sstevel@tonic-gate 	 * Chain the new headers together
255*7c478bd9Sstevel@tonic-gate 	 */
256*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < k0.ks_ndata; i++)
257*7c478bd9Sstevel@tonic-gate 		headers[i - 1].ks_next = &headers[i];
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	headers[k0.ks_ndata - 1].ks_next = NULL;
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 	/*
262*7c478bd9Sstevel@tonic-gate 	 * Remove all deleted kstats from the chain.
263*7c478bd9Sstevel@tonic-gate 	 */
264*7c478bd9Sstevel@tonic-gate 	nksp = headers;
265*7c478bd9Sstevel@tonic-gate 	okspp = &kc->kc_chain;
266*7c478bd9Sstevel@tonic-gate 	oksp = kc->kc_chain;
267*7c478bd9Sstevel@tonic-gate 	while (oksp != NULL) {
268*7c478bd9Sstevel@tonic-gate 		next = oksp->ks_next;
269*7c478bd9Sstevel@tonic-gate 		if (nksp != NULL && oksp->ks_kid == nksp->ks_kid) {
270*7c478bd9Sstevel@tonic-gate 			okspp = &oksp->ks_next;
271*7c478bd9Sstevel@tonic-gate 			nksp = nksp->ks_next;
272*7c478bd9Sstevel@tonic-gate 		} else {
273*7c478bd9Sstevel@tonic-gate 			*okspp = oksp->ks_next;
274*7c478bd9Sstevel@tonic-gate 			free(oksp->ks_data);
275*7c478bd9Sstevel@tonic-gate 			free(oksp);
276*7c478bd9Sstevel@tonic-gate 		}
277*7c478bd9Sstevel@tonic-gate 		oksp = next;
278*7c478bd9Sstevel@tonic-gate 	}
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	/*
281*7c478bd9Sstevel@tonic-gate 	 * Add all new kstats to the chain.
282*7c478bd9Sstevel@tonic-gate 	 */
283*7c478bd9Sstevel@tonic-gate 	while (nksp != NULL) {
284*7c478bd9Sstevel@tonic-gate 		kstat_zalloc((void **)okspp, sizeof (kstat_t), 0);
285*7c478bd9Sstevel@tonic-gate 		if ((oksp = *okspp) == NULL) {
286*7c478bd9Sstevel@tonic-gate 			free(headers);
287*7c478bd9Sstevel@tonic-gate 			return (-1);
288*7c478bd9Sstevel@tonic-gate 		}
289*7c478bd9Sstevel@tonic-gate 		*oksp = *nksp;
290*7c478bd9Sstevel@tonic-gate 		okspp = &oksp->ks_next;
291*7c478bd9Sstevel@tonic-gate 		oksp->ks_next = NULL;
292*7c478bd9Sstevel@tonic-gate 		oksp->ks_data = NULL;
293*7c478bd9Sstevel@tonic-gate 		nksp = nksp->ks_next;
294*7c478bd9Sstevel@tonic-gate 	}
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	free(headers);
297*7c478bd9Sstevel@tonic-gate 	kc->kc_chain_id = kcid;
298*7c478bd9Sstevel@tonic-gate 	return (kcid);
299*7c478bd9Sstevel@tonic-gate }
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate kstat_t *
302*7c478bd9Sstevel@tonic-gate kstat_lookup(kstat_ctl_t *kc, char *ks_module, int ks_instance, char *ks_name)
303*7c478bd9Sstevel@tonic-gate {
304*7c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
307*7c478bd9Sstevel@tonic-gate 		if ((ks_module == NULL ||
308*7c478bd9Sstevel@tonic-gate 		    strcmp(ksp->ks_module, ks_module) == 0) &&
309*7c478bd9Sstevel@tonic-gate 		    (ks_instance == -1 || ksp->ks_instance == ks_instance) &&
310*7c478bd9Sstevel@tonic-gate 		    (ks_name == NULL || strcmp(ksp->ks_name, ks_name) == 0))
311*7c478bd9Sstevel@tonic-gate 			return (ksp);
312*7c478bd9Sstevel@tonic-gate 	}
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 	errno = ENOENT;
315*7c478bd9Sstevel@tonic-gate 	return (NULL);
316*7c478bd9Sstevel@tonic-gate }
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate void *
319*7c478bd9Sstevel@tonic-gate kstat_data_lookup(kstat_t *ksp, char *name)
320*7c478bd9Sstevel@tonic-gate {
321*7c478bd9Sstevel@tonic-gate 	int i, size;
322*7c478bd9Sstevel@tonic-gate 	char *namep, *datap;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	switch (ksp->ks_type) {
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 	case KSTAT_TYPE_NAMED:
327*7c478bd9Sstevel@tonic-gate 		size = sizeof (kstat_named_t);
328*7c478bd9Sstevel@tonic-gate 		namep = KSTAT_NAMED_PTR(ksp)->name;
329*7c478bd9Sstevel@tonic-gate 		break;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	case KSTAT_TYPE_TIMER:
332*7c478bd9Sstevel@tonic-gate 		size = sizeof (kstat_timer_t);
333*7c478bd9Sstevel@tonic-gate 		namep = KSTAT_TIMER_PTR(ksp)->name;
334*7c478bd9Sstevel@tonic-gate 		break;
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate 	default:
337*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
338*7c478bd9Sstevel@tonic-gate 		return (NULL);
339*7c478bd9Sstevel@tonic-gate 	}
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	datap = ksp->ks_data;
342*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ksp->ks_ndata; i++) {
343*7c478bd9Sstevel@tonic-gate 		if (strcmp(name, namep) == 0)
344*7c478bd9Sstevel@tonic-gate 			return (datap);
345*7c478bd9Sstevel@tonic-gate 		namep += size;
346*7c478bd9Sstevel@tonic-gate 		datap += size;
347*7c478bd9Sstevel@tonic-gate 	}
348*7c478bd9Sstevel@tonic-gate 	errno = ENOENT;
349*7c478bd9Sstevel@tonic-gate 	return (NULL);
350*7c478bd9Sstevel@tonic-gate }
351