]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sysv_msg.c
xnu-517.11.1.tar.gz
[apple/xnu.git] / bsd / kern / sysv_msg.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Implementation of SVID messages
24 *
25 * Author: Daniel Boulet
26 *
27 * Copyright 1993 Daniel Boulet and RTMX Inc.
28 *
29 * This system call was implemented by Daniel Boulet under contract from RTMX.
30 *
31 * Redistribution and use in source forms, with and without modification,
32 * are permitted provided that this entire comment appears intact.
33 *
34 * Redistribution in binary form may occur without any restrictions.
35 * Obviously, it would be nice if you gave credit where credit is due
36 * but requiring it would be too onerous.
37 *
38 * This software is provided ``AS IS'' without any warranties of any kind.
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysproto.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/msg.h>
47 #include <sys/sysent.h>
48
49 #include <bsm/audit_kernel.h>
50
51 static void msginit __P((void *));
52 SYSINIT(sysv_msg, SI_SUB_SYSV_MSG, SI_ORDER_FIRST, msginit, NULL)
53
54 #define MSG_DEBUG
55 #undef MSG_DEBUG_OK
56
57 #ifndef _SYS_SYSPROTO_H_
58 struct msgctl_args;
59 int msgctl __P((struct proc *p, struct msgctl_args *uap));
60 struct msgget_args;
61 int msgget __P((struct proc *p, struct msgget_args *uap));
62 struct msgsnd_args;
63 int msgsnd __P((struct proc *p, struct msgsnd_args *uap));
64 struct msgrcv_args;
65 int msgrcv __P((struct proc *p, struct msgrcv_args *uap));
66 #endif
67 static void msg_freehdr __P((struct msg *msghdr));
68
69 /* XXX casting to (sy_call_t *) is bogus, as usual. */
70 static sy_call_t *msgcalls[] = {
71 (sy_call_t *)msgctl, (sy_call_t *)msgget,
72 (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
73 };
74
75 static int nfree_msgmaps; /* # of free map entries */
76 static short free_msgmaps; /* head of linked list of free map entries */
77 static struct msg *free_msghdrs; /* list of free msg headers */
78 char *msgpool; /* MSGMAX byte long msg buffer pool */
79 struct msgmap *msgmaps; /* MSGSEG msgmap structures */
80 struct msg *msghdrs; /* MSGTQL msg headers */
81 struct msqid_ds *msqids; /* MSGMNI msqid_ds struct's */
82
83 void
84 msginit(dummy)
85 void *dummy;
86 {
87 register int i;
88
89 /*
90 * msginfo.msgssz should be a power of two for efficiency reasons.
91 * It is also pretty silly if msginfo.msgssz is less than 8
92 * or greater than about 256 so ...
93 */
94
95 i = 8;
96 while (i < 1024 && i != msginfo.msgssz)
97 i <<= 1;
98 if (i != msginfo.msgssz) {
99 printf("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
100 msginfo.msgssz);
101 panic("msginfo.msgssz not a small power of 2");
102 }
103
104 if (msginfo.msgseg > 32767) {
105 printf("msginfo.msgseg=%d\n", msginfo.msgseg);
106 panic("msginfo.msgseg > 32767");
107 }
108
109 if (msgmaps == NULL)
110 panic("msgmaps is NULL");
111
112 for (i = 0; i < msginfo.msgseg; i++) {
113 if (i > 0)
114 msgmaps[i-1].next = i;
115 msgmaps[i].next = -1; /* implies entry is available */
116 }
117 free_msgmaps = 0;
118 nfree_msgmaps = msginfo.msgseg;
119
120 if (msghdrs == NULL)
121 panic("msghdrs is NULL");
122
123 for (i = 0; i < msginfo.msgtql; i++) {
124 msghdrs[i].msg_type = 0;
125 if (i > 0)
126 msghdrs[i-1].msg_next = &msghdrs[i];
127 msghdrs[i].msg_next = NULL;
128 }
129 free_msghdrs = &msghdrs[0];
130
131 if (msqids == NULL)
132 panic("msqids is NULL");
133
134 for (i = 0; i < msginfo.msgmni; i++) {
135 msqids[i].msg_qbytes = 0; /* implies entry is available */
136 msqids[i].msg_perm.seq = 0; /* reset to a known value */
137 }
138 }
139
140 /*
141 * Entry point for all MSG calls
142 */
143 int
144 msgsys(p, uap)
145 struct proc *p;
146 /* XXX actually varargs. */
147 struct msgsys_args /* {
148 u_int which;
149 int a2;
150 int a3;
151 int a4;
152 int a5;
153 int a6;
154 } */ *uap;
155 {
156
157 if (uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
158 return (EINVAL);
159 return ((*msgcalls[uap->which])(p, &uap->a2));
160 }
161
162 static void
163 msg_freehdr(msghdr)
164 struct msg *msghdr;
165 {
166 while (msghdr->msg_ts > 0) {
167 short next;
168 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
169 panic("msghdr->msg_spot out of range");
170 next = msgmaps[msghdr->msg_spot].next;
171 msgmaps[msghdr->msg_spot].next = free_msgmaps;
172 free_msgmaps = msghdr->msg_spot;
173 nfree_msgmaps++;
174 msghdr->msg_spot = next;
175 if (msghdr->msg_ts >= msginfo.msgssz)
176 msghdr->msg_ts -= msginfo.msgssz;
177 else
178 msghdr->msg_ts = 0;
179 }
180 if (msghdr->msg_spot != -1)
181 panic("msghdr->msg_spot != -1");
182 msghdr->msg_next = free_msghdrs;
183 free_msghdrs = msghdr;
184 }
185
186 #ifndef _SYS_SYSPROTO_H_
187 struct msgctl_args {
188 int msqid;
189 int cmd;
190 struct msqid_ds *buf;
191 };
192 #endif
193
194 int
195 msgctl(p, uap)
196 struct proc *p;
197 register struct msgctl_args *uap;
198 {
199 int msqid = uap->msqid;
200 int cmd = uap->cmd;
201 struct msqid_ds *user_msqptr = uap->buf;
202 struct ucred *cred = p->p_ucred;
203 int rval, eval;
204 struct msqid_ds msqbuf;
205 register struct msqid_ds *msqptr;
206
207 #ifdef MSG_DEBUG_OK
208 printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr);
209 #endif
210
211 AUDIT_ARG(svipc_cmd, cmd);
212 AUDIT_ARG(svipc_id, msqid);
213 msqid = IPCID_TO_IX(msqid);
214
215 if (msqid < 0 || msqid >= msginfo.msgmni) {
216 #ifdef MSG_DEBUG_OK
217 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
218 msginfo.msgmni);
219 #endif
220 return(EINVAL);
221 }
222
223 msqptr = &msqids[msqid];
224
225 if (msqptr->msg_qbytes == 0) {
226 #ifdef MSG_DEBUG_OK
227 printf("no such msqid\n");
228 #endif
229 return(EINVAL);
230 }
231 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
232 #ifdef MSG_DEBUG_OK
233 printf("wrong sequence number\n");
234 #endif
235 return(EINVAL);
236 }
237
238 eval = 0;
239 rval = 0;
240
241 switch (cmd) {
242
243 case IPC_RMID:
244 {
245 struct msg *msghdr;
246 if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_M)))
247 return(eval);
248 /* Free the message headers */
249 msghdr = msqptr->msg_first;
250 while (msghdr != NULL) {
251 struct msg *msghdr_tmp;
252
253 /* Free the segments of each message */
254 msqptr->msg_cbytes -= msghdr->msg_ts;
255 msqptr->msg_qnum--;
256 msghdr_tmp = msghdr;
257 msghdr = msghdr->msg_next;
258 msg_freehdr(msghdr_tmp);
259 }
260
261 if (msqptr->msg_cbytes != 0)
262 panic("msg_cbytes is messed up");
263 if (msqptr->msg_qnum != 0)
264 panic("msg_qnum is messed up");
265
266 msqptr->msg_qbytes = 0; /* Mark it as free */
267
268 wakeup((caddr_t)msqptr);
269 }
270
271 break;
272
273 case IPC_SET:
274 if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_M)))
275 return(eval);
276 if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
277 return(eval);
278 if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
279 eval = suser(cred, &p->p_acflag);
280 if (eval)
281 return(eval);
282 }
283 if (msqbuf.msg_qbytes > msginfo.msgmnb) {
284 #ifdef MSG_DEBUG_OK
285 printf("can't increase msg_qbytes beyond %d (truncating)\n",
286 msginfo.msgmnb);
287 #endif
288 msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
289 }
290 if (msqbuf.msg_qbytes == 0) {
291 #ifdef MSG_DEBUG_OK
292 printf("can't reduce msg_qbytes to 0\n");
293 #endif
294 return(EINVAL); /* non-standard errno! */
295 }
296 msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */
297 msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */
298 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
299 (msqbuf.msg_perm.mode & 0777);
300 msqptr->msg_qbytes = msqbuf.msg_qbytes;
301 msqptr->msg_ctime = time_second;
302 break;
303
304 case IPC_STAT:
305 if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
306 #ifdef MSG_DEBUG_OK
307 printf("requester doesn't have read access\n");
308 #endif
309 return(eval);
310 }
311 eval = copyout((caddr_t)msqptr, user_msqptr,
312 sizeof(struct msqid_ds));
313 break;
314
315 default:
316 #ifdef MSG_DEBUG_OK
317 printf("invalid command %d\n", cmd);
318 #endif
319 return(EINVAL);
320 }
321
322 if (eval == 0)
323 p->p_retval[0] = rval;
324 return(eval);
325 }
326
327 #ifndef _SYS_SYSPROTO_H_
328 struct msgget_args {
329 key_t key;
330 int msgflg;
331 };
332 #endif
333
334 int
335 msgget(p, uap)
336 struct proc *p;
337 register struct msgget_args *uap;
338 {
339 int msqid, eval;
340 int key = uap->key;
341 int msgflg = uap->msgflg;
342 struct ucred *cred = p->p_ucred;
343 register struct msqid_ds *msqptr = NULL;
344
345 #ifdef MSG_DEBUG_OK
346 printf("msgget(0x%x, 0%o)\n", key, msgflg);
347 #endif
348
349 if (key != IPC_PRIVATE) {
350 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
351 msqptr = &msqids[msqid];
352 if (msqptr->msg_qbytes != 0 &&
353 msqptr->msg_perm.key == key)
354 break;
355 }
356 if (msqid < msginfo.msgmni) {
357 #ifdef MSG_DEBUG_OK
358 printf("found public key\n");
359 #endif
360 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
361 #ifdef MSG_DEBUG_OK
362 printf("not exclusive\n");
363 #endif
364 return(EEXIST);
365 }
366 if ((eval = ipcperm(cred, &msqptr->msg_perm, msgflg & 0700 ))) {
367 #ifdef MSG_DEBUG_OK
368 printf("requester doesn't have 0%o access\n",
369 msgflg & 0700);
370 #endif
371 return(eval);
372 }
373 goto found;
374 }
375 }
376
377 #ifdef MSG_DEBUG_OK
378 printf("need to allocate the msqid_ds\n");
379 #endif
380 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
381 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
382 /*
383 * Look for an unallocated and unlocked msqid_ds.
384 * msqid_ds's can be locked by msgsnd or msgrcv while
385 * they are copying the message in/out. We can't
386 * re-use the entry until they release it.
387 */
388 msqptr = &msqids[msqid];
389 if (msqptr->msg_qbytes == 0 &&
390 (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
391 break;
392 }
393 if (msqid == msginfo.msgmni) {
394 #ifdef MSG_DEBUG_OK
395 printf("no more msqid_ds's available\n");
396 #endif
397 return(ENOSPC);
398 }
399 #ifdef MSG_DEBUG_OK
400 printf("msqid %d is available\n", msqid);
401 #endif
402 msqptr->msg_perm.key = key;
403 msqptr->msg_perm.cuid = cred->cr_uid;
404 msqptr->msg_perm.uid = cred->cr_uid;
405 msqptr->msg_perm.cgid = cred->cr_gid;
406 msqptr->msg_perm.gid = cred->cr_gid;
407 msqptr->msg_perm.mode = (msgflg & 0777);
408 /* Make sure that the returned msqid is unique */
409 msqptr->msg_perm.seq++;
410 msqptr->msg_first = NULL;
411 msqptr->msg_last = NULL;
412 msqptr->msg_cbytes = 0;
413 msqptr->msg_qnum = 0;
414 msqptr->msg_qbytes = msginfo.msgmnb;
415 msqptr->msg_lspid = 0;
416 msqptr->msg_lrpid = 0;
417 msqptr->msg_stime = 0;
418 msqptr->msg_rtime = 0;
419 msqptr->msg_ctime = time_second;
420 } else {
421 #ifdef MSG_DEBUG_OK
422 printf("didn't find it and wasn't asked to create it\n");
423 #endif
424 return(ENOENT);
425 }
426
427 found:
428 /* Construct the unique msqid */
429 p->p_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
430 AUDIT_ARG(svipc_id, p->p_retval[0]);
431 return(0);
432 }
433
434 #ifndef _SYS_SYSPROTO_H_
435 struct msgsnd_args {
436 int msqid;
437 void *msgp;
438 size_t msgsz;
439 int msgflg;
440 };
441 #endif
442
443 int
444 msgsnd(p, uap)
445 struct proc *p;
446 register struct msgsnd_args *uap;
447 {
448 int msqid = uap->msqid;
449 void *user_msgp = uap->msgp;
450 size_t msgsz = uap->msgsz;
451 int msgflg = uap->msgflg;
452 int segs_needed, eval;
453 struct ucred *cred = p->p_ucred;
454 register struct msqid_ds *msqptr;
455 register struct msg *msghdr;
456 short next;
457
458 #ifdef MSG_DEBUG_OK
459 printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
460 msgflg);
461 #endif
462
463 AUDIT_ARG(svipc_id, msqid);
464 msqid = IPCID_TO_IX(msqid);
465
466 if (msqid < 0 || msqid >= msginfo.msgmni) {
467 #ifdef MSG_DEBUG_OK
468 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
469 msginfo.msgmni);
470 #endif
471 return(EINVAL);
472 }
473
474 msqptr = &msqids[msqid];
475 if (msqptr->msg_qbytes == 0) {
476 #ifdef MSG_DEBUG_OK
477 printf("no such message queue id\n");
478 #endif
479 return(EINVAL);
480 }
481 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
482 #ifdef MSG_DEBUG_OK
483 printf("wrong sequence number\n");
484 #endif
485 return(EINVAL);
486 }
487
488 if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_W))) {
489 #ifdef MSG_DEBUG_OK
490 printf("requester doesn't have write access\n");
491 #endif
492 return(eval);
493 }
494
495 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
496 #ifdef MSG_DEBUG_OK
497 printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
498 segs_needed);
499 #endif
500 for (;;) {
501 int need_more_resources = 0;
502
503 /*
504 * check msgsz
505 * (inside this loop in case msg_qbytes changes while we sleep)
506 */
507
508 if (msgsz > msqptr->msg_qbytes) {
509 #ifdef MSG_DEBUG_OK
510 printf("msgsz > msqptr->msg_qbytes\n");
511 #endif
512 return(EINVAL);
513 }
514
515 if (msqptr->msg_perm.mode & MSG_LOCKED) {
516 #ifdef MSG_DEBUG_OK
517 printf("msqid is locked\n");
518 #endif
519 need_more_resources = 1;
520 }
521 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
522 #ifdef MSG_DEBUG_OK
523 printf("msgsz + msg_cbytes > msg_qbytes\n");
524 #endif
525 need_more_resources = 1;
526 }
527 if (segs_needed > nfree_msgmaps) {
528 #ifdef MSG_DEBUG_OK
529 printf("segs_needed > nfree_msgmaps\n");
530 #endif
531 need_more_resources = 1;
532 }
533 if (free_msghdrs == NULL) {
534 #ifdef MSG_DEBUG_OK
535 printf("no more msghdrs\n");
536 #endif
537 need_more_resources = 1;
538 }
539
540 if (need_more_resources) {
541 int we_own_it;
542
543 if ((msgflg & IPC_NOWAIT) != 0) {
544 #ifdef MSG_DEBUG_OK
545 printf("need more resources but caller doesn't want to wait\n");
546 #endif
547 return(EAGAIN);
548 }
549
550 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
551 #ifdef MSG_DEBUG_OK
552 printf("we don't own the msqid_ds\n");
553 #endif
554 we_own_it = 0;
555 } else {
556 /* Force later arrivals to wait for our
557 request */
558 #ifdef MSG_DEBUG_OK
559 printf("we own the msqid_ds\n");
560 #endif
561 msqptr->msg_perm.mode |= MSG_LOCKED;
562 we_own_it = 1;
563 }
564 #ifdef MSG_DEBUG_OK
565 printf("goodnight\n");
566 #endif
567 eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH,
568 "msgwait", 0);
569 #ifdef MSG_DEBUG_OK
570 printf("good morning, eval=%d\n", eval);
571 #endif
572 if (we_own_it)
573 msqptr->msg_perm.mode &= ~MSG_LOCKED;
574 if (eval != 0) {
575 #ifdef MSG_DEBUG_OK
576 printf("msgsnd: interrupted system call\n");
577 #endif
578 return(EINTR);
579 }
580
581 /*
582 * Make sure that the msq queue still exists
583 */
584
585 if (msqptr->msg_qbytes == 0) {
586 #ifdef MSG_DEBUG_OK
587 printf("msqid deleted\n");
588 #endif
589 /* The SVID says to return EIDRM. */
590 #ifdef EIDRM
591 return(EIDRM);
592 #else
593 /* Unfortunately, BSD doesn't define that code
594 yet! */
595 return(EINVAL);
596 #endif
597 }
598
599 } else {
600 #ifdef MSG_DEBUG_OK
601 printf("got all the resources that we need\n");
602 #endif
603 break;
604 }
605 }
606
607 /*
608 * We have the resources that we need.
609 * Make sure!
610 */
611
612 if (msqptr->msg_perm.mode & MSG_LOCKED)
613 panic("msg_perm.mode & MSG_LOCKED");
614 if (segs_needed > nfree_msgmaps)
615 panic("segs_needed > nfree_msgmaps");
616 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
617 panic("msgsz + msg_cbytes > msg_qbytes");
618 if (free_msghdrs == NULL)
619 panic("no more msghdrs");
620
621 /*
622 * Re-lock the msqid_ds in case we page-fault when copying in the
623 * message
624 */
625
626 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
627 panic("msqid_ds is already locked");
628 msqptr->msg_perm.mode |= MSG_LOCKED;
629
630 /*
631 * Allocate a message header
632 */
633
634 msghdr = free_msghdrs;
635 free_msghdrs = msghdr->msg_next;
636 msghdr->msg_spot = -1;
637 msghdr->msg_ts = msgsz;
638
639 /*
640 * Allocate space for the message
641 */
642
643 while (segs_needed > 0) {
644 if (nfree_msgmaps <= 0)
645 panic("not enough msgmaps");
646 if (free_msgmaps == -1)
647 panic("nil free_msgmaps");
648 next = free_msgmaps;
649 if (next <= -1)
650 panic("next too low #1");
651 if (next >= msginfo.msgseg)
652 panic("next out of range #1");
653 #ifdef MSG_DEBUG_OK
654 printf("allocating segment %d to message\n", next);
655 #endif
656 free_msgmaps = msgmaps[next].next;
657 nfree_msgmaps--;
658 msgmaps[next].next = msghdr->msg_spot;
659 msghdr->msg_spot = next;
660 segs_needed--;
661 }
662
663 /*
664 * Copy in the message type
665 */
666
667 if ((eval = copyin(user_msgp, &msghdr->msg_type,
668 sizeof(msghdr->msg_type))) != 0) {
669 #ifdef MSG_DEBUG_OK
670 printf("error %d copying the message type\n", eval);
671 #endif
672 msg_freehdr(msghdr);
673 msqptr->msg_perm.mode &= ~MSG_LOCKED;
674 wakeup((caddr_t)msqptr);
675 return(eval);
676 }
677 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
678
679 /*
680 * Validate the message type
681 */
682
683 if (msghdr->msg_type < 1) {
684 msg_freehdr(msghdr);
685 msqptr->msg_perm.mode &= ~MSG_LOCKED;
686 wakeup((caddr_t)msqptr);
687 #ifdef MSG_DEBUG_OK
688 printf("mtype (%d) < 1\n", msghdr->msg_type);
689 #endif
690 return(EINVAL);
691 }
692
693 /*
694 * Copy in the message body
695 */
696
697 next = msghdr->msg_spot;
698 while (msgsz > 0) {
699 size_t tlen;
700 if (msgsz > msginfo.msgssz)
701 tlen = msginfo.msgssz;
702 else
703 tlen = msgsz;
704 if (next <= -1)
705 panic("next too low #2");
706 if (next >= msginfo.msgseg)
707 panic("next out of range #2");
708 if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
709 tlen)) != 0) {
710 #ifdef MSG_DEBUG_OK
711 printf("error %d copying in message segment\n", eval);
712 #endif
713 msg_freehdr(msghdr);
714 msqptr->msg_perm.mode &= ~MSG_LOCKED;
715 wakeup((caddr_t)msqptr);
716 return(eval);
717 }
718 msgsz -= tlen;
719 user_msgp = (char *)user_msgp + tlen;
720 next = msgmaps[next].next;
721 }
722 if (next != -1)
723 panic("didn't use all the msg segments");
724
725 /*
726 * We've got the message. Unlock the msqid_ds.
727 */
728
729 msqptr->msg_perm.mode &= ~MSG_LOCKED;
730
731 /*
732 * Make sure that the msqid_ds is still allocated.
733 */
734
735 if (msqptr->msg_qbytes == 0) {
736 msg_freehdr(msghdr);
737 wakeup((caddr_t)msqptr);
738 /* The SVID says to return EIDRM. */
739 #ifdef EIDRM
740 return(EIDRM);
741 #else
742 /* Unfortunately, BSD doesn't define that code yet! */
743 return(EINVAL);
744 #endif
745 }
746
747 /*
748 * Put the message into the queue
749 */
750
751 if (msqptr->msg_first == NULL) {
752 msqptr->msg_first = msghdr;
753 msqptr->msg_last = msghdr;
754 } else {
755 msqptr->msg_last->msg_next = msghdr;
756 msqptr->msg_last = msghdr;
757 }
758 msqptr->msg_last->msg_next = NULL;
759
760 msqptr->msg_cbytes += msghdr->msg_ts;
761 msqptr->msg_qnum++;
762 msqptr->msg_lspid = p->p_pid;
763 msqptr->msg_stime = time_second;
764
765 wakeup((caddr_t)msqptr);
766 p->p_retval[0] = 0;
767 return(0);
768 }
769
770 #ifndef _SYS_SYSPROTO_H_
771 struct msgrcv_args {
772 int msqid;
773 void *msgp;
774 size_t msgsz;
775 long msgtyp;
776 int msgflg;
777 };
778 #endif
779
780 int
781 msgrcv(p, uap)
782 struct proc *p;
783 register struct msgrcv_args *uap;
784 {
785 int msqid = uap->msqid;
786 void *user_msgp = uap->msgp;
787 size_t msgsz = uap->msgsz;
788 long msgtyp = uap->msgtyp;
789 int msgflg = uap->msgflg;
790 size_t len;
791 struct ucred *cred = p->p_ucred;
792 register struct msqid_ds *msqptr;
793 register struct msg *msghdr;
794 int eval;
795 short next;
796
797 #ifdef MSG_DEBUG_OK
798 printf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
799 msgsz, msgtyp, msgflg);
800 #endif
801
802 AUDIT_ARG(svipc_id, msqid);
803 msqid = IPCID_TO_IX(msqid);
804
805 if (msqid < 0 || msqid >= msginfo.msgmni) {
806 #ifdef MSG_DEBUG_OK
807 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
808 msginfo.msgmni);
809 #endif
810 return(EINVAL);
811 }
812
813 msqptr = &msqids[msqid];
814 if (msqptr->msg_qbytes == 0) {
815 #ifdef MSG_DEBUG_OK
816 printf("no such message queue id\n");
817 #endif
818 return(EINVAL);
819 }
820 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
821 #ifdef MSG_DEBUG_OK
822 printf("wrong sequence number\n");
823 #endif
824 return(EINVAL);
825 }
826
827 if ((eval = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
828 #ifdef MSG_DEBUG_OK
829 printf("requester doesn't have read access\n");
830 #endif
831 return(eval);
832 }
833
834 msghdr = NULL;
835 while (msghdr == NULL) {
836 if (msgtyp == 0) {
837 msghdr = msqptr->msg_first;
838 if (msghdr != NULL) {
839 if (msgsz < msghdr->msg_ts &&
840 (msgflg & MSG_NOERROR) == 0) {
841 #ifdef MSG_DEBUG_OK
842 printf("first message on the queue is too big (want %d, got %d)\n",
843 msgsz, msghdr->msg_ts);
844 #endif
845 return(E2BIG);
846 }
847 if (msqptr->msg_first == msqptr->msg_last) {
848 msqptr->msg_first = NULL;
849 msqptr->msg_last = NULL;
850 } else {
851 msqptr->msg_first = msghdr->msg_next;
852 if (msqptr->msg_first == NULL)
853 panic("msg_first/last messed up #1");
854 }
855 }
856 } else {
857 struct msg *previous;
858 struct msg **prev;
859
860 previous = NULL;
861 prev = &(msqptr->msg_first);
862 while ((msghdr = *prev) != NULL) {
863 /*
864 * Is this message's type an exact match or is
865 * this message's type less than or equal to
866 * the absolute value of a negative msgtyp?
867 * Note that the second half of this test can
868 * NEVER be true if msgtyp is positive since
869 * msg_type is always positive!
870 */
871
872 if (msgtyp == msghdr->msg_type ||
873 msghdr->msg_type <= -msgtyp) {
874 #ifdef MSG_DEBUG_OK
875 printf("found message type %d, requested %d\n",
876 msghdr->msg_type, msgtyp);
877 #endif
878 if (msgsz < msghdr->msg_ts &&
879 (msgflg & MSG_NOERROR) == 0) {
880 #ifdef MSG_DEBUG_OK
881 printf("requested message on the queue is too big (want %d, got %d)\n",
882 msgsz, msghdr->msg_ts);
883 #endif
884 return(E2BIG);
885 }
886 *prev = msghdr->msg_next;
887 if (msghdr == msqptr->msg_last) {
888 if (previous == NULL) {
889 if (prev !=
890 &msqptr->msg_first)
891 panic("msg_first/last messed up #2");
892 msqptr->msg_first =
893 NULL;
894 msqptr->msg_last =
895 NULL;
896 } else {
897 if (prev ==
898 &msqptr->msg_first)
899 panic("msg_first/last messed up #3");
900 msqptr->msg_last =
901 previous;
902 }
903 }
904 break;
905 }
906 previous = msghdr;
907 prev = &(msghdr->msg_next);
908 }
909 }
910
911 /*
912 * We've either extracted the msghdr for the appropriate
913 * message or there isn't one.
914 * If there is one then bail out of this loop.
915 */
916
917 if (msghdr != NULL)
918 break;
919
920 /*
921 * Hmph! No message found. Does the user want to wait?
922 */
923
924 if ((msgflg & IPC_NOWAIT) != 0) {
925 #ifdef MSG_DEBUG_OK
926 printf("no appropriate message found (msgtyp=%d)\n",
927 msgtyp);
928 #endif
929 /* The SVID says to return ENOMSG. */
930 #ifdef ENOMSG
931 return(ENOMSG);
932 #else
933 /* Unfortunately, BSD doesn't define that code yet! */
934 return(EAGAIN);
935 #endif
936 }
937
938 /*
939 * Wait for something to happen
940 */
941
942 #ifdef MSG_DEBUG_OK
943 printf("msgrcv: goodnight\n");
944 #endif
945 eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH, "msgwait",
946 0);
947 #ifdef MSG_DEBUG_OK
948 printf("msgrcv: good morning (eval=%d)\n", eval);
949 #endif
950
951 if (eval != 0) {
952 #ifdef MSG_DEBUG_OK
953 printf("msgsnd: interrupted system call\n");
954 #endif
955 return(EINTR);
956 }
957
958 /*
959 * Make sure that the msq queue still exists
960 */
961
962 if (msqptr->msg_qbytes == 0 ||
963 msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
964 #ifdef MSG_DEBUG_OK
965 printf("msqid deleted\n");
966 #endif
967 /* The SVID says to return EIDRM. */
968 #ifdef EIDRM
969 return(EIDRM);
970 #else
971 /* Unfortunately, BSD doesn't define that code yet! */
972 return(EINVAL);
973 #endif
974 }
975 }
976
977 /*
978 * Return the message to the user.
979 *
980 * First, do the bookkeeping (before we risk being interrupted).
981 */
982
983 msqptr->msg_cbytes -= msghdr->msg_ts;
984 msqptr->msg_qnum--;
985 msqptr->msg_lrpid = p->p_pid;
986 msqptr->msg_rtime = time_second;
987
988 /*
989 * Make msgsz the actual amount that we'll be returning.
990 * Note that this effectively truncates the message if it is too long
991 * (since msgsz is never increased).
992 */
993
994 #ifdef MSG_DEBUG_OK
995 printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
996 msghdr->msg_ts);
997 #endif
998 if (msgsz > msghdr->msg_ts)
999 msgsz = msghdr->msg_ts;
1000
1001 /*
1002 * Return the type to the user.
1003 */
1004
1005 eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp,
1006 sizeof(msghdr->msg_type));
1007 if (eval != 0) {
1008 #ifdef MSG_DEBUG_OK
1009 printf("error (%d) copying out message type\n", eval);
1010 #endif
1011 msg_freehdr(msghdr);
1012 wakeup((caddr_t)msqptr);
1013 return(eval);
1014 }
1015 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1016
1017 /*
1018 * Return the segments to the user
1019 */
1020
1021 next = msghdr->msg_spot;
1022 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1023 size_t tlen;
1024
1025 if (msgsz > msginfo.msgssz)
1026 tlen = msginfo.msgssz;
1027 else
1028 tlen = msgsz;
1029 if (next <= -1)
1030 panic("next too low #3");
1031 if (next >= msginfo.msgseg)
1032 panic("next out of range #3");
1033 eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
1034 user_msgp, tlen);
1035 if (eval != 0) {
1036 #ifdef MSG_DEBUG_OK
1037 printf("error (%d) copying out message segment\n",
1038 eval);
1039 #endif
1040 msg_freehdr(msghdr);
1041 wakeup((caddr_t)msqptr);
1042 return(eval);
1043 }
1044 user_msgp = (char *)user_msgp + tlen;
1045 next = msgmaps[next].next;
1046 }
1047
1048 /*
1049 * Done, return the actual number of bytes copied out.
1050 */
1051
1052 msg_freehdr(msghdr);
1053 wakeup((caddr_t)msqptr);
1054 p->p_retval[0] = msgsz;
1055 return(0);
1056 }