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