sysv_msg.c revision 1.49 1 /* $NetBSD: sysv_msg.c,v 1.49 2007/03/13 03:43:54 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Implementation of SVID messages
42 *
43 * Author: Daniel Boulet
44 *
45 * Copyright 1993 Daniel Boulet and RTMX Inc.
46 *
47 * This system call was implemented by Daniel Boulet under contract from RTMX.
48 *
49 * Redistribution and use in source forms, with and without modification,
50 * are permitted provided that this entire comment appears intact.
51 *
52 * Redistribution in binary form may occur without any restrictions.
53 * Obviously, it would be nice if you gave credit where credit is due
54 * but requiring it would be too onerous.
55 *
56 * This software is provided ``AS IS'' without any warranties of any kind.
57 */
58
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: sysv_msg.c,v 1.49 2007/03/13 03:43:54 ad Exp $");
61
62 #define SYSVMSG
63
64 #include <sys/param.h>
65 #include <sys/kernel.h>
66 #include <sys/msg.h>
67 #include <sys/sysctl.h>
68 #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
69 #include <sys/syscallargs.h>
70 #include <sys/kauth.h>
71
72 #define MSG_DEBUG
73 #undef MSG_DEBUG_OK
74
75 #ifdef MSG_DEBUG_OK
76 #define MSG_PRINTF(a) printf a
77 #else
78 #define MSG_PRINTF(a)
79 #endif
80
81 static int nfree_msgmaps; /* # of free map entries */
82 static short free_msgmaps; /* head of linked list of free map entries */
83 static struct __msg *free_msghdrs; /* list of free msg headers */
84 static char *msgpool; /* MSGMAX byte long msg buffer pool */
85 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
86 static struct __msg *msghdrs; /* MSGTQL msg headers */
87
88 kmsq_t *msqs; /* MSGMNI msqid_ds struct's */
89 kmutex_t msgmutex; /* subsystem lock */
90
91 static void msg_freehdr(struct __msg *);
92
93 void
94 msginit(void)
95 {
96 int i, sz;
97 vaddr_t v;
98
99 /*
100 * msginfo.msgssz should be a power of two for efficiency reasons.
101 * It is also pretty silly if msginfo.msgssz is less than 8
102 * or greater than about 256 so ...
103 */
104
105 i = 8;
106 while (i < 1024 && i != msginfo.msgssz)
107 i <<= 1;
108 if (i != msginfo.msgssz) {
109 MSG_PRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
110 msginfo.msgssz));
111 panic("msginfo.msgssz not a small power of 2");
112 }
113
114 if (msginfo.msgseg > 32767) {
115 MSG_PRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
116 panic("msginfo.msgseg > 32767");
117 }
118
119 /* Allocate pageable memory for our structures */
120 sz = msginfo.msgmax
121 + msginfo.msgseg * sizeof(struct msgmap)
122 + msginfo.msgtql * sizeof(struct __msg)
123 + msginfo.msgmni * sizeof(kmsq_t);
124 v = uvm_km_alloc(kernel_map, round_page(sz), 0,
125 UVM_KMF_WIRED|UVM_KMF_ZERO);
126 if (v == 0)
127 panic("sysv_msg: cannot allocate memory");
128 msgpool = (void *)v;
129 msgmaps = (void *) (msgpool + msginfo.msgmax);
130 msghdrs = (void *) (msgmaps + msginfo.msgseg);
131 msqs = (void *) (msghdrs + msginfo.msgtql);
132
133 for (i = 0; i < msginfo.msgseg; i++) {
134 if (i > 0)
135 msgmaps[i-1].next = i;
136 msgmaps[i].next = -1; /* implies entry is available */
137 }
138 free_msgmaps = 0;
139 nfree_msgmaps = msginfo.msgseg;
140
141 if (msghdrs == NULL)
142 panic("msghdrs is NULL");
143
144 for (i = 0; i < msginfo.msgtql; i++) {
145 msghdrs[i].msg_type = 0;
146 if (i > 0)
147 msghdrs[i-1].msg_next = &msghdrs[i];
148 msghdrs[i].msg_next = NULL;
149 }
150 free_msghdrs = &msghdrs[0];
151
152 if (msqs == NULL)
153 panic("msqs is NULL");
154
155 for (i = 0; i < msginfo.msgmni; i++) {
156 msqs[i].msq_u.msg_qbytes = 0; /* implies entry is available */
157 msqs[i].msq_u.msg_perm._seq = 0;/* reset to a known value */
158 cv_init(&msqs[i].msq_cv, "msgwait");
159 }
160
161 mutex_init(&msgmutex, MUTEX_DEFAULT, IPL_NONE);
162 }
163
164 static void
165 msg_freehdr(struct __msg *msghdr)
166 {
167
168 KASSERT(mutex_owned(&msgmutex));
169
170 while (msghdr->msg_ts > 0) {
171 short next;
172 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
173 panic("msghdr->msg_spot out of range");
174 next = msgmaps[msghdr->msg_spot].next;
175 msgmaps[msghdr->msg_spot].next = free_msgmaps;
176 free_msgmaps = msghdr->msg_spot;
177 nfree_msgmaps++;
178 msghdr->msg_spot = next;
179 if (msghdr->msg_ts >= msginfo.msgssz)
180 msghdr->msg_ts -= msginfo.msgssz;
181 else
182 msghdr->msg_ts = 0;
183 }
184 if (msghdr->msg_spot != -1)
185 panic("msghdr->msg_spot != -1");
186 msghdr->msg_next = free_msghdrs;
187 free_msghdrs = msghdr;
188 }
189
190 int
191 sys___msgctl13(struct lwp *l, void *v, register_t *retval)
192 {
193 struct sys___msgctl13_args /* {
194 syscallarg(int) msqid;
195 syscallarg(int) cmd;
196 syscallarg(struct msqid_ds *) buf;
197 } */ *uap = v;
198 struct msqid_ds msqbuf;
199 int cmd, error;
200
201 cmd = SCARG(uap, cmd);
202
203 if (cmd == IPC_SET) {
204 error = copyin(SCARG(uap, buf), &msqbuf, sizeof(msqbuf));
205 if (error)
206 return (error);
207 }
208
209 error = msgctl1(l, SCARG(uap, msqid), cmd,
210 (cmd == IPC_SET || cmd == IPC_STAT) ? &msqbuf : NULL);
211
212 if (error == 0 && cmd == IPC_STAT)
213 error = copyout(&msqbuf, SCARG(uap, buf), sizeof(msqbuf));
214
215 return (error);
216 }
217
218 int
219 msgctl1(struct lwp *l, int msqid, int cmd, struct msqid_ds *msqbuf)
220 {
221 kauth_cred_t cred = l->l_cred;
222 struct msqid_ds *msqptr;
223 kmsq_t *msq;
224 int error = 0, ix;
225
226 MSG_PRINTF(("call to msgctl1(%d, %d)\n", msqid, cmd));
227
228 ix = IPCID_TO_IX(msqid);
229
230 mutex_enter(&msgmutex);
231
232 if (ix < 0 || ix >= msginfo.msgmni) {
233 MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", ix,
234 msginfo.msgmni));
235 error = EINVAL;
236 goto unlock;
237 }
238
239 msq = &msqs[ix];
240 msqptr = &msq->msq_u;
241
242 if (msqptr->msg_qbytes == 0) {
243 MSG_PRINTF(("no such msqid\n"));
244 error = EINVAL;
245 goto unlock;
246 }
247 if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqid)) {
248 MSG_PRINTF(("wrong sequence number\n"));
249 error = EINVAL;
250 goto unlock;
251 }
252
253 switch (cmd) {
254 case IPC_RMID:
255 {
256 struct __msg *msghdr;
257 if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_M)) != 0)
258 break;
259 /* Free the message headers */
260 msghdr = msqptr->_msg_first;
261 while (msghdr != NULL) {
262 struct __msg *msghdr_tmp;
263
264 /* Free the segments of each message */
265 msqptr->_msg_cbytes -= msghdr->msg_ts;
266 msqptr->msg_qnum--;
267 msghdr_tmp = msghdr;
268 msghdr = msghdr->msg_next;
269 msg_freehdr(msghdr_tmp);
270 }
271
272 if (msqptr->_msg_cbytes != 0)
273 panic("msg_cbytes is screwed up");
274 if (msqptr->msg_qnum != 0)
275 panic("msg_qnum is screwed up");
276
277 msqptr->msg_qbytes = 0; /* Mark it as free */
278
279 cv_broadcast(&msq->msq_cv);
280 }
281 break;
282
283 case IPC_SET:
284 if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_M)))
285 break;
286 if (msqbuf->msg_qbytes > msqptr->msg_qbytes &&
287 kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
288 NULL) != 0) {
289 error = EPERM;
290 break;
291 }
292 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
293 MSG_PRINTF(("can't increase msg_qbytes beyond %d "
294 "(truncating)\n", msginfo.msgmnb));
295 /* silently restrict qbytes to system limit */
296 msqbuf->msg_qbytes = msginfo.msgmnb;
297 }
298 if (msqbuf->msg_qbytes == 0) {
299 MSG_PRINTF(("can't reduce msg_qbytes to 0\n"));
300 error = EINVAL; /* XXX non-standard errno! */
301 break;
302 }
303 msqptr->msg_perm.uid = msqbuf->msg_perm.uid;
304 msqptr->msg_perm.gid = msqbuf->msg_perm.gid;
305 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
306 (msqbuf->msg_perm.mode & 0777);
307 msqptr->msg_qbytes = msqbuf->msg_qbytes;
308 msqptr->msg_ctime = time_second;
309 break;
310
311 case IPC_STAT:
312 if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
313 MSG_PRINTF(("requester doesn't have read access\n"));
314 break;
315 }
316 memcpy(msqbuf, msqptr, sizeof(struct msqid_ds));
317 break;
318
319 default:
320 MSG_PRINTF(("invalid command %d\n", cmd));
321 error = EINVAL;
322 break;
323 }
324
325 unlock:
326 mutex_exit(&msgmutex);
327 return (error);
328 }
329
330 int
331 sys_msgget(struct lwp *l, void *v, register_t *retval)
332 {
333 struct sys_msgget_args /* {
334 syscallarg(key_t) key;
335 syscallarg(int) msgflg;
336 } */ *uap = v;
337 int msqid, error = 0;
338 int key = SCARG(uap, key);
339 int msgflg = SCARG(uap, msgflg);
340 kauth_cred_t cred = l->l_cred;
341 struct msqid_ds *msqptr = NULL;
342 kmsq_t *msq;
343
344 mutex_enter(&msgmutex);
345
346 MSG_PRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
347
348 if (key != IPC_PRIVATE) {
349 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
350 msq = &msqs[msqid];
351 msqptr = &msq->msq_u;
352 if (msqptr->msg_qbytes != 0 &&
353 msqptr->msg_perm._key == key)
354 break;
355 }
356 if (msqid < msginfo.msgmni) {
357 MSG_PRINTF(("found public key\n"));
358 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
359 MSG_PRINTF(("not exclusive\n"));
360 error = EEXIST;
361 goto unlock;
362 }
363 if ((error = ipcperm(cred, &msqptr->msg_perm,
364 msgflg & 0700 ))) {
365 MSG_PRINTF(("requester doesn't have 0%o access\n",
366 msgflg & 0700));
367 goto unlock;
368 }
369 goto found;
370 }
371 }
372
373 MSG_PRINTF(("need to allocate the msqid_ds\n"));
374 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
375 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
376 /*
377 * Look for an unallocated and unlocked msqid_ds.
378 * msqid_ds's can be locked by msgsnd or msgrcv while
379 * they are copying the message in/out. We can't
380 * re-use the entry until they release it.
381 */
382 msq = &msqs[msqid];
383 msqptr = &msq->msq_u;
384 if (msqptr->msg_qbytes == 0 &&
385 (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
386 break;
387 }
388 if (msqid == msginfo.msgmni) {
389 MSG_PRINTF(("no more msqid_ds's available\n"));
390 error = ENOSPC;
391 goto unlock;
392 }
393 MSG_PRINTF(("msqid %d is available\n", msqid));
394 msqptr->msg_perm._key = key;
395 msqptr->msg_perm.cuid = kauth_cred_geteuid(cred);
396 msqptr->msg_perm.uid = kauth_cred_geteuid(cred);
397 msqptr->msg_perm.cgid = kauth_cred_getegid(cred);
398 msqptr->msg_perm.gid = kauth_cred_getegid(cred);
399 msqptr->msg_perm.mode = (msgflg & 0777);
400 /* Make sure that the returned msqid is unique */
401 msqptr->msg_perm._seq++;
402 msqptr->_msg_first = NULL;
403 msqptr->_msg_last = NULL;
404 msqptr->_msg_cbytes = 0;
405 msqptr->msg_qnum = 0;
406 msqptr->msg_qbytes = msginfo.msgmnb;
407 msqptr->msg_lspid = 0;
408 msqptr->msg_lrpid = 0;
409 msqptr->msg_stime = 0;
410 msqptr->msg_rtime = 0;
411 msqptr->msg_ctime = time_second;
412 } else {
413 MSG_PRINTF(("didn't find it and wasn't asked to create it\n"));
414 error = ENOENT;
415 goto unlock;
416 }
417
418 found:
419 /* Construct the unique msqid */
420 *retval = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
421
422 unlock:
423 mutex_exit(&msgmutex);
424 return (error);
425 }
426
427 int
428 sys_msgsnd(struct lwp *l, void *v, register_t *retval)
429 {
430 struct sys_msgsnd_args /* {
431 syscallarg(int) msqid;
432 syscallarg(const void *) msgp;
433 syscallarg(size_t) msgsz;
434 syscallarg(int) msgflg;
435 } */ *uap = v;
436
437 return msgsnd1(l, SCARG(uap, msqid), SCARG(uap, msgp),
438 SCARG(uap, msgsz), SCARG(uap, msgflg), sizeof(long), copyin);
439 }
440
441 int
442 msgsnd1(struct lwp *l, int msqidr, const char *user_msgp, size_t msgsz,
443 int msgflg, size_t typesz, copyin_t fetch_type)
444 {
445 int segs_needed, error = 0, msqid;
446 kauth_cred_t cred = l->l_cred;
447 struct msqid_ds *msqptr;
448 struct __msg *msghdr;
449 kmsq_t *msq;
450 short next;
451
452 MSG_PRINTF(("call to msgsnd(%d, %p, %lld, %d)\n", msqid, user_msgp,
453 (long long)msgsz, msgflg));
454
455 msqid = IPCID_TO_IX(msqidr);
456
457 mutex_enter(&msgmutex);
458
459 if (msqid < 0 || msqid >= msginfo.msgmni) {
460 MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
461 msginfo.msgmni));
462 error = EINVAL;
463 goto unlock;
464 }
465
466 msq = &msqs[msqid];
467 msqptr = &msq->msq_u;
468
469 if (msqptr->msg_qbytes == 0) {
470 MSG_PRINTF(("no such message queue id\n"));
471 error = EINVAL;
472 goto unlock;
473 }
474 if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
475 MSG_PRINTF(("wrong sequence number\n"));
476 error = EINVAL;
477 goto unlock;
478 }
479
480 if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_W))) {
481 MSG_PRINTF(("requester doesn't have write access\n"));
482 goto unlock;
483 }
484
485 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
486 MSG_PRINTF(("msgsz=%lld, msgssz=%d, segs_needed=%d\n",
487 (long long)msgsz, msginfo.msgssz, segs_needed));
488 for (;;) {
489 int need_more_resources = 0;
490
491 /*
492 * check msgsz [cannot be negative since it is unsigned]
493 * (inside this loop in case msg_qbytes changes while we sleep)
494 */
495
496 if (msgsz > msqptr->msg_qbytes) {
497 MSG_PRINTF(("msgsz > msqptr->msg_qbytes\n"));
498 error = EINVAL;
499 goto unlock;
500 }
501
502 if (msqptr->msg_perm.mode & MSG_LOCKED) {
503 MSG_PRINTF(("msqid is locked\n"));
504 need_more_resources = 1;
505 }
506 if (msgsz + msqptr->_msg_cbytes > msqptr->msg_qbytes) {
507 MSG_PRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
508 need_more_resources = 1;
509 }
510 if (segs_needed > nfree_msgmaps) {
511 MSG_PRINTF(("segs_needed > nfree_msgmaps\n"));
512 need_more_resources = 1;
513 }
514 if (free_msghdrs == NULL) {
515 MSG_PRINTF(("no more msghdrs\n"));
516 need_more_resources = 1;
517 }
518
519 if (need_more_resources) {
520 int we_own_it;
521
522 if ((msgflg & IPC_NOWAIT) != 0) {
523 MSG_PRINTF(("need more resources but caller "
524 "doesn't want to wait\n"));
525 error = EAGAIN;
526 goto unlock;
527 }
528
529 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
530 MSG_PRINTF(("we don't own the msqid_ds\n"));
531 we_own_it = 0;
532 } else {
533 /* Force later arrivals to wait for our
534 request */
535 MSG_PRINTF(("we own the msqid_ds\n"));
536 msqptr->msg_perm.mode |= MSG_LOCKED;
537 we_own_it = 1;
538 }
539 MSG_PRINTF(("goodnight\n"));
540 error = cv_wait_sig(&msq->msq_cv, &msgmutex);
541 MSG_PRINTF(("good morning, error=%d\n", error));
542 if (we_own_it)
543 msqptr->msg_perm.mode &= ~MSG_LOCKED;
544 if (error != 0) {
545 MSG_PRINTF(("msgsnd: interrupted system "
546 "call\n"));
547 error = EINTR;
548 goto unlock;
549 }
550
551 /*
552 * Make sure that the msq queue still exists
553 */
554
555 if (msqptr->msg_qbytes == 0) {
556 MSG_PRINTF(("msqid deleted\n"));
557 error = EIDRM;
558 goto unlock;
559 }
560 } else {
561 MSG_PRINTF(("got all the resources that we need\n"));
562 break;
563 }
564 }
565
566 /*
567 * We have the resources that we need.
568 * Make sure!
569 */
570
571 if (msqptr->msg_perm.mode & MSG_LOCKED)
572 panic("msg_perm.mode & MSG_LOCKED");
573 if (segs_needed > nfree_msgmaps)
574 panic("segs_needed > nfree_msgmaps");
575 if (msgsz + msqptr->_msg_cbytes > msqptr->msg_qbytes)
576 panic("msgsz + msg_cbytes > msg_qbytes");
577 if (free_msghdrs == NULL)
578 panic("no more msghdrs");
579
580 /*
581 * Re-lock the msqid_ds in case we page-fault when copying in the
582 * message
583 */
584
585 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
586 panic("msqid_ds is already locked");
587 msqptr->msg_perm.mode |= MSG_LOCKED;
588
589 /*
590 * Allocate a message header
591 */
592
593 msghdr = free_msghdrs;
594 free_msghdrs = msghdr->msg_next;
595 msghdr->msg_spot = -1;
596 msghdr->msg_ts = msgsz;
597
598 /*
599 * Allocate space for the message
600 */
601
602 while (segs_needed > 0) {
603 if (nfree_msgmaps <= 0)
604 panic("not enough msgmaps");
605 if (free_msgmaps == -1)
606 panic("nil free_msgmaps");
607 next = free_msgmaps;
608 if (next <= -1)
609 panic("next too low #1");
610 if (next >= msginfo.msgseg)
611 panic("next out of range #1");
612 MSG_PRINTF(("allocating segment %d to message\n", next));
613 free_msgmaps = msgmaps[next].next;
614 nfree_msgmaps--;
615 msgmaps[next].next = msghdr->msg_spot;
616 msghdr->msg_spot = next;
617 segs_needed--;
618 }
619
620 /*
621 * Copy in the message type
622 */
623 mutex_exit(&msgmutex);
624 error = (*fetch_type)(user_msgp, &msghdr->msg_type, typesz);
625 mutex_enter(&msgmutex);
626 if (error != 0) {
627 MSG_PRINTF(("error %d copying the message type\n", error));
628 msg_freehdr(msghdr);
629 msqptr->msg_perm.mode &= ~MSG_LOCKED;
630 cv_broadcast(&msq->msq_cv);
631 goto unlock;
632 }
633 user_msgp += typesz;
634
635 /*
636 * Validate the message type
637 */
638
639 if (msghdr->msg_type < 1) {
640 msg_freehdr(msghdr);
641 msqptr->msg_perm.mode &= ~MSG_LOCKED;
642 cv_broadcast(&msq->msq_cv);
643 MSG_PRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
644 goto unlock;
645 }
646
647 /*
648 * Copy in the message body
649 */
650
651 next = msghdr->msg_spot;
652 while (msgsz > 0) {
653 size_t tlen;
654 if (msgsz > msginfo.msgssz)
655 tlen = msginfo.msgssz;
656 else
657 tlen = msgsz;
658 if (next <= -1)
659 panic("next too low #2");
660 if (next >= msginfo.msgseg)
661 panic("next out of range #2");
662 mutex_exit(&msgmutex);
663 error = copyin(user_msgp, &msgpool[next * msginfo.msgssz], tlen);
664 mutex_enter(&msgmutex);
665 if (error != 0) {
666 MSG_PRINTF(("error %d copying in message segment\n",
667 error));
668 msg_freehdr(msghdr);
669 msqptr->msg_perm.mode &= ~MSG_LOCKED;
670 cv_broadcast(&msq->msq_cv);
671 goto unlock;
672 }
673 msgsz -= tlen;
674 user_msgp += tlen;
675 next = msgmaps[next].next;
676 }
677 if (next != -1)
678 panic("didn't use all the msg segments");
679
680 /*
681 * We've got the message. Unlock the msqid_ds.
682 */
683
684 msqptr->msg_perm.mode &= ~MSG_LOCKED;
685
686 /*
687 * Make sure that the msqid_ds is still allocated.
688 */
689
690 if (msqptr->msg_qbytes == 0) {
691 msg_freehdr(msghdr);
692 cv_broadcast(&msq->msq_cv);
693 error = EIDRM;
694 goto unlock;
695 }
696
697 /*
698 * Put the message into the queue
699 */
700
701 if (msqptr->_msg_first == NULL) {
702 msqptr->_msg_first = msghdr;
703 msqptr->_msg_last = msghdr;
704 } else {
705 msqptr->_msg_last->msg_next = msghdr;
706 msqptr->_msg_last = msghdr;
707 }
708 msqptr->_msg_last->msg_next = NULL;
709
710 msqptr->_msg_cbytes += msghdr->msg_ts;
711 msqptr->msg_qnum++;
712 msqptr->msg_lspid = l->l_proc->p_pid;
713 msqptr->msg_stime = time_second;
714
715 cv_broadcast(&msq->msq_cv);
716
717 unlock:
718 mutex_exit(&msgmutex);
719 return error;
720 }
721
722 int
723 sys_msgrcv(struct lwp *l, void *v, register_t *retval)
724 {
725 struct sys_msgrcv_args /* {
726 syscallarg(int) msqid;
727 syscallarg(void *) msgp;
728 syscallarg(size_t) msgsz;
729 syscallarg(long) msgtyp;
730 syscallarg(int) msgflg;
731 } */ *uap = v;
732
733 return msgrcv1(l, SCARG(uap, msqid), SCARG(uap, msgp),
734 SCARG(uap, msgsz), SCARG(uap, msgtyp), SCARG(uap, msgflg),
735 sizeof(long), copyout, retval);
736 }
737
738 int
739 msgrcv1(struct lwp *l, int msqidr, char *user_msgp, size_t msgsz, long msgtyp,
740 int msgflg, size_t typesz, copyout_t put_type, register_t *retval)
741 {
742 size_t len;
743 kauth_cred_t cred = l->l_cred;
744 struct msqid_ds *msqptr;
745 struct __msg *msghdr;
746 int error = 0, msqid;
747 kmsq_t *msq;
748 short next;
749
750 MSG_PRINTF(("call to msgrcv(%d, %p, %lld, %ld, %d)\n", msqid,
751 user_msgp, (long long)msgsz, msgtyp, msgflg));
752
753 msqid = IPCID_TO_IX(msqidr);
754
755 mutex_enter(&msgmutex);
756
757 if (msqid < 0 || msqid >= msginfo.msgmni) {
758 MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
759 msginfo.msgmni));
760 error = EINVAL;
761 goto unlock;
762 }
763
764 msq = &msqs[msqid];
765 msqptr = &msq->msq_u;
766
767 if (msqptr->msg_qbytes == 0) {
768 MSG_PRINTF(("no such message queue id\n"));
769 error = EINVAL;
770 goto unlock;
771 }
772 if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
773 MSG_PRINTF(("wrong sequence number\n"));
774 error = EINVAL;
775 goto unlock;
776 }
777
778 if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
779 MSG_PRINTF(("requester doesn't have read access\n"));
780 goto unlock;
781 }
782
783 #if 0
784 /* cannot happen, msgsz is unsigned */
785 if (msgsz < 0) {
786 MSG_PRINTF(("msgsz < 0\n"));
787 error = EINVAL;
788 goto unlock;
789 }
790 #endif
791
792 msghdr = NULL;
793 while (msghdr == NULL) {
794 if (msgtyp == 0) {
795 msghdr = msqptr->_msg_first;
796 if (msghdr != NULL) {
797 if (msgsz < msghdr->msg_ts &&
798 (msgflg & MSG_NOERROR) == 0) {
799 MSG_PRINTF(("first message on the "
800 "queue is too big "
801 "(want %lld, got %d)\n",
802 (long long)msgsz, msghdr->msg_ts));
803 error = E2BIG;
804 goto unlock;
805 }
806 if (msqptr->_msg_first == msqptr->_msg_last) {
807 msqptr->_msg_first = NULL;
808 msqptr->_msg_last = NULL;
809 } else {
810 msqptr->_msg_first = msghdr->msg_next;
811 if (msqptr->_msg_first == NULL)
812 panic("msg_first/last screwed "
813 "up #1");
814 }
815 }
816 } else {
817 struct __msg *previous;
818 struct __msg **prev;
819
820 for (previous = NULL, prev = &msqptr->_msg_first;
821 (msghdr = *prev) != NULL;
822 previous = msghdr, prev = &msghdr->msg_next) {
823 /*
824 * Is this message's type an exact match or is
825 * this message's type less than or equal to
826 * the absolute value of a negative msgtyp?
827 * Note that the second half of this test can
828 * NEVER be true if msgtyp is positive since
829 * msg_type is always positive!
830 */
831
832 if (msgtyp == msghdr->msg_type ||
833 msghdr->msg_type <= -msgtyp) {
834 MSG_PRINTF(("found message type %ld, "
835 "requested %ld\n",
836 msghdr->msg_type, msgtyp));
837 if (msgsz < msghdr->msg_ts &&
838 (msgflg & MSG_NOERROR) == 0) {
839 MSG_PRINTF(("requested message "
840 "on the queue is too big "
841 "(want %lld, got %d)\n",
842 (long long)msgsz,
843 msghdr->msg_ts));
844 error = E2BIG;
845 goto unlock;
846 }
847 *prev = msghdr->msg_next;
848 if (msghdr == msqptr->_msg_last) {
849 if (previous == NULL) {
850 if (prev !=
851 &msqptr->_msg_first)
852 panic("msg_first/last screwed up #2");
853 msqptr->_msg_first =
854 NULL;
855 msqptr->_msg_last =
856 NULL;
857 } else {
858 if (prev ==
859 &msqptr->_msg_first)
860 panic("msg_first/last screwed up #3");
861 msqptr->_msg_last =
862 previous;
863 }
864 }
865 break;
866 }
867 }
868 }
869
870 /*
871 * We've either extracted the msghdr for the appropriate
872 * message or there isn't one.
873 * If there is one then bail out of this loop.
874 */
875
876 if (msghdr != NULL)
877 break;
878
879 /*
880 * Hmph! No message found. Does the user want to wait?
881 */
882
883 if ((msgflg & IPC_NOWAIT) != 0) {
884 MSG_PRINTF(("no appropriate message found (msgtyp=%ld)\n",
885 msgtyp));
886 /* The SVID says to return ENOMSG. */
887 #ifdef ENOMSG
888 error = ENOMSG;
889 #else
890 /* Unfortunately, BSD doesn't define that code yet! */
891 error = EAGAIN;
892 #endif
893 goto unlock;
894 }
895
896 /*
897 * Wait for something to happen
898 */
899
900 MSG_PRINTF(("msgrcv: goodnight\n"));
901 error = cv_wait_sig(&msq->msq_cv, &msgmutex);
902 MSG_PRINTF(("msgrcv: good morning (error=%d)\n", error));
903
904 if (error != 0) {
905 MSG_PRINTF(("msgsnd: interrupted system call\n"));
906 error = EINTR;
907 goto unlock;
908 }
909
910 /*
911 * Make sure that the msq queue still exists
912 */
913
914 if (msqptr->msg_qbytes == 0 ||
915 msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
916 MSG_PRINTF(("msqid deleted\n"));
917 error = EIDRM;
918 goto unlock;
919 }
920 }
921
922 /*
923 * Return the message to the user.
924 *
925 * First, do the bookkeeping (before we risk being interrupted).
926 */
927
928 msqptr->_msg_cbytes -= msghdr->msg_ts;
929 msqptr->msg_qnum--;
930 msqptr->msg_lrpid = l->l_proc->p_pid;
931 msqptr->msg_rtime = time_second;
932
933 /*
934 * Make msgsz the actual amount that we'll be returning.
935 * Note that this effectively truncates the message if it is too long
936 * (since msgsz is never increased).
937 */
938
939 MSG_PRINTF(("found a message, msgsz=%lld, msg_ts=%d\n",
940 (long long)msgsz, msghdr->msg_ts));
941 if (msgsz > msghdr->msg_ts)
942 msgsz = msghdr->msg_ts;
943
944 /*
945 * Return the type to the user.
946 */
947 mutex_exit(&msgmutex);
948 error = (*put_type)(&msghdr->msg_type, user_msgp, typesz);
949 mutex_enter(&msgmutex);
950 if (error != 0) {
951 MSG_PRINTF(("error (%d) copying out message type\n", error));
952 msg_freehdr(msghdr);
953 cv_broadcast(&msq->msq_cv);
954 goto unlock;
955 }
956 user_msgp += typesz;
957
958 /*
959 * Return the segments to the user
960 */
961
962 next = msghdr->msg_spot;
963 for (len = 0; len < msgsz; len += msginfo.msgssz) {
964 size_t tlen;
965
966 if (msgsz - len > msginfo.msgssz)
967 tlen = msginfo.msgssz;
968 else
969 tlen = msgsz - len;
970 if (next <= -1)
971 panic("next too low #3");
972 if (next >= msginfo.msgseg)
973 panic("next out of range #3");
974 mutex_exit(&msgmutex);
975 error = copyout(&msgpool[next * msginfo.msgssz],
976 user_msgp, tlen);
977 mutex_enter(&msgmutex);
978 if (error != 0) {
979 MSG_PRINTF(("error (%d) copying out message segment\n",
980 error));
981 msg_freehdr(msghdr);
982 cv_broadcast(&msq->msq_cv);
983 goto unlock;
984 }
985 user_msgp += tlen;
986 next = msgmaps[next].next;
987 }
988
989 /*
990 * Done, return the actual number of bytes copied out.
991 */
992
993 msg_freehdr(msghdr);
994 cv_broadcast(&msq->msq_cv);
995 *retval = msgsz;
996
997 unlock:
998 mutex_exit(&msgmutex);
999 return error;
1000 }
1001