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 2144109
cache_vrnd_lock(void)
63
{
64 2144109
        PTOK(pthread_mutex_lock(&cache_vrnd_mtx));
65 2144109
}
66
67
static void
68 2144109
cache_vrnd_unlock(void)
69
{
70 2144109
        PTOK(pthread_mutex_unlock(&cache_vrnd_mtx));
71 2144109
}
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 361533
THR_SetBusyobj(const struct busyobj *bo)
86
{
87
88 361533
        PTOK(pthread_setspecific(bo_key, bo));
89 361533
}
90
91
struct busyobj *
92 512
THR_GetBusyobj(void)
93
{
94
95 512
        return (pthread_getspecific(bo_key));
96
}
97
98
void
99 235112
THR_SetRequest(const struct req *req)
100
{
101
102 235112
        PTOK(pthread_setspecific(req_key, req));
103 235112
}
104
105
struct req *
106 592
THR_GetRequest(void)
107
{
108
109 592
        return (pthread_getspecific(req_key));
110
}
111
112
void
113 759723
THR_SetWorker(const struct worker *wrk)
114
{
115
116 759723
        PTOK(pthread_setspecific(wrk_key, wrk));
117 759723
}
118
119
struct worker *
120 173881
THR_GetWorker(void)
121
{
122
123 173881
        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 1355939
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 1355939
        if (strlen(name) > 15) {
143 37391
                bprintf(buf, "%.14s~", name);
144 37391
                name = buf;
145 37391
        }
146
147
        //lint --e{438} Last value assigned not used
148 1355939
        PTOK(pthread_setname_np(pthread_self(), name));
149 1355939
}
150
#endif
151
152
void
153 1355957
THR_SetName(const char *name)
154
{
155
156 1355957
        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 1355957
        thr_setname_generic(name);
164
#  endif
165
#endif
166 1355957
}
167
168
const char *
169 512
THR_GetName(void)
170
{
171
172 512
        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 1355838
THR_Init(void)
182
{
183 1355838
        if (altstack.ss_sp != NULL)
184 1354158
                AZ(sigaltstack(&altstack, NULL));
185 1355838
}
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 331853
VXID_Get(const struct worker *wrk, uint64_t mask)
201
{
202
        struct vxid_pool *v;
203
        vxid_t retval;
204
205 331853
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
206 331853
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
207 331853
        v = wrk->wpriv->vxid_pool;
208 331853
        AZ(mask & VSL_IDENTMASK);
209 663307
        while (v->count == 0 || v->next >= VRT_INTEGER_MAX) {
210 331454
                Lck_Lock(&vxid_lock);
211 331454
                v->next = vxid_base;
212 331454
                v->count = vxid_chunk;
213 331454
                vxid_base += v->count;
214 331454
                if (vxid_base >= VRT_INTEGER_MAX)
215 80
                        vxid_base = 1;
216 331454
                Lck_Unlock(&vxid_lock);
217
        }
218 331853
        v->count--;
219 331853
        assert(v->next > 0);
220 331853
        assert(v->next < VSL_CLIENTMARKER);
221 331853
        retval.vxid = v->next | mask;
222 331853
        v->next++;
223 331853
        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 37080
cli_debug_xid(struct cli *cli, const char * const *av, void *priv)
236
{
237 37080
        (void)priv;
238 37080
        if (av[2] != NULL) {
239 37080
                vxid_base = strtoull(av[2], NULL, 0);
240 37080
                vxid_chunk = 0;
241 37080
                if (av[3] != NULL)
242 0
                        vxid_chunk = strtoul(av[3], NULL, 0);
243 37080
                if (vxid_chunk == 0)
244 37080
                        vxid_chunk = 1;
245 37080
        }
246 37080
        VCLI_Out(cli, "XID is %ju chunk %u", (uintmax_t)vxid_base, vxid_chunk);
247 37080
}
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 120
cli_debug_srandom(struct cli *cli, const char * const *av, void *priv)
267
{
268 120
        unsigned seed = 1;
269
270 120
        (void)priv;
271 120
        (void)cli;
272 120
        if (av[2] != NULL)
273 0
                seed = strtoul(av[2], NULL, 0);
274 120
        VRND_SeedTestable(seed);
275 120
}
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 80
child_signal_handler(int s, siginfo_t *si, void *c)
304
{
305
        char buf[1024];
306
        struct sigaction sa;
307
        struct req *req;
308 80
        const char *a, *p, *info = NULL;
309
310 80
        (void)c;
311
        /* Don't come back */
312 80
        memset(&sa, 0, sizeof sa);
313 80
        sa.sa_handler = SIG_DFL;
314 80
        (void)sigaction(SIGSEGV, &sa, NULL);
315 80
        (void)sigaction(SIGBUS, &sa, NULL);
316 80
        (void)sigaction(SIGABRT, &sa, NULL);
317
318 80
        while (s == SIGSEGV || s == SIGBUS) {
319 80
                req = THR_GetRequest();
320 80
                if (req == NULL || req->wrk == NULL)
321 0
                        break;
322 80
                a = TRUST_ME(si->si_addr);
323 80
                p = TRUST_ME(req->wrk);
324 80
                p += sizeof *req->wrk;
325
                // rough safe estimate - top of stack
326 80
                if (a > p + cache_param->wthread_stacksize)
327 0
                        break;
328 80
                if (a < p - 2 * cache_param->wthread_stacksize)
329 40
                        break;
330 40
                info = "\nTHIS PROBABLY IS A STACK OVERFLOW - "
331
                        "check thread_pool_stack parameter";
332 40
                break;
333
        }
334 80
        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 80
        VAS_Fail(__func__,
339
                 __FILE__,
340
                 __LINE__,
341 80
                 buf,
342
                 VAS_WRONG);
343
}
344
345
/*=====================================================================
346
 * Magic for panicking properly on signals
347
 */
348
349
static void
350 37352
child_sigmagic(size_t altstksz)
351
{
352
        struct sigaction sa;
353
354 37352
        memset(&sa, 0, sizeof sa);
355
356 37352
        size_t sz = vmax_t(size_t, SIGSTKSZ + 4096, altstksz);
357 37352
        altstack.ss_sp = mmap(NULL, sz,  PROT_READ | PROT_WRITE,
358
                              MAP_PRIVATE | MAP_ANONYMOUS,
359
                              -1, 0);
360 37352
        AN(altstack.ss_sp != MAP_FAILED);
361 37352
        AN(altstack.ss_sp);
362 37352
        altstack.ss_size = sz;
363 37352
        altstack.ss_flags = 0;
364 37352
        sa.sa_flags |= SA_ONSTACK;
365
366 37352
        THR_Init();
367
368 37352
        sa.sa_sigaction = child_signal_handler;
369 37352
        sa.sa_flags |= SA_SIGINFO;
370 37352
        (void)sigaction(SIGBUS, &sa, NULL);
371 37352
        (void)sigaction(SIGABRT, &sa, NULL);
372 37352
        (void)sigaction(SIGSEGV, &sa, NULL);
373 37352
}
374
375
static void
376 85
cli_quit(int sig)
377
{
378
379 85
        if (!IS_CLI()) {
380 0
                PTOK(pthread_kill(cli_thread, sig));
381 0
                return;
382
        }
383
384 85
        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 37200
ccf_child_start(struct cli *cli, const char * const *av, void *priv)
393
{
394 37200
        (void)av;
395 37200
        (void)priv;
396
397 37200
        VCA_Start(cli);
398 37200
}
399
400
static struct cli_proto child_cmds[] = {
401
        { CLICMD_SERVER_START,          "", ccf_child_start },
402
        { NULL }
403
};
404
405
void
406 37392
child_main(int sigmagic, size_t altstksz)
407
{
408
409 37392
        if (sigmagic)
410 37352
                child_sigmagic(altstksz);
411 37392
        (void)signal(SIGINT, SIG_DFL);
412 37392
        (void)signal(SIGTERM, SIG_DFL);
413 37392
        (void)signal(SIGQUIT, cli_quit);
414
415
#if defined(__FreeBSD__) && __FreeBSD_version >= 1000000
416 37392
        malloc_message = child_malloc_fail;
417
#endif
418
419
        /* Before anything uses pthreads in anger */
420 37392
        PTOK(pthread_mutexattr_init(&mtxattr_errorcheck));
421 37392
        PTOK(pthread_mutexattr_settype(&mtxattr_errorcheck, PTHREAD_MUTEX_ERRORCHECK));
422
423 37392
        cache_param = heritage.param;
424
425 37392
        PTOK(pthread_key_create(&req_key, NULL));
426 37392
        PTOK(pthread_key_create(&bo_key, NULL));
427 37392
        PTOK(pthread_key_create(&wrk_key, NULL));
428 37392
        PTOK(pthread_key_create(&witness_key, free));
429 37392
        PTOK(pthread_key_create(&name_key, NULL));
430 37392
        PTOK(pthread_key_create(&panic_key, NULL));
431
432 37392
        THR_SetName("cache-main");
433
434 37392
        PTOK(pthread_mutex_init(&cache_vrnd_mtx, &mtxattr_errorcheck));
435 37392
        VRND_Lock = cache_vrnd_lock;
436 37392
        VRND_Unlock = cache_vrnd_unlock;
437
438 37392
        VSM_Init();     /* First, LCK needs it. */
439
440 37392
        LCK_Init();     /* Second, locking */
441
442 37392
        Lck_New(&vxid_lock, lck_vxid);
443
444 37392
        CLI_Init();
445 37392
        PAN_Init();
446 37392
        VFP_Init();
447
448 37392
        ObjInit();
449
450 37392
        WRK_Init();
451
452 37392
        VCL_Init();
453 37392
        VCL_VRT_Init();
454
455 37392
        HTTP_Init();
456
457 37392
        VBO_Init();
458 37392
        VCP_Init();
459 37392
        VBP_Init();
460 37392
        VDI_Init();
461 37392
        VBE_InitCfg();
462 37392
        Pool_Init();
463 37392
        V1P_Init();
464 37392
        V2D_Init();
465
466 37392
        EXP_Init();
467 37392
        HSH_Init(heritage.hash);
468 37392
        BAN_Init();
469
470 37392
        VCA_Init();
471
472 37392
        STV_open();
473
474 37392
        VMOD_Init();
475
476 37392
        BAN_Compile();
477
478 37392
        VRND_SeedAll();
479
480
481 37392
        CLI_AddFuncs(debug_cmds);
482 37392
        CLI_AddFuncs(child_cmds);
483
484
#ifdef WITH_PERSISTENT_STORAGE
485
        /* Wait for persistent storage to load if asked to */
486 37392
        if (FEATURE(FEATURE_WAIT_SILO))
487 960
                SMP_Ready();
488
#endif
489
490 37392
        CLI_Run();
491
492 37392
        if (shutdown_delay > 0)
493 0
                VTIM_sleep(shutdown_delay);
494
495 37392
        VCA_Shutdown();
496 37392
        BAN_Shutdown();
497 37392
        EXP_Shutdown();
498 37392
        STV_close();
499
500 37392
        printf("Child dies\n");
501 37392
}