[master] 91fe120b3 Move the writing of the compiled C source to VCC

Poul-Henning Kamp phk at FreeBSD.org
Wed May 22 08:50:14 UTC 2019


commit 91fe120b364498ee32279ed07a7b48effe99576a
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed May 22 07:03:25 2019 +0000

    Move the writing of the compiled C source to VCC

diff --git a/bin/varnishd/mgt/mgt_vcc.c b/bin/varnishd/mgt/mgt_vcc.c
index cf7852e7c..8a4074efa 100644
--- a/bin/varnishd/mgt/mgt_vcc.c
+++ b/bin/varnishd/mgt/mgt_vcc.c
@@ -85,12 +85,11 @@ mgt_DumpBuiltin(void)
 static void v_matchproto_(vsub_func_f)
 run_vcc(void *priv)
 {
-	struct vsb *csrc;
 	struct vsb *sb = NULL;
 	struct vcc_priv *vp;
-	int fd, i, l;
 	struct vcc *vcc;
 	struct stevedore *stv;
+	int i;
 
 	VJ_subproc(JAIL_SUBPROC_VCC);
 	CAST_OBJ_NOTNULL(vp, priv, VCC_PRIV_MAGIC);
@@ -108,28 +107,11 @@ run_vcc(void *priv)
 	STV_Foreach(stv)
 		VCC_Predef(vcc, "VCL_STEVEDORE", stv->ident);
 	mgt_vcl_export_labels(vcc);
-	csrc = VCC_Compile(vcc, &sb, vp->vclsrc, vp->vclsrcfile);
-	AZ(VSB_finish(sb));
+	i = VCC_Compile(vcc, &sb, vp->vclsrc, vp->vclsrcfile, VGC_SRC);
 	if (VSB_len(sb))
 		printf("%s", VSB_data(sb));
 	VSB_destroy(&sb);
-	if (csrc == NULL)
-		exit(2);
-
-	fd = open(VGC_SRC, O_WRONLY|O_TRUNC|O_CREAT, 0600);
-	if (fd < 0) {
-		fprintf(stderr, "VCC cannot open %s", vp->csrcfile);
-		exit(2);
-	}
-	l = VSB_len(csrc);
-	i = write(fd, VSB_data(csrc), l);
-	if (i != l) {
-		fprintf(stderr, "VCC cannot write %s", vp->csrcfile);
-		exit(2);
-	}
-	closefd(&fd);
-	VSB_destroy(&csrc);
-	exit(0);
+	exit(i == 0 ? 0 : 2);
 }
 
 /*--------------------------------------------------------------------
diff --git a/include/libvcc.h b/include/libvcc.h
index 31b2a7ff0..45f012a1a 100644
--- a/include/libvcc.h
+++ b/include/libvcc.h
@@ -42,5 +42,5 @@ void VCC_VMOD_path(struct vcc *, const char *);
 void VCC_Predef(struct vcc *, const char *type, const char *name);
 void VCC_VCL_Range(unsigned *, unsigned *);
 
-struct vsb *VCC_Compile(struct vcc *, struct vsb **,
-    const char *vclsrc, const char *vclsrcfile);
+int VCC_Compile(struct vcc *, struct vsb **,
+    const char *, const char *, const char *);
diff --git a/lib/libvcc/vcc_compile.c b/lib/libvcc/vcc_compile.c
index c0ffc19ba..8b8c2e9d3 100644
--- a/lib/libvcc/vcc_compile.c
+++ b/lib/libvcc/vcc_compile.c
@@ -52,9 +52,11 @@
 
 #include "config.h"
 
+#include <fcntl.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "vcc_compile.h"
 
@@ -729,25 +731,48 @@ VCC_VCL_Range(unsigned *lo, unsigned *hi)
  * formatted into the vsb.
  */
 
-struct vsb *
+int
 VCC_Compile(struct vcc *tl, struct vsb **sb,
-    const char *vclsrc, const char *vclsrcfile)
+    const char *vclsrc, const char *vclsrcfile,
+    const char *ofile)
 {
 	struct source *sp;
 	struct vsb *r = NULL;
+	int fo, retval = 0;
 
 	CHECK_OBJ_NOTNULL(tl, VCC_MAGIC);
 	AN(sb);
 	AN(vclsrcfile);
-
+	AN(ofile);
 	if (vclsrc != NULL)
 		sp = vcc_new_source(vclsrc, NULL, vclsrcfile);
 	else
 		sp = vcc_file_source(tl, vclsrcfile);
+
 	if (sp != NULL)
 		r = vcc_CompileSource(tl, sp);
+
+	if (r != NULL) {
+		fo = open(ofile, O_WRONLY|O_TRUNC|O_CREAT, 0600);
+		if (fo < 0) {
+			VSB_printf(tl->sb,
+			    "Could not open C-source file %s: %s\n",
+			    ofile, strerror(errno));
+		} else {
+			if (VSB_tofile(fo, r)) {
+				VSB_printf(tl->sb,
+				    "Could not write C-source to %s: %s\n",
+				    ofile, strerror(errno));
+			}
+			closefd(&fo);
+		}
+		VSB_destroy(&r);
+	} else {
+		retval = -1;
+	}
+	AZ(VSB_finish(tl->sb));
 	*sb = tl->sb;
-	return (r);
+	return (retval);
 }
 
 /*--------------------------------------------------------------------


More information about the varnish-commit mailing list