From 395f5a836f6d557a861ac14fddb1f2ca81be3831 Mon Sep 17 00:00:00 2001 From: Mike Kazantsev Date: Thu, 8 Feb 2024 23:33:32 +0500 Subject: [PATCH] fix: build log_proxy command in wrapper as an argv array, don't use gshell parser Should address earlier "(null)"-prefix bug from #35 in a more obvious way, and fix #37. --- src/log_proxy.c | 1 - src/log_proxy_wrapper.c | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/log_proxy.c b/src/log_proxy.c index 557e070..e127ab3 100644 --- a/src/log_proxy.c +++ b/src/log_proxy.c @@ -228,7 +228,6 @@ int main(int argc, char *argv[]) signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); set_default_values_from_env(); - if ( strcmp( timestamp_prefix, "(null)" ) == 0 ) timestamp_prefix = NULL; log_file = compute_file_path(log_directory, argv[1]); // Create log directory if not existing gchar *log_dir = g_path_get_dirname(log_file); diff --git a/src/log_proxy_wrapper.c b/src/log_proxy_wrapper.c index 286086c..a43ad24 100644 --- a/src/log_proxy_wrapper.c +++ b/src/log_proxy_wrapper.c @@ -1,6 +1,4 @@ #include -#include -#include #include #include #include @@ -60,17 +58,40 @@ gchar *make_fifo(const gchar *label) { } void spawn_logproxy_async(const gchar *fifo_path, const gchar *log_path) { - gchar *use_locks_str = ""; + gchar *rotation_size_str = g_strdup_printf("%li", rotation_size); + gchar *rotation_time_str = g_strdup_printf("%li", rotation_time); + gchar *rotated_files_str = g_strdup_printf("%i", rotated_files); + gchar *argv[20] = { + "log_proxy", + "-s", rotation_size_str, + "-t", rotation_time_str, + "-S", rotation_suffix, + "-d", log_directory, + "-n", rotated_files_str, + "-r", "-f", (gchar*) fifo_path + }; + int argc = 14; // initial number of fixed arguments above + if (use_locks) { - use_locks_str = "--use-locks"; + argv[argc++] = "--use-locks"; + } + if (timestamp_prefix != NULL) { + argv[argc++] = "-T"; + argv[argc++] = timestamp_prefix; } - gchar *cli = g_strdup_printf("log_proxy -s %li -t %li -S \"%s\" -d \"%s\" -T \"%s\" -n %i %s -r -f \"%s\" \"%s\"", rotation_size, rotation_time, rotation_suffix, log_directory, timestamp_prefix, rotated_files, use_locks_str, fifo_path, log_path); - gboolean spawn_res = g_spawn_command_line_async(cli, NULL); + argv[argc++] = (gchar*) log_path; + argv[argc] = NULL; + + gboolean spawn_res = g_spawn_async( + NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL ); if (spawn_res == FALSE) { - g_critical("can't spawn %s => exit", cli); + g_critical("can't spawn [ %s ] => exit", g_strjoinv(" ", argv)); exit(1); } - g_free(cli); + + g_free(rotation_size_str); + g_free(rotation_time_str); + g_free(rotated_files_str); } int main(int argc, char *argv[])