]> sourceware.org Git - newlib-cygwin.git/blame - winsup/cygserver/sysv_msg.cc
* bsd_helper.cc (ipcexit_hookthread): Fix whitespace and handle leak.
[newlib-cygwin.git] / winsup / cygserver / sysv_msg.cc
CommitLineData
282113ba
CV
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/*
21 * This file is heavily changed to become part of Cygwin's cygserver.
22 */
23
24#ifdef __OUTSIDE_CYGWIN__
25#include "woutsup.h"
26#include <sys/cdefs.h>
27#ifndef __FBSDID
28#define __FBSDID(s) const char version[] = (s)
29#endif
30__FBSDID("$FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/kern/sysv_msg.c,v 1.52 2003/11/07 04:47:14 rwatson Exp $");
31
32#define _KERNEL 1
33#define __BSD_VISIBLE 1
34#include <sys/types.h>
35#include <sys/sysproto.h>
36#include <sys/ipc.h>
37#include <sys/param.h>
38#include <sys/msg.h>
39#include <malloc.h>
40#include <errno.h>
41#include <time.h>
42#include "cygserver.h"
43#include "process.h"
44#include "cygserver_ipc.h"
45
46#ifdef __CYGWIN__
47#define MSG_DEBUG
48#endif /* __CYGWIN__ */
49
50#ifdef MSG_DEBUG
51#define DPRINTF(a) debug_printf a
52#else
53#define DPRINTF(a)
54#endif
55
56static void msg_freehdr(struct msg *msghdr);
57
58#ifndef __CYGWIN__
59int msgctl(struct thread *, struct msgctl_args *);
60int msgget(struct thread *, struct msgget_args *);
61int msgsnd(struct thread *, struct msgsnd_args *);
62int msgrcv(struct thread *, struct msgrcv_args *);
63
64static sy_call_t *msgcalls[] = {
65 (sy_call_t *)msgctl, (sy_call_t *)msgget,
66 (sy_call_t *)msgsnd, (sy_call_t *)msgrcv
67};
68#endif /* __CYGWIN__ */
69
70
71struct msg {
72 struct msg *msg_next; /* next msg in the chain */
73 long msg_type; /* type of this message */
74 /* >0 -> type of this message */
75 /* 0 -> free header */
76 u_short msg_ts; /* size of this message */
77 short msg_spot; /* location of start of msg in buffer */
78};
79
80
81#ifndef MSGSSZ
82#define MSGSSZ 8 /* Each segment must be 2^N long */
83#endif
84#ifndef MSGSEG
85#define MSGSEG 2048 /* must be less than 32767 */
86#endif
87#define MSGMAX (MSGSSZ*MSGSEG)
88#ifndef MSGMNB
89#define MSGMNB 2048 /* max # of bytes in a queue */
90#endif
91#ifndef MSGMNI
92#define MSGMNI 40
93#endif
94#ifndef MSGTQL
95#define MSGTQL 40
96#endif
97
98/*
99 * Based on the configuration parameters described in an SVR2 (yes, two)
100 * config(1m) man page.
101 *
102 * Each message is broken up and stored in segments that are msgssz bytes
103 * long. For efficiency reasons, this should be a power of two. Also,
104 * it doesn't make sense if it is less than 8 or greater than about 256.
105 * Consequently, msginit in kern/sysv_msg.c checks that msgssz is a power of
106 * two between 8 and 1024 inclusive (and panic's if it isn't).
107 */
108struct msginfo msginfo = {
109 MSGMAX, /* max chars in a message */
110 MSGMNB, /* max chars in a queue */
111 MSGMNI, /* # of message queue identifiers */
112 MSGTQL, /* max messages in system */
113 MSGSSZ, /* size of a message segment */
114 /* (must be small power of 2 greater than 4) */
115 MSGSEG /* number of message segments */
116};
117
118/*
119 * macros to convert between msqid_ds's and msqid's.
120 * (specific to this implementation)
121 */
122#define MSQID(ix,ds) ((ix) & 0xffff | (((ds).msg_perm.seq << 16) & 0xffff0000))
123#define MSQID_IX(id) ((id) & 0xffff)
124#define MSQID_SEQ(id) (((id) >> 16) & 0xffff)
125
126/*
127 * The rest of this file is specific to this particular implementation.
128 */
129
130struct msgmap {
131 short next; /* next segment in buffer */
132 /* -1 -> available */
133 /* 0..(MSGSEG-1) -> index of next segment */
134};
135
136#define MSG_LOCKED 01000 /* Is this msqid_ds locked? */
137
138static int nfree_msgmaps; /* # of free map entries */
139static short free_msgmaps; /* head of linked list of free map entries */
140static struct msg *free_msghdrs;/* list of free msg headers */
141static char *msgpool; /* MSGMAX byte long msg buffer pool */
142static struct msgmap *msgmaps; /* MSGSEG msgmap structures */
143static struct msg *msghdrs; /* MSGTQL msg headers */
144static struct msqid_ds *msqids; /* MSGMNI msqid_ds struct's */
145static struct mtx msq_mtx; /* global mutex for message queues. */
146
147#ifdef __CYGWIN__
148static struct msg_info msg_info;
149#endif /* __CYGWIN__ */
150
151void
152msginit()
153{
154 int i;
155
156 TUNABLE_INT_FETCH("kern.ipc.msgseg", &msginfo.msgseg);
157 TUNABLE_INT_FETCH("kern.ipc.msgssz", &msginfo.msgssz);
158 msginfo.msgmax = msginfo.msgseg * msginfo.msgssz;
11377ba6 159 TUNABLE_INT_FETCH("kern.ipc.msgmnb", &msginfo.msgmnb);
282113ba 160 TUNABLE_INT_FETCH("kern.ipc.msgmni", &msginfo.msgmni);
11377ba6 161 TUNABLE_INT_FETCH("kern.ipc.msgtql", &msginfo.msgtql);
282113ba
CV
162
163 msgpool = (char *) sys_malloc(msginfo.msgmax, M_MSG, M_WAITOK);
164 if (msgpool == NULL)
165 panic("msgpool is NULL");
166 msgmaps = (msgmap *) sys_malloc(sizeof(struct msgmap) * msginfo.msgseg, M_MSG, M_WAITOK);
167 if (msgmaps == NULL)
168 panic("msgmaps is NULL");
169 msghdrs = (msg *) sys_malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK);
170 if (msghdrs == NULL)
171 panic("msghdrs is NULL");
172 msqids = (msqid_ds *) sys_malloc(sizeof(struct msqid_ds) * msginfo.msgmni, M_MSG, M_WAITOK);
173 if (msqids == NULL)
174 panic("msqids is NULL");
175
176 /*
177 * msginfo.msgssz should be a power of two for efficiency reasons.
178 * It is also pretty silly if msginfo.msgssz is less than 8
179 * or greater than about 256 so ...
180 */
181
182 i = 8;
183 while (i < 1024 && i != msginfo.msgssz)
184 i <<= 1;
185 if (i != msginfo.msgssz) {
186 DPRINTF(("msginfo.msgssz=%d (0x%x)\n", msginfo.msgssz,
187 msginfo.msgssz));
188 panic("msginfo.msgssz not a small power of 2");
189 }
190
191 if (msginfo.msgseg > 32767) {
192 DPRINTF(("msginfo.msgseg=%d\n", msginfo.msgseg));
193 panic("msginfo.msgseg > 32767");
194 }
195
196 if (msgmaps == NULL)
197 panic("msgmaps is NULL");
198
199 for (i = 0; i < msginfo.msgseg; i++) {
200 if (i > 0)
201 msgmaps[i-1].next = i;
202 msgmaps[i].next = -1; /* implies entry is available */
203 }
204 free_msgmaps = 0;
205 nfree_msgmaps = msginfo.msgseg;
206
207 if (msghdrs == NULL)
208 panic("msghdrs is NULL");
209
210 for (i = 0; i < msginfo.msgtql; i++) {
211 msghdrs[i].msg_type = 0;
212 if (i > 0)
213 msghdrs[i-1].msg_next = &msghdrs[i];
214 msghdrs[i].msg_next = NULL;
215 }
216 free_msghdrs = &msghdrs[0];
217
218 if (msqids == NULL)
219 panic("msqids is NULL");
220
221 for (i = 0; i < msginfo.msgmni; i++) {
222 msqids[i].msg_qbytes = 0; /* implies entry is available */
223 msqids[i].msg_perm.seq = 0; /* reset to a known value */
224 msqids[i].msg_perm.mode = 0;
225 }
226 mtx_init(&msq_mtx, "msq", NULL, MTX_DEF);
227}
228
229int
230msgunload()
231{
232 struct msqid_ds *msqptr;
233 int msqid;
234
235 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
236 /*
237 * Look for an unallocated and unlocked msqid_ds.
238 * msqid_ds's can be locked by msgsnd or msgrcv while
239 * they are copying the message in/out. We can't
240 * re-use the entry until they release it.
241 */
242 msqptr = &msqids[msqid];
243 if (msqptr->msg_qbytes != 0 ||
244 (msqptr->msg_perm.mode & MSG_LOCKED) != 0)
245 break;
246 }
247#ifndef __CYGWIN__
248 if (msqid != msginfo.msgmni)
249 return (EBUSY);
250#endif /* __CYGWIN__ */
251
252 sys_free(msgpool, M_MSG);
253 sys_free(msgmaps, M_MSG);
254 sys_free(msghdrs, M_MSG);
255 sys_free(msqids, M_MSG);
256 mtx_destroy(&msq_mtx);
257 return (0);
258}
259
260
261#ifndef __CYGWIN__
262static int
263sysvmsg_modload(struct module *module, int cmd, void *arg)
264{
265 int error = 0;
266
267 switch (cmd) {
268 case MOD_LOAD:
269 msginit();
270 break;
271 case MOD_UNLOAD:
272 error = msgunload();
273 break;
274 case MOD_SHUTDOWN:
275 break;
276 default:
277 error = EINVAL;
278 break;
279 }
280 return (error);
281}
282
283static moduledata_t sysvmsg_mod = {
284 "sysvmsg",
285 &sysvmsg_modload,
286 NULL
287};
288
289SYSCALL_MODULE_HELPER(msgsys);
290SYSCALL_MODULE_HELPER(msgctl);
291SYSCALL_MODULE_HELPER(msgget);
292SYSCALL_MODULE_HELPER(msgsnd);
293SYSCALL_MODULE_HELPER(msgrcv);
294
295DECLARE_MODULE(sysvmsg, sysvmsg_mod,
296 SI_SUB_SYSV_MSG, SI_ORDER_FIRST);
297MODULE_VERSION(sysvmsg, 1);
298
299/*
300 * Entry point for all MSG calls
301 *
302 * MPSAFE
303 */
304int
305msgsys(thread *td, struct msgsys_args *uap)
306{
307 int error;
308
309 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
310 return (ENOSYS);
311 if (uap->which < 0 ||
312 (unsigned) uap->which >= sizeof(msgcalls)/sizeof(msgcalls[0]))
313 return (EINVAL);
314 error = (*msgcalls[uap->which])(td, &uap->a2);
315 return (error);
316}
317#endif
318
319static void
320msg_freehdr(struct msg *msghdr)
321{
322 while (msghdr->msg_ts > 0) {
323 short next;
324 if (msghdr->msg_spot < 0 || msghdr->msg_spot >= msginfo.msgseg)
325 panic("msghdr->msg_spot out of range");
326 next = msgmaps[msghdr->msg_spot].next;
327 msgmaps[msghdr->msg_spot].next = free_msgmaps;
328 free_msgmaps = msghdr->msg_spot;
329 nfree_msgmaps++;
330 msghdr->msg_spot = next;
331 if (msghdr->msg_ts >= msginfo.msgssz)
332 msghdr->msg_ts -= msginfo.msgssz;
333 else
334 msghdr->msg_ts = 0;
335 }
336 if (msghdr->msg_spot != -1)
337 panic("msghdr->msg_spot != -1");
338 msghdr->msg_next = free_msghdrs;
339 free_msghdrs = msghdr;
340}
341
342#ifndef _SYS_SYSPROTO_H_
343struct msgctl_args {
344 int msqid;
345 int cmd;
346 struct msqid_ds *buf;
347};
348#endif
349
350/*
351 * MPSAFE
352 */
353int
354msgctl(struct thread *td, struct msgctl_args *uap)
355{
356 int msqid = uap->msqid;
357 int cmd = uap->cmd;
358 struct msqid_ds *user_msqptr = uap->buf;
359 int rval, error;
360 struct msqid_ds msqbuf;
361 register struct msqid_ds *msqptr;
362
363 DPRINTF(("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr));
364
365 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
366 return (ENOSYS);
367
368#ifdef __CYGWIN__
369 if (cmd == IPC_INFO) {
370 if (!msqid) {
371 error = copyout(&msginfo, user_msqptr,
372 sizeof(struct msginfo));
373 td->td_retval[0] = error ? -1 : 0;
374 return (error);
375 }
376 if (msqid > msginfo.msgmni)
377 msqid = msginfo.msgmni;
378 error = copyout(msqids, user_msqptr,
379 msqid * sizeof(struct msqid_ds));
380 td->td_retval[0] = error ? -1 : 0;
381 return (error);
382 } else if (cmd == MSG_INFO) {
383 mtx_lock(&msq_mtx);
384 error = copyout(&msg_info, user_msqptr,
385 sizeof(struct msg_info));
386 td->td_retval[0] = error ? -1 : 0;
387 mtx_unlock(&msq_mtx);
388 return (error);
389 }
390#endif /* __CYGWIN__ */
391 msqid = IPCID_TO_IX(msqid);
392
393 if (msqid < 0 || msqid >= msginfo.msgmni) {
394 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
395 msginfo.msgmni));
396 return (EINVAL);
397 }
398 if (cmd == IPC_SET &&
399 (error = copyin(user_msqptr, &msqbuf, sizeof(msqbuf))) != 0)
400 return (error);
401
402 msqptr = &msqids[msqid];
403
404 mtx_lock(&msq_mtx);
405 if (msqptr->msg_qbytes == 0) {
406 DPRINTF(("no such msqid\n"));
407 error = EINVAL;
408 goto done2;
409 }
410 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
411 DPRINTF(("wrong sequence number\n"));
412 error = EINVAL;
413 goto done2;
414 }
415
416 error = 0;
417 rval = 0;
418
419 switch (cmd) {
420
421 case IPC_RMID:
422 {
423 struct msg *msghdr;
424 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_M)))
425 goto done2;
426 /* Free the message headers */
427 msghdr = msqptr->msg_first;
428 while (msghdr != NULL) {
429 struct msg *msghdr_tmp;
430
431 /* Free the segments of each message */
432 msqptr->msg_cbytes -= msghdr->msg_ts;
433 msqptr->msg_qnum--;
434 msghdr_tmp = msghdr;
435 msghdr = msghdr->msg_next;
436 msg_freehdr(msghdr_tmp);
437 }
438
439 if (msqptr->msg_cbytes != 0)
440 panic("msg_cbytes is screwed up");
441 if (msqptr->msg_qnum != 0)
442 panic("msg_qnum is screwed up");
443
444 msqptr->msg_qbytes = 0; /* Mark it as free */
445#ifdef __CYGWIN__
446 msg_info.msg_ids--;
447#endif /* __CYGWIN__ */
448
449 wakeup(msqptr);
450 }
451
452 break;
453
454 case IPC_SET:
455 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_M)))
456 goto done2;
457 if (msqbuf.msg_qbytes > msqptr->msg_qbytes) {
458 error = suser(td);
459 if (error)
460 goto done2;
461 }
462 if (msqbuf.msg_qbytes > (unsigned long) msginfo.msgmnb) {
463 DPRINTF(("can't increase msg_qbytes beyond %d"
464 "(truncating)\n", msginfo.msgmnb));
465 msqbuf.msg_qbytes = msginfo.msgmnb; /* silently restrict qbytes to system limit */
466 }
467 if (msqbuf.msg_qbytes == 0) {
468 DPRINTF(("can't reduce msg_qbytes to 0\n"));
469 error = EINVAL; /* non-standard errno! */
470 goto done2;
471 }
472 msqptr->msg_perm.uid = msqbuf.msg_perm.uid; /* change the owner */
473 msqptr->msg_perm.gid = msqbuf.msg_perm.gid; /* change the owner */
474 msqptr->msg_perm.mode = (msqptr->msg_perm.mode & ~0777) |
475 (msqbuf.msg_perm.mode & 0777);
476 msqptr->msg_qbytes = msqbuf.msg_qbytes;
477 msqptr->msg_ctime = time (NULL);
478 break;
479
480 case IPC_STAT:
481 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_R))) {
482 DPRINTF(("requester doesn't have read access\n"));
483 goto done2;
484 }
485 break;
486
487 default:
488 DPRINTF(("invalid command %d\n", cmd));
489 error = EINVAL;
490 goto done2;
491 }
492
493 if (error == 0)
494 td->td_retval[0] = rval;
495done2:
496 mtx_unlock(&msq_mtx);
497 if (cmd == IPC_STAT && error == 0)
498 error = copyout(msqptr, user_msqptr, sizeof(struct msqid_ds));
499 return(error);
500}
501
502#ifndef _SYS_SYSPROTO_H_
503struct msgget_args {
504 key_t key;
505 int msgflg;
506};
507#endif
508
509/*
510 * MPSAFE
511 */
512int
513msgget(struct thread *td, struct msgget_args *uap)
514{
515 int msqid, error = 0;
516 key_t key = uap->key;
517 unsigned msgflg = uap->msgflg;
518 register struct msqid_ds *msqptr = NULL;
519
520 DPRINTF(("msgget(0x%x, 0%o)\n", key, msgflg));
521
522 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
523 return (ENOSYS);
524
525 mtx_lock(&msq_mtx);
526 if (key != IPC_PRIVATE) {
527 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
528 msqptr = &msqids[msqid];
529 if (msqptr->msg_qbytes != 0 &&
530 msqptr->msg_perm.key == key)
531 break;
532 }
533 if (msqid < msginfo.msgmni) {
534 DPRINTF(("found public key\n"));
535 if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL)) {
536 DPRINTF(("not exclusive\n"));
537 error = EEXIST;
538 goto done2;
539 }
540 if ((error = ipcperm(td, &msqptr->msg_perm, msgflg & 0700))) {
541 DPRINTF(("requester doesn't have 0%o access\n",
542 msgflg & 0700));
543 goto done2;
544 }
545 goto found;
546 }
547 }
548
549 DPRINTF(("need to allocate the msqid_ds\n"));
550 if (key == IPC_PRIVATE || (msgflg & IPC_CREAT)) {
551 for (msqid = 0; msqid < msginfo.msgmni; msqid++) {
552 /*
553 * Look for an unallocated and unlocked msqid_ds.
554 * msqid_ds's can be locked by msgsnd or msgrcv while
555 * they are copying the message in/out. We can't
556 * re-use the entry until they release it.
557 */
558 msqptr = &msqids[msqid];
559 if (msqptr->msg_qbytes == 0 &&
560 (msqptr->msg_perm.mode & MSG_LOCKED) == 0)
561 break;
562 }
563 if (msqid == msginfo.msgmni) {
564 DPRINTF(("no more msqid_ds's available\n"));
565 error = ENOSPC;
566 goto done2;
567 }
568 DPRINTF(("msqid %d is available\n", msqid));
569 msqptr->msg_perm.key = key;
570#ifdef __CYGWIN__
571 msqptr->msg_perm.cuid = td->ipcblk->uid;
572 msqptr->msg_perm.uid = td->ipcblk->uid;
573 msqptr->msg_perm.cgid = td->ipcblk->gid;
574 msqptr->msg_perm.gid = td->ipcblk->gid;
575#else
576 msqptr->msg_perm.cuid = cred->cr_uid;
577 msqptr->msg_perm.uid = cred->cr_uid;
578 msqptr->msg_perm.cgid = cred->cr_gid;
579 msqptr->msg_perm.gid = cred->cr_gid;
580#endif /* __CYGWIN__ */
581 msqptr->msg_perm.mode = (msgflg & 0777);
582 /* Make sure that the returned msqid is unique */
583 msqptr->msg_perm.seq = (msqptr->msg_perm.seq + 1) & 0x7fff;
584 msqptr->msg_first = NULL;
585 msqptr->msg_last = NULL;
586 msqptr->msg_cbytes = 0;
587 msqptr->msg_qnum = 0;
588 msqptr->msg_qbytes = msginfo.msgmnb;
589 msqptr->msg_lspid = 0;
590 msqptr->msg_lrpid = 0;
591 msqptr->msg_stime = 0;
592 msqptr->msg_rtime = 0;
593 msqptr->msg_ctime = time (NULL);
594#ifdef __CYGWIN__
595 msg_info.msg_ids++;
596#endif /* __CYGWIN__ */
597 } else {
598 DPRINTF(("didn't find it and wasn't asked to create it\n"));
599 error = ENOENT;
600 goto done2;
601 }
602
603found:
604 /* Construct the unique msqid */
605 td->td_retval[0] = IXSEQ_TO_IPCID(msqid, msqptr->msg_perm);
606done2:
607 mtx_unlock(&msq_mtx);
608 return (error);
609}
610
611#ifndef _SYS_SYSPROTO_H_
612struct msgsnd_args {
613 int msqid;
614 const void *msgp;
615 size_t msgsz;
616 int msgflg;
617};
618#endif
619
620/*
621 * MPSAFE
622 */
623int
624msgsnd(struct thread *td, struct msgsnd_args *uap)
625{
626 int msqid = uap->msqid;
627 const void *user_msgp = uap->msgp;
628 size_t msgsz = uap->msgsz;
629 int msgflg = uap->msgflg;
630 int segs_needed, error = 0;
631 register struct msqid_ds *msqptr;
632 register struct msg *msghdr;
633 short next;
634
635 DPRINTF(("call to msgsnd(%d, 0x%x, %d, %d)\n", msqid, user_msgp, msgsz,
636 msgflg));
637
638 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
639 return (ENOSYS);
640
641 mtx_lock(&msq_mtx);
642 msqid = IPCID_TO_IX(msqid);
643
644 if (msqid < 0 || msqid >= msginfo.msgmni) {
645 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
646 msginfo.msgmni));
647 error = EINVAL;
648 goto done2;
649 }
650
651 msqptr = &msqids[msqid];
652 if (msqptr->msg_qbytes == 0) {
653 DPRINTF(("no such message queue id\n"));
654 error = EINVAL;
655 goto done2;
656 }
657 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
658 DPRINTF(("wrong sequence number\n"));
659 error = EINVAL;
660 goto done2;
661 }
662
663 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_W))) {
664 DPRINTF(("requester doesn't have write access\n"));
665 goto done2;
666 }
667
668 segs_needed = (msgsz + msginfo.msgssz - 1) / msginfo.msgssz;
669 DPRINTF(("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz,
670 segs_needed));
671 for (;;) {
672 int need_more_resources = 0;
673
674 /*
675 * check msgsz
676 * (inside this loop in case msg_qbytes changes while we sleep)
677 */
678
679 if (msgsz > msqptr->msg_qbytes) {
680 DPRINTF(("msgsz > msqptr->msg_qbytes\n"));
681 error = EINVAL;
682 goto done2;
683 }
684
685 if (msqptr->msg_perm.mode & MSG_LOCKED) {
686 DPRINTF(("msqid is locked\n"));
687 need_more_resources = 1;
688 }
689 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes) {
690 DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n"));
691 need_more_resources = 1;
692 }
693 if (segs_needed > nfree_msgmaps) {
694 DPRINTF(("segs_needed > nfree_msgmaps\n"));
695 need_more_resources = 1;
696 }
697 if (free_msghdrs == NULL) {
698 DPRINTF(("no more msghdrs\n"));
699 need_more_resources = 1;
700 }
701
702 if (need_more_resources) {
703 int we_own_it;
704
705 if ((msgflg & IPC_NOWAIT) != 0) {
706 DPRINTF(("need more resources but caller "
707 "doesn't want to wait\n"));
708 error = EAGAIN;
709 goto done2;
710 }
711
712 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0) {
713 DPRINTF(("we don't own the msqid_ds\n"));
714 we_own_it = 0;
715 } else {
716 /* Force later arrivals to wait for our
717 request */
718 DPRINTF(("we own the msqid_ds\n"));
719 msqptr->msg_perm.mode |= MSG_LOCKED;
720 we_own_it = 1;
721 }
722 DPRINTF(("goodnight\n"));
723 error = msleep(msqptr, &msq_mtx, (PZERO - 4) | PCATCH,
724 "msgwait", 0);
725 DPRINTF(("good morning, error=%d\n", error));
726 if (we_own_it)
727 msqptr->msg_perm.mode &= ~MSG_LOCKED;
728 if (error != 0) {
729 DPRINTF(("msgsnd: interrupted system call\n"));
730#ifdef __CYGWIN__
731 if (error != EIDRM)
732#endif /* __CYGWIN__ */
733 error = EINTR;
734 goto done2;
735 }
736
737 /*
738 * Make sure that the msq queue still exists
739 */
740
741 if (msqptr->msg_qbytes == 0) {
742 DPRINTF(("msqid deleted\n"));
743 error = EIDRM;
744 goto done2;
745 }
746
747 } else {
748 DPRINTF(("got all the resources that we need\n"));
749 break;
750 }
751 }
752
753 /*
754 * We have the resources that we need.
755 * Make sure!
756 */
757
758 if (msqptr->msg_perm.mode & MSG_LOCKED)
759 panic("msg_perm.mode & MSG_LOCKED");
760 if (segs_needed > nfree_msgmaps)
761 panic("segs_needed > nfree_msgmaps");
762 if (msgsz + msqptr->msg_cbytes > msqptr->msg_qbytes)
763 panic("msgsz + msg_cbytes > msg_qbytes");
764 if (free_msghdrs == NULL)
765 panic("no more msghdrs");
766
767 /*
768 * Re-lock the msqid_ds in case we page-fault when copying in the
769 * message
770 */
771
772 if ((msqptr->msg_perm.mode & MSG_LOCKED) != 0)
773 panic("msqid_ds is already locked");
774 msqptr->msg_perm.mode |= MSG_LOCKED;
775
776 /*
777 * Allocate a message header
778 */
779
780 msghdr = free_msghdrs;
781 free_msghdrs = msghdr->msg_next;
782 msghdr->msg_spot = -1;
783 msghdr->msg_ts = msgsz;
784
785 /*
786 * Allocate space for the message
787 */
788
789 while (segs_needed > 0) {
790 if (nfree_msgmaps <= 0)
791 panic("not enough msgmaps");
792 if (free_msgmaps == -1)
793 panic("nil free_msgmaps");
794 next = free_msgmaps;
795 if (next <= -1)
796 panic("next too low #1");
797 if (next >= msginfo.msgseg)
798 panic("next out of range #1");
799 DPRINTF(("allocating segment %d to message\n", next));
800 free_msgmaps = msgmaps[next].next;
801 nfree_msgmaps--;
802 msgmaps[next].next = msghdr->msg_spot;
803 msghdr->msg_spot = next;
804 segs_needed--;
805 }
806
807 /*
808 * Copy in the message type
809 */
810
811 mtx_unlock(&msq_mtx);
812 if ((error = copyin(user_msgp, &msghdr->msg_type,
813 sizeof(msghdr->msg_type))) != 0) {
814 mtx_lock(&msq_mtx);
815 DPRINTF(("error %d copying the message type\n", error));
816 msg_freehdr(msghdr);
817 msqptr->msg_perm.mode &= ~MSG_LOCKED;
818 wakeup(msqptr);
819 goto done2;
820 }
821 mtx_lock(&msq_mtx);
822 user_msgp = (const char *)user_msgp + sizeof(msghdr->msg_type);
823
824 /*
825 * Validate the message type
826 */
827
828 if (msghdr->msg_type < 1) {
829 msg_freehdr(msghdr);
830 msqptr->msg_perm.mode &= ~MSG_LOCKED;
831 wakeup(msqptr);
832 DPRINTF(("mtype (%d) < 1\n", msghdr->msg_type));
833 error = EINVAL;
834 goto done2;
835 }
836
837 /*
838 * Copy in the message body
839 */
840
841 next = msghdr->msg_spot;
842 while (msgsz > 0) {
843 size_t tlen;
844 if (msgsz > (unsigned long) msginfo.msgssz)
845 tlen = msginfo.msgssz;
846 else
847 tlen = msgsz;
848 if (next <= -1)
849 panic("next too low #2");
850 if (next >= msginfo.msgseg)
851 panic("next out of range #2");
852 mtx_unlock(&msq_mtx);
853 if ((error = copyin(user_msgp, &msgpool[next * msginfo.msgssz],
854 tlen)) != 0) {
855 mtx_lock(&msq_mtx);
856 DPRINTF(("error %d copying in message segment\n",
857 error));
858 msg_freehdr(msghdr);
859 msqptr->msg_perm.mode &= ~MSG_LOCKED;
860 wakeup(msqptr);
861 goto done2;
862 }
863 mtx_lock(&msq_mtx);
864 msgsz -= tlen;
865 user_msgp = (const char *)user_msgp + tlen;
866 next = msgmaps[next].next;
867 }
868 if (next != -1)
869 panic("didn't use all the msg segments");
870
871 /*
872 * We've got the message. Unlock the msqid_ds.
873 */
874
875 msqptr->msg_perm.mode &= ~MSG_LOCKED;
876
877 /*
878 * Make sure that the msqid_ds is still allocated.
879 */
880
881 if (msqptr->msg_qbytes == 0) {
882 msg_freehdr(msghdr);
883 wakeup(msqptr);
884 error = EIDRM;
885 goto done2;
886 }
887
888 /*
889 * Put the message into the queue
890 */
891
892 if (msqptr->msg_first == NULL) {
893 msqptr->msg_first = msghdr;
894 msqptr->msg_last = msghdr;
895 } else {
896 msqptr->msg_last->msg_next = msghdr;
897 msqptr->msg_last = msghdr;
898 }
899 msqptr->msg_last->msg_next = NULL;
900
901 msqptr->msg_cbytes += msghdr->msg_ts;
902 msqptr->msg_qnum++;
903 msqptr->msg_lspid = td->td_proc->p_pid;
904 msqptr->msg_stime = time (NULL);
905
906#ifdef __CYGWIN__
907 msg_info.msg_num++;
908 msg_info.msg_tot += uap->msgsz;
909#endif /* __CYGWIN__ */
910
911 wakeup(msqptr);
912 td->td_retval[0] = 0;
913done2:
914 mtx_unlock(&msq_mtx);
915 return (error);
916}
917
918#ifndef _SYS_SYSPROTO_H_
919struct msgrcv_args {
920 int msqid;
921 void *msgp;
922 size_t msgsz;
923 long msgtyp;
924 int msgflg;
925};
926#endif
927
928/*
929 * MPSAFE
930 */
931int
932msgrcv(struct thread *td, struct msgrcv_args *uap)
933{
934 int msqid = uap->msqid;
935 void *user_msgp = uap->msgp;
936 size_t msgsz = uap->msgsz;
937 long msgtyp = uap->msgtyp;
938 int msgflg = uap->msgflg;
939 size_t len;
940 register struct msqid_ds *msqptr;
941 register struct msg *msghdr;
942 int error = 0;
943 short next;
944
945 DPRINTF(("call to msgrcv(%d, 0x%x, %d, %ld, %d)\n", msqid, user_msgp,
946 msgsz, msgtyp, msgflg));
947
948 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
949 return (ENOSYS);
950
951 msqid = IPCID_TO_IX(msqid);
952
953 if (msqid < 0 || msqid >= msginfo.msgmni) {
954 DPRINTF(("msqid (%d) out of range (0<=msqid<%d)\n", msqid,
955 msginfo.msgmni));
956 return (EINVAL);
957 }
958
959 msqptr = &msqids[msqid];
960 mtx_lock(&msq_mtx);
961 if (msqptr->msg_qbytes == 0) {
962 DPRINTF(("no such message queue id\n"));
963 error = EINVAL;
964 goto done2;
965 }
966 if (msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
967 DPRINTF(("wrong sequence number\n"));
968 error = EINVAL;
969 goto done2;
970 }
971
972 if ((error = ipcperm(td, &msqptr->msg_perm, IPC_R))) {
973 DPRINTF(("requester doesn't have read access\n"));
974 goto done2;
975 }
976
977 msghdr = NULL;
978 while (msghdr == NULL) {
979 if (msgtyp == 0) {
980 msghdr = msqptr->msg_first;
981 if (msghdr != NULL) {
982 if (msgsz < msghdr->msg_ts &&
983 (msgflg & MSG_NOERROR) == 0) {
984 DPRINTF(("first message on the queue "
985 "is too big (want %d, got %d)\n",
986 msgsz, msghdr->msg_ts));
987 error = E2BIG;
988 goto done2;
989 }
990 if (msqptr->msg_first == msqptr->msg_last) {
991 msqptr->msg_first = NULL;
992 msqptr->msg_last = NULL;
993 } else {
994 msqptr->msg_first = msghdr->msg_next;
995 if (msqptr->msg_first == NULL)
996 panic("msg_first/last screwed up #1");
997 }
998 }
999 } else {
1000 struct msg *previous;
1001 struct msg **prev;
1002
1003 previous = NULL;
1004 prev = &(msqptr->msg_first);
1005 while ((msghdr = *prev) != NULL) {
1006 /*
1007 * Is this message's type an exact match or is
1008 * this message's type less than or equal to
1009 * the absolute value of a negative msgtyp?
1010 * Note that the second half of this test can
1011 * NEVER be true if msgtyp is positive since
1012 * msg_type is always positive!
1013 */
1014
1015 if (msgtyp == msghdr->msg_type ||
1016 msghdr->msg_type <= -msgtyp) {
1017 DPRINTF(("found message type %d, "
1018 "requested %d\n",
1019 msghdr->msg_type, msgtyp));
1020 if (msgsz < msghdr->msg_ts &&
1021 (msgflg & MSG_NOERROR) == 0) {
1022 DPRINTF(("requested message "
1023 "on the queue is too big "
1024 "(want %d, got %d)\n",
1025 msgsz, msghdr->msg_ts));
1026 error = E2BIG;
1027 goto done2;
1028 }
1029 *prev = msghdr->msg_next;
1030 if (msghdr == msqptr->msg_last) {
1031 if (previous == NULL) {
1032 if (prev !=
1033 &msqptr->msg_first)
1034 panic("msg_first/last screwed up #2");
1035 msqptr->msg_first =
1036 NULL;
1037 msqptr->msg_last =
1038 NULL;
1039 } else {
1040 if (prev ==
1041 &msqptr->msg_first)
1042 panic("msg_first/last screwed up #3");
1043 msqptr->msg_last =
1044 previous;
1045 }
1046 }
1047 break;
1048 }
1049 previous = msghdr;
1050 prev = &(msghdr->msg_next);
1051 }
1052 }
1053
1054 /*
1055 * We've either extracted the msghdr for the appropriate
1056 * message or there isn't one.
1057 * If there is one then bail out of this loop.
1058 */
1059
1060 if (msghdr != NULL)
1061 break;
1062
1063 /*
1064 * Hmph! No message found. Does the user want to wait?
1065 */
1066
1067 if ((msgflg & IPC_NOWAIT) != 0) {
1068 DPRINTF(("no appropriate message found (msgtyp=%d)\n",
1069 msgtyp));
1070 /* The SVID says to return ENOMSG. */
1071 error = ENOMSG;
1072 goto done2;
1073 }
1074
1075 /*
1076 * Wait for something to happen
1077 */
1078
1079 DPRINTF(("msgrcv: goodnight\n"));
1080 error = msleep(msqptr, &msq_mtx, (PZERO - 4) | PCATCH,
1081 "msgwait", 0);
1082 DPRINTF(("msgrcv: good morning (error=%d)\n", error));
1083
1084 if (error != 0) {
1085 DPRINTF(("msgsnd: interrupted system call\n"));
1086#ifdef __CYGWIN__
1087 if (error != EIDRM)
1088#endif /* __CYGWIN__ */
1089 error = EINTR;
1090 goto done2;
1091 }
1092
1093 /*
1094 * Make sure that the msq queue still exists
1095 */
1096
1097 if (msqptr->msg_qbytes == 0 ||
1098 msqptr->msg_perm.seq != IPCID_TO_SEQ(uap->msqid)) {
1099 DPRINTF(("msqid deleted\n"));
1100 error = EIDRM;
1101 goto done2;
1102 }
1103 }
1104
1105 /*
1106 * Return the message to the user.
1107 *
1108 * First, do the bookkeeping (before we risk being interrupted).
1109 */
1110
1111 msqptr->msg_cbytes -= msghdr->msg_ts;
1112 msqptr->msg_qnum--;
1113 msqptr->msg_lrpid = td->td_proc->p_pid;
1114 msqptr->msg_rtime = time (NULL);
1115
1116 /*
1117 * Make msgsz the actual amount that we'll be returning.
1118 * Note that this effectively truncates the message if it is too long
1119 * (since msgsz is never increased).
1120 */
1121
1122 DPRINTF(("found a message, msgsz=%d, msg_ts=%d\n", msgsz,
1123 msghdr->msg_ts));
1124 if (msgsz > msghdr->msg_ts)
1125 msgsz = msghdr->msg_ts;
1126
1127 /*
1128 * Return the type to the user.
1129 */
1130
1131 mtx_unlock(&msq_mtx);
1132 error = copyout(&(msghdr->msg_type), user_msgp,
1133 sizeof(msghdr->msg_type));
1134 mtx_lock(&msq_mtx);
1135 if (error != 0) {
1136 DPRINTF(("error (%d) copying out message type\n", error));
1137 msg_freehdr(msghdr);
1138 wakeup(msqptr);
1139 goto done2;
1140 }
1141 user_msgp = (char *)user_msgp + sizeof(msghdr->msg_type);
1142
1143 /*
1144 * Return the segments to the user
1145 */
1146
1147 next = msghdr->msg_spot;
1148 for (len = 0; len < msgsz; len += msginfo.msgssz) {
1149 size_t tlen;
1150
1151 if (msgsz - len > (unsigned long) msginfo.msgssz)
1152 tlen = msginfo.msgssz;
1153 else
1154 tlen = msgsz - len;
1155 if (next <= -1)
1156 panic("next too low #3");
1157 if (next >= msginfo.msgseg)
1158 panic("next out of range #3");
1159 mtx_unlock(&msq_mtx);
1160 error = copyout(&msgpool[next * msginfo.msgssz],
1161 user_msgp, tlen);
1162 mtx_lock(&msq_mtx);
1163 if (error != 0) {
1164 DPRINTF(("error (%d) copying out message segment\n",
1165 error));
1166 msg_freehdr(msghdr);
1167 wakeup(msqptr);
1168 goto done2;
1169 }
1170 user_msgp = (char *)user_msgp + tlen;
1171 next = msgmaps[next].next;
1172 }
1173
1174 /*
1175 * Done, return the actual number of bytes copied out.
1176 */
1177
1178#ifdef __CYGWIN__
1179 msg_info.msg_num--;
1180 msg_info.msg_tot -= msgsz;
1181#endif /* __CYGWIN__ */
1182
1183 msg_freehdr(msghdr);
1184 wakeup(msqptr);
1185 td->td_retval[0] = msgsz;
1186done2:
1187 mtx_unlock(&msq_mtx);
1188 return (error);
1189}
1190
1191#ifndef __CYGWIN__
1192static int
1193sysctl_msqids(SYSCTL_HANDLER_ARGS)
1194{
1195
1196 return (SYSCTL_OUT(req, msqids,
1197 sizeof(struct msqid_ds) * msginfo.msgmni));
1198}
1199
1200SYSCTL_DECL(_kern_ipc);
1201SYSCTL_INT(_kern_ipc, OID_AUTO, msgmax, CTLFLAG_RD, &msginfo.msgmax, 0, "");
1202SYSCTL_INT(_kern_ipc, OID_AUTO, msgmni, CTLFLAG_RDTUN, &msginfo.msgmni, 0, "");
1203SYSCTL_INT(_kern_ipc, OID_AUTO, msgmnb, CTLFLAG_RD, &msginfo.msgmnb, 0, "");
1204SYSCTL_INT(_kern_ipc, OID_AUTO, msgtql, CTLFLAG_RD, &msginfo.msgtql, 0, "");
1205SYSCTL_INT(_kern_ipc, OID_AUTO, msgssz, CTLFLAG_RDTUN, &msginfo.msgssz, 0, "");
1206SYSCTL_INT(_kern_ipc, OID_AUTO, msgseg, CTLFLAG_RDTUN, &msginfo.msgseg, 0, "");
1207SYSCTL_PROC(_kern_ipc, OID_AUTO, msqids, CTLFLAG_RD,
1208 NULL, 0, sysctl_msqids, "", "Message queue IDs");
1209#endif /* __CYGWIN__ */
1210#endif /* __OUTSIDE_CYGWIN__ */
This page took 0.146382 seconds and 5 git commands to generate.