r3620 - in branches/2.0/varnish-cache: bin/varnishd include lib/libvarnish

tfheen at projects.linpro.no tfheen at projects.linpro.no
Thu Feb 5 13:14:59 CET 2009


Author: tfheen
Date: 2009-02-05 13:14:59 +0100 (Thu, 05 Feb 2009)
New Revision: 3620

Added:
   branches/2.0/varnish-cache/lib/libvarnish/subproc.c
Modified:
   branches/2.0/varnish-cache/bin/varnishd/mgt_vcc.c
   branches/2.0/varnish-cache/include/libvarnish.h
   branches/2.0/varnish-cache/lib/libvarnish/Makefile.am
Log:
Merge r3413: Move subprocess code into a library function

Move the code for running stuff in a sub-process out to a library
function, and give it the ability to limit how many lines of
output we get back from the subprocess, in order to muzzle the
C-compiler somewhat.




Modified: branches/2.0/varnish-cache/bin/varnishd/mgt_vcc.c
===================================================================
--- branches/2.0/varnish-cache/bin/varnishd/mgt_vcc.c	2009-02-05 11:57:13 UTC (rev 3619)
+++ branches/2.0/varnish-cache/bin/varnishd/mgt_vcc.c	2009-02-05 12:14:59 UTC (rev 3620)
@@ -47,7 +47,6 @@
 #include "compat/asprintf.h"
 #endif
 #include "vsb.h"
-#include "vlu.h"
 
 #include "vqueue.h"
 
@@ -124,16 +123,13 @@
  * Errors goes in sb;
  */
 
-static int
-mgt_cc_vlu(void *priv, const char *str)
+static void
+run_cc(void *priv)
 {
-	struct vsb *vsb;
-
-	vsb = priv;
-	vsb_printf(vsb, "C-compiler said: %s\n", str);
-	return (0);
+	(void)execl("/bin/sh", "/bin/sh", "-c", priv, NULL);
 }
 
+
 static char *
 mgt_run_cc(const char *source, struct vsb *sb)
 {
@@ -142,10 +138,8 @@
 	char sf[] = "./vcl.########.c";
 	char of[sizeof sf + 1];
 	char *retval;
-	int rv, p[2], sfd, srclen, status;
-	pid_t pid;
+	int sfd, srclen;
 	void *dlh;
-	struct vlu *vlu;
 
 	/* Create temporary C source file */
 	sfd = vtmpfile(sf);
@@ -178,57 +172,8 @@
 	AZ(vsb_overflowed(&cmdsb));
 	/* XXX check vsb state */
 
-	if (pipe(p) < 0) {
-		vsb_printf(sb, "%s(): pipe() failed: %s",
-		    __func__, strerror(errno));
+	if (SUB_run(sb, run_cc, cmdline, "C-compiler", 10)) {
 		(void)unlink(sf);
-		return (NULL);
-	}
-	assert(p[0] > STDERR_FILENO);
-	assert(p[1] > STDERR_FILENO);
-	if ((pid = fork()) < 0) {
-		vsb_printf(sb, "%s(): fork() failed: %s",
-		    __func__, strerror(errno));
-		AZ(close(p[0]));
-		AZ(close(p[1]));
-		(void)unlink(sf);
-		return (NULL);
-	}
-	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);
-		(void)execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
-		_exit(1);
-	}
-	AZ(close(p[1]));
-	vlu = VLU_New(sb, mgt_cc_vlu, 0);
-	while (!VLU_Fd(p[0], vlu))
-		continue;
-	AZ(close(p[0]));
-	VLU_Destroy(vlu);
-	(void)unlink(sf);
-	do {
-		rv = waitpid(pid, &status, 0);
-		if (rv < 0 && errno != EINTR) {
-			vsb_printf(sb, "%s(): waitpid() failed: %s",
-			    __func__, strerror(errno));
-			(void)unlink(of);
-			return (NULL);
-		}
-	} while (rv < 0);
-	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
-		vsb_printf(sb, "%s(): Compiler failed", __func__);
-		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");
 		(void)unlink(of);
 		return (NULL);
 	}

Modified: branches/2.0/varnish-cache/include/libvarnish.h
===================================================================
--- branches/2.0/varnish-cache/include/libvarnish.h	2009-02-05 11:57:13 UTC (rev 3619)
+++ branches/2.0/varnish-cache/include/libvarnish.h	2009-02-05 12:14:59 UTC (rev 3620)
@@ -37,6 +37,8 @@
 #define NULL ((void*)0)
 #endif
 
+struct vsb;
+
 /* from libvarnish/argv.c */
 void FreeArgv(char **argv);
 char **ParseArgv(const char *s, int flag);
@@ -50,6 +52,10 @@
 /* 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/tcp.c */
 /* NI_MAXHOST and NI_MAXSERV are ridiculously long for numeric format */
 #define TCP_ADDRBUFSIZE		64

Modified: branches/2.0/varnish-cache/lib/libvarnish/Makefile.am
===================================================================
--- branches/2.0/varnish-cache/lib/libvarnish/Makefile.am	2009-02-05 11:57:13 UTC (rev 3619)
+++ branches/2.0/varnish-cache/lib/libvarnish/Makefile.am	2009-02-05 12:14:59 UTC (rev 3620)
@@ -10,6 +10,7 @@
 	argv.c \
 	assert.c \
 	binary_heap.c \
+	subproc.c \
 	cli.c \
 	cli_common.c \
 	crc32.c \

Copied: branches/2.0/varnish-cache/lib/libvarnish/subproc.c (from rev 3413, trunk/varnish-cache/lib/libvarnish/subproc.c)
===================================================================
--- branches/2.0/varnish-cache/lib/libvarnish/subproc.c	                        (rev 0)
+++ branches/2.0/varnish-cache/lib/libvarnish/subproc.c	2009-02-05 12:14:59 UTC (rev 3620)
@@ -0,0 +1,133 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2008 Linpro 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.
+ *
+ * $Id$
+ *
+ * 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.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",
+			    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");
+		return (-1);
+	}
+	return (0);
+}



More information about the varnish-commit mailing list