From 5c63107bde768595f4f2e196492d7cae8aab1815 Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Thu, 20 Apr 2023 20:33:55 +0200 Subject: [PATCH] libdlogutil: pipe requests aliased buffers Change-Id: I74dd76cf99c40355676a36dd7a5bea7e2c4a67af Signed-off-by: Michal Bloch --- src/libdlogutil/fd_info.h | 2 +- src/libdlogutil/fdi_logger.c | 5 +++-- src/libdlogutil/fdi_pipe.c | 12 ++++++++++-- src/libdlogutil/fdi_pipe_internal.h | 1 + src/libdlogutil/fdi_zero_copy.c | 3 ++- src/libdlogutil/logretrieve.c | 4 +--- src/tests/fdi_pipe_neg.c | 22 +++++++++++++--------- src/tests/fdi_pipe_pos.c | 21 +++++++++++++++------ 8 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/libdlogutil/fd_info.h b/src/libdlogutil/fd_info.h index 279ffff..3f285df 100644 --- a/src/libdlogutil/fd_info.h +++ b/src/libdlogutil/fd_info.h @@ -43,7 +43,7 @@ struct fd_info { struct fd_ops { /// Lifetime management: constructor and destructor - int (*create)(struct fd_info *fdi, const struct log_config *conf, log_id_t id, list_head *used_paths, log_id_t *aliased); + int (*create)(struct fd_info *fdi, const struct log_config *conf, log_id_t id, list_head *used_paths, log_id_t aliased[static LOG_ID_MAX]); void (*destroy)(struct fd_info *fdi); /// Read an entry into an internal buffer; requires `prepare_print` to have been called diff --git a/src/libdlogutil/fdi_logger.c b/src/libdlogutil/fdi_logger.c index 64b11c9..647197f 100644 --- a/src/libdlogutil/fdi_logger.c +++ b/src/libdlogutil/fdi_logger.c @@ -98,7 +98,7 @@ static log_id_t path_already_opened(char *path, list_head used_paths) * @param[in] conf The configuration * @return 0 on success, -errno on failure */ -static int logger_create(struct fd_info *fdi, const struct log_config *conf, log_id_t id, list_head *used_paths, log_id_t *aliased) +static int logger_create(struct fd_info *fdi, const struct log_config *conf, log_id_t id, list_head *used_paths, log_id_t aliased[static LOG_ID_MAX]) { assert(fdi); assert(conf); @@ -124,7 +124,7 @@ static int logger_create(struct fd_info *fdi, const struct log_config *conf, log log_id_t already = path_already_opened(actual_path, *used_paths); if (already != LOG_ID_INVALID) { - *aliased = already; + aliased[id] = already; ret = -EALREADY; goto free_fdi; } @@ -143,6 +143,7 @@ static int logger_create(struct fd_info *fdi, const struct log_config *conf, log goto free_fdi; } + aliased[id] = id; lpd->entry = NULL; lpd->monitor = false; fdi->priv_data = lpd; diff --git a/src/libdlogutil/fdi_pipe.c b/src/libdlogutil/fdi_pipe.c index 0b4a552..1e6a2b4 100644 --- a/src/libdlogutil/fdi_pipe.c +++ b/src/libdlogutil/fdi_pipe.c @@ -255,7 +255,7 @@ static int send_logger_request(struct log_filter *filters, int dump, bool monito * @param[in] conf The configuration * @return 0 on success, -errno on failure */ -static int pipe_create(struct fd_info *fdi, const struct log_config *conf, log_id_t id, list_head *used_paths, log_id_t *aliased) +static int pipe_create(struct fd_info *fdi, const struct log_config *conf, log_id_t id, list_head *used_paths, log_id_t aliased[static LOG_ID_MAX]) { assert(fdi); assert(conf); @@ -277,11 +277,19 @@ static int pipe_create(struct fd_info *fdi, const struct log_config *conf, log_i close(sock_fd); return -ENOMEM; } + + aliased[id] = id; + + ppd->enabled_buffers = 0; ppd->data_len = 0; ppd->sock_fd = sock_fd; ppd->offset = 0; ppd->eof = false; + for (int i = 0; i < LOG_ID_MAX; ++i) + if (aliased[i] == id) + bit_set(&ppd->enabled_buffers, i); + fdi->priv_data = ppd; return 0; } @@ -328,7 +336,7 @@ static int pipe_prepare_print(struct fd_info *fdi, int dump, bool monitor, struc monitor = false; } - int r = send_logger_request(filter_object, dump, monitor, ppd->sock_fd, compress, 1 << fdi->id); + int r = send_logger_request(filter_object, dump, monitor, ppd->sock_fd, compress, ppd->enabled_buffers); if (r < 0) return r; diff --git a/src/libdlogutil/fdi_pipe_internal.h b/src/libdlogutil/fdi_pipe_internal.h index f85edbf..4a98e4e 100644 --- a/src/libdlogutil/fdi_pipe_internal.h +++ b/src/libdlogutil/fdi_pipe_internal.h @@ -5,6 +5,7 @@ struct pipe_priv_data { size_t data_len; size_t offset; + int enabled_buffers; char buff[RECEIVE_BUFFER_SIZE]; int sock_fd; bool eof; diff --git a/src/libdlogutil/fdi_zero_copy.c b/src/libdlogutil/fdi_zero_copy.c index 62a22e7..db82ddf 100644 --- a/src/libdlogutil/fdi_zero_copy.c +++ b/src/libdlogutil/fdi_zero_copy.c @@ -67,7 +67,7 @@ static int zero_copy_clear(struct fd_info *fdi) return r; } -static int zero_copy_create(struct fd_info *fdi, const struct log_config *conf, log_id_t id, list_head *used_paths, log_id_t *aliased) +static int zero_copy_create(struct fd_info *fdi, const struct log_config *conf, log_id_t id, list_head *used_paths, log_id_t aliased[static LOG_ID_MAX]) { // We can't clear, so the flag is R not RW fdi->fd = open(ZERO_COPY_DUMP_DEVICE_NAME, O_RDONLY | O_CLOEXEC); @@ -81,6 +81,7 @@ static int zero_copy_create(struct fd_info *fdi, const struct log_config *conf, return -ENOMEM; } + aliased[id] = id; zpd->this_read_ts = 0; zpd->prev_read_ts = 0; zpd->items = NULL; diff --git a/src/libdlogutil/logretrieve.c b/src/libdlogutil/logretrieve.c index fdabbe4..f19eaaf 100644 --- a/src/libdlogutil/logretrieve.c +++ b/src/libdlogutil/logretrieve.c @@ -89,9 +89,7 @@ int create_initial_fdis(struct fd_info ***fdis, int enabled_buffers, backend_t b if (!fdi) return -ENOMEM; - int r = fdi->ops->create(fdi, conf, i, &used_paths, &aliased[i]); - if (r == 0) - aliased[i] = i; + int r = fdi->ops->create(fdi, conf, i, &used_paths, aliased); if (r < 0) { // TODO: Inform about that again somewhere else (except if r == -EALREADY and aliased[i] != INVALID) fdi_free(fdi); diff --git a/src/tests/fdi_pipe_neg.c b/src/tests/fdi_pipe_neg.c index 8d7422a..50d9cc5 100644 --- a/src/tests/fdi_pipe_neg.c +++ b/src/tests/fdi_pipe_neg.c @@ -20,26 +20,30 @@ int main(void) { struct fd_info info = { .ops = &ops_pipe, - .id = 123, + .id = LOG_ID_RADIO, }; struct log_config conf = {}; list_head *fake_list = (list_head *)54321; // It's not used anyway :) - assert(ops_pipe.create(&info, &conf, LOG_ID_RADIO, fake_list, &(log_id_t){ LOG_ID_INVALID }) == -ENOENT); + log_id_t aliases[LOG_ID_MAX]; + for (int i = 0; i < LOG_ID_MAX; ++i) + aliases[i] = LOG_ID_INVALID; + aliases[LOG_ID_RADIO] = LOG_ID_RADIO; + assert(ops_pipe.create(&info, &conf, LOG_ID_RADIO, fake_list, aliases) == -ENOENT); log_config_set(&conf, "radio_ctl_sock", "the_wrong_path"); - assert(ops_pipe.create(&info, &conf, LOG_ID_RADIO, fake_list, &(log_id_t){ LOG_ID_INVALID }) == -ECONNREFUSED); + assert(ops_pipe.create(&info, &conf, LOG_ID_RADIO, fake_list, aliases) == -ECONNREFUSED); assert(last_sock == 0); log_config_set(&conf, "radio_ctl_sock", the_right_path); malloc_fail = true; - assert(ops_pipe.create(&info, &conf, LOG_ID_RADIO, fake_list, &(log_id_t){ LOG_ID_INVALID }) == -ENOMEM); + assert(ops_pipe.create(&info, &conf, LOG_ID_RADIO, fake_list, aliases) == -ENOMEM); assert(last_sock == 1 && closed[0]); malloc_fail = false; - assert(ops_pipe.create(&info, &conf, LOG_ID_RADIO, fake_list, &(log_id_t){ LOG_ID_INVALID }) == 0); + assert(ops_pipe.create(&info, &conf, LOG_ID_RADIO, fake_list, aliases) == 0); struct log_filter *filter = log_filter_new(); assert(filter); @@ -47,23 +51,23 @@ int main(void) correct_sockfd = 2; correct_request = DLOG_REQ_HANDLE_LOGUTIL; - correct_send_data = "dlogutil filter0:V *:S"; + correct_send_data = "dlogutil -b radio filter0:V *:S"; correct_send_datalen = strlen(correct_send_data) + 1; send_return = -1; assert(ops_pipe.prepare_print(&info, false, false, filter, NULL) == -1); - correct_send_data = "dlogutil -d filter0:V *:S"; + correct_send_data = "dlogutil -d -b radio filter0:V *:S"; correct_send_datalen = strlen(correct_send_data) + 1; assert(ops_pipe.prepare_print(&info, true, false, filter, NULL) == -1); log_filter_set_filterspec(filter, "filter1"); - correct_send_data = "dlogutil filter1:V filter0:V *:S"; + correct_send_data = "dlogutil -b radio filter1:V filter0:V *:S"; correct_send_datalen = strlen(correct_send_data) + 1; assert(ops_pipe.prepare_print(&info, false, false, filter, NULL) == -1); log_filter_set_tid(filter, 123); log_filter_set_pid(filter, 456); - correct_send_data = "dlogutil --pid 456 --tid 123 filter1:V filter0:V *:S"; + correct_send_data = "dlogutil -b radio --pid 456 --tid 123 filter1:V filter0:V *:S"; correct_send_datalen = strlen(correct_send_data) + 1; assert(ops_pipe.prepare_print(&info, false, false, filter, NULL) == -1); diff --git a/src/tests/fdi_pipe_pos.c b/src/tests/fdi_pipe_pos.c index b4ca172..d5c3817 100644 --- a/src/tests/fdi_pipe_pos.c +++ b/src/tests/fdi_pipe_pos.c @@ -46,7 +46,11 @@ void big_filter_test() log_config_set(&conf, "kmsg_ctl_sock", the_right_path); malloc_fail = false; - assert(ops_pipe.create(&info, &conf, LOG_ID_KMSG, fake_list, &(log_id_t){ LOG_ID_INVALID }) == 0); + log_id_t aliases[LOG_ID_MAX]; + for (int i = 0; i < NELEMS(aliases); ++i) + aliases[i] = LOG_ID_INVALID; + aliases[LOG_ID_KMSG] = LOG_ID_KMSG; + assert(ops_pipe.create(&info, &conf, LOG_ID_KMSG, fake_list, aliases) == 0); struct log_filter *filter = log_filter_new(); assert(filter); @@ -57,9 +61,9 @@ void big_filter_test() char just_big_enough_filter[MAX_LOGGER_REQUEST_LEN]; char very_big_string_for_comparison[MAX_LOGGER_REQUEST_LEN]; - int no_of_repetitions = (MAX_LOGGER_REQUEST_LEN - sizeof "dlogutil *:E plus some breathing room") / (sizeof (filter_test_word) - 1); + int no_of_repetitions = (MAX_LOGGER_REQUEST_LEN - sizeof "dlogutil -b kmsg *:E plus some breathing room") / (sizeof (filter_test_word) - 1); - const char beginning[] = "dlogutil "; + const char beginning[] = "dlogutil -b kmsg "; memcpy(very_big_string_for_comparison, beginning, sizeof(beginning) - 1); size_t filter_len = 0; size_t compar_len = sizeof(beginning) - 1; @@ -116,10 +120,15 @@ int main(void) struct log_config conf = {}; list_head *fake_list = (list_head *)54321; // It's not used anyway :) - log_config_set(&conf, "kmsg_ctl_sock", the_right_path); + log_config_set(&conf, "radio_ctl_sock", the_right_path); malloc_fail = false; - assert(ops_pipe.create(&info, &conf, LOG_ID_KMSG, fake_list, &(log_id_t){ LOG_ID_INVALID }) == 0); + log_id_t aliases[LOG_ID_MAX]; + for (int i = 0; i < NELEMS(aliases); ++i) + aliases[i] = LOG_ID_INVALID; + aliases[LOG_ID_RADIO] = LOG_ID_RADIO; + aliases[LOG_ID_KMSG] = LOG_ID_RADIO; + assert(ops_pipe.create(&info, &conf, LOG_ID_RADIO, fake_list, aliases) == 0); assert(last_sock == 1 && !closed[0]); struct pipe_priv_data *ppd = info.priv_data; @@ -142,7 +151,7 @@ int main(void) send_return = 0; recv_pipe_fail = false; - correct_send_data = "dlogutil -b radio --pid 456 --tid 123 filter1:V filter0:V *:S"; + correct_send_data = "dlogutil -b radio -b kmsg --pid 456 --tid 123 filter1:V filter0:V *:S"; correct_send_datalen = strlen(correct_send_data) + 1; assert(ops_pipe.prepare_print(&info, false, false, filter, NULL) == 0); assert(info.fd == 2); -- 2.7.4