File: | lwan.c |
Warning: | line 324, column 17 Potential memory leak |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * lwan - simple web server | |||
3 | * Copyright (c) 2012, 2013 Leandro A. F. Pereira <leandro@hardinfo.org> | |||
4 | * | |||
5 | * This program is free software; you can redistribute it and/or | |||
6 | * modify it under the terms of the GNU General Public License | |||
7 | * as published by the Free Software Foundation; either version 2 | |||
8 | * of the License, or any later version. | |||
9 | * | |||
10 | * This program is distributed in the hope that it will be useful, | |||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
13 | * GNU General Public License for more details. | |||
14 | * | |||
15 | * You should have received a copy of the GNU General Public License | |||
16 | * along with this program; if not, write to the Free Software | |||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | |||
18 | * USA. | |||
19 | */ | |||
20 | ||||
21 | #define _GNU_SOURCE | |||
22 | #include <assert.h> | |||
23 | #include <ctype.h> | |||
24 | #include <dlfcn.h> | |||
25 | #include <errno(*__errno_location ()).h> | |||
26 | #include <fcntl.h> | |||
27 | #include <libproc.h> | |||
28 | #include <limits.h> | |||
29 | #include <signal.h> | |||
30 | #include <stdlib.h> | |||
31 | #include <string.h> | |||
32 | #include <sys/resource.h> | |||
33 | #include <sys/socket.h> | |||
34 | #include <sys/types.h> | |||
35 | #include <unistd.h> | |||
36 | ||||
37 | #include "lwan-private.h" | |||
38 | ||||
39 | #include "lwan-config.h" | |||
40 | #include "lwan-http-authorize.h" | |||
41 | ||||
42 | #if defined(HAVE_LUA) | |||
43 | #include "lwan-lua.h" | |||
44 | #endif | |||
45 | ||||
46 | /* See detect_fastest_monotonic_clock() */ | |||
47 | clockid_t monotonic_clock_id = CLOCK_MONOTONIC1; | |||
48 | ||||
49 | static const struct lwan_config default_config = { | |||
50 | .listener = "localhost:8080", | |||
51 | .keep_alive_timeout = 15, | |||
52 | .quiet = false0, | |||
53 | .reuse_port = false0, | |||
54 | .proxy_protocol = false0, | |||
55 | .allow_cors = false0, | |||
56 | .expires = 1 * ONE_WEEK(((60 * 60) * 24) * 7), | |||
57 | .n_threads = 0, | |||
58 | .max_post_data_size = 10 * DEFAULT_BUFFER_SIZE4096, | |||
59 | .allow_post_temp_file = false0, | |||
60 | }; | |||
61 | ||||
62 | LWAN_HANDLER(brew_coffee)static enum lwan_http_status lwan_handler_brew_coffee( struct lwan_request *, struct lwan_response *, void *); static const struct lwan_handler_info __attribute__((used, section("lwan_handler" ))) lwan_handler_info_brew_coffee = {.name = "brew_coffee", . handler = lwan_handler_brew_coffee}; static enum lwan_http_status lwan_handler_brew_coffee( struct lwan_request *request __attribute__ ((unused)), struct lwan_response *response __attribute__((unused )), void *data __attribute__((unused))) | |||
63 | { | |||
64 | /* Placeholder handler so that __start_lwan_handler and __stop_lwan_handler | |||
65 | * symbols will get defined. | |||
66 | */ | |||
67 | return HTTP_I_AM_A_TEAPOT; | |||
68 | } | |||
69 | ||||
70 | __attribute__((no_sanitize_address)) | |||
71 | static void *find_handler(const char *name) | |||
72 | { | |||
73 | const struct lwan_handler_info *handler; | |||
74 | ||||
75 | 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 )++) { | |||
76 | if (streq(handler->name, name)) | |||
77 | return handler->handler; | |||
78 | } | |||
79 | ||||
80 | return NULL((void*)0); | |||
81 | } | |||
82 | ||||
83 | __attribute__((no_sanitize_address)) | |||
84 | static const struct lwan_module *find_module(const char *name) | |||
85 | { | |||
86 | const struct lwan_module_info *module; | |||
87 | ||||
88 | 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 )++) { | |||
89 | if (streq(module->name, name)) | |||
90 | return module->module; | |||
91 | } | |||
92 | ||||
93 | return NULL((void*)0); | |||
94 | } | |||
95 | ||||
96 | static void destroy_urlmap(void *data) | |||
97 | { | |||
98 | struct lwan_url_map *url_map = data; | |||
99 | ||||
100 | if (url_map->module) { | |||
101 | const struct lwan_module *module = url_map->module; | |||
102 | ||||
103 | if (module->destroy) | |||
104 | module->destroy(url_map->data); | |||
105 | } else if (url_map->data && url_map->flags & HANDLER_DATA_IS_HASH_TABLE) { | |||
106 | hash_free(url_map->data); | |||
107 | } | |||
108 | ||||
109 | free(url_map->authorization.realm); | |||
110 | free(url_map->authorization.password_file); | |||
111 | free((char *)url_map->prefix); | |||
112 | free(url_map); | |||
113 | } | |||
114 | ||||
115 | static struct lwan_url_map *add_url_map(struct lwan_trie *t, const char *prefix, | |||
116 | const struct lwan_url_map *map) | |||
117 | { | |||
118 | struct lwan_url_map *copy = malloc(sizeof(*copy)); | |||
119 | ||||
120 | if (!copy) | |||
121 | 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" , 121, __FUNCTION__, "Could not copy URL map"); | |||
122 | ||||
123 | memcpy(copy, map, sizeof(*copy)); | |||
124 | ||||
125 | copy->prefix = strdup(prefix ? prefix : copy->prefix); | |||
126 | if (!copy->prefix) | |||
127 | 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" , 127, __FUNCTION__, "Could not copy URL prefix"); | |||
128 | ||||
129 | copy->prefix_len = strlen(copy->prefix); | |||
130 | lwan_trie_add(t, copy->prefix, copy); | |||
131 | ||||
132 | return copy; | |||
133 | } | |||
134 | ||||
135 | static bool_Bool can_override_header(const char *name) | |||
136 | { | |||
137 | /* NOTE: Update lwan_prepare_response_header_full() in lwan-response.c | |||
138 | * if new headers are added here. */ | |||
139 | ||||
140 | if (!strcasecmp(name, "Date")) | |||
141 | return false0; | |||
142 | if (!strcasecmp(name, "Expires")) | |||
143 | return false0; | |||
144 | if (!strcasecmp(name, "WWW-Authenticate")) | |||
145 | return false0; | |||
146 | if (!strcasecmp(name, "Connection")) | |||
147 | return false0; | |||
148 | if (!strcasecmp(name, "Content-Type")) | |||
149 | return false0; | |||
150 | if (!strcasecmp(name, "Transfer-Encoding")) | |||
151 | return false0; | |||
152 | if (!strncasecmp(name, "Access-Control-Allow-", | |||
153 | sizeof("Access-Control-Allow-") - 1)) | |||
154 | return false0; | |||
155 | ||||
156 | return true1; | |||
157 | } | |||
158 | ||||
159 | static void build_response_headers(struct lwan *l, | |||
160 | const struct lwan_key_value *kv) | |||
161 | { | |||
162 | bool_Bool set_server = false0; | |||
163 | ||||
164 | 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" , 164, __extension__ __PRETTY_FUNCTION__); })); | |||
165 | ||||
166 | lwan_strbuf_init(&l->headers); | |||
167 | ||||
168 | for (; kv && kv->key; kv++) { | |||
169 | if (!can_override_header(kv->key)) { | |||
170 | lwan_status_warning("Cannot override header '%s'", kv->key)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 170, __FUNCTION__, "Cannot override header '%s'", kv->key ); | |||
171 | } else { | |||
172 | if (!strcasecmp(kv->key, "Server")) | |||
173 | set_server = true1; | |||
174 | ||||
175 | lwan_strbuf_append_printf(&l->headers, "\r\n%s: %s", kv->key, | |||
176 | kv->value); | |||
177 | } | |||
178 | } | |||
179 | ||||
180 | if (!set_server) | |||
181 | lwan_strbuf_append_strz(&l->headers, "\r\nServer: lwan"); | |||
182 | ||||
183 | lwan_strbuf_append_strz(&l->headers, "\r\n\r\n"); | |||
184 | } | |||
185 | ||||
186 | static void parse_global_headers(struct config *c, | |||
187 | struct lwan *lwan) | |||
188 | { | |||
189 | struct lwan_key_value_array hdrs; | |||
190 | const struct config_line *l; | |||
191 | struct lwan_key_value *kv; | |||
192 | ||||
193 | lwan_key_value_array_init(&hdrs); | |||
194 | ||||
195 | while ((l = config_read_line(c))) { | |||
196 | switch (l->type) { | |||
197 | case CONFIG_LINE_TYPE_SECTION: | |||
198 | config_error( | |||
199 | c, "No sections are supported under the 'headers' section"); | |||
200 | goto cleanup; | |||
201 | ||||
202 | case CONFIG_LINE_TYPE_LINE: | |||
203 | kv = lwan_key_value_array_append(&hdrs); | |||
204 | if (!kv) { | |||
205 | lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 206, __FUNCTION__, "Could not allocate memory for custom response header" ) | |||
206 | "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 206, __FUNCTION__, "Could not allocate memory for custom response header" ); | |||
207 | } | |||
208 | ||||
209 | kv->key = strdup(l->key); | |||
210 | if (!kv->key) { | |||
211 | lwan_status_critical_perror(lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 212, __FUNCTION__, "Could not allocate memory for custom response header" ) | |||
212 | "Could not allocate memory for custom response header")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 212, __FUNCTION__, "Could not allocate memory for custom response header" ); | |||
213 | } | |||
214 | ||||
215 | kv->value = strdup(l->value); | |||
216 | if (!kv->value) { | |||
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 | break; | |||
221 | ||||
222 | case CONFIG_LINE_TYPE_SECTION_END: | |||
223 | kv = lwan_key_value_array_append(&hdrs); | |||
224 | if (!kv) { | |||
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 | ||||
229 | kv->key = NULL((void*)0); | |||
230 | kv->value = NULL((void*)0); | |||
231 | ||||
232 | build_response_headers(lwan, lwan_key_value_array_get_array(&hdrs)); | |||
233 | goto cleanup; | |||
234 | } | |||
235 | } | |||
236 | ||||
237 | config_error(c, "EOF while looking for end of 'headers' section"); | |||
238 | ||||
239 | cleanup: | |||
240 | LWAN_ARRAY_FOREACH (&hdrs, kv)for (kv = (&hdrs)->base.base; kv < ((typeof(kv))(& hdrs)->base.base + (&hdrs)->base.elements); kv++) { | |||
241 | free(kv->key); | |||
242 | free(kv->value); | |||
243 | } | |||
244 | lwan_key_value_array_reset(&hdrs); | |||
245 | } | |||
246 | ||||
247 | static void parse_listener_prefix_authorization(struct config *c, | |||
248 | const struct config_line *l, | |||
249 | struct lwan_url_map *url_map) | |||
250 | { | |||
251 | if (!streq(l->value, "basic")) { | |||
252 | config_error(c, "Only basic authorization supported"); | |||
253 | return; | |||
254 | } | |||
255 | ||||
256 | memset(&url_map->authorization, 0, sizeof(url_map->authorization)); | |||
257 | ||||
258 | while ((l = config_read_line(c))) { | |||
259 | switch (l->type) { | |||
260 | case CONFIG_LINE_TYPE_LINE: | |||
261 | if (streq(l->key, "realm")) { | |||
262 | free(url_map->authorization.realm); | |||
263 | url_map->authorization.realm = strdup(l->value); | |||
264 | } else if (streq(l->key, "password_file")) { | |||
265 | free(url_map->authorization.password_file); | |||
266 | url_map->authorization.password_file = realpath(l->value, NULL((void*)0)); | |||
267 | if (!url_map->authorization.password_file) | |||
268 | config_error(c, "Could not determine full path for password file: %s", l->value); | |||
269 | } | |||
270 | break; | |||
271 | ||||
272 | case CONFIG_LINE_TYPE_SECTION: | |||
273 | config_error(c, "Unexpected section: %s", l->key); | |||
274 | goto error; | |||
275 | ||||
276 | case CONFIG_LINE_TYPE_SECTION_END: | |||
277 | if (!url_map->authorization.realm) | |||
278 | url_map->authorization.realm = strdup("Lwan"); | |||
279 | if (!url_map->authorization.password_file) | |||
280 | url_map->authorization.password_file = strdup("htpasswd"); | |||
281 | ||||
282 | url_map->flags |= HANDLER_MUST_AUTHORIZE; | |||
283 | return; | |||
284 | } | |||
285 | } | |||
286 | ||||
287 | config_error(c, "Could not find end of authorization section"); | |||
288 | ||||
289 | error: | |||
290 | free(url_map->authorization.realm); | |||
291 | free(url_map->authorization.password_file); | |||
292 | } | |||
293 | ||||
294 | __attribute__((no_sanitize_address)) | |||
295 | static const char *get_module_name(const struct lwan_module *module) | |||
296 | { | |||
297 | const struct lwan_module_info *iter; | |||
298 | ||||
299 | 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)++ ) { | |||
300 | if (iter->module == module) | |||
301 | return iter->name; | |||
302 | } | |||
303 | ||||
304 | return "<unknown>"; | |||
305 | } | |||
306 | ||||
307 | static void parse_listener_prefix(struct config *c, | |||
308 | const struct config_line *l, | |||
309 | struct lwan *lwan, | |||
310 | const struct lwan_module *module, | |||
311 | void *handler) | |||
312 | { | |||
313 | struct lwan_url_map url_map = {}; | |||
314 | struct hash *hash = hash_str_new(free, free); | |||
315 | 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); })); | |||
316 | struct config *isolated; | |||
317 | ||||
318 | isolated = config_isolate_section(c, l); | |||
319 | if (!isolated) { | |||
320 | config_error(c, "Could not isolate configuration file"); | |||
321 | goto out; | |||
322 | } | |||
323 | ||||
324 | while ((l = config_read_line(c))) { | |||
| ||||
325 | switch (l->type) { | |||
326 | case CONFIG_LINE_TYPE_LINE: | |||
327 | hash_add(hash, strdup(l->key), strdup(l->value)); | |||
328 | break; | |||
329 | ||||
330 | case CONFIG_LINE_TYPE_SECTION: | |||
331 | if (streq(l->key, "authorization")) { | |||
332 | parse_listener_prefix_authorization(c, l, &url_map); | |||
333 | } else if (!config_skip_section(c, l)) { | |||
334 | config_error(c, "Could not skip section"); | |||
335 | goto out; | |||
336 | } | |||
337 | break; | |||
338 | ||||
339 | case CONFIG_LINE_TYPE_SECTION_END: | |||
340 | goto add_map; | |||
341 | } | |||
342 | } | |||
343 | ||||
344 | config_error(c, "Expecting section end while parsing prefix"); | |||
345 | goto out; | |||
346 | ||||
347 | add_map: | |||
348 | 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" , 348, __extension__ __PRETTY_FUNCTION__); })); | |||
349 | ||||
350 | if (handler) { | |||
351 | url_map.handler = handler; | |||
352 | url_map.flags |= HANDLER_PARSE_MASK | HANDLER_DATA_IS_HASH_TABLE; | |||
353 | url_map.data = hash; | |||
354 | url_map.module = NULL((void*)0); | |||
355 | ||||
356 | hash = NULL((void*)0); | |||
357 | } else if (module->create_from_hash && module->handle_request) { | |||
358 | lwan_status_debug("Initializing module %s from config",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 359, __FUNCTION__, "Initializing module %s from config", get_module_name (module)) | |||
359 | get_module_name(module))lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 359, __FUNCTION__, "Initializing module %s from config", get_module_name (module)); | |||
360 | ||||
361 | url_map.data = module->create_from_hash(prefix, hash); | |||
362 | if (!url_map.data) { | |||
363 | config_error(c, "Could not create module instance"); | |||
364 | goto out; | |||
365 | } | |||
366 | ||||
367 | if (module->parse_conf && !module->parse_conf(url_map.data, isolated)) { | |||
368 | const char *msg = config_last_error(isolated); | |||
369 | ||||
370 | config_error(c, "Error from module: %s", msg ? msg : "Unknown"); | |||
371 | goto out; | |||
372 | } | |||
373 | ||||
374 | url_map.handler = module->handle_request; | |||
375 | url_map.flags |= module->flags; | |||
376 | url_map.module = module; | |||
377 | } else if (UNLIKELY(!module->create_from_hash)__builtin_expect(((!module->create_from_hash)), (0))) { | |||
378 | config_error(c, "Module isn't prepared to load settings from a file; " | |||
379 | "create_from_hash() method isn't present"); | |||
380 | goto out; | |||
381 | } else if (UNLIKELY(!module->handle_request)__builtin_expect(((!module->handle_request)), (0))) { | |||
382 | config_error(c, "Module does not have handle_request() method"); | |||
383 | goto out; | |||
384 | } | |||
385 | ||||
386 | add_url_map(&lwan->url_map_trie, prefix, &url_map); | |||
387 | ||||
388 | out: | |||
389 | hash_free(hash); | |||
390 | config_close(isolated); | |||
391 | } | |||
392 | ||||
393 | void lwan_set_url_map(struct lwan *l, const struct lwan_url_map *map) | |||
394 | { | |||
395 | lwan_trie_destroy(&l->url_map_trie); | |||
396 | if (UNLIKELY(!lwan_trie_init(&l->url_map_trie, destroy_urlmap))__builtin_expect(((!lwan_trie_init(&l->url_map_trie, destroy_urlmap ))), (0))) | |||
397 | lwan_status_critical_perror("Could not initialize trie")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 397, __FUNCTION__, "Could not initialize trie"); | |||
398 | ||||
399 | for (; map->prefix; map++) { | |||
400 | struct lwan_url_map *copy = add_url_map(&l->url_map_trie, NULL((void*)0), map); | |||
401 | ||||
402 | if (copy->module && copy->module->create) { | |||
403 | lwan_status_debug("Initializing module %s from struct",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 404, __FUNCTION__, "Initializing module %s from struct", get_module_name (copy->module)) | |||
404 | get_module_name(copy->module))lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 404, __FUNCTION__, "Initializing module %s from struct", get_module_name (copy->module)); | |||
405 | ||||
406 | copy->data = copy->module->create(map->prefix, copy->args); | |||
407 | if (!copy->data) { | |||
408 | lwan_status_critical("Could not initialize module %s",lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 409, __FUNCTION__, "Could not initialize module %s", get_module_name (copy->module)) | |||
409 | get_module_name(copy->module))lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 409, __FUNCTION__, "Could not initialize module %s", get_module_name (copy->module)); | |||
410 | } | |||
411 | ||||
412 | copy->flags = copy->module->flags; | |||
413 | copy->handler = copy->module->handle_request; | |||
414 | } else { | |||
415 | copy->flags = HANDLER_PARSE_MASK; | |||
416 | } | |||
417 | } | |||
418 | } | |||
419 | ||||
420 | static void parse_listener(struct config *c, | |||
421 | const struct config_line *l, | |||
422 | struct lwan *lwan) | |||
423 | { | |||
424 | free(lwan->config.listener); | |||
425 | lwan->config.listener = strdup(l->value); | |||
426 | ||||
427 | while ((l = config_read_line(c))) { | |||
428 | switch (l->type) { | |||
429 | case CONFIG_LINE_TYPE_LINE: | |||
430 | config_error(c, "Expecting prefix section"); | |||
431 | return; | |||
432 | case CONFIG_LINE_TYPE_SECTION: | |||
433 | if (l->key[0] == '&') { | |||
434 | void *handler = find_handler(l->key + 1); | |||
435 | if (handler) { | |||
436 | parse_listener_prefix(c, l, lwan, NULL((void*)0), handler); | |||
437 | continue; | |||
438 | } | |||
439 | ||||
440 | config_error(c, "Could not find handler name: %s", l->key + 1); | |||
441 | return; | |||
442 | } | |||
443 | ||||
444 | const struct lwan_module *module = find_module(l->key); | |||
445 | if (module) { | |||
446 | parse_listener_prefix(c, l, lwan, module, NULL((void*)0)); | |||
447 | continue; | |||
448 | } | |||
449 | ||||
450 | config_error(c, "Invalid section or module not found: %s", l->key); | |||
451 | return; | |||
452 | case CONFIG_LINE_TYPE_SECTION_END: | |||
453 | return; | |||
454 | } | |||
455 | } | |||
456 | ||||
457 | config_error(c, "Expecting section end while parsing listener"); | |||
458 | } | |||
459 | ||||
460 | const char *lwan_get_config_path(char *path_buf, size_t path_buf_len) | |||
461 | { | |||
462 | char buffer[PATH_MAX4096]; | |||
463 | ||||
464 | if (proc_pidpath(getpid(), buffer, sizeof(buffer)) < 0) | |||
465 | goto out; | |||
466 | ||||
467 | char *path = strrchr(buffer, '/'); | |||
468 | if (!path) | |||
469 | goto out; | |||
470 | int ret = snprintf(path_buf, path_buf_len, "%s.conf", path + 1); | |||
471 | if (ret < 0 || ret >= (int)path_buf_len) | |||
472 | goto out; | |||
473 | ||||
474 | return path_buf; | |||
475 | ||||
476 | out: | |||
477 | return "lwan.conf"; | |||
478 | } | |||
479 | ||||
480 | static bool_Bool setup_from_config(struct lwan *lwan, const char *path) | |||
481 | { | |||
482 | const struct config_line *line; | |||
483 | struct config *conf; | |||
484 | bool_Bool has_listener = false0; | |||
485 | char path_buf[PATH_MAX4096]; | |||
486 | ||||
487 | if (!path
| |||
488 | path = lwan_get_config_path(path_buf, sizeof(path_buf)); | |||
489 | lwan_status_info("Loading configuration file: %s", path)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 489, __FUNCTION__, "Loading configuration file: %s", path); | |||
490 | ||||
491 | conf = config_open(path); | |||
492 | if (!conf) | |||
493 | return false0; | |||
494 | ||||
495 | if (!lwan_trie_init(&lwan->url_map_trie, destroy_urlmap)) | |||
496 | return false0; | |||
497 | ||||
498 | while ((line = config_read_line(conf))) { | |||
499 | switch (line->type) { | |||
500 | case CONFIG_LINE_TYPE_LINE: | |||
501 | if (streq(line->key, "keep_alive_timeout")) { | |||
502 | lwan->config.keep_alive_timeout = (unsigned int)parse_long( | |||
503 | line->value, default_config.keep_alive_timeout); | |||
504 | } else if (streq(line->key, "quiet")) { | |||
505 | lwan->config.quiet = | |||
506 | parse_bool(line->value, default_config.quiet); | |||
507 | } else if (streq(line->key, "reuse_port")) { | |||
508 | lwan->config.reuse_port = | |||
509 | parse_bool(line->value, default_config.reuse_port); | |||
510 | } else if (streq(line->key, "proxy_protocol")) { | |||
511 | lwan->config.proxy_protocol = | |||
512 | parse_bool(line->value, default_config.proxy_protocol); | |||
513 | } else if (streq(line->key, "allow_cors")) { | |||
514 | lwan->config.allow_cors = | |||
515 | parse_bool(line->value, default_config.allow_cors); | |||
516 | } else if (streq(line->key, "expires")) { | |||
517 | lwan->config.expires = | |||
518 | parse_time_period(line->value, default_config.expires); | |||
519 | } else if (streq(line->key, "error_template")) { | |||
520 | free(lwan->config.error_template); | |||
521 | lwan->config.error_template = strdup(line->value); | |||
522 | } else if (streq(line->key, "threads")) { | |||
523 | long n_threads = | |||
524 | parse_long(line->value, default_config.n_threads); | |||
525 | if (n_threads < 0) | |||
526 | config_error(conf, "Invalid number of threads: %ld", | |||
527 | n_threads); | |||
528 | lwan->config.n_threads = (unsigned int)n_threads; | |||
529 | } else if (streq(line->key, "max_post_data_size")) { | |||
530 | long max_post_data_size = parse_long( | |||
531 | line->value, (long)default_config.max_post_data_size); | |||
532 | if (max_post_data_size < 0) | |||
533 | config_error(conf, "Negative maximum post data size"); | |||
534 | else if (max_post_data_size > 128 * (1 << 20)) | |||
535 | config_error(conf, | |||
536 | "Maximum post data can't be over 128MiB"); | |||
537 | lwan->config.max_post_data_size = (size_t)max_post_data_size; | |||
538 | } else if (streq(line->key, "allow_temp_files")) { | |||
539 | lwan->config.allow_post_temp_file = | |||
540 | !!strstr(line->value, "post"); | |||
541 | } else { | |||
542 | config_error(conf, "Unknown config key: %s", line->key); | |||
543 | } | |||
544 | break; | |||
545 | case CONFIG_LINE_TYPE_SECTION: | |||
546 | if (streq(line->key, "listener")) { | |||
547 | if (!has_listener
| |||
548 | parse_listener(conf, line, lwan); | |||
549 | has_listener = true1; | |||
550 | } else { | |||
551 | config_error(conf, "Only one listener supported"); | |||
552 | } | |||
553 | } else if (streq(line->key, "straitjacket")) { | |||
554 | lwan_straitjacket_enforce_from_config(conf); | |||
555 | } else if (streq(line->key, "headers")) { | |||
556 | parse_global_headers(conf, lwan); | |||
557 | } else { | |||
558 | config_error(conf, "Unknown section type: %s", line->key); | |||
559 | } | |||
560 | break; | |||
561 | case CONFIG_LINE_TYPE_SECTION_END: | |||
562 | config_error(conf, "Unexpected section end"); | |||
563 | } | |||
564 | } | |||
565 | ||||
566 | if (config_last_error(conf)) { | |||
567 | 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" , 568, __FUNCTION__, "Error on config file \"%s\", line %d: %s" , path, config_cur_line(conf), config_last_error(conf)) | |||
568 | config_cur_line(conf), config_last_error(conf))lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 568, __FUNCTION__, "Error on config file \"%s\", line %d: %s" , path, config_cur_line(conf), config_last_error(conf)); | |||
569 | } | |||
570 | ||||
571 | config_close(conf); | |||
572 | ||||
573 | return true1; | |||
574 | } | |||
575 | ||||
576 | static void try_setup_from_config(struct lwan *l, | |||
577 | const struct lwan_config *config) | |||
578 | { | |||
579 | if (!setup_from_config(l, config->config_file_path)) { | |||
580 | if (config->config_file_path) { | |||
581 | lwan_status_critical("Could not read config file: %s",lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 582, __FUNCTION__, "Could not read config file: %s", config ->config_file_path) | |||
582 | config->config_file_path)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 582, __FUNCTION__, "Could not read config file: %s", config ->config_file_path); | |||
583 | } | |||
584 | } | |||
585 | ||||
586 | lwan_status_init(l); /* `quiet` key might have changed value. */ | |||
587 | ||||
588 | l->config.request_flags = | |||
589 | (l->config.proxy_protocol ? REQUEST_ALLOW_PROXY_REQS : 0) | | |||
590 | (l->config.allow_cors ? REQUEST_ALLOW_CORS : 0); | |||
591 | } | |||
592 | ||||
593 | static rlim_t setup_open_file_count_limits(void) | |||
594 | { | |||
595 | struct rlimit r; | |||
596 | ||||
597 | if (getrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) { | |||
598 | 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" , 600, __FUNCTION__, "Could not obtain maximum number of file " "descriptors. Assuming %d", 256) | |||
599 | "descriptors. Assuming %d",lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 600, __FUNCTION__, "Could not obtain maximum number of file " "descriptors. Assuming %d", 256) | |||
600 | OPEN_MAX)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 600, __FUNCTION__, "Could not obtain maximum number of file " "descriptors. Assuming %d", 256); | |||
601 | return OPEN_MAX256; | |||
602 | } | |||
603 | ||||
604 | if (r.rlim_max != r.rlim_cur) { | |||
605 | const rlim_t current = r.rlim_cur; | |||
606 | ||||
607 | if (r.rlim_max == RLIM_INFINITY0xffffffffffffffffuLL && r.rlim_cur < OPEN_MAX256) { | |||
608 | r.rlim_cur = OPEN_MAX256; | |||
609 | } else if (r.rlim_cur < r.rlim_max) { | |||
610 | r.rlim_cur = r.rlim_max; | |||
611 | } else { | |||
612 | /* Shouldn't happen, so just return the current value. */ | |||
613 | goto out; | |||
614 | } | |||
615 | ||||
616 | if (setrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &r) < 0) { | |||
617 | 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" , 619, __FUNCTION__, "Could not raise maximum number of file " "descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max , current) | |||
618 | "descriptors to %" PRIu64 ". Leaving at "lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 619, __FUNCTION__, "Could not raise maximum number of file " "descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max , current) | |||
619 | "%" PRIu64, r.rlim_max, current)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 619, __FUNCTION__, "Could not raise maximum number of file " "descriptors to %" "l" "u" ". Leaving at " "%" "l" "u", r.rlim_max , current); | |||
620 | r.rlim_cur = current; | |||
621 | } | |||
622 | } | |||
623 | ||||
624 | out: | |||
625 | return r.rlim_cur; | |||
626 | } | |||
627 | ||||
628 | static void allocate_connections(struct lwan *l, size_t max_open_files) | |||
629 | { | |||
630 | const size_t sz = max_open_files * sizeof(struct lwan_connection); | |||
631 | ||||
632 | l->conns = lwan_aligned_alloc(sz, 64); | |||
633 | if (UNLIKELY(!l->conns)__builtin_expect(((!l->conns)), (0))) | |||
634 | lwan_status_critical_perror("lwan_alloc_aligned")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 634, __FUNCTION__, "lwan_alloc_aligned"); | |||
635 | ||||
636 | memset(l->conns, 0, sz); | |||
637 | } | |||
638 | ||||
639 | static unsigned int get_number_of_cpus(void) | |||
640 | { | |||
641 | long n_online_cpus = sysconf(_SC_NPROCESSORS_ONLN_SC_NPROCESSORS_ONLN); | |||
642 | ||||
643 | if (UNLIKELY(n_online_cpus < 0)__builtin_expect(((n_online_cpus < 0)), (0))) { | |||
644 | lwan_status_warning(lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 645, __FUNCTION__, "Could not get number of online CPUs, assuming 1 CPU" ) | |||
645 | "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" , 645, __FUNCTION__, "Could not get number of online CPUs, assuming 1 CPU" ); | |||
646 | return 1; | |||
647 | } | |||
648 | ||||
649 | return (unsigned int)n_online_cpus; | |||
650 | } | |||
651 | ||||
652 | void lwan_init(struct lwan *l) { lwan_init_with_config(l, &default_config); } | |||
| ||||
653 | ||||
654 | const struct lwan_config *lwan_get_default_config(void) | |||
655 | { | |||
656 | return &default_config; | |||
657 | } | |||
658 | ||||
659 | static char *dup_or_null(const char *s) | |||
660 | { | |||
661 | return s ? strdup(s) : NULL((void*)0); | |||
662 | } | |||
663 | ||||
664 | void lwan_init_with_config(struct lwan *l, const struct lwan_config *config) | |||
665 | { | |||
666 | /* Load defaults */ | |||
667 | memset(l, 0, sizeof(*l)); | |||
668 | memcpy(&l->config, config, sizeof(*config)); | |||
669 | l->config.listener = dup_or_null(l->config.listener); | |||
670 | l->config.config_file_path = dup_or_null(l->config.config_file_path); | |||
671 | ||||
672 | /* Initialize status first, as it is used by other things during | |||
673 | * their initialization. */ | |||
674 | lwan_status_init(l); | |||
675 | ||||
676 | /* These will only print debugging messages. Debug messages are always | |||
677 | * printed if we're on a debug build, so the quiet setting will be | |||
678 | * respected. */ | |||
679 | lwan_job_thread_init(); | |||
680 | lwan_tables_init(); | |||
681 | ||||
682 | try_setup_from_config(l, config); | |||
683 | ||||
684 | if (!lwan_strbuf_get_length(&l->headers)) | |||
685 | build_response_headers(l, config->global_headers); | |||
686 | ||||
687 | lwan_response_init(l); | |||
688 | ||||
689 | /* Continue initialization as normal. */ | |||
690 | lwan_status_debug("Initializing lwan web server")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 690, __FUNCTION__, "Initializing lwan web server"); | |||
691 | ||||
692 | l->n_cpus = get_number_of_cpus(); | |||
693 | if (!l->config.n_threads) { | |||
694 | l->thread.count = l->n_cpus; | |||
695 | if (l->thread.count == 1) | |||
696 | l->thread.count = 2; | |||
697 | } else if (l->config.n_threads > 3 * l->n_cpus) { | |||
698 | l->thread.count = l->n_cpus * 3; | |||
699 | ||||
700 | 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" , 702, __FUNCTION__, "%d threads requested, but only %d online CPUs; " "capping to %d threads", l->config.n_threads, l->n_cpus , 3 * l->n_cpus) | |||
701 | "capping to %d threads",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 702, __FUNCTION__, "%d threads requested, but only %d online CPUs; " "capping to %d threads", l->config.n_threads, l->n_cpus , 3 * l->n_cpus) | |||
702 | l->config.n_threads, l->n_cpus, 3 * l->n_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 702, __FUNCTION__, "%d threads requested, but only %d online CPUs; " "capping to %d threads", l->config.n_threads, l->n_cpus , 3 * l->n_cpus); | |||
703 | } else if (l->config.n_threads > 255) { | |||
704 | l->thread.count = 256; | |||
705 | ||||
706 | 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" , 707, __FUNCTION__, "%d threads requested, but max 256 supported" , l->config.n_threads) | |||
707 | l->config.n_threads)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 707, __FUNCTION__, "%d threads requested, but max 256 supported" , l->config.n_threads); | |||
708 | } else { | |||
709 | l->thread.count = l->config.n_threads; | |||
710 | } | |||
711 | ||||
712 | rlim_t max_open_files = setup_open_file_count_limits(); | |||
713 | allocate_connections(l, (size_t)max_open_files); | |||
714 | ||||
715 | l->thread.max_fd = (unsigned)max_open_files / (unsigned)l->thread.count; | |||
716 | 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" , 717, __FUNCTION__, "Using %d threads, maximum %d sockets per thread" , l->thread.count, l->thread.max_fd) | |||
717 | l->thread.count, l->thread.max_fd)lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 717, __FUNCTION__, "Using %d threads, maximum %d sockets per thread" , l->thread.count, l->thread.max_fd); | |||
718 | ||||
719 | signal(SIGPIPE13, SIG_IGN((__sighandler_t) 1)); | |||
720 | ||||
721 | lwan_readahead_init(); | |||
722 | lwan_thread_init(l); | |||
723 | lwan_socket_init(l); | |||
724 | lwan_http_authorize_init(); | |||
725 | } | |||
726 | ||||
727 | void lwan_shutdown(struct lwan *l) | |||
728 | { | |||
729 | lwan_status_info("Shutting down")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 729, __FUNCTION__, "Shutting down"); | |||
730 | ||||
731 | free(l->config.listener); | |||
732 | free(l->config.error_template); | |||
733 | free(l->config.config_file_path); | |||
734 | ||||
735 | lwan_job_thread_shutdown(); | |||
736 | lwan_thread_shutdown(l); | |||
737 | ||||
738 | lwan_status_debug("Shutting down URL handlers")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 738, __FUNCTION__, "Shutting down URL handlers"); | |||
739 | lwan_trie_destroy(&l->url_map_trie); | |||
740 | ||||
741 | lwan_strbuf_free(&l->headers); | |||
742 | free(l->conns); | |||
743 | ||||
744 | lwan_response_shutdown(l); | |||
745 | lwan_tables_shutdown(); | |||
746 | lwan_status_shutdown(l); | |||
747 | lwan_http_authorize_shutdown(); | |||
748 | lwan_readahead_shutdown(); | |||
749 | } | |||
750 | ||||
751 | static ALWAYS_INLINEinline __attribute__((always_inline)) int schedule_client(struct lwan *l, int fd) | |||
752 | { | |||
753 | struct lwan_thread *thread = l->conns[fd].thread; | |||
754 | ||||
755 | lwan_thread_add_client(thread, fd); | |||
756 | ||||
757 | return (int)(thread - l->thread.threads); | |||
758 | } | |||
759 | ||||
760 | static volatile sig_atomic_t main_socket = -1; | |||
761 | ||||
762 | static_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; })] | |||
763 | "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; })]; | |||
764 | ||||
765 | static void sigint_handler(int signal_number __attribute__((unused))) | |||
766 | { | |||
767 | if (main_socket < 0) | |||
768 | return; | |||
769 | ||||
770 | shutdown((int)main_socket, SHUT_RDWRSHUT_RDWR); | |||
771 | close((int)main_socket); | |||
772 | ||||
773 | main_socket = -1; | |||
774 | } | |||
775 | ||||
776 | enum herd_accept { HERD_MORE = 0, HERD_GONE = -1, HERD_SHUTDOWN = 1 }; | |||
777 | ||||
778 | struct core_bitmap { | |||
779 | uint64_t bitmap[4]; | |||
780 | }; | |||
781 | ||||
782 | static ALWAYS_INLINEinline __attribute__((always_inline)) enum herd_accept | |||
783 | accept_one(struct lwan *l, struct core_bitmap *cores) | |||
784 | { | |||
785 | int fd = accept4((int)main_socket, NULL((void*)0), NULL((void*)0), SOCK_NONBLOCKSOCK_NONBLOCK | SOCK_CLOEXECSOCK_CLOEXEC); | |||
786 | ||||
787 | if (LIKELY(fd >= 0)__builtin_expect((!!(fd >= 0)), (1))) { | |||
788 | int core = schedule_client(l, fd); | |||
789 | ||||
790 | cores->bitmap[core / 64] |= UINT64_C(1)1UL<<(core % 64); | |||
791 | ||||
792 | return HERD_MORE; | |||
793 | } | |||
794 | ||||
795 | switch (errno(*__errno_location ())) { | |||
796 | case EAGAIN11: | |||
797 | return HERD_GONE; | |||
798 | ||||
799 | case EBADF9: | |||
800 | case ECONNABORTED103: | |||
801 | case EINVAL22: | |||
802 | if (main_socket < 0) { | |||
803 | lwan_status_info("Signal 2 (Interrupt) received")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 803, __FUNCTION__, "Signal 2 (Interrupt) received"); | |||
804 | } else { | |||
805 | lwan_status_info("Main socket closed for unknown reasons")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 805, __FUNCTION__, "Main socket closed for unknown reasons" ); | |||
806 | } | |||
807 | return HERD_SHUTDOWN; | |||
808 | ||||
809 | default: | |||
810 | lwan_status_perror("accept")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 810, __FUNCTION__, "accept"); | |||
811 | return HERD_MORE; | |||
812 | } | |||
813 | } | |||
814 | ||||
815 | void lwan_main_loop(struct lwan *l) | |||
816 | { | |||
817 | struct core_bitmap cores = {}; | |||
818 | ||||
819 | 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" , 819, __extension__ __PRETTY_FUNCTION__); })); | |||
820 | main_socket = l->main_socket; | |||
821 | ||||
822 | if (signal(SIGINT2, sigint_handler) == SIG_ERR((__sighandler_t) -1)) | |||
823 | lwan_status_critical("Could not set signal handler")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 823, __FUNCTION__, "Could not set signal handler"); | |||
824 | ||||
825 | lwan_status_info("Ready to serve")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan.c" , 825, __FUNCTION__, "Ready to serve"); | |||
826 | ||||
827 | while (true1) { | |||
828 | enum herd_accept ha; | |||
829 | ||||
830 | fcntl(l->main_socket, F_SETFL4, 0); | |||
831 | ha = accept_one(l, &cores); | |||
832 | if (ha == HERD_MORE) { | |||
833 | fcntl(l->main_socket, F_SETFL4, O_NONBLOCK04000); | |||
834 | ||||
835 | do { | |||
836 | ha = accept_one(l, &cores); | |||
837 | } while (ha == HERD_MORE); | |||
838 | } | |||
839 | ||||
840 | if (UNLIKELY(ha > HERD_MORE)__builtin_expect(((ha > HERD_MORE)), (0))) | |||
841 | break; | |||
842 | ||||
843 | 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++) { | |||
844 | for (uint64_t c = cores.bitmap[i]; c; c ^= c & -c) { | |||
845 | size_t core = (size_t)__builtin_ctzl(c); | |||
846 | lwan_thread_nudge(&l->thread.threads[i * 64 + core]); | |||
847 | } | |||
848 | } | |||
849 | cores = (struct core_bitmap){}; | |||
850 | } | |||
851 | } | |||
852 | ||||
853 | #ifdef CLOCK_MONOTONIC_COARSE6 | |||
854 | __attribute__((constructor)) static void detect_fastest_monotonic_clock(void) | |||
855 | { | |||
856 | struct timespec ts; | |||
857 | ||||
858 | if (!clock_gettime(CLOCK_MONOTONIC_COARSE6, &ts)) | |||
859 | monotonic_clock_id = CLOCK_MONOTONIC_COARSE6; | |||
860 | } | |||
861 | #endif | |||
862 | ||||
863 | void lwan_set_thread_name(const char *name) | |||
864 | { | |||
865 | char thread_name[16]; | |||
866 | char process_name[PATH_MAX4096]; | |||
867 | char *tmp; | |||
868 | int ret; | |||
869 | ||||
870 | if (proc_pidpath(getpid(), process_name, sizeof(process_name)) < 0) | |||
871 | return; | |||
872 | ||||
873 | tmp = strrchr(process_name, '/'); | |||
874 | if (!tmp) | |||
875 | return; | |||
876 | ||||
877 | ret = snprintf(thread_name, sizeof(thread_name), "%s %s", tmp + 1, name); | |||
878 | if (ret < 0) | |||
879 | return; | |||
880 | ||||
881 | pthread_set_name_np(pthread_self(), thread_name); | |||
882 | } |