diff --git a/libdaemon/client/daemon-client.c b/libdaemon/client/daemon-client.c index 371dc5c48..a821cf38b 100644 --- a/libdaemon/client/daemon-client.c +++ b/libdaemon/client/daemon-client.c @@ -25,41 +25,61 @@ #include #include // 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");