]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/sysv_msg.c
Add UPDATING entries and bump version.
[FreeBSD/FreeBSD.git] / sys / kern / sysv_msg.c
1 /*-
2  * Implementation of SVID messages
3  *
4  * Author:  Daniel Boulet
5  *
6  * Copyright 1993 Daniel Boulet and RTMX Inc.
7  *
8  * This system call was implemented by Daniel Boulet under contract from RTMX.
9  *
10  * Redistribution and use in source forms, with and without modification,
11  * are permitted provided that this entire comment appears intact.
12  *
13  * Redistribution in binary form may occur without any restrictions.
14  * Obviously, it would be nice if you gave credit where credit is due
15  * but requiring it would be too onerous.
16  *
17  * This software is provided ``AS IS'' without any warranties of any kind.
18  */
19 /*-
20  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
21  *
22  * Copyright (c) 2003-2005 McAfee, Inc.
23  * Copyright (c) 2016-2017 Robert N. M. Watson
24  * All rights reserved.
25  *
26  * This software was developed for the FreeBSD Project in part by McAfee
27  * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
28  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
29  * program.
30  *
31  * Portions of this software were developed by BAE Systems, the University of
32  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
33  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
34  * Computing (TC) research program.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60
61 #include "opt_sysvipc.h"
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/sysproto.h>
66 #include <sys/kernel.h>
67 #include <sys/priv.h>
68 #include <sys/proc.h>
69 #include <sys/lock.h>
70 #include <sys/mutex.h>
71 #include <sys/module.h>
72 #include <sys/mount.h>
73 #include <sys/msg.h>
74 #include <sys/racct.h>
75 #include <sys/sx.h>
76 #include <sys/syscall.h>
77 #include <sys/syscallsubr.h>
78 #include <sys/sysent.h>
79 #include <sys/sysctl.h>
80 #include <sys/malloc.h>
81 #include <sys/jail.h>
82
83 #include <security/audit/audit.h>
84 #include <security/mac/mac_framework.h>
85
86 FEATURE(sysv_msg, "System V message queues support");
87
88 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
89
90 static int msginit(void);
91 static int msgunload(void);
92 static int sysvmsg_modload(struct module *, int, void *);
93 static void msq_remove(struct msqid_kernel *);
94 static struct prison *msg_find_prison(struct ucred *);
95 static int msq_prison_cansee(struct prison *, struct msqid_kernel *);
96 static int msg_prison_check(void *, void *);
97 static int msg_prison_set(void *, void *);
98 static int msg_prison_get(void *, void *);
99 static int msg_prison_remove(void *, void *);
100 static void msg_prison_cleanup(struct prison *);
101
102
103 #ifdef MSG_DEBUG
104 #define DPRINTF(a)      printf a
105 #else
106 #define DPRINTF(a)      (void)0
107 #endif
108
109 static void msg_freehdr(struct msg *msghdr);
110
111 #ifndef MSGSSZ
112 #define MSGSSZ  8               /* Each segment must be 2^N long */
113 #endif
114 #ifndef MSGSEG
115 #define MSGSEG  2048            /* must be less than 32767 */
116 #endif
117 #define MSGMAX  (MSGSSZ*MSGSEG)
118 #ifndef MSGMNB
119 #define MSGMNB  2048            /* max # of bytes in a queue */
120 #endif
121 #ifndef MSGMNI
122 #define MSGMNI  40
123 #endif
124 #ifndef MSGTQL
125 #define MSGTQL  40
126 #endif
127
128 /*
129  * Based on the configuration parameters described in an SVR2 (yes, two)
130  * config(1m) man page.
131  *
132  * Each message is broken up and stored in segments that are msgssz bytes
133  * long.  For efficiency reasons, this should be a power of two.  Also,
134  * it doesn't make sense if it is less than 8 or greater than about 256.
135  * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
136  * two between 8 and 1024 inclusive (and panic's if it isn't).
137  */
138 struct msginfo msginfo = {
139                 MSGMAX,         /* max chars in a message */
140                 MSGMNI,         /* # of message queue identifiers */
141                 MSGMNB,         /* max chars in a queue */
142                 MSGTQL,         /* max messages in system */
143                 MSGSSZ,         /* size of a message segment */
144                                 /* (must be small power of 2 greater than 4) */
145                 MSGSEG          /* number of message segments */
146 };
147
148 /*
149  * macros to convert between msqid_ds's and msqid's.
150  * (specific to this implementation)
151  */
152 #define MSQID(ix,ds)    ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
153 #define MSQID_IX(id)    ((id) & 0xffff)
154 #define MSQID_SEQ(id)   (((id) >> 16) & 0xffff)
155
156 /*
157  * The rest of this file is specific to this particular implementation.
158  */
159
160 struct msgmap {
161         short   next;           /* next segment in buffer */
162                                 /* -1 -> available */
163                                 /* 0..(MSGSEG-1) -> index of next segment */
164 };
165
166 #define MSG_LOCKED      01000   /* Is this msqid_ds locked? */
167
168 static int nfree_msgmaps;       /* # of free map entries */
169 static short free_msgmaps;      /* head of linked list of free map entries */
170 static struct msg *free_msghdrs;/* list of free msg headers */
171 static char *msgpool;           /* MSGMAX byte long msg buffer pool */
172 static struct msgmap *msgmaps;  /* MSGSEG msgmap structures */
173 static struct msg *msghdrs;     /* MSGTQL msg headers */
174 static struct msqid_kernel *msqids;     /* MSGMNI msqid_kernel struct's */
175 static struct mtx msq_mtx;      /* global mutex for message queues. */
176 static unsigned msg_prison_slot;/* prison OSD slot */
177
178 static struct syscall_helper_data msg_syscalls[] = {
179         SYSCALL_INIT_HELPER(msgctl),
180         SYSCALL_INIT_HELPER(msgget),
181         SYSCALL_INIT_HELPER(msgsnd),
182         SYSCALL_INIT_HELPER(msgrcv),
183 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
184     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
185         SYSCALL_INIT_HELPER(msgsys),
186         SYSCALL_INIT_HELPER_COMPAT(freebsd7_msgctl),
187 #endif
188         SYSCALL_INIT_LAST
189 };
190
191 #ifdef COMPAT_FREEBSD32
192 #include <compat/freebsd32/freebsd32.h>
193 #include <compat/freebsd32/freebsd32_ipc.h>
194 #include <compat/freebsd32/freebsd32_proto.h>
195 #include <compat/freebsd32/freebsd32_signal.h>
196 #include <compat/freebsd32/freebsd32_syscall.h>
197 #include <compat/freebsd32/freebsd32_util.h>
198
199 static struct syscall_helper_data msg32_syscalls[] = {
200         SYSCALL32_INIT_HELPER(freebsd32_msgctl),
201         SYSCALL32_INIT_HELPER(freebsd32_msgsnd),
202         SYSCALL32_INIT_HELPER(freebsd32_msgrcv),
203         SYSCALL32_INIT_HELPER_COMPAT(msgget),
204         SYSCALL32_INIT_HELPER(freebsd32_msgsys),
205 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
206     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
207         SYSCALL32_INIT_HELPER(freebsd7_freebsd32_msgctl),
208 #endif
209         SYSCALL_INIT_LAST
210 };
211 #endif
212
213 static int
214 msginit()
215 {
216         struct prison *pr;
217         void **rsv;
218         int i, error;
219         osd_method_t methods[PR_MAXMETHOD] = {
220             [PR_METHOD_CHECK] =         msg_prison_check,
221             [PR_METHOD_SET] =           msg_prison_set,
222             [PR_METHOD_GET] =           msg_prison_get,
223             [PR_METHOD_REMOVE] =        msg_prison_remove,
224         };
225
226         msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
227         msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
228         msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
229         msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
230         msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
231             M_WAITOK | M_ZERO);
232
233         /*
234          * msginfo.msgssz should be a power of two for efficiency reasons.
235          * It is also pretty silly if msginfo.msgssz is less than 8
236          * or greater than about 256 so ...
237          */
238
239         i = 8;
240         while (i < 1024 && i != msginfo.msgssz)
241                 i <<= 1;
242         if (i != msginfo.msgssz) {
243                 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
244                     msginfo.msgssz));
245                 panic("msginfo.msgssz not a small power of 2");
246         }
247
248         if (msginfo.msgseg > 32767) {
249                 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
250                 panic("msginfo.msgseg > 32767");
251         }
252
253         for (i = 0; i < msginfo.msgseg; i++) {
254                 if (i > 0)
255                         msgmaps[i-1].next = i;
256                 msgmaps[i].next = -1;   /* implies entry is available */
257         }
258         free_msgmaps = 0;
259         nfree_msgmaps = msginfo.msgseg;
260
261         for (i = 0; i < msginfo.msgtql; i++) {
262                 msghdrs[i].msg_type = 0;
263                 if (i > 0)
264                         msghdrs[i-1].msg_next = &msghdrs[i];
265                 msghdrs[i].msg_next = NULL;
266 #ifdef MAC
267                 mac_sysvmsg_init(&msghdrs[i]);
268 #endif
269         }
270         free_msghdrs = &msghdrs[0];
271
272         for (i = 0; i < msginfo.msgmni; i++) {
273                 msqids[i].u.msg_qbytes = 0;     /* implies entry is available */
274                 msqids[i].u.msg_perm.seq = 0;   /* reset to a known value */
275                 msqids[i].u.msg_perm.mode = 0;
276 #ifdef MAC
277                 mac_sysvmsq_init(&msqids[i]);
278 #endif
279         }
280         mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
281
282         /* Set current prisons according to their allow.sysvipc. */
283         msg_prison_slot = osd_jail_register(NULL, methods);
284         rsv = osd_reserve(msg_prison_slot);
285         prison_lock(&prison0);
286         (void)osd_jail_set_reserved(&prison0, msg_prison_slot, rsv, &prison0);
287         prison_unlock(&prison0);
288         rsv = NULL;
289         sx_slock(&allprison_lock);
290         TAILQ_FOREACH(pr, &allprison, pr_list) {
291                 if (rsv == NULL)
292                         rsv = osd_reserve(msg_prison_slot);
293                 prison_lock(pr);
294                 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
295                         (void)osd_jail_set_reserved(pr, msg_prison_slot, rsv,
296                             &prison0);
297                         rsv = NULL;
298                 }
299                 prison_unlock(pr);
300         }
301         if (rsv != NULL)
302                 osd_free_reserved(rsv);
303         sx_sunlock(&allprison_lock);
304
305         error = syscall_helper_register(msg_syscalls, SY_THR_STATIC_KLD);
306         if (error != 0)
307                 return (error);
308 #ifdef COMPAT_FREEBSD32
309         error = syscall32_helper_register(msg32_syscalls, SY_THR_STATIC_KLD);
310         if (error != 0)
311                 return (error);
312 #endif
313         return (0);
314 }
315
316 static int
317 msgunload()
318 {
319         struct msqid_kernel *msqkptr;
320         int msqid;
321 #ifdef MAC
322         int i;
323 #endif
324
325         syscall_helper_unregister(msg_syscalls);
326 #ifdef COMPAT_FREEBSD32
327         syscall32_helper_unregister(msg32_syscalls);
328 #endif
329
330         for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
331                 msqkptr = &msqids[msqid];
332                 if (msqkptr->u.msg_qbytes != 0 ||
333                     (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
334                         break;
335         }
336         if (msqid != msginfo.msgmni)
337                 return (EBUSY);
338
339         if (msg_prison_slot != 0)
340                 osd_jail_deregister(msg_prison_slot);
341 #ifdef MAC
342         for (i = 0; i < msginfo.msgtql; i++)
343                 mac_sysvmsg_destroy(&msghdrs[i]);
344         for (msqid = 0; msqid < msginfo.msgmni; msqid++)
345                 mac_sysvmsq_destroy(&msqids[msqid]);
346 #endif
347         free(msgpool, M_MSG);
348         free(msgmaps, M_MSG);
349         free(msghdrs, M_MSG);
350         free(msqids, M_MSG);
351         mtx_destroy(&msq_mtx);
352         return (0);
353 }
354
355
356 static int
357 sysvmsg_modload(struct module *module, int cmd, void *arg)
358 {
359         int error = 0;
360
361         switch (cmd) {
362         case MOD_LOAD:
363                 error = msginit();
364                 if (error != 0)
365                         msgunload();
366                 break;
367         case MOD_UNLOAD:
368                 error = msgunload();
369                 break;
370         case MOD_SHUTDOWN:
371                 break;
372         default:
373                 error = EINVAL;
374                 break;
375         }
376         return (error);
377 }
378
379 static moduledata_t sysvmsg_mod = {
380         "sysvmsg",
381         &sysvmsg_modload,
382         NULL
383 };
384
385 DECLARE_MODULE(sysvmsg, sysvmsg_mod, SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
386 MODULE_VERSION(sysvmsg, 1);
387
388 static void
389 msg_freehdr(struct msg *msghdr)
390 {
391         while (msghdr->msg_ts > 0) {
392                 short next;
393                 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
394                         panic("msghdr->msg_spot out of range");
395                 next = msgmaps[msghdr->msg_spot].next;
396                 msgmaps[msghdr->msg_spot].next = free_msgmaps;
397                 free_msgmaps = msghdr->msg_spot;
398                 nfree_msgmaps++;
399                 msghdr->msg_spot = next;
400                 if (msghdr->msg_ts >= msginfo.msgssz)
401                         msghdr->msg_ts -= msginfo.msgssz;
402                 else
403                         msghdr->msg_ts = 0;
404         }
405         if (msghdr->msg_spot != -1)
406                 panic("msghdr->msg_spot != -1");
407         msghdr->msg_next = free_msghdrs;
408         free_msghdrs = msghdr;
409 #ifdef MAC
410         mac_sysvmsg_cleanup(msghdr);
411 #endif
412 }
413
414 static void
415 msq_remove(struct msqid_kernel *msqkptr)
416 {
417         struct msg *msghdr;
418
419         racct_sub_cred(msqkptr->cred, RACCT_NMSGQ, 1);
420         racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, msqkptr->u.msg_qnum);
421         racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msqkptr->u.msg_cbytes);
422         crfree(msqkptr->cred);
423         msqkptr->cred = NULL;
424
425         /* Free the message headers */
426         msghdr = msqkptr->u.__msg_first;
427         while (msghdr != NULL) {
428                 struct msg *msghdr_tmp;
429
430                 /* Free the segments of each message */
431                 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
432                 msqkptr->u.msg_qnum--;
433                 msghdr_tmp = msghdr;
434                 msghdr = msghdr->msg_next;
435                 msg_freehdr(msghdr_tmp);
436         }
437
438         if (msqkptr->u.msg_cbytes != 0)
439                 panic("msg_cbytes is screwed up");
440         if (msqkptr->u.msg_qnum != 0)
441                 panic("msg_qnum is screwed up");
442
443         msqkptr->u.msg_qbytes = 0;      /* Mark it as free */
444
445 #ifdef MAC
446         mac_sysvmsq_cleanup(msqkptr);
447 #endif
448
449         wakeup(msqkptr);
450 }
451
452 static struct prison *
453 msg_find_prison(struct ucred *cred)
454 {
455         struct prison *pr, *rpr;
456
457         pr = cred->cr_prison;
458         prison_lock(pr);
459         rpr = osd_jail_get(pr, msg_prison_slot);
460         prison_unlock(pr);
461         return rpr;
462 }
463
464 static int
465 msq_prison_cansee(struct prison *rpr, struct msqid_kernel *msqkptr)
466 {
467
468         if (msqkptr->cred == NULL ||
469             !(rpr == msqkptr->cred->cr_prison ||
470               prison_ischild(rpr, msqkptr->cred->cr_prison)))
471                 return (EINVAL);
472         return (0);
473 }
474
475 #ifndef _SYS_SYSPROTO_H_
476 struct msgctl_args {
477         int     msqid;
478         int     cmd;
479         struct  msqid_ds *buf;
480 };
481 #endif
482 int
483 sys_msgctl(struct thread *td, struct msgctl_args *uap)
484 {
485         int msqid = uap->msqid;
486         int cmd = uap->cmd;
487         struct msqid_ds msqbuf;
488         int error;
489
490         DPRINTF(("call to msgctl(%d, %d, %p)\n", msqid, cmd, uap->buf));
491         if (cmd == IPC_SET &&
492             (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
493                 return (error);
494         error = kern_msgctl(td, msqid, cmd, &msqbuf);
495         if (cmd == IPC_STAT && error == 0)
496                 error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
497         return (error);
498 }
499
500 int
501 kern_msgctl(struct thread *td, int msqid, int cmd, struct msqid_ds *msqbuf)
502 {
503         int rval, error, msqix;
504         struct msqid_kernel *msqkptr;
505         struct prison *rpr;
506
507         rpr = msg_find_prison(td->td_ucred);
508         if (rpr == NULL)
509                 return (ENOSYS);
510
511         AUDIT_ARG_SVIPC_CMD(cmd);
512         AUDIT_ARG_SVIPC_ID(msqid);
513         msqix = IPCID_TO_IX(msqid);
514
515         if (msqix < 0 || msqix >= msginfo.msgmni) {
516                 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
517                     msginfo.msgmni));
518                 return (EINVAL);
519         }
520
521         msqkptr = &msqids[msqix];
522
523         mtx_lock(&msq_mtx);
524         if (msqkptr->u.msg_qbytes == 0) {
525                 DPRINTF(("no such msqid\n"));
526                 error = EINVAL;
527                 goto done2;
528         }
529         if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
530                 DPRINTF(("wrong sequence number\n"));
531                 error = EINVAL;
532                 goto done2;
533         }
534
535         error = msq_prison_cansee(rpr, msqkptr);
536         if (error != 0) {
537                 DPRINTF(("requester can't see prison\n"));
538                 goto done2;
539         }
540
541 #ifdef MAC
542         error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd);
543         if (error != 0)
544                 goto done2;
545 #endif
546
547         error = 0;
548         rval = 0;
549
550         switch (cmd) {
551
552         case IPC_RMID:
553         {
554 #ifdef MAC
555                 struct msg *msghdr;
556 #endif
557                 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
558                         goto done2;
559
560 #ifdef MAC
561                 /*
562                  * Check that the thread has MAC access permissions to
563                  * individual msghdrs.  Note: We need to do this in a
564                  * separate loop because the actual loop alters the
565                  * msq/msghdr info as it progresses, and there is no going
566                  * back if half the way through we discover that the
567                  * thread cannot free a certain msghdr.  The msq will get
568                  * into an inconsistent state.
569                  */
570                 for (msghdr = msqkptr->u.__msg_first; msghdr != NULL;
571                     msghdr = msghdr->msg_next) {
572                         error = mac_sysvmsq_check_msgrmid(td->td_ucred, msghdr);
573                         if (error != 0)
574                                 goto done2;
575                 }
576 #endif
577
578                 msq_remove(msqkptr);
579         }
580
581                 break;
582
583         case IPC_SET:
584                 AUDIT_ARG_SVIPC_PERM(&msqbuf->msg_perm);
585                 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
586                         goto done2;
587                 if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
588                         error = priv_check(td, PRIV_IPC_MSGSIZE);
589                         if (error)
590                                 goto done2;
591                 }
592                 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
593                         DPRINTF(("can't increase msg_qbytes beyond %d"
594                             "(truncating)\n", msginfo.msgmnb));
595                         msqbuf->msg_qbytes = msginfo.msgmnb;    /* silently restrict qbytes to system limit */
596                 }
597                 if (msqbuf->msg_qbytes == 0) {
598                         DPRINTF(("can't reduce msg_qbytes to 0\n"));
599                         error = EINVAL;         /* non-standard errno! */
600                         goto done2;
601                 }
602                 msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */
603                 msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */
604                 msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
605                     (msqbuf->msg_perm.mode & 0777);
606                 msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
607                 msqkptr->u.msg_ctime = time_second;
608                 break;
609
610         case IPC_STAT:
611                 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
612                         DPRINTF(("requester doesn't have read access\n"));
613                         goto done2;
614                 }
615                 *msqbuf = msqkptr->u;
616                 if (td->td_ucred->cr_prison != msqkptr->cred->cr_prison)
617                         msqbuf->msg_perm.key = IPC_PRIVATE;
618
619                 /*
620                  * Try to hide the fact that the structure layout is shared by
621                  * both the kernel and userland.  These pointers are not useful
622                  * to userspace.
623                  */
624                 msqbuf->__msg_first = msqbuf->__msg_last = NULL;
625                 break;
626
627         default:
628                 DPRINTF(("invalid command %d\n", cmd));
629                 error = EINVAL;
630                 goto done2;
631         }
632
633         if (error == 0)
634                 td->td_retval[0] = rval;
635 done2:
636         mtx_unlock(&msq_mtx);
637         return (error);
638 }
639
640 #ifndef _SYS_SYSPROTO_H_
641 struct msgget_args {
642         key_t   key;
643         int     msgflg;
644 };
645 #endif
646
647 int
648 sys_msgget(struct thread *td, struct msgget_args *uap)
649 {
650         int msqid, error = 0;
651         int key = uap->key;
652         int msgflg = uap->msgflg;
653         struct ucred *cred = td->td_ucred;
654         struct msqid_kernel *msqkptr = NULL;
655
656         DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
657
658         if (msg_find_prison(cred) == NULL)
659                 return (ENOSYS);
660
661         mtx_lock(&msq_mtx);
662         if (key != IPC_PRIVATE) {
663                 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
664                         msqkptr = &msqids[msqid];
665                         if (msqkptr->u.msg_qbytes != 0 &&
666                             msqkptr->cred != NULL &&
667                             msqkptr->cred->cr_prison == cred->cr_prison &&
668                             msqkptr->u.msg_perm.key == key)
669                                 break;
670                 }
671                 if (msqid < msginfo.msgmni) {
672                         DPRINTF(("found public key\n"));
673                         if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
674                                 DPRINTF(("not exclusive\n"));
675                                 error = EEXIST;
676                                 goto done2;
677                         }
678                         AUDIT_ARG_SVIPC_ID(IXSEQ_TO_IPCID(msqid,
679                             msqkptr->u.msg_perm));
680                         if ((error = ipcperm(td, &msqkptr->u.msg_perm,
681                             msgflg & 0700))) {
682                                 DPRINTF(("requester doesn't have 0%o access\n",
683                                     msgflg & 0700));
684                                 goto done2;
685                         }
686 #ifdef MAC
687                         error = mac_sysvmsq_check_msqget(cred, msqkptr);
688                         if (error != 0)
689                                 goto done2;
690 #endif
691                         goto found;
692                 }
693         }
694
695         DPRINTF(("need to allocate the msqid_ds\n"));
696         if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
697                 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
698                         /*
699                          * Look for an unallocated and unlocked msqid_ds.
700                          * msqid_ds's can be locked by msgsnd or msgrcv while
701                          * they are copying the message in/out.  We can't
702                          * re-use the entry until they release it.
703                          */
704                         msqkptr = &msqids[msqid];
705                         if (msqkptr->u.msg_qbytes == 0 &&
706                             (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
707                                 break;
708                 }
709                 if (msqid == msginfo.msgmni) {
710                         DPRINTF(("no more msqid_ds's available\n"));
711                         error = ENOSPC;
712                         goto done2;
713                 }
714 #ifdef RACCT
715                 if (racct_enable) {
716                         PROC_LOCK(td->td_proc);
717                         error = racct_add(td->td_proc, RACCT_NMSGQ, 1);
718                         PROC_UNLOCK(td->td_proc);
719                         if (error != 0) {
720                                 error = ENOSPC;
721                                 goto done2;
722                         }
723                 }
724 #endif
725                 DPRINTF(("msqid %d is available\n", msqid));
726                 msqkptr->u.msg_perm.key = key;
727                 msqkptr->u.msg_perm.cuid = cred->cr_uid;
728                 msqkptr->u.msg_perm.uid = cred->cr_uid;
729                 msqkptr->u.msg_perm.cgid = cred->cr_gid;
730                 msqkptr->u.msg_perm.gid = cred->cr_gid;
731                 msqkptr->u.msg_perm.mode = (msgflg & 0777);
732                 msqkptr->cred = crhold(cred);
733                 /* Make sure that the returned msqid is unique */
734                 msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
735                 msqkptr->u.__msg_first = NULL;
736                 msqkptr->u.__msg_last = NULL;
737                 msqkptr->u.msg_cbytes = 0;
738                 msqkptr->u.msg_qnum = 0;
739                 msqkptr->u.msg_qbytes = msginfo.msgmnb;
740                 msqkptr->u.msg_lspid = 0;
741                 msqkptr->u.msg_lrpid = 0;
742                 msqkptr->u.msg_stime = 0;
743                 msqkptr->u.msg_rtime = 0;
744                 msqkptr->u.msg_ctime = time_second;
745 #ifdef MAC
746                 mac_sysvmsq_create(cred, msqkptr);
747 #endif
748                 AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
749         } else {
750                 DPRINTF(("didn't find it and wasn't asked to create it\n"));
751                 error = ENOENT;
752                 goto done2;
753         }
754
755 found:
756         /* Construct the unique msqid */
757         td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
758 done2:
759         mtx_unlock(&msq_mtx);
760         return (error);
761 }
762
763 #ifndef _SYS_SYSPROTO_H_
764 struct msgsnd_args {
765         int     msqid;
766         const void      *msgp;  /* XXX msgp is actually mtext. */
767         size_t  msgsz;
768         int     msgflg;
769 };
770 #endif
771 int
772 kern_msgsnd(struct thread *td, int msqid, const void *msgp,
773     size_t msgsz, int msgflg, long mtype)
774 {
775         int msqix, segs_needed, error = 0;
776         struct msqid_kernel *msqkptr;
777         struct msg *msghdr;
778         struct prison *rpr;
779         short next;
780 #ifdef RACCT
781         size_t saved_msgsz = 0;
782 #endif
783
784         rpr = msg_find_prison(td->td_ucred);
785         if (rpr == NULL)
786                 return (ENOSYS);
787
788         mtx_lock(&msq_mtx);
789         AUDIT_ARG_SVIPC_ID(msqid);
790         msqix = IPCID_TO_IX(msqid);
791
792         if (msqix < 0 || msqix >= msginfo.msgmni) {
793                 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
794                     msginfo.msgmni));
795                 error = EINVAL;
796                 goto done2;
797         }
798
799         msqkptr = &msqids[msqix];
800         AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
801         if (msqkptr->u.msg_qbytes == 0) {
802                 DPRINTF(("no such message queue id\n"));
803                 error = EINVAL;
804                 goto done2;
805         }
806         if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
807                 DPRINTF(("wrong sequence number\n"));
808                 error = EINVAL;
809                 goto done2;
810         }
811
812         if ((error = msq_prison_cansee(rpr, msqkptr))) {
813                 DPRINTF(("requester can't see prison\n"));
814                 goto done2;
815         }
816
817         if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
818                 DPRINTF(("requester doesn't have write access\n"));
819                 goto done2;
820         }
821
822 #ifdef MAC
823         error = mac_sysvmsq_check_msqsnd(td->td_ucred, msqkptr);
824         if (error != 0)
825                 goto done2;
826 #endif
827
828 #ifdef RACCT
829         if (racct_enable) {
830                 PROC_LOCK(td->td_proc);
831                 if (racct_add(td->td_proc, RACCT_MSGQQUEUED, 1)) {
832                         PROC_UNLOCK(td->td_proc);
833                         error = EAGAIN;
834                         goto done2;
835                 }
836                 saved_msgsz = msgsz;
837                 if (racct_add(td->td_proc, RACCT_MSGQSIZE, msgsz)) {
838                         racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
839                         PROC_UNLOCK(td->td_proc);
840                         error = EAGAIN;
841                         goto done2;
842                 }
843                 PROC_UNLOCK(td->td_proc);
844         }
845 #endif
846
847         segs_needed = howmany(msgsz, msginfo.msgssz);
848         DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz,
849             msginfo.msgssz, segs_needed));
850         for (;;) {
851                 int need_more_resources = 0;
852
853                 /*
854                  * check msgsz
855                  * (inside this loop in case msg_qbytes changes while we sleep)
856                  */
857
858                 if (msgsz > msqkptr->u.msg_qbytes) {
859                         DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
860                         error = EINVAL;
861                         goto done3;
862                 }
863
864                 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
865                         DPRINTF(("msqid is locked\n"));
866                         need_more_resources = 1;
867                 }
868                 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
869                         DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
870                         need_more_resources = 1;
871                 }
872                 if (segs_needed > nfree_msgmaps) {
873                         DPRINTF(("segs_needed > nfree_msgmaps\n"));
874                         need_more_resources = 1;
875                 }
876                 if (free_msghdrs == NULL) {
877                         DPRINTF(("no more msghdrs\n"));
878                         need_more_resources = 1;
879                 }
880
881                 if (need_more_resources) {
882                         int we_own_it;
883
884                         if ((msgflg & IPC_NOWAIT) != 0) {
885                                 DPRINTF(("need more resources but caller "
886                                     "doesn't want to wait\n"));
887                                 error = EAGAIN;
888                                 goto done3;
889                         }
890
891                         if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
892                                 DPRINTF(("we don't own the msqid_ds\n"));
893                                 we_own_it = 0;
894                         } else {
895                                 /* Force later arrivals to wait for our
896                                    request */
897                                 DPRINTF(("we own the msqid_ds\n"));
898                                 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
899                                 we_own_it = 1;
900                         }
901                         DPRINTF(("msgsnd:  goodnight\n"));
902                         error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
903                             "msgsnd", hz);
904                         DPRINTF(("msgsnd:  good morning, error=%d\n", error));
905                         if (we_own_it)
906                                 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
907                         if (error == EWOULDBLOCK) {
908                                 DPRINTF(("msgsnd:  timed out\n"));
909                                 continue;
910                         }
911                         if (error != 0) {
912                                 DPRINTF(("msgsnd:  interrupted system call\n"));
913                                 error = EINTR;
914                                 goto done3;
915                         }
916
917                         /*
918                          * Make sure that the msq queue still exists
919                          */
920
921                         if (msqkptr->u.msg_qbytes == 0) {
922                                 DPRINTF(("msqid deleted\n"));
923                                 error = EIDRM;
924                                 goto done3;
925                         }
926
927                 } else {
928                         DPRINTF(("got all the resources that we need\n"));
929                         break;
930                 }
931         }
932
933         /*
934          * We have the resources that we need.
935          * Make sure!
936          */
937
938         if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
939                 panic("msg_perm.mode & MSG_LOCKED");
940         if (segs_needed > nfree_msgmaps)
941                 panic("segs_needed > nfree_msgmaps");
942         if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
943                 panic("msgsz + msg_cbytes > msg_qbytes");
944         if (free_msghdrs == NULL)
945                 panic("no more msghdrs");
946
947         /*
948          * Re-lock the msqid_ds in case we page-fault when copying in the
949          * message
950          */
951
952         if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
953                 panic("msqid_ds is already locked");
954         msqkptr->u.msg_perm.mode |= MSG_LOCKED;
955
956         /*
957          * Allocate a message header
958          */
959
960         msghdr = free_msghdrs;
961         free_msghdrs = msghdr->msg_next;
962         msghdr->msg_spot = -1;
963         msghdr->msg_ts = msgsz;
964         msghdr->msg_type = mtype;
965 #ifdef MAC
966         /*
967          * XXXMAC: Should the mac_sysvmsq_check_msgmsq check follow here
968          * immediately?  Or, should it be checked just before the msg is
969          * enqueued in the msgq (as it is done now)?
970          */
971         mac_sysvmsg_create(td->td_ucred, msqkptr, msghdr);
972 #endif
973
974         /*
975          * Allocate space for the message
976          */
977
978         while (segs_needed > 0) {
979                 if (nfree_msgmaps <= 0)
980                         panic("not enough msgmaps");
981                 if (free_msgmaps == -1)
982                         panic("nil free_msgmaps");
983                 next = free_msgmaps;
984                 if (next <= -1)
985                         panic("next too low #1");
986                 if (next >= msginfo.msgseg)
987                         panic("next out of range #1");
988                 DPRINTF(("allocating segment %d to message\n", next));
989                 free_msgmaps = msgmaps[next].next;
990                 nfree_msgmaps--;
991                 msgmaps[next].next = msghdr->msg_spot;
992                 msghdr->msg_spot = next;
993                 segs_needed--;
994         }
995
996         /*
997          * Validate the message type
998          */
999
1000         if (msghdr->msg_type < 1) {
1001                 msg_freehdr(msghdr);
1002                 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1003                 wakeup(msqkptr);
1004                 DPRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
1005                 error = EINVAL;
1006                 goto done3;
1007         }
1008
1009         /*
1010          * Copy in the message body
1011          */
1012
1013         next = msghdr->msg_spot;
1014         while (msgsz > 0) {
1015                 size_t tlen;
1016                 if (msgsz > msginfo.msgssz)
1017                         tlen = msginfo.msgssz;
1018                 else
1019                         tlen = msgsz;
1020                 if (next <= -1)
1021                         panic("next too low #2");
1022                 if (next >= msginfo.msgseg)
1023                         panic("next out of range #2");
1024                 mtx_unlock(&msq_mtx);
1025                 if ((error = copyin(msgp, &msgpool[next * msginfo.msgssz],
1026                     tlen)) != 0) {
1027                         mtx_lock(&msq_mtx);
1028                         DPRINTF(("error %d copying in message segment\n",
1029                             error));
1030                         msg_freehdr(msghdr);
1031                         msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1032                         wakeup(msqkptr);
1033                         goto done3;
1034                 }
1035                 mtx_lock(&msq_mtx);
1036                 msgsz -= tlen;
1037                 msgp = (const char *)msgp + tlen;
1038                 next = msgmaps[next].next;
1039         }
1040         if (next != -1)
1041                 panic("didn't use all the msg segments");
1042
1043         /*
1044          * We've got the message.  Unlock the msqid_ds.
1045          */
1046
1047         msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
1048
1049         /*
1050          * Make sure that the msqid_ds is still allocated.
1051          */
1052
1053         if (msqkptr->u.msg_qbytes == 0) {
1054                 msg_freehdr(msghdr);
1055                 wakeup(msqkptr);
1056                 error = EIDRM;
1057                 goto done3;
1058         }
1059
1060 #ifdef MAC
1061         /*
1062          * Note: Since the task/thread allocates the msghdr and usually
1063          * primes it with its own MAC label, for a majority of policies, it
1064          * won't be necessary to check whether the msghdr has access
1065          * permissions to the msgq.  The mac_sysvmsq_check_msqsnd check would
1066          * suffice in that case.  However, this hook may be required where
1067          * individual policies derive a non-identical label for the msghdr
1068          * from the current thread label and may want to check the msghdr
1069          * enqueue permissions, along with read/write permissions to the
1070          * msgq.
1071          */
1072         error = mac_sysvmsq_check_msgmsq(td->td_ucred, msghdr, msqkptr);
1073         if (error != 0) {
1074                 msg_freehdr(msghdr);
1075                 wakeup(msqkptr);
1076                 goto done3;
1077         }
1078 #endif
1079
1080         /*
1081          * Put the message into the queue
1082          */
1083         if (msqkptr->u.__msg_first == NULL) {
1084                 msqkptr->u.__msg_first = msghdr;
1085                 msqkptr->u.__msg_last = msghdr;
1086         } else {
1087                 msqkptr->u.__msg_last->msg_next = msghdr;
1088                 msqkptr->u.__msg_last = msghdr;
1089         }
1090         msqkptr->u.__msg_last->msg_next = NULL;
1091
1092         msqkptr->u.msg_cbytes += msghdr->msg_ts;
1093         msqkptr->u.msg_qnum++;
1094         msqkptr->u.msg_lspid = td->td_proc->p_pid;
1095         msqkptr->u.msg_stime = time_second;
1096
1097         wakeup(msqkptr);
1098         td->td_retval[0] = 0;
1099 done3:
1100 #ifdef RACCT
1101         if (racct_enable && error != 0) {
1102                 PROC_LOCK(td->td_proc);
1103                 racct_sub(td->td_proc, RACCT_MSGQQUEUED, 1);
1104                 racct_sub(td->td_proc, RACCT_MSGQSIZE, saved_msgsz);
1105                 PROC_UNLOCK(td->td_proc);
1106         }
1107 #endif
1108 done2:
1109         mtx_unlock(&msq_mtx);
1110         return (error);
1111 }
1112
1113 int
1114 sys_msgsnd(struct thread *td, struct msgsnd_args *uap)
1115 {
1116         int error;
1117         long mtype;
1118
1119         DPRINTF(("call to msgsnd(%d, %p, %zu, %d)\n", uap->msqid, uap->msgp,
1120             uap->msgsz, uap->msgflg));
1121
1122         if ((error = copyin(uap->msgp, &mtype, sizeof(mtype))) != 0) {
1123                 DPRINTF(("error %d copying the message type\n", error));
1124                 return (error);
1125         }
1126         return (kern_msgsnd(td, uap->msqid,
1127             (const char *)uap->msgp + sizeof(mtype),
1128             uap->msgsz, uap->msgflg, mtype));
1129 }
1130
1131 #ifndef _SYS_SYSPROTO_H_
1132 struct msgrcv_args {
1133         int     msqid;
1134         void    *msgp;
1135         size_t  msgsz;
1136         long    msgtyp;
1137         int     msgflg;
1138 };
1139 #endif
1140 /* XXX msgp is actually mtext. */
1141 int
1142 kern_msgrcv(struct thread *td, int msqid, void *msgp, size_t msgsz, long msgtyp,
1143     int msgflg, long *mtype)
1144 {
1145         size_t len;
1146         struct msqid_kernel *msqkptr;
1147         struct msg *msghdr;
1148         struct prison *rpr;
1149         int msqix, error = 0;
1150         short next;
1151
1152         rpr = msg_find_prison(td->td_ucred);
1153         if (rpr == NULL)
1154                 return (ENOSYS);
1155
1156         AUDIT_ARG_SVIPC_ID(msqid);
1157         msqix = IPCID_TO_IX(msqid);
1158
1159         if (msqix < 0 || msqix >= msginfo.msgmni) {
1160                 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
1161                     msginfo.msgmni));
1162                 return (EINVAL);
1163         }
1164
1165         msqkptr = &msqids[msqix];
1166         mtx_lock(&msq_mtx);
1167         AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm);
1168         if (msqkptr->u.msg_qbytes == 0) {
1169                 DPRINTF(("no such message queue id\n"));
1170                 error = EINVAL;
1171                 goto done2;
1172         }
1173         if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1174                 DPRINTF(("wrong sequence number\n"));
1175                 error = EINVAL;
1176                 goto done2;
1177         }
1178
1179         if ((error = msq_prison_cansee(rpr, msqkptr))) {
1180                 DPRINTF(("requester can't see prison\n"));
1181                 goto done2;
1182         }
1183
1184         if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1185                 DPRINTF(("requester doesn't have read access\n"));
1186                 goto done2;
1187         }
1188
1189 #ifdef MAC
1190         error = mac_sysvmsq_check_msqrcv(td->td_ucred, msqkptr);
1191         if (error != 0)
1192                 goto done2;
1193 #endif
1194
1195         msghdr = NULL;
1196         while (msghdr == NULL) {
1197                 if (msgtyp == 0) {
1198                         msghdr = msqkptr->u.__msg_first;
1199                         if (msghdr != NULL) {
1200                                 if (msgsz < msghdr->msg_ts &&
1201                                     (msgflg & MSG_NOERROR) == 0) {
1202                                         DPRINTF(("first message on the queue "
1203                                             "is too big (want %zu, got %d)\n",
1204                                             msgsz, msghdr->msg_ts));
1205                                         error = E2BIG;
1206                                         goto done2;
1207                                 }
1208 #ifdef MAC
1209                                 error = mac_sysvmsq_check_msgrcv(td->td_ucred,
1210                                     msghdr);
1211                                 if (error != 0)
1212                                         goto done2;
1213 #endif
1214                                 if (msqkptr->u.__msg_first ==
1215                                     msqkptr->u.__msg_last) {
1216                                         msqkptr->u.__msg_first = NULL;
1217                                         msqkptr->u.__msg_last = NULL;
1218                                 } else {
1219                                         msqkptr->u.__msg_first = msghdr->msg_next;
1220                                         if (msqkptr->u.__msg_first == NULL)
1221                                                 panic("msg_first/last screwed up #1");
1222                                 }
1223                         }
1224                 } else {
1225                         struct msg *previous;
1226                         struct msg **prev;
1227
1228                         previous = NULL;
1229                         prev = &(msqkptr->u.__msg_first);
1230                         while ((msghdr = *prev) != NULL) {
1231                                 /*
1232                                  * Is this message's type an exact match or is
1233                                  * this message's type less than or equal to
1234                                  * the absolute value of a negative msgtyp?
1235                                  * Note that the second half of this test can
1236                                  * NEVER be true if msgtyp is positive since
1237                                  * msg_type is always positive!
1238                                  */
1239
1240                                 if (msgtyp == msghdr->msg_type ||
1241                                     msghdr->msg_type <= -msgtyp) {
1242                                         DPRINTF(("found message type %ld, "
1243                                             "requested %ld\n",
1244                                             msghdr->msg_type, msgtyp));
1245                                         if (msgsz < msghdr->msg_ts &&
1246                                             (msgflg & MSG_NOERROR) == 0) {
1247                                                 DPRINTF(("requested message "
1248                                                     "on the queue is too big "
1249                                                     "(want %zu, got %hu)\n",
1250                                                     msgsz, msghdr->msg_ts));
1251                                                 error = E2BIG;
1252                                                 goto done2;
1253                                         }
1254 #ifdef MAC
1255                                         error = mac_sysvmsq_check_msgrcv(
1256                                             td->td_ucred, msghdr);
1257                                         if (error != 0)
1258                                                 goto done2;
1259 #endif
1260                                         *prev = msghdr->msg_next;
1261                                         if (msghdr == msqkptr->u.__msg_last) {
1262                                                 if (previous == NULL) {
1263                                                         if (prev !=
1264                                                             &msqkptr->u.__msg_first)
1265                                                                 panic("__msg_first/last screwed up #2");
1266                                                         msqkptr->u.__msg_first =
1267                                                             NULL;
1268                                                         msqkptr->u.__msg_last =
1269                                                             NULL;
1270                                                 } else {
1271                                                         if (prev ==
1272                                                             &msqkptr->u.__msg_first)
1273                                                                 panic("__msg_first/last screwed up #3");
1274                                                         msqkptr->u.__msg_last =
1275                                                             previous;
1276                                                 }
1277                                         }
1278                                         break;
1279                                 }
1280                                 previous = msghdr;
1281                                 prev = &(msghdr->msg_next);
1282                         }
1283                 }
1284
1285                 /*
1286                  * We've either extracted the msghdr for the appropriate
1287                  * message or there isn't one.
1288                  * If there is one then bail out of this loop.
1289                  */
1290
1291                 if (msghdr != NULL)
1292                         break;
1293
1294                 /*
1295                  * Hmph!  No message found.  Does the user want to wait?
1296                  */
1297
1298                 if ((msgflg & IPC_NOWAIT) != 0) {
1299                         DPRINTF(("no appropriate message found (msgtyp=%ld)\n",
1300                             msgtyp));
1301                         /* The SVID says to return ENOMSG. */
1302                         error = ENOMSG;
1303                         goto done2;
1304                 }
1305
1306                 /*
1307                  * Wait for something to happen
1308                  */
1309
1310                 DPRINTF(("msgrcv:  goodnight\n"));
1311                 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1312                     "msgrcv", 0);
1313                 DPRINTF(("msgrcv:  good morning (error=%d)\n", error));
1314
1315                 if (error != 0) {
1316                         DPRINTF(("msgrcv:  interrupted system call\n"));
1317                         error = EINTR;
1318                         goto done2;
1319                 }
1320
1321                 /*
1322                  * Make sure that the msq queue still exists
1323                  */
1324
1325                 if (msqkptr->u.msg_qbytes == 0 ||
1326                     msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
1327                         DPRINTF(("msqid deleted\n"));
1328                         error = EIDRM;
1329                         goto done2;
1330                 }
1331         }
1332
1333         /*
1334          * Return the message to the user.
1335          *
1336          * First, do the bookkeeping (before we risk being interrupted).
1337          */
1338
1339         msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1340         msqkptr->u.msg_qnum--;
1341         msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1342         msqkptr->u.msg_rtime = time_second;
1343
1344         racct_sub_cred(msqkptr->cred, RACCT_MSGQQUEUED, 1);
1345         racct_sub_cred(msqkptr->cred, RACCT_MSGQSIZE, msghdr->msg_ts);
1346
1347         /*
1348          * Make msgsz the actual amount that we'll be returning.
1349          * Note that this effectively truncates the message if it is too long
1350          * (since msgsz is never increased).
1351          */
1352
1353         DPRINTF(("found a message, msgsz=%zu, msg_ts=%hu\n", msgsz,
1354             msghdr->msg_ts));
1355         if (msgsz > msghdr->msg_ts)
1356                 msgsz = msghdr->msg_ts;
1357         *mtype = msghdr->msg_type;
1358
1359         /*
1360          * Return the segments to the user
1361          */
1362
1363         next = msghdr->msg_spot;
1364         for (len = 0; len < msgsz; len += msginfo.msgssz) {
1365                 size_t tlen;
1366
1367                 if (msgsz - len > msginfo.msgssz)
1368                         tlen = msginfo.msgssz;
1369                 else
1370                         tlen = msgsz - len;
1371                 if (next <= -1)
1372                         panic("next too low #3");
1373                 if (next >= msginfo.msgseg)
1374                         panic("next out of range #3");
1375                 mtx_unlock(&msq_mtx);
1376                 error = copyout(&msgpool[next * msginfo.msgssz], msgp, tlen);
1377                 mtx_lock(&msq_mtx);
1378                 if (error != 0) {
1379                         DPRINTF(("error (%d) copying out message segment\n",
1380                             error));
1381                         msg_freehdr(msghdr);
1382                         wakeup(msqkptr);
1383                         goto done2;
1384                 }
1385                 msgp = (char *)msgp + tlen;
1386                 next = msgmaps[next].next;
1387         }
1388
1389         /*
1390          * Done, return the actual number of bytes copied out.
1391          */
1392
1393         msg_freehdr(msghdr);
1394         wakeup(msqkptr);
1395         td->td_retval[0] = msgsz;
1396 done2:
1397         mtx_unlock(&msq_mtx);
1398         return (error);
1399 }
1400
1401 int
1402 sys_msgrcv(struct thread *td, struct msgrcv_args *uap)
1403 {
1404         int error;
1405         long mtype;
1406
1407         DPRINTF(("call to msgrcv(%d, %p, %zu, %ld, %d)\n", uap->msqid,
1408             uap->msgp, uap->msgsz, uap->msgtyp, uap->msgflg));
1409
1410         if ((error = kern_msgrcv(td, uap->msqid,
1411             (char *)uap->msgp + sizeof(mtype), uap->msgsz,
1412             uap->msgtyp, uap->msgflg, &mtype)) != 0)
1413                 return (error);
1414         if ((error = copyout(&mtype, uap->msgp, sizeof(mtype))) != 0)
1415                 DPRINTF(("error %d copying the message type\n", error));
1416         return (error);
1417 }
1418
1419 static int
1420 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1421 {
1422         struct msqid_kernel tmsqk;
1423 #ifdef COMPAT_FREEBSD32
1424         struct msqid_kernel32 tmsqk32;
1425 #endif
1426         struct prison *pr, *rpr;
1427         void *outaddr;
1428         size_t outsize;
1429         int error, i;
1430
1431         pr = req->td->td_ucred->cr_prison;
1432         rpr = msg_find_prison(req->td->td_ucred);
1433         error = 0;
1434         for (i = 0; i < msginfo.msgmni; i++) {
1435                 mtx_lock(&msq_mtx);
1436                 if (msqids[i].u.msg_qbytes == 0 || rpr == NULL ||
1437                     msq_prison_cansee(rpr, &msqids[i]) != 0)
1438                         bzero(&tmsqk, sizeof(tmsqk));
1439                 else {
1440                         tmsqk = msqids[i];
1441                         if (tmsqk.cred->cr_prison != pr)
1442                                 tmsqk.u.msg_perm.key = IPC_PRIVATE;
1443                 }
1444                 mtx_unlock(&msq_mtx);
1445 #ifdef COMPAT_FREEBSD32
1446                 if (SV_CURPROC_FLAG(SV_ILP32)) {
1447                         bzero(&tmsqk32, sizeof(tmsqk32));
1448                         freebsd32_ipcperm_out(&tmsqk.u.msg_perm,
1449                             &tmsqk32.u.msg_perm);
1450                         /* Don't copy u.msg_first or u.msg_last */
1451                         CP(tmsqk, tmsqk32, u.msg_cbytes);
1452                         CP(tmsqk, tmsqk32, u.msg_qnum);
1453                         CP(tmsqk, tmsqk32, u.msg_qbytes);
1454                         CP(tmsqk, tmsqk32, u.msg_lspid);
1455                         CP(tmsqk, tmsqk32, u.msg_lrpid);
1456                         CP(tmsqk, tmsqk32, u.msg_stime);
1457                         CP(tmsqk, tmsqk32, u.msg_rtime);
1458                         CP(tmsqk, tmsqk32, u.msg_ctime);
1459                         /* Don't copy label or cred */
1460                         outaddr = &tmsqk32;
1461                         outsize = sizeof(tmsqk32);
1462                 } else
1463 #endif
1464                 {
1465                         /* Don't leak kernel pointers */
1466                         tmsqk.u.__msg_first = NULL;
1467                         tmsqk.u.__msg_last = NULL;
1468                         tmsqk.label = NULL;
1469                         tmsqk.cred = NULL;
1470                         /*
1471                          * XXX: some padding also exists, but we take care to
1472                          * allocate our pool of msqid_kernel structs with
1473                          * zeroed memory so this should be OK.
1474                          */
1475                         outaddr = &tmsqk;
1476                         outsize = sizeof(tmsqk);
1477                 }
1478                 error = SYSCTL_OUT(req, outaddr, outsize);
1479                 if (error != 0)
1480                         break;
1481         }
1482         return (error);
1483 }
1484
1485 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1486     "Maximum message size");
1487 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1488     "Number of message queue identifiers");
1489 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1490     "Maximum number of bytes in a queue");
1491 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1492     "Maximum number of messages in the system");
1493 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1494     "Size of a message segment");
1495 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1496     "Number of message segments");
1497 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids,
1498     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
1499     NULL, 0, sysctl_msqids, "",
1500     "Array of struct msqid_kernel for each potential message queue");
1501
1502 static int
1503 msg_prison_check(void *obj, void *data)
1504 {
1505         struct prison *pr = obj;
1506         struct prison *prpr;
1507         struct vfsoptlist *opts = data;
1508         int error, jsys;
1509
1510         /*
1511          * sysvmsg is a jailsys integer.
1512          * It must be "disable" if the parent jail is disabled.
1513          */
1514         error = vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1515         if (error != ENOENT) {
1516                 if (error != 0)
1517                         return (error);
1518                 switch (jsys) {
1519                 case JAIL_SYS_DISABLE:
1520                         break;
1521                 case JAIL_SYS_NEW:
1522                 case JAIL_SYS_INHERIT:
1523                         prison_lock(pr->pr_parent);
1524                         prpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1525                         prison_unlock(pr->pr_parent);
1526                         if (prpr == NULL)
1527                                 return (EPERM);
1528                         break;
1529                 default:
1530                         return (EINVAL);
1531                 }
1532         }
1533
1534         return (0);
1535 }
1536
1537 static int
1538 msg_prison_set(void *obj, void *data)
1539 {
1540         struct prison *pr = obj;
1541         struct prison *tpr, *orpr, *nrpr, *trpr;
1542         struct vfsoptlist *opts = data;
1543         void *rsv;
1544         int jsys, descend;
1545
1546         /*
1547          * sysvmsg controls which jail is the root of the associated msgs (this
1548          * jail or same as the parent), or if the feature is available at all.
1549          */
1550         if (vfs_copyopt(opts, "sysvmsg", &jsys, sizeof(jsys)) == ENOENT)
1551                 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1552                     ? JAIL_SYS_INHERIT
1553                     : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1554                     ? JAIL_SYS_DISABLE
1555                     : -1;
1556         if (jsys == JAIL_SYS_DISABLE) {
1557                 prison_lock(pr);
1558                 orpr = osd_jail_get(pr, msg_prison_slot);
1559                 if (orpr != NULL)
1560                         osd_jail_del(pr, msg_prison_slot);
1561                 prison_unlock(pr);
1562                 if (orpr != NULL) {
1563                         if (orpr == pr)
1564                                 msg_prison_cleanup(pr);
1565                         /* Disable all child jails as well. */
1566                         FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1567                                 prison_lock(tpr);
1568                                 trpr = osd_jail_get(tpr, msg_prison_slot);
1569                                 if (trpr != NULL) {
1570                                         osd_jail_del(tpr, msg_prison_slot);
1571                                         prison_unlock(tpr);
1572                                         if (trpr == tpr)
1573                                                 msg_prison_cleanup(tpr);
1574                                 } else {
1575                                         prison_unlock(tpr);
1576                                         descend = 0;
1577                                 }
1578                         }
1579                 }
1580         } else if (jsys != -1) {
1581                 if (jsys == JAIL_SYS_NEW)
1582                         nrpr = pr;
1583                 else {
1584                         prison_lock(pr->pr_parent);
1585                         nrpr = osd_jail_get(pr->pr_parent, msg_prison_slot);
1586                         prison_unlock(pr->pr_parent);
1587                 }
1588                 rsv = osd_reserve(msg_prison_slot);
1589                 prison_lock(pr);
1590                 orpr = osd_jail_get(pr, msg_prison_slot);
1591                 if (orpr != nrpr)
1592                         (void)osd_jail_set_reserved(pr, msg_prison_slot, rsv,
1593                             nrpr);
1594                 else
1595                         osd_free_reserved(rsv);
1596                 prison_unlock(pr);
1597                 if (orpr != nrpr) {
1598                         if (orpr == pr)
1599                                 msg_prison_cleanup(pr);
1600                         if (orpr != NULL) {
1601                                 /* Change child jails matching the old root, */
1602                                 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1603                                         prison_lock(tpr);
1604                                         trpr = osd_jail_get(tpr,
1605                                             msg_prison_slot);
1606                                         if (trpr == orpr) {
1607                                                 (void)osd_jail_set(tpr,
1608                                                     msg_prison_slot, nrpr);
1609                                                 prison_unlock(tpr);
1610                                                 if (trpr == tpr)
1611                                                         msg_prison_cleanup(tpr);
1612                                         } else {
1613                                                 prison_unlock(tpr);
1614                                                 descend = 0;
1615                                         }
1616                                 }
1617                         }
1618                 }
1619         }
1620
1621         return (0);
1622 }
1623
1624 static int
1625 msg_prison_get(void *obj, void *data)
1626 {
1627         struct prison *pr = obj;
1628         struct prison *rpr;
1629         struct vfsoptlist *opts = data;
1630         int error, jsys;
1631
1632         /* Set sysvmsg based on the jail's root prison. */
1633         prison_lock(pr);
1634         rpr = osd_jail_get(pr, msg_prison_slot);
1635         prison_unlock(pr);
1636         jsys = rpr == NULL ? JAIL_SYS_DISABLE
1637             : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1638         error = vfs_setopt(opts, "sysvmsg", &jsys, sizeof(jsys));
1639         if (error == ENOENT)
1640                 error = 0;
1641         return (error);
1642 }
1643
1644 static int
1645 msg_prison_remove(void *obj, void *data __unused)
1646 {
1647         struct prison *pr = obj;
1648         struct prison *rpr;
1649
1650         prison_lock(pr);
1651         rpr = osd_jail_get(pr, msg_prison_slot);
1652         prison_unlock(pr);
1653         if (rpr == pr)
1654                 msg_prison_cleanup(pr);
1655         return (0);
1656 }
1657
1658 static void
1659 msg_prison_cleanup(struct prison *pr)
1660 {
1661         struct msqid_kernel *msqkptr;
1662         int i;
1663
1664         /* Remove any msqs that belong to this jail. */
1665         mtx_lock(&msq_mtx);
1666         for (i = 0; i < msginfo.msgmni; i++) {
1667                 msqkptr = &msqids[i];
1668                 if (msqkptr->u.msg_qbytes != 0 &&
1669                     msqkptr->cred != NULL && msqkptr->cred->cr_prison == pr)
1670                         msq_remove(msqkptr);
1671         }
1672         mtx_unlock(&msq_mtx);
1673 }
1674
1675 SYSCTL_JAIL_PARAM_SYS_NODE(sysvmsg, CTLFLAG_RW, "SYSV message queues");
1676
1677 #ifdef COMPAT_FREEBSD32
1678 int
1679 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1680 {
1681
1682 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1683     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1684         AUDIT_ARG_SVIPC_WHICH(uap->which);
1685         switch (uap->which) {
1686         case 0:
1687                 return (freebsd7_freebsd32_msgctl(td,
1688                     (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
1689         case 2:
1690                 return (freebsd32_msgsnd(td,
1691                     (struct freebsd32_msgsnd_args *)&uap->a2));
1692         case 3:
1693                 return (freebsd32_msgrcv(td,
1694                     (struct freebsd32_msgrcv_args *)&uap->a2));
1695         default:
1696                 return (sys_msgsys(td, (struct msgsys_args *)uap));
1697         }
1698 #else
1699         return (nosys(td, NULL));
1700 #endif
1701 }
1702
1703 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1704     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1705 int
1706 freebsd7_freebsd32_msgctl(struct thread *td,
1707     struct freebsd7_freebsd32_msgctl_args *uap)
1708 {
1709         struct msqid_ds msqbuf;
1710         struct msqid_ds32_old msqbuf32;
1711         int error;
1712
1713         if (uap->cmd == IPC_SET) {
1714                 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1715                 if (error)
1716                         return (error);
1717                 freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1718                 PTRIN_CP(msqbuf32, msqbuf, __msg_first);
1719                 PTRIN_CP(msqbuf32, msqbuf, __msg_last);
1720                 CP(msqbuf32, msqbuf, msg_cbytes);
1721                 CP(msqbuf32, msqbuf, msg_qnum);
1722                 CP(msqbuf32, msqbuf, msg_qbytes);
1723                 CP(msqbuf32, msqbuf, msg_lspid);
1724                 CP(msqbuf32, msqbuf, msg_lrpid);
1725                 CP(msqbuf32, msqbuf, msg_stime);
1726                 CP(msqbuf32, msqbuf, msg_rtime);
1727                 CP(msqbuf32, msqbuf, msg_ctime);
1728         }
1729         error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1730         if (error)
1731                 return (error);
1732         if (uap->cmd == IPC_STAT) {
1733                 bzero(&msqbuf32, sizeof(msqbuf32));
1734                 freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1735                 PTROUT_CP(msqbuf, msqbuf32, __msg_first);
1736                 PTROUT_CP(msqbuf, msqbuf32, __msg_last);
1737                 CP(msqbuf, msqbuf32, msg_cbytes);
1738                 CP(msqbuf, msqbuf32, msg_qnum);
1739                 CP(msqbuf, msqbuf32, msg_qbytes);
1740                 CP(msqbuf, msqbuf32, msg_lspid);
1741                 CP(msqbuf, msqbuf32, msg_lrpid);
1742                 CP(msqbuf, msqbuf32, msg_stime);
1743                 CP(msqbuf, msqbuf32, msg_rtime);
1744                 CP(msqbuf, msqbuf32, msg_ctime);
1745                 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1746         }
1747         return (error);
1748 }
1749 #endif
1750
1751 int
1752 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1753 {
1754         struct msqid_ds msqbuf;
1755         struct msqid_ds32 msqbuf32;
1756         int error;
1757
1758         if (uap->cmd == IPC_SET) {
1759                 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1760                 if (error)
1761                         return (error);
1762                 freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1763                 PTRIN_CP(msqbuf32, msqbuf, __msg_first);
1764                 PTRIN_CP(msqbuf32, msqbuf, __msg_last);
1765                 CP(msqbuf32, msqbuf, msg_cbytes);
1766                 CP(msqbuf32, msqbuf, msg_qnum);
1767                 CP(msqbuf32, msqbuf, msg_qbytes);
1768                 CP(msqbuf32, msqbuf, msg_lspid);
1769                 CP(msqbuf32, msqbuf, msg_lrpid);
1770                 CP(msqbuf32, msqbuf, msg_stime);
1771                 CP(msqbuf32, msqbuf, msg_rtime);
1772                 CP(msqbuf32, msqbuf, msg_ctime);
1773         }
1774         error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1775         if (error)
1776                 return (error);
1777         if (uap->cmd == IPC_STAT) {
1778                 freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1779                 PTROUT_CP(msqbuf, msqbuf32, __msg_first);
1780                 PTROUT_CP(msqbuf, msqbuf32, __msg_last);
1781                 CP(msqbuf, msqbuf32, msg_cbytes);
1782                 CP(msqbuf, msqbuf32, msg_qnum);
1783                 CP(msqbuf, msqbuf32, msg_qbytes);
1784                 CP(msqbuf, msqbuf32, msg_lspid);
1785                 CP(msqbuf, msqbuf32, msg_lrpid);
1786                 CP(msqbuf, msqbuf32, msg_stime);
1787                 CP(msqbuf, msqbuf32, msg_rtime);
1788                 CP(msqbuf, msqbuf32, msg_ctime);
1789                 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1790         }
1791         return (error);
1792 }
1793
1794 int
1795 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1796 {
1797         const void *msgp;
1798         long mtype;
1799         int32_t mtype32;
1800         int error;
1801
1802         msgp = PTRIN(uap->msgp);
1803         if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1804                 return (error);
1805         mtype = mtype32;
1806         return (kern_msgsnd(td, uap->msqid,
1807             (const char *)msgp + sizeof(mtype32),
1808             uap->msgsz, uap->msgflg, mtype));
1809 }
1810
1811 int
1812 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1813 {
1814         void *msgp;
1815         long mtype;
1816         int32_t mtype32;
1817         int error;
1818
1819         msgp = PTRIN(uap->msgp);
1820         if ((error = kern_msgrcv(td, uap->msqid,
1821             (char *)msgp + sizeof(mtype32), uap->msgsz,
1822             uap->msgtyp, uap->msgflg, &mtype)) != 0)
1823                 return (error);
1824         mtype32 = (int32_t)mtype;
1825         return (copyout(&mtype32, msgp, sizeof(mtype32)));
1826 }
1827 #endif
1828
1829 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1830     defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1831
1832 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1833 static sy_call_t *msgcalls[] = {
1834         (sy_call_t *)freebsd7_msgctl, (sy_call_t *)sys_msgget,
1835         (sy_call_t *)sys_msgsnd, (sy_call_t *)sys_msgrcv
1836 };
1837
1838 /*
1839  * Entry point for all MSG calls.
1840  *
1841  * XXX actually varargs.
1842  * struct msgsys_args {
1843  *              int     which;
1844  *              int     a2;
1845  *              int     a3;
1846  *              int     a4;
1847  *              int     a5;
1848  *              int     a6;
1849  *      } *uap;
1850  */
1851 int
1852 sys_msgsys(struct thread *td, struct msgsys_args *uap)
1853 {
1854         int error;
1855
1856         AUDIT_ARG_SVIPC_WHICH(uap->which);
1857         if (uap->which < 0 || uap->which >= nitems(msgcalls))
1858                 return (EINVAL);
1859         error = (*msgcalls[uap->which])(td, &uap->a2);
1860         return (error);
1861 }
1862
1863 #ifndef CP
1864 #define CP(src, dst, fld)       do { (dst).fld = (src).fld; } while (0)
1865 #endif
1866
1867 #ifndef _SYS_SYSPROTO_H_
1868 struct freebsd7_msgctl_args {
1869         int     msqid;
1870         int     cmd;
1871         struct  msqid_ds_old *buf;
1872 };
1873 #endif
1874 int
1875 freebsd7_msgctl(struct thread *td, struct freebsd7_msgctl_args *uap)
1876 {
1877         struct msqid_ds_old msqold;
1878         struct msqid_ds msqbuf;
1879         int error;
1880
1881         DPRINTF(("call to freebsd7_msgctl(%d, %d, %p)\n", uap->msqid, uap->cmd,
1882             uap->buf));
1883         if (uap->cmd == IPC_SET) {
1884                 error = copyin(uap->buf, &msqold, sizeof(msqold));
1885                 if (error)
1886                         return (error);
1887                 ipcperm_old2new(&msqold.msg_perm, &msqbuf.msg_perm);
1888                 CP(msqold, msqbuf, __msg_first);
1889                 CP(msqold, msqbuf, __msg_last);
1890                 CP(msqold, msqbuf, msg_cbytes);
1891                 CP(msqold, msqbuf, msg_qnum);
1892                 CP(msqold, msqbuf, msg_qbytes);
1893                 CP(msqold, msqbuf, msg_lspid);
1894                 CP(msqold, msqbuf, msg_lrpid);
1895                 CP(msqold, msqbuf, msg_stime);
1896                 CP(msqold, msqbuf, msg_rtime);
1897                 CP(msqold, msqbuf, msg_ctime);
1898         }
1899         error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1900         if (error)
1901                 return (error);
1902         if (uap->cmd == IPC_STAT) {
1903                 bzero(&msqold, sizeof(msqold));
1904                 ipcperm_new2old(&msqbuf.msg_perm, &msqold.msg_perm);
1905                 CP(msqbuf, msqold, __msg_first);
1906                 CP(msqbuf, msqold, __msg_last);
1907                 CP(msqbuf, msqold, msg_cbytes);
1908                 CP(msqbuf, msqold, msg_qnum);
1909                 CP(msqbuf, msqold, msg_qbytes);
1910                 CP(msqbuf, msqold, msg_lspid);
1911                 CP(msqbuf, msqold, msg_lrpid);
1912                 CP(msqbuf, msqold, msg_stime);
1913                 CP(msqbuf, msqold, msg_rtime);
1914                 CP(msqbuf, msqold, msg_ctime);
1915                 error = copyout(&msqold, uap->buf, sizeof(struct msqid_ds_old));
1916         }
1917         return (error);
1918 }
1919
1920 #undef CP
1921
1922 #endif  /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1923            COMPAT_FREEBSD7 */