From: phk Date: Mon, 7 Aug 2006 20:24:47 +0000 (+0000) Subject: Improve the "-d" and "-d -d" facilities. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e0ba11abb92a94bb5b99fbd1e16cd7cc193a58d6;p=varnish Improve the "-d" and "-d -d" facilities. When we close a CLI and it had fd# 0 and/or fd#1, reopen these as /dev/null so the will not be reused for the CLI pipe to the child on next restart, otherwise stdout/stderr output from the manager would get sent there and confuse the clients CLI reader. Don't double free a pointer to the CLI buffer. Accept non-zero results from cli_readres() errors are non-fatal. Use stderr more consistently for manager debugging. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@749 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/mgt_child.c b/varnish-cache/bin/varnishd/mgt_child.c index c0650e82..c870f048 100644 --- a/varnish-cache/bin/varnishd/mgt_child.c +++ b/varnish-cache/bin/varnishd/mgt_child.c @@ -67,7 +67,7 @@ child_listener(struct ev *e, int what) return (1); } buf[i] = '\0'; - printf("Child said (%d, %d): <<%s>>\n", child_state, child_pid, buf); + fprintf(stderr, "Child said (%d, %d): <<%s>>\n", child_state, child_pid, buf); return (0); } @@ -128,7 +128,7 @@ start_child(void) exit (1); } - printf("start child pid %d\n", i); + fprintf(stderr, "start child pid %d\n", i); AZ(close(child_fds[1])); child_fds[1] = -1; @@ -183,7 +183,7 @@ stop_child(void) } ev_poker = NULL; - printf("Clean child\n"); + fprintf(stderr, "Clean child\n"); mgt_cli_stop_child(); /* We tell the child to die gracefully by closing the CLI */ @@ -192,7 +192,7 @@ stop_child(void) AZ(close(heritage.fds[3])); heritage.fds[3] = -1; - printf("Child stopping\n"); + fprintf(stderr, "Child stopping\n"); } /*--------------------------------------------------------------------*/ @@ -214,16 +214,16 @@ mgt_sigchld(struct ev *e, int what) r = wait4(-1, &status, WNOHANG, NULL); if (r != child_pid) { - printf("Unknown child died pid=%d status=0x%x\n", + fprintf(stderr, "Unknown child died pid=%d status=0x%x\n", r, status); return (0); } - printf("Cache child died pid=%d status=0x%x\n", r, status); + fprintf(stderr, "Cache child died pid=%d status=0x%x\n", r, status); child_pid = -1; if (child_state == CH_RUNNING) { child_state = CH_DIED; - printf("Clean child\n"); + fprintf(stderr, "Clean child\n"); mgt_cli_stop_child(); /* We tell the child to die gracefully by closing the CLI */ @@ -241,7 +241,7 @@ mgt_sigchld(struct ev *e, int what) AZ(close(child_fds[0])); child_fds[0] = -1; - printf("Child cleaned\n"); + fprintf(stderr, "Child cleaned\n"); if (child_state == CH_DIED) start_child(); @@ -258,7 +258,7 @@ mgt_sigint(struct ev *e, int what) (void)e; (void)what; - printf("Manager got SIGINT\n"); + fprintf(stderr, "Manager got SIGINT\n"); fflush(stdout); if (child_pid >= 0) stop_child(); @@ -276,6 +276,7 @@ mgt_run(int dflag) { struct sigaction sac; struct ev *e; + int i; mgt_pid = getpid(); @@ -320,9 +321,10 @@ mgt_run(int dflag) if (!dflag) start_child(); - ev_schedule(mgt_evb); + i = ev_schedule(mgt_evb); + fprintf(stderr, "ev_schedule = %d\n", i); - printf("manager dies\n"); + fprintf(stderr, "manager dies\n"); exit(2); } diff --git a/varnish-cache/bin/varnishd/mgt_cli.c b/varnish-cache/bin/varnishd/mgt_cli.c index 7eb423bd..41918e4e 100644 --- a/varnish-cache/bin/varnishd/mgt_cli.c +++ b/varnish-cache/bin/varnishd/mgt_cli.c @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -189,7 +190,6 @@ mgt_cli_askchild(unsigned *status, char **resp, const char *fmt, ...) j = write(cli_o, p, i); free(p); if (j != i) { - free(p); if (status != NULL) *status = CLIS_COMMS; if (resp != NULL) @@ -198,7 +198,6 @@ mgt_cli_askchild(unsigned *status, char **resp, const char *fmt, ...) } i = cli_readres(cli_i, &u, resp, 3.0); - assert(i == 0); if (status != NULL) *status = u; return (u == CLIS_OK ? 0 : u); @@ -281,7 +280,14 @@ fprintf(stderr, "CLI <%s>\n", cp->buf); vsb_delete(cp->cli->sb); free(cp->buf); close(cp->fdi); + if (cp->fdi == 0) + open("/dev/null", O_RDONLY); close(cp->fdo); + if (cp->fdo == 1) { + close(2); + open("/dev/null", O_WRONLY); + open("/dev/null", O_WRONLY); + } free(cp); return (1); } diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index 0bdababc..7069fe2c 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -403,10 +403,10 @@ main(int argc, char *argv[]) if (dflag == 1) DebugStunt(); - if (dflag != 2) + if (dflag < 2) daemon(dflag, dflag); - if (dflag) - printf("%d\n%d\n%d\n", getpid(), getsid(0), getpgrp()); + if (dflag == 1) + printf("%d\n", getpid()); mgt_cli_init();