Annotation of src/usr.bin/vmstat/dkstats.c, Revision 1.30
1.30 ! deraadt 1: /* $OpenBSD: dkstats.c,v 1.29 2006/03/31 04:06:13 deraadt Exp $ */
1.2 deraadt 2: /* $NetBSD: dkstats.c,v 1.1 1996/05/10 23:19:27 thorpej Exp $ */
1.1 tholo 3:
4: /*
1.2 deraadt 5: * Copyright (c) 1996 John M. Vinopal
1.1 tholo 6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed for the NetBSD Project
19: * by John M. Vinopal.
20: * 4. The name of the author may not be used to endorse or promote products
21: * derived from this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
1.10 angelos 36: #include <sys/param.h>
1.1 tholo 37: #include <sys/dkstat.h>
38: #include <sys/time.h>
39: #include <sys/disk.h>
1.10 angelos 40: #include <sys/sysctl.h>
41: #include <sys/tty.h>
1.1 tholo 42:
43: #include <err.h>
44: #include <fcntl.h>
45: #include <kvm.h>
46: #include <limits.h>
47: #include <nlist.h>
48: #include <stdio.h>
49: #include <stdlib.h>
50: #include <string.h>
51: #include <unistd.h>
52: #include "dkstats.h"
53:
1.16 deraadt 54: #if !defined(NOKVM)
1.1 tholo 55: static struct nlist namelist[] = {
1.18 deraadt 56: #define X_TK_NIN 0 /* sysctl */
57: { "_tk_nin" },
58: #define X_TK_NOUT 1 /* sysctl */
59: { "_tk_nout" },
60: #define X_CP_TIME 2 /* sysctl */
61: { "_cp_time" },
62: #define X_HZ 3 /* sysctl */
63: { "_hz" },
64: #define X_STATHZ 4 /* sysctl */
1.1 tholo 65: { "_stathz" },
1.18 deraadt 66: #define X_DISK_COUNT 5 /* sysctl */
67: { "_disk_count" },
68: #define X_DISKLIST 6 /* sysctl */
69: { "_disklist" },
1.1 tholo 70: { NULL },
71: };
1.16 deraadt 72: #define KVM_ERROR(_string) { \
73: warnx("%s", (_string)); \
74: errx(1, "%s", kvm_geterr(kd)); \
75: }
76:
77: /*
1.30 ! deraadt 78: * Dereference the namelist pointer `v' and fill in the local copy
1.16 deraadt 79: * 'p' which is of size 's'.
80: */
81: #define deref_nl(v, p, s) deref_kptr((void *)namelist[(v)].n_value, (p), (s));
82: static void deref_kptr(void *, void *, size_t);
83: #endif /* !defined(NOKVM) */
1.1 tholo 84:
85: /* Structures to hold the statistics. */
86: struct _disk cur, last;
87:
88: /* Kernel pointers: nlistf and memf defined in calling program. */
1.19 deraadt 89: #if !defined(NOKVM)
1.20 deraadt 90: extern kvm_t *kd;
1.19 deraadt 91: #endif
1.1 tholo 92: extern char *nlistf;
93: extern char *memf;
94:
1.19 deraadt 95: #if !defined(NOKVM)
1.1 tholo 96: /* Pointer to list of disks. */
97: static struct disk *dk_drivehead = NULL;
1.19 deraadt 98: #endif
1.1 tholo 99:
100: /* Backward compatibility references. */
1.30 ! deraadt 101: int dk_ndrive = 0;
1.1 tholo 102: int *dk_select;
103: char **dr_name;
104:
105: /* Missing from <sys/time.h> */
1.16 deraadt 106: #define timerset(tvp, uvp) \
107: ((uvp)->tv_sec = (tvp)->tv_sec); \
108: ((uvp)->tv_usec = (tvp)->tv_usec)
109:
110: #define SWAP(fld) tmp = cur.fld; \
111: cur.fld -= last.fld; \
112: last.fld = tmp
1.1 tholo 113:
114: /*
115: * Take the delta between the present values and the last recorded
116: * values, storing the present values in the 'last' structure, and
117: * the delta values in the 'cur' structure.
118: */
119: void
1.21 deraadt 120: dkswap(void)
1.1 tholo 121: {
122: u_int64_t tmp;
123: int i;
124:
1.22 tdeval 125: for (i = 0; i < cur.dk_ndrive; i++) {
1.1 tholo 126: struct timeval tmp_timer;
127:
128: if (!cur.dk_select[i])
129: continue;
130:
131: /* Delta Values. */
1.24 tedu 132: SWAP(dk_rxfer[i]);
133: SWAP(dk_wxfer[i]);
1.1 tholo 134: SWAP(dk_seek[i]);
1.24 tedu 135: SWAP(dk_rbytes[i]);
136: SWAP(dk_wbytes[i]);
1.1 tholo 137:
138: /* Delta Time. */
139: timerclear(&tmp_timer);
140: timerset(&(cur.dk_time[i]), &tmp_timer);
141: timersub(&tmp_timer, &(last.dk_time[i]), &(cur.dk_time[i]));
142: timerclear(&(last.dk_time[i]));
143: timerset(&tmp_timer, &(last.dk_time[i]));
144: }
145: for (i = 0; i < CPUSTATES; i++) {
1.29 deraadt 146: long ltmp;
147:
148: ltmp = cur.cp_time[i];
149: cur.cp_time[i] -= last.cp_time[i];
150: last.cp_time[i] = ltmp;
1.1 tholo 151: }
152: SWAP(tk_nin);
153: SWAP(tk_nout);
154:
155: #undef SWAP
156: }
157:
158: /*
1.30 ! deraadt 159: * Read the disk statistics for each disk in the disk list.
1.1 tholo 160: * Also collect statistics for tty i/o and cpu ticks.
161: */
162: void
1.21 deraadt 163: dkreadstats(void)
1.1 tholo 164: {
1.19 deraadt 165: #if !defined(NOKVM)
1.1 tholo 166: struct disk cur_disk, *p;
1.19 deraadt 167: #endif
1.22 tdeval 168: int i, j, mib[3];
1.10 angelos 169: size_t size;
1.22 tdeval 170: char *disknames, *name, *bufpp, **dk_name;
1.13 angelos 171: struct diskstats *q;
1.1 tholo 172:
1.22 tdeval 173: last.dk_ndrive = cur.dk_ndrive;
174:
1.10 angelos 175: if (nlistf == NULL && memf == NULL) {
1.22 tdeval 176: /* Get the number of attached drives. */
177: mib[0] = CTL_HW;
178: mib[1] = HW_DISKCOUNT;
179: size = sizeof(dk_ndrive);
180: if (sysctl(mib, 2, &dk_ndrive, &size, NULL, 0) < 0 ) {
181: warn("could not read hw.diskcount");
182: dk_ndrive = 0;
183: }
184:
185: if (cur.dk_ndrive != dk_ndrive) {
186: /* Re-read the disk names. */
1.29 deraadt 187: dk_name = calloc((size_t)dk_ndrive, sizeof(char *));
1.22 tdeval 188: if (dk_name == NULL)
189: err(1, NULL);
190: mib[0] = CTL_HW;
191: mib[1] = HW_DISKNAMES;
192: size = 0;
193: if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0)
194: err(1, "can't get hw.disknames");
195: disknames = malloc(size);
196: if (disknames == NULL)
197: err(1, NULL);
198: if (sysctl(mib, 2, disknames, &size, NULL, 0) < 0)
199: err(1, "can't get hw.disknames");
200: bufpp = disknames;
1.23 millert 201: for (i = 0; i < dk_ndrive && (name = strsep(&bufpp, ",")) != NULL; i++)
202: dk_name[i] = name;
1.22 tdeval 203: disknames = cur.dk_name[0]; /* To free old names. */
204:
205: if (dk_ndrive < cur.dk_ndrive) {
206: for (i = 0, j = 0; i < dk_ndrive; i++, j++) {
207: while (j < cur.dk_ndrive &&
208: strcmp(cur.dk_name[j], dk_name[i]))
209: j++;
210: if (i == j) continue;
211:
212: if (j >= cur.dk_ndrive) {
213: cur.dk_select[i] = 1;
1.24 tedu 214: last.dk_rxfer[i] = 0;
215: last.dk_wxfer[i] = 0;
1.22 tdeval 216: last.dk_seek[i] = 0;
1.24 tedu 217: last.dk_rbytes[i] = 0;
218: last.dk_wbytes[i] = 0;
1.22 tdeval 219: bzero(&last.dk_time[i],
220: sizeof(struct timeval));
221: continue;
222: }
223:
224: cur.dk_select[i] = cur.dk_select[j];
1.24 tedu 225: last.dk_rxfer[i] = last.dk_rxfer[j];
226: last.dk_wxfer[i] = last.dk_wxfer[j];
1.22 tdeval 227: last.dk_seek[i] = last.dk_seek[j];
1.24 tedu 228: last.dk_rbytes[i] = last.dk_rbytes[j];
229: last.dk_wbytes[i] = last.dk_wbytes[j];
1.22 tdeval 230: last.dk_time[i] = last.dk_time[j];
231: }
232:
233: cur.dk_select = realloc(cur.dk_select,
234: dk_ndrive * sizeof(*cur.dk_select));
1.24 tedu 235: cur.dk_rxfer = realloc(cur.dk_rxfer,
236: dk_ndrive * sizeof(*cur.dk_rxfer));
237: cur.dk_wxfer = realloc(cur.dk_wxfer,
238: dk_ndrive * sizeof(*cur.dk_wxfer));
1.22 tdeval 239: cur.dk_seek = realloc(cur.dk_seek,
240: dk_ndrive * sizeof(*cur.dk_seek));
1.24 tedu 241: cur.dk_rbytes = realloc(cur.dk_rbytes,
242: dk_ndrive * sizeof(*cur.dk_rbytes));
243: cur.dk_wbytes = realloc(cur.dk_wbytes,
244: dk_ndrive * sizeof(*cur.dk_wbytes));
1.22 tdeval 245: cur.dk_time = realloc(cur.dk_time,
246: dk_ndrive * sizeof(*cur.dk_time));
1.24 tedu 247: last.dk_rxfer = realloc(last.dk_rxfer,
248: dk_ndrive * sizeof(*last.dk_rxfer));
1.25 millert 249: last.dk_wxfer = realloc(last.dk_wxfer,
250: dk_ndrive * sizeof(*last.dk_wxfer));
1.22 tdeval 251: last.dk_seek = realloc(last.dk_seek,
252: dk_ndrive * sizeof(*last.dk_seek));
1.24 tedu 253: last.dk_rbytes = realloc(last.dk_rbytes,
254: dk_ndrive * sizeof(*last.dk_rbytes));
255: last.dk_wbytes = realloc(last.dk_wbytes,
256: dk_ndrive * sizeof(*last.dk_wbytes));
1.22 tdeval 257: last.dk_time = realloc(last.dk_time,
258: dk_ndrive * sizeof(*last.dk_time));
259: } else {
260: cur.dk_select = realloc(cur.dk_select,
261: dk_ndrive * sizeof(*cur.dk_select));
1.24 tedu 262: cur.dk_rxfer = realloc(cur.dk_rxfer,
263: dk_ndrive * sizeof(*cur.dk_rxfer));
264: cur.dk_wxfer = realloc(cur.dk_wxfer,
265: dk_ndrive * sizeof(*cur.dk_wxfer));
1.22 tdeval 266: cur.dk_seek = realloc(cur.dk_seek,
267: dk_ndrive * sizeof(*cur.dk_seek));
1.24 tedu 268: cur.dk_rbytes = realloc(cur.dk_rbytes,
269: dk_ndrive * sizeof(*cur.dk_rbytes));
270: cur.dk_wbytes = realloc(cur.dk_wbytes,
271: dk_ndrive * sizeof(*cur.dk_wbytes));
1.22 tdeval 272: cur.dk_time = realloc(cur.dk_time,
273: dk_ndrive * sizeof(*cur.dk_time));
1.24 tedu 274: last.dk_rxfer = realloc(last.dk_rxfer,
275: dk_ndrive * sizeof(*last.dk_rxfer));
276: last.dk_wxfer = realloc(last.dk_wxfer,
277: dk_ndrive * sizeof(*last.dk_wxfer));
1.22 tdeval 278: last.dk_seek = realloc(last.dk_seek,
279: dk_ndrive * sizeof(*last.dk_seek));
1.24 tedu 280: last.dk_rbytes = realloc(last.dk_rbytes,
281: dk_ndrive * sizeof(*last.dk_rbytes));
282: last.dk_wbytes = realloc(last.dk_wbytes,
283: dk_ndrive * sizeof(*last.dk_wbytes));
1.22 tdeval 284: last.dk_time = realloc(last.dk_time,
285: dk_ndrive * sizeof(*last.dk_time));
286:
287: for (i = dk_ndrive - 1, j = cur.dk_ndrive - 1;
288: i >= 0; i--) {
289:
290: if (j < 0 ||
291: strcmp(cur.dk_name[j], dk_name[i]))
292: {
293: cur.dk_select[i] = 1;
1.24 tedu 294: last.dk_rxfer[i] = 0;
295: last.dk_wxfer[i] = 0;
1.22 tdeval 296: last.dk_seek[i] = 0;
1.24 tedu 297: last.dk_rbytes[i] = 0;
298: last.dk_wbytes[i] = 0;
1.22 tdeval 299: bzero(&last.dk_time[i],
300: sizeof(struct timeval));
301: continue;
302: }
303:
304: if (i > j) {
305: cur.dk_select[i] =
306: cur.dk_select[j];
1.24 tedu 307: last.dk_rxfer[i] =
308: last.dk_rxfer[j];
309: last.dk_wxfer[i] =
310: last.dk_wxfer[j];
1.22 tdeval 311: last.dk_seek[i] =
312: last.dk_seek[j];
1.24 tedu 313: last.dk_rbytes[i] =
314: last.dk_rbytes[j];
315: last.dk_wbytes[i] =
316: last.dk_wbytes[j];
1.22 tdeval 317: last.dk_time[i] =
318: last.dk_time[j];
319: }
320: j--;
321: }
322: }
323:
324: cur.dk_ndrive = dk_ndrive;
325: free(disknames);
326: cur.dk_name = dk_name;
327: dr_name = cur.dk_name;
328: dk_select = cur.dk_select;
329: }
330:
331: size = cur.dk_ndrive * sizeof(struct diskstats);
1.10 angelos 332: mib[0] = CTL_HW;
333: mib[1] = HW_DISKSTATS;
1.13 angelos 334: q = malloc(size);
335: if (q == NULL)
1.10 angelos 336: err(1, NULL);
1.13 angelos 337: if (sysctl(mib, 2, q, &size, NULL, 0) < 0) {
1.22 tdeval 338: #ifdef DEBUG
1.10 angelos 339: warn("could not read hw.diskstats");
1.22 tdeval 340: #endif /* DEBUG */
341: bzero(q, cur.dk_ndrive * sizeof(struct diskstats));
1.10 angelos 342: }
343:
1.22 tdeval 344: for (i = 0; i < cur.dk_ndrive; i++) {
1.24 tedu 345: cur.dk_rxfer[i] = q[i].ds_rxfer;
346: cur.dk_wxfer[i] = q[i].ds_wxfer;
1.13 angelos 347: cur.dk_seek[i] = q[i].ds_seek;
1.24 tedu 348: cur.dk_rbytes[i] = q[i].ds_rbytes;
349: cur.dk_wbytes[i] = q[i].ds_wbytes;
1.13 angelos 350: timerset(&(q[i].ds_time), &(cur.dk_time[i]));
1.10 angelos 351: }
1.14 deraadt 352: free(q);
1.10 angelos 353:
1.30 ! deraadt 354: size = sizeof(cur.cp_time);
1.10 angelos 355: mib[0] = CTL_KERN;
356: mib[1] = KERN_CPTIME;
357: if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0) {
1.11 angelos 358: warn("could not read kern.cp_time");
1.10 angelos 359: bzero(cur.cp_time, sizeof(cur.cp_time));
360: }
361: size = sizeof(cur.tk_nin);
362: mib[0] = CTL_KERN;
363: mib[1] = KERN_TTY;
364: mib[2] = KERN_TTY_TKNIN;
365: if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0) {
1.11 angelos 366: warn("could not read kern.tty.tk_nin");
1.10 angelos 367: cur.tk_nin = 0;
368: }
369: size = sizeof(cur.tk_nin);
370: mib[0] = CTL_KERN;
371: mib[1] = KERN_TTY;
372: mib[2] = KERN_TTY_TKNOUT;
373: if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0) {
1.11 angelos 374: warn("could not read kern.tty.tk_nout");
1.10 angelos 375: cur.tk_nout = 0;
376: }
377: } else {
1.16 deraadt 378: #if !defined(NOKVM)
1.10 angelos 379: p = dk_drivehead;
380:
1.22 tdeval 381: for (i = 0; i < cur.dk_ndrive; i++) {
1.10 angelos 382: deref_kptr(p, &cur_disk, sizeof(cur_disk));
1.24 tedu 383: cur.dk_rxfer[i] = cur_disk.dk_rxfer;
384: cur.dk_wxfer[i] = cur_disk.dk_wxfer;
1.10 angelos 385: cur.dk_seek[i] = cur_disk.dk_seek;
1.24 tedu 386: cur.dk_rbytes[i] = cur_disk.dk_rbytes;
387: cur.dk_wbytes[i] = cur_disk.dk_wbytes;
1.10 angelos 388: timerset(&(cur_disk.dk_time), &(cur.dk_time[i]));
1.27 otto 389: p = TAILQ_NEXT(&cur_disk, dk_link);
1.10 angelos 390: }
391: deref_nl(X_CP_TIME, cur.cp_time, sizeof(cur.cp_time));
392: deref_nl(X_TK_NIN, &cur.tk_nin, sizeof(cur.tk_nin));
393: deref_nl(X_TK_NOUT, &cur.tk_nout, sizeof(cur.tk_nout));
1.16 deraadt 394: #endif /* !defined(NOKVM) */
1.1 tholo 395: }
396: }
397:
398: /*
399: * Perform all of the initialization and memory allocation needed to
400: * track disk statistics.
401: */
402: int
1.28 otto 403: dkinit(int sel)
1.1 tholo 404: {
1.19 deraadt 405: #if !defined(NOKVM)
1.1 tholo 406: struct disklist_head disk_head;
407: struct disk cur_disk, *p;
408: char errbuf[_POSIX2_LINE_MAX];
1.19 deraadt 409: #endif
1.1 tholo 410: static int once = 0;
411: extern int hz;
1.10 angelos 412: int i, mib[2];
413: size_t size;
414: struct clockinfo clkinfo;
415: char *disknames, *name, *bufpp;
1.26 djm 416: gid_t gid;
1.1 tholo 417:
418: if (once)
419: return(1);
420:
1.26 djm 421: gid = getgid();
1.10 angelos 422: if (nlistf != NULL || memf != NULL) {
1.16 deraadt 423: #if !defined(NOKVM)
1.26 djm 424: if (memf != NULL)
425: if (setresgid(gid, gid, gid) == -1)
426: err(1, "setresgid");
1.20 deraadt 427:
1.10 angelos 428: /* Open the kernel. */
1.20 deraadt 429: if (kd == NULL &&
430: (kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
1.10 angelos 431: errbuf)) == NULL)
432: errx(1, "kvm_openfiles: %s", errbuf);
1.20 deraadt 433:
1.26 djm 434: if (memf == NULL)
435: if (setresgid(gid, gid, gid) == -1)
436: err(1, "setresgid");
1.10 angelos 437:
438: /* Obtain the namelist symbols from the kernel. */
439: if (kvm_nlist(kd, namelist))
440: KVM_ERROR("kvm_nlist failed to read symbols.");
441:
442: /* Get the number of attached drives. */
1.22 tdeval 443: deref_nl(X_DISK_COUNT, &cur.dk_ndrive, sizeof(cur.dk_ndrive));
1.10 angelos 444:
1.22 tdeval 445: if (cur.dk_ndrive < 0)
446: errx(1, "invalid _disk_count %d.", cur.dk_ndrive);
1.10 angelos 447:
1.1 tholo 448: /* Get a pointer to the first disk. */
449: deref_nl(X_DISKLIST, &disk_head, sizeof(disk_head));
1.27 otto 450: dk_drivehead = TAILQ_FIRST(&disk_head);
1.10 angelos 451:
452: /* Get ticks per second. */
453: deref_nl(X_STATHZ, &hz, sizeof(hz));
454: if (!hz)
455: deref_nl(X_HZ, &hz, sizeof(hz));
1.16 deraadt 456: #endif /* !defined(NOKVM) */
1.10 angelos 457: } else {
458: /* Get the number of attached drives. */
459: mib[0] = CTL_HW;
460: mib[1] = HW_DISKCOUNT;
1.22 tdeval 461: size = sizeof(cur.dk_ndrive);
462: if (sysctl(mib, 2, &cur.dk_ndrive, &size, NULL, 0) < 0 ) {
1.11 angelos 463: warn("could not read hw.diskcount");
1.22 tdeval 464: cur.dk_ndrive = 0;
1.10 angelos 465: }
466:
467: /* Get ticks per second. */
468: mib[0] = CTL_KERN;
469: mib[1] = KERN_CLOCKRATE;
470: size = sizeof(clkinfo);
471: if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) {
1.11 angelos 472: warn("could not read kern.clockrate");
1.10 angelos 473: hz = 0;
474: } else
475: hz = clkinfo.stathz;
1.1 tholo 476: }
477:
478: /* allocate space for the statistics */
1.29 deraadt 479: cur.dk_time = calloc((size_t)cur.dk_ndrive, sizeof(struct timeval));
480: cur.dk_rxfer = calloc((size_t)cur.dk_ndrive, sizeof(u_int64_t));
481: cur.dk_wxfer = calloc((size_t)cur.dk_ndrive, sizeof(u_int64_t));
482: cur.dk_seek = calloc((size_t)cur.dk_ndrive, sizeof(u_int64_t));
483: cur.dk_rbytes = calloc((size_t)cur.dk_ndrive, sizeof(u_int64_t));
484: cur.dk_wbytes = calloc((size_t)cur.dk_ndrive, sizeof(u_int64_t));
485: last.dk_time = calloc((size_t)cur.dk_ndrive, sizeof(struct timeval));
486: last.dk_rxfer = calloc((size_t)cur.dk_ndrive, sizeof(u_int64_t));
487: last.dk_wxfer = calloc((size_t)cur.dk_ndrive, sizeof(u_int64_t));
488: last.dk_seek = calloc((size_t)cur.dk_ndrive, sizeof(u_int64_t));
489: last.dk_rbytes = calloc((size_t)cur.dk_ndrive, sizeof(u_int64_t));
490: last.dk_wbytes = calloc((size_t)cur.dk_ndrive, sizeof(u_int64_t));
491: cur.dk_select = calloc((size_t)cur.dk_ndrive, sizeof(int));
492: cur.dk_name = calloc((size_t)cur.dk_ndrive, sizeof(char *));
1.30 ! deraadt 493:
1.24 tedu 494: if (!cur.dk_time || !cur.dk_rxfer || !cur.dk_wxfer || !cur.dk_seek ||
495: !cur.dk_rbytes || !cur.dk_wbytes || !last.dk_time ||
496: !last.dk_rxfer || !last.dk_wxfer || !last.dk_seek ||
497: !cur.dk_select || !cur.dk_name)
1.1 tholo 498: errx(1, "Memory allocation failure.");
499:
500: /* Set up the compatibility interfaces. */
1.22 tdeval 501: dk_ndrive = cur.dk_ndrive;
1.1 tholo 502: dk_select = cur.dk_select;
503: dr_name = cur.dk_name;
504:
1.22 tdeval 505: /* Read the disk names and set initial selection. */
1.10 angelos 506: if (nlistf == NULL && memf == NULL) {
507: mib[0] = CTL_HW;
508: mib[1] = HW_DISKNAMES;
509: size = 0;
510: if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0)
511: err(1, "can't get hw.disknames");
1.12 angelos 512: disknames = malloc(size);
1.10 angelos 513: if (disknames == NULL)
514: err(1, NULL);
515: if (sysctl(mib, 2, disknames, &size, NULL, 0) < 0)
516: err(1, "can't get hw.disknames");
517: bufpp = disknames;
1.23 millert 518: for (i = 0; i < dk_ndrive && (name = strsep(&bufpp, ",")) != NULL; i++) {
519: cur.dk_name[i] = name;
1.28 otto 520: cur.dk_select[i] = sel;
1.10 angelos 521: }
522: } else {
1.16 deraadt 523: #if !defined(NOKVM)
1.10 angelos 524: p = dk_drivehead;
1.22 tdeval 525: for (i = 0; i < cur.dk_ndrive; i++) {
1.10 angelos 526: char buf[10];
1.16 deraadt 527:
1.10 angelos 528: deref_kptr(p, &cur_disk, sizeof(cur_disk));
529: deref_kptr(cur_disk.dk_name, buf, sizeof(buf));
530: cur.dk_name[i] = strdup(buf);
531: if (!cur.dk_name[i])
532: errx(1, "Memory allocation failure.");
1.28 otto 533: cur.dk_select[i] = sel;
1.1 tholo 534:
1.27 otto 535: p = TAILQ_NEXT(&cur_disk, dk_link);
1.10 angelos 536: }
1.16 deraadt 537: #endif /* !defined(NOKVM) */
1.1 tholo 538: }
539:
540: /* Never do this initalization again. */
541: once = 1;
542: return(1);
543: }
544:
1.16 deraadt 545: #if !defined(NOKVM)
1.1 tholo 546: /*
1.30 ! deraadt 547: * Dereference the kernel pointer `kptr' and fill in the local copy
1.1 tholo 548: * pointed to by `ptr'. The storage space must be pre-allocated,
549: * and the size of the copy passed in `len'.
550: */
551: static void
1.21 deraadt 552: deref_kptr(void *kptr, void *ptr, size_t len)
1.1 tholo 553: {
554: char buf[128];
555:
1.6 art 556: if (kvm_read(kd, (u_long)kptr, ptr, len) != len) {
1.1 tholo 557: bzero(buf, sizeof(buf));
558: snprintf(buf, (sizeof(buf) - 1),
1.2 deraadt 559: "can't dereference kptr 0x%lx", (u_long)kptr);
1.1 tholo 560: KVM_ERROR(buf);
561: }
562: }
1.17 drahn 563: #endif /* !defined(NOKVM) */