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