sysv_ipc_50.c revision 1.2.6.2 1 1.2.6.2 yamt /* $NetBSD: sysv_ipc_50.c,v 1.2.6.2 2009/05/04 08:12:17 yamt Exp $ */
2 1.2.6.2 yamt
3 1.2.6.2 yamt /*-
4 1.2.6.2 yamt * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
5 1.2.6.2 yamt * All rights reserved.
6 1.2.6.2 yamt *
7 1.2.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.2.6.2 yamt * by Charles M. Hannum.
9 1.2.6.2 yamt *
10 1.2.6.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.2.6.2 yamt * modification, are permitted provided that the following conditions
12 1.2.6.2 yamt * are met:
13 1.2.6.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.2.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.2.6.2 yamt * documentation and/or other materials provided with the distribution.
18 1.2.6.2 yamt *
19 1.2.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.2.6.2 yamt */
31 1.2.6.2 yamt
32 1.2.6.2 yamt #include <sys/cdefs.h>
33 1.2.6.2 yamt __KERNEL_RCSID(0, "$NetBSD: sysv_ipc_50.c,v 1.2.6.2 2009/05/04 08:12:17 yamt Exp $");
34 1.2.6.2 yamt
35 1.2.6.2 yamt #ifdef _KERNEL_OPT
36 1.2.6.2 yamt #include "opt_sysv.h"
37 1.2.6.2 yamt #include "opt_compat_netbsd.h"
38 1.2.6.2 yamt #endif
39 1.2.6.2 yamt
40 1.2.6.2 yamt #include <sys/param.h>
41 1.2.6.2 yamt #include <sys/kernel.h>
42 1.2.6.2 yamt #include <sys/proc.h>
43 1.2.6.2 yamt #include <sys/ipc.h>
44 1.2.6.2 yamt #ifdef SYSVMSG
45 1.2.6.2 yamt #include <sys/msg.h>
46 1.2.6.2 yamt #endif
47 1.2.6.2 yamt #ifdef SYSVSEM
48 1.2.6.2 yamt #include <sys/sem.h>
49 1.2.6.2 yamt #endif
50 1.2.6.2 yamt #ifdef SYSVSHM
51 1.2.6.2 yamt #include <sys/shm.h>
52 1.2.6.2 yamt #endif
53 1.2.6.2 yamt #include <sys/systm.h>
54 1.2.6.2 yamt #include <sys/malloc.h>
55 1.2.6.2 yamt #include <sys/mount.h>
56 1.2.6.2 yamt #include <sys/vnode.h>
57 1.2.6.2 yamt #include <sys/stat.h>
58 1.2.6.2 yamt #include <sys/sysctl.h>
59 1.2.6.2 yamt #include <sys/kauth.h>
60 1.2.6.2 yamt
61 1.2.6.2 yamt #ifdef COMPAT_50
62 1.2.6.2 yamt #include <compat/sys/ipc.h>
63 1.2.6.2 yamt #ifdef SYSVMSG
64 1.2.6.2 yamt #include <compat/sys/msg.h>
65 1.2.6.2 yamt #endif
66 1.2.6.2 yamt #ifdef SYSVSEM
67 1.2.6.2 yamt #include <compat/sys/sem.h>
68 1.2.6.2 yamt #endif
69 1.2.6.2 yamt #ifdef SYSVSHM
70 1.2.6.2 yamt #include <compat/sys/shm.h>
71 1.2.6.2 yamt #endif
72 1.2.6.2 yamt
73 1.2.6.2 yamt /*
74 1.2.6.2 yamt * Check for ipc permission
75 1.2.6.2 yamt */
76 1.2.6.2 yamt
77 1.2.6.2 yamt int
78 1.2.6.2 yamt sysctl_kern_sysvipc50(SYSCTLFN_ARGS)
79 1.2.6.2 yamt {
80 1.2.6.2 yamt void *where = oldp;
81 1.2.6.2 yamt size_t *sizep = oldlenp;
82 1.2.6.2 yamt #ifdef SYSVMSG
83 1.2.6.2 yamt struct msg_sysctl_info50 *msgsi = NULL;
84 1.2.6.2 yamt #endif
85 1.2.6.2 yamt #ifdef SYSVSEM
86 1.2.6.2 yamt struct sem_sysctl_info50 *semsi = NULL;
87 1.2.6.2 yamt #endif
88 1.2.6.2 yamt #ifdef SYSVSHM
89 1.2.6.2 yamt struct shm_sysctl_info50 *shmsi = NULL;
90 1.2.6.2 yamt #endif
91 1.2.6.2 yamt size_t infosize, dssize, tsize, buflen;
92 1.2.6.2 yamt void *bf = NULL;
93 1.2.6.2 yamt char *start;
94 1.2.6.2 yamt int32_t nds;
95 1.2.6.2 yamt int i, error, ret;
96 1.2.6.2 yamt
97 1.2.6.2 yamt if (namelen != 1)
98 1.2.6.2 yamt return EINVAL;
99 1.2.6.2 yamt
100 1.2.6.2 yamt start = where;
101 1.2.6.2 yamt buflen = *sizep;
102 1.2.6.2 yamt
103 1.2.6.2 yamt switch (*name) {
104 1.2.6.2 yamt case KERN_SYSVIPC_OMSG_INFO:
105 1.2.6.2 yamt #ifdef SYSVMSG
106 1.2.6.2 yamt infosize = sizeof(msgsi->msginfo);
107 1.2.6.2 yamt nds = msginfo.msgmni;
108 1.2.6.2 yamt dssize = sizeof(msgsi->msgids[0]);
109 1.2.6.2 yamt break;
110 1.2.6.2 yamt #else
111 1.2.6.2 yamt return EINVAL;
112 1.2.6.2 yamt #endif
113 1.2.6.2 yamt case KERN_SYSVIPC_OSEM_INFO:
114 1.2.6.2 yamt #ifdef SYSVSEM
115 1.2.6.2 yamt infosize = sizeof(semsi->seminfo);
116 1.2.6.2 yamt nds = seminfo.semmni;
117 1.2.6.2 yamt dssize = sizeof(semsi->semids[0]);
118 1.2.6.2 yamt break;
119 1.2.6.2 yamt #else
120 1.2.6.2 yamt return EINVAL;
121 1.2.6.2 yamt #endif
122 1.2.6.2 yamt case KERN_SYSVIPC_OSHM_INFO:
123 1.2.6.2 yamt #ifdef SYSVSHM
124 1.2.6.2 yamt infosize = sizeof(shmsi->shminfo);
125 1.2.6.2 yamt nds = shminfo.shmmni;
126 1.2.6.2 yamt dssize = sizeof(shmsi->shmids[0]);
127 1.2.6.2 yamt break;
128 1.2.6.2 yamt #else
129 1.2.6.2 yamt return EINVAL;
130 1.2.6.2 yamt #endif
131 1.2.6.2 yamt default:
132 1.2.6.2 yamt return EPASSTHROUGH;
133 1.2.6.2 yamt }
134 1.2.6.2 yamt /*
135 1.2.6.2 yamt * Round infosize to 64 bit boundary if requesting more than just
136 1.2.6.2 yamt * the info structure or getting the total data size.
137 1.2.6.2 yamt */
138 1.2.6.2 yamt if (where == NULL || *sizep > infosize)
139 1.2.6.2 yamt infosize = roundup(infosize, sizeof(quad_t));
140 1.2.6.2 yamt tsize = infosize + nds * dssize;
141 1.2.6.2 yamt
142 1.2.6.2 yamt /* Return just the total size required. */
143 1.2.6.2 yamt if (where == NULL) {
144 1.2.6.2 yamt *sizep = tsize;
145 1.2.6.2 yamt return 0;
146 1.2.6.2 yamt }
147 1.2.6.2 yamt
148 1.2.6.2 yamt /* Not enough room for even the info struct. */
149 1.2.6.2 yamt if (buflen < infosize) {
150 1.2.6.2 yamt *sizep = 0;
151 1.2.6.2 yamt return ENOMEM;
152 1.2.6.2 yamt }
153 1.2.6.2 yamt bf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK | M_ZERO);
154 1.2.6.2 yamt
155 1.2.6.2 yamt switch (*name) {
156 1.2.6.2 yamt #ifdef SYSVMSG
157 1.2.6.2 yamt case KERN_SYSVIPC_OMSG_INFO:
158 1.2.6.2 yamt msgsi = (struct msg_sysctl_info50 *)bf;
159 1.2.6.2 yamt msgsi->msginfo = msginfo;
160 1.2.6.2 yamt break;
161 1.2.6.2 yamt #endif
162 1.2.6.2 yamt #ifdef SYSVSEM
163 1.2.6.2 yamt case KERN_SYSVIPC_OSEM_INFO:
164 1.2.6.2 yamt semsi = (struct sem_sysctl_info50 *)bf;
165 1.2.6.2 yamt semsi->seminfo = seminfo;
166 1.2.6.2 yamt break;
167 1.2.6.2 yamt #endif
168 1.2.6.2 yamt #ifdef SYSVSHM
169 1.2.6.2 yamt case KERN_SYSVIPC_OSHM_INFO:
170 1.2.6.2 yamt shmsi = (struct shm_sysctl_info50 *)bf;
171 1.2.6.2 yamt shmsi->shminfo = shminfo;
172 1.2.6.2 yamt break;
173 1.2.6.2 yamt #endif
174 1.2.6.2 yamt }
175 1.2.6.2 yamt buflen -= infosize;
176 1.2.6.2 yamt
177 1.2.6.2 yamt ret = 0;
178 1.2.6.2 yamt if (buflen > 0) {
179 1.2.6.2 yamt /* Fill in the IPC data structures. */
180 1.2.6.2 yamt for (i = 0; i < nds; i++) {
181 1.2.6.2 yamt if (buflen < dssize) {
182 1.2.6.2 yamt ret = ENOMEM;
183 1.2.6.2 yamt break;
184 1.2.6.2 yamt }
185 1.2.6.2 yamt switch (*name) {
186 1.2.6.2 yamt #ifdef SYSVMSG
187 1.2.6.2 yamt case KERN_SYSVIPC_OMSG_INFO:
188 1.2.6.2 yamt mutex_enter(&msgmutex);
189 1.2.6.2 yamt SYSCTL_FILL_MSG(msqs[i].msq_u, msgsi->msgids[i]);
190 1.2.6.2 yamt mutex_exit(&msgmutex);
191 1.2.6.2 yamt break;
192 1.2.6.2 yamt #endif
193 1.2.6.2 yamt #ifdef SYSVSEM
194 1.2.6.2 yamt case KERN_SYSVIPC_OSEM_INFO:
195 1.2.6.2 yamt SYSCTL_FILL_SEM(sema[i], semsi->semids[i]);
196 1.2.6.2 yamt break;
197 1.2.6.2 yamt #endif
198 1.2.6.2 yamt #ifdef SYSVSHM
199 1.2.6.2 yamt case KERN_SYSVIPC_OSHM_INFO:
200 1.2.6.2 yamt SYSCTL_FILL_SHM(shmsegs[i], shmsi->shmids[i]);
201 1.2.6.2 yamt break;
202 1.2.6.2 yamt #endif
203 1.2.6.2 yamt }
204 1.2.6.2 yamt buflen -= dssize;
205 1.2.6.2 yamt }
206 1.2.6.2 yamt }
207 1.2.6.2 yamt *sizep -= buflen;
208 1.2.6.2 yamt error = copyout(bf, start, *sizep);
209 1.2.6.2 yamt /* If copyout succeeded, use return code set earlier. */
210 1.2.6.2 yamt if (error == 0)
211 1.2.6.2 yamt error = ret;
212 1.2.6.2 yamt if (bf)
213 1.2.6.2 yamt free(bf, M_TEMP);
214 1.2.6.2 yamt return error;
215 1.2.6.2 yamt }
216 1.2.6.2 yamt #endif
217