1 1.7 pgoyette /* $NetBSD: netbsd32_mqueue.c,v 1.7 2019/01/27 02:08:40 pgoyette Exp $ */ 2 1.1 martin 3 1.1 martin /*- 4 1.1 martin * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 1.1 martin * All rights reserved. 6 1.1 martin * 7 1.1 martin * This code is derived from software developed for The NetBSD Foundation. 8 1.1 martin * 9 1.1 martin * Redistribution and use in source and binary forms, with or without 10 1.1 martin * modification, are permitted provided that the following conditions 11 1.1 martin * are met: 12 1.1 martin * 1. Redistributions of source code must retain the above copyright 13 1.1 martin * notice, this list of conditions and the following disclaimer. 14 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 martin * notice, this list of conditions and the following disclaimer in the 16 1.1 martin * documentation and/or other materials provided with the distribution. 17 1.1 martin * 18 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 1.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 1.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 1.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 1.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 1.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 1.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 1.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 1.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 1.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 1.1 martin * POSSIBILITY OF SUCH DAMAGE. 29 1.1 martin */ 30 1.1 martin 31 1.1 martin #include <sys/cdefs.h> 32 1.7 pgoyette __KERNEL_RCSID(0, "$NetBSD: netbsd32_mqueue.c,v 1.7 2019/01/27 02:08:40 pgoyette Exp $"); 33 1.1 martin 34 1.1 martin #if defined(_KERNEL_OPT) 35 1.1 martin #include "opt_compat_netbsd.h" 36 1.1 martin #endif 37 1.1 martin 38 1.1 martin #include <sys/param.h> 39 1.1 martin #include <sys/dirent.h> 40 1.1 martin #include <sys/filedesc.h> 41 1.1 martin #include <sys/fcntl.h> 42 1.1 martin #include <sys/module.h> 43 1.6 pgoyette #include <sys/syscallvar.h> 44 1.1 martin 45 1.1 martin #include <compat/netbsd32/netbsd32.h> 46 1.1 martin #include <compat/netbsd32/netbsd32_syscall.h> 47 1.1 martin #include <compat/netbsd32/netbsd32_syscallargs.h> 48 1.1 martin #include <compat/netbsd32/netbsd32_conv.h> 49 1.1 martin 50 1.1 martin int 51 1.1 martin netbsd32_mq_open(struct lwp *l, const struct netbsd32_mq_open_args *uap, 52 1.1 martin register_t *retval) 53 1.1 martin { 54 1.1 martin /* { 55 1.1 martin syscallarg(const netbsd32_charp) name; 56 1.1 martin syscallarg(int) oflag; 57 1.1 martin syscallarg(mode_t) mode; 58 1.1 martin syscallarg(struct netbsd32_mq_attrp_t) attr; 59 1.1 martin } */ 60 1.1 martin struct netbsd32_mq_attr attr32; 61 1.1 martin struct mq_attr *attr = NULL, a; 62 1.1 martin int error; 63 1.1 martin 64 1.5 christos if ((SCARG(uap, oflag) & O_CREAT) && SCARG_P32(uap, attr) != NULL) { 65 1.5 christos error = copyin(SCARG_P32(uap, attr), &attr32, sizeof(attr32)); 66 1.1 martin if (error) 67 1.1 martin return error; 68 1.1 martin netbsd32_to_mq_attr(&attr32, &a); 69 1.1 martin attr = &a; 70 1.1 martin } 71 1.1 martin 72 1.1 martin return mq_handle_open(l, SCARG_P32(uap, name), SCARG(uap, oflag), 73 1.1 martin SCARG(uap, mode), attr, retval); 74 1.1 martin } 75 1.1 martin 76 1.1 martin int 77 1.1 martin netbsd32_mq_close(struct lwp *l, const struct netbsd32_mq_close_args *uap, 78 1.1 martin register_t *retval) 79 1.1 martin { 80 1.1 martin /* { 81 1.1 martin syscallarg(mqd_t) mqdes; 82 1.1 martin } */ 83 1.1 martin 84 1.1 martin return netbsd32_close(l, (const void*)uap, retval); 85 1.1 martin } 86 1.1 martin 87 1.1 martin int 88 1.1 martin netbsd32_mq_unlink(struct lwp *l, const struct netbsd32_mq_unlink_args *uap, 89 1.1 martin register_t *retval) 90 1.1 martin { 91 1.1 martin /* { 92 1.1 martin syscallarg(const netbsd32_charp) name; 93 1.1 martin } */ 94 1.1 martin struct sys_mq_unlink_args ua; 95 1.1 martin 96 1.1 martin NETBSD32TOP_UAP(name, const char); 97 1.1 martin return sys_mq_unlink(l, &ua, retval); 98 1.1 martin } 99 1.1 martin 100 1.1 martin int 101 1.1 martin netbsd32_mq_getattr(struct lwp *l, const struct netbsd32_mq_getattr_args *uap, 102 1.1 martin register_t *retval) 103 1.1 martin { 104 1.1 martin /* { 105 1.1 martin syscallarg(mqd_t) mqdes; 106 1.1 martin syscallarg(netbsd32_mq_attrp_t) mqstat; 107 1.1 martin } */ 108 1.1 martin struct mqueue *mq; 109 1.1 martin struct mq_attr attr; 110 1.1 martin struct netbsd32_mq_attr a32; 111 1.1 martin int error; 112 1.1 martin 113 1.1 martin error = mqueue_get(SCARG(uap, mqdes), 0, &mq); 114 1.1 martin if (error) 115 1.1 martin return error; 116 1.1 martin 117 1.1 martin memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr)); 118 1.1 martin mutex_exit(&mq->mq_mtx); 119 1.1 martin fd_putfile((int)SCARG(uap, mqdes)); 120 1.1 martin netbsd32_from_mq_attr(&attr, &a32); 121 1.1 martin return copyout(&a32, SCARG_P32(uap,mqstat), sizeof(a32)); 122 1.1 martin } 123 1.1 martin 124 1.1 martin int 125 1.1 martin netbsd32_mq_setattr(struct lwp *l, const struct netbsd32_mq_setattr_args *uap, 126 1.1 martin register_t *retval) 127 1.1 martin { 128 1.1 martin /* { 129 1.1 martin syscallarg(mqd_t) mqdes; 130 1.1 martin syscallarg(const netbsd32_mq_attrp_t) mqstat; 131 1.1 martin syscallarg(netbsd32_mq_attrp_t) omqstat; 132 1.1 martin } */ 133 1.1 martin struct mqueue *mq; 134 1.1 martin struct netbsd32_mq_attr attr32; 135 1.1 martin struct mq_attr attr; 136 1.1 martin int error, nonblock; 137 1.1 martin 138 1.1 martin error = copyin(SCARG_P32(uap, mqstat), &attr32, sizeof(attr32)); 139 1.1 martin if (error) 140 1.1 martin return error; 141 1.1 martin netbsd32_to_mq_attr(&attr32, &attr); 142 1.1 martin nonblock = (attr.mq_flags & O_NONBLOCK); 143 1.1 martin 144 1.1 martin error = mqueue_get(SCARG(uap, mqdes), 0, &mq); 145 1.1 martin if (error) 146 1.1 martin return error; 147 1.1 martin 148 1.1 martin /* Copy the old attributes, if needed */ 149 1.1 martin if (SCARG_P32(uap, omqstat)) 150 1.1 martin memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr)); 151 1.1 martin 152 1.1 martin /* Ignore everything, except O_NONBLOCK */ 153 1.1 martin if (nonblock) 154 1.1 martin mq->mq_attrib.mq_flags |= O_NONBLOCK; 155 1.1 martin else 156 1.1 martin mq->mq_attrib.mq_flags &= ~O_NONBLOCK; 157 1.1 martin 158 1.1 martin mutex_exit(&mq->mq_mtx); 159 1.1 martin fd_putfile((int)SCARG(uap, mqdes)); 160 1.1 martin 161 1.1 martin /* 162 1.1 martin * Copy the data to the user-space. 163 1.1 martin * Note: According to POSIX, the new attributes should not be set in 164 1.1 martin * case of fail - this would be violated. 165 1.1 martin */ 166 1.1 martin if (SCARG_P32(uap, omqstat)) { 167 1.1 martin netbsd32_from_mq_attr(&attr, &attr32); 168 1.1 martin error = copyout(&attr32, SCARG_P32(uap, omqstat), 169 1.1 martin sizeof(attr32)); 170 1.1 martin } 171 1.1 martin 172 1.1 martin return error; 173 1.1 martin } 174 1.1 martin 175 1.1 martin int 176 1.1 martin netbsd32_mq_notify(struct lwp *l, const struct netbsd32_mq_notify_args *uap, 177 1.1 martin register_t *result) 178 1.1 martin { 179 1.1 martin /* { 180 1.1 martin syscallarg(mqd_t) mqdes; 181 1.1 martin syscallarg(const netbsd32_sigeventp_t) notification; 182 1.1 martin } */ 183 1.1 martin struct mqueue *mq; 184 1.1 martin struct netbsd32_sigevent sig32; 185 1.1 martin int error; 186 1.1 martin 187 1.1 martin if (SCARG_P32(uap, notification)) { 188 1.1 martin /* Get the signal from user-space */ 189 1.1 martin error = copyin(SCARG_P32(uap, notification), &sig32, 190 1.1 martin sizeof(sig32)); 191 1.1 martin if (error) 192 1.1 martin return error; 193 1.1 martin if (sig32.sigev_notify == SIGEV_SIGNAL && 194 1.1 martin (sig32.sigev_signo <=0 || sig32.sigev_signo >= NSIG)) 195 1.1 martin return EINVAL; 196 1.1 martin } 197 1.1 martin 198 1.1 martin error = mqueue_get(SCARG(uap, mqdes), 0, &mq); 199 1.1 martin if (error) { 200 1.1 martin return error; 201 1.1 martin } 202 1.1 martin if (SCARG_P32(uap, notification)) { 203 1.1 martin /* Register notification: set the signal and target process */ 204 1.1 martin if (mq->mq_notify_proc == NULL) { 205 1.1 martin netbsd32_to_sigevent(&sig32, &mq->mq_sig_notify); 206 1.1 martin mq->mq_notify_proc = l->l_proc; 207 1.1 martin } else { 208 1.1 martin /* Fail if someone else already registered */ 209 1.1 martin error = EBUSY; 210 1.1 martin } 211 1.1 martin } else { 212 1.1 martin /* Unregister the notification */ 213 1.1 martin mq->mq_notify_proc = NULL; 214 1.1 martin } 215 1.1 martin mutex_exit(&mq->mq_mtx); 216 1.1 martin fd_putfile((int)SCARG(uap, mqdes)); 217 1.1 martin 218 1.1 martin return error; 219 1.1 martin } 220 1.1 martin 221 1.1 martin int 222 1.1 martin netbsd32_mq_send(struct lwp *l, const struct netbsd32_mq_send_args *uap, 223 1.1 martin register_t *result) 224 1.1 martin { 225 1.1 martin /* { 226 1.1 martin syscallarg(mqd_t) mqdes; 227 1.1 martin syscallarg(const netbsd32_charp) msg_ptr; 228 1.1 martin syscallarg(netbsd32_size_t) msg_len; 229 1.1 martin syscallarg(unsigned) msg_prio; 230 1.1 martin } */ 231 1.1 martin 232 1.1 martin 233 1.1 martin return mq_send1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr), 234 1.1 martin SCARG(uap, msg_len), SCARG(uap, msg_prio), NULL); 235 1.1 martin } 236 1.1 martin 237 1.1 martin int 238 1.1 martin netbsd32_mq_receive(struct lwp *l, const struct netbsd32_mq_receive_args *uap, 239 1.1 martin register_t *retval) 240 1.1 martin { 241 1.1 martin /* { 242 1.1 martin syscallarg(mqd_t) mqdes; 243 1.1 martin syscallarg(netbsd32_charp) msg_ptr; 244 1.1 martin syscallarg(netbsd32_size_t) msg_len; 245 1.1 martin syscallarg(netbsd32_uintp) msg_prio; 246 1.1 martin } */ 247 1.1 martin ssize_t mlen; 248 1.1 martin int error; 249 1.1 martin 250 1.1 martin error = mq_recv1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr), 251 1.1 martin SCARG(uap, msg_len), SCARG_P32(uap, msg_prio), NULL, &mlen); 252 1.1 martin if (error == 0) 253 1.1 martin *retval = mlen; 254 1.1 martin 255 1.1 martin return error; 256 1.1 martin } 257 1.1 martin 258 1.2 martin int 259 1.2 martin netbsd32___mq_timedsend50(struct lwp *l, 260 1.2 martin const struct netbsd32___mq_timedsend50_args *uap, register_t *retval) 261 1.2 martin { 262 1.2 martin /* { 263 1.2 martin syscallarg(mqd_t) mqdes; 264 1.2 martin syscallarg(const netbsd32_charp) msg_ptr; 265 1.2 martin syscallarg(netbsd32_size_t) msg_len; 266 1.2 martin syscallarg(unsigned) msg_prio; 267 1.2 martin syscallarg(const netbsd32_timespecp_t) abs_timeout; 268 1.2 martin } */ 269 1.2 martin struct timespec ts, *tsp; 270 1.2 martin struct netbsd32_timespec ts32; 271 1.2 martin int error; 272 1.2 martin 273 1.2 martin /* Get and convert time value */ 274 1.2 martin if (SCARG_P32(uap, abs_timeout)) { 275 1.2 martin error = copyin(SCARG_P32(uap, abs_timeout), &ts32, 276 1.2 martin sizeof(ts32)); 277 1.2 martin if (error) 278 1.2 martin return error; 279 1.2 martin netbsd32_to_timespec(&ts32, &ts); 280 1.2 martin tsp = &ts; 281 1.2 martin } else { 282 1.2 martin tsp = NULL; 283 1.2 martin } 284 1.2 martin 285 1.2 martin return mq_send1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr), 286 1.2 martin SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp); 287 1.2 martin } 288 1.2 martin 289 1.2 martin int 290 1.2 martin netbsd32___mq_timedreceive50(struct lwp *l, 291 1.2 martin const struct netbsd32___mq_timedreceive50_args *uap, register_t *retval) 292 1.2 martin { 293 1.2 martin /* { 294 1.2 martin syscallarg(mqd_t) mqdes; 295 1.2 martin syscallarg(netbsd32_charp) msg_ptr; 296 1.2 martin syscallarg(netbsd32_size_t) msg_len; 297 1.2 martin syscallarg(netbsd32_uintp) msg_prio; 298 1.2 martin syscallarg(const netbsd32_timespecp_t) abs_timeout; 299 1.2 martin } */ 300 1.2 martin struct timespec ts, *tsp; 301 1.2 martin struct netbsd32_timespec ts32; 302 1.2 martin ssize_t mlen; 303 1.2 martin int error; 304 1.2 martin 305 1.2 martin /* Get and convert time value */ 306 1.2 martin if (SCARG_P32(uap, abs_timeout)) { 307 1.2 martin error = copyin(SCARG_P32(uap, abs_timeout), &ts32, 308 1.2 martin sizeof(ts32)); 309 1.2 martin if (error) 310 1.2 martin return error; 311 1.2 martin netbsd32_to_timespec(&ts32, &ts); 312 1.2 martin tsp = &ts; 313 1.2 martin } else { 314 1.2 martin tsp = NULL; 315 1.2 martin } 316 1.2 martin 317 1.2 martin error = mq_recv1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr), 318 1.2 martin SCARG(uap, msg_len), SCARG_P32(uap, msg_prio), tsp, &mlen); 319 1.2 martin if (error == 0) 320 1.2 martin *retval = mlen; 321 1.2 martin 322 1.2 martin return error; 323 1.2 martin } 324 1.1 martin 325 1.6 pgoyette #ifdef COMPAT_50 326 1.6 pgoyette 327 1.6 pgoyette int 328 1.6 pgoyette compat_50_netbsd32_mq_timedsend(struct lwp *l, 329 1.6 pgoyette const struct compat_50_netbsd32_mq_timedsend_args *uap, 330 1.6 pgoyette register_t *retval) 331 1.6 pgoyette { 332 1.6 pgoyette /* { 333 1.6 pgoyette syscallarg(mqd_t) mqdes; 334 1.6 pgoyette syscallarg(const netbsd32_charp) msg_ptr; 335 1.6 pgoyette syscallarg(netbsd32_size_t) msg_len; 336 1.6 pgoyette syscallarg(unsigned) msg_prio; 337 1.6 pgoyette syscallarg(const netbsd32_timespec50p_t) abs_timeout; 338 1.6 pgoyette } */ 339 1.6 pgoyette struct timespec ts, *tsp; 340 1.6 pgoyette struct netbsd32_timespec50 ts32; 341 1.6 pgoyette int error; 342 1.6 pgoyette 343 1.6 pgoyette /* Get and convert time value */ 344 1.6 pgoyette if (SCARG_P32(uap, abs_timeout)) { 345 1.6 pgoyette error = copyin(SCARG_P32(uap, abs_timeout), &ts32, 346 1.6 pgoyette sizeof(ts32)); 347 1.6 pgoyette if (error) 348 1.6 pgoyette return error; 349 1.6 pgoyette netbsd32_to_timespec50(&ts32, &ts); 350 1.6 pgoyette tsp = &ts; 351 1.6 pgoyette } else { 352 1.6 pgoyette tsp = NULL; 353 1.6 pgoyette } 354 1.6 pgoyette 355 1.6 pgoyette return mq_send1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr), 356 1.6 pgoyette SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp); 357 1.6 pgoyette } 358 1.6 pgoyette 359 1.6 pgoyette int 360 1.6 pgoyette compat_50_netbsd32_mq_timedreceive(struct lwp *l, 361 1.6 pgoyette const struct compat_50_netbsd32_mq_timedreceive_args *uap, 362 1.6 pgoyette register_t *retval) 363 1.6 pgoyette { 364 1.6 pgoyette /* { 365 1.6 pgoyette syscallarg(mqd_t) mqdes; 366 1.6 pgoyette syscallarg(netbsd32_charp) msg_ptr; 367 1.6 pgoyette syscallarg(netbsd32_size_t) msg_len; 368 1.6 pgoyette syscallarg(netbsd32_uintp) msg_prio; 369 1.6 pgoyette syscallarg(const netbsd32_timespec50p_t) abs_timeout; 370 1.6 pgoyette } */ 371 1.6 pgoyette struct timespec ts, *tsp; 372 1.6 pgoyette struct netbsd32_timespec50 ts32; 373 1.6 pgoyette ssize_t mlen; 374 1.6 pgoyette int error; 375 1.6 pgoyette 376 1.6 pgoyette /* Get and convert time value */ 377 1.6 pgoyette if (SCARG_P32(uap, abs_timeout)) { 378 1.6 pgoyette error = copyin(SCARG_P32(uap, abs_timeout), &ts32, 379 1.6 pgoyette sizeof(ts32)); 380 1.6 pgoyette if (error) 381 1.6 pgoyette return error; 382 1.6 pgoyette netbsd32_to_timespec50(&ts32, &ts); 383 1.6 pgoyette tsp = &ts; 384 1.6 pgoyette } else { 385 1.6 pgoyette tsp = NULL; 386 1.6 pgoyette } 387 1.6 pgoyette 388 1.6 pgoyette error = mq_recv1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr), 389 1.6 pgoyette SCARG(uap, msg_len), SCARG_P32(uap, msg_prio), tsp, &mlen); 390 1.6 pgoyette if (error == 0) 391 1.6 pgoyette *retval = mlen; 392 1.6 pgoyette 393 1.6 pgoyette return error; 394 1.6 pgoyette } 395 1.6 pgoyette 396 1.6 pgoyette #endif /* COMPAT_50 */ 397 1.6 pgoyette 398 1.6 pgoyette #define _PKG_ENTRY(name) \ 399 1.6 pgoyette { NETBSD32_SYS_ ## name, 0, (sy_call_t *)name } 400 1.6 pgoyette 401 1.6 pgoyette static const struct syscall_package compat_mqueue_syscalls[] = { 402 1.6 pgoyette _PKG_ENTRY(netbsd32_mq_open), 403 1.6 pgoyette _PKG_ENTRY(netbsd32_mq_close), 404 1.6 pgoyette _PKG_ENTRY(netbsd32_mq_unlink), 405 1.6 pgoyette _PKG_ENTRY(netbsd32_mq_getattr), 406 1.6 pgoyette _PKG_ENTRY(netbsd32_mq_setattr), 407 1.6 pgoyette _PKG_ENTRY(netbsd32_mq_notify), 408 1.6 pgoyette _PKG_ENTRY(netbsd32_mq_send), 409 1.6 pgoyette _PKG_ENTRY(netbsd32_mq_receive), 410 1.6 pgoyette _PKG_ENTRY(netbsd32___mq_timedsend50), 411 1.6 pgoyette _PKG_ENTRY(netbsd32___mq_timedreceive50), 412 1.6 pgoyette #ifdef COMPAT_50 413 1.6 pgoyette _PKG_ENTRY(compat_50_netbsd32_mq_timedsend), 414 1.6 pgoyette _PKG_ENTRY(compat_50_netbsd32_mq_timedreceive), 415 1.6 pgoyette #endif 416 1.6 pgoyette {0, 0, NULL} 417 1.6 pgoyette }; 418 1.6 pgoyette 419 1.6 pgoyette MODULE(MODULE_CLASS_EXEC, compat_netbsd32_mqueue, "mqueue,compat_netbsd32"); 420 1.6 pgoyette 421 1.6 pgoyette static int 422 1.6 pgoyette compat_netbsd32_mqueue_modcmd(modcmd_t cmd, void *arg) 423 1.6 pgoyette { 424 1.6 pgoyette int error; 425 1.6 pgoyette 426 1.6 pgoyette switch (cmd) { 427 1.6 pgoyette case MODULE_CMD_INIT: 428 1.6 pgoyette error = syscall_establish(&emul_netbsd32, 429 1.6 pgoyette compat_mqueue_syscalls); 430 1.6 pgoyette break; 431 1.6 pgoyette case MODULE_CMD_FINI: 432 1.6 pgoyette error = syscall_disestablish(&emul_netbsd32, 433 1.6 pgoyette compat_mqueue_syscalls); 434 1.6 pgoyette break; 435 1.6 pgoyette default: 436 1.6 pgoyette error = ENOTTY; 437 1.6 pgoyette break; 438 1.6 pgoyette } 439 1.6 pgoyette return error; 440 1.6 pgoyette } 441