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