Bring back tids_start() function.
authorJennifer Richards <jennifer@painless-security.com>
Wed, 11 Jan 2017 16:24:37 +0000 (11:24 -0500)
committerJennifer Richards <jennifer@painless-security.com>
Wed, 11 Jan 2017 16:24:37 +0000 (11:24 -0500)
tid/example/tids_main.c
tid/tids.c

index 9b87ef7..7b58840 100644 (file)
@@ -355,11 +355,6 @@ int main (int argc,
   TIDS_INSTANCE *tids;
   TR_NAME *gssname = NULL;
   struct cmdline_args opts={NULL};
-#define MAX_SOCKETS 10
-  int tids_socket[MAX_SOCKETS];
-  size_t n_sockets;
-  struct pollfd poll_fds[MAX_SOCKETS];
-  size_t ii=0;
 
   /* parse the command line*/
   argp_parse(&argp, argc, argv, 0, 0, &opts);
@@ -391,31 +386,7 @@ int main (int argc,
   }
 
   tids->ipaddr = opts.ip_address;
-
-  /* get listener for tids port */
-  n_sockets = tids_get_listener(tids, &tids_req_handler, auth_handler, opts.hostname, TID_PORT, gssname,
-                                tids_socket, MAX_SOCKETS);
-
-  for (ii=0; ii<n_sockets; ii++) {
-    poll_fds[ii].fd=tids_socket[ii];
-    poll_fds[ii].events=POLLIN; /* poll on ready for reading */
-    poll_fds[ii].revents=0;
-  }
-
-  /* main event loop */
-  while (1) {
-    /* wait up to 100 ms for an event, then handle any idle work */
-    if(poll(poll_fds, n_sockets, 100) > 0) {
-      for (ii=0; ii<n_sockets; ii++) {
-        if (poll_fds[ii].revents & POLLIN) {
-          if (0 != tids_accept(tids, tids_socket[ii])) {
-            tr_err("Error handling tids request.");
-          }
-        }
-      }
-    }
-    /* idle loop stuff here */
-  }
+  (void) tids_start(tids, &tids_req_handler, auth_handler, opts.hostname, TID_PORT, gssname);
 
   /* Clean-up the TID server instance */
   tids_destroy(tids);
index b0a6789..a08182d 100644 (file)
@@ -44,6 +44,7 @@
 #include <netinet/in.h>
 #include <jansson.h>
 #include <talloc.h>
+#include <poll.h>
 #include <tid_internal.h>
 #include <gsscon.h>
 #include <tr_debug.h>
@@ -430,7 +431,7 @@ int tids_get_listener(TIDS_INSTANCE *tids,
 
   tids->tids_port = port;
   n_fd=tids_listen(tids, port, fd_out, max_fd);
-  if (n_fd==0)
+  if (n_fd<=0)
     tr_debug("tids_get_listener: Error opening port %d");
   else {
     /* opening port succeeded */
@@ -492,6 +493,66 @@ int tids_accept(TIDS_INSTANCE *tids, int listen)
   return 0;
 }
 
+/* Process tids requests forever. Should not return except on error. */
+#define MAX_SOCKETS 10
+int tids_start (TIDS_INSTANCE *tids, 
+                TIDS_REQ_FUNC *req_handler,
+                tids_auth_func *auth_handler,
+                const char *hostname,
+                unsigned int port,
+                void *cookie)
+{
+  int fd[MAX_SOCKETS]={0};
+  size_t n_fd=0;
+  struct pollfd poll_fd[MAX_SOCKETS]={{0}};
+  int ii=0;
+
+  n_fd=tids_get_listener(tids, req_handler, auth_handler, hostname, port, cookie, fd, MAX_SOCKETS);
+  if (n_fd <= 0) {
+    perror ("Error from tids_listen()");
+    return 1;
+  }
+
+  tr_info("Trust Path Query Server starting on host %s:%d.", hostname, port);
+
+  /* set up the poll structs */
+  for (ii=0; ii<n_fd; ii++) {
+    poll_fd[ii].fd=fd[ii];
+    poll_fd[ii].events=POLLIN;
+  }
+
+  while(1) {   /* accept incoming conns until we are stopped */
+    /* clear out events from previous iteration */
+    for (ii=0; ii<n_fd; ii++)
+      poll_fd[ii].revents=0;
+
+    /* wait indefinitely for a connection */
+    if (poll(poll_fd, n_fd, -1) < 0) {
+      perror("Error from poll()");
+      return 1;
+    }
+
+    /* fork handlers for any sockets that have data */
+    for (ii=0; ii<n_fd; ii++) {
+      if (poll_fd[ii].revents == 0)
+        continue;
+
+      if ((poll_fd[ii].revents & POLLERR) || (poll_fd[ii].revents & POLLNVAL)) {
+        perror("Error polling fd");
+        continue;
+      }
+
+      if (poll_fd[ii].revents & POLLIN) {
+        if (tids_accept(tids, poll_fd[ii].fd))
+          tr_err("tids_start: error in tids_accept().");
+      }
+    }
+  }
+
+  return 1;    /* should never get here, loops "forever" */
+}
+#undef MAX_SOCKETS
+
 void tids_destroy (TIDS_INSTANCE *tids)
 {
   /* clean up logfiles */