]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bnet.c
Make PurgeMigrationJob directive name correspond to doc
[bacula/bacula] / bacula / src / lib / bnet.c
index 8f802b15111eaa286f8b88bc474c96734e8db3b7..b2a63b9c6cbab9a22d190f2220f23cf4f23bc92f 100644 (file)
@@ -1,12 +1,12 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version two of the GNU General Public
+   modify it under the terms of version three of the GNU Affero General Public
    License as published by the Free Software Foundation and included
    in the file LICENSE.
 
@@ -15,7 +15,7 @@
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
+   You should have received a copy of the GNU Affero General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
@@ -33,7 +33,6 @@
  * Adapted and enhanced for Bacula, originally written
  * for inclusion in the Apcupsd package
  *
- *   Version $Id$
  */
 
 
@@ -79,19 +78,38 @@ int32_t read_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
       errno = 0;
       nread = socketRead(bsock->m_fd, ptr, nleft);
       if (bsock->is_timed_out() || bsock->is_terminated()) {
-         return nread;
+         return -1;
       }
+
+#ifdef HAVE_WIN32
+      /*
+       * For Windows, we must simulate Unix errno on a socket
+       *  error in order to handle errors correctly.
+       */
+      if (nread == SOCKET_ERROR) {
+        DWORD err = WSAGetLastError();
+        nread = -1;
+        if (err == WSAEINTR) {
+           errno = EINTR;
+        } else if (err == WSAEWOULDBLOCK) {
+           errno = EAGAIN;
+        } else {
+           errno = EIO;            /* some other error */
+        }
+     }
+#endif
+
       if (nread == -1) {
          if (errno == EINTR) {
             continue;
          }
          if (errno == EAGAIN) {
-            bmicrosleep(0, 200000);  /* try again in 200ms */
+            bmicrosleep(0, 20000);  /* try again in 20ms */
             continue;
          }
       }
       if (nread <= 0) {
-         return nread;             /* error, or EOF */
+         return -1;                /* error, or EOF */
       }
       nleft -= nread;
       ptr += nread;
@@ -135,8 +153,27 @@ int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
          errno = 0;
          nwritten = socketWrite(bsock->m_fd, ptr, nleft);
          if (bsock->is_timed_out() || bsock->is_terminated()) {
-            return nwritten;
+            return -1;
          }
+
+#ifdef HAVE_WIN32
+         /*
+          * For Windows, we must simulate Unix errno on a socket
+          *  error in order to handle errors correctly.
+          */
+         if (nwritten == SOCKET_ERROR) {
+            DWORD err = WSAGetLastError();
+            nwritten = -1;
+            if (err == WSAEINTR) {
+               errno = EINTR;
+            } else if (err == WSAEWOULDBLOCK) {
+               errno = EAGAIN;
+            } else {
+               errno = EIO;        /* some other error */
+            }
+         }
+#endif
+
       } while (nwritten == -1 && errno == EINTR);
       /*
        * If connection is non-blocking, we will get EAGAIN, so
@@ -149,13 +186,13 @@ int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
 
          FD_ZERO(&fdset);
          FD_SET((unsigned)bsock->m_fd, &fdset);
-         tv.tv_sec = 10;
+         tv.tv_sec = 1;
          tv.tv_usec = 0;
          select(bsock->m_fd + 1, NULL, &fdset, NULL, &tv);
          continue;
       }
       if (nwritten <= 0) {
-         return nwritten;          /* error */
+         return -1;                /* error */
       }
       nleft -= nwritten;
       ptr += nwritten;
@@ -652,8 +689,10 @@ const char *bnet_sig_to_ascii(BSOCK * bs)
       return "BNET_HEARTBEAT";
    case BNET_HB_RESPONSE:
       return "BNET_HB_RESPONSE";
-   case BNET_PROMPT:
-      return "BNET_PROMPT";
+   case BNET_SUB_PROMPT:
+      return "BNET_SUB_PROMPT";
+   case BNET_TEXT_INPUT:
+      return "BNET_TEXT_INPUT";
    default:
       sprintf(buf, _("Unknown sig %d"), (int)bs->msglen);
       return buf;
@@ -673,7 +712,7 @@ BSOCK *init_bsock(JCR * jcr, int sockfd, const char *who, const char *host, int
    bsock->tls = NULL;
    bsock->errors = 0;
    bsock->m_blocking = 1;
-   bsock->msg = get_pool_memory(PM_MESSAGE);
+   bsock->msg = get_pool_memory(PM_BSOCK);
    bsock->errmsg = get_pool_memory(PM_MESSAGE);
    bsock->set_who(bstrdup(who));
    bsock->set_host(bstrdup(host));
@@ -693,7 +732,7 @@ BSOCK *dup_bsock(BSOCK *osock)
 {
    BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
    memcpy(bsock, osock, sizeof(BSOCK));
-   bsock->msg = get_pool_memory(PM_MESSAGE);
+   bsock->msg = get_pool_memory(PM_BSOCK);
    bsock->errmsg = get_pool_memory(PM_MESSAGE);
    if (osock->who()) {
       bsock->set_who(bstrdup(osock->who()));