+/*
+ * Routine: mach_msg_rcv_link_special_reply_port
+ * Purpose:
+ * Link the special reply port(rcv right) to the
+ * other end of the sync ipc channel.
+ * Conditions:
+ * Nothing locked.
+ * Returns:
+ * None.
+ */
+static mach_msg_return_t
+mach_msg_rcv_link_special_reply_port(
+ ipc_port_t special_reply_port,
+ mach_port_name_t dest_name_port)
+{
+ ipc_port_t dest_port = IP_NULL;
+ kern_return_t kr;
+ int qos;
+
+ if (current_thread()->ith_special_reply_port != special_reply_port) {
+ return MACH_RCV_INVALID_NOTIFY;
+ }
+
+ /* Copyin the destination port */
+ if (!MACH_PORT_VALID(dest_name_port)) {
+ return MACH_RCV_INVALID_NOTIFY;
+ }
+
+ kr = ipc_object_copyin(current_space(),
+ dest_name_port, MACH_MSG_TYPE_COPY_SEND,
+ (ipc_object_t *) &dest_port);
+
+ /*
+ * The receive right of dest port might have gone away,
+ * do not fail the receive in that case.
+ */
+ if (kr == KERN_SUCCESS && IP_VALID(dest_port)) {
+
+ /* Get the effective qos of the thread */
+ qos = proc_get_effective_thread_policy(current_thread(), TASK_POLICY_QOS);
+
+ ipc_port_link_special_reply_port_with_qos(special_reply_port,
+ dest_port, qos);
+
+ /* release the send right */
+ ipc_port_release_send(dest_port);
+ }
+ return MACH_MSG_SUCCESS;
+}
+
+/*
+ * Routine: mach_msg_rcv_unlink_special_reply_port
+ * Purpose:
+ * Unlink the special reply port to the other end
+ * of the sync ipc channel.
+ * Condition:
+ * Nothing locked.
+ * Returns:
+ * None.
+ */
+static void
+mach_msg_rcv_unlink_special_reply_port(void)
+{
+ thread_t self = current_thread();
+ ipc_port_t special_reply_port = self->ith_special_reply_port;
+ mach_msg_option_t option = self->ith_option;
+
+ if ((special_reply_port == IP_NULL) ||
+ !(option & MACH_RCV_SYNC_WAIT)) {
+ return;
+ }
+
+ ipc_port_unlink_special_reply_port(special_reply_port,
+ IPC_PORT_UNLINK_SR_ALLOW_SYNC_QOS_LINKAGE);
+}
+