2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Implementation of SVID messages
33 * Author: Daniel Boulet
35 * Copyright 1993 Daniel Boulet and RTMX Inc.
37 * This system call was implemented by Daniel Boulet under contract from RTMX.
39 * Redistribution and use in source forms, with and without modification,
40 * are permitted provided that this entire comment appears intact.
42 * Redistribution in binary form may occur without any restrictions.
43 * Obviously, it would be nice if you gave credit where credit is due
44 * but requiring it would be too onerous.
46 * This software is provided ``AS IS'' without any warranties of any kind.
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/proc_internal.h>
53 #include <sys/kauth.h>
55 #include <sys/malloc.h>
56 #include <mach/mach_types.h>
58 #include <bsm/audit_kernel.h>
60 #include <sys/filedesc.h>
61 #include <sys/file_internal.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysproto.h>
66 static void msginit(void *);
71 static void msg_freehdr(struct msg
*msghdr
);
73 typedef int sy_call_t(struct proc
*, void *, int *);
75 /* XXX casting to (sy_call_t *) is bogus, as usual. */
76 static sy_call_t
*msgcalls
[] = {
77 (sy_call_t
*)msgctl
, (sy_call_t
*)msgget
,
78 (sy_call_t
*)msgsnd
, (sy_call_t
*)msgrcv
81 static int nfree_msgmaps
; /* # of free map entries */
82 static short free_msgmaps
; /* free map entries list head */
83 static struct msg
*free_msghdrs
; /* list of free msg headers */
84 char *msgpool
; /* MSGMAX byte long msg buffer pool */
85 struct msgmap
*msgmaps
; /* MSGSEG msgmap structures */
86 struct msg
*msghdrs
; /* MSGTQL msg headers */
87 struct user_msqid_ds
*msqids
; /* MSGMNI user_msqid_ds struct's */
89 static lck_grp_t
*sysv_msg_subsys_lck_grp
;
90 static lck_grp_attr_t
*sysv_msg_subsys_lck_grp_attr
;
91 static lck_attr_t
*sysv_msg_subsys_lck_attr
;
92 static lck_mtx_t sysv_msg_subsys_mutex
;
94 #define SYSV_MSG_SUBSYS_LOCK() lck_mtx_lock(&sysv_msg_subsys_mutex)
95 #define SYSV_MSG_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_msg_subsys_mutex)
97 void sysv_msg_lock_init(void);
100 #ifdef __APPLE_API_PRIVATE
101 struct msginfo msginfo
= {
102 MSGMAX
, /* = (MSGSSZ*MSGSEG) : max chars in a message */
103 MSGMNI
, /* = 40 : max message queue identifiers */
104 MSGMNB
, /* = 2048 : max chars in a queue */
105 MSGTQL
, /* = 40 : max messages in system */
106 MSGSSZ
, /* = 8 : size of a message segment (2^N long) */
107 MSGSEG
/* = 2048 : number of message segments */
109 #endif /* __APPLE_API_PRIVATE */
111 /* Initialize the mutex governing access to the SysV msg subsystem */
112 __private_extern__
void
113 sysv_msg_lock_init( void )
115 sysv_msg_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
116 lck_grp_attr_setstat(sysv_msg_subsys_lck_grp_attr
);
118 sysv_msg_subsys_lck_grp
= lck_grp_alloc_init("sysv_msg_subsys_lock", sysv_msg_subsys_lck_grp_attr
);
120 sysv_msg_subsys_lck_attr
= lck_attr_alloc_init();
121 /* lck_attr_setdebug(sysv_msg_subsys_lck_attr); */
122 lck_mtx_init(&sysv_msg_subsys_mutex
, sysv_msg_subsys_lck_grp
, sysv_msg_subsys_lck_attr
);
125 static __inline__ user_time_t
134 * NOTE: Source and target may *NOT* overlap! (target is smaller)
137 msqid_ds_64to32(struct user_msqid_ds
*in
, struct msqid_ds
*out
)
139 out
->msg_perm
= in
->msg_perm
;
140 out
->msg_qnum
= in
->msg_qnum
;
141 out
->msg_cbytes
= in
->msg_cbytes
; /* for ipcs */
142 out
->msg_qbytes
= in
->msg_qbytes
;
143 out
->msg_lspid
= in
->msg_lspid
;
144 out
->msg_lrpid
= in
->msg_lrpid
;
145 out
->msg_stime
= in
->msg_stime
; /* XXX loss of range */
146 out
->msg_rtime
= in
->msg_rtime
; /* XXX loss of range */
147 out
->msg_ctime
= in
->msg_ctime
; /* XXX loss of range */
151 * NOTE: Source and target may are permitted to overlap! (source is smaller);
152 * this works because we copy fields in order from the end of the struct to
156 msqid_ds_32to64(struct msqid_ds
*in
, struct user_msqid_ds
*out
)
158 out
->msg_ctime
= in
->msg_ctime
;
159 out
->msg_rtime
= in
->msg_rtime
;
160 out
->msg_stime
= in
->msg_stime
;
161 out
->msg_lrpid
= in
->msg_lrpid
;
162 out
->msg_lspid
= in
->msg_lspid
;
163 out
->msg_qbytes
= in
->msg_qbytes
;
164 out
->msg_cbytes
= in
->msg_cbytes
; /* for ipcs */
165 out
->msg_qnum
= in
->msg_qnum
;
166 out
->msg_perm
= in
->msg_perm
;
169 /* This routine assumes the system is locked prior to calling this routine */
171 msginit(__unused
void *dummy
)
173 static int initted
= 0;
176 /* Lazy initialization on first system call; we don't have SYSINIT(). */
181 msgpool
= (char *)_MALLOC(msginfo
.msgmax
, M_SHM
, M_WAITOK
);
182 MALLOC(msgmaps
, struct msgmap
*,
183 sizeof(struct msgmap
) * msginfo
.msgseg
,
185 MALLOC(msghdrs
, struct msg
*,
186 sizeof(struct msg
) * msginfo
.msgtql
,
188 MALLOC(msqids
, struct user_msqid_ds
*,
189 sizeof(struct user_msqid_ds
) * msginfo
.msgmni
,
193 * msginfo.msgssz should be a power of two for efficiency reasons.
194 * It is also pretty silly if msginfo.msgssz is less than 8
195 * or greater than about 256 so ...
199 while (i
< 1024 && i
!= msginfo
.msgssz
)
201 if (i
!= msginfo
.msgssz
) {
202 printf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
204 panic("msginfo.msgssz not a small power of 2");
207 if (msginfo
.msgseg
> 32767) {
208 printf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
209 panic("msginfo.msgseg > 32767");
213 panic("msgmaps is NULL");
215 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
217 msgmaps
[i
-1].next
= i
;
218 msgmaps
[i
].next
= -1; /* implies entry is available */
221 nfree_msgmaps
= msginfo
.msgseg
;
224 panic("msghdrs is NULL");
226 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
227 msghdrs
[i
].msg_type
= 0;
229 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
230 msghdrs
[i
].msg_next
= NULL
;
232 free_msghdrs
= &msghdrs
[0];
235 panic("msqids is NULL");
237 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
238 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
239 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
244 * Entry point for all MSG calls
246 /* XXX actually varargs. */
248 msgsys(struct proc
*p
, struct msgsys_args
*uap
, register_t
*retval
)
250 if (uap
->which
>= sizeof(msgcalls
)/sizeof(msgcalls
[0]))
252 return ((*msgcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
256 msg_freehdr(struct msg
*msghdr
)
258 while (msghdr
->msg_ts
> 0) {
260 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
261 panic("msghdr->msg_spot out of range");
262 next
= msgmaps
[msghdr
->msg_spot
].next
;
263 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
264 free_msgmaps
= msghdr
->msg_spot
;
266 msghdr
->msg_spot
= next
;
267 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
268 msghdr
->msg_ts
-= msginfo
.msgssz
;
272 if (msghdr
->msg_spot
!= -1)
273 panic("msghdr->msg_spot != -1");
274 msghdr
->msg_next
= free_msghdrs
;
275 free_msghdrs
= msghdr
;
279 msgctl(struct proc
*p
, struct msgctl_args
*uap
, register_t
*retval
)
281 int msqid
= uap
->msqid
;
283 kauth_cred_t cred
= kauth_cred_get();
285 struct user_msqid_ds msqbuf
;
286 struct user_msqid_ds
*msqptr
;
287 struct user_msqid_ds umsds
;
289 SYSV_MSG_SUBSYS_LOCK();
294 printf("call to msgctl(%d, %d, 0x%qx)\n", msqid
, cmd
, uap
->buf
);
297 AUDIT_ARG(svipc_cmd
, cmd
);
298 AUDIT_ARG(svipc_id
, msqid
);
299 msqid
= IPCID_TO_IX(msqid
);
301 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
303 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
310 msqptr
= &msqids
[msqid
];
312 if (msqptr
->msg_qbytes
== 0) {
314 printf("no such msqid\n");
319 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
321 printf("wrong sequence number\n");
335 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_M
)))
338 /* Free the message headers */
339 msghdr
= msqptr
->msg_first
;
340 while (msghdr
!= NULL
) {
341 struct msg
*msghdr_tmp
;
343 /* Free the segments of each message */
344 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
347 msghdr
= msghdr
->msg_next
;
348 msg_freehdr(msghdr_tmp
);
351 if (msqptr
->msg_cbytes
!= 0)
352 panic("msg_cbytes is messed up");
353 if (msqptr
->msg_qnum
!= 0)
354 panic("msg_qnum is messed up");
356 msqptr
->msg_qbytes
= 0; /* Mark it as free */
358 wakeup((caddr_t
)msqptr
);
364 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_M
)))
367 SYSV_MSG_SUBSYS_UNLOCK();
369 if (IS_64BIT_PROCESS(p
)) {
370 eval
= copyin(uap
->buf
, &msqbuf
, sizeof(struct user_msqid_ds
));
372 eval
= copyin(uap
->buf
, &msqbuf
, sizeof(struct msqid_ds
));
373 /* convert in place; ugly, but safe */
374 msqid_ds_32to64((struct msqid_ds
*)&msqbuf
, &msqbuf
);
379 SYSV_MSG_SUBSYS_LOCK();
381 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
382 eval
= suser(cred
, &p
->p_acflag
);
388 /* compare (msglen_t) value against restrict (int) value */
389 if (msqbuf
.msg_qbytes
> (msglen_t
)msginfo
.msgmnb
) {
391 printf("can't increase msg_qbytes beyond %d (truncating)\n",
394 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
396 if (msqbuf
.msg_qbytes
== 0) {
398 printf("can't reduce msg_qbytes to 0\n");
403 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
404 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
405 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
406 (msqbuf
.msg_perm
.mode
& 0777);
407 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
408 msqptr
->msg_ctime
= sysv_msgtime();
412 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_R
))) {
414 printf("requester doesn't have read access\n");
419 bcopy(msqptr
, &umsds
, sizeof(struct user_msqid_ds
));
421 SYSV_MSG_SUBSYS_UNLOCK();
422 if (IS_64BIT_PROCESS(p
)) {
423 eval
= copyout(&umsds
, uap
->buf
, sizeof(struct user_msqid_ds
));
425 struct msqid_ds msqid_ds32
;
426 msqid_ds_64to32(&umsds
, &msqid_ds32
);
427 eval
= copyout(&msqid_ds32
, uap
->buf
, sizeof(struct msqid_ds
));
429 SYSV_MSG_SUBSYS_LOCK();
434 printf("invalid command %d\n", cmd
);
443 SYSV_MSG_SUBSYS_UNLOCK();
448 msgget(__unused
struct proc
*p
, struct msgget_args
*uap
, register_t
*retval
)
452 int msgflg
= uap
->msgflg
;
453 kauth_cred_t cred
= kauth_cred_get();
454 struct user_msqid_ds
*msqptr
= NULL
;
456 SYSV_MSG_SUBSYS_LOCK();
460 printf("msgget(0x%x, 0%o)\n", key
, msgflg
);
463 if (key
!= IPC_PRIVATE
) {
464 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
465 msqptr
= &msqids
[msqid
];
466 if (msqptr
->msg_qbytes
!= 0 &&
467 msqptr
->msg_perm
.key
== key
)
470 if (msqid
< msginfo
.msgmni
) {
472 printf("found public key\n");
474 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
476 printf("not exclusive\n");
481 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
483 printf("requester doesn't have 0%o access\n",
493 printf("need to allocate the user_msqid_ds\n");
495 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
496 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
498 * Look for an unallocated and unlocked user_msqid_ds.
499 * user_msqid_ds's can be locked by msgsnd or msgrcv
500 * while they are copying the message in/out. We
501 * can't re-use the entry until they release it.
503 msqptr
= &msqids
[msqid
];
504 if (msqptr
->msg_qbytes
== 0 &&
505 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
508 if (msqid
== msginfo
.msgmni
) {
510 printf("no more user_msqid_ds's available\n");
516 printf("msqid %d is available\n", msqid
);
518 msqptr
->msg_perm
.key
= key
;
519 msqptr
->msg_perm
.cuid
= kauth_cred_getuid(cred
);
520 msqptr
->msg_perm
.uid
= kauth_cred_getuid(cred
);
521 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
522 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
523 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
524 /* Make sure that the returned msqid is unique */
525 msqptr
->msg_perm
.seq
++;
526 msqptr
->msg_first
= NULL
;
527 msqptr
->msg_last
= NULL
;
528 msqptr
->msg_cbytes
= 0;
529 msqptr
->msg_qnum
= 0;
530 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
531 msqptr
->msg_lspid
= 0;
532 msqptr
->msg_lrpid
= 0;
533 msqptr
->msg_stime
= 0;
534 msqptr
->msg_rtime
= 0;
535 msqptr
->msg_ctime
= sysv_msgtime();
538 printf("didn't find it and wasn't asked to create it\n");
545 /* Construct the unique msqid */
546 *retval
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
547 AUDIT_ARG(svipc_id
, *retval
);
550 SYSV_MSG_SUBSYS_UNLOCK();
556 msgsnd(struct proc
*p
, struct msgsnd_args
*uap
, register_t
*retval
)
558 int msqid
= uap
->msqid
;
559 user_addr_t user_msgp
= uap
->msgp
;
560 size_t msgsz
= (size_t)uap
->msgsz
; /* limit to 4G */
561 int msgflg
= uap
->msgflg
;
562 int segs_needed
, eval
;
563 struct user_msqid_ds
*msqptr
;
569 SYSV_MSG_SUBSYS_LOCK();
573 printf("call to msgsnd(%d, 0x%qx, %d, %d)\n", msqid
, user_msgp
, msgsz
,
577 AUDIT_ARG(svipc_id
, msqid
);
578 msqid
= IPCID_TO_IX(msqid
);
580 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
582 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
589 msqptr
= &msqids
[msqid
];
590 if (msqptr
->msg_qbytes
== 0) {
592 printf("no such message queue id\n");
597 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
599 printf("wrong sequence number\n");
605 if ((eval
= ipcperm(kauth_cred_get(), &msqptr
->msg_perm
, IPC_W
))) {
607 printf("requester doesn't have write access\n");
612 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
614 printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
618 int need_more_resources
= 0;
622 * (inside this loop in case msg_qbytes changes while we sleep)
625 if (msgsz
> msqptr
->msg_qbytes
) {
627 printf("msgsz > msqptr->msg_qbytes\n");
633 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
635 printf("msqid is locked\n");
637 need_more_resources
= 1;
639 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
641 printf("msgsz + msg_cbytes > msg_qbytes\n");
643 need_more_resources
= 1;
645 if (segs_needed
> nfree_msgmaps
) {
647 printf("segs_needed > nfree_msgmaps\n");
649 need_more_resources
= 1;
651 if (free_msghdrs
== NULL
) {
653 printf("no more msghdrs\n");
655 need_more_resources
= 1;
658 if (need_more_resources
) {
661 if ((msgflg
& IPC_NOWAIT
) != 0) {
663 printf("need more resources but caller doesn't want to wait\n");
669 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
671 printf("we don't own the user_msqid_ds\n");
675 /* Force later arrivals to wait for our
678 printf("we own the user_msqid_ds\n");
680 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
684 printf("goodnight\n");
686 eval
= msleep((caddr_t
)msqptr
, &sysv_msg_subsys_mutex
, (PZERO
- 4) | PCATCH
,
689 printf("good morning, eval=%d\n", eval
);
692 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
695 printf("msgsnd: interrupted system call\n");
702 * Make sure that the msq queue still exists
705 if (msqptr
->msg_qbytes
== 0) {
707 printf("msqid deleted\n");
709 /* The SVID says to return EIDRM. */
713 /* Unfortunately, BSD doesn't define that code
723 printf("got all the resources that we need\n");
730 * We have the resources that we need.
734 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
735 panic("msg_perm.mode & MSG_LOCKED");
736 if (segs_needed
> nfree_msgmaps
)
737 panic("segs_needed > nfree_msgmaps");
738 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
739 panic("msgsz + msg_cbytes > msg_qbytes");
740 if (free_msghdrs
== NULL
)
741 panic("no more msghdrs");
744 * Re-lock the user_msqid_ds in case we page-fault when copying in
748 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
749 panic("user_msqid_ds is already locked");
750 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
753 * Allocate a message header
756 msghdr
= free_msghdrs
;
757 free_msghdrs
= msghdr
->msg_next
;
758 msghdr
->msg_spot
= -1;
759 msghdr
->msg_ts
= msgsz
;
762 * Allocate space for the message
765 while (segs_needed
> 0) {
766 if (nfree_msgmaps
<= 0)
767 panic("not enough msgmaps");
768 if (free_msgmaps
== -1)
769 panic("nil free_msgmaps");
772 panic("next too low #1");
773 if (next
>= msginfo
.msgseg
)
774 panic("next out of range #1");
776 printf("allocating segment %d to message\n", next
);
778 free_msgmaps
= msgmaps
[next
].next
;
780 msgmaps
[next
].next
= msghdr
->msg_spot
;
781 msghdr
->msg_spot
= next
;
786 * Copy in the message type. For a 64 bit process, this is 64 bits,
787 * but we only ever use the low 32 bits, so the cast is OK.
789 if (IS_64BIT_PROCESS(p
)) {
790 SYSV_MSG_SUBSYS_UNLOCK();
791 eval
= copyin(user_msgp
, &msgtype
, sizeof(msgtype
));
792 SYSV_MSG_SUBSYS_LOCK();
793 msghdr
->msg_type
= CAST_DOWN(long,msgtype
);
794 user_msgp
= user_msgp
+ sizeof(msgtype
); /* ptr math */
796 SYSV_MSG_SUBSYS_UNLOCK();
797 eval
= copyin(user_msgp
, &msghdr
->msg_type
, sizeof(long));
798 SYSV_MSG_SUBSYS_LOCK();
799 user_msgp
= user_msgp
+ sizeof(long); /* ptr math */
804 printf("error %d copying the message type\n", eval
);
807 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
808 wakeup((caddr_t
)msqptr
);
814 * Validate the message type
816 if (msghdr
->msg_type
< 1) {
818 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
819 wakeup((caddr_t
)msqptr
);
821 printf("mtype (%d) < 1\n", msghdr
->msg_type
);
828 * Copy in the message body
830 next
= msghdr
->msg_spot
;
833 /* compare input (size_t) value against restrict (int) value */
834 if (msgsz
> (size_t)msginfo
.msgssz
)
835 tlen
= msginfo
.msgssz
;
839 panic("next too low #2");
840 if (next
>= msginfo
.msgseg
)
841 panic("next out of range #2");
843 SYSV_MSG_SUBSYS_UNLOCK();
844 eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
], tlen
);
845 SYSV_MSG_SUBSYS_LOCK();
849 printf("error %d copying in message segment\n", eval
);
852 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
853 wakeup((caddr_t
)msqptr
);
858 user_msgp
= user_msgp
+ tlen
; /* ptr math */
859 next
= msgmaps
[next
].next
;
862 panic("didn't use all the msg segments");
865 * We've got the message. Unlock the user_msqid_ds.
868 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
871 * Make sure that the user_msqid_ds is still allocated.
874 if (msqptr
->msg_qbytes
== 0) {
876 wakeup((caddr_t
)msqptr
);
877 /* The SVID says to return EIDRM. */
881 /* Unfortunately, BSD doesn't define that code yet! */
888 * Put the message into the queue
891 if (msqptr
->msg_first
== NULL
) {
892 msqptr
->msg_first
= msghdr
;
893 msqptr
->msg_last
= msghdr
;
895 msqptr
->msg_last
->msg_next
= msghdr
;
896 msqptr
->msg_last
= msghdr
;
898 msqptr
->msg_last
->msg_next
= NULL
;
900 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
902 msqptr
->msg_lspid
= p
->p_pid
;
903 msqptr
->msg_stime
= sysv_msgtime();
905 wakeup((caddr_t
)msqptr
);
910 SYSV_MSG_SUBSYS_UNLOCK();
916 msgrcv(struct proc
*p
, struct msgrcv_args
*uap
, user_ssize_t
*retval
)
918 int msqid
= uap
->msqid
;
919 user_addr_t user_msgp
= uap
->msgp
;
920 size_t msgsz
= (size_t)uap
->msgsz
; /* limit to 4G */
921 long msgtyp
= (long)uap
->msgtyp
; /* limit to 32 bits */
922 int msgflg
= uap
->msgflg
;
924 struct user_msqid_ds
*msqptr
;
931 SYSV_MSG_SUBSYS_LOCK();
935 printf("call to msgrcv(%d, 0x%qx, %d, %ld, %d)\n", msqid
, user_msgp
,
936 msgsz
, msgtyp
, msgflg
);
939 AUDIT_ARG(svipc_id
, msqid
);
940 msqid
= IPCID_TO_IX(msqid
);
942 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
944 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
951 msqptr
= &msqids
[msqid
];
952 if (msqptr
->msg_qbytes
== 0) {
954 printf("no such message queue id\n");
959 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
961 printf("wrong sequence number\n");
967 if ((eval
= ipcperm(kauth_cred_get(), &msqptr
->msg_perm
, IPC_R
))) {
969 printf("requester doesn't have read access\n");
975 while (msghdr
== NULL
) {
977 msghdr
= msqptr
->msg_first
;
978 if (msghdr
!= NULL
) {
979 if (msgsz
< msghdr
->msg_ts
&&
980 (msgflg
& MSG_NOERROR
) == 0) {
982 printf("first message on the queue is too big (want %d, got %d)\n",
983 msgsz
, msghdr
->msg_ts
);
988 if (msqptr
->msg_first
== msqptr
->msg_last
) {
989 msqptr
->msg_first
= NULL
;
990 msqptr
->msg_last
= NULL
;
992 msqptr
->msg_first
= msghdr
->msg_next
;
993 if (msqptr
->msg_first
== NULL
)
994 panic("msg_first/last messed up #1");
998 struct msg
*previous
;
1002 prev
= &(msqptr
->msg_first
);
1003 while ((msghdr
= *prev
) != NULL
) {
1005 * Is this message's type an exact match or is
1006 * this message's type less than or equal to
1007 * the absolute value of a negative msgtyp?
1008 * Note that the second half of this test can
1009 * NEVER be true if msgtyp is positive since
1010 * msg_type is always positive!
1013 if (msgtyp
== msghdr
->msg_type
||
1014 msghdr
->msg_type
<= -msgtyp
) {
1016 printf("found message type %d, requested %d\n",
1017 msghdr
->msg_type
, msgtyp
);
1019 if (msgsz
< msghdr
->msg_ts
&&
1020 (msgflg
& MSG_NOERROR
) == 0) {
1022 printf("requested message on the queue is too big (want %d, got %d)\n",
1023 msgsz
, msghdr
->msg_ts
);
1028 *prev
= msghdr
->msg_next
;
1029 if (msghdr
== msqptr
->msg_last
) {
1030 if (previous
== NULL
) {
1033 panic("msg_first/last messed up #2");
1041 panic("msg_first/last messed up #3");
1049 prev
= &(msghdr
->msg_next
);
1054 * We've either extracted the msghdr for the appropriate
1055 * message or there isn't one.
1056 * If there is one then bail out of this loop.
1063 * Hmph! No message found. Does the user want to wait?
1066 if ((msgflg
& IPC_NOWAIT
) != 0) {
1068 printf("no appropriate message found (msgtyp=%d)\n",
1071 /* The SVID says to return ENOMSG. */
1075 /* Unfortunately, BSD doesn't define that code yet! */
1082 * Wait for something to happen
1086 printf("msgrcv: goodnight\n");
1088 eval
= msleep((caddr_t
)msqptr
, &sysv_msg_subsys_mutex
, (PZERO
- 4) | PCATCH
, "msgwait",
1091 printf("msgrcv: good morning (eval=%d)\n", eval
);
1096 printf("msgsnd: interrupted system call\n");
1103 * Make sure that the msq queue still exists
1106 if (msqptr
->msg_qbytes
== 0 ||
1107 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
1109 printf("msqid deleted\n");
1111 /* The SVID says to return EIDRM. */
1115 /* Unfortunately, BSD doesn't define that code yet! */
1123 * Return the message to the user.
1125 * First, do the bookkeeping (before we risk being interrupted).
1128 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
1130 msqptr
->msg_lrpid
= p
->p_pid
;
1131 msqptr
->msg_rtime
= sysv_msgtime();
1134 * Make msgsz the actual amount that we'll be returning.
1135 * Note that this effectively truncates the message if it is too long
1136 * (since msgsz is never increased).
1140 printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
1143 if (msgsz
> msghdr
->msg_ts
)
1144 msgsz
= msghdr
->msg_ts
;
1147 * Return the type to the user.
1151 * Copy out the message type. For a 64 bit process, this is 64 bits,
1152 * but we only ever use the low 32 bits, so the cast is OK.
1154 if (IS_64BIT_PROCESS(p
)) {
1155 msgtype
= msghdr
->msg_type
;
1156 SYSV_MSG_SUBSYS_UNLOCK();
1157 eval
= copyout(&msgtype
, user_msgp
, sizeof(msgtype
));
1158 SYSV_MSG_SUBSYS_LOCK();
1159 user_msgp
= user_msgp
+ sizeof(msgtype
); /* ptr math */
1161 msg_type_long
= msghdr
->msg_type
;
1162 SYSV_MSG_SUBSYS_UNLOCK();
1163 eval
= copyout(&msg_type_long
, user_msgp
, sizeof(long));
1164 SYSV_MSG_SUBSYS_LOCK();
1165 user_msgp
= user_msgp
+ sizeof(long); /* ptr math */
1170 printf("error (%d) copying out message type\n", eval
);
1172 msg_freehdr(msghdr
);
1173 wakeup((caddr_t
)msqptr
);
1180 * Return the segments to the user
1183 next
= msghdr
->msg_spot
;
1184 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1187 /* compare input (size_t) value against restrict (int) value */
1188 if (msgsz
> (size_t)msginfo
.msgssz
)
1189 tlen
= msginfo
.msgssz
;
1193 panic("next too low #3");
1194 if (next
>= msginfo
.msgseg
)
1195 panic("next out of range #3");
1196 SYSV_MSG_SUBSYS_UNLOCK();
1197 eval
= copyout(&msgpool
[next
* msginfo
.msgssz
],
1199 SYSV_MSG_SUBSYS_LOCK();
1202 printf("error (%d) copying out message segment\n",
1205 msg_freehdr(msghdr
);
1206 wakeup((caddr_t
)msqptr
);
1209 user_msgp
= user_msgp
+ tlen
; /* ptr math */
1210 next
= msgmaps
[next
].next
;
1214 * Done, return the actual number of bytes copied out.
1217 msg_freehdr(msghdr
);
1218 wakeup((caddr_t
)msqptr
);
1222 SYSV_MSG_SUBSYS_UNLOCK();
1227 IPCS_msg_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
1228 __unused
int arg2
, struct sysctl_req
*req
)
1233 struct IPCS_command u32
;
1234 struct user_IPCS_command u64
;
1236 struct msqid_ds msqid_ds32
; /* post conversion, 32 bit version */
1238 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
1239 size_t msqid_ds_sz
= sizeof(struct user_msqid_ds
);
1240 struct proc
*p
= current_proc();
1242 if (!IS_64BIT_PROCESS(p
)) {
1243 ipcs_sz
= sizeof(struct IPCS_command
);
1244 msqid_ds_sz
= sizeof(struct msqid_ds
);
1247 /* Copy in the command structure */
1248 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
1252 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
1253 ipcs
.u64
.ipcs_data
= CAST_USER_ADDR_T(ipcs
.u32
.ipcs_data
);
1255 /* Let us version this interface... */
1256 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
1260 SYSV_MSG_SUBSYS_LOCK();
1262 switch(ipcs
.u64
.ipcs_op
) {
1263 case IPCS_MSG_CONF
: /* Obtain global configuration data */
1264 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct msginfo
)) {
1268 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
1272 SYSV_MSG_SUBSYS_UNLOCK();
1273 error
= copyout(&msginfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1274 SYSV_MSG_SUBSYS_LOCK();
1277 case IPCS_MSG_ITER
: /* Iterate over existing segments */
1278 /* Not done up top so we can set limits via sysctl (later) */
1281 cursor
= ipcs
.u64
.ipcs_cursor
;
1282 if (cursor
< 0 || cursor
>= msginfo
.msgmni
) {
1286 if (ipcs
.u64
.ipcs_datalen
!= (int)msqid_ds_sz
) {
1290 for( ; cursor
< msginfo
.msgmni
; cursor
++) {
1291 if (msqids
[cursor
].msg_qbytes
!= 0) /* allocated */
1295 if (cursor
== msginfo
.msgmni
) {
1300 msqid_dsp
= &msqids
[cursor
]; /* default: 64 bit */
1303 * If necessary, convert the 64 bit kernel segment
1304 * descriptor to a 32 bit user one.
1306 if (!IS_64BIT_PROCESS(p
)) {
1307 msqid_ds_64to32(msqid_dsp
, &msqid_ds32
);
1308 msqid_dsp
= &msqid_ds32
;
1310 SYSV_MSG_SUBSYS_UNLOCK();
1311 error
= copyout(msqid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1314 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
1316 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
1317 ipcs
.u32
.ipcs_data
= CAST_DOWN(void *,ipcs
.u64
.ipcs_data
);
1318 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
1320 SYSV_MSG_SUBSYS_LOCK();
1328 SYSV_MSG_SUBSYS_UNLOCK();
1332 SYSCTL_DECL(_kern_sysv_ipcs
);
1333 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, msg
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1334 0, 0, IPCS_msg_sysctl
,
1335 "S,IPCS_msg_command",
1336 "ipcs msg command interface");