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