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 68750
mgt_SHM_static_alloc(const void *ptr, ssize_t size,
57
    const char *category, const char *ident)
58
{
59
        void *p;
60
61 68750
        p = VSMW_Allocf(mgt_vsmw, NULL, category, size, "%s", ident);
62 68750
        AN(p);
63 68750
        memcpy(p, ptr, size);
64 68750
}
65
66
/*--------------------------------------------------------------------
67
 * Exit handler that clears the owning pid from the SHMLOG
68
 */
69
70
static void
71 109050
mgt_shm_atexit(void)
72
{
73
74
        /* Do not let VCC kill our VSM */
75 109050
        if (getpid() != heritage.mgt_pid)
76 86125
                return;
77 22925
        VJ_master(JAIL_MASTER_FILE);
78 22925
        VSMW_Destroy(&mgt_vsmw);
79 22925
        if (!MGT_DO_DEBUG(DBG_VTC_MODE)) {
80 300
                VJ_master(JAIL_MASTER_SYSTEM);
81 300
                AZ(system("rm -rf " VSM_MGT_DIRNAME));
82 300
                AZ(system("rm -rf " VSM_CHILD_DIRNAME));
83 300
        }
84 22925
        VJ_master(JAIL_MASTER_LOW);
85 109050
}
86
87
/*--------------------------------------------------------------------
88
 * Initialize VSM subsystem
89
 */
90
91
void
92 22925
mgt_SHM_Init(void)
93
{
94
        int fd;
95
96 22925
        VJ_master(JAIL_MASTER_SYSTEM);
97 22925
        AZ(system("rm -rf " VSM_MGT_DIRNAME));
98 22925
        VJ_master(JAIL_MASTER_FILE);
99 22925
        AZ(mkdir(VSM_MGT_DIRNAME, 0755));
100 22925
        fd = open(VSM_MGT_DIRNAME, O_RDONLY);
101 22925
        VJ_fix_fd(fd, JAIL_FIXFD_VSMMGT);
102 22925
        VJ_master(JAIL_MASTER_LOW);
103 22925
        mgt_vsmw = VSMW_New(fd, 0640, "_.index");
104 22925
        AN(mgt_vsmw);
105
106 22925
        heritage.proc_vsmw = mgt_vsmw;
107
108
        /* Setup atexit handler */
109 22925
        AZ(atexit(mgt_shm_atexit));
110 22925
}
111
112
void
113 22775
mgt_SHM_ChildNew(void)
114
{
115
116 22775
        VJ_master(JAIL_MASTER_SYSTEM);
117 22775
        AZ(system("rm -rf " VSM_CHILD_DIRNAME));
118 22775
        VJ_master(JAIL_MASTER_FILE);
119 22775
        AZ(mkdir(VSM_CHILD_DIRNAME, 0750));
120
121 22775
        heritage.vsm_fd = open(VSM_CHILD_DIRNAME, O_RDONLY);
122 22775
        assert(heritage.vsm_fd >= 0);
123 22775
        VJ_fix_fd(heritage.vsm_fd, JAIL_FIXFD_VSMWRK);
124 22775
        VJ_master(JAIL_MASTER_LOW);
125
126 22775
        MCH_Fd_Inherit(heritage.vsm_fd, "VSMW");
127
128 22775
        heritage.param = VSMW_Allocf(mgt_vsmw, NULL, VSM_CLASS_PARAM,
129
            sizeof *heritage.param, "");
130 22775
        AN(heritage.param);
131 22775
        *heritage.param = mgt_param;
132
133 22775
        heritage.panic_str_len = 64 * 1024;
134 45550
        heritage.panic_str = VSMW_Allocf(mgt_vsmw, NULL, "Panic",
135 22775
            heritage.panic_str_len, "");
136 22775
        AN(heritage.panic_str);
137 22775
}
138
139
void
140 22775
mgt_SHM_ChildDestroy(void)
141
{
142
143 22775
        closefd(&heritage.vsm_fd);
144 22775
        if (!MGT_DO_DEBUG(DBG_VTC_MODE)) {
145 200
                VJ_master(JAIL_MASTER_SYSTEM);
146 200
                AZ(system("rm -rf " VSM_CHILD_DIRNAME));
147 200
                VJ_master(JAIL_MASTER_LOW);
148 200
        }
149 22775
        VSMW_Free(mgt_vsmw, (void**)&heritage.panic_str);
150 22775
        VSMW_Free(mgt_vsmw, (void**)&heritage.param);
151 22775
}