Mailing List Archive

r1000 - trunk/varnish-cache/bin/varnishd
Author: phk
Date: 2006-09-16 11:01:21 +0200 (Sat, 16 Sep 2006)
New Revision: 1000

Modified:
trunk/varnish-cache/bin/varnishd/common.h
trunk/varnish-cache/bin/varnishd/mgt.h
trunk/varnish-cache/bin/varnishd/mgt_param.c
trunk/varnish-cache/bin/varnishd/tcp.c
trunk/varnish-cache/bin/varnishd/varnishd.c
Log:
Generalize parameter setting at start up.

Give the managers main a struct cli and use that to call into
the paramter stuff for setting listen address. More params to follow.

When setting the listen address, check that getaddrinfo() doesn't
hate it.



Modified: trunk/varnish-cache/bin/varnishd/common.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/common.h 2006-09-16 08:20:39 UTC (rev 999)
+++ trunk/varnish-cache/bin/varnishd/common.h 2006-09-16 09:01:21 UTC (rev 1000)
@@ -2,6 +2,7 @@
* $Id$
*/

+struct cli;
struct sockaddr;

/* shmlog.c */
@@ -17,3 +18,5 @@
void TCP_myname(int sock, char *abuf, unsigned alen, char *pbuf, unsigned plen);
int TCP_parse(const char *str, char **addr, char **port);
int TCP_open(const char *addr, const char *port, int http);
+void TCP_check(struct cli *cli, const char *addr, const char *port);
+

Modified: trunk/varnish-cache/bin/varnishd/mgt.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt.h 2006-09-16 08:20:39 UTC (rev 999)
+++ trunk/varnish-cache/bin/varnishd/mgt.h 2006-09-16 09:01:21 UTC (rev 1000)
@@ -7,6 +7,8 @@

#include "libvarnish.h"

+struct cli;
+
extern struct evbase *mgt_evb;

/* mgt_child.c */
@@ -23,7 +25,8 @@
int mgt_cli_telnet(const char *T_arg);

/* mgt_param.c */
-void MCF_ParamInit(void);
+void MCF_ParamInit(struct cli *);
+void MCF_ParamSet(struct cli *, const char *param, const char *val);

/* mgt_vcc.c */
void mgt_vcc_init(void);

Modified: trunk/varnish-cache/bin/varnishd/mgt_param.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt_param.c 2006-09-16 08:20:39 UTC (rev 999)
+++ trunk/varnish-cache/bin/varnishd/mgt_param.c 2006-09-16 09:01:21 UTC (rev 1000)
@@ -2,6 +2,7 @@
* $Id$
*/

+#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
@@ -9,9 +10,11 @@

#include "cli.h"
#include "cli_priv.h"
+#include "cli_common.h"
#include "mgt.h"
#include "mgt_cli.h"

+#include "vsb.h"
#include "heritage.h"

struct parspec;
@@ -40,10 +43,8 @@
return;
}
*dst = u;
- }
- if (cli == NULL)
- return;
- cli_out(cli, "%u [seconds]\n", *dst);
+ } else
+ cli_out(cli, "%u [seconds]\n", *dst);
}

/*--------------------------------------------------------------------*/
@@ -55,9 +56,8 @@
(void)par;
if (arg != NULL)
params->default_ttl = strtoul(arg, NULL, 0);
- if (cli == NULL)
- return;
- cli_out(cli, "%u [seconds]\n", params->default_ttl);
+ else
+ cli_out(cli, "%u [seconds]\n", params->default_ttl);
}

/*--------------------------------------------------------------------*/
@@ -76,10 +76,8 @@
return;
}
params->wthread_min = u;
- }
- if (cli == NULL)
- return;
- cli_out(cli, "%u [threads]\n", params->wthread_min);
+ } else
+ cli_out(cli, "%u [threads]\n", params->wthread_min);
}

/*--------------------------------------------------------------------*/
@@ -99,8 +97,6 @@
}
params->wthread_max = u;
}
- if (cli == NULL)
- return;
if (params->wthread_max == UINT_MAX)
cli_out(cli, "unlimited\n");
else
@@ -133,10 +129,8 @@
return;
}
params->mem_workspace = u;
- }
- if (cli == NULL)
- return;
- cli_out(cli, "%u [bytes]\n", params->mem_workspace);
+ } else
+ cli_out(cli, "%u [bytes]\n", params->mem_workspace);
}

/*--------------------------------------------------------------------*/
@@ -182,10 +176,8 @@
return;
}
params->auto_restart = u;
- }
- if (cli == NULL)
- return;
- cli_out(cli, "%u {1 = yes, 0 = no}\n", params->auto_restart);
+ } else
+ cli_out(cli, "%u {1 = yes, 0 = no}\n", params->auto_restart);
}

/*--------------------------------------------------------------------*/
@@ -199,10 +191,8 @@
if (arg != NULL) {
u = strtoul(arg, NULL, 0);
params->fetch_chunksize = u * 1024;
- }
- if (cli == NULL)
- return;
- cli_out(cli, "%u [kb]\n", params->fetch_chunksize * 1024);
+ } else
+ cli_out(cli, "%u [kb]\n", params->fetch_chunksize * 1024);
}

#ifdef HAVE_SENDFILE
@@ -217,10 +207,8 @@
if (arg != NULL) {
u = strtoul(arg, NULL, 0);
params->sendfile_threshold = u;
- }
- if (cli == NULL)
- return;
- cli_out(cli, "%u [bytes]\n", params->sendfile_threshold);
+ } else
+ cli_out(cli, "%u [bytes]\n", params->sendfile_threshold);
}
#endif /* HAVE_SENDFILE */

@@ -248,10 +236,8 @@
cli_result(cli, CLIS_PARAM);
return;
}
- }
- if (cli == NULL)
- return;
- cli_out(cli, params->vcl_trace ? "on\n" : "off\n");
+ } else
+ cli_out(cli, params->vcl_trace ? "on\n" : "off\n");
}

/*--------------------------------------------------------------------*/
@@ -268,21 +254,22 @@
cli_result(cli, CLIS_PARAM);
return;
}
+ if (p == NULL) {
+ p = strdup("http");
+ AN(p);
+ }
+ TCP_check(cli, a, p);
+ if (cli->result != CLIS_OK)
+ return;
free(params->listen_address);
free(params->listen_host);
free(params->listen_port);
params->listen_address = strdup(arg);
AN(params->listen_address);
params->listen_host = a;
- if (p == NULL) {
- p = strdup("http");
- AN(p);
- }
params->listen_port = p;
- }
- if (cli == NULL)
- return;
- cli_out(cli, "%s", params->listen_address);
+ } else
+ cli_out(cli, "%s\n", params->listen_address);
}
/*--------------------------------------------------------------------*/

@@ -429,31 +416,38 @@
/*--------------------------------------------------------------------*/

void
-mcf_param_set(struct cli *cli, char **av, void *priv)
+MCF_ParamSet(struct cli *cli, const char *param, const char *val)
{
struct parspec *pp;

- (void)priv;
for (pp = parspec; pp->name != NULL; pp++) {
- if (!strcmp(pp->name, av[2])) {
- cli_out(cli, "%-20s ", pp->name);
- pp->func(cli, pp, av[3]);
+ if (!strcmp(pp->name, param)) {
+ pp->func(cli, pp, val);
return;
}
}
- if (av[2] != NULL) {
- cli_result(cli, CLIS_PARAM);
- cli_out(cli, "Unknown paramter \"%s\".", av[2]);
- }
+ cli_result(cli, CLIS_PARAM);
+ cli_out(cli, "Unknown paramter \"%s\".", param);
}

+
/*--------------------------------------------------------------------*/

void
-MCF_ParamInit(void)
+mcf_param_set(struct cli *cli, char **av, void *priv)
{
+
+ (void)priv;
+ MCF_ParamSet(cli, av[2], av[3]);
+}
+
+/*--------------------------------------------------------------------*/
+
+void
+MCF_ParamInit(struct cli *cli)
+{
struct parspec *pp;

for (pp = parspec; pp->name != NULL; pp++)
- pp->func(NULL, pp, pp->def);
+ pp->func(cli, pp, pp->def);
}

Modified: trunk/varnish-cache/bin/varnishd/tcp.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/tcp.c 2006-09-16 08:20:39 UTC (rev 999)
+++ trunk/varnish-cache/bin/varnishd/tcp.c 2006-09-16 09:01:21 UTC (rev 1000)
@@ -22,6 +22,8 @@
#endif

#include "mgt.h"
+#include "cli.h"
+#include "cli_priv.h"

/*--------------------------------------------------------------------*/

@@ -109,6 +111,27 @@
return (0);
}

+/*--------------------------------------------------------------------*/
+
+void
+TCP_check(struct cli *cli, const char *addr, const char *port)
+{
+ struct addrinfo hints, *res;
+ int ret;
+
+ memset(&hints, 0, sizeof hints);
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_PASSIVE;
+ ret = getaddrinfo(addr, port, &hints, &res);
+ if (ret == 0) {
+ freeaddrinfo(res);
+ return;
+ }
+ cli_out(cli, "getaddrinfo(%s, %s): %s\n",
+ addr, port, gai_strerror(ret));
+ cli_result(cli, CLIS_PARAM);
+}
+
int
TCP_open(const char *addr, const char *port, int http)
{

Modified: trunk/varnish-cache/bin/varnishd/varnishd.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/varnishd.c 2006-09-16 08:20:39 UTC (rev 999)
+++ trunk/varnish-cache/bin/varnishd/varnishd.c 2006-09-16 09:01:21 UTC (rev 1000)
@@ -315,12 +315,25 @@

/*--------------------------------------------------------------------*/

+static void
+cli_check(struct cli *cli)
+{
+ if (cli->result == CLIS_OK) {
+ vsb_clear(cli->sb);
+ return;
+ }
+ vsb_finish(cli->sb);
+ fprintf(stderr, "Error:\n%s\n", vsb_data(cli->sb));
+ exit (2);
+}
+
+/*--------------------------------------------------------------------*/
+
int
main(int argc, char *argv[])
{
int o;
unsigned d_flag = 0;
- char *addr, *port;
const char *a_arg = NULL;
const char *b_arg = NULL;
const char *f_arg = NULL;
@@ -328,16 +341,23 @@
const char *s_arg = "file";
const char *T_arg = NULL;
struct params param;
+ struct cli cli[1];

setbuf(stdout, NULL);
setbuf(stderr, NULL);

+ memset(cli, 0, sizeof cli);
+ cli->sb = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
+ XXXAN(cli->sb);
+ cli->result = CLIS_OK;
+
heritage.socket = -1;
memset(&param, 0, sizeof param);
params = &param;
mgt_vcc_init();

- MCF_ParamInit();
+ MCF_ParamInit(cli);
+ cli_check(cli);

while ((o = getopt(argc, argv, "a:b:df:h:s:t:T:Vw:")) != -1)
switch (o) {
@@ -398,19 +418,9 @@
setup_storage(s_arg);
setup_hash(h_flag);

- if (a_arg != NULL) {
- if (TCP_parse(a_arg, &addr, &port) != 0) {
- fprintf(stderr, "invalid listen address\n");
- exit (2);
- }
- free(params->listen_address);
- free(params->listen_host);
- free(params->listen_port);
- params->listen_address = strdup(a_arg);
- AN(params->listen_address);
- params->listen_host = addr;
- params->listen_port = port;
- }
+ if (a_arg != NULL)
+ MCF_ParamSet(cli, "listen_address", a_arg);
+ cli_check(cli);

VSL_MgtInit(SHMLOG_FILENAME, 8*1024*1024);
r1000 - trunk/varnish-cache/bin/varnishd [ In reply to ]
In message <20060916090122.1072A1EC372 at projects.linpro.no>, phk at projects.linpro
.no writes:
>Author: phk
>Date: 2006-09-16 11:01:21 +0200 (Sat, 16 Sep 2006)
>New Revision: 1000

Wheee ---------^

--
Poul-Henning Kamp | UNIX since Zilog Zeus 3.20
phk at FreeBSD.ORG | TCP/IP since RFC 956
FreeBSD committer | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.
r1000 - trunk/varnish-cache/bin/varnishd [ In reply to ]
phk at projects.linpro.no writes:
> Log:
> Generalize parameter setting at start up.
>
> Give the managers main a struct cli and use that to call into
> the paramter stuff for setting listen address. More params to follow.
>
> When setting the listen address, check that getaddrinfo() doesn't
> hate it.

I'd suggest adding the following parameters:

- worker thread min/max/timeout (replaces the -w option)
- hard minimum TTL (replaces the -t option)

and possibly:

- hash algorithm (rehash when it changes)
- storage file (discard previous when it changes)

DES
--
Dag-Erling Sm?rgrav
Senior Software Developer
Linpro AS - www.linpro.no
r1000 - trunk/varnish-cache/bin/varnishd [ In reply to ]
In message <ujrk644jg33.fsf at cat.linpro.no>, Dag-Erling =?iso-8859-1?Q?Sm=F8rgra
v?= writes:
>phk at projects.linpro.no writes:

>> Generalize parameter setting at start up.

>I'd suggest adding the following parameters:

Yes, there are many things we can tweak here, but I don't
think this is the time to get carried away.

> - hash algorithm (rehash when it changes)

No way. It will require a child restart.

> - storage file (discard previous when it changes)

Same.


--
Poul-Henning Kamp | UNIX since Zilog Zeus 3.20
phk at FreeBSD.ORG | TCP/IP since RFC 956
FreeBSD committer | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.