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