/
mod_python.c
2810 lines (2309 loc) · 88.3 KB
/
mod_python.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2004 Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Originally developed by Gregory Trubetskoy.
*
*
* mod_python.c
*
* $Id$
*
* See accompanying documentation and source code comments
* for details.
*
*/
#include "mod_python.h"
/* Server object for main server as supplied to python_init(). */
static server_rec *main_server = NULL;
/* List of available Python obCallBacks/Interpreters
* (In a Python dictionary) */
static PyObject * interpreters = NULL;
#if APR_HAS_THREADS
static apr_thread_mutex_t* interpreters_lock = 0;
#endif
apr_pool_t *child_init_pool = NULL;
/* Optional functions imported from mod_include when loaded: */
static APR_OPTIONAL_FN_TYPE(ap_register_include_handler) *optfn_register_include_handler;
static APR_OPTIONAL_FN_TYPE(ap_ssi_get_tag_and_value) *optfn_ssi_get_tag_and_value;
static APR_OPTIONAL_FN_TYPE(ap_ssi_parse_string) *optfn_ssi_parse_string;
/**
** make_interpreter
**
* Creates a new Python interpreter.
*/
static PyInterpreterState *make_interpreter(const char *name, server_rec *srv)
{
PyThreadState *tstate;
/* create a new interpeter */
tstate = Py_NewInterpreter();
if (! tstate) {
/* couldn't create an interpreter, this is bad */
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, srv,
"make_interpreter: Py_NewInterpreter() returned NULL. No more memory?");
return NULL;
}
/* release the thread state */
PyThreadState_Swap(NULL);
/* Strictly speaking we don't need that tstate created
* by Py_NewInterpreter but is preferable to waste it than re-write
* a cousin to Py_NewInterpreter
* XXX (maybe we can destroy it?)
*/
return tstate->interp;
}
/**
** make_obcallback
**
* This function instantiates an obCallBack object.
* NOTE: The obCallBack object is instantiated by Python
* code. This C module calls into Python code which returns
* the reference to obCallBack.
*/
static PyObject * make_obcallback(char *name, server_rec *s)
{
PyObject *m = NULL;
PyObject *obCallBack = NULL;
/* This makes _apache appear imported, and subsequent
* >>> import _apache
* will not give an error.
*/
/* Py_InitModule("_apache", _apache_module_methods); */
init_apache();
/* Now execute the equivalent of
* >>> import <module>
* >>> <initstring>
* in the __main__ module to start up Python.
*/
if (! ((m = PyImport_ImportModule(MODULENAME)))) {
PyObject *path;
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
"make_obcallback: could not import %s.\n",
(!MODULENAME) ? "<null>" : MODULENAME);
PyErr_Print();
fflush(stderr);
path = PyObject_Repr(PySys_GetObject("path"));
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
"make_obcallback: Python path being used \"%s\".",
PyString_AsString(path));
Py_DECREF(path);
return NULL;
}
if (m && ! ((obCallBack = PyObject_CallMethod(m,
INITFUNC, "sO", name, MpServer_FromServer(main_server))))) {
const char *mp_compile_version = MPV_STRING;
const char *mp_dynamic_version = "<unknown>";
PyObject *o = NULL;
PyObject *d = NULL;
PyObject *f = NULL;
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
"make_obcallback: could not call %s.\n",
(!INITFUNC) ? "<null>" : INITFUNC);
PyErr_Print();
fflush(stderr);
m = PyImport_ImportModule("mod_python");
if (m) {
d = PyModule_GetDict(m);
o = PyDict_GetItemString(d, "version");
f = PyDict_GetItemString(d, "__file__");
if (o) {
mp_dynamic_version = PyString_AsString(o);
}
}
if (strcmp(mp_compile_version, mp_dynamic_version) != 0) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
"make_obcallback: mod_python version mismatch, expected '%s', found '%s'.",
mp_compile_version, mp_dynamic_version);
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
"make_obcallback: mod_python modules location '%s'.",
PyString_AsString(f));
}
Py_XDECREF(o);
Py_XDECREF(f);
}
Py_XDECREF(m);
return obCallBack;
}
/**
** save_interpreter
**
* Save away the interpreter.
*/
static interpreterdata *save_interpreter(const char *name, PyInterpreterState *istate)
{
PyObject *p;
interpreterdata *idata = NULL;
idata = (interpreterdata *)malloc(sizeof(interpreterdata));
idata->istate = istate;
/* obcallback will be created on first use */
idata->obcallback = NULL;
p = PyCObject_FromVoidPtr((void *) idata, NULL);
PyDict_SetItemString(interpreters, (char *)name, p);
Py_DECREF(p);
return idata;
}
/*
* python_interpreter_name
*
* Get name of current interpreter. Must be called while lock is held.
* This is effectively a shortcut for accessing "apache.interpreter".
*/
PyObject *python_interpreter_name()
{
PyObject *m = NULL;
PyObject *d = NULL;
PyObject *o = NULL;
m = PyImport_ImportModule("mod_python.apache");
if (m) {
d = PyModule_GetDict(m);
o = PyDict_GetItemString(d, "interpreter");
if (o) {
Py_INCREF(o);
Py_DECREF(m);
return o;
}
}
return 0;
}
/**
** get_interpreter
**
* Get interpreter given its name.
* NOTE: This function will acquire lock
*/
static interpreterdata *get_interpreter(const char *name, server_rec *srv)
{
PyObject *p;
PyThreadState *tstate;
interpreterdata *idata = NULL;
if (! name)
name = MAIN_INTERPRETER;
#if APR_HAS_THREADS
apr_thread_mutex_lock(interpreters_lock);
#endif
#ifdef WITH_THREAD
PyEval_AcquireLock();
#endif
if (!interpreters) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, srv,
"get_interpreter: interpreters dictionary not initialised.");
return NULL;
}
p = PyDict_GetItemString(interpreters, (char *)name);
if (!p)
{
PyInterpreterState *istate = make_interpreter(name, srv);
if (istate)
idata = save_interpreter(name, istate);
}
else {
idata = (interpreterdata *)PyCObject_AsVoidPtr(p);
}
#ifdef WITH_THREAD
PyEval_ReleaseLock();
#endif
if (! idata) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, srv,
"get_interpreter: cannot get interpreter data (no more memory?)");
#if APR_HAS_THREADS
apr_thread_mutex_unlock(interpreters_lock);
#endif
return NULL;
}
/* create thread state and acquire lock */
tstate = PyThreadState_New(idata->istate);
#ifdef WITH_THREAD
PyEval_AcquireThread(tstate);
#else
PyThreadState_Swap(tstate);
#endif
if (!idata->obcallback) {
idata->obcallback = make_obcallback((char*)name,srv);
if (!idata->obcallback)
{
#ifdef WITH_THREAD
PyEval_ReleaseThread(tstate);
#else
PyThreadState_Swap(NULL);
#endif
PyThreadState_Delete(tstate);
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, srv,
"get_interpreter: no interpreter callback found.");
#if APR_HAS_THREADS
apr_thread_mutex_unlock(interpreters_lock);
#endif
return NULL;
}
}
#if APR_HAS_THREADS
apr_thread_mutex_unlock(interpreters_lock);
#endif
return idata;
}
/**
** release_interpreter
**
* Release interpreter.
* NOTE: This function will release lock
*/
static void release_interpreter(void)
{
PyThreadState *tstate = PyThreadState_Get();
#ifdef WITH_THREAD
PyEval_ReleaseThread(tstate);
#else
PyThreadState_Swap(NULL);
#endif
PyThreadState_Delete(tstate);
}
/**
** pytho_cleanup
**
* This function gets called for clean ups registered
* with register_cleanup(). Clean ups registered via
* PythonCleanupHandler run in python_cleanup_handler()
* below.
*/
apr_status_t python_cleanup(void *data)
{
interpreterdata *idata;
cleanup_info *ci = (cleanup_info *)data;
/* get/create interpreter */
if (ci->request_rec)
idata = get_interpreter(ci->interpreter, ci->request_rec->server);
else
idata = get_interpreter(ci->interpreter, ci->server_rec);
if (!idata) {
Py_DECREF(ci->handler);
Py_XDECREF(ci->data);
free((void *)ci->interpreter);
free(ci);
return APR_SUCCESS; /* this is ignored anyway */
}
/*
* Call the cleanup function.
*/
if (! PyObject_CallFunction(ci->handler, "O", ci->data)) {
PyObject *ptype;
PyObject *pvalue;
PyObject *ptb;
PyObject *handler;
PyObject *stype;
PyObject *svalue;
PyErr_Fetch(&ptype, &pvalue, &ptb);
handler = PyObject_Str(ci->handler);
stype = PyObject_Str(ptype);
svalue = PyObject_Str(pvalue);
Py_XDECREF(ptype);
Py_XDECREF(pvalue);
Py_XDECREF(ptb);
if (ci->request_rec) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0,
ci->request_rec,
"python_cleanup: Error calling cleanup object %s",
PyString_AsString(handler));
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0,
ci->request_rec,
" %s: %s", PyString_AsString(stype),
PyString_AsString(svalue));
}
else {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0,
ci->server_rec,
"python_cleanup: Error calling cleanup object %s",
PyString_AsString(handler));
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0,
ci->server_rec,
" %s: %s", PyString_AsString(stype),
PyString_AsString(svalue));
}
Py_DECREF(handler);
Py_DECREF(stype);
Py_DECREF(svalue);
}
Py_DECREF(ci->handler);
Py_DECREF(ci->data);
free((void *)ci->interpreter);
free(ci);
release_interpreter();
return APR_SUCCESS;
}
static apr_status_t init_mutexes(server_rec *s, apr_pool_t *p, py_global_config *glb)
{
int max_threads = 0;
int max_procs = 0;
int is_threaded = 0;
int is_forked = 0;
int max_clients;
int locks;
int n;
const char *val;
const char *mutex_dir;
py_config *conf;
conf = (py_config *) ap_get_module_config(s->module_config,
&python_module);
/* figure out maximum possible concurrent connections */
/* MAX_DAEMON_USED seems to account for MaxClients, as opposed to
MAX_DAEMONS, which is ServerLimit
*/
ap_mpm_query(AP_MPMQ_IS_THREADED, &is_threaded);
if (is_threaded != AP_MPMQ_NOT_SUPPORTED) {
ap_mpm_query(AP_MPMQ_MAX_THREADS, &max_threads);
}
ap_mpm_query(AP_MPMQ_IS_FORKED, &is_forked);
if (is_forked != AP_MPMQ_NOT_SUPPORTED) {
/* XXX This looks strange, and it is. prefork.c seems to use
MAX_DAEMON_USED the same way that worker.c uses
MAX_DAEMONS (prefork is wrong IMO) */
ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_procs);
if (max_procs == -1) {
ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_procs);
}
}
max_clients = (((max_threads <= 0) ? 1 : max_threads) *
((max_procs <= 0) ? 1 : max_procs));
/* On some systems the locking mechanism chosen uses valuable
system resources, notably on RH 8 it will use sysv ipc for
which Linux by default provides only 128 semaphores
system-wide, and on many other systems flock is used, which
results in a relatively large number of open files.
The maximum number of locks can be specified at
compile time using "./configure --with-max-locks value" or
at run time with "PythonOption mod_python.mutex_locks value".
If the PythonOption directive is used, it must be in a
server config context, otherwise it will be ignored.
The optimal number of necessary locks is not clear, perhaps a
small number is more than sufficient - if someone took the
time to run some research on this, that'd be most welcome!
*/
val = apr_table_get(conf->options, "mod_python.mutex_locks");
if (val) {
locks = atoi(val);
} else {
locks = MAX_LOCKS;
}
locks = (max_clients > locks) ? locks : max_clients;
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
"mod_python: Creating %d session mutexes based "
"on %d max processes and %d max threads.",
locks, max_procs, max_threads);
glb->g_locks = (apr_global_mutex_t **)
apr_palloc(p, locks * sizeof(apr_global_mutex_t *));
glb->nlocks = locks;
glb->parent_pid = getpid();
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
/* On some sytems a directory for the mutex lock files is required.
This mutex directory can be specifed at compile time using
"./configure --with-mutex-dir value" or at run time with
"PythonOption mod_python.mutex_directory value".
If the PythonOption directive is used, it must be in a
server config context, otherwise it will be ignored.
XXX Should we check if mutex_dir exists and has the correct
permissions?
*/
mutex_dir = apr_table_get(conf->options, "mod_python.mutex_directory");
if (!mutex_dir)
mutex_dir = MUTEX_DIR;
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
"mod_python: using mutex_directory %s ",
mutex_dir);
#endif
for (n=0; n<locks; n++) {
apr_status_t rc;
apr_global_mutex_t **mutex = glb->g_locks;
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
char fname[255];
/* XXX What happens if len(mutex_dir) > 255 - len(mpmtx%d%d)? */
snprintf(fname, 255, "%s/mpmtx%d%d", mutex_dir, glb->parent_pid, n);
#else
char *fname = NULL;
#endif
rc = apr_global_mutex_create(&mutex[n], fname, APR_LOCK_DEFAULT,
p);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
"mod_python: Failed to create global mutex %d of %d (%s).",
n, locks, (!fname) ? "<null>" : fname);
if (n > 1) {
/* we were able to create at least two, so lets just print a
warning/hint and proceed
*/
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
"mod_python: We can probably continue, but with diminished ability "
"to process session locks.");
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
"mod_python: Hint: On Linux, the problem may be the number of "
"available semaphores, check 'sysctl kernel.sem'");
/* now free two locks so that if there is another
module or two that wants a lock, it will be ok */
apr_global_mutex_destroy(mutex[n-1]);
glb->nlocks = n-1;
if (n > 2) {
apr_global_mutex_destroy(mutex[n-2]);
glb->nlocks = n-2;
}
break;
}
else {
return rc;
}
}
else {
/*XXX As of httpd 2.0.4, the below should be just
a call to unixd_set_global_mutex_perms(mutex[n]); and
nothing else... For now, while 2.0.48 isn't commonplace yet,
this ugly code should be here */
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
if (!geteuid()) {
chown(fname, unixd_config.user_id, -1);
unixd_set_global_mutex_perms(mutex[n]);
}
#endif
}
}
return APR_SUCCESS;
}
static apr_status_t reinit_mutexes(server_rec *s, apr_pool_t *p, py_global_config *glb)
{
int n;
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
/* Determine the directory to use for mutex lock files.
See init_mutexes function for more details.
*/
const char *mutex_dir;
py_config *conf;
conf = (py_config *) ap_get_module_config(s->module_config,
&python_module);
mutex_dir = apr_table_get(conf->options, "mod_python.mutex_directory");
if (!mutex_dir)
mutex_dir = MUTEX_DIR;
#endif
for (n=0; n< glb->nlocks; n++) {
apr_status_t rc;
apr_global_mutex_t **mutex = glb->g_locks;
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
char fname[255];
snprintf(fname, 255, "%s/mpmtx%d%d", mutex_dir, glb->parent_pid, n);
#else
char *fname = NULL;
#endif
rc = apr_global_mutex_child_init(&mutex[n], fname, p);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_STARTUP, rc, s,
"mod_python: Failed to reinit global mutex %s.",
(!fname) ? "<null>" : fname);
return rc;
}
}
return APR_SUCCESS;
}
/**
** python_create_global_config
**
* This creates the part of config that survives
* server restarts
*
*/
static py_global_config *python_create_global_config(server_rec *s)
{
apr_pool_t *pool = s->process->pool;
py_global_config *glb;
/* do we already have it in s->process->pool? */
apr_pool_userdata_get((void **)&glb, MP_CONFIG_KEY, pool);
if (glb) {
return glb;
}
/* otherwise, create it */
glb = (py_global_config *)apr_palloc(pool, sizeof(*glb));
apr_pool_userdata_set(glb, MP_CONFIG_KEY,
apr_pool_cleanup_null,
pool);
return glb;
}
/**
** python_init()
**
* Called by Apache at mod_python initialization time.
*/
static int python_init(apr_pool_t *p, apr_pool_t *ptemp,
apr_pool_t *plog, server_rec *s)
{
char buff[255];
void *data;
py_global_config *glb;
const char *userdata_key = "python_init";
apr_status_t rc;
const char *py_compile_version = PY_VERSION;
const char *py_dynamic_version = 0;
/* The "initialized" flag is a fudge for Mac OS X. It
* addresses two issues. The first is that when an Apache
* "restart" is performed, Apache will unload the mod_python
* shared object, but not the Python framework. This means
* that when "python_init()" is called after the restart,
* the mod_python initialization will not run if only the
* initialized state of Python is checked, because Python
* is already initialized. The second problem is that for
* some older revisions of Mac OS X, even on the initial
* startup of Apache, the "Py_IsInitialized()" function
* would return true and mod_python wouldn't initialize
* itself correctly and would crash.
*/
static int initialized = 0;
apr_pool_userdata_get(&data, userdata_key, s->process->pool);
if (!data) {
apr_pool_userdata_set((const void *)1, userdata_key,
apr_pool_cleanup_null, s->process->pool);
return OK;
}
/* mod_python version */
ap_add_version_component(p, VERSION_COMPONENT);
py_dynamic_version = strtok((char *)Py_GetVersion(), " ");
if (strcmp(py_compile_version, py_dynamic_version) != 0) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
"python_init: Python version mismatch, expected '%s', found '%s'.",
py_compile_version, py_dynamic_version);
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
"python_init: Python executable found '%s'.",
Py_GetProgramFullPath());
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
"python_init: Python path being used '%s'.",
Py_GetPath());
}
/* Python version */
sprintf(buff, "Python/%.200s", py_dynamic_version);
ap_add_version_component(p, buff);
/* cache main server */
main_server = s;
/* global config */
glb = python_create_global_config(s);
if ((rc = init_mutexes(s, p, glb)) != APR_SUCCESS) {
return rc;
}
/* initialize global Python interpreter if necessary */
if (initialized == 0 || !Py_IsInitialized())
{
initialized = 1;
/* initialze the interpreter */
Py_Initialize();
#if APR_HAS_THREADS
apr_thread_mutex_create(&interpreters_lock, APR_THREAD_MUTEX_UNNESTED, p);
#endif
#ifdef WITH_THREAD
/* create and acquire the interpreter lock */
PyEval_InitThreads();
#endif
/* create the obCallBack dictionary */
interpreters = PyDict_New();
if (! interpreters) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
"python_init: PyDict_New() failed! No more memory?");
exit(1);
}
#ifdef WITH_THREAD
/* release the lock; now other threads can run */
PyEval_ReleaseLock();
#endif
}
return OK;
}
/**
** python_create_config
**
* Called by create_dir_config and create_srv_config
*/
static py_config *python_create_config(apr_pool_t *p)
{
py_config *conf =
(py_config *) apr_pcalloc(p, sizeof(py_config));
conf->authoritative = 1;
conf->options = apr_table_make(p, 4);
conf->directives = apr_table_make(p, 4);
conf->hlists = apr_hash_make(p);
conf->in_filters = apr_hash_make(p);
conf->out_filters = apr_hash_make(p);
return conf;
}
/**
** python_create_dir_config
**
* Allocate memory and initialize the strucure that will
* hold configuration parametes.
*
* This function is called on every hit it seems.
*/
static void *python_create_dir_config(apr_pool_t *p, char *dir)
{
py_config *conf = python_create_config(p);
/* make sure directory ends with a slash */
if (dir && (dir[strlen(dir) - 1] != '/'))
conf->config_dir = apr_pstrcat(p, dir, "/", NULL);
else
conf->config_dir = apr_pstrdup(p, dir);
return conf;
}
/**
** python_create_srv_config
**
* Allocate memory and initialize the strucure that will
* hold configuration parametes.
*/
static void *python_create_srv_config(apr_pool_t *p, server_rec *srv)
{
py_config *conf = python_create_config(p);
return conf;
}
/**
** modpython_table_overlap
**
* Replaces the apr_table_overlap() function using a specific pool
* for the resulting table.
*/
static apr_table_t *modpython_table_overlap(apr_pool_t *p,
apr_table_t *current_table,
apr_table_t *new_table)
{
apr_table_t *merge = apr_table_overlay(p, current_table, new_table);
apr_table_compress(merge, APR_OVERLAP_TABLES_SET);
return merge;
}
/**
** python_merge_dir_config
**
*/
static void *python_merge_config(apr_pool_t *p, void *current_conf,
void *new_conf)
{
py_config *merged_conf =
(py_config *) apr_pcalloc(p, sizeof(py_config));
py_config *cc = (py_config *) current_conf;
py_config *nc = (py_config *) new_conf;
apr_hash_index_t *hi;
char *key;
apr_ssize_t klen;
hl_entry *hle;
py_handler *fh;
/* we basically allow the local configuration to override global,
* by first copying current values and then new values on top
*/
/** create **/
merged_conf->hlists = apr_hash_make(p);
merged_conf->in_filters = apr_hash_make(p);
merged_conf->out_filters = apr_hash_make(p);
/** merge directives and options **/
merged_conf->directives = modpython_table_overlap(p, cc->directives,
nc->directives);
merged_conf->options = modpython_table_overlap(p, cc->options,
nc->options);
/** copy current **/
merged_conf->authoritative = cc->authoritative;
merged_conf->config_dir = apr_pstrdup(p, cc->config_dir);
for (hi = apr_hash_first(p, cc->hlists); hi; hi=apr_hash_next(hi)) {
apr_hash_this(hi, (const void **)&key, &klen, (void **)&hle);
apr_hash_set(merged_conf->hlists, key, klen, (void *)hle);
}
for (hi = apr_hash_first(p, cc->in_filters); hi; hi=apr_hash_next(hi)) {
apr_hash_this(hi, (const void **)&key, &klen, (void **)&fh);
apr_hash_set(merged_conf->in_filters, key, klen, (void *)fh);
}
for (hi = apr_hash_first(p, cc->out_filters); hi; hi=apr_hash_next(hi)) {
apr_hash_this(hi, (const void **)&key, &klen, (void **)&fh);
apr_hash_set(merged_conf->out_filters, key, klen, (void *)fh);
}
/** copy new **/
if (nc->authoritative != merged_conf->authoritative)
merged_conf->authoritative = nc->authoritative;
if (nc->config_dir)
merged_conf->config_dir = apr_pstrdup(p, nc->config_dir);
for (hi = apr_hash_first(p, nc->hlists); hi; hi=apr_hash_next(hi)) {
apr_hash_this(hi, (const void**)&key, &klen, (void **)&hle);
apr_hash_set(merged_conf->hlists, key, klen, (void *)hle);
}
for (hi = apr_hash_first(p, nc->in_filters); hi; hi=apr_hash_next(hi)) {
apr_hash_this(hi, (const void**)&key, &klen, (void **)&fh);
apr_hash_set(merged_conf->in_filters, key, klen, (void *)fh);
}
for (hi = apr_hash_first(p, nc->out_filters); hi; hi=apr_hash_next(hi)) {
apr_hash_this(hi, (const void**)&key, &klen, (void **)&fh);
apr_hash_set(merged_conf->out_filters, key, klen, (void *)fh);
}
return (void *) merged_conf;
}
/**
** python_directive
**
* Called by non-handler directives
*
*/
static const char *python_directive(cmd_parms *cmd, void * mconfig,
char *key, const char *val)
{
py_config *conf;
conf = (py_config *) mconfig;
apr_table_set(conf->directives, key, val);
return NULL;
}
/* returns a parent if it matches the given directive */
static const ap_directive_t * find_parent(const ap_directive_t *dirp,
const char *what)
{
while (dirp->parent != NULL) {
dirp = dirp->parent;
if (strcasecmp(dirp->directive, what) == 0)
return dirp;
}
return NULL;
}
#ifdef WIN32
#define USE_ICASE AP_REG_ICASE
#else
#define USE_ICASE 0
#endif
static void determine_context(apr_pool_t *p, const cmd_parms* cmd,
char **dp, int *gx, ap_regex_t **rx)
{
const ap_directive_t *context = 0;
const ap_directive_t *directive = 0;
const char *endp, *arg;
char *directory = 0;
int d_is_fnmatch = 0;
ap_regex_t *regex = 0;
directive = cmd->directive;
/* Skip any enclosing File directive if one exists */
if ((context = find_parent(directive, "<Files")) ||
(context = find_parent(directive, "<FilesMatch"))) {
directive = context;
}
if ((context = find_parent(directive, "<Directory"))) {
arg = context->args;
endp = ap_strrchr_c(arg, '>');
arg = apr_pstrndup(p, arg, endp - arg);
directory = ap_getword_conf(p, &arg);
if (!strcmp(directory, "~")) {
directory = ap_getword_conf(p, &arg);
regex = ap_pregcomp(p, cmd->path, AP_REG_EXTENDED|USE_ICASE);
} else if (ap_is_matchexp(directory)) {
d_is_fnmatch = 1;
}
} else if ((context = find_parent(directive, "<DirectoryMatch"))) {
arg = context->args;
endp = ap_strrchr_c(arg, '>');
arg = apr_pstrndup(p, arg, endp - arg);
directory = ap_getword_conf(p, &arg);
regex = ap_pregcomp(p, directory, AP_REG_EXTENDED|USE_ICASE);
}
else if (cmd->config_file != NULL) {
/* cmd->config_file is NULL when in main Apache
* configuration file as the file is completely
* read in before the directive is processed as
* EXEC_ON_READ is not set in req_override field
* of command_struct table entry. Thus know then
* we are being used in a .htaccess file. */
directory = ap_make_dirstr_parent(p, directive->filename);
}
/* Only canonicalize path and add trailing slash at
* this point if no pattern matching to be done at
* a later time. */
if (!d_is_fnmatch && !regex) {
char *newpath = 0;
apr_status_t rv;
rv = apr_filepath_merge(&newpath, NULL, directory,