// terminate all profiling by critical error // TODO: don't send data to host static void terminate_error(char *errstr, int send_to_host) { LOGE("termination all with err '%s'\n", errstr); struct msg_data_t *msg = NULL; if (send_to_host != 0) { msg = gen_message_error(errstr); if (msg) { if (write_to_buf(msg) != 0) LOGE("write to buf fail\n"); free_msg_data(msg); } else { LOGI("cannot generate error message\n"); } } terminate_all(); }
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; }