Commit 39be4882 authored by Dag Erling Smørgrav's avatar Dag Erling Smørgrav

Further refine -n handling. The argument to -n is now either an absolute

path, or a path relative to $localstatedir/varnish.  By default, it is set
to the host name, which results in $localstatedir/varnish/$hostname.

This logic is centralized in instance.c, which is compiled into both
varnishd and libvarnishapi, with prototypes in varnishapi.h and heritage.h.


git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@1581 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 917c96f5
......@@ -5,9 +5,12 @@ run the 'configure' script in the top-level directory, then run 'make'
and 'make install'.
If you obtained the sources directly from the Subversion repository,
you will need to run autogen.sh first to create the 'configure' script.
you will need to run autogen.sh first to create the configure script.
Additional 'configure' options of interest:
Varnish will store run-time state in $localstatedir/varnish; you may
want to tune this using configure's --localstatedir parameter.
Additional configure options of interest:
--enable-developer-warnings
enable strict warnings (default is NO)
......
......@@ -34,6 +34,7 @@ varnishd_SOURCES = \
cache_ws.c \
hash_simple_list.c \
hash_classic.c \
instance.c \
mgt_child.c \
mgt_cli.c \
mgt_event.c \
......
......@@ -63,7 +63,7 @@ struct heritage {
/* Hash method */
struct hash_slinger *hash;
const char *n_arg;
char name[1024];
};
struct params {
......@@ -128,3 +128,5 @@ extern volatile struct params *params;
extern struct heritage heritage;
void child_main(void);
int varnish_instance(const char *n_arg, char *name, size_t namelen, char *dir, size_t dirlen);
../../lib/libvarnishapi/instance.c
\ No newline at end of file
......@@ -204,7 +204,7 @@ start_child(void)
AZ(close(heritage.fds[0]));
AZ(close(heritage.fds[3]));
setproctitle("Varnish-Chld %s", heritage.n_arg);
setproctitle("Varnish-Chld %s", heritage.name);
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
......@@ -408,7 +408,7 @@ mgt_run(int dflag, const char *T_arg)
e->name = "mgt_sigchild";
AZ(ev_add(mgt_evb, e));
setproctitle("Varnish-Mgr %s", heritage.n_arg);
setproctitle("Varnish-Mgr %s", heritage.name);
sac.sa_handler = SIG_IGN;
sac.sa_flags = SA_RESTART;
......
......@@ -28,7 +28,7 @@
.\"
.\" $Id$
.\"
.Dd June 15, 2007
.Dd June 26, 2007
.Dt VARNISHD 1
.Os
.Sh NAME
......@@ -130,9 +130,13 @@ See
for a list of supported algorithms.
.It Fl n
Specify a name for this instance.
This is a shortcut for specifying the
.Va name
run-time parameter.
Amonst other things, this name is used to construct the name of the
directory in which
.Nm
keeps temporary files and persistent state.
If the specified name begins with a forward slash, it is interpreted
as the absolute path to the directory which should be used for this
purpose.
.It Fl P Ar file
Write the process's PID to the specified
.Ar file .
......@@ -402,14 +406,6 @@ The default is 0.0.0.0:80.
The depth of the TCP listen queue.
.Pp
The default is 512.
.It Va name
The name of this
.Nm
instance.
All temporary files are stored in
.Pa /tmp/ Ns Va name .
.Pp
The default is the hostname.
.It Va overflow_max
The maximum depth of the overflow queue as a percentage of
.Va thread_pool_max .
......
......@@ -50,6 +50,10 @@
#include "compat/daemon.h"
#endif
#ifndef HAVE_STRLCPY
#include "compat/strlcpy.h"
#endif
#include "vsb.h"
#include "vpf.h"
......@@ -409,13 +413,14 @@ main(int argc, char *argv[])
const char *f_arg = NULL;
int f_fd = -1;
const char *h_arg = "classic";
const char *n_arg = "/tmp";
const char *n_arg = NULL;
const char *P_arg = NULL;
const char *s_arg = "file";
const char *T_arg = NULL;
char *p;
struct cli cli[1];
struct pidfh *pfh = NULL;
char dirname[1024];
setbuf(stdout, NULL);
setbuf(stderr, NULL);
......@@ -524,7 +529,7 @@ main(int argc, char *argv[])
fprintf(stderr, "One of -b or -f must be specified\n");
usage();
}
if (f_arg != NULL) {
f_fd = open(f_arg, O_RDONLY);
if (f_fd < 0) {
......@@ -534,20 +539,25 @@ main(int argc, char *argv[])
}
}
if (mkdir(n_arg, 0755) < 0 && errno != EEXIST) {
if (varnish_instance(n_arg, heritage.name, sizeof heritage.name,
dirname, sizeof dirname) != 0) {
fprintf(stderr, "Invalid instance name: %s\n",
strerror(errno));
exit(1);
}
if (mkdir(dirname, 0755) < 0 && errno != EEXIST) {
fprintf(stderr, "Cannot create working directory '%s': %s\n",
n_arg, strerror(errno));
dirname, strerror(errno));
exit(1);
}
if (chdir(n_arg) < 0) {
if (chdir(dirname) < 0) {
fprintf(stderr, "Cannot change to working directory '%s': %s\n",
n_arg, strerror(errno));
dirname, strerror(errno));
exit(1);
}
heritage.n_arg = n_arg;
/* XXX: should this be relative to the -n arg ? */
if (P_arg && (pfh = vpf_open(P_arg, 0600, NULL)) == NULL) {
perror(P_arg);
......
......@@ -102,6 +102,11 @@ AC_CHECK_FUNCS([kqueue])
AC_CHECK_FUNCS([epoll_ctl])
AC_CHECK_FUNCS([poll])
# Run-time directory
VARNISH_STATE_DIR=`eval "echo $localstatedir/varnish"`
AC_DEFINE_UNQUOTED(VARNISH_STATE_DIR, "$VARNISH_STATE_DIR",
[Base directory for run-time state])
# Now that we're done using the compiler to look for functions and
# libraries, set CFLAGS to what we want them to be for our own code
......
......@@ -58,21 +58,7 @@ int VSL_Arg(struct VSL_data *vd, int arg, const char *opt);
struct varnish_stats *VSL_OpenStats(const char *varnish_name);
extern const char *VSL_tags[256];
/* varnish_debug.c */
void vdb_panic(const char *, ...) V_DEAD;
/* varnish_log.c */
typedef struct vlo_buffer vlo_buffer_t;
vlo_buffer_t *vlo_open(const char *, size_t, int);
ssize_t vlo_write(vlo_buffer_t *, const void *, size_t);
vlo_buffer_t *vlo_attach(const char *);
ssize_t vlo_read(vlo_buffer_t *, const void *, size_t);
#if 0
uuid_t vlo_get_uuid(vlo_buffer_t *);
#endif
int vlo_close(vlo_buffer_t *);
/* varnish_util.c */
int vut_open_lock(const char *, int, int, int);
/* instance.c */
int varnish_instance(const char *n_arg, char *name, size_t namelen, char *dir, size_t dirlen);
#endif
......@@ -6,6 +6,7 @@ lib_LTLIBRARIES = libvarnishapi.la
libvarnishapi_la_SOURCES = \
base64.c \
instance.c \
shmlog.c
libvarnishapi_la_CFLAGS = -include config.h
/*-
* Copyright (c) 2007 Linpro AS
* All rights reserved.
*
* Author: Dag-Erling Smørgrav <des@linpro.no>
*
* 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$
*/
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include "varnishapi.h"
int
varnish_instance(const char *n_arg,
char *name, size_t namelen,
char *dir, size_t dirlen)
{
size_t len;
if (n_arg == NULL) {
if (gethostname(name, namelen) != 0)
return (-1);
} else {
len = snprintf(name, namelen, "%s", n_arg);
if (len >= namelen) {
errno = ENAMETOOLONG;
return (-1);
}
}
if (*name == '/')
len = snprintf(dir, dirlen, "%s", name);
else
len = snprintf(dir, dirlen, "%s/%s", VARNISH_STATE_DIR, name);
if (len >= dirlen) {
errno = ENAMETOOLONG;
return (-1);
}
return (0);
}
......@@ -29,16 +29,18 @@
* $Id$
*/
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <regex.h>
#include <sys/mman.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shmlog.h"
#include "miniobj.h"
......@@ -106,28 +108,36 @@ vsl_shmem_map(const char *varnish_name)
{
int i;
struct shmloghead slh;
char buf[BUFSIZ];
char name[PATH_MAX], dirname[PATH_MAX], logname[PATH_MAX];
if (vsl_lh != NULL)
return (0);
sprintf(buf, "/tmp/%s/%s", varnish_name, SHMLOG_FILENAME);
if (varnish_instance(varnish_name, name,
sizeof name, dirname, sizeof dirname) != 0) {
fprintf(stderr, "Invalid instance name: %s\n",
strerror(errno));
return (1);
}
vsl_fd = open(buf, O_RDONLY);
/* XXX check overflow */
snprintf(logname, sizeof logname, "%s/%s", dirname, SHMLOG_FILENAME);
vsl_fd = open(logname, O_RDONLY);
if (vsl_fd < 0) {
fprintf(stderr, "Cannot open %s: %s\n",
buf, strerror(errno));
logname, strerror(errno));
return (1);
}
i = read(vsl_fd, &slh, sizeof slh);
if (i != sizeof slh) {
fprintf(stderr, "Cannot read %s: %s\n",
buf, strerror(errno));
logname, strerror(errno));
return (1);
}
if (slh.magic != SHMLOGHEAD_MAGIC) {
fprintf(stderr, "Wrong magic number in file %s\n",
buf);
logname);
return (1);
}
......@@ -135,7 +145,7 @@ vsl_shmem_map(const char *varnish_name)
PROT_READ, MAP_SHARED|MAP_HASSEMAPHORE, vsl_fd, 0);
if (vsl_lh == MAP_FAILED) {
fprintf(stderr, "Cannot mmap %s: %s\n",
buf, strerror(errno));
logname, strerror(errno));
return (1);
}
return (0);
......@@ -172,17 +182,12 @@ VSL_Select(struct VSL_data *vd, unsigned tag)
int
VSL_OpenLog(struct VSL_data *vd, const char *varnish_name)
{
char hostname[1024];
unsigned char *p;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (vd->fi != NULL)
return (0);
if (varnish_name == NULL) {
gethostname(hostname, sizeof hostname);
varnish_name = hostname;
}
if (vsl_shmem_map(varnish_name))
return (-1);
......
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