1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 /* 33 * Inter-Process Communication Message Facility. 34 * 35 * See os/ipc.c for a description of common IPC functionality. 36 * 37 * Resource controls 38 * ----------------- 39 * 40 * Control: zone.max-msg-ids (rc_zone_msgmni) 41 * Description: Maximum number of message queue ids allowed a zone. 42 * 43 * When msgget() is used to allocate a message queue, one id is 44 * allocated. If the id allocation doesn't succeed, msgget() fails 45 * and errno is set to ENOSPC. Upon successful msgctl(, IPC_RMID) 46 * the id is deallocated. 47 * 48 * Control: project.max-msg-ids (rc_project_msgmni) 49 * Description: Maximum number of message queue ids allowed a project. 50 * 51 * When msgget() is used to allocate a message queue, one id is 52 * allocated. If the id allocation doesn't succeed, msgget() fails 53 * and errno is set to ENOSPC. Upon successful msgctl(, IPC_RMID) 54 * the id is deallocated. 55 * 56 * Control: process.max-msg-qbytes (rc_process_msgmnb) 57 * Description: Maximum number of bytes of messages on a message queue. 58 * 59 * When msgget() successfully allocates a message queue, the minimum 60 * enforced value of this limit is used to initialize msg_qbytes. 61 * 62 * Control: process.max-msg-messages (rc_process_msgtql) 63 * Description: Maximum number of messages on a message queue. 64 * 65 * When msgget() successfully allocates a message queue, the minimum 66 * enforced value of this limit is used to initialize a per-queue 67 * limit on the number of messages. 68 */ 69 70 #include <sys/types.h> 71 #include <sys/t_lock.h> 72 #include <sys/param.h> 73 #include <sys/cred.h> 74 #include <sys/user.h> 75 #include <sys/proc.h> 76 #include <sys/time.h> 77 #include <sys/ipc.h> 78 #include <sys/ipc_impl.h> 79 #include <sys/msg.h> 80 #include <sys/msg_impl.h> 81 #include <sys/list.h> 82 #include <sys/systm.h> 83 #include <sys/sysmacros.h> 84 #include <sys/cpuvar.h> 85 #include <sys/kmem.h> 86 #include <sys/ddi.h> 87 #include <sys/errno.h> 88 #include <sys/cmn_err.h> 89 #include <sys/debug.h> 90 #include <sys/project.h> 91 #include <sys/modctl.h> 92 #include <sys/syscall.h> 93 #include <sys/policy.h> 94 #include <sys/zone.h> 95 96 #include <c2/audit.h> 97 98 /* 99 * The following tunables are obsolete. Though for compatibility we 100 * still read and interpret msginfo_msgmnb, msginfo_msgmni, and 101 * msginfo_msgtql (see os/project.c and os/rctl_proc.c), the preferred 102 * mechanism for administrating the IPC Message facility is through the 103 * resource controls described at the top of this file. 104 */ 105 size_t msginfo_msgmax = 2048; /* (obsolete) */ 106 size_t msginfo_msgmnb = 4096; /* (obsolete) */ 107 int msginfo_msgmni = 50; /* (obsolete) */ 108 int msginfo_msgtql = 40; /* (obsolete) */ 109 int msginfo_msgssz = 8; /* (obsolete) */ 110 int msginfo_msgmap = 0; /* (obsolete) */ 111 ushort_t msginfo_msgseg = 1024; /* (obsolete) */ 112 113 extern rctl_hndl_t rc_zone_msgmni; 114 extern rctl_hndl_t rc_project_msgmni; 115 extern rctl_hndl_t rc_process_msgmnb; 116 extern rctl_hndl_t rc_process_msgtql; 117 static ipc_service_t *msq_svc; 118 static zone_key_t msg_zone_key; 119 120 static void msg_dtor(kipc_perm_t *); 121 static void msg_rmid(kipc_perm_t *); 122 static void msg_remove_zone(zoneid_t, void *); 123 124 /* 125 * Module linkage information for the kernel. 126 */ 127 static ssize_t msgsys(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2, 128 uintptr_t a4, uintptr_t a5); 129 130 static struct sysent ipcmsg_sysent = { 131 6, 132 #ifdef _LP64 133 SE_ARGC | SE_NOUNLOAD | SE_64RVAL, 134 #else 135 SE_ARGC | SE_NOUNLOAD | SE_32RVAL1, 136 #endif 137 (int (*)())msgsys 138 }; 139 140 #ifdef _SYSCALL32_IMPL 141 static ssize32_t msgsys32(int opcode, uint32_t a0, uint32_t a1, uint32_t a2, 142 uint32_t a4, uint32_t a5); 143 144 static struct sysent ipcmsg_sysent32 = { 145 6, 146 SE_ARGC | SE_NOUNLOAD | SE_32RVAL1, 147 (int (*)())msgsys32 148 }; 149 #endif /* _SYSCALL32_IMPL */ 150 151 static struct modlsys modlsys = { 152 &mod_syscallops, "System V message facility", &ipcmsg_sysent 153 }; 154 155 #ifdef _SYSCALL32_IMPL 156 static struct modlsys modlsys32 = { 157 &mod_syscallops32, "32-bit System V message facility", &ipcmsg_sysent32 158 }; 159 #endif 160 161 /* 162 * Big Theory statement for message queue correctness 163 * 164 * The msgrcv and msgsnd functions no longer uses cv_broadcast to wake up 165 * receivers who are waiting for an event. Using the cv_broadcast method 166 * resulted in negative scaling when the number of waiting receivers are large 167 * (the thundering herd problem). Instead, the receivers waiting to receive a 168 * message are now linked in a queue-like fashion and awaken one at a time in 169 * a controlled manner. 170 * 171 * Receivers can block on two different classes of waiting list: 172 * 1) "sendwait" list, which is the more complex list of the two. The 173 * receiver will be awakened by a sender posting a new message. There 174 * are two types of "sendwait" list used: 175 * a) msg_wait_snd: handles all receivers who are looking for 176 * a message type >= 0, but was unable to locate a match. 177 * 178 * slot 0: reserved for receivers that have designated they 179 * will take any message type. 180 * rest: consist of receivers requesting a specific type 181 * but the type was not present. The entries are 182 * hashed into a bucket in an attempt to keep 183 * any list search relatively short. 184 * b) msg_wait_snd_ngt: handles all receivers that have designated 185 * a negative message type. Unlike msg_wait_snd, the hash bucket 186 * serves a range of negative message types (-1 to -5, -6 to -10 187 * and so forth), where the last bucket is reserved for all the 188 * negative message types that hash outside of MSG_MAX_QNUM - 1. 189 * This is done this way to simplify the operation of locating a 190 * negative message type. 191 * 192 * 2) "copyout" list, where the receiver is awakened by another 193 * receiver after a message is copied out. This is a linked list 194 * of waiters that are awakened one at a time. Although the solution is 195 * not optimal, the complexity that would be added in for waking 196 * up the right entry far exceeds any potential pay back (too many 197 * correctness and corner case issues). 198 * 199 * The lists are doubly linked. In the case of the "sendwait" 200 * list, this allows the thread to remove itself from the list without having 201 * to traverse the list. In the case of the "copyout" list it simply allows 202 * us to use common functions with the "sendwait" list. 203 * 204 * To make sure receivers are not hung out to dry, we must guarantee: 205 * 1. If any queued message matches any receiver, then at least one 206 * matching receiver must be processing the request. 207 * 2. Blocking on the copyout queue is only temporary while messages 208 * are being copied out. The process is guaranted to wakeup 209 * when it gets to front of the queue (copyout is a FIFO). 210 * 211 * Rules for blocking and waking up: 212 * 1. A receiver entering msgrcv must examine all messages for a match 213 * before blocking on a sendwait queue. 214 * 2. If the receiver blocks because the message it chose is already 215 * being copied out, then when it wakes up needs to start start 216 * checking the messages from the beginning. 217 * 3) When ever a process returns from msgrcv for any reason, if it 218 * had attempted to copy a message or blocked waiting for a copy 219 * to complete it needs to wakeup the next receiver blocked on 220 * a copy out. 221 * 4) When a message is sent, the sender selects a process waiting 222 * for that type of message. This selection process rotates between 223 * receivers types of 0, negative and positive to prevent starvation of 224 * any one particular receiver type. 225 * 5) The following are the scenarios for processes that are awakened 226 * by a msgsnd: 227 * a) The process finds the message and is able to copy 228 * it out. Once complete, the process returns. 229 * b) The message that was sent that triggered the wakeup is no 230 * longer available (another process found the message first). 231 * We issue a wakeup on copy queue and then go back to 232 * sleep waiting for another matching message to be sent. 233 * c) The message that was supposed to be processed was 234 * already serviced by another process. However a different 235 * message is present which we can service. The message 236 * is copied and the process returns. 237 * d) The message is found, but some sort of error occurs that 238 * prevents the message from being copied. The receiver 239 * wakes up the next sender that can service this message 240 * type and returns an error to the caller. 241 * e) The message is found, but it is marked as being copied 242 * out. The receiver then goes to sleep on the copyout 243 * queue where it will be awakened again sometime in the future. 244 * 245 * 246 * 6) Whenever a message is found that matches the message type designated, 247 * but is being copied out we have to block on the copyout queue. 248 * After process copying finishes the copy out, it must wakeup (either 249 * directly or indirectly) all receivers who blocked on its copyout, 250 * so they are guaranteed a chance to examine the remaining messages. 251 * This is implemented via a chain of wakeups: Y wakes X, who wakes Z, 252 * and so on. The chain cannot be broken. This leads to the following 253 * cases: 254 * a) A receiver is finished copying the message (or encountered) 255 * an error), the first entry on the copyout queue is woken 256 * up. 257 * b) When the receiver is woken up, it attempts to locate 258 * a message type match. 259 * c) If a message type is found and 260 * -- MSG_RCVCOPY flag is not set, the message is 261 * marked for copying out. Regardless of the copyout 262 * success the next entry on the copyout queue is 263 * awakened and the operation is completed. 264 * -- MSG_RCVCOPY is set, we simply go back to sleep again 265 * on the copyout queue. 266 * d) If the message type is not found then we wakeup the next 267 * process on the copyout queue. 268 */ 269 270 static uint_t msg_type_hash(long); 271 static int msgq_check_err(kmsqid_t *qp, int cvres); 272 static int msg_rcvq_sleep(list_t *, msgq_wakeup_t *, kmutex_t **, 273 kmsqid_t *); 274 static int msg_copyout(kmsqid_t *, long, kmutex_t **, size_t *, size_t, 275 struct msg *, struct ipcmsgbuf *, int); 276 static void msg_rcvq_wakeup_all(list_t *); 277 static void msg_wakeup_rdr(kmsqid_t *, msg_select_t **, long); 278 static msgq_wakeup_t *msg_fnd_any_snd(kmsqid_t *, int, long); 279 static msgq_wakeup_t *msg_fnd_any_rdr(kmsqid_t *, int, long); 280 static msgq_wakeup_t *msg_fnd_neg_snd(kmsqid_t *, int, long); 281 static msgq_wakeup_t *msg_fnd_spc_snd(kmsqid_t *, int, long); 282 static struct msg *msgrcv_lookup(kmsqid_t *, long); 283 284 msg_select_t msg_fnd_sndr[] = { 285 { msg_fnd_any_snd, &msg_fnd_sndr[1] }, 286 { msg_fnd_spc_snd, &msg_fnd_sndr[2] }, 287 { msg_fnd_neg_snd, &msg_fnd_sndr[0] } 288 }; 289 290 msg_select_t msg_fnd_rdr[1] = { 291 { msg_fnd_any_rdr, &msg_fnd_rdr[0] }, 292 }; 293 294 static struct modlinkage modlinkage = { 295 MODREV_1, 296 &modlsys, 297 #ifdef _SYSCALL32_IMPL 298 &modlsys32, 299 #endif 300 NULL 301 }; 302 303 304 int 305 _init(void) 306 { 307 int result; 308 309 msq_svc = ipcs_create("msqids", rc_project_msgmni, rc_zone_msgmni, 310 sizeof (kmsqid_t), msg_dtor, msg_rmid, AT_IPC_MSG, 311 offsetof(ipc_rqty_t, ipcq_msgmni)); 312 zone_key_create(&msg_zone_key, NULL, msg_remove_zone, NULL); 313 314 if ((result = mod_install(&modlinkage)) == 0) 315 return (0); 316 317 (void) zone_key_delete(msg_zone_key); 318 ipcs_destroy(msq_svc); 319 320 return (result); 321 } 322 323 int 324 _fini(void) 325 { 326 return (EBUSY); 327 } 328 329 int 330 _info(struct modinfo *modinfop) 331 { 332 return (mod_info(&modlinkage, modinfop)); 333 } 334 335 static void 336 msg_dtor(kipc_perm_t *perm) 337 { 338 kmsqid_t *qp = (kmsqid_t *)perm; 339 int ii; 340 341 for (ii = 0; ii <= MSG_MAX_QNUM; ii++) { 342 ASSERT(list_is_empty(&qp->msg_wait_snd[ii])); 343 ASSERT(list_is_empty(&qp->msg_wait_snd_ngt[ii])); 344 list_destroy(&qp->msg_wait_snd[ii]); 345 list_destroy(&qp->msg_wait_snd_ngt[ii]); 346 } 347 ASSERT(list_is_empty(&qp->msg_cpy_block)); 348 list_destroy(&qp->msg_cpy_block); 349 ASSERT(qp->msg_snd_cnt == 0); 350 ASSERT(qp->msg_cbytes == 0); 351 list_destroy(&qp->msg_list); 352 } 353 354 355 #define msg_hold(mp) (mp)->msg_copycnt++ 356 357 /* 358 * msg_rele - decrement the reference count on the message. When count 359 * reaches zero, free message header and contents. 360 */ 361 static void 362 msg_rele(struct msg *mp) 363 { 364 ASSERT(mp->msg_copycnt > 0); 365 if (mp->msg_copycnt-- == 1) { 366 if (mp->msg_addr) 367 kmem_free(mp->msg_addr, mp->msg_size); 368 kmem_free(mp, sizeof (struct msg)); 369 } 370 } 371 372 /* 373 * msgunlink - Unlink msg from queue, decrement byte count and wake up anyone 374 * waiting for free bytes on queue. 375 * 376 * Called with queue locked. 377 */ 378 static void 379 msgunlink(kmsqid_t *qp, struct msg *mp) 380 { 381 list_remove(&qp->msg_list, mp); 382 qp->msg_qnum--; 383 qp->msg_cbytes -= mp->msg_size; 384 msg_rele(mp); 385 386 /* Wake up waiting writers */ 387 if (qp->msg_snd_cnt) 388 cv_broadcast(&qp->msg_snd_cv); 389 } 390 391 static void 392 msg_rmid(kipc_perm_t *perm) 393 { 394 kmsqid_t *qp = (kmsqid_t *)perm; 395 struct msg *mp; 396 int ii; 397 398 399 while ((mp = list_head(&qp->msg_list)) != NULL) 400 msgunlink(qp, mp); 401 ASSERT(qp->msg_cbytes == 0); 402 403 /* 404 * Wake up everyone who is in a wait state of some sort 405 * for this message queue. 406 */ 407 for (ii = 0; ii <= MSG_MAX_QNUM; ii++) { 408 msg_rcvq_wakeup_all(&qp->msg_wait_snd[ii]); 409 msg_rcvq_wakeup_all(&qp->msg_wait_snd_ngt[ii]); 410 } 411 msg_rcvq_wakeup_all(&qp->msg_cpy_block); 412 if (qp->msg_snd_cnt) 413 cv_broadcast(&qp->msg_snd_cv); 414 } 415 416 /* 417 * msgctl system call. 418 * 419 * gets q lock (via ipc_lookup), releases before return. 420 * may call users of msg_lock 421 */ 422 static int 423 msgctl(int msgid, int cmd, void *arg) 424 { 425 STRUCT_DECL(msqid_ds, ds); /* SVR4 queue work area */ 426 kmsqid_t *qp; /* ptr to associated q */ 427 int error; 428 struct cred *cr; 429 model_t mdl = get_udatamodel(); 430 struct msqid_ds64 ds64; 431 kmutex_t *lock; 432 proc_t *pp = curproc; 433 434 STRUCT_INIT(ds, mdl); 435 cr = CRED(); 436 437 /* 438 * Perform pre- or non-lookup actions (e.g. copyins, RMID). 439 */ 440 switch (cmd) { 441 case IPC_SET: 442 if (copyin(arg, STRUCT_BUF(ds), STRUCT_SIZE(ds))) 443 return (set_errno(EFAULT)); 444 break; 445 446 case IPC_SET64: 447 if (copyin(arg, &ds64, sizeof (struct msqid_ds64))) 448 return (set_errno(EFAULT)); 449 break; 450 451 case IPC_RMID: 452 if (error = ipc_rmid(msq_svc, msgid, cr)) 453 return (set_errno(error)); 454 return (0); 455 } 456 457 /* 458 * get msqid_ds for this msgid 459 */ 460 if ((lock = ipc_lookup(msq_svc, msgid, (kipc_perm_t **)&qp)) == NULL) 461 return (set_errno(EINVAL)); 462 463 switch (cmd) { 464 case IPC_SET: 465 if (STRUCT_FGET(ds, msg_qbytes) > qp->msg_qbytes && 466 secpolicy_ipc_config(cr) != 0) { 467 mutex_exit(lock); 468 return (set_errno(EPERM)); 469 } 470 if (error = ipcperm_set(msq_svc, cr, &qp->msg_perm, 471 &STRUCT_BUF(ds)->msg_perm, mdl)) { 472 mutex_exit(lock); 473 return (set_errno(error)); 474 } 475 qp->msg_qbytes = STRUCT_FGET(ds, msg_qbytes); 476 qp->msg_ctime = gethrestime_sec(); 477 break; 478 479 case IPC_STAT: 480 if (error = ipcperm_access(&qp->msg_perm, MSG_R, cr)) { 481 mutex_exit(lock); 482 return (set_errno(error)); 483 } 484 485 if (qp->msg_rcv_cnt) 486 qp->msg_perm.ipc_mode |= MSG_RWAIT; 487 if (qp->msg_snd_cnt) 488 qp->msg_perm.ipc_mode |= MSG_WWAIT; 489 ipcperm_stat(&STRUCT_BUF(ds)->msg_perm, &qp->msg_perm, mdl); 490 qp->msg_perm.ipc_mode &= ~(MSG_RWAIT|MSG_WWAIT); 491 STRUCT_FSETP(ds, msg_first, NULL); /* kernel addr */ 492 STRUCT_FSETP(ds, msg_last, NULL); 493 STRUCT_FSET(ds, msg_cbytes, qp->msg_cbytes); 494 STRUCT_FSET(ds, msg_qnum, qp->msg_qnum); 495 STRUCT_FSET(ds, msg_qbytes, qp->msg_qbytes); 496 STRUCT_FSET(ds, msg_lspid, qp->msg_lspid); 497 STRUCT_FSET(ds, msg_lrpid, qp->msg_lrpid); 498 STRUCT_FSET(ds, msg_stime, qp->msg_stime); 499 STRUCT_FSET(ds, msg_rtime, qp->msg_rtime); 500 STRUCT_FSET(ds, msg_ctime, qp->msg_ctime); 501 break; 502 503 case IPC_SET64: 504 mutex_enter(&pp->p_lock); 505 if ((ds64.msgx_qbytes > qp->msg_qbytes) && 506 secpolicy_ipc_config(cr) != 0 && 507 rctl_test(rc_process_msgmnb, pp->p_rctls, pp, 508 ds64.msgx_qbytes, RCA_SAFE) & RCT_DENY) { 509 mutex_exit(&pp->p_lock); 510 mutex_exit(lock); 511 return (set_errno(EPERM)); 512 } 513 mutex_exit(&pp->p_lock); 514 if (error = ipcperm_set64(msq_svc, cr, &qp->msg_perm, 515 &ds64.msgx_perm)) { 516 mutex_exit(lock); 517 return (set_errno(error)); 518 } 519 qp->msg_qbytes = ds64.msgx_qbytes; 520 qp->msg_ctime = gethrestime_sec(); 521 break; 522 523 case IPC_STAT64: 524 if (qp->msg_rcv_cnt) 525 qp->msg_perm.ipc_mode |= MSG_RWAIT; 526 if (qp->msg_snd_cnt) 527 qp->msg_perm.ipc_mode |= MSG_WWAIT; 528 ipcperm_stat64(&ds64.msgx_perm, &qp->msg_perm); 529 qp->msg_perm.ipc_mode &= ~(MSG_RWAIT|MSG_WWAIT); 530 ds64.msgx_cbytes = qp->msg_cbytes; 531 ds64.msgx_qnum = qp->msg_qnum; 532 ds64.msgx_qbytes = qp->msg_qbytes; 533 ds64.msgx_lspid = qp->msg_lspid; 534 ds64.msgx_lrpid = qp->msg_lrpid; 535 ds64.msgx_stime = qp->msg_stime; 536 ds64.msgx_rtime = qp->msg_rtime; 537 ds64.msgx_ctime = qp->msg_ctime; 538 break; 539 540 default: 541 mutex_exit(lock); 542 return (set_errno(EINVAL)); 543 } 544 545 mutex_exit(lock); 546 547 /* 548 * Do copyout last (after releasing mutex). 549 */ 550 switch (cmd) { 551 case IPC_STAT: 552 if (copyout(STRUCT_BUF(ds), arg, STRUCT_SIZE(ds))) 553 return (set_errno(EFAULT)); 554 break; 555 556 case IPC_STAT64: 557 if (copyout(&ds64, arg, sizeof (struct msqid_ds64))) 558 return (set_errno(EFAULT)); 559 break; 560 } 561 562 return (0); 563 } 564 565 /* 566 * Remove all message queues associated with a given zone. Called by 567 * zone_shutdown when the zone is halted. 568 */ 569 /*ARGSUSED1*/ 570 static void 571 msg_remove_zone(zoneid_t zoneid, void *arg) 572 { 573 ipc_remove_zone(msq_svc, zoneid); 574 } 575 576 /* 577 * msgget system call. 578 */ 579 static int 580 msgget(key_t key, int msgflg) 581 { 582 kmsqid_t *qp; 583 kmutex_t *lock; 584 int id, error; 585 int ii; 586 proc_t *pp = curproc; 587 588 top: 589 if (error = ipc_get(msq_svc, key, msgflg, (kipc_perm_t **)&qp, &lock)) 590 return (set_errno(error)); 591 592 if (IPC_FREE(&qp->msg_perm)) { 593 mutex_exit(lock); 594 mutex_exit(&pp->p_lock); 595 596 list_create(&qp->msg_list, sizeof (struct msg), 597 offsetof(struct msg, msg_node)); 598 qp->msg_qnum = 0; 599 qp->msg_lspid = qp->msg_lrpid = 0; 600 qp->msg_stime = qp->msg_rtime = 0; 601 qp->msg_ctime = gethrestime_sec(); 602 qp->msg_ngt_cnt = 0; 603 qp->msg_neg_copy = 0; 604 for (ii = 0; ii <= MSG_MAX_QNUM; ii++) { 605 list_create(&qp->msg_wait_snd[ii], 606 sizeof (msgq_wakeup_t), 607 offsetof(msgq_wakeup_t, msgw_list)); 608 list_create(&qp->msg_wait_snd_ngt[ii], 609 sizeof (msgq_wakeup_t), 610 offsetof(msgq_wakeup_t, msgw_list)); 611 } 612 /* 613 * The proper initialization of msg_lowest_type is to the 614 * highest possible value. By doing this we guarantee that 615 * when the first send happens, the lowest type will be set 616 * properly. 617 */ 618 qp->msg_lowest_type = LONG_MAX; 619 list_create(&qp->msg_cpy_block, 620 sizeof (msgq_wakeup_t), 621 offsetof(msgq_wakeup_t, msgw_list)); 622 qp->msg_fnd_sndr = &msg_fnd_sndr[0]; 623 qp->msg_fnd_rdr = &msg_fnd_rdr[0]; 624 qp->msg_rcv_cnt = 0; 625 qp->msg_snd_cnt = 0; 626 627 if (error = ipc_commit_begin(msq_svc, key, msgflg, 628 (kipc_perm_t *)qp)) { 629 if (error == EAGAIN) 630 goto top; 631 return (set_errno(error)); 632 } 633 qp->msg_qbytes = rctl_enforced_value(rc_process_msgmnb, 634 pp->p_rctls, pp); 635 qp->msg_qmax = rctl_enforced_value(rc_process_msgtql, 636 pp->p_rctls, pp); 637 lock = ipc_commit_end(msq_svc, &qp->msg_perm); 638 } 639 if (audit_active) 640 audit_ipcget(AT_IPC_MSG, (void *)qp); 641 id = qp->msg_perm.ipc_id; 642 mutex_exit(lock); 643 return (id); 644 } 645 646 static ssize_t 647 msgrcv(int msqid, struct ipcmsgbuf *msgp, size_t msgsz, long msgtyp, int msgflg) 648 { 649 struct msg *smp; /* ptr to best msg on q */ 650 kmsqid_t *qp; /* ptr to associated q */ 651 kmutex_t *lock; 652 size_t xtsz; /* transfer byte count */ 653 int error = 0; 654 int cvres; 655 uint_t msg_hash; 656 msgq_wakeup_t msg_entry; 657 658 CPU_STATS_ADDQ(CPU, sys, msg, 1); /* bump msg send/rcv count */ 659 660 msg_hash = msg_type_hash(msgtyp); 661 if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL) { 662 return ((ssize_t)set_errno(EINVAL)); 663 } 664 ipc_hold(msq_svc, (kipc_perm_t *)qp); 665 666 if (error = ipcperm_access(&qp->msg_perm, MSG_R, CRED())) { 667 goto msgrcv_out; 668 } 669 670 /* 671 * Various information (including the condvar_t) required for the 672 * process to sleep is provided by it's stack. 673 */ 674 msg_entry.msgw_thrd = curthread; 675 msg_entry.msgw_snd_wake = 0; 676 msg_entry.msgw_type = msgtyp; 677 findmsg: 678 smp = msgrcv_lookup(qp, msgtyp); 679 680 if (smp) { 681 /* 682 * We found a possible message to copy out. 683 */ 684 if ((smp->msg_flags & MSG_RCVCOPY) == 0) { 685 long t = msg_entry.msgw_snd_wake; 686 /* 687 * It is available, attempt to copy it. 688 */ 689 error = msg_copyout(qp, msgtyp, &lock, &xtsz, msgsz, 690 smp, msgp, msgflg); 691 692 /* 693 * It is possible to consume a different message 694 * type then what originally awakened for (negative 695 * types). If this happens a check must be done to 696 * to determine if another receiver is available 697 * for the waking message type, Failure to do this 698 * can result in a message on the queue that can be 699 * serviced by a sleeping receiver. 700 */ 701 if (!error && t && (smp->msg_type != t)) 702 msg_wakeup_rdr(qp, &qp->msg_fnd_sndr, t); 703 704 /* 705 * Don't forget to wakeup a sleeper that blocked because 706 * we were copying things out. 707 */ 708 msg_wakeup_rdr(qp, &qp->msg_fnd_rdr, 0); 709 goto msgrcv_out; 710 } 711 /* 712 * The selected message is being copied out, so block. We do 713 * not need to wake the next person up on the msg_cpy_block list 714 * due to the fact some one is copying out and they will get 715 * things moving again once the copy is completed. 716 */ 717 cvres = msg_rcvq_sleep(&qp->msg_cpy_block, 718 &msg_entry, &lock, qp); 719 error = msgq_check_err(qp, cvres); 720 if (error) { 721 goto msgrcv_out; 722 } 723 goto findmsg; 724 } 725 /* 726 * There isn't a message to copy out that matches the designated 727 * criteria. 728 */ 729 if (msgflg & IPC_NOWAIT) { 730 error = ENOMSG; 731 goto msgrcv_out; 732 } 733 msg_wakeup_rdr(qp, &qp->msg_fnd_rdr, 0); 734 735 /* 736 * Wait for new message. We keep the negative and positive types 737 * separate for performance reasons. 738 */ 739 msg_entry.msgw_snd_wake = 0; 740 if (msgtyp >= 0) { 741 cvres = msg_rcvq_sleep(&qp->msg_wait_snd[msg_hash], 742 &msg_entry, &lock, qp); 743 } else { 744 qp->msg_ngt_cnt++; 745 cvres = msg_rcvq_sleep(&qp->msg_wait_snd_ngt[msg_hash], 746 &msg_entry, &lock, qp); 747 qp->msg_ngt_cnt--; 748 } 749 750 if (!(error = msgq_check_err(qp, cvres))) { 751 goto findmsg; 752 } 753 754 msgrcv_out: 755 if (error) { 756 msg_wakeup_rdr(qp, &qp->msg_fnd_rdr, 0); 757 if (msg_entry.msgw_snd_wake) { 758 msg_wakeup_rdr(qp, &qp->msg_fnd_sndr, 759 msg_entry.msgw_snd_wake); 760 } 761 ipc_rele(msq_svc, (kipc_perm_t *)qp); 762 return ((ssize_t)set_errno(error)); 763 } 764 ipc_rele(msq_svc, (kipc_perm_t *)qp); 765 return ((ssize_t)xtsz); 766 } 767 768 static int 769 msgq_check_err(kmsqid_t *qp, int cvres) 770 { 771 if (IPC_FREE(&qp->msg_perm)) { 772 return (EIDRM); 773 } 774 775 if (cvres == 0) { 776 return (EINTR); 777 } 778 779 return (0); 780 } 781 782 static int 783 msg_copyout(kmsqid_t *qp, long msgtyp, kmutex_t **lock, size_t *xtsz_ret, 784 size_t msgsz, struct msg *smp, struct ipcmsgbuf *msgp, int msgflg) 785 { 786 size_t xtsz; 787 STRUCT_HANDLE(ipcmsgbuf, umsgp); 788 model_t mdl = get_udatamodel(); 789 int copyerror = 0; 790 791 STRUCT_SET_HANDLE(umsgp, mdl, msgp); 792 if (msgsz < smp->msg_size) { 793 if ((msgflg & MSG_NOERROR) == 0) { 794 return (E2BIG); 795 } else { 796 xtsz = msgsz; 797 } 798 } else { 799 xtsz = smp->msg_size; 800 } 801 *xtsz_ret = xtsz; 802 803 /* 804 * To prevent a DOS attack we mark the message as being 805 * copied out and release mutex. When the copy is completed 806 * we need to acquire the mutex and make the appropriate updates. 807 */ 808 ASSERT((smp->msg_flags & MSG_RCVCOPY) == 0); 809 smp->msg_flags |= MSG_RCVCOPY; 810 msg_hold(smp); 811 if (msgtyp < 0) { 812 ASSERT(qp->msg_neg_copy == 0); 813 qp->msg_neg_copy = 1; 814 } 815 mutex_exit(*lock); 816 817 if (mdl == DATAMODEL_NATIVE) { 818 copyerror = copyout(&smp->msg_type, msgp, 819 sizeof (smp->msg_type)); 820 } else { 821 /* 822 * 32-bit callers need an imploded msg type. 823 */ 824 int32_t msg_type32 = smp->msg_type; 825 826 copyerror = copyout(&msg_type32, msgp, 827 sizeof (msg_type32)); 828 } 829 830 if (copyerror == 0 && xtsz) { 831 copyerror = copyout(smp->msg_addr, 832 STRUCT_FADDR(umsgp, mtext), xtsz); 833 } 834 835 /* 836 * Reclaim the mutex and make sure the message queue still exists. 837 */ 838 839 *lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id); 840 if (msgtyp < 0) { 841 qp->msg_neg_copy = 0; 842 } 843 ASSERT(smp->msg_flags & MSG_RCVCOPY); 844 smp->msg_flags &= ~MSG_RCVCOPY; 845 msg_rele(smp); 846 if (IPC_FREE(&qp->msg_perm)) { 847 return (EIDRM); 848 } 849 if (copyerror) { 850 return (EFAULT); 851 } 852 qp->msg_lrpid = ttoproc(curthread)->p_pid; 853 qp->msg_rtime = gethrestime_sec(); 854 msgunlink(qp, smp); 855 return (0); 856 } 857 858 static struct msg * 859 msgrcv_lookup(kmsqid_t *qp, long msgtyp) 860 { 861 struct msg *smp = NULL; 862 long qp_low; 863 struct msg *mp; /* ptr to msg on q */ 864 long low_msgtype; 865 static struct msg neg_copy_smp; 866 867 mp = list_head(&qp->msg_list); 868 if (msgtyp == 0) { 869 smp = mp; 870 } else { 871 qp_low = qp->msg_lowest_type; 872 if (msgtyp > 0) { 873 /* 874 * If our lowest possible message type is larger than 875 * the message type desired, then we know there is 876 * no entry present. 877 */ 878 if (qp_low > msgtyp) { 879 return (NULL); 880 } 881 882 for (; mp; mp = list_next(&qp->msg_list, mp)) { 883 if (msgtyp == mp->msg_type) { 884 smp = mp; 885 break; 886 } 887 } 888 } else { 889 /* 890 * We have kept track of the lowest possible message 891 * type on the send queue. This allows us to terminate 892 * the search early if we find a message type of that 893 * type. Note, the lowest type may not be the actual 894 * lowest value in the system, it is only guaranteed 895 * that there isn't a value lower than that. 896 */ 897 low_msgtype = -msgtyp; 898 if (low_msgtype < qp_low) { 899 return (NULL); 900 } 901 if (qp->msg_neg_copy) { 902 neg_copy_smp.msg_flags = MSG_RCVCOPY; 903 return (&neg_copy_smp); 904 } 905 for (; mp; mp = list_next(&qp->msg_list, mp)) { 906 if (mp->msg_type <= low_msgtype && 907 !(smp && smp->msg_type <= mp->msg_type)) { 908 smp = mp; 909 low_msgtype = mp->msg_type; 910 if (low_msgtype == qp_low) { 911 break; 912 } 913 } 914 } 915 if (smp) { 916 /* 917 * Update the lowest message type. 918 */ 919 qp->msg_lowest_type = smp->msg_type; 920 } 921 } 922 } 923 return (smp); 924 } 925 926 /* 927 * msgids system call. 928 */ 929 static int 930 msgids(int *buf, uint_t nids, uint_t *pnids) 931 { 932 int error; 933 934 if (error = ipc_ids(msq_svc, buf, nids, pnids)) 935 return (set_errno(error)); 936 937 return (0); 938 } 939 940 #define RND(x) roundup((x), sizeof (size_t)) 941 #define RND32(x) roundup((x), sizeof (size32_t)) 942 943 /* 944 * msgsnap system call. 945 */ 946 static int 947 msgsnap(int msqid, caddr_t buf, size_t bufsz, long msgtyp) 948 { 949 struct msg *mp; /* ptr to msg on q */ 950 kmsqid_t *qp; /* ptr to associated q */ 951 kmutex_t *lock; 952 size_t size; 953 size_t nmsg; 954 struct msg **snaplist; 955 int error, i; 956 model_t mdl = get_udatamodel(); 957 STRUCT_DECL(msgsnap_head, head); 958 STRUCT_DECL(msgsnap_mhead, mhead); 959 960 STRUCT_INIT(head, mdl); 961 STRUCT_INIT(mhead, mdl); 962 963 if (bufsz < STRUCT_SIZE(head)) 964 return (set_errno(EINVAL)); 965 966 if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL) 967 return (set_errno(EINVAL)); 968 969 if (error = ipcperm_access(&qp->msg_perm, MSG_R, CRED())) { 970 mutex_exit(lock); 971 return (set_errno(error)); 972 } 973 ipc_hold(msq_svc, (kipc_perm_t *)qp); 974 975 /* 976 * First compute the required buffer size and 977 * the number of messages on the queue. 978 */ 979 size = nmsg = 0; 980 for (mp = list_head(&qp->msg_list); mp; 981 mp = list_next(&qp->msg_list, mp)) { 982 if (msgtyp == 0 || 983 (msgtyp > 0 && msgtyp == mp->msg_type) || 984 (msgtyp < 0 && mp->msg_type <= -msgtyp)) { 985 nmsg++; 986 if (mdl == DATAMODEL_NATIVE) 987 size += RND(mp->msg_size); 988 else 989 size += RND32(mp->msg_size); 990 } 991 } 992 993 size += STRUCT_SIZE(head) + nmsg * STRUCT_SIZE(mhead); 994 if (size > bufsz) 995 nmsg = 0; 996 997 if (nmsg > 0) { 998 /* 999 * Mark the messages as being copied. 1000 */ 1001 snaplist = (struct msg **)kmem_alloc(nmsg * 1002 sizeof (struct msg *), KM_SLEEP); 1003 i = 0; 1004 for (mp = list_head(&qp->msg_list); mp; 1005 mp = list_next(&qp->msg_list, mp)) { 1006 if (msgtyp == 0 || 1007 (msgtyp > 0 && msgtyp == mp->msg_type) || 1008 (msgtyp < 0 && mp->msg_type <= -msgtyp)) { 1009 msg_hold(mp); 1010 snaplist[i] = mp; 1011 i++; 1012 } 1013 } 1014 } 1015 mutex_exit(lock); 1016 1017 /* 1018 * Copy out the buffer header. 1019 */ 1020 STRUCT_FSET(head, msgsnap_size, size); 1021 STRUCT_FSET(head, msgsnap_nmsg, nmsg); 1022 if (copyout(STRUCT_BUF(head), buf, STRUCT_SIZE(head))) 1023 error = EFAULT; 1024 1025 buf += STRUCT_SIZE(head); 1026 1027 /* 1028 * Now copy out the messages one by one. 1029 */ 1030 for (i = 0; i < nmsg; i++) { 1031 mp = snaplist[i]; 1032 if (error == 0) { 1033 STRUCT_FSET(mhead, msgsnap_mlen, mp->msg_size); 1034 STRUCT_FSET(mhead, msgsnap_mtype, mp->msg_type); 1035 if (copyout(STRUCT_BUF(mhead), buf, STRUCT_SIZE(mhead))) 1036 error = EFAULT; 1037 buf += STRUCT_SIZE(mhead); 1038 1039 if (error == 0 && 1040 mp->msg_size != 0 && 1041 copyout(mp->msg_addr, buf, mp->msg_size)) 1042 error = EFAULT; 1043 if (mdl == DATAMODEL_NATIVE) 1044 buf += RND(mp->msg_size); 1045 else 1046 buf += RND32(mp->msg_size); 1047 } 1048 lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id); 1049 msg_rele(mp); 1050 /* Check for msg q deleted or reallocated */ 1051 if (IPC_FREE(&qp->msg_perm)) 1052 error = EIDRM; 1053 mutex_exit(lock); 1054 } 1055 1056 (void) ipc_lock(msq_svc, qp->msg_perm.ipc_id); 1057 ipc_rele(msq_svc, (kipc_perm_t *)qp); 1058 1059 if (nmsg > 0) 1060 kmem_free(snaplist, nmsg * sizeof (struct msg *)); 1061 1062 if (error) 1063 return (set_errno(error)); 1064 return (0); 1065 } 1066 1067 #define MSG_PREALLOC_LIMIT 8192 1068 1069 /* 1070 * msgsnd system call. 1071 */ 1072 static int 1073 msgsnd(int msqid, struct ipcmsgbuf *msgp, size_t msgsz, int msgflg) 1074 { 1075 kmsqid_t *qp; 1076 kmutex_t *lock = NULL; 1077 struct msg *mp = NULL; 1078 long type; 1079 int error = 0; 1080 model_t mdl = get_udatamodel(); 1081 STRUCT_HANDLE(ipcmsgbuf, umsgp); 1082 1083 CPU_STATS_ADDQ(CPU, sys, msg, 1); /* bump msg send/rcv count */ 1084 STRUCT_SET_HANDLE(umsgp, mdl, msgp); 1085 1086 if (mdl == DATAMODEL_NATIVE) { 1087 if (copyin(msgp, &type, sizeof (type))) 1088 return (set_errno(EFAULT)); 1089 } else { 1090 int32_t type32; 1091 if (copyin(msgp, &type32, sizeof (type32))) 1092 return (set_errno(EFAULT)); 1093 type = type32; 1094 } 1095 1096 if (type < 1) 1097 return (set_errno(EINVAL)); 1098 1099 /* 1100 * We want the value here large enough that most of the 1101 * the message operations will use the "lockless" path, 1102 * but small enough that a user can not reserve large 1103 * chunks of kernel memory unless they have a valid 1104 * reason to. 1105 */ 1106 if (msgsz <= MSG_PREALLOC_LIMIT) { 1107 /* 1108 * We are small enough that we can afford to do the 1109 * allocation now. This saves dropping the lock 1110 * and then reacquiring the lock. 1111 */ 1112 mp = kmem_zalloc(sizeof (struct msg), KM_SLEEP); 1113 mp->msg_copycnt = 1; 1114 mp->msg_size = msgsz; 1115 if (msgsz) { 1116 mp->msg_addr = kmem_alloc(msgsz, KM_SLEEP); 1117 if (copyin(STRUCT_FADDR(umsgp, mtext), 1118 mp->msg_addr, msgsz) == -1) { 1119 error = EFAULT; 1120 goto msgsnd_out; 1121 } 1122 } 1123 } 1124 1125 if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL) { 1126 error = EINVAL; 1127 goto msgsnd_out; 1128 } 1129 1130 ipc_hold(msq_svc, (kipc_perm_t *)qp); 1131 1132 if (msgsz > qp->msg_qbytes) { 1133 error = EINVAL; 1134 goto msgsnd_out; 1135 } 1136 1137 if (error = ipcperm_access(&qp->msg_perm, MSG_W, CRED())) 1138 goto msgsnd_out; 1139 1140 top: 1141 /* 1142 * Allocate space on q, message header, & buffer space. 1143 */ 1144 ASSERT(qp->msg_qnum <= qp->msg_qmax); 1145 while ((msgsz > qp->msg_qbytes - qp->msg_cbytes) || 1146 (qp->msg_qnum == qp->msg_qmax)) { 1147 int cvres; 1148 1149 if (msgflg & IPC_NOWAIT) { 1150 error = EAGAIN; 1151 goto msgsnd_out; 1152 } 1153 1154 qp->msg_snd_cnt++; 1155 cvres = cv_wait_sig(&qp->msg_snd_cv, lock); 1156 lock = ipc_relock(msq_svc, qp->msg_perm.ipc_id, lock); 1157 qp->msg_snd_cnt--; 1158 1159 if (error = msgq_check_err(qp, cvres)) { 1160 goto msgsnd_out; 1161 } 1162 } 1163 1164 if (mp == NULL) { 1165 int failure; 1166 1167 mutex_exit(lock); 1168 ASSERT(msgsz > 0); 1169 mp = kmem_zalloc(sizeof (struct msg), KM_SLEEP); 1170 mp->msg_addr = kmem_alloc(msgsz, KM_SLEEP); 1171 mp->msg_size = msgsz; 1172 mp->msg_copycnt = 1; 1173 1174 failure = (copyin(STRUCT_FADDR(umsgp, mtext), 1175 mp->msg_addr, msgsz) == -1); 1176 lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id); 1177 if (IPC_FREE(&qp->msg_perm)) { 1178 error = EIDRM; 1179 goto msgsnd_out; 1180 } 1181 if (failure) { 1182 error = EFAULT; 1183 goto msgsnd_out; 1184 } 1185 goto top; 1186 } 1187 1188 /* 1189 * Everything is available, put msg on q. 1190 */ 1191 qp->msg_qnum++; 1192 qp->msg_cbytes += msgsz; 1193 qp->msg_lspid = curproc->p_pid; 1194 qp->msg_stime = gethrestime_sec(); 1195 mp->msg_type = type; 1196 if (qp->msg_lowest_type > type) 1197 qp->msg_lowest_type = type; 1198 list_insert_tail(&qp->msg_list, mp); 1199 /* 1200 * Get the proper receiver going. 1201 */ 1202 msg_wakeup_rdr(qp, &qp->msg_fnd_sndr, type); 1203 1204 msgsnd_out: 1205 if (lock) 1206 ipc_rele(msq_svc, (kipc_perm_t *)qp); /* drops lock */ 1207 1208 if (error) { 1209 if (mp) 1210 msg_rele(mp); 1211 return (set_errno(error)); 1212 } 1213 1214 return (0); 1215 } 1216 1217 static void 1218 msg_wakeup_rdr(kmsqid_t *qp, msg_select_t **flist, long type) 1219 { 1220 msg_select_t *walker = *flist; 1221 msgq_wakeup_t *wakeup; 1222 uint_t msg_hash; 1223 1224 msg_hash = msg_type_hash(type); 1225 1226 do { 1227 wakeup = walker->selection(qp, msg_hash, type); 1228 walker = walker->next_selection; 1229 } while (!wakeup && walker != *flist); 1230 1231 *flist = (*flist)->next_selection; 1232 if (wakeup) { 1233 if (type) { 1234 wakeup->msgw_snd_wake = type; 1235 } 1236 cv_signal(&wakeup->msgw_wake_cv); 1237 } 1238 } 1239 1240 static uint_t 1241 msg_type_hash(long msg_type) 1242 { 1243 if (msg_type < 0) { 1244 long hash = -msg_type / MSG_NEG_INTERVAL; 1245 /* 1246 * Negative message types are hashed over an 1247 * interval. Any message type that hashes 1248 * beyond MSG_MAX_QNUM is automatically placed 1249 * in the last bucket. 1250 */ 1251 if (hash > MSG_MAX_QNUM) 1252 hash = MSG_MAX_QNUM; 1253 return (hash); 1254 } 1255 1256 /* 1257 * 0 or positive message type. The first bucket is reserved for 1258 * message receivers of type 0, the other buckets we hash into. 1259 */ 1260 if (msg_type) 1261 return (1 + (msg_type % MSG_MAX_QNUM)); 1262 return (0); 1263 } 1264 1265 /* 1266 * Routines to see if we have a receiver of type 0 either blocked waiting 1267 * for a message. Simply return the first guy on the list. 1268 */ 1269 1270 static msgq_wakeup_t * 1271 /* ARGSUSED */ 1272 msg_fnd_any_snd(kmsqid_t *qp, int msg_hash, long type) 1273 { 1274 msgq_wakeup_t *walker; 1275 1276 walker = list_head(&qp->msg_wait_snd[0]); 1277 1278 if (walker) 1279 list_remove(&qp->msg_wait_snd[0], walker); 1280 return (walker); 1281 } 1282 1283 static msgq_wakeup_t * 1284 /* ARGSUSED */ 1285 msg_fnd_any_rdr(kmsqid_t *qp, int msg_hash, long type) 1286 { 1287 msgq_wakeup_t *walker; 1288 1289 walker = list_head(&qp->msg_cpy_block); 1290 if (walker) 1291 list_remove(&qp->msg_cpy_block, walker); 1292 return (walker); 1293 } 1294 1295 static msgq_wakeup_t * 1296 msg_fnd_spc_snd(kmsqid_t *qp, int msg_hash, long type) 1297 { 1298 msgq_wakeup_t *walker; 1299 1300 walker = list_head(&qp->msg_wait_snd[msg_hash]); 1301 1302 while (walker && walker->msgw_type != type) 1303 walker = list_next(&qp->msg_wait_snd[msg_hash], walker); 1304 if (walker) 1305 list_remove(&qp->msg_wait_snd[msg_hash], walker); 1306 return (walker); 1307 } 1308 1309 /* ARGSUSED */ 1310 static msgq_wakeup_t * 1311 msg_fnd_neg_snd(kmsqid_t *qp, int msg_hash, long type) 1312 { 1313 msgq_wakeup_t *qptr; 1314 int count; 1315 int check_index; 1316 int neg_index; 1317 int nbuckets; 1318 1319 if (!qp->msg_ngt_cnt) { 1320 return (NULL); 1321 } 1322 neg_index = msg_type_hash(-type); 1323 1324 /* 1325 * Check for a match among the negative type queues. Any buckets 1326 * at neg_index or larger can match the type. Use the last send 1327 * time to randomize the starting bucket to prevent starvation. 1328 * Search all buckets from neg_index to MSG_MAX_QNUM, starting 1329 * from the random starting point, and wrapping around after 1330 * MSG_MAX_QNUM. 1331 */ 1332 1333 nbuckets = MSG_MAX_QNUM - neg_index + 1; 1334 check_index = neg_index + (qp->msg_stime % nbuckets); 1335 1336 for (count = nbuckets; count > 0; count--) { 1337 qptr = list_head(&qp->msg_wait_snd_ngt[check_index]); 1338 while (qptr) { 1339 /* 1340 * The lowest hash bucket may actually contain 1341 * message types that are not valid for this 1342 * request. This can happen due to the fact that 1343 * the message buckets actually contain a consecutive 1344 * range of types. 1345 */ 1346 if (-qptr->msgw_type >= type) { 1347 list_remove(&qp->msg_wait_snd_ngt[check_index], 1348 qptr); 1349 return (qptr); 1350 } 1351 qptr = list_next(&qp->msg_wait_snd_ngt[check_index], 1352 qptr); 1353 } 1354 if (++check_index > MSG_MAX_QNUM) { 1355 check_index = neg_index; 1356 } 1357 } 1358 return (NULL); 1359 } 1360 1361 static int 1362 msg_rcvq_sleep(list_t *queue, msgq_wakeup_t *entry, kmutex_t **lock, 1363 kmsqid_t *qp) 1364 { 1365 int cvres; 1366 1367 cv_init(&entry->msgw_wake_cv, NULL, 0, NULL); 1368 1369 list_insert_tail(queue, entry); 1370 1371 qp->msg_rcv_cnt++; 1372 cvres = cv_wait_sig(&entry->msgw_wake_cv, *lock); 1373 *lock = ipc_relock(msq_svc, qp->msg_perm.ipc_id, *lock); 1374 qp->msg_rcv_cnt--; 1375 1376 if (list_link_active(&entry->msgw_list)) { 1377 /* 1378 * We woke up unexpectedly, remove ourself. 1379 */ 1380 list_remove(queue, entry); 1381 } 1382 1383 return (cvres); 1384 } 1385 1386 static void 1387 msg_rcvq_wakeup_all(list_t *q_ptr) 1388 { 1389 msgq_wakeup_t *q_walk; 1390 1391 while (q_walk = list_head(q_ptr)) { 1392 list_remove(q_ptr, q_walk); 1393 cv_signal(&q_walk->msgw_wake_cv); 1394 } 1395 } 1396 1397 /* 1398 * msgsys - System entry point for msgctl, msgget, msgrcv, and msgsnd 1399 * system calls. 1400 */ 1401 static ssize_t 1402 msgsys(int opcode, uintptr_t a1, uintptr_t a2, uintptr_t a3, 1403 uintptr_t a4, uintptr_t a5) 1404 { 1405 ssize_t error; 1406 1407 switch (opcode) { 1408 case MSGGET: 1409 error = msgget((key_t)a1, (int)a2); 1410 break; 1411 case MSGCTL: 1412 error = msgctl((int)a1, (int)a2, (void *)a3); 1413 break; 1414 case MSGRCV: 1415 error = msgrcv((int)a1, (struct ipcmsgbuf *)a2, 1416 (size_t)a3, (long)a4, (int)a5); 1417 break; 1418 case MSGSND: 1419 error = msgsnd((int)a1, (struct ipcmsgbuf *)a2, 1420 (size_t)a3, (int)a4); 1421 break; 1422 case MSGIDS: 1423 error = msgids((int *)a1, (uint_t)a2, (uint_t *)a3); 1424 break; 1425 case MSGSNAP: 1426 error = msgsnap((int)a1, (caddr_t)a2, (size_t)a3, (long)a4); 1427 break; 1428 default: 1429 error = set_errno(EINVAL); 1430 break; 1431 } 1432 1433 return (error); 1434 } 1435 1436 #ifdef _SYSCALL32_IMPL 1437 /* 1438 * msgsys32 - System entry point for msgctl, msgget, msgrcv, and msgsnd 1439 * system calls for 32-bit callers on LP64 kernel. 1440 */ 1441 static ssize32_t 1442 msgsys32(int opcode, uint32_t a1, uint32_t a2, uint32_t a3, 1443 uint32_t a4, uint32_t a5) 1444 { 1445 ssize_t error; 1446 1447 switch (opcode) { 1448 case MSGGET: 1449 error = msgget((key_t)a1, (int)a2); 1450 break; 1451 case MSGCTL: 1452 error = msgctl((int)a1, (int)a2, (void *)(uintptr_t)a3); 1453 break; 1454 case MSGRCV: 1455 error = msgrcv((int)a1, (struct ipcmsgbuf *)(uintptr_t)a2, 1456 (size_t)a3, (long)(int32_t)a4, (int)a5); 1457 break; 1458 case MSGSND: 1459 error = msgsnd((int)a1, (struct ipcmsgbuf *)(uintptr_t)a2, 1460 (size_t)(int32_t)a3, (int)a4); 1461 break; 1462 case MSGIDS: 1463 error = msgids((int *)(uintptr_t)a1, (uint_t)a2, 1464 (uint_t *)(uintptr_t)a3); 1465 break; 1466 case MSGSNAP: 1467 error = msgsnap((int)a1, (caddr_t)(uintptr_t)a2, (size_t)a3, 1468 (long)(int32_t)a4); 1469 break; 1470 default: 1471 error = set_errno(EINVAL); 1472 break; 1473 } 1474 1475 return (error); 1476 } 1477 #endif /* SYSCALL32_IMPL */ 1478