Fix the val reading for MSG_ERROR_EXIT. Use 0-length MSG_DATA when
[rsync/rsync.git] / io.c
diff --git a/io.c b/io.c
index 2c162a6..11015f5 100644 (file)
--- a/io.c
+++ b/io.c
@@ -38,7 +38,6 @@ extern int bwlimit;
 extern size_t bwlimit_writemax;
 extern int io_timeout;
 extern int am_server;
-extern int am_daemon;
 extern int am_sender;
 extern int am_generator;
 extern int msgs2stderr;
@@ -168,10 +167,10 @@ static void check_timeout(void)
        t = time(NULL);
 
        if (t - last_io_in >= io_timeout) {
-               if (!am_server && !am_daemon) {
-                       rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
-                               (int)(t-last_io_in));
-               }
+               if (am_server)
+                       msgs2stderr = 1;
+               rprintf(FERROR, "[%s] io timeout after %d seconds -- exiting\n",
+                       who_am_i(), (int)(t-last_io_in));
                exit_cleanup(RERR_TIMEOUT);
        }
 }
@@ -776,7 +775,7 @@ static char *perform_io(size_t needed, int flags)
                        iobuf.in.len += n;
                }
 
-               if (iobuf.out_fd >= 0 && FD_ISSET(iobuf.out_fd, &w_fds)) {
+               if (out && FD_ISSET(iobuf.out_fd, &w_fds)) {
                        size_t len = iobuf.raw_flushing_ends_before ? iobuf.raw_flushing_ends_before - out->pos : out->len;
                        int n;
 
@@ -897,7 +896,7 @@ void noop_io_until_death(void)
                read_buf(iobuf.in_fd, buf, sizeof buf);
 }
 
-/* Buffer a message for the multiplexed output stream.  Is never used for MSG_DATA. */
+/* Buffer a message for the multiplexed output stream.  Is not used for (normal) MSG_DATA. */
 int send_msg(enum msgcode code, const char *buf, size_t len, int convert)
 {
        char *hdr;
@@ -959,8 +958,8 @@ int send_msg(enum msgcode code, const char *buf, size_t len, int convert)
        {
                size_t siz;
 
-               if ((pos += 4) >= iobuf.msg.size)
-                       pos -= iobuf.msg.size;
+               if ((pos += 4) == iobuf.msg.size)
+                       pos = 0;
 
                /* Handle a split copy if we wrap around the end of the circular buffer. */
                if (pos >= iobuf.msg.pos && (siz = iobuf.msg.size - pos) < len) {
@@ -1314,18 +1313,16 @@ void maybe_flush_socket(int important)
                io_flush(NORMAL_FLUSH);
 }
 
+/* This never adds new non-msg-buffer data, since we don't know the state
+ * of the raw-data buffer. */
 void maybe_send_keepalive(void)
 {
        if (time(NULL) - last_io_out >= allowed_lull) {
                if (!iobuf.msg.len && iobuf.out.len == iobuf.out_empty_len) {
-                       if (protocol_version < 29)
-                               return; /* there's nothing we can do */
                        if (protocol_version >= 30)
                                send_msg(MSG_NOOP, "", 0, 0);
-                       else {
-                               write_int(iobuf.out_fd, cur_flist->used);
-                               write_shortint(iobuf.out_fd, ITEM_IS_NEW);
-                       }
+                       else
+                               send_msg(MSG_DATA, "", 0, 0);
                }
                if (iobuf.msg.len)
                        perform_io(iobuf.msg.size - iobuf.msg.len + 1, PIO_NEED_MSGROOM);
@@ -1374,7 +1371,8 @@ static void read_a_msg(void)
                 * possible that this points off the end of the buffer, in
                 * which case the gradual reading of the input stream will
                 * cause this value to decrease and eventually become real. */
-               iobuf.raw_input_ends_before = iobuf.in.pos + msg_bytes;
+               if (msg_bytes)
+                       iobuf.raw_input_ends_before = iobuf.in.pos + msg_bytes;
                iobuf.in_multiplexed = 1;
                break;
        case MSG_STATS:
@@ -1411,9 +1409,11 @@ static void read_a_msg(void)
                }
                break;
        case MSG_NOOP:
+               if (msg_bytes != 0)
+                       goto invalid_msg;
+               iobuf.in_multiplexed = 1;
                if (am_sender)
                        maybe_send_keepalive();
-               iobuf.in_multiplexed = 1;
                break;
        case MSG_DELETED:
                if (msg_bytes >= sizeof data)
@@ -1534,7 +1534,7 @@ static void read_a_msg(void)
                        goto invalid_msg;
                iobuf.in_multiplexed = 1;
                if (DEBUG_GTE(EXIT, 3))
-                       rprintf(FINFO, "[%s] got MSG_ERROR_EXIT with %d bytes\n", who_am_i(), msg_bytes);
+                       rprintf(FINFO, "[%s] got MSG_ERROR_EXIT with %ld bytes\n", who_am_i(), (long)msg_bytes);
                if (msg_bytes == 0) {
                        if (!am_sender && !am_generator) {
                                if (DEBUG_GTE(EXIT, 3)) {
@@ -1544,23 +1544,19 @@ static void read_a_msg(void)
                                send_msg(MSG_ERROR_EXIT, "", 0, 0);
                                io_flush(FULL_FLUSH);
                        }
-                       val = 0;
-               } else {
-                       val = raw_read_int();
-                       if (protocol_version >= 31) {
-                               if (am_generator) {
-                                       if (DEBUG_GTE(EXIT, 3)) {
-                                               rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n",
-                                                       who_am_i(), val);
-                                       }
-                                       send_msg_int(MSG_ERROR_EXIT, val);
-                               } else {
-                                       if (DEBUG_GTE(EXIT, 3)) {
-                                               rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT (len 0)\n",
-                                                       who_am_i());
-                                       }
-                                       send_msg(MSG_ERROR_EXIT, "", 0, 0);
+               } else if (protocol_version >= 31) {
+                       if (am_generator) {
+                               if (DEBUG_GTE(EXIT, 3)) {
+                                       rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n",
+                                               who_am_i(), val);
                                }
+                               send_msg_int(MSG_ERROR_EXIT, val);
+                       } else {
+                               if (DEBUG_GTE(EXIT, 3)) {
+                                       rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT (len 0)\n",
+                                               who_am_i());
+                               }
+                               send_msg(MSG_ERROR_EXIT, "", 0, 0);
                        }
                }
                /* Send a negative linenum so that we don't end up