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