sysv_ipc.c revision 1.25.6.1 1 1.25.6.1 skrll /* $NetBSD: sysv_ipc.c,v 1.25.6.1 2015/06/06 14:40:22 skrll Exp $ */
2 1.7 cgd
3 1.13 mycroft /*-
4 1.20 ad * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
5 1.13 mycroft * All rights reserved.
6 1.13 mycroft *
7 1.13 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.13 mycroft * by Charles M. Hannum.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.6 hpeyerl * 2. Redistributions in binary form must reproduce the above copyright
16 1.6 hpeyerl * notice, this list of conditions and the following disclaimer in the
17 1.6 hpeyerl * documentation and/or other materials provided with the distribution.
18 1.1 cgd *
19 1.13 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.13 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.13 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.13 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.13 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.13 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.13 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.13 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.13 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.13 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.13 mycroft * POSSIBILITY OF SUCH DAMAGE.
30 1.1 cgd */
31 1.15 lukem
32 1.15 lukem #include <sys/cdefs.h>
33 1.25.6.1 skrll __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.25.6.1 2015/06/06 14:40:22 skrll Exp $");
34 1.18 christos
35 1.25.6.1 skrll #ifdef _KERNEL_OPT
36 1.18 christos #include "opt_sysv.h"
37 1.25.6.1 skrll #endif
38 1.25.6.1 skrll
39 1.25.6.1 skrll #include <sys/syscall.h>
40 1.25.6.1 skrll #include <sys/syscallargs.h>
41 1.25.6.1 skrll #include <sys/syscallvar.h>
42 1.2 mycroft #include <sys/param.h>
43 1.2 mycroft #include <sys/kernel.h>
44 1.2 mycroft #include <sys/proc.h>
45 1.2 mycroft #include <sys/ipc.h>
46 1.18 christos #ifdef SYSVMSG
47 1.18 christos #include <sys/msg.h>
48 1.18 christos #endif
49 1.18 christos #ifdef SYSVSEM
50 1.18 christos #include <sys/sem.h>
51 1.18 christos #endif
52 1.18 christos #ifdef SYSVSHM
53 1.18 christos #include <sys/shm.h>
54 1.18 christos #endif
55 1.4 hpeyerl #include <sys/systm.h>
56 1.23 rmind #include <sys/kmem.h>
57 1.25.6.1 skrll #include <sys/module.h>
58 1.10 mycroft #include <sys/mount.h>
59 1.10 mycroft #include <sys/vnode.h>
60 1.12 mycroft #include <sys/stat.h>
61 1.18 christos #include <sys/sysctl.h>
62 1.17 elad #include <sys/kauth.h>
63 1.1 cgd
64 1.25.6.1 skrll static int (*kern_sysvipc50_sysctl_p)(SYSCTLFN_ARGS);
65 1.25.6.1 skrll
66 1.25.6.1 skrll /*
67 1.25.6.1 skrll * Values in support of System V compatible shared memory. XXX
68 1.25.6.1 skrll * (originally located in sys/conf/param.c)
69 1.25.6.1 skrll */
70 1.25.6.1 skrll #ifdef SYSVSHM
71 1.25.6.1 skrll #if !defined(SHMMAX) && defined(SHMMAXPGS)
72 1.25.6.1 skrll #define SHMMAX SHMMAXPGS /* shminit() performs a `*= PAGE_SIZE' */
73 1.25.6.1 skrll #elif !defined(SHMMAX)
74 1.25.6.1 skrll #define SHMMAX 0
75 1.25.6.1 skrll #endif
76 1.25.6.1 skrll #ifndef SHMMIN
77 1.25.6.1 skrll #define SHMMIN 1
78 1.25.6.1 skrll #endif
79 1.25.6.1 skrll #ifndef SHMMNI
80 1.25.6.1 skrll #define SHMMNI 128 /* <64k, see IPCID_TO_IX in ipc.h */
81 1.25.6.1 skrll #endif
82 1.25.6.1 skrll #ifndef SHMSEG
83 1.25.6.1 skrll #define SHMSEG 128
84 1.25.6.1 skrll #endif
85 1.25.6.1 skrll
86 1.25.6.1 skrll struct shminfo shminfo = {
87 1.25.6.1 skrll SHMMAX,
88 1.25.6.1 skrll SHMMIN,
89 1.25.6.1 skrll SHMMNI,
90 1.25.6.1 skrll SHMSEG,
91 1.25.6.1 skrll 0
92 1.25.6.1 skrll };
93 1.25.6.1 skrll #endif
94 1.25.6.1 skrll
95 1.25.6.1 skrll /*
96 1.25.6.1 skrll * Values in support of System V compatible semaphores.
97 1.25.6.1 skrll */
98 1.25.6.1 skrll #ifdef SYSVSEM
99 1.25.6.1 skrll struct seminfo seminfo = {
100 1.25.6.1 skrll SEMMAP, /* # of entries in semaphore map */
101 1.25.6.1 skrll SEMMNI, /* # of semaphore identifiers */
102 1.25.6.1 skrll SEMMNS, /* # of semaphores in system */
103 1.25.6.1 skrll SEMMNU, /* # of undo structures in system */
104 1.25.6.1 skrll SEMMSL, /* max # of semaphores per id */
105 1.25.6.1 skrll SEMOPM, /* max # of operations per semop call */
106 1.25.6.1 skrll SEMUME, /* max # of undo entries per process */
107 1.25.6.1 skrll SEMUSZ, /* size in bytes of undo structure */
108 1.25.6.1 skrll SEMVMX, /* semaphore maximum value */
109 1.25.6.1 skrll SEMAEM /* adjust on exit max value */
110 1.25.6.1 skrll };
111 1.25.6.1 skrll #endif
112 1.25.6.1 skrll
113 1.25.6.1 skrll /*
114 1.25.6.1 skrll * Values in support of System V compatible messages.
115 1.25.6.1 skrll */
116 1.25.6.1 skrll #ifdef SYSVMSG
117 1.25.6.1 skrll struct msginfo msginfo = {
118 1.25.6.1 skrll MSGMAX, /* max chars in a message */
119 1.25.6.1 skrll MSGMNI, /* # of message queue identifiers */
120 1.25.6.1 skrll MSGMNB, /* max chars in a queue */
121 1.25.6.1 skrll MSGTQL, /* max messages in system */
122 1.25.6.1 skrll MSGSSZ, /* size of a message segment */
123 1.25.6.1 skrll /* (must be small power of 2 greater than 4) */
124 1.25.6.1 skrll MSGSEG /* number of message segments */
125 1.25.6.1 skrll };
126 1.25.6.1 skrll #endif
127 1.25.6.1 skrll
128 1.25.6.1 skrll MODULE(MODULE_CLASS_EXEC, sysv_ipc, NULL);
129 1.25.6.1 skrll
130 1.25.6.1 skrll #ifdef _MODULE
131 1.25.6.1 skrll SYSCTL_SETUP_PROTO(sysctl_ipc_setup);
132 1.25.6.1 skrll SYSCTL_SETUP_PROTO(sysctl_ipc_shm_setup);
133 1.25.6.1 skrll SYSCTL_SETUP_PROTO(sysctl_ipc_sem_setup);
134 1.25.6.1 skrll SYSCTL_SETUP_PROTO(sysctl_ipc_msg_setup);
135 1.25.6.1 skrll
136 1.25.6.1 skrll static struct sysctllog *sysctl_sysvipc_clog = NULL;
137 1.25.6.1 skrll #endif
138 1.25.6.1 skrll
139 1.25.6.1 skrll static const struct syscall_package sysvipc_syscalls[] = {
140 1.25.6.1 skrll { SYS___shmctl50, 0, (sy_call_t *)sys___shmctl50 },
141 1.25.6.1 skrll { SYS_shmat, 0, (sy_call_t *)sys_shmat },
142 1.25.6.1 skrll { SYS_shmdt, 0, (sy_call_t *)sys_shmdt },
143 1.25.6.1 skrll { SYS_shmget, 0, (sy_call_t *)sys_shmget },
144 1.25.6.1 skrll { SYS_____semctl50, 0, (sy_call_t *)sys_____semctl50 },
145 1.25.6.1 skrll { SYS_semget, 0, (sy_call_t *)sys_semget },
146 1.25.6.1 skrll { SYS_semop, 0, (sy_call_t *)sys_semop },
147 1.25.6.1 skrll { SYS_semconfig, 0, (sy_call_t *)sys_semconfig },
148 1.25.6.1 skrll { SYS___msgctl50, 0, (sy_call_t *)sys___msgctl50 },
149 1.25.6.1 skrll { SYS_msgget, 0, (sy_call_t *)sys_msgget },
150 1.25.6.1 skrll { SYS_msgsnd, 0, (sy_call_t *)sys_msgsnd },
151 1.25.6.1 skrll { SYS_msgrcv, 0, (sy_call_t *)sys_msgrcv },
152 1.25.6.1 skrll { 0, 0, NULL }
153 1.25.6.1 skrll };
154 1.25.6.1 skrll
155 1.25.6.1 skrll static int
156 1.25.6.1 skrll sysv_ipc_modcmd(modcmd_t cmd, void *arg)
157 1.25.6.1 skrll {
158 1.25.6.1 skrll int error = 0;
159 1.25.6.1 skrll
160 1.25.6.1 skrll switch (cmd) {
161 1.25.6.1 skrll case MODULE_CMD_INIT:
162 1.25.6.1 skrll /* Link the system calls */
163 1.25.6.1 skrll error = syscall_establish(NULL, sysvipc_syscalls);
164 1.25.6.1 skrll
165 1.25.6.1 skrll /* Initialize all sysctl sub-trees */
166 1.25.6.1 skrll #ifdef _MODULE
167 1.25.6.1 skrll sysctl_ipc_setup(&sysctl_sysvipc_clog);
168 1.25.6.1 skrll #ifdef SYSVMSG
169 1.25.6.1 skrll sysctl_ipc_msg_setup(&sysctl_sysvipc_clog);
170 1.25.6.1 skrll #endif
171 1.25.6.1 skrll #ifdef SYSVSHM
172 1.25.6.1 skrll sysctl_ipc_shm_setup(&sysctl_sysvipc_clog);
173 1.25.6.1 skrll #endif
174 1.25.6.1 skrll #ifdef SYSVSEM
175 1.25.6.1 skrll sysctl_ipc_sem_setup(&sysctl_sysvipc_clog);
176 1.25.6.1 skrll #endif
177 1.25.6.1 skrll /* Assume no compat sysctl routine for now */
178 1.25.6.1 skrll kern_sysvipc50_sysctl_p = NULL;
179 1.25.6.1 skrll
180 1.25.6.1 skrll /* Initialize each sub-component */
181 1.25.6.1 skrll #ifdef SYSVSHM
182 1.25.6.1 skrll shminit();
183 1.25.6.1 skrll #endif
184 1.25.6.1 skrll #ifdef SYSVSEM
185 1.25.6.1 skrll seminit();
186 1.25.6.1 skrll #endif
187 1.25.6.1 skrll #ifdef SYSVMSG
188 1.25.6.1 skrll msginit();
189 1.25.6.1 skrll #endif
190 1.25.6.1 skrll #endif /* _MODULE */
191 1.25.6.1 skrll break;
192 1.25.6.1 skrll case MODULE_CMD_FINI:
193 1.25.6.1 skrll /*
194 1.25.6.1 skrll * Make sure no subcomponents are active. Each one
195 1.25.6.1 skrll * tells us if it is busy, and if it was _not_ busy,
196 1.25.6.1 skrll * we assume it has already done its own clean-up.
197 1.25.6.1 skrll * So we might need to re-init any components that
198 1.25.6.1 skrll * are successfully fini'd if we find one that is
199 1.25.6.1 skrll * still busy.
200 1.25.6.1 skrll */
201 1.25.6.1 skrll #ifdef SYSVSHM
202 1.25.6.1 skrll if (shmfini()) {
203 1.25.6.1 skrll return EBUSY;
204 1.25.6.1 skrll }
205 1.25.6.1 skrll #endif
206 1.25.6.1 skrll #ifdef SYSVSEM
207 1.25.6.1 skrll if (semfini()) {
208 1.25.6.1 skrll #ifdef SYSVSHM
209 1.25.6.1 skrll shminit();
210 1.25.6.1 skrll #endif
211 1.25.6.1 skrll return EBUSY;
212 1.25.6.1 skrll }
213 1.25.6.1 skrll #endif
214 1.25.6.1 skrll #ifdef SYSVMSG
215 1.25.6.1 skrll if (msgfini()) {
216 1.25.6.1 skrll #ifdef SYSVSEM
217 1.25.6.1 skrll seminit();
218 1.25.6.1 skrll #endif
219 1.25.6.1 skrll #ifdef SYSVSHM
220 1.25.6.1 skrll shminit();
221 1.22 christos #endif
222 1.25.6.1 skrll return EBUSY;
223 1.25.6.1 skrll }
224 1.25.6.1 skrll #endif
225 1.25.6.1 skrll
226 1.25.6.1 skrll /* Unlink the system calls. */
227 1.25.6.1 skrll error = syscall_disestablish(NULL, sysvipc_syscalls);
228 1.25.6.1 skrll if (error)
229 1.25.6.1 skrll return error;
230 1.25.6.1 skrll
231 1.25.6.1 skrll #ifdef _MODULE
232 1.25.6.1 skrll /* Remove the sysctl sub-trees */
233 1.25.6.1 skrll sysctl_teardown(&sysctl_sysvipc_clog);
234 1.25.6.1 skrll #endif
235 1.25.6.1 skrll
236 1.25.6.1 skrll /* Remove the kauth listener */
237 1.25.6.1 skrll sysvipcfini();
238 1.25.6.1 skrll break;
239 1.25.6.1 skrll default:
240 1.25.6.1 skrll return ENOTTY;
241 1.25.6.1 skrll }
242 1.25.6.1 skrll return error;
243 1.25.6.1 skrll }
244 1.25.6.1 skrll
245 1.25.6.1 skrll void
246 1.25.6.1 skrll sysvipc50_set_compat_sysctl(int (*compat_sysctl)(SYSCTLFN_PROTO))
247 1.25.6.1 skrll {
248 1.25.6.1 skrll
249 1.25.6.1 skrll kern_sysvipc50_sysctl_p = compat_sysctl;
250 1.25.6.1 skrll }
251 1.22 christos
252 1.24 elad static kauth_listener_t sysvipc_listener = NULL;
253 1.1 cgd
254 1.24 elad static int
255 1.24 elad sysvipc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
256 1.24 elad void *arg0, void *arg1, void *arg2, void *arg3)
257 1.1 cgd {
258 1.12 mycroft mode_t mask;
259 1.17 elad int ismember = 0;
260 1.24 elad struct ipc_perm *perm;
261 1.24 elad int mode;
262 1.24 elad enum kauth_system_req req;
263 1.24 elad
264 1.24 elad req = (enum kauth_system_req)arg0;
265 1.24 elad
266 1.24 elad if (!(action == KAUTH_SYSTEM_SYSVIPC &&
267 1.24 elad req == KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS))
268 1.24 elad return KAUTH_RESULT_DEFER;
269 1.12 mycroft
270 1.24 elad perm = arg1;
271 1.24 elad mode = (int)(uintptr_t)arg2;
272 1.1 cgd
273 1.9 mycroft if (mode == IPC_M) {
274 1.17 elad if (kauth_cred_geteuid(cred) == perm->uid ||
275 1.17 elad kauth_cred_geteuid(cred) == perm->cuid)
276 1.24 elad return (KAUTH_RESULT_ALLOW);
277 1.24 elad return (KAUTH_RESULT_DEFER); /* EPERM */
278 1.1 cgd }
279 1.4 hpeyerl
280 1.12 mycroft mask = 0;
281 1.12 mycroft
282 1.17 elad if (kauth_cred_geteuid(cred) == perm->uid ||
283 1.17 elad kauth_cred_geteuid(cred) == perm->cuid) {
284 1.12 mycroft if (mode & IPC_R)
285 1.12 mycroft mask |= S_IRUSR;
286 1.12 mycroft if (mode & IPC_W)
287 1.12 mycroft mask |= S_IWUSR;
288 1.24 elad return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
289 1.12 mycroft }
290 1.12 mycroft
291 1.17 elad if (kauth_cred_getegid(cred) == perm->gid ||
292 1.17 elad (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
293 1.17 elad kauth_cred_getegid(cred) == perm->cgid ||
294 1.17 elad (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
295 1.12 mycroft if (mode & IPC_R)
296 1.12 mycroft mask |= S_IRGRP;
297 1.12 mycroft if (mode & IPC_W)
298 1.12 mycroft mask |= S_IWGRP;
299 1.24 elad return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
300 1.12 mycroft }
301 1.12 mycroft
302 1.12 mycroft if (mode & IPC_R)
303 1.12 mycroft mask |= S_IROTH;
304 1.12 mycroft if (mode & IPC_W)
305 1.12 mycroft mask |= S_IWOTH;
306 1.24 elad return ((perm->mode & mask) == mask ? KAUTH_RESULT_ALLOW : KAUTH_RESULT_DEFER /* EACCES */);
307 1.24 elad }
308 1.24 elad
309 1.24 elad /*
310 1.24 elad * Check for ipc permission
311 1.24 elad */
312 1.24 elad
313 1.24 elad int
314 1.24 elad ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
315 1.24 elad {
316 1.24 elad int error;
317 1.24 elad
318 1.24 elad error = kauth_authorize_system(cred, KAUTH_SYSTEM_SYSVIPC,
319 1.24 elad KAUTH_REQ_SYSTEM_SYSVIPC_BYPASS, perm, KAUTH_ARG(mode), NULL);
320 1.24 elad if (error == 0)
321 1.24 elad return (0);
322 1.24 elad
323 1.24 elad /* Adjust EPERM and EACCES errors until there's a better way to do this. */
324 1.24 elad if (mode != IPC_M)
325 1.24 elad error = EACCES;
326 1.24 elad
327 1.24 elad return error;
328 1.24 elad }
329 1.24 elad
330 1.24 elad void
331 1.25.6.1 skrll sysvipcfini(void)
332 1.25.6.1 skrll {
333 1.25.6.1 skrll
334 1.25.6.1 skrll KASSERT(sysvipc_listener != NULL);
335 1.25.6.1 skrll kauth_unlisten_scope(sysvipc_listener);
336 1.25.6.1 skrll }
337 1.25.6.1 skrll
338 1.25.6.1 skrll void
339 1.24 elad sysvipcinit(void)
340 1.24 elad {
341 1.24 elad
342 1.24 elad if (sysvipc_listener != NULL)
343 1.24 elad return;
344 1.24 elad
345 1.24 elad sysvipc_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
346 1.24 elad sysvipc_listener_cb, NULL);
347 1.1 cgd }
348 1.18 christos
349 1.18 christos static int
350 1.18 christos sysctl_kern_sysvipc(SYSCTLFN_ARGS)
351 1.18 christos {
352 1.18 christos void *where = oldp;
353 1.23 rmind size_t sz, *sizep = oldlenp;
354 1.18 christos #ifdef SYSVMSG
355 1.18 christos struct msg_sysctl_info *msgsi = NULL;
356 1.18 christos #endif
357 1.18 christos #ifdef SYSVSEM
358 1.18 christos struct sem_sysctl_info *semsi = NULL;
359 1.18 christos #endif
360 1.18 christos #ifdef SYSVSHM
361 1.18 christos struct shm_sysctl_info *shmsi = NULL;
362 1.18 christos #endif
363 1.18 christos size_t infosize, dssize, tsize, buflen;
364 1.18 christos void *bf = NULL;
365 1.18 christos char *start;
366 1.18 christos int32_t nds;
367 1.18 christos int i, error, ret;
368 1.18 christos
369 1.25.6.1 skrll /*
370 1.25.6.1 skrll * If compat_sysv module has loaded the compat sysctl, call it. If
371 1.25.6.1 skrll * it handles the request completely (either success or error), just
372 1.25.6.1 skrll * return. Otherwise fallthrough to the non-compat_sysv sysctl code.
373 1.25.6.1 skrll */
374 1.25.6.1 skrll if (kern_sysvipc50_sysctl_p != NULL) {
375 1.25.6.1 skrll error = (*kern_sysvipc50_sysctl_p)(SYSCTLFN_CALL(rnode));
376 1.25.6.1 skrll if (error != EPASSTHROUGH)
377 1.25.6.1 skrll return error;
378 1.22 christos }
379 1.25.6.1 skrll
380 1.18 christos if (namelen != 1)
381 1.18 christos return EINVAL;
382 1.18 christos
383 1.18 christos start = where;
384 1.18 christos buflen = *sizep;
385 1.18 christos
386 1.18 christos switch (*name) {
387 1.18 christos case KERN_SYSVIPC_MSG_INFO:
388 1.18 christos #ifdef SYSVMSG
389 1.18 christos infosize = sizeof(msgsi->msginfo);
390 1.18 christos nds = msginfo.msgmni;
391 1.18 christos dssize = sizeof(msgsi->msgids[0]);
392 1.18 christos break;
393 1.18 christos #else
394 1.18 christos return EINVAL;
395 1.18 christos #endif
396 1.18 christos case KERN_SYSVIPC_SEM_INFO:
397 1.18 christos #ifdef SYSVSEM
398 1.18 christos infosize = sizeof(semsi->seminfo);
399 1.18 christos nds = seminfo.semmni;
400 1.18 christos dssize = sizeof(semsi->semids[0]);
401 1.18 christos break;
402 1.18 christos #else
403 1.18 christos return EINVAL;
404 1.18 christos #endif
405 1.18 christos case KERN_SYSVIPC_SHM_INFO:
406 1.18 christos #ifdef SYSVSHM
407 1.18 christos infosize = sizeof(shmsi->shminfo);
408 1.18 christos nds = shminfo.shmmni;
409 1.18 christos dssize = sizeof(shmsi->shmids[0]);
410 1.18 christos break;
411 1.18 christos #else
412 1.18 christos return EINVAL;
413 1.18 christos #endif
414 1.18 christos default:
415 1.18 christos return EINVAL;
416 1.18 christos }
417 1.18 christos /*
418 1.18 christos * Round infosize to 64 bit boundary if requesting more than just
419 1.18 christos * the info structure or getting the total data size.
420 1.18 christos */
421 1.18 christos if (where == NULL || *sizep > infosize)
422 1.18 christos infosize = roundup(infosize, sizeof(quad_t));
423 1.18 christos tsize = infosize + nds * dssize;
424 1.18 christos
425 1.18 christos /* Return just the total size required. */
426 1.18 christos if (where == NULL) {
427 1.18 christos *sizep = tsize;
428 1.18 christos return 0;
429 1.18 christos }
430 1.18 christos
431 1.18 christos /* Not enough room for even the info struct. */
432 1.18 christos if (buflen < infosize) {
433 1.18 christos *sizep = 0;
434 1.18 christos return ENOMEM;
435 1.18 christos }
436 1.23 rmind sz = min(tsize, buflen);
437 1.23 rmind bf = kmem_zalloc(sz, KM_SLEEP);
438 1.18 christos
439 1.18 christos switch (*name) {
440 1.18 christos #ifdef SYSVMSG
441 1.18 christos case KERN_SYSVIPC_MSG_INFO:
442 1.18 christos msgsi = (struct msg_sysctl_info *)bf;
443 1.18 christos msgsi->msginfo = msginfo;
444 1.18 christos break;
445 1.18 christos #endif
446 1.18 christos #ifdef SYSVSEM
447 1.18 christos case KERN_SYSVIPC_SEM_INFO:
448 1.18 christos semsi = (struct sem_sysctl_info *)bf;
449 1.18 christos semsi->seminfo = seminfo;
450 1.18 christos break;
451 1.18 christos #endif
452 1.18 christos #ifdef SYSVSHM
453 1.18 christos case KERN_SYSVIPC_SHM_INFO:
454 1.18 christos shmsi = (struct shm_sysctl_info *)bf;
455 1.18 christos shmsi->shminfo = shminfo;
456 1.18 christos break;
457 1.18 christos #endif
458 1.18 christos }
459 1.18 christos buflen -= infosize;
460 1.18 christos
461 1.18 christos ret = 0;
462 1.18 christos if (buflen > 0) {
463 1.18 christos /* Fill in the IPC data structures. */
464 1.18 christos for (i = 0; i < nds; i++) {
465 1.18 christos if (buflen < dssize) {
466 1.18 christos ret = ENOMEM;
467 1.18 christos break;
468 1.18 christos }
469 1.18 christos switch (*name) {
470 1.18 christos #ifdef SYSVMSG
471 1.18 christos case KERN_SYSVIPC_MSG_INFO:
472 1.20 ad mutex_enter(&msgmutex);
473 1.22 christos SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
474 1.20 ad mutex_exit(&msgmutex);
475 1.18 christos break;
476 1.18 christos #endif
477 1.18 christos #ifdef SYSVSEM
478 1.18 christos case KERN_SYSVIPC_SEM_INFO:
479 1.22 christos SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
480 1.18 christos break;
481 1.18 christos #endif
482 1.18 christos #ifdef SYSVSHM
483 1.18 christos case KERN_SYSVIPC_SHM_INFO:
484 1.22 christos SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
485 1.18 christos break;
486 1.18 christos #endif
487 1.18 christos }
488 1.18 christos buflen -= dssize;
489 1.18 christos }
490 1.18 christos }
491 1.18 christos *sizep -= buflen;
492 1.18 christos error = copyout(bf, start, *sizep);
493 1.18 christos /* If copyout succeeded, use return code set earlier. */
494 1.18 christos if (error == 0)
495 1.18 christos error = ret;
496 1.18 christos if (bf)
497 1.23 rmind kmem_free(bf, sz);
498 1.18 christos return error;
499 1.18 christos }
500 1.18 christos
501 1.18 christos SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
502 1.18 christos {
503 1.18 christos
504 1.18 christos sysctl_createv(clog, 0, NULL, NULL,
505 1.18 christos CTLFLAG_PERMANENT,
506 1.18 christos CTLTYPE_NODE, "ipc",
507 1.18 christos SYSCTL_DESCR("SysV IPC options"),
508 1.18 christos NULL, 0, NULL, 0,
509 1.18 christos CTL_KERN, KERN_SYSVIPC, CTL_EOL);
510 1.18 christos
511 1.18 christos sysctl_createv(clog, 0, NULL, NULL,
512 1.18 christos CTLFLAG_PERMANENT,
513 1.18 christos CTLTYPE_STRUCT, "sysvipc_info",
514 1.18 christos SYSCTL_DESCR("System V style IPC information"),
515 1.18 christos sysctl_kern_sysvipc, 0, NULL, 0,
516 1.18 christos CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);
517 1.18 christos }
518