19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1980, 1992, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
329ff712b0SMark Murray
339b50d902SRodney W. Grimes
349b50d902SRodney W. Grimes #include <sys/types.h>
35342e2faaSThomas Moestl #include <sys/sysctl.h>
36342e2faaSThomas Moestl
37821df508SXin LI #include <err.h>
389ff712b0SMark Murray #include <errno.h>
39342e2faaSThomas Moestl #include <stdlib.h>
409ff712b0SMark Murray #include <string.h>
41342e2faaSThomas Moestl
429b50d902SRodney W. Grimes #include "systat.h"
439b50d902SRodney W. Grimes #include "extern.h"
449b50d902SRodney W. Grimes
459b50d902SRodney W. Grimes int
kvm_ckread(void * a,void * b,int l)4693b9f504SXin LI kvm_ckread(void *a, void *b, int l)
479b50d902SRodney W. Grimes {
489b50d902SRodney W. Grimes if (kvm_read(kd, (u_long)a, b, l) != l) {
499b50d902SRodney W. Grimes if (verbose)
509ff712b0SMark Murray error("error reading kmem at %p", a);
519b50d902SRodney W. Grimes return (0);
529b50d902SRodney W. Grimes }
539b50d902SRodney W. Grimes else
549b50d902SRodney W. Grimes return (1);
559b50d902SRodney W. Grimes }
56342e2faaSThomas Moestl
57*9a8fc6bbSMichael Reifenberger void
getsysctl(const char * name,void * ptr,size_t len)58*9a8fc6bbSMichael Reifenberger getsysctl(const char *name, void *ptr, size_t len)
59342e2faaSThomas Moestl {
60342e2faaSThomas Moestl size_t nlen = len;
61*9a8fc6bbSMichael Reifenberger
629ff712b0SMark Murray if (sysctlbyname(name, ptr, &nlen, NULL, 0) != 0) {
63342e2faaSThomas Moestl error("sysctl(%s...) failed: %s", name,
64342e2faaSThomas Moestl strerror(errno));
65342e2faaSThomas Moestl }
66342e2faaSThomas Moestl if (nlen != len) {
67970bdbf5SMichael Tuexen error("sysctl(%s...) expected %zu, got %zu", name, len, nlen);
68342e2faaSThomas Moestl }
69342e2faaSThomas Moestl }
70342e2faaSThomas Moestl
71342e2faaSThomas Moestl /*
72342e2faaSThomas Moestl * Read sysctl data with variable size. Try some times (with increasing
73342e2faaSThomas Moestl * buffers), fail if still too small.
74342e2faaSThomas Moestl * This is needed sysctls with possibly raplidly increasing data sizes,
75342e2faaSThomas Moestl * but imposes little overhead in the case of constant sizes.
76342e2faaSThomas Moestl * Returns NULL on error, or a pointer to freshly malloc()'ed memory that holds
77342e2faaSThomas Moestl * the requested data.
78342e2faaSThomas Moestl * If szp is not NULL, the size of the returned data will be written into *szp.
79342e2faaSThomas Moestl */
80342e2faaSThomas Moestl
81342e2faaSThomas Moestl /* Some defines: Number of tries. */
82342e2faaSThomas Moestl #define SD_NTRIES 10
83342e2faaSThomas Moestl /* Percent of over-allocation (initial) */
84342e2faaSThomas Moestl #define SD_MARGIN 10
85342e2faaSThomas Moestl /*
86342e2faaSThomas Moestl * Factor for over-allocation in percent (the margin is increased by this on
87342e2faaSThomas Moestl * any failed try).
88342e2faaSThomas Moestl */
89342e2faaSThomas Moestl #define SD_FACTOR 50
90342e2faaSThomas Moestl /* Maximum supported MIB depth */
91342e2faaSThomas Moestl #define SD_MAXMIB 16
92342e2faaSThomas Moestl
93342e2faaSThomas Moestl char *
sysctl_dynread(const char * n,size_t * szp)9493b9f504SXin LI sysctl_dynread(const char *n, size_t *szp)
95342e2faaSThomas Moestl {
96342e2faaSThomas Moestl char *rv = NULL;
97342e2faaSThomas Moestl int mib[SD_MAXMIB];
98342e2faaSThomas Moestl size_t mibsz = SD_MAXMIB;
99342e2faaSThomas Moestl size_t mrg = SD_MARGIN;
100342e2faaSThomas Moestl size_t sz;
101342e2faaSThomas Moestl int i;
102342e2faaSThomas Moestl
103342e2faaSThomas Moestl /* cache the MIB */
104342e2faaSThomas Moestl if (sysctlnametomib(n, mib, &mibsz) == -1) {
105342e2faaSThomas Moestl if (errno == ENOMEM) {
106342e2faaSThomas Moestl error("XXX: SD_MAXMIB too small, please bump!");
107342e2faaSThomas Moestl }
108342e2faaSThomas Moestl return NULL;
109342e2faaSThomas Moestl }
110342e2faaSThomas Moestl for (i = 0; i < SD_NTRIES; i++) {
111342e2faaSThomas Moestl /* get needed buffer size */
112342e2faaSThomas Moestl if (sysctl(mib, mibsz, NULL, &sz, NULL, 0) == -1)
113342e2faaSThomas Moestl break;
114342e2faaSThomas Moestl sz += sz * mrg / 100;
115342e2faaSThomas Moestl if ((rv = (char *)malloc(sz)) == NULL) {
116342e2faaSThomas Moestl error("Out of memory!");
117342e2faaSThomas Moestl return NULL;
118342e2faaSThomas Moestl }
119342e2faaSThomas Moestl if (sysctl(mib, mibsz, rv, &sz, NULL, 0) == -1) {
120342e2faaSThomas Moestl free(rv);
121342e2faaSThomas Moestl rv = NULL;
122342e2faaSThomas Moestl if (errno == ENOMEM) {
123342e2faaSThomas Moestl mrg += mrg * SD_FACTOR / 100;
124342e2faaSThomas Moestl } else
125342e2faaSThomas Moestl break;
126342e2faaSThomas Moestl } else {
127342e2faaSThomas Moestl /* success */
128342e2faaSThomas Moestl if (szp != NULL)
129342e2faaSThomas Moestl *szp = sz;
130342e2faaSThomas Moestl break;
131342e2faaSThomas Moestl }
132342e2faaSThomas Moestl }
133342e2faaSThomas Moestl
134342e2faaSThomas Moestl return rv;
135342e2faaSThomas Moestl }
136