]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bnet_server.c
Fix compiler warning noted in bug #2309
[bacula/bacula] / bacula / src / lib / bnet_server.c
index ef9bc47ada652bc4a485de09cba545278a69c9a2..0144047d28f5e8212b09b4a776ed35e5687e05c1 100644 (file)
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Bacula(R) - The Network Backup Solution
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of
-   the License, or (at your option) any later version.
+   Copyright (C) 2000-2016 Kern Sibbald
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-   General Public License for more details.
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
 
-   You should have received a copy of the GNU General Public
-   License along with this program; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-   MA 02111-1307, USA.
+   You may use this file and others of this release according to the
+   license defined in the LICENSE file, which includes the Affero General
+   Public License, v3.0 ("AGPLv3") and some additional permissions and
+   terms pursuant to its AGPLv3 Section 7.
 
- */
- /* 
-  * Originally written by Kern Sibbald for inclusion in apcupsd.
+   This notice must be preserved when any source code is 
+   conveyed and/or propagated.
+
+   Bacula(R) is a registered trademark of Kern Sibbald.
+*/
+ /*
+  * Originally written by Kern Sibbald for inclusion in apcupsd,
+  *  but heavily modified for Bacula
+  *
   */
 
 #include "bacula.h"
 #include <netinet/in.h>
 #include <sys/socket.h>
+#include <stdlib.h>
 #include <arpa/inet.h>
 #include <netdb.h>
-
-#ifdef HAVE_LIBWRAP
-#include "tcpd.h"
-int allow_severity = LOG_NOTICE;
-int deny_severity = LOG_WARNING;
-static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+#ifdef HAVE_ARPA_NAMESER_H
+#include <arpa/nameser.h>
 #endif
-
-/* Become Threaded Network Server */
-void
-bnet_thread_server(int port, int max_clients, workq_t *client_wq, 
-                  void handle_client_request(void *bsock))
-{
-   int newsockfd, sockfd, stat;
-   socklen_t clilen;
-   struct sockaddr_in cli_addr;       /* client's address */
-   struct sockaddr_in serv_addr;      /* our address */
-   int tlog;
-   fd_set ready, sockset;
-   int turnon = 1;
-   char *caller;
-#ifdef HAVE_LIBWRAP
-   struct request_info request;
+#ifdef HAVE_RESOLV_H
+//#include <resolv.h>
 #endif
 
-   /*
-    * Open a TCP socket  
-    */
-   for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
-      if (tlog <= 0) {
-        tlog = 60; 
-         Emsg1(M_ERROR, 0, "Cannot open stream socket: %s. Retrying ...\n", strerror(errno));
-      }
-      sleep(10);
-   }
 
-   /*
-    * Reuse old sockets 
-    */
-   if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
-      Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
-   }
-
-   /* 
-    * Bind our local address so that the client can send to us.
-    */
-   bzero((char *) &serv_addr, sizeof(serv_addr));
-   serv_addr.sin_family = AF_INET;
-   serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
-   serv_addr.sin_port = htons(port);
-
-   for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
-      if (tlog <= 0) {
-        tlog = 2*60;                 /* Complain every 2 minutes */
-         Emsg2(M_WARNING, 0, "Cannot bind port %d: %s. Retrying ...\n", port, strerror(errno));
-      }
-      sleep(5);
-   }
-   listen(sockfd, 5);                /* tell system we are ready */
-
-   FD_ZERO(&sockset);
-   FD_SET(sockfd, &sockset);
-
-   /* Start work queue thread */
-   if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
-      Emsg1(M_ABORT, 0, "Could not init client queue: ERR=%s\n", strerror(stat));
-   }
-
-   for (;;) {
-      /* 
-       * Wait for a connection from a client process.
-       */
-      ready = sockset;
-      if ((stat = select(sockfd+1, &ready, NULL, NULL, NULL)) < 0) {
-        if (errno == EINTR || errno == EAGAIN) {
-           errno = 0;
-           continue;
-        }
-        close(sockfd);
-         Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
-        break;
-      }
-      clilen = sizeof(cli_addr);
-      newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
 #ifdef HAVE_LIBWRAP
-      P(mutex);                      /* hosts_access is not thread safe */
-      request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
-      fromhost(&request);
-      if (!hosts_access(&request)) {
-        V(mutex);
-         Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
-              inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
-        close(newsockfd);
-        continue;
-      }
-      V(mutex);
+#include "tcpd.h"
+int allow_severity = LOG_NOTICE;
+int deny_severity = LOG_WARNING;
 #endif
 
-      /*
-       * Receive notification when connection dies.
-       */
-      if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
-         Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
-      }
-
-      /* see who client is. i.e. who connected to us. */
-      caller = inet_ntoa(cli_addr.sin_addr);
-      if (caller == NULL) {
-         caller = "unknown client";
-      }
-
-      /* Queue client to be served */
-      if ((stat = workq_add(client_wq, 
-            (void *)init_bsock(newsockfd, "client", caller, port))) != 0) {
-         Emsg1(M_ABORT, 0, "Could not add job to client queue: ERR=%s\n", strerror(stat));
-      }
-   }
-}   
+static bool quit = false;
 
-
-/*
- * Bind an address so that we may accept connections
- * one at a time.
- */
-BSOCK *
-bnet_bind(int port)
+void bnet_stop_thread_server(pthread_t tid)
 {
-   int sockfd;
-   struct sockaddr_in serv_addr;      /* our address */
-   int tlog;
-   int turnon = 1;
-
-   /*
-    * Open a TCP socket  
-    */
-   for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
-      if (tlog <= 0) {
-        tlog = 2*60; 
-         Emsg1(M_ERROR, 0, "Cannot open stream socket: %s\n", strerror(errno));
-      }
-      sleep(60);
+   quit = true;
+   if (!pthread_equal(tid, pthread_self())) {
+      pthread_kill(tid, TIMEOUT_SIGNAL);
    }
-
-   /*
-    * Reuse old sockets 
-    */
-   if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
-      Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
-   }
-
-   /* 
-    * Bind our local address so that the client can send to us.
-    */
-   bzero((char *) &serv_addr, sizeof(serv_addr));
-   serv_addr.sin_family = AF_INET;
-   serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
-   serv_addr.sin_port = htons(port);
-
-   for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
-      if (tlog <= 0) {
-        tlog = 2*60;
-         Emsg2(M_WARNING, 0, "Cannot bind port %d: %s: retrying ...\n", port, strerror(errno));
-      }
-      sleep(5);
-   }
-   listen(sockfd, 1);                /* tell system we are ready */
-   return init_bsock(sockfd, "Server socket", "client", port);
 }
 
-/*
- * Accept a single connection 
- */
-BSOCK *
-bnet_accept(BSOCK *bsock, char *who)
+ /*
+     Become Threaded Network Server
+     This function is able to handle multiple server ips in
+     ipv4 and ipv6 style. The Addresse are give in a comma
+     seperated string in bind_addr
+     In the moment it is inpossible to bind different ports.
+  */
+void bnet_thread_server(dlist *addrs, int max_clients, 
+        workq_t *client_wq, void *handle_client_request(void *bsock))
 {
-   fd_set ready, sockset;
-   int newsockfd, stat, len;
+   int newsockfd, stat;
    socklen_t clilen;
-   struct sockaddr_in cli_addr;       /* client's address */
-   char *caller, *buf;
-   BSOCK *bs;
+   struct sockaddr_storage clientaddr;   /* client's address */
+   int tlog;
    int turnon = 1;
 #ifdef HAVE_LIBWRAP
    struct request_info request;
 #endif
-
-   /* 
-    * Wait for a connection from the client process.
-    */
-   FD_ZERO(&sockset);
-   FD_SET(bsock->fd, &sockset);
-
-   for (;;) {
-      /* 
-       * Wait for a connection from a client process.
-       */
-      ready = sockset;
-      if ((stat = select(bsock->fd+1, &ready, NULL, NULL, NULL)) < 0) {
-        if (errno == EINTR || errno == EAGAIN) {
-           errno = 0;
-           continue;
-        }
-         Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
-        newsockfd = -1;
-        break;
-      }
-      clilen = sizeof(cli_addr);
-      newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
-      break;
-   }
-
-#ifdef HAVE_LIBWRAP
-   P(mutex);
-   request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
-   fromhost(&request);
-   if (!hosts_access(&request)) {
-      V(mutex);
-      Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
-           inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
-      close(newsockfd);
-      return NULL;
-   }
-   V(mutex);
-#endif
-
+   IPADDR *addr;
+   struct s_sockfd {
+      dlink link;                     /* this MUST be the first item */
+      int fd;
+      int port;
+   } *fd_ptr = NULL;
+   char buf[128];
+   dlist sockfds;
+
+   char allbuf[256 * 10];
+   remove_duplicate_addresses(addrs);
+   Dmsg1(20, "Addresses %s\n", build_addresses_str(addrs, allbuf, sizeof(allbuf)));
    /*
-    * Receive notification when connection dies.
+    * Listen on each address provided.
     */
-   if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
-      Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
-   }
-
-   /* see who client is. I.e. who connected to us.
-    * return it in the input message buffer.
-    */
-   if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
-      strcpy(bsock->msg, caller);
-   } else {
-      bsock->msg[0] = 0;
-   }
-   bsock->msglen = strlen(bsock->msg);
-
-   if (newsockfd < 0) {
-      Emsg2(M_FATAL, 0, "Socket accept error for %s. ERR=%s\n", who,
-           strerror(errno));
-      return NULL;
-   } else {
-      if (caller == NULL) {
-         caller = "unknown";
+   foreach_dlist(addr, addrs) {
+      /* Allocate on stack from -- no need to free */
+      fd_ptr = (s_sockfd *)alloca(sizeof(s_sockfd));
+      fd_ptr->port = addr->get_port_net_order();
+      /*
+       * Open a TCP socket
+       */
+      for (tlog= 60; (fd_ptr->fd=socket(addr->get_family(), SOCK_STREAM, 0)) < 0; tlog -= 10) {
+         if (tlog <= 0) {
+            berrno be;
+            char curbuf[256];
+            Emsg3(M_ABORT, 0, _("Cannot open stream socket. ERR=%s. Current %s All %s\n"),
+                       be.bstrerror(),
+                       addr->build_address_str(curbuf, sizeof(curbuf)),
+                       build_addresses_str(addrs, allbuf, sizeof(allbuf)));
+         }
+         bmicrosleep(10, 0);
       }
-      len = strlen(caller) + strlen(who) + 3;
-      buf = (char *) malloc(len);
-      strcpy(buf, who);
-      strcat(buf, ": ");
-      strcat(buf, caller);
-      bs = init_bsock(newsockfd, "client", buf, bsock->port);
-      free(buf);
-      return bs;                     /* return new BSOCK */
-   }
-}   
-
-
-
-
-/*
- * The following code will soon be deleted, don't attempt
- * to use it.
- */
-#ifdef dont_have_threads_junk
-
-/*
- * This routine is called by the main process to
- * track its children. On CYGWIN, the child processes
- * don't always exit when the other end of the socket
- * hangs up. Thus they remain hung on a read(). After
- * 30 seconds, we send them a SIGTERM signal, which 
- * causes them to wake up to the reality of the situation.
- *
- * Also, on certain Unix systems such as SunOS, we must
- * explicitly do the wait, otherwise, the child that dies
- * remains a zombie potentially remaining bound to the
- * port.
- */
-
-#ifdef HAVE_SUN_OS
-
-static void reap_children(int childpid)
-{
-   static int pids[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
-   static int times[10];
-   int i;
-   time_t now;
-
-   time(&now);
-   for (i=0; i<10; i++) {
-      if (pids[i] && (waitpid(pids[i], NULL, WNOHANG) == pids[i])) {
-        pids[i] = 0;
+      /*
+       * Reuse old sockets
+       */
+      if (setsockopt(fd_ptr->fd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon,
+           sizeof(turnon)) < 0) {
+         berrno be;
+         Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"),
+               be.bstrerror());
+      }
+
+      int tmax = 1 * (60 / 5);    /* wait 1 minute max */
+      for (tlog = 0; bind(fd_ptr->fd, addr->get_sockaddr(), addr->get_sockaddr_len()) == SOCKET_ERROR; tlog -= 5) {
+         berrno be;
+         if (tlog <= 0) {
+            tlog = 1 * 60;         /* Complain every 1 minute */
+            Emsg2(M_WARNING, 0, _("Cannot bind port %d: ERR=%s: Retrying ...\n"),
+                  ntohs(fd_ptr->port), be.bstrerror());
+            Dmsg2(20, "Cannot bind port %d: ERR=%s: Retrying ...\n",
+                  ntohs(fd_ptr->port), be.bstrerror());
+
+         }
+         bmicrosleep(5, 0);
+         if (--tmax <= 0) {
+            Emsg2(M_ABORT, 0, _("Cannot bind port %d: ERR=%s.\n"), ntohs(fd_ptr->port),
+                  be.bstrerror());
+            Pmsg2(000, "Aborting cannot bind port %d: ERR=%s.\n", ntohs(fd_ptr->port),
+                  be.bstrerror());
+         }
+      }
+      if (listen(fd_ptr->fd, 50) < 0) {      /* tell system we are ready */
+         berrno be;
+         Emsg2(M_ABORT, 0, _("Cannot bind port %d: ERR=%s.\n"), ntohs(fd_ptr->port),
+               be.bstrerror());
       } else {
-        if (pids[i] && ((now - times[i]) > 30))
-           kill(pids[i], SIGTERM);
-      }
-   }
-   for (i=0; i<10; i++) {
-      if (pids[i] && (waitpid(pids[i], NULL, WNOHANG) == pids[i])) {
-        pids[i] = 0;
-      }
-      if (childpid && (pids[i] == 0)) {
-        pids[i] = childpid;
-        times[i] = now;
-        childpid = 0;
+         sockfds.append(fd_ptr);
       }
    }
-}
-
-#endif /* HAVE_SUN_OS */
-
-/* Become network server */
-void
-bnet_server(int port, void handle_client_request(BSOCK *bsock))
-{
-   int newsockfd, sockfd, clilen, childpid, stat;
-   struct sockaddr_in cli_addr;       /* client's address */
-   struct sockaddr_in serv_addr;      /* our address */
-   int tlog;
-   fd_set ready, sockset;
-   int turnon = 1;
-   char *caller;
-#ifdef HAVE_LIBWRAP
-   struct request_info request;
-#endif
-
-   /* **** FIXME **** handle BSD too */
-   signal(SIGCHLD, SIG_IGN);
-   
-   /*
-    * Open a TCP socket  
-    */
-   for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
-      if (tlog <= 0) {
-        tlog = 60; 
-         Emsg1(M_ERROR, 0, "Cannot open stream socket: %s. Retrying ...\n", strerror(errno));
-      }
-      sleep(10);
+   if (sockfds.size() == 0) {
+      Emsg0(M_ABORT, 0, _("No addr/port found to listen on.\n"));
    }
-
-   /*
-    * Reuse old sockets 
-    */
-   if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
-      Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
+   /* Start work queue thread */
+   if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
+      berrno be;
+      be.set_errno(stat);
+      Emsg1(M_ABORT, 0, _("Could not init client queue: ERR=%s\n"), be.bstrerror());
    }
-
    /*
-    * Receive notification when connection dies.
-    */
-   if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
-      Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
-   }
-
-   /* 
-    * Bind our local address so that the client can send to us.
+    * Wait for a connection from the client process.
     */
-   bzero((char *) &serv_addr, sizeof(serv_addr));
-   serv_addr.sin_family = AF_INET;
-   serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
-   serv_addr.sin_port = htons(port);
-
-   for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
-      if (tlog <= 0) {
-        tlog = 2*60;                 /* Complain every 2 minutes */
-         Emsg2(M_WARNING, 0, "Cannot bind port %d: %s. Retrying ...\n", port, strerror(errno));
-      }
-      sleep(5);
-   }
-   listen(sockfd, 5);                /* tell system we are ready */
-
-   FD_ZERO(&sockset);
-   FD_SET(sockfd, &sockset);
-
-   for (;;) {
-      /* 
-       * Wait for a connection from a client process.
-       */
-      ready = sockset;
-      if ((stat = select(sockfd+1, &ready, NULL, NULL, NULL)) < 0) {
-        if (errno == EINTR || errno == EAGAIN) {
-           errno = 0;
-           continue;
-        }
-        close(sockfd);
-         Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
-        break;
-      }
-      clilen = sizeof(cli_addr);
-      newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
-
+   for (; !quit;) {
+      unsigned int maxfd = 0;
+      fd_set sockset;
+      FD_ZERO(&sockset);
+      foreach_dlist(fd_ptr, &sockfds) {
+         FD_SET((unsigned)fd_ptr->fd, &sockset);
+         maxfd = maxfd > (unsigned)fd_ptr->fd ? maxfd : fd_ptr->fd;
+      }
+      errno = 0;
+      if ((stat = select(maxfd + 1, &sockset, NULL, NULL, NULL)) < 0) {
+         berrno be;                   /* capture errno */
+         if (errno == EINTR) {
+            continue;
+         }
+         Emsg1(M_FATAL, 0, _("Error in select: %s\n"), be.bstrerror());
+         break;
+      }
+
+      foreach_dlist(fd_ptr, &sockfds) {
+         if (FD_ISSET(fd_ptr->fd, &sockset)) {
+            /* Got a connection, now accept it. */
+            do {
+               clilen = sizeof(clientaddr);
+               newsockfd = baccept(fd_ptr->fd, (struct sockaddr *)&clientaddr, &clilen);
+               newsockfd = set_socket_errno(newsockfd);
+            } while (newsockfd == SOCKET_ERROR && (errno == EINTR || errno == EAGAIN));
+            if (newsockfd == SOCKET_ERROR) {
+               Dmsg2(20, "Accept=%d errno=%d\n", newsockfd, errno);
+               continue;
+            }
 #ifdef HAVE_LIBWRAP
-      request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
-      fromhost(&request);
-      if (!hosts_access(&request)) {
-         Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
-              inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
-        close(newsockfd);
-        continue;
-      }
-#endif
-
-      /*
-       * Receive notification when connection dies.
-       */
-      if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
-         Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
-      }
-
-
-      /* see who client is. i.e. who connected to us. */
-      caller = inet_ntoa(cli_addr.sin_addr);
-      if (caller == NULL) {
-         caller = "client";
-      }
-
-#ifdef HAVE_CYGWIN
-      childpid = 0;
-      handle_client_request(init_bsock(newsockfd, "client", caller, port));
-#else
-      /* fork to provide the response */
-      for (tlog=0; (childpid = fork()) < 0; tlog -= 5*60 ) {
-        if (tlog <= 0) {
-           tlog = 60*60; 
-            Emsg1(M_FATAL, 0, "Fork error: %s\n", strerror(errno));
-        }
-        sleep(5*60);
-      } 
-      if (childpid == 0) {     /* child process */
-        close(sockfd);              /* close original socket */
-         handle_client_request(init_bsock(newsockfd, "client", caller, port));    /* process the request */
-         Dmsg0(99, "back from handle request\n");
-        close(newsockfd);
-        exit(0);
-      }
+            P(mutex);              /* hosts_access is not thread safe */
+            request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
+            fromhost(&request);
+            if (!hosts_access(&request)) {
+               V(mutex);
+               Jmsg2(NULL, M_SECURITY, 0,
+                     _("Connection from %s:%d refused by hosts.access\n"),
+                     sockaddr_to_ascii((struct sockaddr *)&clientaddr,
+                                       sizeof(clientaddr), buf, sizeof(buf)),
+                     sockaddr_get_port((struct sockaddr *)&clientaddr));
+               close(newsockfd);
+               continue;
+            }
+            V(mutex);
 #endif
 
-      close(newsockfd);             /* parent process */
-#ifdef HAVE_SUN_OS
-      reap_children(childpid);
-#endif /* HAVE_SUN_OS */
-    }
-}   
-
-#endif /* no threads -- not supported any more sorry! */
+            /*
+             * Receive notification when connection dies.
+             */
+            if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon,
+                 sizeof(turnon)) < 0) {
+               berrno be;
+               Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
+                     be.bstrerror());
+            }
+
+            /* see who client is. i.e. who connected to us. */
+            P(mutex);
+            sockaddr_to_ascii((struct sockaddr *)&clientaddr, sizeof(clientaddr), buf, sizeof(buf));
+            V(mutex);
+            BSOCK *bs;
+            bs = init_bsock(NULL, newsockfd, "client", buf, ntohs(fd_ptr->port),
+                    (struct sockaddr *)&clientaddr);
+            if (bs == NULL) {
+               Jmsg0(NULL, M_ABORT, 0, _("Could not create client BSOCK.\n"));
+            }
+
+            /* Queue client to be served */
+            if ((stat = workq_add(client_wq, (void *)bs, NULL, 0)) != 0) {
+               berrno be;
+               be.set_errno(stat);
+               Jmsg1(NULL, M_ABORT, 0, _("Could not add job to client queue: ERR=%s\n"),
+                     be.bstrerror());
+            }
+         }
+      }
+   }
+
+   /* Cleanup open files and pointers to them */
+   while ((fd_ptr = (s_sockfd *)sockfds.first())) {
+      close(fd_ptr->fd);
+      sockfds.remove(fd_ptr);     /* don't free() item it is on stack */
+   }
+
+   /* Stop work queue thread */
+   if ((stat = workq_destroy(client_wq)) != 0) {
+      berrno be;
+      be.set_errno(stat);
+      Emsg1(M_FATAL, 0, _("Could not destroy client queue: ERR=%s\n"),
+            be.bstrerror());
+   }
+}