| | varnish-cache/bin/varnishd/cache/cache_main.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2006 Verdens Gang AS |
2 |
|
* Copyright (c) 2006-2011 Varnish Software AS |
3 |
|
* All rights reserved. |
4 |
|
* |
5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
6 |
|
* |
7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
8 |
|
* |
9 |
|
* Redistribution and use in source and binary forms, with or without |
10 |
|
* modification, are permitted provided that the following conditions |
11 |
|
* are met: |
12 |
|
* 1. Redistributions of source code must retain the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
|
* notice, this list of conditions and the following disclaimer in the |
16 |
|
* documentation and/or other materials provided with the distribution. |
17 |
|
* |
18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
|
* SUCH DAMAGE. |
29 |
|
*/ |
30 |
|
|
31 |
|
#include "config.h" |
32 |
|
|
33 |
|
#include "cache_varnishd.h" |
34 |
|
#include "acceptor/cache_acceptor.h" |
35 |
|
|
36 |
|
#include <signal.h> |
37 |
|
#include <stdio.h> |
38 |
|
#include <stdlib.h> |
39 |
|
|
40 |
|
#include <sys/mman.h> |
41 |
|
|
42 |
|
#ifdef HAVE_PTHREAD_NP_H |
43 |
|
# include <pthread_np.h> |
44 |
|
#endif |
45 |
|
|
46 |
|
#include "common/heritage.h" |
47 |
|
|
48 |
|
#include "vcli_serve.h" |
49 |
|
#include "vnum.h" |
50 |
|
#include "vtim.h" |
51 |
|
#include "vrnd.h" |
52 |
|
|
53 |
|
#include "hash/hash_slinger.h" |
54 |
|
|
55 |
|
volatile struct params *cache_param; |
56 |
|
static pthread_mutex_t cache_vrnd_mtx; |
57 |
|
static vtim_dur shutdown_delay = 0; |
58 |
|
|
59 |
|
pthread_mutexattr_t mtxattr_errorcheck; |
60 |
|
|
61 |
|
static void |
62 |
267800 |
cache_vrnd_lock(void) |
63 |
|
{ |
64 |
267800 |
PTOK(pthread_mutex_lock(&cache_vrnd_mtx)); |
65 |
267800 |
} |
66 |
|
|
67 |
|
static void |
68 |
267800 |
cache_vrnd_unlock(void) |
69 |
|
{ |
70 |
267800 |
PTOK(pthread_mutex_unlock(&cache_vrnd_mtx)); |
71 |
267800 |
} |
72 |
|
|
73 |
|
/*-------------------------------------------------------------------- |
74 |
|
* Per thread storage for the session currently being processed by |
75 |
|
* the thread. This is used for panic messages. |
76 |
|
*/ |
77 |
|
|
78 |
|
static pthread_key_t req_key; |
79 |
|
static pthread_key_t bo_key; |
80 |
|
static pthread_key_t wrk_key; |
81 |
|
pthread_key_t witness_key; |
82 |
|
pthread_key_t panic_key; |
83 |
|
|
84 |
|
void |
85 |
44488 |
THR_SetBusyobj(const struct busyobj *bo) |
86 |
|
{ |
87 |
|
|
88 |
44488 |
PTOK(pthread_setspecific(bo_key, bo)); |
89 |
44488 |
} |
90 |
|
|
91 |
|
struct busyobj * |
92 |
63 |
THR_GetBusyobj(void) |
93 |
|
{ |
94 |
|
|
95 |
63 |
return (pthread_getspecific(bo_key)); |
96 |
|
} |
97 |
|
|
98 |
|
void |
99 |
28785 |
THR_SetRequest(const struct req *req) |
100 |
|
{ |
101 |
|
|
102 |
28785 |
PTOK(pthread_setspecific(req_key, req)); |
103 |
28785 |
} |
104 |
|
|
105 |
|
struct req * |
106 |
73 |
THR_GetRequest(void) |
107 |
|
{ |
108 |
|
|
109 |
73 |
return (pthread_getspecific(req_key)); |
110 |
|
} |
111 |
|
|
112 |
|
void |
113 |
93470 |
THR_SetWorker(const struct worker *wrk) |
114 |
|
{ |
115 |
|
|
116 |
93470 |
PTOK(pthread_setspecific(wrk_key, wrk)); |
117 |
93470 |
} |
118 |
|
|
119 |
|
struct worker * |
120 |
21743 |
THR_GetWorker(void) |
121 |
|
{ |
122 |
|
|
123 |
21743 |
return (pthread_getspecific(wrk_key)); |
124 |
|
} |
125 |
|
|
126 |
|
/*-------------------------------------------------------------------- |
127 |
|
* Name threads if our pthreads implementation supports it. |
128 |
|
*/ |
129 |
|
|
130 |
|
static pthread_key_t name_key; |
131 |
|
|
132 |
|
#if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) && \ |
133 |
|
!defined(__NetBSD__) |
134 |
|
static void |
135 |
167019 |
thr_setname_generic(const char *name) |
136 |
|
{ |
137 |
|
char buf[16]; |
138 |
|
|
139 |
|
/* The Linux kernel enforces a strict limitation of 15 bytes name, |
140 |
|
* truncate the name if we would overflow it. |
141 |
|
*/ |
142 |
167019 |
if (strlen(name) > 15) { |
143 |
4613 |
bprintf(buf, "%.14s~", name); |
144 |
4613 |
name = buf; |
145 |
4613 |
} |
146 |
|
|
147 |
|
//lint --e{438} Last value assigned not used |
148 |
167019 |
PTOK(pthread_setname_np(pthread_self(), name)); |
149 |
167019 |
} |
150 |
|
#endif |
151 |
|
|
152 |
|
void |
153 |
167019 |
THR_SetName(const char *name) |
154 |
|
{ |
155 |
|
|
156 |
167019 |
PTOK(pthread_setspecific(name_key, name)); |
157 |
|
#if defined(HAVE_PTHREAD_SETNAME_NP) |
158 |
|
# if defined(__APPLE__) |
159 |
|
(void)pthread_setname_np(name); |
160 |
|
# elif defined(__NetBSD__) |
161 |
|
(void)pthread_setname_np(pthread_self(), "%s", (char *)(uintptr_t)name); |
162 |
|
# else |
163 |
167019 |
thr_setname_generic(name); |
164 |
|
# endif |
165 |
|
#endif |
166 |
167019 |
} |
167 |
|
|
168 |
|
const char * |
169 |
63 |
THR_GetName(void) |
170 |
|
{ |
171 |
|
|
172 |
63 |
return (pthread_getspecific(name_key)); |
173 |
|
} |
174 |
|
|
175 |
|
/*-------------------------------------------------------------------- |
176 |
|
* Generic setup all our threads should call |
177 |
|
*/ |
178 |
|
static stack_t altstack; |
179 |
|
|
180 |
|
void |
181 |
166999 |
THR_Init(void) |
182 |
|
{ |
183 |
166999 |
if (altstack.ss_sp != NULL) |
184 |
166809 |
AZ(sigaltstack(&altstack, NULL)); |
185 |
166999 |
} |
186 |
|
|
187 |
|
/*-------------------------------------------------------------------- |
188 |
|
* VXID's are unique transaction numbers allocated with a minimum of |
189 |
|
* locking overhead via pools in the worker threads. |
190 |
|
* |
191 |
|
* VXID's are mostly for use in VSL and for that reason we never return |
192 |
|
* zero vxid, in order to reserve that for "unassociated" VSL records. |
193 |
|
*/ |
194 |
|
|
195 |
|
static uint64_t vxid_base = 1; |
196 |
|
static uint32_t vxid_chunk = 32768; |
197 |
|
static struct lock vxid_lock; |
198 |
|
|
199 |
|
vxid_t |
200 |
40795 |
VXID_Get(const struct worker *wrk, uint64_t mask) |
201 |
|
{ |
202 |
|
struct vxid_pool *v; |
203 |
|
vxid_t retval; |
204 |
|
|
205 |
40795 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
206 |
40795 |
CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC); |
207 |
40795 |
v = wrk->wpriv->vxid_pool; |
208 |
40795 |
AZ(mask & VSL_IDENTMASK); |
209 |
81540 |
while (v->count == 0 || v->next >= VRT_INTEGER_MAX) { |
210 |
40745 |
Lck_Lock(&vxid_lock); |
211 |
40745 |
v->next = vxid_base; |
212 |
40745 |
v->count = vxid_chunk; |
213 |
40745 |
vxid_base += v->count; |
214 |
40745 |
if (vxid_base >= VRT_INTEGER_MAX) |
215 |
10 |
vxid_base = 1; |
216 |
40745 |
Lck_Unlock(&vxid_lock); |
217 |
|
} |
218 |
40795 |
v->count--; |
219 |
40795 |
assert(v->next > 0); |
220 |
40795 |
assert(v->next < VSL_CLIENTMARKER); |
221 |
40795 |
retval.vxid = v->next | mask; |
222 |
40795 |
v->next++; |
223 |
40795 |
return (retval); |
224 |
|
} |
225 |
|
|
226 |
|
/*-------------------------------------------------------------------- |
227 |
|
* Debugging aids |
228 |
|
*/ |
229 |
|
|
230 |
|
/* |
231 |
|
* Dumb down the VXID allocation to make it predictable for |
232 |
|
* varnishtest cases |
233 |
|
*/ |
234 |
|
static void v_matchproto_(cli_func_t) |
235 |
4575 |
cli_debug_xid(struct cli *cli, const char * const *av, void *priv) |
236 |
|
{ |
237 |
4575 |
(void)priv; |
238 |
4575 |
if (av[2] != NULL) { |
239 |
4575 |
vxid_base = strtoull(av[2], NULL, 0); |
240 |
4575 |
vxid_chunk = 0; |
241 |
4575 |
if (av[3] != NULL) |
242 |
0 |
vxid_chunk = strtoul(av[3], NULL, 0); |
243 |
4575 |
if (vxid_chunk == 0) |
244 |
4575 |
vxid_chunk = 1; |
245 |
4575 |
} |
246 |
4575 |
VCLI_Out(cli, "XID is %ju chunk %u", (uintmax_t)vxid_base, vxid_chunk); |
247 |
4575 |
} |
248 |
|
|
249 |
|
/* |
250 |
|
* Artificially slow down the process shutdown. |
251 |
|
*/ |
252 |
|
static void v_matchproto_(cli_func_t) |
253 |
0 |
cli_debug_shutdown_delay(struct cli *cli, const char * const *av, void *priv) |
254 |
|
{ |
255 |
|
|
256 |
0 |
(void)cli; |
257 |
0 |
(void)priv; |
258 |
0 |
shutdown_delay = VNUM_duration(av[2]); |
259 |
0 |
} |
260 |
|
|
261 |
|
/* |
262 |
|
* Default to seed=1, this is the only seed value POSIX guarantees will |
263 |
|
* result in a reproducible random number sequence. |
264 |
|
*/ |
265 |
|
static void v_matchproto_(cli_func_t) |
266 |
15 |
cli_debug_srandom(struct cli *cli, const char * const *av, void *priv) |
267 |
|
{ |
268 |
15 |
unsigned seed = 1; |
269 |
|
|
270 |
15 |
(void)priv; |
271 |
15 |
(void)cli; |
272 |
15 |
if (av[2] != NULL) |
273 |
0 |
seed = strtoul(av[2], NULL, 0); |
274 |
15 |
VRND_SeedTestable(seed); |
275 |
15 |
} |
276 |
|
|
277 |
|
static struct cli_proto debug_cmds[] = { |
278 |
|
{ CLICMD_DEBUG_XID, "d", cli_debug_xid }, |
279 |
|
{ CLICMD_DEBUG_SHUTDOWN_DELAY, "d", cli_debug_shutdown_delay }, |
280 |
|
{ CLICMD_DEBUG_SRANDOM, "d", cli_debug_srandom }, |
281 |
|
{ NULL } |
282 |
|
}; |
283 |
|
|
284 |
|
/*-------------------------------------------------------------------- |
285 |
|
* XXX: Think more about which order we start things |
286 |
|
*/ |
287 |
|
|
288 |
|
#if defined(__FreeBSD__) && __FreeBSD_version >= 1000000 |
289 |
|
static void |
290 |
0 |
child_malloc_fail(void *p, const char *s) |
291 |
|
{ |
292 |
0 |
VSL(SLT_Error, NO_VXID, "MALLOC ERROR: %s (%p)", s, p); |
293 |
0 |
fprintf(stderr, "MALLOC ERROR: %s (%p)\n", s, p); |
294 |
0 |
WRONG("Malloc Error"); |
295 |
0 |
} |
296 |
|
#endif |
297 |
|
|
298 |
|
/*===================================================================== |
299 |
|
* signal handler for child process |
300 |
|
*/ |
301 |
|
|
302 |
|
static void v_noreturn_ v_matchproto_() |
303 |
10 |
child_signal_handler(int s, siginfo_t *si, void *c) |
304 |
|
{ |
305 |
|
char buf[1024]; |
306 |
|
struct sigaction sa; |
307 |
|
struct req *req; |
308 |
10 |
const char *a, *p, *info = NULL; |
309 |
|
|
310 |
10 |
(void)c; |
311 |
|
/* Don't come back */ |
312 |
10 |
memset(&sa, 0, sizeof sa); |
313 |
10 |
sa.sa_handler = SIG_DFL; |
314 |
10 |
(void)sigaction(SIGSEGV, &sa, NULL); |
315 |
10 |
(void)sigaction(SIGBUS, &sa, NULL); |
316 |
10 |
(void)sigaction(SIGABRT, &sa, NULL); |
317 |
|
|
318 |
10 |
while (s == SIGSEGV || s == SIGBUS) { |
319 |
10 |
req = THR_GetRequest(); |
320 |
10 |
if (req == NULL || req->wrk == NULL) |
321 |
0 |
break; |
322 |
10 |
a = TRUST_ME(si->si_addr); |
323 |
10 |
p = TRUST_ME(req->wrk); |
324 |
10 |
p += sizeof *req->wrk; |
325 |
|
// rough safe estimate - top of stack |
326 |
10 |
if (a > p + cache_param->wthread_stacksize) |
327 |
0 |
break; |
328 |
10 |
if (a < p - 2 * cache_param->wthread_stacksize) |
329 |
5 |
break; |
330 |
5 |
info = "\nTHIS PROBABLY IS A STACK OVERFLOW - " |
331 |
|
"check thread_pool_stack parameter"; |
332 |
5 |
break; |
333 |
|
} |
334 |
10 |
bprintf(buf, "Signal %d (%s) received at %p si_code %d%s", |
335 |
|
s, strsignal(s), si->si_addr, si->si_code, |
336 |
|
info ? info : ""); |
337 |
|
|
338 |
10 |
VAS_Fail(__func__, |
339 |
|
__FILE__, |
340 |
|
__LINE__, |
341 |
10 |
buf, |
342 |
|
VAS_WRONG); |
343 |
|
} |
344 |
|
|
345 |
|
/*===================================================================== |
346 |
|
* Magic for panicking properly on signals |
347 |
|
*/ |
348 |
|
|
349 |
|
static void |
350 |
4608 |
child_sigmagic(size_t altstksz) |
351 |
|
{ |
352 |
|
struct sigaction sa; |
353 |
|
|
354 |
4608 |
memset(&sa, 0, sizeof sa); |
355 |
|
|
356 |
4608 |
size_t sz = vmax_t(size_t, SIGSTKSZ + 4096, altstksz); |
357 |
4608 |
altstack.ss_sp = mmap(NULL, sz, PROT_READ | PROT_WRITE, |
358 |
|
MAP_PRIVATE | MAP_ANONYMOUS, |
359 |
|
-1, 0); |
360 |
4608 |
AN(altstack.ss_sp != MAP_FAILED); |
361 |
4608 |
AN(altstack.ss_sp); |
362 |
4608 |
altstack.ss_size = sz; |
363 |
4608 |
altstack.ss_flags = 0; |
364 |
4608 |
sa.sa_flags |= SA_ONSTACK; |
365 |
|
|
366 |
4608 |
THR_Init(); |
367 |
|
|
368 |
4608 |
sa.sa_sigaction = child_signal_handler; |
369 |
4608 |
sa.sa_flags |= SA_SIGINFO; |
370 |
4608 |
(void)sigaction(SIGBUS, &sa, NULL); |
371 |
4608 |
(void)sigaction(SIGABRT, &sa, NULL); |
372 |
4608 |
(void)sigaction(SIGSEGV, &sa, NULL); |
373 |
4608 |
} |
374 |
|
|
375 |
|
static void |
376 |
12 |
cli_quit(int sig) |
377 |
|
{ |
378 |
|
|
379 |
12 |
if (!IS_CLI()) { |
380 |
0 |
PTOK(pthread_kill(cli_thread, sig)); |
381 |
0 |
return; |
382 |
|
} |
383 |
|
|
384 |
12 |
WRONG("It's time for the big quit"); |
385 |
0 |
} |
386 |
|
|
387 |
|
/*===================================================================== |
388 |
|
* Run the child process |
389 |
|
*/ |
390 |
|
|
391 |
|
static void v_matchproto_(cli_func_t) |
392 |
4590 |
ccf_child_start(struct cli *cli, const char * const *av, void *priv) |
393 |
|
{ |
394 |
4590 |
(void)av; |
395 |
4590 |
(void)priv; |
396 |
|
|
397 |
4590 |
VCA_Start(cli); |
398 |
4590 |
} |
399 |
|
|
400 |
|
static struct cli_proto child_cmds[] = { |
401 |
|
{ CLICMD_SERVER_START, "", ccf_child_start }, |
402 |
|
{ NULL } |
403 |
|
}; |
404 |
|
|
405 |
|
void |
406 |
4613 |
child_main(int sigmagic, size_t altstksz) |
407 |
|
{ |
408 |
|
|
409 |
4613 |
if (sigmagic) |
410 |
4608 |
child_sigmagic(altstksz); |
411 |
4613 |
(void)signal(SIGINT, SIG_DFL); |
412 |
4613 |
(void)signal(SIGTERM, SIG_DFL); |
413 |
4613 |
(void)signal(SIGQUIT, cli_quit); |
414 |
|
|
415 |
|
#if defined(__FreeBSD__) && __FreeBSD_version >= 1000000 |
416 |
4613 |
malloc_message = child_malloc_fail; |
417 |
|
#endif |
418 |
|
|
419 |
|
/* Before anything uses pthreads in anger */ |
420 |
4613 |
PTOK(pthread_mutexattr_init(&mtxattr_errorcheck)); |
421 |
4613 |
PTOK(pthread_mutexattr_settype(&mtxattr_errorcheck, PTHREAD_MUTEX_ERRORCHECK)); |
422 |
|
|
423 |
4613 |
cache_param = heritage.param; |
424 |
|
|
425 |
4613 |
PTOK(pthread_key_create(&req_key, NULL)); |
426 |
4613 |
PTOK(pthread_key_create(&bo_key, NULL)); |
427 |
4613 |
PTOK(pthread_key_create(&wrk_key, NULL)); |
428 |
4613 |
PTOK(pthread_key_create(&witness_key, free)); |
429 |
4613 |
PTOK(pthread_key_create(&name_key, NULL)); |
430 |
4613 |
PTOK(pthread_key_create(&panic_key, NULL)); |
431 |
|
|
432 |
4613 |
THR_SetName("cache-main"); |
433 |
|
|
434 |
4613 |
PTOK(pthread_mutex_init(&cache_vrnd_mtx, &mtxattr_errorcheck)); |
435 |
4613 |
VRND_Lock = cache_vrnd_lock; |
436 |
4613 |
VRND_Unlock = cache_vrnd_unlock; |
437 |
|
|
438 |
4613 |
VSM_Init(); /* First, LCK needs it. */ |
439 |
|
|
440 |
4613 |
LCK_Init(); /* Second, locking */ |
441 |
|
|
442 |
4613 |
Lck_New(&vxid_lock, lck_vxid); |
443 |
|
|
444 |
4613 |
CLI_Init(); |
445 |
4613 |
PAN_Init(); |
446 |
4613 |
VFP_Init(); |
447 |
|
|
448 |
4613 |
ObjInit(); |
449 |
|
|
450 |
4613 |
WRK_Init(); |
451 |
|
|
452 |
4613 |
VCL_Init(); |
453 |
4613 |
VCL_VRT_Init(); |
454 |
|
|
455 |
4613 |
HTTP_Init(); |
456 |
|
|
457 |
4613 |
VBO_Init(); |
458 |
4613 |
VCP_Init(); |
459 |
4613 |
VBP_Init(); |
460 |
4613 |
VDI_Init(); |
461 |
4613 |
VBE_InitCfg(); |
462 |
4613 |
Pool_Init(); |
463 |
4613 |
V1P_Init(); |
464 |
4613 |
V2D_Init(); |
465 |
|
|
466 |
4613 |
EXP_Init(); |
467 |
4613 |
HSH_Init(heritage.hash); |
468 |
4613 |
BAN_Init(); |
469 |
|
|
470 |
4613 |
VCA_Init(); |
471 |
|
|
472 |
4613 |
STV_open(); |
473 |
|
|
474 |
4613 |
VMOD_Init(); |
475 |
|
|
476 |
4613 |
BAN_Compile(); |
477 |
|
|
478 |
4613 |
VRND_SeedAll(); |
479 |
|
|
480 |
|
|
481 |
4613 |
CLI_AddFuncs(debug_cmds); |
482 |
4613 |
CLI_AddFuncs(child_cmds); |
483 |
|
|
484 |
|
#if WITH_PERSISTENT_STORAGE |
485 |
|
/* Wait for persistent storage to load if asked to */ |
486 |
4613 |
if (FEATURE(FEATURE_WAIT_SILO)) |
487 |
120 |
SMP_Ready(); |
488 |
|
#endif |
489 |
|
|
490 |
4613 |
CLI_Run(); |
491 |
|
|
492 |
4613 |
if (shutdown_delay > 0) |
493 |
0 |
VTIM_sleep(shutdown_delay); |
494 |
|
|
495 |
4613 |
VCA_Shutdown(); |
496 |
4613 |
BAN_Shutdown(); |
497 |
4613 |
EXP_Shutdown(); |
498 |
4613 |
STV_close(); |
499 |
|
|
500 |
4613 |
printf("Child dies\n"); |
501 |
4613 |
} |