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