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

Add shared memory log setup and stuffer function in the child process.

Log whenever we get a CLI ping



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@62 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 608d3f71
......@@ -7,6 +7,7 @@ bin_PROGRAMS = varnishd
varnishd_SOURCES = \
cache_acceptor.c \
cache_main.c \
cache_shmlog.c \
cli_event.c \
mgt_child.c \
tcp.c \
......
......@@ -12,3 +12,11 @@ struct sess {
/* cache_acceptor.c */
void *vca_main(void *arg);
/* cache_shmlog.c */
void VSL_Init(void);
#ifdef SHMLOGHEAD_MAGIC
void VSL(enum shmlogtag tag, unsigned id, const char *fmt, ...);
#endif
......@@ -17,6 +17,7 @@
#include "libvarnish.h"
#include "heritage.h"
#include "shmlog.h"
#include "cache.h"
#include "cli_event.h"
......@@ -62,6 +63,7 @@ cli_func_ping(struct cli *cli, char **av, void *priv __unused)
{
time_t t;
VSL(SLT_CLI, 0, av[1]);
arm_keepalive();
if (av[2] != NULL) {
/* XXX: check clock skew is pointless here */
......@@ -92,6 +94,8 @@ child_main(void)
setbuf(stderr, NULL);
printf("Child starts\n");
VSL_Init();
AZ(pthread_create(&vca_thread, NULL, vca_main, NULL));
eb = event_init();
......@@ -100,9 +104,10 @@ child_main(void)
cli = cli_setup(heritage.fds[2], heritage.fds[1], 0, cli_proto);
evtimer_set(&ev_keepalive, timer_keepalive, NULL);
event_base_set(eb, &ev_keepalive);
arm_keepalive();
i = event_dispatch();
i = event_base_loop(eb, 0);
if (i != 0)
printf("event_dispatch() = %d\n", i);
......
/*
* $Id$
*/
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include <sys/mman.h>
#include "shmlog.h"
#include "heritage.h"
static struct shmloghead *loghead;
static unsigned char *logstart, *logend;
void
VSL(enum shmlogtag tag, unsigned id, const char *fmt, ...)
{
va_list ap;
unsigned char *p, *q;
unsigned m, n;
va_start(ap, fmt);
/* XXX: Lock */
q = NULL;
p = logstart + loghead->ptr;
assert(p < logend);
/*
* Wrap early if we approach the end
* 32 is arbitraryly larger than minimum of 4.
*/
if (p + 32 > logend) {
q = p;
p = logstart;
*p = SLT_ENDMARKER;
}
n = 0;
if (fmt != NULL) {
while (1) {
m = logend - (p + 4);
if (m > 256)
m = 256;
n = vsnprintf((char *)p + 4, m, fmt, ap);
if (n >= 255)
n = 255; /* we truncate long fields */
if (n < m)
break;
/* wraparound */
assert(q == NULL);
q = p;
p = logstart;
*p = SLT_ENDMARKER;
continue; /* Try again */
}
}
p[1] = n;
p[2] = id >> 8;
p[3] = id & 0xff;
p[0] = tag;
if (q != NULL)
*q = SLT_WRAPMARKER;
loghead->ptr = (p + 4 + n) - logstart;
/* XXX: Unlock */
va_end(ap);
}
void
VSL_Init(void)
{
loghead = mmap(NULL, heritage.vsl_size,
PROT_READ|PROT_WRITE,
MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED,
heritage.vsl_fd, 0);
assert(loghead != MAP_FAILED);
/* XXX check sanity of loghead */
logstart = (unsigned char *)loghead + loghead->start;
logend = logstart + loghead->size;
}
......@@ -20,6 +20,10 @@ struct heritage {
#define HERITAGE_NSOCKS 2 /* IPv4 + IPv6 */
int sock_local[HERITAGE_NSOCKS];
int sock_remote[HERITAGE_NSOCKS];
/* Share memory log fd and size (incl header) */
int vsl_fd;
unsigned vsl_size;
};
extern struct heritage heritage;
......
......@@ -5,7 +5,7 @@
*/
#include <assert.h>
#include <err.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
......@@ -169,6 +169,39 @@ usage(void)
/*--------------------------------------------------------------------*/
#include "shmlog.h"
static void
init_vsl(const char *fn, unsigned size)
{
struct shmloghead slh;
int i;
heritage.vsl_fd = open(fn, O_RDWR | O_CREAT, 0600);
if (heritage.vsl_fd < 0) {
fprintf(stderr, "Could not open %s: %s\n",
fn, strerror(errno));
exit (1);
}
i = read(heritage.vsl_fd, &slh, sizeof slh);
if (i == sizeof slh && slh.magic == SHMLOGHEAD_MAGIC) {
/* XXX more checks */
heritage.vsl_size = slh.size + slh.start;
return;
}
slh.magic = SHMLOGHEAD_MAGIC;
slh.size = size;
slh.ptr = 0;
slh.start = sizeof slh;
AZ(lseek(heritage.vsl_fd, 0, SEEK_SET));
i = write(heritage.vsl_fd, &slh, sizeof slh);
assert(i == sizeof slh);
AZ(ftruncate(heritage.vsl_fd, sizeof slh + size));
heritage.vsl_size = slh.size + slh.start;
}
/*--------------------------------------------------------------------*/
/* for development purposes */
#include <printf.h>
......@@ -208,6 +241,8 @@ main(int argc, char *argv[])
*/
open_tcp(portnumber);
init_vsl("/tmp/_vsl", 1000);
testme();
......
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