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