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