No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / sysv_ipc.c
blob6315f354c635a26cea4d4fe877092d70c11f0548
1 /* $NetBSD: sysv_ipc.c,v 1.22 2009/01/19 19:39:41 christos Exp $ */
3 /*-
4 * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.22 2009/01/19 19:39:41 christos Exp $");
35 #include "opt_sysv.h"
36 #include "opt_compat_netbsd.h"
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/ipc.h>
41 #ifdef SYSVMSG
42 #include <sys/msg.h>
43 #endif
44 #ifdef SYSVSEM
45 #include <sys/sem.h>
46 #endif
47 #ifdef SYSVSHM
48 #include <sys/shm.h>
49 #endif
50 #include <sys/systm.h>
51 #include <sys/kmem.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/stat.h>
55 #include <sys/sysctl.h>
56 #include <sys/kauth.h>
58 #ifdef COMPAT_50
59 #include <compat/sys/ipc.h>
60 #endif
63 * Check for ipc permission
66 int
67 ipcperm(kauth_cred_t cred, struct ipc_perm *perm, int mode)
69 mode_t mask;
70 int ismember = 0;
72 if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0)
73 return (0);
75 if (mode == IPC_M) {
76 if (kauth_cred_geteuid(cred) == perm->uid ||
77 kauth_cred_geteuid(cred) == perm->cuid)
78 return (0);
79 return (EPERM);
82 mask = 0;
84 if (kauth_cred_geteuid(cred) == perm->uid ||
85 kauth_cred_geteuid(cred) == perm->cuid) {
86 if (mode & IPC_R)
87 mask |= S_IRUSR;
88 if (mode & IPC_W)
89 mask |= S_IWUSR;
90 return ((perm->mode & mask) == mask ? 0 : EACCES);
93 if (kauth_cred_getegid(cred) == perm->gid ||
94 (kauth_cred_ismember_gid(cred, perm->gid, &ismember) == 0 && ismember) ||
95 kauth_cred_getegid(cred) == perm->cgid ||
96 (kauth_cred_ismember_gid(cred, perm->cgid, &ismember) == 0 && ismember)) {
97 if (mode & IPC_R)
98 mask |= S_IRGRP;
99 if (mode & IPC_W)
100 mask |= S_IWGRP;
101 return ((perm->mode & mask) == mask ? 0 : EACCES);
104 if (mode & IPC_R)
105 mask |= S_IROTH;
106 if (mode & IPC_W)
107 mask |= S_IWOTH;
108 return ((perm->mode & mask) == mask ? 0 : EACCES);
111 static int
112 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
114 void *where = oldp;
115 size_t sz, *sizep = oldlenp;
116 #ifdef SYSVMSG
117 struct msg_sysctl_info *msgsi = NULL;
118 #endif
119 #ifdef SYSVSEM
120 struct sem_sysctl_info *semsi = NULL;
121 #endif
122 #ifdef SYSVSHM
123 struct shm_sysctl_info *shmsi = NULL;
124 #endif
125 size_t infosize, dssize, tsize, buflen;
126 void *bf = NULL;
127 char *start;
128 int32_t nds;
129 int i, error, ret;
131 #ifdef COMPAT_50
132 switch ((error = sysctl_kern_sysvipc50(SYSCTLFN_CALL(rnode)))) {
133 case 0:
134 return 0;
135 case EPASSTHROUGH:
136 break;
137 default:
138 return error;
140 #endif
141 if (namelen != 1)
142 return EINVAL;
144 start = where;
145 buflen = *sizep;
147 switch (*name) {
148 case KERN_SYSVIPC_MSG_INFO:
149 #ifdef SYSVMSG
150 infosize = sizeof(msgsi->msginfo);
151 nds = msginfo.msgmni;
152 dssize = sizeof(msgsi->msgids[0]);
153 break;
154 #else
155 return EINVAL;
156 #endif
157 case KERN_SYSVIPC_SEM_INFO:
158 #ifdef SYSVSEM
159 infosize = sizeof(semsi->seminfo);
160 nds = seminfo.semmni;
161 dssize = sizeof(semsi->semids[0]);
162 break;
163 #else
164 return EINVAL;
165 #endif
166 case KERN_SYSVIPC_SHM_INFO:
167 #ifdef SYSVSHM
168 infosize = sizeof(shmsi->shminfo);
169 nds = shminfo.shmmni;
170 dssize = sizeof(shmsi->shmids[0]);
171 break;
172 #else
173 return EINVAL;
174 #endif
175 default:
176 return EINVAL;
179 * Round infosize to 64 bit boundary if requesting more than just
180 * the info structure or getting the total data size.
182 if (where == NULL || *sizep > infosize)
183 infosize = roundup(infosize, sizeof(quad_t));
184 tsize = infosize + nds * dssize;
186 /* Return just the total size required. */
187 if (where == NULL) {
188 *sizep = tsize;
189 return 0;
192 /* Not enough room for even the info struct. */
193 if (buflen < infosize) {
194 *sizep = 0;
195 return ENOMEM;
197 sz = min(tsize, buflen);
198 bf = kmem_zalloc(sz, KM_SLEEP);
200 switch (*name) {
201 #ifdef SYSVMSG
202 case KERN_SYSVIPC_MSG_INFO:
203 msgsi = (struct msg_sysctl_info *)bf;
204 msgsi->msginfo = msginfo;
205 break;
206 #endif
207 #ifdef SYSVSEM
208 case KERN_SYSVIPC_SEM_INFO:
209 semsi = (struct sem_sysctl_info *)bf;
210 semsi->seminfo = seminfo;
211 break;
212 #endif
213 #ifdef SYSVSHM
214 case KERN_SYSVIPC_SHM_INFO:
215 shmsi = (struct shm_sysctl_info *)bf;
216 shmsi->shminfo = shminfo;
217 break;
218 #endif
220 buflen -= infosize;
222 ret = 0;
223 if (buflen > 0) {
224 /* Fill in the IPC data structures. */
225 for (i = 0; i < nds; i++) {
226 if (buflen < dssize) {
227 ret = ENOMEM;
228 break;
230 switch (*name) {
231 #ifdef SYSVMSG
232 case KERN_SYSVIPC_MSG_INFO:
233 mutex_enter(&msgmutex);
234 SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
235 mutex_exit(&msgmutex);
236 break;
237 #endif
238 #ifdef SYSVSEM
239 case KERN_SYSVIPC_SEM_INFO:
240 SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
241 break;
242 #endif
243 #ifdef SYSVSHM
244 case KERN_SYSVIPC_SHM_INFO:
245 SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
246 break;
247 #endif
249 buflen -= dssize;
252 *sizep -= buflen;
253 error = copyout(bf, start, *sizep);
254 /* If copyout succeeded, use return code set earlier. */
255 if (error == 0)
256 error = ret;
257 if (bf)
258 kmem_free(bf, sz);
259 return error;
262 SYSCTL_SETUP(sysctl_ipc_setup, "sysctl kern.ipc subtree setup")
264 sysctl_createv(clog, 0, NULL, NULL,
265 CTLFLAG_PERMANENT,
266 CTLTYPE_NODE, "kern", NULL,
267 NULL, 0, NULL, 0,
268 CTL_KERN, CTL_EOL);
270 sysctl_createv(clog, 0, NULL, NULL,
271 CTLFLAG_PERMANENT,
272 CTLTYPE_NODE, "ipc",
273 SYSCTL_DESCR("SysV IPC options"),
274 NULL, 0, NULL, 0,
275 CTL_KERN, KERN_SYSVIPC, CTL_EOL);
277 sysctl_createv(clog, 0, NULL, NULL,
278 CTLFLAG_PERMANENT,
279 CTLTYPE_STRUCT, "sysvipc_info",
280 SYSCTL_DESCR("System V style IPC information"),
281 sysctl_kern_sysvipc, 0, NULL, 0,
282 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO, CTL_EOL);