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 = \ ...@@ -42,7 +42,6 @@ varnishd_SOURCES = \
hash_classic.c \ hash_classic.c \
hash_critbit.c \ hash_critbit.c \
hash_simple_list.c \ hash_simple_list.c \
instance.c \
mgt_child.c \ mgt_child.c \
mgt_cli.c \ mgt_cli.c \
mgt_param.c \ mgt_param.c \
......
...@@ -62,7 +62,7 @@ struct heritage { ...@@ -62,7 +62,7 @@ struct heritage {
/* Hash method */ /* Hash method */
struct hash_slinger *hash; struct hash_slinger *hash;
char name[1024]; char *name;
char identity[1024]; char identity[1024];
}; };
...@@ -211,6 +211,3 @@ extern volatile struct params *params; ...@@ -211,6 +211,3 @@ extern volatile struct params *params;
extern struct heritage heritage; extern struct heritage heritage;
void child_main(void); 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) ...@@ -422,7 +422,7 @@ main(int argc, char * const *argv)
char *p, *vcl = NULL; char *p, *vcl = NULL;
struct cli cli[1]; struct cli cli[1];
struct pidfh *pfh = NULL; struct pidfh *pfh = NULL;
char dirname[1024]; char *dirname;
/* /*
* Start out by closing all unwanted file descriptors we might * Start out by closing all unwanted file descriptors we might
...@@ -615,8 +615,7 @@ main(int argc, char * const *argv) ...@@ -615,8 +615,7 @@ main(int argc, char * const *argv)
} }
} }
if (varnish_instance(n_arg, heritage.name, sizeof heritage.name, if (vin_n_arg(n_arg, &heritage.name, &dirname, NULL) != 0) {
dirname, sizeof dirname) != 0) {
fprintf(stderr, "Invalid instance name: %s\n", fprintf(stderr, "Invalid instance name: %s\n",
strerror(errno)); strerror(errno));
exit(1); exit(1);
......
...@@ -109,4 +109,7 @@ enum shmlogtag { ...@@ -109,4 +109,7 @@ enum shmlogtag {
SLT_WRAPMARKER = 255 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 #endif
...@@ -60,7 +60,4 @@ struct varnish_stats *VSL_OpenStats(const char *varnish_name); ...@@ -60,7 +60,4 @@ struct varnish_stats *VSL_OpenStats(const char *varnish_name);
const char *VSL_Name(void); const char *VSL_Name(void);
extern const char *VSL_tags[256]; 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 #endif
...@@ -22,6 +22,7 @@ libvarnish_la_SOURCES = \ ...@@ -22,6 +22,7 @@ libvarnish_la_SOURCES = \
vct.c \ vct.c \
version.c \ version.c \
vev.c \ vev.c \
vin.c \
vlu.c \ vlu.c \
vpf.c \ vpf.c \
vre.c \ vre.c \
...@@ -30,6 +31,7 @@ libvarnish_la_SOURCES = \ ...@@ -30,6 +31,7 @@ libvarnish_la_SOURCES = \
vss.c \ vss.c \
vtmpfile.c vtmpfile.c
libvarnish_la_CFLAGS = -DVARNISH_STATE_DIR='"${VARNISH_STATE_DIR}"'
libvarnish_la_LIBADD = ${RT_LIBS} ${NET_LIBS} ${LIBM} @PCRE_LIBS@ libvarnish_la_LIBADD = ${RT_LIBS} ${NET_LIBS} ${LIBM} @PCRE_LIBS@
DISTCLEANFILES = svn_version.c DISTCLEANFILES = svn_version.c
......
...@@ -24,44 +24,79 @@ ...@@ -24,44 +24,79 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * 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 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*
* XXX: NB: also used in libvarnishapi
*/ */
#include "config.h" #include "config.h"
#include "svnid.h" #include "svnid.h"
SVNID("$Id$") SVNID("$Id: instance.c 4093 2009-06-06 15:56:17Z des $")
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "varnishapi.h" #include "libvarnish.h"
#include "shmlog.h"
int int
varnish_instance(const char *n_arg, vin_n_arg(const char *n_arg, char **name, char **dir, char **vsl)
char *name, size_t namelen, char *dir, size_t dirlen)
{ {
size_t len; char nm[PATH_MAX];
char dn[PATH_MAX];
/* First: determine the name */
if (n_arg == NULL) { if (n_arg == NULL || *n_arg == '\0') {
if (gethostname(name, namelen) != 0) if (gethostname(nm, sizeof nm) != 0)
return (-1); 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 { } else {
len = snprintf(name, namelen, "%s", n_arg); bprintf(dn, "%s/%s", VARNISH_STATE_DIR, nm);
if (len >= namelen) {
errno = ENAMETOOLONG;
return (-1);
}
} }
if (*name == '/') /* Definitive length check */
len = snprintf(dir, dirlen, "%s", name); if (strlen(dn) + 1 + strlen(SHMLOG_FILENAME) >= sizeof dn) {
else
len = snprintf(dir, dirlen, "%s/%s", VARNISH_STATE_DIR, name);
if (len >= dirlen) {
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
return (-1); 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); return (0);
} }
...@@ -7,8 +7,8 @@ lib_LTLIBRARIES = libvarnishapi.la ...@@ -7,8 +7,8 @@ lib_LTLIBRARIES = libvarnishapi.la
libvarnishapi_la_LDFLAGS = -version-info 1:0:0 libvarnishapi_la_LDFLAGS = -version-info 1:0:0
libvarnishapi_la_SOURCES = \ libvarnishapi_la_SOURCES = \
../libvarnish/vin.c \
base64.c \ base64.c \
instance.c \
shmlog.c shmlog.c
libvarnishapi_la_CFLAGS = \ libvarnishapi_la_CFLAGS = \
......
...@@ -127,21 +127,17 @@ vsl_shmem_map(const char *varnish_name) ...@@ -127,21 +127,17 @@ vsl_shmem_map(const char *varnish_name)
{ {
int i; int i;
struct shmloghead slh; struct shmloghead slh;
char dirname[PATH_MAX], logname[PATH_MAX]; char *logname;
if (vsl_lh != NULL) if (vsl_lh != NULL)
return (0); return (0);
if (varnish_instance(varnish_name, vsl_name, if (vin_n_arg(varnish_name, NULL, NULL, &logname)) {
sizeof vsl_name, dirname, sizeof dirname) != 0) {
fprintf(stderr, "Invalid instance name: %s\n", fprintf(stderr, "Invalid instance name: %s\n",
strerror(errno)); strerror(errno));
return (1); return (1);
} }
/* XXX check overflow */
snprintf(logname, sizeof logname, "%s/%s", dirname, SHMLOG_FILENAME);
vsl_fd = open(logname, O_RDONLY); vsl_fd = open(logname, O_RDONLY);
if (vsl_fd < 0) { if (vsl_fd < 0) {
fprintf(stderr, "Cannot open %s: %s\n", 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