This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmod_ruby.c
1546 lines (1379 loc) · 41 KB
/
mod_ruby.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
/*
* $Id$
* Copyright (C) 2000 ZetaBITS, Inc.
* Copyright (C) 2000 Information-technology Promotion Agency, Japan
* Copyright (C) 2001 Shugo Maeda <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdarg.h>
#include <signal.h>
#include "mod_ruby.h"
#include "ruby_config.h"
#include "apachelib.h"
#if defined(HAVE_DLOPEN) && !defined(USE_DLN_A_OUT) && !defined(_AIX)
/* dynamic load with dlopen() */
# define USE_DLN_DLOPEN
#endif
#ifdef USE_DLN_DLOPEN
# include <dlfcn.h>
# ifdef __NetBSD__
# include <nlist.h>
# include <link.h>
# endif
#endif
#ifdef __hpux
#include <errno.h>
#include "dl.h"
#endif
#ifdef NeXT
#if NS_TARGET_MAJOR < 4
#include <mach-o/rld.h>
#else
#include <mach-o/dyld.h>
#endif
#endif
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
#ifdef _WIN32
#define GET_ENVIRON(e) (e = rb_w32_get_environ())
#define FREE_ENVIRON(e) rb_w32_free_environ(e)
static char **my_environ;
#undef environ
#define environ my_environ
#elif defined(__APPLE__)
#include <crt_externs.h>
#undef environ
#define environ (*_NSGetEnviron())
#define GET_ENVIRON(e) (e)
#define FREE_ENVIRON(e)
#else
extern char **environ;
#define GET_ENVIRON(e) (e)
#define FREE_ENVIRON(e)
#endif
RUBY_EXTERN VALUE rb_stdin;
RUBY_EXTERN VALUE rb_stdout;
RUBY_EXTERN VALUE rb_stderr;
#if RUBY_VERSION_CODE < 180
RUBY_EXTERN VALUE rb_defout;
#endif
static VALUE orig_stdin;
static VALUE orig_stdout;
#if RUBY_VERSION_CODE < 180
static VALUE orig_defout;
#endif
static VALUE default_load_path;
static VALUE progname;
static const char *default_kcode;
static int ruby_is_running = 0;
array_header *ruby_required_libraries = NULL;
#if APR_HAS_THREADS
#include "apr_thread_cond.h"
static apr_thread_t *ruby_thread;
static apr_thread_mutex_t *ruby_is_running_mutex;
static apr_thread_cond_t *ruby_is_running_cond;
static apr_thread_mutex_t *ruby_request_queue_mutex;
static apr_thread_cond_t *ruby_request_queue_cond;
int ruby_is_threaded_mpm;
typedef VALUE (*ruby_protect_func_t)(VALUE);
typedef struct ruby_request {
ruby_interp_func_t func;
void *arg;
void *result;
int state;
int done;
apr_thread_cond_t *done_cond;
struct ruby_request *next;
} ruby_request_t;
static ruby_request_t *ruby_request_queue = NULL;
#define SHUTDOWN_RUBY_THREAD NULL
#endif
#define RUBY_OBJECT_HANDLER "ruby-object"
static int ruby_object_handler(request_rec *r);
static int ruby_trans_handler(request_rec *r);
static int ruby_authen_handler(request_rec *r);
static int ruby_authz_handler(request_rec *r);
static int ruby_access_handler(request_rec *r);
static int ruby_type_handler(request_rec *r);
static int ruby_fixup_handler(request_rec *r);
static int ruby_log_handler(request_rec *r);
#ifndef APACHE2 /* Apache 1.x */
static int ruby_header_parser_handler(request_rec *r);
#endif
static int ruby_post_read_request_handler(request_rec *r);
#ifndef AP_INIT_TAKE1
# define AP_INIT_TAKE1(directive, func, mconfig, where, help) \
{ directive, func, mconfig, where, TAKE1, help }
# define AP_INIT_TAKE2(directive, func, mconfig, where, help) \
{ directive, func, mconfig, where, TAKE2, help }
# define AP_INIT_ITERATE(directive, func, mconfig, where, help) \
{ directive, func, mconfig, where, ITERATE, help }
# define AP_INIT_FLAG(directive, func, mconfig, where, help) \
{ directive, func, mconfig, where, FLAG, help }
#endif
static const command_rec ruby_cmds[] = {
AP_INIT_TAKE1("RubyKanjiCode", (const char *(*)())ruby_cmd_kanji_code, NULL, OR_ALL,
"set $KCODE"),
AP_INIT_ITERATE("RubyAddPath", ruby_cmd_add_path, NULL, OR_ALL,
"add path to $:"),
AP_INIT_ITERATE("RubyRequire", ruby_cmd_require, NULL, OR_ALL,
"ruby script name, pulled in via require"),
AP_INIT_ITERATE("RubyPassEnv", ruby_cmd_pass_env, NULL, RSRC_CONF,
"pass environment variables to ENV"),
AP_INIT_TAKE2("RubySetEnv", ruby_cmd_set_env, NULL, OR_ALL,
"Ruby ENV key and value" ),
AP_INIT_TAKE1("RubyTimeOut", ruby_cmd_timeout, NULL, RSRC_CONF,
"time to wait execution of ruby script"),
AP_INIT_TAKE1("RubySafeLevel", ruby_cmd_safe_level, NULL, OR_ALL,
"set default $SAFE"),
AP_INIT_TAKE1("RubyOutputMode", ruby_cmd_output_mode, NULL, OR_ALL,
"set output mode (nosync|sync|syncheader)"),
AP_INIT_FLAG("RubyRestrictDirectives", ruby_cmd_restrict_directives, NULL,
RSRC_CONF, "whether Ruby* directives are restricted from .htaccess"),
AP_INIT_TAKE2("RubyOption", ruby_cmd_option, NULL, OR_ALL,
"set option for application"),
AP_INIT_FLAG("RubyGcPerRequest", ruby_cmd_gc_per_request, NULL,
OR_ALL, "whether to call GC for each request"),
AP_INIT_TAKE1("RubyHandler", ruby_cmd_handler, NULL, OR_ALL,
"set ruby handler object"),
AP_INIT_TAKE1("RubyTransHandler", ruby_cmd_trans_handler, NULL, OR_ALL,
"set translation handler object"),
AP_INIT_TAKE1("RubyAuthenHandler", ruby_cmd_authen_handler, NULL, OR_ALL,
"set authentication handler object"),
AP_INIT_TAKE1("RubyAuthzHandler", ruby_cmd_authz_handler, NULL, OR_ALL,
"set authorization handler object"),
AP_INIT_TAKE1("RubyAccessHandler", ruby_cmd_access_handler, NULL, OR_ALL,
"set access checker object"),
AP_INIT_TAKE1("RubyTypeHandler", ruby_cmd_type_handler, NULL, OR_ALL,
"set type checker object"),
AP_INIT_TAKE1("RubyFixupHandler", ruby_cmd_fixup_handler, NULL, OR_ALL,
"set fixup handler object"),
AP_INIT_TAKE1("RubyLogHandler", ruby_cmd_log_handler, NULL, OR_ALL,
"set log handler object"),
#ifdef APACHE2
AP_INIT_TAKE1("RubyErrorLogHandler", ruby_cmd_error_log_handler, NULL, OR_ALL,
"set log handler object"),
#endif
AP_INIT_TAKE1("RubyHeaderParserHandler", ruby_cmd_header_parser_handler,
NULL, OR_ALL,
"set header parser object"),
AP_INIT_TAKE1("RubyPostReadRequestHandler", ruby_cmd_post_read_request_handler,
NULL, RSRC_CONF,
"set post-read-request handler object"),
AP_INIT_TAKE1("RubyInitHandler", ruby_cmd_init_handler,
NULL, OR_ALL,
"set init handler object"),
AP_INIT_TAKE1("RubyCleanupHandler", ruby_cmd_cleanup_handler,
NULL, OR_ALL,
"set cleanup handler object"),
AP_INIT_TAKE1("RubyChildInitHandler", ruby_cmd_child_init_handler,
NULL, RSRC_CONF,
"set child init handler object"),
{NULL}
};
#ifdef APACHE2
static int ruby_startup(pool*, pool*, pool*, server_rec*);
static void ruby_child_init(pool*, server_rec*);
static void ruby_error_log_handler(const char*, int, int, apr_status_t, const server_rec*, const request_rec*, pool*, const char*);
static void ruby_register_hooks(pool *p)
{
static const char *const post[] = {"mod_autoindex.c", NULL};
ap_hook_post_config(ruby_startup, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_handler(ruby_object_handler, NULL, post, APR_HOOK_MIDDLE);
ap_hook_translate_name(ruby_trans_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_check_user_id(ruby_authen_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_auth_checker(ruby_authz_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_access_checker(ruby_access_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_type_checker(ruby_type_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_fixups(ruby_fixup_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_log_transaction(ruby_log_handler, NULL, NULL, APR_HOOK_MIDDLE);
#ifdef APACHE2
ap_hook_error_log(ruby_error_log_handler, NULL, NULL, APR_HOOK_MIDDLE);
#endif
ap_hook_child_init(ruby_child_init, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_post_read_request(ruby_post_read_request_handler,
NULL, NULL, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA ruby_module =
{
STANDARD20_MODULE_STUFF,
ruby_create_dir_config, /* dir config creater */
ruby_merge_dir_config, /* dir merger --- default is to override */
ruby_create_server_config, /* create per-server config structure */
ruby_merge_server_config, /* merge server config */
ruby_cmds, /* command apr_table_t */
ruby_register_hooks /* register hooks */
};
#else /* Apache 1.x */
static void ruby_startup(server_rec*, pool*);
static void ruby_child_init(server_rec*, pool*);
static const handler_rec ruby_handlers[] =
{
{RUBY_OBJECT_HANDLER, ruby_object_handler},
{NULL}
};
MODULE_VAR_EXPORT module ruby_module =
{
STANDARD_MODULE_STUFF,
ruby_startup, /* initializer */
ruby_create_dir_config, /* dir config creater */
ruby_merge_dir_config, /* dir merger --- default is to override */
ruby_create_server_config, /* create per-server config structure */
ruby_merge_server_config, /* merge server config */
ruby_cmds, /* command table */
ruby_handlers, /* handlers */
ruby_trans_handler, /* filename translation */
ruby_authen_handler, /* check_user_id */
ruby_authz_handler, /* check auth */
ruby_access_handler, /* check access */
ruby_type_handler, /* type_checker */
ruby_fixup_handler, /* fixups */
ruby_log_handler, /* logger */
ruby_header_parser_handler, /* header parser */
ruby_child_init, /* child_init */
NULL, /* child_exit */
ruby_post_read_request_handler, /* post read-request */
#ifdef EAPI
NULL, /* add_module */
NULL, /* remove_module */
NULL, /* rewrite_command */
NULL, /* new_connection */
NULL /* close_connection */
#endif /* EAPI */
};
#endif
#define STRING_LITERAL(s) rb_str_new(s, sizeof(s) - 1)
#define STR_CAT_LITERAL(str, s) rb_str_cat(str, s, sizeof(s) - 1)
typedef struct protect_call_arg {
VALUE recv;
ID mid;
int argc;
VALUE *argv;
} protect_call_arg_t;
static VALUE protect_funcall0(VALUE arg)
{
return rb_funcall2(((protect_call_arg_t *) arg)->recv,
((protect_call_arg_t *) arg)->mid,
((protect_call_arg_t *) arg)->argc,
((protect_call_arg_t *) arg)->argv);
}
VALUE rb_protect_funcall(VALUE recv, ID mid, int *state, int argc, ...)
{
va_list ap;
VALUE *argv;
struct protect_call_arg arg;
if (argc > 0) {
int i;
argv = ALLOCA_N(VALUE, argc);
va_start(ap, argc);
for (i = 0; i < argc; i++) {
argv[i] = va_arg(ap, VALUE);
}
va_end(ap);
}
else {
argv = 0;
}
arg.recv = recv;
arg.mid = mid;
arg.argc = argc;
arg.argv = argv;
return rb_protect(protect_funcall0, (VALUE) &arg, state);
}
static void get_error_pos(VALUE str)
{
char buff[BUFSIZ];
ID this_func = rb_frame_this_func();
if (rb_sourcefile()) {
if (this_func) {
snprintf(buff, BUFSIZ, "%s:%d:in `%s'", rb_sourcefile(), rb_sourceline(),
rb_id2name(this_func));
}
else {
snprintf(buff, BUFSIZ, "%s:%d", rb_sourcefile(), rb_sourceline());
}
rb_str_cat(str, buff, strlen(buff));
}
}
static void get_exception_info(VALUE str)
{
VALUE errat;
VALUE eclass;
VALUE estr;
char *einfo;
int elen;
int state;
if (NIL_P(rb_errinfo())) return;
errat = rb_funcall(rb_errinfo(), rb_intern("backtrace"), 0);
if (!NIL_P(errat)) {
VALUE mesg = RARRAY_PTR(errat)[0];
if (NIL_P(mesg)) {
get_error_pos(str);
}
else {
rb_str_cat(str, RSTRING_PTR(mesg), RSTRING_LEN(mesg));
}
}
eclass = CLASS_OF(rb_errinfo());
estr = rb_protect(rb_obj_as_string, rb_errinfo(), &state);
if (state) {
einfo = "";
elen = 0;
}
else {
einfo = RSTRING_PTR(estr);
elen = RSTRING_LEN(estr);
}
if (eclass == rb_eRuntimeError && elen == 0) {
STR_CAT_LITERAL(str, ": unhandled exception\n");
}
else {
VALUE epath;
epath = rb_class_path(eclass);
if (elen == 0) {
STR_CAT_LITERAL(str, ": ");
rb_str_cat(str, RSTRING_PTR(epath), RSTRING_LEN(epath));
STR_CAT_LITERAL(str, "\n");
}
else {
char *tail = 0;
int len = elen;
if (RSTRING_PTR(epath)[0] == '#') epath = 0;
if ((tail = strchr(einfo, '\n')) != NULL) {
len = tail - einfo;
tail++; /* skip newline */
}
STR_CAT_LITERAL(str, ": ");
rb_str_cat(str, einfo, len);
if (epath) {
STR_CAT_LITERAL(str, " (");
rb_str_cat(str, RSTRING_PTR(epath), RSTRING_LEN(epath));
STR_CAT_LITERAL(str, ")\n");
}
if (tail) {
rb_str_cat(str, tail, elen - len - 1);
STR_CAT_LITERAL(str, "\n");
}
}
}
if (!NIL_P(errat)) {
long i, len;
VALUE *ep;
#define TRACE_MAX (TRACE_HEAD+TRACE_TAIL+5)
#define TRACE_HEAD 8
#define TRACE_TAIL 5
ep = RARRAY_PTR(errat);
len = RARRAY_LEN(errat);
for (i=1; i<len; i++) {
if (TYPE(ep[i]) == T_STRING) {
STR_CAT_LITERAL(str, " from ");
rb_str_cat(str, RSTRING_PTR(ep[i]), RSTRING_LEN(ep[i]));
STR_CAT_LITERAL(str, "\n");
}
if (i == TRACE_HEAD && len > TRACE_MAX) {
char buff[BUFSIZ];
snprintf(buff, BUFSIZ, " ... %ld levels...\n",
len - TRACE_HEAD - TRACE_TAIL);
rb_str_cat(str, buff, strlen(buff));
i = len - TRACE_TAIL;
}
}
}
/* rb_errinfo() = Qnil; */
}
VALUE ruby_get_error_info(int state)
{
char buff[BUFSIZ];
VALUE errmsg;
errmsg = STRING_LITERAL("");
switch (state) {
case TAG_RETURN:
get_error_pos(errmsg);
STR_CAT_LITERAL(errmsg, ": unexpected return\n");
break;
case TAG_NEXT:
get_error_pos(errmsg);
STR_CAT_LITERAL(errmsg, ": unexpected next\n");
break;
case TAG_BREAK:
get_error_pos(errmsg);
STR_CAT_LITERAL(errmsg, ": unexpected break\n");
break;
case TAG_REDO:
get_error_pos(errmsg);
STR_CAT_LITERAL(errmsg, ": unexpected redo\n");
break;
case TAG_RETRY:
get_error_pos(errmsg);
STR_CAT_LITERAL(errmsg, ": retry outside of rescue clause\n");
break;
case TAG_RAISE:
case TAG_FATAL:
get_exception_info(errmsg);
break;
default:
get_error_pos(errmsg);
snprintf(buff, BUFSIZ, ": unknown longjmp status %d", state);
rb_str_cat(errmsg, buff, strlen(buff));
break;
}
return errmsg;
}
void ruby_log_error(const char *file, int line, int level,
const server_rec *s, const char *fmt, ...)
{
va_list args;
char buf[BUFSIZ];
va_start(args, fmt);
vsnprintf(buf, BUFSIZ, fmt, args);
#ifdef APACHE2
ap_log_error(file, line, level, 0, s, "mod_ruby: %s", buf);
#else
ap_log_error(file, line, level, s, "mod_ruby: %s", buf);
#endif
va_end(args);
}
void ruby_log_error_string(server_rec *s, VALUE errmsg)
{
VALUE msgs;
int i;
ruby_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, s, "error in ruby");
msgs = rb_str_split(errmsg, "\n");
for (i = 0; i < RARRAY_LEN(msgs); i++) {
ruby_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, s,
"%s", StringValuePtr(RARRAY_PTR(msgs)[i]));
}
}
static void handle_error(request_rec *r, int state)
{
ruby_request_config *rconf;
VALUE errmsg;
errmsg = ruby_get_error_info(state);
if (r->request_config) {
rconf = get_request_config(r);
if (rconf && !NIL_P(rconf->request_object))
rb_apache_request_set_error(rconf->request_object,
errmsg, rb_errinfo());
}
ruby_log_error_string(r->server, errmsg);
}
int ruby_running()
{
return ruby_is_running;
}
void mod_ruby_setup_loadpath(ruby_server_config *sconf,
ruby_dir_config *dconf)
{
int i, n;
char **paths;
VALUE load_path;
#ifdef RUBY_VM
load_path = GET_LOAD_PATH();
rb_ary_clear(load_path);
#else
rb_load_path = load_path = rb_ary_new();
#endif
for (i = 0; i < RARRAY_LEN(default_load_path); i++) {
rb_ary_push(load_path, rb_str_dup(RARRAY_PTR(default_load_path)[i]));
}
if (sconf && sconf->load_path) {
paths = (char **) sconf->load_path->elts;
n = sconf->load_path->nelts;
for (i = 0; i < n; i++) {
rb_ary_push(load_path, rb_str_new2(paths[i]));
}
}
if (dconf && dconf->load_path) {
paths = (char **) dconf->load_path->elts;
n = dconf->load_path->nelts;
for (i = 0; i < n; i++) {
rb_ary_push(load_path, rb_str_new2(paths[i]));
}
}
}
static int ruby_require_directly(const char *filename,
ruby_server_config *sconf,
ruby_dir_config *dconf)
{
VALUE fname, exit_status;
int state;
mod_ruby_setup_loadpath(sconf, dconf);
fname = rb_str_new2(filename);
rb_protect_funcall(Qnil, rb_intern("require"), &state, 1, fname);
if (state == TAG_RAISE &&
rb_obj_is_kind_of(rb_errinfo(), rb_eSystemExit)) {
exit_status = rb_iv_get(rb_errinfo(), "status");
exit(NUM2INT(exit_status));
}
return state;
}
static void ruby_add_path(const char *path)
{
rb_ary_push(default_load_path, rb_str_new2(path));
}
#ifdef APACHE2
static int ruby_startup(pool *p, pool *plog, pool *ptemp, server_rec *s)
{
ap_add_version_component(p, MOD_RUBY_STRING_VERSION);
#if RUBY_RELEASE_CODE > 20040624
{
char *version = apr_pstrcat(p, "Ruby/", ruby_version,
"(", ruby_release_date, ")",
(char *) NULL);
ap_add_version_component(p, version);
}
#endif
#if APR_HAS_THREADS
ruby_is_threaded_mpm = ap_find_linked_module("prefork.c") == NULL;
#endif
return OK;
}
#else /* Apache 1.x */
static void ruby_startup(server_rec *s, pool *p)
{
ap_add_version_component(MOD_RUBY_STRING_VERSION);
#if RUBY_RELEASE_CODE > 20040624
{
char *version = ap_pstrcat(p, "Ruby/", ruby_version,
"(", ruby_release_date, ")",
(char *) NULL);
ap_add_version_component(version);
}
#endif
}
#endif
#ifdef POSIX_SIGNAL
#define ruby_signal(sig,handle) posix_signal((sig),(handle))
#else
#define ruby_signal(sig,handle) signal((sig),(handle))
#endif
static void ruby_init_interpreter(server_rec *s)
{
ruby_server_config *conf = get_server_config(s);
ruby_library_context *libraries;
char **list;
int i, n;
int state;
#ifdef SIGHUP
RETSIGTYPE (*sighup_handler)_((int));
#endif
#ifdef SIGQUIT
RETSIGTYPE (*sigquit_handler)_((int));
#endif
#ifdef SIGTERM
RETSIGTYPE (*sigterm_handler)_((int));
#endif
#ifdef RUBY_VM
RUBY_INIT_STACK;
#else
VALUE stack_start;
void Init_stack _((VALUE*));
Init_stack(&stack_start);
#endif
#ifdef SIGHUP
sighup_handler = signal(SIGHUP, SIG_DFL);
#endif
#ifdef SIGQUIT
sigquit_handler = signal(SIGQUIT, SIG_DFL);
#endif
#ifdef SIGTERM
sigterm_handler = signal(SIGTERM, SIG_DFL);
#endif
ruby_init();
#ifdef SIGHUP
if (sighup_handler != SIG_ERR)
ruby_signal(SIGHUP, sighup_handler);
#endif
#ifdef SIGQUIT
if (sigquit_handler != SIG_ERR)
ruby_signal(SIGQUIT, sigquit_handler);
#endif
#ifdef SIGTERM
if (sigterm_handler != SIG_ERR)
ruby_signal(SIGTERM, sigterm_handler);
#endif
rb_init_apache();
rb_define_global_const("MOD_RUBY",
STRING_LITERAL(MOD_RUBY_STRING_VERSION));
orig_stdin = rb_stdin;
orig_stdout = rb_stdout;
#if RUBY_VERSION_CODE < 180
orig_defout = rb_defout;
#endif
#ifdef APACHE2
rb_protect_funcall(rb_stderr, rb_intern("sync="), NULL, 1, Qtrue);
#endif
ruby_init_loadpath();
{
char *argv[] = { "ruby", "-e", "" };
ruby_options(3, argv);
}
default_load_path = rb_ary_dup(GET_LOAD_PATH());
rb_global_variable(&default_load_path);
rb_define_variable("$0", &progname);
rb_define_variable("$PROGRAM_NAME", &progname);
list = (char **) conf->load_path->elts;
n = conf->load_path->nelts;
for (i = 0; i < n; i++) {
ruby_add_path(list[i]);
}
conf->load_path = NULL;
default_kcode = rb_get_kcode();
if (ruby_required_libraries) {
libraries = (ruby_library_context *) ruby_required_libraries->elts;
n = ruby_required_libraries->nelts;
for (i = 0; i < n; i++) {
if ((state = ruby_require_directly(libraries[i].filename,
libraries[i].server_config,
libraries[i].dir_config))) {
ruby_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, s,
"failed to require %s",
libraries[i].filename);
ruby_log_error_string(s, ruby_get_error_info(state));
}
}
}
}
#ifndef RUBY_VM
static void dso_unload(void *handle)
{
#if defined(_WIN32)
FreeLibrary((HANDLE) handle);
#elif defined(HPUX) || defined(HPUX10) || defined(HPUX11)
shl_unload((shl_t) handle);
#elif defined(HAVE_DYLD)
NSUnLinkModule(handle, FALSE);
#else
dlclose(handle);
#endif
}
static void ruby_unload_libraries()
{
RUBY_EXTERN VALUE ruby_dln_librefs;
int i;
for (i = 0; i < RARRAY_LEN(ruby_dln_librefs); i++) {
dso_unload((void *) NUM2LONG(RARRAY_PTR(ruby_dln_librefs)[i]));
}
}
#endif
static void ruby_finalize_interpreter()
{
ruby_finalize();
#ifndef RUBY_VM
ruby_unload_libraries();
#endif
}
#if APR_HAS_THREADS
static void* APR_THREAD_FUNC ruby_thread_start(apr_thread_t *t, void *data)
{
server_rec *s = (server_rec *) data;
ruby_request_t *req;
ruby_init_interpreter(s);
apr_thread_mutex_lock(ruby_is_running_mutex);
ruby_is_running = 1;
apr_thread_cond_signal(ruby_is_running_cond);
apr_thread_mutex_unlock(ruby_is_running_mutex);
while (1) {
apr_thread_mutex_lock(ruby_request_queue_mutex);
while (ruby_request_queue == NULL)
apr_thread_cond_wait(ruby_request_queue_cond,
ruby_request_queue_mutex);
req = ruby_request_queue;
if (req->func == SHUTDOWN_RUBY_THREAD)
break;
req->result =
(void *) rb_protect((ruby_protect_func_t) req->func,
(VALUE) req->arg,
&req->state);
ruby_request_queue = ruby_request_queue->next;
req->done = 1;
apr_thread_cond_signal(req->done_cond);
apr_thread_mutex_unlock(ruby_request_queue_mutex);
}
ruby_finalize_interpreter();
req->done = 1;
apr_thread_cond_signal(req->done_cond);
apr_thread_mutex_unlock(ruby_request_queue_mutex);
return NULL;
}
apr_status_t ruby_call_interpreter(pool *p, ruby_interp_func_t func,
void *arg, void **result, int *state)
{
apr_status_t status;
ruby_request_t *req;
req = apr_palloc(p, sizeof(ruby_request_t));
req->func = func;
req->arg = arg;
req->result = NULL;
req->state = 0;
req->done = 0;
status = apr_thread_cond_create(&req->done_cond, p);
if (status != APR_SUCCESS)
return status;
req->next = NULL;
apr_thread_mutex_lock(ruby_request_queue_mutex);
if (ruby_request_queue)
ruby_request_queue->next = req;
else
ruby_request_queue = req;
apr_thread_cond_signal(ruby_request_queue_cond);
while (!req->done)
apr_thread_cond_wait(req->done_cond, ruby_request_queue_mutex);
apr_thread_mutex_unlock(ruby_request_queue_mutex);
if (result)
*result = req->result;
if (state)
*state = req->state;
return APR_SUCCESS;
}
#endif
static APR_CLEANUP_RETURN_TYPE ruby_child_cleanup(void *data)
{
#if APR_HAS_THREADS
if (ruby_is_threaded_mpm) {
pool *p;
apr_status_t status, result;
#ifdef SIGTERM
ruby_signal(SIGTERM, SIG_IGN);
#endif
status = apr_pool_create(&p, NULL);
if (status != APR_SUCCESS)
return status;
status = ruby_call_interpreter(p, SHUTDOWN_RUBY_THREAD, NULL, NULL, NULL);
apr_pool_clear(p);
if (status != APR_SUCCESS)
return status;
status = apr_thread_join(&result, ruby_thread);
return status;
}
else {
#endif
ruby_finalize_interpreter();
APR_CLEANUP_RETURN_SUCCESS();
#if APR_HAS_THREADS
}
#endif
}
static request_rec *fake_request_rec(server_rec *s, pool *p, char *hook)
{
request_rec *r = (request_rec *) apr_pcalloc(p, sizeof(request_rec));
r->pool = p;
r->server = s;
r->per_dir_config = NULL;
r->request_config = NULL;
r->uri = hook;
r->notes = NULL;
return r;
}
static int ruby_handler(request_rec *, array_header *, error_log_data *, ID, int, int);
#ifdef APACHE2
static void ruby_child_init(pool *p, server_rec *s)
#else /* Apache 1.x */
static void ruby_child_init(server_rec *s, pool *p)
#endif
{
ruby_server_config *conf;
request_rec *r;
if (!ruby_running()) {
#if APR_HAS_THREADS
if (ruby_is_threaded_mpm) {
apr_status_t status;
status = apr_thread_mutex_create(&ruby_is_running_mutex,
APR_THREAD_MUTEX_DEFAULT, p);
if (status != APR_SUCCESS) {
ruby_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, s,
"failed to create mutex");
return;
}
status = apr_thread_cond_create(&ruby_is_running_cond, p);
if (status != APR_SUCCESS) {
ruby_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, s,
"failed to create cond");
return;
}
status = apr_thread_mutex_create(&ruby_request_queue_mutex,
APR_THREAD_MUTEX_DEFAULT, p);
if (status != APR_SUCCESS) {
ruby_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, s,
"failed to create mutex");
return;
}
status = apr_thread_cond_create(&ruby_request_queue_cond, p);
if (status != APR_SUCCESS) {
ruby_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, s,
"failed to create cond");
return;
}
status = apr_thread_create(&ruby_thread, NULL, ruby_thread_start, s, p);
if (status != APR_SUCCESS) {
ruby_log_error(APLOG_MARK, APLOG_CRIT | APLOG_NOERRNO, s,
"failed to create ruby thread");
return;
}
apr_thread_mutex_lock(ruby_is_running_mutex);
while (!ruby_running())
apr_thread_cond_wait(ruby_is_running_cond,
ruby_is_running_mutex);
apr_thread_mutex_unlock(ruby_is_running_mutex);
}
else {
#endif
ruby_init_interpreter(s);
ruby_is_running = 1;
#if APR_HAS_THREADS
}
#endif
apr_pool_cleanup_register(p, NULL, ruby_child_cleanup, apr_pool_cleanup_null);
}
r = fake_request_rec(s, p, "RubyChildInitHandler");
conf = get_server_config(r->server);
ruby_handler(r, conf->ruby_child_init_handler, NULL,
rb_intern("child_init"), 0, 0);
}
static void mod_ruby_clearenv(pool *p)
{
char **env;
array_header *names = apr_array_make(p, 1, sizeof(char*));
int i;
env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (s) {
*(char **) apr_array_push(names) = apr_pstrndup(p, *env, s - *env);
}
env++;
}
FREE_ENVIRON(environ);
for (i = 0; i < names->nelts; i++) {
char *name = ((char **) names->elts)[i];
char *val = getenv(name);
if (val) {
ruby_unsetenv(name);
}
}
}
static void mod_ruby_setenv(const char *name, const char *value)
{
if (!name) return;
ruby_unsetenv(name);
if (value && *value)
ruby_setenv(name, value);
}
static void setenv_from_table(table *tbl)
{
const array_header *env_arr;
table_entry *env;
int i;
env_arr = apr_table_elts(tbl);
env = (table_entry *) env_arr->elts;
for (i = 0; i < env_arr->nelts; i++) {
if (env[i].key == NULL)
continue;
mod_ruby_setenv(env[i].key, env[i].val);
}
}