--- /dev/null
+/*
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <poll.h>
+#include <pthread.h>
+
+#include "event.h" /* XXX only as long as it takes */
+
+#include "libvarnish.h"
+#include "shmlog.h"
+#include "cli.h"
+#include "cli_priv.h"
+#include "cli_event.h"
+#include "cache.h"
+#include "sbuf.h"
+#include "heritage.h"
+
+/*--------------------------------------------------------------------*/
+
+static void
+cli_func_ping(struct cli *cli, char **av, void *priv)
+{
+ time_t t;
+
+ (void)priv;
+#if 0
+ arm_keepalive();
+#endif
+ if (av[2] != NULL) {
+ /* XXX: check clock skew is pointless here */
+ }
+ t = time(NULL);
+ cli_out(cli, "PONG %ld", t);
+}
+
+/*--------------------------------------------------------------------*/
+
+struct cli_proto CLI_cmds[] = {
+ { CLI_PING, cli_func_ping },
+#if 0
+ { CLI_URL_QUERY, cli_func_url_query },
+#endif
+ { CLI_URL_PURGE, cli_func_url_purge },
+ { CLI_CONFIG_LOAD, cli_func_config_load },
+ { CLI_CONFIG_LIST, cli_func_config_list },
+ { CLI_CONFIG_UNLOAD, cli_func_config_unload },
+ { CLI_CONFIG_USE, cli_func_config_use },
+ { NULL }
+};
+
+static int
+cli_writes(const char *s, const char *r, const char *t)
+{
+ int i, l;
+ struct iovec iov[3];
+
+ iov[0].iov_base = (void*)(uintptr_t)s;
+ iov[1].iov_base = (void*)(uintptr_t)r;
+ iov[2].iov_base = (void*)(uintptr_t)t;
+ for (l = i = 0; i < 3; i++) {
+ iov[i].iov_len = strlen(iov[i].iov_base);
+ l += iov[i].iov_len;
+ }
+ i = writev(heritage.fds[1], iov, 3);
+ VSL(SLT_CLI, 0, "Wr %d %s %s", i != l, s, r);
+ return (i != l);
+}
+
+void
+CLI_Init(void)
+{
+ struct pollfd pfd[1];
+ char *buf, *p;
+ unsigned nbuf, lbuf;
+ struct cli *cli, clis;
+ int i;
+ char res[30];
+
+ cli = &clis;
+ memset(cli, 0, sizeof *cli);
+
+ cli->sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ assert(cli->sb != NULL);
+ lbuf = 4096;
+ buf = malloc(lbuf);
+ assert(buf != NULL);
+ nbuf = 0;
+ while (1) {
+ pfd[0].fd = heritage.fds[2];
+ pfd[0].events = POLLIN;
+ i = poll(pfd, 1, 5000);
+ if (i == 0)
+ continue;
+ if (nbuf == lbuf) {
+ lbuf += lbuf;
+ buf = realloc(buf, lbuf);
+ assert(buf != NULL);
+ }
+ i = read(heritage.fds[2], buf + nbuf, lbuf - nbuf);
+ if (i <= 0) {
+ VSL(SLT_Error, 0, "CLI read %d (errno=%d)", i, errno);
+ return;
+ }
+ nbuf += i;
+ p = strchr(buf, '\n');
+ if (p == NULL)
+ continue;
+ *p = '\0';
+ VSL(SLT_CLI, 0, "Rd %s", buf);
+ sbuf_clear(cli->sb);
+ cli_dispatch(cli, CLI_cmds, buf);
+ sbuf_finish(cli->sb);
+ sprintf(res, "%d ", cli->result);
+ if (cli_writes(res, sbuf_data(cli->sb), "\n")) {
+ VSL(SLT_Error, 0, "CLI write failed (errno=%d)", errno);
+ return;
+ }
+ i = ++p - buf;
+ assert(i <= nbuf);
+ if (i < nbuf)
+ memcpy(buf, p, nbuf - i);
+ nbuf -= i;
+ }
+}
#include <stdlib.h>
#include <signal.h>
-#include <cli.h>
-#include <cli_priv.h>
-
#include "libvarnish.h"
#include "heritage.h"
#include "shmlog.h"
#include "cache.h"
#include "event.h"
-#include "cli_event.h"
-
-static struct event ev_keepalive;
struct stevedore *stevedore;
-/*--------------------------------------------------------------------*/
-
-static void
-timer_keepalive(int a, short b, void *c)
-{
-
- printf("%s(%d, %d, %p)\n", (const char *)__func__, a, (int)b, c);
- printf("Heeellloooo ? Ohh bother...\n");
- exit (1);
-}
-
-static void
-arm_keepalive(void)
-{
- struct timeval tv;
-
- tv.tv_sec = 30;
- tv.tv_usec = 0;
-
- AZ(evtimer_del(&ev_keepalive));
- AZ(evtimer_add(&ev_keepalive, &tv));
-}
-
-/*--------------------------------------------------------------------*/
-
-static void
-cli_func_url_query(struct cli *cli, char **av, void *priv)
-{
-
- (void)priv;
- cli_out(cli, "url <%s>", av[2]);
- cli_result(cli, CLIS_UNIMPL);
-}
-
-/*--------------------------------------------------------------------*/
-
-static void
-cli_func_ping(struct cli *cli, char **av, void *priv)
-{
- time_t t;
-
- (void)priv;
- VSL(SLT_CLI, 0, av[1]);
- arm_keepalive();
- if (av[2] != NULL) {
- /* XXX: check clock skew is pointless here */
- }
- t = time(NULL);
- cli_out(cli, "PONG %ld\n", t);
-}
-
-/*--------------------------------------------------------------------*/
-
-static struct cli_proto cli_proto[] = {
- { CLI_URL_QUERY, cli_func_url_query },
- { CLI_URL_PURGE, cli_func_url_purge },
- { CLI_CONFIG_LOAD, cli_func_config_load },
- { CLI_CONFIG_LIST, cli_func_config_list },
- { CLI_CONFIG_UNLOAD, cli_func_config_unload },
- { CLI_CONFIG_USE, cli_func_config_use },
- { CLI_PING, cli_func_ping },
- { NULL }
-};
-
/*--------------------------------------------------------------------
* XXX: Think more about which order we start things
*/
void
child_main(void)
{
- struct event_base *eb;
- struct cli *cli;
- int i;
/* XXX: SO_NOSIGPIPE does not work reliably :-( */
signal(SIGPIPE, SIG_IGN);
HSH_Init();
BAN_Init();
- eb = event_init();
- assert(eb != NULL);
-
stevedore = heritage.stevedore;
if (stevedore->open != NULL)
stevedore->open(stevedore);
- cli = cli_setup(eb, heritage.fds[2], heritage.fds[1], 0, cli_proto);
-
- evtimer_set(&ev_keepalive, timer_keepalive, NULL);
- AZ(event_base_set(eb, &ev_keepalive));
- arm_keepalive();
-
printf("Ready\n");
VSL_stats->start_time = time(NULL);
- i = event_base_loop(eb, 0);
- if (i != 0)
- printf("event_dispatch() = %d\n", i);
+
+ CLI_Init();
printf("Child dies\n");
}