void tcp_receive_loop(int unix_sock) { /* init */ tcpmain_sock=unix_sock; /* init com. socket */ if (init_io_wait(&io_w, tcp_max_fd_no, tcp_poll_method)<0) goto error; /* add the unix socket */ if (io_watch_add(&io_w, tcpmain_sock, F_TCPMAIN, 0)<0){ LM_CRIT("failed to add socket to the fd list\n"); goto error; } /* main loop */ switch(io_w.poll_method){ case POLL_POLL: while(1){ io_wait_loop_poll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0); tcp_receive_timeout(); } break; #ifdef HAVE_SELECT case POLL_SELECT: while(1){ io_wait_loop_select(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0); tcp_receive_timeout(); } break; #endif #ifdef HAVE_SIGIO_RT case POLL_SIGIO_RT: while(1){ io_wait_loop_sigio_rt(&io_w, TCP_CHILD_SELECT_TIMEOUT); tcp_receive_timeout(); } break; #endif #ifdef HAVE_EPOLL case POLL_EPOLL_LT: while(1){ io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0); tcp_receive_timeout(); } break; case POLL_EPOLL_ET: while(1){ io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 1); tcp_receive_timeout(); } break; #endif #ifdef HAVE_KQUEUE case POLL_KQUEUE: while(1){ io_wait_loop_kqueue(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0); tcp_receive_timeout(); } break; #endif #ifdef HAVE_DEVPOLL case POLL_DEVPOLL: while(1){ io_wait_loop_devpoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0); tcp_receive_timeout(); } break; #endif default: LM_CRIT("no support for poll method %s (%d)\n", poll_method_name(io_w.poll_method), io_w.poll_method); goto error; } error: destroy_io_wait(&io_w); LM_CRIT("exiting..."); exit(-1); }
/*! \brief tcp main loop */ void tcp_main_loop(void) { struct socket_info* si; int r; /* init io_wait (here because we want the memory allocated only in * the tcp_main process) */ /*! \todo FIXME: TODO: make tcp_max_fd_no a config param */ if (init_io_wait(&io_h, tcp_max_fd_no, tcp_poll_method)<0) goto error; /* init: start watching all the fds*/ /* add all the sockets we listens on for connections */ for (si=tcp_listen; si; si=si->next){ if ((si->proto==PROTO_TCP) &&(si->socket!=-1)){ if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){ LM_CRIT("failed to add listen socket to the fd list\n"); goto error; } }else{ LM_CRIT("non tcp address in tcp_listen\n"); } } #ifdef USE_TLS if (!tls_disable){ for (si=tls_listen; si; si=si->next){ if ((si->proto==PROTO_TLS) && (si->socket!=-1)){ if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){ LM_CRIT("failed to add tls listen socket to the fd list\n"); goto error; } }else{ LM_CRIT("non tls address in tls_listen\n"); } } } #endif /* add all the unix sockets used for communcation with other opensips * processes (get fd, new connection a.s.o) */ for (r=1; r<counted_processes; r++){ /* skip myslef (as process) and -1 socks (disabled) (we can't have 0, we never close it!) */ if (r!=process_no && pt[r].unix_sock>0) if (io_watch_add(&io_h, pt[r].unix_sock, F_PROC, &pt[r])<0){ LM_CRIT("failed to add process %d (%s) unix socket " "to the fd list\n", r, pt[r].desc); goto error; } } /* add all the unix sokets used for communication with the tcp childs */ for (r=0; r<tcp_children_no; r++){ if (tcp_children[r].unix_sock>0)/*we can't have 0, we never close it!*/ if (io_watch_add(&io_h, tcp_children[r].unix_sock, F_TCPCHILD, &tcp_children[r]) <0){ LM_CRIT("failed to add tcp child %d unix socket to " "the fd list\n", r); goto error; } } /* main loop */ switch(io_h.poll_method){ case POLL_POLL: while(1){ /* wait and process IO */ io_wait_loop_poll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0); /* remove old connections */ tcpconn_timeout(0); } break; #ifdef HAVE_SELECT case POLL_SELECT: while(1){ io_wait_loop_select(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0); tcpconn_timeout(0); } break; #endif #ifdef HAVE_SIGIO_RT case POLL_SIGIO_RT: while(1){ io_wait_loop_sigio_rt(&io_h, TCP_MAIN_SELECT_TIMEOUT); tcpconn_timeout(0); } break; #endif #ifdef HAVE_EPOLL case POLL_EPOLL_LT: while(1){ io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0); tcpconn_timeout(0); } break; case POLL_EPOLL_ET: while(1){ io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 1); tcpconn_timeout(0); } break; #endif #ifdef HAVE_KQUEUE case POLL_KQUEUE: while(1){ io_wait_loop_kqueue(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0); tcpconn_timeout(0); } break; #endif #ifdef HAVE_DEVPOLL case POLL_DEVPOLL: while(1){ io_wait_loop_devpoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0); tcpconn_timeout(0); } break; #endif default: LM_CRIT("no support for poll method %s (%d)\n", poll_method_name(io_h.poll_method), io_h.poll_method); goto error; } error: destroy_io_wait(&io_h); LM_CRIT("exiting..."); exit(-1); }
void tcp_receive_loop(int unix_sock) { /* init */ tcpmain_sock=unix_sock; /* init com. socket */ if (init_io_wait(&io_w, get_max_open_fds(), tcp_poll_method)<0) goto error; tcp_reader_prev_ticks=get_ticks_raw(); if (init_local_timer(&tcp_reader_ltimer, get_ticks_raw())!=0) goto error; /* add the unix socket */ if (io_watch_add(&io_w, tcpmain_sock, POLLIN, F_TCPMAIN, 0)<0){ LM_CRIT("failed to add socket to the fd list\n"); goto error; } /* initialize the config framework */ if (cfg_child_init()) goto error; /* main loop */ switch(io_w.poll_method){ case POLL_POLL: while(1){ io_wait_loop_poll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0); tcp_reader_timer_run(); } break; #ifdef HAVE_SELECT case POLL_SELECT: while(1){ io_wait_loop_select(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0); tcp_reader_timer_run(); } break; #endif #ifdef HAVE_SIGIO_RT case POLL_SIGIO_RT: while(1){ io_wait_loop_sigio_rt(&io_w, TCP_CHILD_SELECT_TIMEOUT); tcp_reader_timer_run(); } break; #endif #ifdef HAVE_EPOLL case POLL_EPOLL_LT: while(1){ io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0); tcp_reader_timer_run(); } break; case POLL_EPOLL_ET: while(1){ io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 1); tcp_reader_timer_run(); } break; #endif #ifdef HAVE_KQUEUE case POLL_KQUEUE: while(1){ io_wait_loop_kqueue(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0); tcp_reader_timer_run(); } break; #endif #ifdef HAVE_DEVPOLL case POLL_DEVPOLL: while(1){ io_wait_loop_devpoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0); tcp_reader_timer_run(); } break; #endif default: LM_CRIT("no support for poll method %s (%d)\n", poll_method_name(io_w.poll_method), io_w.poll_method); goto error; } error: destroy_io_wait(&io_w); LM_CRIT("exiting..."); exit(-1); }