aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-echo.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mds-echo.c')
-rw-r--r--src/mds-echo.c77
1 files changed, 36 insertions, 41 deletions
diff --git a/src/mds-echo.c b/src/mds-echo.c
index 31e8b86..ab85c31 100644
--- a/src/mds-echo.c
+++ b/src/mds-echo.c
@@ -99,6 +99,7 @@ int __attribute__((const)) preinitialise_server(void)
*/
int initialise_server(void)
{
+ int stage = 0;
const char* const message =
"Command: intercept\n"
"Message ID: 0\n"
@@ -106,15 +107,15 @@ int initialise_server(void)
"\n"
"Command: echo\n";
- if (full_send(message, strlen(message)))
- return 1;
- fail_if (server_initialised() < 0);
+ fail_if (full_send(message, strlen(message)));
+ fail_if (server_initialised() < 0); stage++;
fail_if (mds_message_initialise(&received));
return 0;
- pfail:
+ fail:
xperror(*argv);
- mds_message_destroy(&received);
+ if (stage == 1)
+ mds_message_destroy(&received);
return 1;
}
@@ -130,13 +131,12 @@ int postinitialise_server(void)
if (connected)
return 0;
- if (reconnect_to_display())
- {
- mds_message_destroy(&received);
- return 1;
- }
+ fail_if (reconnect_to_display());
connected = 1;
return 0;
+ fail:
+ mds_message_destroy(&received);
+ return 1;
}
@@ -184,18 +184,16 @@ int marshal_server(char* state_buf)
*/
int unmarshal_server(char* state_buf)
{
- int r;
/* buf_get_next(state_buf, int, MDS_ECHO_VARS_VERSION); */
buf_next(state_buf, int, 1);
buf_get_next(state_buf, int, connected);
buf_get_next(state_buf, uint32_t, message_id);
- r = mds_message_unmarshal(&received, state_buf);
- if (r)
- {
- xperror(*argv);
- mds_message_destroy(&received);
- }
- return r;
+ fail_if (mds_message_unmarshal(&received, state_buf));
+ return 0;
+ fail:
+ xperror(*argv);
+ mds_message_destroy(&received);
+ return -1;
}
@@ -229,27 +227,26 @@ int master_loop(void)
if (r == -2)
{
eprint("corrupt message received, aborting.");
- goto fail;
+ goto done;
}
else if (errno == EINTR)
continue;
- else if (errno != ECONNRESET)
- goto pfail;
+ else
+ fail_if (errno != ECONNRESET);
eprint("lost connection to server.");
mds_message_destroy(&received);
mds_message_initialise(&received);
connected = 0;
- if (reconnect_to_display())
- goto fail;
+ fail_if (reconnect_to_display());
connected = 1;
}
rc = 0;
- goto fail;
- pfail:
- xperror(*argv);
+ goto done;
fail:
+ xperror(*argv);
+ done:
if (rc || !reexecing)
mds_message_destroy(&received);
free(echo_buffer);
@@ -265,10 +262,12 @@ int master_loop(void)
*/
int echo_message(void)
{
+ char* old_buffer = NULL;
const char* recv_client_id = NULL;
const char* recv_message_id = NULL;
const char* recv_length = NULL;
size_t i, n;
+ int saved_errno;
/* Fetch headers. */
@@ -312,14 +311,7 @@ int echo_message(void)
n += strlen(recv_length) + 1;
if ((echo_buffer_size < n) || (echo_buffer_size * 4 > n))
- {
- char* old_buffer = echo_buffer;
- if (xrealloc(echo_buffer, echo_buffer_size = n, char))
- {
- free(old_buffer);
- return -1;
- }
- }
+ fail_if (xxrealloc(old_buffer, echo_buffer, echo_buffer_size = n, char));
sprintf(echo_buffer, "To: %s\nIn response to: %s\nMessage ID: %" PRIu32 "\n%s%s\n",
recv_client_id, recv_message_id, message_id,
@@ -330,9 +322,12 @@ int echo_message(void)
message_id = message_id == UINT32_MAX ? 0 : (message_id + 1);
/* Send echo. */
- if (full_send(echo_buffer, strlen(echo_buffer)))
- return 1;
+ fail_if (full_send(echo_buffer, strlen(echo_buffer)));
return full_send(received.payload, received.payload_size);
+ fail:
+ saved_errno = errno;
+ free(old_buffer);
+ return errno = saved_errno, -1;
}
@@ -355,14 +350,14 @@ int full_send(const char* message, size_t length)
eprint("Sent more of a message than exists in the message, aborting.");
return -1;
}
- else if ((sent < length) && (errno != EINTR))
- {
- xperror(*argv);
- return -1;
- }
+ else
+ fail_if ((sent < length) && (errno != EINTR));
message += sent;
length -= sent;
}
return 0;
+ fail:
+ xperror(*argv);
+ return -1;
}