]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkvm/kvm_getswapinfo.c
login(1): when exporting variables check the result of setenv(3)
[FreeBSD/FreeBSD.git] / lib / libkvm / kvm_getswapinfo.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999, Matthew Dillon.  All Rights Reserved.
5  * Copyright (c) 2001, Thomas Moestl.  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  *
16  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/stat.h>
35 #include <sys/blist.h>
36 #include <sys/queue.h>
37 #include <sys/sysctl.h>
38
39 #include <vm/swap_pager.h>
40 #include <vm/vm_param.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <kvm.h>
46 #include <nlist.h>
47 #include <paths.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <limits.h>
53
54 #include "kvm_private.h"
55
56 static struct nlist kvm_swap_nl[] = {
57         { .n_name = "_swtailq" },       /* list of swap devices and sizes */
58         { .n_name = "_dmmax" },         /* maximum size of a swap block */
59         { .n_name = NULL }
60 };
61
62 #define NL_SWTAILQ      0
63 #define NL_DMMAX        1
64
65 static int kvm_swap_nl_cached = 0;
66 static int unswdev;  /* number of found swap dev's */
67 static int dmmax;
68
69 static int  kvm_getswapinfo_kvm(kvm_t *, struct kvm_swap *, int, int);
70 static int  kvm_getswapinfo_sysctl(kvm_t *, struct kvm_swap *, int, int);
71 static int  nlist_init(kvm_t *);
72 static int  getsysctl(kvm_t *, const char *, void *, size_t);
73
74 #define KREAD(kd, addr, obj) \
75         (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
76 #define KGET(idx, var)                                                  \
77         KGET2(kvm_swap_nl[(idx)].n_value, var, kvm_swap_nl[(idx)].n_name)
78 #define KGET2(addr, var, msg)                                           \
79         if (KREAD(kd, (u_long)(addr), (var))) {                         \
80                 _kvm_err(kd, kd->program, "cannot read %s", msg);       \
81                 return (-1);                                            \
82         }
83
84 #define GETSWDEVNAME(dev, str, flags)                                   \
85         if (dev == NODEV) {                                             \
86                 strlcpy(str, "[NFS swap]", sizeof(str));                \
87         } else {                                                        \
88                 snprintf(                                               \
89                     str, sizeof(str),"%s%s",                            \
90                     ((flags & SWIF_DEV_PREFIX) ? _PATH_DEV : ""),       \
91                     devname(dev, S_IFCHR)                               \
92                 );                                                      \
93         }
94
95 int
96 kvm_getswapinfo(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max, int flags)
97 {
98
99         /*
100          * clear cache
101          */
102         if (kd == NULL) {
103                 kvm_swap_nl_cached = 0;
104                 return(0);
105         }
106
107         if (ISALIVE(kd)) {
108                 return kvm_getswapinfo_sysctl(kd, swap_ary, swap_max, flags);
109         } else {
110                 return kvm_getswapinfo_kvm(kd, swap_ary, swap_max, flags);
111         }
112 }
113
114 int
115 kvm_getswapinfo_kvm(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max,
116     int flags)
117 {
118         int i, ttl;
119         TAILQ_HEAD(, swdevt) swtailq;
120         struct swdevt *sp, swinfo;
121         struct kvm_swap tot;
122
123         if (!kd->arch->ka_native(kd)) {
124                 _kvm_err(kd, kd->program,
125                     "cannot read swapinfo from non-native core");
126                 return (-1);
127         }
128
129         if (!nlist_init(kd))
130                 return (-1);
131
132         bzero(&tot, sizeof(tot));
133         KGET(NL_SWTAILQ, &swtailq);
134         sp = TAILQ_FIRST(&swtailq);
135         for (i = 0; sp != NULL; i++) {
136                 KGET2(sp, &swinfo, "swinfo");
137                 ttl = swinfo.sw_nblks - dmmax;
138                 if (i < swap_max - 1) {
139                         bzero(&swap_ary[i], sizeof(swap_ary[i]));
140                         swap_ary[i].ksw_total = ttl;
141                         swap_ary[i].ksw_used = swinfo.sw_used;
142                         swap_ary[i].ksw_flags = swinfo.sw_flags;
143                         GETSWDEVNAME(swinfo.sw_dev, swap_ary[i].ksw_devname,
144                              flags);
145                 }
146                 tot.ksw_total += ttl;
147                 tot.ksw_used += swinfo.sw_used;
148                 sp = TAILQ_NEXT(&swinfo, sw_list);
149         }
150
151         if (i >= swap_max)
152                 i = swap_max - 1;
153         if (i >= 0)
154                 swap_ary[i] = tot;
155
156         return(i);
157 }
158
159 #define GETSYSCTL(kd, name, var)                                        \
160             getsysctl(kd, name, &(var), sizeof(var))
161
162 /* The maximum MIB length for vm.swap_info and an additional device number */
163 #define SWI_MAXMIB      3
164
165 int
166 kvm_getswapinfo_sysctl(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max,
167     int flags)
168 {
169         int ti, ttl;
170         size_t mibi, len;
171         int soid[SWI_MAXMIB];
172         struct xswdev xsd;
173         struct kvm_swap tot;
174
175         if (!GETSYSCTL(kd, "vm.dmmax", dmmax))
176                 return -1;
177
178         mibi = SWI_MAXMIB - 1;
179         if (sysctlnametomib("vm.swap_info", soid, &mibi) == -1) {
180                 _kvm_err(kd, kd->program, "sysctlnametomib failed: %s",
181                     strerror(errno));
182                 return -1;
183         }
184         bzero(&tot, sizeof(tot));
185         for (unswdev = 0;; unswdev++) {
186                 soid[mibi] = unswdev;
187                 len = sizeof(xsd);
188                 if (sysctl(soid, mibi + 1, &xsd, &len, NULL, 0) == -1) {
189                         if (errno == ENOENT)
190                                 break;
191                         _kvm_err(kd, kd->program, "cannot read sysctl: %s.",
192                             strerror(errno));
193                         return -1;
194                 }
195                 if (len != sizeof(xsd)) {
196                         _kvm_err(kd, kd->program, "struct xswdev has unexpected "
197                             "size;  kernel and libkvm out of sync?");
198                         return -1;
199                 }
200                 if (xsd.xsw_version != XSWDEV_VERSION) {
201                         _kvm_err(kd, kd->program, "struct xswdev version "
202                             "mismatch; kernel and libkvm out of sync?");
203                         return -1;
204                 }
205
206                 ttl = xsd.xsw_nblks - dmmax;
207                 if (unswdev < swap_max - 1) {
208                         bzero(&swap_ary[unswdev], sizeof(swap_ary[unswdev]));
209                         swap_ary[unswdev].ksw_total = ttl;
210                         swap_ary[unswdev].ksw_used = xsd.xsw_used;
211                         swap_ary[unswdev].ksw_flags = xsd.xsw_flags;
212                         GETSWDEVNAME(xsd.xsw_dev, swap_ary[unswdev].ksw_devname,
213                              flags);
214                 }
215                 tot.ksw_total += ttl;
216                 tot.ksw_used += xsd.xsw_used;
217         }
218
219         ti = unswdev;
220         if (ti >= swap_max)
221                 ti = swap_max - 1;
222         if (ti >= 0)
223                 swap_ary[ti] = tot;
224
225         return(ti);
226 }
227
228 static int
229 nlist_init(kvm_t *kd)
230 {
231
232         if (kvm_swap_nl_cached)
233                 return (1);
234
235         if (kvm_nlist(kd, kvm_swap_nl) < 0)
236                 return (0);
237
238         /* Required entries */
239         if (kvm_swap_nl[NL_SWTAILQ].n_value == 0) {
240                 _kvm_err(kd, kd->program, "unable to find swtailq");
241                 return (0);
242         }
243
244         if (kvm_swap_nl[NL_DMMAX].n_value == 0) {
245                 _kvm_err(kd, kd->program, "unable to find dmmax");
246                 return (0);
247         }
248
249         /* Get globals, type of swap */
250         KGET(NL_DMMAX, &dmmax);
251
252         kvm_swap_nl_cached = 1;
253         return (1);
254 }
255
256 static int
257 getsysctl(kvm_t *kd, const char *name, void *ptr, size_t len)
258 {
259         size_t nlen = len;
260         if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
261                 _kvm_err(kd, kd->program, "cannot read sysctl %s:%s", name,
262                     strerror(errno));
263                 return (0);
264         }
265         if (nlen != len) {
266                 _kvm_err(kd, kd->program, "sysctl %s has unexpected size", name);
267                 return (0);
268         }
269         return (1);
270 }