2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Implementation of SVID messages
31 * Author: Daniel Boulet
33 * Copyright 1993 Daniel Boulet and RTMX Inc.
35 * This system call was implemented by Daniel Boulet under contract from RTMX.
37 * Redistribution and use in source forms, with and without modification,
38 * are permitted provided that this entire comment appears intact.
40 * Redistribution in binary form may occur without any restrictions.
41 * Obviously, it would be nice if you gave credit where credit is due
42 * but requiring it would be too onerous.
44 * This software is provided ``AS IS'' without any warranties of any kind.
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/proc_internal.h>
51 #include <sys/kauth.h>
53 #include <sys/malloc.h>
54 #include <mach/mach_types.h>
56 #include <bsm/audit_kernel.h>
58 #include <sys/filedesc.h>
59 #include <sys/file_internal.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
64 static void msginit(void *);
69 static void msg_freehdr(struct msg
*msghdr
);
71 typedef int sy_call_t(struct proc
*, void *, int *);
73 /* XXX casting to (sy_call_t *) is bogus, as usual. */
74 static sy_call_t
*msgcalls
[] = {
75 (sy_call_t
*)msgctl
, (sy_call_t
*)msgget
,
76 (sy_call_t
*)msgsnd
, (sy_call_t
*)msgrcv
79 static int nfree_msgmaps
; /* # of free map entries */
80 static short free_msgmaps
; /* free map entries list head */
81 static struct msg
*free_msghdrs
; /* list of free msg headers */
82 char *msgpool
; /* MSGMAX byte long msg buffer pool */
83 struct msgmap
*msgmaps
; /* MSGSEG msgmap structures */
84 struct msg
*msghdrs
; /* MSGTQL msg headers */
85 struct user_msqid_ds
*msqids
; /* MSGMNI user_msqid_ds struct's */
87 static lck_grp_t
*sysv_msg_subsys_lck_grp
;
88 static lck_grp_attr_t
*sysv_msg_subsys_lck_grp_attr
;
89 static lck_attr_t
*sysv_msg_subsys_lck_attr
;
90 static lck_mtx_t sysv_msg_subsys_mutex
;
92 #define SYSV_MSG_SUBSYS_LOCK() lck_mtx_lock(&sysv_msg_subsys_mutex)
93 #define SYSV_MSG_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_msg_subsys_mutex)
95 void sysv_msg_lock_init(void);
98 #ifdef __APPLE_API_PRIVATE
99 struct msginfo msginfo
= {
100 MSGMAX
, /* = (MSGSSZ*MSGSEG) : max chars in a message */
101 MSGMNI
, /* = 40 : max message queue identifiers */
102 MSGMNB
, /* = 2048 : max chars in a queue */
103 MSGTQL
, /* = 40 : max messages in system */
104 MSGSSZ
, /* = 8 : size of a message segment (2^N long) */
105 MSGSEG
/* = 2048 : number of message segments */
107 #endif /* __APPLE_API_PRIVATE */
109 /* Initialize the mutex governing access to the SysV msg subsystem */
110 __private_extern__
void
111 sysv_msg_lock_init( void )
113 sysv_msg_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
115 sysv_msg_subsys_lck_grp
= lck_grp_alloc_init("sysv_msg_subsys_lock", sysv_msg_subsys_lck_grp_attr
);
117 sysv_msg_subsys_lck_attr
= lck_attr_alloc_init();
118 lck_mtx_init(&sysv_msg_subsys_mutex
, sysv_msg_subsys_lck_grp
, sysv_msg_subsys_lck_attr
);
121 static __inline__ user_time_t
130 * NOTE: Source and target may *NOT* overlap! (target is smaller)
133 msqid_ds_64to32(struct user_msqid_ds
*in
, struct msqid_ds
*out
)
135 out
->msg_perm
= in
->msg_perm
;
136 out
->msg_qnum
= in
->msg_qnum
;
137 out
->msg_cbytes
= in
->msg_cbytes
; /* for ipcs */
138 out
->msg_qbytes
= in
->msg_qbytes
;
139 out
->msg_lspid
= in
->msg_lspid
;
140 out
->msg_lrpid
= in
->msg_lrpid
;
141 out
->msg_stime
= in
->msg_stime
; /* XXX loss of range */
142 out
->msg_rtime
= in
->msg_rtime
; /* XXX loss of range */
143 out
->msg_ctime
= in
->msg_ctime
; /* XXX loss of range */
147 * NOTE: Source and target may are permitted to overlap! (source is smaller);
148 * this works because we copy fields in order from the end of the struct to
152 msqid_ds_32to64(struct msqid_ds
*in
, struct user_msqid_ds
*out
)
154 out
->msg_ctime
= in
->msg_ctime
;
155 out
->msg_rtime
= in
->msg_rtime
;
156 out
->msg_stime
= in
->msg_stime
;
157 out
->msg_lrpid
= in
->msg_lrpid
;
158 out
->msg_lspid
= in
->msg_lspid
;
159 out
->msg_qbytes
= in
->msg_qbytes
;
160 out
->msg_cbytes
= in
->msg_cbytes
; /* for ipcs */
161 out
->msg_qnum
= in
->msg_qnum
;
162 out
->msg_perm
= in
->msg_perm
;
165 /* This routine assumes the system is locked prior to calling this routine */
167 msginit(__unused
void *dummy
)
169 static int initted
= 0;
172 /* Lazy initialization on first system call; we don't have SYSINIT(). */
177 msgpool
= (char *)_MALLOC(msginfo
.msgmax
, M_SHM
, M_WAITOK
);
178 MALLOC(msgmaps
, struct msgmap
*,
179 sizeof(struct msgmap
) * msginfo
.msgseg
,
181 MALLOC(msghdrs
, struct msg
*,
182 sizeof(struct msg
) * msginfo
.msgtql
,
184 MALLOC(msqids
, struct user_msqid_ds
*,
185 sizeof(struct user_msqid_ds
) * msginfo
.msgmni
,
189 * msginfo.msgssz should be a power of two for efficiency reasons.
190 * It is also pretty silly if msginfo.msgssz is less than 8
191 * or greater than about 256 so ...
195 while (i
< 1024 && i
!= msginfo
.msgssz
)
197 if (i
!= msginfo
.msgssz
) {
198 printf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
200 panic("msginfo.msgssz not a small power of 2");
203 if (msginfo
.msgseg
> 32767) {
204 printf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
205 panic("msginfo.msgseg > 32767");
209 panic("msgmaps is NULL");
211 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
213 msgmaps
[i
-1].next
= i
;
214 msgmaps
[i
].next
= -1; /* implies entry is available */
217 nfree_msgmaps
= msginfo
.msgseg
;
220 panic("msghdrs is NULL");
222 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
223 msghdrs
[i
].msg_type
= 0;
225 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
226 msghdrs
[i
].msg_next
= NULL
;
228 free_msghdrs
= &msghdrs
[0];
231 panic("msqids is NULL");
233 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
234 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
235 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
240 * Entry point for all MSG calls
242 /* XXX actually varargs. */
244 msgsys(struct proc
*p
, struct msgsys_args
*uap
, register_t
*retval
)
246 if (uap
->which
>= sizeof(msgcalls
)/sizeof(msgcalls
[0]))
248 return ((*msgcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
252 msg_freehdr(struct msg
*msghdr
)
254 while (msghdr
->msg_ts
> 0) {
256 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
257 panic("msghdr->msg_spot out of range");
258 next
= msgmaps
[msghdr
->msg_spot
].next
;
259 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
260 free_msgmaps
= msghdr
->msg_spot
;
262 msghdr
->msg_spot
= next
;
263 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
264 msghdr
->msg_ts
-= msginfo
.msgssz
;
268 if (msghdr
->msg_spot
!= -1)
269 panic("msghdr->msg_spot != -1");
270 msghdr
->msg_next
= free_msghdrs
;
271 free_msghdrs
= msghdr
;
275 msgctl(struct proc
*p
, struct msgctl_args
*uap
, register_t
*retval
)
277 int msqid
= uap
->msqid
;
279 kauth_cred_t cred
= kauth_cred_get();
281 struct user_msqid_ds msqbuf
;
282 struct user_msqid_ds
*msqptr
;
283 struct user_msqid_ds umsds
;
285 SYSV_MSG_SUBSYS_LOCK();
290 printf("call to msgctl(%d, %d, 0x%qx)\n", msqid
, cmd
, uap
->buf
);
293 AUDIT_ARG(svipc_cmd
, cmd
);
294 AUDIT_ARG(svipc_id
, msqid
);
295 msqid
= IPCID_TO_IX(msqid
);
297 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
299 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
306 msqptr
= &msqids
[msqid
];
308 if (msqptr
->msg_qbytes
== 0) {
310 printf("no such msqid\n");
315 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
317 printf("wrong sequence number\n");
331 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_M
)))
334 /* Free the message headers */
335 msghdr
= msqptr
->msg_first
;
336 while (msghdr
!= NULL
) {
337 struct msg
*msghdr_tmp
;
339 /* Free the segments of each message */
340 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
343 msghdr
= msghdr
->msg_next
;
344 msg_freehdr(msghdr_tmp
);
347 if (msqptr
->msg_cbytes
!= 0)
348 panic("msg_cbytes is messed up");
349 if (msqptr
->msg_qnum
!= 0)
350 panic("msg_qnum is messed up");
352 msqptr
->msg_qbytes
= 0; /* Mark it as free */
354 wakeup((caddr_t
)msqptr
);
360 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_M
)))
363 SYSV_MSG_SUBSYS_UNLOCK();
365 if (IS_64BIT_PROCESS(p
)) {
366 eval
= copyin(uap
->buf
, &msqbuf
, sizeof(struct user_msqid_ds
));
368 eval
= copyin(uap
->buf
, &msqbuf
, sizeof(struct msqid_ds
));
369 /* convert in place; ugly, but safe */
370 msqid_ds_32to64((struct msqid_ds
*)&msqbuf
, &msqbuf
);
375 SYSV_MSG_SUBSYS_LOCK();
377 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
378 eval
= suser(cred
, &p
->p_acflag
);
384 /* compare (msglen_t) value against restrict (int) value */
385 if (msqbuf
.msg_qbytes
> (msglen_t
)msginfo
.msgmnb
) {
387 printf("can't increase msg_qbytes beyond %d (truncating)\n",
390 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
392 if (msqbuf
.msg_qbytes
== 0) {
394 printf("can't reduce msg_qbytes to 0\n");
399 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
400 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
401 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
402 (msqbuf
.msg_perm
.mode
& 0777);
403 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
404 msqptr
->msg_ctime
= sysv_msgtime();
408 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_R
))) {
410 printf("requester doesn't have read access\n");
415 bcopy(msqptr
, &umsds
, sizeof(struct user_msqid_ds
));
417 SYSV_MSG_SUBSYS_UNLOCK();
418 if (IS_64BIT_PROCESS(p
)) {
419 eval
= copyout(&umsds
, uap
->buf
, sizeof(struct user_msqid_ds
));
421 struct msqid_ds msqid_ds32
;
422 msqid_ds_64to32(&umsds
, &msqid_ds32
);
423 eval
= copyout(&msqid_ds32
, uap
->buf
, sizeof(struct msqid_ds
));
425 SYSV_MSG_SUBSYS_LOCK();
430 printf("invalid command %d\n", cmd
);
439 SYSV_MSG_SUBSYS_UNLOCK();
444 msgget(__unused
struct proc
*p
, struct msgget_args
*uap
, register_t
*retval
)
448 int msgflg
= uap
->msgflg
;
449 kauth_cred_t cred
= kauth_cred_get();
450 struct user_msqid_ds
*msqptr
= NULL
;
452 SYSV_MSG_SUBSYS_LOCK();
456 printf("msgget(0x%x, 0%o)\n", key
, msgflg
);
459 if (key
!= IPC_PRIVATE
) {
460 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
461 msqptr
= &msqids
[msqid
];
462 if (msqptr
->msg_qbytes
!= 0 &&
463 msqptr
->msg_perm
.key
== key
)
466 if (msqid
< msginfo
.msgmni
) {
468 printf("found public key\n");
470 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
472 printf("not exclusive\n");
477 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
479 printf("requester doesn't have 0%o access\n",
489 printf("need to allocate the user_msqid_ds\n");
491 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
492 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
494 * Look for an unallocated and unlocked user_msqid_ds.
495 * user_msqid_ds's can be locked by msgsnd or msgrcv
496 * while they are copying the message in/out. We
497 * can't re-use the entry until they release it.
499 msqptr
= &msqids
[msqid
];
500 if (msqptr
->msg_qbytes
== 0 &&
501 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
504 if (msqid
== msginfo
.msgmni
) {
506 printf("no more user_msqid_ds's available\n");
512 printf("msqid %d is available\n", msqid
);
514 msqptr
->msg_perm
.key
= key
;
515 msqptr
->msg_perm
.cuid
= kauth_cred_getuid(cred
);
516 msqptr
->msg_perm
.uid
= kauth_cred_getuid(cred
);
517 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
518 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
519 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
520 /* Make sure that the returned msqid is unique */
521 msqptr
->msg_perm
.seq
++;
522 msqptr
->msg_first
= NULL
;
523 msqptr
->msg_last
= NULL
;
524 msqptr
->msg_cbytes
= 0;
525 msqptr
->msg_qnum
= 0;
526 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
527 msqptr
->msg_lspid
= 0;
528 msqptr
->msg_lrpid
= 0;
529 msqptr
->msg_stime
= 0;
530 msqptr
->msg_rtime
= 0;
531 msqptr
->msg_ctime
= sysv_msgtime();
534 printf("didn't find it and wasn't asked to create it\n");
541 /* Construct the unique msqid */
542 *retval
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
543 AUDIT_ARG(svipc_id
, *retval
);
546 SYSV_MSG_SUBSYS_UNLOCK();
552 msgsnd(struct proc
*p
, struct msgsnd_args
*uap
, register_t
*retval
)
554 int msqid
= uap
->msqid
;
555 user_addr_t user_msgp
= uap
->msgp
;
556 size_t msgsz
= (size_t)uap
->msgsz
; /* limit to 4G */
557 int msgflg
= uap
->msgflg
;
558 int segs_needed
, eval
;
559 struct user_msqid_ds
*msqptr
;
565 SYSV_MSG_SUBSYS_LOCK();
569 printf("call to msgsnd(%d, 0x%qx, %d, %d)\n", msqid
, user_msgp
, msgsz
,
573 AUDIT_ARG(svipc_id
, msqid
);
574 msqid
= IPCID_TO_IX(msqid
);
576 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
578 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
585 msqptr
= &msqids
[msqid
];
586 if (msqptr
->msg_qbytes
== 0) {
588 printf("no such message queue id\n");
593 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
595 printf("wrong sequence number\n");
601 if ((eval
= ipcperm(kauth_cred_get(), &msqptr
->msg_perm
, IPC_W
))) {
603 printf("requester doesn't have write access\n");
608 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
610 printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
614 int need_more_resources
= 0;
618 * (inside this loop in case msg_qbytes changes while we sleep)
621 if (msgsz
> msqptr
->msg_qbytes
) {
623 printf("msgsz > msqptr->msg_qbytes\n");
629 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
631 printf("msqid is locked\n");
633 need_more_resources
= 1;
635 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
637 printf("msgsz + msg_cbytes > msg_qbytes\n");
639 need_more_resources
= 1;
641 if (segs_needed
> nfree_msgmaps
) {
643 printf("segs_needed > nfree_msgmaps\n");
645 need_more_resources
= 1;
647 if (free_msghdrs
== NULL
) {
649 printf("no more msghdrs\n");
651 need_more_resources
= 1;
654 if (need_more_resources
) {
657 if ((msgflg
& IPC_NOWAIT
) != 0) {
659 printf("need more resources but caller doesn't want to wait\n");
665 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
667 printf("we don't own the user_msqid_ds\n");
671 /* Force later arrivals to wait for our
674 printf("we own the user_msqid_ds\n");
676 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
680 printf("goodnight\n");
682 eval
= msleep((caddr_t
)msqptr
, &sysv_msg_subsys_mutex
, (PZERO
- 4) | PCATCH
,
685 printf("good morning, eval=%d\n", eval
);
688 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
691 printf("msgsnd: interrupted system call\n");
698 * Make sure that the msq queue still exists
701 if (msqptr
->msg_qbytes
== 0) {
703 printf("msqid deleted\n");
705 /* The SVID says to return EIDRM. */
709 /* Unfortunately, BSD doesn't define that code
719 printf("got all the resources that we need\n");
726 * We have the resources that we need.
730 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
731 panic("msg_perm.mode & MSG_LOCKED");
732 if (segs_needed
> nfree_msgmaps
)
733 panic("segs_needed > nfree_msgmaps");
734 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
735 panic("msgsz + msg_cbytes > msg_qbytes");
736 if (free_msghdrs
== NULL
)
737 panic("no more msghdrs");
740 * Re-lock the user_msqid_ds in case we page-fault when copying in
744 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
745 panic("user_msqid_ds is already locked");
746 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
749 * Allocate a message header
752 msghdr
= free_msghdrs
;
753 free_msghdrs
= msghdr
->msg_next
;
754 msghdr
->msg_spot
= -1;
755 msghdr
->msg_ts
= msgsz
;
758 * Allocate space for the message
761 while (segs_needed
> 0) {
762 if (nfree_msgmaps
<= 0)
763 panic("not enough msgmaps");
764 if (free_msgmaps
== -1)
765 panic("nil free_msgmaps");
768 panic("next too low #1");
769 if (next
>= msginfo
.msgseg
)
770 panic("next out of range #1");
772 printf("allocating segment %d to message\n", next
);
774 free_msgmaps
= msgmaps
[next
].next
;
776 msgmaps
[next
].next
= msghdr
->msg_spot
;
777 msghdr
->msg_spot
= next
;
782 * Copy in the message type. For a 64 bit process, this is 64 bits,
783 * but we only ever use the low 32 bits, so the cast is OK.
785 if (IS_64BIT_PROCESS(p
)) {
786 SYSV_MSG_SUBSYS_UNLOCK();
787 eval
= copyin(user_msgp
, &msgtype
, sizeof(msgtype
));
788 SYSV_MSG_SUBSYS_LOCK();
789 msghdr
->msg_type
= CAST_DOWN(long,msgtype
);
790 user_msgp
= user_msgp
+ sizeof(msgtype
); /* ptr math */
792 SYSV_MSG_SUBSYS_UNLOCK();
793 eval
= copyin(user_msgp
, &msghdr
->msg_type
, sizeof(long));
794 SYSV_MSG_SUBSYS_LOCK();
795 user_msgp
= user_msgp
+ sizeof(long); /* ptr math */
800 printf("error %d copying the message type\n", eval
);
803 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
804 wakeup((caddr_t
)msqptr
);
810 * Validate the message type
812 if (msghdr
->msg_type
< 1) {
814 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
815 wakeup((caddr_t
)msqptr
);
817 printf("mtype (%d) < 1\n", msghdr
->msg_type
);
824 * Copy in the message body
826 next
= msghdr
->msg_spot
;
829 /* compare input (size_t) value against restrict (int) value */
830 if (msgsz
> (size_t)msginfo
.msgssz
)
831 tlen
= msginfo
.msgssz
;
835 panic("next too low #2");
836 if (next
>= msginfo
.msgseg
)
837 panic("next out of range #2");
839 SYSV_MSG_SUBSYS_UNLOCK();
840 eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
], tlen
);
841 SYSV_MSG_SUBSYS_LOCK();
845 printf("error %d copying in message segment\n", eval
);
848 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
849 wakeup((caddr_t
)msqptr
);
854 user_msgp
= user_msgp
+ tlen
; /* ptr math */
855 next
= msgmaps
[next
].next
;
858 panic("didn't use all the msg segments");
861 * We've got the message. Unlock the user_msqid_ds.
864 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
867 * Make sure that the user_msqid_ds is still allocated.
870 if (msqptr
->msg_qbytes
== 0) {
872 wakeup((caddr_t
)msqptr
);
873 /* The SVID says to return EIDRM. */
877 /* Unfortunately, BSD doesn't define that code yet! */
884 * Put the message into the queue
887 if (msqptr
->msg_first
== NULL
) {
888 msqptr
->msg_first
= msghdr
;
889 msqptr
->msg_last
= msghdr
;
891 msqptr
->msg_last
->msg_next
= msghdr
;
892 msqptr
->msg_last
= msghdr
;
894 msqptr
->msg_last
->msg_next
= NULL
;
896 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
898 msqptr
->msg_lspid
= p
->p_pid
;
899 msqptr
->msg_stime
= sysv_msgtime();
901 wakeup((caddr_t
)msqptr
);
906 SYSV_MSG_SUBSYS_UNLOCK();
912 msgrcv(struct proc
*p
, struct msgrcv_args
*uap
, user_ssize_t
*retval
)
914 int msqid
= uap
->msqid
;
915 user_addr_t user_msgp
= uap
->msgp
;
916 size_t msgsz
= (size_t)uap
->msgsz
; /* limit to 4G */
917 long msgtyp
= (long)uap
->msgtyp
; /* limit to 32 bits */
918 int msgflg
= uap
->msgflg
;
920 struct user_msqid_ds
*msqptr
;
927 SYSV_MSG_SUBSYS_LOCK();
931 printf("call to msgrcv(%d, 0x%qx, %d, %ld, %d)\n", msqid
, user_msgp
,
932 msgsz
, msgtyp
, msgflg
);
935 AUDIT_ARG(svipc_id
, msqid
);
936 msqid
= IPCID_TO_IX(msqid
);
938 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
940 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
947 msqptr
= &msqids
[msqid
];
948 if (msqptr
->msg_qbytes
== 0) {
950 printf("no such message queue id\n");
955 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
957 printf("wrong sequence number\n");
963 if ((eval
= ipcperm(kauth_cred_get(), &msqptr
->msg_perm
, IPC_R
))) {
965 printf("requester doesn't have read access\n");
971 while (msghdr
== NULL
) {
973 msghdr
= msqptr
->msg_first
;
974 if (msghdr
!= NULL
) {
975 if (msgsz
< msghdr
->msg_ts
&&
976 (msgflg
& MSG_NOERROR
) == 0) {
978 printf("first message on the queue is too big (want %d, got %d)\n",
979 msgsz
, msghdr
->msg_ts
);
984 if (msqptr
->msg_first
== msqptr
->msg_last
) {
985 msqptr
->msg_first
= NULL
;
986 msqptr
->msg_last
= NULL
;
988 msqptr
->msg_first
= msghdr
->msg_next
;
989 if (msqptr
->msg_first
== NULL
)
990 panic("msg_first/last messed up #1");
994 struct msg
*previous
;
998 prev
= &(msqptr
->msg_first
);
999 while ((msghdr
= *prev
) != NULL
) {
1001 * Is this message's type an exact match or is
1002 * this message's type less than or equal to
1003 * the absolute value of a negative msgtyp?
1004 * Note that the second half of this test can
1005 * NEVER be true if msgtyp is positive since
1006 * msg_type is always positive!
1009 if (msgtyp
== msghdr
->msg_type
||
1010 msghdr
->msg_type
<= -msgtyp
) {
1012 printf("found message type %d, requested %d\n",
1013 msghdr
->msg_type
, msgtyp
);
1015 if (msgsz
< msghdr
->msg_ts
&&
1016 (msgflg
& MSG_NOERROR
) == 0) {
1018 printf("requested message on the queue is too big (want %d, got %d)\n",
1019 msgsz
, msghdr
->msg_ts
);
1024 *prev
= msghdr
->msg_next
;
1025 if (msghdr
== msqptr
->msg_last
) {
1026 if (previous
== NULL
) {
1029 panic("msg_first/last messed up #2");
1037 panic("msg_first/last messed up #3");
1045 prev
= &(msghdr
->msg_next
);
1050 * We've either extracted the msghdr for the appropriate
1051 * message or there isn't one.
1052 * If there is one then bail out of this loop.
1059 * Hmph! No message found. Does the user want to wait?
1062 if ((msgflg
& IPC_NOWAIT
) != 0) {
1064 printf("no appropriate message found (msgtyp=%d)\n",
1067 /* The SVID says to return ENOMSG. */
1071 /* Unfortunately, BSD doesn't define that code yet! */
1078 * Wait for something to happen
1082 printf("msgrcv: goodnight\n");
1084 eval
= msleep((caddr_t
)msqptr
, &sysv_msg_subsys_mutex
, (PZERO
- 4) | PCATCH
, "msgwait",
1087 printf("msgrcv: good morning (eval=%d)\n", eval
);
1092 printf("msgsnd: interrupted system call\n");
1099 * Make sure that the msq queue still exists
1102 if (msqptr
->msg_qbytes
== 0 ||
1103 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
1105 printf("msqid deleted\n");
1107 /* The SVID says to return EIDRM. */
1111 /* Unfortunately, BSD doesn't define that code yet! */
1119 * Return the message to the user.
1121 * First, do the bookkeeping (before we risk being interrupted).
1124 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
1126 msqptr
->msg_lrpid
= p
->p_pid
;
1127 msqptr
->msg_rtime
= sysv_msgtime();
1130 * Make msgsz the actual amount that we'll be returning.
1131 * Note that this effectively truncates the message if it is too long
1132 * (since msgsz is never increased).
1136 printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
1139 if (msgsz
> msghdr
->msg_ts
)
1140 msgsz
= msghdr
->msg_ts
;
1143 * Return the type to the user.
1147 * Copy out the message type. For a 64 bit process, this is 64 bits,
1148 * but we only ever use the low 32 bits, so the cast is OK.
1150 if (IS_64BIT_PROCESS(p
)) {
1151 msgtype
= msghdr
->msg_type
;
1152 SYSV_MSG_SUBSYS_UNLOCK();
1153 eval
= copyout(&msgtype
, user_msgp
, sizeof(msgtype
));
1154 SYSV_MSG_SUBSYS_LOCK();
1155 user_msgp
= user_msgp
+ sizeof(msgtype
); /* ptr math */
1157 msg_type_long
= msghdr
->msg_type
;
1158 SYSV_MSG_SUBSYS_UNLOCK();
1159 eval
= copyout(&msg_type_long
, user_msgp
, sizeof(long));
1160 SYSV_MSG_SUBSYS_LOCK();
1161 user_msgp
= user_msgp
+ sizeof(long); /* ptr math */
1166 printf("error (%d) copying out message type\n", eval
);
1168 msg_freehdr(msghdr
);
1169 wakeup((caddr_t
)msqptr
);
1176 * Return the segments to the user
1179 next
= msghdr
->msg_spot
;
1180 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1183 /* compare input (size_t) value against restrict (int) value */
1184 if (msgsz
> (size_t)msginfo
.msgssz
)
1185 tlen
= msginfo
.msgssz
;
1189 panic("next too low #3");
1190 if (next
>= msginfo
.msgseg
)
1191 panic("next out of range #3");
1192 SYSV_MSG_SUBSYS_UNLOCK();
1193 eval
= copyout(&msgpool
[next
* msginfo
.msgssz
],
1195 SYSV_MSG_SUBSYS_LOCK();
1198 printf("error (%d) copying out message segment\n",
1201 msg_freehdr(msghdr
);
1202 wakeup((caddr_t
)msqptr
);
1205 user_msgp
= user_msgp
+ tlen
; /* ptr math */
1206 next
= msgmaps
[next
].next
;
1210 * Done, return the actual number of bytes copied out.
1213 msg_freehdr(msghdr
);
1214 wakeup((caddr_t
)msqptr
);
1218 SYSV_MSG_SUBSYS_UNLOCK();
1223 IPCS_msg_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
1224 __unused
int arg2
, struct sysctl_req
*req
)
1229 struct IPCS_command u32
;
1230 struct user_IPCS_command u64
;
1232 struct msqid_ds msqid_ds32
; /* post conversion, 32 bit version */
1234 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
1235 size_t msqid_ds_sz
= sizeof(struct user_msqid_ds
);
1236 struct proc
*p
= current_proc();
1238 if (!IS_64BIT_PROCESS(p
)) {
1239 ipcs_sz
= sizeof(struct IPCS_command
);
1240 msqid_ds_sz
= sizeof(struct msqid_ds
);
1243 /* Copy in the command structure */
1244 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
1248 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
1249 ipcs
.u64
.ipcs_data
= CAST_USER_ADDR_T(ipcs
.u32
.ipcs_data
);
1251 /* Let us version this interface... */
1252 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
1256 SYSV_MSG_SUBSYS_LOCK();
1258 switch(ipcs
.u64
.ipcs_op
) {
1259 case IPCS_MSG_CONF
: /* Obtain global configuration data */
1260 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct msginfo
)) {
1264 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
1268 SYSV_MSG_SUBSYS_UNLOCK();
1269 error
= copyout(&msginfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1270 SYSV_MSG_SUBSYS_LOCK();
1273 case IPCS_MSG_ITER
: /* Iterate over existing segments */
1274 /* Not done up top so we can set limits via sysctl (later) */
1277 cursor
= ipcs
.u64
.ipcs_cursor
;
1278 if (cursor
< 0 || cursor
>= msginfo
.msgmni
) {
1282 if (ipcs
.u64
.ipcs_datalen
!= (int)msqid_ds_sz
) {
1286 for( ; cursor
< msginfo
.msgmni
; cursor
++) {
1287 if (msqids
[cursor
].msg_qbytes
!= 0) /* allocated */
1291 if (cursor
== msginfo
.msgmni
) {
1296 msqid_dsp
= &msqids
[cursor
]; /* default: 64 bit */
1299 * If necessary, convert the 64 bit kernel segment
1300 * descriptor to a 32 bit user one.
1302 if (!IS_64BIT_PROCESS(p
)) {
1303 msqid_ds_64to32(msqid_dsp
, &msqid_ds32
);
1304 msqid_dsp
= &msqid_ds32
;
1306 SYSV_MSG_SUBSYS_UNLOCK();
1307 error
= copyout(msqid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1310 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
1312 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
1313 ipcs
.u32
.ipcs_data
= CAST_DOWN(void *,ipcs
.u64
.ipcs_data
);
1314 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
1316 SYSV_MSG_SUBSYS_LOCK();
1324 SYSV_MSG_SUBSYS_UNLOCK();
1328 SYSCTL_DECL(_kern_sysv_ipcs
);
1329 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, msg
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1330 0, 0, IPCS_msg_sysctl
,
1331 "S,IPCS_msg_command",
1332 "ipcs msg command interface");