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;
56 "Copyright (c) 2004-2007 Teodor Sigaev <teodor@sigaev.ru>. All rights reserved.\n"
57 "topd - daemon to collect load data from clients\n"
59 "./top [-D] -h IP [-p PORT] [-n NUMCLIENTS]\n"
60 " -D - debug mode (do not daemonize, print to stderr instead of syslog)\n"
61 " -h HOST - listen IP\n"
62 " -p PORT - listen port\n"
63 " -n NUMCLIENTS - max number of clients\n",
69 main( int argc, char *argv[] ) {
71 int child=0, listenport = TOPD_SERVER_DEFAULT_PORT;
75 memset( &cfg, 0, sizeof(TCServer) );
77 while( (ch=getopt(argc, argv, "h:p:c:D"))!=-1) {
86 listenport=atoi(optarg);
100 signal(SIGCHLD, SIG_IGN);
103 opentlog( TL_OPEN_STDERR, TL_DEBUG, NULL);
105 opentlog( TL_OPEN_SYSLOG, TL_INFO, NULL);
107 if ( cfg.debug || (child = fork()) == 0 ) {
109 if ( cfg.debug || (child = fork()) == 0 ) {
111 TC_Connection *newconn;
114 signal(SIGINT, inquireExit);
115 signal(SIGHUP, inquireExit);
120 cfg.mu = allocUnits(n);
122 TC_addConnection( &cfg.pool, TC_fillConnection(NULL, NULL, listenport) );
123 TC_ClientInitConnection( cfg.pool.conn[0], host, listenport);
124 cfg.pool.conn[0]->state = CS_READ;
126 TC_addConnection( &cfg.pool, TC_fillConnection(NULL, NULL, listenport) );
127 cfg.pool.conn[1]->fd = TC_AcceptUdp( host, listenport );
128 cfg.pool.conn[1]->state = CS_READ;
130 while(continueWork) {
131 if ( TC_ReadyIO(cfg.pool.conn, cfg.pool.number, 100 ) ) {
132 if ( cfg.pool.conn[1]->readyio )
133 gotMsg( cfg.pool.conn[1] );
135 tlog(TL_DEBUG,"Ready connection...");
138 if ( cfg.pool.conn[0]->readyio && (newconn=TC_AcceptTcp( cfg.pool.conn[0] )) != NULL ) {
139 if ( newconn->state == CS_CONNECTED ) {
140 tlog(TL_DEBUG,"Accept new connection...");
141 newconn->state = CS_READ;
142 TC_addConnection( &cfg.pool, newconn );
147 TC_deleteConnectionByN(&cfg.pool, 1);
148 TC_deleteConnectionByN(&cfg.pool, 0);
149 for(i=TC_SERVER_CONN_START; i<cfg.pool.number; i++)
150 TC_deleteConnectionByN(&cfg.pool,i);
154 tlog(TL_CRIT,"Can't start child process");