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