varnish-cache/bin/varnishd/cache/cache_main.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2011 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include "cache_varnishd.h"
33
34
#include <signal.h>
35
#include <stdio.h>
36
#include <stdlib.h>
37
38
#ifdef HAVE_SIGALTSTACK
39
#  include <sys/mman.h>
40
#endif
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 "vrnd.h"
50
51
#include "hash/hash_slinger.h"
52
53
54
volatile struct params          *cache_param;
55
static pthread_mutex_t          cache_vrnd_mtx;
56
57
static void
58 644903
cache_vrnd_lock(void)
59
{
60 644903
        AZ(pthread_mutex_lock(&cache_vrnd_mtx));
61 644903
}
62
63
static void
64 644903
cache_vrnd_unlock(void)
65
{
66 644903
        AZ(pthread_mutex_unlock(&cache_vrnd_mtx));
67 644903
}
68
69
/*--------------------------------------------------------------------
70
 * Per thread storage for the session currently being processed by
71
 * the thread.  This is used for panic messages.
72
 */
73
74
static pthread_key_t req_key;
75
static pthread_key_t bo_key;
76
pthread_key_t witness_key;
77
78
void
79 78734
THR_SetBusyobj(const struct busyobj *bo)
80
{
81
82 78734
        AZ(pthread_setspecific(bo_key, bo));
83 78741
}
84
85
struct busyobj *
86 72
THR_GetBusyobj(void)
87
{
88
89 72
        return (pthread_getspecific(bo_key));
90
}
91
92
void
93 45678
THR_SetRequest(const struct req *req)
94
{
95
96 45678
        AZ(pthread_setspecific(req_key, req));
97 45671
}
98
99
struct req *
100 96
THR_GetRequest(void)
101
{
102
103 96
        return (pthread_getspecific(req_key));
104
}
105
106
/*--------------------------------------------------------------------
107
 * Name threads if our pthreads implementation supports it.
108
 */
109
110
static pthread_key_t name_key;
111
112
void
113 296854
THR_SetName(const char *name)
114
{
115
116 296854
        AZ(pthread_setspecific(name_key, name));
117
#if defined(HAVE_PTHREAD_SET_NAME_NP)
118 296926
        pthread_set_name_np(pthread_self(), name);
119
#elif defined(HAVE_PTHREAD_SETNAME_NP)
120
#if defined(__APPLE__)
121
        (void)pthread_setname_np(name);
122
#elif defined(__NetBSD__)
123
        (void)pthread_setname_np(pthread_self(), "%s", (char *)(uintptr_t)name);
124
#else
125
        (void)pthread_setname_np(pthread_self(), name);
126
#endif
127
#endif
128 296928
}
129
130
const char *
131 72
THR_GetName(void)
132
{
133
134 72
        return (pthread_getspecific(name_key));
135
}
136
137
/*--------------------------------------------------------------------
138
 * Generic setup all our threads should call
139
 */
140
#ifdef HAVE_SIGALTSTACK
141
static stack_t altstack;
142
#endif
143
144
void
145 296822
THR_Init(void)
146
{
147
#ifdef HAVE_SIGALTSTACK
148 296822
        if (altstack.ss_sp != NULL)
149 296406
                AZ(sigaltstack(&altstack, NULL));
150
#endif
151 296863
}
152
153
/*--------------------------------------------------------------------
154
 * VXID's are unique transaction numbers allocated with a minimum of
155
 * locking overhead via pools in the worker threads.
156
 *
157
 * VXID's are mostly for use in VSL and for that reason we never return
158
 * zero vxid, in order to reserve that for "unassociated" VSL records.
159
 */
160
161
static uint32_t vxid_base;
162
static uint32_t vxid_chunk = 32768;
163
static struct lock vxid_lock;
164
165
uint32_t
166 67644
VXID_Get(struct worker *wrk, uint32_t mask)
167
{
168
        struct vxid_pool *v;
169
170 67644
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
171 67644
        v = &wrk->vxid_pool;
172 67644
        AZ(VXID(mask));
173
        do {
174 67644
                if (v->count == 0) {
175 67524
                        Lck_Lock(&vxid_lock);
176 67524
                        v->next = vxid_base;
177 67524
                        v->count = vxid_chunk;
178 67524
                        vxid_base = (vxid_base + v->count) & VSL_IDENTMASK;
179 67524
                        Lck_Unlock(&vxid_lock);
180
                }
181 67644
                v->count--;
182 67644
                v->next++;
183 67644
        } while (v->next == 0);
184 67644
        return (v->next | mask);
185
}
186
187
/*--------------------------------------------------------------------
188
 * Debugging aids
189
 */
190
191
/*
192
 * Dumb down the VXID allocation to make it predictable for
193
 * varnishtest cases
194
 */
195
static void v_matchproto_(cli_func_t)
196 8208
cli_debug_xid(struct cli *cli, const char * const *av, void *priv)
197
{
198
        (void)priv;
199 8208
        if (av[2] != NULL) {
200 8208
                vxid_base = strtoul(av[2], NULL, 0);
201 8208
                vxid_chunk = 1;
202
        }
203 8208
        VCLI_Out(cli, "XID is %u", vxid_base);
204 8208
}
205
206
/*
207
 * Default to seed=1, this is the only seed value POSIXl guarantees will
208
 * result in a reproducible random number sequence.
209
 */
210
static void v_matchproto_(cli_func_t)
211 36
cli_debug_srandom(struct cli *cli, const char * const *av, void *priv)
212
{
213 36
        unsigned seed = 1;
214
215
        (void)priv;
216
        (void)cli;
217 36
        if (av[2] != NULL)
218 0
                seed = strtoul(av[2], NULL, 0);
219 36
        VRND_SeedTestable(seed);
220 36
}
221
222
static struct cli_proto debug_cmds[] = {
223
        { CLICMD_DEBUG_XID,                     "d", cli_debug_xid },
224
        { CLICMD_DEBUG_SRANDOM,                 "d", cli_debug_srandom },
225
        { NULL }
226
};
227
228
/*--------------------------------------------------------------------
229
 * XXX: Think more about which order we start things
230
 */
231
232
#if defined(__FreeBSD__) && __FreeBSD_version >= 1000000
233
static void
234 0
child_malloc_fail(void *p, const char *s)
235
{
236 0
        VSL(SLT_Error, 0, "MALLOC ERROR: %s (%p)", s, p);
237 0
        fprintf(stderr, "MALLOC ERROR: %s (%p)\n", s, p);
238 0
        WRONG("Malloc Error");
239
}
240
#endif
241
242
/*=====================================================================
243
 * signal handler for child process
244
 */
245
246
static void v_matchproto_()
247 24
child_signal_handler(int s, siginfo_t *si, void *c)
248
{
249
        char buf[1024];
250
        struct sigaction sa;
251
        struct req *req;
252 24
        const char *a, *p, *info = NULL;
253
254
        (void)c;
255
        /* Don't come back */
256 24
        memset(&sa, 0, sizeof sa);
257 24
        sa.sa_handler = SIG_DFL;
258 24
        (void)sigaction(SIGSEGV, &sa, NULL);
259 24
        (void)sigaction(SIGBUS, &sa, NULL);
260 24
        (void)sigaction(SIGABRT, &sa, NULL);
261
262 24
        while (s == SIGSEGV || s == SIGBUS) {
263 24
                req = THR_GetRequest();
264 24
                if (req == NULL || req->wrk == NULL)
265
                        break;
266 24
                a = TRUST_ME(si->si_addr);
267 24
                p = TRUST_ME(req->wrk);
268 24
                p += sizeof *req->wrk;
269
                // rough safe estimate - top of stack
270 24
                if (a > p + cache_param->wthread_stacksize)
271 0
                        break;
272 24
                if (a < p - 2 * cache_param->wthread_stacksize)
273 12
                        break;
274 12
                info = "\nTHIS PROBABLY IS A STACK OVERFLOW - "
275
                        "check thread_pool_stack parameter";
276 12
                break;
277
        }
278 24
        bprintf(buf, "Signal %d (%s) received at %p si_code %d%s",
279
                s, strsignal(s), si->si_addr, si->si_code,
280
                info ? info : "");
281
282 24
        VAS_Fail(__func__,
283
                 __FILE__,
284
                 __LINE__,
285
                 buf,
286
                 VAS_WRONG);
287
}
288
289
/*=====================================================================
290
 * Magic for panicing properly on signals
291
 */
292
293
static void
294 8208
child_sigmagic(size_t altstksz)
295
{
296
        struct sigaction sa;
297
298 8208
        memset(&sa, 0, sizeof sa);
299
300
#ifdef HAVE_SIGALTSTACK
301 8208
        size_t sz = SIGSTKSZ + 4096;
302 8208
        if (sz < altstksz)
303 8208
                sz = altstksz;
304 8208
        altstack.ss_sp = mmap(NULL, sz,  PROT_READ | PROT_WRITE,
305
                              MAP_PRIVATE | MAP_ANONYMOUS,
306
                              -1, 0);
307 8208
        AN(altstack.ss_sp != MAP_FAILED);
308 8208
        AN(altstack.ss_sp);
309 8208
        altstack.ss_size = sz;
310 8208
        altstack.ss_flags = 0;
311 8208
        sa.sa_flags |= SA_ONSTACK;
312
#else
313
        (void)altstksz;
314
#endif
315
316 8208
        THR_Init();
317
318 8208
        sa.sa_sigaction = child_signal_handler;
319 8208
        sa.sa_flags |= SA_SIGINFO;
320 8208
        (void)sigaction(SIGBUS, &sa, NULL);
321 8208
        (void)sigaction(SIGABRT, &sa, NULL);
322 8208
        (void)sigaction(SIGSEGV, &sa, NULL);
323 8208
}
324
325
326
/*=====================================================================
327
 * Run the child process
328
 */
329
330
void
331 8220
child_main(int sigmagic, size_t altstksz)
332
{
333
334 8220
        if (sigmagic)
335 8208
                child_sigmagic(altstksz);
336 8220
        (void)signal(SIGINT, SIG_DFL);
337 8220
        (void)signal(SIGTERM, SIG_DFL);
338
339 8220
        setbuf(stdout, NULL);
340 8220
        setbuf(stderr, NULL);
341 8220
        printf("Child starts\n");
342
#if defined(__FreeBSD__) && __FreeBSD_version >= 1000000
343 8220
        malloc_message = child_malloc_fail;
344
#endif
345
346 8220
        cache_param = heritage.param;
347
348 8220
        AZ(pthread_key_create(&req_key, NULL));
349 8220
        AZ(pthread_key_create(&bo_key, NULL));
350 8220
        AZ(pthread_key_create(&witness_key, NULL));
351 8220
        AZ(pthread_key_create(&name_key, NULL));
352
353 8220
        THR_SetName("cache-main");
354
355 8220
        AZ(pthread_mutex_init(&cache_vrnd_mtx, NULL));
356 8220
        VRND_Lock = cache_vrnd_lock;
357 8220
        VRND_Unlock = cache_vrnd_unlock;
358
359 8220
        VSM_Init();     /* First, LCK needs it. */
360
361 8220
        LCK_Init();     /* Second, locking */
362
363 8220
        Lck_New(&vxid_lock, lck_vxid);
364
365 8220
        CLI_Init();
366 8220
        PAN_Init();
367 8220
        VFP_Init();
368
369 8220
        ObjInit();
370
371 8220
        VCL_Init();
372 8220
        VCL_VRT_Init();
373
374 8220
        HTTP_Init();
375
376 8220
        VBO_Init();
377 8220
        VTP_Init();
378 8220
        VBP_Init();
379 8220
        VDI_Init();
380 8220
        VBE_InitCfg();
381 8220
        Pool_Init();
382 8220
        V1P_Init();
383 8220
        V2D_Init();
384
385 8220
        EXP_Init();
386 8220
        HSH_Init(heritage.hash);
387 8220
        BAN_Init();
388
389 8220
        VCA_Init();
390
391 8220
        STV_open();
392
393 8220
        VMOD_Init();
394
395 8220
        BAN_Compile();
396
397 8220
        VRND_SeedAll();
398
399 8220
        CLI_AddFuncs(debug_cmds);
400
401
#if WITH_PERSISTENT_STORAGE
402
        /* Wait for persistent storage to load if asked to */
403 8220
        if (FEATURE(FEATURE_WAIT_SILO))
404 288
                SMP_Ready();
405
#endif
406
407 8220
        CLI_Run();
408
409 8148
        VCA_Shutdown();
410 8148
        BAN_Shutdown();
411 8148
        STV_close();
412
413 8148
        printf("Child dies\n");
414 8148
}