public inbox for lvm2-cvs@sourceware.org
help / color / mirror / Atom feed
* LVM2/daemons common/daemon-client.c common/dae ...
@ 2011-06-27 13:58 mornfall
  0 siblings, 0 replies; 3+ messages in thread
From: mornfall @ 2011-06-27 13:58 UTC (permalink / raw)
  To: lvm-devel, lvm2-cvs

CVSROOT:	/cvs/lvm2
Module name:	LVM2
Changes by:	mornfall@sourceware.org	2011-06-27 13:58:11

Modified files:
	daemons/common : daemon-client.c daemon-client.h daemon-server.c 
	                 daemon-server.h 
	daemons/lvmetad: lvmetad-core.c testclient.c 

Log message:
	Also parse the config_tree on the client end (daemon-client.c).

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-client.c.diff?cvsroot=lvm2&r1=1.3&r2=1.4
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-client.h.diff?cvsroot=lvm2&r1=1.4&r2=1.5
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-server.c.diff?cvsroot=lvm2&r1=1.5&r2=1.6
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-server.h.diff?cvsroot=lvm2&r1=1.6&r2=1.7
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/lvmetad/lvmetad-core.c.diff?cvsroot=lvm2&r1=1.3&r2=1.4
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/lvmetad/testclient.c.diff?cvsroot=lvm2&r1=1.4&r2=1.5

--- LVM2/daemons/common/daemon-client.c	2011/06/27 13:46:45	1.3
+++ LVM2/daemons/common/daemon-client.c	2011/06/27 13:58:11	1.4
@@ -44,13 +44,18 @@
 	write_buffer(h.socket_fd, rq.buffer, strlen(rq.buffer));
 
 	if (read_buffer(h.socket_fd, &reply.buffer)) {
-		/* TODO: parse reply.buffer into reply.cft */
+		reply.cft = create_config_tree_from_string(reply.buffer);
 	} else
 		reply.error = 1;
 
 	return reply;
 }
 
+void daemon_reply_destroy(daemon_reply r) {
+	if (r.cft)
+		destroy_config_tree(r.cft);
+}
+
 daemon_reply daemon_send_simple(daemon_handle h, char *id, ...)
 {
 	va_list ap;
--- LVM2/daemons/common/daemon-client.h	2011/06/27 12:26:54	1.4
+++ LVM2/daemons/common/daemon-client.h	2011/06/27 13:58:11	1.5
@@ -41,13 +41,13 @@
 	 *        knobs = [ "twiddle", "tweak" ]
 	 *    }
 	 */
-	struct config_node *cft;
+	struct config_tree *cft;
 } daemon_request;
 
 typedef struct {
 	int error; /* 0 for success */
 	char *buffer; /* textual reply */
-	struct config_node *cft; /* parsed reply, if available */
+	struct config_tree *cft; /* parsed reply, if available */
 } daemon_reply;
 
 /*
@@ -79,6 +79,8 @@
  */
 daemon_reply daemon_send_simple(daemon_handle h, char *id, ...);
 
+void daemon_reply_destroy(daemon_reply r);
+
 /* Shut down the communication to the daemon. Compulsory. */
 void daemon_close(daemon_handle h);
 
--- LVM2/daemons/common/daemon-server.c	2011/06/27 13:46:45	1.5
+++ LVM2/daemons/common/daemon-server.c	2011/06/27 13:58:11	1.6
@@ -25,6 +25,7 @@
 
 #include <syslog.h>
 #include "daemon-server.h"
+#include "daemon-shared.h"
 #include "libdevmapper.h"
 
 #if 0
@@ -200,6 +201,18 @@
 	setsid();
 }
 
+response daemon_reply_simple(char *id, ...)
+{
+	va_list ap;
+	va_start(ap, id);
+	response res = { .buffer = format_buffer(id, ap), .cft = NULL };
+
+	if (!res.buffer)
+		res.error = ENOMEM;
+
+	return res;
+}
+
 struct thread_baton {
 	daemon_state s;
 	client_handle client;
--- LVM2/daemons/common/daemon-server.h	2011/06/27 13:46:45	1.6
+++ LVM2/daemons/common/daemon-server.h	2011/06/27 13:58:11	1.7
@@ -39,6 +39,12 @@
 struct daemon_state;
 
 /*
+ * Craft a simple reply, without the need to construct a config_tree. See
+ * daemon_send_simple in daemon-client.h for the description of the parameters.
+ */
+response daemon_reply_simple(char *id, ...);
+
+/*
  * The callback. Called once per request issued, in the respective client's
  * thread. It is presented by a parsed request (in the form of a config tree).
  * The output is a new config tree that is serialised and sent back to the
--- LVM2/daemons/lvmetad/lvmetad-core.c	2011/06/27 13:46:45	1.3
+++ LVM2/daemons/lvmetad/lvmetad-core.c	2011/06/27 13:58:11	1.4
@@ -6,11 +6,9 @@
 
 static response handler(daemon_state s, client_handle h, request r)
 {
-	response res;
-	fprintf(stderr, "[D] REQUEST: %s\n", find_config_str(r.cft->root, "request", "NONE"));
-	res.error = 1;
-	res.buffer = strdup("hey hey.\n\n");
-	return res;
+	fprintf(stderr, "[D] REQUEST: %s, param = %d\n", find_config_str(r.cft->root, "request", "NONE"),
+		                                         find_config_int(r.cft->root, "param", -1));
+	return daemon_reply_simple("hey there", "param = %d", 42, NULL);
 }
 
 static int setup_post(daemon_state *s)
--- LVM2/daemons/lvmetad/testclient.c	2011/06/27 13:46:45	1.4
+++ LVM2/daemons/lvmetad/testclient.c	2011/06/27 13:58:11	1.5
@@ -5,7 +5,9 @@
 	int i;
 	for (i = 0; i < 5; ++i ) {
 		daemon_reply reply = daemon_send_simple(h, "hello world", "param = %d", 3, NULL);
-		fprintf(stderr, "[C] obtained: %s\n", reply.buffer);
+		fprintf(stderr, "[C] REPLY: %s, param = %d\n", find_config_str(reply.cft->root, "request", "NONE"),
+			                                       find_config_int(reply.cft->root, "param", -1));
+		daemon_reply_destroy(reply);
 	}
 	daemon_close(h);
 	return 0;


^ permalink raw reply	[flat|nested] 3+ messages in thread
* LVM2/daemons common/daemon-client.c common/dae ...
@ 2012-02-23 23:52 mornfall
  0 siblings, 0 replies; 3+ messages in thread
From: mornfall @ 2012-02-23 23:52 UTC (permalink / raw)
  To: lvm-devel, lvm2-cvs

CVSROOT:	/cvs/lvm2
Module name:	LVM2
Changes by:	mornfall@sourceware.org	2012-02-23 23:52:12

Modified files:
	daemons/common : daemon-client.c daemon-client.h daemon-server.c 
	                 daemon-server.h 
	daemons/lvmetad: lvmetad-client.h lvmetad-core.c 

Log message:
	Couple of improvements in the daemon (common + lvmetad) code:
	- some client-side memory leak fixes
	- announce and check protocols and protocol versions

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-client.c.diff?cvsroot=lvm2&r1=1.12&r2=1.13
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-client.h.diff?cvsroot=lvm2&r1=1.9&r2=1.10
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-server.c.diff?cvsroot=lvm2&r1=1.16&r2=1.17
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-server.h.diff?cvsroot=lvm2&r1=1.12&r2=1.13
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/lvmetad/lvmetad-client.h.diff?cvsroot=lvm2&r1=1.8&r2=1.9
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/lvmetad/lvmetad-core.c.diff?cvsroot=lvm2&r1=1.39&r2=1.40

--- LVM2/daemons/common/daemon-client.c	2012/02/15 09:14:54	1.12
+++ LVM2/daemons/common/daemon-client.c	2012/02/23 23:52:11	1.13
@@ -9,7 +9,7 @@
 #include <errno.h> // ENOMEM
 
 daemon_handle daemon_open(daemon_info i) {
-	daemon_handle h = { .protocol = 0 };
+	daemon_handle h = { .protocol_version = 0 };
 	struct sockaddr_un sockaddr;
 
 	if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0) {
@@ -24,10 +24,28 @@
 		perror("connect");
 		goto error;
 	}
+
+	daemon_reply r = daemon_send_simple(h, "hello", NULL);
+	if (r.error || strcmp(daemon_reply_str(r, "response", "unknown"), "OK"))
+		goto error;
+
+	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)))
+		goto error;
+	if (i.protocol_version && h.protocol_version != i.protocol_version)
+		goto error;
+
+	daemon_reply_destroy(r);
 	return h;
 error:
 	if (h.socket_fd >= 0)
 		close(h.socket_fd);
+	if (r.cft)
+		daemon_reply_destroy(r);
 	h.socket_fd = -1;
 	return h;
 }
@@ -43,6 +61,7 @@
 
 	assert(rq.buffer);
 	write_buffer(h.socket_fd, rq.buffer, strlen(rq.buffer));
+	dm_free(rq.buffer);
 
 	if (read_buffer(h.socket_fd, &reply.buffer)) {
 		reply.cft = dm_config_from_string(reply.buffer);
@@ -55,6 +74,7 @@
 void daemon_reply_destroy(daemon_reply r) {
 	if (r.cft)
 		dm_config_destroy(r.cft);
+	dm_free(r.buffer);
 }
 
 daemon_reply daemon_send_simple(daemon_handle h, const char *id, ...)
@@ -72,10 +92,10 @@
 		return err;
 
 	repl = daemon_send(h, rq);
-	dm_free(rq.buffer);
 	return repl;
 }
 
 void daemon_close(daemon_handle h)
 {
+	dm_free((char *)h.protocol);
 }
--- LVM2/daemons/common/daemon-client.h	2012/01/15 15:16:50	1.9
+++ LVM2/daemons/common/daemon-client.h	2012/02/23 23:52:11	1.10
@@ -19,13 +19,21 @@
 
 typedef struct {
 	int socket_fd; /* the fd we use to talk to the daemon */
-	int protocol;  /* version of the protocol the daemon uses */
+	const char *protocol;
+	int protocol_version;  /* version of the protocol the daemon uses */
 } daemon_handle;
 
 typedef struct {
 	const char *path; /* the binary of the daemon */
 	const char *socket; /* path to the comms socket */
 	unsigned autostart:1; /* start the daemon if not running? */
+
+	/*
+	 * If the following are not NULL/0, an attempt to talk to a daemon which
+	 * uses a different protocol or version will fail.
+	 */
+	const char *protocol;
+	int protocol_version;
 } daemon_info;
 
 typedef struct {
--- LVM2/daemons/common/daemon-server.c	2012/01/25 21:30:27	1.16
+++ LVM2/daemons/common/daemon-server.c	2012/02/23 23:52:11	1.17
@@ -239,6 +239,19 @@
 	return 0;
 }
 
+static response builtin_handler(daemon_state s, client_handle h, request r)
+{
+	const char *rq = daemon_request_str(r, "request", "NONE");
+
+	if (!strcmp(rq, "hello")) {
+		return daemon_reply_simple("OK", "protocol = %s", s.protocol ?: "default",
+					   "version = %d", s.protocol_version, NULL);
+	}
+
+	response res = { .buffer = NULL, .error = EPROTO };
+	return res;
+}
+
 static void *client_thread(void *baton)
 {
 	struct thread_baton *b = baton;
@@ -252,7 +265,11 @@
 		req.cft = dm_config_from_string(req.buffer);
 		if (!req.cft)
 			fprintf(stderr, "error parsing request:\n %s\n", req.buffer);
-		res = b->s.handler(b->s, b->client, req);
+
+		res = builtin_handler(b->s, b->client, req);
+
+		if (res.error == EPROTO) /* Not a builtin, delegate to the custom handler. */
+			res = b->s.handler(b->s, b->client, req);
 
 		if (!res.buffer) {
 			dm_config_write_node(res.cft->root, buffer_line, &res);
--- LVM2/daemons/common/daemon-server.h	2011/09/01 13:25:50	1.12
+++ LVM2/daemons/common/daemon-server.h	2012/02/23 23:52:11	1.13
@@ -78,6 +78,9 @@
 	const char *name;
 	const char *pidfile;
 	const char *socket_path;
+	const char *protocol;
+	int protocol_version;
+
 	int log_level;
 	handle_request handler;
 	int (*daemon_init)(struct daemon_state *st);
--- LVM2/daemons/lvmetad/lvmetad-client.h	2012/02/23 17:59:32	1.8
+++ LVM2/daemons/lvmetad/lvmetad-client.h	2012/02/23 23:52:11	1.9
@@ -64,6 +64,8 @@
 	daemon_info lvmetad_info = {
 		.path = "lvmetad",
 		.socket = socket ?: DEFAULT_RUN_DIR "/lvmetad.socket",
+		.protocol = "lvmetad",
+		.protocol_version = 1,
 		.autostart = 0
 	};
 
--- LVM2/daemons/lvmetad/lvmetad-core.c	2012/02/23 17:59:32	1.39
+++ LVM2/daemons/lvmetad/lvmetad-core.c	2012/02/23 23:52:11	1.40
@@ -984,6 +984,8 @@
 		s.socket_path = DEFAULT_RUN_DIR "/lvmetad.socket";
 	s.pidfile = DEFAULT_RUN_DIR "/lvmetad.pid";
         s.log_level = 0;
+	s.protocol = "lvmetad";
+	s.protocol_version = 1;
 
 	// use getopt_long
 	while ((opt = getopt(argc, argv, "?fhVdRs:")) != EOF) {


^ permalink raw reply	[flat|nested] 3+ messages in thread
* LVM2/daemons common/daemon-client.c common/dae ...
@ 2011-06-27 13:46 mornfall
  0 siblings, 0 replies; 3+ messages in thread
From: mornfall @ 2011-06-27 13:46 UTC (permalink / raw)
  To: lvm-devel, lvm2-cvs

CVSROOT:	/cvs/lvm2
Module name:	LVM2
Changes by:	mornfall@sourceware.org	2011-06-27 13:46:45

Modified files:
	daemons/common : daemon-client.c daemon-server.c daemon-server.h 
	daemons/lvmetad: lvmetad-core.c testclient.c 

Log message:
	Parse the incoming config tree in daemon-server.c, providing the
	daemon-specific handler with a struct config_tree pointer to look things up in.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-client.c.diff?cvsroot=lvm2&r1=1.2&r2=1.3
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-server.c.diff?cvsroot=lvm2&r1=1.4&r2=1.5
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/common/daemon-server.h.diff?cvsroot=lvm2&r1=1.5&r2=1.6
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/lvmetad/lvmetad-core.c.diff?cvsroot=lvm2&r1=1.2&r2=1.3
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/daemons/lvmetad/testclient.c.diff?cvsroot=lvm2&r1=1.3&r2=1.4

--- LVM2/daemons/common/daemon-client.c	2011/06/27 13:14:53	1.2
+++ LVM2/daemons/common/daemon-client.c	2011/06/27 13:46:45	1.3
@@ -15,7 +15,7 @@
 		goto error;
 	}
 	memset(&sockaddr, 0, sizeof(sockaddr));
-	fprintf(stderr, "connecting to %s\n", i.socket);
+	fprintf(stderr, "[C] connecting to %s\n", i.socket);
 	strcpy(sockaddr.sun_path, i.socket);
 	sockaddr.sun_family = AF_UNIX;
 	if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr))) {
--- LVM2/daemons/common/daemon-server.c	2011/06/14 02:34:18	1.4
+++ LVM2/daemons/common/daemon-server.c	2011/06/27 13:46:45	1.5
@@ -107,7 +107,7 @@
 		fprintf(stderr, "setting CLOEXEC on socket fd %d failed: %s\n", fd, strerror(errno));
 	fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
 
-	fprintf(stderr, "creating %s\n", s.socket_path);
+	fprintf(stderr, "[D] creating %s\n", s.socket_path);
 	memset(&sockaddr, 0, sizeof(sockaddr));
 	strcpy(sockaddr.sun_path, s.socket_path);
 	sockaddr.sun_family = AF_UNIX;
@@ -213,8 +213,10 @@
 		if (!read_buffer(b->client.socket_fd, &req.buffer))
 			goto fail;
 
-		/* TODO parse the buffer into req.cft */
+		req.cft = create_config_tree_from_string(req.buffer);
 		response res = b->s.handler(b->s, b->client, req);
+		destroy_config_tree(req.cft);
+		dm_free(req.buffer);
 
 		if (!res.buffer) {
 			/* TODO fill in the buffer from res.cft */
@@ -223,7 +225,6 @@
 		write_buffer(b->client.socket_fd, res.buffer, strlen(res.buffer));
 
 		free(res.buffer);
-		free(req.buffer);
 	}
 fail:
 	/* TODO what should we really do here? */
--- LVM2/daemons/common/daemon-server.h	2011/06/14 02:34:18	1.5
+++ LVM2/daemons/common/daemon-server.h	2011/06/27 13:46:45	1.6
@@ -26,13 +26,13 @@
 } client_handle;
 
 typedef struct {
-	struct config_node *cft;
+	struct config_tree *cft;
 	char *buffer;
 } request;
 
 typedef struct {
 	int error;
-	struct config_node *cft;
+	struct config_tree *cft;
 	char *buffer;
 } response;
 
--- LVM2/daemons/lvmetad/lvmetad-core.c	2011/06/27 13:15:49	1.2
+++ LVM2/daemons/lvmetad/lvmetad-core.c	2011/06/27 13:46:45	1.3
@@ -7,7 +7,7 @@
 static response handler(daemon_state s, client_handle h, request r)
 {
 	response res;
-	fprintf(stderr, "---- server obtained:\n%s\n----------------------\n", r.buffer);
+	fprintf(stderr, "[D] REQUEST: %s\n", find_config_str(r.cft->root, "request", "NONE"));
 	res.error = 1;
 	res.buffer = strdup("hey hey.\n\n");
 	return res;
--- LVM2/daemons/lvmetad/testclient.c	2011/06/27 13:15:49	1.3
+++ LVM2/daemons/lvmetad/testclient.c	2011/06/27 13:46:45	1.4
@@ -5,7 +5,7 @@
 	int i;
 	for (i = 0; i < 5; ++i ) {
 		daemon_reply reply = daemon_send_simple(h, "hello world", "param = %d", 3, NULL);
-		fprintf(stderr, "daemon says: %s\n", reply.buffer);
+		fprintf(stderr, "[C] obtained: %s\n", reply.buffer);
 	}
 	daemon_close(h);
 	return 0;


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-02-23 23:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-27 13:58 LVM2/daemons common/daemon-client.c common/dae mornfall
  -- strict thread matches above, loose matches on Subject: below --
2012-02-23 23:52 mornfall
2011-06-27 13:46 mornfall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).