xref: /freebsd/usr.bin/ipcs/ipc.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
5  * 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. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
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.
28  *
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>
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/types.h>
37 #include <sys/sysctl.h>
38 #define	_WANT_SYSVMSG_INTERNALS
39 #include <sys/msg.h>
40 #define	_WANT_SYSVSEM_INTERNALS
41 #include <sys/sem.h>
42 #define	_WANT_SYSVSHM_INTERNALS
43 #include <sys/shm.h>
44 
45 #include <assert.h>
46 #include <err.h>
47 #include <kvm.h>
48 #include <nlist.h>
49 #include <stddef.h>
50 
51 #include "ipc.h"
52 
53 int	use_sysctl = 1;
54 struct semid_kernel	*sema;
55 struct seminfo		seminfo;
56 struct msginfo		msginfo;
57 struct msqid_kernel	*msqids;
58 struct shminfo		shminfo;
59 struct shmid_kernel	*shmsegs;
60 
61 struct nlist symbols[] = {
62 	{ .n_name = "sema" },
63 	{ .n_name = "seminfo" },
64 	{ .n_name = "msginfo" },
65 	{ .n_name = "msqids" },
66 	{ .n_name = "shminfo" },
67 	{ .n_name = "shmsegs" },
68 	{ .n_name = NULL }
69 };
70 
71 #define	SHMINFO_XVEC	X(shmmax, sizeof(u_long))			\
72 			X(shmmin, sizeof(u_long))			\
73 			X(shmmni, sizeof(u_long))			\
74 			X(shmseg, sizeof(u_long))			\
75 			X(shmall, sizeof(u_long))
76 
77 #define	SEMINFO_XVEC	X(semmni, sizeof(int))				\
78 			X(semmns, sizeof(int))				\
79 			X(semmnu, sizeof(int))				\
80 			X(semmsl, sizeof(int))				\
81 			X(semopm, sizeof(int))				\
82 			X(semume, sizeof(int))				\
83 			X(semusz, sizeof(int))				\
84 			X(semvmx, sizeof(int))				\
85 			X(semaem, sizeof(int))
86 
87 #define	MSGINFO_XVEC	X(msgmax, sizeof(int))				\
88 			X(msgmni, sizeof(int))				\
89 			X(msgmnb, sizeof(int))				\
90 			X(msgtql, sizeof(int))				\
91 			X(msgssz, sizeof(int))				\
92 			X(msgseg, sizeof(int))
93 
94 #define	X(a, b)	{ "kern.ipc." #a, offsetof(TYPEC, a), (b) },
95 #define	TYPEC	struct shminfo
96 static struct scgs_vector shminfo_scgsv[] = { SHMINFO_XVEC { .sysctl=NULL } };
97 #undef	TYPEC
98 #define	TYPEC	struct seminfo
99 static struct scgs_vector seminfo_scgsv[] = { SEMINFO_XVEC { .sysctl=NULL } };
100 #undef	TYPEC
101 #define	TYPEC	struct msginfo
102 static struct scgs_vector msginfo_scgsv[] = { MSGINFO_XVEC { .sysctl=NULL } };
103 #undef	TYPEC
104 #undef	X
105 
106 kvm_t *kd;
107 
108 void
109 sysctlgatherstruct(void *addr, size_t size, struct scgs_vector *vecarr)
110 {
111 	struct scgs_vector *xp;
112 	size_t tsiz;
113 	int rv;
114 
115 	for (xp = vecarr; xp->sysctl != NULL; xp++) {
116 		assert(xp->offset <= size);
117 		tsiz = xp->size;
118 		rv = sysctlbyname(xp->sysctl, (char *)addr + xp->offset,
119 		    &tsiz, NULL, 0);
120 		if (rv == -1)
121 			err(1, "sysctlbyname: %s", xp->sysctl);
122 		if (tsiz != xp->size)
123 			errx(1, "%s size mismatch (expected %zu, got %zu)",
124 			    xp->sysctl, xp->size, tsiz);
125 	}
126 }
127 
128 void
129 kget(int idx, void *addr, size_t size)
130 {
131 	const char *symn;		/* symbol name */
132 	size_t tsiz;
133 	int rv;
134 	unsigned long kaddr;
135 	const char *sym2sysctl[] = {	/* symbol to sysctl name table */
136 		"kern.ipc.sema",
137 		"kern.ipc.seminfo",
138 		"kern.ipc.msginfo",
139 		"kern.ipc.msqids",
140 		"kern.ipc.shminfo",
141 		"kern.ipc.shmsegs" };
142 
143 	assert((unsigned)idx <= sizeof(sym2sysctl) / sizeof(*sym2sysctl));
144 	if (!use_sysctl) {
145 		symn = symbols[idx].n_name;
146 		if (*symn == '_')
147 			symn++;
148 		if (symbols[idx].n_type == 0 || symbols[idx].n_value == 0)
149 			errx(1, "symbol %s undefined", symn);
150 		/*
151 		 * For some symbols, the value we retrieve is
152 		 * actually a pointer; since we want the actual value,
153 		 * we have to manually dereference it.
154 		 */
155 		switch (idx) {
156 		case X_MSQIDS:
157 			tsiz = sizeof(msqids);
158 			rv = kvm_read(kd, symbols[idx].n_value,
159 			    &msqids, tsiz);
160 			kaddr = (u_long)msqids;
161 			break;
162 		case X_SHMSEGS:
163 			tsiz = sizeof(shmsegs);
164 			rv = kvm_read(kd, symbols[idx].n_value,
165 			    &shmsegs, tsiz);
166 			kaddr = (u_long)shmsegs;
167 			break;
168 		case X_SEMA:
169 			tsiz = sizeof(sema);
170 			rv = kvm_read(kd, symbols[idx].n_value,
171 			    &sema, tsiz);
172 			kaddr = (u_long)sema;
173 			break;
174 		default:
175 			rv = tsiz = 0;
176 			kaddr = symbols[idx].n_value;
177 			break;
178 		}
179 		if ((unsigned)rv != tsiz)
180 			errx(1, "%s: %s", symn, kvm_geterr(kd));
181 		if ((unsigned)kvm_read(kd, kaddr, addr, size) != size)
182 			errx(1, "%s: %s", symn, kvm_geterr(kd));
183 	} else {
184 		switch (idx) {
185 		case X_SHMINFO:
186 			sysctlgatherstruct(addr, size, shminfo_scgsv);
187 			break;
188 		case X_SEMINFO:
189 			sysctlgatherstruct(addr, size, seminfo_scgsv);
190 			break;
191 		case X_MSGINFO:
192 			sysctlgatherstruct(addr, size, msginfo_scgsv);
193 			break;
194 		default:
195 			tsiz = size;
196 			rv = sysctlbyname(sym2sysctl[idx], addr, &tsiz,
197 			    NULL, 0);
198 			if (rv == -1)
199 				err(1, "sysctlbyname: %s", sym2sysctl[idx]);
200 			if (tsiz != size)
201 				errx(1, "%s size mismatch "
202 				    "(expected %zu, got %zu)",
203 				    sym2sysctl[idx], size, tsiz);
204 			break;
205 		}
206 	}
207 }
208