-#ifdef EVFILT_MACH_IMPLEMENTED
-static void *mach_demand_loop(void *arg __attribute__((unused)))
-{
- mach_msg_empty_rcv_t dummy;
- kern_return_t kr;
- mach_port_name_array_t members;
- mach_msg_type_number_t membersCnt;
- mach_port_status_t status;
- mach_msg_type_number_t statusCnt;
- unsigned int i;
-
- for (;;) {
-
- /*
- * Receive indication of message on demand service
- * ports without actually receiving the message (we'll
- * let the actual server do that.
- */
- kr = mach_msg(&dummy.header, MACH_RCV_MSG|MACH_RCV_LARGE,
- 0, 0, mach_demand_port_set, 0, MACH_PORT_NULL);
- if (kr != MACH_RCV_TOO_LARGE) {
- syslog(LOG_WARNING, "%s(): mach_msg(): %s", __func__, mach_error_string(kr));
- continue;
- }
-
- /*
- * Some port(s) now have messages on them, find out
- * which ones (there is no indication of which port
- * triggered in the MACH_RCV_TOO_LARGE indication).
- */
- kr = mach_port_get_set_status(mach_task_self(),
- mach_demand_port_set, &members, &membersCnt);
- if (kr != KERN_SUCCESS) {
- syslog(LOG_WARNING, "%s(): mach_port_get_set_status(): %s", __func__, mach_error_string(kr));
- continue;
- }
-
- for (i = 0; i < membersCnt; i++) {
- statusCnt = MACH_PORT_RECEIVE_STATUS_COUNT;
- kr = mach_port_get_attributes(mach_task_self(), members[i],
- MACH_PORT_RECEIVE_STATUS, (mach_port_info_t)&status, &statusCnt);
- if (kr != KERN_SUCCESS) {
- syslog(LOG_WARNING, "%s(): mach_port_get_attributes(): %s", __func__, mach_error_string(kr));
- continue;
- }
-
- /*
- * For each port with messages, take it out of the
- * demand service portset, and inform the main thread
- * that it might have to start the server responsible
- * for it.
- */
- if (status.mps_msgcount) {
- kr = mach_port_move_member(mach_task_self(), members[i], MACH_PORT_NULL);
- if (kr != KERN_SUCCESS) {
- syslog(LOG_WARNING, "%s(): mach_port_move_member(): %s", __func__, mach_error_string(kr));
- continue;
- }
- write(machcbwritefd, &(members[i]), sizeof(members[i]));
- }
- }
-
- kr = vm_deallocate(mach_task_self(), (vm_address_t) members,
- (vm_size_t) membersCnt * sizeof(mach_port_name_t));
- if (kr != KERN_SUCCESS) {
- syslog(LOG_WARNING, "%s(): vm_deallocate(): %s", __func__, mach_error_string(kr));
- continue;
- }
- }
-
- return NULL;
-}
-#endif
-