Commit 9db443b9 authored by Dag Erling Smørgrav's avatar Dag Erling Smørgrav

Merged revisions 1853-1855 via svnmerge from

svn+ssh://projects.linpro.no/svn/varnish/trunk/varnish-cache

........
  r1853 | des | 2007-08-19 19:17:58 +0200 (Sun, 19 Aug 2007) | 2 lines
  
  Whitespace cleanup
........
  r1854 | des | 2007-08-19 19:26:45 +0200 (Sun, 19 Aug 2007) | 2 lines
  
  Improve style.
........
  r1855 | des | 2007-08-19 20:12:03 +0200 (Sun, 19 Aug 2007) | 4 lines
  
  Restructure mgt_cli_callback(), and add comments to make it clear what's
  going on.  Also take care of a bug where strchr() was used on a non-NUL-
  terminated buffer.  This fixes #134.
........


git-svn-id: http://www.varnish-cache.org/svn/branches/1.1@1856 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 01c8307d
...@@ -28,45 +28,36 @@ ...@@ -28,45 +28,36 @@
.\" .\"
.\" $Id$ .\" $Id$
.\" .\"
.Dd June 06, 2007 .Dd August 19, 2007
.Dt VARNISHADM 1 .Dt VARNISHADM 1
.Os .Os
.Sh NAME .Sh NAME
.Nm varnishadm .Nm varnishadm
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Fl T Ar address:port <command> .Fl T Ar address:port
.Cm Ar command
.Op Ar ...
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Nm .Nm
utility sends the given command to the utility sends the given command and arguments to the
.Xr varnishd 1 .Xr varnishd 1
instance at address:port and prints the results. 0 is returned on success, 1 instance at address:port and prints the results.
on failure.
.Pp .Pp
The following options are available: The following options are available:
.Bl -tag -width Fl .Bl -tag -width Fl
.It Fl T Ar address:port .It Fl T Ar address:port
Connect via telnet to this address and port. Connect to the management interface at the specified address and port.
.Sh EXAMPLES .El
The following command lists all available commands provided by the .Sh EXIT STATUS
management interface of The exit status of the
.Ed .Nm
.Xr varnishd 1 utility is zero if the command succeeded, and non-zero otherwise.
.Bd -literal -offset 4n
$ varnishadm -T 127.0.0.1:23 help
.Ed
.Sh SEE ALSO .Sh SEE ALSO
.Xr varnishd 1 , .Xr varnishd 1
.Xr varnishhist 1 ,
.Xr varnishncsa 1 ,
.Xr varnishstat 1 ,
.Xr varnishtop 1
.Sh HISTORY .Sh HISTORY
The The
.Nm .Nm
utility was developed by utility and this manual page were written by
.An Cecilie Fritzvold Aq cecilihf@linpro.no .
This manual page was written by
.An Cecilie Fritzvold Aq cecilihf@linpro.no . .An Cecilie Fritzvold Aq cecilihf@linpro.no .
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
* returned * returned
*/ */
static void static void
telnet_mgt(const char* T_arg, int argc, char* argv[]) telnet_mgt(const char *T_arg, int argc, char *argv[])
{ {
struct vss_addr **ta; struct vss_addr **ta;
char *addr, *port; char *addr, *port;
...@@ -62,7 +62,7 @@ telnet_mgt(const char* T_arg, int argc, char* argv[]) ...@@ -62,7 +62,7 @@ telnet_mgt(const char* T_arg, int argc, char* argv[])
free(addr); free(addr);
free(port); free(port);
if (n == 0) { if (n == 0) {
fprintf(stderr, "Could not open TELNET port\n"); fprintf(stderr, "Could not resolve '%s'\n", T_arg);
exit(2); exit(2);
} }
...@@ -121,36 +121,34 @@ telnet_mgt(const char* T_arg, int argc, char* argv[]) ...@@ -121,36 +121,34 @@ telnet_mgt(const char* T_arg, int argc, char* argv[])
static void static void
usage(void) usage(void)
{ {
fprintf(stderr, "usage: varnishadm -T address:port <command> \n"); fprintf(stderr,
"usage: varnishadm -T [address]:port command [...]\n");
exit(1); exit(1);
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int c; const char *T_arg = NULL;
const char *address = NULL; int opt;
int T_arg = 0;
if (argc < 2) while ((opt = getopt(argc, argv, "T:")) != -1) {
usage(); switch (opt) {
while ((c = getopt(argc, argv, "T:")) != -1) {
switch (c) {
case 'T': case 'T':
T_arg = 1; T_arg = optarg;
address = optarg;
break; break;
default: default:
usage(); usage();
} }
} }
if (T_arg) { argc -= optind;
if (optind == argc) argv += optind;
if (T_arg == NULL || argc < 1)
usage(); usage();
telnet_mgt(address, argc - optind, &argv[optind]);
} telnet_mgt(T_arg, argc, argv);
exit(0); exit(0);
} }
...@@ -284,8 +284,8 @@ struct cli_port { ...@@ -284,8 +284,8 @@ struct cli_port {
int fdo; int fdo;
int verbose; int verbose;
char *buf; char *buf;
unsigned nbuf; int nbuf; /* next free position in buf */
unsigned lbuf; int lbuf; /* length of buf */
struct cli cli[1]; struct cli cli[1];
char name[30]; char name[30];
}; };
...@@ -294,39 +294,53 @@ static int ...@@ -294,39 +294,53 @@ static int
mgt_cli_callback(struct ev *e, int what) mgt_cli_callback(struct ev *e, int what)
{ {
struct cli_port *cp; struct cli_port *cp;
char *p; char *p, *q;
int i; int i;
CAST_OBJ_NOTNULL(cp, e->priv, CLI_PORT_MAGIC); CAST_OBJ_NOTNULL(cp, e->priv, CLI_PORT_MAGIC);
while (!(what & (EV_ERR | EV_HUP))) { if (what & (EV_ERR | EV_HUP))
goto cli_close;
/* grow the buffer if it is full */
if (cp->nbuf == cp->lbuf) { if (cp->nbuf == cp->lbuf) {
cp->lbuf += cp->lbuf; cp->lbuf += cp->lbuf;
cp->buf = realloc(cp->buf, cp->lbuf); cp->buf = realloc(cp->buf, cp->lbuf);
XXXAN(cp->buf); XXXAN(cp->buf);
} }
/* read more data into the buffer */
i = read(cp->fdi, cp->buf + cp->nbuf, cp->lbuf - cp->nbuf); i = read(cp->fdi, cp->buf + cp->nbuf, cp->lbuf - cp->nbuf);
if (i <= 0) if (i <= 0)
break; goto cli_close;
cp->nbuf += i; cp->nbuf += i;
p = strchr(cp->buf, '\n');
if (p == NULL) for (p = q = cp->buf; q < cp->buf + cp->nbuf; ++q) {
return (0); if (*q != '\n')
*p = '\0'; continue;
fprintf(stderr, "CLI <%s>\n", cp->buf); /* got a newline == got a command */
*q = '\0';
vsb_clear(cp->cli->sb); vsb_clear(cp->cli->sb);
cli_dispatch(cp->cli, cli_proto, cp->buf); cli_dispatch(cp->cli, cli_proto, p);
vsb_finish(cp->cli->sb); vsb_finish(cp->cli->sb);
/* XXX: cp->verbose */
/* send the result back */
if (cli_writeres(cp->fdo, cp->cli)) if (cli_writeres(cp->fdo, cp->cli))
break; goto cli_close;
i = ++p - cp->buf;
assert(i <= cp->nbuf); /* ready for next command */
if (i < cp->nbuf) p = q + 1;
memcpy(cp->buf, p, cp->nbuf - i);
cp->nbuf -= i;
return (0);
} }
/* see if there's any data left in the buffer */
if (p != q) {
assert(q == cp->buf + cp->nbuf);
cp->nbuf -= (p - cp->buf);
memmove(cp->buf, p, cp->nbuf);
}
return (0);
cli_close:
vsb_delete(cp->cli->sb); vsb_delete(cp->cli->sb);
free(cp->buf); free(cp->buf);
close(cp->fdi); close(cp->fdi);
...@@ -402,7 +416,7 @@ mgt_cli_telnet(const char *T_arg) ...@@ -402,7 +416,7 @@ mgt_cli_telnet(const char *T_arg)
free(addr); free(addr);
free(port); free(port);
if (n == 0) { if (n == 0) {
fprintf(stderr, "Could not open TELNET port\n"); fprintf(stderr, "Could not open management port\n");
exit(2); exit(2);
} }
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
......
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