<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># HG changeset patch
# Parent dd6f2d1eb15a9e658e4d9acd0c11fa90c93ad969
Implement dWrite.

diff -r dd6f2d1eb15a dlib/dlib.c
--- a/dlib/dlib.c
+++ b/dlib/dlib.c
@@ -931,6 +931,29 @@
    return line;
 }
 
+/**
+ * @brief Write buf to fd handling EINTR and data splits.
+ * @return n on success, -1 on error
+ */
+ssize_t dWrite(int fd, const void *buf, size_t n)
+{
+   size_t w;
+   ssize_t m;
+
+   w = 0;
+   while (w &lt; n) {
+      m = write(fd, buf + w, n - w);
+      if (m &lt;= 0) {
+         if (m == -1 &amp;&amp; errno == EINTR)
+            continue;
+         else
+            return -1;
+      }
+      w += m;
+   }
+   return n;
+}
+
 /*
  * Close a FD handling EINTR.
  */
diff -r dd6f2d1eb15a dlib/dlib.h
--- a/dlib/dlib.h
+++ b/dlib/dlib.h
@@ -176,6 +176,7 @@
 char *dGetcwd();
 char *dGethomedir();
 char *dGetline(FILE *stream);
+ssize_t dWrite(int fd, const void *buf, size_t n);
 int dClose(int fd);
 
 #ifdef __cplusplus
diff -r dd6f2d1eb15a dpid/dpid_common.c
--- a/dpid/dpid_common.c
+++ b/dpid/dpid_common.c
@@ -12,6 +12,8 @@
 #include &lt;errno.h&gt;
 #include &lt;stdio.h&gt;
 #include &lt;unistd.h&gt;
+
+#include "../dlib/dlib.h"
 #include "dpid_common.h"
 
 /*
@@ -33,13 +35,10 @@
 {
    ssize_t ret;
 
-   do {
-      ret = write(fd, msg, strlen(msg));
-   } while (ret == -1 &amp;&amp; errno == EINTR);
-   if (ret == -1) {
+   ret = dWrite(fd, msg, strlen(msg));
+   if (ret == -1)
       MSG_ERR("%s:%d: write: %s\n", file, line, dStrerror(errno));
-   }
-   return (ret);
+   return ret;
 }
 
 /*!
diff -r dd6f2d1eb15a src/IO/dpi.c
--- a/src/IO/dpi.c
+++ b/src/IO/dpi.c
@@ -248,31 +248,6 @@
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
 /*
- * Write data into a file descriptor taking care of EINTR
- * and possible data splits.
- * Return value: 1 on success, -1 on error.
- */
-static int Dpi_blocking_write(int fd, const char *msg, int msg_len)
-{
-   int st, sent = 0;
-
-   while (sent &lt; msg_len) {
-      st = write(fd, msg + sent, msg_len - sent);
-      if (st &lt; 0) {
-         if (errno == EINTR) {
-            continue;
-         } else {
-            MSG_ERR("[Dpi_blocking_write] %s\n", dStrerror(errno));
-            break;
-         }
-      }
-      sent += st;
-   }
-
-   return (sent == msg_len) ? 1 : -1;
-}
-
-/*
  * Read all the available data from a filedescriptor.
  * This is intended for short answers, i.e. when we know the server
  * will write it all before being preempted. For answers that may come
@@ -357,8 +332,8 @@
             dFree(path1);
             if (execlp("dpid", "dpid", (char*)NULL) == -1) {
                MSG("Dpi_start_dpid (child): %s\n", dStrerror(errno));
-               if (Dpi_blocking_write(st_pipe[1], "ERROR", 5) == -1) {
-                  MSG("Dpi_start_dpid (child): can't write to pipe.\n");
+               if (dWrite(st_pipe[1], "ERROR", 5) == -1) {
+                  MSG("Dpi_start_dpid (child): can't write to pipe: %s\n", dStrerror(errno));
                }
                dClose(st_pipe[1]);
                _exit (EXIT_FAILURE);
@@ -558,7 +533,7 @@
       request = a_Dpip_build_cmd("cmd=%s msg=%s", "check_server", server_name);
       _MSG("[%s]\n", request);
 
-      if (Dpi_blocking_write(sock_fd, request, strlen(request)) == -1) {
+      if (dWrite(sock_fd, request, strlen(request)) == -1) {
          MSG("Dpi_get_server_port: %s\n", dStrerror(errno));
       } else {
          ok = 1;
@@ -627,8 +602,8 @@
    /* send authentication Key (the server closes sock_fd on auth error) */
    } else if (!(cmd = a_Dpip_build_cmd("cmd=%s msg=%s", "auth", SharedKey))) {
       MSG_ERR("[Dpi_connect_socket] Can't make auth message.\n");
-   } else if (Dpi_blocking_write(sock_fd, cmd, strlen(cmd)) == -1) {
-      MSG_ERR("[Dpi_connect_socket] Can't send auth message.\n");
+   } else if (dWrite(sock_fd, cmd, strlen(cmd)) == -1) {
+      MSG_ERR("[Dpi_connect_socket] Can't send auth message: %s\n", dStrerror(errno));
    } else {
       ret = sock_fd;
    }
@@ -778,8 +753,8 @@
 
    if ((sock_fd = Dpi_connect_socket(server_name)) == -1) {
       MSG_ERR("[a_Dpi_send_blocking_cmd] Can't connect to server.\n");
-   } else if (Dpi_blocking_write(sock_fd, cmd, strlen(cmd)) == -1) {
-      MSG_ERR("[a_Dpi_send_blocking_cmd] Can't send message.\n");
+   } else if (dWrite(sock_fd, cmd, strlen(cmd)) == -1) {
+      MSG_ERR("[a_Dpi_send_blocking_cmd] Can't send message: %s\n", dStrerror(errno));
    } if ((ret = Dpi_blocking_read(sock_fd)) == NULL) {
       MSG_ERR("[a_Dpi_send_blocking_cmd] Can't read message.\n");
    }
diff -r dd6f2d1eb15a src/cookies.c
--- a/src/cookies.c
+++ b/src/cookies.c
@@ -84,7 +84,7 @@
       fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
       if (fd != -1) {
          if (init_str) {
-            rc = write(fd, init_str, strlen(init_str));
+            rc = dWrite(fd, init_str, strlen(init_str));
             if (rc == -1) {
                MSG("Cookies: Could not write initial string to file %s: %s\n",
                    filename, dStrerror(errno));
diff -r dd6f2d1eb15a src/dns.c
--- a/src/dns.c
+++ b/src/dns.c
@@ -339,7 +339,7 @@
    dns_server[channel].addr_list = hosts;
    dns_server[channel].state = DNS_SERVER_RESOLVED;
 
-   write(dns_notify_pipe[1], ".", 1);
+   dWrite(dns_notify_pipe[1], ".", 1);
 
    return NULL;                 /* (avoids a compiler warning) */
 }
</pre></body></html>