From 849a85b250e23148b27b1652a733129a2f1f8e7d Mon Sep 17 00:00:00 2001 From: C Anthony Risinger Date: Thu, 17 Sep 2015 03:47:45 -0500 Subject: [PATCH] Introduce uwsgi.binary_argc If more than uwsgi.binary_path is needed to make uWSGI self-referential on reload, plugins can use this field to skip processing of N initial arguments. eg. if uWSGI is embedded in a host application or interpreter:: python -B -E -m uwsgi --socket=:8000 --show-config [...] argv[1:5] are intended for Python, and not for processing by uWSGI, even though uWSGI might want to manage it (process name). The arguments also cannot just be ignored because they are crucial for proper reloads. --- core/init.c | 25 +++++++++++++++++++++++++ uwsgi.h | 3 +++ 2 files changed, 28 insertions(+) diff --git a/core/init.c b/core/init.c index 927c3dd1..ce888b01 100644 --- a/core/init.c +++ b/core/init.c @@ -62,6 +62,7 @@ void uwsgi_init_default() { uwsgi.cpus = 1; uwsgi.new_argc = -1; + uwsgi.binary_argc = 1; uwsgi.backtrace_depth = 64; uwsgi.max_apps = 64; @@ -261,11 +262,33 @@ void uwsgi_commandline_config() { int argc = uwsgi.argc; char **argv = uwsgi.argv; + // we might want to ignore some arguments not meant for us + char binary_argv0_pretty[256] = {'\0'}; + char *binary_argv0_actual = NULL; + if (uwsgi.new_argc > -1 && uwsgi.new_argv) { argc = uwsgi.new_argc; argv = uwsgi.new_argv; } + if (uwsgi.binary_argc > 1 && argc >= uwsgi.binary_argc) { + char *pretty = (char *)binary_argv0_pretty; + strncat(pretty, argv[0], 255); + + for (i = 1; i < uwsgi.binary_argc; i++) { + if (strlen(pretty) + 1 + strlen(argv[i]) + 1 > 256) + break; + + strcat(pretty, " "); + strcat(pretty, argv[i]); + } + + argc -= uwsgi.binary_argc - 1; + argv += uwsgi.binary_argc - 1; + + binary_argv0_actual = argv[0]; + argv[0] = (char *)binary_argv0_pretty; + } char *optname; while ((i = getopt_long(argc, argv, uwsgi.short_options, uwsgi.long_options, &uwsgi.option_index)) != -1) { @@ -289,6 +312,8 @@ void uwsgi_commandline_config() { add_exported_option(optname, optarg, 0); } + if (binary_argv0_actual != NULL) + argv[0] = binary_argv0_actual; #ifdef UWSGI_DEBUG uwsgi_log("optind:%d argc:%d\n", optind, uwsgi.argc); diff --git a/uwsgi.h b/uwsgi.h index 583b42c4..77c3434d 100755 --- a/uwsgi.h +++ b/uwsgi.h @@ -2811,6 +2811,9 @@ struct uwsgi_server { int spooler_signal_as_task; int log_worker; + + // number of args to consider part of binary_path + int binary_argc; }; struct uwsgi_rpc {