FreeBSD/Linux Kernel Cross Reference
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 * Copyright (c) 2003-2005 McAfee, Inc.
21 * All rights reserved.
22 *
23 * This software was developed for the FreeBSD Project in part by McAfee
24 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
25 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
26 * program.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 */
49
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD: releng/6.1/sys/kern/sysv_msg.c 158179 2006-04-30 16:44:43Z cvs2svn $");
52
53 #include "opt_sysvipc.h"
54 #include "opt_mac.h"
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/sysproto.h>
59 #include <sys/kernel.h>
60 #include <sys/proc.h>
61 #include <sys/lock.h>
62 #include <sys/mac.h>
63 #include <sys/mutex.h>
64 #include <sys/module.h>
65 #include <sys/msg.h>
66 #include <sys/syscall.h>
67 #include <sys/syscallsubr.h>
68 #include <sys/sysent.h>
69 #include <sys/sysctl.h>
70 #include <sys/malloc.h>
71 #include <sys/jail.h>
72
73 static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues");
74
75 static void msginit(void);
76 static int msgunload(void);
77 static int sysvmsg_modload(struct module *, int, void *);
78
79 #ifdef MSG_DEBUG
80 #define DPRINTF(a) printf a
81 #else
82 #define DPRINTF(a)
83 #endif
84 #ifdef MAC_DEBUG
85 #define MPRINTF(a) printf a
86 #else
87 #define MPRINTF(a)
88 #endif
89
90 static void msg_freehdr(struct msg *msghdr);
91
92 /* XXX casting to (sy_call_t *) is bogus, as usual. */
93 static sy_call_t *msgcalls[] = {
94 (sy_call_t *)msgctl, (sy_call_t *)msgget,
95 (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
96 };
97
98 #ifndef MSGSSZ
99 #define MSGSSZ 8 /* Each segment must be 2^N long */
100 #endif
101 #ifndef MSGSEG
102 #define MSGSEG 2048 /* must be less than 32767 */
103 #endif
104 #define MSGMAX (MSGSSZ*MSGSEG)
105 #ifndef MSGMNB
106 #define MSGMNB 2048 /* max # of bytes in a queue */
107 #endif
108 #ifndef MSGMNI
109 #define MSGMNI 40
110 #endif
111 #ifndef MSGTQL
112 #define MSGTQL 40
113 #endif
114
115 /*
116 * Based on the configuration parameters described in an SVR2 (yes, two)
117 * config(1m) man page.
118 *
119 * Each message is broken up and stored in segments that are msgssz bytes
120 * long. For efficiency reasons, this should be a power of two. Also,
121 * it doesn't make sense if it is less than 8 or greater than about 256.
122 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
123 * two between 8 and 1024 inclusive (and panic's if it isn't).
124 */
125 struct msginfo msginfo = {
126 MSGMAX, /* max chars in a message */
127 MSGMNI, /* # of message queue identifiers */
128 MSGMNB, /* max chars in a queue */
129 MSGTQL, /* max messages in system */
130 MSGSSZ, /* size of a message segment */
131 /* (must be small power of 2 greater than 4) */
132 MSGSEG /* number of message segments */
133 };
134
135 /*
136 * macros to convert between msqid_ds's and msqid's.
137 * (specific to this implementation)
138 */
139 #define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
140 #define MSQID_IX(id) ((id) & 0xffff)
141 #define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
142
143 /*
144 * The rest of this file is specific to this particular implementation.
145 */
146
147 struct msgmap {
148 short next; /* next segment in buffer */
149 /* -1 -> available */
150 /* 0..(MSGSEG-1) -> index of next segment */
151 };
152
153 #define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
154
155 static int nfree_msgmaps; /* # of free map entries */
156 static short free_msgmaps; /* head of linked list of free map entries */
157 static struct msg *free_msghdrs;/* list of free msg headers */
158 static char *msgpool; /* MSGMAX byte long msg buffer pool */
159 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
160 static struct msg *msghdrs; /* MSGTQL msg headers */
161 static struct msqid_kernel *msqids; /* MSGMNI msqid_kernel struct's */
162 static struct mtx msq_mtx; /* global mutex for message queues. */
163
164 static void
165 msginit()
166 {
167 register int i;
168
169 TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
170 TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
171 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
172 TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
173 TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
174 TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
175
176 msgpool = malloc(msginfo.msgmax, M_MSG, M_WAITOK);
177 if (msgpool == NULL)
178 panic("msgpool is NULL");
179 msgmaps = malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
180 if (msgmaps == NULL)
181 panic("msgmaps is NULL");
182 msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
183 if (msghdrs == NULL)
184 panic("msghdrs is NULL");
185 msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG,
186 M_WAITOK);
187 if (msqids == NULL)
188 panic("msqids is NULL");
189
190 /*
191 * msginfo.msgssz should be a power of two for efficiency reasons.
192 * It is also pretty silly if msginfo.msgssz is less than 8
193 * or greater than about 256 so ...
194 */
195
196 i = 8;
197 while (i < 1024 && i != msginfo.msgssz)
198 i <<= 1;
199 if (i != msginfo.msgssz) {
200 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
201 msginfo.msgssz));
202 panic("msginfo.msgssz not a small power of 2");
203 }
204
205 if (msginfo.msgseg > 32767) {
206 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
207 panic("msginfo.msgseg > 32767");
208 }
209
210 if (msgmaps == NULL)
211 panic("msgmaps is NULL");
212
213 for (i = 0; i < msginfo.msgseg; i++) {
214 if (i > 0)
215 msgmaps[i-1].next = i;
216 msgmaps[i].next = -1; /* implies entry is available */
217 }
218 free_msgmaps = 0;
219 nfree_msgmaps = msginfo.msgseg;
220
221 if (msghdrs == NULL)
222 panic("msghdrs is NULL");
223
224 for (i = 0; i < msginfo.msgtql; i++) {
225 msghdrs[i].msg_type = 0;
226 if (i > 0)
227 msghdrs[i-1].msg_next = &msghdrs[i];
228 msghdrs[i].msg_next = NULL;
229 #ifdef MAC
230 mac_init_sysv_msgmsg(&msghdrs[i]);
231 #endif
232 }
233 free_msghdrs = &msghdrs[0];
234
235 if (msqids == NULL)
236 panic("msqids is NULL");
237
238 for (i = 0; i < msginfo.msgmni; i++) {
239 msqids[i].u.msg_qbytes = 0; /* implies entry is available */
240 msqids[i].u.msg_perm.seq = 0; /* reset to a known value */
241 msqids[i].u.msg_perm.mode = 0;
242 #ifdef MAC
243 mac_init_sysv_msgqueue(&msqids[i]);
244 #endif
245 }
246 mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
247 }
248
249 static int
250 msgunload()
251 {
252 struct msqid_kernel *msqkptr;
253 int msqid;
254 #ifdef MAC
255 int i;
256 #endif
257
258 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
259 /*
260 * Look for an unallocated and unlocked msqid_ds.
261 * msqid_ds's can be locked by msgsnd or msgrcv while
262 * they are copying the message in/out. We can't
263 * re-use the entry until they release it.
264 */
265 msqkptr = &msqids[msqid];
266 if (msqkptr->u.msg_qbytes != 0 ||
267 (msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
268 break;
269 }
270 if (msqid != msginfo.msgmni)
271 return (EBUSY);
272
273 #ifdef MAC
274 for (i = 0; i < msginfo.msgtql; i++)
275 mac_destroy_sysv_msgmsg(&msghdrs[i]);
276 for (msqid = 0; msqid < msginfo.msgmni; msqid++)
277 mac_destroy_sysv_msgqueue(&msqids[msqid]);
278 #endif
279 free(msgpool, M_MSG);
280 free(msgmaps, M_MSG);
281 free(msghdrs, M_MSG);
282 free(msqids, M_MSG);
283 mtx_destroy(&msq_mtx);
284 return (0);
285 }
286
287
288 static int
289 sysvmsg_modload(struct module *module, int cmd, void *arg)
290 {
291 int error = 0;
292
293 switch (cmd) {
294 case MOD_LOAD:
295 msginit();
296 break;
297 case MOD_UNLOAD:
298 error = msgunload();
299 break;
300 case MOD_SHUTDOWN:
301 break;
302 default:
303 error = EINVAL;
304 break;
305 }
306 return (error);
307 }
308
309 static moduledata_t sysvmsg_mod = {
310 "sysvmsg",
311 &sysvmsg_modload,
312 NULL
313 };
314
315 SYSCALL_MODULE_HELPER(msgsys);
316 SYSCALL_MODULE_HELPER(msgctl);
317 SYSCALL_MODULE_HELPER(msgget);
318 SYSCALL_MODULE_HELPER(msgsnd);
319 SYSCALL_MODULE_HELPER(msgrcv);
320
321 DECLARE_MODULE(sysvmsg, sysvmsg_mod,
322 SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
323 MODULE_VERSION(sysvmsg, 1);
324
325 /*
326 * Entry point for all MSG calls
327 *
328 * MPSAFE
329 */
330 int
331 msgsys(td, uap)
332 struct thread *td;
333 /* XXX actually varargs. */
334 struct msgsys_args /* {
335 int which;
336 int a2;
337 int a3;
338 int a4;
339 int a5;
340 int a6;
341 } */ *uap;
342 {
343 int error;
344
345 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
346 return (ENOSYS);
347 if (uap->which < 0 ||
348 uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
349 return (EINVAL);
350 error = (*msgcalls[uap->which])(td, &uap->a2);
351 return (error);
352 }
353
354 static void
355 msg_freehdr(msghdr)
356 struct msg *msghdr;
357 {
358 while (msghdr->msg_ts > 0) {
359 short next;
360 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
361 panic("msghdr->msg_spot out of range");
362 next = msgmaps[msghdr->msg_spot].next;
363 msgmaps[msghdr->msg_spot].next = free_msgmaps;
364 free_msgmaps = msghdr->msg_spot;
365 nfree_msgmaps++;
366 msghdr->msg_spot = next;
367 if (msghdr->msg_ts >= msginfo.msgssz)
368 msghdr->msg_ts -= msginfo.msgssz;
369 else
370 msghdr->msg_ts = 0;
371 }
372 if (msghdr->msg_spot != -1)
373 panic("msghdr->msg_spot != -1");
374 msghdr->msg_next = free_msghdrs;
375 free_msghdrs = msghdr;
376 #ifdef MAC
377 mac_cleanup_sysv_msgmsg(msghdr);
378 #endif
379 }
380
381 #ifndef _SYS_SYSPROTO_H_
382 struct msgctl_args {
383 int msqid;
384 int cmd;
385 struct msqid_ds *buf;
386 };
387 #endif
388
389 /*
390 * MPSAFE
391 */
392 int
393 msgctl(td, uap)
394 struct thread *td;
395 register struct msgctl_args *uap;
396 {
397 int msqid = uap->msqid;
398 int cmd = uap->cmd;
399 struct msqid_ds msqbuf;
400 int error;
401
402 DPRINTF(("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, uap->buf));
403 if (cmd == IPC_SET &&
404 (error = copyin(uap->buf, &msqbuf, sizeof(msqbuf))) != 0)
405 return (error);
406 error = kern_msgctl(td, msqid, cmd, &msqbuf);
407 if (cmd == IPC_STAT && error == 0)
408 error = copyout(&msqbuf, uap->buf, sizeof(struct msqid_ds));
409 return (error);
410 }
411
412 int
413 kern_msgctl(td, msqid, cmd, msqbuf)
414 struct thread *td;
415 int msqid;
416 int cmd;
417 struct msqid_ds *msqbuf;
418 {
419 int rval, error, msqix;
420 register struct msqid_kernel *msqkptr;
421
422 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
423 return (ENOSYS);
424
425 msqix = IPCID_TO_IX(msqid);
426
427 if (msqix < 0 || msqix >= msginfo.msgmni) {
428 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqix,
429 msginfo.msgmni));
430 return (EINVAL);
431 }
432
433 msqkptr = &msqids[msqix];
434
435 mtx_lock(&msq_mtx);
436 if (msqkptr->u.msg_qbytes == 0) {
437 DPRINTF(("no such msqid\n"));
438 error = EINVAL;
439 goto done2;
440 }
441 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) {
442 DPRINTF(("wrong sequence number\n"));
443 error = EINVAL;
444 goto done2;
445 }
446 #ifdef MAC
447 error = mac_check_sysv_msqctl(td->td_ucred, msqkptr, cmd);
448 if (error != 0) {
449 MPRINTF(("mac_check_sysv_msqctl returned %d\n", error));
450 goto done2;
451 }
452 #endif
453
454 error = 0;
455 rval = 0;
456
457 switch (cmd) {
458
459 case IPC_RMID:
460 {
461 struct msg *msghdr;
462 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
463 goto done2;
464
465 #ifdef MAC
466 /*
467 * Check that the thread has MAC access permissions to
468 * individual msghdrs. Note: We need to do this in a
469 * separate loop because the actual loop alters the
470 * msq/msghdr info as it progresses, and there is no going
471 * back if half the way through we discover that the
472 * thread cannot free a certain msghdr. The msq will get
473 * into an inconsistent state.
474 */
475 for (msghdr = msqkptr->u.msg_first; msghdr != NULL;
476 msghdr = msghdr->msg_next) {
477 error = mac_check_sysv_msgrmid(td->td_ucred, msghdr);
478 if (error != 0) {
479 MPRINTF(("mac_check_sysv_msgrmid returned %d\n",
480 error));
481 goto done2;
482 }
483 }
484 #endif
485
486 /* Free the message headers */
487 msghdr = msqkptr->u.msg_first;
488 while (msghdr != NULL) {
489 struct msg *msghdr_tmp;
490
491 /* Free the segments of each message */
492 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
493 msqkptr->u.msg_qnum--;
494 msghdr_tmp = msghdr;
495 msghdr = msghdr->msg_next;
496 msg_freehdr(msghdr_tmp);
497 }
498
499 if (msqkptr->u.msg_cbytes != 0)
500 panic("msg_cbytes is screwed up");
501 if (msqkptr->u.msg_qnum != 0)
502 panic("msg_qnum is screwed up");
503
504 msqkptr->u.msg_qbytes = 0; /* Mark it as free */
505
506 #ifdef MAC
507 mac_cleanup_sysv_msgqueue(msqkptr);
508 #endif
509
510 wakeup(msqkptr);
511 }
512
513 break;
514
515 case IPC_SET:
516 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_M)))
517 goto done2;
518 if (msqbuf->msg_qbytes > msqkptr->u.msg_qbytes) {
519 error = suser(td);
520 if (error)
521 goto done2;
522 }
523 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
524 DPRINTF(("can't increase msg_qbytes beyond %d"
525 "(truncating)\n", msginfo.msgmnb));
526 msqbuf->msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
527 }
528 if (msqbuf->msg_qbytes == 0) {
529 DPRINTF(("can't reduce msg_qbytes to 0\n"));
530 error = EINVAL; /* non-standard errno! */
531 goto done2;
532 }
533 msqkptr->u.msg_perm.uid = msqbuf->msg_perm.uid; /* change the owner */
534 msqkptr->u.msg_perm.gid = msqbuf->msg_perm.gid; /* change the owner */
535 msqkptr->u.msg_perm.mode = (msqkptr->u.msg_perm.mode & ~0777) |
536 (msqbuf->msg_perm.mode & 0777);
537 msqkptr->u.msg_qbytes = msqbuf->msg_qbytes;
538 msqkptr->u.msg_ctime = time_second;
539 break;
540
541 case IPC_STAT:
542 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
543 DPRINTF(("requester doesn't have read access\n"));
544 goto done2;
545 }
546 *msqbuf = msqkptr->u;
547 break;
548
549 default:
550 DPRINTF(("invalid command %d\n", cmd));
551 error = EINVAL;
552 goto done2;
553 }
554
555 if (error == 0)
556 td->td_retval[0] = rval;
557 done2:
558 mtx_unlock(&msq_mtx);
559 return (error);
560 }
561
562 #ifndef _SYS_SYSPROTO_H_
563 struct msgget_args {
564 key_t key;
565 int msgflg;
566 };
567 #endif
568
569 /*
570 * MPSAFE
571 */
572 int
573 msgget(td, uap)
574 struct thread *td;
575 register struct msgget_args *uap;
576 {
577 int msqid, error = 0;
578 int key = uap->key;
579 int msgflg = uap->msgflg;
580 struct ucred *cred = td->td_ucred;
581 register struct msqid_kernel *msqkptr = NULL;
582
583 DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
584
585 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
586 return (ENOSYS);
587
588 mtx_lock(&msq_mtx);
589 if (key != IPC_PRIVATE) {
590 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
591 msqkptr = &msqids[msqid];
592 if (msqkptr->u.msg_qbytes != 0 &&
593 msqkptr->u.msg_perm.key == key)
594 break;
595 }
596 if (msqid < msginfo.msgmni) {
597 DPRINTF(("found public key\n"));
598 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
599 DPRINTF(("not exclusive\n"));
600 error = EEXIST;
601 goto done2;
602 }
603 if ((error = ipcperm(td, &msqkptr->u.msg_perm,
604 msgflg & 0700))) {
605 DPRINTF(("requester doesn't have 0%o access\n",
606 msgflg & 0700));
607 goto done2;
608 }
609 #ifdef MAC
610 error = mac_check_sysv_msqget(cred, msqkptr);
611 if (error != 0) {
612 MPRINTF(("mac_check_sysv_msqget returned %d\n",
613 error));
614 goto done2;
615 }
616 #endif
617 goto found;
618 }
619 }
620
621 DPRINTF(("need to allocate the msqid_ds\n"));
622 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
623 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
624 /*
625 * Look for an unallocated and unlocked msqid_ds.
626 * msqid_ds's can be locked by msgsnd or msgrcv while
627 * they are copying the message in/out. We can't
628 * re-use the entry until they release it.
629 */
630 msqkptr = &msqids[msqid];
631 if (msqkptr->u.msg_qbytes == 0 &&
632 (msqkptr->u.msg_perm.mode & MSG_LOCKED) == 0)
633 break;
634 }
635 if (msqid == msginfo.msgmni) {
636 DPRINTF(("no more msqid_ds's available\n"));
637 error = ENOSPC;
638 goto done2;
639 }
640 DPRINTF(("msqid %d is available\n", msqid));
641 msqkptr->u.msg_perm.key = key;
642 msqkptr->u.msg_perm.cuid = cred->cr_uid;
643 msqkptr->u.msg_perm.uid = cred->cr_uid;
644 msqkptr->u.msg_perm.cgid = cred->cr_gid;
645 msqkptr->u.msg_perm.gid = cred->cr_gid;
646 msqkptr->u.msg_perm.mode = (msgflg & 0777);
647 /* Make sure that the returned msqid is unique */
648 msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff;
649 msqkptr->u.msg_first = NULL;
650 msqkptr->u.msg_last = NULL;
651 msqkptr->u.msg_cbytes = 0;
652 msqkptr->u.msg_qnum = 0;
653 msqkptr->u.msg_qbytes = msginfo.msgmnb;
654 msqkptr->u.msg_lspid = 0;
655 msqkptr->u.msg_lrpid = 0;
656 msqkptr->u.msg_stime = 0;
657 msqkptr->u.msg_rtime = 0;
658 msqkptr->u.msg_ctime = time_second;
659 #ifdef MAC
660 mac_create_sysv_msgqueue(cred, msqkptr);
661 #endif
662 } else {
663 DPRINTF(("didn't find it and wasn't asked to create it\n"));
664 error = ENOENT;
665 goto done2;
666 }
667
668 found:
669 /* Construct the unique msqid */
670 td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqkptr->u.msg_perm);
671 done2:
672 mtx_unlock(&msq_mtx);
673 return (error);
674 }
675
676 #ifndef _SYS_SYSPROTO_H_
677 struct msgsnd_args {
678 int msqid;
679 const void *msgp;
680 size_t msgsz;
681 int msgflg;
682 };
683 #endif
684
685 /*
686 * MPSAFE
687 */
688 int
689 msgsnd(td, uap)
690 struct thread *td;
691 register struct msgsnd_args *uap;
692 {
693 int msqid = uap->msqid;
694 const void *user_msgp = uap->msgp;
695 size_t msgsz = uap->msgsz;
696 int msgflg = uap->msgflg;
697 int segs_needed, error = 0;
698 register struct msqid_kernel *msqkptr;
699 register struct msg *msghdr;
700 short next;
701
702 DPRINTF(("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
703 msgflg));
704 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
705 return (ENOSYS);
706
707 mtx_lock(&msq_mtx);
708 msqid = IPCID_TO_IX(msqid);
709
710 if (msqid < 0 || msqid >= msginfo.msgmni) {
711 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
712 msginfo.msgmni));
713 error = EINVAL;
714 goto done2;
715 }
716
717 msqkptr = &msqids[msqid];
718 if (msqkptr->u.msg_qbytes == 0) {
719 DPRINTF(("no such message queue id\n"));
720 error = EINVAL;
721 goto done2;
722 }
723 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
724 DPRINTF(("wrong sequence number\n"));
725 error = EINVAL;
726 goto done2;
727 }
728
729 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) {
730 DPRINTF(("requester doesn't have write access\n"));
731 goto done2;
732 }
733
734 #ifdef MAC
735 error = mac_check_sysv_msqsnd(td->td_ucred, msqkptr);
736 if (error != 0) {
737 MPRINTF(("mac_check_sysv_msqsnd returned %d\n", error));
738 goto done2;
739 }
740 #endif
741
742 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
743 DPRINTF(("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
744 segs_needed));
745 for (;;) {
746 int need_more_resources = 0;
747
748 /*
749 * check msgsz
750 * (inside this loop in case msg_qbytes changes while we sleep)
751 */
752
753 if (msgsz > msqkptr->u.msg_qbytes) {
754 DPRINTF(("msgsz > msqkptr->u.msg_qbytes\n"));
755 error = EINVAL;
756 goto done2;
757 }
758
759 if (msqkptr->u.msg_perm.mode & MSG_LOCKED) {
760 DPRINTF(("msqid is locked\n"));
761 need_more_resources = 1;
762 }
763 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) {
764 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
765 need_more_resources = 1;
766 }
767 if (segs_needed > nfree_msgmaps) {
768 DPRINTF(("segs_needed > nfree_msgmaps\n"));
769 need_more_resources = 1;
770 }
771 if (free_msghdrs == NULL) {
772 DPRINTF(("no more msghdrs\n"));
773 need_more_resources = 1;
774 }
775
776 if (need_more_resources) {
777 int we_own_it;
778
779 if ((msgflg & IPC_NOWAIT) != 0) {
780 DPRINTF(("need more resources but caller "
781 "doesn't want to wait\n"));
782 error = EAGAIN;
783 goto done2;
784 }
785
786 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) {
787 DPRINTF(("we don't own the msqid_ds\n"));
788 we_own_it = 0;
789 } else {
790 /* Force later arrivals to wait for our
791 request */
792 DPRINTF(("we own the msqid_ds\n"));
793 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
794 we_own_it = 1;
795 }
796 DPRINTF(("goodnight\n"));
797 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
798 "msgwait", 0);
799 DPRINTF(("good morning, error=%d\n", error));
800 if (we_own_it)
801 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
802 if (error != 0) {
803 DPRINTF(("msgsnd: interrupted system call\n"));
804 error = EINTR;
805 goto done2;
806 }
807
808 /*
809 * Make sure that the msq queue still exists
810 */
811
812 if (msqkptr->u.msg_qbytes == 0) {
813 DPRINTF(("msqid deleted\n"));
814 error = EIDRM;
815 goto done2;
816 }
817
818 } else {
819 DPRINTF(("got all the resources that we need\n"));
820 break;
821 }
822 }
823
824 /*
825 * We have the resources that we need.
826 * Make sure!
827 */
828
829 if (msqkptr->u.msg_perm.mode & MSG_LOCKED)
830 panic("msg_perm.mode & MSG_LOCKED");
831 if (segs_needed > nfree_msgmaps)
832 panic("segs_needed > nfree_msgmaps");
833 if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes)
834 panic("msgsz + msg_cbytes > msg_qbytes");
835 if (free_msghdrs == NULL)
836 panic("no more msghdrs");
837
838 /*
839 * Re-lock the msqid_ds in case we page-fault when copying in the
840 * message
841 */
842
843 if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0)
844 panic("msqid_ds is already locked");
845 msqkptr->u.msg_perm.mode |= MSG_LOCKED;
846
847 /*
848 * Allocate a message header
849 */
850
851 msghdr = free_msghdrs;
852 free_msghdrs = msghdr->msg_next;
853 msghdr->msg_spot = -1;
854 msghdr->msg_ts = msgsz;
855 #ifdef MAC
856 /*
857 * XXXMAC: Should the mac_check_sysv_msgmsq check follow here
858 * immediately? Or, should it be checked just before the msg is
859 * enqueued in the msgq (as it is done now)?
860 */
861 mac_create_sysv_msgmsg(td->td_ucred, msqkptr, msghdr);
862 #endif
863
864 /*
865 * Allocate space for the message
866 */
867
868 while (segs_needed > 0) {
869 if (nfree_msgmaps <= 0)
870 panic("not enough msgmaps");
871 if (free_msgmaps == -1)
872 panic("nil free_msgmaps");
873 next = free_msgmaps;
874 if (next <= -1)
875 panic("next too low #1");
876 if (next >= msginfo.msgseg)
877 panic("next out of range #1");
878 DPRINTF(("allocating segment %d to message\n", next));
879 free_msgmaps = msgmaps[next].next;
880 nfree_msgmaps--;
881 msgmaps[next].next = msghdr->msg_spot;
882 msghdr->msg_spot = next;
883 segs_needed--;
884 }
885
886 /*
887 * Copy in the message type
888 */
889
890 mtx_unlock(&msq_mtx);
891 if ((error = copyin(user_msgp, &msghdr->msg_type,
892 sizeof(msghdr->msg_type))) != 0) {
893 mtx_lock(&msq_mtx);
894 DPRINTF(("error %d copying the message type\n", error));
895 msg_freehdr(msghdr);
896 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
897 wakeup(msqkptr);
898 goto done2;
899 }
900 mtx_lock(&msq_mtx);
901 user_msgp = (const char *)user_msgp + sizeof(msghdr->msg_type);
902
903 /*
904 * Validate the message type
905 */
906
907 if (msghdr->msg_type < 1) {
908 msg_freehdr(msghdr);
909 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
910 wakeup(msqkptr);
911 DPRINTF(("mtype (%d) < 1\n", msghdr->msg_type));
912 error = EINVAL;
913 goto done2;
914 }
915
916 /*
917 * Copy in the message body
918 */
919
920 next = msghdr->msg_spot;
921 while (msgsz > 0) {
922 size_t tlen;
923 if (msgsz > msginfo.msgssz)
924 tlen = msginfo.msgssz;
925 else
926 tlen = msgsz;
927 if (next <= -1)
928 panic("next too low #2");
929 if (next >= msginfo.msgseg)
930 panic("next out of range #2");
931 mtx_unlock(&msq_mtx);
932 if ((error = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
933 tlen)) != 0) {
934 mtx_lock(&msq_mtx);
935 DPRINTF(("error %d copying in message segment\n",
936 error));
937 msg_freehdr(msghdr);
938 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
939 wakeup(msqkptr);
940 goto done2;
941 }
942 mtx_lock(&msq_mtx);
943 msgsz -= tlen;
944 user_msgp = (const char *)user_msgp + tlen;
945 next = msgmaps[next].next;
946 }
947 if (next != -1)
948 panic("didn't use all the msg segments");
949
950 /*
951 * We've got the message. Unlock the msqid_ds.
952 */
953
954 msqkptr->u.msg_perm.mode &= ~MSG_LOCKED;
955
956 /*
957 * Make sure that the msqid_ds is still allocated.
958 */
959
960 if (msqkptr->u.msg_qbytes == 0) {
961 msg_freehdr(msghdr);
962 wakeup(msqkptr);
963 error = EIDRM;
964 goto done2;
965 }
966
967 #ifdef MAC
968 /*
969 * Note: Since the task/thread allocates the msghdr and usually
970 * primes it with its own MAC label, for a majority of policies, it
971 * won't be necessary to check whether the msghdr has access
972 * permissions to the msgq. The mac_check_sysv_msqsnd check would
973 * suffice in that case. However, this hook may be required where
974 * individual policies derive a non-identical label for the msghdr
975 * from the current thread label and may want to check the msghdr
976 * enqueue permissions, along with read/write permissions to the
977 * msgq.
978 */
979 error = mac_check_sysv_msgmsq(td->td_ucred, msghdr, msqkptr);
980 if (error != 0) {
981 MPRINTF(("mac_check_sysv_msqmsq returned %d\n", error));
982 msg_freehdr(msghdr);
983 wakeup(msqkptr);
984 goto done2;
985 }
986 #endif
987
988 /*
989 * Put the message into the queue
990 */
991 if (msqkptr->u.msg_first == NULL) {
992 msqkptr->u.msg_first = msghdr;
993 msqkptr->u.msg_last = msghdr;
994 } else {
995 msqkptr->u.msg_last->msg_next = msghdr;
996 msqkptr->u.msg_last = msghdr;
997 }
998 msqkptr->u.msg_last->msg_next = NULL;
999
1000 msqkptr->u.msg_cbytes += msghdr->msg_ts;
1001 msqkptr->u.msg_qnum++;
1002 msqkptr->u.msg_lspid = td->td_proc->p_pid;
1003 msqkptr->u.msg_stime = time_second;
1004
1005 wakeup(msqkptr);
1006 td->td_retval[0] = 0;
1007 done2:
1008 mtx_unlock(&msq_mtx);
1009 return (error);
1010 }
1011
1012 #ifndef _SYS_SYSPROTO_H_
1013 struct msgrcv_args {
1014 int msqid;
1015 void *msgp;
1016 size_t msgsz;
1017 long msgtyp;
1018 int msgflg;
1019 };
1020 #endif
1021
1022 /*
1023 * MPSAFE
1024 */
1025 int
1026 msgrcv(td, uap)
1027 struct thread *td;
1028 register struct msgrcv_args *uap;
1029 {
1030 int msqid = uap->msqid;
1031 void *user_msgp = uap->msgp;
1032 size_t msgsz = uap->msgsz;
1033 long msgtyp = uap->msgtyp;
1034 int msgflg = uap->msgflg;
1035 size_t len;
1036 register struct msqid_kernel *msqkptr;
1037 register struct msg *msghdr;
1038 int error = 0;
1039 short next;
1040
1041 DPRINTF(("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
1042 msgsz, msgtyp, msgflg));
1043
1044 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
1045 return (ENOSYS);
1046
1047 msqid = IPCID_TO_IX(msqid);
1048
1049 if (msqid < 0 || msqid >= msginfo.msgmni) {
1050 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
1051 msginfo.msgmni));
1052 return (EINVAL);
1053 }
1054
1055 msqkptr = &msqids[msqid];
1056 mtx_lock(&msq_mtx);
1057 if (msqkptr->u.msg_qbytes == 0) {
1058 DPRINTF(("no such message queue id\n"));
1059 error = EINVAL;
1060 goto done2;
1061 }
1062 if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1063 DPRINTF(("wrong sequence number\n"));
1064 error = EINVAL;
1065 goto done2;
1066 }
1067
1068 if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) {
1069 DPRINTF(("requester doesn't have read access\n"));
1070 goto done2;
1071 }
1072
1073 #ifdef MAC
1074 error = mac_check_sysv_msqrcv(td->td_ucred, msqkptr);
1075 if (error != 0) {
1076 MPRINTF(("mac_check_sysv_msqrcv returned %d\n", error));
1077 goto done2;
1078 }
1079 #endif
1080
1081 msghdr = NULL;
1082 while (msghdr == NULL) {
1083 if (msgtyp == 0) {
1084 msghdr = msqkptr->u.msg_first;
1085 if (msghdr != NULL) {
1086 if (msgsz < msghdr->msg_ts &&
1087 (msgflg & MSG_NOERROR) == 0) {
1088 DPRINTF(("first message on the queue "
1089 "is too big (want %d, got %d)\n",
1090 msgsz, msghdr->msg_ts));
1091 error = E2BIG;
1092 goto done2;
1093 }
1094 #ifdef MAC
1095 error = mac_check_sysv_msgrcv(td->td_ucred,
1096 msghdr);
1097 if (error != 0) {
1098 MPRINTF(("mac_check_sysv_msgrcv "
1099 "returned %d\n", error));
1100 goto done2;
1101 }
1102 #endif
1103 if (msqkptr->u.msg_first == msqkptr->u.msg_last) {
1104 msqkptr->u.msg_first = NULL;
1105 msqkptr->u.msg_last = NULL;
1106 } else {
1107 msqkptr->u.msg_first = msghdr->msg_next;
1108 if (msqkptr->u.msg_first == NULL)
1109 panic("msg_first/last screwed up #1");
1110 }
1111 }
1112 } else {
1113 struct msg *previous;
1114 struct msg **prev;
1115
1116 previous = NULL;
1117 prev = &(msqkptr->u.msg_first);
1118 while ((msghdr = *prev) != NULL) {
1119 /*
1120 * Is this message's type an exact match or is
1121 * this message's type less than or equal to
1122 * the absolute value of a negative msgtyp?
1123 * Note that the second half of this test can
1124 * NEVER be true if msgtyp is positive since
1125 * msg_type is always positive!
1126 */
1127
1128 if (msgtyp == msghdr->msg_type ||
1129 msghdr->msg_type <= -msgtyp) {
1130 DPRINTF(("found message type %d, "
1131 "requested %d\n",
1132 msghdr->msg_type, msgtyp));
1133 if (msgsz < msghdr->msg_ts &&
1134 (msgflg & MSG_NOERROR) == 0) {
1135 DPRINTF(("requested message "
1136 "on the queue is too big "
1137 "(want %d, got %d)\n",
1138 msgsz, msghdr->msg_ts));
1139 error = E2BIG;
1140 goto done2;
1141 }
1142 #ifdef MAC
1143 error = mac_check_sysv_msgrcv(
1144 td->td_ucred, msghdr);
1145 if (error != 0) {
1146 MPRINTF(("mac_check_sysv_"
1147 "msgrcv returned %d\n",
1148 error));
1149 goto done2;
1150 }
1151 #endif
1152 *prev = msghdr->msg_next;
1153 if (msghdr == msqkptr->u.msg_last) {
1154 if (previous == NULL) {
1155 if (prev !=
1156 &msqkptr->u.msg_first)
1157 panic("msg_first/last screwed up #2");
1158 msqkptr->u.msg_first =
1159 NULL;
1160 msqkptr->u.msg_last =
1161 NULL;
1162 } else {
1163 if (prev ==
1164 &msqkptr->u.msg_first)
1165 panic("msg_first/last screwed up #3");
1166 msqkptr->u.msg_last =
1167 previous;
1168 }
1169 }
1170 break;
1171 }
1172 previous = msghdr;
1173 prev = &(msghdr->msg_next);
1174 }
1175 }
1176
1177 /*
1178 * We've either extracted the msghdr for the appropriate
1179 * message or there isn't one.
1180 * If there is one then bail out of this loop.
1181 */
1182
1183 if (msghdr != NULL)
1184 break;
1185
1186 /*
1187 * Hmph! No message found. Does the user want to wait?
1188 */
1189
1190 if ((msgflg & IPC_NOWAIT) != 0) {
1191 DPRINTF(("no appropriate message found (msgtyp=%d)\n",
1192 msgtyp));
1193 /* The SVID says to return ENOMSG. */
1194 error = ENOMSG;
1195 goto done2;
1196 }
1197
1198 /*
1199 * Wait for something to happen
1200 */
1201
1202 DPRINTF(("msgrcv: goodnight\n"));
1203 error = msleep(msqkptr, &msq_mtx, (PZERO - 4) | PCATCH,
1204 "msgwait", 0);
1205 DPRINTF(("msgrcv: good morning (error=%d)\n", error));
1206
1207 if (error != 0) {
1208 DPRINTF(("msgsnd: interrupted system call\n"));
1209 error = EINTR;
1210 goto done2;
1211 }
1212
1213 /*
1214 * Make sure that the msq queue still exists
1215 */
1216
1217 if (msqkptr->u.msg_qbytes == 0 ||
1218 msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1219 DPRINTF(("msqid deleted\n"));
1220 error = EIDRM;
1221 goto done2;
1222 }
1223 }
1224
1225 /*
1226 * Return the message to the user.
1227 *
1228 * First, do the bookkeeping (before we risk being interrupted).
1229 */
1230
1231 msqkptr->u.msg_cbytes -= msghdr->msg_ts;
1232 msqkptr->u.msg_qnum--;
1233 msqkptr->u.msg_lrpid = td->td_proc->p_pid;
1234 msqkptr->u.msg_rtime = time_second;
1235
1236 /*
1237 * Make msgsz the actual amount that we'll be returning.
1238 * Note that this effectively truncates the message if it is too long
1239 * (since msgsz is never increased).
1240 */
1241
1242 DPRINTF(("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1243 msghdr->msg_ts));
1244 if (msgsz > msghdr->msg_ts)
1245 msgsz = msghdr->msg_ts;
1246
1247 /*
1248 * Return the type to the user.
1249 */
1250
1251 mtx_unlock(&msq_mtx);
1252 error = copyout(&(msghdr->msg_type), user_msgp,
1253 sizeof(msghdr->msg_type));
1254 mtx_lock(&msq_mtx);
1255 if (error != 0) {
1256 DPRINTF(("error (%d) copying out message type\n", error));
1257 msg_freehdr(msghdr);
1258 wakeup(msqkptr);
1259 goto done2;
1260 }
1261 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1262
1263 /*
1264 * Return the segments to the user
1265 */
1266
1267 next = msghdr->msg_spot;
1268 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1269 size_t tlen;
1270
1271 if (msgsz - len > msginfo.msgssz)
1272 tlen = msginfo.msgssz;
1273 else
1274 tlen = msgsz - len;
1275 if (next <= -1)
1276 panic("next too low #3");
1277 if (next >= msginfo.msgseg)
1278 panic("next out of range #3");
1279 mtx_unlock(&msq_mtx);
1280 error = copyout(&msgpool[next * msginfo.msgssz],
1281 user_msgp, tlen);
1282 mtx_lock(&msq_mtx);
1283 if (error != 0) {
1284 DPRINTF(("error (%d) copying out message segment\n",
1285 error));
1286 msg_freehdr(msghdr);
1287 wakeup(msqkptr);
1288 goto done2;
1289 }
1290 user_msgp = (char *)user_msgp + tlen;
1291 next = msgmaps[next].next;
1292 }
1293
1294 /*
1295 * Done, return the actual number of bytes copied out.
1296 */
1297
1298 msg_freehdr(msghdr);
1299 wakeup(msqkptr);
1300 td->td_retval[0] = msgsz;
1301 done2:
1302 mtx_unlock(&msq_mtx);
1303 return (error);
1304 }
1305
1306 static int
1307 sysctl_msqids(SYSCTL_HANDLER_ARGS)
1308 {
1309
1310 return (SYSCTL_OUT(req, msqids,
1311 sizeof(struct msqid_kernel) * msginfo.msgmni));
1312 }
1313
1314 SYSCTL_DECL(_kern_ipc);
1315 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0,
1316 "Maximum message size");
1317 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0,
1318 "Number of message queue identifiers");
1319 SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RDTUN, &msginfo.msgmnb, 0,
1320 "Maximum number of bytes in a queue");
1321 SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RDTUN, &msginfo.msgtql, 0,
1322 "Maximum number of messages in the system");
1323 SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0,
1324 "Size of a message segment");
1325 SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0,
1326 "Number of message segments");
1327 SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1328 NULL, 0, sysctl_msqids, "", "Message queue IDs");
Cache object: 34cc8221942adad4b5a65f0d1a7c97ea
|