xref: /illumos-gate/usr/src/lib/libkstat/common/kstat.c (revision c0e96d86)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*c0e96d86SHans Rosenfeld  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <stdarg.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <memory.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <poll.h>
397c478bd9Sstevel@tonic-gate #include "kstat.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static void
kstat_zalloc(void ** ptr,size_t size,int free_first)447c478bd9Sstevel@tonic-gate kstat_zalloc(void **ptr, size_t size, int free_first)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	if (free_first)
477c478bd9Sstevel@tonic-gate 		free(*ptr);
487c478bd9Sstevel@tonic-gate 	*ptr = calloc(size, 1);
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static void
kstat_chain_free(kstat_ctl_t * kc)527c478bd9Sstevel@tonic-gate kstat_chain_free(kstat_ctl_t *kc)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	kstat_t *ksp, *nksp;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	ksp = kc->kc_chain;
577c478bd9Sstevel@tonic-gate 	while (ksp) {
587c478bd9Sstevel@tonic-gate 		nksp = ksp->ks_next;
597c478bd9Sstevel@tonic-gate 		free(ksp->ks_data);
607c478bd9Sstevel@tonic-gate 		free(ksp);
617c478bd9Sstevel@tonic-gate 		ksp = nksp;
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 	kc->kc_chain = NULL;
647c478bd9Sstevel@tonic-gate 	kc->kc_chain_id = 0;
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate kstat_ctl_t *
kstat_open(void)687c478bd9Sstevel@tonic-gate kstat_open(void)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kc;
717c478bd9Sstevel@tonic-gate 	int kd;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	kd = open("/dev/kstat", O_RDONLY);
747c478bd9Sstevel@tonic-gate 	if (kd == -1)
757c478bd9Sstevel@tonic-gate 		return (NULL);
767c478bd9Sstevel@tonic-gate 	kstat_zalloc((void **)&kc, sizeof (kstat_ctl_t), 0);
777c478bd9Sstevel@tonic-gate 	if (kc == NULL)
787c478bd9Sstevel@tonic-gate 		return (NULL);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	kc->kc_kd = kd;
817c478bd9Sstevel@tonic-gate 	kc->kc_chain = NULL;
827c478bd9Sstevel@tonic-gate 	kc->kc_chain_id = 0;
837c478bd9Sstevel@tonic-gate 	if (kstat_chain_update(kc) == -1) {
847c478bd9Sstevel@tonic-gate 		int saved_err = errno;
857c478bd9Sstevel@tonic-gate 		(void) kstat_close(kc);
867c478bd9Sstevel@tonic-gate 		errno = saved_err;
877c478bd9Sstevel@tonic-gate 		return (NULL);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	return (kc);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate int
kstat_close(kstat_ctl_t * kc)937c478bd9Sstevel@tonic-gate kstat_close(kstat_ctl_t *kc)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	int rc;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	kstat_chain_free(kc);
987c478bd9Sstevel@tonic-gate 	rc = close(kc->kc_kd);
997c478bd9Sstevel@tonic-gate 	free(kc);
1007c478bd9Sstevel@tonic-gate 	return (rc);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate kid_t
kstat_read(kstat_ctl_t * kc,kstat_t * ksp,void * data)1047c478bd9Sstevel@tonic-gate kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	kid_t kcid;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (ksp->ks_data == NULL && ksp->ks_data_size > 0) {
1097c478bd9Sstevel@tonic-gate 		kstat_zalloc(&ksp->ks_data, ksp->ks_data_size, 0);
1107c478bd9Sstevel@tonic-gate 		if (ksp->ks_data == NULL)
1117c478bd9Sstevel@tonic-gate 			return (-1);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	while ((kcid = (kid_t)ioctl(kc->kc_kd, KSTAT_IOC_READ, ksp)) == -1) {
1147c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN) {
1157c478bd9Sstevel@tonic-gate 			(void) poll(NULL, 0, 100);	/* back off a moment */
1167c478bd9Sstevel@tonic-gate 			continue;			/* and try again */
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 		/*
1197c478bd9Sstevel@tonic-gate 		 * Mating dance for variable-size kstats.
1207c478bd9Sstevel@tonic-gate 		 * You start with a buffer of a certain size,
1217c478bd9Sstevel@tonic-gate 		 * which you hope will hold all the data.
1227c478bd9Sstevel@tonic-gate 		 * If your buffer is too small, the kstat driver
1237c478bd9Sstevel@tonic-gate 		 * returns ENOMEM and sets ksp->ks_data_size to
1247c478bd9Sstevel@tonic-gate 		 * the current size of the kstat's data.  You then
1257c478bd9Sstevel@tonic-gate 		 * resize your buffer and try again.  In practice,
1267c478bd9Sstevel@tonic-gate 		 * this almost always converges in two passes.
1277c478bd9Sstevel@tonic-gate 		 */
128*c0e96d86SHans Rosenfeld 		if (errno == ENOMEM && (ksp->ks_flags &
129*c0e96d86SHans Rosenfeld 		    (KSTAT_FLAG_VAR_SIZE | KSTAT_FLAG_LONGSTRINGS))) {
1307c478bd9Sstevel@tonic-gate 			kstat_zalloc(&ksp->ks_data, ksp->ks_data_size, 1);
1317c478bd9Sstevel@tonic-gate 			if (ksp->ks_data != NULL)
1327c478bd9Sstevel@tonic-gate 				continue;
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		return (-1);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	if (data != NULL) {
1377c478bd9Sstevel@tonic-gate 		(void) memcpy(data, ksp->ks_data, ksp->ks_data_size);
1387c478bd9Sstevel@tonic-gate 		if (ksp->ks_type == KSTAT_TYPE_NAMED &&
1397c478bd9Sstevel@tonic-gate 		    ksp->ks_data_size !=
1407c478bd9Sstevel@tonic-gate 		    ksp->ks_ndata * sizeof (kstat_named_t)) {
1417c478bd9Sstevel@tonic-gate 			/*
1427c478bd9Sstevel@tonic-gate 			 * Has KSTAT_DATA_STRING fields. Fix the pointers.
1437c478bd9Sstevel@tonic-gate 			 */
1447c478bd9Sstevel@tonic-gate 			uint_t i;
1457c478bd9Sstevel@tonic-gate 			kstat_named_t *knp = data;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 			for (i = 0; i < ksp->ks_ndata; i++, knp++) {
1487c478bd9Sstevel@tonic-gate 				if (knp->data_type != KSTAT_DATA_STRING)
1497c478bd9Sstevel@tonic-gate 					continue;
1507c478bd9Sstevel@tonic-gate 				if (KSTAT_NAMED_STR_PTR(knp) == NULL)
1517c478bd9Sstevel@tonic-gate 					continue;
1527c478bd9Sstevel@tonic-gate 				/*
1537c478bd9Sstevel@tonic-gate 				 * The offsets of the strings within the
1547c478bd9Sstevel@tonic-gate 				 * buffers are the same, so add the offset of
1557c478bd9Sstevel@tonic-gate 				 * the string to the beginning of 'data' to fix
1567c478bd9Sstevel@tonic-gate 				 * the pointer so that strings in 'data' don't
1577c478bd9Sstevel@tonic-gate 				 * point at memory in 'ksp->ks_data'.
1587c478bd9Sstevel@tonic-gate 				 */
1597c478bd9Sstevel@tonic-gate 				KSTAT_NAMED_STR_PTR(knp) = (char *)data +
1607c478bd9Sstevel@tonic-gate 				    (KSTAT_NAMED_STR_PTR(knp) -
1617c478bd9Sstevel@tonic-gate 				    (char *)ksp->ks_data);
1627c478bd9Sstevel@tonic-gate 			}
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	return (kcid);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate kid_t
kstat_write(kstat_ctl_t * kc,kstat_t * ksp,void * data)1697c478bd9Sstevel@tonic-gate kstat_write(kstat_ctl_t *kc, kstat_t *ksp, void *data)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	kid_t kcid;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (ksp->ks_data == NULL && ksp->ks_data_size > 0) {
1747c478bd9Sstevel@tonic-gate 		kstat_zalloc(&ksp->ks_data, ksp->ks_data_size, 0);
1757c478bd9Sstevel@tonic-gate 		if (ksp->ks_data == NULL)
1767c478bd9Sstevel@tonic-gate 			return (-1);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 	if (data != NULL) {
1797c478bd9Sstevel@tonic-gate 		(void) memcpy(ksp->ks_data, data, ksp->ks_data_size);
1807c478bd9Sstevel@tonic-gate 		if (ksp->ks_type == KSTAT_TYPE_NAMED) {
1817c478bd9Sstevel@tonic-gate 			kstat_named_t *oknp = data;
1827c478bd9Sstevel@tonic-gate 			kstat_named_t *nknp = KSTAT_NAMED_PTR(ksp);
1837c478bd9Sstevel@tonic-gate 			uint_t i;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 			for (i = 0; i < ksp->ks_ndata; i++, oknp++, nknp++) {
1867c478bd9Sstevel@tonic-gate 				if (nknp->data_type != KSTAT_DATA_STRING)
1877c478bd9Sstevel@tonic-gate 					continue;
1887c478bd9Sstevel@tonic-gate 				if (KSTAT_NAMED_STR_PTR(nknp) == NULL)
1897c478bd9Sstevel@tonic-gate 					continue;
1907c478bd9Sstevel@tonic-gate 				/*
1917c478bd9Sstevel@tonic-gate 				 * The buffer passed in as 'data' has string
1927c478bd9Sstevel@tonic-gate 				 * pointers that point within 'data'.  Fix the
1937c478bd9Sstevel@tonic-gate 				 * pointers so they point into the same offset
1947c478bd9Sstevel@tonic-gate 				 * within the newly allocated buffer.
1957c478bd9Sstevel@tonic-gate 				 */
1967c478bd9Sstevel@tonic-gate 				KSTAT_NAMED_STR_PTR(nknp) =
1977c478bd9Sstevel@tonic-gate 				    (char *)ksp->ks_data +
1987c478bd9Sstevel@tonic-gate 				    (KSTAT_NAMED_STR_PTR(oknp) - (char *)data);
1997c478bd9Sstevel@tonic-gate 			}
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	while ((kcid = (kid_t)ioctl(kc->kc_kd, KSTAT_IOC_WRITE, ksp)) == -1) {
2047c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN) {
2057c478bd9Sstevel@tonic-gate 			(void) poll(NULL, 0, 100);	/* back off a moment */
2067c478bd9Sstevel@tonic-gate 			continue;			/* and try again */
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 		break;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	return (kcid);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * If the current KCID is the same as kc->kc_chain_id, return 0;
2157c478bd9Sstevel@tonic-gate  * if different, update the chain and return the new KCID.
2167c478bd9Sstevel@tonic-gate  * This operation is non-destructive for unchanged kstats.
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate kid_t
kstat_chain_update(kstat_ctl_t * kc)2197c478bd9Sstevel@tonic-gate kstat_chain_update(kstat_ctl_t *kc)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	kstat_t k0, *headers, *oksp, *nksp, **okspp, *next;
2227c478bd9Sstevel@tonic-gate 	int i;
2237c478bd9Sstevel@tonic-gate 	kid_t kcid;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	kcid = (kid_t)ioctl(kc->kc_kd, KSTAT_IOC_CHAIN_ID, NULL);
2267c478bd9Sstevel@tonic-gate 	if (kcid == -1)
2277c478bd9Sstevel@tonic-gate 		return (-1);
2287c478bd9Sstevel@tonic-gate 	if (kcid == kc->kc_chain_id)
2297c478bd9Sstevel@tonic-gate 		return (0);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * kstat 0's data is the kstat chain, so we can get the chain
2337c478bd9Sstevel@tonic-gate 	 * by doing a kstat_read() of this kstat.  The only fields the
2347c478bd9Sstevel@tonic-gate 	 * kstat driver needs are ks_kid (this identifies the kstat),
2357c478bd9Sstevel@tonic-gate 	 * ks_data (the pointer to our buffer), and ks_data_size (the
2367c478bd9Sstevel@tonic-gate 	 * size of our buffer).  By zeroing the struct we set ks_data = NULL
2377c478bd9Sstevel@tonic-gate 	 * and ks_data_size = 0, so that kstat_read() will automatically
2387c478bd9Sstevel@tonic-gate 	 * determine the size and allocate space for us.  We also fill in the
2397c478bd9Sstevel@tonic-gate 	 * name, so that truss can print something meaningful.
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 	bzero(&k0, sizeof (k0));
2427c478bd9Sstevel@tonic-gate 	(void) strcpy(k0.ks_name, "kstat_headers");
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	kcid = kstat_read(kc, &k0, NULL);
2457c478bd9Sstevel@tonic-gate 	if (kcid == -1) {
2467c478bd9Sstevel@tonic-gate 		free(k0.ks_data);
2477c478bd9Sstevel@tonic-gate 		/* errno set by kstat_read() */
2487c478bd9Sstevel@tonic-gate 		return (-1);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	headers = k0.ks_data;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Chain the new headers together
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 	for (i = 1; i < k0.ks_ndata; i++)
2567c478bd9Sstevel@tonic-gate 		headers[i - 1].ks_next = &headers[i];
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	headers[k0.ks_ndata - 1].ks_next = NULL;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/*
2617c478bd9Sstevel@tonic-gate 	 * Remove all deleted kstats from the chain.
2627c478bd9Sstevel@tonic-gate 	 */
2637c478bd9Sstevel@tonic-gate 	nksp = headers;
2647c478bd9Sstevel@tonic-gate 	okspp = &kc->kc_chain;
2657c478bd9Sstevel@tonic-gate 	oksp = kc->kc_chain;
2667c478bd9Sstevel@tonic-gate 	while (oksp != NULL) {
2677c478bd9Sstevel@tonic-gate 		next = oksp->ks_next;
2687c478bd9Sstevel@tonic-gate 		if (nksp != NULL && oksp->ks_kid == nksp->ks_kid) {
2697c478bd9Sstevel@tonic-gate 			okspp = &oksp->ks_next;
2707c478bd9Sstevel@tonic-gate 			nksp = nksp->ks_next;
2717c478bd9Sstevel@tonic-gate 		} else {
2727c478bd9Sstevel@tonic-gate 			*okspp = oksp->ks_next;
2737c478bd9Sstevel@tonic-gate 			free(oksp->ks_data);
2747c478bd9Sstevel@tonic-gate 			free(oksp);
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 		oksp = next;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/*
2807c478bd9Sstevel@tonic-gate 	 * Add all new kstats to the chain.
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 	while (nksp != NULL) {
2837c478bd9Sstevel@tonic-gate 		kstat_zalloc((void **)okspp, sizeof (kstat_t), 0);
2847c478bd9Sstevel@tonic-gate 		if ((oksp = *okspp) == NULL) {
2857c478bd9Sstevel@tonic-gate 			free(headers);
2867c478bd9Sstevel@tonic-gate 			return (-1);
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 		*oksp = *nksp;
2897c478bd9Sstevel@tonic-gate 		okspp = &oksp->ks_next;
2907c478bd9Sstevel@tonic-gate 		oksp->ks_next = NULL;
2917c478bd9Sstevel@tonic-gate 		oksp->ks_data = NULL;
2927c478bd9Sstevel@tonic-gate 		nksp = nksp->ks_next;
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	free(headers);
2967c478bd9Sstevel@tonic-gate 	kc->kc_chain_id = kcid;
2977c478bd9Sstevel@tonic-gate 	return (kcid);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate kstat_t *
kstat_lookup(kstat_ctl_t * kc,char * ks_module,int ks_instance,char * ks_name)3017c478bd9Sstevel@tonic-gate kstat_lookup(kstat_ctl_t *kc, char *ks_module, int ks_instance, char *ks_name)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
3067c478bd9Sstevel@tonic-gate 		if ((ks_module == NULL ||
3077c478bd9Sstevel@tonic-gate 		    strcmp(ksp->ks_module, ks_module) == 0) &&
3087c478bd9Sstevel@tonic-gate 		    (ks_instance == -1 || ksp->ks_instance == ks_instance) &&
3097c478bd9Sstevel@tonic-gate 		    (ks_name == NULL || strcmp(ksp->ks_name, ks_name) == 0))
3107c478bd9Sstevel@tonic-gate 			return (ksp);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	errno = ENOENT;
3147c478bd9Sstevel@tonic-gate 	return (NULL);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate void *
kstat_data_lookup(kstat_t * ksp,char * name)3187c478bd9Sstevel@tonic-gate kstat_data_lookup(kstat_t *ksp, char *name)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	int i, size;
3217c478bd9Sstevel@tonic-gate 	char *namep, *datap;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	switch (ksp->ks_type) {
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	case KSTAT_TYPE_NAMED:
3267c478bd9Sstevel@tonic-gate 		size = sizeof (kstat_named_t);
3277c478bd9Sstevel@tonic-gate 		namep = KSTAT_NAMED_PTR(ksp)->name;
3287c478bd9Sstevel@tonic-gate 		break;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	case KSTAT_TYPE_TIMER:
3317c478bd9Sstevel@tonic-gate 		size = sizeof (kstat_timer_t);
3327c478bd9Sstevel@tonic-gate 		namep = KSTAT_TIMER_PTR(ksp)->name;
3337c478bd9Sstevel@tonic-gate 		break;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	default:
3367c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3377c478bd9Sstevel@tonic-gate 		return (NULL);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	datap = ksp->ks_data;
3417c478bd9Sstevel@tonic-gate 	for (i = 0; i < ksp->ks_ndata; i++) {
3427c478bd9Sstevel@tonic-gate 		if (strcmp(name, namep) == 0)
3437c478bd9Sstevel@tonic-gate 			return (datap);
3447c478bd9Sstevel@tonic-gate 		namep += size;
3457c478bd9Sstevel@tonic-gate 		datap += size;
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 	errno = ENOENT;
3487c478bd9Sstevel@tonic-gate 	return (NULL);
3497c478bd9Sstevel@tonic-gate }
350