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