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