[PATCH 1/2] test/smtp-dummy: add --background option for going background after listen(2)

Tomi Ollila tomi.ollila at iki.fi
Tue Dec 13 01:01:22 PST 2011


To avoid the possibility that smtp-dummy doesn't have chance to bind
its listening socket until something tries to send message to it this
option makes caller wait until socket is already listening for connections.

In case this --background option is used, the pid of running smtp-dummy
is printed on stdout.
---

Resent after whitespace-cleanup in patch 1/2 (this patch).

 test/smtp-dummy.c |   35 ++++++++++++++++++++++++++++++++++-
 1 files changed, 34 insertions(+), 1 deletions(-)

diff --git a/test/smtp-dummy.c b/test/smtp-dummy.c
index 3801a5e..9126c00 100644
--- a/test/smtp-dummy.c
+++ b/test/smtp-dummy.c
@@ -124,9 +124,21 @@ main (int argc, char *argv[])
 	struct hostent *hostinfo;
 	socklen_t peer_addr_len;
 	int reuse;
+	int bg;
+
+	/* XXX Quick implementation -- fix if more functionality is desired. */
+	if (argc >= 2 && strcmp(argv[1], "--background") == 0) {
+		argc--;
+		argv[1] = argv[0];
+		argv++;
+		bg = 1;
+	}
+	else
+		bg = 0;
 
 	if (argc != 2) {
-		fprintf (stderr, "Usage: %s <output-file>\n", argv[0]);
+		fprintf (stderr, "Usage: %s [--background] <output-file>\n",
+			 argv[0]);
 		return 1;
 	}
 
@@ -179,7 +191,27 @@ main (int argc, char *argv[])
 		return 1;
 	}
 
+	if (bg) {
+		int pid = fork ();
+		if (pid > 0) {
+			printf ("%d\n", pid);
+			return 0;
+		}
+		if (pid < 0) {
+			fprintf (stderr, "Error: fork() failed: %s\n",
+				 strerror (errno));
+			close (sock);
+			return 1;
+		}
+		/* Reached if pid == 0. */
+		/* Close stdout so that the one interested in pid value will
+		   also get EOF. */
+		close (1);
+		/* dup2() will re-reserve fd 1 (opportunistically, in case fd 2
+		   is open. If that was not open we don't care fd 1 either.) */
+		dup2 (2, 1);
+	}
+
 	peer_addr_len = sizeof (peer_addr);
 	peer = accept (sock, (struct sockaddr *) &peer_addr, &peer_addr_len);
 	if (peer == -1) {
-- 
1.7.7.3



More information about the notmuch mailing list