sysv_msg.c revision 1.66.12.1 1 /* $NetBSD: sysv_msg.c,v 1.66.12.1 2019/02/23 07:04:12 martin Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Implementation of SVID messages
35 *
36 * Author: Daniel Boulet
37 *
38 * Copyright 1993 Daniel Boulet and RTMX Inc.
39 *
40 * This system call was implemented by Daniel Boulet under contract from RTMX.
41 *
42 * Redistribution and use in source forms, with and without modification,
43 * are permitted provided that this entire comment appears intact.
44 *
45 * Redistribution in binary form may occur without any restrictions.
46 * Obviously, it would be nice if you gave credit where credit is due
47 * but requiring it would be too onerous.
48 *
49 * This software is provided ``AS IS'' without any warranties of any kind.
50 */
51
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: sysv_msg.c,v 1.66.12.1 2019/02/23 07:04:12 martin Exp $");
54
55 #define SYSVMSG
56
57 #include <sys/param.h>
58 #include <sys/kernel.h>
59 #include <sys/msg.h>
60 #include <sys/sysctl.h>
61 #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
62 #include <sys/syscallargs.h>
63 #include <sys/kauth.h>
64
65 #define MSG_DEBUG
66 #undef MSG_DEBUG_OK
67
68 #ifdef MSG_DEBUG_OK
69 #define MSG_PRINTF(a) printf a
70 #else
71 #define MSG_PRINTF(a)
72 #endif
73
74 static int nfree_msgmaps; /* # of free map entries */
75 static short free_msgmaps; /* head of linked list of free map entries */
76 static struct __msg *free_msghdrs; /* list of free msg headers */
77 static char *msgpool; /* MSGMAX byte long msg buffer pool */
78 static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
79 static struct __msg *msghdrs; /* MSGTQL msg headers */
80
81 kmsq_t *msqs; /* MSGMNI msqid_ds struct's */
82 kmutex_t msgmutex; /* subsystem lock */
83
84 static u_int msg_waiters = 0; /* total number of msgrcv waiters */
85 static bool msg_realloc_state;
86 static kcondvar_t msg_realloc_cv;
87
88 static void msg_freehdr(struct __msg *);
89
90 void
91 msginit(void)
92 {
93 int i, sz;
94 vaddr_t v;
95
96 /*
97 * msginfo.msgssz should be a power of two for efficiency reasons.
98 * It is also pretty silly if msginfo.msgssz is less than 8
99 * or greater than about 256 so ...
100 */
101
102 i = 8;
103 while (i < 1024 && i != msginfo.msgssz)
104 i <<= 1;
105 if (i != msginfo.msgssz) {
106 panic("msginfo.msgssz = %d, not a small power of 2",
107 msginfo.msgssz);
108 }
109
110 if (msginfo.msgseg > 32767) {
111 panic("msginfo.msgseg = %d > 32767", msginfo.msgseg);
112 }
113
114 /* Allocate the wired memory for our structures */
115 sz = ALIGN(msginfo.msgmax) +
116 ALIGN(msginfo.msgseg * sizeof(struct msgmap)) +
117 ALIGN(msginfo.msgtql * sizeof(struct __msg)) +
118 ALIGN(msginfo.msgmni * sizeof(kmsq_t));
119 sz = round_page(sz);
120 v = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
121 if (v == 0)
122 panic("sysv_msg: cannot allocate memory");
123 msgpool = (void *)v;
124 msgmaps = (void *)((uintptr_t)msgpool + ALIGN(msginfo.msgmax));
125 msghdrs = (void *)((uintptr_t)msgmaps +
126 ALIGN(msginfo.msgseg * sizeof(struct msgmap)));
127 msqs = (void *)((uintptr_t)msghdrs +
128 ALIGN(msginfo.msgtql * sizeof(struct __msg)));
129
130 for (i = 0; i < (msginfo.msgseg - 1); i++)
131 msgmaps[i].next = i + 1;
132 msgmaps[msginfo.msgseg - 1].next = -1;
133
134 free_msgmaps = 0;
135 nfree_msgmaps = msginfo.msgseg;
136
137 for (i = 0; i < (msginfo.msgtql - 1); i++) {
138 msghdrs[i].msg_type = 0;
139 msghdrs[i].msg_next = &msghdrs[i + 1];
140 }
141 i = msginfo.msgtql - 1;
142 msghdrs[i].msg_type = 0;
143 msghdrs[i].msg_next = NULL;
144 free_msghdrs = &msghdrs[0];
145
146 for (i = 0; i < msginfo.msgmni; i++) {
147 cv_init(&msqs[i].msq_cv, "msgwait");
148 /* Implies entry is available */
149 msqs[i].msq_u.msg_qbytes = 0;
150 /* Reset to a known value */
151 msqs[i].msq_u.msg_perm._seq = 0;
152 }
153
154 mutex_init(&msgmutex, MUTEX_DEFAULT, IPL_NONE);
155 cv_init(&msg_realloc_cv, "msgrealc");
156 msg_realloc_state = false;
157
158 sysvipcinit();
159 }
160
161 static int
162 msgrealloc(int newmsgmni, int newmsgseg)
163 {
164 struct msgmap *new_msgmaps;
165 struct __msg *new_msghdrs, *new_free_msghdrs;
166 char *old_msgpool, *new_msgpool;
167 kmsq_t *new_msqs;
168 vaddr_t v;
169 int i, sz, msqid, newmsgmax, new_nfree_msgmaps;
170 short new_free_msgmaps;
171
172 if (newmsgmni < 1 || newmsgseg < 1)
173 return EINVAL;
174
175 /* Allocate the wired memory for our structures */
176 newmsgmax = msginfo.msgssz * newmsgseg;
177 sz = ALIGN(newmsgmax) +
178 ALIGN(newmsgseg * sizeof(struct msgmap)) +
179 ALIGN(msginfo.msgtql * sizeof(struct __msg)) +
180 ALIGN(newmsgmni * sizeof(kmsq_t));
181 sz = round_page(sz);
182 v = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
183 if (v == 0)
184 return ENOMEM;
185
186 mutex_enter(&msgmutex);
187 if (msg_realloc_state) {
188 mutex_exit(&msgmutex);
189 uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
190 return EBUSY;
191 }
192 msg_realloc_state = true;
193 if (msg_waiters) {
194 /*
195 * Mark reallocation state, wake-up all waiters,
196 * and wait while they will all exit.
197 */
198 for (i = 0; i < msginfo.msgmni; i++)
199 cv_broadcast(&msqs[i].msq_cv);
200 while (msg_waiters)
201 cv_wait(&msg_realloc_cv, &msgmutex);
202 }
203 old_msgpool = msgpool;
204
205 /* We cannot reallocate less memory than we use */
206 i = 0;
207 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
208 struct msqid_ds *mptr;
209 kmsq_t *msq;
210
211 msq = &msqs[msqid];
212 mptr = &msq->msq_u;
213 if (mptr->msg_qbytes || (mptr->msg_perm.mode & MSG_LOCKED))
214 i = msqid;
215 }
216 if (i >= newmsgmni || (msginfo.msgseg - nfree_msgmaps) > newmsgseg) {
217 mutex_exit(&msgmutex);
218 uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
219 return EBUSY;
220 }
221
222 new_msgpool = (void *)v;
223 new_msgmaps = (void *)((uintptr_t)new_msgpool + ALIGN(newmsgmax));
224 new_msghdrs = (void *)((uintptr_t)new_msgmaps +
225 ALIGN(newmsgseg * sizeof(struct msgmap)));
226 new_msqs = (void *)((uintptr_t)new_msghdrs +
227 ALIGN(msginfo.msgtql * sizeof(struct __msg)));
228
229 /* Initialize the structures */
230 for (i = 0; i < (newmsgseg - 1); i++)
231 new_msgmaps[i].next = i + 1;
232 new_msgmaps[newmsgseg - 1].next = -1;
233 new_free_msgmaps = 0;
234 new_nfree_msgmaps = newmsgseg;
235
236 for (i = 0; i < (msginfo.msgtql - 1); i++) {
237 new_msghdrs[i].msg_type = 0;
238 new_msghdrs[i].msg_next = &new_msghdrs[i + 1];
239 }
240 i = msginfo.msgtql - 1;
241 new_msghdrs[i].msg_type = 0;
242 new_msghdrs[i].msg_next = NULL;
243 new_free_msghdrs = &new_msghdrs[0];
244
245 for (i = 0; i < newmsgmni; i++) {
246 new_msqs[i].msq_u.msg_qbytes = 0;
247 new_msqs[i].msq_u.msg_perm._seq = 0;
248 cv_init(&new_msqs[i].msq_cv, "msgwait");
249 }
250
251 /*
252 * Copy all message queue identifiers, message headers and buffer
253 * pools to the new memory location.
254 */
255 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
256 struct __msg *nmsghdr, *msghdr, *pmsghdr;
257 struct msqid_ds *nmptr, *mptr;
258 kmsq_t *nmsq, *msq;
259
260 msq = &msqs[msqid];
261 mptr = &msq->msq_u;
262
263 if (mptr->msg_qbytes == 0 &&
264 (mptr->msg_perm.mode & MSG_LOCKED) == 0)
265 continue;
266
267 nmsq = &new_msqs[msqid];
268 nmptr = &nmsq->msq_u;
269 memcpy(nmptr, mptr, sizeof(struct msqid_ds));
270
271 /*
272 * Go through the message headers, and and copy each
273 * one by taking the new ones, and thus defragmenting.
274 */
275 nmsghdr = pmsghdr = NULL;
276 msghdr = mptr->_msg_first;
277 while (msghdr) {
278 short nnext = 0, next;
279 u_short msgsz, segcnt;
280
281 /* Take an entry from the new list of free msghdrs */
282 nmsghdr = new_free_msghdrs;
283 KASSERT(nmsghdr != NULL);
284 new_free_msghdrs = nmsghdr->msg_next;
285
286 nmsghdr->msg_next = NULL;
287 if (pmsghdr) {
288 pmsghdr->msg_next = nmsghdr;
289 } else {
290 nmptr->_msg_first = nmsghdr;
291 pmsghdr = nmsghdr;
292 }
293 nmsghdr->msg_ts = msghdr->msg_ts;
294 nmsghdr->msg_spot = -1;
295
296 /* Compute the amount of segments and reserve them */
297 msgsz = msghdr->msg_ts;
298 segcnt = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
299 if (segcnt == 0)
300 continue;
301 while (segcnt--) {
302 nnext = new_free_msgmaps;
303 new_free_msgmaps = new_msgmaps[nnext].next;
304 new_nfree_msgmaps--;
305 new_msgmaps[nnext].next = nmsghdr->msg_spot;
306 nmsghdr->msg_spot = nnext;
307 }
308
309 /* Copy all segments */
310 KASSERT(nnext == nmsghdr->msg_spot);
311 next = msghdr->msg_spot;
312 while (msgsz > 0) {
313 size_t tlen;
314
315 if (msgsz >= msginfo.msgssz) {
316 tlen = msginfo.msgssz;
317 msgsz -= msginfo.msgssz;
318 } else {
319 tlen = msgsz;
320 msgsz = 0;
321 }
322
323 /* Copy the message buffer */
324 memcpy(&new_msgpool[nnext * msginfo.msgssz],
325 &msgpool[next * msginfo.msgssz], tlen);
326
327 /* Next entry of the map */
328 nnext = msgmaps[nnext].next;
329 next = msgmaps[next].next;
330 }
331
332 /* Next message header */
333 msghdr = msghdr->msg_next;
334 }
335 nmptr->_msg_last = nmsghdr;
336 }
337 KASSERT((msginfo.msgseg - nfree_msgmaps) ==
338 (newmsgseg - new_nfree_msgmaps));
339
340 sz = ALIGN(msginfo.msgmax) +
341 ALIGN(msginfo.msgseg * sizeof(struct msgmap)) +
342 ALIGN(msginfo.msgtql * sizeof(struct __msg)) +
343 ALIGN(msginfo.msgmni * sizeof(kmsq_t));
344 sz = round_page(sz);
345
346 for (i = 0; i < msginfo.msgmni; i++)
347 cv_destroy(&msqs[i].msq_cv);
348
349 /* Set the pointers and update the new values */
350 msgpool = new_msgpool;
351 msgmaps = new_msgmaps;
352 msghdrs = new_msghdrs;
353 msqs = new_msqs;
354
355 free_msghdrs = new_free_msghdrs;
356 free_msgmaps = new_free_msgmaps;
357 nfree_msgmaps = new_nfree_msgmaps;
358 msginfo.msgmni = newmsgmni;
359 msginfo.msgseg = newmsgseg;
360 msginfo.msgmax = newmsgmax;
361
362 /* Reallocation completed - notify all waiters, if any */
363 msg_realloc_state = false;
364 cv_broadcast(&msg_realloc_cv);
365 mutex_exit(&msgmutex);
366
367 uvm_km_free(kernel_map, (vaddr_t)old_msgpool, sz, UVM_KMF_WIRED);
368 return 0;
369 }
370
371 static void
372 msg_freehdr(struct __msg *msghdr)
373 {
374
375 KASSERT(mutex_owned(&msgmutex));
376
377 while (msghdr->msg_ts > 0) {
378 short next;
379 KASSERT(msghdr->msg_spot >= 0);
380 KASSERT(msghdr->msg_spot < msginfo.msgseg);
381
382 next = msgmaps[msghdr->msg_spot].next;
383 msgmaps[msghdr->msg_spot].next = free_msgmaps;
384 free_msgmaps = msghdr->msg_spot;
385 nfree_msgmaps++;
386 msghdr->msg_spot = next;
387 if (msghdr->msg_ts >= msginfo.msgssz)
388 msghdr->msg_ts -= msginfo.msgssz;
389 else
390 msghdr->msg_ts = 0;
391 }
392 KASSERT(msghdr->msg_spot == -1);
393 msghdr->msg_next = free_msghdrs;
394 free_msghdrs = msghdr;
395 }
396
397 int
398 sys___msgctl50(struct lwp *l, const struct sys___msgctl50_args *uap,
399 register_t *retval)
400 {
401 /* {
402 syscallarg(int) msqid;
403 syscallarg(int) cmd;
404 syscallarg(struct msqid_ds *) buf;
405 } */
406 struct msqid_ds msqbuf;
407 int cmd, error;
408
409 cmd = SCARG(uap, cmd);
410
411 if (cmd == IPC_SET) {
412 error = copyin(SCARG(uap, buf), &msqbuf, sizeof(msqbuf));
413 if (error)
414 return (error);
415 }
416
417 error = msgctl1(l, SCARG(uap, msqid), cmd,
418 (cmd == IPC_SET || cmd == IPC_STAT) ? &msqbuf : NULL);
419
420 if (error == 0 && cmd == IPC_STAT)
421 error = copyout(&msqbuf, SCARG(uap, buf), sizeof(msqbuf));
422
423 return (error);
424 }
425
426 int
427 msgctl1(struct lwp *l, int msqid, int cmd, struct msqid_ds *msqbuf)
428 {
429 kauth_cred_t cred = l->l_cred;
430 struct msqid_ds *msqptr;
431 kmsq_t *msq;
432 int error = 0, ix;
433
434 MSG_PRINTF(("call to msgctl1(%d, %d)\n", msqid, cmd));
435
436 ix = IPCID_TO_IX(msqid);
437
438 mutex_enter(&msgmutex);
439
440 if (ix < 0 || ix >= msginfo.msgmni) {
441 MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", ix,
442 msginfo.msgmni));
443 error = EINVAL;
444 goto unlock;
445 }
446
447 msq = &msqs[ix];
448 msqptr = &msq->msq_u;
449
450 if (msqptr->msg_qbytes == 0) {
451 MSG_PRINTF(("no such msqid\n"));
452 error = EINVAL;
453 goto unlock;
454 }
455 if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqid)) {
456 MSG_PRINTF(("wrong sequence number\n"));
457 error = EINVAL;
458 goto unlock;
459 }
460
461 switch (cmd) {
462 case IPC_RMID:
463 {
464 struct __msg *msghdr;
465 if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_M)) != 0)
466 break;
467 /* Free the message headers */
468 msghdr = msqptr->_msg_first;
469 while (msghdr != NULL) {
470 struct __msg *msghdr_tmp;
471
472 /* Free the segments of each message */
473 msqptr->_msg_cbytes -= msghdr->msg_ts;
474 msqptr->msg_qnum--;
475 msghdr_tmp = msghdr;
476 msghdr = msghdr->msg_next;
477 msg_freehdr(msghdr_tmp);
478 }
479 KASSERT(msqptr->_msg_cbytes == 0);
480 KASSERT(msqptr->msg_qnum == 0);
481
482 /* Mark it as free */
483 msqptr->msg_qbytes = 0;
484 cv_broadcast(&msq->msq_cv);
485 }
486 break;
487
488 case IPC_SET:
489 if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_M)))
490 break;
491 if (msqbuf->msg_qbytes > msqptr->msg_qbytes &&
492 kauth_authorize_system(cred, KAUTH_SYSTEM_SYSVIPC,
493 KAUTH_REQ_SYSTEM_SYSVIPC_MSGQ_OVERSIZE,
494 KAUTH_ARG(msqbuf->msg_qbytes),
495 KAUTH_ARG(msqptr->msg_qbytes), NULL) != 0) {
496 error = EPERM;
497 break;
498 }
499 if (msqbuf->msg_qbytes > msginfo.msgmnb) {
500 MSG_PRINTF(("can't increase msg_qbytes beyond %d "
501 "(truncating)\n", msginfo.msgmnb));
502 /* silently restrict qbytes to system limit */
503 msqbuf->msg_qbytes = msginfo.msgmnb;
504 }
505 if (msqbuf->msg_qbytes == 0) {
506 MSG_PRINTF(("can't reduce msg_qbytes to 0\n"));
507 error = EINVAL; /* XXX non-standard errno! */
508 break;
509 }
510 msqptr->msg_perm.uid = msqbuf->msg_perm.uid;
511 msqptr->msg_perm.gid = msqbuf->msg_perm.gid;
512 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
513 (msqbuf->msg_perm.mode & 0777);
514 msqptr->msg_qbytes = msqbuf->msg_qbytes;
515 msqptr->msg_ctime = time_second;
516 break;
517
518 case IPC_STAT:
519 if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
520 MSG_PRINTF(("requester doesn't have read access\n"));
521 break;
522 }
523 memset(msqbuf, 0, sizeof *msqbuf);
524 msqbuf->msg_perm = msqptr->msg_perm;
525 msqbuf->msg_perm.mode &= 0777;
526 msqbuf->msg_qnum = msqptr->msg_qnum;
527 msqbuf->msg_qbytes = msqptr->msg_qbytes;
528 msqbuf->msg_lspid = msqptr->msg_lspid;
529 msqbuf->msg_lrpid = msqptr->msg_lrpid;
530 msqbuf->msg_stime = msqptr->msg_stime;
531 msqbuf->msg_rtime = msqptr->msg_rtime;
532 msqbuf->msg_ctime = msqptr->msg_ctime;
533 break;
534
535 default:
536 MSG_PRINTF(("invalid command %d\n", cmd));
537 error = EINVAL;
538 break;
539 }
540
541 unlock:
542 mutex_exit(&msgmutex);
543 return (error);
544 }
545
546 int
547 sys_msgget(struct lwp *l, const struct sys_msgget_args *uap, register_t *retval)
548 {
549 /* {
550 syscallarg(key_t) key;
551 syscallarg(int) msgflg;
552 } */
553 int msqid, error = 0;
554 int key = SCARG(uap, key);
555 int msgflg = SCARG(uap, msgflg);
556 kauth_cred_t cred = l->l_cred;
557 struct msqid_ds *msqptr = NULL;
558 kmsq_t *msq;
559
560 mutex_enter(&msgmutex);
561
562 MSG_PRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
563
564 if (key != IPC_PRIVATE) {
565 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
566 msq = &msqs[msqid];
567 msqptr = &msq->msq_u;
568 if (msqptr->msg_qbytes != 0 &&
569 msqptr->msg_perm._key == key)
570 break;
571 }
572 if (msqid < msginfo.msgmni) {
573 MSG_PRINTF(("found public key\n"));
574 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
575 MSG_PRINTF(("not exclusive\n"));
576 error = EEXIST;
577 goto unlock;
578 }
579 if ((error = ipcperm(cred, &msqptr->msg_perm,
580 msgflg & 0700 ))) {
581 MSG_PRINTF(("requester doesn't have 0%o access\n",
582 msgflg & 0700));
583 goto unlock;
584 }
585 goto found;
586 }
587 }
588
589 MSG_PRINTF(("need to allocate the msqid_ds\n"));
590 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
591 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
592 /*
593 * Look for an unallocated and unlocked msqid_ds.
594 * msqid_ds's can be locked by msgsnd or msgrcv while
595 * they are copying the message in/out. We can't
596 * re-use the entry until they release it.
597 */
598 msq = &msqs[msqid];
599 msqptr = &msq->msq_u;
600 if (msqptr->msg_qbytes == 0 &&
601 (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
602 break;
603 }
604 if (msqid == msginfo.msgmni) {
605 MSG_PRINTF(("no more msqid_ds's available\n"));
606 error = ENOSPC;
607 goto unlock;
608 }
609 MSG_PRINTF(("msqid %d is available\n", msqid));
610 msqptr->msg_perm._key = key;
611 msqptr->msg_perm.cuid = kauth_cred_geteuid(cred);
612 msqptr->msg_perm.uid = kauth_cred_geteuid(cred);
613 msqptr->msg_perm.cgid = kauth_cred_getegid(cred);
614 msqptr->msg_perm.gid = kauth_cred_getegid(cred);
615 msqptr->msg_perm.mode = (msgflg & 0777);
616 /* Make sure that the returned msqid is unique */
617 msqptr->msg_perm._seq++;
618 msqptr->_msg_first = NULL;
619 msqptr->_msg_last = NULL;
620 msqptr->_msg_cbytes = 0;
621 msqptr->msg_qnum = 0;
622 msqptr->msg_qbytes = msginfo.msgmnb;
623 msqptr->msg_lspid = 0;
624 msqptr->msg_lrpid = 0;
625 msqptr->msg_stime = 0;
626 msqptr->msg_rtime = 0;
627 msqptr->msg_ctime = time_second;
628 } else {
629 MSG_PRINTF(("didn't find it and wasn't asked to create it\n"));
630 error = ENOENT;
631 goto unlock;
632 }
633
634 found:
635 /* Construct the unique msqid */
636 *retval = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
637
638 unlock:
639 mutex_exit(&msgmutex);
640 return (error);
641 }
642
643 int
644 sys_msgsnd(struct lwp *l, const struct sys_msgsnd_args *uap, register_t *retval)
645 {
646 /* {
647 syscallarg(int) msqid;
648 syscallarg(const void *) msgp;
649 syscallarg(size_t) msgsz;
650 syscallarg(int) msgflg;
651 } */
652
653 return msgsnd1(l, SCARG(uap, msqid), SCARG(uap, msgp),
654 SCARG(uap, msgsz), SCARG(uap, msgflg), sizeof(long), copyin);
655 }
656
657 int
658 msgsnd1(struct lwp *l, int msqidr, const char *user_msgp, size_t msgsz,
659 int msgflg, size_t typesz, copyin_t fetch_type)
660 {
661 int segs_needed, error = 0, msqid;
662 kauth_cred_t cred = l->l_cred;
663 struct msqid_ds *msqptr;
664 struct __msg *msghdr;
665 kmsq_t *msq;
666 short next;
667
668 MSG_PRINTF(("call to msgsnd(%d, %p, %lld, %d)\n", msqidr,
669 user_msgp, (long long)msgsz, msgflg));
670
671 if ((ssize_t)msgsz < 0)
672 return EINVAL;
673
674 restart:
675 msqid = IPCID_TO_IX(msqidr);
676
677 mutex_enter(&msgmutex);
678 /* In case of reallocation, we will wait for completion */
679 while (__predict_false(msg_realloc_state))
680 cv_wait(&msg_realloc_cv, &msgmutex);
681
682 if (msqid < 0 || msqid >= msginfo.msgmni) {
683 MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
684 msginfo.msgmni));
685 error = EINVAL;
686 goto unlock;
687 }
688
689 msq = &msqs[msqid];
690 msqptr = &msq->msq_u;
691
692 if (msqptr->msg_qbytes == 0) {
693 MSG_PRINTF(("no such message queue id\n"));
694 error = EINVAL;
695 goto unlock;
696 }
697 if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
698 MSG_PRINTF(("wrong sequence number\n"));
699 error = EINVAL;
700 goto unlock;
701 }
702
703 if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_W))) {
704 MSG_PRINTF(("requester doesn't have write access\n"));
705 goto unlock;
706 }
707
708 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
709 MSG_PRINTF(("msgsz=%lld, msgssz=%d, segs_needed=%d\n",
710 (long long)msgsz, msginfo.msgssz, segs_needed));
711 for (;;) {
712 int need_more_resources = 0;
713
714 /*
715 * check msgsz [cannot be negative since it is unsigned]
716 * (inside this loop in case msg_qbytes changes while we sleep)
717 */
718
719 if (msgsz > msqptr->msg_qbytes) {
720 MSG_PRINTF(("msgsz > msqptr->msg_qbytes\n"));
721 error = EINVAL;
722 goto unlock;
723 }
724
725 if (msqptr->msg_perm.mode & MSG_LOCKED) {
726 MSG_PRINTF(("msqid is locked\n"));
727 need_more_resources = 1;
728 }
729 if (msgsz + msqptr->_msg_cbytes > msqptr->msg_qbytes) {
730 MSG_PRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
731 need_more_resources = 1;
732 }
733 if (segs_needed > nfree_msgmaps) {
734 MSG_PRINTF(("segs_needed > nfree_msgmaps\n"));
735 need_more_resources = 1;
736 }
737 if (free_msghdrs == NULL) {
738 MSG_PRINTF(("no more msghdrs\n"));
739 need_more_resources = 1;
740 }
741
742 if (need_more_resources) {
743 int we_own_it;
744
745 if ((msgflg & IPC_NOWAIT) != 0) {
746 MSG_PRINTF(("need more resources but caller "
747 "doesn't want to wait\n"));
748 error = EAGAIN;
749 goto unlock;
750 }
751
752 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
753 MSG_PRINTF(("we don't own the msqid_ds\n"));
754 we_own_it = 0;
755 } else {
756 /* Force later arrivals to wait for our
757 request */
758 MSG_PRINTF(("we own the msqid_ds\n"));
759 msqptr->msg_perm.mode |= MSG_LOCKED;
760 we_own_it = 1;
761 }
762
763 msg_waiters++;
764 MSG_PRINTF(("goodnight\n"));
765 error = cv_wait_sig(&msq->msq_cv, &msgmutex);
766 MSG_PRINTF(("good morning, error=%d\n", error));
767 msg_waiters--;
768
769 if (we_own_it)
770 msqptr->msg_perm.mode &= ~MSG_LOCKED;
771
772 /*
773 * In case of such state, notify reallocator and
774 * restart the call.
775 */
776 if (msg_realloc_state) {
777 cv_broadcast(&msg_realloc_cv);
778 mutex_exit(&msgmutex);
779 goto restart;
780 }
781
782 if (error != 0) {
783 MSG_PRINTF(("msgsnd: interrupted system "
784 "call\n"));
785 error = EINTR;
786 goto unlock;
787 }
788
789 /*
790 * Make sure that the msq queue still exists
791 */
792
793 if (msqptr->msg_qbytes == 0) {
794 MSG_PRINTF(("msqid deleted\n"));
795 error = EIDRM;
796 goto unlock;
797 }
798 } else {
799 MSG_PRINTF(("got all the resources that we need\n"));
800 break;
801 }
802 }
803
804 /*
805 * We have the resources that we need.
806 * Make sure!
807 */
808
809 KASSERT((msqptr->msg_perm.mode & MSG_LOCKED) == 0);
810 KASSERT(segs_needed <= nfree_msgmaps);
811 KASSERT(msgsz + msqptr->_msg_cbytes <= msqptr->msg_qbytes);
812 KASSERT(free_msghdrs != NULL);
813
814 /*
815 * Re-lock the msqid_ds in case we page-fault when copying in the
816 * message
817 */
818
819 KASSERT((msqptr->msg_perm.mode & MSG_LOCKED) == 0);
820 msqptr->msg_perm.mode |= MSG_LOCKED;
821
822 /*
823 * Allocate a message header
824 */
825
826 msghdr = free_msghdrs;
827 free_msghdrs = msghdr->msg_next;
828 msghdr->msg_spot = -1;
829 msghdr->msg_ts = msgsz;
830
831 /*
832 * Allocate space for the message
833 */
834
835 while (segs_needed > 0) {
836 KASSERT(nfree_msgmaps > 0);
837 KASSERT(free_msgmaps != -1);
838 KASSERT(free_msgmaps < msginfo.msgseg);
839
840 next = free_msgmaps;
841 MSG_PRINTF(("allocating segment %d to message\n", next));
842 free_msgmaps = msgmaps[next].next;
843 nfree_msgmaps--;
844 msgmaps[next].next = msghdr->msg_spot;
845 msghdr->msg_spot = next;
846 segs_needed--;
847 }
848
849 /*
850 * Copy in the message type
851 */
852 mutex_exit(&msgmutex);
853 error = (*fetch_type)(user_msgp, &msghdr->msg_type, typesz);
854 mutex_enter(&msgmutex);
855 if (error != 0) {
856 MSG_PRINTF(("error %d copying the message type\n", error));
857 msg_freehdr(msghdr);
858 msqptr->msg_perm.mode &= ~MSG_LOCKED;
859 cv_broadcast(&msq->msq_cv);
860 goto unlock;
861 }
862 user_msgp += typesz;
863
864 /*
865 * Validate the message type
866 */
867
868 if (msghdr->msg_type < 1) {
869 msg_freehdr(msghdr);
870 msqptr->msg_perm.mode &= ~MSG_LOCKED;
871 cv_broadcast(&msq->msq_cv);
872 MSG_PRINTF(("mtype (%ld) < 1\n", msghdr->msg_type));
873 error = EINVAL;
874 goto unlock;
875 }
876
877 /*
878 * Copy in the message body
879 */
880
881 next = msghdr->msg_spot;
882 while (msgsz > 0) {
883 size_t tlen;
884 KASSERT(next > -1);
885 KASSERT(next < msginfo.msgseg);
886
887 if (msgsz > msginfo.msgssz)
888 tlen = msginfo.msgssz;
889 else
890 tlen = msgsz;
891 mutex_exit(&msgmutex);
892 error = copyin(user_msgp, &msgpool[next * msginfo.msgssz], tlen);
893 mutex_enter(&msgmutex);
894 if (error != 0) {
895 MSG_PRINTF(("error %d copying in message segment\n",
896 error));
897 msg_freehdr(msghdr);
898 msqptr->msg_perm.mode &= ~MSG_LOCKED;
899 cv_broadcast(&msq->msq_cv);
900 goto unlock;
901 }
902 msgsz -= tlen;
903 user_msgp += tlen;
904 next = msgmaps[next].next;
905 }
906 KASSERT(next == -1);
907
908 /*
909 * We've got the message. Unlock the msqid_ds.
910 */
911
912 msqptr->msg_perm.mode &= ~MSG_LOCKED;
913
914 /*
915 * Make sure that the msqid_ds is still allocated.
916 */
917
918 if (msqptr->msg_qbytes == 0) {
919 msg_freehdr(msghdr);
920 cv_broadcast(&msq->msq_cv);
921 error = EIDRM;
922 goto unlock;
923 }
924
925 /*
926 * Put the message into the queue
927 */
928
929 if (msqptr->_msg_first == NULL) {
930 msqptr->_msg_first = msghdr;
931 msqptr->_msg_last = msghdr;
932 } else {
933 msqptr->_msg_last->msg_next = msghdr;
934 msqptr->_msg_last = msghdr;
935 }
936 msqptr->_msg_last->msg_next = NULL;
937
938 msqptr->_msg_cbytes += msghdr->msg_ts;
939 msqptr->msg_qnum++;
940 msqptr->msg_lspid = l->l_proc->p_pid;
941 msqptr->msg_stime = time_second;
942
943 cv_broadcast(&msq->msq_cv);
944
945 unlock:
946 mutex_exit(&msgmutex);
947 return error;
948 }
949
950 int
951 sys_msgrcv(struct lwp *l, const struct sys_msgrcv_args *uap, register_t *retval)
952 {
953 /* {
954 syscallarg(int) msqid;
955 syscallarg(void *) msgp;
956 syscallarg(size_t) msgsz;
957 syscallarg(long) msgtyp;
958 syscallarg(int) msgflg;
959 } */
960
961 return msgrcv1(l, SCARG(uap, msqid), SCARG(uap, msgp),
962 SCARG(uap, msgsz), SCARG(uap, msgtyp), SCARG(uap, msgflg),
963 sizeof(long), copyout, retval);
964 }
965
966 int
967 msgrcv1(struct lwp *l, int msqidr, char *user_msgp, size_t msgsz, long msgtyp,
968 int msgflg, size_t typesz, copyout_t put_type, register_t *retval)
969 {
970 size_t len;
971 kauth_cred_t cred = l->l_cred;
972 struct msqid_ds *msqptr;
973 struct __msg *msghdr;
974 int error = 0, msqid;
975 kmsq_t *msq;
976 short next;
977
978 MSG_PRINTF(("call to msgrcv(%d, %p, %lld, %ld, %d)\n", msqidr,
979 user_msgp, (long long)msgsz, msgtyp, msgflg));
980
981 if ((ssize_t)msgsz < 0)
982 return EINVAL;
983
984 restart:
985 msqid = IPCID_TO_IX(msqidr);
986
987 mutex_enter(&msgmutex);
988 /* In case of reallocation, we will wait for completion */
989 while (__predict_false(msg_realloc_state))
990 cv_wait(&msg_realloc_cv, &msgmutex);
991
992 if (msqid < 0 || msqid >= msginfo.msgmni) {
993 MSG_PRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
994 msginfo.msgmni));
995 error = EINVAL;
996 goto unlock;
997 }
998
999 msq = &msqs[msqid];
1000 msqptr = &msq->msq_u;
1001
1002 if (msqptr->msg_qbytes == 0) {
1003 MSG_PRINTF(("no such message queue id\n"));
1004 error = EINVAL;
1005 goto unlock;
1006 }
1007 if (msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
1008 MSG_PRINTF(("wrong sequence number\n"));
1009 error = EINVAL;
1010 goto unlock;
1011 }
1012
1013 if ((error = ipcperm(cred, &msqptr->msg_perm, IPC_R))) {
1014 MSG_PRINTF(("requester doesn't have read access\n"));
1015 goto unlock;
1016 }
1017
1018 msghdr = NULL;
1019 while (msghdr == NULL) {
1020 if (msgtyp == 0) {
1021 msghdr = msqptr->_msg_first;
1022 if (msghdr != NULL) {
1023 if (msgsz < msghdr->msg_ts &&
1024 (msgflg & MSG_NOERROR) == 0) {
1025 MSG_PRINTF(("first msg on the queue "
1026 "is too big (want %lld, got %d)\n",
1027 (long long)msgsz, msghdr->msg_ts));
1028 error = E2BIG;
1029 goto unlock;
1030 }
1031 if (msqptr->_msg_first == msqptr->_msg_last) {
1032 msqptr->_msg_first = NULL;
1033 msqptr->_msg_last = NULL;
1034 } else {
1035 msqptr->_msg_first = msghdr->msg_next;
1036 KASSERT(msqptr->_msg_first != NULL);
1037 }
1038 }
1039 } else {
1040 struct __msg *previous;
1041 struct __msg **prev;
1042
1043 for (previous = NULL, prev = &msqptr->_msg_first;
1044 (msghdr = *prev) != NULL;
1045 previous = msghdr, prev = &msghdr->msg_next) {
1046 /*
1047 * Is this message's type an exact match or is
1048 * this message's type less than or equal to
1049 * the absolute value of a negative msgtyp?
1050 * Note that the second half of this test can
1051 * NEVER be true if msgtyp is positive since
1052 * msg_type is always positive!
1053 */
1054
1055 if (msgtyp != msghdr->msg_type &&
1056 msghdr->msg_type > -msgtyp)
1057 continue;
1058
1059 MSG_PRINTF(("found message type %ld, requested %ld\n",
1060 msghdr->msg_type, msgtyp));
1061 if (msgsz < msghdr->msg_ts &&
1062 (msgflg & MSG_NOERROR) == 0) {
1063 MSG_PRINTF(("requested message on the queue "
1064 "is too big (want %lld, got %d)\n",
1065 (long long)msgsz, msghdr->msg_ts));
1066 error = E2BIG;
1067 goto unlock;
1068 }
1069 *prev = msghdr->msg_next;
1070 if (msghdr != msqptr->_msg_last)
1071 break;
1072 if (previous == NULL) {
1073 KASSERT(prev == &msqptr->_msg_first);
1074 msqptr->_msg_first = NULL;
1075 msqptr->_msg_last = NULL;
1076 } else {
1077 KASSERT(prev != &msqptr->_msg_first);
1078 msqptr->_msg_last = previous;
1079 }
1080 break;
1081 }
1082 }
1083
1084 /*
1085 * We've either extracted the msghdr for the appropriate
1086 * message or there isn't one.
1087 * If there is one then bail out of this loop.
1088 */
1089 if (msghdr != NULL)
1090 break;
1091
1092 /*
1093 * Hmph! No message found. Does the user want to wait?
1094 */
1095
1096 if ((msgflg & IPC_NOWAIT) != 0) {
1097 MSG_PRINTF(("no appropriate message found (msgtyp=%ld)\n",
1098 msgtyp));
1099 error = ENOMSG;
1100 goto unlock;
1101 }
1102
1103 /*
1104 * Wait for something to happen
1105 */
1106
1107 msg_waiters++;
1108 MSG_PRINTF(("msgrcv: goodnight\n"));
1109 error = cv_wait_sig(&msq->msq_cv, &msgmutex);
1110 MSG_PRINTF(("msgrcv: good morning (error=%d)\n", error));
1111 msg_waiters--;
1112
1113 /*
1114 * In case of such state, notify reallocator and
1115 * restart the call.
1116 */
1117 if (msg_realloc_state) {
1118 cv_broadcast(&msg_realloc_cv);
1119 mutex_exit(&msgmutex);
1120 goto restart;
1121 }
1122
1123 if (error != 0) {
1124 MSG_PRINTF(("msgsnd: interrupted system call\n"));
1125 error = EINTR;
1126 goto unlock;
1127 }
1128
1129 /*
1130 * Make sure that the msq queue still exists
1131 */
1132
1133 if (msqptr->msg_qbytes == 0 ||
1134 msqptr->msg_perm._seq != IPCID_TO_SEQ(msqidr)) {
1135 MSG_PRINTF(("msqid deleted\n"));
1136 error = EIDRM;
1137 goto unlock;
1138 }
1139 }
1140
1141 /*
1142 * Return the message to the user.
1143 *
1144 * First, do the bookkeeping (before we risk being interrupted).
1145 */
1146
1147 msqptr->_msg_cbytes -= msghdr->msg_ts;
1148 msqptr->msg_qnum--;
1149 msqptr->msg_lrpid = l->l_proc->p_pid;
1150 msqptr->msg_rtime = time_second;
1151
1152 /*
1153 * Make msgsz the actual amount that we'll be returning.
1154 * Note that this effectively truncates the message if it is too long
1155 * (since msgsz is never increased).
1156 */
1157
1158 MSG_PRINTF(("found a message, msgsz=%lld, msg_ts=%d\n",
1159 (long long)msgsz, msghdr->msg_ts));
1160 if (msgsz > msghdr->msg_ts)
1161 msgsz = msghdr->msg_ts;
1162
1163 /*
1164 * Return the type to the user.
1165 */
1166 mutex_exit(&msgmutex);
1167 error = (*put_type)(&msghdr->msg_type, user_msgp, typesz);
1168 mutex_enter(&msgmutex);
1169 if (error != 0) {
1170 MSG_PRINTF(("error (%d) copying out message type\n", error));
1171 msg_freehdr(msghdr);
1172 cv_broadcast(&msq->msq_cv);
1173 goto unlock;
1174 }
1175 user_msgp += typesz;
1176
1177 /*
1178 * Return the segments to the user
1179 */
1180
1181 next = msghdr->msg_spot;
1182 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1183 size_t tlen;
1184 KASSERT(next > -1);
1185 KASSERT(next < msginfo.msgseg);
1186
1187 if (msgsz - len > msginfo.msgssz)
1188 tlen = msginfo.msgssz;
1189 else
1190 tlen = msgsz - len;
1191 mutex_exit(&msgmutex);
1192 error = copyout(&msgpool[next * msginfo.msgssz],
1193 user_msgp, tlen);
1194 mutex_enter(&msgmutex);
1195 if (error != 0) {
1196 MSG_PRINTF(("error (%d) copying out message segment\n",
1197 error));
1198 msg_freehdr(msghdr);
1199 cv_broadcast(&msq->msq_cv);
1200 goto unlock;
1201 }
1202 user_msgp += tlen;
1203 next = msgmaps[next].next;
1204 }
1205
1206 /*
1207 * Done, return the actual number of bytes copied out.
1208 */
1209
1210 msg_freehdr(msghdr);
1211 cv_broadcast(&msq->msq_cv);
1212 *retval = msgsz;
1213
1214 unlock:
1215 mutex_exit(&msgmutex);
1216 return error;
1217 }
1218
1219 /*
1220 * Sysctl initialization and nodes.
1221 */
1222
1223 static int
1224 sysctl_ipc_msgmni(SYSCTLFN_ARGS)
1225 {
1226 int newsize, error;
1227 struct sysctlnode node;
1228 node = *rnode;
1229 node.sysctl_data = &newsize;
1230
1231 newsize = msginfo.msgmni;
1232 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1233 if (error || newp == NULL)
1234 return error;
1235
1236 sysctl_unlock();
1237 error = msgrealloc(newsize, msginfo.msgseg);
1238 sysctl_relock();
1239 return error;
1240 }
1241
1242 static int
1243 sysctl_ipc_msgseg(SYSCTLFN_ARGS)
1244 {
1245 int newsize, error;
1246 struct sysctlnode node;
1247 node = *rnode;
1248 node.sysctl_data = &newsize;
1249
1250 newsize = msginfo.msgseg;
1251 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1252 if (error || newp == NULL)
1253 return error;
1254
1255 sysctl_unlock();
1256 error = msgrealloc(msginfo.msgmni, newsize);
1257 sysctl_relock();
1258 return error;
1259 }
1260
1261 SYSCTL_SETUP(sysctl_ipc_msg_setup, "sysctl kern.ipc subtree setup")
1262 {
1263 const struct sysctlnode *node = NULL;
1264
1265 sysctl_createv(clog, 0, NULL, &node,
1266 CTLFLAG_PERMANENT,
1267 CTLTYPE_NODE, "ipc",
1268 SYSCTL_DESCR("SysV IPC options"),
1269 NULL, 0, NULL, 0,
1270 CTL_KERN, KERN_SYSVIPC, CTL_EOL);
1271
1272 if (node == NULL)
1273 return;
1274
1275 sysctl_createv(clog, 0, &node, NULL,
1276 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1277 CTLTYPE_INT, "msgmni",
1278 SYSCTL_DESCR("Max number of message queue identifiers"),
1279 sysctl_ipc_msgmni, 0, &msginfo.msgmni, 0,
1280 CTL_CREATE, CTL_EOL);
1281 sysctl_createv(clog, 0, &node, NULL,
1282 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1283 CTLTYPE_INT, "msgseg",
1284 SYSCTL_DESCR("Max number of number of message segments"),
1285 sysctl_ipc_msgseg, 0, &msginfo.msgseg, 0,
1286 CTL_CREATE, CTL_EOL);
1287 }
1288