1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-11 09:18:25 +03:00

Improve error handling & reporting in common daemon code.

This commit is contained in:
Petr Rockai 2012-02-26 08:46:28 +00:00
parent 7ce18df26b
commit 50730ddd96
3 changed files with 15 additions and 12 deletions

View File

@ -9,22 +9,18 @@
#include <errno.h> // ENOMEM #include <errno.h> // ENOMEM
daemon_handle daemon_open(daemon_info i) { 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 }; daemon_reply r = { .cft = NULL };
struct sockaddr_un sockaddr; struct sockaddr_un sockaddr;
if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0) { if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0)
perror("socket");
goto error; goto error;
}
memset(&sockaddr, 0, sizeof(sockaddr)); memset(&sockaddr, 0, sizeof(sockaddr));
fprintf(stderr, "[C] connecting to %s\n", i.socket);
strcpy(sockaddr.sun_path, i.socket); strcpy(sockaddr.sun_path, i.socket);
sockaddr.sun_family = AF_UNIX; sockaddr.sun_family = AF_UNIX;
if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr))) { if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr)))
perror("connect");
goto error; goto error;
}
r = daemon_send_simple(h, "hello", NULL); r = daemon_send_simple(h, "hello", NULL);
if (r.error || strcmp(daemon_reply_str(r, "response", "unknown"), "OK")) 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); daemon_reply_destroy(r);
return h; return h;
error: error:
h.error = errno;
if (h.socket_fd >= 0) if (h.socket_fd >= 0)
close(h.socket_fd); close(h.socket_fd);
if (r.cft) if (r.cft)
@ -61,13 +59,15 @@ daemon_reply daemon_send(daemon_handle h, daemon_request rq)
} }
assert(rq.buffer); 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); dm_free(rq.buffer);
if (read_buffer(h.socket_fd, &reply.buffer)) { if (read_buffer(h.socket_fd, &reply.buffer)) {
reply.cft = dm_config_from_string(reply.buffer); reply.cft = dm_config_from_string(reply.buffer);
} else } else
reply.error = 1; reply.error = errno;
return reply; return reply;
} }

View File

@ -21,6 +21,7 @@ typedef struct {
int socket_fd; /* the fd we use to talk to the daemon */ int socket_fd; /* the fd we use to talk to the daemon */
const char *protocol; const char *protocol;
int protocol_version; /* version of the protocol the daemon uses */ int protocol_version; /* version of the protocol the daemon uses */
int error;
} daemon_handle; } daemon_handle;
typedef struct { typedef struct {

View File

@ -24,12 +24,14 @@ int read_buffer(int fd, char **buffer) {
int result = read(fd, (*buffer) + bytes, buffersize - bytes); int result = read(fd, (*buffer) + bytes, buffersize - bytes);
if (result > 0) if (result > 0)
bytes += result; bytes += result;
if (result == 0) if (result == 0) {
errno = ECONNRESET;
goto fail; /* we should never encounter EOF here */ goto fail; /* we should never encounter EOF here */
}
if (result < 0 && errno != EAGAIN && errno != EWOULDBLOCK) if (result < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
goto fail; goto fail;
if ((!strncmp((*buffer) + bytes - 4, "\n##\n", 4))) { if (!strncmp((*buffer) + bytes - 4, "\n##\n", 4)) {
*(*buffer + bytes - 4) = 0; *(*buffer + bytes - 4) = 0;
break; /* success, we have the full message now */ break; /* success, we have the full message now */
} }