1 /*	$NetBSD: netbsd32_compat_14.c,v 1.26 2016/01/07 21:58:28 joerg Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 Eduardo E. Horvath
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_14.c,v 1.26 2016/01/07 21:58:28 joerg Exp $");
33 
34 #ifdef _KERNEL_OPT
35 #include "opt_sysv.h"
36 #include "opt_compat_netbsd.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/ipc.h>
41 #include <sys/systm.h>
42 #include <sys/signal.h>
43 #include <sys/proc.h>
44 #include <sys/mount.h>
45 #include <sys/msg.h>
46 #include <sys/sem.h>
47 #include <sys/shm.h>
48 
49 #include <sys/syscallargs.h>
50 #include <compat/netbsd32/netbsd32.h>
51 #include <compat/netbsd32/netbsd32_syscallargs.h>
52 #include <compat/sys/shm.h>
53 
54 #if defined(COMPAT_14)
55 
56 #if defined(SYSVMSG)
57 static inline void
netbsd32_ipc_perm14_to_native(struct netbsd32_ipc_perm14 * operm,struct ipc_perm * perm)58 netbsd32_ipc_perm14_to_native(struct netbsd32_ipc_perm14 *operm, struct ipc_perm *perm)
59 {
60 
61 #define	CVT(x)	perm->x = operm->x
62 	CVT(uid);
63 	CVT(gid);
64 	CVT(cuid);
65 	CVT(cgid);
66 	CVT(mode);
67 #undef CVT
68 }
69 
70 static inline void
native_to_netbsd32_ipc_perm14(struct ipc_perm * perm,struct netbsd32_ipc_perm14 * operm)71 native_to_netbsd32_ipc_perm14(struct ipc_perm *perm, struct netbsd32_ipc_perm14 *operm)
72 {
73 
74 #define	CVT(x)	operm->x = perm->x
75 	CVT(uid);
76 	CVT(gid);
77 	CVT(cuid);
78 	CVT(cgid);
79 	CVT(mode);
80 #undef CVT
81 
82 	/*
83 	 * Not part of the API, but some programs might look at it.
84 	 */
85 	operm->seq = perm->_seq;
86 	operm->key = (key_t)perm->_key;
87 }
88 
89 static inline void
netbsd32_msqid_ds14_to_native(struct netbsd32_msqid_ds14 * omsqbuf,struct msqid_ds * msqbuf)90 netbsd32_msqid_ds14_to_native(struct netbsd32_msqid_ds14 *omsqbuf, struct msqid_ds *msqbuf)
91 {
92 
93 	netbsd32_ipc_perm14_to_native(&omsqbuf->msg_perm, &msqbuf->msg_perm);
94 
95 #define	CVT(x)	msqbuf->x = omsqbuf->x
96 	CVT(msg_qnum);
97 	CVT(msg_qbytes);
98 	CVT(msg_lspid);
99 	CVT(msg_lrpid);
100 	CVT(msg_stime);
101 	CVT(msg_rtime);
102 	CVT(msg_ctime);
103 #undef CVT
104 }
105 
106 static inline void
native_to_netbsd32_msqid_ds14(struct msqid_ds * msqbuf,struct netbsd32_msqid_ds14 * omsqbuf)107 native_to_netbsd32_msqid_ds14(struct msqid_ds *msqbuf, struct netbsd32_msqid_ds14 *omsqbuf)
108 {
109 
110 	native_to_netbsd32_ipc_perm14(&msqbuf->msg_perm, &omsqbuf->msg_perm);
111 
112 #define	CVT(x)	omsqbuf->x = msqbuf->x
113 	CVT(msg_qnum);
114 	CVT(msg_qbytes);
115 	CVT(msg_lspid);
116 	CVT(msg_lrpid);
117 	CVT(msg_stime);
118 	CVT(msg_rtime);
119 	CVT(msg_ctime);
120 #undef CVT
121 
122 	/*
123 	 * Not part of the API, but some programs might look at it.
124 	 */
125 	omsqbuf->msg_cbytes = msqbuf->_msg_cbytes;
126 }
127 #endif
128 
129 #if defined(SYSVSEM)
130 static inline void
netbsd32_semid_ds14_to_native(struct netbsd32_semid_ds14 * osembuf,struct semid_ds * sembuf)131 netbsd32_semid_ds14_to_native(struct netbsd32_semid_ds14 *osembuf, struct semid_ds *sembuf)
132 {
133 
134 	netbsd32_ipc_perm14_to_native(&osembuf->sem_perm, &sembuf->sem_perm);
135 
136 #define	CVT(x)	sembuf->x = osembuf->x
137 	CVT(sem_nsems);
138 	CVT(sem_otime);
139 	CVT(sem_ctime);
140 #undef CVT
141 }
142 
143 static inline void
native_to_netbsd32_semid_ds14(struct semid_ds * sembuf,struct netbsd32_semid_ds14 * osembuf)144 native_to_netbsd32_semid_ds14(struct semid_ds *sembuf, struct netbsd32_semid_ds14 *osembuf)
145 {
146 
147 	native_to_netbsd32_ipc_perm14(&sembuf->sem_perm, &osembuf->sem_perm);
148 
149 #define	CVT(x)	osembuf->x = sembuf->x
150 	CVT(sem_nsems);
151 	CVT(sem_otime);
152 	CVT(sem_ctime);
153 #undef CVT
154 }
155 
156 static inline void
netbsd32_shmid_ds14_to_native(struct netbsd32_shmid_ds14 * oshmbuf,struct shmid_ds * shmbuf)157 netbsd32_shmid_ds14_to_native(struct netbsd32_shmid_ds14 *oshmbuf, struct shmid_ds *shmbuf)
158 {
159 
160 	netbsd32_ipc_perm14_to_native(&oshmbuf->shm_perm, &shmbuf->shm_perm);
161 
162 #define	CVT(x)	shmbuf->x = oshmbuf->x
163 	CVT(shm_segsz);
164 	CVT(shm_lpid);
165 	CVT(shm_cpid);
166 	CVT(shm_nattch);
167 	CVT(shm_atime);
168 	CVT(shm_dtime);
169 	CVT(shm_ctime);
170 #undef CVT
171 }
172 
173 static inline void
native_to_netbsd32_shmid_ds14(struct shmid_ds * shmbuf,struct netbsd32_shmid_ds14 * oshmbuf)174 native_to_netbsd32_shmid_ds14(struct shmid_ds *shmbuf, struct netbsd32_shmid_ds14 *oshmbuf)
175 {
176 
177 	native_to_netbsd32_ipc_perm14(&shmbuf->shm_perm, &oshmbuf->shm_perm);
178 
179 #define	CVT(x)	oshmbuf->x = shmbuf->x
180 	CVT(shm_segsz);
181 	CVT(shm_lpid);
182 	CVT(shm_cpid);
183 	CVT(shm_nattch);
184 	CVT(shm_atime);
185 	CVT(shm_dtime);
186 	CVT(shm_ctime);
187 #undef CVT
188 }
189 
190 /*
191  * the compat_14 system calls
192  */
193 int
compat_14_netbsd32_msgctl(struct lwp * l,const struct compat_14_netbsd32_msgctl_args * uap,register_t * retval)194 compat_14_netbsd32_msgctl(struct lwp *l, const struct compat_14_netbsd32_msgctl_args *uap, register_t *retval)
195 {
196 	/* {
197 		syscallarg(int) msqid;
198 		syscallarg(int) cmd;
199 		syscallarg(struct msqid_ds14 *) buf;
200 	} */
201 	struct msqid_ds msqbuf;
202 	struct netbsd32_msqid_ds14 omsqbuf;
203 	int cmd, error;
204 
205 	cmd = SCARG(uap, cmd);
206 
207 	if (cmd == IPC_SET) {
208 		error = copyin(SCARG_P32(uap, buf),
209 		    &omsqbuf, sizeof(omsqbuf));
210 		if (error)
211 			return (error);
212 		netbsd32_msqid_ds14_to_native(&omsqbuf, &msqbuf);
213 	}
214 
215 	error = msgctl1(l, SCARG(uap, msqid), cmd,
216 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &msqbuf : NULL);
217 
218 	if (error == 0 && cmd == IPC_STAT) {
219 		native_to_netbsd32_msqid_ds14(&msqbuf, &omsqbuf);
220 		error = copyout(&omsqbuf,
221 		    SCARG_P32(uap, buf), sizeof(omsqbuf));
222 	}
223 
224 	return (error);
225 }
226 #endif
227 
228 #if defined(SYSVSEM)
229 int
compat_14_netbsd32___semctl(struct lwp * l,const struct compat_14_netbsd32___semctl_args * uap,register_t * retval)230 compat_14_netbsd32___semctl(struct lwp *l, const struct compat_14_netbsd32___semctl_args *uap, register_t *retval)
231 {
232 	/* {
233 		syscallarg(int) semid;
234 		syscallarg(int) semnum;
235 		syscallarg(int) cmd;
236 		syscallarg(union __semun *) arg;
237 	} */
238 	union __semun arg;
239 	struct semid_ds sembuf;
240 	struct netbsd32_semid_ds14 osembuf;
241 	int cmd, error;
242 	void *pass_arg = NULL;
243 
244 	cmd = SCARG(uap, cmd);
245 
246 	switch (cmd) {
247 	case IPC_SET:
248 	case IPC_STAT:
249 		pass_arg = &sembuf;
250 		break;
251 
252 	case GETALL:
253 	case SETVAL:
254 	case SETALL:
255 		pass_arg = &arg;
256 		break;
257 	}
258 
259 	if (pass_arg != NULL) {
260 		error = copyin(NETBSD32IPTR64(SCARG(uap, arg)), &arg,
261 		    sizeof(arg));
262 		if (error)
263 			return (error);
264 		if (cmd == IPC_SET) {
265 			error = copyin(arg.buf, &osembuf, sizeof(osembuf));
266 			if (error)
267 				return (error);
268 			netbsd32_semid_ds14_to_native(&osembuf, &sembuf);
269 		}
270 	}
271 
272 	error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
273 	    pass_arg, retval);
274 
275 	if (error == 0 && cmd == IPC_STAT) {
276 		native_to_netbsd32_semid_ds14(&sembuf, &osembuf);
277 		error = copyout(&osembuf, arg.buf, sizeof(osembuf));
278 	}
279 
280 	return (error);
281 }
282 #endif
283 
284 #if defined(SYSVSHM)
285 int
compat_14_netbsd32_shmctl(struct lwp * l,const struct compat_14_netbsd32_shmctl_args * uap,register_t * retval)286 compat_14_netbsd32_shmctl(struct lwp *l, const struct compat_14_netbsd32_shmctl_args *uap, register_t *retval)
287 {
288 	/* {
289 		syscallarg(int) shmid;
290 		syscallarg(int) cmd;
291 		syscallarg(struct netbsd32_shmid_ds14 *) buf;
292 	} */
293 	struct shmid_ds shmbuf;
294 	struct netbsd32_shmid_ds14 oshmbuf;
295 	int cmd, error;
296 
297 	cmd = SCARG(uap, cmd);
298 
299 	if (cmd == IPC_SET) {
300 		error = copyin(SCARG_P32(uap, buf), &oshmbuf, sizeof(oshmbuf));
301 		if (error)
302 			return (error);
303 		netbsd32_shmid_ds14_to_native(&oshmbuf, &shmbuf);
304 	}
305 
306 	error = shmctl1(l, SCARG(uap, shmid), cmd,
307 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
308 
309 	if (error == 0 && cmd == IPC_STAT) {
310 		native_to_netbsd32_shmid_ds14(&shmbuf, &oshmbuf);
311 		error = copyout(&oshmbuf, SCARG_P32(uap, buf), sizeof(oshmbuf));
312 	}
313 
314 	return (error);
315 }
316 #endif
317 #endif /* COMPAT_14 */
318