Re: [PATCH] Properly handle short writes in sigint handlers
authorDmitry Kurochkin <dmitry.kurochkin@gmail.com>
Fri, 23 Dec 2011 19:10:35 +0000 (23:10 +0400)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:41:14 +0000 (09:41 -0800)
19/d9fc1d215e37cefb423d0746450adedc590fce [new file with mode: 0644]

diff --git a/19/d9fc1d215e37cefb423d0746450adedc590fce b/19/d9fc1d215e37cefb423d0746450adedc590fce
new file mode 100644 (file)
index 0000000..df3729f
--- /dev/null
@@ -0,0 +1,126 @@
+Return-Path: <dmitry.kurochkin@gmail.com>\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 1C1E2429E25\r
+       for <notmuch@notmuchmail.org>; Fri, 23 Dec 2011 11:11:22 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+       tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+       FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] 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 3Iu31urD37b8 for <notmuch@notmuchmail.org>;\r
+       Fri, 23 Dec 2011 11:11:21 -0800 (PST)\r
+Received: from mail-ww0-f41.google.com (mail-ww0-f41.google.com\r
+ [74.125.82.41])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 71535431FB6   for <notmuch@notmuchmail.org>; Fri, 23 Dec 2011 11:11:21 -0800\r
+ (PST)\r
+Received: by wgbdt12 with SMTP id dt12so12843645wgb.2\r
+       for <notmuch@notmuchmail.org>; Fri, 23 Dec 2011 11:11:20 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
+       h=from:to:subject:in-reply-to:references:user-agent:date:message-id\r
+       :mime-version:content-type;\r
+       bh=Z07GRNDw2pMdGXC+EhtiigHqvTeYyeOsoPYvmX0tswo=;\r
+       b=Ba04w3rrygJl6vjsVsoSInjNWnsBxMR/A3EK0pxB93VVaqf+cv3niDKIdwE2bZ+Swm\r
+       sl1cNBdTZekN4499Hk69cCx3+u42+ts9G1xBXGczM/qBOyANdFGugQPNWE12r1GLrPfj\r
+       0/la9jKKBC/F9yFtxgPiFQEmG7AXT0o2MrJ1c=\r
+Received: by 10.216.209.99 with SMTP id r77mr8692749weo.25.1324667480243;\r
+       Fri, 23 Dec 2011 11:11:20 -0800 (PST)\r
+Received: from localhost ([91.144.186.21]) by mx.google.com with ESMTPS id\r
+       ej17sm14685080wbb.14.2011.12.23.11.11.18\r
+       (version=TLSv1/SSLv3 cipher=OTHER);\r
+       Fri, 23 Dec 2011 11:11:19 -0800 (PST)\r
+From: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH] Properly handle short writes in sigint handlers\r
+In-Reply-To: <1324584948-8009-1-git-send-email-amdragon@mit.edu>\r
+References: <20111222201553.GK10376@mit.edu>\r
+       <1324584948-8009-1-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.10.2+116~g4c449d4 (http://notmuchmail.org) Emacs/23.3.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Fri, 23 Dec 2011 23:10:35 +0400\r
+Message-ID: <87fwgbkst0.fsf@gmail.com>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\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: Fri, 23 Dec 2011 19:11:22 -0000\r
+\r
+Hi Austin.\r
+\r
+On Thu, 22 Dec 2011 15:15:48 -0500, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> Even if we don't care about errors from write(2), it's still necessary\r
+> to handle short writes in order to use write correctly.  Some versions\r
+> of glibc even mark write as warn_unused_result because of this, so our\r
+> previous usage would trigger compiler warnings.\r
+\r
+I think we should put the write loop into a separate function and reuse\r
+it.\r
+\r
+Also, does it make sense to add a retry counter to prevent infinite loop\r
+if write keeps returning 0?\r
+\r
+Regards,\r
+  Dmitry\r
+\r
+> ---\r
+>  notmuch-new.c |    5 ++++-\r
+>  notmuch-tag.c |    6 +++++-\r
+>  2 files changed, 9 insertions(+), 2 deletions(-)\r
+> \r
+> diff --git a/notmuch-new.c b/notmuch-new.c\r
+> index 3512de7..fc09bbb 100644\r
+> --- a/notmuch-new.c\r
+> +++ b/notmuch-new.c\r
+> @@ -66,8 +66,11 @@ static void\r
+>  handle_sigint (unused (int sig))\r
+>  {\r
+>      static char msg[] = "Stopping...         \n";\r
+> +    const char *pos = msg, *end = msg + sizeof (msg) - 1;\r
+> +    ssize_t c = 0;\r
+>  \r
+> -    (void) write(2, msg, sizeof(msg)-1);\r
+> +    for (; pos < end && c >= 0; pos += c)\r
+> +    c = write (2, pos, end - pos);\r
+>      interrupted = 1;\r
+>  }\r
+>  \r
+> diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+> index 292c5da..0d4873d 100644\r
+> --- a/notmuch-tag.c\r
+> +++ b/notmuch-tag.c\r
+> @@ -26,7 +26,11 @@ static void\r
+>  handle_sigint (unused (int sig))\r
+>  {\r
+>      static char msg[] = "Stopping...         \n";\r
+> -    (void) write(2, msg, sizeof(msg)-1);\r
+> +    const char *pos = msg, *end = msg + sizeof (msg) - 1;\r
+> +    ssize_t c = 0;\r
+> +\r
+> +    for (; pos < end && c >= 0; pos += c)\r
+> +    c = write (2, pos, end - pos);\r
+>      interrupted = 1;\r
+>  }\r
+>  \r
+> -- \r
+> 1.7.7.3\r
+> \r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r