| | 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 |
2143744 |
cache_vrnd_lock(void) |
63 |
|
{ |
64 |
2143744 |
PTOK(pthread_mutex_lock(&cache_vrnd_mtx)); |
65 |
2143744 |
} |
66 |
|
|
67 |
|
static void |
68 |
2143744 |
cache_vrnd_unlock(void) |
69 |
|
{ |
70 |
2143744 |
PTOK(pthread_mutex_unlock(&cache_vrnd_mtx)); |
71 |
2143744 |
} |
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 |
354022 |
THR_SetBusyobj(const struct busyobj *bo) |
86 |
|
{ |
87 |
|
|
88 |
354022 |
PTOK(pthread_setspecific(bo_key, bo)); |
89 |
354022 |
} |
90 |
|
|
91 |
|
struct busyobj * |
92 |
510 |
THR_GetBusyobj(void) |
93 |
|
{ |
94 |
|
|
95 |
510 |
return (pthread_getspecific(bo_key)); |
96 |
|
} |
97 |
|
|
98 |
|
void |
99 |
225067 |
THR_SetRequest(const struct req *req) |
100 |
|
{ |
101 |
|
|
102 |
225067 |
PTOK(pthread_setspecific(req_key, req)); |
103 |
225067 |
} |
104 |
|
|
105 |
|
struct req * |
106 |
590 |
THR_GetRequest(void) |
107 |
|
{ |
108 |
|
|
109 |
590 |
return (pthread_getspecific(req_key)); |
110 |
|
} |
111 |
|
|
112 |
|
void |
113 |
743908 |
THR_SetWorker(const struct worker *wrk) |
114 |
|
{ |
115 |
|
|
116 |
743908 |
PTOK(pthread_setspecific(wrk_key, wrk)); |
117 |
743908 |
} |
118 |
|
|
119 |
|
struct worker * |
120 |
172582 |
THR_GetWorker(void) |
121 |
|
{ |
122 |
|
|
123 |
172582 |
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 |
|
static void |
133 |
1327924 |
thr_setname_generic(const char *name) |
134 |
|
{ |
135 |
|
char buf[16]; |
136 |
|
|
137 |
|
/* The Linux kernel enforces a strict limitation of 15 bytes name, |
138 |
|
* truncate the name if we would overflow it. |
139 |
|
*/ |
140 |
1327924 |
if (strlen(name) > 15) { |
141 |
36629 |
bprintf(buf, "%.14s~", name); |
142 |
36629 |
name = buf; |
143 |
36629 |
} |
144 |
|
|
145 |
|
//lint --e{438} Last value assigned not used |
146 |
1327924 |
PTOK(pthread_setname_np(pthread_self(), name)); |
147 |
1327924 |
} |
148 |
|
|
149 |
|
void |
150 |
1327922 |
THR_SetName(const char *name) |
151 |
|
{ |
152 |
|
|
153 |
1327922 |
PTOK(pthread_setspecific(name_key, name)); |
154 |
|
#if defined(__APPLE__) |
155 |
|
(void)pthread_setname_np(name); |
156 |
|
#elif defined(__NetBSD__) |
157 |
|
(void)pthread_setname_np(pthread_self(), "%s", (char *)(uintptr_t)name); |
158 |
|
#else |
159 |
1327922 |
thr_setname_generic(name); |
160 |
|
#endif |
161 |
1327922 |
} |
162 |
|
|
163 |
|
const char * |
164 |
510 |
THR_GetName(void) |
165 |
|
{ |
166 |
|
|
167 |
510 |
return (pthread_getspecific(name_key)); |
168 |
|
} |
169 |
|
|
170 |
|
/*-------------------------------------------------------------------- |
171 |
|
* Generic setup all our threads should call |
172 |
|
*/ |
173 |
|
static stack_t altstack; |
174 |
|
|
175 |
|
void |
176 |
1327589 |
THR_Init(void) |
177 |
|
{ |
178 |
1327589 |
if (altstack.ss_sp != NULL) |
179 |
1326443 |
AZ(sigaltstack(&altstack, NULL)); |
180 |
1327589 |
} |
181 |
|
|
182 |
|
/*-------------------------------------------------------------------- |
183 |
|
* VXID's are unique transaction numbers allocated with a minimum of |
184 |
|
* locking overhead via pools in the worker threads. |
185 |
|
* |
186 |
|
* VXID's are mostly for use in VSL and for that reason we never return |
187 |
|
* zero vxid, in order to reserve that for "unassociated" VSL records. |
188 |
|
*/ |
189 |
|
|
190 |
|
static uint64_t vxid_base = 1; |
191 |
|
static uint32_t vxid_chunk = 32768; |
192 |
|
static struct lock vxid_lock; |
193 |
|
|
194 |
|
vxid_t |
195 |
323401 |
VXID_Get(const struct worker *wrk, uint64_t mask) |
196 |
|
{ |
197 |
|
struct vxid_pool *v; |
198 |
|
vxid_t retval; |
199 |
|
|
200 |
323401 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
201 |
323401 |
CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC); |
202 |
323401 |
v = wrk->wpriv->vxid_pool; |
203 |
323401 |
AZ(mask & VSL_IDENTMASK); |
204 |
646403 |
while (v->count == 0 || v->next >= VRT_INTEGER_MAX) { |
205 |
323002 |
Lck_Lock(&vxid_lock); |
206 |
323002 |
v->next = vxid_base; |
207 |
323002 |
v->count = vxid_chunk; |
208 |
323002 |
vxid_base += v->count; |
209 |
323002 |
if (vxid_base >= VRT_INTEGER_MAX) |
210 |
80 |
vxid_base = 1; |
211 |
323002 |
Lck_Unlock(&vxid_lock); |
212 |
|
} |
213 |
323401 |
v->count--; |
214 |
323401 |
assert(v->next > 0); |
215 |
323401 |
assert(v->next < VSL_CLIENTMARKER); |
216 |
323401 |
retval.vxid = v->next | mask; |
217 |
323401 |
v->next++; |
218 |
323401 |
return (retval); |
219 |
|
} |
220 |
|
|
221 |
|
/*-------------------------------------------------------------------- |
222 |
|
* Debugging aids |
223 |
|
*/ |
224 |
|
|
225 |
|
/* |
226 |
|
* Dumb down the VXID allocation to make it predictable for |
227 |
|
* varnishtest cases |
228 |
|
*/ |
229 |
|
static void v_matchproto_(cli_func_t) |
230 |
36320 |
cli_debug_xid(struct cli *cli, const char * const *av, void *priv) |
231 |
|
{ |
232 |
36320 |
(void)priv; |
233 |
36320 |
if (av[2] != NULL) { |
234 |
36320 |
vxid_base = strtoull(av[2], NULL, 0); |
235 |
36320 |
vxid_chunk = 0; |
236 |
36320 |
if (av[3] != NULL) |
237 |
0 |
vxid_chunk = strtoul(av[3], NULL, 0); |
238 |
36320 |
if (vxid_chunk == 0) |
239 |
36320 |
vxid_chunk = 1; |
240 |
36320 |
} |
241 |
36320 |
VCLI_Out(cli, "XID is %ju chunk %u", (uintmax_t)vxid_base, vxid_chunk); |
242 |
36320 |
} |
243 |
|
|
244 |
|
/* |
245 |
|
* Artificially slow down the process shutdown. |
246 |
|
*/ |
247 |
|
static void v_matchproto_(cli_func_t) |
248 |
0 |
cli_debug_shutdown_delay(struct cli *cli, const char * const *av, void *priv) |
249 |
|
{ |
250 |
|
|
251 |
0 |
(void)cli; |
252 |
0 |
(void)priv; |
253 |
0 |
shutdown_delay = VNUM_duration(av[2]); |
254 |
0 |
} |
255 |
|
|
256 |
|
/* |
257 |
|
* Default to seed=1, this is the only seed value POSIX guarantees will |
258 |
|
* result in a reproducible random number sequence. |
259 |
|
*/ |
260 |
|
static void v_matchproto_(cli_func_t) |
261 |
120 |
cli_debug_srandom(struct cli *cli, const char * const *av, void *priv) |
262 |
|
{ |
263 |
120 |
unsigned seed = 1; |
264 |
|
|
265 |
120 |
(void)priv; |
266 |
120 |
(void)cli; |
267 |
120 |
if (av[2] != NULL) |
268 |
0 |
seed = strtoul(av[2], NULL, 0); |
269 |
120 |
VRND_SeedTestable(seed); |
270 |
120 |
} |
271 |
|
|
272 |
|
static struct cli_proto debug_cmds[] = { |
273 |
|
{ CLICMD_DEBUG_XID, "d", cli_debug_xid }, |
274 |
|
{ CLICMD_DEBUG_SHUTDOWN_DELAY, "d", cli_debug_shutdown_delay }, |
275 |
|
{ CLICMD_DEBUG_SRANDOM, "d", cli_debug_srandom }, |
276 |
|
{ NULL } |
277 |
|
}; |
278 |
|
|
279 |
|
/*-------------------------------------------------------------------- |
280 |
|
* XXX: Think more about which order we start things |
281 |
|
*/ |
282 |
|
|
283 |
|
#if defined(__FreeBSD__) && __FreeBSD_version >= 1000000 |
284 |
|
static void |
285 |
0 |
child_malloc_fail(void *p, const char *s) |
286 |
|
{ |
287 |
0 |
VSL(SLT_Error, NO_VXID, "MALLOC ERROR: %s (%p)", s, p); |
288 |
0 |
fprintf(stderr, "MALLOC ERROR: %s (%p)\n", s, p); |
289 |
0 |
WRONG("Malloc Error"); |
290 |
0 |
} |
291 |
|
#endif |
292 |
|
|
293 |
|
/*===================================================================== |
294 |
|
* signal handler for child process |
295 |
|
*/ |
296 |
|
|
297 |
|
static void v_noreturn_ v_matchproto_() |
298 |
80 |
child_signal_handler(int s, siginfo_t *si, void *c) |
299 |
|
{ |
300 |
|
char buf[1024]; |
301 |
|
struct sigaction sa; |
302 |
|
struct req *req; |
303 |
80 |
const char *a, *p, *info = NULL; |
304 |
|
|
305 |
80 |
(void)c; |
306 |
|
/* Don't come back */ |
307 |
80 |
memset(&sa, 0, sizeof sa); |
308 |
80 |
sa.sa_handler = SIG_DFL; |
309 |
80 |
(void)sigaction(SIGSEGV, &sa, NULL); |
310 |
80 |
(void)sigaction(SIGBUS, &sa, NULL); |
311 |
80 |
(void)sigaction(SIGABRT, &sa, NULL); |
312 |
|
|
313 |
80 |
while (s == SIGSEGV || s == SIGBUS) { |
314 |
80 |
req = THR_GetRequest(); |
315 |
80 |
if (req == NULL || req->wrk == NULL) |
316 |
0 |
break; |
317 |
80 |
a = TRUST_ME(si->si_addr); |
318 |
80 |
p = TRUST_ME(req->wrk); |
319 |
80 |
p += sizeof *req->wrk; |
320 |
|
// rough safe estimate - top of stack |
321 |
80 |
if (a > p + cache_param->wthread_stacksize) |
322 |
0 |
break; |
323 |
80 |
if (a < p - 2 * cache_param->wthread_stacksize) |
324 |
40 |
break; |
325 |
40 |
info = "\nTHIS PROBABLY IS A STACK OVERFLOW - " |
326 |
|
"check thread_pool_stack parameter"; |
327 |
40 |
break; |
328 |
|
} |
329 |
80 |
bprintf(buf, "Signal %d (%s) received at %p si_code %d%s", |
330 |
|
s, strsignal(s), si->si_addr, si->si_code, |
331 |
|
info ? info : ""); |
332 |
|
|
333 |
80 |
VAS_Fail(__func__, |
334 |
|
__FILE__, |
335 |
|
__LINE__, |
336 |
80 |
buf, |
337 |
|
VAS_WRONG); |
338 |
|
} |
339 |
|
|
340 |
|
/*===================================================================== |
341 |
|
* Magic for panicking properly on signals |
342 |
|
*/ |
343 |
|
|
344 |
|
static void |
345 |
36590 |
child_sigmagic(size_t altstksz) |
346 |
|
{ |
347 |
|
struct sigaction sa; |
348 |
|
|
349 |
36590 |
memset(&sa, 0, sizeof sa); |
350 |
|
|
351 |
36590 |
size_t sz = vmax_t(size_t, SIGSTKSZ + 4096, altstksz); |
352 |
36590 |
altstack.ss_sp = mmap(NULL, sz, PROT_READ | PROT_WRITE, |
353 |
|
MAP_PRIVATE | MAP_ANONYMOUS, |
354 |
|
-1, 0); |
355 |
36590 |
AN(altstack.ss_sp != MAP_FAILED); |
356 |
36590 |
AN(altstack.ss_sp); |
357 |
36590 |
altstack.ss_size = sz; |
358 |
36590 |
altstack.ss_flags = 0; |
359 |
36590 |
sa.sa_flags |= SA_ONSTACK; |
360 |
|
|
361 |
36590 |
THR_Init(); |
362 |
|
|
363 |
36590 |
sa.sa_sigaction = child_signal_handler; |
364 |
36590 |
sa.sa_flags |= SA_SIGINFO; |
365 |
36590 |
(void)sigaction(SIGBUS, &sa, NULL); |
366 |
36590 |
(void)sigaction(SIGABRT, &sa, NULL); |
367 |
36590 |
(void)sigaction(SIGSEGV, &sa, NULL); |
368 |
36590 |
} |
369 |
|
|
370 |
|
static void |
371 |
90 |
cli_quit(int sig) |
372 |
|
{ |
373 |
|
|
374 |
90 |
if (!IS_CLI()) { |
375 |
0 |
PTOK(pthread_kill(cli_thread, sig)); |
376 |
0 |
return; |
377 |
|
} |
378 |
|
|
379 |
90 |
WRONG("It's time for the big quit"); |
380 |
0 |
} |
381 |
|
|
382 |
|
/*===================================================================== |
383 |
|
* Run the child process |
384 |
|
*/ |
385 |
|
|
386 |
|
static void v_matchproto_(cli_func_t) |
387 |
36440 |
ccf_child_start(struct cli *cli, const char * const *av, void *priv) |
388 |
|
{ |
389 |
36440 |
(void)av; |
390 |
36440 |
(void)priv; |
391 |
|
|
392 |
36440 |
VCA_Start(cli); |
393 |
36440 |
} |
394 |
|
|
395 |
|
static struct cli_proto child_cmds[] = { |
396 |
|
{ CLICMD_SERVER_START, "", ccf_child_start }, |
397 |
|
{ NULL } |
398 |
|
}; |
399 |
|
|
400 |
|
void |
401 |
36630 |
child_main(int sigmagic, size_t altstksz) |
402 |
|
{ |
403 |
|
|
404 |
36630 |
if (sigmagic) |
405 |
36590 |
child_sigmagic(altstksz); |
406 |
36630 |
(void)signal(SIGINT, SIG_DFL); |
407 |
36630 |
(void)signal(SIGTERM, SIG_DFL); |
408 |
36630 |
(void)signal(SIGQUIT, cli_quit); |
409 |
|
|
410 |
|
#if defined(__FreeBSD__) && __FreeBSD_version >= 1000000 |
411 |
36630 |
malloc_message = child_malloc_fail; |
412 |
|
#endif |
413 |
|
|
414 |
|
/* Before anything uses pthreads in anger */ |
415 |
36630 |
PTOK(pthread_mutexattr_init(&mtxattr_errorcheck)); |
416 |
36630 |
PTOK(pthread_mutexattr_settype(&mtxattr_errorcheck, PTHREAD_MUTEX_ERRORCHECK)); |
417 |
|
|
418 |
36630 |
cache_param = heritage.param; |
419 |
|
|
420 |
36630 |
PTOK(pthread_key_create(&req_key, NULL)); |
421 |
36630 |
PTOK(pthread_key_create(&bo_key, NULL)); |
422 |
36630 |
PTOK(pthread_key_create(&wrk_key, NULL)); |
423 |
36630 |
PTOK(pthread_key_create(&witness_key, free)); |
424 |
36630 |
PTOK(pthread_key_create(&name_key, NULL)); |
425 |
36630 |
PTOK(pthread_key_create(&panic_key, NULL)); |
426 |
|
|
427 |
36630 |
THR_SetName("cache-main"); |
428 |
|
|
429 |
36630 |
PTOK(pthread_mutex_init(&cache_vrnd_mtx, &mtxattr_errorcheck)); |
430 |
36630 |
VRND_Lock = cache_vrnd_lock; |
431 |
36630 |
VRND_Unlock = cache_vrnd_unlock; |
432 |
|
|
433 |
36630 |
VSM_Init(); /* First, LCK needs it. */ |
434 |
|
|
435 |
36630 |
LCK_Init(); /* Second, locking */ |
436 |
|
|
437 |
36630 |
Lck_New(&vxid_lock, lck_vxid); |
438 |
|
|
439 |
36630 |
CLI_Init(); |
440 |
36630 |
PAN_Init(); |
441 |
36630 |
VFP_Init(); |
442 |
|
|
443 |
36630 |
ObjInit(); |
444 |
|
|
445 |
36630 |
WRK_Init(); |
446 |
|
|
447 |
36630 |
VCL_Init(); |
448 |
36630 |
VCL_VRT_Init(); |
449 |
|
|
450 |
36630 |
HTTP_Init(); |
451 |
|
|
452 |
36630 |
VBO_Init(); |
453 |
36630 |
VCP_Init(); |
454 |
36630 |
VBP_Init(); |
455 |
36630 |
VDI_Init(); |
456 |
36630 |
VBE_InitCfg(); |
457 |
36630 |
Pool_Init(); |
458 |
36630 |
V1P_Init(); |
459 |
36630 |
V2D_Init(); |
460 |
|
|
461 |
36630 |
EXP_Init(); |
462 |
36630 |
HSH_Init(heritage.hash); |
463 |
36630 |
BAN_Init(); |
464 |
|
|
465 |
36630 |
VCA_Init(); |
466 |
|
|
467 |
36630 |
STV_open(); |
468 |
|
|
469 |
36630 |
VMOD_Init(); |
470 |
|
|
471 |
36630 |
BAN_Compile(); |
472 |
|
|
473 |
36630 |
VRND_SeedAll(); |
474 |
|
|
475 |
|
|
476 |
36630 |
CLI_AddFuncs(debug_cmds); |
477 |
36630 |
CLI_AddFuncs(child_cmds); |
478 |
|
|
479 |
|
#if WITH_PERSISTENT_STORAGE |
480 |
|
/* Wait for persistent storage to load if asked to */ |
481 |
36630 |
if (FEATURE(FEATURE_WAIT_SILO)) |
482 |
960 |
SMP_Ready(); |
483 |
|
#endif |
484 |
|
|
485 |
36630 |
CLI_Run(); |
486 |
|
|
487 |
36630 |
if (shutdown_delay > 0) |
488 |
0 |
VTIM_sleep(shutdown_delay); |
489 |
|
|
490 |
36630 |
VCA_Shutdown(); |
491 |
36630 |
BAN_Shutdown(); |
492 |
36630 |
EXP_Shutdown(); |
493 |
36630 |
STV_close(); |
494 |
|
|
495 |
36630 |
printf("Child dies\n"); |
496 |
36630 |
} |