|
@@ -44,7 +44,8 @@ pid_t spawn(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
|
44
|
44
|
|
45
|
45
|
int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
|
46
|
46
|
{
|
47
|
|
- PyObject *entry_fun, *pystdout_flush, *pystderr_flush;
|
|
47
|
+ PyObject *entry_fun, *pystdout_flush, *pystderr_flush,
|
|
48
|
+ *py_setenv, *py_clrenv;
|
48
|
49
|
int count, pipe_out[2], pipe_err[2], pipe_ctl[2], err, piper_status;
|
49
|
50
|
struct sigaction act;
|
50
|
51
|
sigset_t emptyset;
|
|
@@ -74,7 +75,8 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
|
74
|
75
|
fetch_pyflush(&pystdout_flush, &pystderr_flush);
|
75
|
76
|
syslog( LOG_INFO,
|
76
|
77
|
"Worker[%d] Python started", wrk_id);
|
77
|
|
-
|
|
78
|
+
|
|
79
|
+ get_py_setenv(&py_setenv, &py_clrenv);
|
78
|
80
|
// loading module
|
79
|
81
|
entry_fun = import_entrypoint(py_entrypoint);
|
80
|
82
|
|
|
@@ -116,7 +118,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
|
116
|
118
|
//printf("Content-type: text/html\r\n\r\nHello world !\n");
|
117
|
119
|
exit(1);
|
118
|
120
|
}
|
119
|
|
- //TODO : update environ !!!
|
|
121
|
+ update_pyenv(py_setenv, py_clrenv);
|
120
|
122
|
//TODO : check if pipe_ctl lock is really needed anymore
|
121
|
123
|
close(pipe_ctl[1]);
|
122
|
124
|
PyObject_CallObject(entry_fun, NULL);
|
|
@@ -144,7 +146,7 @@ syslog(LOG_DEBUG, "PIPER UNLOCK");
|
144
|
146
|
syslog(LOG_DEBUG, "Worker[%d] request %d END [OK]",
|
145
|
147
|
wrk_id, count);
|
146
|
148
|
}
|
147
|
|
- exit(count == max_reqs ?0:42);
|
|
149
|
+ Py_Exit(count == max_reqs ?0:42);
|
148
|
150
|
}
|
149
|
151
|
|
150
|
152
|
void worker_piper(int wrk_id, int req_id, int pystdout, int pystderr,
|
|
@@ -574,6 +576,104 @@ update_python_fd_err:
|
574
|
576
|
exit(1);
|
575
|
577
|
}
|
576
|
578
|
|
|
579
|
+
|
|
580
|
+void update_pyenv(PyObject *py_setenv, PyObject *py_clrenv)
|
|
581
|
+{
|
|
582
|
+ PyObject *args, *pykey, *pyval, *ret;
|
|
583
|
+ char *key, *value, **cur;
|
|
584
|
+
|
|
585
|
+ cur = environ;
|
|
586
|
+
|
|
587
|
+ PyObject_CallObject(py_clrenv, NULL); // call os.environ.clear()
|
|
588
|
+ while(*cur)
|
|
589
|
+ {
|
|
590
|
+ //key = value = strdup(*cur);
|
|
591
|
+ key = value = *cur;
|
|
592
|
+ while(*value && *value != '=')
|
|
593
|
+ {
|
|
594
|
+ value++;
|
|
595
|
+ }
|
|
596
|
+ if(!*value)
|
|
597
|
+ {
|
|
598
|
+ syslog(LOG_WARNING, "Droping environment value without value : '%s'",
|
|
599
|
+ key);
|
|
600
|
+ cur++;
|
|
601
|
+ continue;
|
|
602
|
+ }
|
|
603
|
+ value++;
|
|
604
|
+ *(value-1) = '\0'; // dirty modification of **environ
|
|
605
|
+syslog(LOG_DEBUG, "PySetEnv '%s'='%s'", key, value);
|
|
606
|
+ pykey = PyUnicode_DecodeLocale(key, "surrogateescape");
|
|
607
|
+ if(!pykey)
|
|
608
|
+ {
|
|
609
|
+ *(value-1) = '='; // **environ restore
|
|
610
|
+ syslog(LOG_ALERT, "Unable to parse environ key string '%s'",
|
|
611
|
+ key);
|
|
612
|
+ log_expt(LOG_ALERT);
|
|
613
|
+ Py_Exit(EXIT_PYERR);
|
|
614
|
+ }
|
|
615
|
+ *(value-1) = '='; // **environ restore
|
|
616
|
+ pyval = PyUnicode_DecodeLocale(value, "surrogateescape");
|
|
617
|
+ if(!pykey)
|
|
618
|
+ {
|
|
619
|
+ syslog(LOG_ALERT, "Unable to parse environ val string '%s'",
|
|
620
|
+ value);
|
|
621
|
+ log_expt(LOG_ALERT);
|
|
622
|
+ Py_Exit(EXIT_PYERR);
|
|
623
|
+ }
|
|
624
|
+ args = Py_BuildValue("OO", pykey, pyval);
|
|
625
|
+ Py_DECREF(pyval);
|
|
626
|
+ Py_DECREF(pykey);
|
|
627
|
+ cur++;
|
|
628
|
+ ret = PyObject_CallObject(py_setenv, args);
|
|
629
|
+ if(ret)
|
|
630
|
+ {
|
|
631
|
+ Py_DECREF(ret);
|
|
632
|
+ }
|
|
633
|
+ if(PyErr_Occurred())
|
|
634
|
+ {
|
|
635
|
+ log_expt(LOG_WARNING);
|
|
636
|
+ }
|
|
637
|
+ //free(key);
|
|
638
|
+ }
|
|
639
|
+
|
|
640
|
+}
|
|
641
|
+
|
|
642
|
+void get_py_setenv(PyObject** pyenv_setitem, PyObject** pyenv_clear)
|
|
643
|
+{
|
|
644
|
+ PyObject *osmod, *pyenv;
|
|
645
|
+ osmod = PyImport_ImportModule("os");
|
|
646
|
+ if(!osmod)
|
|
647
|
+ {
|
|
648
|
+ syslog(LOG_ALERT, "Unable to import os module");
|
|
649
|
+ log_expt(LOG_ALERT);
|
|
650
|
+ Py_Exit(EXIT_PYERR);
|
|
651
|
+ }
|
|
652
|
+ pyenv = PyObject_GetAttrString(osmod, "environ");
|
|
653
|
+ if(!pyenv)
|
|
654
|
+ {
|
|
655
|
+ syslog(LOG_ALERT, "Unable to get os.environ");
|
|
656
|
+ log_expt(LOG_ALERT);
|
|
657
|
+ Py_Exit(EXIT_PYERR);
|
|
658
|
+ }
|
|
659
|
+ Py_DECREF(osmod);
|
|
660
|
+ *pyenv_setitem = PyObject_GetAttrString(pyenv, "__setitem__");
|
|
661
|
+ if(!*pyenv_setitem)
|
|
662
|
+ {
|
|
663
|
+ syslog(LOG_ALERT, "Unable to get os.environ.__setitem__");
|
|
664
|
+ log_expt(LOG_ALERT);
|
|
665
|
+ Py_Exit(EXIT_PYERR);
|
|
666
|
+ }
|
|
667
|
+ *pyenv_clear = PyObject_GetAttrString(pyenv, "clear");
|
|
668
|
+ if(!*pyenv_clear)
|
|
669
|
+ {
|
|
670
|
+ syslog(LOG_ALERT, "Unable to get os.environ.clear()");
|
|
671
|
+ log_expt(LOG_ALERT);
|
|
672
|
+ Py_Exit(EXIT_PYERR);
|
|
673
|
+ }
|
|
674
|
+ Py_DECREF(pyenv);
|
|
675
|
+}
|
|
676
|
+
|
577
|
677
|
void log_expt(int priority)
|
578
|
678
|
{
|
579
|
679
|
if(!PyErr_Occurred())
|