unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Tomi Ollila <tomi.ollila@iki.fi>
To: notmuch@notmuchmail.org
Cc: Tomi Ollila <tomi.ollila@iki.fi>
Subject: [PATCH 1/2] test/smtp-dummy: add --background option for going background after listen(2)
Date: Tue, 13 Dec 2011 00:29:55 +0200	[thread overview]
Message-ID: <1323728996-15152-1-git-send-email-tomi.ollila@iki.fi> (raw)
In-Reply-To: <20111212161800.GO2760@mit.edu>

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.
---
 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,6 +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

  reply	other threads:[~2011-12-12 22:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-12 14:57 [PATCH] test/smtp-dummy: add --background option for backgrounding after listen(2) Tomi Ollila
2011-12-12 16:18 ` Austin Clements
2011-12-12 22:29   ` Tomi Ollila [this message]
2011-12-12 22:29     ` [PATCH 2/2] test/test-lib.sh: launch smtp-dummy with --background and finally kill(1) it Tomi Ollila
2011-12-13  9:01   ` [PATCH 1/2] test/smtp-dummy: add --background option for going background after listen(2) Tomi Ollila
2011-12-13  9:01     ` [PATCH 2/2] test/test-lib.sh: launch smtp-dummy with --background and finally kill(1) it Tomi Ollila
2011-12-19 21:28     ` [PATCH 1/2] test/smtp-dummy: add --background option for going background after listen(2) Dmitry Kurochkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1323728996-15152-1-git-send-email-tomi.ollila@iki.fi \
    --to=tomi.ollila@iki.fi \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).