logutil: pipe: Rework logutil request 61/142861/12
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 16 Aug 2017 10:42:13 +0000 (12:42 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 16 Aug 2017 10:46:18 +0000 (12:46 +0200)
This commit introduces iovec+writev() usage to construct logutil
request instead of malloc.  This allows us to simplify error handling
a bit.

Additionally, fix dlog_control_msg payload length calculation -
it needs to have at most MAX_LOGGER_REQUEST_LEN (including
terminating null byte).

Change-Id: Ib9ab85355a6760a26a9250b20f4ea0dc1b1b2532

src/logger/logger.c
src/logutil/logutil.c

index 39afcd5..27d5842 100644 (file)
@@ -1296,12 +1296,13 @@ static int service_writer_handle_req_util(struct logger* server, struct writer*
                return -EINVAL;
 
        if (msg->length <= sizeof(struct dlog_control_msg) ||
-           msg->length > sizeof(struct dlog_control_msg) + MAX_LOGGER_REQUEST_LEN + 1)
+           msg->length > sizeof(struct dlog_control_msg) + MAX_LOGGER_REQUEST_LEN)
                return -EINVAL;
 
-       msg->data[msg->length - sizeof(struct dlog_control_msg) -1] = 0;
+       if (msg->data[msg->length - sizeof(struct dlog_control_msg)] != 0)
+               return -EINVAL;
 
-       struct reader * rd;
+       struct reader *rd;
        int r = parse_command_line(msg->data, wr, &rd);
        if (r < 0)
                return r;
index 871927f..61a6679 100755 (executable)
@@ -350,32 +350,39 @@ static int do_getsize(struct log_config * conf, char ** names, int cnt)
  * @param[in] sock_fd Socket file descriptor
  * @return 1 on success, 0 on failure
  */
-static int send_logger_request(int argc, char ** argv, int sock_fd)
+static int send_logger_request(int argc, char **argv, int sock_fd)
 {
-       char logger_request[MAX_LOGGER_REQUEST_LEN];
-       int logger_request_len = snprintf(logger_request, MAX_LOGGER_REQUEST_LEN, "dlogutil");
-       struct dlog_control_msg * msg;
-       int i, ret = 1;
+       struct dlog_control_msg ctrl = {
+               .request = DLOG_REQ_HANDLE_LOGUTIL,
+               .flags = 0,
+               .length = sizeof(ctrl) /* needs updating */,
+       };
+       char request_string[MAX_LOGGER_REQUEST_LEN] = "dlogutil";
 
-       for (i = 1; i < argc; ++i)
-               logger_request_len += snprintf(logger_request + logger_request_len, MAX_LOGGER_REQUEST_LEN - logger_request_len, " %s", argv[i]);
+       struct iovec iov[2] = {
+               { .iov_base = &ctrl, .iov_len = sizeof(ctrl) },
+               { .iov_base = &request_string, .iov_len = 0 /* needs updating */ },
+       };
+       int len = strlen(request_string);
 
-       logger_request_len += sizeof(struct dlog_control_msg) + 1;
+       for (int i = 1; i < argc; i++) {
+               int r = snprintf(request_string + len, sizeof(request_string) - len, " %s", argv[i]);
+               if (r > 0)
+                       len += r;
+               else
+                       return 0;
+       }
 
-       msg = calloc(1, logger_request_len);
-       if (!msg)
-               return 0;
-       msg->length = logger_request_len;
-       msg->request = DLOG_REQ_HANDLE_LOGUTIL;
-       msg->flags = 0;
-       memcpy(msg->data, logger_request, logger_request_len - sizeof(struct dlog_control_msg));
-       if (write(sock_fd, msg, logger_request_len) < 0) {
+       len += 1; // for terminating null byte
+
+       ctrl.length += len;
+       iov[1].iov_len = len;
+
+       if (writev(sock_fd, iov, NELEMS(iov)) < 0) {
                printf("Error: could not send a logger request; socket write failed\n");
-               ret = 0;
+               return 0;
        }
-
-       free(msg);
-       return ret;
+       return 1;
 }
 
 /**