Bug Summary

File:lwan.c
Warning:line 339, column 17
Potential leak of memory pointed to by 'value_copy'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name lwan.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -fno-plt -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/buildbot/lwan-worker/clang-analyze/build/src/lib -resource-dir /usr/lib/clang/17 -include /home/buildbot/lwan-worker/clang-analyze/build/lwan-build-config.h -D _FILE_OFFSET_BITS=64 -D _TIME_BITS=64 -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib/missing -I /home/buildbot/lwan-worker/clang-analyze/build -I /usr/include/luajit-2.1 -I /usr/include/valgrind -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib -internal-isystem /usr/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../x86_64-pc-linux-gnu/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-override-init -Wno-unused-parameter -Wno-free-nonheap-object -std=gnu11 -fdebug-compilation-dir=/home/buildbot/lwan-worker/clang-analyze/build/src/lib -ferror-limit 19 -stack-protector 2 -fgnuc-version=4.2.1 -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/buildbot/lwan-worker/clang-analyze/CLANG/2024-03-28-151121-1332190-1 -x c /home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c
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. */
49static_assert_Static_assert(REQUEST_ACCEPT_DEFLATE > REQUEST_METHOD_MASK,
50 "enough bits to store request methods");
51
52/* See detect_fastest_monotonic_clock() */
53clockid_t monotonic_clock_id = CLOCK_MONOTONIC1;
54
55static 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
70LWAN_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))
79static 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))
92static 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
104static 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
123static 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
143static 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 (strcaseequal_neutral_len(name, "Access-Control-Allow-",
161 sizeof("Access-Control-Allow-") - 1))
162 return false0;
163
164 return true1;
165}
166
167static 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
198static 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
251cleanup:
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
259static 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
301error:
302 free(url_map->authorization.realm);
303 free(url_map->authorization.password_file);
304}
305
306__attribute__((no_sanitize_address))
307static 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
319static 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)
21
Assuming 'hash' is non-null
22
Taking false branch
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) {
23
Assuming 'isolated' is non-null
24
Taking false branch
335 config_error(c, "Could not isolate configuration file");
336 goto out;
337 }
338
339 while ((l = config_read_line(c))) {
25
Loop condition is true. Entering loop body
32
Execution continues on line 339
33
Potential leak of memory pointed to by 'value_copy'
340 switch (l->type) {
26
Control jumps to 'case CONFIG_LINE_TYPE_LINE:' at line 341
341 case CONFIG_LINE_TYPE_LINE: {
342 char *key_copy = strdup(l->key);
343 char *value_copy = strdup(l->value);
27
Memory is allocated
344
345 if (!key_copy)
28
Assuming 'key_copy' is non-null
29
Taking false branch
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)
30
Assuming 'value_copy' is non-null
31
Taking false branch
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
371add_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
412out:
413 hash_free(hash);
414 config_close(isolated);
415}
416
417static 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
438void 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))
449void 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 lwan_status_debug("Using handler `%s' for route `%s'",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 462, __FUNCTION__, "Using handler `%s' for route `%s'", iter
->name, iter->route)
462 iter->name, iter->route)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 462, __FUNCTION__, "Using handler `%s' for route `%s'", iter
->name, iter->route)
;
463
464 const struct lwan_url_map map = {.prefix = iter->route,
465 .handler = iter->handler};
466 register_url_map(l, &map);
467 }
468}
469
470const 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
486out:
487 return "lwan.conf";
488}
489
490static void parse_tls_listener(struct config *conf, const struct config_line *line, struct lwan *lwan)
491{
492#if !defined(LWAN_HAVE_MBEDTLS)
493 config_error(conf, "Lwan has been built without mbedTLS support");
494 return;
495#endif
496
497 lwan->config.tls_listener = strdup(line->value);
498 if (!lwan->config.tls_listener) {
499 config_error(conf, "Could not allocate memory for tls_listener");
500 return;
501 }
502
503 lwan->config.ssl.cert = NULL((void*)0);
504 lwan->config.ssl.key = NULL((void*)0);
505
506 while ((line = config_read_line(conf))) {
507 switch (line->type) {
508 case CONFIG_LINE_TYPE_SECTION_END:
509 if (!lwan->config.ssl.cert)
510 config_error(conf, "Missing path to certificate");
511 if (!lwan->config.ssl.key)
512 config_error(conf, "Missing path to private key");
513 return;
514 case CONFIG_LINE_TYPE_SECTION:
515 config_error(conf, "Unexpected section: %s", line->key);
516 return;
517 case CONFIG_LINE_TYPE_LINE:
518 if (streq(line->key, "cert")) {
519 free(lwan->config.ssl.cert);
520 lwan->config.ssl.cert = strdup(line->value);
521 if (!lwan->config.ssl.cert)
522 return lwan_status_critical("Could not copy string")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 522, __FUNCTION__, "Could not copy string")
;
523 } else if (streq(line->key, "key")) {
524 free(lwan->config.ssl.key);
525 lwan->config.ssl.key = strdup(line->value);
526 if (!lwan->config.ssl.key)
527 return lwan_status_critical("Could not copy string")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 527, __FUNCTION__, "Could not copy string")
;
528 } else if (streq(line->key, "hsts")) {
529 lwan->config.ssl.send_hsts_header = parse_bool(line->value, false0);
530 } else {
531 config_error(conf, "Unexpected key: %s", line->key);
532 }
533 }
534 }
535
536 config_error(conf, "Expecting section end while parsing SSL configuration");
537}
538
539static void
540parse_listener(struct config *c, const struct config_line *l, struct lwan *lwan)
541{
542 lwan->config.listener = strdup(l->value);
543 if (!lwan->config.listener)
544 config_error(c, "Could not allocate memory for listener");
545
546 while ((l = config_read_line(c))) {
547 switch (l->type) {
548 case CONFIG_LINE_TYPE_LINE:
549 config_error(c, "Unexpected key %s", l->key);
550 return;
551 case CONFIG_LINE_TYPE_SECTION:
552 config_error(c, "Unexpected section %s", l->key);
553 return;
554 case CONFIG_LINE_TYPE_SECTION_END:
555 return;
556 }
557 }
558
559 config_error(c, "Unexpected EOF while parsing listener");
560}
561
562static void
563parse_site(struct config *c, const struct config_line *l, struct lwan *lwan)
564{
565 while ((l = config_read_line(c))) {
14
Loop condition is true. Entering loop body
566 switch (l->type) {
15
Control jumps to 'case CONFIG_LINE_TYPE_SECTION:' at line 570
567 case CONFIG_LINE_TYPE_LINE:
568 config_error(c, "Expecting prefix section");
569 return;
570 case CONFIG_LINE_TYPE_SECTION:
571 /* FIXME: per-site authorization? */
572
573 if (l->key[0] == '&') {
16
Assuming the condition is false
17
Taking false branch
574 void *handler = find_handler(l->key + 1);
575 if (handler) {
576 parse_listener_prefix(c, l, lwan, NULL((void*)0), handler);
577 continue;
578 }
579
580 config_error(c, "Could not find handler name: %s", l->key + 1);
581 return;
582 }
583
584 const struct lwan_module *module = find_module(l->key);
585 if (module) {
18
Assuming 'module' is non-null
19
Taking true branch
586 parse_listener_prefix(c, l, lwan, module, NULL((void*)0));
20
Calling 'parse_listener_prefix'
587 continue;
588 }
589
590 config_error(c, "Invalid section or module not found: %s", l->key);
591 return;
592 case CONFIG_LINE_TYPE_SECTION_END:
593 return;
594 }
595 }
596
597 config_error(c, "Expecting section end while parsing listener");
598}
599
600static bool_Bool setup_from_config(struct lwan *lwan, const char *path)
601{
602 const struct config_line *line;
603 struct config *conf;
604 bool_Bool has_site = false0;
605 bool_Bool has_listener = false0;
606 bool_Bool has_tls_listener = false0;
607 char path_buf[PATH_MAX4096];
608
609 if (!path
3.1
'path' is null
)
4
Taking true branch
610 path = lwan_get_config_path(path_buf, sizeof(path_buf));
611 lwan_status_info("Loading configuration file: %s", path)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 611, __FUNCTION__, "Loading configuration file: %s", path)
;
612
613 conf = config_open(path);
614 if (!conf)
5
Assuming 'conf' is non-null
6
Taking false branch
615 return false0;
616
617 if (!lwan_trie_init(&lwan->url_map_trie, destroy_urlmap))
7
Assuming the condition is false
8
Taking false branch
618 return false0;
619
620 while ((line = config_read_line(conf))) {
9
Loop condition is true. Entering loop body
621 switch (line->type) {
10
Control jumps to 'case CONFIG_LINE_TYPE_SECTION:' at line 685
622 case CONFIG_LINE_TYPE_LINE:
623 if (streq(line->key, "keep_alive_timeout")) {
624 lwan->config.keep_alive_timeout = (unsigned int)parse_long(
625 line->value, default_config.keep_alive_timeout);
626 } else if (streq(line->key, "quiet")) {
627 lwan->config.quiet =
628 parse_bool(line->value, default_config.quiet);
629 } else if (streq(line->key, "proxy_protocol")) {
630 lwan->config.proxy_protocol =
631 parse_bool(line->value, default_config.proxy_protocol);
632 } else if (streq(line->key, "allow_cors")) {
633 lwan->config.allow_cors =
634 parse_bool(line->value, default_config.allow_cors);
635 } else if (streq(line->key, "use_dynamic_buffer")) {
636 lwan->config.use_dynamic_buffer =
637 parse_bool(line->value, default_config.use_dynamic_buffer);
638 } else if (streq(line->key, "expires")) {
639 lwan->config.expires =
640 parse_time_period(line->value, default_config.expires);
641 } else if (streq(line->key, "error_template")) {
642 free(lwan->config.error_template);
643 lwan->config.error_template = strdup(line->value);
644 } else if (streq(line->key, "threads")) {
645 long n_threads =
646 parse_long(line->value, default_config.n_threads);
647 if (n_threads < 0)
648 config_error(conf, "Invalid number of threads: %ld",
649 n_threads);
650 lwan->config.n_threads = (unsigned int)n_threads;
651 } else if (streq(line->key, "max_post_data_size")) {
652 long max_post_data_size = parse_long(
653 line->value, (long)default_config.max_post_data_size);
654 if (max_post_data_size < 0)
655 config_error(conf, "Negative maximum post data size");
656 else if (max_post_data_size > 128 * (1 << 20))
657 config_error(conf,
658 "Maximum post data can't be over 128MiB");
659 lwan->config.max_post_data_size = (size_t)max_post_data_size;
660 } else if (streq(line->key, "max_put_data_size")) {
661 long max_put_data_size = parse_long(
662 line->value, (long)default_config.max_put_data_size);
663 if (max_put_data_size < 0)
664 config_error(conf, "Negative maximum put data size");
665 else if (max_put_data_size > 128 * (1 << 20))
666 config_error(conf,
667 "Maximum put data can't be over 128MiB");
668 lwan->config.max_put_data_size = (size_t)max_put_data_size;
669 } else if (streq(line->key, "allow_temp_files")) {
670 bool_Bool has_post, has_put;
671
672 if (strstr(line->value, "all")) {
673 has_post = has_put = true1;
674 } else {
675 has_post = !!strstr(line->value, "post");
676 has_put = !!strstr(line->value, "put");
677 }
678
679 lwan->config.allow_post_temp_file = has_post;
680 lwan->config.allow_put_temp_file = has_put;
681 } else {
682 config_error(conf, "Unknown config key: %s", line->key);
683 }
684 break;
685 case CONFIG_LINE_TYPE_SECTION:
686 if (streq(line->key, "site")) {
11
Taking true branch
687 if (!has_site
11.1
'has_site' is false
) {
12
Taking true branch
688 parse_site(conf, line, lwan);
13
Calling 'parse_site'
689 has_site = true1;
690 } else {
691 config_error(conf, "Only one site may be configured");
692 }
693 } else if (streq(line->key, "straitjacket")) {
694 lwan_straitjacket_enforce_from_config(conf);
695 } else if (streq(line->key, "headers")) {
696 parse_global_headers(conf, lwan);
697 } else if (streq(line->key, "listener")) {
698 if (has_listener) {
699 config_error(conf, "Listener already set up");
700 } else {
701 parse_listener(conf, line, lwan);
702 has_listener = true1;
703 }
704 } else if (streq(line->key, "tls_listener")) {
705 if (has_tls_listener) {
706 config_error(conf, "TLS Listener already set up");
707 } else {
708 parse_tls_listener(conf, line, lwan);
709 has_tls_listener = true1;
710 }
711 } else {
712 config_error(conf, "Unknown section type: %s", line->key);
713 }
714 break;
715 case CONFIG_LINE_TYPE_SECTION_END:
716 config_error(conf, "Unexpected section end");
717 }
718 }
719
720 if (config_last_error(conf)) {
721 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"
, 722, __FUNCTION__, "Error on config file \"%s\", line %d: %s"
, path, config_cur_line(conf), config_last_error(conf))
722 config_cur_line(conf), config_last_error(conf))lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 722, __FUNCTION__, "Error on config file \"%s\", line %d: %s"
, path, config_cur_line(conf), config_last_error(conf))
;
723 lwan_trie_destroy(&lwan->url_map_trie);
724 }
725
726 config_close(conf);
727
728 return true1;
729}
730
731static void try_setup_from_config(struct lwan *l,
732 const struct lwan_config *config)
733{
734 if (!setup_from_config(l, config->config_file_path)) {
3
Calling 'setup_from_config'
735 if (config->config_file_path) {
736 lwan_status_critical("Could not read config file: %s",lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 737, __FUNCTION__, "Could not read config file: %s", config
->config_file_path)
737 config->config_file_path)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 737, __FUNCTION__, "Could not read config file: %s", config
->config_file_path)
;
738 }
739 }
740
741 lwan_status_init(l); /* `quiet` key might have changed value. */
742
743 l->config.request_flags =
744 (l->config.proxy_protocol ? REQUEST_ALLOW_PROXY_REQS : 0) |
745 (l->config.allow_cors ? REQUEST_ALLOW_CORS : 0) |
746 (l->config.ssl.send_hsts_header ? REQUEST_WANTS_HSTS_HEADER : 0);
747}
748
749static rlim_t setup_open_file_count_limits(void)
750{
751 struct rlimit r;
752
753 if (getrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) {
754 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"
, 756, __FUNCTION__, "Could not obtain maximum number of file "
"descriptors. Assuming %d", 256)
755 "descriptors. Assuming %d",lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 756, __FUNCTION__, "Could not obtain maximum number of file "
"descriptors. Assuming %d", 256)
756 OPEN_MAX)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 756, __FUNCTION__, "Could not obtain maximum number of file "
"descriptors. Assuming %d", 256)
;
757 return OPEN_MAX256;
758 }
759
760 if (r.rlim_max != r.rlim_cur) {
761 const rlim_t current = r.rlim_cur;
762
763 if (r.rlim_max == RLIM_INFINITY0xffffffffffffffffuLL && r.rlim_cur < OPEN_MAX256) {
764 r.rlim_cur = OPEN_MAX256;
765 } else if (r.rlim_cur < r.rlim_max) {
766 r.rlim_cur = r.rlim_max;
767 } else {
768 /* Shouldn't happen, so just return the current value. */
769 goto out;
770 }
771
772 if (setrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) {
773 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"
, 775, __FUNCTION__, "Could not raise maximum number of file "
"descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max
, current)
774 "descriptors to %" PRIu64 ". Leaving at "lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 775, __FUNCTION__, "Could not raise maximum number of file "
"descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max
, current)
775 "%" PRIu64, r.rlim_max, current)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 775, __FUNCTION__, "Could not raise maximum number of file "
"descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max
, current)
;
776 r.rlim_cur = current;
777 }
778 }
779
780out:
781 return r.rlim_cur;
782}
783
784static void allocate_connections(struct lwan *l, size_t max_open_files)
785{
786 const size_t sz = max_open_files * sizeof(struct lwan_connection);
787
788 l->conns = lwan_aligned_alloc(sz, 64);
789 if (UNLIKELY(!l->conns)__builtin_expect(((!l->conns)), (0)))
790 lwan_status_critical_perror("lwan_alloc_aligned")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 790, __FUNCTION__, "lwan_alloc_aligned")
;
791
792 memset(l->conns, 0, sz);
793}
794
795static void get_number_of_cpus(struct lwan *l)
796{
797 long n_online_cpus = sysconf(_SC_NPROCESSORS_ONLN_SC_NPROCESSORS_ONLN);
798 long n_available_cpus = sysconf(_SC_NPROCESSORS_CONF_SC_NPROCESSORS_CONF);
799
800 if (n_online_cpus < 0) {
801 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 online CPUs, assuming 1 CPU"
)
802 "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"
, 802, __FUNCTION__, "Could not get number of online CPUs, assuming 1 CPU"
)
;
803 n_online_cpus = 1;
804 }
805
806 if (n_available_cpus < 0) {
807 lwan_status_warning(lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 809, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs"
, n_online_cpus)
808 "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"
, 809, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs"
, n_online_cpus)
809 n_online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 809, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs"
, n_online_cpus)
;
810 n_available_cpus = n_online_cpus;
811 }
812
813 l->online_cpus = (unsigned int)n_online_cpus;
814 l->available_cpus = (unsigned int)n_available_cpus;
815}
816
817void lwan_init(struct lwan *l) { lwan_init_with_config(l, &default_config); }
1
Calling 'lwan_init_with_config'
818
819const struct lwan_config *lwan_get_default_config(void)
820{
821 return &default_config;
822}
823
824static char *dup_or_null(const char *s)
825{
826 return s ? strdup(s) : NULL((void*)0);
827}
828
829void lwan_init_with_config(struct lwan *l, const struct lwan_config *config)
830{
831 /* Load defaults */
832 memset(l, 0, sizeof(*l));
833 memcpy(&l->config, config, sizeof(*config));
834 l->config.listener = dup_or_null(l->config.listener);
835 l->config.config_file_path = dup_or_null(l->config.config_file_path);
836 l->config.ssl.key = dup_or_null(l->config.ssl.key);
837 l->config.ssl.cert = dup_or_null(l->config.ssl.cert);
838
839 /* Initialize status first, as it is used by other things during
840 * their initialization. */
841 lwan_status_init(l);
842
843 /* These will only print debugging messages. Debug messages are always
844 * printed if we're on a debug build, so the quiet setting will be
845 * respected. */
846 lwan_job_thread_init();
847 lwan_tables_init();
848
849 /* Get the number of CPUs here because straightjacket might be active
850 * and this will block access to /proc and /sys, which will cause
851 * get_number_of_cpus() to get incorrect fallback values. */
852 get_number_of_cpus(l);
853
854 try_setup_from_config(l, config);
2
Calling 'try_setup_from_config'
855
856 if (!l->headers.len)
857 build_response_headers(l, config->global_headers);
858
859 lwan_response_init(l);
860
861 /* Continue initialization as normal. */
862 lwan_status_debug("Initializing lwan web server")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 862, __FUNCTION__, "Initializing lwan web server")
;
863
864 if (!l->config.n_threads) {
865 l->thread.count = l->online_cpus;
866 if (l->thread.count == 1)
867 l->thread.count = 2;
868 } else if (l->config.n_threads > 3 * l->online_cpus) {
869 l->thread.count = l->online_cpus * 3;
870
871 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"
, 874, __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 "(out of %d configured CPUs); capping to %d threads",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 874, __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)
873 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"
, 874, __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)
874 3 * l->online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 874, __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)
;
875 } else if (l->config.n_threads > 255) {
876 l->thread.count = 256;
877
878 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"
, 879, __FUNCTION__, "%d threads requested, but max 256 supported"
, l->config.n_threads)
879 l->config.n_threads)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 879, __FUNCTION__, "%d threads requested, but max 256 supported"
, l->config.n_threads)
;
880 } else {
881 l->thread.count = l->config.n_threads;
882 }
883
884 rlim_t max_open_files = setup_open_file_count_limits();
885 allocate_connections(l, (size_t)max_open_files);
886
887 l->thread.max_fd = (unsigned)max_open_files / (unsigned)l->thread.count;
888 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"
, 889, __FUNCTION__, "Using %d threads, maximum %d sockets per thread"
, l->thread.count, l->thread.max_fd)
889 l->thread.count, l->thread.max_fd)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 889, __FUNCTION__, "Using %d threads, maximum %d sockets per thread"
, l->thread.count, l->thread.max_fd)
;
890
891 signal(SIGPIPE13, SIG_IGN((__sighandler_t) 1));
892
893 lwan_readahead_init();
894 lwan_thread_init(l);
895 lwan_http_authorize_init();
896}
897
898void lwan_shutdown(struct lwan *l)
899{
900 lwan_status_info("Shutting down")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 900, __FUNCTION__, "Shutting down")
;
901
902 free(l->config.listener);
903 free(l->config.error_template);
904 free(l->config.config_file_path);
905
906 lwan_always_bzero(l->config.ssl.cert, strlen(l->config.ssl.cert));
907 free(l->config.ssl.cert);
908 lwan_always_bzero(l->config.ssl.key, strlen(l->config.ssl.key));
909 free(l->config.ssl.key);
910
911 lwan_job_thread_shutdown();
912 lwan_thread_shutdown(l);
913
914 lwan_status_debug("Shutting down URL handlers")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 914, __FUNCTION__, "Shutting down URL handlers")
;
915 lwan_trie_destroy(&l->url_map_trie);
916
917 free(l->headers.value);
918 free(l->conns);
919
920 lwan_response_shutdown(l);
921 lwan_tables_shutdown();
922 lwan_status_shutdown(l);
923 lwan_http_authorize_shutdown();
924 lwan_readahead_shutdown();
925}
926
927void lwan_main_loop(struct lwan *l)
928{
929 lwan_status_info("Ready to serve")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 929, __FUNCTION__, "Ready to serve")
;
930
931 lwan_job_thread_main_loop();
932}
933
934#ifdef CLOCK_MONOTONIC_COARSE6
935__attribute__((constructor)) static void detect_fastest_monotonic_clock(void)
936{
937 struct timespec ts;
938
939 if (!clock_gettime(CLOCK_MONOTONIC_COARSE6, &ts))
940 monotonic_clock_id = CLOCK_MONOTONIC_COARSE6;
941}
942#endif
943
944void lwan_set_thread_name(const char *name)
945{
946 char thread_name[16];
947 char process_name[PATH_MAX4096];
948 char *tmp;
949 int ret;
950
951 if (proc_pidpath(getpid(), process_name, sizeof(process_name)) < 0)
952 return;
953
954 tmp = strrchr(process_name, '/');
955 if (!tmp)
956 return;
957
958 ret = snprintf(thread_name, sizeof(thread_name), "%s %s", tmp + 1, name);
959 if (ret < 0)
960 return;
961
962 pthread_set_name_np(pthread_self(), thread_name);
963}