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