+static int
+filt_machportprocess(
+ struct knote *kn,
+ struct filt_process_s *process_data,
+ struct kevent_internal_s *kev)
+{
+ ipc_mqueue_t mqueue = kn->kn_ptr.p_mqueue;
+ ipc_object_t object = mqueue_to_object(mqueue);
+ thread_t self = current_thread();
+ boolean_t used_filtprocess_data = FALSE;
+
+ wait_result_t wresult;
+ mach_msg_option_t option;
+ mach_vm_address_t addr;
+ mach_msg_size_t size;
+
+ imq_lock(mqueue);
+
+ /* Capture current state */
+ *kev = kn->kn_kevent;
+
+ /* If already deallocated/moved return one last EOF event */
+ if (kev->flags & EV_EOF) {
+ imq_unlock(mqueue);
+ return 1;
+ }
+
+ /*
+ * Only honor supported receive options. If no options are
+ * provided, just force a MACH_RCV_TOO_LARGE to detect the
+ * name of the port and sizeof the waiting message.
+ */
+ option = kn->kn_sfflags & (MACH_RCV_MSG|MACH_RCV_LARGE|MACH_RCV_LARGE_IDENTITY|
+ MACH_RCV_TRAILER_MASK|MACH_RCV_VOUCHER);
+
+ if (option & MACH_RCV_MSG) {
+ addr = (mach_vm_address_t) kn->kn_ext[0];
+ size = (mach_msg_size_t) kn->kn_ext[1];
+
+ /*
+ * If the kevent didn't specify a buffer and length, carve a buffer
+ * from the filter processing data according to the flags.
+ */
+ if (size == 0 && process_data != NULL) {
+ used_filtprocess_data = TRUE;
+
+ addr = (mach_vm_address_t)process_data->fp_data_out;
+ size = (mach_msg_size_t)process_data->fp_data_resid;
+ option |= (MACH_RCV_LARGE | MACH_RCV_LARGE_IDENTITY);
+ if (process_data->fp_flags & KEVENT_FLAG_STACK_DATA)
+ option |= MACH_RCV_STACK;
+ }
+ } else {
+ /* just detect the port name (if a set) and size of the first message */
+ option = MACH_RCV_LARGE;
+ addr = 0;
+ size = 0;
+ }
+
+ /* just use the reference from here on out */
+ io_reference(object);
+
+ /*
+ * Set up to receive a message or the notification of a
+ * too large message. But never allow this call to wait.
+ * If the user provided aditional options, like trailer
+ * options, pass those through here. But we don't support
+ * scatter lists through this interface.
+ */
+ self->ith_object = object;
+ self->ith_msg_addr = addr;
+ self->ith_rsize = size;
+ self->ith_msize = 0;
+ self->ith_option = option;
+ self->ith_receiver_name = MACH_PORT_NULL;
+ self->ith_continuation = NULL;
+ option |= MACH_RCV_TIMEOUT; // never wait
+ self->ith_state = MACH_RCV_IN_PROGRESS;
+
+ wresult = ipc_mqueue_receive_on_thread(
+ mqueue,
+ option,
+ size, /* max_size */
+ 0, /* immediate timeout */
+ THREAD_INTERRUPTIBLE,
+ self);
+ /* mqueue unlocked */
+
+ /*
+ * If we timed out, or the process is exiting, just release the
+ * reference on the ipc_object and return zero.
+ */
+ if (wresult == THREAD_RESTART || self->ith_state == MACH_RCV_TIMED_OUT) {
+ io_release(object);
+ return 0;
+ }
+
+ assert(wresult == THREAD_NOT_WAITING);
+ assert(self->ith_state != MACH_RCV_IN_PROGRESS);
+
+ /*
+ * If we weren't attempting to receive a message
+ * directly, we need to return the port name in
+ * the kevent structure.
+ */
+ if ((option & MACH_RCV_MSG) != MACH_RCV_MSG) {
+ assert(self->ith_state == MACH_RCV_TOO_LARGE);
+ assert(self->ith_kmsg == IKM_NULL);
+ kev->data = self->ith_receiver_name;
+ io_release(object);
+ return 1;
+ }
+
+ /*
+ * Attempt to receive the message directly, returning
+ * the results in the fflags field.
+ */
+ kev->fflags = mach_msg_receive_results(&size);
+
+ /* kmsg and object reference consumed */
+
+ /*
+ * if the user asked for the identity of ports containing a
+ * a too-large message, return it in the data field (as we
+ * do for messages we didn't try to receive).
+ */
+ if (kev->fflags == MACH_RCV_TOO_LARGE) {
+ kev->ext[1] = self->ith_msize;
+ if (option & MACH_RCV_LARGE_IDENTITY)
+ kev->data = self->ith_receiver_name;
+ else
+ kev->data = MACH_PORT_NULL;
+ } else {
+ kev->ext[1] = size;
+ kev->data = MACH_PORT_NULL;
+ }
+
+ /*
+ * If we used a data buffer carved out from the filt_process data,
+ * store the address used in the knote and adjust the residual and
+ * other parameters for future use.
+ */
+ if (used_filtprocess_data) {
+ assert(process_data->fp_data_resid >= size);
+ process_data->fp_data_resid -= size;
+ if ((process_data->fp_flags & KEVENT_FLAG_STACK_DATA) == 0) {
+ kev->ext[0] = process_data->fp_data_out;
+ process_data->fp_data_out += size;
+ } else {
+ assert(option & MACH_RCV_STACK);
+ kev->ext[0] = process_data->fp_data_out +
+ process_data->fp_data_resid;
+ }
+ }
+
+ /*
+ * Apply message-based QoS values to output kevent as prescribed.
+ * The kev->qos field gets max(msg-qos, kn->kn_qos).
+ * The kev->ext[2] field gets (msg-qos << 32) | (override-qos).
+ *
+ * The mach_msg_receive_results() call saved off the message
+ * QoS values in the continuation save area on successful receive.
+ */
+ if (kev->fflags == MACH_MSG_SUCCESS) {
+ kev->qos = mach_msg_priority_combine(self->ith_qos, kn->kn_qos);
+ kev->ext[2] = ((uint64_t)self->ith_qos << 32) |
+ (uint64_t)self->ith_qos_override;
+ }
+
+ return 1;
+}
+
+/*
+ * Peek to see if the message queue associated with the knote has any
+ * events. This pre-hook is called when a filter uses the stay-
+ * on-queue mechanism (as the knote_link_waitq mechanism does for
+ * portsets) and someone calls select() against the containing kqueue.
+ *
+ * Just peek at the pre-post status of the portset's wait queue
+ * to determine if it has anything interesting. We can do it
+ * without holding the lock, as it is just a snapshot in time
+ * (if this is used as part of really waiting for events, we
+ * will catch changes in this status when the event gets posted
+ * up to the knote's kqueue).
+ */
+static unsigned
+filt_machportpeek(struct knote *kn)
+{
+ ipc_mqueue_t mqueue = kn->kn_ptr.p_mqueue;
+
+ return (ipc_mqueue_set_peek(mqueue));
+}