[PATCH v3 16/20] insert: trap SIGINT and clean up
authorPeter Wang <novalazy@gmail.com>
Sun, 20 Jan 2013 00:50:00 +0000 (11:50 +1100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:53:14 +0000 (09:53 -0800)
ab/96be2a3970531cdfca10a2635be2c6598f67e5 [new file with mode: 0644]

diff --git a/ab/96be2a3970531cdfca10a2635be2c6598f67e5 b/ab/96be2a3970531cdfca10a2635be2c6598f67e5
new file mode 100644 (file)
index 0000000..bf43b50
--- /dev/null
@@ -0,0 +1,140 @@
+Return-Path: <novalazy@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 2E3B0429E34\r
+       for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:52:11 -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 Zr0FaIggHcyU for <notmuch@notmuchmail.org>;\r
+       Sat, 19 Jan 2013 16:52:10 -0800 (PST)\r
+Received: from mail-pa0-f42.google.com (mail-pa0-f42.google.com\r
+       [209.85.220.42]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 13A53429E3C\r
+       for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:51:52 -0800 (PST)\r
+Received: by mail-pa0-f42.google.com with SMTP id rl6so2773792pac.29\r
+       for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:51:51 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+       h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to\r
+       :references; bh=Ff7jWpxkQw2Fq70JM+kXXndzZkMnJ0zG1LWai5ZLAIQ=;\r
+       b=CKl1iaRRFnBkTxHLpUs4p9XzqN9/305LTTHnrL4p7cVzeSM3i1P4jFbkbSmu/ZU8YF\r
+       UliVeZrV2eg6ln9skDBzyAQBYK/oEaH0bFz+9v0HFh8lCK9SfHf+ERcK/SN16Y25E5h5\r
+       GDQTp6LTKg2hfyFRP1gmD2ugsNyBKIr9PlDgh/yzcpcQI8Sv4la1p0Mrapx5KmynxD/6\r
+       Gpm1AQjImyNnhYucnBL3YNXhPLOLVW3Y5zFKCnmd1aL0hIEmgCi+pgXwJPhwkJ9maXBL\r
+       39zaiTMYrgSGCxOMh3zxD3kkRB0XZTrZD8zLX0BKx/DXwQffTqoZJsoZ8zWRsrse+V7N\r
+       sg+w==\r
+X-Received: by 10.66.75.66 with SMTP id a2mr36252872paw.65.1358643111159;\r
+       Sat, 19 Jan 2013 16:51:51 -0800 (PST)\r
+Received: from localhost (215.42.233.220.static.exetel.com.au.\r
+       [220.233.42.215])\r
+       by mx.google.com with ESMTPS id nw9sm5801009pbb.42.2013.01.19.16.51.49\r
+       (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+       Sat, 19 Jan 2013 16:51:50 -0800 (PST)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 16/20] insert: trap SIGINT and clean up\r
+Date: Sun, 20 Jan 2013 11:50:00 +1100\r
+Message-Id: <1358643004-14522-17-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1358643004-14522-1-git-send-email-novalazy@gmail.com>\r
+References: <1358643004-14522-1-git-send-email-novalazy@gmail.com>\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: Sun, 20 Jan 2013 00:52:11 -0000\r
+\r
+The only potentially long-running part of the 'insert' command should be\r
+copying stdin to the 'tmp' file.  If SIGINT is received during the\r
+copying process, abort and clean up the file in 'tmp'.  At all other\r
+points, just ignore the signal and continue.\r
+---\r
+ notmuch-insert.c | 28 ++++++++++++++++++++++++++--\r
+ 1 file changed, 26 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 8012eb4..494a7b0 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -25,6 +25,21 @@\r
+ #include <sys/stat.h>\r
+ #include <fcntl.h>\r
\r
++static volatile sig_atomic_t interrupted;\r
++\r
++static void\r
++handle_sigint (unused (int sig))\r
++{\r
++    static char msg[] = "Stopping...         \n";\r
++\r
++    /* This write is "opportunistic", so it's okay to ignore the\r
++     * result.  It is not required for correctness, and if it does\r
++     * fail or produce a short write, we want to get out of the signal\r
++     * handler as quickly as possible, not retry it. */\r
++    IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));\r
++    interrupted = 1;\r
++}\r
++\r
+ /* Like gethostname but guarantees that a null-terminated hostname is\r
+  * returned, even if it has to make one up.\r
+  * Returns true unless hostname contains a slash. */\r
+@@ -258,7 +273,7 @@ copy_stdin (int fdin, int fdout)\r
+     ssize_t remain;\r
+     ssize_t written;\r
\r
+-    for (;;) {\r
++    while (! interrupted) {\r
+       remain = read (fdin, buf, sizeof (buf));\r
+       if (remain == 0)\r
+           break;\r
+@@ -287,7 +302,7 @@ copy_stdin (int fdin, int fdout)\r
+       } while (remain > 0);\r
+     }\r
\r
+-    return TRUE;\r
++    return ! interrupted;\r
+ }\r
\r
+ /* Add the specified message file to the notmuch database, applying tags.\r
+@@ -372,6 +387,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])\r
+ {\r
+     notmuch_config_t *config;\r
+     notmuch_database_t *notmuch;\r
++    struct sigaction action;\r
+     const char *db_path;\r
+     const char **new_tags;\r
+     size_t new_tags_length;\r
+@@ -443,6 +459,14 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])\r
+       return 1;\r
+     }\r
\r
++    /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying\r
++     * from standard input may be interrupted. */\r
++    memset (&action, 0, sizeof (struct sigaction));\r
++    action.sa_handler = handle_sigint;\r
++    sigemptyset (&action.sa_mask);\r
++    action.sa_flags = 0;\r
++    sigaction (SIGINT, &action, NULL);\r
++\r
+     if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+                              NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
+       return 1;\r
+-- \r
+1.7.12.1\r
+\r