From: Diane Trout Date: Thu, 5 Apr 2012 21:50:48 +0000 (-0700) Subject: Use command line parsing so we can specify different hostnames X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=condor-woldlab.git;a=commitdiff_plain;h=HEAD Use command line parsing so we can specify different hostnames --- diff --git a/src/castor_load.c b/src/castor_load.c index 3c46e15..58e6614 100644 --- a/src/castor_load.c +++ b/src/castor_load.c @@ -1,38 +1,105 @@ #define _POSIX_SOURCE 1 +#define _XOPEN_SOURCE 500 +#include +#include #include #include +#include #include #include #include #include -#include -#include #define BUF_SIZE 512 +#define ERROR -1 +#define SUCCESS 0 + +int usage(const char *cmd, int st); +int try_connecting(struct addrinfo *address); +int generate_condor_variables(int sock); + int main(int argc, char **argv) { int sock = 0; int rv = 0; - int nread = 0; struct addrinfo hints; - struct addrinfo *address; - struct addrinfo *addr_ptr; - char buf[BUF_SIZE]; - - memset(buf, 0, BUF_SIZE); + struct addrinfo *address = 0; + + int c; + int optidx = 0; + char *hostname = NULL; + char *port = NULL; + struct option ol[] = + { + { "host", required_argument, 0, 'h' }, + { "port", required_argument, 0, 'p' }, + { NULL, 0, 0, 0 } + }; + memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; - - rv = getaddrinfo("castor-pvt", "1210", &hints, &address); + if (argc < 5) + return usage(argv[0], -1); + + while ((c = getopt_long(argc, argv, "h:p:", ol, &optidx)) != -1) + { + switch(c) + { + case 'h': + hostname = strdup(optarg); + break; + + case 'p': + port = strdup(optarg); + break; + + default: + fprintf(stderr, "bad option: %c\n", c); + return -1; + } + } + + rv = getaddrinfo(hostname, port, &hints, &address); if (rv != 0) { fprintf(stderr, "Error looking up hostname, %d\n", errno); - exit(-1); + exit(ERROR); + } + + sock = try_connecting(address); + if (sock == -1) { + fprintf(stderr, "Unable to connect"); + exit(ERROR); } - + /* now that we have a connection */ + if (address != NULL) { + freeaddrinfo(address); + } + + if (generate_condor_variables(sock) == -1) { + exit(ERROR); + } + exit(SUCCESS); +} + +int usage(const char *cmd, int st) +{ + fprintf(stderr, "usage:\n %s [options]\n\n", cmd); + fprintf(stderr, "\t-h, --host\tHostname to connect to\n"); + fprintf(stderr, "\t-p, --port\tPort to connect to\n"); + fprintf(stderr, "\n"); + return st; +} + +int try_connecting(struct addrinfo *address) +{ + int sock = 0; + int rv; + struct addrinfo *addr_ptr = 0; + for (addr_ptr = address; addr_ptr != NULL; addr_ptr = addr_ptr->ai_next) { sock = socket(addr_ptr->ai_family, addr_ptr->ai_socktype, @@ -43,24 +110,33 @@ int main(int argc, char **argv) { rv = connect(sock, addr_ptr->ai_addr, addr_ptr->ai_addrlen); if (rv != -1) { - break; + /* we connected to something, return it */ + return sock; } close(sock); } - if (addr_ptr == NULL) { - fprintf(stderr, "Could not connect\n"); - exit(-1); - } - - freeaddrinfo(address); + return (-1); +} +int generate_condor_variables(int sock) +{ + int nread = 0; + int rv; + char buf[BUF_SIZE]; + + memset(buf, 0, BUF_SIZE); nread = read(sock, buf, BUF_SIZE-1); if (nread == -1) { fprintf(stderr, "read error"); - exit(-1); + return -1; } - fprintf(stdout, "castor_load = %s", buf); - fprintf(stdout, "--\n"); - - exit(0); + rv = fprintf(stdout, "castor_load = %s", buf); + if (rv == 0) { + return -1; + } + rv = fprintf(stdout, "--\n"); + if (rv == 0) { + return -1; + } + return 0; }