Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 096BF431FD0 for ; Mon, 19 Dec 2011 13:29:40 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.799 X-Spam-Level: X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Q6W73kDkKDGm for ; Mon, 19 Dec 2011 13:29:39 -0800 (PST) Received: from mail-wi0-f181.google.com (mail-wi0-f181.google.com [209.85.212.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id E7A5E431FB6 for ; Mon, 19 Dec 2011 13:29:38 -0800 (PST) Received: by wibhq2 with SMTP id hq2so1591735wib.26 for ; Mon, 19 Dec 2011 13:29:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:subject:in-reply-to:references:user-agent:date:message-id :mime-version:content-type; bh=PMKfCg/t4qCUDaXXBbaXEFcHYdGCWJ2QJiwHcM6YmaU=; b=HJFsRBPf7nDVTNfqnMeyYKa4BXRmbxETV9zBzXPy1A3SdoiqxHEXsaqehN/iYks7nk ltDbICtzsAFwzMZjD+Hd3Zulkkvcw6rtoJdjBBTf7bwmg92P2R9fCqoy8UdWL7X/32za I/ZnwfPqDvBQWSP54GtOhoXMaIjfM4d7YYFP4= Received: by 10.180.91.201 with SMTP id cg9mr2277512wib.15.1324330176412; Mon, 19 Dec 2011 13:29:36 -0800 (PST) Received: from localhost ([91.144.186.21]) by mx.google.com with ESMTPS id dy1sm27492245wib.18.2011.12.19.13.29.34 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 19 Dec 2011 13:29:35 -0800 (PST) From: Dmitry Kurochkin To: Tomi Ollila , notmuch@notmuchmail.org Subject: Re: [PATCH 1/2] test/smtp-dummy: add --background option for going background after listen(2) In-Reply-To: <1323766883-17607-1-git-send-email-tomi.ollila@iki.fi> References: <20111212161800.GO2760@mit.edu> <1323766883-17607-1-git-send-email-tomi.ollila@iki.fi> User-Agent: Notmuch/0.10.2+96~g74e5ae5 (http://notmuchmail.org) Emacs/23.3.1 (x86_64-pc-linux-gnu) Date: Tue, 20 Dec 2011 01:28:53 +0400 Message-ID: <874nwwxnca.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Dec 2011 21:29:40 -0000 Hi Tomi. On Tue, 13 Dec 2011 11:01:22 +0200, Tomi Ollila wrote: > 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; I would rename bg to background, but that is not important. > + > + /* 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; > Sorry, this code looks ugly and unnecessary complex to me. I really do not like messing with argc and argv. Perhaps something like this would be better: if (argc != 2 && argc != 3) usage(); return 1; if (argc > 2) { if (argv[1] == background) bg = 1; else usage(); return 1; } output = argv[argc - 1]; > if (argc != 2) { > - fprintf (stderr, "Usage: %s \n", argv[0]); > + fprintf (stderr, "Usage: %s [--background] \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); Please use STDOUT_FILENO instead of 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); And STDERR_FILENO and STDOUT_FILENO here. I would prefer to see "stdout" and "stderr" instead of "1" and "2" in the comments as well. Regards, Dmitry > + } > + > peer_addr_len = sizeof (peer_addr); > peer = accept (sock, (struct sockaddr *) &peer_addr, &peer_addr_len); > if (peer == -1) { > -- > 1.7.7.3 > > _______________________________________________ > notmuch mailing list > notmuch@notmuchmail.org > http://notmuchmail.org/mailman/listinfo/notmuch