Commit a4a69df9 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Split shmlog.c into mgt_shmem.c and cache_shmlog.c



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@4794 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 59cc6195
......@@ -33,6 +33,7 @@ varnishd_SOURCES = \
cache_pool.c \
cache_response.c \
cache_session.c \
cache_shmlog.c \
cache_vary.c \
cache_vcl.c \
cache_vrt.c \
......@@ -46,9 +47,9 @@ varnishd_SOURCES = \
mgt_cli.c \
mgt_param.c \
mgt_pool.c \
mgt_shmem.c \
mgt_vcc.c \
rfc2616.c \
shmlog.c \
stevedore.c \
storage_file.c \
storage_malloc.c \
......
......@@ -32,19 +32,15 @@
#include "svnid.h"
SVNID("$Id$")
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
#include <sys/mman.h>
#include "shmlog.h"
#include "cache.h"
static pthread_mutex_t vsl_mtx;
#define LOCKSHM(foo) \
do { \
if (pthread_mutex_trylock(foo)) { \
......@@ -55,20 +51,6 @@ SVNID("$Id$")
#define UNLOCKSHM(foo) AZ(pthread_mutex_unlock(foo))
#ifndef MAP_HASSEMAPHORE
#define MAP_HASSEMAPHORE 0 /* XXX Linux */
#endif
#ifndef MAP_NOSYNC
#define MAP_NOSYNC 0 /* XXX Linux */
#endif
struct varnish_stats *VSL_stats;
static struct shmloghead *loghead;
static unsigned char *logstart;
static pthread_mutex_t vsl_mtx;
static void
vsl_wrap(void)
{
......@@ -316,107 +298,3 @@ VSL_Init(void)
memset(VSL_stats, 0, sizeof *VSL_stats);
loghead->child_pid = getpid();
}
/*--------------------------------------------------------------------*/
static int
vsl_goodold(int fd)
{
struct shmloghead slh;
int i;
memset(&slh, 0, sizeof slh); /* XXX: for flexelint */
i = read(fd, &slh, sizeof slh);
if (i != sizeof slh)
return (0);
if (slh.magic != SHMLOGHEAD_MAGIC)
return (0);
if (slh.hdrsize != sizeof slh)
return (0);
if (slh.start != sizeof slh + sizeof *params)
return (0);
if (slh.master_pid != 0 && !kill(slh.master_pid, 0)) {
fprintf(stderr,
"SHMFILE owned by running varnishd master (pid=%jd)\n",
(intmax_t)slh.master_pid);
fprintf(stderr,
"(Use unique -n arguments if you want multiple "
"instances.)\n");
exit(2);
}
if (slh.child_pid != 0 && !kill(slh.child_pid, 0)) {
fprintf(stderr,
"SHMFILE used by orphan varnishd child process (pid=%jd)\n",
(intmax_t)slh.child_pid);
fprintf(stderr, "(We assume that process is busy dying.)\n");
return (0);
}
/* XXX more checks */
heritage.vsl_size = slh.size + slh.start;
return (1);
}
static void
vsl_buildnew(const char *fn, unsigned size)
{
struct shmloghead slh;
int i;
(void)unlink(fn);
heritage.vsl_fd = open(fn, O_RDWR | O_CREAT | O_EXCL, 0644);
if (heritage.vsl_fd < 0) {
fprintf(stderr, "Could not open %s: %s\n",
fn, strerror(errno));
exit (1);
}
memset(&slh, 0, sizeof slh);
slh.magic = SHMLOGHEAD_MAGIC;
slh.hdrsize = sizeof slh;
slh.size = size;
slh.ptr = 0;
slh.start = sizeof slh + sizeof *params;
i = write(heritage.vsl_fd, &slh, sizeof slh);
xxxassert(i == sizeof slh);
heritage.vsl_size = slh.start + size;
AZ(ftruncate(heritage.vsl_fd, (off_t)heritage.vsl_size));
}
void
VSL_MgtInit(const char *fn, unsigned size)
{
int i;
struct params *pp;
i = open(fn, O_RDWR, 0644);
if (i >= 0 && vsl_goodold(i)) {
fprintf(stderr, "Using old SHMFILE\n");
heritage.vsl_fd = i;
} else {
fprintf(stderr, "Creating new SHMFILE\n");
(void)close(i);
vsl_buildnew(fn, size);
}
loghead = (void *)mmap(NULL, heritage.vsl_size,
PROT_READ|PROT_WRITE,
MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED,
heritage.vsl_fd, 0);
loghead->master_pid = getpid();
xxxassert(loghead != MAP_FAILED);
(void)mlock((void*)loghead, heritage.vsl_size);
VSL_stats = &loghead->stats;
pp = (void *)(loghead + 1);
*pp = *params;
params = pp;
}
void
VSL_MgtPid(void)
{
loghead->master_pid = getpid();
}
......@@ -41,10 +41,12 @@ void VCA_tweak_waiter(struct cli *cli, const char *arg);
/* shmlog.c */
void VSL_Panic(int *len, char **ptr);
/* shmlog.c */
/* mgt_shmem.c */
void VSL_MgtInit(const char *fn, unsigned size);
void VSL_MgtPid(void);
extern struct varnish_stats *VSL_stats;
extern struct shmloghead *loghead;
extern unsigned char *logstart;
/* varnishd.c */
struct vsb;
......
/*-
* Copyright (c) 2006 Verdens Gang AS
* Copyright (c) 2006-2010 Redpill Linpro AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include "svnid.h"
SVNID("$Id$")
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include "shmlog.h"
#include "mgt.h"
#include "heritage.h"
#ifndef MAP_HASSEMAPHORE
#define MAP_HASSEMAPHORE 0 /* XXX Linux */
#endif
#ifndef MAP_NOSYNC
#define MAP_NOSYNC 0 /* XXX Linux */
#endif
struct varnish_stats *VSL_stats;
struct shmloghead *loghead;
unsigned char *logstart;
/*--------------------------------------------------------------------*/
static int
vsl_goodold(int fd)
{
struct shmloghead slh;
int i;
memset(&slh, 0, sizeof slh); /* XXX: for flexelint */
i = read(fd, &slh, sizeof slh);
if (i != sizeof slh)
return (0);
if (slh.magic != SHMLOGHEAD_MAGIC)
return (0);
if (slh.hdrsize != sizeof slh)
return (0);
if (slh.start != sizeof slh + sizeof *params)
return (0);
if (slh.master_pid != 0 && !kill(slh.master_pid, 0)) {
fprintf(stderr,
"SHMFILE owned by running varnishd master (pid=%jd)\n",
(intmax_t)slh.master_pid);
fprintf(stderr,
"(Use unique -n arguments if you want multiple "
"instances.)\n");
exit(2);
}
if (slh.child_pid != 0 && !kill(slh.child_pid, 0)) {
fprintf(stderr,
"SHMFILE used by orphan varnishd child process (pid=%jd)\n",
(intmax_t)slh.child_pid);
fprintf(stderr, "(We assume that process is busy dying.)\n");
return (0);
}
/* XXX more checks */
heritage.vsl_size = slh.size + slh.start;
return (1);
}
static void
vsl_buildnew(const char *fn, unsigned size)
{
struct shmloghead slh;
int i;
(void)unlink(fn);
heritage.vsl_fd = open(fn, O_RDWR | O_CREAT | O_EXCL, 0644);
if (heritage.vsl_fd < 0) {
fprintf(stderr, "Could not open %s: %s\n",
fn, strerror(errno));
exit (1);
}
memset(&slh, 0, sizeof slh);
slh.magic = SHMLOGHEAD_MAGIC;
slh.hdrsize = sizeof slh;
slh.size = size;
slh.ptr = 0;
slh.start = sizeof slh + sizeof *params;
i = write(heritage.vsl_fd, &slh, sizeof slh);
xxxassert(i == sizeof slh);
heritage.vsl_size = slh.start + size;
AZ(ftruncate(heritage.vsl_fd, (off_t)heritage.vsl_size));
}
void
VSL_MgtInit(const char *fn, unsigned size)
{
int i;
struct params *pp;
i = open(fn, O_RDWR, 0644);
if (i >= 0 && vsl_goodold(i)) {
fprintf(stderr, "Using old SHMFILE\n");
heritage.vsl_fd = i;
} else {
fprintf(stderr, "Creating new SHMFILE\n");
(void)close(i);
vsl_buildnew(fn, size);
}
loghead = (void *)mmap(NULL, heritage.vsl_size,
PROT_READ|PROT_WRITE,
MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED,
heritage.vsl_fd, 0);
loghead->master_pid = getpid();
xxxassert(loghead != MAP_FAILED);
(void)mlock((void*)loghead, heritage.vsl_size);
VSL_stats = &loghead->stats;
pp = (void *)(loghead + 1);
*pp = *params;
params = pp;
}
void
VSL_MgtPid(void)
{
loghead->master_pid = getpid();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment