Enable python environnement update from FCGI context
This commit is contained in:
parent
ed745fc3ee
commit
13258ee647
3 changed files with 123 additions and 8 deletions
7
foo.py
7
foo.py
|
|
@ -3,10 +3,9 @@ import os
|
|||
import time
|
||||
|
||||
def entrypoint():
|
||||
sys.stderr.write('Called ! req by %s' % os.getenv('REQUEST_METHOD'))
|
||||
sys.stderr.flush()
|
||||
import os
|
||||
sys.stderr.write('Called ! req by %s' % os.getenv('REMOTE_ADDR'))
|
||||
env = "foo"
|
||||
env = ', '.join(os.environ.keys())
|
||||
env = '<br/>'.join(["'%s'='%s'" % (k, os.environ[k]) for k in os.environ.keys()])
|
||||
msg = "Content-Type: text/html\r\n\r\nHello world !(%0.2f)\nenv : %s" % (time.time(), env)
|
||||
sys.stdout.write(msg)
|
||||
sys.stdout.flush()
|
||||
|
|
|
|||
108
src/pyworker.c
108
src/pyworker.c
|
|
@ -44,7 +44,8 @@ pid_t spawn(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
|
|||
|
||||
int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
|
||||
{
|
||||
PyObject *entry_fun, *pystdout_flush, *pystderr_flush;
|
||||
PyObject *entry_fun, *pystdout_flush, *pystderr_flush,
|
||||
*py_setenv, *py_clrenv;
|
||||
int count, pipe_out[2], pipe_err[2], pipe_ctl[2], err, piper_status;
|
||||
struct sigaction act;
|
||||
sigset_t emptyset;
|
||||
|
|
@ -74,7 +75,8 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
|
|||
fetch_pyflush(&pystdout_flush, &pystderr_flush);
|
||||
syslog( LOG_INFO,
|
||||
"Worker[%d] Python started", wrk_id);
|
||||
|
||||
|
||||
get_py_setenv(&py_setenv, &py_clrenv);
|
||||
// loading module
|
||||
entry_fun = import_entrypoint(py_entrypoint);
|
||||
|
||||
|
|
@ -116,7 +118,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
|
|||
//printf("Content-type: text/html\r\n\r\nHello world !\n");
|
||||
exit(1);
|
||||
}
|
||||
//TODO : update environ !!!
|
||||
update_pyenv(py_setenv, py_clrenv);
|
||||
//TODO : check if pipe_ctl lock is really needed anymore
|
||||
close(pipe_ctl[1]);
|
||||
PyObject_CallObject(entry_fun, NULL);
|
||||
|
|
@ -144,7 +146,7 @@ syslog(LOG_DEBUG, "PIPER UNLOCK");
|
|||
syslog(LOG_DEBUG, "Worker[%d] request %d END [OK]",
|
||||
wrk_id, count);
|
||||
}
|
||||
exit(count == max_reqs ?0:42);
|
||||
Py_Exit(count == max_reqs ?0:42);
|
||||
}
|
||||
|
||||
void worker_piper(int wrk_id, int req_id, int pystdout, int pystderr,
|
||||
|
|
@ -574,6 +576,104 @@ update_python_fd_err:
|
|||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
void update_pyenv(PyObject *py_setenv, PyObject *py_clrenv)
|
||||
{
|
||||
PyObject *args, *pykey, *pyval, *ret;
|
||||
char *key, *value, **cur;
|
||||
|
||||
cur = environ;
|
||||
|
||||
PyObject_CallObject(py_clrenv, NULL); // call os.environ.clear()
|
||||
while(*cur)
|
||||
{
|
||||
//key = value = strdup(*cur);
|
||||
key = value = *cur;
|
||||
while(*value && *value != '=')
|
||||
{
|
||||
value++;
|
||||
}
|
||||
if(!*value)
|
||||
{
|
||||
syslog(LOG_WARNING, "Droping environment value without value : '%s'",
|
||||
key);
|
||||
cur++;
|
||||
continue;
|
||||
}
|
||||
value++;
|
||||
*(value-1) = '\0'; // dirty modification of **environ
|
||||
syslog(LOG_DEBUG, "PySetEnv '%s'='%s'", key, value);
|
||||
pykey = PyUnicode_DecodeLocale(key, "surrogateescape");
|
||||
if(!pykey)
|
||||
{
|
||||
*(value-1) = '='; // **environ restore
|
||||
syslog(LOG_ALERT, "Unable to parse environ key string '%s'",
|
||||
key);
|
||||
log_expt(LOG_ALERT);
|
||||
Py_Exit(EXIT_PYERR);
|
||||
}
|
||||
*(value-1) = '='; // **environ restore
|
||||
pyval = PyUnicode_DecodeLocale(value, "surrogateescape");
|
||||
if(!pykey)
|
||||
{
|
||||
syslog(LOG_ALERT, "Unable to parse environ val string '%s'",
|
||||
value);
|
||||
log_expt(LOG_ALERT);
|
||||
Py_Exit(EXIT_PYERR);
|
||||
}
|
||||
args = Py_BuildValue("OO", pykey, pyval);
|
||||
Py_DECREF(pyval);
|
||||
Py_DECREF(pykey);
|
||||
cur++;
|
||||
ret = PyObject_CallObject(py_setenv, args);
|
||||
if(ret)
|
||||
{
|
||||
Py_DECREF(ret);
|
||||
}
|
||||
if(PyErr_Occurred())
|
||||
{
|
||||
log_expt(LOG_WARNING);
|
||||
}
|
||||
//free(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void get_py_setenv(PyObject** pyenv_setitem, PyObject** pyenv_clear)
|
||||
{
|
||||
PyObject *osmod, *pyenv;
|
||||
osmod = PyImport_ImportModule("os");
|
||||
if(!osmod)
|
||||
{
|
||||
syslog(LOG_ALERT, "Unable to import os module");
|
||||
log_expt(LOG_ALERT);
|
||||
Py_Exit(EXIT_PYERR);
|
||||
}
|
||||
pyenv = PyObject_GetAttrString(osmod, "environ");
|
||||
if(!pyenv)
|
||||
{
|
||||
syslog(LOG_ALERT, "Unable to get os.environ");
|
||||
log_expt(LOG_ALERT);
|
||||
Py_Exit(EXIT_PYERR);
|
||||
}
|
||||
Py_DECREF(osmod);
|
||||
*pyenv_setitem = PyObject_GetAttrString(pyenv, "__setitem__");
|
||||
if(!*pyenv_setitem)
|
||||
{
|
||||
syslog(LOG_ALERT, "Unable to get os.environ.__setitem__");
|
||||
log_expt(LOG_ALERT);
|
||||
Py_Exit(EXIT_PYERR);
|
||||
}
|
||||
*pyenv_clear = PyObject_GetAttrString(pyenv, "clear");
|
||||
if(!*pyenv_clear)
|
||||
{
|
||||
syslog(LOG_ALERT, "Unable to get os.environ.clear()");
|
||||
log_expt(LOG_ALERT);
|
||||
Py_Exit(EXIT_PYERR);
|
||||
}
|
||||
Py_DECREF(pyenv);
|
||||
}
|
||||
|
||||
void log_expt(int priority)
|
||||
{
|
||||
if(!PyErr_Occurred())
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@
|
|||
#define WPIPER_SIG 30
|
||||
#define PYENTRY_FUNNAME "entrypoint"
|
||||
|
||||
extern char **environ;
|
||||
|
||||
typedef unsigned long int pywrkid_t;
|
||||
|
||||
/**@brief Spawn a worker given an entrypoint
|
||||
|
|
@ -98,6 +100,20 @@ void update_python_path();
|
|||
*/
|
||||
void update_python_fd(int[2], int[2]);
|
||||
|
||||
/**@brief Update python sys.environ using current FCI environ
|
||||
* @note For the moment do not delete unset variables only update
|
||||
* from environ and add new one
|
||||
* @param PyObject* os.environ.__setitem__
|
||||
* @param PyObject* os.environ.clean
|
||||
*/
|
||||
void update_pyenv(PyObject*, PyObject*);
|
||||
|
||||
/**@brief Fetch python os.environ.__setitem__ & os.environ.clear()
|
||||
* @param PyObject** setitem
|
||||
* @param PyObject** clear
|
||||
*/
|
||||
void get_py_setenv(PyObject**, PyObject**);
|
||||
|
||||
void log_expt(int priority);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue