summaryrefslogtreecommitdiffstats
path: root/daemons
diff options
context:
space:
mode:
authorPetr Rockai <prockai@redhat.com>2012-02-26 08:46:28 +0000
committerPetr Rockai <prockai@redhat.com>2012-02-26 08:46:28 +0000
commit98b4241b2f68f2baf0eb922e6b975b484f4a4187 (patch)
tree12fcaef3e1917786e77f8659afb83a908ad835e9 /daemons
parentf0582ee66120bd30e0ac935438860afea08fc25b (diff)
downloadlvm2-98b4241b2f68f2baf0eb922e6b975b484f4a4187.tar.gz
lvm2-98b4241b2f68f2baf0eb922e6b975b484f4a4187.tar.xz
lvm2-98b4241b2f68f2baf0eb922e6b975b484f4a4187.zip
Improve error handling & reporting in common daemon code.
Diffstat (limited to 'daemons')
-rw-r--r--daemons/common/daemon-client.c20
-rw-r--r--daemons/common/daemon-client.h1
-rw-r--r--daemons/common/daemon-shared.c6
3 files changed, 15 insertions, 12 deletions
diff --git a/daemons/common/daemon-client.c b/daemons/common/daemon-client.c
index 164699b8..08821a73 100644
--- a/daemons/common/daemon-client.c
+++ b/daemons/common/daemon-client.c
@@ -9,22 +9,18 @@
#include <errno.h> // ENOMEM
daemon_handle daemon_open(daemon_info i) {
- daemon_handle h = { .protocol_version = 0 };
+ daemon_handle h = { .protocol_version = 0, .error = 0 };
daemon_reply r = { .cft = NULL };
struct sockaddr_un sockaddr;
- if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0) {
- perror("socket");
+ if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0)
goto error;
- }
+
memset(&sockaddr, 0, sizeof(sockaddr));
- fprintf(stderr, "[C] connecting to %s\n", i.socket);
strcpy(sockaddr.sun_path, i.socket);
sockaddr.sun_family = AF_UNIX;
- if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr))) {
- perror("connect");
+ if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr)))
goto error;
- }
r = daemon_send_simple(h, "hello", NULL);
if (r.error || strcmp(daemon_reply_str(r, "response", "unknown"), "OK"))
@@ -42,7 +38,9 @@ daemon_handle daemon_open(daemon_info i) {
daemon_reply_destroy(r);
return h;
+
error:
+ h.error = errno;
if (h.socket_fd >= 0)
close(h.socket_fd);
if (r.cft)
@@ -61,13 +59,15 @@ daemon_reply daemon_send(daemon_handle h, daemon_request rq)
}
assert(rq.buffer);
- write_buffer(h.socket_fd, rq.buffer, strlen(rq.buffer));
+ if (!write_buffer(h.socket_fd, rq.buffer, strlen(rq.buffer)))
+ reply.error = errno;
+
dm_free(rq.buffer);
if (read_buffer(h.socket_fd, &reply.buffer)) {
reply.cft = dm_config_from_string(reply.buffer);
} else
- reply.error = 1;
+ reply.error = errno;
return reply;
}
diff --git a/daemons/common/daemon-client.h b/daemons/common/daemon-client.h
index d4cdacd9..9f99fcfb 100644
--- a/daemons/common/daemon-client.h
+++ b/daemons/common/daemon-client.h
@@ -21,6 +21,7 @@ typedef struct {
int socket_fd; /* the fd we use to talk to the daemon */
const char *protocol;
int protocol_version; /* version of the protocol the daemon uses */
+ int error;
} daemon_handle;
typedef struct {
diff --git a/daemons/common/daemon-shared.c b/daemons/common/daemon-shared.c
index 43efba04..5301d2f9 100644
--- a/daemons/common/daemon-shared.c
+++ b/daemons/common/daemon-shared.c
@@ -24,12 +24,14 @@ int read_buffer(int fd, char **buffer) {
int result = read(fd, (*buffer) + bytes, buffersize - bytes);
if (result > 0)
bytes += result;
- if (result == 0)
+ if (result == 0) {
+ errno = ECONNRESET;
goto fail; /* we should never encounter EOF here */
+ }
if (result < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
goto fail;
- if ((!strncmp((*buffer) + bytes - 4, "\n##\n", 4))) {
+ if (!strncmp((*buffer) + bytes - 4, "\n##\n", 4)) {
*(*buffer + bytes - 4) = 0;
break; /* success, we have the full message now */
}