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