File: | lib/lwan.c |
Warning: | line 339, column 17 Potential leak of memory pointed to by 'value_copy' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * lwan - web server | |||
3 | * Copyright (c) 2012, 2013 L. A. F. Pereira <l@tia.mat.br> | |||
4 | * | |||
5 | * This program is free software; you can redistribute it and/or | |||
6 | * modify it under the terms of the GNU General Public License | |||
7 | * as published by the Free Software Foundation; either version 2 | |||
8 | * of the License, or any later version. | |||
9 | * | |||
10 | * This program is distributed in the hope that it will be useful, | |||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
13 | * GNU General Public License for more details. | |||
14 | * | |||
15 | * You should have received a copy of the GNU General Public License | |||
16 | * along with this program; if not, write to the Free Software | |||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | |||
18 | * USA. | |||
19 | */ | |||
20 | ||||
21 | #define _GNU_SOURCE | |||
22 | #include <assert.h> | |||
23 | #include <ctype.h> | |||
24 | #include <dlfcn.h> | |||
25 | #include <errno(*__errno_location ()).h> | |||
26 | #include <fcntl.h> | |||
27 | #include <libproc.h> | |||
28 | #include <limits.h> | |||
29 | #include <signal.h> | |||
30 | #include <stdlib.h> | |||
31 | #include <string.h> | |||
32 | #include <sys/resource.h> | |||
33 | #include <sys/socket.h> | |||
34 | #include <sys/types.h> | |||
35 | #include <unistd.h> | |||
36 | ||||
37 | #include "lwan-private.h" | |||
38 | ||||
39 | #include "lwan-config.h" | |||
40 | #include "lwan-http-authorize.h" | |||
41 | ||||
42 | #if defined(LWAN_HAVE_LUA) | |||
43 | #include "lwan-lua.h" | |||
44 | #endif | |||
45 | ||||
46 | /* Ideally, this would check if all items in enum lwan_request_flags, | |||
47 | * when bitwise-or'd together, would not have have any bit set that | |||
48 | * is also set in REQUEST_METHOD_MASK. */ | |||
49 | static_assert_Static_assert(REQUEST_ACCEPT_DEFLATE > REQUEST_METHOD_MASK, | |||
50 | "enough bits to store request methods"); | |||
51 | ||||
52 | /* See detect_fastest_monotonic_clock() */ | |||
53 | clockid_t monotonic_clock_id = CLOCK_MONOTONIC1; | |||
54 | ||||
55 | static const struct lwan_config default_config = { | |||
56 | .listener = "localhost:8080", | |||
57 | .keep_alive_timeout = 15, | |||
58 | .quiet = false0, | |||
59 | .proxy_protocol = false0, | |||
60 | .allow_cors = false0, | |||
61 | .expires = 1 * ONE_WEEK(((60 * 60) * 24) * 7), | |||
62 | .n_threads = 0, | |||
63 | .max_post_data_size = 10 * DEFAULT_BUFFER_SIZE4096, | |||
64 | .allow_post_temp_file = false0, | |||
65 | .max_put_data_size = 10 * DEFAULT_BUFFER_SIZE4096, | |||
66 | .allow_put_temp_file = false0, | |||
67 | .use_dynamic_buffer = false0, | |||
68 | }; | |||
69 | ||||
70 | LWAN_HANDLER_ROUTE(brew_coffee, NULL /* do not autodetect this route */)static enum lwan_http_status lwan_handler_brew_coffee( struct lwan_request *, struct lwan_response *, void *); static const struct lwan_handler_info __attribute__((used, section("lwan_handler" ))) __attribute__((aligned(8))) lwan_handler_info_brew_coffee = { .name = "brew_coffee", .route = ((void*)0), .handler = lwan_handler_brew_coffee , }; __attribute__((used)) static enum lwan_http_status lwan_handler_brew_coffee ( struct lwan_request *request __attribute__((unused)), struct lwan_response *response __attribute__((unused)), void *data __attribute__ ((unused))) | |||
71 | { | |||
72 | /* Placeholder handler so that __start_lwan_handler and __stop_lwan_handler | |||
73 | * symbols will get defined. | |||
74 | */ | |||
75 | return HTTP_I_AM_A_TEAPOT; | |||
76 | } | |||
77 | ||||
78 | __attribute__((no_sanitize_address)) | |||
79 | static void *find_handler(const char *name) | |||
80 | { | |||
81 | const struct lwan_handler_info *handler; | |||
82 | ||||
83 | LWAN_SECTION_FOREACH(lwan_handler, handler)for (handler = ({ extern const typeof(*handler) __start_lwan_handler []; __start_lwan_handler; }); handler < ({ extern const typeof (*handler) __stop_lwan_handler[]; __stop_lwan_handler; }); (handler )++) { | |||
84 | if (streq(handler->name, name)) | |||
85 | return handler->handler; | |||
86 | } | |||
87 | ||||
88 | return NULL((void*)0); | |||
89 | } | |||
90 | ||||
91 | __attribute__((no_sanitize_address)) | |||
92 | static const struct lwan_module *find_module(const char *name) | |||
93 | { | |||
94 | const struct lwan_module_info *module; | |||
95 | ||||
96 | LWAN_SECTION_FOREACH(lwan_module, module)for (module = ({ extern const typeof(*module) __start_lwan_module []; __start_lwan_module; }); module < ({ extern const typeof (*module) __stop_lwan_module[]; __stop_lwan_module; }); (module )++) { | |||
97 | if (streq(module->name, name)) | |||
98 | return module->module; | |||
99 | } | |||
100 | ||||
101 | return NULL((void*)0); | |||
102 | } | |||
103 | ||||
104 | static void destroy_urlmap(void *data) | |||
105 | { | |||
106 | struct lwan_url_map *url_map = data; | |||
107 | ||||
108 | if (url_map->module) { | |||
109 | const struct lwan_module *module = url_map->module; | |||
110 | ||||
111 | if (module->destroy) | |||
112 | module->destroy(url_map->data); | |||
113 | } else if (url_map->data && url_map->flags & HANDLER_DATA_IS_HASH_TABLE) { | |||
114 | hash_free(url_map->data); | |||
115 | } | |||
116 | ||||
117 | free(url_map->authorization.realm); | |||
118 | free(url_map->authorization.password_file); | |||
119 | free((char *)url_map->prefix); | |||
120 | free(url_map); | |||
121 | } | |||
122 | ||||
123 | static struct lwan_url_map *add_url_map(struct lwan_trie *t, const char *prefix, | |||
124 | const struct lwan_url_map *map) | |||
125 | { | |||
126 | struct lwan_url_map *copy = malloc(sizeof(*copy)); | |||
127 | ||||
128 | if (!copy) | |||
129 | lwan_status_critical_perror("Could not copy URL map")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 129, __FUNCTION__, "Could not copy URL map"); | |||
130 | ||||
131 | memcpy(copy, map, sizeof(*copy)); | |||
132 | ||||
133 | copy->prefix = strdup(prefix ? prefix : copy->prefix); | |||
134 | if (!copy->prefix) | |||
135 | lwan_status_critical_perror("Could not copy URL prefix")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 135, __FUNCTION__, "Could not copy URL prefix"); | |||
136 | ||||
137 | copy->prefix_len = strlen(copy->prefix); | |||
138 | lwan_trie_add(t, copy->prefix, copy); | |||
139 | ||||
140 | return copy; | |||
141 | } | |||
142 | ||||
143 | static bool_Bool can_override_header(const char *name) | |||
144 | { | |||
145 | /* NOTE: Update lwan_prepare_response_header_full() in lwan-response.c | |||
146 | * if new headers are added here. */ | |||
147 | ||||
148 | if (strcaseequal_neutral(name, "Date")) | |||
149 | return false0; | |||
150 | if (strcaseequal_neutral(name, "Expires")) | |||
151 | return false0; | |||
152 | if (strcaseequal_neutral(name, "WWW-Authenticate")) | |||
153 | return false0; | |||
154 | if (strcaseequal_neutral(name, "Connection")) | |||
155 | return false0; | |||
156 | if (strcaseequal_neutral(name, "Content-Type")) | |||
157 | return false0; | |||
158 | if (strcaseequal_neutral(name, "Transfer-Encoding")) | |||
159 | return false0; | |||
160 | if (!strncasecmp(name, "Access-Control-Allow-", | |||
161 | sizeof("Access-Control-Allow-") - 1)) | |||
162 | return false0; | |||
163 | ||||
164 | return true1; | |||
165 | } | |||
166 | ||||
167 | static void build_response_headers(struct lwan *l, | |||
168 | const struct lwan_key_value *kv) | |||
169 | { | |||
170 | struct lwan_strbuf strbuf; | |||
171 | bool_Bool set_server = false0; | |||
172 | ||||
173 | assert(l)((void) sizeof ((l) ? 1 : 0), __extension__ ({ if (l) ; else __assert_fail ("l", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 173, __extension__ __PRETTY_FUNCTION__); })); | |||
174 | ||||
175 | lwan_strbuf_init(&strbuf); | |||
176 | ||||
177 | for (; kv && kv->key; kv++) { | |||
178 | if (!can_override_header(kv->key)) { | |||
179 | lwan_status_warning("Cannot override header '%s'", kv->key)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 179, __FUNCTION__, "Cannot override header '%s'", kv->key ); | |||
180 | } else { | |||
181 | if (strcaseequal_neutral(kv->key, "Server")) | |||
182 | set_server = true1; | |||
183 | ||||
184 | lwan_strbuf_append_printf(&strbuf, "\r\n%s: %s", kv->key, | |||
185 | kv->value); | |||
186 | } | |||
187 | } | |||
188 | ||||
189 | if (!set_server) | |||
190 | lwan_strbuf_append_strz(&strbuf, "\r\nServer: lwan"); | |||
191 | ||||
192 | lwan_strbuf_append_strz(&strbuf, "\r\n\r\n"); | |||
193 | ||||
194 | l->headers = (struct lwan_value){.value = lwan_strbuf_get_buffer(&strbuf), | |||
195 | .len = lwan_strbuf_get_length(&strbuf)}; | |||
196 | } | |||
197 | ||||
198 | static void parse_global_headers(struct config *c, | |||
199 | struct lwan *lwan) | |||
200 | { | |||
201 | struct lwan_key_value_array hdrs; | |||
202 | const struct config_line *l; | |||
203 | struct lwan_key_value *kv; | |||
204 | ||||
205 | lwan_key_value_array_init(&hdrs); | |||
206 | ||||
207 | while ((l = config_read_line(c))) { | |||
208 | switch (l->type) { | |||
209 | case CONFIG_LINE_TYPE_SECTION: | |||
210 | config_error( | |||
211 | c, "No sections are supported under the 'headers' section"); | |||
212 | goto cleanup; | |||
213 | ||||
214 | case CONFIG_LINE_TYPE_LINE: | |||
215 | kv = lwan_key_value_array_append(&hdrs); | |||
216 | if (!kv) { | |||
217 | lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 218, __FUNCTION__, "Could not allocate memory for custom response header" ) | |||
218 | "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 218, __FUNCTION__, "Could not allocate memory for custom response header" ); | |||
219 | } | |||
220 | ||||
221 | kv->key = strdup(l->key); | |||
222 | if (!kv->key) { | |||
223 | lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 224, __FUNCTION__, "Could not allocate memory for custom response header" ) | |||
224 | "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 224, __FUNCTION__, "Could not allocate memory for custom response header" ); | |||
225 | } | |||
226 | ||||
227 | kv->value = strdup(l->value); | |||
228 | if (!kv->value) { | |||
229 | lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 230, __FUNCTION__, "Could not allocate memory for custom response header" ) | |||
230 | "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 230, __FUNCTION__, "Could not allocate memory for custom response header" ); | |||
231 | } | |||
232 | break; | |||
233 | ||||
234 | case CONFIG_LINE_TYPE_SECTION_END: | |||
235 | kv = lwan_key_value_array_append(&hdrs); | |||
236 | if (!kv) { | |||
237 | lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 238, __FUNCTION__, "Could not allocate memory for custom response header" ) | |||
238 | "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 238, __FUNCTION__, "Could not allocate memory for custom response header" ); | |||
239 | } | |||
240 | ||||
241 | kv->key = NULL((void*)0); | |||
242 | kv->value = NULL((void*)0); | |||
243 | ||||
244 | build_response_headers(lwan, lwan_key_value_array_get_array(&hdrs)); | |||
245 | goto cleanup; | |||
246 | } | |||
247 | } | |||
248 | ||||
249 | config_error(c, "EOF while looking for end of 'headers' section"); | |||
250 | ||||
251 | cleanup: | |||
252 | LWAN_ARRAY_FOREACH (&hdrs, kv)for (kv = (&hdrs)->base.base; kv < ((typeof(kv))(& hdrs)->base.base + (&hdrs)->base.elements); kv++) { | |||
253 | free(kv->key); | |||
254 | free(kv->value); | |||
255 | } | |||
256 | lwan_key_value_array_reset(&hdrs); | |||
257 | } | |||
258 | ||||
259 | static void parse_listener_prefix_authorization(struct config *c, | |||
260 | const struct config_line *l, | |||
261 | struct lwan_url_map *url_map) | |||
262 | { | |||
263 | if (!streq(l->value, "basic")) { | |||
264 | config_error(c, "Only basic authorization supported"); | |||
265 | return; | |||
266 | } | |||
267 | ||||
268 | memset(&url_map->authorization, 0, sizeof(url_map->authorization)); | |||
269 | ||||
270 | while ((l = config_read_line(c))) { | |||
271 | switch (l->type) { | |||
272 | case CONFIG_LINE_TYPE_LINE: | |||
273 | if (streq(l->key, "realm")) { | |||
274 | free(url_map->authorization.realm); | |||
275 | url_map->authorization.realm = strdup(l->value); | |||
276 | } else if (streq(l->key, "password_file")) { | |||
277 | free(url_map->authorization.password_file); | |||
278 | url_map->authorization.password_file = realpath(l->value, NULL((void*)0)); | |||
279 | if (!url_map->authorization.password_file) | |||
280 | config_error(c, "Could not determine full path for password file: %s", l->value); | |||
281 | } | |||
282 | break; | |||
283 | ||||
284 | case CONFIG_LINE_TYPE_SECTION: | |||
285 | config_error(c, "Unexpected section: %s", l->key); | |||
286 | goto error; | |||
287 | ||||
288 | case CONFIG_LINE_TYPE_SECTION_END: | |||
289 | if (!url_map->authorization.realm) | |||
290 | url_map->authorization.realm = strdup("Lwan"); | |||
291 | if (!url_map->authorization.password_file) | |||
292 | url_map->authorization.password_file = strdup("htpasswd"); | |||
293 | ||||
294 | url_map->flags |= HANDLER_MUST_AUTHORIZE; | |||
295 | return; | |||
296 | } | |||
297 | } | |||
298 | ||||
299 | config_error(c, "Could not find end of authorization section"); | |||
300 | ||||
301 | error: | |||
302 | free(url_map->authorization.realm); | |||
303 | free(url_map->authorization.password_file); | |||
304 | } | |||
305 | ||||
306 | __attribute__((no_sanitize_address)) | |||
307 | static const char *get_module_name(const struct lwan_module *module) | |||
308 | { | |||
309 | const struct lwan_module_info *iter; | |||
310 | ||||
311 | LWAN_SECTION_FOREACH(lwan_module, iter)for (iter = ({ extern const typeof(*iter) __start_lwan_module []; __start_lwan_module; }); iter < ({ extern const typeof (*iter) __stop_lwan_module[]; __stop_lwan_module; }); (iter)++ ) { | |||
312 | if (iter->module == module) | |||
313 | return iter->name; | |||
314 | } | |||
315 | ||||
316 | return "<unknown>"; | |||
317 | } | |||
318 | ||||
319 | static void parse_listener_prefix(struct config *c, | |||
320 | const struct config_line *l, | |||
321 | struct lwan *lwan, | |||
322 | const struct lwan_module *module, | |||
323 | void *handler) | |||
324 | { | |||
325 | struct lwan_url_map url_map = {}; | |||
326 | struct hash *hash = hash_str_new(free, free); | |||
327 | char *prefix = strdupa(l->value)(__extension__ ({ const char *__old = (l->value); size_t __len = strlen (__old) + 1; char *__new = (char *) __builtin_alloca (__len); (char *) memcpy (__new, __old, __len); })); | |||
328 | struct config *isolated; | |||
329 | ||||
330 | if (!hash) | |||
331 | lwan_status_critical("Could not allocate hash table")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 331, __FUNCTION__, "Could not allocate hash table"); | |||
332 | ||||
333 | isolated = config_isolate_section(c, l); | |||
334 | if (!isolated) { | |||
335 | config_error(c, "Could not isolate configuration file"); | |||
336 | goto out; | |||
337 | } | |||
338 | ||||
339 | while ((l = config_read_line(c))) { | |||
| ||||
340 | switch (l->type) { | |||
341 | case CONFIG_LINE_TYPE_LINE: { | |||
342 | char *key_copy = strdup(l->key); | |||
343 | char *value_copy = strdup(l->value); | |||
344 | ||||
345 | if (!key_copy) | |||
346 | lwan_status_critical("Could not copy key from config file")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 346, __FUNCTION__, "Could not copy key from config file"); | |||
347 | if (!value_copy) | |||
348 | lwan_status_critical("Could not copy value from config file")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 348, __FUNCTION__, "Could not copy value from config file"); | |||
349 | ||||
350 | hash_add(hash, key_copy, value_copy); | |||
351 | break; | |||
352 | } | |||
353 | ||||
354 | case CONFIG_LINE_TYPE_SECTION: | |||
355 | if (streq(l->key, "authorization")) { | |||
356 | parse_listener_prefix_authorization(c, l, &url_map); | |||
357 | } else if (!config_skip_section(c, l)) { | |||
358 | config_error(c, "Could not skip section"); | |||
359 | goto out; | |||
360 | } | |||
361 | break; | |||
362 | ||||
363 | case CONFIG_LINE_TYPE_SECTION_END: | |||
364 | goto add_map; | |||
365 | } | |||
366 | } | |||
367 | ||||
368 | config_error(c, "Expecting section end while parsing prefix"); | |||
369 | goto out; | |||
370 | ||||
371 | add_map: | |||
372 | assert((handler && !module) || (!handler && module))((void) sizeof (((handler && !module) || (!handler && module)) ? 1 : 0), __extension__ ({ if ((handler && ! module) || (!handler && module)) ; else __assert_fail ("(handler && !module) || (!handler && module)" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 372, __extension__ __PRETTY_FUNCTION__); })); | |||
373 | ||||
374 | if (handler) { | |||
375 | url_map.handler = handler; | |||
376 | url_map.flags |= HANDLER_PARSE_MASK | HANDLER_DATA_IS_HASH_TABLE; | |||
377 | url_map.data = hash; | |||
378 | url_map.module = NULL((void*)0); | |||
379 | ||||
380 | hash = NULL((void*)0); | |||
381 | } else if (module->create_from_hash && module->handle_request) { | |||
382 | lwan_status_debug("Initializing module %s from config",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 383, __FUNCTION__, "Initializing module %s from config", get_module_name (module)) | |||
383 | get_module_name(module))lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 383, __FUNCTION__, "Initializing module %s from config", get_module_name (module)); | |||
384 | ||||
385 | url_map.data = module->create_from_hash(prefix, hash); | |||
386 | if (!url_map.data) { | |||
387 | config_error(c, "Could not create module instance"); | |||
388 | goto out; | |||
389 | } | |||
390 | ||||
391 | if (module->parse_conf && !module->parse_conf(url_map.data, isolated)) { | |||
392 | const char *msg = config_last_error(isolated); | |||
393 | ||||
394 | config_error(c, "Error from module: %s", msg ? msg : "Unknown"); | |||
395 | goto out; | |||
396 | } | |||
397 | ||||
398 | url_map.handler = module->handle_request; | |||
399 | url_map.flags |= module->flags; | |||
400 | url_map.module = module; | |||
401 | } else if (UNLIKELY(!module->create_from_hash)__builtin_expect(((!module->create_from_hash)), (0))) { | |||
402 | config_error(c, "Module isn't prepared to load settings from a file; " | |||
403 | "create_from_hash() method isn't present"); | |||
404 | goto out; | |||
405 | } else if (UNLIKELY(!module->handle_request)__builtin_expect(((!module->handle_request)), (0))) { | |||
406 | config_error(c, "Module does not have handle_request() method"); | |||
407 | goto out; | |||
408 | } | |||
409 | ||||
410 | add_url_map(&lwan->url_map_trie, prefix, &url_map); | |||
411 | ||||
412 | out: | |||
413 | hash_free(hash); | |||
414 | config_close(isolated); | |||
415 | } | |||
416 | ||||
417 | static void register_url_map(struct lwan *l, const struct lwan_url_map *map) | |||
418 | { | |||
419 | struct lwan_url_map *copy = add_url_map(&l->url_map_trie, NULL((void*)0), map); | |||
420 | ||||
421 | if (copy->module && copy->module->create) { | |||
422 | lwan_status_debug("Initializing module %s from struct",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 423, __FUNCTION__, "Initializing module %s from struct", get_module_name (copy->module)) | |||
423 | get_module_name(copy->module))lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 423, __FUNCTION__, "Initializing module %s from struct", get_module_name (copy->module)); | |||
424 | ||||
425 | copy->data = copy->module->create(map->prefix, copy->args); | |||
426 | if (!copy->data) { | |||
427 | lwan_status_critical("Could not initialize module %s",lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 428, __FUNCTION__, "Could not initialize module %s", get_module_name (copy->module)) | |||
428 | get_module_name(copy->module))lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 428, __FUNCTION__, "Could not initialize module %s", get_module_name (copy->module)); | |||
429 | } | |||
430 | ||||
431 | copy->flags = copy->module->flags; | |||
432 | copy->handler = copy->module->handle_request; | |||
433 | } else { | |||
434 | copy->flags = HANDLER_PARSE_MASK; | |||
435 | } | |||
436 | } | |||
437 | ||||
438 | void lwan_set_url_map(struct lwan *l, const struct lwan_url_map *map) | |||
439 | { | |||
440 | lwan_trie_destroy(&l->url_map_trie); | |||
441 | if (UNLIKELY(!lwan_trie_init(&l->url_map_trie, destroy_urlmap))__builtin_expect(((!lwan_trie_init(&l->url_map_trie, destroy_urlmap ))), (0))) | |||
442 | lwan_status_critical_perror("Could not initialize trie")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 442, __FUNCTION__, "Could not initialize trie"); | |||
443 | ||||
444 | for (; map->prefix; map++) | |||
445 | register_url_map(l, map); | |||
446 | } | |||
447 | ||||
448 | __attribute__((no_sanitize_address)) | |||
449 | void lwan_detect_url_map(struct lwan *l) | |||
450 | { | |||
451 | const struct lwan_handler_info *iter; | |||
452 | ||||
453 | lwan_trie_destroy(&l->url_map_trie); | |||
454 | if (UNLIKELY(!lwan_trie_init(&l->url_map_trie, destroy_urlmap))__builtin_expect(((!lwan_trie_init(&l->url_map_trie, destroy_urlmap ))), (0))) | |||
455 | lwan_status_critical_perror("Could not initialize trie")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 455, __FUNCTION__, "Could not initialize trie"); | |||
456 | ||||
457 | LWAN_SECTION_FOREACH(lwan_handler, iter)for (iter = ({ extern const typeof(*iter) __start_lwan_handler []; __start_lwan_handler; }); iter < ({ extern const typeof (*iter) __stop_lwan_handler[]; __stop_lwan_handler; }); (iter )++) { | |||
458 | if (!iter->route) | |||
459 | continue; | |||
460 | ||||
461 | const struct lwan_url_map map = {.prefix = iter->route, | |||
462 | .handler = iter->handler}; | |||
463 | register_url_map(l, &map); | |||
464 | } | |||
465 | } | |||
466 | ||||
467 | const char *lwan_get_config_path(char *path_buf, size_t path_buf_len) | |||
468 | { | |||
469 | char buffer[PATH_MAX4096]; | |||
470 | ||||
471 | if (proc_pidpath(getpid(), buffer, sizeof(buffer)) < 0) | |||
472 | goto out; | |||
473 | ||||
474 | char *path = strrchr(buffer, '/'); | |||
475 | if (!path) | |||
476 | goto out; | |||
477 | int ret = snprintf(path_buf, path_buf_len, "%s.conf", path + 1); | |||
478 | if (ret < 0 || ret >= (int)path_buf_len) | |||
479 | goto out; | |||
480 | ||||
481 | return path_buf; | |||
482 | ||||
483 | out: | |||
484 | return "lwan.conf"; | |||
485 | } | |||
486 | ||||
487 | static void parse_tls_listener(struct config *conf, const struct config_line *line, struct lwan *lwan) | |||
488 | { | |||
489 | #if !defined(LWAN_HAVE_MBEDTLS) | |||
490 | config_error(conf, "Lwan has been built without mbedTLS support"); | |||
491 | return; | |||
492 | #endif | |||
493 | ||||
494 | lwan->config.tls_listener = strdup(line->value); | |||
495 | if (!lwan->config.tls_listener) { | |||
496 | config_error(conf, "Could not allocate memory for tls_listener"); | |||
497 | return; | |||
498 | } | |||
499 | ||||
500 | lwan->config.ssl.cert = NULL((void*)0); | |||
501 | lwan->config.ssl.key = NULL((void*)0); | |||
502 | ||||
503 | while ((line = config_read_line(conf))) { | |||
504 | switch (line->type) { | |||
505 | case CONFIG_LINE_TYPE_SECTION_END: | |||
506 | if (!lwan->config.ssl.cert) | |||
507 | config_error(conf, "Missing path to certificate"); | |||
508 | if (!lwan->config.ssl.key) | |||
509 | config_error(conf, "Missing path to private key"); | |||
510 | return; | |||
511 | case CONFIG_LINE_TYPE_SECTION: | |||
512 | config_error(conf, "Unexpected section: %s", line->key); | |||
513 | return; | |||
514 | case CONFIG_LINE_TYPE_LINE: | |||
515 | if (streq(line->key, "cert")) { | |||
516 | free(lwan->config.ssl.cert); | |||
517 | lwan->config.ssl.cert = strdup(line->value); | |||
518 | if (!lwan->config.ssl.cert) | |||
519 | return lwan_status_critical("Could not copy string")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 519, __FUNCTION__, "Could not copy string"); | |||
520 | } else if (streq(line->key, "key")) { | |||
521 | free(lwan->config.ssl.key); | |||
522 | lwan->config.ssl.key = strdup(line->value); | |||
523 | if (!lwan->config.ssl.key) | |||
524 | return lwan_status_critical("Could not copy string")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 524, __FUNCTION__, "Could not copy string"); | |||
525 | } else if (streq(line->key, "hsts")) { | |||
526 | lwan->config.ssl.send_hsts_header = parse_bool(line->value, false0); | |||
527 | } else { | |||
528 | config_error(conf, "Unexpected key: %s", line->key); | |||
529 | } | |||
530 | } | |||
531 | } | |||
532 | ||||
533 | config_error(conf, "Expecting section end while parsing SSL configuration"); | |||
534 | } | |||
535 | ||||
536 | static void | |||
537 | parse_listener(struct config *c, const struct config_line *l, struct lwan *lwan) | |||
538 | { | |||
539 | lwan->config.listener = strdup(l->value); | |||
540 | if (!lwan->config.listener) | |||
541 | config_error(c, "Could not allocate memory for listener"); | |||
542 | ||||
543 | while ((l = config_read_line(c))) { | |||
544 | switch (l->type) { | |||
545 | case CONFIG_LINE_TYPE_LINE: | |||
546 | config_error(c, "Unexpected key %s", l->key); | |||
547 | return; | |||
548 | case CONFIG_LINE_TYPE_SECTION: | |||
549 | config_error(c, "Unexpected section %s", l->key); | |||
550 | return; | |||
551 | case CONFIG_LINE_TYPE_SECTION_END: | |||
552 | return; | |||
553 | } | |||
554 | } | |||
555 | ||||
556 | config_error(c, "Unexpected EOF while parsing listener"); | |||
557 | } | |||
558 | ||||
559 | static void | |||
560 | parse_site(struct config *c, const struct config_line *l, struct lwan *lwan) | |||
561 | { | |||
562 | while ((l = config_read_line(c))) { | |||
563 | switch (l->type) { | |||
564 | case CONFIG_LINE_TYPE_LINE: | |||
565 | config_error(c, "Expecting prefix section"); | |||
566 | return; | |||
567 | case CONFIG_LINE_TYPE_SECTION: | |||
568 | /* FIXME: per-site authorization? */ | |||
569 | ||||
570 | if (l->key[0] == '&') { | |||
571 | void *handler = find_handler(l->key + 1); | |||
572 | if (handler) { | |||
573 | parse_listener_prefix(c, l, lwan, NULL((void*)0), handler); | |||
574 | continue; | |||
575 | } | |||
576 | ||||
577 | config_error(c, "Could not find handler name: %s", l->key + 1); | |||
578 | return; | |||
579 | } | |||
580 | ||||
581 | const struct lwan_module *module = find_module(l->key); | |||
582 | if (module) { | |||
583 | parse_listener_prefix(c, l, lwan, module, NULL((void*)0)); | |||
584 | continue; | |||
585 | } | |||
586 | ||||
587 | config_error(c, "Invalid section or module not found: %s", l->key); | |||
588 | return; | |||
589 | case CONFIG_LINE_TYPE_SECTION_END: | |||
590 | return; | |||
591 | } | |||
592 | } | |||
593 | ||||
594 | config_error(c, "Expecting section end while parsing listener"); | |||
595 | } | |||
596 | ||||
597 | static bool_Bool setup_from_config(struct lwan *lwan, const char *path) | |||
598 | { | |||
599 | const struct config_line *line; | |||
600 | struct config *conf; | |||
601 | bool_Bool has_site = false0; | |||
602 | bool_Bool has_listener = false0; | |||
603 | bool_Bool has_tls_listener = false0; | |||
604 | char path_buf[PATH_MAX4096]; | |||
605 | ||||
606 | if (!path
| |||
607 | path = lwan_get_config_path(path_buf, sizeof(path_buf)); | |||
608 | lwan_status_info("Loading configuration file: %s", path)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 608, __FUNCTION__, "Loading configuration file: %s", path); | |||
609 | ||||
610 | conf = config_open(path); | |||
611 | if (!conf) | |||
612 | return false0; | |||
613 | ||||
614 | if (!lwan_trie_init(&lwan->url_map_trie, destroy_urlmap)) | |||
615 | return false0; | |||
616 | ||||
617 | while ((line = config_read_line(conf))) { | |||
618 | switch (line->type) { | |||
619 | case CONFIG_LINE_TYPE_LINE: | |||
620 | if (streq(line->key, "keep_alive_timeout")) { | |||
621 | lwan->config.keep_alive_timeout = (unsigned int)parse_long( | |||
622 | line->value, default_config.keep_alive_timeout); | |||
623 | } else if (streq(line->key, "quiet")) { | |||
624 | lwan->config.quiet = | |||
625 | parse_bool(line->value, default_config.quiet); | |||
626 | } else if (streq(line->key, "proxy_protocol")) { | |||
627 | lwan->config.proxy_protocol = | |||
628 | parse_bool(line->value, default_config.proxy_protocol); | |||
629 | } else if (streq(line->key, "allow_cors")) { | |||
630 | lwan->config.allow_cors = | |||
631 | parse_bool(line->value, default_config.allow_cors); | |||
632 | } else if (streq(line->key, "use_dynamic_buffer")) { | |||
633 | lwan->config.use_dynamic_buffer = | |||
634 | parse_bool(line->value, default_config.use_dynamic_buffer); | |||
635 | } else if (streq(line->key, "expires")) { | |||
636 | lwan->config.expires = | |||
637 | parse_time_period(line->value, default_config.expires); | |||
638 | } else if (streq(line->key, "error_template")) { | |||
639 | free(lwan->config.error_template); | |||
640 | lwan->config.error_template = strdup(line->value); | |||
641 | } else if (streq(line->key, "threads")) { | |||
642 | long n_threads = | |||
643 | parse_long(line->value, default_config.n_threads); | |||
644 | if (n_threads < 0) | |||
645 | config_error(conf, "Invalid number of threads: %ld", | |||
646 | n_threads); | |||
647 | lwan->config.n_threads = (unsigned int)n_threads; | |||
648 | } else if (streq(line->key, "max_post_data_size")) { | |||
649 | long max_post_data_size = parse_long( | |||
650 | line->value, (long)default_config.max_post_data_size); | |||
651 | if (max_post_data_size < 0) | |||
652 | config_error(conf, "Negative maximum post data size"); | |||
653 | else if (max_post_data_size > 128 * (1 << 20)) | |||
654 | config_error(conf, | |||
655 | "Maximum post data can't be over 128MiB"); | |||
656 | lwan->config.max_post_data_size = (size_t)max_post_data_size; | |||
657 | } else if (streq(line->key, "max_put_data_size")) { | |||
658 | long max_put_data_size = parse_long( | |||
659 | line->value, (long)default_config.max_put_data_size); | |||
660 | if (max_put_data_size < 0) | |||
661 | config_error(conf, "Negative maximum put data size"); | |||
662 | else if (max_put_data_size > 128 * (1 << 20)) | |||
663 | config_error(conf, | |||
664 | "Maximum put data can't be over 128MiB"); | |||
665 | lwan->config.max_put_data_size = (size_t)max_put_data_size; | |||
666 | } else if (streq(line->key, "allow_temp_files")) { | |||
667 | bool_Bool has_post, has_put; | |||
668 | ||||
669 | if (strstr(line->value, "all")) { | |||
670 | has_post = has_put = true1; | |||
671 | } else { | |||
672 | has_post = !!strstr(line->value, "post"); | |||
673 | has_put = !!strstr(line->value, "put"); | |||
674 | } | |||
675 | ||||
676 | lwan->config.allow_post_temp_file = has_post; | |||
677 | lwan->config.allow_put_temp_file = has_put; | |||
678 | } else { | |||
679 | config_error(conf, "Unknown config key: %s", line->key); | |||
680 | } | |||
681 | break; | |||
682 | case CONFIG_LINE_TYPE_SECTION: | |||
683 | if (streq(line->key, "site")) { | |||
684 | if (!has_site
| |||
685 | parse_site(conf, line, lwan); | |||
686 | has_site = true1; | |||
687 | } else { | |||
688 | config_error(conf, "Only one site may be configured"); | |||
689 | } | |||
690 | } else if (streq(line->key, "straitjacket")) { | |||
691 | lwan_straitjacket_enforce_from_config(conf); | |||
692 | } else if (streq(line->key, "headers")) { | |||
693 | parse_global_headers(conf, lwan); | |||
694 | } else if (streq(line->key, "listener")) { | |||
695 | if (has_listener) { | |||
696 | config_error(conf, "Listener already set up"); | |||
697 | } else { | |||
698 | parse_listener(conf, line, lwan); | |||
699 | has_listener = true1; | |||
700 | } | |||
701 | } else if (streq(line->key, "tls_listener")) { | |||
702 | if (has_tls_listener) { | |||
703 | config_error(conf, "TLS Listener already set up"); | |||
704 | } else { | |||
705 | parse_tls_listener(conf, line, lwan); | |||
706 | has_tls_listener = true1; | |||
707 | } | |||
708 | } else { | |||
709 | config_error(conf, "Unknown section type: %s", line->key); | |||
710 | } | |||
711 | break; | |||
712 | case CONFIG_LINE_TYPE_SECTION_END: | |||
713 | config_error(conf, "Unexpected section end"); | |||
714 | } | |||
715 | } | |||
716 | ||||
717 | if (config_last_error(conf)) { | |||
718 | lwan_status_critical("Error on config file \"%s\", line %d: %s", path,lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 719, __FUNCTION__, "Error on config file \"%s\", line %d: %s" , path, config_cur_line(conf), config_last_error(conf)) | |||
719 | config_cur_line(conf), config_last_error(conf))lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 719, __FUNCTION__, "Error on config file \"%s\", line %d: %s" , path, config_cur_line(conf), config_last_error(conf)); | |||
720 | lwan_trie_destroy(&lwan->url_map_trie); | |||
721 | } | |||
722 | ||||
723 | config_close(conf); | |||
724 | ||||
725 | return true1; | |||
726 | } | |||
727 | ||||
728 | static void try_setup_from_config(struct lwan *l, | |||
729 | const struct lwan_config *config) | |||
730 | { | |||
731 | if (!setup_from_config(l, config->config_file_path)) { | |||
732 | if (config->config_file_path) { | |||
733 | lwan_status_critical("Could not read config file: %s",lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 734, __FUNCTION__, "Could not read config file: %s", config ->config_file_path) | |||
734 | config->config_file_path)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 734, __FUNCTION__, "Could not read config file: %s", config ->config_file_path); | |||
735 | } | |||
736 | } | |||
737 | ||||
738 | lwan_status_init(l); /* `quiet` key might have changed value. */ | |||
739 | ||||
740 | l->config.request_flags = | |||
741 | (l->config.proxy_protocol ? REQUEST_ALLOW_PROXY_REQS : 0) | | |||
742 | (l->config.allow_cors ? REQUEST_ALLOW_CORS : 0) | | |||
743 | (l->config.ssl.send_hsts_header ? REQUEST_WANTS_HSTS_HEADER : 0); | |||
744 | } | |||
745 | ||||
746 | static rlim_t setup_open_file_count_limits(void) | |||
747 | { | |||
748 | struct rlimit r; | |||
749 | ||||
750 | if (getrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) { | |||
751 | lwan_status_perror("Could not obtain maximum number of file "lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 753, __FUNCTION__, "Could not obtain maximum number of file " "descriptors. Assuming %d", 256) | |||
752 | "descriptors. Assuming %d",lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 753, __FUNCTION__, "Could not obtain maximum number of file " "descriptors. Assuming %d", 256) | |||
753 | OPEN_MAX)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 753, __FUNCTION__, "Could not obtain maximum number of file " "descriptors. Assuming %d", 256); | |||
754 | return OPEN_MAX256; | |||
755 | } | |||
756 | ||||
757 | if (r.rlim_max != r.rlim_cur) { | |||
758 | const rlim_t current = r.rlim_cur; | |||
759 | ||||
760 | if (r.rlim_max == RLIM_INFINITY0xffffffffffffffffuLL && r.rlim_cur < OPEN_MAX256) { | |||
761 | r.rlim_cur = OPEN_MAX256; | |||
762 | } else if (r.rlim_cur < r.rlim_max) { | |||
763 | r.rlim_cur = r.rlim_max; | |||
764 | } else { | |||
765 | /* Shouldn't happen, so just return the current value. */ | |||
766 | goto out; | |||
767 | } | |||
768 | ||||
769 | if (setrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) { | |||
770 | lwan_status_perror("Could not raise maximum number of file "lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 772, __FUNCTION__, "Could not raise maximum number of file " "descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max , current) | |||
771 | "descriptors to %" PRIu64 ". Leaving at "lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 772, __FUNCTION__, "Could not raise maximum number of file " "descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max , current) | |||
772 | "%" PRIu64, r.rlim_max, current)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 772, __FUNCTION__, "Could not raise maximum number of file " "descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max , current); | |||
773 | r.rlim_cur = current; | |||
774 | } | |||
775 | } | |||
776 | ||||
777 | out: | |||
778 | return r.rlim_cur; | |||
779 | } | |||
780 | ||||
781 | static void allocate_connections(struct lwan *l, size_t max_open_files) | |||
782 | { | |||
783 | const size_t sz = max_open_files * sizeof(struct lwan_connection); | |||
784 | ||||
785 | l->conns = lwan_aligned_alloc(sz, 64); | |||
786 | if (UNLIKELY(!l->conns)__builtin_expect(((!l->conns)), (0))) | |||
787 | lwan_status_critical_perror("lwan_alloc_aligned")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 787, __FUNCTION__, "lwan_alloc_aligned"); | |||
788 | ||||
789 | memset(l->conns, 0, sz); | |||
790 | } | |||
791 | ||||
792 | static void get_number_of_cpus(struct lwan *l) | |||
793 | { | |||
794 | long n_online_cpus = sysconf(_SC_NPROCESSORS_ONLN_SC_NPROCESSORS_ONLN); | |||
795 | long n_available_cpus = sysconf(_SC_NPROCESSORS_CONF_SC_NPROCESSORS_CONF); | |||
796 | ||||
797 | if (n_online_cpus < 0) { | |||
798 | lwan_status_warning(lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 799, __FUNCTION__, "Could not get number of online CPUs, assuming 1 CPU" ) | |||
799 | "Could not get number of online CPUs, assuming 1 CPU")lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 799, __FUNCTION__, "Could not get number of online CPUs, assuming 1 CPU" ); | |||
800 | n_online_cpus = 1; | |||
801 | } | |||
802 | ||||
803 | if (n_available_cpus < 0) { | |||
804 | lwan_status_warning(lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 806, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs" , n_online_cpus) | |||
805 | "Could not get number of available CPUs, assuming %ld CPUs",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 806, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs" , n_online_cpus) | |||
806 | n_online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 806, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs" , n_online_cpus); | |||
807 | n_available_cpus = n_online_cpus; | |||
808 | } | |||
809 | ||||
810 | l->online_cpus = (unsigned int)n_online_cpus; | |||
811 | l->available_cpus = (unsigned int)n_available_cpus; | |||
812 | } | |||
813 | ||||
814 | void lwan_init(struct lwan *l) { lwan_init_with_config(l, &default_config); } | |||
| ||||
815 | ||||
816 | const struct lwan_config *lwan_get_default_config(void) | |||
817 | { | |||
818 | return &default_config; | |||
819 | } | |||
820 | ||||
821 | static char *dup_or_null(const char *s) | |||
822 | { | |||
823 | return s ? strdup(s) : NULL((void*)0); | |||
824 | } | |||
825 | ||||
826 | void lwan_init_with_config(struct lwan *l, const struct lwan_config *config) | |||
827 | { | |||
828 | /* Load defaults */ | |||
829 | memset(l, 0, sizeof(*l)); | |||
830 | memcpy(&l->config, config, sizeof(*config)); | |||
831 | l->config.listener = dup_or_null(l->config.listener); | |||
832 | l->config.config_file_path = dup_or_null(l->config.config_file_path); | |||
833 | l->config.ssl.key = dup_or_null(l->config.ssl.key); | |||
834 | l->config.ssl.cert = dup_or_null(l->config.ssl.cert); | |||
835 | ||||
836 | /* Initialize status first, as it is used by other things during | |||
837 | * their initialization. */ | |||
838 | lwan_status_init(l); | |||
839 | ||||
840 | /* These will only print debugging messages. Debug messages are always | |||
841 | * printed if we're on a debug build, so the quiet setting will be | |||
842 | * respected. */ | |||
843 | lwan_job_thread_init(); | |||
844 | lwan_tables_init(); | |||
845 | ||||
846 | /* Get the number of CPUs here because straightjacket might be active | |||
847 | * and this will block access to /proc and /sys, which will cause | |||
848 | * get_number_of_cpus() to get incorrect fallback values. */ | |||
849 | get_number_of_cpus(l); | |||
850 | ||||
851 | try_setup_from_config(l, config); | |||
852 | ||||
853 | if (!l->headers.len) | |||
854 | build_response_headers(l, config->global_headers); | |||
855 | ||||
856 | lwan_response_init(l); | |||
857 | ||||
858 | /* Continue initialization as normal. */ | |||
859 | lwan_status_debug("Initializing lwan web server")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 859, __FUNCTION__, "Initializing lwan web server"); | |||
860 | ||||
861 | if (!l->config.n_threads) { | |||
862 | l->thread.count = l->online_cpus; | |||
863 | if (l->thread.count == 1) | |||
864 | l->thread.count = 2; | |||
865 | } else if (l->config.n_threads > 3 * l->online_cpus) { | |||
866 | l->thread.count = l->online_cpus * 3; | |||
867 | ||||
868 | lwan_status_warning("%d threads requested, but only %d online CPUs "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 871, __FUNCTION__, "%d threads requested, but only %d online CPUs " "(out of %d configured CPUs); capping to %d threads", l-> config.n_threads, l->online_cpus, l->available_cpus, 3 * l->online_cpus) | |||
869 | "(out of %d configured CPUs); capping to %d threads",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 871, __FUNCTION__, "%d threads requested, but only %d online CPUs " "(out of %d configured CPUs); capping to %d threads", l-> config.n_threads, l->online_cpus, l->available_cpus, 3 * l->online_cpus) | |||
870 | l->config.n_threads, l->online_cpus, l->available_cpus,lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 871, __FUNCTION__, "%d threads requested, but only %d online CPUs " "(out of %d configured CPUs); capping to %d threads", l-> config.n_threads, l->online_cpus, l->available_cpus, 3 * l->online_cpus) | |||
871 | 3 * l->online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 871, __FUNCTION__, "%d threads requested, but only %d online CPUs " "(out of %d configured CPUs); capping to %d threads", l-> config.n_threads, l->online_cpus, l->available_cpus, 3 * l->online_cpus); | |||
872 | } else if (l->config.n_threads > 255) { | |||
873 | l->thread.count = 256; | |||
874 | ||||
875 | lwan_status_warning("%d threads requested, but max 256 supported",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 876, __FUNCTION__, "%d threads requested, but max 256 supported" , l->config.n_threads) | |||
876 | l->config.n_threads)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 876, __FUNCTION__, "%d threads requested, but max 256 supported" , l->config.n_threads); | |||
877 | } else { | |||
878 | l->thread.count = l->config.n_threads; | |||
879 | } | |||
880 | ||||
881 | rlim_t max_open_files = setup_open_file_count_limits(); | |||
882 | allocate_connections(l, (size_t)max_open_files); | |||
883 | ||||
884 | l->thread.max_fd = (unsigned)max_open_files / (unsigned)l->thread.count; | |||
885 | lwan_status_info("Using %d threads, maximum %d sockets per thread",lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 886, __FUNCTION__, "Using %d threads, maximum %d sockets per thread" , l->thread.count, l->thread.max_fd) | |||
886 | l->thread.count, l->thread.max_fd)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 886, __FUNCTION__, "Using %d threads, maximum %d sockets per thread" , l->thread.count, l->thread.max_fd); | |||
887 | ||||
888 | signal(SIGPIPE13, SIG_IGN((__sighandler_t) 1)); | |||
889 | ||||
890 | lwan_readahead_init(); | |||
891 | lwan_thread_init(l); | |||
892 | lwan_http_authorize_init(); | |||
893 | } | |||
894 | ||||
895 | void lwan_shutdown(struct lwan *l) | |||
896 | { | |||
897 | lwan_status_info("Shutting down")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 897, __FUNCTION__, "Shutting down"); | |||
898 | ||||
899 | free(l->config.listener); | |||
900 | free(l->config.error_template); | |||
901 | free(l->config.config_file_path); | |||
902 | ||||
903 | lwan_always_bzero(l->config.ssl.cert, strlen(l->config.ssl.cert)); | |||
904 | free(l->config.ssl.cert); | |||
905 | lwan_always_bzero(l->config.ssl.key, strlen(l->config.ssl.key)); | |||
906 | free(l->config.ssl.key); | |||
907 | ||||
908 | lwan_job_thread_shutdown(); | |||
909 | lwan_thread_shutdown(l); | |||
910 | ||||
911 | lwan_status_debug("Shutting down URL handlers")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 911, __FUNCTION__, "Shutting down URL handlers"); | |||
912 | lwan_trie_destroy(&l->url_map_trie); | |||
913 | ||||
914 | free(l->headers.value); | |||
915 | free(l->conns); | |||
916 | ||||
917 | lwan_response_shutdown(l); | |||
918 | lwan_tables_shutdown(); | |||
919 | lwan_status_shutdown(l); | |||
920 | lwan_http_authorize_shutdown(); | |||
921 | lwan_readahead_shutdown(); | |||
922 | } | |||
923 | ||||
924 | void lwan_main_loop(struct lwan *l) | |||
925 | { | |||
926 | lwan_status_info("Ready to serve")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 926, __FUNCTION__, "Ready to serve"); | |||
927 | ||||
928 | lwan_job_thread_main_loop(); | |||
929 | } | |||
930 | ||||
931 | #ifdef CLOCK_MONOTONIC_COARSE6 | |||
932 | __attribute__((constructor)) static void detect_fastest_monotonic_clock(void) | |||
933 | { | |||
934 | struct timespec ts; | |||
935 | ||||
936 | if (!clock_gettime(CLOCK_MONOTONIC_COARSE6, &ts)) | |||
937 | monotonic_clock_id = CLOCK_MONOTONIC_COARSE6; | |||
938 | } | |||
939 | #endif | |||
940 | ||||
941 | void lwan_set_thread_name(const char *name) | |||
942 | { | |||
943 | char thread_name[16]; | |||
944 | char process_name[PATH_MAX4096]; | |||
945 | char *tmp; | |||
946 | int ret; | |||
947 | ||||
948 | if (proc_pidpath(getpid(), process_name, sizeof(process_name)) < 0) | |||
949 | return; | |||
950 | ||||
951 | tmp = strrchr(process_name, '/'); | |||
952 | if (!tmp) | |||
953 | return; | |||
954 | ||||
955 | ret = snprintf(thread_name, sizeof(thread_name), "%s %s", tmp + 1, name); | |||
956 | if (ret < 0) | |||
957 | return; | |||
958 | ||||
959 | pthread_set_name_np(pthread_self(), thread_name); | |||
960 | } |