From 921de583f472f5a80398d5e72ca7d21b34377fc9 Mon Sep 17 00:00:00 2001 From: Petr Rockai Date: Sun, 15 Jan 2012 11:17:16 +0000 Subject: [PATCH] Unfortunately, blank lines are sometimes produced by config serializer, and this interferes with their role as message separator in the lvmetad protocol. Switch to using "##" on an otherwise blank line as a separator. --- daemons/common/daemon-shared.c | 27 +++++++++++++++++---------- daemons/common/daemon-shared.h | 2 +- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/daemons/common/daemon-shared.c b/daemons/common/daemon-shared.c index 7cbcf4768..335b64be1 100644 --- a/daemons/common/daemon-shared.c +++ b/daemons/common/daemon-shared.c @@ -38,7 +38,7 @@ int read_buffer(int fd, char **buffer) { *buffer = new; } else { (*buffer)[bytes] = 0; - if ((end = strstr((*buffer) + bytes - 2, "\n\n"))) { + if ((end = strstr((*buffer) + bytes - 4, "\n##\n"))) { *end = 0; break; /* success, we have the full message now */ } @@ -58,18 +58,29 @@ fail: * * TODO use select on EWOULDBLOCK/EAGAIN to avoid useless spinning */ -int write_buffer(int fd, char *buffer, int length) { +int write_buffer(int fd, const char *buffer, int length) { + int done = 0; int written = 0; +write: while (1) { int result = write(fd, buffer + written, length - written); if (result > 0) written += result; if (result < 0 && errno != EWOULDBLOCK && errno != EAGAIN) - break; /* too bad */ - if (written == length) - return 1; /* done */ + return 0; /* too bad */ + if (written == length) { + if (done) + return 1; + else + break; /* done */ + } } - return 0; + const char *terminate = "\n##\n"; + buffer = terminate; + length = 4; + written = 0; + done = 1; + goto write; } char *format_buffer(const char *what, const char *id, va_list ap) @@ -106,10 +117,6 @@ char *format_buffer(const char *what, const char *id, va_list ap) if (!buffer) goto fail; } - old = buffer; - dm_asprintf(&buffer, "%s\n", buffer); - dm_free(old); - return buffer; fail: dm_free(buffer); diff --git a/daemons/common/daemon-shared.h b/daemons/common/daemon-shared.h index 12f42fd47..a15e6fdd3 100644 --- a/daemons/common/daemon-shared.h +++ b/daemons/common/daemon-shared.h @@ -2,5 +2,5 @@ #include int read_buffer(int fd, char **buffer); -int write_buffer(int fd, char *buffer, int length); +int write_buffer(int fd, const char *buffer, int length); char *format_buffer(const char *what, const char *id, va_list ap);