Add usage(), minor code cleanups
authorteodor <teodor>
Thu, 29 Nov 2007 15:40:01 +0000 (15:40 +0000)
committerteodor <teodor>
Thu, 29 Nov 2007 15:40:01 +0000 (15:40 +0000)
rtop.c
sendtop.c
td_unit.c
topd.c

diff --git a/rtop.c b/rtop.c
index c204aaf..410e5c6 100644 (file)
--- a/rtop.c
+++ b/rtop.c
 #include "tmalloc.h"
 #include "top.h"
 
+void
+usage() {
+       fputs(
+                "Copyright (c) 2004-2007 Teodor Sigaev <teodor@sigaev.ru>. All rights reserved.\n"
+                "rtop - read collected load of remote servers\n"
+                "./rtop [-D] -h HOST [-p PORT]\n"
+                "  -D      - debug mode\n"
+                "  -h HOST - server\n"
+                "  -p PRT  - server's port\n",
+                stdout
+       );
+       exit(1);
+}
+
 int
 main( int argc, char *argv[] ) {
        int ch;
        int debug=0;
-       char *host="127.0.0.1";
+       char *host=NULL;
        int port=TOPD_SERVER_DEFAULT_PORT;
        int i;
 
@@ -71,14 +85,15 @@ main( int argc, char *argv[] ) {
                                port=atoi(optarg);
                                break;
                        default:
+                               usage();
                                return 1;
                }
        }
 
-        if ( debug )
-                opentlog( TL_OPEN_STDERR,  TL_DEBUG, NULL);
-        else
-                opentlog( TL_OPEN_SYSLOG, TL_INFO, NULL);
+       if (!host)
+               usage();
+
+       opentlog( TL_OPEN_STDERR,  (debug) ? TL_DEBUG : TL_INFO, NULL);
 
        TC_fillConnection(&cs, host, port);
        cs.buf = (char*)pmsg;
index 8ab3269..297eabc 100644 (file)
--- a/sendtop.c
+++ b/sendtop.c
@@ -116,6 +116,21 @@ sendTop(Msg *msg) {
        return TC_sendMsg(msg);
 }
 
+void usage() {
+       fputs(
+               "Copyright (c) 2004-2007 Teodor Sigaev <teodor@sigaev.ru>. All rights reserved.\n"
+               "sendtop - small daemon to send load of box to server\n"
+               "Usage:\n"
+               "./sendtop [-D] -h HOST [-p PORT] [-s PERIOD]\n"
+               "  -D        - debug mode (do not daemonize, print to stderr instead of syslog)\n"
+               "  -h HOST   - server IP to send data\n"
+               "  -p PORT   - port number of server\n"
+               "  -s PERIOD - period of of send (in seconds)\n",
+               stdout
+       );
+
+       exit(1);
+}
 
 extern char *optarg;
 int
@@ -126,7 +141,7 @@ main( int argc, char *argv[] ) {
        int period=30;
 
        msg.port        = TOPD_SERVER_DEFAULT_PORT;
-       msg.host        = "127.0.0.1";
+       msg.host        = NULL;
        msg.msg = NULL;
        msg.sockfd =-1;
 
@@ -145,10 +160,14 @@ main( int argc, char *argv[] ) {
                                debug=1;
                                break;
                        default:
+                               usage();
                                return 1;
                }
        }
 
+       if (!msg.host)
+               usage();
+
        signal(SIGCHLD, SIG_IGN);
 
        if ( debug )
index c736284..77ca588 100644 (file)
--- a/td_unit.c
+++ b/td_unit.c
@@ -39,9 +39,7 @@ MassiveUnit*
 allocUnits(int n ) {
        MassiveUnit *ptr = NULL;
 
-       ptr = (MassiveUnit*)malloc( MUHDRSZ + n * sizeof(UnitInfo) );
-       if ( !ptr ) 
-               tlog(TL_CRIT|TL_EXIT, "allocUnits: no memoru for %d units", n);
+       ptr = (MassiveUnit*)tmalloc( MUHDRSZ + n * sizeof(UnitInfo) );
        ptr->maxnumber = n;
        ptr->number=0;
 
diff --git a/topd.c b/topd.c
index 56a7e03..1f1378c 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 */