-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod_raii.C
280 lines (231 loc) · 8.7 KB
/
mod_raii.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
/*
* Copyright (c) 2005-2011, Guillaume Gimenez <[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:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of G.Gimenez nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 G.GIMENEZ 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.
*
* Authors:
* * Guillaume Gimenez <[email protected]>
*
*/
#include "raii.H"
#include <new>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
namespace raii {
#ifdef DEBUG_ALLOC
long long int total_mem=0;
int total_objects=0;
#endif
extern "C" {
static void register_hooks(apr_pool_t *p);
static int raii_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) __attribute__((unused));
static void raii_child_init(apr_pool_t *p, server_rec *s) __attribute__((unused));
};
/************************
* Global Dispatch List *
************************/
// We have to use C style linkage for the API functions that will be
// linked to apache.
extern "C" {
module AP_MODULE_DECLARE_DATA raii_module =
{ STANDARD20_MODULE_STUFF,
create_raii_config, /* create per-directory config structure */
merge_raii_configs, /* merge per-directory config structures */
NULL,//create_raii_config, /* create per-server config structure */
NULL,//merge_raii_configs, /* merge per-server config structures */
raii_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};
};
extern "C" {
const command_rec raii_cmds[] =
{
AP_INIT_FLAG("Debug",cfg_debug,NULL,ACCESS_CONF|OR_OPTIONS,
"Weither to print debug informations"),
AP_INIT_FLAG("OverrideEternal",cfg_override_eternal,NULL,ACCESS_CONF|OR_OPTIONS,
"Weither to override eternal attribute of servlets"),
AP_INIT_FLAG("SegfaultHandler",cfg_segfault_handler,NULL,RSRC_CONF,
"Weither to install a Segfault Handler"),
AP_INIT_TAKE1("TmpDir",cfg_tmpdir,NULL,RSRC_CONF|ACCESS_CONF|OR_OPTIONS,
"Where to put preprocessed files"),
AP_INIT_TAKE1("DsoDir",cfg_dsodir,NULL,RSRC_CONF|ACCESS_CONF|OR_OPTIONS,
"Where to put compiled files"),
AP_INIT_TAKE1("RaiippPath",cfg_raiipp,NULL,RSRC_CONF|ACCESS_CONF|OR_OPTIONS,
"Where to find raii preprocessor"),
AP_INIT_TAKE1("RaiiRoute",cfg_raii_route,NULL,RSRC_CONF|ACCESS_CONF|OR_OPTIONS,
"To set a particular route for load balancing"),
AP_INIT_TAKE1("ContextPath",cfg_context_path,NULL,ACCESS_CONF|OR_OPTIONS,
"Where to find raii preprocessor"),
AP_INIT_TAKE1("SqlConnection",cfg_sqlconnection,NULL,RSRC_CONF|ACCESS_CONF|OR_OPTIONS,
"Define a sqlrelay connection url"),
AP_INIT_TAKE1("SqlPoolSize",cfg_sqlpoolsize,NULL,RSRC_CONF|ACCESS_CONF|OR_OPTIONS,
"Define the number of connection"),
AP_INIT_TAKE1("BuildCmd",cfg_buildcmd,NULL,RSRC_CONF|ACCESS_CONF|OR_OPTIONS,
"Where to find Build Command"),
AP_INIT_TAKE2("RaiiParameter",cfg_raiiparameter,NULL,RSRC_CONF|ACCESS_CONF|OR_OPTIONS,
"Define custom parameters"),
/*AP_INIT_RAW("Cflags",cfg_cflags,NULL,ACCESS_CONF|RSRC_CONF,
"Additional cflags to compile with"), */
AP_FINITO
};
};
static void register_hooks(apr_pool_t *p)
{
/* for directories, set the mod_autoindex to be called after us */
//static const char * const after[] = { "mod_include.c", NULL };
ap_hook_handler(raii_launch, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_post_config(raii_init, NULL, NULL, APR_HOOK_LAST);
ap_hook_child_init(raii_child_init, NULL, NULL, APR_HOOK_LAST);
}
//cannot throw in these handlers
static void raii_new_handler() {
raise(SIGABRT);
}
static void raii_terminate_handler() {
raise(SIGABRT);
}
static void raii_unexpected_handler() {
raise(SIGABRT);
}
extern "C" apr_status_t cleanup_sctx(void *sctx) {
Logger log("raii("+itostring(getpid())+")");
log.debug();
log("cleaning ServletContexts");
reinterpret_cast<
Map< String, ptr<ServletContext> >* >(sctx)
->~Map< String, ptr<ServletContext> > ();
return OK;
}
extern "C" void sig_nop(int sig,siginfo_t *siginfo, void *secret) {
return;
}
extern "C" void segfaulth(int sig,siginfo_t *siginfo, void *secret);
extern bool dumpStackFlag;
extern "C" void dispatch_signal(int sig,siginfo_t *siginfo, void *secret) {
pid_t pid=getpid();
pid_t tid=syscall(SYS_gettid);
Logger log("dispatcher");
log ("pid is "+ itostring(pid));
log ("tid is "+ itostring(tid));
if ( pid != tid ) {
log(itostring(tid)+" requests tasktrace");
segfaulth(sig,siginfo,secret);
return;
}
char buf[256];
log("dispatching");
snprintf(buf,256,"/proc/%d/task/",pid);
struct dirent **entry;
int m = scandir(buf,&entry,0,versionsort);
for (int n = 0 ; n < m ; ++n ) {
int task = String(entry[n]->d_name).toi();
free(entry[n]);
if ( !task || task == tid )
continue;
log(itostring(tid)+" sends signal to "+itostring(task));
dumpStackFlag=true;
kill(task,sig);
//pthread_cond... plutôt, non ?
time_t now = time(NULL);
while ( dumpStackFlag && now + 1 >= time(NULL) )
sched_yield();
dumpStackFlag=false;
}
if ( m > 0 ) free(entry);
if ( pid == tid ){
log(itostring(tid)+" requests tasktrace");
segfaulth(sig,siginfo,secret);
}
return;
}
static int raii_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
{
{
struct sigaction act;
act.sa_handler=NULL;
act.sa_sigaction=sig_nop;
sigemptyset(&act.sa_mask);
act.sa_flags=SA_SIGINFO | SA_RESTART;
if (sigaction(SIGUSR2,&act,NULL) )
perror("sigaction");
}
std::set_new_handler(raii_new_handler);
std::set_terminate(raii_terminate_handler);
std::set_unexpected(raii_unexpected_handler);
ap_add_version_component(p, RAII_HEADER_STRING);
return OK;
}
static void raii_child_init(apr_pool_t *p, server_rec *s)
{
if(1){
struct sigaction act;
act.sa_handler=NULL;
act.sa_sigaction=segfaulth;
sigemptyset(&act.sa_mask);
act.sa_flags=SA_SIGINFO | SA_RESTART;
if (sigaction(SIGSEGV,&act,NULL) )
perror("sigaction");
if (sigaction(SIGILL,&act,NULL) )
perror("sigaction");
if (sigaction(SIGFPE,&act,NULL) )
perror("sigaction");
if (sigaction(SIGBUS,&act,NULL) )
perror("sigaction");
if (sigaction(SIGABRT,&act,NULL) )
perror("sigaction");
act.sa_sigaction=dispatch_signal;
if (sigaction(SIGUSR2,&act,NULL) )
perror("sigaction");
}
Logger log("raii("+itostring(getpid())+")");
log.debug();
log("child init");
Lock l(servletContextMutex);
void* servletContext_buf = apr_pcalloc(p, sizeof(Map<String, ptr<ServletContext> >));
apr_pool_cleanup_register(p,servletContext_buf,cleanup_sctx,/*cleanup_sctx*/apr_pool_cleanup_null);
servletContext= new(servletContext_buf) Map <String, ptr<ServletContext> >;
//dans cet ordre dans le but de voir les container désalloués après les pools
raii::sql::SQLDriverContainer::clear();
raii::sql::Connection::clearPools();
}
RaiiConfig *get_dconfig(const request_rec *r)
{
if ( !r ) return NULL;
RaiiConfig *plop=(RaiiConfig *)ap_get_module_config(r->per_dir_config, &raii_module);
return plop;
}
// current server config
RaiiConfig *get_sconfig(const server_rec *s)
{
if ( !s ) return NULL;
return (RaiiConfig *) ap_get_module_config(s->module_config, &raii_module);
}
//current req config
RaiiConfig *get_rconfig(const request_rec *r)
{
if ( !r ) return NULL;
return (RaiiConfig *)ap_get_module_config(r->request_config, &raii_module);
}
}