File: | lwan.c |
Warning: | line 334, column 17 Potential memory leak |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * lwan - simple web server | |||
3 | * Copyright (c) 2012, 2013 Leandro A. F. Pereira <leandro@hardinfo.org> | |||
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(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(REQUEST_ACCEPT_DEFLATE > REQUEST_METHOD_MASK,extern int (*__Static_assert_function (void)) [!!sizeof (struct { int __error_if_negative: (REQUEST_ACCEPT_DEFLATE > REQUEST_METHOD_MASK ) ? 2 : -1; })] | |||
50 | "enough bits to store request methods")extern int (*__Static_assert_function (void)) [!!sizeof (struct { int __error_if_negative: (REQUEST_ACCEPT_DEFLATE > REQUEST_METHOD_MASK ) ? 2 : -1; })]; | |||
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(brew_coffee)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" ))) lwan_handler_info_brew_coffee = {.name = "brew_coffee", . handler = lwan_handler_brew_coffee}; 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 (!strcasecmp(name, "Date")) | |||
148 | return false0; | |||
149 | if (!strcasecmp(name, "Expires")) | |||
150 | return false0; | |||
151 | if (!strcasecmp(name, "WWW-Authenticate")) | |||
152 | return false0; | |||
153 | if (!strcasecmp(name, "Connection")) | |||
154 | return false0; | |||
155 | if (!strcasecmp(name, "Content-Type")) | |||
156 | return false0; | |||
157 | if (!strcasecmp(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 | bool_Bool set_server = false0; | |||
170 | ||||
171 | 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" , 171, __extension__ __PRETTY_FUNCTION__); })); | |||
172 | ||||
173 | lwan_strbuf_init(&l->headers); | |||
174 | ||||
175 | for (; kv && kv->key; kv++) { | |||
176 | if (!can_override_header(kv->key)) { | |||
177 | lwan_status_warning("Cannot override header '%s'", kv->key)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 177, __FUNCTION__, "Cannot override header '%s'", kv->key ); | |||
178 | } else { | |||
179 | if (!strcasecmp(kv->key, "Server")) | |||
180 | set_server = true1; | |||
181 | ||||
182 | lwan_strbuf_append_printf(&l->headers, "\r\n%s: %s", kv->key, | |||
183 | kv->value); | |||
184 | } | |||
185 | } | |||
186 | ||||
187 | if (!set_server) | |||
188 | lwan_strbuf_append_strz(&l->headers, "\r\nServer: lwan"); | |||
189 | ||||
190 | lwan_strbuf_append_strz(&l->headers, "\r\n\r\n"); | |||
191 | } | |||
192 | ||||
193 | static void parse_global_headers(struct config *c, | |||
194 | struct lwan *lwan) | |||
195 | { | |||
196 | struct lwan_key_value_array hdrs; | |||
197 | const struct config_line *l; | |||
198 | struct lwan_key_value *kv; | |||
199 | ||||
200 | lwan_key_value_array_init(&hdrs); | |||
201 | ||||
202 | while ((l = config_read_line(c))) { | |||
203 | switch (l->type) { | |||
204 | case CONFIG_LINE_TYPE_SECTION: | |||
205 | config_error( | |||
206 | c, "No sections are supported under the 'headers' section"); | |||
207 | goto cleanup; | |||
208 | ||||
209 | case CONFIG_LINE_TYPE_LINE: | |||
210 | kv = lwan_key_value_array_append(&hdrs); | |||
211 | if (!kv) { | |||
212 | lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 213, __FUNCTION__, "Could not allocate memory for custom response header" ) | |||
213 | "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 213, __FUNCTION__, "Could not allocate memory for custom response header" ); | |||
214 | } | |||
215 | ||||
216 | kv->key = strdup(l->key); | |||
217 | if (!kv->key) { | |||
218 | lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 219, __FUNCTION__, "Could not allocate memory for custom response header" ) | |||
219 | "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 219, __FUNCTION__, "Could not allocate memory for custom response header" ); | |||
220 | } | |||
221 | ||||
222 | kv->value = strdup(l->value); | |||
223 | if (!kv->value) { | |||
224 | lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 225, __FUNCTION__, "Could not allocate memory for custom response header" ) | |||
225 | "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 225, __FUNCTION__, "Could not allocate memory for custom response header" ); | |||
226 | } | |||
227 | break; | |||
228 | ||||
229 | case CONFIG_LINE_TYPE_SECTION_END: | |||
230 | kv = lwan_key_value_array_append(&hdrs); | |||
231 | if (!kv) { | |||
232 | lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 233, __FUNCTION__, "Could not allocate memory for custom response header" ) | |||
233 | "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 233, __FUNCTION__, "Could not allocate memory for custom response header" ); | |||
234 | } | |||
235 | ||||
236 | kv->key = NULL((void*)0); | |||
237 | kv->value = NULL((void*)0); | |||
238 | ||||
239 | build_response_headers(lwan, lwan_key_value_array_get_array(&hdrs)); | |||
240 | goto cleanup; | |||
241 | } | |||
242 | } | |||
243 | ||||
244 | config_error(c, "EOF while looking for end of 'headers' section"); | |||
245 | ||||
246 | cleanup: | |||
247 | LWAN_ARRAY_FOREACH (&hdrs, kv)for (kv = (&hdrs)->base.base; kv < ((typeof(kv))(& hdrs)->base.base + (&hdrs)->base.elements); kv++) { | |||
248 | free(kv->key); | |||
249 | free(kv->value); | |||
250 | } | |||
251 | lwan_key_value_array_reset(&hdrs); | |||
252 | } | |||
253 | ||||
254 | static void parse_listener_prefix_authorization(struct config *c, | |||
255 | const struct config_line *l, | |||
256 | struct lwan_url_map *url_map) | |||
257 | { | |||
258 | if (!streq(l->value, "basic")) { | |||
259 | config_error(c, "Only basic authorization supported"); | |||
260 | return; | |||
261 | } | |||
262 | ||||
263 | memset(&url_map->authorization, 0, sizeof(url_map->authorization)); | |||
264 | ||||
265 | while ((l = config_read_line(c))) { | |||
266 | switch (l->type) { | |||
267 | case CONFIG_LINE_TYPE_LINE: | |||
268 | if (streq(l->key, "realm")) { | |||
269 | free(url_map->authorization.realm); | |||
270 | url_map->authorization.realm = strdup(l->value); | |||
271 | } else if (streq(l->key, "password_file")) { | |||
272 | free(url_map->authorization.password_file); | |||
273 | url_map->authorization.password_file = realpath(l->value, NULL((void*)0)); | |||
274 | if (!url_map->authorization.password_file) | |||
275 | config_error(c, "Could not determine full path for password file: %s", l->value); | |||
276 | } | |||
277 | break; | |||
278 | ||||
279 | case CONFIG_LINE_TYPE_SECTION: | |||
280 | config_error(c, "Unexpected section: %s", l->key); | |||
281 | goto error; | |||
282 | ||||
283 | case CONFIG_LINE_TYPE_SECTION_END: | |||
284 | if (!url_map->authorization.realm) | |||
285 | url_map->authorization.realm = strdup("Lwan"); | |||
286 | if (!url_map->authorization.password_file) | |||
287 | url_map->authorization.password_file = strdup("htpasswd"); | |||
288 | ||||
289 | url_map->flags |= HANDLER_MUST_AUTHORIZE; | |||
290 | return; | |||
291 | } | |||
292 | } | |||
293 | ||||
294 | config_error(c, "Could not find end of authorization section"); | |||
295 | ||||
296 | error: | |||
297 | free(url_map->authorization.realm); | |||
298 | free(url_map->authorization.password_file); | |||
299 | } | |||
300 | ||||
301 | __attribute__((no_sanitize_address)) | |||
302 | static const char *get_module_name(const struct lwan_module *module) | |||
303 | { | |||
304 | const struct lwan_module_info *iter; | |||
305 | ||||
306 | 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)++ ) { | |||
307 | if (iter->module == module) | |||
308 | return iter->name; | |||
309 | } | |||
310 | ||||
311 | return "<unknown>"; | |||
312 | } | |||
313 | ||||
314 | static void parse_listener_prefix(struct config *c, | |||
315 | const struct config_line *l, | |||
316 | struct lwan *lwan, | |||
317 | const struct lwan_module *module, | |||
318 | void *handler) | |||
319 | { | |||
320 | struct lwan_url_map url_map = {}; | |||
321 | struct hash *hash = hash_str_new(free, free); | |||
322 | 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); })); | |||
323 | struct config *isolated; | |||
324 | ||||
325 | if (!hash) | |||
326 | lwan_status_critical("Could not allocate hash table")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 326, __FUNCTION__, "Could not allocate hash table"); | |||
327 | ||||
328 | isolated = config_isolate_section(c, l); | |||
329 | if (!isolated) { | |||
330 | config_error(c, "Could not isolate configuration file"); | |||
331 | goto out; | |||
332 | } | |||
333 | ||||
334 | while ((l = config_read_line(c))) { | |||
| ||||
335 | switch (l->type) { | |||
336 | case CONFIG_LINE_TYPE_LINE: | |||
337 | hash_add(hash, strdup(l->key), strdup(l->value)); | |||
338 | break; | |||
339 | ||||
340 | case CONFIG_LINE_TYPE_SECTION: | |||
341 | if (streq(l->key, "authorization")) { | |||
342 | parse_listener_prefix_authorization(c, l, &url_map); | |||
343 | } else if (!config_skip_section(c, l)) { | |||
344 | config_error(c, "Could not skip section"); | |||
345 | goto out; | |||
346 | } | |||
347 | break; | |||
348 | ||||
349 | case CONFIG_LINE_TYPE_SECTION_END: | |||
350 | goto add_map; | |||
351 | } | |||
352 | } | |||
353 | ||||
354 | config_error(c, "Expecting section end while parsing prefix"); | |||
355 | goto out; | |||
356 | ||||
357 | add_map: | |||
358 | 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" , 358, __extension__ __PRETTY_FUNCTION__); })); | |||
359 | ||||
360 | if (handler) { | |||
361 | url_map.handler = handler; | |||
362 | url_map.flags |= HANDLER_PARSE_MASK | HANDLER_DATA_IS_HASH_TABLE; | |||
363 | url_map.data = hash; | |||
364 | url_map.module = NULL((void*)0); | |||
365 | ||||
366 | hash = NULL((void*)0); | |||
367 | } else if (module->create_from_hash && module->handle_request) { | |||
368 | lwan_status_debug("Initializing module %s from config",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 369, __FUNCTION__, "Initializing module %s from config", get_module_name (module)) | |||
369 | get_module_name(module))lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 369, __FUNCTION__, "Initializing module %s from config", get_module_name (module)); | |||
370 | ||||
371 | url_map.data = module->create_from_hash(prefix, hash); | |||
372 | if (!url_map.data) { | |||
373 | config_error(c, "Could not create module instance"); | |||
374 | goto out; | |||
375 | } | |||
376 | ||||
377 | if (module->parse_conf && !module->parse_conf(url_map.data, isolated)) { | |||
378 | const char *msg = config_last_error(isolated); | |||
379 | ||||
380 | config_error(c, "Error from module: %s", msg ? msg : "Unknown"); | |||
381 | goto out; | |||
382 | } | |||
383 | ||||
384 | url_map.handler = module->handle_request; | |||
385 | url_map.flags |= module->flags; | |||
386 | url_map.module = module; | |||
387 | } else if (UNLIKELY(!module->create_from_hash)__builtin_expect(((!module->create_from_hash)), (0))) { | |||
388 | config_error(c, "Module isn't prepared to load settings from a file; " | |||
389 | "create_from_hash() method isn't present"); | |||
390 | goto out; | |||
391 | } else if (UNLIKELY(!module->handle_request)__builtin_expect(((!module->handle_request)), (0))) { | |||
392 | config_error(c, "Module does not have handle_request() method"); | |||
393 | goto out; | |||
394 | } | |||
395 | ||||
396 | add_url_map(&lwan->url_map_trie, prefix, &url_map); | |||
397 | ||||
398 | out: | |||
399 | hash_free(hash); | |||
400 | config_close(isolated); | |||
401 | } | |||
402 | ||||
403 | void lwan_set_url_map(struct lwan *l, const struct lwan_url_map *map) | |||
404 | { | |||
405 | lwan_trie_destroy(&l->url_map_trie); | |||
406 | if (UNLIKELY(!lwan_trie_init(&l->url_map_trie, destroy_urlmap))__builtin_expect(((!lwan_trie_init(&l->url_map_trie, destroy_urlmap ))), (0))) | |||
407 | lwan_status_critical_perror("Could not initialize trie")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 407, __FUNCTION__, "Could not initialize trie"); | |||
408 | ||||
409 | for (; map->prefix; map++) { | |||
410 | struct lwan_url_map *copy = add_url_map(&l->url_map_trie, NULL((void*)0), map); | |||
411 | ||||
412 | if (copy->module && copy->module->create) { | |||
413 | lwan_status_debug("Initializing module %s from struct",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 414, __FUNCTION__, "Initializing module %s from struct", get_module_name (copy->module)) | |||
414 | get_module_name(copy->module))lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 414, __FUNCTION__, "Initializing module %s from struct", get_module_name (copy->module)); | |||
415 | ||||
416 | copy->data = copy->module->create(map->prefix, copy->args); | |||
417 | if (!copy->data) { | |||
418 | lwan_status_critical("Could not initialize module %s",lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 419, __FUNCTION__, "Could not initialize module %s", get_module_name (copy->module)) | |||
419 | get_module_name(copy->module))lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 419, __FUNCTION__, "Could not initialize module %s", get_module_name (copy->module)); | |||
420 | } | |||
421 | ||||
422 | copy->flags = copy->module->flags; | |||
423 | copy->handler = copy->module->handle_request; | |||
424 | } else { | |||
425 | copy->flags = HANDLER_PARSE_MASK; | |||
426 | } | |||
427 | } | |||
428 | } | |||
429 | ||||
430 | static void parse_listener(struct config *c, | |||
431 | const struct config_line *l, | |||
432 | struct lwan *lwan) | |||
433 | { | |||
434 | free(lwan->config.listener); | |||
435 | lwan->config.listener = strdup(l->value); | |||
436 | ||||
437 | while ((l = config_read_line(c))) { | |||
438 | switch (l->type) { | |||
439 | case CONFIG_LINE_TYPE_LINE: | |||
440 | config_error(c, "Expecting prefix section"); | |||
441 | return; | |||
442 | case CONFIG_LINE_TYPE_SECTION: | |||
443 | if (l->key[0] == '&') { | |||
444 | void *handler = find_handler(l->key + 1); | |||
445 | if (handler) { | |||
446 | parse_listener_prefix(c, l, lwan, NULL((void*)0), handler); | |||
447 | continue; | |||
448 | } | |||
449 | ||||
450 | config_error(c, "Could not find handler name: %s", l->key + 1); | |||
451 | return; | |||
452 | } | |||
453 | ||||
454 | const struct lwan_module *module = find_module(l->key); | |||
455 | if (module) { | |||
456 | parse_listener_prefix(c, l, lwan, module, NULL((void*)0)); | |||
457 | continue; | |||
458 | } | |||
459 | ||||
460 | config_error(c, "Invalid section or module not found: %s", l->key); | |||
461 | return; | |||
462 | case CONFIG_LINE_TYPE_SECTION_END: | |||
463 | return; | |||
464 | } | |||
465 | } | |||
466 | ||||
467 | config_error(c, "Expecting section end while parsing listener"); | |||
468 | } | |||
469 | ||||
470 | const char *lwan_get_config_path(char *path_buf, size_t path_buf_len) | |||
471 | { | |||
472 | char buffer[PATH_MAX4096]; | |||
473 | ||||
474 | if (proc_pidpath(getpid(), buffer, sizeof(buffer)) < 0) | |||
475 | goto out; | |||
476 | ||||
477 | char *path = strrchr(buffer, '/'); | |||
478 | if (!path) | |||
479 | goto out; | |||
480 | int ret = snprintf(path_buf, path_buf_len, "%s.conf", path + 1); | |||
481 | if (ret < 0 || ret >= (int)path_buf_len) | |||
482 | goto out; | |||
483 | ||||
484 | return path_buf; | |||
485 | ||||
486 | out: | |||
487 | return "lwan.conf"; | |||
488 | } | |||
489 | ||||
490 | static bool_Bool setup_from_config(struct lwan *lwan, const char *path) | |||
491 | { | |||
492 | const struct config_line *line; | |||
493 | struct config *conf; | |||
494 | bool_Bool has_listener = false0; | |||
495 | char path_buf[PATH_MAX4096]; | |||
496 | ||||
497 | if (!path
| |||
498 | path = lwan_get_config_path(path_buf, sizeof(path_buf)); | |||
499 | lwan_status_info("Loading configuration file: %s", path)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 499, __FUNCTION__, "Loading configuration file: %s", path); | |||
500 | ||||
501 | conf = config_open(path); | |||
502 | if (!conf) | |||
503 | return false0; | |||
504 | ||||
505 | if (!lwan_trie_init(&lwan->url_map_trie, destroy_urlmap)) | |||
506 | return false0; | |||
507 | ||||
508 | while ((line = config_read_line(conf))) { | |||
509 | switch (line->type) { | |||
510 | case CONFIG_LINE_TYPE_LINE: | |||
511 | if (streq(line->key, "keep_alive_timeout")) { | |||
512 | lwan->config.keep_alive_timeout = (unsigned int)parse_long( | |||
513 | line->value, default_config.keep_alive_timeout); | |||
514 | } else if (streq(line->key, "quiet")) { | |||
515 | lwan->config.quiet = | |||
516 | parse_bool(line->value, default_config.quiet); | |||
517 | } else if (streq(line->key, "proxy_protocol")) { | |||
518 | lwan->config.proxy_protocol = | |||
519 | parse_bool(line->value, default_config.proxy_protocol); | |||
520 | } else if (streq(line->key, "allow_cors")) { | |||
521 | lwan->config.allow_cors = | |||
522 | parse_bool(line->value, default_config.allow_cors); | |||
523 | } else if (streq(line->key, "expires")) { | |||
524 | lwan->config.expires = | |||
525 | parse_time_period(line->value, default_config.expires); | |||
526 | } else if (streq(line->key, "error_template")) { | |||
527 | free(lwan->config.error_template); | |||
528 | lwan->config.error_template = strdup(line->value); | |||
529 | } else if (streq(line->key, "threads")) { | |||
530 | long n_threads = | |||
531 | parse_long(line->value, default_config.n_threads); | |||
532 | if (n_threads < 0) | |||
533 | config_error(conf, "Invalid number of threads: %ld", | |||
534 | n_threads); | |||
535 | lwan->config.n_threads = (unsigned int)n_threads; | |||
536 | } else if (streq(line->key, "max_post_data_size")) { | |||
537 | long max_post_data_size = parse_long( | |||
538 | line->value, (long)default_config.max_post_data_size); | |||
539 | if (max_post_data_size < 0) | |||
540 | config_error(conf, "Negative maximum post data size"); | |||
541 | else if (max_post_data_size > 128 * (1 << 20)) | |||
542 | config_error(conf, | |||
543 | "Maximum post data can't be over 128MiB"); | |||
544 | lwan->config.max_post_data_size = (size_t)max_post_data_size; | |||
545 | } else if (streq(line->key, "max_put_data_size")) { | |||
546 | long max_put_data_size = parse_long( | |||
547 | line->value, (long)default_config.max_put_data_size); | |||
548 | if (max_put_data_size < 0) | |||
549 | config_error(conf, "Negative maximum put data size"); | |||
550 | else if (max_put_data_size > 128 * (1 << 20)) | |||
551 | config_error(conf, | |||
552 | "Maximum put data can't be over 128MiB"); | |||
553 | lwan->config.max_put_data_size = (size_t)max_put_data_size; | |||
554 | } else if (streq(line->key, "allow_temp_files")) { | |||
555 | bool_Bool has_post, has_put; | |||
556 | ||||
557 | if (strstr(line->value, "all")) { | |||
558 | has_post = has_put = true1; | |||
559 | } else { | |||
560 | has_post = !!strstr(line->value, "post"); | |||
561 | has_put = !!strstr(line->value, "put"); | |||
562 | } | |||
563 | ||||
564 | lwan->config.allow_post_temp_file = has_post; | |||
565 | lwan->config.allow_put_temp_file = has_put; | |||
566 | } else { | |||
567 | config_error(conf, "Unknown config key: %s", line->key); | |||
568 | } | |||
569 | break; | |||
570 | case CONFIG_LINE_TYPE_SECTION: | |||
571 | if (streq(line->key, "listener")) { | |||
572 | if (!has_listener
| |||
573 | parse_listener(conf, line, lwan); | |||
574 | has_listener = true1; | |||
575 | } else { | |||
576 | config_error(conf, "Only one listener supported"); | |||
577 | } | |||
578 | } else if (streq(line->key, "straitjacket")) { | |||
579 | lwan_straitjacket_enforce_from_config(conf); | |||
580 | } else if (streq(line->key, "headers")) { | |||
581 | parse_global_headers(conf, lwan); | |||
582 | } else { | |||
583 | config_error(conf, "Unknown section type: %s", line->key); | |||
584 | } | |||
585 | break; | |||
586 | case CONFIG_LINE_TYPE_SECTION_END: | |||
587 | config_error(conf, "Unexpected section end"); | |||
588 | } | |||
589 | } | |||
590 | ||||
591 | if (config_last_error(conf)) { | |||
592 | 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" , 593, __FUNCTION__, "Error on config file \"%s\", line %d: %s" , path, config_cur_line(conf), config_last_error(conf)) | |||
593 | config_cur_line(conf), config_last_error(conf))lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 593, __FUNCTION__, "Error on config file \"%s\", line %d: %s" , path, config_cur_line(conf), config_last_error(conf)); | |||
594 | } | |||
595 | ||||
596 | config_close(conf); | |||
597 | ||||
598 | return true1; | |||
599 | } | |||
600 | ||||
601 | static void try_setup_from_config(struct lwan *l, | |||
602 | const struct lwan_config *config) | |||
603 | { | |||
604 | if (!setup_from_config(l, config->config_file_path)) { | |||
605 | if (config->config_file_path) { | |||
606 | lwan_status_critical("Could not read config file: %s",lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 607, __FUNCTION__, "Could not read config file: %s", config ->config_file_path) | |||
607 | config->config_file_path)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 607, __FUNCTION__, "Could not read config file: %s", config ->config_file_path); | |||
608 | } | |||
609 | } | |||
610 | ||||
611 | lwan_status_init(l); /* `quiet` key might have changed value. */ | |||
612 | ||||
613 | l->config.request_flags = | |||
614 | (l->config.proxy_protocol ? REQUEST_ALLOW_PROXY_REQS : 0) | | |||
615 | (l->config.allow_cors ? REQUEST_ALLOW_CORS : 0); | |||
616 | } | |||
617 | ||||
618 | static rlim_t setup_open_file_count_limits(void) | |||
619 | { | |||
620 | struct rlimit r; | |||
621 | ||||
622 | if (getrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) { | |||
623 | 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" , 625, __FUNCTION__, "Could not obtain maximum number of file " "descriptors. Assuming %d", 256) | |||
624 | "descriptors. Assuming %d",lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 625, __FUNCTION__, "Could not obtain maximum number of file " "descriptors. Assuming %d", 256) | |||
625 | OPEN_MAX)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 625, __FUNCTION__, "Could not obtain maximum number of file " "descriptors. Assuming %d", 256); | |||
626 | return OPEN_MAX256; | |||
627 | } | |||
628 | ||||
629 | if (r.rlim_max != r.rlim_cur) { | |||
630 | const rlim_t current = r.rlim_cur; | |||
631 | ||||
632 | if (r.rlim_max == RLIM_INFINITY0xffffffffffffffffuLL && r.rlim_cur < OPEN_MAX256) { | |||
633 | r.rlim_cur = OPEN_MAX256; | |||
634 | } else if (r.rlim_cur < r.rlim_max) { | |||
635 | r.rlim_cur = r.rlim_max; | |||
636 | } else { | |||
637 | /* Shouldn't happen, so just return the current value. */ | |||
638 | goto out; | |||
639 | } | |||
640 | ||||
641 | if (setrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) { | |||
642 | 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" , 644, __FUNCTION__, "Could not raise maximum number of file " "descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max , current) | |||
643 | "descriptors to %" PRIu64 ". Leaving at "lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 644, __FUNCTION__, "Could not raise maximum number of file " "descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max , current) | |||
644 | "%" PRIu64, r.rlim_max, current)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 644, __FUNCTION__, "Could not raise maximum number of file " "descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max , current); | |||
645 | r.rlim_cur = current; | |||
646 | } | |||
647 | } | |||
648 | ||||
649 | out: | |||
650 | return r.rlim_cur; | |||
651 | } | |||
652 | ||||
653 | static void allocate_connections(struct lwan *l, size_t max_open_files) | |||
654 | { | |||
655 | const size_t sz = max_open_files * sizeof(struct lwan_connection); | |||
656 | ||||
657 | l->conns = lwan_aligned_alloc(sz, 64); | |||
658 | if (UNLIKELY(!l->conns)__builtin_expect(((!l->conns)), (0))) | |||
659 | lwan_status_critical_perror("lwan_alloc_aligned")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 659, __FUNCTION__, "lwan_alloc_aligned"); | |||
660 | ||||
661 | memset(l->conns, 0, sz); | |||
662 | } | |||
663 | ||||
664 | static void get_number_of_cpus(struct lwan *l) | |||
665 | { | |||
666 | long n_online_cpus = sysconf(_SC_NPROCESSORS_ONLN_SC_NPROCESSORS_ONLN); | |||
667 | long n_available_cpus = sysconf(_SC_NPROCESSORS_CONF_SC_NPROCESSORS_CONF); | |||
668 | ||||
669 | if (n_online_cpus < 0) { | |||
670 | lwan_status_warning(lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 671, __FUNCTION__, "Could not get number of online CPUs, assuming 1 CPU" ) | |||
671 | "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" , 671, __FUNCTION__, "Could not get number of online CPUs, assuming 1 CPU" ); | |||
672 | n_online_cpus = 1; | |||
673 | } | |||
674 | ||||
675 | if (n_available_cpus < 0) { | |||
676 | lwan_status_warning(lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 678, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs" , n_online_cpus) | |||
677 | "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" , 678, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs" , n_online_cpus) | |||
678 | n_online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 678, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs" , n_online_cpus); | |||
679 | n_available_cpus = n_online_cpus; | |||
680 | } | |||
681 | ||||
682 | l->online_cpus = (unsigned int)n_online_cpus; | |||
683 | l->available_cpus = (unsigned int)n_available_cpus; | |||
684 | } | |||
685 | ||||
686 | void lwan_init(struct lwan *l) { lwan_init_with_config(l, &default_config); } | |||
| ||||
687 | ||||
688 | const struct lwan_config *lwan_get_default_config(void) | |||
689 | { | |||
690 | return &default_config; | |||
691 | } | |||
692 | ||||
693 | static char *dup_or_null(const char *s) | |||
694 | { | |||
695 | return s ? strdup(s) : NULL((void*)0); | |||
696 | } | |||
697 | ||||
698 | void lwan_init_with_config(struct lwan *l, const struct lwan_config *config) | |||
699 | { | |||
700 | /* Load defaults */ | |||
701 | memset(l, 0, sizeof(*l)); | |||
702 | memcpy(&l->config, config, sizeof(*config)); | |||
703 | l->config.listener = dup_or_null(l->config.listener); | |||
704 | l->config.config_file_path = dup_or_null(l->config.config_file_path); | |||
705 | ||||
706 | /* Initialize status first, as it is used by other things during | |||
707 | * their initialization. */ | |||
708 | lwan_status_init(l); | |||
709 | ||||
710 | /* These will only print debugging messages. Debug messages are always | |||
711 | * printed if we're on a debug build, so the quiet setting will be | |||
712 | * respected. */ | |||
713 | lwan_job_thread_init(); | |||
714 | lwan_tables_init(); | |||
715 | ||||
716 | try_setup_from_config(l, config); | |||
717 | ||||
718 | if (!lwan_strbuf_get_length(&l->headers)) | |||
719 | build_response_headers(l, config->global_headers); | |||
720 | ||||
721 | lwan_response_init(l); | |||
722 | ||||
723 | /* Continue initialization as normal. */ | |||
724 | lwan_status_debug("Initializing lwan web server")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 724, __FUNCTION__, "Initializing lwan web server"); | |||
725 | ||||
726 | get_number_of_cpus(l); | |||
727 | if (!l->config.n_threads) { | |||
728 | l->thread.count = l->online_cpus; | |||
729 | if (l->thread.count == 1) | |||
730 | l->thread.count = 2; | |||
731 | } else if (l->config.n_threads > 3 * l->online_cpus) { | |||
732 | l->thread.count = l->online_cpus * 3; | |||
733 | ||||
734 | 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" , 737, __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) | |||
735 | "(out of %d configured CPUs); capping to %d threads",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 737, __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) | |||
736 | 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" , 737, __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) | |||
737 | 3 * l->online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 737, __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); | |||
738 | } else if (l->config.n_threads > 255) { | |||
739 | l->thread.count = 256; | |||
740 | ||||
741 | 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" , 742, __FUNCTION__, "%d threads requested, but max 256 supported" , l->config.n_threads) | |||
742 | l->config.n_threads)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 742, __FUNCTION__, "%d threads requested, but max 256 supported" , l->config.n_threads); | |||
743 | } else { | |||
744 | l->thread.count = l->config.n_threads; | |||
745 | } | |||
746 | ||||
747 | rlim_t max_open_files = setup_open_file_count_limits(); | |||
748 | allocate_connections(l, (size_t)max_open_files); | |||
749 | ||||
750 | l->thread.max_fd = (unsigned)max_open_files / (unsigned)l->thread.count; | |||
751 | 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" , 752, __FUNCTION__, "Using %d threads, maximum %d sockets per thread" , l->thread.count, l->thread.max_fd) | |||
752 | l->thread.count, l->thread.max_fd)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 752, __FUNCTION__, "Using %d threads, maximum %d sockets per thread" , l->thread.count, l->thread.max_fd); | |||
753 | ||||
754 | signal(SIGPIPE13, SIG_IGN((__sighandler_t) 1)); | |||
755 | ||||
756 | lwan_readahead_init(); | |||
757 | lwan_thread_init(l); | |||
758 | lwan_http_authorize_init(); | |||
759 | } | |||
760 | ||||
761 | void lwan_shutdown(struct lwan *l) | |||
762 | { | |||
763 | lwan_status_info("Shutting down")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 763, __FUNCTION__, "Shutting down"); | |||
764 | ||||
765 | free(l->config.listener); | |||
766 | free(l->config.error_template); | |||
767 | free(l->config.config_file_path); | |||
768 | ||||
769 | lwan_job_thread_shutdown(); | |||
770 | lwan_thread_shutdown(l); | |||
771 | ||||
772 | lwan_status_debug("Shutting down URL handlers")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 772, __FUNCTION__, "Shutting down URL handlers"); | |||
773 | lwan_trie_destroy(&l->url_map_trie); | |||
774 | ||||
775 | lwan_strbuf_free(&l->headers); | |||
776 | free(l->conns); | |||
777 | ||||
778 | lwan_response_shutdown(l); | |||
779 | lwan_tables_shutdown(); | |||
780 | lwan_status_shutdown(l); | |||
781 | lwan_http_authorize_shutdown(); | |||
782 | lwan_readahead_shutdown(); | |||
783 | } | |||
784 | ||||
785 | void lwan_main_loop(struct lwan *l) | |||
786 | { | |||
787 | lwan_status_info("Ready to serve")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 787, __FUNCTION__, "Ready to serve"); | |||
788 | ||||
789 | lwan_job_thread_main_loop(); | |||
790 | } | |||
791 | ||||
792 | #ifdef CLOCK_MONOTONIC_COARSE6 | |||
793 | __attribute__((constructor)) static void detect_fastest_monotonic_clock(void) | |||
794 | { | |||
795 | struct timespec ts; | |||
796 | ||||
797 | if (!clock_gettime(CLOCK_MONOTONIC_COARSE6, &ts)) | |||
798 | monotonic_clock_id = CLOCK_MONOTONIC_COARSE6; | |||
799 | } | |||
800 | #endif | |||
801 | ||||
802 | void lwan_set_thread_name(const char *name) | |||
803 | { | |||
804 | char thread_name[16]; | |||
805 | char process_name[PATH_MAX4096]; | |||
806 | char *tmp; | |||
807 | int ret; | |||
808 | ||||
809 | if (proc_pidpath(getpid(), process_name, sizeof(process_name)) < 0) | |||
810 | return; | |||
811 | ||||
812 | tmp = strrchr(process_name, '/'); | |||
813 | if (!tmp) | |||
814 | return; | |||
815 | ||||
816 | ret = snprintf(thread_name, sizeof(thread_name), "%s %s", tmp + 1, name); | |||
817 | if (ret < 0) | |||
818 | return; | |||
819 | ||||
820 | pthread_set_name_np(pthread_self(), thread_name); | |||
821 | } |