From af1e3e6f0e736a788a0f9897e4545ac801d73bef Mon Sep 17 00:00:00 2001
From: Dag Haavi Finstad <daghf@varnish-software.com>
Date: Mon, 21 Dec 2015 15:00:55 +0100
Subject: [PATCH 2/2] Add .sema_sync() to libvmod-debug.

---
 bin/varnishtest/tests/m00023.vtc    |  33 ++++++++++
 lib/libvmod_debug/Makefile.am       |   3 +-
 lib/libvmod_debug/vmod.vcc          |   4 ++
 lib/libvmod_debug/vmod_debug_sema.c | 122 ++++++++++++++++++++++++++++++++++++
 4 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 bin/varnishtest/tests/m00023.vtc
 create mode 100644 lib/libvmod_debug/vmod_debug_sema.c

diff --git a/bin/varnishtest/tests/m00023.vtc b/bin/varnishtest/tests/m00023.vtc
new file mode 100644
index 0000000..7a53429
--- /dev/null
+++ b/bin/varnishtest/tests/m00023.vtc
@@ -0,0 +1,33 @@
+varnishtest "Test debug.sema_sync"
+
+server s1 {
+	rxreq
+	txresp
+} -start
+
+varnish v1 -vcl+backend {
+	import ${vmod_debug};
+
+	sub vcl_recv {
+		debug.sema_sync("r1", 2);
+	}
+
+	sub vcl_backend_response {
+		debug.sema_sync("r2", 2);
+	}
+} -start
+
+varnish v1  -cliok "param.set debug +syncvsl"
+
+client c1 {
+	txreq
+	rxresp
+	expect resp.status == 200
+} -start
+
+ipcsema r1 sync 2
+delay 0.5
+ipcsema r2 sync 2
+
+client c1 -wait
+
diff --git a/lib/libvmod_debug/Makefile.am b/lib/libvmod_debug/Makefile.am
index 9115713..244cc86 100644
--- a/lib/libvmod_debug/Makefile.am
+++ b/lib/libvmod_debug/Makefile.am
@@ -18,7 +18,8 @@ libvmod_debug_la_LDFLAGS = $(AM_LDFLAGS) -module -export-dynamic -avoid-version
 libvmod_debug_la_SOURCES = \
 	vmod_debug.c \
 	vmod_debug_obj.c \
-	vmod_debug_dyn.c
+	vmod_debug_dyn.c \
+	vmod_debug_sema.c
 
 nodist_libvmod_debug_la_SOURCES = \
 	vcc_if.c \
diff --git a/lib/libvmod_debug/vmod.vcc b/lib/libvmod_debug/vmod.vcc
index 42159fb..d6b9ac7 100644
--- a/lib/libvmod_debug/vmod.vcc
+++ b/lib/libvmod_debug/vmod.vcc
@@ -142,3 +142,7 @@ Find how much unallocated space there is left in a workspace.
 $Function VOID vcl_release_delay(DURATION)
 
 Hold a reference to the VCL when it goes cold for the given delay.
+
+$Function VOID sema_sync(STRING key, INT semval)
+
+Sync with a varnishtest ipc semaphore.
diff --git a/lib/libvmod_debug/vmod_debug_sema.c b/lib/libvmod_debug/vmod_debug_sema.c
new file mode 100644
index 0000000..b9ce88a
--- /dev/null
+++ b/lib/libvmod_debug/vmod_debug_sema.c
@@ -0,0 +1,122 @@
+/*-
+ * Copyright (c) 2015 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Dag Haavi Finstad <daghf@varnish-software.com>
+ *
+ * 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.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/sem.h>
+
+#include "cache/cache.h"
+
+#include "vrt.h"
+#include "vcc_if.h"
+#include "vsha256.h"
+#include "vtim.h"
+
+static int
+sem_decr(int semid)
+{
+	struct sembuf sb;
+
+	sb.sem_num = 0;
+	sb.sem_op = -1;
+	sb.sem_flg = 0;
+
+	return(semop(semid, &sb, 1));
+}
+
+static int
+sem_wait(int semid)
+{
+	struct sembuf sb;
+
+	sb.sem_num = 0;
+	sb.sem_op = 0;
+	sb.sem_flg = 0;
+
+	return(semop(semid, &sb, 1));
+}
+
+static int
+sem_get(const char *name, int semval)
+{
+	int semid;
+	struct semid_ds buf;
+	SHA256_CTX sctx;
+	unsigned char digest[SHA256_LEN];
+	const char *rnd;
+	key_t key;
+
+	rnd = getenv("VTC_IPC_RAND");
+	AN(rnd);
+
+	SHA256_Init(&sctx);
+	SHA256_Update(&sctx, name, strlen(name));
+	SHA256_Update(&sctx, rnd, strlen(rnd));
+	SHA256_Final(digest, &sctx);
+	memcpy(&key, digest, sizeof key);
+
+	if (key == IPC_PRIVATE)
+		key++;
+
+	/* See comments in bin/varnishtest/vtc_ipcsema.c:sema_new  */
+	semid = semget(key, 1, IPC_CREAT|IPC_EXCL|0666);
+	if (semid >= 0) {
+		assert(semctl(semid, 0, SETVAL, semval + 1) != -1);
+		AZ(sem_decr(semid));
+	} else if (errno == EEXIST) {
+		semid = semget(key, 1, 0);
+		assert(semid >= 0);
+		while (1) {
+			semctl(semid, 0, IPC_STAT, &buf);
+			if (buf.sem_otime != 0)
+				break;
+			VTIM_sleep(0.01);
+		}
+	} else {
+		INCOMPL();
+	}
+
+	return (semid);
+}
+
+
+VCL_VOID
+vmod_sema_sync(VRT_CTX, VCL_STRING k, VCL_INT semval)
+{
+	int semid = sem_get(k, (int) semval);
+	VSL(SLT_Debug, 0, "sema_sync: wait %s", k);
+	AZ(sem_decr(semid));
+	AZ(sem_wait(semid));
+	VSL(SLT_Debug, 0, "sema_sync: wake %s", k);
+}
-- 
2.6.4

