8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / avs / dsstat / common.c
blob45f81455911a13f807881f9b8df59455d2fda254
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <errno.h>
31 #include <kstat.h>
32 #include <signal.h>
33 #include <setjmp.h>
35 #include "sdbc_stats.h"
36 #include "report.h"
37 #include "common.h"
39 static sigjmp_buf env;
40 static sig_atomic_t sig_raised = 0;
41 static void sig_handler(int);
43 void
44 sig_handler(int sig)
46 switch (sig) {
47 case SIGSEGV:
48 sig_raised = 1;
49 siglongjmp(env, sig);
50 default:
51 exit(sig);
56 * kstat_retrieve() - populate the ks_data field of the kstat_t structure
58 * This function is a user-land equivalent of a ks_snapshot
60 * parameters
61 * kstat_ctl_t *kc - kstat_ctl_t structure representing returned from
62 * kstat_open()
63 * kstat_t *ksp - kstat_t strcture to popluate ks_data into
65 * returns
66 * NULL pointer on failure
67 * kstat_t * structure on success
69 kstat_t *
70 kstat_retrieve(kstat_ctl_t *kc, kstat_t *ksp)
73 kstat_t *rval;
74 kstat_named_t *knp;
75 char *end;
76 int i;
77 struct sigaction segv_act; /* default actions */
79 if (ksp == NULL)
80 return (NULL);
82 if (ksp->ks_data == NULL &&
83 kstat_read(kc, ksp, NULL) == -1)
84 return (NULL);
86 rval = (kstat_t *)calloc(1, sizeof (*ksp));
87 (void) memcpy(rval, ksp, sizeof (*ksp));
89 rval->ks_data = (void *) calloc(1, ksp->ks_data_size);
90 (void) memcpy(rval->ks_data, ksp->ks_data,
91 sizeof (kstat_named_t) * ksp->ks_ndata);
93 /* special handling for variable length string KSTAT_DATA_STRING */
94 knp = (kstat_named_t *)rval->ks_data;
95 end = (char *)(knp + ksp->ks_ndata);
96 for (i = 0; i < ksp->ks_ndata; i++, knp++) {
97 if (knp->data_type == KSTAT_DATA_STRING &&
98 KSTAT_NAMED_STR_PTR(knp) != NULL) {
99 /* catch SIGSEGV (bug 6384130) */
100 sig_raised = 0;
101 (void) sigaction(SIGSEGV, NULL, &segv_act);
102 (void) signal(SIGSEGV, sig_handler);
104 (void) strncpy(end, KSTAT_NAMED_STR_PTR(knp),
105 KSTAT_NAMED_STR_BUFLEN(knp));
106 KSTAT_NAMED_STR_PTR(knp) = end;
107 end += KSTAT_NAMED_STR_BUFLEN(knp);
109 /* bug 6384130 */
110 (void) sigsetjmp(env, 0);
111 if (sig_raised) {
112 bzero(end, KSTAT_NAMED_STR_BUFLEN(knp));
113 KSTAT_NAMED_STR_PTR(knp) = end;
114 end += KSTAT_NAMED_STR_BUFLEN(knp);
116 (void) sigaction(SIGSEGV, &segv_act, NULL);
120 return (rval);
124 * kstat_value() - retrieve value of a field in a kstat_named_t kstat.
126 * parameters
127 * kstat_t *ksp - kstat containing the field
128 * char *name - text string representing the field name
130 * returns
131 * void * - pointer to data retrieved
133 void *
134 kstat_value(kstat_t *ksp, char *name)
136 kstat_named_t *knm;
138 if ((knm = kstat_data_lookup(ksp, name)) == NULL)
139 return (NULL);
141 switch (knm->data_type) {
142 case KSTAT_DATA_CHAR :
143 return (knm->value.c);
144 case KSTAT_DATA_INT32 :
145 return (&knm->value.i32);
146 case KSTAT_DATA_UINT32 :
147 return (&knm->value.ui32);
148 case KSTAT_DATA_INT64 :
149 return (&knm->value.i64);
150 case KSTAT_DATA_UINT64 :
151 return (&knm->value.ui64);
152 case KSTAT_DATA_STRING :
153 return (KSTAT_NAMED_STR_PTR(knm));
156 return (NULL);
160 * kstat_free() - deallocated memory associated with a kstat
162 * paramters
163 * kstat_t ksp - kstat to be deallocated
165 * returns
166 * void
168 void
169 kstat_free(kstat_t *ksp)
171 if (ksp != NULL) {
172 if (ksp->ks_data != NULL)
173 free(ksp->ks_data);
174 free(ksp);
178 uint32_t
179 kstat_delta(kstat_t *pksp, kstat_t *cksp, char *name)
181 uint32_t *pv, *cv;
183 pv = kstat_value(pksp, name);
184 cv = kstat_value(cksp, name);
186 return (u32_delta(*pv, *cv));