From cd29998b40aa5abce5090f2fbedd6047885e5b5d Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Wed, 3 Aug 2011 15:07:15 +0200 Subject: [PATCH] --pyshell and fix for wsgi.url_scheme in pep3333 --- plugins/python/pyloader.c | 6 +++--- plugins/python/python_plugin.c | 16 ++++++++++++++++ plugins/python/uwsgi_python.h | 3 +++ plugins/python/web3_subhandler.c | 8 ++++---- plugins/python/wsgi_subhandler.c | 8 ++++---- spooler.c | 2 -- uwsgi.c | 15 ++++++++++++++- uwsgi.h | 4 ++++ 8 files changed, 48 insertions(+), 14 deletions(-) diff --git a/plugins/python/pyloader.c b/plugins/python/pyloader.c index 0e46e6c5..e93315f4 100644 --- a/plugins/python/pyloader.c +++ b/plugins/python/pyloader.c @@ -286,13 +286,13 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre wi->gateway_version = PyTuple_New(2); PyTuple_SetItem(wi->gateway_version, 0, PyInt_FromLong(1)); PyTuple_SetItem(wi->gateway_version, 1, PyInt_FromLong(0)); - Py_INCREF(wi->gateway_version); + Py_INCREF((PyObject *)wi->gateway_version); wi->uwsgi_version = PyString_FromString(UWSGI_VERSION); - Py_INCREF(wi->uwsgi_version); + Py_INCREF((PyObject *)wi->uwsgi_version); wi->uwsgi_node = PyString_FromString(uwsgi.hostname); - Py_INCREF(wi->uwsgi_node); + Py_INCREF((PyObject *)wi->uwsgi_node); if (uwsgi.threads > 1 && id) { // if we have multiple threads we need to initialize a PyThreadState for each one diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 27c8d2d1..8c7b2a94 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -44,6 +44,7 @@ struct option uwsgi_python_options[] = { {"pep3333-input", no_argument, &up.pep3333_input, 1}, {"reload-os-env", no_argument, &up.reload_os_env, 1}, {"no-site", no_argument, &Py_NoSiteFlag, 1}, + {"pyshell", no_argument, 0, LONG_ARGS_PYSHELL}, {0, 0, 0, 0}, }; @@ -703,6 +704,11 @@ int uwsgi_python_manage_options(int i, char *optarg) { uwsgi_log("you can specify at most %d --pymodule-alias options\n", MAX_PYMODULE_ALIAS); } return 1; + case LONG_ARGS_PYSHELL: + uwsgi.honour_stdin = 1; + up.pyshell = 1; + uwsgi.to_hell = 1; + return 1; case LONG_ARGS_PYIMPORT: uwsgi_string_new_list(&up.import_list, optarg); return 1; @@ -1219,6 +1225,14 @@ void uwsgi_python_fixup() { uwsgi.p[30] = uwsgi.p[0]; } +void uwsgi_python_hijack(void) { + if (up.pyshell) { + PyImport_ImportModule("readline"); + PyRun_InteractiveLoop(stdin, "uwsgi"); + exit(0); + } +} + struct uwsgi_plugin python_plugin = { .name = "python", @@ -1247,6 +1261,8 @@ struct uwsgi_plugin python_plugin = { .suspend = uwsgi_python_suspend, .resume = uwsgi_python_resume, + .hijack_worker = uwsgi_python_hijack, + .signal_handler = uwsgi_python_signal_handler, .rpc = uwsgi_python_rpc, diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index 339cdcea..404d5bf4 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -18,6 +18,7 @@ #define LONG_ARGS_WEB3 LONG_ARGS_PYTHON_BASE + 8 #define LONG_ARGS_PUMP LONG_ARGS_PYTHON_BASE + 9 #define LONG_ARGS_WSGI_LITE LONG_ARGS_PYTHON_BASE + 10 +#define LONG_ARGS_PYSHELL LONG_ARGS_PYTHON_BASE + 11 #define PYTHON_APP_TYPE_WSGI 0 #define PYTHON_APP_TYPE_WEB3 1 @@ -123,6 +124,8 @@ struct uwsgi_python { char *test_module; + int pyshell; + struct uwsgi_string_list *python_path; struct uwsgi_string_list *import_list; struct uwsgi_string_list *post_pymodule_alias; diff --git a/plugins/python/web3_subhandler.c b/plugins/python/web3_subhandler.c index 60bc70ba..c0349268 100644 --- a/plugins/python/web3_subhandler.c +++ b/plugins/python/web3_subhandler.c @@ -81,18 +81,18 @@ void *uwsgi_request_subhandler_web3(struct wsgi_request *wsgi_req, struct uwsgi_ } if (wsgi_req->scheme_len > 0) { - zero = PyString_FromStringAndSize(wsgi_req->scheme, wsgi_req->scheme_len); + zero = UWSGI_PYFROMSTRINGSIZE(wsgi_req->scheme, wsgi_req->scheme_len); } else if (wsgi_req->https_len > 0) { if (!strncasecmp(wsgi_req->https, "on", 2) || wsgi_req->https[0] == '1') { - zero = PyString_FromString("https"); + zero = UWSGI_PYFROMSTRING("https"); } else { - zero = PyString_FromString("http"); + zero = UWSGI_PYFROMSTRING("http"); } } else { - zero = PyString_FromString("http"); + zero = UWSGI_PYFROMSTRING("http"); } PyDict_SetItemString(wsgi_req->async_environ, "web3.url_scheme", zero); Py_DECREF(zero); diff --git a/plugins/python/wsgi_subhandler.c b/plugins/python/wsgi_subhandler.c index ee7ab2b1..6f071c77 100644 --- a/plugins/python/wsgi_subhandler.c +++ b/plugins/python/wsgi_subhandler.c @@ -97,18 +97,18 @@ void *uwsgi_request_subhandler_wsgi(struct wsgi_request *wsgi_req, struct uwsgi_ } if (wsgi_req->scheme_len > 0) { - zero = PyString_FromStringAndSize(wsgi_req->scheme, wsgi_req->scheme_len); + zero = UWSGI_PYFROMSTRINGSIZE(wsgi_req->scheme, wsgi_req->scheme_len); } else if (wsgi_req->https_len > 0) { if (!strncasecmp(wsgi_req->https, "on", 2) || wsgi_req->https[0] == '1') { - zero = PyString_FromString("https"); + zero = UWSGI_PYFROMSTRING("https"); } else { - zero = PyString_FromString("http"); + zero = UWSGI_PYFROMSTRING("http"); } } else { - zero = PyString_FromString("http"); + zero = UWSGI_PYFROMSTRING("http"); } PyDict_SetItemString(wsgi_req->async_environ, "wsgi.url_scheme", zero); Py_DECREF(zero); diff --git a/spooler.c b/spooler.c index edfc5397..884b59e4 100644 --- a/spooler.c +++ b/spooler.c @@ -231,10 +231,8 @@ void spooler() { if (interesting_fd == uwsgi.shared->spooler_signal_pipe[1]) { uint8_t uwsgi_signal; if (read(interesting_fd, &uwsgi_signal, 1) <= 0) { - if (uwsgi.no_orphans) { uwsgi_log_verbose("uWSGI spooler screams: UAAAAAAH my master died, i will follow him...\n"); end_me(0); - } } else { #ifdef UWSGI_DEBUG diff --git a/uwsgi.c b/uwsgi.c index ea8566fb..80878ef6 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -1824,7 +1824,7 @@ int uwsgi_start(void *v_argv) { uwsgi_log("uwsgi socket %d inherited INET address %s fd 0\n", uwsgi_get_socket_num(uwsgi_sock), uwsgi_sock->name); } } - else { + else if (!uwsgi.honour_stdin) { int fd = open("/dev/null", O_RDONLY); if (fd < 0) { uwsgi_error_open("/dev/null"); @@ -2200,6 +2200,19 @@ skipzero: //from now on the process is a real worker } + for (i = 0; i < 0xFF; i++) { + if (uwsgi.p[i]->hijack_worker) { + uwsgi.p[i]->hijack_worker(); + } + } + + for (i = 0; i < uwsgi.gp_cnt; i++) { + if (uwsgi.gp[i]->hijack_worker) { + uwsgi.gp[i]->hijack_worker(); + } + } + + uwsgi_sock = uwsgi.sockets; while (uwsgi_sock) { struct uwsgi_string_list *usl = uwsgi.map_socket; diff --git a/uwsgi.h b/uwsgi.h index 1c0f29b2..991d9f17 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -600,6 +600,8 @@ struct uwsgi_plugin { void (*suspend) (struct wsgi_request *); void (*resume) (struct wsgi_request *); + void (*hijack_worker) (void); + int (*magic) (char *, char *); void *(*encode_string) (char *); @@ -976,6 +978,8 @@ struct uwsgi_server { int log_micros; char *log_strftime; + int honour_stdin; + int log_master; int log_syslog; int original_log_fd;