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-05-08-174439-3919459-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 574
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 bool_Bool has_post, has_put;
560
561 if (strstr(line->value, "all")) {
562 has_post = has_put = true1;
563 } else {
564 has_post = !!strstr(line->value, "post");
565 has_put = !!strstr(line->value, "put");
566 }
567
568 lwan->config.allow_post_temp_file = has_post;
569 lwan->config.allow_put_temp_file = has_put;
570 } else {
571 config_error(conf, "Unknown config key: %s", line->key);
572 }
573 break;
574 case CONFIG_LINE_TYPE_SECTION:
575 if (streq(line->key, "listener")) {
11
Taking true branch
576 if (!has_listener
11.1
'has_listener' is false
) {
12
Taking true branch
577 parse_listener(conf, line, lwan);
13
Calling 'parse_listener'
578 has_listener = true1;
579 } else {
580 config_error(conf, "Only one listener supported");
581 }
582 } else if (streq(line->key, "straitjacket")) {
583 lwan_straitjacket_enforce_from_config(conf);
584 } else if (streq(line->key, "headers")) {
585 parse_global_headers(conf, lwan);
586 } else {
587 config_error(conf, "Unknown section type: %s", line->key);
588 }
589 break;
590 case CONFIG_LINE_TYPE_SECTION_END:
591 config_error(conf, "Unexpected section end");
592 }
593 }
594
595 if (config_last_error(conf)) {
596 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"
, 597, __FUNCTION__, "Error on config file \"%s\", line %d: %s"
, path, config_cur_line(conf), config_last_error(conf))
597 config_cur_line(conf), config_last_error(conf))lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 597, __FUNCTION__, "Error on config file \"%s\", line %d: %s"
, path, config_cur_line(conf), config_last_error(conf))
;
598 }
599
600 config_close(conf);
601
602 return true1;
603}
604
605static void try_setup_from_config(struct lwan *l,
606 const struct lwan_config *config)
607{
608 if (!setup_from_config(l, config->config_file_path)) {
3
Calling 'setup_from_config'
609 if (config->config_file_path) {
610 lwan_status_critical("Could not read config file: %s",lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 611, __FUNCTION__, "Could not read config file: %s", config
->config_file_path)
611 config->config_file_path)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 611, __FUNCTION__, "Could not read config file: %s", config
->config_file_path)
;
612 }
613 }
614
615 lwan_status_init(l); /* `quiet` key might have changed value. */
616
617 l->config.request_flags =
618 (l->config.proxy_protocol ? REQUEST_ALLOW_PROXY_REQS : 0) |
619 (l->config.allow_cors ? REQUEST_ALLOW_CORS : 0);
620}
621
622static rlim_t setup_open_file_count_limits(void)
623{
624 struct rlimit r;
625
626 if (getrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) {
627 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"
, 629, __FUNCTION__, "Could not obtain maximum number of file "
"descriptors. Assuming %d", 256)
628 "descriptors. Assuming %d",lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 629, __FUNCTION__, "Could not obtain maximum number of file "
"descriptors. Assuming %d", 256)
629 OPEN_MAX)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 629, __FUNCTION__, "Could not obtain maximum number of file "
"descriptors. Assuming %d", 256)
;
630 return OPEN_MAX256;
631 }
632
633 if (r.rlim_max != r.rlim_cur) {
634 const rlim_t current = r.rlim_cur;
635
636 if (r.rlim_max == RLIM_INFINITY0xffffffffffffffffuLL && r.rlim_cur < OPEN_MAX256) {
637 r.rlim_cur = OPEN_MAX256;
638 } else if (r.rlim_cur < r.rlim_max) {
639 r.rlim_cur = r.rlim_max;
640 } else {
641 /* Shouldn't happen, so just return the current value. */
642 goto out;
643 }
644
645 if (setrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) {
646 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"
, 648, __FUNCTION__, "Could not raise maximum number of file "
"descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max
, current)
647 "descriptors to %" PRIu64 ". Leaving at "lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 648, __FUNCTION__, "Could not raise maximum number of file "
"descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max
, current)
648 "%" PRIu64, r.rlim_max, current)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 648, __FUNCTION__, "Could not raise maximum number of file "
"descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max
, current)
;
649 r.rlim_cur = current;
650 }
651 }
652
653out:
654 return r.rlim_cur;
655}
656
657static void allocate_connections(struct lwan *l, size_t max_open_files)
658{
659 const size_t sz = max_open_files * sizeof(struct lwan_connection);
660
661 l->conns = lwan_aligned_alloc(sz, 64);
662 if (UNLIKELY(!l->conns)__builtin_expect(((!l->conns)), (0)))
663 lwan_status_critical_perror("lwan_alloc_aligned")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 663, __FUNCTION__, "lwan_alloc_aligned")
;
664
665 memset(l->conns, 0, sz);
666}
667
668static void get_number_of_cpus(struct lwan *l)
669{
670 long n_online_cpus = sysconf(_SC_NPROCESSORS_ONLN_SC_NPROCESSORS_ONLN);
671 long n_available_cpus = sysconf(_SC_NPROCESSORS_CONF_SC_NPROCESSORS_CONF);
672
673 if (n_online_cpus < 0) {
674 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 online CPUs, assuming 1 CPU"
)
675 "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"
, 675, __FUNCTION__, "Could not get number of online CPUs, assuming 1 CPU"
)
;
676 n_online_cpus = 1;
677 }
678
679 if (n_available_cpus < 0) {
680 lwan_status_warning(lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 682, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs"
, n_online_cpus)
681 "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"
, 682, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs"
, n_online_cpus)
682 n_online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 682, __FUNCTION__, "Could not get number of available CPUs, assuming %ld CPUs"
, n_online_cpus)
;
683 n_available_cpus = n_online_cpus;
684 }
685
686 l->online_cpus = (unsigned int)n_online_cpus;
687 l->available_cpus = (unsigned int)n_available_cpus;
688}
689
690void lwan_init(struct lwan *l) { lwan_init_with_config(l, &default_config); }
1
Calling 'lwan_init_with_config'
691
692const struct lwan_config *lwan_get_default_config(void)
693{
694 return &default_config;
695}
696
697static char *dup_or_null(const char *s)
698{
699 return s ? strdup(s) : NULL((void*)0);
700}
701
702void lwan_init_with_config(struct lwan *l, const struct lwan_config *config)
703{
704 /* Load defaults */
705 memset(l, 0, sizeof(*l));
706 memcpy(&l->config, config, sizeof(*config));
707 l->config.listener = dup_or_null(l->config.listener);
708 l->config.config_file_path = dup_or_null(l->config.config_file_path);
709
710 /* Initialize status first, as it is used by other things during
711 * their initialization. */
712 lwan_status_init(l);
713
714 /* These will only print debugging messages. Debug messages are always
715 * printed if we're on a debug build, so the quiet setting will be
716 * respected. */
717 lwan_job_thread_init();
718 lwan_tables_init();
719
720 try_setup_from_config(l, config);
2
Calling 'try_setup_from_config'
721
722 if (!lwan_strbuf_get_length(&l->headers))
723 build_response_headers(l, config->global_headers);
724
725 lwan_response_init(l);
726
727 /* Continue initialization as normal. */
728 lwan_status_debug("Initializing lwan web server")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 728, __FUNCTION__, "Initializing lwan web server")
;
729
730 get_number_of_cpus(l);
731 if (!l->config.n_threads) {
732 l->thread.count = l->online_cpus;
733 if (l->thread.count == 1)
734 l->thread.count = 2;
735 } else if (l->config.n_threads > 3 * l->online_cpus) {
736 l->thread.count = l->online_cpus * 3;
737
738 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"
, 741, __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)
739 "(out of %d configured CPUs); capping to %d threads",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 741, __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)
740 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"
, 741, __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)
741 3 * l->online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 741, __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)
;
742 } else if (l->config.n_threads > 255) {
743 l->thread.count = 256;
744
745 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"
, 746, __FUNCTION__, "%d threads requested, but max 256 supported"
, l->config.n_threads)
746 l->config.n_threads)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 746, __FUNCTION__, "%d threads requested, but max 256 supported"
, l->config.n_threads)
;
747 } else {
748 l->thread.count = l->config.n_threads;
749 }
750
751 rlim_t max_open_files = setup_open_file_count_limits();
752 allocate_connections(l, (size_t)max_open_files);
753
754 l->thread.max_fd = (unsigned)max_open_files / (unsigned)l->thread.count;
755 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"
, 756, __FUNCTION__, "Using %d threads, maximum %d sockets per thread"
, l->thread.count, l->thread.max_fd)
756 l->thread.count, l->thread.max_fd)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 756, __FUNCTION__, "Using %d threads, maximum %d sockets per thread"
, l->thread.count, l->thread.max_fd)
;
757
758 signal(SIGPIPE13, SIG_IGN((__sighandler_t) 1));
759
760 lwan_readahead_init();
761 lwan_thread_init(l);
762 lwan_socket_init(l);
763 lwan_http_authorize_init();
764}
765
766void lwan_shutdown(struct lwan *l)
767{
768 lwan_status_info("Shutting down")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 768, __FUNCTION__, "Shutting down")
;
769
770 free(l->config.listener);
771 free(l->config.error_template);
772 free(l->config.config_file_path);
773
774 lwan_job_thread_shutdown();
775 lwan_thread_shutdown(l);
776
777 lwan_status_debug("Shutting down URL handlers")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 777, __FUNCTION__, "Shutting down URL handlers")
;
778 lwan_trie_destroy(&l->url_map_trie);
779
780 lwan_strbuf_free(&l->headers);
781 free(l->conns);
782
783 lwan_response_shutdown(l);
784 lwan_tables_shutdown();
785 lwan_status_shutdown(l);
786 lwan_http_authorize_shutdown();
787 lwan_readahead_shutdown();
788}
789
790static ALWAYS_INLINEinline __attribute__((always_inline)) int schedule_client(struct lwan *l, int fd)
791{
792 struct lwan_thread *thread = l->conns[fd].thread;
793
794 lwan_thread_add_client(thread, fd);
795
796 return (int)(thread - l->thread.threads);
797}
798
799static volatile sig_atomic_t main_socket = -1;
800
801static_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; })]
802 "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; })]
;
803
804static void sigint_handler(int signal_number __attribute__((unused)))
805{
806 if (main_socket < 0)
807 return;
808
809 shutdown((int)main_socket, SHUT_RDWRSHUT_RDWR);
810 close((int)main_socket);
811
812 main_socket = -1;
813}
814
815enum herd_accept { HERD_MORE = 0, HERD_GONE = -1, HERD_SHUTDOWN = 1 };
816
817struct core_bitmap {
818 uint64_t bitmap[4];
819};
820
821static ALWAYS_INLINEinline __attribute__((always_inline)) enum herd_accept
822accept_one(struct lwan *l, struct core_bitmap *cores)
823{
824 int fd = accept4((int)main_socket, NULL((void*)0), NULL((void*)0), SOCK_NONBLOCKSOCK_NONBLOCK | SOCK_CLOEXECSOCK_CLOEXEC);
825
826 if (LIKELY(fd >= 0)__builtin_expect((!!(fd >= 0)), (1))) {
827 int core = schedule_client(l, fd);
828
829 cores->bitmap[core / 64] |= UINT64_C(1)1UL<<(core % 64);
830
831 return HERD_MORE;
832 }
833
834 switch (errno(*__errno_location ())) {
835 case EAGAIN11:
836 return HERD_GONE;
837
838 case EBADF9:
839 case ECONNABORTED103:
840 case EINVAL22:
841 if (main_socket < 0) {
842 lwan_status_info("Signal 2 (Interrupt) received")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 842, __FUNCTION__, "Signal 2 (Interrupt) received")
;
843 } else {
844 lwan_status_info("Main socket closed for unknown reasons")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 844, __FUNCTION__, "Main socket closed for unknown reasons"
)
;
845 }
846 return HERD_SHUTDOWN;
847
848 default:
849 lwan_status_perror("accept")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 849, __FUNCTION__, "accept")
;
850 return HERD_MORE;
851 }
852}
853
854void lwan_main_loop(struct lwan *l)
855{
856 struct core_bitmap cores = {};
857
858 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"
, 858, __extension__ __PRETTY_FUNCTION__); }))
;
859 main_socket = l->main_socket;
860
861 if (signal(SIGINT2, sigint_handler) == SIG_ERR((__sighandler_t) -1))
862 lwan_status_critical("Could not set signal handler")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 862, __FUNCTION__, "Could not set signal handler")
;
863
864 lwan_status_info("Ready to serve")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c"
, 864, __FUNCTION__, "Ready to serve")
;
865
866 while (true1) {
867 enum herd_accept ha;
868
869 fcntl(l->main_socket, F_SETFL4, 0);
870 ha = accept_one(l, &cores);
871 if (ha == HERD_MORE) {
872 fcntl(l->main_socket, F_SETFL4, O_NONBLOCK04000);
873
874 do {
875 ha = accept_one(l, &cores);
876 } while (ha == HERD_MORE);
877 }
878
879 if (UNLIKELY(ha > HERD_MORE)__builtin_expect(((ha > HERD_MORE)), (0)))
880 break;
881
882 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++) {
883 for (uint64_t c = cores.bitmap[i]; c; c ^= c & -c) {
884 size_t core = (size_t)__builtin_ctzl(c);
885 lwan_thread_nudge(&l->thread.threads[i * 64 + core]);
886 }
887 }
888 cores = (struct core_bitmap){};
889 }
890}
891
892#ifdef CLOCK_MONOTONIC_COARSE6
893__attribute__((constructor)) static void detect_fastest_monotonic_clock(void)
894{
895 struct timespec ts;
896
897 if (!clock_gettime(CLOCK_MONOTONIC_COARSE6, &ts))
898 monotonic_clock_id = CLOCK_MONOTONIC_COARSE6;
899}
900#endif
901
902void lwan_set_thread_name(const char *name)
903{
904 char thread_name[16];
905 char process_name[PATH_MAX4096];
906 char *tmp;
907 int ret;
908
909 if (proc_pidpath(getpid(), process_name, sizeof(process_name)) < 0)
910 return;
911
912 tmp = strrchr(process_name, '/');
913 if (!tmp)
914 return;
915
916 ret = snprintf(thread_name, sizeof(thread_name), "%s %s", tmp + 1, name);
917 if (ret < 0)
918 return;
919
920 pthread_set_name_np(pthread_self(), thread_name);
921}