From: Chandrika Srinivasan <chandrika.srinivasan@citrix.com>
Date: Tue, 10 Oct 2017 17:37:07 +0100
CA-268288: Send logpath as an additional write

For tapdisk commands that include an additional logpath(for CBT),
send information as an additional write and not as part of the protocol
message structure

Signed-off-by: Chandrika Srinivasan <chandrika.srinivasan@citrix.com>
Reviewed-by: Mark Syms <mark.syms@citrix.com>

diff --git a/control/tap-ctl-ipc.c b/control/tap-ctl-ipc.c
index b1815b3..e3b35fc 100644
--- a/control/tap-ctl-ipc.c
+++ b/control/tap-ctl-ipc.c
@@ -159,6 +159,40 @@ tap_ctl_send_and_receive(int sfd, tapdisk_message_t *message,
 	return 0;
 }
 
+int
+tap_ctl_send_and_receive_with_logpath(int sfd, tapdisk_message_t *message,
+			 const char *logpath, struct timeval *timeout)
+{
+	int err, ret;
+
+	err = tap_ctl_write_message(sfd, message, timeout);
+	if (err) {
+		EPRINTF("failed to send '%s' message\n",
+			tapdisk_message_name(message->type));
+		return err;
+	}
+
+	char buf[TAPDISK_MESSAGE_MAX_PATH_LENGTH];
+
+	snprintf(buf, TAPDISK_MESSAGE_MAX_PATH_LENGTH - 1, "%s", logpath);  
+
+	ret = write(sfd, &buf, sizeof(buf));
+
+	if (ret == -1) {
+		EPRINTF("Failed to send logpath with '%s' message\n",
+			tapdisk_message_name(message->type));
+	}	
+
+	err = tap_ctl_read_message(sfd, message, timeout);
+	if (err) {
+		EPRINTF("failed to receive '%s' message\n",
+			tapdisk_message_name(message->type));
+		return err;
+	}
+
+	return 0;
+}
+
 char *
 tap_ctl_socket_name(int id)
 {
@@ -252,3 +286,19 @@ tap_ctl_connect_send_and_receive(int id, tapdisk_message_t *message,
 	close(sfd);
 	return err;
 }
+
+int
+tap_ctl_connect_send_receive_with_logpath(int id, tapdisk_message_t *message,
+				 const char *logpath, struct timeval *timeout)
+{
+	int err, sfd;
+
+	err = tap_ctl_connect_id(id, &sfd);
+	if (err)
+		return err;
+
+	err = tap_ctl_send_and_receive_with_logpath(sfd, message, logpath, timeout);
+
+	close(sfd);
+	return err;
+}
diff --git a/control/tap-ctl-open.c b/control/tap-ctl-open.c
index 305c731..5374833 100644
--- a/control/tap-ctl-open.c
+++ b/control/tap-ctl-open.c
@@ -72,18 +72,14 @@ tap_ctl_open(const int id, const int minor, const char *params, int flags,
 			return ENAMETOOLONG;
 		}
 	}
-
 	if (logpath) {
-		err = snprintf(message.u.params.logpath,
-			       sizeof(message.u.params.logpath) - 1, "%s",
-			       logpath);
-		if (err >= sizeof(message.u.params.logpath)) {
-			EPRINTF("logpath too long\n");
-			return ENAMETOOLONG;
-		}
+		err = tap_ctl_connect_send_receive_with_logpath
+								(id, &message, logpath, NULL);
+	}
+	else {
+		err = tap_ctl_connect_send_and_receive(id, &message, NULL);
 	}
 
-	err = tap_ctl_connect_send_and_receive(id, &message, NULL);
 	if (err)
 		return err;
 
diff --git a/control/tap-ctl-unpause.c b/control/tap-ctl-unpause.c
index f551c05..8a66b60 100644
--- a/control/tap-ctl-unpause.c
+++ b/control/tap-ctl-unpause.c
@@ -65,17 +65,14 @@ tap_ctl_unpause(const int id, const int minor, const char *params, int flags,
 			return -ENAMETOOLONG;
 		}
 	}
-    if (logpath) {
-        err = snprintf(message.u.params.logpath,
-                   sizeof(message.u.params.logpath) - 1, "%s",
-                   logpath);
-        if (err >= sizeof(message.u.params.logpath)) {
-            EPRINTF("logpath too long\n");
-            return ENAMETOOLONG;
-        }
-    }
+	if (logpath) {
+		err = tap_ctl_connect_send_receive_with_logpath
+									(id, &message, logpath, NULL);
+	}
+	else {
+		err = tap_ctl_connect_send_and_receive(id, &message, NULL);
+	}
 
-	err = tap_ctl_connect_send_and_receive(id, &message, NULL);
 	if (err)
 		return err;
 
diff --git a/drivers/tapdisk-control.c b/drivers/tapdisk-control.c
index e0f3144..f99d1fd 100644
--- a/drivers/tapdisk-control.c
+++ b/drivers/tapdisk-control.c
@@ -702,7 +702,7 @@ static int
 tapdisk_control_open_image(struct tapdisk_ctl_conn *conn,
 			   tapdisk_message_t *request, tapdisk_message_t * const response)
 {
-	int err;
+	int err, ret;
 	td_vbd_t *vbd;
 	td_flag_t flags;
 
@@ -738,9 +738,10 @@ tapdisk_control_open_image(struct tapdisk_ctl_conn *conn,
 	if (request->u.params.flags & TAPDISK_MESSAGE_FLAG_VHD_INDEX)
 		flags |= TD_OPEN_VHD_INDEX;
 	if (request->u.params.flags & TAPDISK_MESSAGE_FLAG_ADD_LOG) {
-		char *logpath = strdup(request->u.params.logpath);
-		if (!logpath) {
-			err = -errno;
+		char *logpath = malloc(TAPDISK_MESSAGE_MAX_PATH_LENGTH);
+		ret = read(conn->fd, logpath, TAPDISK_MESSAGE_MAX_PATH_LENGTH);
+		if (ret < 0) {
+			err = -EIO;
 			goto out;
 		}
 		vbd->logpath = logpath;
@@ -980,7 +981,7 @@ static int
 tapdisk_control_resume_vbd(struct tapdisk_ctl_conn *conn,
 			   tapdisk_message_t *request, tapdisk_message_t * const response)
 {
-	int err;
+	int err, ret;
 	td_vbd_t *vbd;
 	const char *desc = NULL;
 
@@ -1014,9 +1015,10 @@ tapdisk_control_resume_vbd(struct tapdisk_ctl_conn *conn,
 	}
 
 	if (request->u.params.flags & TAPDISK_MESSAGE_FLAG_ADD_LOG) {
-		char *logpath = strdup(request->u.params.logpath);
-		if (!logpath) {
-			err = -errno;
+		char *logpath = malloc(TAPDISK_MESSAGE_MAX_PATH_LENGTH);
+		ret = read(conn->fd, logpath, TAPDISK_MESSAGE_MAX_PATH_LENGTH);
+		if (ret < 0) {
+			err = -EIO;
 			goto out;
 		}
 		vbd->logpath = logpath;
diff --git a/include/tap-ctl.h b/include/tap-ctl.h
index c8b0c11..fec1b10 100644
--- a/include/tap-ctl.h
+++ b/include/tap-ctl.h
@@ -77,6 +77,10 @@ int tap_ctl_send_and_receive(int fd, tapdisk_message_t *message,
 int tap_ctl_connect_send_and_receive(int id,
 				     tapdisk_message_t *message,
 				     struct timeval *timeout);
+int tap_ctl_connect_send_receive_with_logpath(int id,
+				     tapdisk_message_t *message,
+				     const char *logpath,
+				     struct timeval *timeout);
 char *tap_ctl_socket_name(int id);
 
 typedef struct {
diff --git a/include/tapdisk-message.h b/include/tapdisk-message.h
index e409b81..e9504b0 100644
--- a/include/tapdisk-message.h
+++ b/include/tapdisk-message.h
@@ -75,7 +75,6 @@ struct tapdisk_message_params {
 	uint32_t                         prt_devnum;
 	uint16_t                         req_timeout;
 	char                             secondary[TAPDISK_MESSAGE_MAX_PATH_LENGTH];
-	char                             logpath[TAPDISK_MESSAGE_MAX_PATH_LENGTH];
 };
 
 struct tapdisk_message_image {
