From 187100f2bf5d139b7146b08f22bc4fe68a920e17 Mon Sep 17 00:00:00 2001 From: "roberto@sirius" Date: Thu, 4 Nov 2010 17:52:38 +0100 Subject: [PATCH] uwsgi.opt dictionary --- ini.c | 1 + ldap.c | 1 + plugins/python/python_plugin.c | 36 ++++++++++++++++++ utils.c | 59 ++++++++++++++++++++++++++++++ uwsgi.c | 67 ++++++++++++++++++---------------- uwsgi.h | 10 +++++ xmlconf.c | 1 + 7 files changed, 144 insertions(+), 31 deletions(-) diff --git a/ini.c b/ini.c index eb0f8ef4..2dd89e66 100644 --- a/ini.c +++ b/ini.c @@ -162,6 +162,7 @@ void uwsgi_ini_config(char *file, struct option *long_options) { if (!strcmp(key, aopt->name)) { if (aopt->flag) { *aopt->flag = aopt->val; + add_exported_option(0, (char *)key); } else { manage_opt(aopt->val, val); diff --git a/ldap.c b/ldap.c index 10ed3676..1be87314 100644 --- a/ldap.c +++ b/ldap.c @@ -325,6 +325,7 @@ void uwsgi_ldap_config(struct option *long_options) { if (!strcmp(uwsgi_attr, aopt->name)) { if (aopt->flag) { *aopt->flag = aopt->val; + add_exported_option(0, (char*) uwsgi_attr); } else { manage_opt(aopt->val, uwsgi_val); diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 2b0455a8..bf3349db 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -432,6 +432,42 @@ void uwsgi_uwsgi_config(char *module) { exit(1); } + PyObject *py_opt_dict = PyDict_New(); + for(i=0;ikey)) ) { + // if there is already a key, the value must be a list of values + PyObject *py_opt_item = PyDict_GetItemString(py_opt_dict, uwsgi.exported_opts[i]->key); + if (PyList_Check(py_opt_item)) { + PyList_Append(py_opt_item, PyString_FromString( uwsgi.exported_opts[i]->value )); + } + else { + PyObject *py_opt_list = PyList_New(0); + PyList_Append(py_opt_list, py_opt_item); + if (uwsgi.exported_opts[i]->value == NULL) { + PyList_Append(py_opt_list, Py_True); + } + else { + PyList_Append(py_opt_list, PyString_FromString(uwsgi.exported_opts[i]->value)); + } + + PyDict_SetItemString(py_opt_dict, uwsgi.exported_opts[i]->key, py_opt_list); + } + } + else { + if (uwsgi.exported_opts[i]->value == NULL) { + PyDict_SetItemString(py_opt_dict, uwsgi.exported_opts[i]->key, Py_True); + } + else { + PyDict_SetItemString(py_opt_dict, uwsgi.exported_opts[i]->key, PyString_FromString(uwsgi.exported_opts[i]->value)); + } + } + } + + if (PyDict_SetItemString(up.embedded_dict, "opt", py_opt_dict)) { + PyErr_Print(); + exit(1); + } + #ifdef UNBIT if (PyDict_SetItemString(up.embedded_dict, "unbit", Py_True)) { #else diff --git a/utils.c b/utils.c index fa9cebe2..2176e966 100644 --- a/utils.c +++ b/utils.c @@ -876,3 +876,62 @@ int uwsgi_read_whole_body(struct wsgi_request *wsgi_req, char *buf, size_t len) return 1; } +void add_exported_option(int i, char *value) { + + char *key = NULL; + struct option *lopt, *aopt; + + if (i == 0) { + key = value; + value = NULL; + } + else { + lopt = uwsgi.long_options; + while ((aopt = lopt)) { + if (!aopt->name) + break; + if (aopt->val == 0 && *aopt->flag == i) { + key = (char *) aopt->name; + break; + } + if (aopt->val == i) { + key = (char *) aopt->name; + break; + } + lopt++; + } + } + + + if (!key) return; + + + + if (!uwsgi.exported_opts) { + uwsgi.exported_opts = malloc(sizeof(struct uwsgi_opt*)); + if (!uwsgi.exported_opts) { + uwsgi_error("malloc()"); + exit(1); + } + memset(uwsgi.exported_opts, 0, sizeof(struct uwsgi_opt*)); + } + else { + uwsgi.exported_opts = realloc(uwsgi.exported_opts, sizeof(struct uwsgi_opt*) * (uwsgi.exported_opts_cnt+1)); + if (!uwsgi.exported_opts) { + uwsgi_error("realloc()"); + exit(1); + } + memset(uwsgi.exported_opts + (sizeof(struct uwsgi_opt*) * uwsgi.exported_opts_cnt) , 0, sizeof(struct uwsgi_opt*)); + } + + + uwsgi.exported_opts[uwsgi.exported_opts_cnt] = malloc(sizeof(struct uwsgi_opt)); + if (!uwsgi.exported_opts[uwsgi.exported_opts_cnt]) { + uwsgi_error("malloc()"); + exit(1); + } + uwsgi.exported_opts[uwsgi.exported_opts_cnt]->key = key; + uwsgi.exported_opts[uwsgi.exported_opts_cnt]->value = value; + uwsgi.exported_opts_cnt++; + +} diff --git a/uwsgi.c b/uwsgi.c index 4e653ac9..f94b2817 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -27,7 +27,6 @@ struct uwsgi_server uwsgi; extern char **environ; -static struct option *long_options = NULL; static char *short_options = NULL; static char *base_short_options = "s:p:t:x:d:l:v:b:mcaCTiMhrR:z:A:Q:L"; @@ -340,8 +339,6 @@ int main(int argc, char *argv[], char *envp[]) char *plugins_requested; - int option_index = 0; - #ifdef UWSGI_HTTP pid_t http_pid; @@ -468,8 +465,13 @@ int main(int argc, char *argv[], char *envp[]) } build_options(); - while ((i = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) { - manage_opt(i, optarg); + while ((i = getopt_long(argc, argv, short_options, uwsgi.long_options, &uwsgi.option_index)) != -1) { + if (i == 0) { + add_exported_option(0, (char *)uwsgi.long_options[uwsgi.option_index].name); + } + else { + manage_opt(i, optarg); + } } @@ -517,22 +519,22 @@ uwsgi.wsgi_config = lazy; #ifdef UWSGI_XML if (uwsgi.xml_config != NULL) { - uwsgi_xml_config(uwsgi.wsgi_req, long_options); + uwsgi_xml_config(uwsgi.wsgi_req, uwsgi.long_options); } #endif #ifdef UWSGI_INI if (uwsgi.ini != NULL) { - uwsgi_ini_config(uwsgi.ini, long_options); + uwsgi_ini_config(uwsgi.ini, uwsgi.long_options); } #endif #ifdef UWSGI_LDAP if (uwsgi.ldap != NULL) { - uwsgi_ldap_config(long_options); + uwsgi_ldap_config(uwsgi.long_options); } #endif //parse environ - parse_sys_envs(environ, long_options); + parse_sys_envs(environ, uwsgi.long_options); //call after_opt hooks @@ -1469,10 +1471,10 @@ end: uwsgi.ldap = optarg; return 1; case LONG_ARGS_LDAP_SCHEMA: - uwsgi_ldap_schema_dump(long_options); + uwsgi_ldap_schema_dump(uwsgi.long_options); return 1; case LONG_ARGS_LDAP_SCHEMA_LDIF: - uwsgi_ldap_schema_dump_ldif(long_options); + uwsgi_ldap_schema_dump_ldif(uwsgi.long_options); return 1; #endif case LONG_ARGS_MODE: @@ -1899,12 +1901,14 @@ end: int j; if (manage_base_opt(i, optarg)) { + add_exported_option( i, optarg ); return; } for (j = 0; j < 0xFF; j++) { if (uwsgi.p[j]->manage_opt) { if (uwsgi.p[j]->manage_opt(i, optarg)) { + add_exported_option( i, optarg ); return; } } @@ -1913,6 +1917,7 @@ end: for (j = 0; j < uwsgi.gp_cnt; j++) { if (uwsgi.gp[j]->manage_opt) { if (uwsgi.gp[j]->manage_opt(i, optarg)) { + add_exported_option( i, optarg ); return; } } @@ -2023,11 +2028,11 @@ void build_options() { } } - if (long_options) { - free(long_options); + if (uwsgi.long_options) { + free(uwsgi.long_options); } - long_options = malloc(sizeof(struct option) * (opt_count + 1)); - if (!long_options) { + uwsgi.long_options = malloc(sizeof(struct option) * (opt_count + 1)); + if (!uwsgi.long_options) { uwsgi_error("malloc()"); exit(1); } @@ -2036,10 +2041,10 @@ void build_options() { while ((aopt = lopt)) { if (!aopt->name) break; - long_options[opt_count].name = aopt->name; - long_options[opt_count].has_arg = aopt->has_arg; - long_options[opt_count].flag = aopt->flag; - long_options[opt_count].val = aopt->val; + uwsgi.long_options[opt_count].name = aopt->name; + uwsgi.long_options[opt_count].has_arg = aopt->has_arg; + uwsgi.long_options[opt_count].flag = aopt->flag; + uwsgi.long_options[opt_count].val = aopt->val; opt_count++; lopt++; } @@ -2052,10 +2057,10 @@ void build_options() { while ((aopt = lopt)) { if (!aopt->name) break; - long_options[opt_count].name = aopt->name; - long_options[opt_count].has_arg = aopt->has_arg; - long_options[opt_count].flag = aopt->flag; - long_options[opt_count].val = aopt->val; + uwsgi.long_options[opt_count].name = aopt->name; + uwsgi.long_options[opt_count].has_arg = aopt->has_arg; + uwsgi.long_options[opt_count].flag = aopt->flag; + uwsgi.long_options[opt_count].val = aopt->val; opt_count++; lopt++; } @@ -2070,18 +2075,18 @@ void build_options() { while ((aopt = lopt)) { if (!aopt->name) break; - long_options[opt_count].name = aopt->name; - long_options[opt_count].has_arg = aopt->has_arg; - long_options[opt_count].flag = aopt->flag; - long_options[opt_count].val = aopt->val; + uwsgi.long_options[opt_count].name = aopt->name; + uwsgi.long_options[opt_count].has_arg = aopt->has_arg; + uwsgi.long_options[opt_count].flag = aopt->flag; + uwsgi.long_options[opt_count].val = aopt->val; opt_count++; lopt++; } } - long_options[opt_count].name = 0; - long_options[opt_count].has_arg = 0; - long_options[opt_count].flag = 0; - long_options[opt_count].val = 0; + uwsgi.long_options[opt_count].name = 0; + uwsgi.long_options[opt_count].has_arg = 0; + uwsgi.long_options[opt_count].flag = 0; + uwsgi.long_options[opt_count].val = 0; } diff --git a/uwsgi.h b/uwsgi.h index 9c3429fc..4872896e 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -172,6 +172,10 @@ #define UWSGI_LISTEN_QUEUE 511 #endif +struct uwsgi_opt { + char *key; + char *value; +}; #define MAX_CLUSTER_NODES 100 @@ -556,6 +560,10 @@ struct uwsgi_server { int apps_cnt; int default_app; + int option_index; + struct option *long_options; + struct uwsgi_opt **exported_opts; + int exported_opts_cnt; //base for all the requests(even on async mode) struct wsgi_request **wsgi_requests; struct wsgi_request *wsgi_req; @@ -1113,3 +1121,5 @@ ssize_t uwsgi_sendfile(struct wsgi_request *); void uwsgi_register_loop(char *, void *); void *uwsgi_get_loop(char *); + +void add_exported_option(int, char *); diff --git a/xmlconf.c b/xmlconf.c index 1bdb0b34..98607964 100644 --- a/xmlconf.c +++ b/xmlconf.c @@ -134,6 +134,7 @@ void uwsgi_xml_config(struct wsgi_request *wsgi_req, struct option *long_options if (aopt->flag) { *aopt->flag = aopt->val; + add_exported_option(0, (char *) node->name); } else { if (node->children) {