]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bnet_server.c
Use the command line utility dropdb instead of the psql command
[bacula/bacula] / bacula / src / lib / bnet_server.c
index a3abc674bffc68195e47b40a8b215dedeff425b4..308ef2c87e0cb3be1606997b2ceb5c2476ff2f05 100644 (file)
   */
 
 #include "bacula.h"
+#undef DEV_BSIZE
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
 #include <netdb.h>
+#ifdef HAVE_ARPA_NAMESER_H
+#include <arpa/nameser.h>
+#endif
+#ifdef HAVE_RESOLV_H
+#include <resolv.h>
+#endif
+
+
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
 #ifdef HAVE_LIBWRAP
 #include "tcpd.h"
 int allow_severity = LOG_NOTICE;
 int deny_severity = LOG_WARNING;
-static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 #endif
 
 /* Become Threaded Network Server */
 void
-bnet_thread_server(int port, int max_clients, workq_t *client_wq, 
-                  void handle_client_request(void *bsock))
+bnet_thread_server(char *bind_addr, 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 */
+   struct in_addr bind_ip;           /* address to bind to */
    int tlog;
    fd_set ready, sockset;
    int turnon = 1;
@@ -60,35 +70,47 @@ bnet_thread_server(int port, int max_clients, workq_t *client_wq,
    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));
+         Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s. Retrying ...\n"), strerror(errno));
       }
-      sleep(10);
+      bmicrosleep(10, 0);
    }
 
    /*
     * 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));
+   if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&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));
+   bind_ip.s_addr = htonl(INADDR_ANY);
+   if (bind_addr && bind_addr[0]) {
+#ifdef HAVE_INET_PTON
+      if (inet_pton(AF_INET, bind_addr, &bind_ip) <= 0) {
+#else
+      if (inet_aton(bind_addr, &bind_ip) <= 0) {
+#endif
+         Emsg1(M_WARNING, 0, _("Invalid bind address: %s, using INADDR_ANY\n"),
+           bind_addr);
+        bind_ip.s_addr = htonl(INADDR_ANY);
+      }
+   }
+   memset((char *) &serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
-   serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
+   serv_addr.sin_addr.s_addr = bind_ip.s_addr;
    serv_addr.sin_port = htons(port);
 
    int tmax = 30 * (60 / 5);         /* wait 30 minutes max */
    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));
+         Emsg2(M_WARNING, 0, _("Cannot bind port %d: %s. Retrying ...\n"), port, strerror(errno));
       }
-      sleep(5);
+      bmicrosleep(5, 0);
       if (--tmax <= 0) {
-         Emsg2(M_ABORT, 0, "Cannot bind port %d: %s.\n", port, strerror(errno));
+         Emsg2(M_ABORT, 0, _("Cannot bind port %d: %s.\n"), port, strerror(errno));
       }
    }
    listen(sockfd, 5);                /* tell system we are ready */
@@ -98,7 +120,7 @@ bnet_thread_server(int port, int max_clients, workq_t *client_wq,
 
    /* 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));
+      Emsg1(M_ABORT, 0, _("Could not init client queue: ERR=%s\n"), strerror(stat));
    }
 
    for (;;) {
@@ -106,17 +128,23 @@ bnet_thread_server(int port, int max_clients, workq_t *client_wq,
        * 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;
-        }
+      do {
+        errno = 0;
+        stat = select(sockfd+1, &ready, NULL, NULL, NULL);       
+      } while(stat == -1 && (errno == EINTR || errno == EAGAIN));
+      if (stat < 0) {
         close(sockfd);
-         Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
+         Emsg1(M_FATAL, 0, _("Error in select: %s\n"), strerror(errno));
         break;
       }
-      clilen = sizeof(cli_addr);
-      newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
+      do {
+        clilen = sizeof(cli_addr);
+        errno = 0;
+        newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
+      } while (newsockfd < 0 && (errno == EINTR || errno == EAGAIN));
+      if (newsockfd < 0) {
+        continue;
+      }
 
 #ifdef HAVE_LIBWRAP
       P(mutex);                      /* hosts_access is not thread safe */
@@ -124,7 +152,7 @@ bnet_thread_server(int port, int max_clients, workq_t *client_wq,
       fromhost(&request);
       if (!hosts_access(&request)) {
         V(mutex);
-         Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
+         Jmsg2(NULL, 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;
@@ -135,21 +163,28 @@ bnet_thread_server(int port, int max_clients, workq_t *client_wq,
       /*
        * 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));
+      if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&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);
+      P(mutex);
+      caller = inet_ntoa(cli_addr.sin_addr);  /* NOT thread safe, use mutex */
       if (caller == NULL) {
          caller = "unknown client";
       }
 
+      BSOCK *bs = init_bsock(NULL, newsockfd, "client", caller, port);
+      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 *)init_bsock(newsockfd, "client", caller, port))) != 0) {
-         Emsg1(M_ABORT, 0, "Could not add job to client queue: ERR=%s\n", strerror(stat));
+      if ((stat = workq_add(client_wq, (void *)bs, NULL, 0)) != 0) {
+        V(mutex);
+         Jmsg1(NULL, M_ABORT, 0, _("Could not add job to client queue: ERR=%s\n"), strerror(stat));
       }
+      V(mutex);
    }
 }   
 
@@ -171,18 +206,21 @@ bnet_bind(int port)
     * Open a TCP socket  
     */
    for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
+      if (errno == EINTR) {
+        continue;
+      }
       if (tlog <= 0) {
         tlog = 2*60; 
-         Emsg1(M_ERROR, 0, "Cannot open stream socket: %s\n", strerror(errno));
+         Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s\n"), strerror(errno));
       }
-      sleep(60);
+      bmicrosleep(60, 0);
    }
 
    /*
     * 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));
+   if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
+      Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n") , strerror(errno));
    }
 
    /* 
@@ -194,14 +232,17 @@ bnet_bind(int port)
    serv_addr.sin_port = htons(port);
 
    for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
+      if (errno == EINTR) {
+        continue;
+      }
       if (tlog <= 0) {
         tlog = 2*60;
-         Emsg2(M_WARNING, 0, "Cannot bind port %d: %s: retrying ...\n", port, strerror(errno));
+         Emsg2(M_WARNING, 0, _("Cannot bind port %d: %s: retrying ...\n"), port, strerror(errno));
       }
-      sleep(5);
+      bmicrosleep(5, 0);
    }
    listen(sockfd, 1);                /* tell system we are ready */
-   return init_bsock(sockfd, "Server socket", "client", port);
+   return init_bsock(NULL, sockfd, _("Server socket"), _("client"), port);
 }
 
 /*
@@ -237,13 +278,17 @@ bnet_accept(BSOCK *bsock, char *who)
            errno = 0;
            continue;
         }
-         Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
+         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;
+      do {
+        clilen = sizeof(cli_addr);
+        newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
+      } while (newsockfd < 0 && errno == EINTR);
+      if (newsockfd >= 0) {
+        break;
+      }
    }
 
 #ifdef HAVE_LIBWRAP
@@ -252,7 +297,7 @@ bnet_accept(BSOCK *bsock, char *who)
    fromhost(&request);
    if (!hosts_access(&request)) {
       V(mutex);
-      Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
+      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;
@@ -263,22 +308,22 @@ bnet_accept(BSOCK *bsock, char *who)
    /*
     * 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));
+   if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&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);
+      pm_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,
+      Emsg2(M_FATAL, 0, _("Socket accept error for %s. ERR=%s\n"), who,
            strerror(errno));
       return NULL;
    } else {
@@ -290,199 +335,10 @@ bnet_accept(BSOCK *bsock, char *who)
       strcpy(buf, who);
       strcat(buf, ": ");
       strcat(buf, caller);
-      bs = init_bsock(newsockfd, "client", buf, bsock->port);
+      bs = init_bsock(NULL, newsockfd, "client", buf, bsock->port);
       free(buf);
       return bs;                     /* return new BSOCK */
    }
 }   
 
 #endif
-
-
-
-/*
- * The following code will soon be deleted, don't attempt
- * to use it.
- */
-#ifdef dont_have_threads
-
-/*
- * 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;
-      } 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;
-      }
-   }
-}
-
-#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);
-   }
-
-   /*
-    * 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));
-   }
-
-   /*
-    * 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.
-    */
-   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);
-
-#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);
-      }
-#endif
-
-      close(newsockfd);             /* parent process */
-#ifdef HAVE_SUN_OS
-      reap_children(childpid);
-#endif /* HAVE_SUN_OS */
-    }
-}   
-
-#endif /* no threads -- not supported any more sorry! */