Re: [PATCH V3 1/2] test/smtp-dummy: add --background option and functionality
authorMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 20 Sep 2012 16:37:51 +0000 (18:37 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:49:33 +0000 (09:49 -0800)
0e/8d7870912fa5a255aea67aef9463550a835ab1 [new file with mode: 0644]

diff --git a/0e/8d7870912fa5a255aea67aef9463550a835ab1 b/0e/8d7870912fa5a255aea67aef9463550a835ab1
new file mode 100644 (file)
index 0000000..67c0b1d
--- /dev/null
@@ -0,0 +1,176 @@
+Return-Path: <sojkam1@fel.cvut.cz>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 385B9431FAF\r
+       for <notmuch@notmuchmail.org>; Thu, 20 Sep 2012 09:38:04 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id OADjm0cGuJWs for <notmuch@notmuchmail.org>;\r
+       Thu, 20 Sep 2012 09:38:03 -0700 (PDT)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+       by olra.theworths.org (Postfix) with ESMTP id C1FEA431FAE\r
+       for <notmuch@notmuchmail.org>; Thu, 20 Sep 2012 09:38:02 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.4])\r
+       by max.feld.cvut.cz (Postfix) with ESMTP id CBE9F19F33B0;\r
+       Thu, 20 Sep 2012 18:37:57 +0200 (CEST)\r
+X-Virus-Scanned: IMAP AMAVIS\r
+Received: from max.feld.cvut.cz ([192.168.200.1])\r
+       by localhost (styx.feld.cvut.cz [192.168.200.4]) (amavisd-new,\r
+       port 10044)\r
+       with ESMTP id X4nqNXQtRn0q; Thu, 20 Sep 2012 18:37:55 +0200 (CEST)\r
+Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
+       by max.feld.cvut.cz (Postfix) with ESMTP id DE76E19F3392;\r
+       Thu, 20 Sep 2012 18:37:55 +0200 (CEST)\r
+Received: from steelpick.2x.cz (note-sojka.felk.cvut.cz [147.32.86.30])\r
+       (Authenticated sender: sojkam1)\r
+       by imap.feld.cvut.cz (Postfix) with ESMTPSA id BC95E660904;\r
+       Thu, 20 Sep 2012 18:37:52 +0200 (CEST)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.80)\r
+       (envelope-from <sojkam1@fel.cvut.cz>)\r
+       id 1TEjl9-0003E9-Ub; Thu, 20 Sep 2012 18:37:51 +0200\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: Tomi Ollila <tomi.ollila@iki.fi>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH V3 1/2] test/smtp-dummy: add --background option and\r
+       functionality\r
+In-Reply-To: <1347978182-8771-1-git-send-email-tomi.ollila@iki.fi>\r
+References: <1347978182-8771-1-git-send-email-tomi.ollila@iki.fi>\r
+User-Agent: Notmuch/0.14+23~g9d68aca (http://notmuchmail.org) Emacs/24.2.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Thu, 20 Sep 2012 18:37:51 +0200\r
+Message-ID: <87zk4k4pls.fsf@steelpick.2x.cz>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\r
+Cc: Tomi Ollila <too@iki.fi>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 20 Sep 2012 16:38:04 -0000\r
+\r
+Hi Tomi,\r
+\r
+On Tue, Sep 18 2012, Tomi Ollila wrote:\r
+> From: Tomi Ollila <too@iki.fi>\r
+>\r
+> When shell executes background process using '&' the scheduling of\r
+> that new process is arbitrary. It could be that smtp-dummy doesn't\r
+> get execution time to listen() it's server socket until some other\r
+> process attempts to connect() to it. The --background option in\r
+> smtp-dummy makes it to go background *after* it started to listen\r
+> its server socket.\r
+>\r
+> When --background option is used, the line "smtp_dummy_pid='<pid>'"\r
+> is printed to stdout from where shell can eval it.\r
+> ---\r
+>\r
+> This is v3 of id:"1323766883-17607-1-git-send-email-tomi.ollila@iki.fi"\r
+>\r
+> addressing (some) Dmitry's comments.\r
+>\r
+>  test/smtp-dummy.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-\r
+>  1 file changed, 54 insertions(+), 1 deletion(-)\r
+>\r
+> diff --git a/test/smtp-dummy.c b/test/smtp-dummy.c\r
+> index 86d4316..bb13668 100644\r
+> --- a/test/smtp-dummy.c\r
+> +++ b/test/smtp-dummy.c\r
+> @@ -119,6 +119,7 @@ do_smtp_to_file (FILE *peer, FILE *output)\r
+>  int\r
+>  main (int argc, char *argv[])\r
+>  {\r
+> +    const char * progname;\r
+>      char *output_filename;\r
+>      FILE *peer_file, *output;\r
+>      int sock, peer, err;\r
+> @@ -126,9 +127,31 @@ main (int argc, char *argv[])\r
+>      struct hostent *hostinfo;\r
+>      socklen_t peer_addr_len;\r
+>      int reuse;\r
+> +    int background;\r
+> +\r
+> +    progname = argv[0];\r
+> +\r
+> +    background = 0;\r
+> +    for (; argc >= 2; argc--, argv++) {\r
+> +            if (argv[1][0] != '-')\r
+\r
+This is ugly, but all right. I do not think we need to have high\r
+standards for this piece of code :)\r
+\r
+> +                    break;\r
+> +            if (strcmp (argv[1], "--") == 0) {\r
+> +                    argc--;\r
+> +                    argv++;\r
+> +                    break;\r
+> +            }\r
+> +            if (strcmp (argv[1], "--background") == 0) {\r
+> +                    background = 1;\r
+> +                    continue;\r
+> +            }\r
+> +            fprintf(stderr, "%s: unregognized option '%s'\n",\r
+> +                    progname, argv[1]);\r
+> +            return 1;\r
+> +    }\r
+>\r
+>      if (argc != 2) {\r
+> -            fprintf (stderr, "Usage: %s <output-file>\n", argv[0]);\r
+> +            fprintf (stderr,\r
+> +                     "Usage: %s [--background] <output-file>\n", progname);\r
+>              return 1;\r
+>      }\r
+>\r
+> @@ -181,6 +204,36 @@ main (int argc, char *argv[])\r
+>              return 1;\r
+>      }\r
+>\r
+> +    if (background) {\r
+> +            int pid = fork ();\r
+> +            if (pid > 0) {\r
+> +                    printf ("smtp_dummy_pid='%d'\n", pid);\r
+> +                    fflush (stdout);\r
+> +                    close (sock);\r
+> +                    return 0;\r
+> +            }\r
+> +            if (pid < 0) {\r
+> +                    fprintf (stderr, "Error: fork() failed: %s\n",\r
+> +                             strerror (errno));\r
+> +                    close (sock);\r
+> +                    return 1;\r
+> +            }\r
+> +            /* Reached if pid == 0 (the child process). */\r
+> +            /* Close stdout so that the one interested in pid value will\r
+> +               also get EOF. */\r
+> +            close (STDOUT_FILENO);\r
+> +            /* dup2() will re-reserve fd of stdout (1) (opportunistically),\r
+> +               in case fd of stderr (2) is open. If that was not open we\r
+> +               don't care fd of stdout (1) either. */\r
+> +            dup2 (STDERR_FILENO, STDOUT_FILENO);\r
+> +\r
+> +            /* This process is now out of reach of shell's job control.\r
+> +               To resolve the rare but possible condition where this\r
+> +               "daemon" is started but never connected this process will\r
+> +               (only) have 30 seconds to exist. */\r
+> +            alarm (30);\r
+\r
+30 seconds is probably enough. If it turns out that we need longer\r
+timeout in some scenario, I'd suggest to call alarm() also when\r
+something is received on the socket to extend the timeout.\r
+\r
+Overall, both patches LGTM.\r
+\r
+-Michal\r