1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

libdaemon: add logging to daemon_open

Log all conditions encountered in daemon_open().

Only store errno when known to be set.
This commit is contained in:
Alasdair G Kergon 2013-01-04 23:29:59 +00:00
parent 6d760b2c63
commit 981962b339

View File

@ -25,41 +25,61 @@
#include <assert.h>
#include <errno.h> // ENOMEM
daemon_handle daemon_open(daemon_info i) {
daemon_handle daemon_open(daemon_info i)
{
daemon_handle h = { .protocol_version = 0, .error = 0 };
daemon_reply r = { 0 };
struct sockaddr_un sockaddr = { .sun_family = AF_UNIX };
if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0)
goto error;
log_debug("%s: Opening daemon socket to %s for protocol %s version %d.",
i.socket, i.path, i.protocol, i.protocol_version);
if (!dm_strncpy(sockaddr.sun_path, i.socket, sizeof(sockaddr.sun_path))) {
fprintf(stderr, "%s: daemon socket path too long.\n", i.socket);
if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0) {
h.error = errno;
log_sys_error("socket", i.socket);
goto error;
}
if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr)))
if (!dm_strncpy(sockaddr.sun_path, i.socket, sizeof(sockaddr.sun_path))) {
log_error("%s: Daemon socket path too long.", i.socket);
goto error;
}
if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr))) {
h.error = errno;
log_sys_error("connect", i.socket);
goto error;
}
log_debug("Sending daemon %s: hello", i.path);
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")) {
h.error = r.error;
log_error("Daemon %s returned error %d", i.path, r.error);
goto error;
}
/* Check protocol and version matches */
h.protocol = daemon_reply_str(r, "protocol", NULL);
if (h.protocol)
h.protocol = dm_strdup(h.protocol); /* keep around */
h.protocol_version = daemon_reply_int(r, "version", 0);
if (i.protocol && (!h.protocol || strcmp(h.protocol, i.protocol)))
if (i.protocol && (!h.protocol || strcmp(h.protocol, i.protocol))) {
log_error("Daemon %s: requested protocol %s != %s",
i.path, i.protocol, h.protocol ? : "");
goto error;
if (i.protocol_version && h.protocol_version != i.protocol_version)
}
if (i.protocol_version && h.protocol_version != i.protocol_version) {
log_error("Daemon %s: requested protocol version %d != %d",
i.path, i.protocol_version, h.protocol_version);
goto error;
}
daemon_reply_destroy(r);
return h;
error:
h.error = errno;
if (h.socket_fd >= 0)
if (close(h.socket_fd))
log_sys_error("close", "daemon_open");