diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index e0a9387b..a21c943f 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -43,7 +43,7 @@ void uwsgi_python_pthread_child(void) { // fake method PyMethodDef null_methods[] = { - { NULL, NULL}, + {NULL, NULL}, }; PyMethodDef uwsgi_spit_method[] = { {"uwsgi_spit", py_uwsgi_spit, METH_VARARGS, ""} }; @@ -142,7 +142,7 @@ void uwsgi_python_post_fork() { PyObject *random_args = PyTuple_New(1); // pass no args PyTuple_SetItem(random_args, 0, Py_None); - PyEval_CallObject( random_seed, random_args ); + PyEval_CallObject(random_seed, random_args); if (PyErr_Occurred()) { PyErr_Print(); } @@ -162,25 +162,23 @@ void uwsgi_python_post_fork() { PyErr_Clear(); #endif - UWSGI_RELEASE_GIL - -} +UWSGI_RELEASE_GIL} PyObject *uwsgi_pyimport_by_filename(char *name, char *filename) { - FILE *pyfile; - struct _node *py_file_node = NULL; - PyObject *py_compiled_node, *py_file_module; + FILE *pyfile; + struct _node *py_file_node = NULL; + PyObject *py_compiled_node, *py_file_module; int is_a_package = 0; struct stat pystat; char *real_filename = filename; - - pyfile = fopen(filename, "r"); - if (!pyfile) { - uwsgi_log("failed to open python file %s\n", filename); - exit(1); - } + + pyfile = fopen(filename, "r"); + if (!pyfile) { + uwsgi_log("failed to open python file %s\n", filename); + exit(1); + } if (fstat(fileno(pyfile), &pystat)) { uwsgi_error("fstat()"); @@ -192,41 +190,41 @@ PyObject *uwsgi_pyimport_by_filename(char *name, char *filename) { fclose(pyfile); real_filename = uwsgi_concat2(filename, "/__init__.py"); pyfile = fopen(real_filename, "r"); - if (!pyfile) { - uwsgi_error("fopen()"); - exit(1); - } + if (!pyfile) { + uwsgi_error("fopen()"); + exit(1); + } } - py_file_node = PyParser_SimpleParseFile(pyfile, real_filename, Py_file_input); - if (!py_file_node) { - PyErr_Print(); - uwsgi_log( "failed to parse file %s\n", real_filename); - exit(1); - } + py_file_node = PyParser_SimpleParseFile(pyfile, real_filename, Py_file_input); + if (!py_file_node) { + PyErr_Print(); + uwsgi_log("failed to parse file %s\n", real_filename); + exit(1); + } - fclose(pyfile); + fclose(pyfile); - py_compiled_node = (PyObject *) PyNode_Compile(py_file_node, real_filename); + py_compiled_node = (PyObject *) PyNode_Compile(py_file_node, real_filename); - if (!py_compiled_node) { - PyErr_Print(); - uwsgi_log( "failed to compile python file %s\n", real_filename); - exit(1); - } + if (!py_compiled_node) { + PyErr_Print(); + uwsgi_log("failed to compile python file %s\n", real_filename); + exit(1); + } - py_file_module = PyImport_ExecCodeModule(name, py_compiled_node); - if (!py_file_module) { - PyErr_Print(); - exit(1); - } + py_file_module = PyImport_ExecCodeModule(name, py_compiled_node); + if (!py_file_module) { + PyErr_Print(); + exit(1); + } - Py_DECREF(py_compiled_node); + Py_DECREF(py_compiled_node); - if( is_a_package) { + if (is_a_package) { PyObject *py_file_module_dict = PyModule_GetDict(py_file_module); if (py_file_module_dict) { - PyDict_SetItemString(py_file_module_dict, "__path__", Py_BuildValue("[O]",PyString_FromString(filename))); + PyDict_SetItemString(py_file_module_dict, "__path__", Py_BuildValue("[O]", PyString_FromString(filename))); } free(real_filename); } @@ -281,9 +279,9 @@ void init_uwsgi_vars() { } // check here - PyString_Concat( &venv_path, PyString_FromString(venv_version) ); + PyString_Concat(&venv_path, PyString_FromString(venv_version)); - if ( PyList_Insert(pypath, 0, venv_path) ) { + if (PyList_Insert(pypath, 0, venv_path)) { PyErr_Print(); } @@ -295,20 +293,20 @@ void init_uwsgi_vars() { } #endif - if (PyList_Insert(pypath, 0, UWSGI_PYFROMSTRING(".") ) != 0) { + if (PyList_Insert(pypath, 0, UWSGI_PYFROMSTRING(".")) != 0) { PyErr_Print(); } for (i = 0; i < up.python_path_cnt; i++) { - if (PyList_Insert(pypath, 0, UWSGI_PYFROMSTRING(up.python_path[i]) ) != 0) { + if (PyList_Insert(pypath, 0, UWSGI_PYFROMSTRING(up.python_path[i])) != 0) { PyErr_Print(); } else { - uwsgi_log( "added %s to pythonpath.\n", up.python_path[i]); + uwsgi_log("added %s to pythonpath.\n", up.python_path[i]); } } - for(i=0;ikey)) ) { - 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 - if (PyDict_SetItemString(up.embedded_dict, "unbit", Py_None)) { -#endif - PyErr_Print(); - exit(1); - } - - if (PyDict_SetItemString(up.embedded_dict, "buffer_size", PyInt_FromLong(uwsgi.buffer_size))) { - PyErr_Print(); - exit(1); - } - - if (PyDict_SetItemString(up.embedded_dict, "started_on", PyInt_FromLong(uwsgi.start_tv.tv_sec))) { - PyErr_Print(); - exit(1); - } - - if (PyDict_SetItemString(up.embedded_dict, "start_response", up.wsgi_spitout)) { - PyErr_Print(); - exit(1); - } - - if (PyDict_SetItemString(up.embedded_dict, "fastfuncs", PyList_New(256))) { - PyErr_Print(); - exit(1); - } - - - if (PyDict_SetItemString(up.embedded_dict, "applications", Py_None)) { - PyErr_Print(); - exit(1); - } - - if (uwsgi.is_a_reload) { - if (PyDict_SetItemString(up.embedded_dict, "is_a_reload", Py_True)) { - PyErr_Print(); - exit(1); - } + PyObject *py_opt_dict = PyDict_New(); + for (i = 0; i < uwsgi.exported_opts_cnt; i++) { + if (PyDict_Contains(py_opt_dict, PyString_FromString(uwsgi.exported_opts[i]->key))) { + 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 { - if (PyDict_SetItemString(up.embedded_dict, "is_a_reload", Py_False)) { - PyErr_Print(); - exit(1); + 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)); } - } - up.embedded_args = PyTuple_New(2); - if (!up.embedded_args) { - PyErr_Print(); - exit(1); + PyDict_SetItemString(py_opt_dict, uwsgi.exported_opts[i]->key, py_opt_list); } - - if (PyDict_SetItemString(up.embedded_dict, "message_manager_marshal", Py_None)) { - PyErr_Print(); - exit(1); + } + else { + if (uwsgi.exported_opts[i]->value == NULL) { + PyDict_SetItemString(py_opt_dict, uwsgi.exported_opts[i]->key, Py_True); } - - up.fastfuncslist = PyDict_GetItemString(up.embedded_dict, "fastfuncs"); - if (!up.fastfuncslist) { - PyErr_Print(); - exit(1); + else { + PyDict_SetItemString(py_opt_dict, uwsgi.exported_opts[i]->key, PyString_FromString(uwsgi.exported_opts[i]->value)); } + } + } - init_uwsgi_module_advanced(new_uwsgi_module); + 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 + if (PyDict_SetItemString(up.embedded_dict, "unbit", Py_None)) { +#endif + PyErr_Print(); + exit(1); + } + + if (PyDict_SetItemString(up.embedded_dict, "buffer_size", PyInt_FromLong(uwsgi.buffer_size))) { + PyErr_Print(); + exit(1); + } + + if (PyDict_SetItemString(up.embedded_dict, "started_on", PyInt_FromLong(uwsgi.start_tv.tv_sec))) { + PyErr_Print(); + exit(1); + } + + if (PyDict_SetItemString(up.embedded_dict, "start_response", up.wsgi_spitout)) { + PyErr_Print(); + exit(1); + } + + if (PyDict_SetItemString(up.embedded_dict, "fastfuncs", PyList_New(256))) { + PyErr_Print(); + exit(1); + } + + + if (PyDict_SetItemString(up.embedded_dict, "applications", Py_None)) { + PyErr_Print(); + exit(1); + } + + if (uwsgi.is_a_reload) { + if (PyDict_SetItemString(up.embedded_dict, "is_a_reload", Py_True)) { + PyErr_Print(); + exit(1); + } + } + else { + if (PyDict_SetItemString(up.embedded_dict, "is_a_reload", Py_False)) { + PyErr_Print(); + exit(1); + } + } + + up.embedded_args = PyTuple_New(2); + if (!up.embedded_args) { + PyErr_Print(); + exit(1); + } + + if (PyDict_SetItemString(up.embedded_dict, "message_manager_marshal", Py_None)) { + PyErr_Print(); + exit(1); + } + + up.fastfuncslist = PyDict_GetItemString(up.embedded_dict, "fastfuncs"); + if (!up.fastfuncslist) { + PyErr_Print(); + exit(1); + } + + init_uwsgi_module_advanced(new_uwsgi_module); #ifdef UWSGI_SPOOLER - if (uwsgi.spool_dir != NULL) { - init_uwsgi_module_spooler(new_uwsgi_module); - } + if (uwsgi.spool_dir != NULL) { + init_uwsgi_module_spooler(new_uwsgi_module); + } #endif - if (uwsgi.sharedareasize > 0 && uwsgi.sharedarea) { - init_uwsgi_module_sharedarea(new_uwsgi_module); - } + if (uwsgi.sharedareasize > 0 && uwsgi.sharedarea) { + init_uwsgi_module_sharedarea(new_uwsgi_module); + } - if (uwsgi.cache_max_items > 0) { - init_uwsgi_module_cache(new_uwsgi_module); - } - } + if (uwsgi.cache_max_items > 0) { + init_uwsgi_module_cache(new_uwsgi_module); + } +} #endif @@ -564,18 +593,18 @@ int uwsgi_python_magic(char *mountpoint, char *lazy) { up.callable = qc + 1; } - if (!strcmp(lazy+strlen(lazy)-3, ".py")) { + if (!strcmp(lazy + strlen(lazy) - 3, ".py")) { up.file_config = lazy; return 1; } - else if (!strcmp(lazy+strlen(lazy)-5, ".wsgi")) { + else if (!strcmp(lazy + strlen(lazy) - 5, ".wsgi")) { up.file_config = lazy; return 1; } - else if (qc && strchr(lazy,'.')) { + else if (qc && strchr(lazy, '.')) { up.wsgi_config = lazy; return 1; - } + } // reset lazy if (qc) { @@ -585,72 +614,73 @@ int uwsgi_python_magic(char *mountpoint, char *lazy) { } - int uwsgi_python_manage_options(int i, char *optarg) { +int uwsgi_python_manage_options(int i, char *optarg) { - switch(i) { - case 'w': - up.wsgi_config = optarg; - return 1; - case LONG_ARGS_WSGI_FILE: - case LONG_ARGS_FILE_CONFIG: - up.file_config = optarg; - return 1; - case LONG_ARGS_PYMODULE_ALIAS: - if (up.pymodule_alias_cnt < MAX_PYMODULE_ALIAS) { - up.pymodule_alias[up.pymodule_alias_cnt] = optarg; - up.pymodule_alias_cnt++; - } - else { - uwsgi_log( "you can specify at most %d --pymodule-alias options\n", MAX_PYMODULE_ALIAS); - } - return 1; - case LONG_ARGS_PYTHONPATH: - if (up.python_path_cnt < MAX_PYTHONPATH) { - up.python_path[up.python_path_cnt] = optarg; - up.python_path_cnt++; - } - else { - uwsgi_log( "you can specify at most %d --pythonpath options\n", MAX_PYTHONPATH); - } - return 1; - case LONG_ARGS_PYARGV: - up.argv = optarg; - return 1; - case 'j': - up.test_module = optarg; - return 1; - case 'H': - up.home = optarg; - return 1; - case 'O': - up.optimize = atoi(optarg); - return 1; - case LONG_ARGS_CALLABLE: - up.callable = optarg; - return 1; - - - case LONG_ARGS_INI_PASTE: - uwsgi.ini = optarg; - if (uwsgi.ini[0] != '/') { - up.paste = uwsgi_concat4("config:", uwsgi.cwd, "/", uwsgi.ini); - } else { - up.paste = uwsgi_concat2("config:", uwsgi.ini); - } - return 1; - case LONG_ARGS_PASTE: - up.paste = optarg; - return 1; - - - - } - - return 0; + switch (i) { + case 'w': + up.wsgi_config = optarg; + return 1; + case LONG_ARGS_WSGI_FILE: + case LONG_ARGS_FILE_CONFIG: + up.file_config = optarg; + return 1; + case LONG_ARGS_PYMODULE_ALIAS: + if (up.pymodule_alias_cnt < MAX_PYMODULE_ALIAS) { + up.pymodule_alias[up.pymodule_alias_cnt] = optarg; + up.pymodule_alias_cnt++; } + else { + uwsgi_log("you can specify at most %d --pymodule-alias options\n", MAX_PYMODULE_ALIAS); + } + return 1; + case LONG_ARGS_PYTHONPATH: + if (up.python_path_cnt < MAX_PYTHONPATH) { + up.python_path[up.python_path_cnt] = optarg; + up.python_path_cnt++; + } + else { + uwsgi_log("you can specify at most %d --pythonpath options\n", MAX_PYTHONPATH); + } + return 1; + case LONG_ARGS_PYARGV: + up.argv = optarg; + return 1; + case 'j': + up.test_module = optarg; + return 1; + case 'H': + up.home = optarg; + return 1; + case 'O': + up.optimize = atoi(optarg); + return 1; + case LONG_ARGS_CALLABLE: + up.callable = optarg; + return 1; + + + case LONG_ARGS_INI_PASTE: + uwsgi.ini = optarg; + if (uwsgi.ini[0] != '/') { + up.paste = uwsgi_concat4("config:", uwsgi.cwd, "/", uwsgi.ini); + } + else { + up.paste = uwsgi_concat2("config:", uwsgi.ini); + } + return 1; + case LONG_ARGS_PASTE: + up.paste = optarg; + return 1; + + + + } + + return 0; +} int uwsgi_python_mount_app(char *mountpoint, char *app) { - + uwsgi.wsgi_req->script_name = mountpoint; uwsgi.wsgi_req->script_name_len = strlen(mountpoint); if (uwsgi.single_interpreter) { @@ -660,56 +690,56 @@ int uwsgi_python_mount_app(char *mountpoint, char *app) { } - void uwsgi_python_init_apps() { +void uwsgi_python_init_apps() { - if (up.wsgi_config != NULL) { - init_uwsgi_app(LOADER_UWSGI, up.wsgi_config, uwsgi.wsgi_req, up.main_thread); - } + if (up.wsgi_config != NULL) { + init_uwsgi_app(LOADER_UWSGI, up.wsgi_config, uwsgi.wsgi_req, up.main_thread); + } - if (up.file_config != NULL) { - init_uwsgi_app(LOADER_FILE, up.file_config, uwsgi.wsgi_req, up.main_thread); - } - if (up.paste != NULL) { - init_uwsgi_app(LOADER_PASTE, up.paste, uwsgi.wsgi_req, up.main_thread); - } - if (up.eval != NULL) { - init_uwsgi_app(LOADER_EVAL, up.eval, uwsgi.wsgi_req, up.main_thread); - } + if (up.file_config != NULL) { + init_uwsgi_app(LOADER_FILE, up.file_config, uwsgi.wsgi_req, up.main_thread); + } + if (up.paste != NULL) { + init_uwsgi_app(LOADER_PASTE, up.paste, uwsgi.wsgi_req, up.main_thread); + } + if (up.eval != NULL) { + init_uwsgi_app(LOADER_EVAL, up.eval, uwsgi.wsgi_req, up.main_thread); + } - } +} - void uwsgi_python_enable_threads() { +void uwsgi_python_enable_threads() { - PyEval_InitThreads(); - if (pthread_key_create(&up.upt_save_key, NULL)) { - uwsgi_error("pthread_key_create()"); - exit(1); - } - if (pthread_key_create(&up.upt_gil_key, NULL)) { - uwsgi_error("pthread_key_create()"); - exit(1); - } - pthread_setspecific(up.upt_save_key, (void *) PyThreadState_Get()); - pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Get()); - pthread_mutex_init(&up.lock_pyloaders, NULL); - pthread_atfork(uwsgi_python_pthread_prepare, uwsgi_python_pthread_parent, uwsgi_python_pthread_child); - up.gil_get = gil_real_get; - up.gil_release = gil_real_release; + PyEval_InitThreads(); + if (pthread_key_create(&up.upt_save_key, NULL)) { + uwsgi_error("pthread_key_create()"); + exit(1); + } + if (pthread_key_create(&up.upt_gil_key, NULL)) { + uwsgi_error("pthread_key_create()"); + exit(1); + } + pthread_setspecific(up.upt_save_key, (void *) PyThreadState_Get()); + pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Get()); + pthread_mutex_init(&up.lock_pyloaders, NULL); + pthread_atfork(uwsgi_python_pthread_prepare, uwsgi_python_pthread_parent, uwsgi_python_pthread_child); + up.gil_get = gil_real_get; + up.gil_release = gil_real_release; - up.swap_ts = threaded_swap_ts; - up.reset_ts = threaded_reset_ts; - uwsgi_log("threads support enabled\n"); - } + up.swap_ts = threaded_swap_ts; + up.reset_ts = threaded_reset_ts; + uwsgi_log("threads support enabled\n"); +} - void uwsgi_python_init_thread(int core_id) { +void uwsgi_python_init_thread(int core_id) { - // set a new ThreadState for each thread - PyThreadState *pts; - pts = PyThreadState_New(up.main_thread->interp); - pthread_setspecific(up.upt_save_key, (void *) pts); - pthread_setspecific(up.upt_gil_key, (void *) pts); + // set a new ThreadState for each thread + PyThreadState *pts; + pts = PyThreadState_New(up.main_thread->interp); + pthread_setspecific(up.upt_save_key, (void *) pts); + pthread_setspecific(up.upt_gil_key, (void *) pts); - } +} int uwsgi_python_xml(char *node, char *content) { @@ -760,11 +790,11 @@ int uwsgi_python_xml(char *node, char *content) { void uwsgi_python_suspend(struct wsgi_request *wsgi_req) { - PyThreadState* tstate = PyThreadState_GET(); + PyThreadState *tstate = PyThreadState_GET(); uwsgi_log("suspending python\n"); - up.current_recursion_depth = tstate->recursion_depth; - up.current_frame = tstate->frame; + up.current_recursion_depth = tstate->recursion_depth; + up.current_frame = tstate->frame; } @@ -773,13 +803,14 @@ int uwsgi_python_signal_handler(uint8_t sig, void *handler, char *payload, uint8 PyObject *args = PyTuple_New(2); PyObject *ret; - if (!args) return -1; + if (!args) + return -1; PyTuple_SetItem(args, 0, PyInt_FromLong(sig)); PyTuple_SetItem(args, 1, PyString_FromStringAndSize(payload, payload_size)); ret = python_call(handler, args, 0); - + if (ret) { return 0; } @@ -787,7 +818,7 @@ int uwsgi_python_signal_handler(uint8_t sig, void *handler, char *payload, uint8 return -1; } -uint16_t uwsgi_python_rpc(void * func, uint8_t argc, char **argv, char *buffer) { +uint16_t uwsgi_python_rpc(void *func, uint8_t argc, char **argv, char *buffer) { uint8_t i; PyObject *pyargs = PyTuple_New(argc); @@ -795,9 +826,10 @@ uint16_t uwsgi_python_rpc(void * func, uint8_t argc, char **argv, char *buffer) char *rv; size_t rl; - if (!pyargs) return 0; + if (!pyargs) + return 0; - for(i=0;irecursion_depth = up.current_recursion_depth; - tstate->frame = up.current_frame; + tstate->frame = up.current_frame; } - struct uwsgi_plugin python_plugin = { +struct uwsgi_plugin python_plugin = { - .name = "python", - .modifier1 = 0, - .init = uwsgi_python_init, - .post_fork = uwsgi_python_post_fork, - .options = uwsgi_python_options, - .manage_opt = uwsgi_python_manage_options, - .short_options = "w:O:H:j:", - .request = uwsgi_request_wsgi, - .after_request = uwsgi_after_request_wsgi, - .init_apps = uwsgi_python_init_apps, + .name = "python", + .modifier1 = 0, + .init = uwsgi_python_init, + .post_fork = uwsgi_python_post_fork, + .options = uwsgi_python_options, + .manage_opt = uwsgi_python_manage_options, + .short_options = "w:O:H:j:", + .request = uwsgi_request_wsgi, + .after_request = uwsgi_after_request_wsgi, + .init_apps = uwsgi_python_init_apps, - .mount_app = uwsgi_python_mount_app, + .mount_app = uwsgi_python_mount_app, - .enable_threads = uwsgi_python_enable_threads, - .init_thread = uwsgi_python_init_thread, - .manage_xml = uwsgi_python_xml, + .enable_threads = uwsgi_python_enable_threads, + .init_thread = uwsgi_python_init_thread, + .manage_xml = uwsgi_python_xml, - .magic = uwsgi_python_magic, + .magic = uwsgi_python_magic, - .suspend = uwsgi_python_suspend, - .resume = uwsgi_python_resume, + .suspend = uwsgi_python_suspend, + .resume = uwsgi_python_resume, - .signal_handler = uwsgi_python_signal_handler, - .rpc = uwsgi_python_rpc, + .signal_handler = uwsgi_python_signal_handler, + .rpc = uwsgi_python_rpc, - .spooler = uwsgi_python_spooler, - /* - .help = uwsgi_python_help, - */ - - }; + .spooler = uwsgi_python_spooler, + /* + .help = uwsgi_python_help, + */ +}; diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index 95e53405..ae9689b7 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -7,7 +7,7 @@ char *spool_buffer = NULL; extern struct uwsgi_server uwsgi; extern struct uwsgi_python up; -char *uwsgi_encode_pydict(PyObject *pydict, uint16_t *size) { +char *uwsgi_encode_pydict(PyObject * pydict, uint16_t * size) { int i; PyObject *zero, *key, *val; @@ -18,23 +18,23 @@ char *uwsgi_encode_pydict(PyObject *pydict, uint16_t *size) { PyObject *vars = PyDict_Items(pydict); - if (!vars) { + if (!vars) { PyErr_Print(); - return NULL; - } - + return NULL; + } + *size = 0; // calc the packet size // try to fallback whenever possible for (i = 0; i < PyList_Size(vars); i++) { - zero = PyList_GetItem(vars, i); + zero = PyList_GetItem(vars, i); if (!zero) { PyErr_Print(); continue; } - if (!PyTuple_Check(zero)) { + if (!PyTuple_Check(zero)) { uwsgi_log("invalid python dictionary item\n"); continue; } @@ -43,16 +43,16 @@ char *uwsgi_encode_pydict(PyObject *pydict, uint16_t *size) { uwsgi_log("invalid python dictionary item\n"); continue; } - key = PyTuple_GetItem(zero, 0); - val = PyTuple_GetItem(zero, 1); + key = PyTuple_GetItem(zero, 0); + val = PyTuple_GetItem(zero, 1); - if (!PyString_Check(key) || !PyString_Check(val)) { + if (!PyString_Check(key) || !PyString_Check(val)) { continue; } - keysize = PyString_Size(key); - valsize = PyString_Size(val); + keysize = PyString_Size(key); + valsize = PyString_Size(val); *size += (keysize + 2 + valsize + 2); @@ -63,7 +63,7 @@ char *uwsgi_encode_pydict(PyObject *pydict, uint16_t *size) { if (*size <= 4) { uwsgi_log("empty python dictionary\n"); return NULL; - } + } // remember to free this memory !!! buf = malloc(*size); @@ -75,60 +75,60 @@ char *uwsgi_encode_pydict(PyObject *pydict, uint16_t *size) { bufptr = buf; for (i = 0; i < PyList_Size(vars); i++) { - zero = PyList_GetItem(vars, i); - if (!zero) { - PyErr_Print(); - continue; - } + zero = PyList_GetItem(vars, i); + if (!zero) { + PyErr_Print(); + continue; + } - if (!PyTuple_Check(zero)) { - uwsgi_log("invalid python dictionary item\n"); - Py_DECREF(zero); - continue; - } + if (!PyTuple_Check(zero)) { + uwsgi_log("invalid python dictionary item\n"); + Py_DECREF(zero); + continue; + } - if (PyTuple_Size(zero) < 2) { - uwsgi_log("invalid python dictionary item\n"); - Py_DECREF(zero); - continue; - } - key = PyTuple_GetItem(zero, 0); - val = PyTuple_GetItem(zero, 1); + if (PyTuple_Size(zero) < 2) { + uwsgi_log("invalid python dictionary item\n"); + Py_DECREF(zero); + continue; + } + key = PyTuple_GetItem(zero, 0); + val = PyTuple_GetItem(zero, 1); if (!key || !val) { PyErr_Print(); } - if (!PyString_Check(key) || !PyString_Check(val)) { - Py_DECREF(zero); - continue; - } + if (!PyString_Check(key) || !PyString_Check(val)) { + Py_DECREF(zero); + continue; + } - keysize = PyString_Size(key); - valsize = PyString_Size(val); - if (bufptr + keysize + 2 + valsize + 2 <= buf + *size) { + keysize = PyString_Size(key); + valsize = PyString_Size(val); + if (bufptr + keysize + 2 + valsize + 2 <= buf + *size) { #ifdef __BIG_ENDIAN__ keysize = uwsgi_swap16(keysize); #endif - memcpy(bufptr, &keysize, 2); - bufptr += 2; + memcpy(bufptr, &keysize, 2); + bufptr += 2; #ifdef __BIG_ENDIAN__ - keysize = uwsgi_swap16(keysize); + keysize = uwsgi_swap16(keysize); #endif - memcpy(bufptr, PyString_AsString(key), keysize); - bufptr += keysize; + memcpy(bufptr, PyString_AsString(key), keysize); + bufptr += keysize; #ifdef __BIG_ENDIAN__ - valsize = uwsgi_swap16(valsize); + valsize = uwsgi_swap16(valsize); #endif - memcpy(bufptr, &valsize, 2); - bufptr += 2; + memcpy(bufptr, &valsize, 2); + bufptr += 2; #ifdef __BIG_ENDIAN__ - valsize = uwsgi_swap16(valsize); + valsize = uwsgi_swap16(valsize); #endif - memcpy(bufptr, PyString_AsString(val), valsize); - bufptr += valsize; + memcpy(bufptr, PyString_AsString(val), valsize); + bufptr += valsize; } Py_DECREF(zero); @@ -144,45 +144,45 @@ PyObject *py_uwsgi_close(PyObject * self, PyObject * args) { int fd; if (!PyArg_ParseTuple(args, "i:close", &fd)) { - return NULL; - } + return NULL; + } close(fd); Py_INCREF(Py_None); return Py_None; - + } PyObject *py_uwsgi_register_timer(PyObject * self, PyObject * args) { - uint8_t uwsgi_signal; - uint8_t signal_kind; - PyObject *handler; - int secs; + uint8_t uwsgi_signal; + uint8_t signal_kind; + PyObject *handler; + int secs; - if (!PyArg_ParseTuple(args, "BiBO:register_timer", &uwsgi_signal, &secs, &signal_kind, &handler)) { - return NULL; - } + if (!PyArg_ParseTuple(args, "BiBO:register_timer", &uwsgi_signal, &secs, &signal_kind, &handler)) { + return NULL; + } - uwsgi_register_timer(uwsgi_signal, secs, signal_kind, handler, 0); + uwsgi_register_timer(uwsgi_signal, secs, signal_kind, handler, 0); - Py_INCREF(Py_None); - return Py_None; + Py_INCREF(Py_None); + return Py_None; } PyObject *py_uwsgi_register_file_monitor(PyObject * self, PyObject * args) { uint8_t uwsgi_signal; - uint8_t signal_kind; - PyObject *handler; + uint8_t signal_kind; + PyObject *handler; char *filename; if (!PyArg_ParseTuple(args, "BsBO:register_file_monitor", &uwsgi_signal, &filename, &signal_kind, &handler)) { - return NULL; - } + return NULL; + } uwsgi_register_file_monitor(uwsgi_signal, filename, signal_kind, handler, 0); @@ -192,37 +192,39 @@ PyObject *py_uwsgi_register_file_monitor(PyObject * self, PyObject * args) { PyObject *py_uwsgi_call(PyObject * self, PyObject * args) { - char buffer[0xffff]; - char *func; - uint16_t size = 0; - PyObject *py_func; - int argc = PyTuple_Size(args) ; + char buffer[0xffff]; + char *func; + uint16_t size = 0; + PyObject *py_func; + int argc = PyTuple_Size(args); int i; char *argv[0xff]; - // TODO better error reporting - if (argc < 1) goto clear; + // TODO better error reporting + if (argc < 1) + goto clear; - py_func = PyTuple_GetItem(args, 0); + py_func = PyTuple_GetItem(args, 0); - if (!PyString_Check(py_func)) goto clear; + if (!PyString_Check(py_func)) + goto clear; - func = PyString_AsString(py_func); + func = PyString_AsString(py_func); - for(i=0;i<(argc-1);i++) { - argv[i] = PyString_AsString( PyTuple_GetItem(args, i+1) ); + for (i = 0; i < (argc - 1); i++) { + argv[i] = PyString_AsString(PyTuple_GetItem(args, i + 1)); } - size = uwsgi_rpc(func, argc-1, argv, buffer); + size = uwsgi_rpc(func, argc - 1, argv, buffer); - if (size > 0) { - return PyString_FromStringAndSize(buffer, size); - } + if (size > 0) { + return PyString_FromStringAndSize(buffer, size); + } -clear: + clear: - Py_INCREF(Py_None); - return Py_None; + Py_INCREF(Py_None); + return Py_None; } PyObject *py_uwsgi_rpc_list(PyObject * self, PyObject * args) { @@ -230,11 +232,11 @@ PyObject *py_uwsgi_rpc_list(PyObject * self, PyObject * args) { int i; PyObject *rpc_list = PyTuple_New(uwsgi.shared->rpc_count); - for(i=0;irpc_count;i++) { - if (uwsgi.shared->rpc_table[i].name[0] != 0) { + for (i = 0; i < uwsgi.shared->rpc_count; i++) { + if (uwsgi.shared->rpc_table[i].name[0] != 0) { PyTuple_SetItem(rpc_list, i, PyString_FromString(uwsgi.shared->rpc_table[i].name)); - } - } + } + } return rpc_list; @@ -247,7 +249,7 @@ PyObject *py_uwsgi_rpc(PyObject * self, PyObject * args) { uint16_t size = 0; PyObject *py_node, *py_func; struct uwsgi_header uh; - int argc = PyTuple_Size(args) ; + int argc = PyTuple_Size(args); char *argv[0xff]; int i, fd; uint16_t pktsize = 0, ulen; @@ -257,7 +259,8 @@ PyObject *py_uwsgi_rpc(PyObject * self, PyObject * args) { struct pollfd upoll; // TODO better error reporting - if (argc < 2) goto clear; + if (argc < 2) + goto clear; py_node = PyTuple_GetItem(args, 0); @@ -269,13 +272,14 @@ PyObject *py_uwsgi_rpc(PyObject * self, PyObject * args) { } py_func = PyTuple_GetItem(args, 1); - - if (!PyString_Check(py_func)) goto clear; - + + if (!PyString_Check(py_func)) + goto clear; + func = PyString_AsString(py_func); - for(i=0;i<(argc-2);i++) { - argv[i] = PyString_AsString( PyTuple_GetItem(args, i+2) ); + for (i = 0; i < (argc - 2); i++) { + argv[i] = PyString_AsString(PyTuple_GetItem(args, i + 2)); } if (node == (char *) "") { @@ -283,15 +287,16 @@ PyObject *py_uwsgi_rpc(PyObject * self, PyObject * args) { } else { - - // connect to node - fd = uwsgi_connect(node, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0); - if (fd < 0) goto clear; + // connect to node + fd = uwsgi_connect(node, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0); + + if (fd < 0) + goto clear; // prepare a uwsgi array pktsize = 2 + strlen(func); - for(i=0;i>8) & 0xff); - memcpy(bufptr, func, ulen); bufptr += ulen; - for(i=0;i> 8) & 0xff); + memcpy(bufptr, func, ulen); + bufptr += ulen; + + for (i = 0; i < argc - 2; i++) { ulen = strlen(argv[i]); - *bufptr++ = (uint8_t) (ulen & 0xff); - *bufptr++ = (uint8_t) ((ulen >>8) & 0xff); - memcpy(bufptr, argv[i], ulen); bufptr += ulen; + *bufptr++ = (uint8_t) (ulen & 0xff); + *bufptr++ = (uint8_t) ((ulen >> 8) & 0xff); + memcpy(bufptr, argv[i], ulen); + bufptr += ulen; } if (write(fd, &uh, 4) != 4) { @@ -333,14 +340,14 @@ PyObject *py_uwsgi_rpc(PyObject * self, PyObject * args) { size = uh.pktsize; } } - + } if (size > 0) { return PyString_FromStringAndSize(buffer, size); } -clear: + clear: Py_INCREF(Py_None); return Py_None; @@ -353,8 +360,8 @@ PyObject *py_uwsgi_register_rpc(PyObject * self, PyObject * args) { PyObject *func; if (!PyArg_ParseTuple(args, "sO|B:register_rpc", &name, &func, &argc)) { - return NULL; - } + return NULL; + } if (uwsgi_register_rpc(name, 0, argc, func)) { @@ -371,8 +378,8 @@ PyObject *py_uwsgi_attach_daemon(PyObject * self, PyObject * args) { char *command = NULL; if (!PyArg_ParseTuple(args, "s:attach_daemon", &command)) { - return NULL; - } + return NULL; + } if (uwsgi_attach_daemon(command)) { Py_INCREF(Py_None); @@ -391,8 +398,8 @@ PyObject *py_uwsgi_register_signal(PyObject * self, PyObject * args) { char *payload = NULL; if (!PyArg_ParseTuple(args, "BBO|s:register_signal", &uwsgi_signal, &signal_kind, &handler, &payload)) { - return NULL; - } + return NULL; + } if (payload == NULL) { uwsgi_register_signal(uwsgi_signal, signal_kind, handler, 0, NULL, 0); @@ -402,7 +409,7 @@ PyObject *py_uwsgi_register_signal(PyObject * self, PyObject * args) { } Py_INCREF(Py_None); - return Py_None; + return Py_None; } PyObject *py_uwsgi_signal(PyObject * self, PyObject * args) { @@ -411,11 +418,11 @@ PyObject *py_uwsgi_signal(PyObject * self, PyObject * args) { ssize_t rlen; if (!PyArg_ParseTuple(args, "B:signal", &uwsgi_signal)) { - return NULL; - } + return NULL; + } uwsgi_log("sending %d to master\n", uwsgi_signal); - + rlen = write(uwsgi.shared->worker_signal_pipe[1], &uwsgi_signal, 1); if (rlen != 1) { uwsgi_error("write()"); @@ -424,17 +431,17 @@ PyObject *py_uwsgi_signal(PyObject * self, PyObject * args) { Py_INCREF(Py_None); return Py_None; - + } PyObject *py_uwsgi_log_this(PyObject * self, PyObject * args) { - struct wsgi_request *wsgi_req = current_wsgi_req(); + struct wsgi_request *wsgi_req = current_wsgi_req(); wsgi_req->log_this = 1; Py_INCREF(Py_None); - return Py_None; + return Py_None; } PyObject *py_uwsgi_recv_frame(PyObject * self, PyObject * args) { @@ -456,21 +463,21 @@ PyObject *py_uwsgi_recv_frame(PyObject * self, PyObject * args) { if (!PyArg_ParseTuple(args, "icc:recv_frame", &fd, &null1, &null2)) { - return NULL; - } + return NULL; + } -get_data: + get_data: frame_ptr = frame; if (wsgi_req->frame_len > 0) { // we have already some data buffered // search for the prefix and adjust frame_pos - bufptr = wsgi_req->buffer+wsgi_req->frame_pos; - for(i=0;iframe_len;i++) { + bufptr = wsgi_req->buffer + wsgi_req->frame_pos; + for (i = 0; i < wsgi_req->frame_len; i++) { if (bufptr[i] == prefix) { bufptr++; found_start = 1; break; - } + } bufptr++; wsgi_req->frame_pos++; } @@ -478,35 +485,35 @@ get_data: wsgi_req->frame_len -= i; if (found_start) { // we have found the prefix, copy it in the frame area until suffix or end of the buffer - for(i=0;iframe_len;i++) { + for (i = 0; i < wsgi_req->frame_len; i++) { uwsgi_log("%d %d\n", bufptr[i], frame_size); if (bufptr[i] == suffix) { wsgi_req->frame_len -= i; goto return_a_frame; } - *frame_ptr++= bufptr[i]; - frame_size++; + *frame_ptr++ = bufptr[i]; + frame_size++; wsgi_req->frame_pos++; } } } - + // we have already get the prefix ? if (found_start) { // wait for more data -read_more_data: + read_more_data: rlen = uwsgi_waitfd(fd, -1); if (rlen > 0) { wsgi_req->frame_pos = 0; wsgi_req->frame_len = read(fd, wsgi_req->buffer, uwsgi.buffer_size); bufptr = wsgi_req->buffer; - for(i=0;iframe_len;i++) { + for (i = 0; i < wsgi_req->frame_len; i++) { if (bufptr[i] == suffix) { - goto return_a_frame; - } - *frame_ptr++= bufptr[i]; - frame_size++; + goto return_a_frame; + } + *frame_ptr++ = bufptr[i]; + frame_size++; } goto read_more_data; } @@ -518,54 +525,57 @@ read_more_data: else { // read a whole frame directly from the socket rlen = uwsgi_waitfd(fd, -1); - if (rlen > 0) { - wsgi_req->frame_pos = 0; - wsgi_req->frame_len = read(fd, wsgi_req->buffer, uwsgi.buffer_size); + if (rlen > 0) { + wsgi_req->frame_pos = 0; + wsgi_req->frame_len = read(fd, wsgi_req->buffer, uwsgi.buffer_size); uwsgi_log("read %d bytes %.*s\n", wsgi_req->frame_len, wsgi_req->frame_len, wsgi_req->buffer); - if (wsgi_req->frame_len == 0) goto return_a_frame; - goto get_data; - } - else if (rlen == 0) { - uwsgi_log("timeout waiting for frame\n"); - } + if (wsgi_req->frame_len == 0) + goto return_a_frame; + goto get_data; + } + else if (rlen == 0) { + uwsgi_log("timeout waiting for frame\n"); + } } -return_a_frame: + return_a_frame: uwsgi_log("returning a frame\n"); return PyString_FromStringAndSize(frame, frame_size); - + } PyObject *py_uwsgi_recv_block(PyObject * self, PyObject * args) { - + char buf[4096]; char *bufptr; - ssize_t rlen = 0, len ; + ssize_t rlen = 0, len; int fd, size, remains, ret, timeout = -1; - + if (!PyArg_ParseTuple(args, "ii|i:recv_block", &fd, &size, &timeout)) { - return NULL; - } + return NULL; + } - if (fd < 0) goto clear; + if (fd < 0) + goto clear; UWSGI_RELEASE_GIL - // security check - if (size > 4096) size = 4096; - + // security check + if (size > 4096) + size = 4096; + remains = size; bufptr = buf; - while(remains > 0) { + while (remains > 0) { uwsgi_log("%d %d %d\n", remains, size, timeout); ret = uwsgi_waitfd(fd, timeout); if (ret > 0) { - len = read(fd, bufptr, UMIN(remains, size)) ; + len = read(fd, bufptr, UMIN(remains, size)); if (len > 0) { - bufptr+=len; + bufptr += len; rlen += len; - remains -= len; + remains -= len; } else { break; @@ -577,38 +587,35 @@ PyObject *py_uwsgi_recv_block(PyObject * self, PyObject * args) { } } - UWSGI_GET_GIL - - if ( rlen == size) { + UWSGI_GET_GIL if (rlen == size) { return PyString_FromStringAndSize(buf, rlen); } -clear: + clear: Py_INCREF(Py_None); return Py_None; } PyObject *py_uwsgi_recv(PyObject * self, PyObject * args) { - + int fd, max_size = 4096; char buf[4096]; - ssize_t rlen ; - + ssize_t rlen; + if (!PyArg_ParseTuple(args, "i|i:recv", &fd, &max_size)) { - return NULL; - } + return NULL; + } UWSGI_RELEASE_GIL - // security check - if (max_size > 4096) max_size = 4096; + // security check + if (max_size > 4096) + max_size = 4096; - rlen = read(fd, buf, max_size) ; + rlen = read(fd, buf, max_size); - UWSGI_GET_GIL - - if ( rlen > 0) { + UWSGI_GET_GIL if (rlen > 0) { return PyString_FromStringAndSize(buf, rlen); } @@ -619,23 +626,24 @@ PyObject *py_uwsgi_recv(PyObject * self, PyObject * args) { PyObject *py_uwsgi_is_connected(PyObject * self, PyObject * args) { int fd, soopt; - socklen_t solen = sizeof(int); + socklen_t solen = sizeof(int); if (!PyArg_ParseTuple(args, "i:is_connected", &fd)) { - return NULL; - } + return NULL; + } - if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) { - uwsgi_error("getsockopt()"); + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) { + uwsgi_error("getsockopt()"); + goto clear; + } + /* is something bad ? */ + if (soopt) goto clear; - } - /* is something bad ? */ - if (soopt) goto clear; Py_INCREF(Py_True); return Py_True; -clear: + clear: Py_INCREF(Py_None); return Py_None; @@ -647,11 +655,11 @@ PyObject *py_uwsgi_send(PyObject * self, PyObject * args) { PyObject *data; PyObject *arg1, *arg2; - int uwsgi_fd = uwsgi.wsgi_req->poll.fd ; + int uwsgi_fd = uwsgi.wsgi_req->poll.fd; if (!PyArg_ParseTuple(args, "O|O:send", &arg1, &arg2)) { - return NULL; - } + return NULL; + } if (PyTuple_Size(args) > 1) { uwsgi_fd = PyInt_AsLong(arg1); @@ -661,17 +669,13 @@ PyObject *py_uwsgi_send(PyObject * self, PyObject * args) { data = arg1; } - UWSGI_RELEASE_GIL - - if (write(uwsgi_fd, PyString_AsString(data), PyString_Size(data)) < 0) { + UWSGI_RELEASE_GIL if (write(uwsgi_fd, PyString_AsString(data), PyString_Size(data)) < 0) { uwsgi_error("write()"); - UWSGI_GET_GIL - Py_INCREF(Py_None); + UWSGI_GET_GIL Py_INCREF(Py_None); return Py_None; } - UWSGI_GET_GIL - Py_INCREF(Py_True); + UWSGI_GET_GIL Py_INCREF(Py_True); return Py_True; } @@ -705,7 +709,8 @@ PyObject *py_uwsgi_advanced_sendfile(PyObject * self, PyObject * args) { } else { fd = PyObject_AsFileDescriptor(what); - if (fd < 0) goto clear; + if (fd < 0) + goto clear; // check for mixing file_wrapper and sendfile if (fd == uwsgi.wsgi_req->sendfile_fd) { @@ -724,9 +729,11 @@ PyObject *py_uwsgi_advanced_sendfile(PyObject * self, PyObject * args) { } - if (!filesize) goto clear2; + if (!filesize) + goto clear2; - if (!chunk) chunk = 4096; + if (!chunk) + chunk = 4096; uwsgi.wsgi_req->response_size += uwsgi_do_sendfile(uwsgi.wsgi_req->poll.fd, fd, filesize, chunk, &pos, 0); @@ -734,9 +741,9 @@ PyObject *py_uwsgi_advanced_sendfile(PyObject * self, PyObject * args) { Py_INCREF(Py_True); return Py_True; -clear2: + clear2: close(fd); -clear: + clear: Py_INCREF(Py_None); return Py_None; @@ -775,7 +782,7 @@ PyObject *py_uwsgi_warning(PyObject * self, PyObject * args) { len = strlen(message); if (len > 80) { - uwsgi_log( "- warning message must be max 80 chars, it will be truncated -"); + uwsgi_log("- warning message must be max 80 chars, it will be truncated -"); memcpy(uwsgi.shared->warning_message, message, 80); uwsgi.shared->warning_message[80] = 0; } @@ -795,7 +802,7 @@ PyObject *py_uwsgi_log(PyObject * self, PyObject * args) { return NULL; } - uwsgi_log( "%s\n", logline); + uwsgi_log("%s\n", logline); Py_INCREF(Py_True); return Py_True; @@ -807,322 +814,328 @@ PyObject *py_uwsgi_lock(PyObject * self, PyObject * args) { #ifdef UWSGI_SPOOLER if (uwsgi.numproc > 1 && uwsgi.mypid != uwsgi.workers[0].pid && uwsgi.mypid != uwsgi.shared->spooler_pid) { #else - if (uwsgi.numproc > 1 && uwsgi.mypid != uwsgi.workers[0].pid) { + if (uwsgi.numproc > 1 && uwsgi.mypid != uwsgi.workers[0].pid) { #endif - uwsgi_lock(uwsgi.user_lock); - UWSGI_SET_LOCKING; - } + uwsgi_lock(uwsgi.user_lock); + UWSGI_SET_LOCKING; + } + Py_INCREF(Py_None); + return Py_None; +} + +PyObject *py_uwsgi_unlock(PyObject * self, PyObject * args) { + + uwsgi_unlock(uwsgi.user_lock); + UWSGI_UNSET_LOCKING; + + Py_INCREF(Py_None); + return Py_None; +} + +PyObject *py_uwsgi_sharedarea_inclong(PyObject * self, PyObject * args) { + int pos = 0; + long value = 0; + + if (uwsgi.sharedareasize <= 0) { Py_INCREF(Py_None); return Py_None; } - PyObject *py_uwsgi_unlock(PyObject * self, PyObject * args) { - - uwsgi_unlock(uwsgi.user_lock); - UWSGI_UNSET_LOCKING; + if (!PyArg_ParseTuple(args, "ii:sharedarea_inclong", &pos, &value)) { + return NULL; + } + if (pos + 4 >= uwsgi.page_size * uwsgi.sharedareasize) { Py_INCREF(Py_None); return Py_None; } - PyObject *py_uwsgi_sharedarea_inclong(PyObject * self, PyObject * args) { - int pos = 0; - long value = 0; + memcpy(&value, uwsgi.sharedarea + pos, 4); + value++; + memcpy(uwsgi.sharedarea + pos, &value, 4); - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } + return PyInt_FromLong(value); - if (!PyArg_ParseTuple(args, "ii:sharedarea_inclong", &pos, &value)) { - return NULL; - } +} - if (pos + 4 >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - memcpy(&value, uwsgi.sharedarea + pos, 4); - value++; - memcpy(uwsgi.sharedarea + pos, &value, 4); - - return PyInt_FromLong(value); +PyObject *py_uwsgi_sharedarea_writelong(PyObject * self, PyObject * args) { + int pos = 0; + long value; + if (uwsgi.sharedareasize <= 0) { + Py_INCREF(Py_None); + return Py_None; } - PyObject *py_uwsgi_sharedarea_writelong(PyObject * self, PyObject * args) { - int pos = 0; - long value; - - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } - - if (!PyArg_ParseTuple(args, "ii:sharedarea_writelong", &pos, &value)) { - return NULL; - } - - if (pos + 4 >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - memcpy(uwsgi.sharedarea + pos, &value, 4); - - return PyInt_FromLong(value); - + if (!PyArg_ParseTuple(args, "ii:sharedarea_writelong", &pos, &value)) { + return NULL; } - PyObject *py_uwsgi_sharedarea_write(PyObject * self, PyObject * args) { - int pos = 0; - char *value; - - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } - - if (!PyArg_ParseTuple(args, "is:sharedarea_write", &pos, &value)) { - return NULL; - } - - if (pos + (int) strlen(value) >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - memcpy(uwsgi.sharedarea + pos, value, strlen(value)); - - return PyInt_FromLong(strlen(value)); - + if (pos + 4 >= uwsgi.page_size * uwsgi.sharedareasize) { + Py_INCREF(Py_None); + return Py_None; } - PyObject *py_uwsgi_sharedarea_writebyte(PyObject * self, PyObject * args) { - int pos = 0; - char value; + memcpy(uwsgi.sharedarea + pos, &value, 4); - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } + return PyInt_FromLong(value); +} - if (!PyArg_ParseTuple(args, "ib:sharedarea_writebyte", &pos, &value)) { - return NULL; - } - - if (pos >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - uwsgi.sharedarea[pos] = value; - - return PyInt_FromLong(uwsgi.sharedarea[pos]); +PyObject *py_uwsgi_sharedarea_write(PyObject * self, PyObject * args) { + int pos = 0; + char *value; + if (uwsgi.sharedareasize <= 0) { + Py_INCREF(Py_None); + return Py_None; } - PyObject *py_uwsgi_sharedarea_readlong(PyObject * self, PyObject * args) { - int pos = 0; - long value; + if (!PyArg_ParseTuple(args, "is:sharedarea_write", &pos, &value)) { + return NULL; + } - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } + if (pos + (int) strlen(value) >= uwsgi.page_size * uwsgi.sharedareasize) { + Py_INCREF(Py_None); + return Py_None; + } - if (!PyArg_ParseTuple(args, "i:sharedarea_readlong", &pos)) { - return NULL; - } + memcpy(uwsgi.sharedarea + pos, value, strlen(value)); - if (pos + 4 >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } + return PyInt_FromLong(strlen(value)); - memcpy(&value, uwsgi.sharedarea + pos, 4); +} - return PyInt_FromLong(value); +PyObject *py_uwsgi_sharedarea_writebyte(PyObject * self, PyObject * args) { + int pos = 0; + char value; + if (uwsgi.sharedareasize <= 0) { + Py_INCREF(Py_None); + return Py_None; } - PyObject *py_uwsgi_sharedarea_readbyte(PyObject * self, PyObject * args) { - int pos = 0; - - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } - - if (!PyArg_ParseTuple(args, "i:sharedarea_readbyte", &pos)) { - return NULL; - } - - if (pos >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - return PyInt_FromLong(uwsgi.sharedarea[pos]); - + if (!PyArg_ParseTuple(args, "ib:sharedarea_writebyte", &pos, &value)) { + return NULL; } - PyObject *py_uwsgi_sharedarea_read(PyObject * self, PyObject * args) { - int pos = 0; - int len = 1; - - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } - - if (!PyArg_ParseTuple(args, "i|i:sharedarea_read", &pos, &len)) { - return NULL; - } - - if (pos + len >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - return PyString_FromStringAndSize(uwsgi.sharedarea + pos, len); + if (pos >= uwsgi.page_size * uwsgi.sharedareasize) { + Py_INCREF(Py_None); + return Py_None; } + uwsgi.sharedarea[pos] = value; + + return PyInt_FromLong(uwsgi.sharedarea[pos]); + +} + +PyObject *py_uwsgi_sharedarea_readlong(PyObject * self, PyObject * args) { + int pos = 0; + long value; + + if (uwsgi.sharedareasize <= 0) { + Py_INCREF(Py_None); + return Py_None; + } + + if (!PyArg_ParseTuple(args, "i:sharedarea_readlong", &pos)) { + return NULL; + } + + if (pos + 4 >= uwsgi.page_size * uwsgi.sharedareasize) { + Py_INCREF(Py_None); + return Py_None; + } + + memcpy(&value, uwsgi.sharedarea + pos, 4); + + return PyInt_FromLong(value); + +} + + +PyObject *py_uwsgi_sharedarea_readbyte(PyObject * self, PyObject * args) { + int pos = 0; + + if (uwsgi.sharedareasize <= 0) { + Py_INCREF(Py_None); + return Py_None; + } + + if (!PyArg_ParseTuple(args, "i:sharedarea_readbyte", &pos)) { + return NULL; + } + + if (pos >= uwsgi.page_size * uwsgi.sharedareasize) { + Py_INCREF(Py_None); + return Py_None; + } + + return PyInt_FromLong(uwsgi.sharedarea[pos]); + +} + +PyObject *py_uwsgi_sharedarea_read(PyObject * self, PyObject * args) { + int pos = 0; + int len = 1; + + if (uwsgi.sharedareasize <= 0) { + Py_INCREF(Py_None); + return Py_None; + } + + if (!PyArg_ParseTuple(args, "i|i:sharedarea_read", &pos, &len)) { + return NULL; + } + + if (pos + len >= uwsgi.page_size * uwsgi.sharedareasize) { + Py_INCREF(Py_None); + return Py_None; + } + + return PyString_FromStringAndSize(uwsgi.sharedarea + pos, len); +} + #ifdef UWSGI_SPOOLER - PyObject *py_uwsgi_spooler_freq(PyObject * self, PyObject * args) { - - if (!PyArg_ParseTuple(args, "i", &uwsgi.shared->spooler_frequency)) { - return NULL; - } - - Py_INCREF(Py_True); - return Py_True; +PyObject *py_uwsgi_spooler_freq(PyObject * self, PyObject * args) { + if (!PyArg_ParseTuple(args, "i", &uwsgi.shared->spooler_frequency)) { + return NULL; } - PyObject *py_uwsgi_spooler_jobs(PyObject * self, PyObject * args) { + Py_INCREF(Py_True); + return Py_True; - DIR *sdir; - struct dirent *dp; - char *abs_path; - struct stat sf_lstat; +} - PyObject *jobslist = PyList_New(0); +PyObject *py_uwsgi_spooler_jobs(PyObject * self, PyObject * args) { - sdir = opendir(uwsgi.spool_dir); + DIR *sdir; + struct dirent *dp; + char *abs_path; + struct stat sf_lstat; - if (sdir) { - while ((dp = readdir(sdir)) != NULL) { - if (!strncmp("uwsgi_spoolfile_on_", dp->d_name, 19)) { - abs_path = malloc(strlen(uwsgi.spool_dir) + 1 + strlen(dp->d_name) + 1); - if (!abs_path) { - uwsgi_error("malloc()"); - closedir(sdir); - goto clear; - } + PyObject *jobslist = PyList_New(0); - memset(abs_path, 0 , strlen(uwsgi.spool_dir) + 1 + strlen(dp->d_name) + 1); + sdir = opendir(uwsgi.spool_dir); - memcpy(abs_path, uwsgi.spool_dir, strlen(uwsgi.spool_dir)); - memcpy(abs_path + strlen(uwsgi.spool_dir) , "/", 1); - memcpy(abs_path + strlen(uwsgi.spool_dir) + 1, dp->d_name, strlen(dp->d_name) ); - - - if (lstat(abs_path, &sf_lstat)) { - free(abs_path); - continue; - } - if (!S_ISREG(sf_lstat.st_mode)) { - free(abs_path); - continue; - } - if (!access(abs_path, R_OK | W_OK)) { - if (PyList_Append(jobslist, PyString_FromString(abs_path))) { - PyErr_Print(); - } - } - free(abs_path); + if (sdir) { + while ((dp = readdir(sdir)) != NULL) { + if (!strncmp("uwsgi_spoolfile_on_", dp->d_name, 19)) { + abs_path = malloc(strlen(uwsgi.spool_dir) + 1 + strlen(dp->d_name) + 1); + if (!abs_path) { + uwsgi_error("malloc()"); + closedir(sdir); + goto clear; } + + memset(abs_path, 0, strlen(uwsgi.spool_dir) + 1 + strlen(dp->d_name) + 1); + + memcpy(abs_path, uwsgi.spool_dir, strlen(uwsgi.spool_dir)); + memcpy(abs_path + strlen(uwsgi.spool_dir), "/", 1); + memcpy(abs_path + strlen(uwsgi.spool_dir) + 1, dp->d_name, strlen(dp->d_name)); + + + if (lstat(abs_path, &sf_lstat)) { + free(abs_path); + continue; + } + if (!S_ISREG(sf_lstat.st_mode)) { + free(abs_path); + continue; + } + if (!access(abs_path, R_OK | W_OK)) { + if (PyList_Append(jobslist, PyString_FromString(abs_path))) { + PyErr_Print(); + } + } + free(abs_path); } - closedir(sdir); } - -clear: - return jobslist; - + closedir(sdir); } + clear: + return jobslist; - PyObject *py_uwsgi_send_spool(PyObject * self, PyObject * args) { - PyObject *spool_dict, *spool_vars; - PyObject *zero, *key, *val; - uint16_t keysize, valsize; - char *cur_buf; - int i; - //char spool_filename[1024]; +} + + +PyObject *py_uwsgi_send_spool(PyObject * self, PyObject * args, PyObject *kw) { + PyObject *spool_dict, *spool_vars; + PyObject *zero, *key, *val; + uint16_t keysize, valsize; + char *cur_buf; + int i; + char spool_filename[1024]; + struct wsgi_request *wsgi_req = current_wsgi_req(); + + spool_dict = PyTuple_GetItem(args, 0); + + if (spool_dict) { - spool_dict = PyTuple_GetItem(args, 0); if (!PyDict_Check(spool_dict)) { - uwsgi_log("The argument of spooler callable must be a dictionary.\n"); - Py_INCREF(Py_None); - return Py_None; + return PyErr_Format(PyExc_ValueError, "The argument of spooler callable must be a dictionary"); } + } + else { + // clear the error + PyErr_Clear(); + spool_dict = kw; + } - spool_vars = PyDict_Items(spool_dict); - if (!spool_vars) { - Py_INCREF(Py_None); - return Py_None; - } + + if (!spool_dict) { + return PyErr_Format(PyExc_ValueError, "The argument of spooler callable must be a dictionary"); + } - cur_buf = spool_buffer; + spool_vars = PyDict_Items(spool_dict); + if (!spool_vars) { + Py_INCREF(Py_None); + return Py_None; + } - for (i = 0; i < PyList_Size(spool_vars); i++) { - zero = PyList_GetItem(spool_vars, i); - if (zero) { - if (PyTuple_Check(zero)) { - key = PyTuple_GetItem(zero, 0); - val = PyTuple_GetItem(zero, 1); + cur_buf = spool_buffer; - if (PyString_Check(key) && PyString_Check(val)) { + for (i = 0; i < PyList_Size(spool_vars); i++) { + zero = PyList_GetItem(spool_vars, i); + if (zero) { + if (PyTuple_Check(zero)) { + key = PyTuple_GetItem(zero, 0); + val = PyTuple_GetItem(zero, 1); + + if (PyString_Check(key) && PyString_Check(val)) { - keysize = PyString_Size(key); - valsize = PyString_Size(val); - if (cur_buf + keysize + 2 + valsize + 2 <= spool_buffer + uwsgi.buffer_size) { + keysize = PyString_Size(key); + valsize = PyString_Size(val); + if (cur_buf + keysize + 2 + valsize + 2 <= spool_buffer + uwsgi.buffer_size) { #ifdef __BIG_ENDIAN__ - keysize = uwsgi_swap16(keysize); + keysize = uwsgi_swap16(keysize); #endif - memcpy(cur_buf, &keysize, 2); - cur_buf += 2; + memcpy(cur_buf, &keysize, 2); + cur_buf += 2; #ifdef __BIG_ENDIAN__ - keysize = uwsgi_swap16(keysize); + keysize = uwsgi_swap16(keysize); #endif - memcpy(cur_buf, PyString_AsString(key), keysize); - cur_buf += keysize; + memcpy(cur_buf, PyString_AsString(key), keysize); + cur_buf += keysize; #ifdef __BIG_ENDIAN__ - valsize = uwsgi_swap16(valsize); + valsize = uwsgi_swap16(valsize); #endif - memcpy(cur_buf, &valsize, 2); - cur_buf += 2; + memcpy(cur_buf, &valsize, 2); + cur_buf += 2; #ifdef __BIG_ENDIAN__ - valsize = uwsgi_swap16(valsize); + valsize = uwsgi_swap16(valsize); #endif - memcpy(cur_buf, PyString_AsString(val), valsize); - cur_buf += valsize; - } - else { - Py_DECREF(zero); - Py_INCREF(Py_None); - return Py_None; - } + memcpy(cur_buf, PyString_AsString(val), valsize); + cur_buf += valsize; } else { - uwsgi_log("spooler callable dictionary must contains only strings.\n"); Py_DECREF(zero); Py_INCREF(Py_None); return Py_None; @@ -1130,431 +1143,432 @@ clear: } else { Py_DECREF(zero); - Py_INCREF(Py_None); - return Py_None; + return PyErr_Format(PyExc_ValueError, "spooler callable dictionary must contains only strings"); } } else { + Py_DECREF(zero); Py_INCREF(Py_None); return Py_None; } } - - //i = spool_request(spool_filename, uwsgi.workers[0].requests + 1, spool_buffer, cur_buf - spool_buffer); - if (i > 0) { - return Py_True; + else { + Py_INCREF(Py_None); + return Py_None; } + } - Py_DECREF(spool_vars); + i = spool_request(spool_filename, uwsgi.workers[0].requests + 1, wsgi_req->async_id, spool_buffer, cur_buf - spool_buffer); + + Py_DECREF(spool_vars); + + if (i > 0) { + Py_INCREF(Py_True); + return Py_True; + } + Py_INCREF(Py_None); + return Py_None; +} +#endif + +PyObject *py_uwsgi_send_multi_message(PyObject * self, PyObject * args) { + + + int i; + int clen; + int pret; + int managed; + struct pollfd *multipoll; + char *buffer; + struct uwsgi_header uh; + PyObject *arg_cluster; + + PyObject *cluster_node; + + PyObject *arg_host, *arg_port, *arg_message; + + PyObject *arg_modifier1, *arg_modifier2, *arg_timeout; + + PyObject *marshalled; + PyObject *retobject; + + + arg_cluster = PyTuple_GetItem(args, 0); + if (!PyTuple_Check(arg_cluster)) { Py_INCREF(Py_None); return Py_None; } -#endif - - PyObject *py_uwsgi_send_multi_message(PyObject * self, PyObject * args) { - int i; - int clen; - int pret; - int managed; - struct pollfd *multipoll; - char *buffer; - struct uwsgi_header uh; - PyObject *arg_cluster; + arg_modifier1 = PyTuple_GetItem(args, 1); + if (!PyInt_Check(arg_modifier1)) { + Py_INCREF(Py_None); + return Py_None; + } - PyObject *cluster_node; + arg_modifier2 = PyTuple_GetItem(args, 2); + if (!PyInt_Check(arg_modifier2)) { + Py_INCREF(Py_None); + return Py_None; + } - PyObject *arg_host, *arg_port, *arg_message; - - PyObject *arg_modifier1, *arg_modifier2, *arg_timeout; - - PyObject *marshalled; - PyObject *retobject; + arg_timeout = PyTuple_GetItem(args, 3); + if (!PyInt_Check(arg_timeout)) { + Py_INCREF(Py_None); + return Py_None; + } - arg_cluster = PyTuple_GetItem(args, 0); - if (!PyTuple_Check(arg_cluster)) { - Py_INCREF(Py_None); - return Py_None; + /* iterate cluster */ + clen = PyTuple_Size(arg_cluster); + multipoll = malloc(clen * sizeof(struct pollfd)); + if (!multipoll) { + uwsgi_error("malloc"); + Py_INCREF(Py_None); + return Py_None; + } + + + buffer = malloc(uwsgi.buffer_size * clen); + if (!buffer) { + uwsgi_error("malloc"); + free(multipoll); + Py_INCREF(Py_None); + return Py_None; + } + + + for (i = 0; i < clen; i++) { + multipoll[i].events = POLLIN; + + cluster_node = PyTuple_GetItem(arg_cluster, i); + arg_host = PyTuple_GetItem(cluster_node, 0); + if (!PyString_Check(arg_host)) { + goto clear; + } + + arg_port = PyTuple_GetItem(cluster_node, 1); + if (!PyInt_Check(arg_port)) { + goto clear; + } + + arg_message = PyTuple_GetItem(cluster_node, 2); + if (!arg_message) { + goto clear; } - arg_modifier1 = PyTuple_GetItem(args, 1); - if (!PyInt_Check(arg_modifier1)) { - Py_INCREF(Py_None); - return Py_None; - } - - arg_modifier2 = PyTuple_GetItem(args, 2); - if (!PyInt_Check(arg_modifier2)) { - Py_INCREF(Py_None); - return Py_None; - } - - arg_timeout = PyTuple_GetItem(args, 3); - if (!PyInt_Check(arg_timeout)) { - Py_INCREF(Py_None); - return Py_None; - } - - - /* iterate cluster */ - clen = PyTuple_Size(arg_cluster); - multipoll = malloc(clen * sizeof(struct pollfd)); - if (!multipoll) { - uwsgi_error("malloc"); - Py_INCREF(Py_None); - return Py_None; - } - - - buffer = malloc(uwsgi.buffer_size * clen); - if (!buffer) { - uwsgi_error("malloc"); - free(multipoll); - Py_INCREF(Py_None); - return Py_None; - } - - - for (i = 0; i < clen; i++) { - multipoll[i].events = POLLIN; - - cluster_node = PyTuple_GetItem(arg_cluster, i); - arg_host = PyTuple_GetItem(cluster_node, 0); - if (!PyString_Check(arg_host)) { + switch (PyInt_AsLong(arg_modifier1)) { + case UWSGI_MODIFIER_MESSAGE_MARSHAL: + marshalled = PyMarshal_WriteObjectToString(arg_message, 1); + if (!marshalled) { + PyErr_Print(); goto clear; } - - arg_port = PyTuple_GetItem(cluster_node, 1); - if (!PyInt_Check(arg_port)) { - goto clear; - } - - arg_message = PyTuple_GetItem(cluster_node, 2); - if (!arg_message) { - goto clear; + multipoll[i].fd = uwsgi_enqueue_message(PyString_AsString(arg_host), PyInt_AsLong(arg_port), PyInt_AsLong(arg_modifier1), PyInt_AsLong(arg_modifier2), PyString_AsString(marshalled), PyString_Size(marshalled), PyInt_AsLong(arg_timeout)); + Py_DECREF(marshalled); + if (multipoll[i].fd < 0) { + goto multiclear; } + break; + } - switch (PyInt_AsLong(arg_modifier1)) { - case UWSGI_MODIFIER_MESSAGE_MARSHAL: - marshalled = PyMarshal_WriteObjectToString(arg_message, 1); - if (!marshalled) { - PyErr_Print(); - goto clear; + } + + managed = 0; + retobject = PyTuple_New(clen); + if (!retobject) { + PyErr_Print(); + goto multiclear; + } + + while (managed < clen) { + pret = poll(multipoll, clen, PyInt_AsLong(arg_timeout) * 1000); + if (pret < 0) { + uwsgi_error("poll()"); + goto megamulticlear; + } + else if (pret == 0) { + uwsgi_log("timeout on multiple send !\n"); + goto megamulticlear; + } + else { + for (i = 0; i < clen; i++) { + if (multipoll[i].revents & POLLIN) { + if (!uwsgi_parse_response(&multipoll[i], PyInt_AsLong(arg_timeout), &uh, &buffer[i])) { + goto megamulticlear; } - multipoll[i].fd = uwsgi_enqueue_message(PyString_AsString(arg_host), PyInt_AsLong(arg_port), PyInt_AsLong(arg_modifier1), PyInt_AsLong(arg_modifier2), PyString_AsString(marshalled), PyString_Size(marshalled), PyInt_AsLong(arg_timeout)); - Py_DECREF(marshalled); - if (multipoll[i].fd < 0) { - goto multiclear; - } - break; - } - - - } - - managed = 0; - retobject = PyTuple_New(clen); - if (!retobject) { - PyErr_Print(); - goto multiclear; - } - - while (managed < clen) { - pret = poll(multipoll, clen, PyInt_AsLong(arg_timeout) * 1000); - if (pret < 0) { - uwsgi_error("poll()"); - goto megamulticlear; - } - else if (pret == 0) { - uwsgi_log( "timeout on multiple send !\n"); - goto megamulticlear; - } - else { - for (i = 0; i < clen; i++) { - if (multipoll[i].revents & POLLIN) { - if (!uwsgi_parse_response(&multipoll[i], PyInt_AsLong(arg_timeout), &uh, &buffer[i])) { + else { + if (PyTuple_SetItem(retobject, i, PyMarshal_ReadObjectFromString(&buffer[i], uh.pktsize))) { + PyErr_Print(); goto megamulticlear; } - else { - if (PyTuple_SetItem(retobject, i, PyMarshal_ReadObjectFromString(&buffer[i], uh.pktsize))) { - PyErr_Print(); - goto megamulticlear; - } - close(multipoll[i].fd); - managed++; - } + close(multipoll[i].fd); + managed++; } } } } + } - return retobject; + return retobject; -megamulticlear: + megamulticlear: - Py_DECREF(retobject); + Py_DECREF(retobject); -multiclear: + multiclear: - for (i = 0; i < clen; i++) { - close(multipoll[i].fd); - } -clear: + for (i = 0; i < clen; i++) { + close(multipoll[i].fd); + } + clear: - free(multipoll); - free(buffer); + free(multipoll); + free(buffer); + Py_INCREF(Py_None); + return Py_None; + +} + + +PyObject *py_uwsgi_get_option(PyObject * self, PyObject * args) { + int opt_id; + + if (!PyArg_ParseTuple(args, "i:get_option", &opt_id)) { + return NULL; + } + + return PyInt_FromLong(uwsgi.shared->options[(uint8_t) opt_id]); +} + +PyObject *py_uwsgi_set_option(PyObject * self, PyObject * args) { + int opt_id; + int value; + + if (!PyArg_ParseTuple(args, "ii:set_option", &opt_id, &value)) { + return NULL; + } + + uwsgi.shared->options[(uint8_t) opt_id] = (uint32_t) value; + return PyInt_FromLong(value); +} + +PyObject *py_uwsgi_load_plugin(PyObject * self, PyObject * args) { + int modifier; + char *plugin_name = NULL; + char *pargs = NULL; + + if (!PyArg_ParseTuple(args, "is|s:load_plugin", &modifier, &plugin_name, &pargs)) { + return NULL; + } + + if (uwsgi_load_plugin(modifier, plugin_name, pargs, 1)) { Py_INCREF(Py_None); return Py_None; - } - - PyObject *py_uwsgi_get_option(PyObject * self, PyObject * args) { - int opt_id; - - if (!PyArg_ParseTuple(args, "i:get_option", &opt_id)) { - return NULL; - } - - return PyInt_FromLong(uwsgi.shared->options[(uint8_t) opt_id]); - } - - PyObject *py_uwsgi_set_option(PyObject * self, PyObject * args) { - int opt_id; - int value; - - if (!PyArg_ParseTuple(args, "ii:set_option", &opt_id, &value)) { - return NULL; - } - - uwsgi.shared->options[(uint8_t) opt_id] = (uint32_t) value; - return PyInt_FromLong(value); - } - - PyObject *py_uwsgi_load_plugin(PyObject * self, PyObject * args) { - int modifier; - char *plugin_name = NULL; - char *pargs = NULL; - - if (!PyArg_ParseTuple(args, "is|s:load_plugin", &modifier, &plugin_name, &pargs)) { - return NULL; - } - - if (uwsgi_load_plugin(modifier, plugin_name, pargs, 1)) { - Py_INCREF(Py_None); - return Py_None; - } - - Py_INCREF(Py_True); - return Py_True; - } + Py_INCREF(Py_True); + return Py_True; +} #ifdef UWSGI_MULTICAST - PyObject *py_uwsgi_multicast(PyObject * self, PyObject * args) { +PyObject *py_uwsgi_multicast(PyObject * self, PyObject * args) { - char *host, *message; - ssize_t ret; - - if (!PyArg_ParseTuple(args, "ss:send_multicast_message", &host, &message)) { - return NULL; - } - - ret = send_udp_message(UWSGI_MODIFIER_MULTICAST, host, message, strlen(message)); - - if (ret <= 0) { - Py_INCREF(Py_None); - return Py_None; - } - - Py_INCREF(Py_True); - return Py_True; + char *host, *message; + ssize_t ret; + if (!PyArg_ParseTuple(args, "ss:send_multicast_message", &host, &message)) { + return NULL; } -#endif - PyObject *py_uwsgi_has_hook(PyObject * self, PyObject * args) { - int modifier1; - - if (!PyArg_ParseTuple(args, "i:has_hook", &modifier1)) { - return NULL; - } - - /* - if (uwsgi.shared->hooks[modifier1] != unconfigured_hook) { - Py_INCREF(Py_True); - return Py_True; - } - */ + ret = send_udp_message(UWSGI_MODIFIER_MULTICAST, host, message, strlen(message)); + if (ret <= 0) { Py_INCREF(Py_None); return Py_None; } + Py_INCREF(Py_True); + return Py_True; + +} +#endif + +PyObject *py_uwsgi_has_hook(PyObject * self, PyObject * args) { + int modifier1; + + if (!PyArg_ParseTuple(args, "i:has_hook", &modifier1)) { + return NULL; + } + + /* + if (uwsgi.shared->hooks[modifier1] != unconfigured_hook) { + Py_INCREF(Py_True); + return Py_True; + } + */ + + Py_INCREF(Py_None); + return Py_None; +} + struct uwsgi_Iter; -typedef struct uwsgi_Iter{ - PyObject_HEAD - int fd; +typedef struct uwsgi_Iter { + PyObject_HEAD int fd; int timeout; int close; - int started ; + int started; int has_cl; uint16_t size; uint16_t sent; uint8_t modifier1; uint8_t modifier2; - PyObject* (*func)(struct uwsgi_Iter *); + PyObject *(*func) (struct uwsgi_Iter *); } uwsgi_Iter; -PyObject* uwsgi_Iter_iter(PyObject *self) { +PyObject *uwsgi_Iter_iter(PyObject * self) { Py_INCREF(self); return self; } -PyObject* py_fcgi_iterator(uwsgi_Iter *ui) { +PyObject *py_fcgi_iterator(uwsgi_Iter * ui) { uint16_t size = 0; char body[0xffff]; size = fcgi_get_record(ui->fd, body); if (size) { - return PyString_FromStringAndSize(body, size); + return PyString_FromStringAndSize(body, size); } return NULL; } -PyObject* uwsgi_Iter_next(PyObject *self) { +PyObject *uwsgi_Iter_next(PyObject * self) { int rlen; - uwsgi_Iter *ui = (uwsgi_Iter *)self; + uwsgi_Iter *ui = (uwsgi_Iter *) self; char buf[4096]; int i = 4; struct uwsgi_header uh; - char *ub = (char *) &uh ; + char *ub = (char *) &uh; PyObject *ptr; - UWSGI_RELEASE_GIL - - if (ui->func) { + UWSGI_RELEASE_GIL if (ui->func) { ptr = ui->func(ui); if (ptr) { - return ptr; + return ptr; } } else { - if (!ui->started) { - memset(&uh, 0, 4); - while(i) { - rlen = uwsgi_waitfd(ui->fd, ui->timeout); - if (rlen > 0) { - rlen = read(ui->fd, ub , i); - if (rlen <= 0) { - goto clear; + if (!ui->started) { + memset(&uh, 0, 4); + while (i) { + rlen = uwsgi_waitfd(ui->fd, ui->timeout); + if (rlen > 0) { + rlen = read(ui->fd, ub, i); + if (rlen <= 0) { + goto clear; + } + else { + i -= rlen; + ub += rlen; + } } else { - i -= rlen; - ub += rlen; + goto clear; } } + + ui->started = 1; + + if (uh.modifier1 == 'H') { + ui->size = 0; + UWSGI_GET_GIL return PyString_FromStringAndSize((char *) &uh, 4); + } else { - goto clear; + ui->has_cl = 1; + ui->size = uh.pktsize; + ui->sent = 0; + uwsgi_log("NEED TO READ %d\n", uh.pktsize); } } - ui->started = 1; + if (ui->sent >= ui->size && ui->has_cl) { + goto clear; + } - if (uh.modifier1 == 'H') { - ui->size = 0; - UWSGI_GET_GIL - return PyString_FromStringAndSize((char *) &uh, 4); + rlen = uwsgi_waitfd(ui->fd, ui->timeout); + if (rlen > 0) { + if (ui->has_cl) { + rlen = read(ui->fd, buf, UMIN((ui->size - ui->sent), 4096)); + } + else { + rlen = read(ui->fd, buf, 4096); + } + if (rlen < 0) { + uwsgi_error("read()"); + } + else if (rlen > 0) { + ui->sent += rlen; + UWSGI_GET_GIL return PyString_FromStringAndSize(buf, rlen); + } } - else { - ui->has_cl = 1; - ui->size = uh.pktsize; - ui->sent = 0; - uwsgi_log("NEED TO READ %d\n", uh.pktsize); + else if (rlen == 0) { + uwsgi_log("uwsgi request timed out waiting for response\n"); } } - if (ui->sent >= ui->size && ui->has_cl) { - goto clear; - } - - rlen = uwsgi_waitfd(ui->fd, ui->timeout); - if (rlen > 0) { - if (ui->has_cl) { - rlen = read(ui->fd, buf, UMIN((ui->size-ui->sent), 4096)); - } - else { - rlen = read(ui->fd, buf, 4096); - } - if (rlen < 0) { - uwsgi_error("read()"); - } - else if (rlen > 0) { - ui->sent+=rlen; - UWSGI_GET_GIL - return PyString_FromStringAndSize(buf, rlen); - } - } - else if (rlen == 0) { - uwsgi_log("uwsgi request timed out waiting for response\n"); - } - } - if (ui->close) { close(ui->fd); } - -clear: - UWSGI_GET_GIL - PyErr_SetNone(PyExc_StopIteration); + + clear: + UWSGI_GET_GIL PyErr_SetNone(PyExc_StopIteration); return NULL; } static PyTypeObject uwsgi_IterType = { - PyVarObject_HEAD_INIT(NULL, 0) - "uwsgi._Iter", /*tp_name*/ - sizeof(uwsgi_Iter), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - 0, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ + PyVarObject_HEAD_INIT(NULL, 0) + "uwsgi._Iter", /*tp_name */ + sizeof(uwsgi_Iter), /*tp_basicsize */ + 0, /*tp_itemsize */ + 0, /*tp_dealloc */ + 0, /*tp_print */ + 0, /*tp_getattr */ + 0, /*tp_setattr */ + 0, /*tp_compare */ + 0, /*tp_repr */ + 0, /*tp_as_number */ + 0, /*tp_as_sequence */ + 0, /*tp_as_mapping */ + 0, /*tp_hash */ + 0, /*tp_call */ + 0, /*tp_str */ + 0, /*tp_getattro */ + 0, /*tp_setattro */ + 0, /*tp_as_buffer */ #if defined(Py_TPFLAGS_HAVE_ITER) - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, #else - Py_TPFLAGS_DEFAULT, + Py_TPFLAGS_DEFAULT, #endif - "uwsgi response iterator object.", /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - uwsgi_Iter_iter, /* tp_iter: __iter__() method */ - uwsgi_Iter_next /* tp_iternext: next() method */ + "uwsgi response iterator object.", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + uwsgi_Iter_iter, /* tp_iter: __iter__() method */ + uwsgi_Iter_next /* tp_iternext: next() method */ }; @@ -1562,8 +1576,8 @@ PyObject *py_uwsgi_connect(PyObject * self, PyObject * args) { char *socket_name = NULL; if (!PyArg_ParseTuple(args, "s:connect", &socket_name)) { - return NULL; - } + return NULL; + } return PyInt_FromLong(uwsgi_connect(socket_name, 0, 0)); } @@ -1572,65 +1586,61 @@ PyObject *py_uwsgi_async_connect(PyObject * self, PyObject * args) { char *socket_name = NULL; if (!PyArg_ParseTuple(args, "s:async_connect", &socket_name)) { - return NULL; - } + return NULL; + } return PyInt_FromLong(uwsgi_connect(socket_name, 0, 1)); } PyObject *py_uwsgi_async_send_message(PyObject * self, PyObject * args) { - PyObject *pyobj = NULL, *marshalled = NULL; + PyObject *pyobj = NULL, *marshalled = NULL; int uwsgi_fd; - int modifier1 = 0; - int modifier2 = 0; + int modifier1 = 0; + int modifier2 = 0; - ssize_t ret ; + ssize_t ret; - char *encoded; - uint16_t esize = 0; + char *encoded; + uint16_t esize = 0; - if (!PyArg_ParseTuple(args, "iiiO:async_send_message", &uwsgi_fd, &modifier1, &modifier2, &pyobj)) { - return NULL; - } + if (!PyArg_ParseTuple(args, "iiiO:async_send_message", &uwsgi_fd, &modifier1, &modifier2, &pyobj)) { + return NULL; + } - if (uwsgi_fd < 0) goto clear; + if (uwsgi_fd < 0) + goto clear; - // now check for the type of object to send (fallback to marshal) - if (PyDict_Check(pyobj)) { - encoded = uwsgi_encode_pydict(pyobj, &esize); - if (esize > 0) { - UWSGI_RELEASE_GIL - ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, -1, 0, 0); - free(encoded); - } - } - else if (PyString_Check(pyobj)) { + // now check for the type of object to send (fallback to marshal) + if (PyDict_Check(pyobj)) { + encoded = uwsgi_encode_pydict(pyobj, &esize); + if (esize > 0) { + UWSGI_RELEASE_GIL ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, -1, 0, 0); + free(encoded); + } + } + else if (PyString_Check(pyobj)) { encoded = PyString_AsString(pyobj); - esize = PyString_Size(pyobj) ; - UWSGI_RELEASE_GIL - ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, -1, 0, 0); - } - else { - marshalled = PyMarshal_WriteObjectToString(pyobj, 1); - if (!marshalled) { - PyErr_Print(); - goto clear; - } - + esize = PyString_Size(pyobj); + UWSGI_RELEASE_GIL ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, -1, 0, 0); + } + else { + marshalled = PyMarshal_WriteObjectToString(pyobj, 1); + if (!marshalled) { + PyErr_Print(); + goto clear; + } + encoded = PyString_AsString(marshalled); esize = PyString_Size(marshalled); - UWSGI_RELEASE_GIL - ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, -1, 0, 0); - } + UWSGI_RELEASE_GIL ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, -1, 0, 0); + } - UWSGI_GET_GIL + UWSGI_GET_GIL clear: -clear: - - Py_INCREF(Py_None); - return Py_None; + Py_INCREF(Py_None); + return Py_None; } @@ -1648,78 +1658,80 @@ PyObject *py_uwsgi_fcgi(PyObject * self, PyObject * args) { PyObject *zero, *key, *val; if (!PyArg_ParseTuple(args, "sO|ii:fcgi", &node, &dict, &stdin_fd, &stdin_size)) { - return NULL; - } + return NULL; + } fd = uwsgi_connect(node, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0); - - if (fd < 0) goto clear2; - if (!PyDict_Check(dict)) goto clear; + if (fd < 0) + goto clear2; + + if (!PyDict_Check(dict)) + goto clear; fcgi_send_record(fd, 1, 8, FCGI_BEGIN_REQUEST); PyObject *vars = PyDict_Items(dict); - if (!vars) goto clear; - - for (i = 0; i < PyList_Size(vars); i++) { - zero = PyList_GetItem(vars, i); - if (!zero) { - PyErr_Print(); - continue; - } + if (!vars) + goto clear; + + for (i = 0; i < PyList_Size(vars); i++) { + zero = PyList_GetItem(vars, i); + if (!zero) { + PyErr_Print(); + continue; + } key = PyTuple_GetItem(zero, 0); val = PyTuple_GetItem(zero, 1); - if (!PyString_Check(key) || !PyString_Check(val)) continue; - - fcgi_send_param(fd, - PyString_AsString(key), PyString_Size(key), - PyString_AsString(val), PyString_Size(val)); + if (!PyString_Check(key) || !PyString_Check(val)) + continue; + + fcgi_send_param(fd, PyString_AsString(key), PyString_Size(key), PyString_AsString(val), PyString_Size(val)); } fcgi_send_record(fd, 4, 0, ""); if (stdin_fd > -1 && stdin_size) { - while(stdin_size) { + while (stdin_size) { len = read(stdin_fd, stdin_buf, UMIN(0xffff, stdin_size)); if (len < 0) { uwsgi_error("read()"); break; } fcgi_send_record(fd, 5, len, stdin_buf); - stdin_size-=len; + stdin_size -= len; } } fcgi_send_record(fd, 5, 0, ""); // request sent, return the iterator response - ui = PyObject_New(uwsgi_Iter, &uwsgi_IterType); - if (!ui) { - PyErr_Print(); - goto clear; - } + ui = PyObject_New(uwsgi_Iter, &uwsgi_IterType); + if (!ui) { + PyErr_Print(); + goto clear; + } - ui->fd = fd; - ui->timeout = -1; - ui->close = 1; - ui->started = 0; - ui->has_cl = 0; - ui->sent = 0; - ui->size = 0; + ui->fd = fd; + ui->timeout = -1; + ui->close = 1; + ui->started = 0; + ui->has_cl = 0; + ui->sent = 0; + ui->size = 0; ui->func = py_fcgi_iterator; - return (PyObject *) ui; - -clear: + return (PyObject *) ui; + + clear: close(fd); -clear2: + clear2: Py_INCREF(Py_None); return Py_None; - + } PyObject *py_uwsgi_send_message(PyObject * self, PyObject * args) { @@ -1732,13 +1744,13 @@ PyObject *py_uwsgi_send_message(PyObject * self, PyObject * args) { int fd = -1; int cl = 0; - ssize_t ret ; + ssize_t ret; int uwsgi_fd = -1; char *encoded; uint16_t esize = 0; int close_fd = 0; - + uwsgi_Iter *ui; if (!PyArg_ParseTuple(args, "OiiO|iii:send_message", &destination, &modifier1, &modifier2, &pyobj, &timeout, &fd, &cl)) { @@ -1754,41 +1766,38 @@ PyObject *py_uwsgi_send_message(PyObject * self, PyObject * args) { close_fd = 1; } - if (uwsgi_fd < 0) goto clear; + if (uwsgi_fd < 0) + goto clear; // now check for the type of object to send (fallback to marshal) if (PyDict_Check(pyobj)) { - encoded = uwsgi_encode_pydict(pyobj, &esize); - if (esize > 0) { - UWSGI_RELEASE_GIL - ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, fd, cl, timeout); - free(encoded); - } - } - else if (PyString_Check(pyobj)) { - encoded = PyString_AsString(pyobj); - esize = PyString_Size(pyobj) ; - UWSGI_RELEASE_GIL - ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, fd, cl, timeout); - } - else { - marshalled = PyMarshal_WriteObjectToString(pyobj, 1); - if (!marshalled) { - PyErr_Print(); - goto clear; - } + encoded = uwsgi_encode_pydict(pyobj, &esize); + if (esize > 0) { + UWSGI_RELEASE_GIL ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, fd, cl, timeout); + free(encoded); + } + } + else if (PyString_Check(pyobj)) { + encoded = PyString_AsString(pyobj); + esize = PyString_Size(pyobj); + UWSGI_RELEASE_GIL ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, fd, cl, timeout); + } + else { + marshalled = PyMarshal_WriteObjectToString(pyobj, 1); + if (!marshalled) { + PyErr_Print(); + goto clear; + } - encoded = PyString_AsString(marshalled); - esize = PyString_Size(marshalled); - UWSGI_RELEASE_GIL - ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, fd, cl, timeout); - } + encoded = PyString_AsString(marshalled); + esize = PyString_Size(marshalled); + UWSGI_RELEASE_GIL ret = uwsgi_send_message(uwsgi_fd, (uint8_t) modifier1, (uint8_t) modifier2, encoded, esize, fd, cl, timeout); + } - UWSGI_GET_GIL - - // request sent, return the iterator response - ui = PyObject_New(uwsgi_Iter, &uwsgi_IterType); + UWSGI_GET_GIL + // request sent, return the iterator response + ui = PyObject_New(uwsgi_Iter, &uwsgi_IterType); if (!ui) { PyErr_Print(); goto clear2; @@ -1802,12 +1811,13 @@ PyObject *py_uwsgi_send_message(PyObject * self, PyObject * args) { ui->sent = 0; ui->size = 0; ui->func = NULL; - + return (PyObject *) ui; -clear2: - if (close_fd) close(uwsgi_fd); -clear: + clear2: + if (close_fd) + close(uwsgi_fd); + clear: Py_INCREF(Py_None); return Py_None; @@ -1815,161 +1825,161 @@ clear: } /* uWSGI masterpid */ - PyObject *py_uwsgi_masterpid(PyObject * self, PyObject * args) { - if (uwsgi.master_process) { - return PyInt_FromLong(uwsgi.workers[0].pid); - } - return PyInt_FromLong(0); +PyObject *py_uwsgi_masterpid(PyObject * self, PyObject * args) { + if (uwsgi.master_process) { + return PyInt_FromLong(uwsgi.workers[0].pid); } + return PyInt_FromLong(0); +} /* uWSGI total_requests */ - PyObject *py_uwsgi_total_requests(PyObject * self, PyObject * args) { - return PyInt_FromLong(uwsgi.workers[0].requests); - } +PyObject *py_uwsgi_total_requests(PyObject * self, PyObject * args) { + return PyInt_FromLong(uwsgi.workers[0].requests); +} /* uWSGI workers */ - PyObject *py_uwsgi_workers(PyObject * self, PyObject * args) { +PyObject *py_uwsgi_workers(PyObject * self, PyObject * args) { - PyObject *worker_dict, *zero; - int i; - - for (i = 0; i < uwsgi.numproc; i++) { - worker_dict = PyTuple_GetItem(up.workers_tuple, i); - if (!worker_dict) { - goto clear; - } - - PyDict_Clear(worker_dict); - - zero = PyInt_FromLong(uwsgi.workers[i + 1].id); - if (PyDict_SetItemString(worker_dict, "id", zero)) { - goto clear; - } - Py_DECREF(zero); - - zero = PyInt_FromLong(uwsgi.workers[i + 1].pid); - if (PyDict_SetItemString(worker_dict, "pid", zero)) { - goto clear; - } - Py_DECREF(zero); - - zero = PyInt_FromLong(uwsgi.workers[i + 1].requests); - if (PyDict_SetItemString(worker_dict, "requests", zero)) { - goto clear; - } - Py_DECREF(zero); - - zero = PyInt_FromLong(uwsgi.workers[i + 1].exceptions); - if (PyDict_SetItemString(worker_dict, "exceptions", zero)) { - goto clear; - } - Py_DECREF(zero); - - zero = PyInt_FromLong(uwsgi.workers[i + 1].rss_size); - if (PyDict_SetItemString(worker_dict, "rss", zero)) { - goto clear; - } - Py_DECREF(zero); - - zero = PyInt_FromLong(uwsgi.workers[i + 1].vsz_size); - if (PyDict_SetItemString(worker_dict, "vsz", zero)) { - goto clear; - } - Py_DECREF(zero); - - zero = PyFloat_FromDouble(uwsgi.workers[i + 1].running_time); - if (PyDict_SetItemString(worker_dict, "running_time", zero)) { - goto clear; - } - Py_DECREF(zero); - - zero = PyLong_FromLong(uwsgi.workers[i + 1].last_spawn); - if (PyDict_SetItemString(worker_dict, "last_spawn", zero)) { - goto clear; - } - Py_DECREF(zero); - - zero = PyLong_FromLong(uwsgi.workers[i + 1].respawn_count); - if (PyDict_SetItemString(worker_dict, "respawn_count", zero)) { - goto clear; - } - Py_DECREF(zero); - - /* return a tuple of current status ! (in_request, blocking, locking, ) - - zero = PyLong_FromLong(uwsgi.workers[i+1].in_request); - if (PyDict_SetItemString(worker_dict, "in_request", zero)) { - goto clear; - } - Py_DECREF(zero); - */ + PyObject *worker_dict, *zero; + int i; + for (i = 0; i < uwsgi.numproc; i++) { + worker_dict = PyTuple_GetItem(up.workers_tuple, i); + if (!worker_dict) { + goto clear; } + PyDict_Clear(worker_dict); - Py_INCREF(up.workers_tuple); - return up.workers_tuple; + zero = PyInt_FromLong(uwsgi.workers[i + 1].id); + if (PyDict_SetItemString(worker_dict, "id", zero)) { + goto clear; + } + Py_DECREF(zero); -clear: - PyErr_Print(); - PyErr_Clear(); - Py_INCREF(Py_None); - return Py_None; + zero = PyInt_FromLong(uwsgi.workers[i + 1].pid); + if (PyDict_SetItemString(worker_dict, "pid", zero)) { + goto clear; + } + Py_DECREF(zero); + + zero = PyInt_FromLong(uwsgi.workers[i + 1].requests); + if (PyDict_SetItemString(worker_dict, "requests", zero)) { + goto clear; + } + Py_DECREF(zero); + + zero = PyInt_FromLong(uwsgi.workers[i + 1].exceptions); + if (PyDict_SetItemString(worker_dict, "exceptions", zero)) { + goto clear; + } + Py_DECREF(zero); + + zero = PyInt_FromLong(uwsgi.workers[i + 1].rss_size); + if (PyDict_SetItemString(worker_dict, "rss", zero)) { + goto clear; + } + Py_DECREF(zero); + + zero = PyInt_FromLong(uwsgi.workers[i + 1].vsz_size); + if (PyDict_SetItemString(worker_dict, "vsz", zero)) { + goto clear; + } + Py_DECREF(zero); + + zero = PyFloat_FromDouble(uwsgi.workers[i + 1].running_time); + if (PyDict_SetItemString(worker_dict, "running_time", zero)) { + goto clear; + } + Py_DECREF(zero); + + zero = PyLong_FromLong(uwsgi.workers[i + 1].last_spawn); + if (PyDict_SetItemString(worker_dict, "last_spawn", zero)) { + goto clear; + } + Py_DECREF(zero); + + zero = PyLong_FromLong(uwsgi.workers[i + 1].respawn_count); + if (PyDict_SetItemString(worker_dict, "respawn_count", zero)) { + goto clear; + } + Py_DECREF(zero); + + /* return a tuple of current status ! (in_request, blocking, locking, ) + + zero = PyLong_FromLong(uwsgi.workers[i+1].in_request); + if (PyDict_SetItemString(worker_dict, "in_request", zero)) { + goto clear; + } + Py_DECREF(zero); + */ } + + Py_INCREF(up.workers_tuple); + return up.workers_tuple; + + clear: + PyErr_Print(); + PyErr_Clear(); + Py_INCREF(Py_None); + return Py_None; + +} + /* uWSGI reload */ - PyObject *py_uwsgi_reload(PyObject * self, PyObject * args) { +PyObject *py_uwsgi_reload(PyObject * self, PyObject * args) { - if (kill(uwsgi.workers[0].pid, SIGHUP)) { - uwsgi_error("kill()"); - Py_INCREF(Py_None); - return Py_None; - } + if (kill(uwsgi.workers[0].pid, SIGHUP)) { + uwsgi_error("kill()"); + Py_INCREF(Py_None); + return Py_None; + } + Py_INCREF(Py_True); + return Py_True; +} + + /* blocking hint */ +PyObject *py_uwsgi_set_blocking(PyObject * self, PyObject * args) { + + if (uwsgi.master_process) { + uwsgi.workers[uwsgi.mywid].status |= UWSGI_STATUS_BLOCKING; Py_INCREF(Py_True); return Py_True; } - /* blocking hint */ - PyObject *py_uwsgi_set_blocking(PyObject * self, PyObject * args) { - if (uwsgi.master_process) { - uwsgi.workers[uwsgi.mywid].status |= UWSGI_STATUS_BLOCKING; - Py_INCREF(Py_True); - return Py_True; - } + Py_INCREF(Py_None); + return Py_None; +} - Py_INCREF(Py_None); - return Py_None; - } +PyObject *py_uwsgi_request_id(PyObject * self, PyObject * args) { + return PyInt_FromLong(uwsgi.workers[uwsgi.mywid].requests); +} +PyObject *py_uwsgi_worker_id(PyObject * self, PyObject * args) { + return PyInt_FromLong(uwsgi.mywid); +} - PyObject *py_uwsgi_request_id(PyObject * self, PyObject * args) { - return PyInt_FromLong(uwsgi.workers[uwsgi.mywid].requests); - } +PyObject *py_uwsgi_logsize(PyObject * self, PyObject * args) { + return PyInt_FromLong(uwsgi.shared->logsize); +} - PyObject *py_uwsgi_worker_id(PyObject * self, PyObject * args) { - return PyInt_FromLong(uwsgi.mywid); - } +PyObject *py_uwsgi_mem(PyObject * self, PyObject * args) { - PyObject *py_uwsgi_logsize(PyObject * self, PyObject * args) { - return PyInt_FromLong(uwsgi.shared->logsize); - } + PyObject *ml = PyTuple_New(2); - PyObject *py_uwsgi_mem(PyObject * self, PyObject * args) { + get_memusage(); - PyObject *ml = PyTuple_New(2); + PyTuple_SetItem(ml, 0, PyLong_FromLong(uwsgi.workers[uwsgi.mywid].rss_size)); + PyTuple_SetItem(ml, 1, PyLong_FromLong(uwsgi.workers[uwsgi.mywid].vsz_size)); - get_memusage(); + return ml; - PyTuple_SetItem(ml, 0, PyLong_FromLong(uwsgi.workers[uwsgi.mywid].rss_size)); - PyTuple_SetItem(ml, 1, PyLong_FromLong(uwsgi.workers[uwsgi.mywid].vsz_size)); - - return ml; - - } +} PyObject *py_uwsgi_cl(PyObject * self, PyObject * args) { @@ -1979,192 +1989,193 @@ PyObject *py_uwsgi_cl(PyObject * self, PyObject * args) { } - PyObject *py_uwsgi_disconnect(PyObject * self, PyObject * args) { +PyObject *py_uwsgi_disconnect(PyObject * self, PyObject * args) { - struct wsgi_request *wsgi_req = current_wsgi_req(); + struct wsgi_request *wsgi_req = current_wsgi_req(); #ifdef UWSGI_DEBUG - uwsgi_log( "disconnecting worker %d (pid :%d) from session...\n", uwsgi.mywid, uwsgi.mypid); + uwsgi_log("disconnecting worker %d (pid :%d) from session...\n", uwsgi.mywid, uwsgi.mypid); #endif - fclose(wsgi_req->async_post); - wsgi_req->fd_closed = 1; + fclose(wsgi_req->async_post); + wsgi_req->fd_closed = 1; - Py_INCREF(Py_True); - return Py_True; + Py_INCREF(Py_True); + return Py_True; +} + +PyObject *py_uwsgi_parse_file(PyObject * self, PyObject * args) { + + char *filename; + int fd; + ssize_t len; + char *buffer, *ptrbuf, *bufferend, *keybuf; + uint16_t strsize = 0, keysize = 0; + + struct uwsgi_header uh; + PyObject *zero; + + if (!PyArg_ParseTuple(args, "s:parsefile", &filename)) { + return NULL; } - PyObject *py_uwsgi_parse_file(PyObject * self, PyObject * args) { + fd = open(filename, O_RDONLY); + if (fd < 0) { + uwsgi_error("open()"); + goto clear; + } - char *filename; - int fd; - ssize_t len; - char *buffer, *ptrbuf, *bufferend, *keybuf; - uint16_t strsize = 0, keysize = 0; + len = read(fd, &uh, 4); + if (len != 4) { + uwsgi_error("read()"); + goto clear2; + } - struct uwsgi_header uh; - PyObject *zero; + buffer = malloc(uh.pktsize); + if (!buffer) { + uwsgi_error("malloc()"); + goto clear2; + } + len = read(fd, buffer, uh.pktsize); + if (len != uh.pktsize) { + uwsgi_error("read()"); + free(buffer); + goto clear2; + } - if (!PyArg_ParseTuple(args, "s:parsefile", &filename)) { - return NULL; - } + ptrbuf = buffer; + bufferend = ptrbuf + uh.pktsize; - fd = open(filename, O_RDONLY); - if (fd < 0) { - uwsgi_error("open()"); - goto clear; - } + if (!uh.modifier1 || uh.modifier1 == UWSGI_MODIFIER_SPOOL_REQUEST) { + zero = PyDict_New(); - len = read(fd, &uh, 4); - if (len != 4) { - uwsgi_error("read()"); - goto clear2; - } - - buffer = malloc(uh.pktsize); - if (!buffer) { - uwsgi_error("malloc()"); - goto clear2; - } - len = read(fd, buffer, uh.pktsize); - if (len != uh.pktsize) { - uwsgi_error("read()"); - free(buffer); - goto clear2; - } - - ptrbuf = buffer; - bufferend = ptrbuf + uh.pktsize; - - if (!uh.modifier1 || uh.modifier1 == UWSGI_MODIFIER_SPOOL_REQUEST) { - zero = PyDict_New(); - - while (ptrbuf < bufferend) { - if (ptrbuf + 2 < bufferend) { - memcpy(&strsize, ptrbuf, 2); + while (ptrbuf < bufferend) { + if (ptrbuf + 2 < bufferend) { + memcpy(&strsize, ptrbuf, 2); #ifdef __BIG_ENDIAN__ - strsize = uwsgi_swap16(strsize); + strsize = uwsgi_swap16(strsize); #endif - /* key cannot be null */ - if (!strsize) { - uwsgi_log( "uwsgi key cannot be null.\n"); - goto clear3; - } + /* key cannot be null */ + if (!strsize) { + uwsgi_log("uwsgi key cannot be null.\n"); + goto clear3; + } - ptrbuf += 2; - if (ptrbuf + strsize < bufferend) { - // var key - keybuf = ptrbuf; - keysize = strsize; - ptrbuf += strsize; - // value can be null (even at the end) so use <= - if (ptrbuf + 2 <= bufferend) { - memcpy(&strsize, ptrbuf, 2); + ptrbuf += 2; + if (ptrbuf + strsize < bufferend) { + // var key + keybuf = ptrbuf; + keysize = strsize; + ptrbuf += strsize; + // value can be null (even at the end) so use <= + if (ptrbuf + 2 <= bufferend) { + memcpy(&strsize, ptrbuf, 2); #ifdef __BIG_ENDIAN__ - strsize = uwsgi_swap16(strsize); + strsize = uwsgi_swap16(strsize); #endif - ptrbuf += 2; - if (ptrbuf + strsize <= bufferend) { - PyDict_SetItem(zero, PyString_FromStringAndSize( keybuf, keysize ), PyString_FromStringAndSize( ptrbuf, strsize )); - ptrbuf += strsize; - } - else { - goto clear3; - } + ptrbuf += 2; + if (ptrbuf + strsize <= bufferend) { + PyDict_SetItem(zero, PyString_FromStringAndSize(keybuf, keysize), PyString_FromStringAndSize(ptrbuf, strsize)); + ptrbuf += strsize; } else { goto clear3; } } - } - else { - goto clear3; + else { + goto clear3; + } } } - - return zero; - + else { + goto clear3; + } } + return zero; + + } + + goto clear; + + clear3: + Py_DECREF(zero); + clear2: + close(fd); + clear: + Py_INCREF(Py_None); + return Py_None; + +} + +PyObject *py_uwsgi_grunt(PyObject * self, PyObject * args) { + + pid_t grunt_pid; + int i; + struct wsgi_request *wsgi_req = current_wsgi_req(); + + if (uwsgi.grunt) { + uwsgi_log("spawning a grunt from worker %d (pid :%d)...\n", uwsgi.mywid, uwsgi.mypid); + } + else { + uwsgi_log("grunt support is disabled !!!\n"); goto clear; - -clear3: - Py_DECREF(zero); -clear2: - close(fd); -clear: - Py_INCREF(Py_None); - return Py_None; - } - PyObject *py_uwsgi_grunt(PyObject * self, PyObject * args) { - - pid_t grunt_pid; - int i; - struct wsgi_request *wsgi_req = current_wsgi_req(); - - if (uwsgi.grunt) { - uwsgi_log( "spawning a grunt from worker %d (pid :%d)...\n", uwsgi.mywid, uwsgi.mypid); - } - else { - uwsgi_log( "grunt support is disabled !!!\n" ); - goto clear; - } - - grunt_pid = fork(); - if (grunt_pid < 0) { - uwsgi_error("fork()"); - goto clear; - } - else if (grunt_pid == 0) { - for(i=0;iasync_post); - wsgi_req->fd_closed = 1; - -clear: - Py_INCREF(Py_None); - return Py_None; + grunt_pid = fork(); + if (grunt_pid < 0) { + uwsgi_error("fork()"); + goto clear; } + else if (grunt_pid == 0) { + for (i = 0; i < uwsgi.sockets_cnt; i++) { + close(uwsgi.sockets[i].fd); + } + // create a new session + setsid(); + // exit on SIGPIPE + signal(SIGPIPE, (void *) &end_me); + uwsgi.mywid = uwsgi.numproc + 1; + uwsgi.mypid = getpid(); + memset(&uwsgi.workers[uwsgi.mywid], 0, sizeof(struct uwsgi_worker)); + // this is pratically useless... + uwsgi.workers[uwsgi.mywid].id = uwsgi.mywid; + // this field will be overwrite after each call + uwsgi.workers[uwsgi.mywid].pid = uwsgi.mypid; + // TODO + // manage thread in grunt processes + Py_INCREF(Py_True); + return Py_True; + } + + // close connection on the worker + fclose(wsgi_req->async_post); + wsgi_req->fd_closed = 1; + + clear: + Py_INCREF(Py_None); + return Py_None; +} #ifdef UWSGI_SPOOLER - static PyMethodDef uwsgi_spooler_methods[] = { - {"send_to_spooler", py_uwsgi_send_spool, METH_VARARGS, ""}, - {"set_spooler_frequency", py_uwsgi_spooler_freq, METH_VARARGS, ""}, - {"spooler_jobs", py_uwsgi_spooler_jobs, METH_VARARGS, ""}, - {NULL, NULL}, - }; +static PyMethodDef uwsgi_spooler_methods[] = { + {"send_to_spooler", (PyCFunction) py_uwsgi_send_spool, METH_KEYWORDS, ""}, + {"spool", (PyCFunction) py_uwsgi_send_spool, METH_KEYWORDS, ""}, + {"set_spooler_frequency", py_uwsgi_spooler_freq, METH_VARARGS, ""}, + {"spooler_jobs", py_uwsgi_spooler_jobs, METH_VARARGS, ""}, + {NULL, NULL}, +}; #endif PyObject *py_uwsgi_suspend(PyObject * self, PyObject * args) { - struct wsgi_request *wsgi_req = current_wsgi_req(); + struct wsgi_request *wsgi_req = current_wsgi_req(); - uwsgi.schedule_to_main(wsgi_req); + uwsgi.schedule_to_main(wsgi_req); - Py_INCREF(Py_True); - return Py_True; + Py_INCREF(Py_True); + return Py_True; } @@ -2186,22 +2197,22 @@ PyObject *py_uwsgi_cluster_node_name(PyObject * self, PyObject * args) { char *node = NULL; if (!PyArg_ParseTuple(args, "|s:cluster_node_name", &node)) { - return NULL; - } + return NULL; + } if (node == NULL) { return PyString_FromString(uwsgi.hostname); } for (i = 0; i < MAX_CLUSTER_NODES; i++) { - ucn = &uwsgi.shared->nodes[i]; - if (ucn->name[0] != 0) { + ucn = &uwsgi.shared->nodes[i]; + if (ucn->name[0] != 0) { uwsgi_log("node_name: %s %s\n", node, ucn->name); if (!strcmp(ucn->name, node)) { return PyString_FromString(ucn->nodename); } - } - } + } + } Py_INCREF(Py_None); return Py_None; @@ -2215,13 +2226,13 @@ PyObject *py_uwsgi_cluster_nodes(PyObject * self, PyObject * args) { PyObject *clist = PyList_New(0); for (i = 0; i < MAX_CLUSTER_NODES; i++) { - ucn = &uwsgi.shared->nodes[i]; - if (ucn->name[0] != 0) { + ucn = &uwsgi.shared->nodes[i]; + if (ucn->name[0] != 0) { if (ucn->status == UWSGI_NODE_OK) { PyList_Append(clist, PyString_FromString(ucn->name)); } - } - } + } + } return clist; @@ -2229,12 +2240,14 @@ PyObject *py_uwsgi_cluster_nodes(PyObject * self, PyObject * args) { PyObject *py_uwsgi_cluster_best_node(PyObject * self, PyObject * args) { - char *node = uwsgi_cluster_best_node() ; - if (node == NULL) goto clear; - if (node[0] == 0) goto clear; + char *node = uwsgi_cluster_best_node(); + if (node == NULL) + goto clear; + if (node[0] == 0) + goto clear; return PyString_FromString(node); -clear: + clear: Py_INCREF(Py_None); return Py_None; @@ -2245,79 +2258,79 @@ clear: static PyMethodDef uwsgi_advanced_methods[] = { - {"send_message", py_uwsgi_send_message, METH_VARARGS, ""}, - {"send_multi_message", py_uwsgi_send_multi_message, METH_VARARGS, ""}, - {"reload", py_uwsgi_reload, METH_VARARGS, ""}, - {"workers", py_uwsgi_workers, METH_VARARGS, ""}, - {"masterpid", py_uwsgi_masterpid, METH_VARARGS, ""}, - {"total_requests", py_uwsgi_total_requests, METH_VARARGS, ""}, - {"getoption", py_uwsgi_get_option, METH_VARARGS, ""}, - {"get_option", py_uwsgi_get_option, METH_VARARGS, ""}, - {"setoption", py_uwsgi_set_option, METH_VARARGS, ""}, - {"set_option", py_uwsgi_set_option, METH_VARARGS, ""}, - {"sorry_i_need_to_block", py_uwsgi_set_blocking, METH_VARARGS, ""}, - {"request_id", py_uwsgi_request_id, METH_VARARGS, ""}, - {"worker_id", py_uwsgi_worker_id, METH_VARARGS, ""}, - {"log", py_uwsgi_log, METH_VARARGS, ""}, - {"log_this_request", py_uwsgi_log_this, METH_VARARGS, ""}, - {"disconnect", py_uwsgi_disconnect, METH_VARARGS, ""}, - {"grunt", py_uwsgi_grunt, METH_VARARGS, ""}, - {"load_plugin", py_uwsgi_load_plugin, METH_VARARGS, ""}, - {"lock", py_uwsgi_lock, METH_VARARGS, ""}, - {"unlock", py_uwsgi_unlock, METH_VARARGS, ""}, - {"send", py_uwsgi_send, METH_VARARGS, ""}, - {"cl", py_uwsgi_cl, METH_VARARGS, ""}, + {"send_message", py_uwsgi_send_message, METH_VARARGS, ""}, + {"send_multi_message", py_uwsgi_send_multi_message, METH_VARARGS, ""}, + {"reload", py_uwsgi_reload, METH_VARARGS, ""}, + {"workers", py_uwsgi_workers, METH_VARARGS, ""}, + {"masterpid", py_uwsgi_masterpid, METH_VARARGS, ""}, + {"total_requests", py_uwsgi_total_requests, METH_VARARGS, ""}, + {"getoption", py_uwsgi_get_option, METH_VARARGS, ""}, + {"get_option", py_uwsgi_get_option, METH_VARARGS, ""}, + {"setoption", py_uwsgi_set_option, METH_VARARGS, ""}, + {"set_option", py_uwsgi_set_option, METH_VARARGS, ""}, + {"sorry_i_need_to_block", py_uwsgi_set_blocking, METH_VARARGS, ""}, + {"request_id", py_uwsgi_request_id, METH_VARARGS, ""}, + {"worker_id", py_uwsgi_worker_id, METH_VARARGS, ""}, + {"log", py_uwsgi_log, METH_VARARGS, ""}, + {"log_this_request", py_uwsgi_log_this, METH_VARARGS, ""}, + {"disconnect", py_uwsgi_disconnect, METH_VARARGS, ""}, + {"grunt", py_uwsgi_grunt, METH_VARARGS, ""}, + {"load_plugin", py_uwsgi_load_plugin, METH_VARARGS, ""}, + {"lock", py_uwsgi_lock, METH_VARARGS, ""}, + {"unlock", py_uwsgi_unlock, METH_VARARGS, ""}, + {"send", py_uwsgi_send, METH_VARARGS, ""}, + {"cl", py_uwsgi_cl, METH_VARARGS, ""}, - {"attach_daemon", py_uwsgi_attach_daemon, METH_VARARGS, ""}, + {"attach_daemon", py_uwsgi_attach_daemon, METH_VARARGS, ""}, - {"register_signal", py_uwsgi_register_signal, METH_VARARGS, ""}, - {"signal", py_uwsgi_signal, METH_VARARGS, ""}, - {"register_file_monitor", py_uwsgi_register_file_monitor, METH_VARARGS, ""}, - {"register_timer", py_uwsgi_register_timer, METH_VARARGS, ""}, + {"register_signal", py_uwsgi_register_signal, METH_VARARGS, ""}, + {"signal", py_uwsgi_signal, METH_VARARGS, ""}, + {"register_file_monitor", py_uwsgi_register_file_monitor, METH_VARARGS, ""}, + {"register_timer", py_uwsgi_register_timer, METH_VARARGS, ""}, - {"register_rpc", py_uwsgi_register_rpc, METH_VARARGS, ""}, - {"rpc", py_uwsgi_rpc, METH_VARARGS, ""}, - {"rpc_list", py_uwsgi_rpc_list, METH_VARARGS, ""}, - {"call", py_uwsgi_call, METH_VARARGS, ""}, + {"register_rpc", py_uwsgi_register_rpc, METH_VARARGS, ""}, + {"rpc", py_uwsgi_rpc, METH_VARARGS, ""}, + {"rpc_list", py_uwsgi_rpc_list, METH_VARARGS, ""}, + {"call", py_uwsgi_call, METH_VARARGS, ""}, #ifdef UWSGI_SENDFILE - {"sendfile", py_uwsgi_advanced_sendfile, METH_VARARGS, ""}, + {"sendfile", py_uwsgi_advanced_sendfile, METH_VARARGS, ""}, #endif - {"set_warning_message", py_uwsgi_warning, METH_VARARGS, ""}, - {"mem", py_uwsgi_mem, METH_VARARGS, ""}, - {"has_hook", py_uwsgi_has_hook, METH_VARARGS, ""}, - {"logsize", py_uwsgi_logsize, METH_VARARGS, ""}, + {"set_warning_message", py_uwsgi_warning, METH_VARARGS, ""}, + {"mem", py_uwsgi_mem, METH_VARARGS, ""}, + {"has_hook", py_uwsgi_has_hook, METH_VARARGS, ""}, + {"logsize", py_uwsgi_logsize, METH_VARARGS, ""}, #ifdef UWSGI_MULTICAST - {"send_multicast_message", py_uwsgi_multicast, METH_VARARGS, ""}, - {"cluster_nodes", py_uwsgi_cluster_nodes, METH_VARARGS, ""}, - {"cluster_node_name", py_uwsgi_cluster_node_name, METH_VARARGS, ""}, - {"cluster", py_uwsgi_cluster, METH_VARARGS, ""}, - {"cluster_best_node", py_uwsgi_cluster_best_node, METH_VARARGS, ""}, + {"send_multicast_message", py_uwsgi_multicast, METH_VARARGS, ""}, + {"cluster_nodes", py_uwsgi_cluster_nodes, METH_VARARGS, ""}, + {"cluster_node_name", py_uwsgi_cluster_node_name, METH_VARARGS, ""}, + {"cluster", py_uwsgi_cluster, METH_VARARGS, ""}, + {"cluster_best_node", py_uwsgi_cluster_best_node, METH_VARARGS, ""}, #endif #ifdef UWSGI_ASYNC - {"async_sleep", py_uwsgi_async_sleep, METH_VARARGS, ""}, - {"async_connect", py_uwsgi_async_connect, METH_VARARGS, ""}, - {"async_send_message", py_uwsgi_async_send_message, METH_VARARGS, ""}, + {"async_sleep", py_uwsgi_async_sleep, METH_VARARGS, ""}, + {"async_connect", py_uwsgi_async_connect, METH_VARARGS, ""}, + {"async_send_message", py_uwsgi_async_send_message, METH_VARARGS, ""}, - {"green_schedule", py_uwsgi_suspend, METH_VARARGS, ""}, - {"suspend", py_uwsgi_suspend, METH_VARARGS, ""}, - {"wait_fd_read", py_eventfd_read, METH_VARARGS, ""}, - {"wait_fd_write", py_eventfd_write, METH_VARARGS, ""}, + {"green_schedule", py_uwsgi_suspend, METH_VARARGS, ""}, + {"suspend", py_uwsgi_suspend, METH_VARARGS, ""}, + {"wait_fd_read", py_eventfd_read, METH_VARARGS, ""}, + {"wait_fd_write", py_eventfd_write, METH_VARARGS, ""}, #endif - {"connect", py_uwsgi_connect, METH_VARARGS, ""}, - {"is_connected", py_uwsgi_is_connected, METH_VARARGS, ""}, - {"send", py_uwsgi_send, METH_VARARGS, ""}, - {"recv", py_uwsgi_recv, METH_VARARGS, ""}, - {"recv_block", py_uwsgi_recv_block, METH_VARARGS, ""}, - {"recv_frame", py_uwsgi_recv_frame, METH_VARARGS, ""}, - {"close", py_uwsgi_close, METH_VARARGS, ""}, + {"connect", py_uwsgi_connect, METH_VARARGS, ""}, + {"is_connected", py_uwsgi_is_connected, METH_VARARGS, ""}, + {"send", py_uwsgi_send, METH_VARARGS, ""}, + {"recv", py_uwsgi_recv, METH_VARARGS, ""}, + {"recv_block", py_uwsgi_recv_block, METH_VARARGS, ""}, + {"recv_frame", py_uwsgi_recv_frame, METH_VARARGS, ""}, + {"close", py_uwsgi_close, METH_VARARGS, ""}, - {"fcgi", py_uwsgi_fcgi, METH_VARARGS, ""}, - - {"parsefile", py_uwsgi_parse_file, METH_VARARGS, ""}, - //{"call_hook", py_uwsgi_call_hook, METH_VARARGS, ""}, - - {NULL, NULL}, + {"fcgi", py_uwsgi_fcgi, METH_VARARGS, ""}, + + {"parsefile", py_uwsgi_parse_file, METH_VARARGS, ""}, + //{"call_hook", py_uwsgi_call_hook, METH_VARARGS, ""}, + + {NULL, NULL}, }; @@ -2334,59 +2347,59 @@ static PyMethodDef uwsgi_sa_methods[] = { PyObject *py_uwsgi_cache_del(PyObject * self, PyObject * args) { - char *key ; - char *value ; + char *key; + char *value; - if (!PyArg_ParseTuple(args, "s:cache_del", &key, &value)) { - return NULL; - } + if (!PyArg_ParseTuple(args, "s:cache_del", &key, &value)) { + return NULL; + } - if (uwsgi_cache_del(key, strlen(key))) { - Py_INCREF(Py_None); - return Py_None; - } + if (uwsgi_cache_del(key, strlen(key))) { + Py_INCREF(Py_None); + return Py_None; + } - Py_INCREF(Py_True); - return Py_True; + Py_INCREF(Py_True); + return Py_True; } PyObject *py_uwsgi_cache_set(PyObject * self, PyObject * args) { - char *key ; - char *value ; + char *key; + char *value; Py_ssize_t vallen = 0; uint64_t expires = 0; - if (!PyArg_ParseTuple(args, "ss#|i:cache_set", &key, &value, &vallen, &expires)) { - return NULL; - } - - if (vallen > 0xffff) { - return PyErr_Format(PyExc_ValueError, "uWSGI cache items size must be < 64K, requested %d bytes", (int )vallen); + if (!PyArg_ParseTuple(args, "ss#|i:cache_set", &key, &value, &vallen, &expires)) { + return NULL; } - if (uwsgi_cache_set(key, strlen(key), value, vallen, expires)) { - Py_INCREF(Py_None); - return Py_None; - } + if (vallen > 0xffff) { + return PyErr_Format(PyExc_ValueError, "uWSGI cache items size must be < 64K, requested %d bytes", (int) vallen); + } + + if (uwsgi_cache_set(key, strlen(key), value, vallen, expires)) { + Py_INCREF(Py_None); + return Py_None; + } Py_INCREF(Py_True); - return Py_True; + return Py_True; } PyObject *py_uwsgi_cache_exists(PyObject * self, PyObject * args) { - char *key ; + char *key; if (!PyArg_ParseTuple(args, "s:cache_exists", &key)) { - return NULL; - } + return NULL; + } if (uwsgi_cache_exists(key, strlen(key))) { Py_INCREF(Py_True); @@ -2395,25 +2408,25 @@ PyObject *py_uwsgi_cache_exists(PyObject * self, PyObject * args) { Py_INCREF(Py_None); return Py_None; - + } PyObject *py_uwsgi_cache_get(PyObject * self, PyObject * args) { - char *key ; + char *key; uint16_t valsize; - char *value ; - + char *value; + if (!PyArg_ParseTuple(args, "s:cache_get", &key)) { - return NULL; - } + return NULL; + } value = uwsgi_cache_get(key, strlen(key), &valsize); - + if (value) { return PyString_FromStringAndSize(value, valsize); } - + Py_INCREF(Py_None); return Py_None; @@ -2430,40 +2443,40 @@ static PyMethodDef uwsgi_cache_methods[] = { #ifdef UWSGI_SPOOLER - void init_uwsgi_module_spooler(PyObject * current_uwsgi_module) { - PyMethodDef *uwsgi_function; - PyObject *uwsgi_module_dict; +void init_uwsgi_module_spooler(PyObject * current_uwsgi_module) { + PyMethodDef *uwsgi_function; + PyObject *uwsgi_module_dict; - uwsgi_module_dict = PyModule_GetDict(current_uwsgi_module); - if (!uwsgi_module_dict) { - uwsgi_log( "could not get uwsgi module __dict__\n"); - exit(1); - } - - spool_buffer = malloc(uwsgi.buffer_size); - if (!spool_buffer) { - uwsgi_error("malloc()"); - exit(1); - } - - - for (uwsgi_function = uwsgi_spooler_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) { - PyObject *func = PyCFunction_New(uwsgi_function, NULL); - PyDict_SetItemString(uwsgi_module_dict, uwsgi_function->ml_name, func); - Py_DECREF(func); - } + uwsgi_module_dict = PyModule_GetDict(current_uwsgi_module); + if (!uwsgi_module_dict) { + uwsgi_log("could not get uwsgi module __dict__\n"); + exit(1); } + + spool_buffer = malloc(uwsgi.buffer_size); + if (!spool_buffer) { + uwsgi_error("malloc()"); + exit(1); + } + + + for (uwsgi_function = uwsgi_spooler_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) { + PyObject *func = PyCFunction_New(uwsgi_function, NULL); + PyDict_SetItemString(uwsgi_module_dict, uwsgi_function->ml_name, func); + Py_DECREF(func); + } +} #endif void init_uwsgi_module_advanced(PyObject * current_uwsgi_module) { - PyMethodDef *uwsgi_function; - PyObject *uwsgi_module_dict; + PyMethodDef *uwsgi_function; + PyObject *uwsgi_module_dict; - uwsgi_module_dict = PyModule_GetDict(current_uwsgi_module); - if (!uwsgi_module_dict) { - uwsgi_log( "could not get uwsgi module __dict__\n"); - exit(1); - } + uwsgi_module_dict = PyModule_GetDict(current_uwsgi_module); + if (!uwsgi_module_dict) { + uwsgi_log("could not get uwsgi module __dict__\n"); + exit(1); + } uwsgi_IterType.tp_new = PyType_GenericNew; if (PyType_Ready(&uwsgi_IterType) < 0) { @@ -2479,38 +2492,38 @@ void init_uwsgi_module_advanced(PyObject * current_uwsgi_module) { } - void init_uwsgi_module_cache(PyObject * current_uwsgi_module) { - PyMethodDef *uwsgi_function; - PyObject *uwsgi_module_dict; +void init_uwsgi_module_cache(PyObject * current_uwsgi_module) { + PyMethodDef *uwsgi_function; + PyObject *uwsgi_module_dict; - uwsgi_module_dict = PyModule_GetDict(current_uwsgi_module); - if (!uwsgi_module_dict) { - uwsgi_log( "could not get uwsgi module __dict__\n"); - exit(1); - } - - for (uwsgi_function = uwsgi_cache_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) { - PyObject *func = PyCFunction_New(uwsgi_function, NULL); - PyDict_SetItemString(uwsgi_module_dict, uwsgi_function->ml_name, func); - Py_DECREF(func); - } + uwsgi_module_dict = PyModule_GetDict(current_uwsgi_module); + if (!uwsgi_module_dict) { + uwsgi_log("could not get uwsgi module __dict__\n"); + exit(1); } - void init_uwsgi_module_sharedarea(PyObject * current_uwsgi_module) { - PyMethodDef *uwsgi_function; - PyObject *uwsgi_module_dict; - - uwsgi_module_dict = PyModule_GetDict(current_uwsgi_module); - if (!uwsgi_module_dict) { - uwsgi_log( "could not get uwsgi module __dict__\n"); - exit(1); - } - - for (uwsgi_function = uwsgi_sa_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) { - PyObject *func = PyCFunction_New(uwsgi_function, NULL); - PyDict_SetItemString(uwsgi_module_dict, uwsgi_function->ml_name, func); - Py_DECREF(func); - } + for (uwsgi_function = uwsgi_cache_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) { + PyObject *func = PyCFunction_New(uwsgi_function, NULL); + PyDict_SetItemString(uwsgi_module_dict, uwsgi_function->ml_name, func); + Py_DECREF(func); } +} + +void init_uwsgi_module_sharedarea(PyObject * current_uwsgi_module) { + PyMethodDef *uwsgi_function; + PyObject *uwsgi_module_dict; + + uwsgi_module_dict = PyModule_GetDict(current_uwsgi_module); + if (!uwsgi_module_dict) { + uwsgi_log("could not get uwsgi module __dict__\n"); + exit(1); + } + + for (uwsgi_function = uwsgi_sa_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) { + PyObject *func = PyCFunction_New(uwsgi_function, NULL); + PyDict_SetItemString(uwsgi_module_dict, uwsgi_function->ml_name, func); + Py_DECREF(func); + } +} #endif diff --git a/spooler.c b/spooler.c index 8553b8f7..16d976ee 100644 --- a/spooler.c +++ b/spooler.c @@ -13,18 +13,28 @@ pid_t spooler_start() { exit(1); } else if (pid == 0) { - for(i=0;i 0) { - uwsgi_log( "spawned the uWSGI spooler on dir %s with pid %d\n", uwsgi.spool_dir, pid); + uwsgi_log("spawned the uWSGI spooler on dir %s with pid %d\n", uwsgi.spool_dir, pid); } return pid; } +void destroy_spool(char *file) { + + if (unlink(file)) { + uwsgi_error("unlink()"); + uwsgi_log("something horrible happened to the spooler. Better to kill it.\n"); + exit(1); + } + +} + int spool_request(char *filename, int rn, int core_id, char *buffer, int size) { @@ -32,15 +42,19 @@ int spool_request(char *filename, int rn, int core_id, char *buffer, int size) { int fd; struct uwsgi_header uh; + uwsgi_lock(uwsgi.spooler_lock); + gettimeofday(&tv, NULL); if (snprintf(filename, 1024, "%s/uwsgi_spoolfile_on_%s_%d_%d_%d_%llu_%llu", uwsgi.spool_dir, uwsgi.hostname, (int) getpid(), rn, core_id, (unsigned long long) tv.tv_sec, (unsigned long long) tv.tv_usec) <= 0) { + uwsgi_unlock(uwsgi.spooler_lock); return 0; } fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR); if (fd < 0) { uwsgi_error("open()"); + uwsgi_unlock(uwsgi.spooler_lock); return 0; } @@ -48,40 +62,44 @@ int spool_request(char *filename, int rn, int core_id, char *buffer, int size) { if (lockf(fd, F_LOCK, 0)) { uwsgi_error("lockf()"); #else - if (flock(fd, LOCK_EX)) { - uwsgi_error("flock()"); + if (flock(fd, LOCK_EX)) { + uwsgi_error("flock()"); #endif - close(fd); - return 0; - } - - uh.modifier1 = 17; - uh.modifier2 = 0; - uh.pktsize = (uint16_t) size; -#ifdef __BIG_ENDIAN__ - uh.pktsize = uwsgi_swap16(uh.pktsize); -#endif - - if (write(fd, &uh, 4) != 4) { - goto clear; - } - - if (write(fd, buffer, size) != size) { - goto clear; - } - - close(fd); - - uwsgi_log( "written %d bytes to spool file %s\n", size + 4, filename); - - return 1; - - -clear: - uwsgi_error("write()"); - unlink(filename); close(fd); + uwsgi_unlock(uwsgi.spooler_lock); return 0; + } + + uh.modifier1 = 17; + uh.modifier2 = 0; + uh.pktsize = (uint16_t) size; +#ifdef __BIG_ENDIAN__ + uh.pktsize = uwsgi_swap16(uh.pktsize); +#endif + + if (write(fd, &uh, 4) != 4) { + goto clear; + } + + if (write(fd, buffer, size) != size) { + goto clear; + } + + close(fd); + + uwsgi_log("written %d bytes to spool file %s\n", size + 4, filename); + + uwsgi_unlock(uwsgi.spooler_lock); + + return 1; + + + clear: + uwsgi_unlock(uwsgi.spooler_lock); + uwsgi_error("write()"); + unlink(filename); + close(fd); + return 0; } @@ -89,20 +107,17 @@ clear: void spooler() { DIR *sdir; struct dirent *dp; + int i, ret; int spool_fd; - uint16_t uwstrlen; - int rlen = 0; - int datasize; // prevent process blindly reading stdin to make mess int nullfd; struct uwsgi_header uh; - char *key; - char *val; + char spool_buf[0xffff]; if (chdir(uwsgi.spool_dir)) { uwsgi_error("chdir()"); @@ -111,11 +126,11 @@ void spooler() { // asked by Marco Beri #ifdef __HAIKU__ - uwsgi_log( "lowering spooler priority to %d\n", B_LOW_PRIORITY); - set_thread_priority(find_thread(NULL), B_LOW_PRIORITY); + uwsgi_log("lowering spooler priority to %d\n", B_LOW_PRIORITY); + set_thread_priority(find_thread(NULL), B_LOW_PRIORITY); #else - uwsgi_log( "lowering spooler priority to %d\n", PRIO_MAX); - setpriority(PRIO_PROCESS, getpid(), PRIO_MAX); + uwsgi_log("lowering spooler priority to %d\n", PRIO_MAX); + setpriority(PRIO_PROCESS, getpid(), PRIO_MAX); #endif nullfd = open("/dev/null", O_RDONLY); @@ -124,193 +139,133 @@ void spooler() { exit(1); } - if (nullfd != 0) { - dup2(nullfd, 0); - close(nullfd); - } + if (nullfd != 0) { + dup2(nullfd, 0); + close(nullfd); + } - for (;;) { + for (;;) { - sleep(uwsgi.shared->spooler_frequency); + sleep(uwsgi.shared->spooler_frequency); - sdir = opendir(uwsgi.spool_dir); - if (sdir) { - while ((dp = readdir(sdir)) != NULL) { - if (!strncmp("uwsgi_spoolfile_on_", dp->d_name, 19)) { - struct stat sf_lstat; - if (lstat(dp->d_name, &sf_lstat)) { + sdir = opendir(uwsgi.spool_dir); + if (sdir) { + while ((dp = readdir(sdir)) != NULL) { + if (!strncmp("uwsgi_spoolfile_on_", dp->d_name, 19)) { + struct stat sf_lstat; + if (lstat(dp->d_name, &sf_lstat)) { + continue; + } + if (!S_ISREG(sf_lstat.st_mode)) { + continue; + } + if (!access(dp->d_name, R_OK | W_OK)) { + uwsgi_log("managing spool request %s ...\n", dp->d_name); + + spool_fd = open(dp->d_name, O_RDONLY); + if (spool_fd < 0) { + uwsgi_error("open()"); continue; } - if (!S_ISREG(sf_lstat.st_mode)) { - continue; - } - if (!access(dp->d_name, R_OK | W_OK)) { - uwsgi_log( "managing spool request %s ...\n", dp->d_name); - - spool_fd = open(dp->d_name, O_RDONLY); - if (spool_fd < 0) { - uwsgi_error("open()"); - continue; - } #ifdef __sun__ - if (lockf(spool_fd, F_LOCK, 0)) { - uwsgi_error("lockf()"); + if (lockf(spool_fd, F_LOCK, 0)) { + uwsgi_error("lockf()"); #else - if (flock(spool_fd, LOCK_EX)) { - uwsgi_error("flock()"); + if (flock(spool_fd, LOCK_EX)) { + uwsgi_error("flock()"); #endif - close(spool_fd); - continue; - } + close(spool_fd); + continue; + } - if (read(spool_fd, &uh, 4) != 4) { - uwsgi_error("read()"); - close(spool_fd); - continue; - } + if (read(spool_fd, &uh, 4) != 4) { + uwsgi_error("read()"); + close(spool_fd); + continue; + } #ifdef __BIG_ENDIAN__ - uh.pktsize = uwsgi_swap16(uh.pktsize); + uh.pktsize = uwsgi_swap16(uh.pktsize); #endif - datasize = 0; - while (datasize < uh.pktsize) { - rlen = read(spool_fd, &uwstrlen, 2); - if (rlen != 2) { - uwsgi_error("read()"); - goto next_spool; - } - datasize += rlen; - key = NULL; - val = NULL; - if (uwstrlen > 0) { - key = malloc(uwstrlen + 1); - if (!key) { - uwsgi_error("malloc()"); - goto retry_later; - } - rlen = read(spool_fd, key, uwstrlen); - if (rlen != uwstrlen) { - uwsgi_error("read()"); - free(key); - goto next_spool; - } - datasize += rlen; - key[rlen] = 0; + if (read(spool_fd, spool_buf, uh.pktsize) != uh.pktsize) { + uwsgi_error("read()"); + destroy_spool(dp->d_name); + close(spool_fd); + continue; + } + + close(spool_fd); - rlen = read(spool_fd, &uwstrlen, 2); - if (rlen != 2) { - uwsgi_error("read()"); - free(key); - goto next_spool; - } - datasize += rlen; + for(i=0;i<0xff;i++) { + if (uwsgi.p[i]->spooler) { + ret = uwsgi.p[i]->spooler(spool_buf, uh.pktsize); + if (ret == 0) continue; + if (ret == -2) { - if (uwstrlen > 0) { - val = malloc(uwstrlen + 1); - if (!val) { - free(key); - uwsgi_error("malloc()"); - goto retry_later; - } - - rlen = read(spool_fd, val, uwstrlen); - if (rlen != uwstrlen) { - uwsgi_error("read()"); - free(key); - goto next_spool; - } - datasize += rlen; - val[rlen] = 0; - } - - /* - if (PyDict_SetItemString(spool_env, key, PyString_FromStringAndSize(val, uwstrlen))) { - PyErr_Print(); - free(key); - free(val); - goto retry_later; - } - */ - - free(key); - free(val); - } - else { - break; - } + uwsgi_log("done with task/spool %s\n", dp->d_name); + destroy_spool(dp->d_name); } - - - /* - pass request to plugins - */ - - uwsgi_log( "done with task/spool %s\n", dp->d_name); -next_spool: - - if (unlink(dp->d_name)) { - uwsgi_error("unlink"); - uwsgi_log( "something horrible happened to the spooler. Better to kill it.\n"); - exit(1); - } -retry_later: - close(spool_fd); + // re-spool it + break; } } - } - closedir(sdir); - } - else { - uwsgi_error("opendir()"); - } + } + } } + closedir(sdir); } + else { + uwsgi_error("opendir()"); + } + + } +} int uwsgi_request_spooler(struct wsgi_request *wsgi_req) { - int i; - char spool_filename[1024]; + int i; + char spool_filename[1024]; - if (uwsgi.spool_dir == NULL) { - uwsgi_log( "the spooler is inactive !!!...skip\n"); - uwsgi_send_empty_pkt(wsgi_req->poll.fd, NULL, 255, 0); - return -1; - } + if (uwsgi.spool_dir == NULL) { + uwsgi_log("the spooler is inactive !!!...skip\n"); + uwsgi_send_empty_pkt(wsgi_req->poll.fd, NULL, 255, 0); + return -1; + } - uwsgi_log( "managing spool request...\n"); - i = spool_request(spool_filename, uwsgi.workers[0].requests + 1, wsgi_req->buffer, wsgi_req->uh.pktsize); - wsgi_req->uh.modifier1 = 255; - wsgi_req->uh.pktsize = 0; - if (i > 0) { - wsgi_req->uh.modifier2 = 1; - if (write(wsgi_req->poll.fd, wsgi_req, 4) != 4) { - uwsgi_log( "disconnected client, remove spool file.\n"); - /* client disconnect, remove spool file */ - if (unlink(spool_filename)) { - uwsgi_error("unlink()"); - uwsgi_log( "something horrible happened !!! check your spooler ASAP !!!\n"); - goodbye_cruel_world(); - } - } - return 0; + uwsgi_log("managing spool request...\n"); + i = spool_request(spool_filename, uwsgi.workers[0].requests + 1, wsgi_req->async_id, wsgi_req->buffer, wsgi_req->uh.pktsize); + wsgi_req->uh.modifier1 = 255; + wsgi_req->uh.pktsize = 0; + if (i > 0) { + wsgi_req->uh.modifier2 = 1; + if (write(wsgi_req->poll.fd, wsgi_req, 4) != 4) { + uwsgi_log("disconnected client, remove spool file.\n"); + /* client disconnect, remove spool file */ + if (unlink(spool_filename)) { + uwsgi_error("unlink()"); + uwsgi_log("something horrible happened !!! check your spooler ASAP !!!\n"); + goodbye_cruel_world(); } - else { - /* announce a failed spool request */ - wsgi_req->uh.modifier2 = 0; - i = write(wsgi_req->poll.fd, wsgi_req, 4); - if (i != 4) { - uwsgi_error("write()"); - } - } - - return -1; } + return 0; + } + else { + /* announce a failed spool request */ + wsgi_req->uh.modifier2 = 0; + i = write(wsgi_req->poll.fd, wsgi_req, 4); + if (i != 4) { + uwsgi_error("write()"); + } + } + + return -1; +} #else #warning "*** Spooler support is disabled ***" diff --git a/uwsgi.c b/uwsgi.c index b35cffa4..e91a9fa1 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -1077,6 +1077,12 @@ int uwsgi_start(void *v_argv) { uwsgi_lock_init(uwsgi.daemon_table_lock); } + if (uwsgi.spool_dir) { + // spooler lock + uwsgi.spooler_lock = uwsgi_mmap_shared_lock(); + uwsgi_lock_init(uwsgi.spooler_lock); + } + uwsgi.rpc_table_lock = uwsgi_mmap_shared_lock(); uwsgi_lock_init(uwsgi.rpc_table_lock); diff --git a/uwsgi.h b/uwsgi.h index 2870c271..9079f8cc 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -451,6 +451,8 @@ struct uwsgi_plugin { char* (*decode_string)(void *); int (*signal_handler)(uint8_t, void *, char *, uint8_t); + int (*spooler)(char *, uint16_t); + uint16_t (*rpc)(void *, uint8_t, char **, char *); }; @@ -936,6 +938,7 @@ struct uwsgi_server { void *fmon_table_lock; void *timer_table_lock; void *rpc_table_lock; + void *spooler_lock; void *daemon_table_lock; @@ -1148,7 +1151,7 @@ void snmp_init(void); #endif #ifdef UWSGI_SPOOLER -int spool_request(char *, int, char *, int); +int spool_request(char *, int, int, char *, int); void spooler(void); pid_t spooler_start(void); #endif