void tcp_worker_proc( int unix_sock, int max_fd ) { /* init reactor for TCP worker */ tcpmain_sock=unix_sock; /* init com. socket */ if ( init_worker_reactor( "TCP_worker", max_fd, RCT_PRIO_MAX)<0 ) { goto error; } /* start watching for the timer jobs */ if (reactor_add_reader( timer_fd_out, F_TIMER_JOB, RCT_PRIO_TIMER,NULL)<0){ LM_CRIT("failed to add timer pipe_out to reactor\n"); goto error; } /* add the unix socket */ if (reactor_add_reader( tcpmain_sock, F_TCPMAIN, RCT_PRIO_PROC, NULL)<0) { LM_CRIT("failed to add socket to the fd list\n"); goto error; } /* main loop */ reactor_main_loop( TCP_CHILD_SELECT_TIMEOUT, error, tcp_receive_timeout()); error: destroy_worker_reactor(); LM_CRIT("exiting..."); exit(-1); }
static void tcp_main_server(void) { static unsigned int last_sec = 0; int flags; struct socket_info* si; int n; /* we run in a separate, dedicated process, with its own reactor * (reactors are per process) */ if (init_worker_reactor("TCP_main", RCT_PRIO_MAX)<0) goto error; /* now start watching all the fds*/ /* add all the sockets we listens on for connections */ for( n=PROTO_FIRST ; n<PROTO_LAST ; n++ ) if ( is_tcp_based_proto(n) ) for( si=protos[n].listeners ; si ; si=si->next ) { if ( (si->socket!=-1) && reactor_add_reader( si->socket, F_TCP_LISTENER, RCT_PRIO_NET, si)<0 ) { LM_ERR("failed to add listen socket to reactor\n"); goto error; } } /* add all the unix sockets used for communcation with other opensips * processes (get fd, new connection a.s.o) */ for (n=1; n<counted_processes; n++) { /* skip myslef (as process) and -1 socks (disabled) (we can't have 0, we never close it!) */ if (n!=process_no && pt[n].unix_sock>0) if (reactor_add_reader( pt[n].unix_sock, F_TCP_WORKER, RCT_PRIO_PROC, &pt[n])<0){ LM_ERR("failed to add process %d (%s) unix socket " "to the fd list\n", n, pt[n].desc); goto error; } } /* add all the unix sokets used for communication with the tcp childs */ for (n=0; n<tcp_children_no; n++) { /*we can't have 0, we never close it!*/ if (tcp_children[n].unix_sock>0) { /* make socket non-blocking */ flags=fcntl(tcp_children[n].unix_sock, F_GETFL); if (flags==-1){ LM_ERR("fcntl failed: (%d) %s\n", errno, strerror(errno)); goto error; } if (fcntl(tcp_children[n].unix_sock,F_SETFL,flags|O_NONBLOCK)==-1){ LM_ERR("set non-blocking failed: (%d) %s\n", errno, strerror(errno)); goto error; } /* add socket for listening */ if (reactor_add_reader( tcp_children[n].unix_sock, F_TCP_TCPWORKER, RCT_PRIO_PROC, &tcp_children[n])<0) { LM_ERR("failed to add tcp child %d unix socket to " "the fd list\n", n); goto error; } } } /* main loop (requires "handle_io()" implementation) */ reactor_main_loop( TCP_MAIN_SELECT_TIMEOUT, error, tcpconn_lifetime(last_sec, 0) ); error: destroy_worker_reactor(); LM_CRIT("exiting..."); exit(-1); }