[master] c012d86 Cleanup VSUB_ and eliminate from libvarnish.h
Poul-Henning Kamp
phk at varnish-cache.org
Sun Oct 9 20:51:35 CEST 2011
commit c012d86e43957404511107fb37247c4249d7c841
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Sun Oct 9 18:29:10 2011 +0000
Cleanup VSUB_ and eliminate from libvarnish.h
diff --git a/bin/varnishd/mgt_vcc.c b/bin/varnishd/mgt_vcc.c
index f8e81ca..bbf813a 100644
--- a/bin/varnishd/mgt_vcc.c
+++ b/bin/varnishd/mgt_vcc.c
@@ -42,6 +42,7 @@
#include "libvcl.h"
#include "vcli.h"
+#include "vsub.h"
#include "vcl.h"
#include "cli_priv.h"
#include "mgt_cli.h"
@@ -238,7 +239,7 @@ mgt_run_cc(const char *vcl, struct vsb *sb, int C_flag)
vp.magic = VCC_PRIV_MAGIC;
vp.sf = sf;
vp.vcl = vcl;
- if (SUB_run(sb, run_vcc, &vp, "VCC-compiler", -1)) {
+ if (VSUB_run(sb, run_vcc, &vp, "VCC-compiler", -1)) {
(void)unlink(sf);
return (NULL);
}
@@ -261,13 +262,13 @@ mgt_run_cc(const char *vcl, struct vsb *sb, int C_flag)
cmdsb = mgt_make_cc_cmd(sf, of);
/* Run the C-compiler in a sub-shell */
- i = SUB_run(sb, run_cc, VSB_data(cmdsb), "C-compiler", 10);
+ i = VSUB_run(sb, run_cc, VSB_data(cmdsb), "C-compiler", 10);
(void)unlink(sf);
VSB_delete(cmdsb);
if (!i)
- i = SUB_run(sb, run_dlopen, of, "dlopen", 10);
+ i = VSUB_run(sb, run_dlopen, of, "dlopen", 10);
if (i) {
(void)unlink(of);
diff --git a/include/Makefile.am b/include/Makefile.am
index 66c2ddd..2cbe5d1 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -54,6 +54,7 @@ nobase_noinst_HEADERS = \
vrt.h \
vrt_obj.h \
vsb.h \
+ vsub.h \
vsha256.h \
vss.h \
vtcp.h
diff --git a/include/libvarnish.h b/include/libvarnish.h
index a40aa20..5bbe073 100644
--- a/include/libvarnish.h
+++ b/include/libvarnish.h
@@ -41,11 +41,6 @@ struct vsb;
/* from libvarnish/num.c */
const char *str2bytes(const char *p, uintmax_t *r, uintmax_t rel);
-/* from libvarnish/subproc.c */
-typedef void sub_func_f(void*);
-int SUB_run(struct vsb *sb, sub_func_f *func, void *priv, const char *name,
- int maxlines);
-
/* from libvarnish/time.c */
#define TIM_FORMAT_SIZE 30
void TIM_format(double t, char *p);
diff --git a/include/vsub.h b/include/vsub.h
new file mode 100644
index 0000000..6705b11
--- /dev/null
+++ b/include/vsub.h
@@ -0,0 +1,35 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2011 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * 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
+ * SUCH DAMAGE.
+ *
+ */
+
+/* from libvarnish/subproc.c */
+typedef void vsub_func_f(void*);
+
+int VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
+ int maxlines);
diff --git a/lib/libvarnish/Makefile.am b/lib/libvarnish/Makefile.am
index b8651cc..0148460 100644
--- a/lib/libvarnish/Makefile.am
+++ b/lib/libvarnish/Makefile.am
@@ -8,7 +8,7 @@ libvarnish_la_SOURCES = \
vav.c \
vas.c \
binary_heap.c \
- subproc.c \
+ vsub.c \
cli_auth.c \
cli_common.c \
cli_serve.c \
diff --git a/lib/libvarnish/subproc.c b/lib/libvarnish/subproc.c
deleted file mode 100644
index 99f924e..0000000
--- a/lib/libvarnish/subproc.c
+++ /dev/null
@@ -1,133 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2011 Varnish Software AS
- * All rights reserved.
- *
- * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * 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
- * SUCH DAMAGE.
- *
- * Run stuff in a child process
- */
-
-#include "config.h"
-
-#include <fcntl.h>
-#include <string.h>
-#include <unistd.h>
-#include <stdio.h>
-
-#include <sys/wait.h>
-
-#include "vsb.h"
-#include "vlu.h"
-#include "libvarnish.h"
-
-struct sub_priv {
- const char *name;
- struct vsb *sb;
- int lines;
- int maxlines;
-};
-
-static int
-sub_vlu(void *priv, const char *str)
-{
- struct sub_priv *sp;
-
- sp = priv;
- if (!sp->lines++)
- VSB_printf(sp->sb, "Message from %s:\n", sp->name);
- if (sp->maxlines < 0 || sp->lines <= sp->maxlines)
- VSB_printf(sp->sb, "%s\n", str);
- return (0);
-}
-
-int
-SUB_run(struct vsb *sb, sub_func_f *func, void *priv, const char *name,
- int maxlines)
-{
- int rv, p[2], sfd, status;
- pid_t pid;
- struct vlu *vlu;
- struct sub_priv sp;
-
- sp.sb = sb;
- sp.name = name;
- sp.lines = 0;
- sp.maxlines = maxlines;
-
- if (pipe(p) < 0) {
- VSB_printf(sb, "Starting %s: pipe() failed: %s",
- name, strerror(errno));
- return (-1);
- }
- assert(p[0] > STDERR_FILENO);
- assert(p[1] > STDERR_FILENO);
- if ((pid = fork()) < 0) {
- VSB_printf(sb, "Starting %s: fork() failed: %s",
- name, strerror(errno));
- AZ(close(p[0]));
- AZ(close(p[1]));
- return (-1);
- }
- if (pid == 0) {
- AZ(close(STDIN_FILENO));
- assert(open("/dev/null", O_RDONLY) == STDIN_FILENO);
- assert(dup2(p[1], STDOUT_FILENO) == STDOUT_FILENO);
- assert(dup2(p[1], STDERR_FILENO) == STDERR_FILENO);
- /* Close all other fds */
- for (sfd = STDERR_FILENO + 1; sfd < 100; sfd++)
- (void)close(sfd);
- func(priv);
- _exit(1);
- }
- AZ(close(p[1]));
- vlu = VLU_New(&sp, sub_vlu, 0);
- while (!VLU_Fd(p[0], vlu))
- continue;
- AZ(close(p[0]));
- VLU_Destroy(vlu);
- if (sp.maxlines >= 0 && sp.lines > sp.maxlines)
- VSB_printf(sb, "[%d lines truncated]\n",
- sp.lines - sp.maxlines);
- do {
- rv = waitpid(pid, &status, 0);
- if (rv < 0 && errno != EINTR) {
- VSB_printf(sb, "Running %s: waitpid() failed: %s\n",
- name, strerror(errno));
- return (-1);
- }
- } while (rv < 0);
- if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
- VSB_printf(sb, "Running %s failed", name);
- if (WIFEXITED(status))
- VSB_printf(sb, ", exit %d", WEXITSTATUS(status));
- if (WIFSIGNALED(status))
- VSB_printf(sb, ", signal %d", WTERMSIG(status));
- if (WCOREDUMP(status))
- VSB_printf(sb, ", core dumped");
- VSB_printf(sb, "\n");
- return (-1);
- }
- return (0);
-}
diff --git a/lib/libvarnish/vsub.c b/lib/libvarnish/vsub.c
new file mode 100644
index 0000000..90ceacb
--- /dev/null
+++ b/lib/libvarnish/vsub.c
@@ -0,0 +1,134 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2011 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * 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
+ * SUCH DAMAGE.
+ *
+ * Run stuff in a child process
+ */
+
+#include "config.h"
+
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include <sys/wait.h>
+
+#include "vas.h"
+#include "vlu.h"
+#include "vsb.h"
+#include "vsub.h"
+
+struct vsub_priv {
+ const char *name;
+ struct vsb *sb;
+ int lines;
+ int maxlines;
+};
+
+static int
+vsub_vlu(void *priv, const char *str)
+{
+ struct vsub_priv *sp;
+
+ sp = priv;
+ if (!sp->lines++)
+ VSB_printf(sp->sb, "Message from %s:\n", sp->name);
+ if (sp->maxlines < 0 || sp->lines <= sp->maxlines)
+ VSB_printf(sp->sb, "%s\n", str);
+ return (0);
+}
+
+int
+VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
+ int maxlines)
+{
+ int rv, p[2], sfd, status;
+ pid_t pid;
+ struct vlu *vlu;
+ struct vsub_priv sp;
+
+ sp.sb = sb;
+ sp.name = name;
+ sp.lines = 0;
+ sp.maxlines = maxlines;
+
+ if (pipe(p) < 0) {
+ VSB_printf(sb, "Starting %s: pipe() failed: %s",
+ name, strerror(errno));
+ return (-1);
+ }
+ assert(p[0] > STDERR_FILENO);
+ assert(p[1] > STDERR_FILENO);
+ if ((pid = fork()) < 0) {
+ VSB_printf(sb, "Starting %s: fork() failed: %s",
+ name, strerror(errno));
+ AZ(close(p[0]));
+ AZ(close(p[1]));
+ return (-1);
+ }
+ if (pid == 0) {
+ AZ(close(STDIN_FILENO));
+ assert(open("/dev/null", O_RDONLY) == STDIN_FILENO);
+ assert(dup2(p[1], STDOUT_FILENO) == STDOUT_FILENO);
+ assert(dup2(p[1], STDERR_FILENO) == STDERR_FILENO);
+ /* Close all other fds */
+ for (sfd = STDERR_FILENO + 1; sfd < 100; sfd++)
+ (void)close(sfd);
+ func(priv);
+ _exit(1);
+ }
+ AZ(close(p[1]));
+ vlu = VLU_New(&sp, vsub_vlu, 0);
+ while (!VLU_Fd(p[0], vlu))
+ continue;
+ AZ(close(p[0]));
+ VLU_Destroy(vlu);
+ if (sp.maxlines >= 0 && sp.lines > sp.maxlines)
+ VSB_printf(sb, "[%d lines truncated]\n",
+ sp.lines - sp.maxlines);
+ do {
+ rv = waitpid(pid, &status, 0);
+ if (rv < 0 && errno != EINTR) {
+ VSB_printf(sb, "Running %s: waitpid() failed: %s\n",
+ name, strerror(errno));
+ return (-1);
+ }
+ } while (rv < 0);
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+ VSB_printf(sb, "Running %s failed", name);
+ if (WIFEXITED(status))
+ VSB_printf(sb, ", exit %d", WEXITSTATUS(status));
+ if (WIFSIGNALED(status))
+ VSB_printf(sb, ", signal %d", WTERMSIG(status));
+ if (WCOREDUMP(status))
+ VSB_printf(sb, ", core dumped");
+ VSB_printf(sb, "\n");
+ return (-1);
+ }
+ return (0);
+}
More information about the varnish-commit
mailing list