+/*
+ * Routine: thread_get_special_reply_port [mach trap]
+ * Purpose:
+ * Allocate a special reply port for the calling thread.
+ * Conditions:
+ * Nothing locked.
+ * Returns:
+ * mach_port_name_t: send right & receive right for special reply port.
+ * MACH_PORT_NULL if there are any resource failures
+ * or other errors.
+ */
+
+mach_port_name_t
+thread_get_special_reply_port(
+ __unused struct thread_get_special_reply_port_args *args)
+{
+ ipc_port_t port;
+ mach_port_name_t name;
+ kern_return_t kr;
+ thread_t thread = current_thread();
+ ipc_port_init_flags_t flags = IPC_PORT_INIT_MESSAGE_QUEUE |
+ IPC_PORT_INIT_MAKE_SEND_RIGHT | IPC_PORT_INIT_SPECIAL_REPLY;
+
+ /* unbind the thread special reply port */
+ if (IP_VALID(thread->ith_special_reply_port)) {
+ kr = ipc_port_unbind_special_reply_port(thread, TRUE);
+ if (kr != KERN_SUCCESS) {
+ return MACH_PORT_NULL;
+ }
+ }
+
+ kr = ipc_port_alloc(current_task()->itk_space, flags, &name, &port);
+ if (kr == KERN_SUCCESS) {
+ ipc_port_bind_special_reply_port_locked(port);
+ ip_unlock(port);
+ } else {
+ name = MACH_PORT_NULL;
+ }
+ return name;
+}
+
+/*
+ * Routine: ipc_port_bind_special_reply_port_locked
+ * Purpose:
+ * Bind the given port to current thread as a special reply port.
+ * Conditions:
+ * Port locked.
+ * Returns:
+ * None.
+ */
+
+static void
+ipc_port_bind_special_reply_port_locked(
+ ipc_port_t port)
+{
+ thread_t thread = current_thread();
+ assert(thread->ith_special_reply_port == NULL);
+ assert(port->ip_specialreply);
+ assert(port->ip_sync_link_state == PORT_SYNC_LINK_ANY);
+
+ ip_reference(port);
+ thread->ith_special_reply_port = port;
+ port->ip_messages.imq_srp_owner_thread = thread;
+
+ ipc_special_reply_port_bits_reset(port);
+}
+
+/*
+ * Routine: ipc_port_unbind_special_reply_port
+ * Purpose:
+ * Unbind the thread's special reply port.
+ * If the special port has threads waiting on turnstile,
+ * update it's inheritor.
+ * Condition:
+ * Nothing locked.
+ * Returns:
+ * None.
+ */
+static kern_return_t
+ipc_port_unbind_special_reply_port(
+ thread_t thread,
+ boolean_t unbind_active_port)
+{
+ ipc_port_t special_reply_port = thread->ith_special_reply_port;
+
+ ip_lock(special_reply_port);
+
+ /* Return error if port active and unbind_active_port set to FALSE */
+ if (unbind_active_port == FALSE && ip_active(special_reply_port)) {
+ ip_unlock(special_reply_port);
+ return KERN_FAILURE;
+ }
+
+ thread->ith_special_reply_port = NULL;
+ ipc_port_adjust_special_reply_port_locked(special_reply_port, NULL,
+ IPC_PORT_ADJUST_UNLINK_THREAD, FALSE);
+ /* port unlocked */
+
+ ip_release(special_reply_port);
+ return KERN_SUCCESS;
+}
+