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