Commit 16b7701e authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Split shmlog.c into more appropriately named vsl*[.ch] files



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@4804 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent dc84bde0
......@@ -9,7 +9,9 @@ libvarnishapi_la_LDFLAGS = -version-info 1:0:0
libvarnishapi_la_SOURCES = \
../libvarnish/vin.c \
base64.c \
shmlog.c
vsl.c \
vsl_arg.c \
vsl_log.c
libvarnishapi_la_CFLAGS = \
-DVARNISH_STATE_DIR='"${VARNISH_STATE_DIR}"'
/*-
* 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 <sys/types.h>
#include <sys/mman.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shmlog.h"
#include "vre.h"
#include "vbm.h"
#include "miniobj.h"
#include "varnishapi.h"
#include "vsl.h"
#ifndef MAP_HASSEMAPHORE
#define MAP_HASSEMAPHORE 0 /* XXX Linux */
#endif
/*--------------------------------------------------------------------*/
struct VSL_data *
VSL_New(void)
{
struct VSL_data *vd;
vd = calloc(sizeof *vd, 1);
assert(vd != NULL);
vd->regflags = 0;
vd->magic = VSL_MAGIC;
vd->vsl_fd = -1;
/* XXX: Allocate only if log access */
vd->vbm_client = vbit_init(4096);
vd->vbm_backend = vbit_init(4096);
vd->vbm_supress = vbit_init(256);
vd->vbm_select = vbit_init(256);
vd->r_fd = -1;
/* XXX: Allocate only if -r option given ? */
vd->rbuflen = SHMLOG_NEXTTAG + 256;
vd->rbuf = malloc(vd->rbuflen);
assert(vd->rbuf != NULL);
return (vd);
}
/*--------------------------------------------------------------------*/
void
VSL_Delete(struct VSL_data *vd)
{
VSL_Close(vd);
vbit_destroy(vd->vbm_client);
vbit_destroy(vd->vbm_backend);
vbit_destroy(vd->vbm_supress);
vbit_destroy(vd->vbm_select);
free(vd->n_opt);
free(vd->rbuf);
free(vd);
}
/*--------------------------------------------------------------------*/
int
VSL_Open(struct VSL_data *vd)
{
int i;
struct shmloghead slh;
char *logname;
if (vd->vsl_lh != NULL)
return (0);
if (vin_n_arg(vd->n_opt, NULL, NULL, &logname)) {
fprintf(stderr, "Invalid instance name: %s\n",
strerror(errno));
return (1);
}
vd->vsl_fd = open(logname, O_RDONLY);
if (vd->vsl_fd < 0) {
fprintf(stderr, "Cannot open %s: %s\n",
logname, strerror(errno));
return (1);
}
i = read(vd->vsl_fd, &slh, sizeof slh);
if (i != sizeof slh) {
fprintf(stderr, "Cannot read %s: %s\n",
logname, strerror(errno));
return (1);
}
if (slh.magic != SHMLOGHEAD_MAGIC) {
fprintf(stderr, "Wrong magic number in file %s\n",
logname);
return (1);
}
vd->vsl_lh = (void *)mmap(NULL, slh.size + sizeof slh,
PROT_READ, MAP_SHARED|MAP_HASSEMAPHORE, vd->vsl_fd, 0);
if (vd->vsl_lh == MAP_FAILED) {
fprintf(stderr, "Cannot mmap %s: %s\n",
logname, strerror(errno));
return (1);
}
return (0);
}
/*--------------------------------------------------------------------*/
void
VSL_Close(struct VSL_data *vd)
{
if (vd->vsl_lh == NULL)
return;
assert(0 == munmap((void*)vd->vsl_lh,
vd->vsl_lh->size + sizeof *vd->vsl_lh));
vd->vsl_lh = NULL;
assert(vd->vsl_fd >= 0);
assert(0 == close(vd->vsl_fd));
vd->vsl_fd = -1;
}
/*--------------------------------------------------------------------*/
static struct shmalloc *
vsl_find_alloc(struct VSL_data *vd, const char *type, const char *ident)
{
struct shmalloc *sha;
assert (vd->vsl_lh != NULL);
for(sha = &vd->vsl_lh->head; ; sha = SHA_NEXT(sha)) {
CHECK_OBJ_NOTNULL(sha, SHMALLOC_MAGIC);
if (strcmp(sha->type, type))
continue;
if (ident != NULL && strcmp(sha->ident, ident))
continue;
return (sha);
}
return (NULL);
}
/*--------------------------------------------------------------------*/
struct varnish_stats *
VSL_OpenStats(struct VSL_data *vd)
{
struct shmalloc *sha;
if (VSL_Open(vd))
return (NULL);
sha = vsl_find_alloc(vd, VSL_STAT_TYPE, "");
assert(sha != NULL);
return (SHA_PTR(sha));
}
/*--------------------------------------------------------------------*/
int
VSL_OpenLog(struct VSL_data *vd)
{
unsigned char *p;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (vd->r_fd != -1)
return (0);
if (VSL_Open(vd))
return (-1);
vd->head = vd->vsl_lh;
vd->logstart = (unsigned char *)vd->vsl_lh + vd->vsl_lh->start;
vd->logend = vd->logstart + vd->vsl_lh->size;
vd->ptr = vd->logstart;
if (!vd->d_opt && vd->r_fd == -1) {
for (p = vd->ptr; *p != SLT_ENDMARKER; )
p += SHMLOG_LEN(p) + SHMLOG_NEXTTAG;
vd->ptr = p;
}
return (0);
}
/*--------------------------------------------------------------------*/
const char *
VSL_Name(struct VSL_data *vd)
{
return (vd->n_opt);
}
/*-
* 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.
*
* $Id$
*/
/* Parameters */
#define SLEEP_USEC (50*1000)
#define TIMEOUT_USEC (5*1000*1000)
struct VSL_data {
unsigned magic;
#define VSL_MAGIC 0x6e3bd69b
struct shmloghead *head;
unsigned char *logstart;
unsigned char *logend;
unsigned char *ptr;
/* for -r option */
int r_fd;
unsigned rbuflen;
unsigned char *rbuf;
unsigned L_opt;
char *n_opt;
int b_opt;
int c_opt;
int d_opt;
unsigned flags;
#define F_SEEN_IX (1 << 0)
#define F_NON_BLOCKING (1 << 1)
/*
* These two bitmaps mark fd's as belonging to client or backend
* transactions respectively.
*/
struct vbitmap *vbm_client;
struct vbitmap *vbm_backend;
/*
* Bit map of programatically selected tags, that cannot be suppressed.
* This way programs can make sure they will see certain tags, even
* if the user tries to supress them with -x/-X
*/
struct vbitmap *vbm_select; /* index: tag */
/* Bit map of tags selected/supressed with -[iIxX] options */
struct vbitmap *vbm_supress; /* index: tag */
int regflags;
vre_t *regincl;
vre_t *regexcl;
unsigned long skip;
unsigned long keep;
int vsl_fd;
struct shmloghead *vsl_lh;
};
/*-
* 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 <sys/types.h>
#include <sys/mman.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shmlog.h"
#include "vre.h"
#include "vbm.h"
#include "miniobj.h"
#include "varnishapi.h"
#include "vsl.h"
#ifndef MAP_HASSEMAPHORE
#define MAP_HASSEMAPHORE 0 /* XXX Linux */
#endif
/*--------------------------------------------------------------------*/
static int
vsl_r_arg(struct VSL_data *vd, const char *opt)
{
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (!strcmp(opt, "-"))
vd->r_fd = STDIN_FILENO;
else
vd->r_fd = open(opt, O_RDONLY);
if (vd->r_fd < 0) {
perror(opt);
return (-1);
}
return (1);
}
/*--------------------------------------------------------------------*/
static int
vsl_IX_arg(struct VSL_data *vd, const char *opt, int arg)
{
vre_t **rp;
const char *error;
int erroroffset;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (arg == 'I')
rp = &vd->regincl;
else
rp = &vd->regexcl;
if (*rp != NULL) {
fprintf(stderr, "Option %c can only be given once", arg);
return (-1);
}
*rp = VRE_compile(opt, vd->regflags, &error, &erroroffset);
if (*rp == NULL) {
fprintf(stderr, "Illegal regex: %s\n", error);
return (-1);
}
return (1);
}
/*--------------------------------------------------------------------*/
static int
vsl_ix_arg(struct VSL_data *vd, const char *opt, int arg)
{
int i, j, l;
const char *b, *e, *p, *q;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
/* If first option is 'i', set all bits for supression */
if (arg == 'i' && !(vd->flags & F_SEEN_IX))
for (i = 0; i < 256; i++)
vbit_set(vd->vbm_supress, i);
vd->flags |= F_SEEN_IX;
for (b = opt; *b; b = e) {
while (isspace(*b))
b++;
e = strchr(b, ',');
if (e == NULL)
e = strchr(b, '\0');
l = e - b;
if (*e == ',')
e++;
while (isspace(b[l - 1]))
l--;
for (i = 0; i < 256; i++) {
if (VSL_tags[i] == NULL)
continue;
p = VSL_tags[i];
q = b;
for (j = 0; j < l; j++)
if (tolower(*q++) != tolower(*p++))
break;
if (j != l || *p != '\0')
continue;
if (arg == 'x')
vbit_set(vd->vbm_supress, i);
else
vbit_clr(vd->vbm_supress, i);
break;
}
if (i == 256) {
fprintf(stderr,
"Could not match \"%*.*s\" to any tag\n", l, l, b);
return (-1);
}
}
return (1);
}
/*--------------------------------------------------------------------*/
static int
vsl_s_arg(struct VSL_data *vd, const char *opt)
{
char *end;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (*opt == '\0') {
fprintf(stderr, "number required for -s\n");
return (-1);
}
vd->skip = strtoul(opt, &end, 10);
if (*end != '\0') {
fprintf(stderr, "invalid number for -s\n");
return (-1);
}
return (1);
}
/*--------------------------------------------------------------------*/
static int
vsl_k_arg(struct VSL_data *vd, const char *opt)
{
char *end;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (*opt == '\0') {
fprintf(stderr, "number required for -k\n");
return (-1);
}
vd->keep = strtoul(opt, &end, 10);
if (*end != '\0') {
fprintf(stderr, "invalid number for -k\n");
return (-1);
}
return (1);
}
/*--------------------------------------------------------------------*/
int
VSL_Arg(struct VSL_data *vd, int arg, const char *opt)
{
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
switch (arg) {
case 'b': vd->b_opt = !vd->b_opt; return (1);
case 'c': vd->c_opt = !vd->c_opt; return (1);
case 'd':
vd->d_opt = !vd->d_opt;
vd->flags |= F_NON_BLOCKING;
return (1);
case 'i': case 'x': return (vsl_ix_arg(vd, opt, arg));
case 'k': return (vsl_k_arg(vd, opt));
case 'n':
free(vd->n_opt);
vd->n_opt = strdup(opt);
assert(vd->n_opt != NULL);
return (1);
case 'r': return (vsl_r_arg(vd, opt));
case 's': return (vsl_s_arg(vd, opt));
case 'I': case 'X': return (vsl_IX_arg(vd, opt, arg));
case 'C': vd->regflags = VRE_CASELESS; return (1);
case 'L':
vd->L_opt = strtoul(opt, NULL, 0);
if (vd->L_opt < 1024 || vd->L_opt > 65000) {
fprintf(stderr, "%s\n", VIN_L_MSG);
exit (1);
}
free(vd->n_opt);
vd->n_opt = vin_L_arg(vd->L_opt);
assert(vd->n_opt != NULL);
return (1);
default:
return (0);
}
}
......@@ -51,61 +51,7 @@ SVNID("$Id$")
#include "miniobj.h"
#include "varnishapi.h"
/* Parameters */
#define SLEEP_USEC (50*1000)
#define TIMEOUT_USEC (5*1000*1000)
struct VSL_data {
unsigned magic;
#define VSL_MAGIC 0x6e3bd69b
struct shmloghead *head;
unsigned char *logstart;
unsigned char *logend;
unsigned char *ptr;
/* for -r option */
int r_fd;
unsigned rbuflen;
unsigned char *rbuf;
unsigned L_opt;
char *n_opt;
int b_opt;
int c_opt;
int d_opt;
unsigned flags;
#define F_SEEN_IX (1 << 0)
#define F_NON_BLOCKING (1 << 1)
/*
* These two bitmaps mark fd's as belonging to client or backend
* transactions respectively.
*/
struct vbitmap *vbm_client;
struct vbitmap *vbm_backend;
/*
* Bit map of programatically selected tags, that cannot be suppressed.
* This way programs can make sure they will see certain tags, even
* if the user tries to supress them with -x/-X
*/
struct vbitmap *vbm_select; /* index: tag */
/* Bit map of tags selected/supressed with -[iIxX] options */
struct vbitmap *vbm_supress; /* index: tag */
int regflags;
vre_t *regincl;
vre_t *regexcl;
unsigned long skip;
unsigned long keep;
int vsl_fd;
struct shmloghead *vsl_lh;
};
#include "vsl.h"
#ifndef MAP_HASSEMAPHORE
#define MAP_HASSEMAPHORE 0 /* XXX Linux */
......@@ -123,96 +69,6 @@ const char *VSL_tags[256] = {
/*--------------------------------------------------------------------*/
int
VSL_Open(struct VSL_data *vd)
{
int i;
struct shmloghead slh;
char *logname;
if (vd->vsl_lh != NULL)
return (0);
if (vin_n_arg(vd->n_opt, NULL, NULL, &logname)) {
fprintf(stderr, "Invalid instance name: %s\n",
strerror(errno));
return (1);
}
vd->vsl_fd = open(logname, O_RDONLY);
if (vd->vsl_fd < 0) {
fprintf(stderr, "Cannot open %s: %s\n",
logname, strerror(errno));
return (1);
}
i = read(vd->vsl_fd, &slh, sizeof slh);
if (i != sizeof slh) {
fprintf(stderr, "Cannot read %s: %s\n",
logname, strerror(errno));
return (1);
}
if (slh.magic != SHMLOGHEAD_MAGIC) {
fprintf(stderr, "Wrong magic number in file %s\n",
logname);
return (1);
}
vd->vsl_lh = (void *)mmap(NULL, slh.size + sizeof slh,
PROT_READ, MAP_SHARED|MAP_HASSEMAPHORE, vd->vsl_fd, 0);
if (vd->vsl_lh == MAP_FAILED) {
fprintf(stderr, "Cannot mmap %s: %s\n",
logname, strerror(errno));
return (1);
}
return (0);
}
/*--------------------------------------------------------------------*/
struct VSL_data *
VSL_New(void)
{
struct VSL_data *vd;
vd = calloc(sizeof *vd, 1);
assert(vd != NULL);
vd->regflags = 0;
vd->magic = VSL_MAGIC;
vd->vsl_fd = -1;
/* XXX: Allocate only if log access */
vd->vbm_client = vbit_init(4096);
vd->vbm_backend = vbit_init(4096);
vd->vbm_supress = vbit_init(256);
vd->vbm_select = vbit_init(256);
vd->r_fd = -1;
/* XXX: Allocate only if -r option given ? */
vd->rbuflen = SHMLOG_NEXTTAG + 256;
vd->rbuf = malloc(vd->rbuflen);
assert(vd->rbuf != NULL);
return (vd);
}
/*--------------------------------------------------------------------*/
void
VSL_Delete(struct VSL_data *vd)
{
VSL_Close(vd);
vbit_destroy(vd->vbm_client);
vbit_destroy(vd->vbm_backend);
vbit_destroy(vd->vbm_supress);
vbit_destroy(vd->vbm_select);
free(vd->n_opt);
free(vd->rbuf);
free(vd);
}
/*--------------------------------------------------------------------*/
void
VSL_Select(struct VSL_data *vd, unsigned tag)
{
......@@ -221,32 +77,6 @@ VSL_Select(struct VSL_data *vd, unsigned tag)
vbit_set(vd->vbm_select, tag);
}
/*--------------------------------------------------------------------*/
int
VSL_OpenLog(struct VSL_data *vd)
{
unsigned char *p;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (vd->r_fd != -1)
return (0);
if (VSL_Open(vd))
return (-1);
vd->head = vd->vsl_lh;
vd->logstart = (unsigned char *)vd->vsl_lh + vd->vsl_lh->start;
vd->logend = vd->logstart + vd->vsl_lh->size;
vd->ptr = vd->logstart;
if (!vd->d_opt && vd->r_fd == -1) {
for (p = vd->ptr; *p != SLT_ENDMARKER; )
p += SHMLOG_LEN(p) + SHMLOG_NEXTTAG;
vd->ptr = p;
}
return (0);
}
/*--------------------------------------------------------------------*/
......@@ -434,230 +264,3 @@ VSL_H_Print(void *priv, enum shmlogtag tag, unsigned fd, unsigned len,
fprintf(fo, "%5d %-12s %c %.*s\n", fd, VSL_tags[tag], type, len, ptr);
return (0);
}
/*--------------------------------------------------------------------*/
static int
vsl_r_arg(struct VSL_data *vd, const char *opt)
{
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (!strcmp(opt, "-"))
vd->r_fd = STDIN_FILENO;
else
vd->r_fd = open(opt, O_RDONLY);
if (vd->r_fd < 0) {
perror(opt);
return (-1);
}
return (1);
}
/*--------------------------------------------------------------------*/
static int
vsl_IX_arg(struct VSL_data *vd, const char *opt, int arg)
{
vre_t **rp;
const char *error;
int erroroffset;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (arg == 'I')
rp = &vd->regincl;
else
rp = &vd->regexcl;
if (*rp != NULL) {
fprintf(stderr, "Option %c can only be given once", arg);
return (-1);
}
*rp = VRE_compile(opt, vd->regflags, &error, &erroroffset);
if (*rp == NULL) {
fprintf(stderr, "Illegal regex: %s\n", error);
return (-1);
}
return (1);
}
/*--------------------------------------------------------------------*/
static int
vsl_ix_arg(struct VSL_data *vd, const char *opt, int arg)
{
int i, j, l;
const char *b, *e, *p, *q;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
/* If first option is 'i', set all bits for supression */
if (arg == 'i' && !(vd->flags & F_SEEN_IX))
for (i = 0; i < 256; i++)
vbit_set(vd->vbm_supress, i);
vd->flags |= F_SEEN_IX;
for (b = opt; *b; b = e) {
while (isspace(*b))
b++;
e = strchr(b, ',');
if (e == NULL)
e = strchr(b, '\0');
l = e - b;
if (*e == ',')
e++;
while (isspace(b[l - 1]))
l--;
for (i = 0; i < 256; i++) {
if (VSL_tags[i] == NULL)
continue;
p = VSL_tags[i];
q = b;
for (j = 0; j < l; j++)
if (tolower(*q++) != tolower(*p++))
break;
if (j != l || *p != '\0')
continue;
if (arg == 'x')
vbit_set(vd->vbm_supress, i);
else
vbit_clr(vd->vbm_supress, i);
break;
}
if (i == 256) {
fprintf(stderr,
"Could not match \"%*.*s\" to any tag\n", l, l, b);
return (-1);
}
}
return (1);
}
/*--------------------------------------------------------------------*/
static int
vsl_s_arg(struct VSL_data *vd, const char *opt)
{
char *end;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (*opt == '\0') {
fprintf(stderr, "number required for -s\n");
return (-1);
}
vd->skip = strtoul(opt, &end, 10);
if (*end != '\0') {
fprintf(stderr, "invalid number for -s\n");
return (-1);
}
return (1);
}
/*--------------------------------------------------------------------*/
static int
vsl_k_arg(struct VSL_data *vd, const char *opt)
{
char *end;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (*opt == '\0') {
fprintf(stderr, "number required for -k\n");
return (-1);
}
vd->keep = strtoul(opt, &end, 10);
if (*end != '\0') {
fprintf(stderr, "invalid number for -k\n");
return (-1);
}
return (1);
}
/*--------------------------------------------------------------------*/
int
VSL_Arg(struct VSL_data *vd, int arg, const char *opt)
{
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
switch (arg) {
case 'b': vd->b_opt = !vd->b_opt; return (1);
case 'c': vd->c_opt = !vd->c_opt; return (1);
case 'd':
vd->d_opt = !vd->d_opt;
vd->flags |= F_NON_BLOCKING;
return (1);
case 'i': case 'x': return (vsl_ix_arg(vd, opt, arg));
case 'k': return (vsl_k_arg(vd, opt));
case 'n':
free(vd->n_opt);
vd->n_opt = strdup(opt);
assert(vd->n_opt != NULL);
return (1);
case 'r': return (vsl_r_arg(vd, opt));
case 's': return (vsl_s_arg(vd, opt));
case 'I': case 'X': return (vsl_IX_arg(vd, opt, arg));
case 'C': vd->regflags = VRE_CASELESS; return (1);
case 'L':
vd->L_opt = strtoul(opt, NULL, 0);
if (vd->L_opt < 1024 || vd->L_opt > 65000) {
fprintf(stderr, "%s\n", VIN_L_MSG);
exit (1);
}
free(vd->n_opt);
vd->n_opt = vin_L_arg(vd->L_opt);
assert(vd->n_opt != NULL);
return (1);
default:
return (0);
}
}
/*--------------------------------------------------------------------*/
static
struct shmalloc *
vsl_find_alloc(struct VSL_data *vd, const char *type, const char *ident)
{
struct shmalloc *sha;
assert (vd->vsl_lh != NULL);
for(sha = &vd->vsl_lh->head; ; sha = SHA_NEXT(sha)) {
CHECK_OBJ_NOTNULL(sha, SHMALLOC_MAGIC);
if (strcmp(sha->type, type))
continue;
if (ident != NULL && strcmp(sha->ident, ident))
continue;
return (sha);
}
return (NULL);
}
struct varnish_stats *
VSL_OpenStats(struct VSL_data *vd)
{
struct shmalloc *sha;
if (VSL_Open(vd))
return (NULL);
sha = vsl_find_alloc(vd, VSL_STAT_TYPE, "");
assert(sha != NULL);
return (SHA_PTR(sha));
}
void
VSL_Close(struct VSL_data *vd)
{
if (vd->vsl_lh == NULL)
return;
assert(0 == munmap((void*)vd->vsl_lh,
vd->vsl_lh->size + sizeof *vd->vsl_lh));
vd->vsl_lh = NULL;
assert(vd->vsl_fd >= 0);
assert(0 == close(vd->vsl_fd));
vd->vsl_fd = -1;
}
const char *
VSL_Name(struct VSL_data *vd)
{
return (vd->n_opt);
}
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