fetch.c revision 74671
1/*-
2 * Copyright (c) 1980, 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/usr.bin/systat/fetch.c 74671 2001-03-23 03:58:25Z tmm $
34 */
35
36#ifndef lint
37static char sccsid[] = "@(#)fetch.c	8.1 (Berkeley) 6/6/93";
38#endif /* not lint */
39
40#include <sys/types.h>
41#include <sys/sysctl.h>
42
43#include <errno.h>
44#include <err.h>
45#include <string.h>
46#include <stdlib.h>
47
48#include "systat.h"
49#include "extern.h"
50
51int
52kvm_ckread(a, b, l)
53	void *a, *b;
54	int l;
55{
56	if (kvm_read(kd, (u_long)a, b, l) != l) {
57		if (verbose)
58			error("error reading kmem at %x", a);
59		return (0);
60	}
61	else
62		return (1);
63}
64
65void getsysctl(name, ptr, len)
66	char *name;
67	void *ptr;
68	size_t len;
69{
70	int err;
71	size_t nlen = len;
72	if ((err = sysctlbyname(name, ptr, &nlen, NULL, 0)) != 0) {
73		error("sysctl(%s...) failed: %s", name,
74		    strerror(errno));
75	}
76	if (nlen != len) {
77		error("sysctl(%s...) expected %d, got %d", name,
78		    len, nlen);
79    }
80}
81
82/*
83 * Read sysctl data with variable size. Try some times (with increasing
84 * buffers), fail if still too small.
85 * This is needed sysctls with possibly raplidly increasing data sizes,
86 * but imposes little overhead in the case of constant sizes.
87 * Returns NULL on error, or a pointer to freshly malloc()'ed memory that holds
88 * the requested data.
89 * If szp is not NULL, the size of the returned data will be written into *szp.
90 */
91
92/* Some defines: Number of tries. */
93#define SD_NTRIES  10
94/* Percent of over-allocation (initial) */
95#define SD_MARGIN  10
96/*
97 * Factor for over-allocation in percent (the margin is increased by this on
98 * any failed try).
99 */
100#define SD_FACTOR  50
101/* Maximum supported MIB depth */
102#define SD_MAXMIB  16
103
104char *
105sysctl_dynread(n, szp)
106	char *n;
107	size_t *szp;
108{
109	char   *rv = NULL;
110	int    mib[SD_MAXMIB];
111	size_t mibsz = SD_MAXMIB;
112	size_t mrg = SD_MARGIN;
113	size_t sz;
114	int i;
115
116	/* cache the MIB */
117	if (sysctlnametomib(n, mib, &mibsz) == -1) {
118		if (errno == ENOMEM) {
119			error("XXX: SD_MAXMIB too small, please bump!");
120		}
121		return NULL;
122	}
123	for (i = 0; i < SD_NTRIES; i++) {
124		/* get needed buffer size */
125		if (sysctl(mib, mibsz, NULL, &sz, NULL, 0) == -1)
126			break;
127		sz += sz * mrg / 100;
128		if ((rv = (char *)malloc(sz)) == NULL) {
129			error("Out of memory!");
130			return NULL;
131		}
132		if (sysctl(mib, mibsz, rv, &sz, NULL, 0) == -1) {
133			free(rv);
134			rv = NULL;
135			if (errno == ENOMEM) {
136				mrg += mrg * SD_FACTOR / 100;
137			} else
138				break;
139		} else {
140			/* success */
141			if (szp != NULL)
142				*szp = sz;
143			break;
144		}
145	}
146
147	return rv;
148}
149