[Skiboot] [PATCH RFC 2/5] external/opal-prd: Move client socket common code to a function

Neelesh Gupta neelegup at linux.vnet.ibm.com
Wed Jul 29 02:43:07 AEST 2015


This patch moves out the common code to a function send_prd_control()
which can be invoked for sending the control message to the opal-prd
daemon. It avoids the duplication of the code into the individual
functions corresponding to variety of control messages.

Signed-off-by: Neelesh Gupta <neelegup at linux.vnet.ibm.com>
---
 external/opal-prd/opal-prd.c |   85 ++++++++++++++++++++++++++++--------------
 1 file changed, 57 insertions(+), 28 deletions(-)

diff --git a/external/opal-prd/opal-prd.c b/external/opal-prd/opal-prd.c
index 88b2350..585d086 100644
--- a/external/opal-prd/opal-prd.c
+++ b/external/opal-prd/opal-prd.c
@@ -83,6 +83,7 @@ enum control_msg_type {
 	CONTROL_MSG_DISABLE_OCCS	= 0x01,
 	CONTROL_MSG_TEMP_OCC_RESET	= 0x02,
 	CONTROL_MSG_TEMP_OCC_ERROR	= 0x03,
+	CONTROL_MSG_INVALID             = 0xf0,
 };
 
 struct control_msg {
@@ -1022,6 +1023,7 @@ static int handle_prd_control(struct opal_prd_ctx *ctx, int fd)
 	if (rc != sizeof(msg)) {
 		pr_log(LOG_WARNING, "CTRL: failed to receive "
 				"control message: %m");
+		msg.type = CONTROL_MSG_INVALID;
 		rc = -1;
 		goto out_send;
 	}
@@ -1278,27 +1280,12 @@ out_close:
 	return rc;
 }
 
-static int send_occ_control(struct opal_prd_ctx *ctx, const char *str)
+static int send_prd_control(struct control_msg *msg, size_t send_sz,
+			    size_t recv_sz)
 {
 	struct sockaddr_un addr;
-	struct control_msg msg;
 	int sd, rc;
 
-	memset(&msg, 0, sizeof(msg));
-
-	if (!strcmp(str, "enable")) {
-		msg.type = CONTROL_MSG_ENABLE_OCCS;
-	} else if (!strcmp(str, "disable")) {
-		msg.type = CONTROL_MSG_DISABLE_OCCS;
-	} else if (!strcmp(str, "reset")) {
-		msg.type = CONTROL_MSG_TEMP_OCC_RESET;
-	} else if (!strcmp(str, "process-error")) {
-		msg.type = CONTROL_MSG_TEMP_OCC_ERROR;
-	} else {
-		pr_log(LOG_ERR, "OCC: Invalid OCC action '%s'", str);
-		return -1;
-	}
-
 	sd = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (!sd) {
 		pr_log(LOG_ERR, "CTRL: Failed to create control socket: %m");
@@ -1314,37 +1301,79 @@ static int send_occ_control(struct opal_prd_ctx *ctx, const char *str)
 		goto out_close;
 	}
 
-	rc = send(sd, &msg, sizeof(msg), 0);
-	if (rc != sizeof(msg)) {
+	rc = send(sd, msg, send_sz, 0);
+	if (rc != send_sz) {
 		pr_log(LOG_ERR, "CTRL: Failed to send control message: %m");
 		rc = -1;
 		goto out_close;
 	}
 
 	/* wait for our reply */
-	rc = recv(sd, &msg, sizeof(msg), 0);
+	rc = recv(sd, msg, recv_sz, 0);
 	if (rc < 0) {
 		pr_log(LOG_ERR, "CTRL: Failed to receive control message: %m");
 		goto out_close;
 
-	} else if (rc != sizeof(msg)) {
-		pr_log(LOG_WARNING, "CTRL: Short read from control socket");
-		rc = -1;
-		goto out_close;
 	}
 
-	if (msg.response || ctx->debug) {
-		pr_debug("OCC: OCC action %s returned status %ld",
-				str, msg.response);
+	/* Validate the received message size */
+	switch (msg->type) {
+	case CONTROL_MSG_ENABLE_OCCS:
+	case CONTROL_MSG_DISABLE_OCCS:
+	case CONTROL_MSG_TEMP_OCC_RESET:
+	case CONTROL_MSG_TEMP_OCC_ERROR:
+		if (rc != recv_sz) {
+			pr_log(LOG_WARNING, "CTRL: Short read from control "
+					"socket");
+			rc = -1;
+			goto out_close;
+		}
+		break;
+	case CONTROL_MSG_INVALID:
+		pr_log(LOG_WARNING, "CTRL: Invalid message type received");
+		break;
+	default:
+		pr_log(LOG_WARNING, "CTRL: Unrecognised message type: %d",
+		       msg->type);
+		rc = -1;
+		goto out_close;
 	}
 
-	rc = msg.response;
+	rc = msg->response;
 
 out_close:
 	close(sd);
 	return rc;
 }
 
+static int send_occ_control(struct opal_prd_ctx *ctx, const char *str)
+{
+	struct control_msg msg;
+	int rc;
+
+	memset(&msg, 0, sizeof(msg));
+
+	if (!strcmp(str, "enable")) {
+		msg.type = CONTROL_MSG_ENABLE_OCCS;
+	} else if (!strcmp(str, "disable")) {
+		msg.type = CONTROL_MSG_DISABLE_OCCS;
+	} else if (!strcmp(str, "reset")) {
+		msg.type = CONTROL_MSG_TEMP_OCC_RESET;
+	} else if (!strcmp(str, "process-error")) {
+		msg.type = CONTROL_MSG_TEMP_OCC_ERROR;
+	} else {
+		pr_log(LOG_ERR, "OCC: Invalid OCC action '%s'", str);
+		return -1;
+	}
+
+	rc = send_prd_control(&msg, sizeof(msg), sizeof(msg));
+	if (msg.response || ctx->debug)
+		pr_debug("OCC: OCC action %s returned status %ld",
+				str, msg.response);
+
+	return rc;
+}
+
 static void usage(const char *progname)
 {
 	printf("Usage:\n");



More information about the Skiboot mailing list