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