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;
* @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;
}
/**