]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
win32: Modify namedpipe to configure the message protocol
authorEric Bollengier <eric@baculasystems.com>
Fri, 21 Apr 2023 13:25:41 +0000 (15:25 +0200)
committerEric Bollengier <eric@baculasystems.com>
Thu, 14 Sep 2023 11:57:01 +0000 (13:57 +0200)
bacula/src/findlib/namedpipe.c
bacula/src/findlib/namedpipe.h
bacula/src/lib/bsys.c

index f724b259ecd0eac690b21cb746af8df521d18de5..54fc6f7d0f6b80a47d9ba448401e2143cdcd0c05 100644 (file)
    Bacula(R) is a registered trademark of Kern Sibbald.
 */
 
+/* Written by Eric Bollengier */
 #ifdef HAVE_WIN32
-
 # include <windows.h> 
 # include <conio.h>
 # include <stdbool.h>
+# include <io.h>
 
 #else  /* !HAVE_WIN32 */
 
 # define Dmsg(level, ...) printf(__VA_ARGS__ ) 
 #endif
 
+void namedpipe_use_messages(NamedPipe *self)
+{
+   self->use_msg = true;
+}
+
 #ifdef HAVE_WIN32
 
 void namedpipe_init(NamedPipe *self)
 {
    self->fd   = INVALID_HANDLE_VALUE;
    self->ifd  = -1;
+   self->use_msg = false;
 }
 
 void namedpipe_free(NamedPipe *self)
@@ -58,17 +65,21 @@ void namedpipe_free(NamedPipe *self)
       CloseHandle(self->fd);
       self->fd = INVALID_HANDLE_VALUE;
       self->ifd = -1;
+      self->use_msg = false;
    }
 }
 #define BUFSIZE 8192
 int namedpipe_create(NamedPipe *self, const char *path, mode_t mode)
 {
+   DWORD flag = 0;
+   if (self->use_msg) {
+      flag  = PIPE_READMODE_MESSAGE | PIPE_TYPE_MESSAGE;       // message type pipe 
+   }
    /* On windows,  */
    self->fd = CreateNamedPipeA( 
       path,                     // pipe name 
       PIPE_ACCESS_DUPLEX,       // read/write access 
-      PIPE_TYPE_MESSAGE |       // message type pipe 
-      PIPE_READMODE_MESSAGE |   // message-read mode 
+      flag |
       PIPE_WAIT,                // blocking mode 
       PIPE_UNLIMITED_INSTANCES, // max. instances  
       BUFSIZE,                  // output buffer size 
@@ -84,14 +95,15 @@ int namedpipe_create(NamedPipe *self, const char *path, mode_t mode)
    return 0;
 }
 
-int namedpipe_open(NamedPipe *self, const char *path, mode_t mode)
+intptr_t namedpipe_open(NamedPipe *self, const char *path, mode_t mode)
 {
-   bool fConnected=false;
    int  retry = 30;
+   self->connected = false;
+   self->mode = mode;
 
    if (self->fd != INVALID_HANDLE_VALUE) { /* server mode */
 
-      fConnected = ConnectNamedPipe(self->fd, NULL) ? 
+      self->connected = ConnectNamedPipe(self->fd, NULL) ? 
          TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); 
 
    } else {                               /* client mode */
@@ -136,40 +148,55 @@ int namedpipe_open(NamedPipe *self, const char *path, mode_t mode)
       } 
    }
 
-   DWORD dwMode = PIPE_READMODE_MESSAGE; 
+   DWORD dwMode = 0;
+   if (self->use_msg) {
+      dwMode = PIPE_READMODE_MESSAGE;
+   }
    
-   fConnected = SetNamedPipeHandleState( 
+   self->connected = SetNamedPipeHandleState( 
       self->fd, // pipe handle 
       &dwMode,  // new pipe mode 
       NULL,     // don't set maximum bytes 
       NULL);    // don't set maximum time 
 
-   if (!fConnected) {
+   if (!self->connected) {
       Dmsg(10, "SetNamedPipeHandleState failed, ERR=%d.\n", 
            (int)GetLastError()); 
    }
 
-   if (fConnected) {
-      int m = 0;
-      if (mode & O_WRONLY || mode & O_APPEND) {
+   return (intptr_t)self->fd;
+}
+
+int namedpipe_get_fd(NamedPipe *self)
+{
+   if (self->fd == INVALID_HANDLE_VALUE) {
+      return -1;
+   }
+   if (self->connected) {
+      if (self->mode & O_WRONLY || self->mode & O_APPEND) {
          m |= O_APPEND;
-         
-      } else if (mode & O_RDONLY) {
+      
+      } else if (self->mode & O_RDONLY) {
          m |= O_RDONLY;
       }
       self->ifd = _open_osfhandle((intptr_t)self->fd, m);
+      self->fd = INVALID_HANDLE_VALUE;
    }
-
    return self->ifd;
 }
 
-
 #else  /* !HAVE_WIN32 */
 
+int namedpipe_get_fd(NamedPipe *self)
+{
+   return self->ifd;
+}
+
 void namedpipe_init(NamedPipe *self)
 {
    self->fd   = -1;
    self->ifd  = -1;
+   self->use_msg = 0;
    self->name = NULL;
 }
 
@@ -179,6 +206,7 @@ void namedpipe_free(NamedPipe *self)
       close(self->fd);
       self->fd  = -1;
       self->ifd = -1;
+      self->use_msg = 0;
    }
    if (self->name) {
       unlink(self->name);
@@ -199,10 +227,10 @@ int namedpipe_create(NamedPipe *self, const char *path, mode_t mode)
    return 0;
 }
 
-int namedpipe_open(NamedPipe *self, const char *path, mode_t mode)
+intptr_t namedpipe_open(NamedPipe *self, const char *path, mode_t mode)
 {
    self->ifd = self->fd = open(path, mode);
-   return self->fd;
+   return (intptr_t)self->fd;
 }
 
 #endif  /* HAVE_WIN32 */
index ea3f00c03c3085457a2133b34e23626e9f42b6e8..960545f7a0cea8c59c572c49dec207d5b2bffece 100644 (file)
 typedef struct {
 #ifdef HAVE_WIN32
    HANDLE    fd;
+   mode_t    mode;
+   int       connected;
 #else
    char     *name;
    int       fd;
 #endif
    int       ifd;
+   int       use_msg;
 } NamedPipe;
 
 
 void namedpipe_init(NamedPipe *self);
 void namedpipe_free(NamedPipe *self);
 int namedpipe_create(NamedPipe *self, const char *path, mode_t mode);
-int namedpipe_open(NamedPipe *self, const char *path, mode_t mode);
+void namedpipe_use_messages(NamedPipe *self);
+intptr_t namedpipe_open(NamedPipe *self, const char *path, mode_t mode);
+int namedpipe_get_fd(NamedPipe *self);
 
 #endif
index 4c1b848cb34af6e8310a24a16f6cac0c60f205bc..588059f18da003b75abf4c0b21b4b1300b483e19 100644 (file)
@@ -2137,7 +2137,8 @@ void *th1(void *a)
       namedpipe_free(&p);
       exit(2);
    }
-   fd = namedpipe_open(&p, buf, O_RDONLY);
+   namedpipe_open(&p, buf, O_RDONLY);
+   fd = namedpipe_get_fd(&p);
    if (fd < 0) {
       berrno be;
       Dmsg2(0, "R: Unable to open the fifo %s. ERR=%s\n", buf, be.bstrerror());
@@ -2192,7 +2193,8 @@ void *th2(void *a)
       exit(2);
    }
 
-   fd = namedpipe_open(&p, buf, O_WRONLY);
+   namedpipe_open(&p, buf, O_WRONLY);
+   fd = namedpipe_get_fd(&p);
    if (fd < 0) {
       berrno be;
       Dmsg2(0, "W: Unable to open the fifo %s. ERR=%s\n", buf, be.bstrerror());