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