2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Implementation of SVID messages
26 * Author: Daniel Boulet
28 * Copyright 1993 Daniel Boulet and RTMX Inc.
30 * This system call was implemented by Daniel Boulet under contract from RTMX.
32 * Redistribution and use in source forms, with and without modification,
33 * are permitted provided that this entire comment appears intact.
35 * Redistribution in binary form may occur without any restrictions.
36 * Obviously, it would be nice if you gave credit where credit is due
37 * but requiring it would be too onerous.
39 * This software is provided ``AS IS'' without any warranties of any kind.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/proc_internal.h>
46 #include <sys/kauth.h>
48 #include <sys/malloc.h>
49 #include <mach/mach_types.h>
51 #include <bsm/audit_kernel.h>
53 #include <sys/filedesc.h>
54 #include <sys/file_internal.h>
55 #include <sys/sysctl.h>
56 #include <sys/sysproto.h>
59 static void msginit(void *);
64 static void msg_freehdr(struct msg
*msghdr
);
66 typedef int sy_call_t(struct proc
*, void *, int *);
68 /* XXX casting to (sy_call_t *) is bogus, as usual. */
69 static sy_call_t
*msgcalls
[] = {
70 (sy_call_t
*)msgctl
, (sy_call_t
*)msgget
,
71 (sy_call_t
*)msgsnd
, (sy_call_t
*)msgrcv
74 static int nfree_msgmaps
; /* # of free map entries */
75 static short free_msgmaps
; /* free map entries list head */
76 static struct msg
*free_msghdrs
; /* list of free msg headers */
77 char *msgpool
; /* MSGMAX byte long msg buffer pool */
78 struct msgmap
*msgmaps
; /* MSGSEG msgmap structures */
79 struct msg
*msghdrs
; /* MSGTQL msg headers */
80 struct user_msqid_ds
*msqids
; /* MSGMNI user_msqid_ds struct's */
82 static lck_grp_t
*sysv_msg_subsys_lck_grp
;
83 static lck_grp_attr_t
*sysv_msg_subsys_lck_grp_attr
;
84 static lck_attr_t
*sysv_msg_subsys_lck_attr
;
85 static lck_mtx_t sysv_msg_subsys_mutex
;
87 #define SYSV_MSG_SUBSYS_LOCK() lck_mtx_lock(&sysv_msg_subsys_mutex)
88 #define SYSV_MSG_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_msg_subsys_mutex)
90 void sysv_msg_lock_init(void);
93 #ifdef __APPLE_API_PRIVATE
94 struct msginfo msginfo
= {
95 MSGMAX
, /* = (MSGSSZ*MSGSEG) : max chars in a message */
96 MSGMNI
, /* = 40 : max message queue identifiers */
97 MSGMNB
, /* = 2048 : max chars in a queue */
98 MSGTQL
, /* = 40 : max messages in system */
99 MSGSSZ
, /* = 8 : size of a message segment (2^N long) */
100 MSGSEG
/* = 2048 : number of message segments */
102 #endif /* __APPLE_API_PRIVATE */
104 /* Initialize the mutex governing access to the SysV msg subsystem */
105 __private_extern__
void
106 sysv_msg_lock_init( void )
108 sysv_msg_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
109 lck_grp_attr_setstat(sysv_msg_subsys_lck_grp_attr
);
111 sysv_msg_subsys_lck_grp
= lck_grp_alloc_init("sysv_msg_subsys_lock", sysv_msg_subsys_lck_grp_attr
);
113 sysv_msg_subsys_lck_attr
= lck_attr_alloc_init();
114 /* lck_attr_setdebug(sysv_msg_subsys_lck_attr); */
115 lck_mtx_init(&sysv_msg_subsys_mutex
, sysv_msg_subsys_lck_grp
, sysv_msg_subsys_lck_attr
);
118 static __inline__ user_time_t
127 * NOTE: Source and target may *NOT* overlap! (target is smaller)
130 msqid_ds_64to32(struct user_msqid_ds
*in
, struct msqid_ds
*out
)
132 out
->msg_perm
= in
->msg_perm
;
133 out
->msg_qnum
= in
->msg_qnum
;
134 out
->msg_cbytes
= in
->msg_cbytes
; /* for ipcs */
135 out
->msg_qbytes
= in
->msg_qbytes
;
136 out
->msg_lspid
= in
->msg_lspid
;
137 out
->msg_lrpid
= in
->msg_lrpid
;
138 out
->msg_stime
= in
->msg_stime
; /* XXX loss of range */
139 out
->msg_rtime
= in
->msg_rtime
; /* XXX loss of range */
140 out
->msg_ctime
= in
->msg_ctime
; /* XXX loss of range */
144 * NOTE: Source and target may are permitted to overlap! (source is smaller);
145 * this works because we copy fields in order from the end of the struct to
149 msqid_ds_32to64(struct msqid_ds
*in
, struct user_msqid_ds
*out
)
151 out
->msg_ctime
= in
->msg_ctime
;
152 out
->msg_rtime
= in
->msg_rtime
;
153 out
->msg_stime
= in
->msg_stime
;
154 out
->msg_lrpid
= in
->msg_lrpid
;
155 out
->msg_lspid
= in
->msg_lspid
;
156 out
->msg_qbytes
= in
->msg_qbytes
;
157 out
->msg_cbytes
= in
->msg_cbytes
; /* for ipcs */
158 out
->msg_qnum
= in
->msg_qnum
;
159 out
->msg_perm
= in
->msg_perm
;
162 /* This routine assumes the system is locked prior to calling this routine */
164 msginit(__unused
void *dummy
)
166 static int initted
= 0;
169 /* Lazy initialization on first system call; we don't have SYSINIT(). */
174 msgpool
= (char *)_MALLOC(msginfo
.msgmax
, M_SHM
, M_WAITOK
);
175 MALLOC(msgmaps
, struct msgmap
*,
176 sizeof(struct msgmap
) * msginfo
.msgseg
,
178 MALLOC(msghdrs
, struct msg
*,
179 sizeof(struct msg
) * msginfo
.msgtql
,
181 MALLOC(msqids
, struct user_msqid_ds
*,
182 sizeof(struct user_msqid_ds
) * msginfo
.msgmni
,
186 * msginfo.msgssz should be a power of two for efficiency reasons.
187 * It is also pretty silly if msginfo.msgssz is less than 8
188 * or greater than about 256 so ...
192 while (i
< 1024 && i
!= msginfo
.msgssz
)
194 if (i
!= msginfo
.msgssz
) {
195 printf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
197 panic("msginfo.msgssz not a small power of 2");
200 if (msginfo
.msgseg
> 32767) {
201 printf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
202 panic("msginfo.msgseg > 32767");
206 panic("msgmaps is NULL");
208 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
210 msgmaps
[i
-1].next
= i
;
211 msgmaps
[i
].next
= -1; /* implies entry is available */
214 nfree_msgmaps
= msginfo
.msgseg
;
217 panic("msghdrs is NULL");
219 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
220 msghdrs
[i
].msg_type
= 0;
222 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
223 msghdrs
[i
].msg_next
= NULL
;
225 free_msghdrs
= &msghdrs
[0];
228 panic("msqids is NULL");
230 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
231 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
232 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
237 * Entry point for all MSG calls
239 /* XXX actually varargs. */
241 msgsys(struct proc
*p
, struct msgsys_args
*uap
, register_t
*retval
)
243 if (uap
->which
>= sizeof(msgcalls
)/sizeof(msgcalls
[0]))
245 return ((*msgcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
249 msg_freehdr(struct msg
*msghdr
)
251 while (msghdr
->msg_ts
> 0) {
253 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
254 panic("msghdr->msg_spot out of range");
255 next
= msgmaps
[msghdr
->msg_spot
].next
;
256 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
257 free_msgmaps
= msghdr
->msg_spot
;
259 msghdr
->msg_spot
= next
;
260 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
261 msghdr
->msg_ts
-= msginfo
.msgssz
;
265 if (msghdr
->msg_spot
!= -1)
266 panic("msghdr->msg_spot != -1");
267 msghdr
->msg_next
= free_msghdrs
;
268 free_msghdrs
= msghdr
;
272 msgctl(struct proc
*p
, struct msgctl_args
*uap
, register_t
*retval
)
274 int msqid
= uap
->msqid
;
276 kauth_cred_t cred
= kauth_cred_get();
278 struct user_msqid_ds msqbuf
;
279 struct user_msqid_ds
*msqptr
;
280 struct user_msqid_ds umsds
;
282 SYSV_MSG_SUBSYS_LOCK();
287 printf("call to msgctl(%d, %d, 0x%qx)\n", msqid
, cmd
, uap
->buf
);
290 AUDIT_ARG(svipc_cmd
, cmd
);
291 AUDIT_ARG(svipc_id
, msqid
);
292 msqid
= IPCID_TO_IX(msqid
);
294 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
296 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
303 msqptr
= &msqids
[msqid
];
305 if (msqptr
->msg_qbytes
== 0) {
307 printf("no such msqid\n");
312 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
314 printf("wrong sequence number\n");
328 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_M
)))
331 /* Free the message headers */
332 msghdr
= msqptr
->msg_first
;
333 while (msghdr
!= NULL
) {
334 struct msg
*msghdr_tmp
;
336 /* Free the segments of each message */
337 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
340 msghdr
= msghdr
->msg_next
;
341 msg_freehdr(msghdr_tmp
);
344 if (msqptr
->msg_cbytes
!= 0)
345 panic("msg_cbytes is messed up");
346 if (msqptr
->msg_qnum
!= 0)
347 panic("msg_qnum is messed up");
349 msqptr
->msg_qbytes
= 0; /* Mark it as free */
351 wakeup((caddr_t
)msqptr
);
357 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_M
)))
360 SYSV_MSG_SUBSYS_UNLOCK();
362 if (IS_64BIT_PROCESS(p
)) {
363 eval
= copyin(uap
->buf
, &msqbuf
, sizeof(struct user_msqid_ds
));
365 eval
= copyin(uap
->buf
, &msqbuf
, sizeof(struct msqid_ds
));
366 /* convert in place; ugly, but safe */
367 msqid_ds_32to64((struct msqid_ds
*)&msqbuf
, &msqbuf
);
372 SYSV_MSG_SUBSYS_LOCK();
374 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
375 eval
= suser(cred
, &p
->p_acflag
);
381 /* compare (msglen_t) value against restrict (int) value */
382 if (msqbuf
.msg_qbytes
> (msglen_t
)msginfo
.msgmnb
) {
384 printf("can't increase msg_qbytes beyond %d (truncating)\n",
387 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
389 if (msqbuf
.msg_qbytes
== 0) {
391 printf("can't reduce msg_qbytes to 0\n");
396 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
397 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
398 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
399 (msqbuf
.msg_perm
.mode
& 0777);
400 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
401 msqptr
->msg_ctime
= sysv_msgtime();
405 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_R
))) {
407 printf("requester doesn't have read access\n");
412 bcopy(msqptr
, &umsds
, sizeof(struct user_msqid_ds
));
414 SYSV_MSG_SUBSYS_UNLOCK();
415 if (IS_64BIT_PROCESS(p
)) {
416 eval
= copyout(&umsds
, uap
->buf
, sizeof(struct user_msqid_ds
));
418 struct msqid_ds msqid_ds32
;
419 msqid_ds_64to32(&umsds
, &msqid_ds32
);
420 eval
= copyout(&msqid_ds32
, uap
->buf
, sizeof(struct msqid_ds
));
422 SYSV_MSG_SUBSYS_LOCK();
427 printf("invalid command %d\n", cmd
);
436 SYSV_MSG_SUBSYS_UNLOCK();
441 msgget(__unused
struct proc
*p
, struct msgget_args
*uap
, register_t
*retval
)
445 int msgflg
= uap
->msgflg
;
446 kauth_cred_t cred
= kauth_cred_get();
447 struct user_msqid_ds
*msqptr
= NULL
;
449 SYSV_MSG_SUBSYS_LOCK();
453 printf("msgget(0x%x, 0%o)\n", key
, msgflg
);
456 if (key
!= IPC_PRIVATE
) {
457 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
458 msqptr
= &msqids
[msqid
];
459 if (msqptr
->msg_qbytes
!= 0 &&
460 msqptr
->msg_perm
.key
== key
)
463 if (msqid
< msginfo
.msgmni
) {
465 printf("found public key\n");
467 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
469 printf("not exclusive\n");
474 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
476 printf("requester doesn't have 0%o access\n",
486 printf("need to allocate the user_msqid_ds\n");
488 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
489 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
491 * Look for an unallocated and unlocked user_msqid_ds.
492 * user_msqid_ds's can be locked by msgsnd or msgrcv
493 * while they are copying the message in/out. We
494 * can't re-use the entry until they release it.
496 msqptr
= &msqids
[msqid
];
497 if (msqptr
->msg_qbytes
== 0 &&
498 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
501 if (msqid
== msginfo
.msgmni
) {
503 printf("no more user_msqid_ds's available\n");
509 printf("msqid %d is available\n", msqid
);
511 msqptr
->msg_perm
.key
= key
;
512 msqptr
->msg_perm
.cuid
= kauth_cred_getuid(cred
);
513 msqptr
->msg_perm
.uid
= kauth_cred_getuid(cred
);
514 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
515 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
516 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
517 /* Make sure that the returned msqid is unique */
518 msqptr
->msg_perm
.seq
++;
519 msqptr
->msg_first
= NULL
;
520 msqptr
->msg_last
= NULL
;
521 msqptr
->msg_cbytes
= 0;
522 msqptr
->msg_qnum
= 0;
523 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
524 msqptr
->msg_lspid
= 0;
525 msqptr
->msg_lrpid
= 0;
526 msqptr
->msg_stime
= 0;
527 msqptr
->msg_rtime
= 0;
528 msqptr
->msg_ctime
= sysv_msgtime();
531 printf("didn't find it and wasn't asked to create it\n");
538 /* Construct the unique msqid */
539 *retval
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
540 AUDIT_ARG(svipc_id
, *retval
);
543 SYSV_MSG_SUBSYS_UNLOCK();
549 msgsnd(struct proc
*p
, struct msgsnd_args
*uap
, register_t
*retval
)
551 int msqid
= uap
->msqid
;
552 user_addr_t user_msgp
= uap
->msgp
;
553 size_t msgsz
= (size_t)uap
->msgsz
; /* limit to 4G */
554 int msgflg
= uap
->msgflg
;
555 int segs_needed
, eval
;
556 struct user_msqid_ds
*msqptr
;
562 SYSV_MSG_SUBSYS_LOCK();
566 printf("call to msgsnd(%d, 0x%qx, %d, %d)\n", msqid
, user_msgp
, msgsz
,
570 AUDIT_ARG(svipc_id
, msqid
);
571 msqid
= IPCID_TO_IX(msqid
);
573 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
575 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
582 msqptr
= &msqids
[msqid
];
583 if (msqptr
->msg_qbytes
== 0) {
585 printf("no such message queue id\n");
590 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
592 printf("wrong sequence number\n");
598 if ((eval
= ipcperm(kauth_cred_get(), &msqptr
->msg_perm
, IPC_W
))) {
600 printf("requester doesn't have write access\n");
605 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
607 printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
611 int need_more_resources
= 0;
615 * (inside this loop in case msg_qbytes changes while we sleep)
618 if (msgsz
> msqptr
->msg_qbytes
) {
620 printf("msgsz > msqptr->msg_qbytes\n");
626 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
628 printf("msqid is locked\n");
630 need_more_resources
= 1;
632 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
634 printf("msgsz + msg_cbytes > msg_qbytes\n");
636 need_more_resources
= 1;
638 if (segs_needed
> nfree_msgmaps
) {
640 printf("segs_needed > nfree_msgmaps\n");
642 need_more_resources
= 1;
644 if (free_msghdrs
== NULL
) {
646 printf("no more msghdrs\n");
648 need_more_resources
= 1;
651 if (need_more_resources
) {
654 if ((msgflg
& IPC_NOWAIT
) != 0) {
656 printf("need more resources but caller doesn't want to wait\n");
662 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
664 printf("we don't own the user_msqid_ds\n");
668 /* Force later arrivals to wait for our
671 printf("we own the user_msqid_ds\n");
673 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
677 printf("goodnight\n");
679 eval
= msleep((caddr_t
)msqptr
, &sysv_msg_subsys_mutex
, (PZERO
- 4) | PCATCH
,
682 printf("good morning, eval=%d\n", eval
);
685 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
688 printf("msgsnd: interrupted system call\n");
695 * Make sure that the msq queue still exists
698 if (msqptr
->msg_qbytes
== 0) {
700 printf("msqid deleted\n");
702 /* The SVID says to return EIDRM. */
706 /* Unfortunately, BSD doesn't define that code
716 printf("got all the resources that we need\n");
723 * We have the resources that we need.
727 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
728 panic("msg_perm.mode & MSG_LOCKED");
729 if (segs_needed
> nfree_msgmaps
)
730 panic("segs_needed > nfree_msgmaps");
731 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
732 panic("msgsz + msg_cbytes > msg_qbytes");
733 if (free_msghdrs
== NULL
)
734 panic("no more msghdrs");
737 * Re-lock the user_msqid_ds in case we page-fault when copying in
741 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
742 panic("user_msqid_ds is already locked");
743 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
746 * Allocate a message header
749 msghdr
= free_msghdrs
;
750 free_msghdrs
= msghdr
->msg_next
;
751 msghdr
->msg_spot
= -1;
752 msghdr
->msg_ts
= msgsz
;
755 * Allocate space for the message
758 while (segs_needed
> 0) {
759 if (nfree_msgmaps
<= 0)
760 panic("not enough msgmaps");
761 if (free_msgmaps
== -1)
762 panic("nil free_msgmaps");
765 panic("next too low #1");
766 if (next
>= msginfo
.msgseg
)
767 panic("next out of range #1");
769 printf("allocating segment %d to message\n", next
);
771 free_msgmaps
= msgmaps
[next
].next
;
773 msgmaps
[next
].next
= msghdr
->msg_spot
;
774 msghdr
->msg_spot
= next
;
779 * Copy in the message type. For a 64 bit process, this is 64 bits,
780 * but we only ever use the low 32 bits, so the cast is OK.
782 if (IS_64BIT_PROCESS(p
)) {
783 SYSV_MSG_SUBSYS_UNLOCK();
784 eval
= copyin(user_msgp
, &msgtype
, sizeof(msgtype
));
785 SYSV_MSG_SUBSYS_LOCK();
786 msghdr
->msg_type
= CAST_DOWN(long,msgtype
);
787 user_msgp
= user_msgp
+ sizeof(msgtype
); /* ptr math */
789 SYSV_MSG_SUBSYS_UNLOCK();
790 eval
= copyin(user_msgp
, &msghdr
->msg_type
, sizeof(long));
791 SYSV_MSG_SUBSYS_LOCK();
792 user_msgp
= user_msgp
+ sizeof(long); /* ptr math */
797 printf("error %d copying the message type\n", eval
);
800 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
801 wakeup((caddr_t
)msqptr
);
807 * Validate the message type
809 if (msghdr
->msg_type
< 1) {
811 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
812 wakeup((caddr_t
)msqptr
);
814 printf("mtype (%d) < 1\n", msghdr
->msg_type
);
821 * Copy in the message body
823 next
= msghdr
->msg_spot
;
826 /* compare input (size_t) value against restrict (int) value */
827 if (msgsz
> (size_t)msginfo
.msgssz
)
828 tlen
= msginfo
.msgssz
;
832 panic("next too low #2");
833 if (next
>= msginfo
.msgseg
)
834 panic("next out of range #2");
836 SYSV_MSG_SUBSYS_UNLOCK();
837 eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
], tlen
);
838 SYSV_MSG_SUBSYS_LOCK();
842 printf("error %d copying in message segment\n", eval
);
845 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
846 wakeup((caddr_t
)msqptr
);
851 user_msgp
= user_msgp
+ tlen
; /* ptr math */
852 next
= msgmaps
[next
].next
;
855 panic("didn't use all the msg segments");
858 * We've got the message. Unlock the user_msqid_ds.
861 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
864 * Make sure that the user_msqid_ds is still allocated.
867 if (msqptr
->msg_qbytes
== 0) {
869 wakeup((caddr_t
)msqptr
);
870 /* The SVID says to return EIDRM. */
874 /* Unfortunately, BSD doesn't define that code yet! */
881 * Put the message into the queue
884 if (msqptr
->msg_first
== NULL
) {
885 msqptr
->msg_first
= msghdr
;
886 msqptr
->msg_last
= msghdr
;
888 msqptr
->msg_last
->msg_next
= msghdr
;
889 msqptr
->msg_last
= msghdr
;
891 msqptr
->msg_last
->msg_next
= NULL
;
893 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
895 msqptr
->msg_lspid
= p
->p_pid
;
896 msqptr
->msg_stime
= sysv_msgtime();
898 wakeup((caddr_t
)msqptr
);
903 SYSV_MSG_SUBSYS_UNLOCK();
909 msgrcv(struct proc
*p
, struct msgrcv_args
*uap
, user_ssize_t
*retval
)
911 int msqid
= uap
->msqid
;
912 user_addr_t user_msgp
= uap
->msgp
;
913 size_t msgsz
= (size_t)uap
->msgsz
; /* limit to 4G */
914 long msgtyp
= (long)uap
->msgtyp
; /* limit to 32 bits */
915 int msgflg
= uap
->msgflg
;
917 struct user_msqid_ds
*msqptr
;
924 SYSV_MSG_SUBSYS_LOCK();
928 printf("call to msgrcv(%d, 0x%qx, %d, %ld, %d)\n", msqid
, user_msgp
,
929 msgsz
, msgtyp
, msgflg
);
932 AUDIT_ARG(svipc_id
, msqid
);
933 msqid
= IPCID_TO_IX(msqid
);
935 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
937 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
944 msqptr
= &msqids
[msqid
];
945 if (msqptr
->msg_qbytes
== 0) {
947 printf("no such message queue id\n");
952 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
954 printf("wrong sequence number\n");
960 if ((eval
= ipcperm(kauth_cred_get(), &msqptr
->msg_perm
, IPC_R
))) {
962 printf("requester doesn't have read access\n");
968 while (msghdr
== NULL
) {
970 msghdr
= msqptr
->msg_first
;
971 if (msghdr
!= NULL
) {
972 if (msgsz
< msghdr
->msg_ts
&&
973 (msgflg
& MSG_NOERROR
) == 0) {
975 printf("first message on the queue is too big (want %d, got %d)\n",
976 msgsz
, msghdr
->msg_ts
);
981 if (msqptr
->msg_first
== msqptr
->msg_last
) {
982 msqptr
->msg_first
= NULL
;
983 msqptr
->msg_last
= NULL
;
985 msqptr
->msg_first
= msghdr
->msg_next
;
986 if (msqptr
->msg_first
== NULL
)
987 panic("msg_first/last messed up #1");
991 struct msg
*previous
;
995 prev
= &(msqptr
->msg_first
);
996 while ((msghdr
= *prev
) != NULL
) {
998 * Is this message's type an exact match or is
999 * this message's type less than or equal to
1000 * the absolute value of a negative msgtyp?
1001 * Note that the second half of this test can
1002 * NEVER be true if msgtyp is positive since
1003 * msg_type is always positive!
1006 if (msgtyp
== msghdr
->msg_type
||
1007 msghdr
->msg_type
<= -msgtyp
) {
1009 printf("found message type %d, requested %d\n",
1010 msghdr
->msg_type
, msgtyp
);
1012 if (msgsz
< msghdr
->msg_ts
&&
1013 (msgflg
& MSG_NOERROR
) == 0) {
1015 printf("requested message on the queue is too big (want %d, got %d)\n",
1016 msgsz
, msghdr
->msg_ts
);
1021 *prev
= msghdr
->msg_next
;
1022 if (msghdr
== msqptr
->msg_last
) {
1023 if (previous
== NULL
) {
1026 panic("msg_first/last messed up #2");
1034 panic("msg_first/last messed up #3");
1042 prev
= &(msghdr
->msg_next
);
1047 * We've either extracted the msghdr for the appropriate
1048 * message or there isn't one.
1049 * If there is one then bail out of this loop.
1056 * Hmph! No message found. Does the user want to wait?
1059 if ((msgflg
& IPC_NOWAIT
) != 0) {
1061 printf("no appropriate message found (msgtyp=%d)\n",
1064 /* The SVID says to return ENOMSG. */
1068 /* Unfortunately, BSD doesn't define that code yet! */
1075 * Wait for something to happen
1079 printf("msgrcv: goodnight\n");
1081 eval
= msleep((caddr_t
)msqptr
, &sysv_msg_subsys_mutex
, (PZERO
- 4) | PCATCH
, "msgwait",
1084 printf("msgrcv: good morning (eval=%d)\n", eval
);
1089 printf("msgsnd: interrupted system call\n");
1096 * Make sure that the msq queue still exists
1099 if (msqptr
->msg_qbytes
== 0 ||
1100 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
1102 printf("msqid deleted\n");
1104 /* The SVID says to return EIDRM. */
1108 /* Unfortunately, BSD doesn't define that code yet! */
1116 * Return the message to the user.
1118 * First, do the bookkeeping (before we risk being interrupted).
1121 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
1123 msqptr
->msg_lrpid
= p
->p_pid
;
1124 msqptr
->msg_rtime
= sysv_msgtime();
1127 * Make msgsz the actual amount that we'll be returning.
1128 * Note that this effectively truncates the message if it is too long
1129 * (since msgsz is never increased).
1133 printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
1136 if (msgsz
> msghdr
->msg_ts
)
1137 msgsz
= msghdr
->msg_ts
;
1140 * Return the type to the user.
1144 * Copy out the message type. For a 64 bit process, this is 64 bits,
1145 * but we only ever use the low 32 bits, so the cast is OK.
1147 if (IS_64BIT_PROCESS(p
)) {
1148 msgtype
= msghdr
->msg_type
;
1149 SYSV_MSG_SUBSYS_UNLOCK();
1150 eval
= copyout(&msgtype
, user_msgp
, sizeof(msgtype
));
1151 SYSV_MSG_SUBSYS_LOCK();
1152 user_msgp
= user_msgp
+ sizeof(msgtype
); /* ptr math */
1154 msg_type_long
= msghdr
->msg_type
;
1155 SYSV_MSG_SUBSYS_UNLOCK();
1156 eval
= copyout(&msg_type_long
, user_msgp
, sizeof(long));
1157 SYSV_MSG_SUBSYS_LOCK();
1158 user_msgp
= user_msgp
+ sizeof(long); /* ptr math */
1163 printf("error (%d) copying out message type\n", eval
);
1165 msg_freehdr(msghdr
);
1166 wakeup((caddr_t
)msqptr
);
1173 * Return the segments to the user
1176 next
= msghdr
->msg_spot
;
1177 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1180 /* compare input (size_t) value against restrict (int) value */
1181 if (msgsz
> (size_t)msginfo
.msgssz
)
1182 tlen
= msginfo
.msgssz
;
1186 panic("next too low #3");
1187 if (next
>= msginfo
.msgseg
)
1188 panic("next out of range #3");
1189 SYSV_MSG_SUBSYS_UNLOCK();
1190 eval
= copyout(&msgpool
[next
* msginfo
.msgssz
],
1192 SYSV_MSG_SUBSYS_LOCK();
1195 printf("error (%d) copying out message segment\n",
1198 msg_freehdr(msghdr
);
1199 wakeup((caddr_t
)msqptr
);
1202 user_msgp
= user_msgp
+ tlen
; /* ptr math */
1203 next
= msgmaps
[next
].next
;
1207 * Done, return the actual number of bytes copied out.
1210 msg_freehdr(msghdr
);
1211 wakeup((caddr_t
)msqptr
);
1215 SYSV_MSG_SUBSYS_UNLOCK();
1220 IPCS_msg_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
1221 __unused
int arg2
, struct sysctl_req
*req
)
1226 struct IPCS_command u32
;
1227 struct user_IPCS_command u64
;
1229 struct msqid_ds msqid_ds32
; /* post conversion, 32 bit version */
1231 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
1232 size_t msqid_ds_sz
= sizeof(struct user_msqid_ds
);
1233 struct proc
*p
= current_proc();
1235 if (!IS_64BIT_PROCESS(p
)) {
1236 ipcs_sz
= sizeof(struct IPCS_command
);
1237 msqid_ds_sz
= sizeof(struct msqid_ds
);
1240 /* Copy in the command structure */
1241 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
1245 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
1246 ipcs
.u64
.ipcs_data
= CAST_USER_ADDR_T(ipcs
.u32
.ipcs_data
);
1248 /* Let us version this interface... */
1249 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
1253 SYSV_MSG_SUBSYS_LOCK();
1255 switch(ipcs
.u64
.ipcs_op
) {
1256 case IPCS_MSG_CONF
: /* Obtain global configuration data */
1257 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct msginfo
)) {
1261 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
1265 SYSV_MSG_SUBSYS_UNLOCK();
1266 error
= copyout(&msginfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1267 SYSV_MSG_SUBSYS_LOCK();
1270 case IPCS_MSG_ITER
: /* Iterate over existing segments */
1271 /* Not done up top so we can set limits via sysctl (later) */
1274 cursor
= ipcs
.u64
.ipcs_cursor
;
1275 if (cursor
< 0 || cursor
>= msginfo
.msgmni
) {
1279 if (ipcs
.u64
.ipcs_datalen
!= (int)msqid_ds_sz
) {
1283 for( ; cursor
< msginfo
.msgmni
; cursor
++) {
1284 if (msqids
[cursor
].msg_qbytes
!= 0) /* allocated */
1288 if (cursor
== msginfo
.msgmni
) {
1293 msqid_dsp
= &msqids
[cursor
]; /* default: 64 bit */
1296 * If necessary, convert the 64 bit kernel segment
1297 * descriptor to a 32 bit user one.
1299 if (!IS_64BIT_PROCESS(p
)) {
1300 msqid_ds_64to32(msqid_dsp
, &msqid_ds32
);
1301 msqid_dsp
= &msqid_ds32
;
1303 SYSV_MSG_SUBSYS_UNLOCK();
1304 error
= copyout(msqid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1307 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
1309 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
1310 ipcs
.u32
.ipcs_data
= CAST_DOWN(void *,ipcs
.u64
.ipcs_data
);
1311 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
1313 SYSV_MSG_SUBSYS_LOCK();
1321 SYSV_MSG_SUBSYS_UNLOCK();
1325 SYSCTL_DECL(_kern_sysv_ipcs
);
1326 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, msg
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1327 0, 0, IPCS_msg_sysctl
,
1328 "S,IPCS_msg_command",
1329 "ipcs msg command interface");