Use command line parsing so we can specify different hostnames master
authorDiane Trout <diane@caltech.edu>
Thu, 5 Apr 2012 21:50:48 +0000 (14:50 -0700)
committerDiane Trout <diane@caltech.edu>
Thu, 5 Apr 2012 21:50:48 +0000 (14:50 -0700)
src/castor_load.c

index 3c46e1586d9188a414219eaa9aa5594c584b71bd..58e6614b0c58333d68dda8caf6f25039bb2fc622 100644 (file)
 #define _POSIX_SOURCE 1
+#define _XOPEN_SOURCE 500
 
+#include <errno.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netdb.h>
 #include <unistd.h>
-#include <string.h>
-#include <errno.h>
 
 #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;
 }