proc->thread stage 2: MAJOR revamping of system calls, ucred, jail API,
[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.3 2003/06/23 17:55: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  * 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 proc *p = curproc;
252         int msqid = uap->msqid;
253         int cmd = uap->cmd;
254         struct msqid_ds *user_msqptr = uap->buf;
255         int rval, eval;
256         struct msqid_ds msqbuf;
257         register struct msqid_ds *msqptr;
258
259 #ifdef MSG_DEBUG_OK
260         printf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr);
261 #endif
262
263         if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
264                 return (ENOSYS);
265
266         msqid = IPCID_TO_IX(msqid);
267
268         if (msqid < 0 || msqid >= msginfo.msgmni) {
269 #ifdef MSG_DEBUG_OK
270                 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
271                     msginfo.msgmni);
272 #endif
273                 return(EINVAL);
274         }
275
276         msqptr = &msqids[msqid];
277
278         if (msqptr->msg_qbytes == 0) {
279 #ifdef MSG_DEBUG_OK
280                 printf("no such msqid\n");
281 #endif
282                 return(EINVAL);
283         }
284         if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
285 #ifdef MSG_DEBUG_OK
286                 printf("wrong sequence number\n");
287 #endif
288                 return(EINVAL);
289         }
290
291         eval = 0;
292         rval = 0;
293
294         switch (cmd) {
295
296         case IPC_RMID:
297         {
298                 struct msg *msghdr;
299                 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)))
300                         return(eval);
301                 /* Free the message headers */
302                 msghdr = msqptr->msg_first;
303                 while (msghdr != NULL) {
304                         struct msg *msghdr_tmp;
305
306                         /* Free the segments of each message */
307                         msqptr->msg_cbytes -= msghdr->msg_ts;
308                         msqptr->msg_qnum--;
309                         msghdr_tmp = msghdr;
310                         msghdr = msghdr->msg_next;
311                         msg_freehdr(msghdr_tmp);
312                 }
313
314                 if (msqptr->msg_cbytes != 0)
315                         panic("msg_cbytes is screwed up");
316                 if (msqptr->msg_qnum != 0)
317                         panic("msg_qnum is screwed up");
318
319                 msqptr->msg_qbytes = 0; /* Mark it as free */
320
321                 wakeup((caddr_t)msqptr);
322         }
323
324                 break;
325
326         case IPC_SET:
327                 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_M)))
328                         return(eval);
329                 if ((eval = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
330                         return(eval);
331                 if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
332                         eval = suser();
333                         if (eval)
334                                 return(eval);
335                 }
336                 if (msqbuf.msg_qbytes > msginfo.msgmnb) {
337 #ifdef MSG_DEBUG_OK
338                         printf("can't increase msg_qbytes beyond %d (truncating)\n",
339                             msginfo.msgmnb);
340 #endif
341                         msqbuf.msg_qbytes = msginfo.msgmnb;     /* silently restrict qbytes to system limit */
342                 }
343                 if (msqbuf.msg_qbytes == 0) {
344 #ifdef MSG_DEBUG_OK
345                         printf("can't reduce msg_qbytes to 0\n");
346 #endif
347                         return(EINVAL);         /* non-standard errno! */
348                 }
349                 msqptr->msg_perm.uid = msqbuf.msg_perm.uid;     /* change the owner */
350                 msqptr->msg_perm.gid = msqbuf.msg_perm.gid;     /* change the owner */
351                 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
352                     (msqbuf.msg_perm.mode & 0777);
353                 msqptr->msg_qbytes = msqbuf.msg_qbytes;
354                 msqptr->msg_ctime = time_second;
355                 break;
356
357         case IPC_STAT:
358                 if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) {
359 #ifdef MSG_DEBUG_OK
360                         printf("requester doesn't have read access\n");
361 #endif
362                         return(eval);
363                 }
364                 eval = copyout((caddr_t)msqptr, user_msqptr,
365                     sizeof(struct msqid_ds));
366                 break;
367
368         default:
369 #ifdef MSG_DEBUG_OK
370                 printf("invalid command %d\n", cmd);
371 #endif
372                 return(EINVAL);
373         }
374
375         if (eval == 0)
376                 p->p_retval[0] = rval;
377         return(eval);
378 }
379
380 #ifndef _SYS_SYSPROTO_H_
381 struct msgget_args {
382         key_t   key;
383         int     msgflg;
384 };
385 #endif
386
387 int
388 msgget(struct msgget_args *uap)
389 {
390         struct proc *p = curproc;
391         int msqid, eval;
392         int key = uap->key;
393         int msgflg = uap->msgflg;
394         struct ucred *cred = p->p_ucred;
395         register struct msqid_ds *msqptr = NULL;
396
397 #ifdef MSG_DEBUG_OK
398         printf("msgget(0x%x, 0%o)\n", key, msgflg);
399 #endif
400
401         if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
402                 return (ENOSYS);
403
404         if (key != IPC_PRIVATE) {
405                 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
406                         msqptr = &msqids[msqid];
407                         if (msqptr->msg_qbytes != 0 &&
408                             msqptr->msg_perm.key == key)
409                                 break;
410                 }
411                 if (msqid < msginfo.msgmni) {
412 #ifdef MSG_DEBUG_OK
413                         printf("found public key\n");
414 #endif
415                         if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
416 #ifdef MSG_DEBUG_OK
417                                 printf("not exclusive\n");
418 #endif
419                                 return(EEXIST);
420                         }
421                         if ((eval = ipcperm(p, &msqptr->msg_perm, msgflg & 0700 ))) {
422 #ifdef MSG_DEBUG_OK
423                                 printf("requester doesn't have 0%o access\n",
424                                     msgflg & 0700);
425 #endif
426                                 return(eval);
427                         }
428                         goto found;
429                 }
430         }
431
432 #ifdef MSG_DEBUG_OK
433         printf("need to allocate the msqid_ds\n");
434 #endif
435         if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
436                 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
437                         /*
438                          * Look for an unallocated and unlocked msqid_ds.
439                          * msqid_ds's can be locked by msgsnd or msgrcv while
440                          * they are copying the message in/out.  We can't
441                          * re-use the entry until they release it.
442                          */
443                         msqptr = &msqids[msqid];
444                         if (msqptr->msg_qbytes == 0 &&
445                             (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
446                                 break;
447                 }
448                 if (msqid == msginfo.msgmni) {
449 #ifdef MSG_DEBUG_OK
450                         printf("no more msqid_ds's available\n");
451 #endif
452                         return(ENOSPC);
453                 }
454 #ifdef MSG_DEBUG_OK
455                 printf("msqid %d is available\n", msqid);
456 #endif
457                 msqptr->msg_perm.key = key;
458                 msqptr->msg_perm.cuid = cred->cr_uid;
459                 msqptr->msg_perm.uid = cred->cr_uid;
460                 msqptr->msg_perm.cgid = cred->cr_gid;
461                 msqptr->msg_perm.gid = cred->cr_gid;
462                 msqptr->msg_perm.mode = (msgflg & 0777);
463                 /* Make sure that the returned msqid is unique */
464                 msqptr->msg_perm.seq = (msqptr->msg_perm.seq + 1) & 0x7fff;
465                 msqptr->msg_first = NULL;
466                 msqptr->msg_last = NULL;
467                 msqptr->msg_cbytes = 0;
468                 msqptr->msg_qnum = 0;
469                 msqptr->msg_qbytes = msginfo.msgmnb;
470                 msqptr->msg_lspid = 0;
471                 msqptr->msg_lrpid = 0;
472                 msqptr->msg_stime = 0;
473                 msqptr->msg_rtime = 0;
474                 msqptr->msg_ctime = time_second;
475         } else {
476 #ifdef MSG_DEBUG_OK
477                 printf("didn't find it and wasn't asked to create it\n");
478 #endif
479                 return(ENOENT);
480         }
481
482 found:
483         /* Construct the unique msqid */
484         p->p_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
485         return(0);
486 }
487
488 #ifndef _SYS_SYSPROTO_H_
489 struct msgsnd_args {
490         int     msqid;
491         void    *msgp;
492         size_t  msgsz;
493         int     msgflg;
494 };
495 #endif
496
497 int
498 msgsnd(struct msgsnd_args *uap)
499 {
500         struct proc *p = curproc;
501         int msqid = uap->msqid;
502         void *user_msgp = uap->msgp;
503         size_t msgsz = uap->msgsz;
504         int msgflg = uap->msgflg;
505         int segs_needed, eval;
506         register struct msqid_ds *msqptr;
507         register struct msg *msghdr;
508         short next;
509
510 #ifdef MSG_DEBUG_OK
511         printf("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
512             msgflg);
513 #endif
514
515         if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
516                 return (ENOSYS);
517
518         msqid = IPCID_TO_IX(msqid);
519
520         if (msqid < 0 || msqid >= msginfo.msgmni) {
521 #ifdef MSG_DEBUG_OK
522                 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
523                     msginfo.msgmni);
524 #endif
525                 return(EINVAL);
526         }
527
528         msqptr = &msqids[msqid];
529         if (msqptr->msg_qbytes == 0) {
530 #ifdef MSG_DEBUG_OK
531                 printf("no such message queue id\n");
532 #endif
533                 return(EINVAL);
534         }
535         if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
536 #ifdef MSG_DEBUG_OK
537                 printf("wrong sequence number\n");
538 #endif
539                 return(EINVAL);
540         }
541
542         if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_W))) {
543 #ifdef MSG_DEBUG_OK
544                 printf("requester doesn't have write access\n");
545 #endif
546                 return(eval);
547         }
548
549         segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
550 #ifdef MSG_DEBUG_OK
551         printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
552             segs_needed);
553 #endif
554         for (;;) {
555                 int need_more_resources = 0;
556
557                 /*
558                  * check msgsz
559                  * (inside this loop in case msg_qbytes changes while we sleep)
560                  */
561
562                 if (msgsz > msqptr->msg_qbytes) {
563 #ifdef MSG_DEBUG_OK
564                         printf("msgsz > msqptr->msg_qbytes\n");
565 #endif
566                         return(EINVAL);
567                 }
568
569                 if (msqptr->msg_perm.mode & MSG_LOCKED) {
570 #ifdef MSG_DEBUG_OK
571                         printf("msqid is locked\n");
572 #endif
573                         need_more_resources = 1;
574                 }
575                 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
576 #ifdef MSG_DEBUG_OK
577                         printf("msgsz + msg_cbytes > msg_qbytes\n");
578 #endif
579                         need_more_resources = 1;
580                 }
581                 if (segs_needed > nfree_msgmaps) {
582 #ifdef MSG_DEBUG_OK
583                         printf("segs_needed > nfree_msgmaps\n");
584 #endif
585                         need_more_resources = 1;
586                 }
587                 if (free_msghdrs == NULL) {
588 #ifdef MSG_DEBUG_OK
589                         printf("no more msghdrs\n");
590 #endif
591                         need_more_resources = 1;
592                 }
593
594                 if (need_more_resources) {
595                         int we_own_it;
596
597                         if ((msgflg & IPC_NOWAIT) != 0) {
598 #ifdef MSG_DEBUG_OK
599                                 printf("need more resources but caller doesn't want to wait\n");
600 #endif
601                                 return(EAGAIN);
602                         }
603
604                         if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
605 #ifdef MSG_DEBUG_OK
606                                 printf("we don't own the msqid_ds\n");
607 #endif
608                                 we_own_it = 0;
609                         } else {
610                                 /* Force later arrivals to wait for our
611                                    request */
612 #ifdef MSG_DEBUG_OK
613                                 printf("we own the msqid_ds\n");
614 #endif
615                                 msqptr->msg_perm.mode |= MSG_LOCKED;
616                                 we_own_it = 1;
617                         }
618 #ifdef MSG_DEBUG_OK
619                         printf("goodnight\n");
620 #endif
621                         eval = tsleep((caddr_t)msqptr, (PZERO - 4) | PCATCH,
622                             "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, (PZERO - 4) | PCATCH, "msgwait",
987                     0);
988 #ifdef MSG_DEBUG_OK
989                 printf("msgrcv:  good morning (eval=%d)\n", eval);
990 #endif
991
992                 if (eval != 0) {
993 #ifdef MSG_DEBUG_OK
994                         printf("msgsnd:  interrupted system call\n");
995 #endif
996                         return(EINTR);
997                 }
998
999                 /*
1000                  * Make sure that the msq queue still exists
1001                  */
1002
1003                 if (msqptr->msg_qbytes == 0 ||
1004                     msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1005 #ifdef MSG_DEBUG_OK
1006                         printf("msqid deleted\n");
1007 #endif
1008                         return(EIDRM);
1009                 }
1010         }
1011
1012         /*
1013          * Return the message to the user.
1014          *
1015          * First, do the bookkeeping (before we risk being interrupted).
1016          */
1017
1018         msqptr->msg_cbytes -= msghdr->msg_ts;
1019         msqptr->msg_qnum--;
1020         msqptr->msg_lrpid = p->p_pid;
1021         msqptr->msg_rtime = time_second;
1022
1023         /*
1024          * Make msgsz the actual amount that we'll be returning.
1025          * Note that this effectively truncates the message if it is too long
1026          * (since msgsz is never increased).
1027          */
1028
1029 #ifdef MSG_DEBUG_OK
1030         printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1031             msghdr->msg_ts);
1032 #endif
1033         if (msgsz > msghdr->msg_ts)
1034                 msgsz = msghdr->msg_ts;
1035
1036         /*
1037          * Return the type to the user.
1038          */
1039
1040         eval = copyout((caddr_t)&(msghdr->msg_type), user_msgp,
1041             sizeof(msghdr->msg_type));
1042         if (eval != 0) {
1043 #ifdef MSG_DEBUG_OK
1044                 printf("error (%d) copying out message type\n", eval);
1045 #endif
1046                 msg_freehdr(msghdr);
1047                 wakeup((caddr_t)msqptr);
1048                 return(eval);
1049         }
1050         user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1051
1052         /*
1053          * Return the segments to the user
1054          */
1055
1056         next = msghdr->msg_spot;
1057         for (len = 0; len < msgsz; len += msginfo.msgssz) {
1058                 size_t tlen;
1059
1060                 if (msgsz - len > msginfo.msgssz)
1061                         tlen = msginfo.msgssz;
1062                 else
1063                         tlen = msgsz - len;
1064                 if (next <= -1)
1065                         panic("next too low #3");
1066                 if (next >= msginfo.msgseg)
1067                         panic("next out of range #3");
1068                 eval = copyout((caddr_t)&msgpool[next * msginfo.msgssz],
1069                     user_msgp, tlen);
1070                 if (eval != 0) {
1071 #ifdef MSG_DEBUG_OK
1072                         printf("error (%d) copying out message segment\n",
1073                             eval);
1074 #endif
1075                         msg_freehdr(msghdr);
1076                         wakeup((caddr_t)msqptr);
1077                         return(eval);
1078                 }
1079                 user_msgp = (char *)user_msgp + tlen;
1080                 next = msgmaps[next].next;
1081         }
1082
1083         /*
1084          * Done, return the actual number of bytes copied out.
1085          */
1086
1087         msg_freehdr(msghdr);
1088         wakeup((caddr_t)msqptr);
1089         p->p_retval[0] = msgsz;
1090         return(0);
1091 }
1092
1093 static int
1094 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1095 {
1096
1097         return (SYSCTL_OUT(req, msqids,
1098             sizeof(struct msqid_ds) * msginfo.msgmni));
1099 }
1100
1101 TUNABLE_INT("kern.ipc.msgseg", &msginfo.msgseg);
1102 TUNABLE_INT("kern.ipc.msgssz", &msginfo.msgssz);
1103 TUNABLE_INT("kern.ipc.msgmni", &msginfo.msgmni);
1104
1105 SYSCTL_DECL(_kern_ipc);
1106 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0, "");
1107 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RD, &msginfo.msgmni, 0, "");
1108 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RD, &msginfo.msgmnb, 0, "");
1109 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RD, &msginfo.msgtql, 0, "");
1110 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RD, &msginfo.msgssz, 0, "");
1111 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RD, &msginfo.msgseg, 0, "");
1112 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1113     NULL, 0, sysctl_msqids, "", "Message queue IDs");