2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 1988, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <stdarg.h>
2N/A#include <ctype.h>
2N/A#include <unistd.h>
2N/A#include <memory.h>
2N/A#include <strings.h>
2N/A#include <string.h>
2N/A#include <fcntl.h>
2N/A#include <errno.h>
2N/A#include <poll.h>
2N/A#include "kstat.h"
2N/A
2N/A/*LINTLIBRARY*/
2N/A
2N/A/*
2N/A * Use a hash for the kstat records we have read in. This is done
2N/A * to reduce the number of records that have to be looked up when
2N/A * dealing with a large configuration system. Note this hash is
2N/A * internal to libkstat only. The pointer to the hash is associated
2N/A * with the kstat_ctl_t data structure. When the kstat_ctl_t is free, the
2N/A * hash is removed.
2N/A */
2N/Astruct kstat_hashed_records {
2N/A char *module_name;
2N/A char *ks_name;
2N/A int instance;
2N/A int allocated;
2N/A kstat_t *ksp;
2N/A struct kstat_hashed_records *next_entry;
2N/A };
2N/A
2N/A#define KSTAT_HASHED_ENTRIES 8192
2N/A#define KSP_INST_HASH(x) ((x) & (KSTAT_HASHED_ENTRIES-1))
2N/Astatic void kstat_hashed_add_ksp(kstat_t *, kstat_ctl_t *);
2N/A
2N/Astatic int
2N/Astring_path_hash(char *path)
2N/A
2N/A{
2N/A int val = 0;
2N/A int index = 0;
2N/A
2N/A if (!path)
2N/A return (0);
2N/A
2N/A /*
2N/A * To force a good hashing of device paths, we need
2N/A * to scatter the hash around based on the character
2N/A * placement in the string.
2N/A * If the character is not an integer value, we simply
2N/A * multiply it by the position value in the string.
2N/A * If the character is an integer, then we use 1 of
2N/A * 4 different calculations based on the position
2N/A * the string. If we do not do this, then things like
2N/A * 1 and 2 can end up having the same hash position.
2N/A * Simple usage of a long or short value is no good
2N/A * because the length of names can get quite long.
2N/A */
2N/A while (path[index]) {
2N/A if (path[index] < '0' || path[index] > '9') {
2N/A val += ((int)path[index])*(index+1);
2N/A index++;
2N/A continue;
2N/A }
2N/A switch (index & 3) {
2N/A case 0:
2N/A val += ((int)path[index])*(index+1);
2N/A break;
2N/A case 1:
2N/A val += ((int)path[index])*(index+1)*3;
2N/A break;
2N/A case 2:
2N/A val = val * ((int)path[index])*(index+1);
2N/A break;
2N/A case 3:
2N/A val = val + val/((int)path[index]);
2N/A break;
2N/A }
2N/A index++;
2N/A }
2N/A return (val);
2N/A}
2N/A
2N/Astatic int
2N/Akstat_record_hash(char *name, char *module, int instance, char **tmp_name,
2N/A int *allocated)
2N/A
2N/A{
2N/A int val;
2N/A char *tmp;
2N/A/*
2N/A * Extra chars in the string that we need to allow for
2N/A * spaces, underscores and others.
2N/A */
2N/A#define EXTRA_CHARS 12
2N/A
2N/A if (!name || name[0] == '\0') {
2N/A int len;
2N/A
2N/A if (!module || instance < 0)
2N/A return (-1);
2N/A /*
2N/A * Need to build a temporary string to use.
2N/A */
2N/A len = strlen(module)+EXTRA_CHARS;
2N/Aagain:
2N/A *tmp_name = tmp = malloc(len);
2N/A if (!tmp)
2N/A return (-1);
2N/A *allocated = 1;
2N/A snprintf(tmp, len, "%s_%d", module, instance);
2N/A } else {
2N/A *tmp_name = tmp = name;
2N/A }
2N/A
2N/A val = string_path_hash(tmp);
2N/A val += string_path_hash(module);
2N/A if (instance > 0)
2N/A val += KSP_INST_HASH(instance);
2N/A
2N/A if (val < 0)
2N/A val = val * -1;
2N/A val = (val & (KSTAT_HASHED_ENTRIES - 1));
2N/A
2N/A return (val);
2N/A}
2N/A
2N/A
2N/Astatic kstat_t *
2N/Akstat_hash_search(char *ks_module, int ks_instance, char *ks_name,
2N/A kstat_ctl_t *kc)
2N/A{
2N/A kstat_t *ksp;
2N/A struct kstat_hashed_records *hash_entry;
2N/A struct kstat_hashed_records **hash_head;
2N/A int hash_index;
2N/A char *tmp_name;
2N/A int allocated = 0;
2N/A
2N/A hash_index = kstat_record_hash(ks_name, ks_module, ks_instance,
2N/A &tmp_name, &allocated);
2N/A /*
2N/A * If the entry was allocated free it up we do not care
2N/A * about it here.
2N/A */
2N/A if (allocated)
2N/A free(tmp_name);
2N/A
2N/A if (hash_index >= 0 &&
2N/A (hash_head = (struct kstat_hashed_records **)kc->kc_private)) {
2N/A hash_entry = hash_head[hash_index];
2N/A while (hash_entry) {
2N/A if ((!ks_module || (strcmp(hash_entry->module_name,
2N/A ks_module)) == 0) && ks_name &&
2N/A (strcmp(hash_entry->ks_name, ks_name) == 0)) {
2N/A if (ks_instance < 0 || hash_entry->instance ==
2N/A ks_instance) {
2N/A /* Entry matches, return. */
2N/A return (hash_entry->ksp);
2N/A }
2N/A }
2N/A hash_entry = hash_entry->next_entry;
2N/A }
2N/A }
2N/A
2N/Aout:
2N/A /*
2N/A * We did not find the entry in the hash table. Check the kstat_t list
2N/A * and see if it is there. If it is add the entry to the hash.
2N/A */
2N/A for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
2N/A if ((ksp->ks_instance == ks_instance) &&
2N/A (ks_module && strcmp(ksp->ks_module, ks_module) == 0) &&
2N/A (ks_name && strcmp(ksp->ks_name, ks_name) == 0)) {
2N/A kstat_hashed_add_ksp(ksp, kc);
2N/A return (ksp);
2N/A }
2N/A }
2N/A
2N/A errno = ENOENT;
2N/A return (NULL);
2N/A}
2N/A
2N/Astatic void
2N/Akstat_hashed_add_ksp(kstat_t *ksp, kstat_ctl_t *kc)
2N/A
2N/A{
2N/A struct kstat_hashed_records *ks_entry;
2N/A int hash_index;
2N/A struct kstat_hashed_records **hash_head;
2N/A char *tmp_name;
2N/A int allocated = 0;
2N/A
2N/A if (!ksp->ks_module || !ksp->ks_instance)
2N/A return;
2N/A
2N/A if (!kc->kc_private) {
2N/A kc->kc_private = (void **) (hash_head =
2N/A (struct kstat_hashed_records **)
2N/A calloc(KSTAT_HASHED_ENTRIES,
2N/A sizeof (struct kstat_hashed_records **)));
2N/A
2N/A if (!kc->kc_private)
2N/A return;
2N/A } else {
2N/A hash_head = (struct kstat_hashed_records **)kc->kc_private;
2N/A }
2N/A
2N/A
2N/A hash_index = kstat_record_hash(ksp->ks_name, ksp->ks_module,
2N/A ksp->ks_instance, &tmp_name, &allocated);
2N/A if (hash_index < 0)
2N/A return;
2N/A ks_entry = malloc(sizeof (struct kstat_hashed_records));
2N/A if (!ks_entry)
2N/A return;
2N/A ks_entry->ksp = ksp;
2N/A ks_entry->instance = ksp->ks_instance;
2N/A ks_entry->module_name = ksp->ks_module;
2N/A if (ksp->ks_name[0] != NULL) {
2N/A ks_entry->ks_name = ksp->ks_name;
2N/A } else {
2N/A ks_entry->ks_name = tmp_name;
2N/A }
2N/A ks_entry->next_entry = hash_head[hash_index];
2N/A ks_entry->allocated = allocated;
2N/A hash_head[hash_index] = ks_entry;
2N/A}
2N/A
2N/Astatic void
2N/Akstat_hashed_remove_ksp(kstat_t *ksp, kstat_ctl_t *kc)
2N/A
2N/A{
2N/A int hash_index;
2N/A struct kstat_hashed_records *hash_entry, *prev_entry = NULL;
2N/A struct kstat_hashed_records **hash_head;
2N/A char *tmp_name;
2N/A int allocated = 0;
2N/A
2N/A if (!kc->kc_private || !ksp)
2N/A return;
2N/A
2N/A hash_index = kstat_record_hash(ksp->ks_name, ksp->ks_module,
2N/A ksp->ks_instance, &tmp_name, &allocated);
2N/A
2N/A /*
2N/A * If hash_index < 0 then there is not enough information in the ksp to
2N/A * be able to figure out what to remove.
2N/A */
2N/A if (hash_index < 0)
2N/A return;
2N/A
2N/A hash_head = (struct kstat_hashed_records **)kc->kc_private;
2N/A hash_entry = hash_head[hash_index];
2N/A /* Walk the hash chain for the ksp. */
2N/A while (hash_entry) {
2N/A if (hash_entry->ksp == ksp)
2N/A break;
2N/A prev_entry = hash_entry;
2N/A hash_entry = hash_entry->next_entry;
2N/A }
2N/A /* If we found an entry, then remove it. */
2N/A if (hash_entry) {
2N/A if (!prev_entry) {
2N/A hash_head[hash_index] =
2N/A hash_head[hash_index]->next_entry;
2N/A } else {
2N/A prev_entry->next_entry = hash_entry->next_entry;
2N/A }
2N/A if (hash_entry->allocated)
2N/A free(hash_entry->ks_name);
2N/A free(hash_entry);
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Akstat_zalloc(void **ptr, size_t size, int free_first)
2N/A{
2N/A if (free_first)
2N/A free(*ptr);
2N/A *ptr = calloc(size, 1);
2N/A}
2N/A
2N/Astatic void
2N/Akstat_chain_free(kstat_ctl_t *kc)
2N/A{
2N/A kstat_t *ksp, *nksp;
2N/A
2N/A ksp = kc->kc_chain;
2N/A while (ksp) {
2N/A nksp = ksp->ks_next;
2N/A kstat_hashed_remove_ksp(ksp, kc);
2N/A free(ksp->ks_data);
2N/A free(ksp);
2N/A ksp = nksp;
2N/A }
2N/A /* Free the kc_private data if present. */
2N/A if (kc->kc_private) {
2N/A free(kc->kc_private);
2N/A kc->kc_private = (void **) NULL;
2N/A }
2N/A kc->kc_chain = NULL;
2N/A kc->kc_chain_id = 0;
2N/A}
2N/A
2N/Akstat_ctl_t *
2N/Akstat_open(void)
2N/A{
2N/A kstat_ctl_t *kc;
2N/A int kd;
2N/A
2N/A kd = open("/dev/kstat", O_RDONLY);
2N/A if (kd == -1)
2N/A return (NULL);
2N/A kstat_zalloc((void **)&kc, sizeof (kstat_ctl_t), 0);
2N/A if (kc == NULL)
2N/A return (NULL);
2N/A
2N/A kc->kc_kd = kd;
2N/A kc->kc_chain = NULL;
2N/A kc->kc_chain_id = 0;
2N/A if (kstat_chain_update(kc) == -1) {
2N/A int saved_err = errno;
2N/A (void) kstat_close(kc);
2N/A errno = saved_err;
2N/A return (NULL);
2N/A }
2N/A return (kc);
2N/A}
2N/A
2N/Aint
2N/Akstat_close(kstat_ctl_t *kc)
2N/A{
2N/A int rc;
2N/A
2N/A kstat_chain_free(kc);
2N/A rc = close(kc->kc_kd);
2N/A free(kc);
2N/A return (rc);
2N/A}
2N/A
2N/Akid_t
2N/Akstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
2N/A{
2N/A kid_t kcid;
2N/A
2N/A if (ksp->ks_data == NULL && ksp->ks_data_size > 0) {
2N/A kstat_zalloc(&ksp->ks_data, ksp->ks_data_size, 0);
2N/A if (ksp->ks_data == NULL)
2N/A return (-1);
2N/A }
2N/A while ((kcid = (kid_t)ioctl(kc->kc_kd, KSTAT_IOC_READ, ksp)) == -1) {
2N/A if (errno == EAGAIN) {
2N/A (void) poll(NULL, 0, 100); /* back off a moment */
2N/A continue; /* and try again */
2N/A }
2N/A /*
2N/A * Mating dance for variable-size kstats.
2N/A * You start with a buffer of a certain size,
2N/A * which you hope will hold all the data.
2N/A * If your buffer is too small, the kstat driver
2N/A * returns ENOMEM and sets ksp->ks_data_size to
2N/A * the current size of the kstat's data. You then
2N/A * resize your buffer and try again. In practice,
2N/A * this almost always converges in two passes.
2N/A */
2N/A if (errno == ENOMEM && (ksp->ks_flags & KSTAT_FLAG_VAR_SIZE)) {
2N/A kstat_zalloc(&ksp->ks_data, ksp->ks_data_size, 1);
2N/A if (ksp->ks_data != NULL)
2N/A continue;
2N/A }
2N/A return (-1);
2N/A }
2N/A if (data != NULL) {
2N/A (void) memcpy(data, ksp->ks_data, ksp->ks_data_size);
2N/A if (ksp->ks_type == KSTAT_TYPE_NAMED &&
2N/A ksp->ks_data_size !=
2N/A ksp->ks_ndata * sizeof (kstat_named_t)) {
2N/A /*
2N/A * Has KSTAT_DATA_STRING fields. Fix the pointers.
2N/A */
2N/A uint_t i;
2N/A kstat_named_t *knp = data;
2N/A
2N/A for (i = 0; i < ksp->ks_ndata; i++, knp++) {
2N/A if (knp->data_type != KSTAT_DATA_STRING)
2N/A continue;
2N/A if (KSTAT_NAMED_STR_PTR(knp) == NULL)
2N/A continue;
2N/A /*
2N/A * The offsets of the strings within the
2N/A * buffers are the same, so add the offset of
2N/A * the string to the beginning of 'data' to fix
2N/A * the pointer so that strings in 'data' don't
2N/A * point at memory in 'ksp->ks_data'.
2N/A */
2N/A KSTAT_NAMED_STR_PTR(knp) = (char *)data +
2N/A (KSTAT_NAMED_STR_PTR(knp) -
2N/A (char *)ksp->ks_data);
2N/A }
2N/A }
2N/A }
2N/A return (kcid);
2N/A}
2N/A
2N/Akid_t
2N/Akstat_write(kstat_ctl_t *kc, kstat_t *ksp, void *data)
2N/A{
2N/A kid_t kcid;
2N/A
2N/A if (ksp->ks_data == NULL && ksp->ks_data_size > 0) {
2N/A kstat_zalloc(&ksp->ks_data, ksp->ks_data_size, 0);
2N/A if (ksp->ks_data == NULL)
2N/A return (-1);
2N/A }
2N/A if (data != NULL) {
2N/A (void) memcpy(ksp->ks_data, data, ksp->ks_data_size);
2N/A if (ksp->ks_type == KSTAT_TYPE_NAMED) {
2N/A kstat_named_t *oknp = data;
2N/A kstat_named_t *nknp = KSTAT_NAMED_PTR(ksp);
2N/A uint_t i;
2N/A
2N/A for (i = 0; i < ksp->ks_ndata; i++, oknp++, nknp++) {
2N/A if (nknp->data_type != KSTAT_DATA_STRING)
2N/A continue;
2N/A if (KSTAT_NAMED_STR_PTR(nknp) == NULL)
2N/A continue;
2N/A /*
2N/A * The buffer passed in as 'data' has string
2N/A * pointers that point within 'data'. Fix the
2N/A * pointers so they point into the same offset
2N/A * within the newly allocated buffer.
2N/A */
2N/A KSTAT_NAMED_STR_PTR(nknp) =
2N/A (char *)ksp->ks_data +
2N/A (KSTAT_NAMED_STR_PTR(oknp) - (char *)data);
2N/A }
2N/A }
2N/A
2N/A }
2N/A while ((kcid = (kid_t)ioctl(kc->kc_kd, KSTAT_IOC_WRITE, ksp)) == -1) {
2N/A if (errno == EAGAIN) {
2N/A (void) poll(NULL, 0, 100); /* back off a moment */
2N/A continue; /* and try again */
2N/A }
2N/A break;
2N/A }
2N/A return (kcid);
2N/A}
2N/A
2N/A/*
2N/A * If the current KCID is the same as kc->kc_chain_id, return 0;
2N/A * if different, update the chain and return the new KCID.
2N/A * This operation is non-destructive for unchanged kstats.
2N/A */
2N/Akid_t
2N/Akstat_chain_update(kstat_ctl_t *kc)
2N/A{
2N/A kstat_t k0, *headers, *oksp, *nksp, **okspp, *next;
2N/A int i;
2N/A kid_t kcid;
2N/A
2N/A kcid = (kid_t)ioctl(kc->kc_kd, KSTAT_IOC_CHAIN_ID, NULL);
2N/A if (kcid == -1)
2N/A return (-1);
2N/A if (kcid == kc->kc_chain_id)
2N/A return (0);
2N/A
2N/A /*
2N/A * kstat 0's data is the kstat chain, so we can get the chain
2N/A * by doing a kstat_read() of this kstat. The only fields the
2N/A * kstat driver needs are ks_kid (this identifies the kstat),
2N/A * ks_data (the pointer to our buffer), and ks_data_size (the
2N/A * size of our buffer). By zeroing the struct we set ks_data = NULL
2N/A * and ks_data_size = 0, so that kstat_read() will automatically
2N/A * determine the size and allocate space for us. We also fill in the
2N/A * name, so that truss can print something meaningful.
2N/A */
2N/A bzero(&k0, sizeof (k0));
2N/A (void) strcpy(k0.ks_name, "kstat_headers");
2N/A
2N/A kcid = kstat_read(kc, &k0, NULL);
2N/A if (kcid == -1) {
2N/A free(k0.ks_data);
2N/A /* errno set by kstat_read() */
2N/A return (-1);
2N/A }
2N/A headers = k0.ks_data;
2N/A
2N/A /*
2N/A * Chain the new headers together
2N/A */
2N/A for (i = 1; i < k0.ks_ndata; i++)
2N/A headers[i - 1].ks_next = &headers[i];
2N/A
2N/A headers[k0.ks_ndata - 1].ks_next = NULL;
2N/A
2N/A /*
2N/A * Remove all deleted kstats from the chain.
2N/A */
2N/A nksp = headers;
2N/A okspp = &kc->kc_chain;
2N/A oksp = kc->kc_chain;
2N/A while (oksp != NULL) {
2N/A next = oksp->ks_next;
2N/A if (nksp != NULL && oksp->ks_kid == nksp->ks_kid) {
2N/A okspp = &oksp->ks_next;
2N/A nksp = nksp->ks_next;
2N/A } else {
2N/A kstat_hashed_remove_ksp(oksp, kc);
2N/A *okspp = oksp->ks_next;
2N/A free(oksp->ks_data);
2N/A free(oksp);
2N/A }
2N/A oksp = next;
2N/A }
2N/A
2N/A /*
2N/A * Add all new kstats to the chain.
2N/A */
2N/A while (nksp != NULL) {
2N/A kstat_zalloc((void **)okspp, sizeof (kstat_t), 0);
2N/A if ((oksp = *okspp) == NULL) {
2N/A free(headers);
2N/A return (-1);
2N/A }
2N/A *oksp = *nksp;
2N/A okspp = &oksp->ks_next;
2N/A oksp->ks_next = NULL;
2N/A oksp->ks_data = NULL;
2N/A kstat_hashed_add_ksp(oksp, kc);
2N/A nksp = nksp->ks_next;
2N/A }
2N/A
2N/A free(headers);
2N/A kc->kc_chain_id = kcid;
2N/A return (kcid);
2N/A}
2N/A
2N/Akstat_t *
2N/Akstat_lookup(kstat_ctl_t *kc, char *ks_module, int ks_instance, char *ks_name)
2N/A{
2N/A kstat_t *ksp;
2N/A
2N/A if (ks_instance != -1 && ks_module && ks_name) {
2N/A return (kstat_hash_search(ks_module, ks_instance, ks_name, kc));
2N/A }
2N/A for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
2N/A if ((ks_instance == -1 || ksp->ks_instance == ks_instance) &&
2N/A (ks_module == NULL ||
2N/A strcmp(ksp->ks_module, ks_module) == 0) &&
2N/A (ks_name == NULL || strcmp(ksp->ks_name, ks_name) == 0)) {
2N/A /* Attempt to add the entry to the hash. */
2N/A kstat_hashed_add_ksp(ksp, kc);
2N/A return (ksp);
2N/A }
2N/A }
2N/A
2N/A errno = ENOENT;
2N/A return (NULL);
2N/A}
2N/A
2N/Avoid *
2N/Akstat_data_lookup(kstat_t *ksp, char *name)
2N/A{
2N/A int i, size;
2N/A char *namep, *datap;
2N/A
2N/A switch (ksp->ks_type) {
2N/A
2N/A case KSTAT_TYPE_NAMED:
2N/A size = sizeof (kstat_named_t);
2N/A namep = KSTAT_NAMED_PTR(ksp)->name;
2N/A break;
2N/A
2N/A case KSTAT_TYPE_TIMER:
2N/A size = sizeof (kstat_timer_t);
2N/A namep = KSTAT_TIMER_PTR(ksp)->name;
2N/A break;
2N/A
2N/A default:
2N/A errno = EINVAL;
2N/A return (NULL);
2N/A }
2N/A
2N/A datap = ksp->ks_data;
2N/A for (i = 0; i < ksp->ks_ndata; i++) {
2N/A if (strcmp(name, namep) == 0)
2N/A return (datap);
2N/A namep += size;
2N/A datap += size;
2N/A }
2N/A errno = ENOENT;
2N/A return (NULL);
2N/A}