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