Module Name:    src
Committed By:   pooka
Date:           Tue Nov 30 14:29:05 UTC 2010

Modified Files:
        src/tests/dev/md: h_mdserv.c t_md.sh

Log Message:
Get rid of the "sleep 1" by using rump_daemonize_begin/end().
Notably, md is a little tricky for this, since the ioctl that
configures the service also blocks in the kernel.  Therefore, use
an additional pthread to probe when the service is fully configured
and the server can detach.

Also, rawpart love.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/tests/dev/md/h_mdserv.c src/tests/dev/md/t_md.sh

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/dev/md/h_mdserv.c
diff -u src/tests/dev/md/h_mdserv.c:1.1 src/tests/dev/md/h_mdserv.c:1.2
--- src/tests/dev/md/h_mdserv.c:1.1	Tue Nov 23 15:38:54 2010
+++ src/tests/dev/md/h_mdserv.c	Tue Nov 30 14:29:05 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: h_mdserv.c,v 1.1 2010/11/23 15:38:54 pooka Exp $	*/
+/*	$NetBSD: h_mdserv.c,v 1.2 2010/11/30 14:29:05 pooka Exp $	*/
 
 #include <sys/types.h>
 #include <sys/mman.h>
@@ -6,28 +6,104 @@
 
 #include <dev/md.h>
 
+#include <err.h>
+#include <errno.h>
 #include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 
 #include <rump/rump.h>
 #include <rump/rump_syscalls.h>
 
-#include "../../h_macros.h"
-
 #define MDSIZE (1024*1024)
 
+#define REQUIRE(a, msg) if ((a) != 0) err(1, msg);
+
+static void *
+prober(void *arg)
+{
+	int fd, error;
+	char buf[4];
+	ssize_t n;
+
+	fd = rump_sys_open(arg, O_RDONLY);
+	for (;;) {
+		n = rump_sys_read(fd, buf, sizeof(buf));
+
+		switch (n) {
+		case 4:
+			error = 0;
+			goto out;
+
+		case -1:
+			if (errno == ENXIO) {
+				usleep(1000);
+				continue;
+			}
+
+			/* FALLTHROUGH */
+		default:
+			error = EPIPE;
+			goto out;
+		}
+	}
+ out:
+
+	error = rump_daemonize_done(error);
+	REQUIRE(error, "rump_daemonize_done");
+
+	if (error)
+		exit(1);
+
+	return NULL;
+}
+
 int
-main(void)
+main(int argc, char *argv[])
 {
+	pthread_t pt;
 	struct md_conf md;
-	int fd;
+	int fd, error;
+
+	if (argc != 2)
+		exit(1);
 
 	md.md_addr = malloc(MDSIZE);
 	md.md_size = MDSIZE;
 	md.md_type = MD_UMEM_SERVER;
 
-	RL(rump_init());
-	RL(fd = rump_sys_open("/dev/rmd0d", O_RDWR));
-	RL(rump_sys_ioctl(fd, MD_SETCONF, &md));
-	pause();
+	error = rump_daemonize_begin();
+	REQUIRE(error, "rump_daemonize_begin");
+
+	error = rump_init();
+	REQUIRE(error, "rump_init");
+
+	error = rump_init_server("unix://commsock");
+	REQUIRE(error, "init server");
+
+	if ((fd = rump_sys_open(argv[1], O_RDWR)) == -1)
+		err(1, "open");
+
+	/*
+	 * Now, configuring the md driver also causes our process
+	 * to start acting as the worker for the md.  Splitting is
+	 * into two steps in the driver is not easy, since md is
+	 * supposed to be unconfigured when the process dies
+	 * (process may exit between calling ioctl1 and ioctl2).
+	 * So, start a probe thread which attempt to read the md
+	 * and declares the md as configured when the read is
+	 * succesful.
+	 */
+	error = pthread_create(&pt, NULL, prober, argv[1]);
+	REQUIRE(error, "pthread_create");
+	pthread_detach(pt);
+
+	if (rump_sys_ioctl(fd, MD_SETCONF, &md) == -1) {
+		rump_daemonize_done(errno);
+		exit(1);
+	}
+
+	return 0;
 }
Index: src/tests/dev/md/t_md.sh
diff -u src/tests/dev/md/t_md.sh:1.1 src/tests/dev/md/t_md.sh:1.2
--- src/tests/dev/md/t_md.sh:1.1	Tue Nov 23 15:38:54 2010
+++ src/tests/dev/md/t_md.sh	Tue Nov 30 14:29:05 2010
@@ -1,4 +1,4 @@
-#	$NetBSD: t_md.sh,v 1.1 2010/11/23 15:38:54 pooka Exp $
+#	$NetBSD: t_md.sh,v 1.2 2010/11/30 14:29:05 pooka Exp $
 #
 # Copyright (c) 2010 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -35,12 +35,13 @@
 basic_body()
 {
 
-	RUMPSOCK=unix://commsock
-	env RUMP_SP_SERVER=${RUMPSOCK} $(atf_get_srcdir)/h_mdserv &
+	# Scope out raw part.  This is actually the *host* raw partition,
+	# but just let it slide for now, since they *should* be the same.
+	rawpart=`sysctl -n kern.rawpartition | tr '01234' 'abcde'`
 
-	sleep 1 # XXX: wait for server to start
+	atf_check -s exit:0 $(atf_get_srcdir)/h_mdserv /dev/rmd0${rawpart}
 
-	export RUMP_SP_CLIENT=${RUMPSOCK}
+	export RUMP_SERVER=unix://commsock
 	atf_check -s exit:0 -e ignore dd if=/bin/ls rof=/dev/rmd0d seek=100 count=10
 	atf_check -s exit:0 -e ignore dd of=testfile rif=/dev/rmd0d skip=100 count=10
 	atf_check -s exit:0 -e ignore -o file:testfile dd if=/bin/ls count=10

Reply via email to