Skip to content

Commit

Permalink
Merge pull request #38 from mk-fg/use_argv_array_in_wrapper
Browse files Browse the repository at this point in the history
fix: build log_proxy command in wrapper as an argv array, don't use gshell parser
  • Loading branch information
thebaptiste authored Feb 9, 2024
2 parents 8dbc161 + 395f5a8 commit 0eb49b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/log_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 29 additions & 8 deletions src/log_proxy_wrapper.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gprintf.h>
#include <locale.h>
#include <sys/stat.h>
#include <unistd.h>
Expand Down Expand Up @@ -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[])
Expand Down

0 comments on commit 0eb49b5

Please sign in to comment.