2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The split of ipcs.c into ipcs.c and ipc.c to accommodate the
30 * changes in ipcrm.c was done by Edwin Groothuis <edwin@FreeBSD.org>
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/sysctl.h>
53 struct semid_kernel *sema;
54 struct seminfo seminfo;
55 struct msginfo msginfo;
56 struct msqid_kernel *msqids;
57 struct shminfo shminfo;
58 struct shmid_kernel *shmsegs;
60 struct nlist symbols[] = {
62 { .n_name = "seminfo" },
63 { .n_name = "msginfo" },
64 { .n_name = "msqids" },
65 { .n_name = "shminfo" },
66 { .n_name = "shmsegs" },
70 #define SHMINFO_XVEC X(shmmax, sizeof(u_long)) \
71 X(shmmin, sizeof(u_long)) \
72 X(shmmni, sizeof(u_long)) \
73 X(shmseg, sizeof(u_long)) \
74 X(shmall, sizeof(u_long))
76 #define SEMINFO_XVEC X(semmni, sizeof(int)) \
77 X(semmns, sizeof(int)) \
78 X(semmnu, sizeof(int)) \
79 X(semmsl, sizeof(int)) \
80 X(semopm, sizeof(int)) \
81 X(semume, sizeof(int)) \
82 X(semusz, sizeof(int)) \
83 X(semvmx, sizeof(int)) \
84 X(semaem, sizeof(int))
86 #define MSGINFO_XVEC X(msgmax, sizeof(int)) \
87 X(msgmni, sizeof(int)) \
88 X(msgmnb, sizeof(int)) \
89 X(msgtql, sizeof(int)) \
90 X(msgssz, sizeof(int)) \
91 X(msgseg, sizeof(int))
93 #define X(a, b) { "kern.ipc." #a, offsetof(TYPEC, a), (b) },
94 #define TYPEC struct shminfo
95 static struct scgs_vector shminfo_scgsv[] = { SHMINFO_XVEC { .sysctl=NULL } };
97 #define TYPEC struct seminfo
98 static struct scgs_vector seminfo_scgsv[] = { SEMINFO_XVEC { .sysctl=NULL } };
100 #define TYPEC struct msginfo
101 static struct scgs_vector msginfo_scgsv[] = { MSGINFO_XVEC { .sysctl=NULL } };
108 sysctlgatherstruct(void *addr, size_t size, struct scgs_vector *vecarr)
110 struct scgs_vector *xp;
114 for (xp = vecarr; xp->sysctl != NULL; xp++) {
115 assert(xp->offset <= size);
117 rv = sysctlbyname(xp->sysctl, (char *)addr + xp->offset,
120 err(1, "sysctlbyname: %s", xp->sysctl);
121 if (tsiz != xp->size)
122 errx(1, "%s size mismatch (expected %zu, got %zu)",
123 xp->sysctl, xp->size, tsiz);
128 kget(int idx, void *addr, size_t size)
130 const char *symn; /* symbol name */
134 const char *sym2sysctl[] = { /* symbol to sysctl name table */
140 "kern.ipc.shmsegs" };
142 assert((unsigned)idx <= sizeof(sym2sysctl) / sizeof(*sym2sysctl));
144 symn = symbols[idx].n_name;
147 if (symbols[idx].n_type == 0 || symbols[idx].n_value == 0)
148 errx(1, "symbol %s undefined", symn);
150 * For some symbols, the value we retrieve is
151 * actually a pointer; since we want the actual value,
152 * we have to manually dereference it.
156 tsiz = sizeof(msqids);
157 rv = kvm_read(kd, symbols[idx].n_value,
159 kaddr = (u_long)msqids;
162 tsiz = sizeof(shmsegs);
163 rv = kvm_read(kd, symbols[idx].n_value,
165 kaddr = (u_long)shmsegs;
169 rv = kvm_read(kd, symbols[idx].n_value,
171 kaddr = (u_long)sema;
175 kaddr = symbols[idx].n_value;
178 if ((unsigned)rv != tsiz)
179 errx(1, "%s: %s", symn, kvm_geterr(kd));
180 if ((unsigned)kvm_read(kd, kaddr, addr, size) != size)
181 errx(1, "%s: %s", symn, kvm_geterr(kd));
185 sysctlgatherstruct(addr, size, shminfo_scgsv);
188 sysctlgatherstruct(addr, size, seminfo_scgsv);
191 sysctlgatherstruct(addr, size, msginfo_scgsv);
195 rv = sysctlbyname(sym2sysctl[idx], addr, &tsiz,
198 err(1, "sysctlbyname: %s", sym2sysctl[idx]);
200 errx(1, "%s size mismatch "
201 "(expected %zu, got %zu)",
202 sym2sysctl[idx], size, tsiz);