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