Code Coverage: vinyl-cache/bin/vinyld/cache/cache_main.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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_int.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
#include <sys/resource.h>
42
43
#ifdef HAVE_PTHREAD_NP_H
44
#  include <pthread_np.h>
45
#endif
46
47
#include "common/heritage.h"
48
49
#include "vcli_serve.h"
50
#include "vnum.h"
51
#include "vtim.h"
52
#include "vrnd.h"
53
54
#include "hash/hash_slinger.h"
55
56
volatile struct params          *cache_param;
57
static pthread_mutex_t          cache_vrnd_mtx;
58
static vtim_dur                 shutdown_delay = 0;
59
60
pthread_mutexattr_t mtxattr_errorcheck;
61
62
static void
63
cache_vrnd_lock(void)
64 53454
{
65 53454
        PTOK(pthread_mutex_lock(&cache_vrnd_mtx));
66 53454
}
67
68
static void
69
cache_vrnd_unlock(void)
70 53454
{
71 53454
        PTOK(pthread_mutex_unlock(&cache_vrnd_mtx));
72 53454
}
73
74
/*--------------------------------------------------------------------
75
 * Per thread storage for the session currently being processed by
76
 * the thread.  This is used for panic messages.
77
 */
78
79
static pthread_key_t req_key;
80
static pthread_key_t bo_key;
81
static pthread_key_t wrk_key;
82
#ifdef ENABLE_WITNESS
83
pthread_key_t witness_key;
84
#endif
85
pthread_key_t panic_key;
86
87
void
88
THR_SetBusyobj(const struct busyobj *bo)
89 11939
{
90
91 11939
        PTOK(pthread_setspecific(bo_key, bo));
92 11939
}
93
94
struct busyobj *
95
THR_GetBusyobj(void)
96 11
{
97
98 11
        return (pthread_getspecific(bo_key));
99 11
}
100
101
void
102
THR_SetRequest(const struct req *req)
103 14736
{
104
105 14736
        PTOK(pthread_setspecific(req_key, req));
106 14736
}
107
108
struct req *
109
THR_GetRequest(void)
110 6528
{
111
112 6528
        return (pthread_getspecific(req_key));
113 6528
}
114
115
void
116
THR_SetWorker(const struct worker *wrk)
117 21532
{
118
119 21532
        PTOK(pthread_setspecific(wrk_key, wrk));
120 21532
}
121
122
struct worker *
123
THR_GetWorker(void)
124 6731
{
125
126 6731
        return (pthread_getspecific(wrk_key));
127 6731
}
128
129
/*--------------------------------------------------------------------
130
 * Name threads if our pthreads implementation supports it.
131
 */
132
133
static pthread_key_t name_key;
134
135
#if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) &&  \
136
    !defined(__NetBSD__)
137
static void
138
thr_setname_generic(const char *name)
139 37921
{
140 37921
        char buf[16];
141
142
        /* The Linux kernel enforces a strict limitation of 15 bytes name,
143
         * truncate the name if we would overflow it.
144
         */
145 37921
        if (vstrlen(name) > 15) {
146 1026
                bprintf(buf, "%.14s~", name);
147 1026
                name = buf;
148 1026
        }
149
150
        //lint --e{438} Last value assigned not used
151 37921
        PTOK(pthread_setname_np(pthread_self(), name));
152 37921
}
153
#endif
154
155
void
156
THR_SetName(const char *name)
157 37915
{
158
159 37915
        PTOK(pthread_setspecific(name_key, name));
160 37921
#if defined(HAVE_PTHREAD_SETNAME_NP)
161
#  if defined(__APPLE__)
162
        (void)pthread_setname_np(name);
163
#  elif defined(__NetBSD__)
164
        (void)pthread_setname_np(pthread_self(), "%s", (char *)(uintptr_t)name);
165
#  else
166 37921
        thr_setname_generic(name);
167 37921
#  endif
168
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
169
        (void)pthread_set_name_np(pthread_self(), name);
170
#endif
171 37921
}
172
173
const char *
174
THR_GetName(void)
175 11
{
176
177 11
        return (pthread_getspecific(name_key));
178 11
}
179
180
/*--------------------------------------------------------------------
181
 * Generic setup all our threads should call
182
 */
183
static stack_t altstack;
184
185
void
186
THR_Init(void)
187 37912
{
188 37912
        if (altstack.ss_sp != NULL)
189 37877
                AZ(sigaltstack(&altstack, NULL));
190 37912
}
191
192
/*--------------------------------------------------------------------
193
 * VXID's are unique transaction numbers allocated with a minimum of
194
 * locking overhead via pools in the worker threads.
195
 *
196
 * VXID's are mostly for use in VSL and for that reason we never return
197
 * zero vxid, in order to reserve that for "unassociated" VSL records.
198
 */
199
200
static uint64_t vxid_base = 1;
201
static uint32_t vxid_chunk = 32768;
202
static struct lock vxid_lock;
203
204
vxid_t
205
VXID_Get(const struct worker *wrk, uint64_t mask)
206 14298
{
207 14298
        struct vxid_pool *v;
208 14298
        vxid_t retval;
209
210 14298
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
211 14298
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
212 14298
        v = wrk->wpriv->vxid_pool;
213 14298
        AZ(mask & VSL_IDENTMASK);
214 28585
        while (v->count == 0 || v->next >= VRT_INTEGER_MAX) {
215 14287
                Lck_Lock(&vxid_lock);
216 14287
                v->next = vxid_base;
217 14287
                v->count = vxid_chunk;
218 14287
                vxid_base += v->count;
219 14287
                if (vxid_base >= VRT_INTEGER_MAX)
220 2
                        vxid_base = 1;
221 14287
                Lck_Unlock(&vxid_lock);
222 14287
        }
223 14298
        v->count--;
224 14298
        assert(v->next > 0);
225 14298
        assert(v->next < VSL_CLIENTMARKER);
226 14298
        retval.vxid = v->next | mask;
227 14298
        v->next++;
228 14298
        return (retval);
229 14298
}
230
231
/*--------------------------------------------------------------------
232
 * Debugging aids
233
 */
234
235
/*
236
 * Dumb down the VXID allocation to make it predictable for
237
 * vinyltest cases
238
 */
239
static void v_matchproto_(cli_func_t)
240
cli_debug_xid(struct cli *cli, const char * const *av, void *priv)
241 1015
{
242 1015
        (void)priv;
243 1015
        if (av[2] != NULL) {
244 1015
                vxid_base = strtoull(av[2], NULL, 0);
245 1015
                vxid_chunk = 0;
246 1015
                if (av[3] != NULL)
247 0
                        vxid_chunk = strtoul(av[3], NULL, 0);
248 1015
                if (vxid_chunk == 0)
249 1015
                        vxid_chunk = 1;
250 1015
        }
251 1015
        VCLI_Out(cli, "XID is %ju chunk %u", (uintmax_t)vxid_base, vxid_chunk);
252 1015
}
253
254
/*
255
 * Artificially slow down the process shutdown.
256
 */
257
static void v_matchproto_(cli_func_t)
258
cli_debug_shutdown_delay(struct cli *cli, const char * const *av, void *priv)
259 0
{
260
261 0
        (void)cli;
262 0
        (void)priv;
263 0
        shutdown_delay = VNUM_duration(av[2]);
264 0
}
265
266
/*
267
 * Default to seed=1, this is the only seed value POSIX guarantees will
268
 * result in a reproducible random number sequence.
269
 */
270
static void v_matchproto_(cli_func_t)
271
cli_debug_srandom(struct cli *cli, const char * const *av, void *priv)
272 3
{
273 3
        unsigned seed = 1;
274
275 3
        (void)priv;
276 3
        (void)cli;
277 3
        if (av[2] != NULL)
278 0
                seed = strtoul(av[2], NULL, 0);
279 3
        VRND_SeedTestable(seed);
280 3
}
281
282
static struct cli_proto debug_cmds[] = {
283
        { CLICMD_DEBUG_XID,             "d", cli_debug_xid },
284
        { CLICMD_DEBUG_SHUTDOWN_DELAY,  "d", cli_debug_shutdown_delay },
285
        { CLICMD_DEBUG_SRANDOM,         "d", cli_debug_srandom },
286
        { NULL }
287
};
288
289
/*--------------------------------------------------------------------
290
 * XXX: Think more about which order we start things
291
 */
292
293
#if defined(__FreeBSD__) && __FreeBSD_version >= 1000000
294
static void
295
child_malloc_fail(void *p, const char *s)
296 0
{
297 0
        VSL(SLT_Error, NO_VXID, "MALLOC ERROR: %s (%p)", s, p);
298 0
        fprintf(stderr, "MALLOC ERROR: %s (%p)\n", s, p);
299 0
        WRONG("Malloc Error");
300 0
}
301
#endif
302
303
/*=====================================================================
304
 * signal handler for child process
305
 */
306
307
static void v_noreturn_ v_matchproto_()
308
child_signal_handler(int s, siginfo_t *si, void *c)
309 2
{
310 2
        char buf[1024];
311 2
        struct sigaction sa;
312 2
        struct req *req;
313 2
        const char *a, *p, *info = NULL;
314
315 2
        (void)c;
316
        /* Don't come back */
317 2
        memset(&sa, 0, sizeof sa);
318 2
        sa.sa_handler = SIG_DFL;
319 2
        (void)sigaction(SIGSEGV, &sa, NULL);
320 2
        (void)sigaction(SIGBUS, &sa, NULL);
321 2
        (void)sigaction(SIGABRT, &sa, NULL);
322
323 2
        while (s == SIGSEGV || s == SIGBUS) {
324 2
                req = THR_GetRequest();
325 2
                if (req == NULL || req->wrk == NULL)
326 0
                        break;
327 2
                a = TRUST_ME(si->si_addr);
328 2
                p = TRUST_ME(req->wrk);
329 2
                p += sizeof *req->wrk;
330
                // rough safe estimate - top of stack
331 2
                if (a > p + cache_param->wthread_stacksize)
332 0
                        break;
333 2
                if (a < p - 2 * cache_param->wthread_stacksize)
334 1
                        break;
335 1
                info = "\nTHIS PROBABLY IS A STACK OVERFLOW - "
336 1
                        "check thread_pool_stack parameter";
337 1
                break;
338 2
        }
339 2
        bprintf(buf, "Signal %d (%s) received at %p si_code %d%s",
340 2
                s, strsignal(s), si->si_addr, si->si_code,
341 2
                info ? info : "");
342
343 2
        VAS_Fail(__func__,
344 2
                 __FILE__,
345 2
                 __LINE__,
346 2
                 buf,
347 2
                 VAS_WRONG);
348 2
}
349
350
/*=====================================================================
351
 * Magic for panicking properly on signals
352
 */
353
354
static void
355
child_sigmagic(size_t altstksz)
356 1025
{
357 1025
        struct sigaction sa;
358
359 1025
        memset(&sa, 0, sizeof sa);
360
361 1025
        size_t sz = vmax_t(size_t, SIGSTKSZ + 4096, altstksz);
362 1025
        altstack.ss_sp = mmap(NULL, sz,  PROT_READ | PROT_WRITE,
363 1025
                              MAP_PRIVATE | MAP_ANONYMOUS,
364 1025
                              -1, 0);
365 1025
        AN(altstack.ss_sp != MAP_FAILED);
366 1025
        AN(altstack.ss_sp);
367 1025
        altstack.ss_size = sz;
368 1025
        altstack.ss_flags = 0;
369 1025
        sa.sa_flags |= SA_ONSTACK;
370
371 1025
        THR_Init();
372
373 1025
        sa.sa_sigaction = child_signal_handler;
374 1025
        sa.sa_flags |= SA_SIGINFO;
375 1025
        (void)sigaction(SIGBUS, &sa, NULL);
376 1025
        (void)sigaction(SIGABRT, &sa, NULL);
377 1025
        (void)sigaction(SIGSEGV, &sa, NULL);
378 1025
}
379
380
static void
381
cli_quit(int sig)
382 3
{
383
384 3
        if (!IS_CLI()) {
385 0
                PTOK(pthread_kill(cli_thread, sig));
386 0
                return;
387 0
        }
388
389 3
        WRONG("It's time for the big quit");
390 3
}
391
392
static void
393
child_sig_nore(int sig)
394 2226
{
395
396 2226
        (void)sig;
397 2226
}
398
399
/*=====================================================================
400
 * XXX Generalize?
401
 */
402
403
static void
404
bit_set(volatile uint8_t *p, unsigned no)
405 1013
{
406 1013
        uint8_t b;
407
408 1013
        p += (no >> 3);
409 1013
        b = (0x80 >> (no & 7));
410 1013
        *p |= b;
411 1013
}
412
413
/*=====================================================================
414
 * Run the child process
415
 */
416
417
static void v_matchproto_(cli_func_t)
418
ccf_child_start(struct cli *cli, const char * const *av, void *priv)
419 1023
{
420 1023
        (void)av;
421 1023
        (void)priv;
422
423 1023
        VCA_Start(cli);
424 1023
}
425
426
static struct cli_proto child_cmds[] = {
427
        { CLICMD_SERVER_START,          "", ccf_child_start },
428
        { NULL }
429
};
430
431
#define CAP 17U
432
433
static void
434
t_vscarab1(struct vscarab *scarab)
435 3078
{
436 3078
        struct viov *v;
437 3078
        uint64_t sum;
438
439 3078
        VSCARAB_CHECK_NOTNULL(scarab);
440 3078
        AZ(scarab->used);
441
442 3078
        v = VSCARAB_GET(scarab);
443 3078
        AN(v);
444 3078
        v->lease = 12;
445
446 3078
        VSCARAB_ADD(scarab, (struct viov){.lease = 30});
447
448 3078
        sum = 0;
449 3078
        VSCARAB_FOREACH(v, scarab)
450 6156
                sum += v->lease;
451
452 3078
        assert(sum == 42);
453 3078
}
454
455
static void
456
t_vscarab(void)
457 1026
{
458 1026
        char testbuf[VSCARAB_SIZE(CAP)];
459 1026
        struct vscarab *frombuf = (void *)testbuf;
460 1026
        VSCARAB_INIT(frombuf, CAP);
461 1026
        t_vscarab1(frombuf);
462
463
        // ---
464
465 1026
        VSCARAB_LOCAL(scarab, CAP);
466 1026
        t_vscarab1(scarab);
467
468
        // ---
469
470 1026
        struct vscarab *heap;
471 1026
        VSCARAB_ALLOC(heap, CAP);
472 1026
        t_vscarab1(heap);
473 1026
        free(heap);
474 1026
}
475
476
static void
477
sigusr1(void)
478 1026
{
479 1026
        struct sigaction sa;
480
481 1026
        memset(&sa, 0, sizeof sa);
482 1026
        AZ(sigemptyset(&sa.sa_mask));
483 1026
        AZ(sigaddset(&sa.sa_mask, SIGUSR1));
484 1026
        sa.sa_handler = child_sig_nore;
485 1026
        AZ(sigaction(SIGUSR1, &sa, NULL));
486 1026
}
487
488
void
489
child_main(int sigmagic, size_t altstksz)
490 1026
{
491 1026
        t_vscarab();
492
493 1026
        if (sigmagic)
494 1025
                child_sigmagic(altstksz);
495 1026
        (void)signal(SIGINT, SIG_DFL);
496 1026
        (void)signal(SIGTERM, SIG_DFL);
497 1026
        (void)signal(SIGQUIT, cli_quit);
498 1026
        sigusr1();
499
500 1026
#if defined(__FreeBSD__) && __FreeBSD_version >= 1000000
501 1026
        malloc_message = child_malloc_fail;
502 1026
#endif
503
504
        /* Before anything uses pthreads in anger */
505 1026
        PTOK(pthread_mutexattr_init(&mtxattr_errorcheck));
506 1026
        PTOK(pthread_mutexattr_settype(&mtxattr_errorcheck, PTHREAD_MUTEX_ERRORCHECK));
507
508 1026
        cache_param = heritage.param;
509
510 1026
        PTOK(pthread_key_create(&req_key, NULL));
511 1026
        PTOK(pthread_key_create(&bo_key, NULL));
512 1026
        PTOK(pthread_key_create(&wrk_key, NULL));
513 1026
#ifdef ENABLE_WITNESS
514 1026
        PTOK(pthread_key_create(&witness_key, free));
515 1026
#endif
516 1026
        PTOK(pthread_key_create(&name_key, NULL));
517 1026
        PTOK(pthread_key_create(&panic_key, NULL));
518
519 1026
        THR_SetName("cache-main");
520
521 1026
        PTOK(pthread_mutex_init(&cache_vrnd_mtx, &mtxattr_errorcheck));
522 1026
        VRND_Lock = cache_vrnd_lock;
523 1026
        VRND_Unlock = cache_vrnd_unlock;
524
525 1026
        VSM_Init();     /* First, LCK needs it. */
526
527 1026
        LCK_Init();     /* Second, locking */
528
529 1026
        Lck_New(&vxid_lock, lck_vxid);
530
531 1026
        CLI_Init();
532 1026
        PAN_Init();
533
534 1026
        ObjInit();
535
536 1026
        WRK_Init();
537
538 1026
        VCL_Init();
539 1026
        VCL_VRT_Init();
540
541 1026
        HTTP_Init();
542
543 1026
        VBO_Init();
544 1026
        VCP_Init();
545 1026
        VBP_Init();
546 1026
        VDI_Init();
547 1026
        VBE_InitCfg();
548 1026
        Pool_Init();
549 1026
        V1P_Init();
550 1026
        V2D_Init();
551
552 1026
        EXP_Init();
553 1026
        HSH_Init(heritage.hash);
554 1026
        BAN_Init();
555
556 1026
        VCA_Init();
557
558 1026
        STV_open();
559
560 1026
        VMOD_Init();
561
562 1026
        BAN_Compile();
563
564 1026
        VRND_SeedAll();
565
566
567 1026
        CLI_AddFuncs(debug_cmds);
568 1026
        CLI_AddFuncs(child_cmds);
569
570 1026
#ifdef WITH_PERSISTENT_STORAGE
571
        /* Wait for persistent storage to load if asked to */
572 1026
        if (FEATURE(FEATURE_WAIT_SILO))
573 24
                SMP_Ready();
574 1026
#endif
575
576 1026
        CLI_Run();
577
578 1026
        bit_set(cache_param->debug_bits, DBG_VCLREL);
579
580 1026
        if (shutdown_delay > 0)
581 0
                VTIM_sleep(shutdown_delay);
582
583 1026
        VCA_Shutdown();
584 1026
        Pool_Stop();
585 1026
        BAN_Shutdown();
586 1026
        STV_warn();
587 1026
        VCL_Shutdown();
588 1026
        EXP_Shutdown();
589 1026
        STV_close();
590 1026
        Pool_Fini();
591
592 1026
        struct rusage usage = {0};
593 1026
        if (! getrusage(RUSAGE_SELF, &usage)) {
594 1013
                printf("Child dies usr=%f sys=%f\n",
595 1013
                    tv_vtim(usage.ru_utime),
596 1013
                    tv_vtim(usage.ru_stime));
597 1013
        } else
598 13
                printf("Child dies\n");
599 1026
}