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 2141450
cache_vrnd_lock(void)
63
{
64 2141450
        PTOK(pthread_mutex_lock(&cache_vrnd_mtx));
65 2141450
}
66
67
static void
68 2141450
cache_vrnd_unlock(void)
69
{
70 2141450
        PTOK(pthread_mutex_unlock(&cache_vrnd_mtx));
71 2141450
}
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 354175
THR_SetBusyobj(const struct busyobj *bo)
86
{
87
88 354175
        PTOK(pthread_setspecific(bo_key, bo));
89 354175
}
90
91
struct busyobj *
92 516
THR_GetBusyobj(void)
93
{
94
95 516
        return (pthread_getspecific(bo_key));
96
}
97
98
void
99 229158
THR_SetRequest(const struct req *req)
100
{
101
102 229158
        PTOK(pthread_setspecific(req_key, req));
103 229158
}
104
105
struct req *
106 596
THR_GetRequest(void)
107
{
108
109 596
        return (pthread_getspecific(req_key));
110
}
111
112
void
113 745975
THR_SetWorker(const struct worker *wrk)
114
{
115
116 745975
        PTOK(pthread_setspecific(wrk_key, wrk));
117 745975
}
118
119
struct worker *
120 173870
THR_GetWorker(void)
121
{
122
123 173870
        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 1330740
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 1330740
        if (strlen(name) > 15) {
141 36676
                bprintf(buf, "%.14s~", name);
142 36676
                name = buf;
143 36676
        }
144
145
        //lint --e{438} Last value assigned not used
146 1330740
        PTOK(pthread_setname_np(pthread_self(), name));
147 1330740
}
148
149
void
150 1330755
THR_SetName(const char *name)
151
{
152
153 1330755
        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 1330755
        thr_setname_generic(name);
160
#endif
161 1330755
}
162
163
const char *
164 516
THR_GetName(void)
165
{
166
167 516
        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 1330622
THR_Init(void)
177
{
178 1330622
        if (altstack.ss_sp != NULL)
179 1329290
                AZ(sigaltstack(&altstack, NULL));
180 1330622
}
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 324808
VXID_Get(const struct worker *wrk, uint64_t mask)
196
{
197
        struct vxid_pool *v;
198
        vxid_t retval;
199
200 324808
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
201 324808
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
202 324808
        v = wrk->wpriv->vxid_pool;
203 324808
        AZ(mask & VSL_IDENTMASK);
204 649216
        while (v->count == 0 || v->next >= VRT_INTEGER_MAX) {
205 324408
                Lck_Lock(&vxid_lock);
206 324408
                v->next = vxid_base;
207 324408
                v->count = vxid_chunk;
208 324408
                vxid_base += v->count;
209 324408
                if (vxid_base >= VRT_INTEGER_MAX)
210 80
                        vxid_base = 1;
211 324408
                Lck_Unlock(&vxid_lock);
212
        }
213 324808
        v->count--;
214 324808
        assert(v->next > 0);
215 324808
        assert(v->next < VSL_CLIENTMARKER);
216 324808
        retval.vxid = v->next | mask;
217 324808
        v->next++;
218 324808
        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 36360
cli_debug_xid(struct cli *cli, const char * const *av, void *priv)
231
{
232 36360
        (void)priv;
233 36360
        if (av[2] != NULL) {
234 36360
                vxid_base = strtoull(av[2], NULL, 0);
235 36360
                vxid_chunk = 0;
236 36360
                if (av[3] != NULL)
237 0
                        vxid_chunk = strtoul(av[3], NULL, 0);
238 36360
                if (vxid_chunk == 0)
239 36360
                        vxid_chunk = 1;
240 36360
        }
241 36360
        VCLI_Out(cli, "XID is %ju chunk %u", (uintmax_t)vxid_base, vxid_chunk);
242 36360
}
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 36636
child_sigmagic(size_t altstksz)
346
{
347
        struct sigaction sa;
348
349 36636
        memset(&sa, 0, sizeof sa);
350
351 36636
        size_t sz = vmax_t(size_t, SIGSTKSZ + 4096, altstksz);
352 36636
        altstack.ss_sp = mmap(NULL, sz,  PROT_READ | PROT_WRITE,
353
                              MAP_PRIVATE | MAP_ANONYMOUS,
354
                              -1, 0);
355 36636
        AN(altstack.ss_sp != MAP_FAILED);
356 36636
        AN(altstack.ss_sp);
357 36636
        altstack.ss_size = sz;
358 36636
        altstack.ss_flags = 0;
359 36636
        sa.sa_flags |= SA_ONSTACK;
360
361 36636
        THR_Init();
362
363 36636
        sa.sa_sigaction = child_signal_handler;
364 36636
        sa.sa_flags |= SA_SIGINFO;
365 36636
        (void)sigaction(SIGBUS, &sa, NULL);
366 36636
        (void)sigaction(SIGABRT, &sa, NULL);
367 36636
        (void)sigaction(SIGSEGV, &sa, NULL);
368 36636
}
369
370
static void
371 85
cli_quit(int sig)
372
{
373
374 85
        if (!IS_CLI()) {
375 0
                PTOK(pthread_kill(cli_thread, sig));
376 0
                return;
377
        }
378
379 85
        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 36480
ccf_child_start(struct cli *cli, const char * const *av, void *priv)
388
{
389 36480
        (void)av;
390 36480
        (void)priv;
391
392 36480
        VCA_Start(cli);
393 36480
}
394
395
static struct cli_proto child_cmds[] = {
396
        { CLICMD_SERVER_START,          "", ccf_child_start },
397
        { NULL }
398
};
399
400
void
401 36676
child_main(int sigmagic, size_t altstksz)
402
{
403
404 36676
        if (sigmagic)
405 36636
                child_sigmagic(altstksz);
406 36676
        (void)signal(SIGINT, SIG_DFL);
407 36676
        (void)signal(SIGTERM, SIG_DFL);
408 36676
        (void)signal(SIGQUIT, cli_quit);
409
410
#if defined(__FreeBSD__) && __FreeBSD_version >= 1000000
411 36676
        malloc_message = child_malloc_fail;
412
#endif
413
414
        /* Before anything uses pthreads in anger */
415 36676
        PTOK(pthread_mutexattr_init(&mtxattr_errorcheck));
416 36676
        PTOK(pthread_mutexattr_settype(&mtxattr_errorcheck, PTHREAD_MUTEX_ERRORCHECK));
417
418 36676
        cache_param = heritage.param;
419
420 36676
        PTOK(pthread_key_create(&req_key, NULL));
421 36676
        PTOK(pthread_key_create(&bo_key, NULL));
422 36676
        PTOK(pthread_key_create(&wrk_key, NULL));
423 36676
        PTOK(pthread_key_create(&witness_key, free));
424 36676
        PTOK(pthread_key_create(&name_key, NULL));
425 36676
        PTOK(pthread_key_create(&panic_key, NULL));
426
427 36676
        THR_SetName("cache-main");
428
429 36676
        PTOK(pthread_mutex_init(&cache_vrnd_mtx, &mtxattr_errorcheck));
430 36676
        VRND_Lock = cache_vrnd_lock;
431 36676
        VRND_Unlock = cache_vrnd_unlock;
432
433 36676
        VSM_Init();     /* First, LCK needs it. */
434
435 36676
        LCK_Init();     /* Second, locking */
436
437 36676
        Lck_New(&vxid_lock, lck_vxid);
438
439 36676
        CLI_Init();
440 36676
        PAN_Init();
441 36676
        VFP_Init();
442
443 36676
        ObjInit();
444
445 36676
        WRK_Init();
446
447 36676
        VCL_Init();
448 36676
        VCL_VRT_Init();
449
450 36676
        HTTP_Init();
451
452 36676
        VBO_Init();
453 36676
        VCP_Init();
454 36676
        VBP_Init();
455 36676
        VDI_Init();
456 36676
        VBE_InitCfg();
457 36676
        Pool_Init();
458 36676
        V1P_Init();
459 36676
        V2D_Init();
460
461 36676
        EXP_Init();
462 36676
        HSH_Init(heritage.hash);
463 36676
        BAN_Init();
464
465 36676
        VCA_Init();
466
467 36676
        STV_open();
468
469 36676
        VMOD_Init();
470
471 36676
        BAN_Compile();
472
473 36676
        VRND_SeedAll();
474
475
476 36676
        CLI_AddFuncs(debug_cmds);
477 36676
        CLI_AddFuncs(child_cmds);
478
479
#if WITH_PERSISTENT_STORAGE
480
        /* Wait for persistent storage to load if asked to */
481 36676
        if (FEATURE(FEATURE_WAIT_SILO))
482 960
                SMP_Ready();
483
#endif
484
485 36676
        CLI_Run();
486
487 36676
        if (shutdown_delay > 0)
488 0
                VTIM_sleep(shutdown_delay);
489
490 36676
        VCA_Shutdown();
491 36676
        BAN_Shutdown();
492 36676
        EXP_Shutdown();
493 36676
        STV_close();
494
495 36676
        printf("Child dies\n");
496 36676
}