~registry/lvm2/master

« back to all changes in this revision

Viewing changes to libdaemon/client/daemon-client.c

  • Committer: Alasdair G Kergon
  • Date: 2013-01-04 23:29:59 UTC
  • Revision ID: git-v1:981962b339123338335da5a3cda822117808c591
libdaemon: add logging to daemon_open

Log all conditions encountered in daemon_open().

Only store errno when known to be set.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <assert.h>
26
26
#include <errno.h> // ENOMEM
27
27
 
28
 
daemon_handle daemon_open(daemon_info i) {
 
28
daemon_handle daemon_open(daemon_info i)
 
29
{
29
30
        daemon_handle h = { .protocol_version = 0, .error = 0 };
30
31
        daemon_reply r = { 0 };
31
32
        struct sockaddr_un sockaddr = { .sun_family = AF_UNIX };
32
33
 
33
 
        if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0)
 
34
        log_debug("%s: Opening daemon socket to %s for protocol %s version %d.",
 
35
                  i.socket, i.path, i.protocol, i.protocol_version);
 
36
 
 
37
        if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0) {
 
38
                h.error = errno;
 
39
                log_sys_error("socket", i.socket);
34
40
                goto error;
 
41
        }
35
42
 
36
43
        if (!dm_strncpy(sockaddr.sun_path, i.socket, sizeof(sockaddr.sun_path))) {
37
 
                fprintf(stderr, "%s: daemon socket path too long.\n", i.socket);
38
 
                goto error;
39
 
        }
40
 
 
41
 
        if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr)))
42
 
                goto error;
43
 
 
 
44
                log_error("%s: Daemon socket path too long.", i.socket);
 
45
                goto error;
 
46
        }
 
47
 
 
48
        if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr))) {
 
49
                h.error = errno;
 
50
                log_sys_error("connect", i.socket);
 
51
                goto error;
 
52
        }
 
53
 
 
54
        log_debug("Sending daemon %s: hello", i.path);
44
55
        r = daemon_send_simple(h, "hello", NULL);
45
 
        if (r.error || strcmp(daemon_reply_str(r, "response", "unknown"), "OK"))
 
56
        if (r.error || strcmp(daemon_reply_str(r, "response", "unknown"), "OK")) {
 
57
                h.error = r.error;
 
58
                log_error("Daemon %s returned error %d", i.path, r.error);
46
59
                goto error;
 
60
        }
47
61
 
 
62
        /* Check protocol and version matches */
48
63
        h.protocol = daemon_reply_str(r, "protocol", NULL);
49
64
        if (h.protocol)
50
65
                h.protocol = dm_strdup(h.protocol); /* keep around */
51
66
        h.protocol_version = daemon_reply_int(r, "version", 0);
52
67
 
53
 
        if (i.protocol && (!h.protocol || strcmp(h.protocol, i.protocol)))
54
 
                goto error;
55
 
        if (i.protocol_version && h.protocol_version != i.protocol_version)
56
 
                goto error;
 
68
        if (i.protocol && (!h.protocol || strcmp(h.protocol, i.protocol))) {
 
69
                log_error("Daemon %s: requested protocol %s != %s",
 
70
                        i.path, i.protocol, h.protocol ? : "");
 
71
                goto error;
 
72
        }
 
73
        if (i.protocol_version && h.protocol_version != i.protocol_version) {
 
74
                log_error("Daemon %s: requested protocol version %d != %d",
 
75
                          i.path, i.protocol_version, h.protocol_version);
 
76
                goto error;
 
77
        }
57
78
 
58
79
        daemon_reply_destroy(r);
59
80
        return h;
60
81
 
61
82
error:
62
 
        h.error = errno;
63
83
        if (h.socket_fd >= 0)
64
84
                if (close(h.socket_fd))
65
85
                        log_sys_error("close", "daemon_open");