varnish-cache/bin/varnishd/mgt/mgt_shmem.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
32
#include "config.h"
33
34
#include <sys/stat.h>
35
36
#include <fcntl.h>
37
#include <stdarg.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <unistd.h>
42
43
#include "mgt/mgt.h"
44
45
#include "vsm_priv.h"
46
47
#include "common/heritage.h"
48
#include "common/vsmw.h"
49
50
static struct vsmw *mgt_vsmw;
51
52
/*--------------------------------------------------------------------
53
 */
54
55
void
56 113000
mgt_SHM_static_alloc(const void *ptr, ssize_t size,
57
    const char *category, const char *ident)
58
{
59
        void *p;
60
61 113000
        p = VSMW_Allocf(mgt_vsmw, NULL, category, size, "%s", ident);
62 113000
        AN(p);
63 113000
        memcpy(p, ptr, size);
64 113000
}
65
66
/*--------------------------------------------------------------------
67
 * Exit handler that clears the owning pid from the SHMLOG
68
 */
69
70
static void
71 179440
mgt_shm_atexit(void)
72
{
73
74
        /* Do not let VCC kill our VSM */
75 179440
        if (getpid() != heritage.mgt_pid)
76 141760
                return;
77 37680
        VJ_master(JAIL_MASTER_FILE);
78 37680
        VSMW_Destroy(&mgt_vsmw);
79 37680
        if (!MGT_DO_DEBUG(DBG_VTC_MODE)) {
80 520
                VJ_master(JAIL_MASTER_SYSTEM);
81 520
                AZ(system("rm -rf " VSM_MGT_DIRNAME));
82 520
                AZ(system("rm -rf " VSM_CHILD_DIRNAME));
83 520
        }
84 37680
        VJ_master(JAIL_MASTER_LOW);
85 179440
}
86
87
/*--------------------------------------------------------------------
88
 * Initialize VSM subsystem
89
 */
90
91
void
92 37680
mgt_SHM_Init(void)
93
{
94
        int fd;
95
96 37680
        VJ_master(JAIL_MASTER_SYSTEM);
97 37680
        AZ(system("rm -rf " VSM_MGT_DIRNAME));
98 37680
        VJ_master(JAIL_MASTER_FILE);
99 37680
        AZ(mkdir(VSM_MGT_DIRNAME, 0755));
100 37680
        fd = open(VSM_MGT_DIRNAME, O_RDONLY);
101 37680
        VJ_fix_fd(fd, JAIL_FIXFD_VSMMGT);
102 37680
        VJ_master(JAIL_MASTER_LOW);
103 37680
        mgt_vsmw = VSMW_New(fd, 0640, "_.index");
104 37680
        AN(mgt_vsmw);
105
106 37680
        heritage.proc_vsmw = mgt_vsmw;
107
108
        /* Setup atexit handler */
109 37680
        AZ(atexit(mgt_shm_atexit));
110 37680
}
111
112
void
113 37560
mgt_SHM_ChildNew(void)
114
{
115
116 37560
        VJ_master(JAIL_MASTER_SYSTEM);
117 37560
        AZ(system("rm -rf " VSM_CHILD_DIRNAME));
118 37560
        VJ_master(JAIL_MASTER_FILE);
119 37560
        AZ(mkdir(VSM_CHILD_DIRNAME, 0750));
120
121 37560
        heritage.vsm_fd = open(VSM_CHILD_DIRNAME, O_RDONLY);
122 37560
        assert(heritage.vsm_fd >= 0);
123 37560
        VJ_fix_fd(heritage.vsm_fd, JAIL_FIXFD_VSMWRK);
124 37560
        VJ_master(JAIL_MASTER_LOW);
125
126 37560
        MCH_Fd_Inherit(heritage.vsm_fd, "VSMW");
127
128 37560
        heritage.param = VSMW_Allocf(mgt_vsmw, NULL, VSM_CLASS_PARAM,
129
            sizeof *heritage.param, "");
130 37560
        AN(heritage.param);
131 37560
        *heritage.param = mgt_param;
132
133 37560
        heritage.panic_str_len = mgt_param.panic_buffer;
134 75120
        heritage.panic_str = VSMW_Allocf(mgt_vsmw, NULL, "Panic",
135 37560
            heritage.panic_str_len, "");
136 37560
        AN(heritage.panic_str);
137 37560
}
138
139
void
140 37560
mgt_SHM_ChildDestroy(void)
141
{
142
143 37560
        closefd(&heritage.vsm_fd);
144 37560
        if (!MGT_DO_DEBUG(DBG_VTC_MODE)) {
145 320
                VJ_master(JAIL_MASTER_SYSTEM);
146 320
                AZ(system("rm -rf " VSM_CHILD_DIRNAME));
147 320
                VJ_master(JAIL_MASTER_LOW);
148 320
        }
149 37560
        VSMW_Free(mgt_vsmw, (void**)&heritage.panic_str);
150 37560
        VSMW_Free(mgt_vsmw, (void**)&heritage.param);
151 37560
}