2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/sysctl.h>
38 #include "connection.h"
43 static int continueWork=1;
54 main( int argc, char *argv[] ) {
56 int child=0, listenport = TOPD_SERVER_DEFAULT_PORT;
58 char *host="127.0.0.1";
60 memset( &cfg, 0, sizeof(TCServer) );
62 while( (ch=getopt(argc, argv, "h:p:c:s:D"))!=-1) {
71 listenport=atoi(optarg);
81 signal(SIGCHLD, SIG_IGN);
84 opentlog( TL_OPEN_STDERR, TL_DEBUG, NULL);
86 opentlog( TL_OPEN_SYSLOG, TL_INFO, NULL);
88 if ( cfg.debug || (child = fork()) == 0 ) {
90 if ( cfg.debug || (child = fork()) == 0 ) {
92 TC_Connection *newconn;
95 signal(SIGINT, inquireExit);
96 signal(SIGHUP, inquireExit);
101 cfg.mu = allocUnits(n);
103 TC_addConnection( &cfg.pool, TC_fillConnection(NULL, NULL, listenport) );
104 TC_ClientInitConnection( cfg.pool.conn[0], NULL, listenport);
105 cfg.pool.conn[0]->state = CS_READ;
107 TC_addConnection( &cfg.pool, TC_fillConnection(NULL, NULL, listenport) );
108 cfg.pool.conn[1]->fd = TC_AcceptUdp( host, listenport );
109 cfg.pool.conn[1]->state = CS_READ;
111 while(continueWork) {
112 if ( TC_ReadyIO(cfg.pool.conn, cfg.pool.number, 100 ) ) {
113 if ( cfg.pool.conn[1]->readyio )
114 gotMsg( cfg.pool.conn[1] );
116 tlog(TL_DEBUG,"Ready connection...");
119 if ( cfg.pool.conn[0]->readyio && (newconn=TC_AcceptTcp( cfg.pool.conn[0] )) != NULL ) {
120 if ( newconn->state == CS_CONNECTED ) {
121 tlog(TL_DEBUG,"Accept new connection...");
122 newconn->state = CS_READ;
123 TC_addConnection( &cfg.pool, newconn );
127 TC_deleteConnectionByN(&cfg.pool, 0);
128 close( cfg.pool.conn[1]->fd );
129 for(i=TC_SERVER_CONN_START; i<cfg.pool.number; i++)
130 TC_deleteConnectionByN(&cfg.pool,i);
134 tlog(TL_CRIT,"Can't start child process");