2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Implementation of SVID messages
25 * Author: Daniel Boulet
27 * Copyright 1993 Daniel Boulet and RTMX Inc.
29 * This system call was implemented by Daniel Boulet under contract from RTMX.
31 * Redistribution and use in source forms, with and without modification,
32 * are permitted provided that this entire comment appears intact.
34 * Redistribution in binary form may occur without any restrictions.
35 * Obviously, it would be nice if you gave credit where credit is due
36 * but requiring it would be too onerous.
38 * This software is provided ``AS IS'' without any warranties of any kind.
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/proc_internal.h>
45 #include <sys/kauth.h>
47 #include <sys/malloc.h>
48 #include <mach/mach_types.h>
50 #include <bsm/audit_kernel.h>
52 #include <sys/filedesc.h>
53 #include <sys/file_internal.h>
54 #include <sys/sysctl.h>
55 #include <sys/sysproto.h>
58 static void msginit(void *);
63 static void msg_freehdr(struct msg
*msghdr
);
65 typedef int sy_call_t(struct proc
*, void *, int *);
67 /* XXX casting to (sy_call_t *) is bogus, as usual. */
68 static sy_call_t
*msgcalls
[] = {
69 (sy_call_t
*)msgctl
, (sy_call_t
*)msgget
,
70 (sy_call_t
*)msgsnd
, (sy_call_t
*)msgrcv
73 static int nfree_msgmaps
; /* # of free map entries */
74 static short free_msgmaps
; /* free map entries list head */
75 static struct msg
*free_msghdrs
; /* list of free msg headers */
76 char *msgpool
; /* MSGMAX byte long msg buffer pool */
77 struct msgmap
*msgmaps
; /* MSGSEG msgmap structures */
78 struct msg
*msghdrs
; /* MSGTQL msg headers */
79 struct user_msqid_ds
*msqids
; /* MSGMNI user_msqid_ds struct's */
81 static lck_grp_t
*sysv_msg_subsys_lck_grp
;
82 static lck_grp_attr_t
*sysv_msg_subsys_lck_grp_attr
;
83 static lck_attr_t
*sysv_msg_subsys_lck_attr
;
84 static lck_mtx_t sysv_msg_subsys_mutex
;
86 #define SYSV_MSG_SUBSYS_LOCK() lck_mtx_lock(&sysv_msg_subsys_mutex)
87 #define SYSV_MSG_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_msg_subsys_mutex)
89 void sysv_msg_lock_init(void);
92 #ifdef __APPLE_API_PRIVATE
93 struct msginfo msginfo
= {
94 MSGMAX
, /* = (MSGSSZ*MSGSEG) : max chars in a message */
95 MSGMNI
, /* = 40 : max message queue identifiers */
96 MSGMNB
, /* = 2048 : max chars in a queue */
97 MSGTQL
, /* = 40 : max messages in system */
98 MSGSSZ
, /* = 8 : size of a message segment (2^N long) */
99 MSGSEG
/* = 2048 : number of message segments */
101 #endif /* __APPLE_API_PRIVATE */
103 /* Initialize the mutex governing access to the SysV msg subsystem */
104 __private_extern__
void
105 sysv_msg_lock_init( void )
107 sysv_msg_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
109 sysv_msg_subsys_lck_grp
= lck_grp_alloc_init("sysv_msg_subsys_lock", sysv_msg_subsys_lck_grp_attr
);
111 sysv_msg_subsys_lck_attr
= lck_attr_alloc_init();
112 lck_mtx_init(&sysv_msg_subsys_mutex
, sysv_msg_subsys_lck_grp
, sysv_msg_subsys_lck_attr
);
115 static __inline__ user_time_t
124 * NOTE: Source and target may *NOT* overlap! (target is smaller)
127 msqid_ds_64to32(struct user_msqid_ds
*in
, struct msqid_ds
*out
)
129 out
->msg_perm
= in
->msg_perm
;
130 out
->msg_qnum
= in
->msg_qnum
;
131 out
->msg_cbytes
= in
->msg_cbytes
; /* for ipcs */
132 out
->msg_qbytes
= in
->msg_qbytes
;
133 out
->msg_lspid
= in
->msg_lspid
;
134 out
->msg_lrpid
= in
->msg_lrpid
;
135 out
->msg_stime
= in
->msg_stime
; /* XXX loss of range */
136 out
->msg_rtime
= in
->msg_rtime
; /* XXX loss of range */
137 out
->msg_ctime
= in
->msg_ctime
; /* XXX loss of range */
141 * NOTE: Source and target may are permitted to overlap! (source is smaller);
142 * this works because we copy fields in order from the end of the struct to
146 msqid_ds_32to64(struct msqid_ds
*in
, struct user_msqid_ds
*out
)
148 out
->msg_ctime
= in
->msg_ctime
;
149 out
->msg_rtime
= in
->msg_rtime
;
150 out
->msg_stime
= in
->msg_stime
;
151 out
->msg_lrpid
= in
->msg_lrpid
;
152 out
->msg_lspid
= in
->msg_lspid
;
153 out
->msg_qbytes
= in
->msg_qbytes
;
154 out
->msg_cbytes
= in
->msg_cbytes
; /* for ipcs */
155 out
->msg_qnum
= in
->msg_qnum
;
156 out
->msg_perm
= in
->msg_perm
;
159 /* This routine assumes the system is locked prior to calling this routine */
161 msginit(__unused
void *dummy
)
163 static int initted
= 0;
166 /* Lazy initialization on first system call; we don't have SYSINIT(). */
171 msgpool
= (char *)_MALLOC(msginfo
.msgmax
, M_SHM
, M_WAITOK
);
172 MALLOC(msgmaps
, struct msgmap
*,
173 sizeof(struct msgmap
) * msginfo
.msgseg
,
175 MALLOC(msghdrs
, struct msg
*,
176 sizeof(struct msg
) * msginfo
.msgtql
,
178 MALLOC(msqids
, struct user_msqid_ds
*,
179 sizeof(struct user_msqid_ds
) * msginfo
.msgmni
,
183 * msginfo.msgssz should be a power of two for efficiency reasons.
184 * It is also pretty silly if msginfo.msgssz is less than 8
185 * or greater than about 256 so ...
189 while (i
< 1024 && i
!= msginfo
.msgssz
)
191 if (i
!= msginfo
.msgssz
) {
192 printf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
194 panic("msginfo.msgssz not a small power of 2");
197 if (msginfo
.msgseg
> 32767) {
198 printf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
199 panic("msginfo.msgseg > 32767");
203 panic("msgmaps is NULL");
205 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
207 msgmaps
[i
-1].next
= i
;
208 msgmaps
[i
].next
= -1; /* implies entry is available */
211 nfree_msgmaps
= msginfo
.msgseg
;
214 panic("msghdrs is NULL");
216 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
217 msghdrs
[i
].msg_type
= 0;
219 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
220 msghdrs
[i
].msg_next
= NULL
;
222 free_msghdrs
= &msghdrs
[0];
225 panic("msqids is NULL");
227 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
228 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
229 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
234 * Entry point for all MSG calls
236 /* XXX actually varargs. */
238 msgsys(struct proc
*p
, struct msgsys_args
*uap
, register_t
*retval
)
240 if (uap
->which
>= sizeof(msgcalls
)/sizeof(msgcalls
[0]))
242 return ((*msgcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
246 msg_freehdr(struct msg
*msghdr
)
248 while (msghdr
->msg_ts
> 0) {
250 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
251 panic("msghdr->msg_spot out of range");
252 next
= msgmaps
[msghdr
->msg_spot
].next
;
253 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
254 free_msgmaps
= msghdr
->msg_spot
;
256 msghdr
->msg_spot
= next
;
257 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
258 msghdr
->msg_ts
-= msginfo
.msgssz
;
262 if (msghdr
->msg_spot
!= -1)
263 panic("msghdr->msg_spot != -1");
264 msghdr
->msg_next
= free_msghdrs
;
265 free_msghdrs
= msghdr
;
269 msgctl(struct proc
*p
, struct msgctl_args
*uap
, register_t
*retval
)
271 int msqid
= uap
->msqid
;
273 kauth_cred_t cred
= kauth_cred_get();
275 struct user_msqid_ds msqbuf
;
276 struct user_msqid_ds
*msqptr
;
277 struct user_msqid_ds umsds
;
279 SYSV_MSG_SUBSYS_LOCK();
284 printf("call to msgctl(%d, %d, 0x%qx)\n", msqid
, cmd
, uap
->buf
);
287 AUDIT_ARG(svipc_cmd
, cmd
);
288 AUDIT_ARG(svipc_id
, msqid
);
289 msqid
= IPCID_TO_IX(msqid
);
291 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
293 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
300 msqptr
= &msqids
[msqid
];
302 if (msqptr
->msg_qbytes
== 0) {
304 printf("no such msqid\n");
309 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
311 printf("wrong sequence number\n");
325 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_M
)))
328 /* Free the message headers */
329 msghdr
= msqptr
->msg_first
;
330 while (msghdr
!= NULL
) {
331 struct msg
*msghdr_tmp
;
333 /* Free the segments of each message */
334 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
337 msghdr
= msghdr
->msg_next
;
338 msg_freehdr(msghdr_tmp
);
341 if (msqptr
->msg_cbytes
!= 0)
342 panic("msg_cbytes is messed up");
343 if (msqptr
->msg_qnum
!= 0)
344 panic("msg_qnum is messed up");
346 msqptr
->msg_qbytes
= 0; /* Mark it as free */
348 wakeup((caddr_t
)msqptr
);
354 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_M
)))
357 SYSV_MSG_SUBSYS_UNLOCK();
359 if (IS_64BIT_PROCESS(p
)) {
360 eval
= copyin(uap
->buf
, &msqbuf
, sizeof(struct user_msqid_ds
));
362 eval
= copyin(uap
->buf
, &msqbuf
, sizeof(struct msqid_ds
));
363 /* convert in place; ugly, but safe */
364 msqid_ds_32to64((struct msqid_ds
*)&msqbuf
, &msqbuf
);
369 SYSV_MSG_SUBSYS_LOCK();
371 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
372 eval
= suser(cred
, &p
->p_acflag
);
378 /* compare (msglen_t) value against restrict (int) value */
379 if (msqbuf
.msg_qbytes
> (msglen_t
)msginfo
.msgmnb
) {
381 printf("can't increase msg_qbytes beyond %d (truncating)\n",
384 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
386 if (msqbuf
.msg_qbytes
== 0) {
388 printf("can't reduce msg_qbytes to 0\n");
393 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
394 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
395 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
396 (msqbuf
.msg_perm
.mode
& 0777);
397 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
398 msqptr
->msg_ctime
= sysv_msgtime();
402 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_R
))) {
404 printf("requester doesn't have read access\n");
409 bcopy(msqptr
, &umsds
, sizeof(struct user_msqid_ds
));
411 SYSV_MSG_SUBSYS_UNLOCK();
412 if (IS_64BIT_PROCESS(p
)) {
413 eval
= copyout(&umsds
, uap
->buf
, sizeof(struct user_msqid_ds
));
415 struct msqid_ds msqid_ds32
;
416 msqid_ds_64to32(&umsds
, &msqid_ds32
);
417 eval
= copyout(&msqid_ds32
, uap
->buf
, sizeof(struct msqid_ds
));
419 SYSV_MSG_SUBSYS_LOCK();
424 printf("invalid command %d\n", cmd
);
433 SYSV_MSG_SUBSYS_UNLOCK();
438 msgget(__unused
struct proc
*p
, struct msgget_args
*uap
, register_t
*retval
)
442 int msgflg
= uap
->msgflg
;
443 kauth_cred_t cred
= kauth_cred_get();
444 struct user_msqid_ds
*msqptr
= NULL
;
446 SYSV_MSG_SUBSYS_LOCK();
450 printf("msgget(0x%x, 0%o)\n", key
, msgflg
);
453 if (key
!= IPC_PRIVATE
) {
454 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
455 msqptr
= &msqids
[msqid
];
456 if (msqptr
->msg_qbytes
!= 0 &&
457 msqptr
->msg_perm
.key
== key
)
460 if (msqid
< msginfo
.msgmni
) {
462 printf("found public key\n");
464 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
466 printf("not exclusive\n");
471 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
473 printf("requester doesn't have 0%o access\n",
483 printf("need to allocate the user_msqid_ds\n");
485 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
486 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
488 * Look for an unallocated and unlocked user_msqid_ds.
489 * user_msqid_ds's can be locked by msgsnd or msgrcv
490 * while they are copying the message in/out. We
491 * can't re-use the entry until they release it.
493 msqptr
= &msqids
[msqid
];
494 if (msqptr
->msg_qbytes
== 0 &&
495 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
498 if (msqid
== msginfo
.msgmni
) {
500 printf("no more user_msqid_ds's available\n");
506 printf("msqid %d is available\n", msqid
);
508 msqptr
->msg_perm
.key
= key
;
509 msqptr
->msg_perm
.cuid
= kauth_cred_getuid(cred
);
510 msqptr
->msg_perm
.uid
= kauth_cred_getuid(cred
);
511 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
512 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
513 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
514 /* Make sure that the returned msqid is unique */
515 msqptr
->msg_perm
.seq
++;
516 msqptr
->msg_first
= NULL
;
517 msqptr
->msg_last
= NULL
;
518 msqptr
->msg_cbytes
= 0;
519 msqptr
->msg_qnum
= 0;
520 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
521 msqptr
->msg_lspid
= 0;
522 msqptr
->msg_lrpid
= 0;
523 msqptr
->msg_stime
= 0;
524 msqptr
->msg_rtime
= 0;
525 msqptr
->msg_ctime
= sysv_msgtime();
528 printf("didn't find it and wasn't asked to create it\n");
535 /* Construct the unique msqid */
536 *retval
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
537 AUDIT_ARG(svipc_id
, *retval
);
540 SYSV_MSG_SUBSYS_UNLOCK();
546 msgsnd(struct proc
*p
, struct msgsnd_args
*uap
, register_t
*retval
)
548 int msqid
= uap
->msqid
;
549 user_addr_t user_msgp
= uap
->msgp
;
550 size_t msgsz
= (size_t)uap
->msgsz
; /* limit to 4G */
551 int msgflg
= uap
->msgflg
;
552 int segs_needed
, eval
;
553 struct user_msqid_ds
*msqptr
;
559 SYSV_MSG_SUBSYS_LOCK();
563 printf("call to msgsnd(%d, 0x%qx, %d, %d)\n", msqid
, user_msgp
, msgsz
,
567 AUDIT_ARG(svipc_id
, msqid
);
568 msqid
= IPCID_TO_IX(msqid
);
570 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
572 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
579 msqptr
= &msqids
[msqid
];
580 if (msqptr
->msg_qbytes
== 0) {
582 printf("no such message queue id\n");
587 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
589 printf("wrong sequence number\n");
595 if ((eval
= ipcperm(kauth_cred_get(), &msqptr
->msg_perm
, IPC_W
))) {
597 printf("requester doesn't have write access\n");
602 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
604 printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
608 int need_more_resources
= 0;
612 * (inside this loop in case msg_qbytes changes while we sleep)
615 if (msgsz
> msqptr
->msg_qbytes
) {
617 printf("msgsz > msqptr->msg_qbytes\n");
623 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
625 printf("msqid is locked\n");
627 need_more_resources
= 1;
629 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
631 printf("msgsz + msg_cbytes > msg_qbytes\n");
633 need_more_resources
= 1;
635 if (segs_needed
> nfree_msgmaps
) {
637 printf("segs_needed > nfree_msgmaps\n");
639 need_more_resources
= 1;
641 if (free_msghdrs
== NULL
) {
643 printf("no more msghdrs\n");
645 need_more_resources
= 1;
648 if (need_more_resources
) {
651 if ((msgflg
& IPC_NOWAIT
) != 0) {
653 printf("need more resources but caller doesn't want to wait\n");
659 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
661 printf("we don't own the user_msqid_ds\n");
665 /* Force later arrivals to wait for our
668 printf("we own the user_msqid_ds\n");
670 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
674 printf("goodnight\n");
676 eval
= msleep((caddr_t
)msqptr
, &sysv_msg_subsys_mutex
, (PZERO
- 4) | PCATCH
,
679 printf("good morning, eval=%d\n", eval
);
682 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
685 printf("msgsnd: interrupted system call\n");
692 * Make sure that the msq queue still exists
695 if (msqptr
->msg_qbytes
== 0) {
697 printf("msqid deleted\n");
699 /* The SVID says to return EIDRM. */
703 /* Unfortunately, BSD doesn't define that code
713 printf("got all the resources that we need\n");
720 * We have the resources that we need.
724 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
725 panic("msg_perm.mode & MSG_LOCKED");
726 if (segs_needed
> nfree_msgmaps
)
727 panic("segs_needed > nfree_msgmaps");
728 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
729 panic("msgsz + msg_cbytes > msg_qbytes");
730 if (free_msghdrs
== NULL
)
731 panic("no more msghdrs");
734 * Re-lock the user_msqid_ds in case we page-fault when copying in
738 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
739 panic("user_msqid_ds is already locked");
740 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
743 * Allocate a message header
746 msghdr
= free_msghdrs
;
747 free_msghdrs
= msghdr
->msg_next
;
748 msghdr
->msg_spot
= -1;
749 msghdr
->msg_ts
= msgsz
;
752 * Allocate space for the message
755 while (segs_needed
> 0) {
756 if (nfree_msgmaps
<= 0)
757 panic("not enough msgmaps");
758 if (free_msgmaps
== -1)
759 panic("nil free_msgmaps");
762 panic("next too low #1");
763 if (next
>= msginfo
.msgseg
)
764 panic("next out of range #1");
766 printf("allocating segment %d to message\n", next
);
768 free_msgmaps
= msgmaps
[next
].next
;
770 msgmaps
[next
].next
= msghdr
->msg_spot
;
771 msghdr
->msg_spot
= next
;
776 * Copy in the message type. For a 64 bit process, this is 64 bits,
777 * but we only ever use the low 32 bits, so the cast is OK.
779 if (IS_64BIT_PROCESS(p
)) {
780 SYSV_MSG_SUBSYS_UNLOCK();
781 eval
= copyin(user_msgp
, &msgtype
, sizeof(msgtype
));
782 SYSV_MSG_SUBSYS_LOCK();
783 msghdr
->msg_type
= CAST_DOWN(long,msgtype
);
784 user_msgp
= user_msgp
+ sizeof(msgtype
); /* ptr math */
786 SYSV_MSG_SUBSYS_UNLOCK();
787 eval
= copyin(user_msgp
, &msghdr
->msg_type
, sizeof(long));
788 SYSV_MSG_SUBSYS_LOCK();
789 user_msgp
= user_msgp
+ sizeof(long); /* ptr math */
794 printf("error %d copying the message type\n", eval
);
797 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
798 wakeup((caddr_t
)msqptr
);
804 * Validate the message type
806 if (msghdr
->msg_type
< 1) {
808 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
809 wakeup((caddr_t
)msqptr
);
811 printf("mtype (%d) < 1\n", msghdr
->msg_type
);
818 * Copy in the message body
820 next
= msghdr
->msg_spot
;
823 /* compare input (size_t) value against restrict (int) value */
824 if (msgsz
> (size_t)msginfo
.msgssz
)
825 tlen
= msginfo
.msgssz
;
829 panic("next too low #2");
830 if (next
>= msginfo
.msgseg
)
831 panic("next out of range #2");
833 SYSV_MSG_SUBSYS_UNLOCK();
834 eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
], tlen
);
835 SYSV_MSG_SUBSYS_LOCK();
839 printf("error %d copying in message segment\n", eval
);
842 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
843 wakeup((caddr_t
)msqptr
);
848 user_msgp
= user_msgp
+ tlen
; /* ptr math */
849 next
= msgmaps
[next
].next
;
852 panic("didn't use all the msg segments");
855 * We've got the message. Unlock the user_msqid_ds.
858 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
861 * Make sure that the user_msqid_ds is still allocated.
864 if (msqptr
->msg_qbytes
== 0) {
866 wakeup((caddr_t
)msqptr
);
867 /* The SVID says to return EIDRM. */
871 /* Unfortunately, BSD doesn't define that code yet! */
878 * Put the message into the queue
881 if (msqptr
->msg_first
== NULL
) {
882 msqptr
->msg_first
= msghdr
;
883 msqptr
->msg_last
= msghdr
;
885 msqptr
->msg_last
->msg_next
= msghdr
;
886 msqptr
->msg_last
= msghdr
;
888 msqptr
->msg_last
->msg_next
= NULL
;
890 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
892 msqptr
->msg_lspid
= p
->p_pid
;
893 msqptr
->msg_stime
= sysv_msgtime();
895 wakeup((caddr_t
)msqptr
);
900 SYSV_MSG_SUBSYS_UNLOCK();
906 msgrcv(struct proc
*p
, struct msgrcv_args
*uap
, user_ssize_t
*retval
)
908 int msqid
= uap
->msqid
;
909 user_addr_t user_msgp
= uap
->msgp
;
910 size_t msgsz
= (size_t)uap
->msgsz
; /* limit to 4G */
911 long msgtyp
= (long)uap
->msgtyp
; /* limit to 32 bits */
912 int msgflg
= uap
->msgflg
;
914 struct user_msqid_ds
*msqptr
;
921 SYSV_MSG_SUBSYS_LOCK();
925 printf("call to msgrcv(%d, 0x%qx, %d, %ld, %d)\n", msqid
, user_msgp
,
926 msgsz
, msgtyp
, msgflg
);
929 AUDIT_ARG(svipc_id
, msqid
);
930 msqid
= IPCID_TO_IX(msqid
);
932 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
934 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
941 msqptr
= &msqids
[msqid
];
942 if (msqptr
->msg_qbytes
== 0) {
944 printf("no such message queue id\n");
949 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
951 printf("wrong sequence number\n");
957 if ((eval
= ipcperm(kauth_cred_get(), &msqptr
->msg_perm
, IPC_R
))) {
959 printf("requester doesn't have read access\n");
965 while (msghdr
== NULL
) {
967 msghdr
= msqptr
->msg_first
;
968 if (msghdr
!= NULL
) {
969 if (msgsz
< msghdr
->msg_ts
&&
970 (msgflg
& MSG_NOERROR
) == 0) {
972 printf("first message on the queue is too big (want %d, got %d)\n",
973 msgsz
, msghdr
->msg_ts
);
978 if (msqptr
->msg_first
== msqptr
->msg_last
) {
979 msqptr
->msg_first
= NULL
;
980 msqptr
->msg_last
= NULL
;
982 msqptr
->msg_first
= msghdr
->msg_next
;
983 if (msqptr
->msg_first
== NULL
)
984 panic("msg_first/last messed up #1");
988 struct msg
*previous
;
992 prev
= &(msqptr
->msg_first
);
993 while ((msghdr
= *prev
) != NULL
) {
995 * Is this message's type an exact match or is
996 * this message's type less than or equal to
997 * the absolute value of a negative msgtyp?
998 * Note that the second half of this test can
999 * NEVER be true if msgtyp is positive since
1000 * msg_type is always positive!
1003 if (msgtyp
== msghdr
->msg_type
||
1004 msghdr
->msg_type
<= -msgtyp
) {
1006 printf("found message type %d, requested %d\n",
1007 msghdr
->msg_type
, msgtyp
);
1009 if (msgsz
< msghdr
->msg_ts
&&
1010 (msgflg
& MSG_NOERROR
) == 0) {
1012 printf("requested message on the queue is too big (want %d, got %d)\n",
1013 msgsz
, msghdr
->msg_ts
);
1018 *prev
= msghdr
->msg_next
;
1019 if (msghdr
== msqptr
->msg_last
) {
1020 if (previous
== NULL
) {
1023 panic("msg_first/last messed up #2");
1031 panic("msg_first/last messed up #3");
1039 prev
= &(msghdr
->msg_next
);
1044 * We've either extracted the msghdr for the appropriate
1045 * message or there isn't one.
1046 * If there is one then bail out of this loop.
1053 * Hmph! No message found. Does the user want to wait?
1056 if ((msgflg
& IPC_NOWAIT
) != 0) {
1058 printf("no appropriate message found (msgtyp=%d)\n",
1061 /* The SVID says to return ENOMSG. */
1065 /* Unfortunately, BSD doesn't define that code yet! */
1072 * Wait for something to happen
1076 printf("msgrcv: goodnight\n");
1078 eval
= msleep((caddr_t
)msqptr
, &sysv_msg_subsys_mutex
, (PZERO
- 4) | PCATCH
, "msgwait",
1081 printf("msgrcv: good morning (eval=%d)\n", eval
);
1086 printf("msgsnd: interrupted system call\n");
1093 * Make sure that the msq queue still exists
1096 if (msqptr
->msg_qbytes
== 0 ||
1097 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
1099 printf("msqid deleted\n");
1101 /* The SVID says to return EIDRM. */
1105 /* Unfortunately, BSD doesn't define that code yet! */
1113 * Return the message to the user.
1115 * First, do the bookkeeping (before we risk being interrupted).
1118 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
1120 msqptr
->msg_lrpid
= p
->p_pid
;
1121 msqptr
->msg_rtime
= sysv_msgtime();
1124 * Make msgsz the actual amount that we'll be returning.
1125 * Note that this effectively truncates the message if it is too long
1126 * (since msgsz is never increased).
1130 printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
1133 if (msgsz
> msghdr
->msg_ts
)
1134 msgsz
= msghdr
->msg_ts
;
1137 * Return the type to the user.
1141 * Copy out the message type. For a 64 bit process, this is 64 bits,
1142 * but we only ever use the low 32 bits, so the cast is OK.
1144 if (IS_64BIT_PROCESS(p
)) {
1145 msgtype
= msghdr
->msg_type
;
1146 SYSV_MSG_SUBSYS_UNLOCK();
1147 eval
= copyout(&msgtype
, user_msgp
, sizeof(msgtype
));
1148 SYSV_MSG_SUBSYS_LOCK();
1149 user_msgp
= user_msgp
+ sizeof(msgtype
); /* ptr math */
1151 msg_type_long
= msghdr
->msg_type
;
1152 SYSV_MSG_SUBSYS_UNLOCK();
1153 eval
= copyout(&msg_type_long
, user_msgp
, sizeof(long));
1154 SYSV_MSG_SUBSYS_LOCK();
1155 user_msgp
= user_msgp
+ sizeof(long); /* ptr math */
1160 printf("error (%d) copying out message type\n", eval
);
1162 msg_freehdr(msghdr
);
1163 wakeup((caddr_t
)msqptr
);
1170 * Return the segments to the user
1173 next
= msghdr
->msg_spot
;
1174 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1177 /* compare input (size_t) value against restrict (int) value */
1178 if (msgsz
> (size_t)msginfo
.msgssz
)
1179 tlen
= msginfo
.msgssz
;
1183 panic("next too low #3");
1184 if (next
>= msginfo
.msgseg
)
1185 panic("next out of range #3");
1186 SYSV_MSG_SUBSYS_UNLOCK();
1187 eval
= copyout(&msgpool
[next
* msginfo
.msgssz
],
1189 SYSV_MSG_SUBSYS_LOCK();
1192 printf("error (%d) copying out message segment\n",
1195 msg_freehdr(msghdr
);
1196 wakeup((caddr_t
)msqptr
);
1199 user_msgp
= user_msgp
+ tlen
; /* ptr math */
1200 next
= msgmaps
[next
].next
;
1204 * Done, return the actual number of bytes copied out.
1207 msg_freehdr(msghdr
);
1208 wakeup((caddr_t
)msqptr
);
1212 SYSV_MSG_SUBSYS_UNLOCK();
1217 IPCS_msg_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
1218 __unused
int arg2
, struct sysctl_req
*req
)
1223 struct IPCS_command u32
;
1224 struct user_IPCS_command u64
;
1226 struct msqid_ds msqid_ds32
; /* post conversion, 32 bit version */
1228 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
1229 size_t msqid_ds_sz
= sizeof(struct user_msqid_ds
);
1230 struct proc
*p
= current_proc();
1232 if (!IS_64BIT_PROCESS(p
)) {
1233 ipcs_sz
= sizeof(struct IPCS_command
);
1234 msqid_ds_sz
= sizeof(struct msqid_ds
);
1237 /* Copy in the command structure */
1238 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
1242 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
1243 ipcs
.u64
.ipcs_data
= CAST_USER_ADDR_T(ipcs
.u32
.ipcs_data
);
1245 /* Let us version this interface... */
1246 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
1250 SYSV_MSG_SUBSYS_LOCK();
1252 switch(ipcs
.u64
.ipcs_op
) {
1253 case IPCS_MSG_CONF
: /* Obtain global configuration data */
1254 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct msginfo
)) {
1258 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
1262 SYSV_MSG_SUBSYS_UNLOCK();
1263 error
= copyout(&msginfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1264 SYSV_MSG_SUBSYS_LOCK();
1267 case IPCS_MSG_ITER
: /* Iterate over existing segments */
1268 /* Not done up top so we can set limits via sysctl (later) */
1271 cursor
= ipcs
.u64
.ipcs_cursor
;
1272 if (cursor
< 0 || cursor
>= msginfo
.msgmni
) {
1276 if (ipcs
.u64
.ipcs_datalen
!= (int)msqid_ds_sz
) {
1280 for( ; cursor
< msginfo
.msgmni
; cursor
++) {
1281 if (msqids
[cursor
].msg_qbytes
!= 0) /* allocated */
1285 if (cursor
== msginfo
.msgmni
) {
1290 msqid_dsp
= &msqids
[cursor
]; /* default: 64 bit */
1293 * If necessary, convert the 64 bit kernel segment
1294 * descriptor to a 32 bit user one.
1296 if (!IS_64BIT_PROCESS(p
)) {
1297 msqid_ds_64to32(msqid_dsp
, &msqid_ds32
);
1298 msqid_dsp
= &msqid_ds32
;
1300 SYSV_MSG_SUBSYS_UNLOCK();
1301 error
= copyout(msqid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1304 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
1306 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
1307 ipcs
.u32
.ipcs_data
= CAST_DOWN(void *,ipcs
.u64
.ipcs_data
);
1308 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
1310 SYSV_MSG_SUBSYS_LOCK();
1318 SYSV_MSG_SUBSYS_UNLOCK();
1322 SYSCTL_DECL(_kern_sysv_ipcs
);
1323 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, msg
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1324 0, 0, IPCS_msg_sysctl
,
1325 "S,IPCS_msg_command",
1326 "ipcs msg command interface");