]>
sourceware.org Git - lvm2.git/blob - libdaemon/client/daemon-client.c
2 * Copyright (C) 2011-2012 Red Hat, Inc.
4 * This file is part of LVM2.
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.
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
15 #include "daemon-shared.h"
16 #include "daemon-client.h"
19 #include <sys/socket.h>
24 #include <errno.h> // ENOMEM
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
;
31 if ((h
.socket_fd
= socket(PF_UNIX
, SOCK_STREAM
/* | SOCK_NONBLOCK */, 0)) < 0)
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
)))
40 r
= daemon_send_simple(h
, "hello", NULL
);
41 if (r
.error
|| strcmp(daemon_reply_str(r
, "response", "unknown"), "OK"))
44 h
.protocol
= daemon_reply_str(r
, "protocol", NULL
);
46 h
.protocol
= dm_strdup(h
.protocol
); /* keep around */
47 h
.protocol_version
= daemon_reply_int(r
, "version", 0);
49 if (i
.protocol
&& (!h
.protocol
|| strcmp(h
.protocol
, i
.protocol
)))
51 if (i
.protocol_version
&& h
.protocol_version
!= i
.protocol_version
)
54 daemon_reply_destroy(r
);
62 daemon_reply_destroy(r
);
67 daemon_reply
daemon_send(daemon_handle h
, daemon_request rq
)
69 daemon_reply reply
= { .cft
= NULL
, .error
= 0 };
70 assert(h
.socket_fd
>= 0);
73 /* TODO: build the buffer from rq.cft */
77 if (!write_buffer(h
.socket_fd
, rq
.buffer
, strlen(rq
.buffer
)))
80 if (read_buffer(h
.socket_fd
, &reply
.buffer
)) {
81 reply
.cft
= dm_config_from_string(reply
.buffer
);
88 void daemon_reply_destroy(daemon_reply r
) {
90 dm_config_destroy(r
.cft
);
94 daemon_reply
daemon_send_simple(daemon_handle h
, const char *id
, ...)
96 static const daemon_reply err
= { .error
= ENOMEM
, .buffer
= NULL
, .cft
= NULL
};
97 daemon_request rq
= { .cft
= NULL
};
102 rq
.buffer
= format_buffer("request", id
, ap
);
108 repl
= daemon_send(h
, rq
);
114 void daemon_close(daemon_handle h
)
116 dm_free((char *)h
.protocol
);
This page took 0.039879 seconds and 6 git commands to generate.