Nuke huge mbuf macros stage 2/2: Cleanup the MCL*() cluster inlines by
[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.5 2003/07/19 21:14:39 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  * msgsys_args(u_int which, int a2, ...) (VARARGS)
203  */
204 int
205 msgsys(struct msgsys_args *uap)
206 {
207         struct proc *p = curproc;
208
209         if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
210                 return (ENOSYS);
211
212         if (uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
213                 return (EINVAL);
214         return ((*msgcalls[uap->which])(&uap->a2));
215 }
216
217 static void
218 msg_freehdr(struct msg *msghdr)
219 {
220         while (msghdr->msg_ts > 0) {
221                 short next;
222                 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
223                         panic("msghdr->msg_spot out of range");
224                 next = msgmaps[msghdr->msg_spot].next;
225                 msgmaps[msghdr->msg_spot].next = free_msgmaps;
226                 free_msgmaps = msghdr->msg_spot;
227                 nfree_msgmaps++;
228                 msghdr->msg_spot = next;
229                 if (msghdr->msg_ts >= msginfo.msgssz)
230                         msghdr->msg_ts -= msginfo.msgssz;
231                 else
232                         msghdr->msg_ts = 0;
233         }
234         if (msghdr->msg_spot != -1)
235                 panic("msghdr->msg_spot != -1");
236         msghdr->msg_next = free_msghdrs;
237         free_msghdrs = msghdr;
238 }
239
240 #ifndef _SYS_SYSPROTO_H_
241 struct msgctl_args {
242         int     msqid;
243         int     cmd;
244         struct  msqid_ds *buf;
245 };
246 #endif
247
248 int
249 msgctl(struct msgctl_args *uap)
250 {
251         struct thread *td = curthread;
252         struct proc *p = td->td_proc;
253         int msqid = uap->msqid;
254         int cmd = uap->cmd;
255         struct msqid_ds *user_msqptr = uap->buf;
256         int rval, eval;
257         struct msqid_ds msqbuf;
258         register struct msqid_ds *msqptr;
259
260 #ifdef MSG_DEBUG_OK
261         printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr);
262 #endif
263
264         if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
265                 return (ENOSYS);
266
267         msqid = IPCID_TO_IX(msqid);
268
269         if (msqid < 0 || msqid >= msginfo.msgmni) {
270 #ifdef MSG_DEBUG_OK
271                 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
272                     msginfo.msgmni);
273 #endif
274                 return(EINVAL);
275         }
276
277         msqptr = &msqids[msqid];
278
279         if (msqptr->msg_qbytes == 0) {
280 #ifdef MSG_DEBUG_OK
281                 printf("no such msqid\n");
282 #endif
283                 return(EINVAL);
284         }
285         if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
286 #ifdef MSG_DEBUG_OK
287                 printf("wrong sequence number\n");
288 #endif
289                 return(EINVAL);
290         }
291
292         eval = 0;
293         rval = 0;
294
295         switch (cmd) {
296
297         case IPC_RMID:
298         {
299                 struct msg *msghdr;
300                 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)))
301                         return(eval);
302                 /* Free the message headers */
303                 msghdr = msqptr->msg_first;
304                 while (msghdr != NULL) {
305                         struct msg *msghdr_tmp;
306
307                         /* Free the segments of each message */
308                         msqptr->msg_cbytes -= msghdr->msg_ts;
309                         msqptr->msg_qnum--;
310                         msghdr_tmp = msghdr;
311                         msghdr = msghdr->msg_next;
312                         msg_freehdr(msghdr_tmp);
313                 }
314
315                 if (msqptr->msg_cbytes != 0)
316                         panic("msg_cbytes is screwed up");
317                 if (msqptr->msg_qnum != 0)
318                         panic("msg_qnum is screwed up");
319
320                 msqptr->msg_qbytes = 0; /* Mark it as free */
321
322                 wakeup((caddr_t)msqptr);
323         }
324
325                 break;
326
327         case IPC_SET:
328                 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)))
329                         return(eval);
330                 if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
331                         return(eval);
332                 if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
333                         eval = suser(td);
334                         if (eval)
335                                 return(eval);
336                 }
337                 if (msqbuf.msg_qbytes > msginfo.msgmnb) {
338 #ifdef MSG_DEBUG_OK
339                         printf("can't increase msg_qbytes beyond %d (truncating)\n",
340                             msginfo.msgmnb);
341 #endif
342                         msqbuf.msg_qbytes = msginfo.msgmnb;     /* silently restrict qbytes to system limit */
343                 }
344                 if (msqbuf.msg_qbytes == 0) {
345 #ifdef MSG_DEBUG_OK
346                         printf("can't reduce msg_qbytes to 0\n");
347 #endif
348                         return(EINVAL);         /* non-standard errno! */
349                 }
350                 msqptr->msg_perm.uid = msqbuf.msg_perm.uid;     /* change the owner */
351                 msqptr->msg_perm.gid = msqbuf.msg_perm.gid;     /* change the owner */
352                 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
353                     (msqbuf.msg_perm.mode & 0777);
354                 msqptr->msg_qbytes = msqbuf.msg_qbytes;
355                 msqptr->msg_ctime = time_second;
356                 break;
357
358         case IPC_STAT:
359                 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
360 #ifdef MSG_DEBUG_OK
361                         printf("requester doesn't have read access\n");
362 #endif
363                         return(eval);
364                 }
365                 eval = copyout((caddr_t)msqptr, user_msqptr,
366                     sizeof(struct msqid_ds));
367                 break;
368
369         default:
370 #ifdef MSG_DEBUG_OK
371                 printf("invalid command %d\n", cmd);
372 #endif
373                 return(EINVAL);
374         }
375
376         if (eval == 0)
377                 p->p_retval[0] = rval;
378         return(eval);
379 }
380
381 #ifndef _SYS_SYSPROTO_H_
382 struct msgget_args {
383         key_t   key;
384         int     msgflg;
385 };
386 #endif
387
388 int
389 msgget(struct msgget_args *uap)
390 {
391         struct proc *p = curproc;
392         int msqid, eval;
393         int key = uap->key;
394         int msgflg = uap->msgflg;
395         struct ucred *cred = p->p_ucred;
396         register struct msqid_ds *msqptr = NULL;
397
398 #ifdef MSG_DEBUG_OK
399         printf("msgget(0x%x, 0%o)\n", key, msgflg);
400 #endif
401
402         if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
403                 return (ENOSYS);
404
405         if (key != IPC_PRIVATE) {
406                 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
407                         msqptr = &msqids[msqid];
408                         if (msqptr->msg_qbytes != 0 &&
409                             msqptr->msg_perm.key == key)
410                                 break;
411                 }
412                 if (msqid < msginfo.msgmni) {
413 #ifdef MSG_DEBUG_OK
414                         printf("found public key\n");
415 #endif
416                         if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
417 #ifdef MSG_DEBUG_OK
418                                 printf("not exclusive\n");
419 #endif
420                                 return(EEXIST);
421                         }
422                         if ((eval = ipcperm(p, &msqptr->msg_perm, msgflg & 0700 ))) {
423 #ifdef MSG_DEBUG_OK
424                                 printf("requester doesn't have 0%o access\n",
425                                     msgflg & 0700);
426 #endif
427                                 return(eval);
428                         }
429                         goto found;
430                 }
431         }
432
433 #ifdef MSG_DEBUG_OK
434         printf("need to allocate the msqid_ds\n");
435 #endif
436         if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
437                 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
438                         /*
439                          * Look for an unallocated and unlocked msqid_ds.
440                          * msqid_ds's can be locked by msgsnd or msgrcv while
441                          * they are copying the message in/out.  We can't
442                          * re-use the entry until they release it.
443                          */
444                         msqptr = &msqids[msqid];
445                         if (msqptr->msg_qbytes == 0 &&
446                             (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
447                                 break;
448                 }
449                 if (msqid == msginfo.msgmni) {
450 #ifdef MSG_DEBUG_OK
451                         printf("no more msqid_ds's available\n");
452 #endif
453                         return(ENOSPC);
454                 }
455 #ifdef MSG_DEBUG_OK
456                 printf("msqid %d is available\n", msqid);
457 #endif
458                 msqptr->msg_perm.key = key;
459                 msqptr->msg_perm.cuid = cred->cr_uid;
460                 msqptr->msg_perm.uid = cred->cr_uid;
461                 msqptr->msg_perm.cgid = cred->cr_gid;
462                 msqptr->msg_perm.gid = cred->cr_gid;
463                 msqptr->msg_perm.mode = (msgflg & 0777);
464                 /* Make sure that the returned msqid is unique */
465                 msqptr->msg_perm.seq = (msqptr->msg_perm.seq + 1) & 0x7fff;
466                 msqptr->msg_first = NULL;
467                 msqptr->msg_last = NULL;
468                 msqptr->msg_cbytes = 0;
469                 msqptr->msg_qnum = 0;
470                 msqptr->msg_qbytes = msginfo.msgmnb;
471                 msqptr->msg_lspid = 0;
472                 msqptr->msg_lrpid = 0;
473                 msqptr->msg_stime = 0;
474                 msqptr->msg_rtime = 0;
475                 msqptr->msg_ctime = time_second;
476         } else {
477 #ifdef MSG_DEBUG_OK
478                 printf("didn't find it and wasn't asked to create it\n");
479 #endif
480                 return(ENOENT);
481         }
482
483 found:
484         /* Construct the unique msqid */
485         p->p_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
486         return(0);
487 }
488
489 #ifndef _SYS_SYSPROTO_H_
490 struct msgsnd_args {
491         int     msqid;
492         void    *msgp;
493         size_t  msgsz;
494         int     msgflg;
495 };
496 #endif
497
498 int
499 msgsnd(struct msgsnd_args *uap)
500 {
501         struct proc *p = curproc;
502         int msqid = uap->msqid;
503         void *user_msgp = uap->msgp;
504         size_t msgsz = uap->msgsz;
505         int msgflg = uap->msgflg;
506         int segs_needed, eval;
507         register struct msqid_ds *msqptr;
508         register struct msg *msghdr;
509         short next;
510
511 #ifdef MSG_DEBUG_OK
512         printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
513             msgflg);
514 #endif
515
516         if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
517                 return (ENOSYS);
518
519         msqid = IPCID_TO_IX(msqid);
520
521         if (msqid < 0 || msqid >= msginfo.msgmni) {
522 #ifdef MSG_DEBUG_OK
523                 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
524                     msginfo.msgmni);
525 #endif
526                 return(EINVAL);
527         }
528
529         msqptr = &msqids[msqid];
530         if (msqptr->msg_qbytes == 0) {
531 #ifdef MSG_DEBUG_OK
532                 printf("no such message queue id\n");
533 #endif
534                 return(EINVAL);
535         }
536         if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
537 #ifdef MSG_DEBUG_OK
538                 printf("wrong sequence number\n");
539 #endif
540                 return(EINVAL);
541         }
542
543         if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_W))) {
544 #ifdef MSG_DEBUG_OK
545                 printf("requester doesn't have write access\n");
546 #endif
547                 return(eval);
548         }
549
550         segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
551 #ifdef MSG_DEBUG_OK
552         printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
553             segs_needed);
554 #endif
555         for (;;) {
556                 int need_more_resources = 0;
557
558                 /*
559                  * check msgsz
560                  * (inside this loop in case msg_qbytes changes while we sleep)
561                  */
562
563                 if (msgsz > msqptr->msg_qbytes) {
564 #ifdef MSG_DEBUG_OK
565                         printf("msgsz > msqptr->msg_qbytes\n");
566 #endif
567                         return(EINVAL);
568                 }
569
570                 if (msqptr->msg_perm.mode & MSG_LOCKED) {
571 #ifdef MSG_DEBUG_OK
572                         printf("msqid is locked\n");
573 #endif
574                         need_more_resources = 1;
575                 }
576                 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
577 #ifdef MSG_DEBUG_OK
578                         printf("msgsz + msg_cbytes > msg_qbytes\n");
579 #endif
580                         need_more_resources = 1;
581                 }
582                 if (segs_needed > nfree_msgmaps) {
583 #ifdef MSG_DEBUG_OK
584                         printf("segs_needed > nfree_msgmaps\n");
585 #endif
586                         need_more_resources = 1;
587                 }
588                 if (free_msghdrs == NULL) {
589 #ifdef MSG_DEBUG_OK
590                         printf("no more msghdrs\n");
591 #endif
592                         need_more_resources = 1;
593                 }
594
595                 if (need_more_resources) {
596                         int we_own_it;
597
598                         if ((msgflg & IPC_NOWAIT) != 0) {
599 #ifdef MSG_DEBUG_OK
600                                 printf("need more resources but caller doesn't want to wait\n");
601 #endif
602                                 return(EAGAIN);
603                         }
604
605                         if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
606 #ifdef MSG_DEBUG_OK
607                                 printf("we don't own the msqid_ds\n");
608 #endif
609                                 we_own_it = 0;
610                         } else {
611                                 /* Force later arrivals to wait for our
612                                    request */
613 #ifdef MSG_DEBUG_OK
614                                 printf("we own the msqid_ds\n");
615 #endif
616                                 msqptr->msg_perm.mode |= MSG_LOCKED;
617                                 we_own_it = 1;
618                         }
619 #ifdef MSG_DEBUG_OK
620                         printf("goodnight\n");
621 #endif
622                         eval = tsleep((caddr_t)msqptr, PCATCH, "msgwait", 0);
623 #ifdef MSG_DEBUG_OK
624                         printf("good morning, eval=%d\n", eval);
625 #endif
626                         if (we_own_it)
627                                 msqptr->msg_perm.mode &= ~MSG_LOCKED;
628                         if (eval != 0) {
629 #ifdef MSG_DEBUG_OK
630                                 printf("msgsnd:  interrupted system call\n");
631 #endif
632                                 return(EINTR);
633                         }
634
635                         /*
636                          * Make sure that the msq queue still exists
637                          */
638
639                         if (msqptr->msg_qbytes == 0) {
640 #ifdef MSG_DEBUG_OK
641                                 printf("msqid deleted\n");
642 #endif
643                                 return(EIDRM);
644                         }
645
646                 } else {
647 #ifdef MSG_DEBUG_OK
648                         printf("got all the resources that we need\n");
649 #endif
650                         break;
651                 }
652         }
653
654         /*
655          * We have the resources that we need.
656          * Make sure!
657          */
658
659         if (msqptr->msg_perm.mode & MSG_LOCKED)
660                 panic("msg_perm.mode & MSG_LOCKED");
661         if (segs_needed > nfree_msgmaps)
662                 panic("segs_needed > nfree_msgmaps");
663         if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
664                 panic("msgsz + msg_cbytes > msg_qbytes");
665         if (free_msghdrs == NULL)
666                 panic("no more msghdrs");
667
668         /*
669          * Re-lock the msqid_ds in case we page-fault when copying in the
670          * message
671          */
672
673         if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
674                 panic("msqid_ds is already locked");
675         msqptr->msg_perm.mode |= MSG_LOCKED;
676
677         /*
678          * Allocate a message header
679          */
680
681         msghdr = free_msghdrs;
682         free_msghdrs = msghdr->msg_next;
683         msghdr->msg_spot = -1;
684         msghdr->msg_ts = msgsz;
685
686         /*
687          * Allocate space for the message
688          */
689
690         while (segs_needed > 0) {
691                 if (nfree_msgmaps <= 0)
692                         panic("not enough msgmaps");
693                 if (free_msgmaps == -1)
694                         panic("nil free_msgmaps");
695                 next = free_msgmaps;
696                 if (next <= -1)
697                         panic("next too low #1");
698                 if (next >= msginfo.msgseg)
699                         panic("next out of range #1");
700 #ifdef MSG_DEBUG_OK
701                 printf("allocating segment %d to message\n", next);
702 #endif
703                 free_msgmaps = msgmaps[next].next;
704                 nfree_msgmaps--;
705                 msgmaps[next].next = msghdr->msg_spot;
706                 msghdr->msg_spot = next;
707                 segs_needed--;
708         }
709
710         /*
711          * Copy in the message type
712          */
713
714         if ((eval = copyin(user_msgp, &msghdr->msg_type,
715             sizeof(msghdr->msg_type))) != 0) {
716 #ifdef MSG_DEBUG_OK
717                 printf("error %d copying the message type\n", eval);
718 #endif
719                 msg_freehdr(msghdr);
720                 msqptr->msg_perm.mode &= ~MSG_LOCKED;
721                 wakeup((caddr_t)msqptr);
722                 return(eval);
723         }
724         user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
725
726         /*
727          * Validate the message type
728          */
729
730         if (msghdr->msg_type < 1) {
731                 msg_freehdr(msghdr);
732                 msqptr->msg_perm.mode &= ~MSG_LOCKED;
733                 wakeup((caddr_t)msqptr);
734 #ifdef MSG_DEBUG_OK
735                 printf("mtype (%d) < 1\n", msghdr->msg_type);
736 #endif
737                 return(EINVAL);
738         }
739
740         /*
741          * Copy in the message body
742          */
743
744         next = msghdr->msg_spot;
745         while (msgsz > 0) {
746                 size_t tlen;
747                 if (msgsz > msginfo.msgssz)
748                         tlen = msginfo.msgssz;
749                 else
750                         tlen = msgsz;
751                 if (next <= -1)
752                         panic("next too low #2");
753                 if (next >= msginfo.msgseg)
754                         panic("next out of range #2");
755                 if ((eval = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
756                     tlen)) != 0) {
757 #ifdef MSG_DEBUG_OK
758                         printf("error %d copying in message segment\n", eval);
759 #endif
760                         msg_freehdr(msghdr);
761                         msqptr->msg_perm.mode &= ~MSG_LOCKED;
762                         wakeup((caddr_t)msqptr);
763                         return(eval);
764                 }
765                 msgsz -= tlen;
766                 user_msgp = (char *)user_msgp + tlen;
767                 next = msgmaps[next].next;
768         }
769         if (next != -1)
770                 panic("didn't use all the msg segments");
771
772         /*
773          * We've got the message.  Unlock the msqid_ds.
774          */
775
776         msqptr->msg_perm.mode &= ~MSG_LOCKED;
777
778         /*
779          * Make sure that the msqid_ds is still allocated.
780          */
781
782         if (msqptr->msg_qbytes == 0) {
783                 msg_freehdr(msghdr);
784                 wakeup((caddr_t)msqptr);
785                 return(EIDRM);
786         }
787
788         /*
789          * Put the message into the queue
790          */
791
792         if (msqptr->msg_first == NULL) {
793                 msqptr->msg_first = msghdr;
794                 msqptr->msg_last = msghdr;
795         } else {
796                 msqptr->msg_last->msg_next = msghdr;
797                 msqptr->msg_last = msghdr;
798         }
799         msqptr->msg_last->msg_next = NULL;
800
801         msqptr->msg_cbytes += msghdr->msg_ts;
802         msqptr->msg_qnum++;
803         msqptr->msg_lspid = p->p_pid;
804         msqptr->msg_stime = time_second;
805
806         wakeup((caddr_t)msqptr);
807         p->p_retval[0] = 0;
808         return(0);
809 }
810
811 #ifndef _SYS_SYSPROTO_H_
812 struct msgrcv_args {
813         int     msqid;
814         void    *msgp;
815         size_t  msgsz;
816         long    msgtyp;
817         int     msgflg;
818 };
819 #endif
820
821 int
822 msgrcv(struct msgrcv_args *uap)
823 {
824         struct proc *p = curproc;
825         int msqid = uap->msqid;
826         void *user_msgp = uap->msgp;
827         size_t msgsz = uap->msgsz;
828         long msgtyp = uap->msgtyp;
829         int msgflg = uap->msgflg;
830         size_t len;
831         register struct msqid_ds *msqptr;
832         register struct msg *msghdr;
833         int eval;
834         short next;
835
836 #ifdef MSG_DEBUG_OK
837         printf("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
838             msgsz, msgtyp, msgflg);
839 #endif
840
841         if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
842                 return (ENOSYS);
843
844         msqid = IPCID_TO_IX(msqid);
845
846         if (msqid < 0 || msqid >= msginfo.msgmni) {
847 #ifdef MSG_DEBUG_OK
848                 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
849                     msginfo.msgmni);
850 #endif
851                 return(EINVAL);
852         }
853
854         msqptr = &msqids[msqid];
855         if (msqptr->msg_qbytes == 0) {
856 #ifdef MSG_DEBUG_OK
857                 printf("no such message queue id\n");
858 #endif
859                 return(EINVAL);
860         }
861         if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
862 #ifdef MSG_DEBUG_OK
863                 printf("wrong sequence number\n");
864 #endif
865                 return(EINVAL);
866         }
867
868         if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
869 #ifdef MSG_DEBUG_OK
870                 printf("requester doesn't have read access\n");
871 #endif
872                 return(eval);
873         }
874
875         msghdr = NULL;
876         while (msghdr == NULL) {
877                 if (msgtyp == 0) {
878                         msghdr = msqptr->msg_first;
879                         if (msghdr != NULL) {
880                                 if (msgsz < msghdr->msg_ts &&
881                                     (msgflg & MSG_NOERROR) == 0) {
882 #ifdef MSG_DEBUG_OK
883                                         printf("first message on the queue is too big (want %d, got %d)\n",
884                                             msgsz, msghdr->msg_ts);
885 #endif
886                                         return(E2BIG);
887                                 }
888                                 if (msqptr->msg_first == msqptr->msg_last) {
889                                         msqptr->msg_first = NULL;
890                                         msqptr->msg_last = NULL;
891                                 } else {
892                                         msqptr->msg_first = msghdr->msg_next;
893                                         if (msqptr->msg_first == NULL)
894                                                 panic("msg_first/last screwed up #1");
895                                 }
896                         }
897                 } else {
898                         struct msg *previous;
899                         struct msg **prev;
900
901                         previous = NULL;
902                         prev = &(msqptr->msg_first);
903                         while ((msghdr = *prev) != NULL) {
904                                 /*
905                                  * Is this message's type an exact match or is
906                                  * this message's type less than or equal to
907                                  * the absolute value of a negative msgtyp?
908                                  * Note that the second half of this test can
909                                  * NEVER be true if msgtyp is positive since
910                                  * msg_type is always positive!
911                                  */
912
913                                 if (msgtyp == msghdr->msg_type ||
914                                     msghdr->msg_type <= -msgtyp) {
915 #ifdef MSG_DEBUG_OK
916                                         printf("found message type %d, requested %d\n",
917                                             msghdr->msg_type, msgtyp);
918 #endif
919                                         if (msgsz < msghdr->msg_ts &&
920                                             (msgflg & MSG_NOERROR) == 0) {
921 #ifdef MSG_DEBUG_OK
922                                                 printf("requested message on the queue is too big (want %d, got %d)\n",
923                                                     msgsz, msghdr->msg_ts);
924 #endif
925                                                 return(E2BIG);
926                                         }
927                                         *prev = msghdr->msg_next;
928                                         if (msghdr == msqptr->msg_last) {
929                                                 if (previous == NULL) {
930                                                         if (prev !=
931                                                             &msqptr->msg_first)
932                                                                 panic("msg_first/last screwed up #2");
933                                                         msqptr->msg_first =
934                                                             NULL;
935                                                         msqptr->msg_last =
936                                                             NULL;
937                                                 } else {
938                                                         if (prev ==
939                                                             &msqptr->msg_first)
940                                                                 panic("msg_first/last screwed up #3");
941                                                         msqptr->msg_last =
942                                                             previous;
943                                                 }
944                                         }
945                                         break;
946                                 }
947                                 previous = msghdr;
948                                 prev = &(msghdr->msg_next);
949                         }
950                 }
951
952                 /*
953                  * We've either extracted the msghdr for the appropriate
954                  * message or there isn't one.
955                  * If there is one then bail out of this loop.
956                  */
957
958                 if (msghdr != NULL)
959                         break;
960
961                 /*
962                  * Hmph!  No message found.  Does the user want to wait?
963                  */
964
965                 if ((msgflg & IPC_NOWAIT) != 0) {
966 #ifdef MSG_DEBUG_OK
967                         printf("no appropriate message found (msgtyp=%d)\n",
968                             msgtyp);
969 #endif
970                         /* The SVID says to return ENOMSG. */
971 #ifdef ENOMSG
972                         return(ENOMSG);
973 #else
974                         /* Unfortunately, BSD doesn't define that code yet! */
975                         return(EAGAIN);
976 #endif
977                 }
978
979                 /*
980                  * Wait for something to happen
981                  */
982
983 #ifdef MSG_DEBUG_OK
984                 printf("msgrcv:  goodnight\n");
985 #endif
986                 eval = tsleep((caddr_t)msqptr, PCATCH, "msgwait", 0);
987 #ifdef MSG_DEBUG_OK
988                 printf("msgrcv:  good morning (eval=%d)\n", eval);
989 #endif
990
991                 if (eval != 0) {
992 #ifdef MSG_DEBUG_OK
993                         printf("msgsnd:  interrupted system call\n");
994 #endif
995                         return(EINTR);
996                 }
997
998                 /*
999                  * Make sure that the msq queue still exists
1000                  */
1001
1002                 if (msqptr->msg_qbytes == 0 ||
1003                     msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1004 #ifdef MSG_DEBUG_OK
1005                         printf("msqid deleted\n");
1006 #endif
1007                         return(EIDRM);
1008                 }
1009         }
1010
1011         /*
1012          * Return the message to the user.
1013          *
1014          * First, do the bookkeeping (before we risk being interrupted).
1015          */
1016
1017         msqptr->msg_cbytes -= msghdr->msg_ts;
1018         msqptr->msg_qnum--;
1019         msqptr->msg_lrpid = p->p_pid;
1020         msqptr->msg_rtime = time_second;
1021
1022         /*
1023          * Make msgsz the actual amount that we'll be returning.
1024          * Note that this effectively truncates the message if it is too long
1025          * (since msgsz is never increased).
1026          */
1027
1028 #ifdef MSG_DEBUG_OK
1029         printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1030             msghdr->msg_ts);
1031 #endif
1032         if (msgsz > msghdr->msg_ts)
1033                 msgsz = msghdr->msg_ts;
1034
1035         /*
1036          * Return the type to the user.
1037          */
1038
1039         eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp,
1040             sizeof(msghdr->msg_type));
1041         if (eval != 0) {
1042 #ifdef MSG_DEBUG_OK
1043                 printf("error (%d) copying out message type\n", eval);
1044 #endif
1045                 msg_freehdr(msghdr);
1046                 wakeup((caddr_t)msqptr);
1047                 return(eval);
1048         }
1049         user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1050
1051         /*
1052          * Return the segments to the user
1053          */
1054
1055         next = msghdr->msg_spot;
1056         for (len = 0; len < msgsz; len += msginfo.msgssz) {
1057                 size_t tlen;
1058
1059                 if (msgsz - len > msginfo.msgssz)
1060                         tlen = msginfo.msgssz;
1061                 else
1062                         tlen = msgsz - len;
1063                 if (next <= -1)
1064                         panic("next too low #3");
1065                 if (next >= msginfo.msgseg)
1066                         panic("next out of range #3");
1067                 eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
1068                     user_msgp, tlen);
1069                 if (eval != 0) {
1070 #ifdef MSG_DEBUG_OK
1071                         printf("error (%d) copying out message segment\n",
1072                             eval);
1073 #endif
1074                         msg_freehdr(msghdr);
1075                         wakeup((caddr_t)msqptr);
1076                         return(eval);
1077                 }
1078                 user_msgp = (char *)user_msgp + tlen;
1079                 next = msgmaps[next].next;
1080         }
1081
1082         /*
1083          * Done, return the actual number of bytes copied out.
1084          */
1085
1086         msg_freehdr(msghdr);
1087         wakeup((caddr_t)msqptr);
1088         p->p_retval[0] = msgsz;
1089         return(0);
1090 }
1091
1092 static int
1093 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1094 {
1095
1096         return (SYSCTL_OUT(req, msqids,
1097             sizeof(struct msqid_ds) * msginfo.msgmni));
1098 }
1099
1100 TUNABLE_INT("kern.ipc.msgseg", &msginfo.msgseg);
1101 TUNABLE_INT("kern.ipc.msgssz", &msginfo.msgssz);
1102 TUNABLE_INT("kern.ipc.msgmni", &msginfo.msgmni);
1103
1104 SYSCTL_DECL(_kern_ipc);
1105 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0, "");
1106 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RD, &msginfo.msgmni, 0, "");
1107 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RD, &msginfo.msgmnb, 0, "");
1108 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RD, &msginfo.msgtql, 0, "");
1109 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RD, &msginfo.msgssz, 0, "");
1110 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RD, &msginfo.msgseg, 0, "");
1111 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1112     NULL, 0, sysctl_msqids, "", "Message queue IDs");