Bug Summary

File:lwan.c
Warning:line 335, column 17
Potential memory leak

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name lwan.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -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 -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -fno-plt -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/clang/11.1.0 -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 /usr/include/luajit-2.0 -I /usr/include/valgrind -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib -I /home/buildbot/lwan-worker/clang-analyze/build -internal-isystem /usr/local/include -internal-isystem /usr/lib/clang/11.1.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-unused-parameter -Wno-free-nonheap-object -std=gnu99 -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 -o /home/buildbot/lwan-worker/clang-analyze/CLANG/2021-04-10-223947-2102580-1 -x c /home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c
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. */
49static_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() */
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 .reuse_port = false0,
60 .proxy_protocol = false0,
61 .allow_cors = false0,
62 .expires = 1 * ONE_WEEK(((60 * 60) * 24) * 7),
63 .n_threads = 0,
64 .max_post_data_size = 10 * DEFAULT_BUFFER_SIZE4096,
65 .allow_post_temp_file = false0,
66 .max_put_data_size = 10 * DEFAULT_BUFFER_SIZE4096,
67 .allow_put_temp_file = false0,
68};
69
70LWAN_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)))
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 (!strcasecmp(name, "Date"))
149 return false0;
150 if (!strcasecmp(name, "Expires"))
151 return false0;
152 if (!strcasecmp(name, "WWW-Authenticate"))
153 return false0;
154 if (!strcasecmp(name, "Connection"))
155 return false0;
156 if (!strcasecmp(name, "Content-Type"))
157 return false0;
158 if (!strcasecmp(name, "Transfer-Encoding"))
159 return false0;
160 if (!strncasecmp(name, "Access-Control-Allow-",
161 sizeof("Access-Control-Allow-") - 1))
162 return false0;
163
164 return true1;
165}
166
167static void build_response_headers(struct lwan *l,
168 const struct lwan_key_value *kv)
169{
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(&l->headers);
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 (!strcasecmp(kv->key, "Server"))
181 set_server = true1;
182
183 lwan_strbuf_append_printf(&l->headers, "\r\n%s: %s", kv->key,
184 kv->value);
185 }
186 }
187
188 if (!set_server)
189 lwan_strbuf_append_strz(&l->headers, "\r\nServer: lwan");
190
191 lwan_strbuf_append_strz(&l->headers, "\r\n\r\n");
192}
193
194static void parse_global_headers(struct config *c,
195 struct lwan *lwan)
196{
197 struct lwan_key_value_array hdrs;
198 const struct config_line *l;
199 struct lwan_key_value *kv;
200
201 lwan_key_value_array_init(&hdrs);
202
203 while ((l = config_read_line(c))) {
204 switch (l->type) {
205 case CONFIG_LINE_TYPE_SECTION:
206 config_error(
207 c, "No sections are supported under the 'headers' section");
208 goto cleanup;
209
210 case CONFIG_LINE_TYPE_LINE:
211 kv = lwan_key_value_array_append(&hdrs);
212 if (!kv) {
213 lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 214, __FUNCTION__, "Could not allocate memory for custom response header"
)
214 "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 214, __FUNCTION__, "Could not allocate memory for custom response header"
)
;
215 }
216
217 kv->key = strdup(l->key);
218 if (!kv->key) {
219 lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 220, __FUNCTION__, "Could not allocate memory for custom response header"
)
220 "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 220, __FUNCTION__, "Could not allocate memory for custom response header"
)
;
221 }
222
223 kv->value = strdup(l->value);
224 if (!kv->value) {
225 lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 226, __FUNCTION__, "Could not allocate memory for custom response header"
)
226 "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 226, __FUNCTION__, "Could not allocate memory for custom response header"
)
;
227 }
228 break;
229
230 case CONFIG_LINE_TYPE_SECTION_END:
231 kv = lwan_key_value_array_append(&hdrs);
232 if (!kv) {
233 lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 234, __FUNCTION__, "Could not allocate memory for custom response header"
)
234 "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 234, __FUNCTION__, "Could not allocate memory for custom response header"
)
;
235 }
236
237 kv->key = NULL((void*)0);
238 kv->value = NULL((void*)0);
239
240 build_response_headers(lwan, lwan_key_value_array_get_array(&hdrs));
241 goto cleanup;
242 }
243 }
244
245 config_error(c, "EOF while looking for end of 'headers' section");
246
247cleanup:
248 LWAN_ARRAY_FOREACH (&hdrs, kv)for (kv = (&hdrs)->base.base; kv < ((typeof(kv))(&
hdrs)->base.base + (&hdrs)->base.elements); kv++)
{
249 free(kv->key);
250 free(kv->value);
251 }
252 lwan_key_value_array_reset(&hdrs);
253}
254
255static void parse_listener_prefix_authorization(struct config *c,
256 const struct config_line *l,
257 struct lwan_url_map *url_map)
258{
259 if (!streq(l->value, "basic")) {
260 config_error(c, "Only basic authorization supported");
261 return;
262 }
263
264 memset(&url_map->authorization, 0, sizeof(url_map->authorization));
265
266 while ((l = config_read_line(c))) {
267 switch (l->type) {
268 case CONFIG_LINE_TYPE_LINE:
269 if (streq(l->key, "realm")) {
270 free(url_map->authorization.realm);
271 url_map->authorization.realm = strdup(l->value);
272 } else if (streq(l->key, "password_file")) {
273 free(url_map->authorization.password_file);
274 url_map->authorization.password_file = realpath(l->value, NULL((void*)0));
275 if (!url_map->authorization.password_file)
276 config_error(c, "Could not determine full path for password file: %s", l->value);
277 }
278 break;
279
280 case CONFIG_LINE_TYPE_SECTION:
281 config_error(c, "Unexpected section: %s", l->key);
282 goto error;
283
284 case CONFIG_LINE_TYPE_SECTION_END:
285 if (!url_map->authorization.realm)
286 url_map->authorization.realm = strdup("Lwan");
287 if (!url_map->authorization.password_file)
288 url_map->authorization.password_file = strdup("htpasswd");
289
290 url_map->flags |= HANDLER_MUST_AUTHORIZE;
291 return;
292 }
293 }
294
295 config_error(c, "Could not find end of authorization section");
296
297error:
298 free(url_map->authorization.realm);
299 free(url_map->authorization.password_file);
300}
301
302__attribute__((no_sanitize_address))
303static const char *get_module_name(const struct lwan_module *module)
304{
305 const struct lwan_module_info *iter;
306
307 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)++
)
{
308 if (iter->module == module)
309 return iter->name;
310 }
311
312 return "<unknown>";
313}
314
315static void parse_listener_prefix(struct config *c,
316 const struct config_line *l,
317 struct lwan *lwan,
318 const struct lwan_module *module,
319 void *handler)
320{
321 struct lwan_url_map url_map = {};
322 struct hash *hash = hash_str_new(free, free);
323 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); }))
;
324 struct config *isolated;
325
326 if (!hash)
21
Assuming 'hash' is non-null
22
Taking false branch
327 lwan_status_critical("Could not allocate hash table")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 327, __FUNCTION__, "Could not allocate hash table")
;
328
329 isolated = config_isolate_section(c, l);
330 if (!isolated) {
23
Assuming 'isolated' is non-null
24
Taking false branch
331 config_error(c, "Could not isolate configuration file");
332 goto out;
333 }
334
335 while ((l = config_read_line(c))) {
25
Loop condition is true. Entering loop body
29
Potential memory leak
336 switch (l->type) {
26
Control jumps to 'case CONFIG_LINE_TYPE_LINE:' at line 337
337 case CONFIG_LINE_TYPE_LINE:
338 hash_add(hash, strdup(l->key), strdup(l->value));
27
Memory is allocated
339 break;
28
Execution continues on line 335
340
341 case CONFIG_LINE_TYPE_SECTION:
342 if (streq(l->key, "authorization")) {
343 parse_listener_prefix_authorization(c, l, &url_map);
344 } else if (!config_skip_section(c, l)) {
345 config_error(c, "Could not skip section");
346 goto out;
347 }
348 break;
349
350 case CONFIG_LINE_TYPE_SECTION_END:
351 goto add_map;
352 }
353 }
354
355 config_error(c, "Expecting section end while parsing prefix");
356 goto out;
357
358add_map:
359 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"
, 359, __extension__ __PRETTY_FUNCTION__); }))
;
360
361 if (handler) {
362 url_map.handler = handler;
363 url_map.flags |= HANDLER_PARSE_MASK | HANDLER_DATA_IS_HASH_TABLE;
364 url_map.data = hash;
365 url_map.module = NULL((void*)0);
366
367 hash = NULL((void*)0);
368 } else if (module->create_from_hash && module->handle_request) {
369 lwan_status_debug("Initializing module %s from config",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 370, __FUNCTION__, "Initializing module %s from config", get_module_name
(module))
370 get_module_name(module))lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 370, __FUNCTION__, "Initializing module %s from config", get_module_name
(module))
;
371
372 url_map.data = module->create_from_hash(prefix, hash);
373 if (!url_map.data) {
374 config_error(c, "Could not create module instance");
375 goto out;
376 }
377
378 if (module->parse_conf && !module->parse_conf(url_map.data, isolated)) {
379 const char *msg = config_last_error(isolated);
380
381 config_error(c, "Error from module: %s", msg ? msg : "Unknown");
382 goto out;
383 }
384
385 url_map.handler = module->handle_request;
386 url_map.flags |= module->flags;
387 url_map.module = module;
388 } else if (UNLIKELY(!module->create_from_hash)__builtin_expect(((!module->create_from_hash)), (0))) {
389 config_error(c, "Module isn't prepared to load settings from a file; "
390 "create_from_hash() method isn't present");
391 goto out;
392 } else if (UNLIKELY(!module->handle_request)__builtin_expect(((!module->handle_request)), (0))) {
393 config_error(c, "Module does not have handle_request() method");
394 goto out;
395 }
396
397 add_url_map(&lwan->url_map_trie, prefix, &url_map);
398
399out:
400 hash_free(hash);
401 config_close(isolated);
402}
403
404void lwan_set_url_map(struct lwan *l, const struct lwan_url_map *map)
405{
406 lwan_trie_destroy(&l->url_map_trie);
407 if (UNLIKELY(!lwan_trie_init(&l->url_map_trie, destroy_urlmap))__builtin_expect(((!lwan_trie_init(&l->url_map_trie, destroy_urlmap
))), (0))
)
408 lwan_status_critical_perror("Could not initialize trie")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 408, __FUNCTION__, "Could not initialize trie")
;
409
410 for (; map->prefix; map++) {
411 struct lwan_url_map *copy = add_url_map(&l->url_map_trie, NULL((void*)0), map);
412
413 if (copy->module && copy->module->create) {
414 lwan_status_debug("Initializing module %s from struct",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 415, __FUNCTION__, "Initializing module %s from struct", get_module_name
(copy->module))
415 get_module_name(copy->module))lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 415, __FUNCTION__, "Initializing module %s from struct", get_module_name
(copy->module))
;
416
417 copy->data = copy->module->create(map->prefix, copy->args);
418 if (!copy->data) {
419 lwan_status_critical("Could not initialize module %s",lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 420, __FUNCTION__, "Could not initialize module %s", get_module_name
(copy->module))
420 get_module_name(copy->module))lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 420, __FUNCTION__, "Could not initialize module %s", get_module_name
(copy->module))
;
421 }
422
423 copy->flags = copy->module->flags;
424 copy->handler = copy->module->handle_request;
425 } else {
426 copy->flags = HANDLER_PARSE_MASK;
427 }
428 }
429}
430
431static void parse_listener(struct config *c,
432 const struct config_line *l,
433 struct lwan *lwan)
434{
435 free(lwan->config.listener);
436 lwan->config.listener = strdup(l->value);
437
438 while ((l = config_read_line(c))) {
14
Loop condition is true. Entering loop body
439 switch (l->type) {
15
Control jumps to 'case CONFIG_LINE_TYPE_SECTION:' at line 443
440 case CONFIG_LINE_TYPE_LINE:
441 config_error(c, "Expecting prefix section");
442 return;
443 case CONFIG_LINE_TYPE_SECTION:
444 if (l->key[0] == '&') {
16
Assuming the condition is false
17
Taking false branch
445 void *handler = find_handler(l->key + 1);
446 if (handler) {
447 parse_listener_prefix(c, l, lwan, NULL((void*)0), handler);
448 continue;
449 }
450
451 config_error(c, "Could not find handler name: %s", l->key + 1);
452 return;
453 }
454
455 const struct lwan_module *module = find_module(l->key);
456 if (module) {
18
Assuming 'module' is non-null
19
Taking true branch
457 parse_listener_prefix(c, l, lwan, module, NULL((void*)0));
20
Calling 'parse_listener_prefix'
458 continue;
459 }
460
461 config_error(c, "Invalid section or module not found: %s", l->key);
462 return;
463 case CONFIG_LINE_TYPE_SECTION_END:
464 return;
465 }
466 }
467
468 config_error(c, "Expecting section end while parsing listener");
469}
470
471const char *lwan_get_config_path(char *path_buf, size_t path_buf_len)
472{
473 char buffer[PATH_MAX4096];
474
475 if (proc_pidpath(getpid(), buffer, sizeof(buffer)) < 0)
476 goto out;
477
478 char *path = strrchr(buffer, '/');
479 if (!path)
480 goto out;
481 int ret = snprintf(path_buf, path_buf_len, "%s.conf", path + 1);
482 if (ret < 0 || ret >= (int)path_buf_len)
483 goto out;
484
485 return path_buf;
486
487out:
488 return "lwan.conf";
489}
490
491static bool_Bool setup_from_config(struct lwan *lwan, const char *path)
492{
493 const struct config_line *line;
494 struct config *conf;
495 bool_Bool has_listener = false0;
496 char path_buf[PATH_MAX4096];
497
498 if (!path
3.1
'path' is null
)
4
Taking true branch
499 path = lwan_get_config_path(path_buf, sizeof(path_buf));
500 lwan_status_info("Loading configuration file: %s", path)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 500, __FUNCTION__, "Loading configuration file: %s", path)
;
501
502 conf = config_open(path);
503 if (!conf)
5
Assuming 'conf' is non-null
6
Taking false branch
504 return false0;
505
506 if (!lwan_trie_init(&lwan->url_map_trie, destroy_urlmap))
7
Assuming the condition is false
8
Taking false branch
507 return false0;
508
509 while ((line = config_read_line(conf))) {
9
Loop condition is true. Entering loop body
510 switch (line->type) {
10
Control jumps to 'case CONFIG_LINE_TYPE_SECTION:' at line 567
511 case CONFIG_LINE_TYPE_LINE:
512 if (streq(line->key, "keep_alive_timeout")) {
513 lwan->config.keep_alive_timeout = (unsigned int)parse_long(
514 line->value, default_config.keep_alive_timeout);
515 } else if (streq(line->key, "quiet")) {
516 lwan->config.quiet =
517 parse_bool(line->value, default_config.quiet);
518 } else if (streq(line->key, "reuse_port")) {
519 lwan->config.reuse_port =
520 parse_bool(line->value, default_config.reuse_port);
521 } else if (streq(line->key, "proxy_protocol")) {
522 lwan->config.proxy_protocol =
523 parse_bool(line->value, default_config.proxy_protocol);
524 } else if (streq(line->key, "allow_cors")) {
525 lwan->config.allow_cors =
526 parse_bool(line->value, default_config.allow_cors);
527 } else if (streq(line->key, "expires")) {
528 lwan->config.expires =
529 parse_time_period(line->value, default_config.expires);
530 } else if (streq(line->key, "error_template")) {
531 free(lwan->config.error_template);
532 lwan->config.error_template = strdup(line->value);
533 } else if (streq(line->key, "threads")) {
534 long n_threads =
535 parse_long(line->value, default_config.n_threads);
536 if (n_threads < 0)
537 config_error(conf, "Invalid number of threads: %ld",
538 n_threads);
539 lwan->config.n_threads = (unsigned int)n_threads;
540 } else if (streq(line->key, "max_post_data_size")) {
541 long max_post_data_size = parse_long(
542 line->value, (long)default_config.max_post_data_size);
543 if (max_post_data_size < 0)
544 config_error(conf, "Negative maximum post data size");
545 else if (max_post_data_size > 128 * (1 << 20))
546 config_error(conf,
547 "Maximum post data can't be over 128MiB");
548 lwan->config.max_post_data_size = (size_t)max_post_data_size;
549 } else if (streq(line->key, "max_put_data_size")) {
550 long max_put_data_size = parse_long(
551 line->value, (long)default_config.max_put_data_size);
552 if (max_put_data_size < 0)
553 config_error(conf, "Negative maximum put data size");
554 else if (max_put_data_size > 128 * (1 << 20))
555 config_error(conf,
556 "Maximum put data can't be over 128MiB");
557 lwan->config.max_put_data_size = (size_t)max_put_data_size;
558 } else if (streq(line->key, "allow_temp_files")) {
559 if (strstr(line->value, "post"))
560 lwan->config.allow_post_temp_file = true1;
561 if (strstr(line->value, "put"))
562 lwan->config.allow_put_temp_file = true1;
563 } else {
564 config_error(conf, "Unknown config key: %s", line->key);
565 }
566 break;
567 case CONFIG_LINE_TYPE_SECTION:
568 if (streq(line->key, "listener")) {
11
Taking true branch
569 if (!has_listener
11.1
'has_listener' is false
) {
12
Taking true branch
570 parse_listener(conf, line, lwan);
13
Calling 'parse_listener'
571 has_listener = true1;
572 } else {
573 config_error(conf, "Only one listener supported");
574 }
575 } else if (streq(line->key, "straitjacket")) {
576 lwan_straitjacket_enforce_from_config(conf);
577 } else if (streq(line->key, "headers")) {
578 parse_global_headers(conf, lwan);
579 } else {
580 config_error(conf, "Unknown section type: %s", line->key);
581 }
582 break;
583 case CONFIG_LINE_TYPE_SECTION_END:
584 config_error(conf, "Unexpected section end");
585 }
586 }
587
588 if (config_last_error(conf)) {
589 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"
, 590, __FUNCTION__, "Error on config file \"%s\", line %d: %s"
, path, config_cur_line(conf), config_last_error(conf))
590 config_cur_line(conf), config_last_error(conf))lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 590, __FUNCTION__, "Error on config file \"%s\", line %d: %s"
, path, config_cur_line(conf), config_last_error(conf))
;
591 }
592
593 config_close(conf);
594
595 return true1;
596}
597
598static void try_setup_from_config(struct lwan *l,
599 const struct lwan_config *config)
600{
601 if (!setup_from_config(l, config->config_file_path)) {
3
Calling 'setup_from_config'
602 if (config->config_file_path) {
603 lwan_status_critical("Could not read config file: %s",lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 604, __FUNCTION__, "Could not read config file: %s", config
->config_file_path)
604 config->config_file_path)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 604, __FUNCTION__, "Could not read config file: %s", config
->config_file_path)
;
605 }
606 }
607
608 lwan_status_init(l); /* `quiet` key might have changed value. */
609
610 l->config.request_flags =
611 (l->config.proxy_protocol ? REQUEST_ALLOW_PROXY_REQS : 0) |
612 (l->config.allow_cors ? REQUEST_ALLOW_CORS : 0);
613}
614
615static rlim_t setup_open_file_count_limits(void)
616{
617 struct rlimit r;
618
619 if (getrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) {
620 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"
, 622, __FUNCTION__, "Could not obtain maximum number of file "
"descriptors. Assuming %d", 256)
621 "descriptors. Assuming %d",lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 622, __FUNCTION__, "Could not obtain maximum number of file "
"descriptors. Assuming %d", 256)
622 OPEN_MAX)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 622, __FUNCTION__, "Could not obtain maximum number of file "
"descriptors. Assuming %d", 256)
;
623 return OPEN_MAX256;
624 }
625
626 if (r.rlim_max != r.rlim_cur) {
627 const rlim_t current = r.rlim_cur;
628
629 if (r.rlim_max == RLIM_INFINITY0xffffffffffffffffuLL && r.rlim_cur < OPEN_MAX256) {
630 r.rlim_cur = OPEN_MAX256;
631 } else if (r.rlim_cur < r.rlim_max) {
632 r.rlim_cur = r.rlim_max;
633 } else {
634 /* Shouldn't happen, so just return the current value. */
635 goto out;
636 }
637
638 if (setrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) {
639 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"
, 641, __FUNCTION__, "Could not raise maximum number of file "
"descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max
, current)
640 "descriptors to %" PRIu64 ". Leaving at "lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 641, __FUNCTION__, "Could not raise maximum number of file "
"descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max
, current)
641 "%" PRIu64, r.rlim_max, current)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 641, __FUNCTION__, "Could not raise maximum number of file "
"descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max
, current)
;
642 r.rlim_cur = current;
643 }
644 }
645
646out:
647 return r.rlim_cur;
648}
649
650static void allocate_connections(struct lwan *l, size_t max_open_files)
651{
652 const size_t sz = max_open_files * sizeof(struct lwan_connection);
653
654 l->conns = lwan_aligned_alloc(sz, 64);
655 if (UNLIKELY(!l->conns)__builtin_expect(((!l->conns)), (0)))
656 lwan_status_critical_perror("lwan_alloc_aligned")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 656, __FUNCTION__, "lwan_alloc_aligned")
;
657
658 memset(l->conns, 0, sz);
659}
660
661static void get_number_of_cpus(struct lwan *l)
662{
663 long n_online_cpus = sysconf(_SC_NPROCESSORS_ONLN_SC_NPROCESSORS_ONLN);
664 long n_available_cpus = sysconf(_SC_NPROCESSORS_CONF_SC_NPROCESSORS_CONF);
665
666 if (n_online_cpus < 0) {
667 lwan_status_warning(lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 668, __FUNCTION__, "Could not get number of online CPUs, assuming 1 CPU"
)
668 "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"
, 668, __FUNCTION__, "Could not get number of online CPUs, assuming 1 CPU"
)
;
669 n_online_cpus = 1;
670 }
671
672 if (n_available_cpus < 0) {
673 lwan_status_warning(lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 675, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs"
, n_online_cpus)
674 "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"
, 675, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs"
, n_online_cpus)
675 n_online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 675, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs"
, n_online_cpus)
;
676 n_available_cpus = n_online_cpus;
677 }
678
679 l->online_cpus = (unsigned int)n_online_cpus;
680 l->available_cpus = (unsigned int)n_available_cpus;
681}
682
683void lwan_init(struct lwan *l) { lwan_init_with_config(l, &default_config); }
1
Calling 'lwan_init_with_config'
684
685const struct lwan_config *lwan_get_default_config(void)
686{
687 return &default_config;
688}
689
690static char *dup_or_null(const char *s)
691{
692 return s ? strdup(s) : NULL((void*)0);
693}
694
695void lwan_init_with_config(struct lwan *l, const struct lwan_config *config)
696{
697 /* Load defaults */
698 memset(l, 0, sizeof(*l));
699 memcpy(&l->config, config, sizeof(*config));
700 l->config.listener = dup_or_null(l->config.listener);
701 l->config.config_file_path = dup_or_null(l->config.config_file_path);
702
703 /* Initialize status first, as it is used by other things during
704 * their initialization. */
705 lwan_status_init(l);
706
707 /* These will only print debugging messages. Debug messages are always
708 * printed if we're on a debug build, so the quiet setting will be
709 * respected. */
710 lwan_job_thread_init();
711 lwan_tables_init();
712
713 try_setup_from_config(l, config);
2
Calling 'try_setup_from_config'
714
715 if (!lwan_strbuf_get_length(&l->headers))
716 build_response_headers(l, config->global_headers);
717
718 lwan_response_init(l);
719
720 /* Continue initialization as normal. */
721 lwan_status_debug("Initializing lwan web server")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 721, __FUNCTION__, "Initializing lwan web server")
;
722
723 get_number_of_cpus(l);
724 if (!l->config.n_threads) {
725 l->thread.count = l->online_cpus;
726 if (l->thread.count == 1)
727 l->thread.count = 2;
728 } else if (l->config.n_threads > 3 * l->online_cpus) {
729 l->thread.count = l->online_cpus * 3;
730
731 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"
, 734, __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)
732 "(out of %d configured CPUs); capping to %d threads",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 734, __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)
733 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"
, 734, __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)
734 3 * l->online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 734, __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 } else if (l->config.n_threads > 255) {
736 l->thread.count = 256;
737
738 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"
, 739, __FUNCTION__, "%d threads requested, but max 256 supported"
, l->config.n_threads)
739 l->config.n_threads)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 739, __FUNCTION__, "%d threads requested, but max 256 supported"
, l->config.n_threads)
;
740 } else {
741 l->thread.count = l->config.n_threads;
742 }
743
744 rlim_t max_open_files = setup_open_file_count_limits();
745 allocate_connections(l, (size_t)max_open_files);
746
747 l->thread.max_fd = (unsigned)max_open_files / (unsigned)l->thread.count;
748 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"
, 749, __FUNCTION__, "Using %d threads, maximum %d sockets per thread"
, l->thread.count, l->thread.max_fd)
749 l->thread.count, l->thread.max_fd)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 749, __FUNCTION__, "Using %d threads, maximum %d sockets per thread"
, l->thread.count, l->thread.max_fd)
;
750
751 signal(SIGPIPE13, SIG_IGN((__sighandler_t) 1));
752
753 lwan_readahead_init();
754 lwan_thread_init(l);
755 lwan_socket_init(l);
756 lwan_http_authorize_init();
757}
758
759void lwan_shutdown(struct lwan *l)
760{
761 lwan_status_info("Shutting down")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 761, __FUNCTION__, "Shutting down")
;
762
763 free(l->config.listener);
764 free(l->config.error_template);
765 free(l->config.config_file_path);
766
767 lwan_job_thread_shutdown();
768 lwan_thread_shutdown(l);
769
770 lwan_status_debug("Shutting down URL handlers")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 770, __FUNCTION__, "Shutting down URL handlers")
;
771 lwan_trie_destroy(&l->url_map_trie);
772
773 lwan_strbuf_free(&l->headers);
774 free(l->conns);
775
776 lwan_response_shutdown(l);
777 lwan_tables_shutdown();
778 lwan_status_shutdown(l);
779 lwan_http_authorize_shutdown();
780 lwan_readahead_shutdown();
781}
782
783static ALWAYS_INLINEinline __attribute__((always_inline)) int schedule_client(struct lwan *l, int fd)
784{
785 struct lwan_thread *thread = l->conns[fd].thread;
786
787 lwan_thread_add_client(thread, fd);
788
789 return (int)(thread - l->thread.threads);
790}
791
792static volatile sig_atomic_t main_socket = -1;
793
794static_assert(sizeof(main_socket) >= sizeof(int),extern int (*__Static_assert_function (void)) [!!sizeof (struct
{ int __error_if_negative: (sizeof(main_socket) >= sizeof
(int)) ? 2 : -1; })]
795 "size of sig_atomic_t > size of int")extern int (*__Static_assert_function (void)) [!!sizeof (struct
{ int __error_if_negative: (sizeof(main_socket) >= sizeof
(int)) ? 2 : -1; })]
;
796
797static void sigint_handler(int signal_number __attribute__((unused)))
798{
799 if (main_socket < 0)
800 return;
801
802 shutdown((int)main_socket, SHUT_RDWRSHUT_RDWR);
803 close((int)main_socket);
804
805 main_socket = -1;
806}
807
808enum herd_accept { HERD_MORE = 0, HERD_GONE = -1, HERD_SHUTDOWN = 1 };
809
810struct core_bitmap {
811 uint64_t bitmap[4];
812};
813
814static ALWAYS_INLINEinline __attribute__((always_inline)) enum herd_accept
815accept_one(struct lwan *l, struct core_bitmap *cores)
816{
817 int fd = accept4((int)main_socket, NULL((void*)0), NULL((void*)0), SOCK_NONBLOCKSOCK_NONBLOCK | SOCK_CLOEXECSOCK_CLOEXEC);
818
819 if (LIKELY(fd >= 0)__builtin_expect((!!(fd >= 0)), (1))) {
820 int core = schedule_client(l, fd);
821
822 cores->bitmap[core / 64] |= UINT64_C(1)1UL<<(core % 64);
823
824 return HERD_MORE;
825 }
826
827 switch (errno(*__errno_location ())) {
828 case EAGAIN11:
829 return HERD_GONE;
830
831 case EBADF9:
832 case ECONNABORTED103:
833 case EINVAL22:
834 if (main_socket < 0) {
835 lwan_status_info("Signal 2 (Interrupt) received")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 835, __FUNCTION__, "Signal 2 (Interrupt) received")
;
836 } else {
837 lwan_status_info("Main socket closed for unknown reasons")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 837, __FUNCTION__, "Main socket closed for unknown reasons"
)
;
838 }
839 return HERD_SHUTDOWN;
840
841 default:
842 lwan_status_perror("accept")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 842, __FUNCTION__, "accept")
;
843 return HERD_MORE;
844 }
845}
846
847void lwan_main_loop(struct lwan *l)
848{
849 struct core_bitmap cores = {};
850
851 assert(main_socket == -1)((void) sizeof ((main_socket == -1) ? 1 : 0), __extension__ (
{ if (main_socket == -1) ; else __assert_fail ("main_socket == -1"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 851, __extension__ __PRETTY_FUNCTION__); }))
;
852 main_socket = l->main_socket;
853
854 if (signal(SIGINT2, sigint_handler) == SIG_ERR((__sighandler_t) -1))
855 lwan_status_critical("Could not set signal handler")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 855, __FUNCTION__, "Could not set signal handler")
;
856
857 lwan_status_info("Ready to serve")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 857, __FUNCTION__, "Ready to serve")
;
858
859 while (true1) {
860 enum herd_accept ha;
861
862 fcntl(l->main_socket, F_SETFL4, 0);
863 ha = accept_one(l, &cores);
864 if (ha == HERD_MORE) {
865 fcntl(l->main_socket, F_SETFL4, O_NONBLOCK04000);
866
867 do {
868 ha = accept_one(l, &cores);
869 } while (ha == HERD_MORE);
870 }
871
872 if (UNLIKELY(ha > HERD_MORE)__builtin_expect(((ha > HERD_MORE)), (0)))
873 break;
874
875 for (size_t i = 0; i < N_ELEMENTS(cores.bitmap)((!sizeof(char[1 - 2 * __builtin_types_compatible_p( __typeof__
(cores.bitmap), __typeof__(&(cores.bitmap)[0]))])) | sizeof
(cores.bitmap) / sizeof(cores.bitmap[0]))
; i++) {
876 for (uint64_t c = cores.bitmap[i]; c; c ^= c & -c) {
877 size_t core = (size_t)__builtin_ctzl(c);
878 lwan_thread_nudge(&l->thread.threads[i * 64 + core]);
879 }
880 }
881 cores = (struct core_bitmap){};
882 }
883}
884
885#ifdef CLOCK_MONOTONIC_COARSE6
886__attribute__((constructor)) static void detect_fastest_monotonic_clock(void)
887{
888 struct timespec ts;
889
890 if (!clock_gettime(CLOCK_MONOTONIC_COARSE6, &ts))
891 monotonic_clock_id = CLOCK_MONOTONIC_COARSE6;
892}
893#endif
894
895void lwan_set_thread_name(const char *name)
896{
897 char thread_name[16];
898 char process_name[PATH_MAX4096];
899 char *tmp;
900 int ret;
901
902 if (proc_pidpath(getpid(), process_name, sizeof(process_name)) < 0)
903 return;
904
905 tmp = strrchr(process_name, '/');
906 if (!tmp)
907 return;
908
909 ret = snprintf(thread_name, sizeof(thread_name), "%s %s", tmp + 1, name);
910 if (ret < 0)
911 return;
912
913 pthread_set_name_np(pthread_self(), thread_name);
914}