~ubuntu-branches/ubuntu/vivid/lvm2/vivid

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Bastian Blank
  • Date: 2012-05-01 20:27:50 UTC
  • mto: (3.1.23 sid)
  • mto: This revision was merged to the branch mainline in revision 72.
  • Revision ID: package-import@ubuntu.com-20120501202750-gljjjtblowwq9mw8
Tags: upstream-2.02.95
ImportĀ upstreamĀ versionĀ 2.02.95

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011-2012 Red Hat, Inc.
 
3
 *
 
4
 * This file is part of LVM2.
 
5
 *
 
6
 * This copyrighted material is made available to anyone wishing to use,
 
7
 * modify, copy, or redistribute it subject to the terms and conditions
 
8
 * of the GNU Lesser General Public License v.2.1.
 
9
 *
 
10
 * You should have received a copy of the GNU Lesser General Public License
 
11
 * along with this program; if not, write to the Free Software Foundation,
 
12
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
13
 */
 
14
 
 
15
#include "daemon-shared.h"
 
16
#include "daemon-client.h"
 
17
 
 
18
#include <sys/un.h>
 
19
#include <sys/socket.h>
 
20
#include <string.h>
 
21
#include <stdio.h>
 
22
#include <unistd.h>
 
23
#include <assert.h>
 
24
#include <errno.h> // ENOMEM
 
25
 
 
26
daemon_handle daemon_open(daemon_info i) {
 
27
        daemon_handle h = { .protocol_version = 0, .error = 0 };
 
28
        daemon_reply r = { .cft = NULL };
 
29
        struct sockaddr_un sockaddr;
 
30
 
 
31
        if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0)
 
32
                goto error;
 
33
 
 
34
        memset(&sockaddr, 0, sizeof(sockaddr));
 
35
        strcpy(sockaddr.sun_path, i.socket);
 
36
        sockaddr.sun_family = AF_UNIX;
 
37
        if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr)))
 
38
                goto error;
 
39
 
 
40
        r = daemon_send_simple(h, "hello", NULL);
 
41
        if (r.error || strcmp(daemon_reply_str(r, "response", "unknown"), "OK"))
 
42
                goto error;
 
43
 
 
44
        h.protocol = daemon_reply_str(r, "protocol", NULL);
 
45
        if (h.protocol)
 
46
                h.protocol = dm_strdup(h.protocol); /* keep around */
 
47
        h.protocol_version = daemon_reply_int(r, "version", 0);
 
48
 
 
49
        if (i.protocol && (!h.protocol || strcmp(h.protocol, i.protocol)))
 
50
                goto error;
 
51
        if (i.protocol_version && h.protocol_version != i.protocol_version)
 
52
                goto error;
 
53
 
 
54
        daemon_reply_destroy(r);
 
55
        return h;
 
56
 
 
57
error:
 
58
        h.error = errno;
 
59
        if (h.socket_fd >= 0)
 
60
                if (close(h.socket_fd))
 
61
                        perror("close");
 
62
        if (r.cft)
 
63
                daemon_reply_destroy(r);
 
64
        h.socket_fd = -1;
 
65
        return h;
 
66
}
 
67
 
 
68
daemon_reply daemon_send(daemon_handle h, daemon_request rq)
 
69
{
 
70
        daemon_reply reply = { .cft = NULL, .error = 0 };
 
71
        assert(h.socket_fd >= 0);
 
72
 
 
73
        if (!rq.buffer) {
 
74
                /* TODO: build the buffer from rq.cft */
 
75
        }
 
76
 
 
77
        assert(rq.buffer);
 
78
        if (!write_buffer(h.socket_fd, rq.buffer, strlen(rq.buffer)))
 
79
                reply.error = errno;
 
80
 
 
81
        if (read_buffer(h.socket_fd, &reply.buffer)) {
 
82
                reply.cft = dm_config_from_string(reply.buffer);
 
83
        } else
 
84
                reply.error = errno;
 
85
 
 
86
        return reply;
 
87
}
 
88
 
 
89
void daemon_reply_destroy(daemon_reply r) {
 
90
        if (r.cft)
 
91
                dm_config_destroy(r.cft);
 
92
        dm_free(r.buffer);
 
93
}
 
94
 
 
95
daemon_reply daemon_send_simple(daemon_handle h, const char *id, ...)
 
96
{
 
97
        static const daemon_reply err = { .error = ENOMEM, .buffer = NULL, .cft = NULL };
 
98
        daemon_request rq = { .cft = NULL };
 
99
        daemon_reply repl;
 
100
        va_list ap;
 
101
 
 
102
        va_start(ap, id);
 
103
        rq.buffer = format_buffer("request", id, ap);
 
104
        va_end(ap);
 
105
 
 
106
        if (!rq.buffer)
 
107
                return err;
 
108
 
 
109
        repl = daemon_send(h, rq);
 
110
        dm_free(rq.buffer);
 
111
 
 
112
        return repl;
 
113
}
 
114
 
 
115
void daemon_close(daemon_handle h)
 
116
{
 
117
        dm_free((char *)h.protocol);
 
118
}