add .gitignore
[remotetop.git] / topd.c
diff --git a/topd.c b/topd.c
index 56a7e03..870fbc1 100644 (file)
--- a/topd.c
+++ b/topd.c
@@ -44,46 +44,65 @@ static int continueWork=1;
 
 static void
 inquireExit(int s) {
-        continueWork=0;
+       continueWork=0;
 }
-                                
+                               
 extern char *optarg;
 extern int optind;
 
+void
+usage() {
+       fputs(
+               "Copyright (c) 2004-2007 Teodor Sigaev <teodor@sigaev.ru>. All rights reserved.\n"
+               "topd - daemon to collect load data from clients\n"
+               "Usage:\n"
+               "./top [-D] -h IP [-p PORT] [-n NUMCLIENTS]\n"
+               "  -D        - debug mode (do not daemonize, print to stderr instead of syslog)\n"
+               "  -h HOST   - listen IP\n"
+               "  -p PORT   - listen port\n"
+               "  -n NUMCLIENTS - max number of clients\n",
+               stdout
+       );
+       exit(1);
+}
 int
 main( int argc, char *argv[] ) {
        int ch;
        int child=0, listenport = TOPD_SERVER_DEFAULT_PORT;
-        int n=1024;
-       char *host="127.0.0.1";
+       int n=1024;
+       char *host=NULL;
 
        memset( &cfg, 0, sizeof(TCServer) );
 
-       while( (ch=getopt(argc, argv, "h:p:c:s:D"))!=-1) {
+       while( (ch=getopt(argc, argv, "h:p:c:D"))!=-1) {
                switch(ch) {
-                        case 'D':
-                                cfg.debug=1;
-                                break;
-                        case 'h':
-                                host=strdup(optarg);
-                                break;
-                        case 'p':
-                                listenport=atoi(optarg);
-                                break;
-                        case 'n':
-                                n=atoi(optarg);
-                                break;
+                       case 'D':
+                               cfg.debug=1;
+                               break;
+                       case 'h':
+                               host=strdup(optarg);
+                               break;
+                       case 'p':
+                               listenport=atoi(optarg);
+                               break;
+                       case 'n':
+                               n=atoi(optarg);
+                               break;
                        default:
+                               usage();
                                return 1;
                }
        }
 
+       if (!host)
+               usage();
+
        signal(SIGCHLD, SIG_IGN);
 
-        if ( cfg.debug )
-                opentlog( TL_OPEN_STDERR,  TL_DEBUG, NULL);
-        else
-                opentlog( TL_OPEN_SYSLOG, TL_INFO, NULL);
+       if ( cfg.debug )
+               opentlog( TL_OPEN_STDERR,  TL_DEBUG, NULL);
+       else
+               opentlog( TL_OPEN_SYSLOG, TL_INFO, NULL);
 
        if ( cfg.debug || (child = fork()) == 0 ) {
                /* child */
@@ -101,7 +120,7 @@ main( int argc, char *argv[] ) {
                        cfg.mu = allocUnits(n);
 
                        TC_addConnection( &cfg.pool, TC_fillConnection(NULL, NULL, listenport) );
-                       TC_ClientInitConnection( cfg.pool.conn[0], NULL, listenport);
+                       TC_ClientInitConnection( cfg.pool.conn[0], host, listenport);
                        cfg.pool.conn[0]->state = CS_READ;
 
                        TC_addConnection( &cfg.pool, TC_fillConnection(NULL, NULL, listenport) );
@@ -124,9 +143,8 @@ main( int argc, char *argv[] ) {
                                        }
                                }
                        }
-                       TC_deleteConnectionByN(&cfg.pool, 0);
-                       close( cfg.pool.conn[1]->fd );
-                       for(i=TC_SERVER_CONN_START; i<cfg.pool.number; i++) 
+
+                       for(i=cfg.pool.number-1; i>=0; i--) 
                                TC_deleteConnectionByN(&cfg.pool,i);
                }
        }