int pm_next_loop(void (*child_changed_status)(process_struct *ps)) { sigsetjmp(saved_jump_buf, 1); pm_can_jump = 0; while (!terminated && (HASH_COUNT(exited_children) > 0 || signaled)) pm_check_children(child_changed_status, terminated); pm_check_pending_processes(); // Check for pending signals arrived while we were in the signal handler pm_can_jump = 1; if (terminated) return -1; else return 0; }
int main (int argc, char const *argv[]) { // Consider making this multi-plexing fd_set rfds, wfds; // temp file descriptor list for select() int rnum = 0, wnum = 0; if (parse_the_command_line(argc, argv)) return 0; setup_erl_daemon_signal_handlers(); if (setup()) return -1; // Create the timeout struct struct timeval m_tv; m_tv.tv_usec = 0; m_tv.tv_sec = 5; /* Do stuff */ while (!terminated) { debug(dbg, 4, "preparing next loop...\n"); if (pm_next_loop(child_changed_status) < 0) break; // Erlang fun... pull the next command from the read_fds parameter on the erlang fd pm_set_can_not_jump(); // clean socket lists FD_ZERO(&rfds); FD_ZERO(&wfds); FD_SET(read_handle, &rfds); rnum = read_handle; // FD_SET(write_handle, &wfds); wnum = -1; // Block until something happens with select int num_ready_socks = select( (wnum > rnum) ? wnum+1 : rnum+1, (-1 != rnum) ? &rfds : NULL, (-1 != wnum) ? &wfds : NULL, // never will write... yet? (fd_set *) 0, &m_tv ); debug(dbg, 4, "number of ready sockets from select: %d\n", num_ready_socks); int interrupted = (num_ready_socks < 0 && errno == EINTR); pm_set_can_jump(); if (interrupted || num_ready_socks == 0) { if (pm_check_children(child_changed_status, terminated) < 0) continue; } else if (num_ready_socks < 0) { perror("select"); exit(9); } else if ( FD_ISSET(read_handle, &rfds) ) { // Read from read_handle a command sent by Erlang unsigned char* buf; int len = 0; if ((len = ei_read(read_handle, &buf)) < 0) { terminated = len; break; } if (decode_and_run_erlang(buf, len)) { // Something is afoot (failed) } else { // Everything went well } } else { // Something else } } terminate_all(); return 0; }