From 3a5d147ac290526a22dcb2730b293092ac0851a2 Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Tue, 9 Jan 2018 11:20:02 +0100 Subject: [PATCH] Refactor control messages * remove flags (present implicitly, unused explicitly) * add request result Change-Id: Ib751ddf85e9aec9286dda78cf27cc2aad8fcb8d3 Signed-off-by: Michal Bloch --- include/logcommon.h | 4 +++- include/logpipe.h | 19 +++++++++---------- src/logger/logger.c | 4 ++-- src/logutil/logutil.c | 41 +++++++++++----------------------------- src/shared/logcommon.c | 51 ++++++++++++++++++++++++++++++++++++-------------- 5 files changed, 62 insertions(+), 57 deletions(-) diff --git a/include/logcommon.h b/include/logcommon.h index fa3e707..9e01e4a 100644 --- a/include/logcommon.h +++ b/include/logcommon.h @@ -58,7 +58,9 @@ log_id_t log_id_by_name(const char *name); log_priority log_priority_from_char(char c); void syslog_critical_failure(const char * message); -int send_pipe(int sockfd, int pipefd, int type); +int send_dlog_request(int sockfd, int request, void *data, int datalen); +int send_dlog_reply(int sockfd, int request, int result, void *data, int datalen); +int send_pipe(int sockfd, int pipefd); int recv_pipe(int sockfd); int connect_sock(const char *path); diff --git a/include/logpipe.h b/include/logpipe.h index 4063353..ea68e83 100644 --- a/include/logpipe.h +++ b/include/logpipe.h @@ -29,23 +29,22 @@ struct dlog_pipe_header { #define DLOG_CTRL_REQ_PIPE {sizeof(struct dlog_control_msg), DLOG_REQ_PIPE, 0} enum { - DLOG_REQ_PIPE = 1, - DLOG_REQ_CLEAR, - DLOG_REQ_HANDLE_LOGUTIL, - DLOG_REQ_MAX + DLOG_REQ_REPLY = 1 << 0, + DLOG_REQ_PIPE = 1 << 1, + DLOG_REQ_CLEAR = 1 << 2, + DLOG_REQ_HANDLE_LOGUTIL = 1 << 3, }; enum { - DLOG_FLAG_ANSW = 0x01, - DLOG_FLAG_WRITE = 0x02, - DLOG_FLAG_READ = 0x04 + DLOG_REQ_RESULT_OK = 0, + DLOG_REQ_RESULT_ERR, }; struct dlog_control_msg { unsigned char length; - signed char request; /* signed - declared explicitly to suppress static analyzer SVACE warning: */ - /* "In some build environments it [char] can be interpreted as 'unsigned char' [...]" */ - char flags; + signed char request; /* signed - declared explicitly to suppress static analyzer SVACE warning: */ + /* "In some build environments it [char] can be interpreted as 'unsigned char' [...]" */ + char result; char data[0]; }; diff --git a/src/logger/logger.c b/src/logger/logger.c index 7677fb4..a8ef62e 100755 --- a/src/logger/logger.c +++ b/src/logger/logger.c @@ -1299,7 +1299,7 @@ static int parse_command_line(const char *cmdl, struct writer *wr, struct logger set_write_fd_entity(&reader->fd_entity, write_fd); assert(wr); - retval = send_pipe(wr->fd_entity.fd, read_fd, DLOG_FLAG_READ); + retval = send_pipe(wr->fd_entity.fd, read_fd); close(read_fd); if (retval) goto cleanup; @@ -1477,7 +1477,7 @@ static int service_writer_handle_req_pipe(struct logger* server, struct writer* if (r < 0) goto err_close; - r = send_pipe(wr->fd_entity.fd, pipe_fd[1], DLOG_FLAG_WRITE); + r = send_pipe(wr->fd_entity.fd, pipe_fd[1]); if (r < 0) goto err_remove; close(pipe_fd[1]); diff --git a/src/logutil/logutil.c b/src/logutil/logutil.c index 7c57459..5fad7b5 100755 --- a/src/logutil/logutil.c +++ b/src/logutil/logutil.c @@ -290,17 +290,11 @@ static int do_clear_pipe(int sock_fd) { assert(sock_fd >= 0); - struct dlog_control_msg msg = { - .length = sizeof(msg), - .request = DLOG_REQ_CLEAR, - .flags = 0, - }; - - if (write(sock_fd, &msg, msg.length) < 0) { - printf("Error: could not send a CLEAR request to logger; socket write failed\n"); - return 0; - } - return 1; + int r = send_dlog_request(sock_fd, DLOG_REQ_CLEAR, NULL, 0); + if (r < 0) + printf("Error: could not send a CLEAR request to logger %s\n", strerror(-r)); + + return r; } /** * @brief Get buffer filled size @@ -367,21 +361,11 @@ static int do_getsize(struct log_config *conf, int enabled_buffers) * @param[in] argc Argument count * @param[in] argv Argument values * @param[in] sock_fd Socket file descriptor - * @return 1 on success, 0 on failure + * @return 0 on success, -errno on failure */ static int send_logger_request(int argc, char **argv, int sock_fd) { - 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"; - - 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); for (int i = 1; i < argc; i++) { @@ -395,14 +379,11 @@ static int send_logger_request(int argc, char **argv, int sock_fd) len += needed; } - ctrl.length += len; - iov[1].iov_len = len; - - if (writev(sock_fd, iov, NELEMS(iov)) < 0) { + int r = send_dlog_request(sock_fd, DLOG_REQ_HANDLE_LOGUTIL, request_string, len); + if (r < 0) printf("Error: could not send a logger request; socket write failed\n"); - return 0; - } - return 1; + + return r; } /** @@ -776,7 +757,7 @@ struct fd_info *process_buffer_pipe(const char *buffer_name, int clear, struct l argv_processed[argc_processed++] = argv[i]; } - if (!send_logger_request(argc_processed, argv_processed, sock_fd)) { + if (send_logger_request(argc_processed, argv_processed, sock_fd) < 0) { printf("Error: could not send request to logger daemon\n"); goto cleanup; } diff --git a/src/shared/logcommon.c b/src/shared/logcommon.c index 4659f1c..ab91b27 100644 --- a/src/shared/logcommon.c +++ b/src/shared/logcommon.c @@ -96,35 +96,34 @@ static void init_pipe_msg_header(struct pipe_msg_header *msg, struct dlog_contro } /** - * @brief Initialize message data for sending pipe fd - * @param[in] data dlog control message to be send with pipe file descriptor - * @param[in] type The type of the pipe (DLOG_FLAG_READ or DLOG_FLAG_WRITE) - * @note type of the pipe (DLOG_FLAG_READ or DLOG_FLAG_WRITE) is ignored on receiver side + * @brief Initialize message data for sending a control message + * @param[in] data dlog control message to be filled + * @param[in] request The request type + * @param[in] result The result of the request */ -static void init_pipe_msg_data(struct dlog_control_msg *data, int type) +static void init_pipe_msg_data(struct dlog_control_msg *data, int request, int result) { assert(data); *data = (struct dlog_control_msg) { .length = sizeof(*data), - .request = DLOG_REQ_PIPE, - .flags = DLOG_FLAG_ANSW | type + .request = request, + .result = result, }; } /** * @brief verify dlog control message data on receiver side * @param[in] data dlog control message received - * @return non zero if possitively verified - * @note type of the pipe (DLOG_FLAG_READ or DLOG_FLAG_WRITE) ignored + * @return non zero if positively verified */ static int verify_pipe_msg_data(struct dlog_control_msg *data) { assert(data); return data->length == sizeof(*data) - && data->request == DLOG_REQ_PIPE - && data->flags|DLOG_FLAG_ANSW; + && data->request == (DLOG_REQ_PIPE | DLOG_REQ_REPLY) + && data->result == DLOG_REQ_RESULT_OK; } /** @@ -211,19 +210,43 @@ void syslog_critical_failure(const char * message) closelog(); } +static int __send_dlog_msg(int sockfd, int result, int request, void *data, int datalen) +{ + struct dlog_control_msg msg; + + init_pipe_msg_data(&msg, request, result); + msg.length += datalen; + + struct iovec iov[2] = { + { .iov_base = &msg, .iov_len = sizeof(msg) }, + { .iov_base = data, .iov_len = datalen }, + }; + + return (writev(sockfd, iov, data ? 2 : 1) < 0) ? -errno : 0; +} + +int send_dlog_request(int sockfd, int request, void *data, int datalen) +{ + return __send_dlog_msg(sockfd, 0, request, data, datalen); +} + +int send_dlog_reply(int sockfd, int request, int result, void *data, int datalen) +{ + return __send_dlog_msg(sockfd, result, DLOG_REQ_REPLY | request, NULL, 0); +} + /** * @brief Send a pipe over socket * @details Sends a pipe over socket * @param[in] sockfd The FD of the socket * @param[in] pipefd The pipe to send over the socket - * @param[in] type The type of the pipe (DLOG_FLAG_READ or DLOG_FLAG_WRITE) * @return 0 on success, else -errno */ -int send_pipe(int sockfd, int pipefd, int type) +int send_pipe(int sockfd, int pipefd) { struct pipe_msg_header msg; struct dlog_control_msg data; - init_pipe_msg_data(&data, type); + init_pipe_msg_data(&data, DLOG_REQ_REPLY | DLOG_REQ_PIPE, DLOG_REQ_RESULT_OK); init_pipe_msg_header(&msg, &data); init_pipe_msg_control(&msg.hdr, pipefd); -- 2.7.4