95
99
if (! ((m = PyImport_ImportModule(MODULENAME)))) {
96
102
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
97
"make_obcallback: could not import %s.\n", MODULENAME);
103
"make_obcallback: could not import %s.\n",
104
(!MODULENAME) ? "<null>" : MODULENAME);
109
path = PyObject_Repr(PySys_GetObject("path"));
111
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
112
"make_obcallback: Python path being used \"%s\".",
113
PyString_AsString(path));
101
if (m && ! ((obCallBack = PyObject_CallMethod(m, INITFUNC, NULL)))) {
120
if (m && ! ((obCallBack = PyObject_CallMethod(m,
121
INITFUNC, "sO", name, MpServer_FromServer(s))))) {
123
const char *mp_compile_version = MPV_STRING;
124
const char *mp_dynamic_version = "<unknown>";
102
129
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
103
"make_obcallback: could not call %s.\n", INITFUNC);
130
"make_obcallback: could not call %s.\n",
131
(!INITFUNC) ? "<null>" : INITFUNC);
136
m = PyImport_ImportModule("mod_python");
138
d = PyModule_GetDict(m);
139
o = PyDict_GetItemString(d, "version");
140
f = PyDict_GetItemString(d, "__file__");
143
mp_dynamic_version = PyString_AsString(o);
147
if (strcmp(mp_compile_version, mp_dynamic_version) != 0) {
148
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
149
"make_obcallback: mod_python version mismatch, expected '%s', found '%s'.",
150
mp_compile_version, mp_dynamic_version);
151
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
152
"make_obcallback: mod_python modules location '%s'.",
153
PyString_AsString(f));
107
162
return obCallBack;
168
* Save away the interpreter.
171
static interpreterdata *save_interpreter(const char *name, PyInterpreterState *istate)
174
interpreterdata *idata = NULL;
176
idata = (interpreterdata *)malloc(sizeof(interpreterdata));
177
idata->istate = istate;
178
/* obcallback will be created on first use */
179
idata->obcallback = NULL;
180
p = PyCObject_FromVoidPtr((void *) idata, NULL);
181
PyDict_SetItemString(interpreters, (char *)name, p);
111
188
** get_interpreter
113
190
* Get interpreter given its name.
124
201
name = MAIN_INTERPRETER;
204
apr_thread_mutex_lock(interpreters_lock);
126
206
#ifdef WITH_THREAD
127
207
PyEval_AcquireLock();
211
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, srv,
212
"python_handler: interpreters dictionary not initialised.");
133
216
p = PyDict_GetItemString(interpreters, (char *)name);
136
219
PyInterpreterState *istate = make_interpreter(name, srv);
138
idata = (interpreterdata *)malloc(sizeof(interpreterdata));
139
idata->istate = istate;
140
/* obcallback will be created on first use */
141
idata->obcallback = NULL;
142
p = PyCObject_FromVoidPtr((void *) idata, NULL);
143
PyDict_SetItemString(interpreters, (char *)name, p);
222
idata = save_interpreter(name, istate);
147
225
idata = (interpreterdata *)PyCObject_AsVoidPtr(p);
168
249
if (!idata->obcallback) {
170
idata->obcallback = make_obcallback(srv);
251
idata->obcallback = make_obcallback((char*)name,srv);
172
253
if (!idata->obcallback)
174
255
#ifdef WITH_THREAD
175
256
PyEval_ReleaseThread(tstate);
258
PyThreadState_Swap(NULL);
177
260
PyThreadState_Delete(tstate);
261
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, srv,
262
"python_handler: no interpreter callback found.");
313
407
max_clients = (((max_threads <= 0) ? 1 : max_threads) *
314
408
((max_procs <= 0) ? 1 : max_procs));
316
/* XXX On some systems the locking mechanism chosen uses valuable
410
/* On some systems the locking mechanism chosen uses valuable
317
411
system resources, notably on RH 8 it will use sysv ipc for
318
412
which Linux by default provides only 128 semaphores
319
413
system-wide, and on many other systems flock is used, which
320
results in a relatively large number of open files. So for now
321
we get by with MAX_LOCKS constant for lack of a better
414
results in a relatively large number of open files.
416
The maximum number of locks can be specified at
417
compile time using "./configure --with-max-locks value" or
418
at run time with "PythonOption mod_python.mutex_locks value".
420
If the PythonOption directive is used, it must be in a
421
server config context, otherwise it will be ignored.
324
423
The optimal number of necessary locks is not clear, perhaps a
325
424
small number is more than sufficient - if someone took the
326
425
time to run some research on this, that'd be most welcome!
328
locks = (max_clients > MAX_LOCKS) ? MAX_LOCKS : max_clients;
427
val = apr_table_get(conf->options, "mod_python.mutex_locks");
434
locks = (max_clients > locks) ? locks : max_clients;
330
436
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
331
437
"mod_python: Creating %d session mutexes based "
337
443
glb->nlocks = locks;
338
444
glb->parent_pid = getpid();
446
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
447
/* On some sytems a directory for the mutex lock files is required.
448
This mutex directory can be specifed at compile time using
449
"./configure --with-mutex-dir value" or at run time with
450
"PythonOption mod_python.mutex_directory value".
452
If the PythonOption directive is used, it must be in a
453
server config context, otherwise it will be ignored.
455
XXX Should we check if mutex_dir exists and has the correct
458
mutex_dir = apr_table_get(conf->options, "mod_python.mutex_directory");
460
mutex_dir = MUTEX_DIR;
462
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
463
"mod_python: using mutex_directory %s ",
340
467
for (n=0; n<locks; n++) {
342
469
apr_global_mutex_t **mutex = glb->g_locks;
344
471
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
347
snprintf(fname, 255, "/tmp/mpmtx%d%d", glb->parent_pid, n);
473
/* XXX What happens if len(mutex_dir) > 255 - len(mpmtx%d%d)? */
474
snprintf(fname, 255, "%s/mpmtx%d%d", mutex_dir, glb->parent_pid, n);
349
476
char *fname = NULL;
355
482
"mod_python: Failed to create global mutex %d of %d (%s).",
356
483
n, locks, (!fname) ? "<null>" : fname);
358
/* we were able to crate at least two, so lets just print a
485
/* we were able to create at least two, so lets just print a
359
486
warning/hint and proceed
361
488
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
531
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
532
/* Determine the directory to use for mutex lock files.
533
See init_mutexes function for more details.
538
conf = (py_config *) ap_get_module_config(s->module_config,
540
mutex_dir = apr_table_get(conf->options, "mod_python.mutex_directory");
542
mutex_dir = MUTEX_DIR;
404
545
for (n=0; n< glb->nlocks; n++) {
406
547
apr_global_mutex_t **mutex = glb->g_locks;
408
549
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
410
snprintf(fname, 255, "/tmp/mpmtx%d%d", glb->parent_pid, n);
551
snprintf(fname, 255, "%s/mpmtx%d%d", mutex_dir, glb->parent_pid, n);
412
553
char *fname = NULL;
469
610
const char *userdata_key = "python_init";
613
const char *py_compile_version = PY_VERSION;
614
const char *py_dynamic_version = 0;
616
/* The "initialized" flag is a fudge for Mac OS X. It
617
* addresses two issues. The first is that when an Apache
618
* "restart" is performed, Apache will unload the mod_python
619
* shared object, but not the Python framework. This means
620
* that when "python_init()" is called after the restart,
621
* the mod_python initialization will not run if only the
622
* initialized state of Python is checked, because Python
623
* is already initialized. The second problem is that for
624
* some older revisions of Mac OS X, even on the initial
625
* startup of Apache, the "Py_IsInitialized()" function
626
* would return true and mod_python wouldn't initialize
627
* itself correctly and would crash.
629
static int initialized = 0;
472
631
apr_pool_userdata_get(&data, userdata_key, s->process->pool);
474
633
apr_pool_userdata_set((const void *)1, userdata_key,
479
638
/* mod_python version */
480
639
ap_add_version_component(p, VERSION_COMPONENT);
641
py_dynamic_version = strtok((char *)Py_GetVersion(), " ");
643
if (strcmp(py_compile_version, py_dynamic_version) != 0) {
644
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
645
"python_init: Python version mismatch, expected '%s', found '%s'.",
646
py_compile_version, py_dynamic_version);
647
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
648
"python_init: Python executable found '%s'.",
649
Py_GetProgramFullPath());
650
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
651
"python_init: Python path being used '%s'.",
482
655
/* Python version */
483
sprintf(buff, "Python/%.200s", strtok((char *)Py_GetVersion(), " "));
656
sprintf(buff, "Python/%.200s", py_dynamic_version);
484
657
ap_add_version_component(p, buff);
486
659
/* global config */
492
665
/* initialize global Python interpreter if necessary */
493
if (! Py_IsInitialized())
666
if (initialized == 0 || !Py_IsInitialized())
496
670
/* initialze the interpreter */
674
apr_thread_mutex_create(&interpreters_lock, APR_THREAD_MUTEX_UNNESTED, p);
499
676
#ifdef WITH_THREAD
500
677
/* create and acquire the interpreter lock */
501
678
PyEval_InitThreads();
503
/* Release the thread state because we will never use
504
* the main interpreter, only sub interpreters created later. */
505
PyThreadState_Swap(NULL);
507
681
/* create the obCallBack dictionary */
508
682
interpreters = PyDict_New();
558
731
py_config *conf = python_create_config(p);
560
733
/* make sure directory ends with a slash */
561
if (dir && (dir[strlen(dir) - 1] != SLASH))
562
conf->config_dir = apr_pstrcat(p, dir, SLASH_S, NULL);
734
if (dir && (dir[strlen(dir) - 1] != '/'))
735
conf->config_dir = apr_pstrcat(p, dir, "/", NULL);
564
737
conf->config_dir = apr_pstrdup(p, dir);
761
** modpython_table_overlap
763
* Replaces the apr_table_overlap() function using a specific pool
764
* for the resulting table.
767
static apr_table_t *modpython_table_overlap(apr_pool_t *p,
768
apr_table_t *current_table,
769
apr_table_t *new_table)
771
apr_table_t *merge = apr_table_overlay(p, current_table, new_table);
772
apr_table_compress(merge, APR_OVERLAP_TABLES_SET);
585
777
** python_merge_dir_config
589
static void *python_merge_config(apr_pool_t *p, void *current_conf,
781
static void *python_merge_config(apr_pool_t *p, void *current_conf,
593
py_config *merged_conf =
785
py_config *merged_conf =
594
786
(py_config *) apr_pcalloc(p, sizeof(py_config));
595
787
py_config *cc = (py_config *) current_conf;
596
788
py_config *nc = (py_config *) new_conf;
608
merged_conf->directives = apr_table_make(p, 4);
609
merged_conf->options = apr_table_make(p, 4);
610
800
merged_conf->hlists = apr_hash_make(p);
611
801
merged_conf->in_filters = apr_hash_make(p);
612
802
merged_conf->out_filters = apr_hash_make(p);
804
/** merge directives and options **/
805
merged_conf->directives = modpython_table_overlap(p, cc->directives,
807
merged_conf->options = modpython_table_overlap(p, cc->options,
614
810
/** copy current **/
616
811
merged_conf->authoritative = cc->authoritative;
617
812
merged_conf->config_dir = apr_pstrdup(p, cc->config_dir);
618
apr_table_overlap(merged_conf->directives, cc->directives,
619
APR_OVERLAP_TABLES_SET);
620
apr_table_overlap(merged_conf->options, cc->options,
621
APR_OVERLAP_TABLES_SET);
623
814
for (hi = apr_hash_first(p, cc->hlists); hi; hi=apr_hash_next(hi)) {
624
815
apr_hash_this(hi, (const void **)&key, &klen, (void **)&hle);
642
833
if (nc->config_dir)
643
834
merged_conf->config_dir = apr_pstrdup(p, nc->config_dir);
645
apr_table_overlap(merged_conf->directives, nc->directives,
646
APR_OVERLAP_TABLES_SET);
647
apr_table_overlap(merged_conf->options, nc->options,
648
APR_OVERLAP_TABLES_SET);
650
836
for (hi = apr_hash_first(p, nc->hlists); hi; hi=apr_hash_next(hi)) {
651
837
apr_hash_this(hi, (const void**)&key, &klen, (void **)&hle);
652
838
apr_hash_set(merged_conf->hlists, key, klen, (void *)hle);
887
1083
if ((s = apr_table_get(conf->directives, "PythonInterpPerDirectory"))) {
889
1085
/* base interpreter on directory where the file is found */
890
if (req && ap_is_directory(req->pool, req->filename))
891
return ap_make_dirstr_parent(req->pool,
892
apr_pstrcat(req->pool, req->filename,
1086
if (req && ap_is_directory(req->pool, req->filename)) {
1087
/** XXX I suppose that if req->filename is a directory, there already
1088
is a trailing slash in req->filename. This is due to the fact
1089
that Apache redirect any request from /directory to /directory/.
1090
That's why the tests below are commented out, they should be useless.
1092
/* if (req->filename[strlen(req->filename)-1]=='/') { */
1093
return ap_make_dirstr_parent(req->pool, req->filename);
1096
return ap_make_dirstr_parent(req->pool,
1097
apr_pstrcat(req->pool, req->filename,
895
1102
if (req && req->filename)
896
1103
return ap_make_dirstr_parent(req->pool, req->filename);
1015
1222
/* get/create interpreter */
1016
1223
idata = get_interpreter(interp_name, req->server);
1226
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, req,
1227
"python_handler: Can't get/create interpreter.");
1019
1228
return HTTP_INTERNAL_SERVER_ERROR;
1021
1231
/* create/acquire request object */
1022
1232
request_obj = get_request_object(req, interp_name, phase);
1026
1236
request_obj->extension = apr_pstrdup(req->pool, ext);
1028
/* create a hahdler list object */
1029
request_obj->hlo = (hlistobject *)MpHList_FromHLEntry(hle);
1239
/* create a handler list object from dynamically registered handlers */
1240
request_obj->hlo = (hlistobject *)MpHList_FromHLEntry(dynhle);
1243
/* create a handler list object */
1244
request_obj->hlo = (hlistobject *)MpHList_FromHLEntry(hle);
1031
/* add dynamically registered handlers, if any */
1033
MpHList_Append(request_obj->hlo, dynhle);
1246
/* add dynamically registered handlers, if any */
1248
MpHList_Append(request_obj->hlo, dynhle);
1199
1415
/* get/create interpreter */
1200
1416
idata = get_interpreter(interp_name, con->base_server);
1419
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, con->base_server,
1420
"python_handler: Can't get/create interpreter.");
1203
1421
return HTTP_INTERNAL_SERVER_ERROR;
1205
1424
/* create connection object */
1206
1425
conn_obj = (connobject*) MpConn_FromConn(con);
1296
1515
/* get/create interpreter */
1297
1516
idata = get_interpreter(interp_name, req->server);
1519
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, req,
1520
"python_handler: Can't get/create interpreter.");
1300
1521
return HTTP_INTERNAL_SERVER_ERROR;
1302
1524
/* create/acquire request object */
1303
1525
request_obj = get_request_object(req, interp_name,
1434
1656
static const char *directive_PythonDebug(cmd_parms *cmd, void *mconfig,
1436
const char *rc = python_directive_flag(mconfig, "PythonDebug", val);
1658
const char *rc = python_directive_flag(mconfig, "PythonDebug", val, 0);
1439
1661
py_config *conf = ap_get_module_config(cmd->server->module_config,
1440
1662
&python_module);
1442
return python_directive_flag(conf, "PythonDebug", val);
1664
return python_directive_flag(conf, "PythonDebug", val, 0);
1453
1675
static const char *directive_PythonEnablePdb(cmd_parms *cmd, void *mconfig,
1455
const char *rc = python_directive_flag(mconfig, "PythonEnablePdb", val);
1677
const char *rc = python_directive_flag(mconfig, "PythonEnablePdb", val, 0);
1458
1680
py_config *conf = ap_get_module_config(cmd->server->module_config,
1459
1681
&python_module);
1460
return python_directive_flag(conf, "PythonEnablePdb", val);
1682
return python_directive_flag(conf, "PythonEnablePdb", val, 0);
1472
1694
static const char *directive_PythonInterpPerDirective(cmd_parms *cmd,
1473
1695
void *mconfig, int val) {
1474
const char *rc = python_directive_flag(mconfig, "PythonInterpPerDirective", val);
1696
const char *rc = python_directive_flag(mconfig, "PythonInterpPerDirective", val, 0);
1477
1699
py_config *conf = ap_get_module_config(cmd->server->module_config,
1478
1700
&python_module);
1479
return python_directive_flag(conf, "PythonInterpPerDirective", val);
1701
return python_directive_flag(conf, "PythonInterpPerDirective", val, 0);
1503
1725
static const char *directive_PythonAutoReload(cmd_parms *cmd,
1504
1726
void *mconfig, int val) {
1505
const char *rc = python_directive_flag(mconfig, "PythonAutoReload", val);
1727
const char *rc = python_directive_flag(mconfig, "PythonAutoReload", val, 1);
1508
1730
py_config *conf = ap_get_module_config(cmd->server->module_config,
1509
1731
&python_module);
1510
return python_directive_flag(conf, "PythonAutoReload", val);
1732
return python_directive_flag(conf, "PythonAutoReload", val, 1);
1523
1745
static const char *directive_PythonOption(cmd_parms *cmd, void * mconfig,
1524
1746
const char *key, const char *val)
1527
1748
py_config *conf;
1529
1750
conf = (py_config *) mconfig;
1530
apr_table_set(conf->options, key, val);
1532
conf = ap_get_module_config(cmd->server->module_config,
1534
apr_table_set(conf->options, key, val);
1753
apr_table_set(conf->options, key, val);
1756
conf = ap_get_module_config(cmd->server->module_config,
1758
apr_table_set(conf->options, key, val);
1762
/** We don't remove the value, but set it
1763
to an empty string. There is no possibility
1764
of colliding with an actual value, since
1765
an entry string precisely means 'remove the value' */
1766
apr_table_set(conf->options, key, "");
1769
conf = ap_get_module_config(cmd->server->module_config,
1771
apr_table_set(conf->options, key, "");
1614
1853
python_directive_handler(cmd, mconfig, "PythonLogHandler", val, SILENT);
1615
1854
python_directive_handler(cmd, mconfig, "PythonCleanupHandler", val, SILENT);
1857
* XXX There is a bug here with PythonConnectionHandler which can
1858
* cause an infinite loop when the handler is added to the handler
1859
* list. Cause is unknown so simply disable it for now. If someone
1860
* really needs a connection handler, they can use the directive
1861
* PythonConnectionHandler explicitly still and not rely on the
1862
* PythonHandlerModule directive doing it automatically.
1617
1864
python_directive_handler(cmd, srv_conf, "PythonConnectionHandler", val, SILENT);
1819
2073
/* now import the specified module */
1820
2074
if (! PyImport_ImportModule((char *)module_name)) {
1821
if (PyErr_Occurred())
2075
if (PyErr_Occurred()) {
1823
2079
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
1824
"directive_PythonImport: error importing %s", module_name);
2080
"directive_PythonImport: error importing %s", (!module_name) ? "<null>" : module_name);
1827
2083
/* release interpreter */
2013
2269
"PythonOptimize", directive_PythonOptimize, NULL, RSRC_CONF,
2014
2270
"Set the equivalent of the -O command-line flag on the interpreter."),
2016
2272
"PythonOption", directive_PythonOption, NULL, OR_ALL,
2017
2273
"Useful to pass custom configuration information to scripts."),