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();
108 lck_grp_attr_setstat(sysv_msg_subsys_lck_grp_attr
);
110 sysv_msg_subsys_lck_grp
= lck_grp_alloc_init("sysv_msg_subsys_lock", sysv_msg_subsys_lck_grp_attr
);
112 sysv_msg_subsys_lck_attr
= lck_attr_alloc_init();
113 /* lck_attr_setdebug(sysv_msg_subsys_lck_attr); */
114 lck_mtx_init(&sysv_msg_subsys_mutex
, sysv_msg_subsys_lck_grp
, sysv_msg_subsys_lck_attr
);
117 static __inline__ user_time_t
126 * NOTE: Source and target may *NOT* overlap! (target is smaller)
129 msqid_ds_64to32(struct user_msqid_ds
*in
, struct msqid_ds
*out
)
131 out
->msg_perm
= in
->msg_perm
;
132 out
->msg_qnum
= in
->msg_qnum
;
133 out
->msg_cbytes
= in
->msg_cbytes
; /* for ipcs */
134 out
->msg_qbytes
= in
->msg_qbytes
;
135 out
->msg_lspid
= in
->msg_lspid
;
136 out
->msg_lrpid
= in
->msg_lrpid
;
137 out
->msg_stime
= in
->msg_stime
; /* XXX loss of range */
138 out
->msg_rtime
= in
->msg_rtime
; /* XXX loss of range */
139 out
->msg_ctime
= in
->msg_ctime
; /* XXX loss of range */
143 * NOTE: Source and target may are permitted to overlap! (source is smaller);
144 * this works because we copy fields in order from the end of the struct to
148 msqid_ds_32to64(struct msqid_ds
*in
, struct user_msqid_ds
*out
)
150 out
->msg_ctime
= in
->msg_ctime
;
151 out
->msg_rtime
= in
->msg_rtime
;
152 out
->msg_stime
= in
->msg_stime
;
153 out
->msg_lrpid
= in
->msg_lrpid
;
154 out
->msg_lspid
= in
->msg_lspid
;
155 out
->msg_qbytes
= in
->msg_qbytes
;
156 out
->msg_cbytes
= in
->msg_cbytes
; /* for ipcs */
157 out
->msg_qnum
= in
->msg_qnum
;
158 out
->msg_perm
= in
->msg_perm
;
161 /* This routine assumes the system is locked prior to calling this routine */
163 msginit(__unused
void *dummy
)
165 static int initted
= 0;
168 /* Lazy initialization on first system call; we don't have SYSINIT(). */
173 msgpool
= (char *)_MALLOC(msginfo
.msgmax
, M_SHM
, M_WAITOK
);
174 MALLOC(msgmaps
, struct msgmap
*,
175 sizeof(struct msgmap
) * msginfo
.msgseg
,
177 MALLOC(msghdrs
, struct msg
*,
178 sizeof(struct msg
) * msginfo
.msgtql
,
180 MALLOC(msqids
, struct user_msqid_ds
*,
181 sizeof(struct user_msqid_ds
) * msginfo
.msgmni
,
185 * msginfo.msgssz should be a power of two for efficiency reasons.
186 * It is also pretty silly if msginfo.msgssz is less than 8
187 * or greater than about 256 so ...
191 while (i
< 1024 && i
!= msginfo
.msgssz
)
193 if (i
!= msginfo
.msgssz
) {
194 printf("msginfo.msgssz=%d (0x%x)\n", msginfo
.msgssz
,
196 panic("msginfo.msgssz not a small power of 2");
199 if (msginfo
.msgseg
> 32767) {
200 printf("msginfo.msgseg=%d\n", msginfo
.msgseg
);
201 panic("msginfo.msgseg > 32767");
205 panic("msgmaps is NULL");
207 for (i
= 0; i
< msginfo
.msgseg
; i
++) {
209 msgmaps
[i
-1].next
= i
;
210 msgmaps
[i
].next
= -1; /* implies entry is available */
213 nfree_msgmaps
= msginfo
.msgseg
;
216 panic("msghdrs is NULL");
218 for (i
= 0; i
< msginfo
.msgtql
; i
++) {
219 msghdrs
[i
].msg_type
= 0;
221 msghdrs
[i
-1].msg_next
= &msghdrs
[i
];
222 msghdrs
[i
].msg_next
= NULL
;
224 free_msghdrs
= &msghdrs
[0];
227 panic("msqids is NULL");
229 for (i
= 0; i
< msginfo
.msgmni
; i
++) {
230 msqids
[i
].msg_qbytes
= 0; /* implies entry is available */
231 msqids
[i
].msg_perm
.seq
= 0; /* reset to a known value */
236 * Entry point for all MSG calls
238 /* XXX actually varargs. */
240 msgsys(struct proc
*p
, struct msgsys_args
*uap
, register_t
*retval
)
242 if (uap
->which
>= sizeof(msgcalls
)/sizeof(msgcalls
[0]))
244 return ((*msgcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
248 msg_freehdr(struct msg
*msghdr
)
250 while (msghdr
->msg_ts
> 0) {
252 if (msghdr
->msg_spot
< 0 || msghdr
->msg_spot
>= msginfo
.msgseg
)
253 panic("msghdr->msg_spot out of range");
254 next
= msgmaps
[msghdr
->msg_spot
].next
;
255 msgmaps
[msghdr
->msg_spot
].next
= free_msgmaps
;
256 free_msgmaps
= msghdr
->msg_spot
;
258 msghdr
->msg_spot
= next
;
259 if (msghdr
->msg_ts
>= msginfo
.msgssz
)
260 msghdr
->msg_ts
-= msginfo
.msgssz
;
264 if (msghdr
->msg_spot
!= -1)
265 panic("msghdr->msg_spot != -1");
266 msghdr
->msg_next
= free_msghdrs
;
267 free_msghdrs
= msghdr
;
271 msgctl(struct proc
*p
, struct msgctl_args
*uap
, register_t
*retval
)
273 int msqid
= uap
->msqid
;
275 kauth_cred_t cred
= kauth_cred_get();
277 struct user_msqid_ds msqbuf
;
278 struct user_msqid_ds
*msqptr
;
279 struct user_msqid_ds umsds
;
281 SYSV_MSG_SUBSYS_LOCK();
286 printf("call to msgctl(%d, %d, 0x%qx)\n", msqid
, cmd
, uap
->buf
);
289 AUDIT_ARG(svipc_cmd
, cmd
);
290 AUDIT_ARG(svipc_id
, msqid
);
291 msqid
= IPCID_TO_IX(msqid
);
293 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
295 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
302 msqptr
= &msqids
[msqid
];
304 if (msqptr
->msg_qbytes
== 0) {
306 printf("no such msqid\n");
311 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
313 printf("wrong sequence number\n");
327 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_M
)))
330 /* Free the message headers */
331 msghdr
= msqptr
->msg_first
;
332 while (msghdr
!= NULL
) {
333 struct msg
*msghdr_tmp
;
335 /* Free the segments of each message */
336 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
339 msghdr
= msghdr
->msg_next
;
340 msg_freehdr(msghdr_tmp
);
343 if (msqptr
->msg_cbytes
!= 0)
344 panic("msg_cbytes is messed up");
345 if (msqptr
->msg_qnum
!= 0)
346 panic("msg_qnum is messed up");
348 msqptr
->msg_qbytes
= 0; /* Mark it as free */
350 wakeup((caddr_t
)msqptr
);
356 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_M
)))
359 SYSV_MSG_SUBSYS_UNLOCK();
361 if (IS_64BIT_PROCESS(p
)) {
362 eval
= copyin(uap
->buf
, &msqbuf
, sizeof(struct user_msqid_ds
));
364 eval
= copyin(uap
->buf
, &msqbuf
, sizeof(struct msqid_ds
));
365 /* convert in place; ugly, but safe */
366 msqid_ds_32to64((struct msqid_ds
*)&msqbuf
, &msqbuf
);
371 SYSV_MSG_SUBSYS_LOCK();
373 if (msqbuf
.msg_qbytes
> msqptr
->msg_qbytes
) {
374 eval
= suser(cred
, &p
->p_acflag
);
380 /* compare (msglen_t) value against restrict (int) value */
381 if (msqbuf
.msg_qbytes
> (msglen_t
)msginfo
.msgmnb
) {
383 printf("can't increase msg_qbytes beyond %d (truncating)\n",
386 msqbuf
.msg_qbytes
= msginfo
.msgmnb
; /* silently restrict qbytes to system limit */
388 if (msqbuf
.msg_qbytes
== 0) {
390 printf("can't reduce msg_qbytes to 0\n");
395 msqptr
->msg_perm
.uid
= msqbuf
.msg_perm
.uid
; /* change the owner */
396 msqptr
->msg_perm
.gid
= msqbuf
.msg_perm
.gid
; /* change the owner */
397 msqptr
->msg_perm
.mode
= (msqptr
->msg_perm
.mode
& ~0777) |
398 (msqbuf
.msg_perm
.mode
& 0777);
399 msqptr
->msg_qbytes
= msqbuf
.msg_qbytes
;
400 msqptr
->msg_ctime
= sysv_msgtime();
404 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, IPC_R
))) {
406 printf("requester doesn't have read access\n");
411 bcopy(msqptr
, &umsds
, sizeof(struct user_msqid_ds
));
413 SYSV_MSG_SUBSYS_UNLOCK();
414 if (IS_64BIT_PROCESS(p
)) {
415 eval
= copyout(&umsds
, uap
->buf
, sizeof(struct user_msqid_ds
));
417 struct msqid_ds msqid_ds32
;
418 msqid_ds_64to32(&umsds
, &msqid_ds32
);
419 eval
= copyout(&msqid_ds32
, uap
->buf
, sizeof(struct msqid_ds
));
421 SYSV_MSG_SUBSYS_LOCK();
426 printf("invalid command %d\n", cmd
);
435 SYSV_MSG_SUBSYS_UNLOCK();
440 msgget(__unused
struct proc
*p
, struct msgget_args
*uap
, register_t
*retval
)
444 int msgflg
= uap
->msgflg
;
445 kauth_cred_t cred
= kauth_cred_get();
446 struct user_msqid_ds
*msqptr
= NULL
;
448 SYSV_MSG_SUBSYS_LOCK();
452 printf("msgget(0x%x, 0%o)\n", key
, msgflg
);
455 if (key
!= IPC_PRIVATE
) {
456 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
457 msqptr
= &msqids
[msqid
];
458 if (msqptr
->msg_qbytes
!= 0 &&
459 msqptr
->msg_perm
.key
== key
)
462 if (msqid
< msginfo
.msgmni
) {
464 printf("found public key\n");
466 if ((msgflg
& IPC_CREAT
) && (msgflg
& IPC_EXCL
)) {
468 printf("not exclusive\n");
473 if ((eval
= ipcperm(cred
, &msqptr
->msg_perm
, msgflg
& 0700 ))) {
475 printf("requester doesn't have 0%o access\n",
485 printf("need to allocate the user_msqid_ds\n");
487 if (key
== IPC_PRIVATE
|| (msgflg
& IPC_CREAT
)) {
488 for (msqid
= 0; msqid
< msginfo
.msgmni
; msqid
++) {
490 * Look for an unallocated and unlocked user_msqid_ds.
491 * user_msqid_ds's can be locked by msgsnd or msgrcv
492 * while they are copying the message in/out. We
493 * can't re-use the entry until they release it.
495 msqptr
= &msqids
[msqid
];
496 if (msqptr
->msg_qbytes
== 0 &&
497 (msqptr
->msg_perm
.mode
& MSG_LOCKED
) == 0)
500 if (msqid
== msginfo
.msgmni
) {
502 printf("no more user_msqid_ds's available\n");
508 printf("msqid %d is available\n", msqid
);
510 msqptr
->msg_perm
.key
= key
;
511 msqptr
->msg_perm
.cuid
= kauth_cred_getuid(cred
);
512 msqptr
->msg_perm
.uid
= kauth_cred_getuid(cred
);
513 msqptr
->msg_perm
.cgid
= cred
->cr_gid
;
514 msqptr
->msg_perm
.gid
= cred
->cr_gid
;
515 msqptr
->msg_perm
.mode
= (msgflg
& 0777);
516 /* Make sure that the returned msqid is unique */
517 msqptr
->msg_perm
.seq
++;
518 msqptr
->msg_first
= NULL
;
519 msqptr
->msg_last
= NULL
;
520 msqptr
->msg_cbytes
= 0;
521 msqptr
->msg_qnum
= 0;
522 msqptr
->msg_qbytes
= msginfo
.msgmnb
;
523 msqptr
->msg_lspid
= 0;
524 msqptr
->msg_lrpid
= 0;
525 msqptr
->msg_stime
= 0;
526 msqptr
->msg_rtime
= 0;
527 msqptr
->msg_ctime
= sysv_msgtime();
530 printf("didn't find it and wasn't asked to create it\n");
537 /* Construct the unique msqid */
538 *retval
= IXSEQ_TO_IPCID(msqid
, msqptr
->msg_perm
);
539 AUDIT_ARG(svipc_id
, *retval
);
542 SYSV_MSG_SUBSYS_UNLOCK();
548 msgsnd(struct proc
*p
, struct msgsnd_args
*uap
, register_t
*retval
)
550 int msqid
= uap
->msqid
;
551 user_addr_t user_msgp
= uap
->msgp
;
552 size_t msgsz
= (size_t)uap
->msgsz
; /* limit to 4G */
553 int msgflg
= uap
->msgflg
;
554 int segs_needed
, eval
;
555 struct user_msqid_ds
*msqptr
;
561 SYSV_MSG_SUBSYS_LOCK();
565 printf("call to msgsnd(%d, 0x%qx, %d, %d)\n", msqid
, user_msgp
, msgsz
,
569 AUDIT_ARG(svipc_id
, msqid
);
570 msqid
= IPCID_TO_IX(msqid
);
572 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
574 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
581 msqptr
= &msqids
[msqid
];
582 if (msqptr
->msg_qbytes
== 0) {
584 printf("no such message queue id\n");
589 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
591 printf("wrong sequence number\n");
597 if ((eval
= ipcperm(kauth_cred_get(), &msqptr
->msg_perm
, IPC_W
))) {
599 printf("requester doesn't have write access\n");
604 segs_needed
= (msgsz
+ msginfo
.msgssz
- 1) / msginfo
.msgssz
;
606 printf("msgsz=%d, msgssz=%d, segs_needed=%d\n", msgsz
, msginfo
.msgssz
,
610 int need_more_resources
= 0;
614 * (inside this loop in case msg_qbytes changes while we sleep)
617 if (msgsz
> msqptr
->msg_qbytes
) {
619 printf("msgsz > msqptr->msg_qbytes\n");
625 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
) {
627 printf("msqid is locked\n");
629 need_more_resources
= 1;
631 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
) {
633 printf("msgsz + msg_cbytes > msg_qbytes\n");
635 need_more_resources
= 1;
637 if (segs_needed
> nfree_msgmaps
) {
639 printf("segs_needed > nfree_msgmaps\n");
641 need_more_resources
= 1;
643 if (free_msghdrs
== NULL
) {
645 printf("no more msghdrs\n");
647 need_more_resources
= 1;
650 if (need_more_resources
) {
653 if ((msgflg
& IPC_NOWAIT
) != 0) {
655 printf("need more resources but caller doesn't want to wait\n");
661 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0) {
663 printf("we don't own the user_msqid_ds\n");
667 /* Force later arrivals to wait for our
670 printf("we own the user_msqid_ds\n");
672 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
676 printf("goodnight\n");
678 eval
= msleep((caddr_t
)msqptr
, &sysv_msg_subsys_mutex
, (PZERO
- 4) | PCATCH
,
681 printf("good morning, eval=%d\n", eval
);
684 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
687 printf("msgsnd: interrupted system call\n");
694 * Make sure that the msq queue still exists
697 if (msqptr
->msg_qbytes
== 0) {
699 printf("msqid deleted\n");
701 /* The SVID says to return EIDRM. */
705 /* Unfortunately, BSD doesn't define that code
715 printf("got all the resources that we need\n");
722 * We have the resources that we need.
726 if (msqptr
->msg_perm
.mode
& MSG_LOCKED
)
727 panic("msg_perm.mode & MSG_LOCKED");
728 if (segs_needed
> nfree_msgmaps
)
729 panic("segs_needed > nfree_msgmaps");
730 if (msgsz
+ msqptr
->msg_cbytes
> msqptr
->msg_qbytes
)
731 panic("msgsz + msg_cbytes > msg_qbytes");
732 if (free_msghdrs
== NULL
)
733 panic("no more msghdrs");
736 * Re-lock the user_msqid_ds in case we page-fault when copying in
740 if ((msqptr
->msg_perm
.mode
& MSG_LOCKED
) != 0)
741 panic("user_msqid_ds is already locked");
742 msqptr
->msg_perm
.mode
|= MSG_LOCKED
;
745 * Allocate a message header
748 msghdr
= free_msghdrs
;
749 free_msghdrs
= msghdr
->msg_next
;
750 msghdr
->msg_spot
= -1;
751 msghdr
->msg_ts
= msgsz
;
754 * Allocate space for the message
757 while (segs_needed
> 0) {
758 if (nfree_msgmaps
<= 0)
759 panic("not enough msgmaps");
760 if (free_msgmaps
== -1)
761 panic("nil free_msgmaps");
764 panic("next too low #1");
765 if (next
>= msginfo
.msgseg
)
766 panic("next out of range #1");
768 printf("allocating segment %d to message\n", next
);
770 free_msgmaps
= msgmaps
[next
].next
;
772 msgmaps
[next
].next
= msghdr
->msg_spot
;
773 msghdr
->msg_spot
= next
;
778 * Copy in the message type. For a 64 bit process, this is 64 bits,
779 * but we only ever use the low 32 bits, so the cast is OK.
781 if (IS_64BIT_PROCESS(p
)) {
782 SYSV_MSG_SUBSYS_UNLOCK();
783 eval
= copyin(user_msgp
, &msgtype
, sizeof(msgtype
));
784 SYSV_MSG_SUBSYS_LOCK();
785 msghdr
->msg_type
= CAST_DOWN(long,msgtype
);
786 user_msgp
= user_msgp
+ sizeof(msgtype
); /* ptr math */
788 SYSV_MSG_SUBSYS_UNLOCK();
789 eval
= copyin(user_msgp
, &msghdr
->msg_type
, sizeof(long));
790 SYSV_MSG_SUBSYS_LOCK();
791 user_msgp
= user_msgp
+ sizeof(long); /* ptr math */
796 printf("error %d copying the message type\n", eval
);
799 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
800 wakeup((caddr_t
)msqptr
);
806 * Validate the message type
808 if (msghdr
->msg_type
< 1) {
810 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
811 wakeup((caddr_t
)msqptr
);
813 printf("mtype (%d) < 1\n", msghdr
->msg_type
);
820 * Copy in the message body
822 next
= msghdr
->msg_spot
;
825 /* compare input (size_t) value against restrict (int) value */
826 if (msgsz
> (size_t)msginfo
.msgssz
)
827 tlen
= msginfo
.msgssz
;
831 panic("next too low #2");
832 if (next
>= msginfo
.msgseg
)
833 panic("next out of range #2");
835 SYSV_MSG_SUBSYS_UNLOCK();
836 eval
= copyin(user_msgp
, &msgpool
[next
* msginfo
.msgssz
], tlen
);
837 SYSV_MSG_SUBSYS_LOCK();
841 printf("error %d copying in message segment\n", eval
);
844 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
845 wakeup((caddr_t
)msqptr
);
850 user_msgp
= user_msgp
+ tlen
; /* ptr math */
851 next
= msgmaps
[next
].next
;
854 panic("didn't use all the msg segments");
857 * We've got the message. Unlock the user_msqid_ds.
860 msqptr
->msg_perm
.mode
&= ~MSG_LOCKED
;
863 * Make sure that the user_msqid_ds is still allocated.
866 if (msqptr
->msg_qbytes
== 0) {
868 wakeup((caddr_t
)msqptr
);
869 /* The SVID says to return EIDRM. */
873 /* Unfortunately, BSD doesn't define that code yet! */
880 * Put the message into the queue
883 if (msqptr
->msg_first
== NULL
) {
884 msqptr
->msg_first
= msghdr
;
885 msqptr
->msg_last
= msghdr
;
887 msqptr
->msg_last
->msg_next
= msghdr
;
888 msqptr
->msg_last
= msghdr
;
890 msqptr
->msg_last
->msg_next
= NULL
;
892 msqptr
->msg_cbytes
+= msghdr
->msg_ts
;
894 msqptr
->msg_lspid
= p
->p_pid
;
895 msqptr
->msg_stime
= sysv_msgtime();
897 wakeup((caddr_t
)msqptr
);
902 SYSV_MSG_SUBSYS_UNLOCK();
908 msgrcv(struct proc
*p
, struct msgrcv_args
*uap
, user_ssize_t
*retval
)
910 int msqid
= uap
->msqid
;
911 user_addr_t user_msgp
= uap
->msgp
;
912 size_t msgsz
= (size_t)uap
->msgsz
; /* limit to 4G */
913 long msgtyp
= (long)uap
->msgtyp
; /* limit to 32 bits */
914 int msgflg
= uap
->msgflg
;
916 struct user_msqid_ds
*msqptr
;
923 SYSV_MSG_SUBSYS_LOCK();
927 printf("call to msgrcv(%d, 0x%qx, %d, %ld, %d)\n", msqid
, user_msgp
,
928 msgsz
, msgtyp
, msgflg
);
931 AUDIT_ARG(svipc_id
, msqid
);
932 msqid
= IPCID_TO_IX(msqid
);
934 if (msqid
< 0 || msqid
>= msginfo
.msgmni
) {
936 printf("msqid (%d) out of range (0<=msqid<%d)\n", msqid
,
943 msqptr
= &msqids
[msqid
];
944 if (msqptr
->msg_qbytes
== 0) {
946 printf("no such message queue id\n");
951 if (msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
953 printf("wrong sequence number\n");
959 if ((eval
= ipcperm(kauth_cred_get(), &msqptr
->msg_perm
, IPC_R
))) {
961 printf("requester doesn't have read access\n");
967 while (msghdr
== NULL
) {
969 msghdr
= msqptr
->msg_first
;
970 if (msghdr
!= NULL
) {
971 if (msgsz
< msghdr
->msg_ts
&&
972 (msgflg
& MSG_NOERROR
) == 0) {
974 printf("first message on the queue is too big (want %d, got %d)\n",
975 msgsz
, msghdr
->msg_ts
);
980 if (msqptr
->msg_first
== msqptr
->msg_last
) {
981 msqptr
->msg_first
= NULL
;
982 msqptr
->msg_last
= NULL
;
984 msqptr
->msg_first
= msghdr
->msg_next
;
985 if (msqptr
->msg_first
== NULL
)
986 panic("msg_first/last messed up #1");
990 struct msg
*previous
;
994 prev
= &(msqptr
->msg_first
);
995 while ((msghdr
= *prev
) != NULL
) {
997 * Is this message's type an exact match or is
998 * this message's type less than or equal to
999 * the absolute value of a negative msgtyp?
1000 * Note that the second half of this test can
1001 * NEVER be true if msgtyp is positive since
1002 * msg_type is always positive!
1005 if (msgtyp
== msghdr
->msg_type
||
1006 msghdr
->msg_type
<= -msgtyp
) {
1008 printf("found message type %d, requested %d\n",
1009 msghdr
->msg_type
, msgtyp
);
1011 if (msgsz
< msghdr
->msg_ts
&&
1012 (msgflg
& MSG_NOERROR
) == 0) {
1014 printf("requested message on the queue is too big (want %d, got %d)\n",
1015 msgsz
, msghdr
->msg_ts
);
1020 *prev
= msghdr
->msg_next
;
1021 if (msghdr
== msqptr
->msg_last
) {
1022 if (previous
== NULL
) {
1025 panic("msg_first/last messed up #2");
1033 panic("msg_first/last messed up #3");
1041 prev
= &(msghdr
->msg_next
);
1046 * We've either extracted the msghdr for the appropriate
1047 * message or there isn't one.
1048 * If there is one then bail out of this loop.
1055 * Hmph! No message found. Does the user want to wait?
1058 if ((msgflg
& IPC_NOWAIT
) != 0) {
1060 printf("no appropriate message found (msgtyp=%d)\n",
1063 /* The SVID says to return ENOMSG. */
1067 /* Unfortunately, BSD doesn't define that code yet! */
1074 * Wait for something to happen
1078 printf("msgrcv: goodnight\n");
1080 eval
= msleep((caddr_t
)msqptr
, &sysv_msg_subsys_mutex
, (PZERO
- 4) | PCATCH
, "msgwait",
1083 printf("msgrcv: good morning (eval=%d)\n", eval
);
1088 printf("msgsnd: interrupted system call\n");
1095 * Make sure that the msq queue still exists
1098 if (msqptr
->msg_qbytes
== 0 ||
1099 msqptr
->msg_perm
.seq
!= IPCID_TO_SEQ(uap
->msqid
)) {
1101 printf("msqid deleted\n");
1103 /* The SVID says to return EIDRM. */
1107 /* Unfortunately, BSD doesn't define that code yet! */
1115 * Return the message to the user.
1117 * First, do the bookkeeping (before we risk being interrupted).
1120 msqptr
->msg_cbytes
-= msghdr
->msg_ts
;
1122 msqptr
->msg_lrpid
= p
->p_pid
;
1123 msqptr
->msg_rtime
= sysv_msgtime();
1126 * Make msgsz the actual amount that we'll be returning.
1127 * Note that this effectively truncates the message if it is too long
1128 * (since msgsz is never increased).
1132 printf("found a message, msgsz=%d, msg_ts=%d\n", msgsz
,
1135 if (msgsz
> msghdr
->msg_ts
)
1136 msgsz
= msghdr
->msg_ts
;
1139 * Return the type to the user.
1143 * Copy out the message type. For a 64 bit process, this is 64 bits,
1144 * but we only ever use the low 32 bits, so the cast is OK.
1146 if (IS_64BIT_PROCESS(p
)) {
1147 msgtype
= msghdr
->msg_type
;
1148 SYSV_MSG_SUBSYS_UNLOCK();
1149 eval
= copyout(&msgtype
, user_msgp
, sizeof(msgtype
));
1150 SYSV_MSG_SUBSYS_LOCK();
1151 user_msgp
= user_msgp
+ sizeof(msgtype
); /* ptr math */
1153 msg_type_long
= msghdr
->msg_type
;
1154 SYSV_MSG_SUBSYS_UNLOCK();
1155 eval
= copyout(&msg_type_long
, user_msgp
, sizeof(long));
1156 SYSV_MSG_SUBSYS_LOCK();
1157 user_msgp
= user_msgp
+ sizeof(long); /* ptr math */
1162 printf("error (%d) copying out message type\n", eval
);
1164 msg_freehdr(msghdr
);
1165 wakeup((caddr_t
)msqptr
);
1172 * Return the segments to the user
1175 next
= msghdr
->msg_spot
;
1176 for (len
= 0; len
< msgsz
; len
+= msginfo
.msgssz
) {
1179 /* compare input (size_t) value against restrict (int) value */
1180 if (msgsz
> (size_t)msginfo
.msgssz
)
1181 tlen
= msginfo
.msgssz
;
1185 panic("next too low #3");
1186 if (next
>= msginfo
.msgseg
)
1187 panic("next out of range #3");
1188 SYSV_MSG_SUBSYS_UNLOCK();
1189 eval
= copyout(&msgpool
[next
* msginfo
.msgssz
],
1191 SYSV_MSG_SUBSYS_LOCK();
1194 printf("error (%d) copying out message segment\n",
1197 msg_freehdr(msghdr
);
1198 wakeup((caddr_t
)msqptr
);
1201 user_msgp
= user_msgp
+ tlen
; /* ptr math */
1202 next
= msgmaps
[next
].next
;
1206 * Done, return the actual number of bytes copied out.
1209 msg_freehdr(msghdr
);
1210 wakeup((caddr_t
)msqptr
);
1214 SYSV_MSG_SUBSYS_UNLOCK();
1219 IPCS_msg_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
1220 __unused
int arg2
, struct sysctl_req
*req
)
1225 struct IPCS_command u32
;
1226 struct user_IPCS_command u64
;
1228 struct msqid_ds msqid_ds32
; /* post conversion, 32 bit version */
1230 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
1231 size_t msqid_ds_sz
= sizeof(struct user_msqid_ds
);
1232 struct proc
*p
= current_proc();
1234 if (!IS_64BIT_PROCESS(p
)) {
1235 ipcs_sz
= sizeof(struct IPCS_command
);
1236 msqid_ds_sz
= sizeof(struct msqid_ds
);
1239 /* Copy in the command structure */
1240 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
1244 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
1245 ipcs
.u64
.ipcs_data
= CAST_USER_ADDR_T(ipcs
.u32
.ipcs_data
);
1247 /* Let us version this interface... */
1248 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
1252 SYSV_MSG_SUBSYS_LOCK();
1254 switch(ipcs
.u64
.ipcs_op
) {
1255 case IPCS_MSG_CONF
: /* Obtain global configuration data */
1256 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct msginfo
)) {
1260 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
1264 SYSV_MSG_SUBSYS_UNLOCK();
1265 error
= copyout(&msginfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1266 SYSV_MSG_SUBSYS_LOCK();
1269 case IPCS_MSG_ITER
: /* Iterate over existing segments */
1270 /* Not done up top so we can set limits via sysctl (later) */
1273 cursor
= ipcs
.u64
.ipcs_cursor
;
1274 if (cursor
< 0 || cursor
>= msginfo
.msgmni
) {
1278 if (ipcs
.u64
.ipcs_datalen
!= (int)msqid_ds_sz
) {
1282 for( ; cursor
< msginfo
.msgmni
; cursor
++) {
1283 if (msqids
[cursor
].msg_qbytes
!= 0) /* allocated */
1287 if (cursor
== msginfo
.msgmni
) {
1292 msqid_dsp
= &msqids
[cursor
]; /* default: 64 bit */
1295 * If necessary, convert the 64 bit kernel segment
1296 * descriptor to a 32 bit user one.
1298 if (!IS_64BIT_PROCESS(p
)) {
1299 msqid_ds_64to32(msqid_dsp
, &msqid_ds32
);
1300 msqid_dsp
= &msqid_ds32
;
1302 SYSV_MSG_SUBSYS_UNLOCK();
1303 error
= copyout(msqid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
1306 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
1308 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
1309 ipcs
.u32
.ipcs_data
= CAST_DOWN(void *,ipcs
.u64
.ipcs_data
);
1310 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
1312 SYSV_MSG_SUBSYS_LOCK();
1320 SYSV_MSG_SUBSYS_UNLOCK();
1324 SYSCTL_DECL(_kern_sysv_ipcs
);
1325 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, msg
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1326 0, 0, IPCS_msg_sysctl
,
1327 "S,IPCS_msg_command",
1328 "ipcs msg command interface");