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