-
Notifications
You must be signed in to change notification settings - Fork 7
/
flood_farm.c
291 lines (257 loc) · 9.86 KB
/
flood_farm.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
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 Aaron Bannert and Justin Erenkrantz, eBuilt.
*/
#include <apr_errno.h>
#include <apr_thread_proc.h>
#include <apr_strings.h>
#if APR_HAVE_STRINGS_H
#include <strings.h> /* strncasecmp */
#endif
#if APR_HAVE_STRING_H
#include <string.h> /* strncasecmp */
#endif
#if APR_HAVE_STDLIB_H
#include <stdlib.h> /* strtol */
#endif
#include "config.h"
#include "flood_farmer.h"
#include "flood_farm.h"
extern apr_file_t *local_stdout;
extern apr_file_t *local_stderr;
struct farm_t {
const char *name;
int n_farmers;
#if APR_HAS_THREADS
apr_thread_t **farmers; /* pointer to array of threads */
#else
apr_proc_t **farmers; /* pointer to array of processes */
#endif
};
typedef struct farm_t farm_t;
struct farmer_worker_info_t {
const char *farmer_name;
config_t *config;
};
typedef struct farmer_worker_info_t farmer_worker_info_t;
#if APR_HAS_THREADS
/**
* Worker function that is assigned to a thread. Each worker is
* called a farmer in our system.
*/
static void * APR_THREAD_FUNC farmer_worker(apr_thread_t *thd, void *data)
{
apr_status_t stat;
apr_pool_t *pool;
farmer_worker_info_t *info;
info = (farmer_worker_info_t *)data;
pool = apr_thread_pool_get(thd);
/* should we create a subpool here? */
#ifdef FARM_DEBUG
apr_file_printf(local_stdout, "Starting farmer_worker thread '%s'.\n",
info->farmer_name);
#endif
if ((stat = run_farmer(info->config, info->farmer_name,
pool)) != APR_SUCCESS) {
char buf[256];
apr_strerror(stat, (char*) &buf, 256);
apr_file_printf(local_stderr, "Error running farmer '%s': %s.\n",
info->farmer_name, (char*) &buf);
/* just die for now, later try to return status */
}
#if 0 /* this gets uncommented after apr_thread_exit() fixes are commited */
apr_thread_exit(thd, APR_SUCCESS);
#endif
return NULL;
}
#else
static void *farmer_worker(void *data)
{
apr_status_t stat;
apr_pool_t *pool;
farmer_worker_info_t *info;
info = (farmer_worker_info_t *)data;
apr_pool_create(&pool, NULL);
/* should we create a subpool here? */
#ifdef FARM_DEBUG
apr_file_printf(local_stdout, "Starting farmer_worker child '%s'.\n",
info->farmer_name);
#endif
if ((stat = run_farmer(info->config, info->farmer_name,
pool)) != APR_SUCCESS) {
char buf[256];
apr_strerror(stat, (char*) &buf, 256);
apr_file_printf(local_stderr, "Error running farmer '%s': %s.\n",
info->farmer_name, (char*) &buf);
/* just die for now, later try to return status */
}
return NULL;
}
#endif
apr_status_t run_farm(config_t *config, const char *farm_name, apr_pool_t *pool)
{
#if APR_HAS_THREADS
apr_status_t child_stat;
#endif
apr_status_t stat;
int usefarmer_count, i, j;
long farmer_start_count = 1;
apr_time_t farmer_start_delay;
char *xml_farm, **usefarmer_names;
struct apr_xml_elem *e, *root_elem, *farm_elem;
struct apr_xml_attr *use_elem;
farm_t *farm;
farmer_worker_info_t *infovec;
farmer_start_delay = 0;
xml_farm = apr_pstrdup(pool, XML_FARM);
/* get the root config node */
if ((stat = retrieve_root_xml_elem(&root_elem, config)) != APR_SUCCESS) {
return stat;
}
/* get farmer node from config */
if ((stat = retrieve_xml_elem_with_childmatch(
&farm_elem, root_elem,
xml_farm, XML_FARM_NAME, farm_name)) != APR_SUCCESS)
return stat;
/* count the number of "usefarmer" children */
usefarmer_count = 0;
for (e = farm_elem->first_child; e; e = e->next) {
if (strncasecmp(e->name, XML_FARM_USEFARMER, FLOOD_STRLEN_MAX) == 0) {
int found_count = 0;
for (use_elem = e->attr; use_elem; use_elem = use_elem->next) {
if (use_elem->name &&
strncasecmp(use_elem->name, XML_FARM_USEFARMER_COUNT,
FLOOD_STRLEN_MAX) == 0) {
found_count = 1;
usefarmer_count += strtol(use_elem->value, NULL, 10);
}
}
if (!found_count) {
usefarmer_count++;
}
}
}
if (!usefarmer_count)
usefarmer_count = 1;
/* create each of the children and put their names in an array */
usefarmer_names = apr_palloc(pool, sizeof(char*) * (usefarmer_count + 1));
/* set the sentinel (no need for pcalloc()) */
usefarmer_names[usefarmer_count] = NULL;
i = 0;
for (e = farm_elem->first_child; e; e = e->next) {
int handled_usefarmers = 0;
if (strncasecmp(e->name, XML_FARM_USEFARMER, FLOOD_STRLEN_MAX) == 0) {
for (use_elem = e->attr; use_elem; use_elem = use_elem->next) {
if (use_elem->name && strncasecmp(use_elem->name,
XML_FARM_USEFARMER_COUNT, FLOOD_STRLEN_MAX) == 0) {
for (j = strtol(use_elem->value, NULL, 10); j > 0; j--) {
usefarmer_names[i++] = apr_pstrdup(pool,
e->first_cdata.first->text);
}
handled_usefarmers = 1;
}
else if (use_elem->name &&
strncasecmp(use_elem->name, XML_FARM_USEFARMER_DELAY,
FLOOD_STRLEN_MAX) == 0) {
char *endptr;
farmer_start_delay = strtoll(use_elem->value, &endptr, 10);
if (*endptr != '\0')
{
apr_file_printf(local_stderr,
"Attribute %s has invalid value %s.\n",
XML_FARM_USEFARMER_DELAY,
use_elem->value);
return APR_EGENERAL;
}
farmer_start_delay *= APR_USEC_PER_SEC;
}
else if (use_elem->name &&
strncasecmp(use_elem->name, XML_FARM_USEFARMER_START,
FLOOD_STRLEN_MAX) == 0) {
char *endptr;
farmer_start_count = strtol(use_elem->value, &endptr, 10);
if (*endptr != '\0')
{
apr_file_printf(local_stderr,
"Attribute %s has invalid value %s.\n",
XML_FARM_USEFARMER_START,
use_elem->value);
return APR_EGENERAL;
}
}
}
if (!handled_usefarmers)
usefarmer_names[i++] = apr_pstrdup(pool,
e->first_cdata.first->text);
}
}
/* create the farm object */
farm = apr_pcalloc(pool, sizeof(farm_t));
farm->name = apr_pstrdup(pool, farm_name);
farm->n_farmers = usefarmer_count;
#if APR_HAS_THREADS
farm->farmers = apr_pcalloc(pool,
sizeof(apr_thread_t*) * (usefarmer_count + 1));
apr_thread_mutex_create(&config->mutex, APR_THREAD_MUTEX_DEFAULT, pool);
#else
farm->farmers = apr_pcalloc(pool,
sizeof(apr_proc_t*) * (usefarmer_count + 1));
for (i = 0; i < usefarmer_count + 1; i++)
farm->farmers[i] = apr_pcalloc(pool, sizeof(apr_proc_t*));
#endif
infovec = apr_pcalloc(pool, sizeof(farmer_worker_info_t) * usefarmer_count);
/* for each of my farmers, start them */
for (i = 0; i < usefarmer_count; i++) {
infovec[i].farmer_name = usefarmer_names[i];
infovec[i].config = config;
#if APR_HAS_THREADS
if ((stat = apr_thread_create(&farm->farmers[i],
NULL,
farmer_worker,
(void*) &infovec[i],
pool)) != APR_SUCCESS) {
/* error, perhaps shutdown other threads then exit? */
return stat;
}
#else
if (apr_proc_fork(farm->farmers[i], pool) == APR_INCHILD)
{
farmer_worker(&infovec[i]);
exit(0);
}
#endif
if (farmer_start_delay && (i+1) % farmer_start_count == 0)
apr_sleep(farmer_start_delay);
}
for (i = 0; i < usefarmer_count; i++) {
#if APR_HAS_THREADS
stat = apr_thread_join(&child_stat, farm->farmers[i]);
if (stat != APR_SUCCESS && stat != APR_INCOMPLETE) {
#else
if ((stat = apr_proc_wait(farm->farmers[i], NULL, NULL, APR_WAIT)) != APR_CHILD_DONE) {
#endif
apr_file_printf(local_stderr, "Error joining farmer thread '%d' ('%s').\n",
i, usefarmer_names[i]);
return stat;
} else {
#ifdef FARM_DEBUG
apr_file_printf(local_stdout, "Farmer '%d' ('%s') completed successfully.\n",
i, usefarmer_names[i]);
#endif
}
}
return APR_SUCCESS;
}