1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1994-1995 Søren Schmidt 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/syscallsubr.h> 35 #include <sys/sysproto.h> 36 #include <sys/proc.h> 37 #include <sys/limits.h> 38 #include <sys/msg.h> 39 #include <sys/sem.h> 40 #include <sys/shm.h> 41 #include <sys/stat.h> 42 43 #include "opt_compat.h" 44 45 #ifdef COMPAT_LINUX32 46 #include <machine/../linux32/linux.h> 47 #include <machine/../linux32/linux32_proto.h> 48 #else 49 #include <machine/../linux/linux.h> 50 #include <machine/../linux/linux_proto.h> 51 #endif 52 #include <compat/linux/linux_ipc.h> 53 #include <compat/linux/linux_ipc64.h> 54 #include <compat/linux/linux_timer.h> 55 #include <compat/linux/linux_util.h> 56 57 /* 58 * old, pre 2.4 kernel 59 */ 60 struct l_ipc_perm { 61 l_key_t key; 62 l_uid16_t uid; 63 l_gid16_t gid; 64 l_uid16_t cuid; 65 l_gid16_t cgid; 66 l_ushort mode; 67 l_ushort seq; 68 }; 69 70 struct l_seminfo { 71 l_int semmap; 72 l_int semmni; 73 l_int semmns; 74 l_int semmnu; 75 l_int semmsl; 76 l_int semopm; 77 l_int semume; 78 l_int semusz; 79 l_int semvmx; 80 l_int semaem; 81 }; 82 83 struct l_shminfo { 84 l_int shmmax; 85 l_int shmmin; 86 l_int shmmni; 87 l_int shmseg; 88 l_int shmall; 89 }; 90 91 struct l_shm_info { 92 l_int used_ids; 93 l_ulong shm_tot; /* total allocated shm */ 94 l_ulong shm_rss; /* total resident shm */ 95 l_ulong shm_swp; /* total swapped shm */ 96 l_ulong swap_attempts; 97 l_ulong swap_successes; 98 }; 99 100 struct l_msginfo { 101 l_int msgpool; 102 l_int msgmap; 103 l_int msgmax; 104 l_int msgmnb; 105 l_int msgmni; 106 l_int msgssz; 107 l_int msgtql; 108 l_ushort msgseg; 109 }; 110 111 static void 112 bsd_to_linux_shminfo( struct shminfo *bpp, struct l_shminfo64 *lpp) 113 { 114 115 lpp->shmmax = bpp->shmmax; 116 lpp->shmmin = bpp->shmmin; 117 lpp->shmmni = bpp->shmmni; 118 lpp->shmseg = bpp->shmseg; 119 lpp->shmall = bpp->shmall; 120 } 121 122 static void 123 bsd_to_linux_shm_info( struct shm_info *bpp, struct l_shm_info *lpp) 124 { 125 126 lpp->used_ids = bpp->used_ids; 127 lpp->shm_tot = bpp->shm_tot; 128 lpp->shm_rss = bpp->shm_rss; 129 lpp->shm_swp = bpp->shm_swp; 130 lpp->swap_attempts = bpp->swap_attempts; 131 lpp->swap_successes = bpp->swap_successes; 132 } 133 134 static void 135 linux_to_bsd_ipc_perm(struct l_ipc64_perm *lpp, struct ipc_perm *bpp) 136 { 137 138 bpp->key = lpp->key; 139 bpp->uid = lpp->uid; 140 bpp->gid = lpp->gid; 141 bpp->cuid = lpp->cuid; 142 bpp->cgid = lpp->cgid; 143 bpp->mode = lpp->mode; 144 bpp->seq = lpp->seq; 145 } 146 147 static void 148 bsd_to_linux_ipc_perm(struct ipc_perm *bpp, struct l_ipc64_perm *lpp) 149 { 150 151 lpp->key = bpp->key; 152 lpp->uid = bpp->uid; 153 lpp->gid = bpp->gid; 154 lpp->cuid = bpp->cuid; 155 lpp->cgid = bpp->cgid; 156 lpp->mode = bpp->mode & (S_IRWXU|S_IRWXG|S_IRWXO); 157 lpp->seq = bpp->seq; 158 } 159 160 struct l_msqid_ds { 161 struct l_ipc_perm msg_perm; 162 l_uintptr_t msg_first; /* first message on queue,unused */ 163 l_uintptr_t msg_last; /* last message in queue,unused */ 164 l_time_t msg_stime; /* last msgsnd time */ 165 l_time_t msg_rtime; /* last msgrcv time */ 166 l_time_t msg_ctime; /* last change time */ 167 l_ulong msg_lcbytes; /* Reuse junk fields for 32 bit */ 168 l_ulong msg_lqbytes; /* ditto */ 169 l_ushort msg_cbytes; /* current number of bytes on queue */ 170 l_ushort msg_qnum; /* number of messages in queue */ 171 l_ushort msg_qbytes; /* max number of bytes on queue */ 172 l_pid_t msg_lspid; /* pid of last msgsnd */ 173 l_pid_t msg_lrpid; /* last receive pid */ 174 }; 175 176 struct l_semid_ds { 177 struct l_ipc_perm sem_perm; 178 l_time_t sem_otime; 179 l_time_t sem_ctime; 180 l_uintptr_t sem_base; 181 l_uintptr_t sem_pending; 182 l_uintptr_t sem_pending_last; 183 l_uintptr_t undo; 184 l_ushort sem_nsems; 185 }; 186 187 struct l_shmid_ds { 188 struct l_ipc_perm shm_perm; 189 l_int shm_segsz; 190 l_time_t shm_atime; 191 l_time_t shm_dtime; 192 l_time_t shm_ctime; 193 l_ushort shm_cpid; 194 l_ushort shm_lpid; 195 l_short shm_nattch; 196 l_ushort private1; 197 l_uintptr_t private2; 198 l_uintptr_t private3; 199 }; 200 201 static void 202 linux_to_bsd_semid_ds(struct l_semid64_ds *lsp, struct semid_ds *bsp) 203 { 204 205 linux_to_bsd_ipc_perm(&lsp->sem_perm, &bsp->sem_perm); 206 bsp->sem_otime = lsp->sem_otime; 207 bsp->sem_ctime = lsp->sem_ctime; 208 bsp->sem_nsems = lsp->sem_nsems; 209 } 210 211 static void 212 bsd_to_linux_semid_ds(struct semid_ds *bsp, struct l_semid64_ds *lsp) 213 { 214 215 bsd_to_linux_ipc_perm(&bsp->sem_perm, &lsp->sem_perm); 216 lsp->sem_otime = bsp->sem_otime; 217 lsp->sem_ctime = bsp->sem_ctime; 218 lsp->sem_nsems = bsp->sem_nsems; 219 } 220 221 static void 222 linux_to_bsd_shmid_ds(struct l_shmid64_ds *lsp, struct shmid_ds *bsp) 223 { 224 225 linux_to_bsd_ipc_perm(&lsp->shm_perm, &bsp->shm_perm); 226 bsp->shm_segsz = lsp->shm_segsz; 227 bsp->shm_lpid = lsp->shm_lpid; 228 bsp->shm_cpid = lsp->shm_cpid; 229 bsp->shm_nattch = lsp->shm_nattch; 230 bsp->shm_atime = lsp->shm_atime; 231 bsp->shm_dtime = lsp->shm_dtime; 232 bsp->shm_ctime = lsp->shm_ctime; 233 } 234 235 static void 236 bsd_to_linux_shmid_ds(struct shmid_ds *bsp, struct l_shmid64_ds *lsp) 237 { 238 239 bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->shm_perm); 240 lsp->shm_segsz = bsp->shm_segsz; 241 lsp->shm_lpid = bsp->shm_lpid; 242 lsp->shm_cpid = bsp->shm_cpid; 243 lsp->shm_nattch = bsp->shm_nattch; 244 lsp->shm_atime = bsp->shm_atime; 245 lsp->shm_dtime = bsp->shm_dtime; 246 lsp->shm_ctime = bsp->shm_ctime; 247 } 248 249 static void 250 linux_to_bsd_msqid_ds(struct l_msqid64_ds *lsp, struct msqid_ds *bsp) 251 { 252 253 linux_to_bsd_ipc_perm(&lsp->msg_perm, &bsp->msg_perm); 254 bsp->msg_cbytes = lsp->msg_cbytes; 255 bsp->msg_qnum = lsp->msg_qnum; 256 bsp->msg_qbytes = lsp->msg_qbytes; 257 bsp->msg_lspid = lsp->msg_lspid; 258 bsp->msg_lrpid = lsp->msg_lrpid; 259 bsp->msg_stime = lsp->msg_stime; 260 bsp->msg_rtime = lsp->msg_rtime; 261 bsp->msg_ctime = lsp->msg_ctime; 262 } 263 264 static void 265 bsd_to_linux_msqid_ds(struct msqid_ds *bsp, struct l_msqid64_ds *lsp) 266 { 267 268 bsd_to_linux_ipc_perm(&bsp->msg_perm, &lsp->msg_perm); 269 lsp->msg_cbytes = bsp->msg_cbytes; 270 lsp->msg_qnum = bsp->msg_qnum; 271 lsp->msg_qbytes = bsp->msg_qbytes; 272 lsp->msg_lspid = bsp->msg_lspid; 273 lsp->msg_lrpid = bsp->msg_lrpid; 274 lsp->msg_stime = bsp->msg_stime; 275 lsp->msg_rtime = bsp->msg_rtime; 276 lsp->msg_ctime = bsp->msg_ctime; 277 } 278 279 static int 280 linux_ipc64_perm_to_ipc_perm(struct l_ipc64_perm *in, struct l_ipc_perm *out) 281 { 282 283 out->key = in->key; 284 out->uid = in->uid; 285 out->gid = in->gid; 286 out->cuid = in->cuid; 287 out->cgid = in->cgid; 288 out->mode = in->mode; 289 out->seq = in->seq; 290 291 /* Linux does not check overflow */ 292 if (out->uid != in->uid || out->gid != in->gid || 293 out->cuid != in->cuid || out->cgid != in->cgid || 294 out->mode != in->mode) 295 return (EOVERFLOW); 296 else 297 return (0); 298 } 299 300 static int 301 linux_msqid_pullup(l_int ver, struct l_msqid64_ds *linux_msqid64, caddr_t uaddr) 302 { 303 struct l_msqid_ds linux_msqid; 304 int error; 305 306 if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64)) 307 return (copyin(uaddr, linux_msqid64, sizeof(*linux_msqid64))); 308 309 error = copyin(uaddr, &linux_msqid, sizeof(linux_msqid)); 310 if (error != 0) 311 return (error); 312 313 bzero(linux_msqid64, sizeof(*linux_msqid64)); 314 linux_msqid64->msg_perm.uid = linux_msqid.msg_perm.uid; 315 linux_msqid64->msg_perm.gid = linux_msqid.msg_perm.gid; 316 linux_msqid64->msg_perm.mode = linux_msqid.msg_perm.mode; 317 if (linux_msqid.msg_qbytes == 0) 318 linux_msqid64->msg_qbytes = linux_msqid.msg_lqbytes; 319 else 320 linux_msqid64->msg_qbytes = linux_msqid.msg_qbytes; 321 return (0); 322 } 323 324 static int 325 linux_msqid_pushdown(l_int ver, struct l_msqid64_ds *linux_msqid64, caddr_t uaddr) 326 { 327 struct l_msqid_ds linux_msqid; 328 int error; 329 330 if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64)) 331 return (copyout(linux_msqid64, uaddr, sizeof(*linux_msqid64))); 332 333 bzero(&linux_msqid, sizeof(linux_msqid)); 334 error = linux_ipc64_perm_to_ipc_perm(&linux_msqid64->msg_perm, 335 &linux_msqid.msg_perm); 336 if (error != 0) 337 return (error); 338 339 linux_msqid.msg_stime = linux_msqid64->msg_stime; 340 linux_msqid.msg_rtime = linux_msqid64->msg_rtime; 341 linux_msqid.msg_ctime = linux_msqid64->msg_ctime; 342 343 if (linux_msqid64->msg_cbytes > USHRT_MAX) 344 linux_msqid.msg_cbytes = USHRT_MAX; 345 else 346 linux_msqid.msg_cbytes = linux_msqid64->msg_cbytes; 347 linux_msqid.msg_lcbytes = linux_msqid64->msg_cbytes; 348 if (linux_msqid64->msg_qnum > USHRT_MAX) 349 linux_msqid.msg_qnum = USHRT_MAX; 350 else 351 linux_msqid.msg_qnum = linux_msqid64->msg_qnum; 352 if (linux_msqid64->msg_qbytes > USHRT_MAX) 353 linux_msqid.msg_qbytes = USHRT_MAX; 354 else 355 linux_msqid.msg_qbytes = linux_msqid64->msg_qbytes; 356 linux_msqid.msg_lqbytes = linux_msqid64->msg_qbytes; 357 linux_msqid.msg_lspid = linux_msqid64->msg_lspid; 358 linux_msqid.msg_lrpid = linux_msqid64->msg_lrpid; 359 360 /* Linux does not check overflow */ 361 if (linux_msqid.msg_stime != linux_msqid64->msg_stime || 362 linux_msqid.msg_rtime != linux_msqid64->msg_rtime || 363 linux_msqid.msg_ctime != linux_msqid64->msg_ctime) 364 return (EOVERFLOW); 365 return (copyout(&linux_msqid, uaddr, sizeof(linux_msqid))); 366 } 367 368 static int 369 linux_semid_pullup(l_int ver, struct l_semid64_ds *linux_semid64, caddr_t uaddr) 370 { 371 struct l_semid_ds linux_semid; 372 int error; 373 374 if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64)) 375 return (copyin(uaddr, linux_semid64, sizeof(*linux_semid64))); 376 error = copyin(uaddr, &linux_semid, sizeof(linux_semid)); 377 if (error != 0) 378 return (error); 379 380 bzero(linux_semid64, sizeof(*linux_semid64)); 381 linux_semid64->sem_perm.uid = linux_semid.sem_perm.uid; 382 linux_semid64->sem_perm.gid = linux_semid.sem_perm.gid; 383 linux_semid64->sem_perm.mode = linux_semid.sem_perm.mode; 384 return (0); 385 } 386 387 static int 388 linux_semid_pushdown(l_int ver, struct l_semid64_ds *linux_semid64, caddr_t uaddr) 389 { 390 struct l_semid_ds linux_semid; 391 int error; 392 393 if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64)) 394 return (copyout(linux_semid64, uaddr, sizeof(*linux_semid64))); 395 396 bzero(&linux_semid, sizeof(linux_semid)); 397 error = linux_ipc64_perm_to_ipc_perm(&linux_semid64->sem_perm, 398 &linux_semid.sem_perm); 399 if (error != 0) 400 return (error); 401 402 linux_semid.sem_otime = linux_semid64->sem_otime; 403 linux_semid.sem_ctime = linux_semid64->sem_ctime; 404 linux_semid.sem_nsems = linux_semid64->sem_nsems; 405 406 /* Linux does not check overflow */ 407 if (linux_semid.sem_otime != linux_semid64->sem_otime || 408 linux_semid.sem_ctime != linux_semid64->sem_ctime || 409 linux_semid.sem_nsems != linux_semid64->sem_nsems) 410 return (EOVERFLOW); 411 return (copyout(&linux_semid, uaddr, sizeof(linux_semid))); 412 } 413 414 static int 415 linux_shmid_pullup(l_int ver, struct l_shmid64_ds *linux_shmid64, caddr_t uaddr) 416 { 417 struct l_shmid_ds linux_shmid; 418 int error; 419 420 if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64)) 421 return (copyin(uaddr, linux_shmid64, sizeof(*linux_shmid64))); 422 423 error = copyin(uaddr, &linux_shmid, sizeof(linux_shmid)); 424 if (error != 0) 425 return (error); 426 427 bzero(linux_shmid64, sizeof(*linux_shmid64)); 428 linux_shmid64->shm_perm.uid = linux_shmid.shm_perm.uid; 429 linux_shmid64->shm_perm.gid = linux_shmid.shm_perm.gid; 430 linux_shmid64->shm_perm.mode = linux_shmid.shm_perm.mode; 431 return (0); 432 } 433 434 static int 435 linux_shmid_pushdown(l_int ver, struct l_shmid64_ds *linux_shmid64, caddr_t uaddr) 436 { 437 struct l_shmid_ds linux_shmid; 438 int error; 439 440 if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64)) 441 return (copyout(linux_shmid64, uaddr, sizeof(*linux_shmid64))); 442 443 bzero(&linux_shmid, sizeof(linux_shmid)); 444 error = linux_ipc64_perm_to_ipc_perm(&linux_shmid64->shm_perm, 445 &linux_shmid.shm_perm); 446 if (error != 0) 447 return (error); 448 449 linux_shmid.shm_segsz = linux_shmid64->shm_segsz; 450 linux_shmid.shm_atime = linux_shmid64->shm_atime; 451 linux_shmid.shm_dtime = linux_shmid64->shm_dtime; 452 linux_shmid.shm_ctime = linux_shmid64->shm_ctime; 453 linux_shmid.shm_cpid = linux_shmid64->shm_cpid; 454 linux_shmid.shm_lpid = linux_shmid64->shm_lpid; 455 linux_shmid.shm_nattch = linux_shmid64->shm_nattch; 456 457 /* Linux does not check overflow */ 458 if (linux_shmid.shm_segsz != linux_shmid64->shm_segsz || 459 linux_shmid.shm_atime != linux_shmid64->shm_atime || 460 linux_shmid.shm_dtime != linux_shmid64->shm_dtime || 461 linux_shmid.shm_ctime != linux_shmid64->shm_ctime || 462 linux_shmid.shm_cpid != linux_shmid64->shm_cpid || 463 linux_shmid.shm_lpid != linux_shmid64->shm_lpid || 464 linux_shmid.shm_nattch != linux_shmid64->shm_nattch) 465 return (EOVERFLOW); 466 return (copyout(&linux_shmid, uaddr, sizeof(linux_shmid))); 467 } 468 469 static int 470 linux_shminfo_pushdown(l_int ver, struct l_shminfo64 *linux_shminfo64, 471 caddr_t uaddr) 472 { 473 struct l_shminfo linux_shminfo; 474 475 if (ver == LINUX_IPC_64 || SV_CURPROC_FLAG(SV_LP64)) 476 return (copyout(linux_shminfo64, uaddr, 477 sizeof(*linux_shminfo64))); 478 479 bzero(&linux_shminfo, sizeof(linux_shminfo)); 480 linux_shminfo.shmmax = linux_shminfo64->shmmax; 481 linux_shminfo.shmmin = linux_shminfo64->shmmin; 482 linux_shminfo.shmmni = linux_shminfo64->shmmni; 483 linux_shminfo.shmseg = linux_shminfo64->shmseg; 484 linux_shminfo.shmall = linux_shminfo64->shmall; 485 return (copyout(&linux_shminfo, uaddr, sizeof(linux_shminfo))); 486 } 487 488 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 489 int 490 linux_semtimedop_time64(struct thread *td, struct linux_semtimedop_time64_args *args) 491 { 492 struct timespec ts, *tsa; 493 int error; 494 495 if (args->timeout) { 496 error = linux_get_timespec64(&ts, args->timeout); 497 if (error != 0) 498 return (error); 499 tsa = &ts; 500 } else 501 tsa = NULL; 502 503 return (kern_semop(td, args->semid, PTRIN(args->tsops), 504 args->nsops, tsa)); 505 } 506 #endif /* __i386__) || (__amd64__ && COMPAT_LINUX32) */ 507 508 int 509 linux_semtimedop(struct thread *td, struct linux_semtimedop_args *args) 510 { 511 struct timespec ts, *tsa; 512 int error; 513 514 if (args->timeout) { 515 error = linux_get_timespec(&ts, args->timeout); 516 if (error != 0) 517 return (error); 518 tsa = &ts; 519 } else 520 tsa = NULL; 521 522 return (kern_semop(td, args->semid, PTRIN(args->tsops), 523 args->nsops, tsa)); 524 } 525 526 int 527 linux_semget(struct thread *td, struct linux_semget_args *args) 528 { 529 struct semget_args bsd_args = { 530 .key = args->key, 531 .nsems = args->nsems, 532 .semflg = args->semflg 533 }; 534 535 if (args->nsems < 0) 536 return (EINVAL); 537 return (sys_semget(td, &bsd_args)); 538 } 539 540 int 541 linux_semctl(struct thread *td, struct linux_semctl_args *args) 542 { 543 struct l_semid64_ds linux_semid64; 544 struct l_seminfo linux_seminfo; 545 struct semid_ds semid; 546 union semun semun; 547 register_t rval; 548 int cmd, error; 549 550 memset(&linux_seminfo, 0, sizeof(linux_seminfo)); 551 memset(&linux_semid64, 0, sizeof(linux_semid64)); 552 553 switch (args->cmd & ~LINUX_IPC_64) { 554 case LINUX_IPC_RMID: 555 cmd = IPC_RMID; 556 break; 557 case LINUX_GETNCNT: 558 cmd = GETNCNT; 559 break; 560 case LINUX_GETPID: 561 cmd = GETPID; 562 break; 563 case LINUX_GETVAL: 564 cmd = GETVAL; 565 break; 566 case LINUX_GETZCNT: 567 cmd = GETZCNT; 568 break; 569 case LINUX_SETVAL: 570 cmd = SETVAL; 571 semun.val = args->arg.val; 572 break; 573 case LINUX_IPC_SET: 574 cmd = IPC_SET; 575 error = linux_semid_pullup(args->cmd & LINUX_IPC_64, 576 &linux_semid64, PTRIN(args->arg.buf)); 577 if (error != 0) 578 return (error); 579 linux_to_bsd_semid_ds(&linux_semid64, &semid); 580 semun.buf = &semid; 581 return (kern_semctl(td, args->semid, args->semnum, cmd, &semun, 582 td->td_retval)); 583 case LINUX_IPC_STAT: 584 cmd = IPC_STAT; 585 semun.buf = &semid; 586 error = kern_semctl(td, args->semid, args->semnum, cmd, &semun, 587 &rval); 588 if (error != 0) 589 return (error); 590 bsd_to_linux_semid_ds(&semid, &linux_semid64); 591 return (linux_semid_pushdown(args->cmd & LINUX_IPC_64, 592 &linux_semid64, PTRIN(args->arg.buf))); 593 case LINUX_SEM_STAT: 594 cmd = SEM_STAT; 595 semun.buf = &semid; 596 error = kern_semctl(td, args->semid, args->semnum, cmd, &semun, 597 &rval); 598 if (error != 0) 599 return (error); 600 bsd_to_linux_semid_ds(&semid, &linux_semid64); 601 error = linux_semid_pushdown(args->cmd & LINUX_IPC_64, 602 &linux_semid64, PTRIN(args->arg.buf)); 603 if (error == 0) 604 td->td_retval[0] = rval; 605 return (error); 606 case LINUX_IPC_INFO: 607 case LINUX_SEM_INFO: 608 bcopy(&seminfo, &linux_seminfo.semmni, sizeof(linux_seminfo) - 609 sizeof(linux_seminfo.semmap) ); 610 /* 611 * Linux does not use the semmap field but populates it with 612 * the defined value from SEMMAP, which really is redefined to 613 * SEMMNS, which they define as SEMMNI * SEMMSL. Try to 614 * simulate this returning our dynamic semmns value. 615 */ 616 linux_seminfo.semmap = linux_seminfo.semmns; 617 /* XXX BSD equivalent? 618 #define used_semids 10 619 #define used_sems 10 620 linux_seminfo.semusz = used_semids; 621 linux_seminfo.semaem = used_sems; 622 */ 623 error = copyout(&linux_seminfo, 624 PTRIN(args->arg.buf), sizeof(linux_seminfo)); 625 if (error != 0) 626 return (error); 627 /* 628 * TODO: Linux return the last assigned id, not the semmni. 629 */ 630 td->td_retval[0] = seminfo.semmni; 631 return (0); 632 case LINUX_GETALL: 633 cmd = GETALL; 634 semun.array = PTRIN(args->arg.array); 635 break; 636 case LINUX_SETALL: 637 cmd = SETALL; 638 semun.array = PTRIN(args->arg.array); 639 break; 640 default: 641 linux_msg(td, "ipc type %d is not implemented", 642 args->cmd & ~LINUX_IPC_64); 643 return (EINVAL); 644 } 645 return (kern_semctl(td, args->semid, args->semnum, cmd, &semun, 646 td->td_retval)); 647 } 648 649 int 650 linux_msgsnd(struct thread *td, struct linux_msgsnd_args *args) 651 { 652 const void *msgp; 653 long mtype; 654 l_long lmtype; 655 int error; 656 657 if ((l_long)args->msgsz < 0 || args->msgsz > (l_long)msginfo.msgmax) 658 return (EINVAL); 659 msgp = PTRIN(args->msgp); 660 if ((error = copyin(msgp, &lmtype, sizeof(lmtype))) != 0) 661 return (error); 662 mtype = (long)lmtype; 663 return (kern_msgsnd(td, args->msqid, 664 (const char *)msgp + sizeof(lmtype), 665 args->msgsz, args->msgflg, mtype)); 666 } 667 668 int 669 linux_msgrcv(struct thread *td, struct linux_msgrcv_args *args) 670 { 671 void *msgp; 672 long mtype; 673 l_long lmtype; 674 int error; 675 676 if ((l_long)args->msgsz < 0 || args->msgsz > (l_long)msginfo.msgmax) 677 return (EINVAL); 678 msgp = PTRIN(args->msgp); 679 if ((error = kern_msgrcv(td, args->msqid, 680 (char *)msgp + sizeof(lmtype), args->msgsz, 681 args->msgtyp, args->msgflg, &mtype)) != 0) 682 return (error); 683 lmtype = (l_long)mtype; 684 return (copyout(&lmtype, msgp, sizeof(lmtype))); 685 } 686 687 int 688 linux_msgget(struct thread *td, struct linux_msgget_args *args) 689 { 690 struct msgget_args bsd_args = { 691 .key = args->key, 692 .msgflg = args->msgflg 693 }; 694 695 return (sys_msgget(td, &bsd_args)); 696 } 697 698 int 699 linux_msgctl(struct thread *td, struct linux_msgctl_args *args) 700 { 701 int error, bsd_cmd; 702 struct l_msqid64_ds linux_msqid64; 703 struct msqid_ds bsd_msqid; 704 705 memset(&linux_msqid64, 0, sizeof(linux_msqid64)); 706 707 bsd_cmd = args->cmd & ~LINUX_IPC_64; 708 switch (bsd_cmd) { 709 case LINUX_IPC_INFO: 710 case LINUX_MSG_INFO: { 711 struct l_msginfo linux_msginfo; 712 713 memset(&linux_msginfo, 0, sizeof(linux_msginfo)); 714 /* 715 * XXX MSG_INFO uses the same data structure but returns different 716 * dynamic counters in msgpool, msgmap, and msgtql fields. 717 */ 718 linux_msginfo.msgpool = (long)msginfo.msgmni * 719 (long)msginfo.msgmnb / 1024L; /* XXX MSG_INFO. */ 720 linux_msginfo.msgmap = msginfo.msgmnb; /* XXX MSG_INFO. */ 721 linux_msginfo.msgmax = msginfo.msgmax; 722 linux_msginfo.msgmnb = msginfo.msgmnb; 723 linux_msginfo.msgmni = msginfo.msgmni; 724 linux_msginfo.msgssz = msginfo.msgssz; 725 linux_msginfo.msgtql = msginfo.msgtql; /* XXX MSG_INFO. */ 726 linux_msginfo.msgseg = msginfo.msgseg; 727 error = copyout(&linux_msginfo, PTRIN(args->buf), 728 sizeof(linux_msginfo)); 729 if (error == 0) 730 td->td_retval[0] = msginfo.msgmni; /* XXX */ 731 732 return (error); 733 } 734 735 /* 736 * TODO: implement this 737 * case LINUX_MSG_STAT: 738 */ 739 case LINUX_IPC_STAT: 740 /* NOTHING */ 741 break; 742 743 case LINUX_IPC_SET: 744 error = linux_msqid_pullup(args->cmd & LINUX_IPC_64, 745 &linux_msqid64, PTRIN(args->buf)); 746 if (error != 0) 747 return (error); 748 linux_to_bsd_msqid_ds(&linux_msqid64, &bsd_msqid); 749 break; 750 751 case LINUX_IPC_RMID: 752 /* NOTHING */ 753 break; 754 755 default: 756 return (EINVAL); 757 break; 758 } 759 760 error = kern_msgctl(td, args->msqid, bsd_cmd, &bsd_msqid); 761 if (error != 0) { 762 if (bsd_cmd == LINUX_IPC_RMID && error == EACCES) 763 return (EPERM); 764 if (bsd_cmd != LINUX_IPC_RMID || error != EINVAL) 765 return (error); 766 } 767 768 if (bsd_cmd == LINUX_IPC_STAT) { 769 bsd_to_linux_msqid_ds(&bsd_msqid, &linux_msqid64); 770 return (linux_msqid_pushdown(args->cmd & LINUX_IPC_64, 771 &linux_msqid64, PTRIN(args->buf))); 772 } 773 774 return (0); 775 } 776 777 int 778 linux_shmat(struct thread *td, struct linux_shmat_args *args) 779 { 780 struct shmat_args bsd_args = { 781 .shmid = args->shmid, 782 .shmaddr = PTRIN(args->shmaddr), 783 .shmflg = args->shmflg 784 }; 785 786 return (sys_shmat(td, &bsd_args)); 787 } 788 789 int 790 linux_shmdt(struct thread *td, struct linux_shmdt_args *args) 791 { 792 struct shmdt_args bsd_args = { 793 .shmaddr = PTRIN(args->shmaddr) 794 }; 795 796 return (sys_shmdt(td, &bsd_args)); 797 } 798 799 int 800 linux_shmget(struct thread *td, struct linux_shmget_args *args) 801 { 802 struct shmget_args bsd_args = { 803 .key = args->key, 804 .size = args->size, 805 .shmflg = args->shmflg 806 }; 807 808 return (sys_shmget(td, &bsd_args)); 809 } 810 811 int 812 linux_shmctl(struct thread *td, struct linux_shmctl_args *args) 813 { 814 struct l_shmid64_ds linux_shmid64; 815 struct l_shminfo64 linux_shminfo64; 816 struct l_shm_info linux_shm_info; 817 struct shmid_ds bsd_shmid; 818 int error; 819 820 memset(&linux_shm_info, 0, sizeof(linux_shm_info)); 821 memset(&linux_shmid64, 0, sizeof(linux_shmid64)); 822 memset(&linux_shminfo64, 0, sizeof(linux_shminfo64)); 823 824 switch (args->cmd & ~LINUX_IPC_64) { 825 case LINUX_IPC_INFO: { 826 struct shminfo bsd_shminfo; 827 828 /* Perform shmctl wanting removed segments lookup */ 829 error = kern_shmctl(td, args->shmid, IPC_INFO, 830 (void *)&bsd_shminfo, NULL); 831 if (error != 0) 832 return (error); 833 834 bsd_to_linux_shminfo(&bsd_shminfo, &linux_shminfo64); 835 836 return (linux_shminfo_pushdown(args->cmd & LINUX_IPC_64, 837 &linux_shminfo64, PTRIN(args->buf))); 838 } 839 840 case LINUX_SHM_INFO: { 841 struct shm_info bsd_shm_info; 842 843 /* Perform shmctl wanting removed segments lookup */ 844 error = kern_shmctl(td, args->shmid, SHM_INFO, 845 (void *)&bsd_shm_info, NULL); 846 if (error != 0) 847 return (error); 848 849 bsd_to_linux_shm_info(&bsd_shm_info, &linux_shm_info); 850 851 return (copyout(&linux_shm_info, PTRIN(args->buf), 852 sizeof(struct l_shm_info))); 853 } 854 855 case LINUX_IPC_STAT: 856 /* Perform shmctl wanting removed segments lookup */ 857 error = kern_shmctl(td, args->shmid, IPC_STAT, 858 (void *)&bsd_shmid, NULL); 859 if (error != 0) 860 return (error); 861 862 bsd_to_linux_shmid_ds(&bsd_shmid, &linux_shmid64); 863 864 return (linux_shmid_pushdown(args->cmd & LINUX_IPC_64, 865 &linux_shmid64, PTRIN(args->buf))); 866 867 case LINUX_SHM_STAT: 868 /* Perform shmctl wanting removed segments lookup */ 869 error = kern_shmctl(td, args->shmid, IPC_STAT, 870 (void *)&bsd_shmid, NULL); 871 if (error != 0) 872 return (error); 873 874 bsd_to_linux_shmid_ds(&bsd_shmid, &linux_shmid64); 875 876 return (linux_shmid_pushdown(args->cmd & LINUX_IPC_64, 877 &linux_shmid64, PTRIN(args->buf))); 878 879 case LINUX_IPC_SET: 880 error = linux_shmid_pullup(args->cmd & LINUX_IPC_64, 881 &linux_shmid64, PTRIN(args->buf)); 882 if (error != 0) 883 return (error); 884 885 linux_to_bsd_shmid_ds(&linux_shmid64, &bsd_shmid); 886 887 /* Perform shmctl wanting removed segments lookup */ 888 return (kern_shmctl(td, args->shmid, IPC_SET, 889 (void *)&bsd_shmid, NULL)); 890 891 case LINUX_IPC_RMID: { 892 void *buf; 893 894 if (args->buf == 0) 895 buf = NULL; 896 else { 897 error = linux_shmid_pullup(args->cmd & LINUX_IPC_64, 898 &linux_shmid64, PTRIN(args->buf)); 899 if (error != 0) 900 return (error); 901 linux_to_bsd_shmid_ds(&linux_shmid64, &bsd_shmid); 902 buf = (void *)&bsd_shmid; 903 } 904 return (kern_shmctl(td, args->shmid, IPC_RMID, buf, NULL)); 905 } 906 907 case LINUX_SHM_LOCK: 908 /* FALLTHROUGH */ 909 case LINUX_SHM_UNLOCK: 910 /* FALLTHROUGH */ 911 default: 912 linux_msg(td, "ipc type %d not implemented", 913 args->cmd & ~LINUX_IPC_64); 914 return (EINVAL); 915 } 916 } 917 918 MODULE_DEPEND(linux, sysvmsg, 1, 1, 1); 919 MODULE_DEPEND(linux, sysvsem, 1, 1, 1); 920 MODULE_DEPEND(linux, sysvshm, 1, 1, 1); 921