xref: /freebsd/usr.bin/ipcs/ipc.c (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 /*
2  * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
19  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The split of ipcs.c into ipcs.c and ipc.c to accomodate the
28  * changes in ipcrm.c was done by Edwin Groothuis <edwin@FreeBSD.org>
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/types.h>
35 #include <sys/sysctl.h>
36 #define _KERNEL
37 #include <sys/sem.h>
38 #include <sys/shm.h>
39 #include <sys/msg.h>
40 #undef _KERNEL
41 
42 #include <assert.h>
43 #include <err.h>
44 #include <kvm.h>
45 #include <nlist.h>
46 #include <stddef.h>
47 #include <stdio.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 void	kget(int idx, void *addr, size_t size);
59 
60 struct nlist symbols[] = {
61 	{"sema"},
62 	{"seminfo"},
63 	{"msginfo"},
64 	{"msqids"},
65 	{"shminfo"},
66 	{"shmsegs"},
67 	{NULL}
68 };
69 
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))
75 
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))
85 
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))
92 
93 #define	X(a, b)	{ "kern.ipc." #a, offsetof(TYPEC, a), (b) },
94 #define	TYPEC	struct shminfo
95 struct scgs_vector shminfo_scgsv[] = { SHMINFO_XVEC { NULL } };
96 #undef	TYPEC
97 #define	TYPEC	struct seminfo
98 struct scgs_vector seminfo_scgsv[] = { SEMINFO_XVEC { NULL } };
99 #undef	TYPEC
100 #define	TYPEC	struct msginfo
101 struct scgs_vector msginfo_scgsv[] = { MSGINFO_XVEC { NULL } };
102 #undef	TYPEC
103 #undef	X
104 
105 kvm_t *kd;
106 
107 void
108 sysctlgatherstruct(void *addr, size_t size, struct scgs_vector *vecarr)
109 {
110 	struct scgs_vector *xp;
111 	size_t tsiz;
112 	int rv;
113 
114 	for (xp = vecarr; xp->sysctl != NULL; xp++) {
115 		assert(xp->offset <= size);
116 		tsiz = xp->size;
117 		rv = sysctlbyname(xp->sysctl, (char *)addr + xp->offset,
118 		    &tsiz, NULL, 0);
119 		if (rv == -1)
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);
124 	}
125 }
126 
127 void
128 kget(int idx, void *addr, size_t size)
129 {
130 	const char *symn;		/* symbol name */
131 	size_t tsiz;
132 	int rv;
133 	unsigned long kaddr;
134 	const char *sym2sysctl[] = {	/* symbol to sysctl name table */
135 		"kern.ipc.sema",
136 		"kern.ipc.seminfo",
137 		"kern.ipc.msginfo",
138 		"kern.ipc.msqids",
139 		"kern.ipc.shminfo",
140 		"kern.ipc.shmsegs" };
141 
142 	assert((unsigned)idx <= sizeof(sym2sysctl) / sizeof(*sym2sysctl));
143 	if (!use_sysctl) {
144 		symn = symbols[idx].n_name;
145 		if (*symn == '_')
146 			symn++;
147 		if (symbols[idx].n_type == 0 || symbols[idx].n_value == 0)
148 			errx(1, "symbol %s undefined", symn);
149 		/*
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.
153 		 */
154 		switch (idx) {
155 		case X_MSQIDS:
156 			tsiz = sizeof(msqids);
157 			rv = kvm_read(kd, symbols[idx].n_value,
158 			    &msqids, tsiz);
159 			kaddr = (u_long)msqids;
160 			break;
161 		case X_SHMSEGS:
162 			tsiz = sizeof(shmsegs);
163 			rv = kvm_read(kd, symbols[idx].n_value,
164 			    &shmsegs, tsiz);
165 			kaddr = (u_long)shmsegs;
166 			break;
167 		case X_SEMA:
168 			tsiz = sizeof(sema);
169 			rv = kvm_read(kd, symbols[idx].n_value,
170 			    &sema, tsiz);
171 			kaddr = (u_long)sema;
172 			break;
173 		default:
174 			rv = tsiz = 0;
175 			kaddr = symbols[idx].n_value;
176 			break;
177 		}
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));
182 	} else {
183 		switch (idx) {
184 		case X_SHMINFO:
185 			sysctlgatherstruct(addr, size, shminfo_scgsv);
186 			break;
187 		case X_SEMINFO:
188 			sysctlgatherstruct(addr, size, seminfo_scgsv);
189 			break;
190 		case X_MSGINFO:
191 			sysctlgatherstruct(addr, size, msginfo_scgsv);
192 			break;
193 		default:
194 			tsiz = size;
195 			rv = sysctlbyname(sym2sysctl[idx], addr, &tsiz,
196 			    NULL, 0);
197 			if (rv == -1)
198 				err(1, "sysctlbyname: %s", sym2sysctl[idx]);
199 			if (tsiz != size)
200 				errx(1, "%s size mismatch "
201 				    "(expected %zu, got %zu)",
202 				    sym2sysctl[idx], size, tsiz);
203 			break;
204 		}
205 	}
206 }
207