Commit 182f9daa authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Cleanup the -n argument to name/dir/shmfilename munging and put it in a

single sourcefile, which appears in both libvarnish and libvarnishapi.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@4759 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 1c0a014e
......@@ -42,7 +42,6 @@ varnishd_SOURCES = \
hash_classic.c \
hash_critbit.c \
hash_simple_list.c \
instance.c \
mgt_child.c \
mgt_cli.c \
mgt_param.c \
......
......@@ -62,7 +62,7 @@ struct heritage {
/* Hash method */
struct hash_slinger *hash;
char name[1024];
char *name;
char identity[1024];
};
......@@ -211,6 +211,3 @@ 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
......@@ -422,7 +422,7 @@ main(int argc, char * const *argv)
char *p, *vcl = NULL;
struct cli cli[1];
struct pidfh *pfh = NULL;
char dirname[1024];
char *dirname;
/*
* Start out by closing all unwanted file descriptors we might
......@@ -615,8 +615,7 @@ main(int argc, char * const *argv)
}
}
if (varnish_instance(n_arg, heritage.name, sizeof heritage.name,
dirname, sizeof dirname) != 0) {
if (vin_n_arg(n_arg, &heritage.name, &dirname, NULL) != 0) {
fprintf(stderr, "Invalid instance name: %s\n",
strerror(errno));
exit(1);
......
......@@ -109,4 +109,7 @@ enum shmlogtag {
SLT_WRAPMARKER = 255
};
/* This function lives in both libvarnish and libvarnishapi */
int vin_n_arg(const char *n_arg, char **name, char **dir, char **vsl);
#endif
......@@ -60,7 +60,4 @@ struct varnish_stats *VSL_OpenStats(const char *varnish_name);
const char *VSL_Name(void);
extern const char *VSL_tags[256];
/* instance.c */
int varnish_instance(const char *n_arg, char *name, size_t namelen, char *dir,
size_t dirlen);
#endif
......@@ -22,6 +22,7 @@ libvarnish_la_SOURCES = \
vct.c \
version.c \
vev.c \
vin.c \
vlu.c \
vpf.c \
vre.c \
......@@ -30,6 +31,7 @@ libvarnish_la_SOURCES = \
vss.c \
vtmpfile.c
libvarnish_la_CFLAGS = -DVARNISH_STATE_DIR='"${VARNISH_STATE_DIR}"'
libvarnish_la_LIBADD = ${RT_LIBS} ${NET_LIBS} ${LIBM} @PCRE_LIBS@
DISTCLEANFILES = svn_version.c
......
......@@ -24,44 +24,79 @@
* 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.
*
* XXX: NB: also used in libvarnishapi
*/
#include "config.h"
#include "svnid.h"
SVNID("$Id$")
SVNID("$Id: instance.c 4093 2009-06-06 15:56:17Z des $")
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "varnishapi.h"
#include "libvarnish.h"
#include "shmlog.h"
int
varnish_instance(const char *n_arg,
char *name, size_t namelen, char *dir, size_t dirlen)
vin_n_arg(const char *n_arg, char **name, char **dir, char **vsl)
{
size_t len;
char nm[PATH_MAX];
char dn[PATH_MAX];
/* First: determine the name */
if (n_arg == NULL) {
if (gethostname(name, namelen) != 0)
if (n_arg == NULL || *n_arg == '\0') {
if (gethostname(nm, sizeof nm) != 0)
return (-1);
} else if (strlen(n_arg) >= sizeof nm) {
/* preliminary length check to avoid overflowing nm */
errno = ENAMETOOLONG;
return (-1);
} else
bprintf(nm, "%s", n_arg);
/* Second: find the directory name */
if (*nm == '/')
strcpy(dn, nm);
else if (strlen(VARNISH_STATE_DIR) + 1 + strlen(nm) >= sizeof dn){
/* preliminary length check to avoid overflowing dm */
errno = ENAMETOOLONG;
return (-1);
} else {
len = snprintf(name, namelen, "%s", n_arg);
if (len >= namelen) {
errno = ENAMETOOLONG;
return (-1);
}
bprintf(dn, "%s/%s", VARNISH_STATE_DIR, nm);
}
if (*name == '/')
len = snprintf(dir, dirlen, "%s", name);
else
len = snprintf(dir, dirlen, "%s/%s", VARNISH_STATE_DIR, name);
if (len >= dirlen) {
/* Definitive length check */
if (strlen(dn) + 1 + strlen(SHMLOG_FILENAME) >= sizeof dn) {
errno = ENAMETOOLONG;
return (-1);
}
strcat(dn, "/");
if (name != NULL) {
*name = strdup(nm);
if (*name == NULL)
return (-1);
}
if (dir != NULL) {
*dir = strdup(dn);
if (*dir == NULL)
return (-1);
}
if (vsl != NULL) {
bprintf(nm, "%s%s", dn, SHMLOG_FILENAME);
*vsl = strdup(nm);
if (*vsl == NULL)
return (-1);
}
return (0);
}
......@@ -7,8 +7,8 @@ lib_LTLIBRARIES = libvarnishapi.la
libvarnishapi_la_LDFLAGS = -version-info 1:0:0
libvarnishapi_la_SOURCES = \
../libvarnish/vin.c \
base64.c \
instance.c \
shmlog.c
libvarnishapi_la_CFLAGS = \
......
......@@ -127,21 +127,17 @@ vsl_shmem_map(const char *varnish_name)
{
int i;
struct shmloghead slh;
char dirname[PATH_MAX], logname[PATH_MAX];
char *logname;
if (vsl_lh != NULL)
return (0);
if (varnish_instance(varnish_name, vsl_name,
sizeof vsl_name, dirname, sizeof dirname) != 0) {
if (vin_n_arg(varnish_name, NULL, NULL, &logname)) {
fprintf(stderr, "Invalid instance name: %s\n",
strerror(errno));
return (1);
}
/* 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",
......
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