From: Peter Wang Date: Sun, 25 Nov 2012 01:16:42 +0000 (+1100) Subject: [PATCH v2 16/20] insert: trap SIGINT and clean up X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=b83e7f7a8f3b087102aa688062c206191ee97e0a;p=notmuch-archives.git [PATCH v2 16/20] insert: trap SIGINT and clean up --- diff --git a/46/c6c86c1bf1ff5d4cfc28509ecb02d6aabb2c21 b/46/c6c86c1bf1ff5d4cfc28509ecb02d6aabb2c21 new file mode 100644 index 000000000..a79e5b65d --- /dev/null +++ b/46/c6c86c1bf1ff5d4cfc28509ecb02d6aabb2c21 @@ -0,0 +1,139 @@ +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 06F4D431FAF + for ; Sat, 24 Nov 2012 17:18:36 -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 SF5CcSdigFkr for ; + Sat, 24 Nov 2012 17:18:35 -0800 (PST) +Received: from mail-pa0-f53.google.com (mail-pa0-f53.google.com + [209.85.220.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 7F6ED431FAE + for ; Sat, 24 Nov 2012 17:18:35 -0800 (PST) +Received: by mail-pa0-f53.google.com with SMTP id hz1so3911431pad.26 + for ; Sat, 24 Nov 2012 17:18:35 -0800 (PST) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; + h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; + bh=0TcysSCFUVeU5fnpLaZFvvTrATxquUbMYeavyoCVhWQ=; + b=FP05F1yekaVu3enYVbdrnUZXG/Z3oybND03OuQsVdnQqG9f65y3hSORPYreu+bPcsj + GvYfATftBdVhhgT8vc0NPS1EF4fc5MaiU25m0WU6QgGurNWBAJvIbRHgncS+7vpSMv/h + Ry81oEB9uOkiVD6bxmN1daYKDih4ewI/Q89hkIFJ9UKeEFK45Z39cQYakN7KfhioZTXE + 4EF9PRuYNA6LU+WAyVOdKBwzjx6nqkAwUtUjNnLUa7PO2ofcwM22ocAKT7B1Vu7ZzOKM + ZpTYbjjTnxQnwyadbDfQQyHZNfIlRrcrhJRyuzEzYP8SQvURyNi1Izhg5msfV7jWMP/D + 7N2w== +Received: by 10.68.209.230 with SMTP id mp6mr26312981pbc.8.1353806315268; + Sat, 24 Nov 2012 17:18:35 -0800 (PST) +Received: from localhost (215.42.233.220.static.exetel.com.au. + [220.233.42.215]) + by mx.google.com with ESMTPS id oj1sm6231031pbb.19.2012.11.24.17.18.32 + (version=TLSv1/SSLv3 cipher=OTHER); + Sat, 24 Nov 2012 17:18:34 -0800 (PST) +From: Peter Wang +To: notmuch@notmuchmail.org +Subject: [PATCH v2 16/20] insert: trap SIGINT and clean up +Date: Sun, 25 Nov 2012 12:16:42 +1100 +Message-Id: <1353806206-29133-17-git-send-email-novalazy@gmail.com> +X-Mailer: git-send-email 1.7.12.1 +In-Reply-To: <1353806206-29133-1-git-send-email-novalazy@gmail.com> +References: <1353806206-29133-1-git-send-email-novalazy@gmail.com> +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: Sun, 25 Nov 2012 01:18:36 -0000 + +The only potentially long-running part of the 'insert' command should be +copying stdin to the 'tmp' file. If SIGINT is received during the +copying process, abort and clean up the file in 'tmp'. At all other +points, just ignore the signal and continue. +--- + notmuch-insert.c | 27 +++++++++++++++++++++++++-- + 1 file changed, 25 insertions(+), 2 deletions(-) + +diff --git a/notmuch-insert.c b/notmuch-insert.c +index 831b322..28653ee 100644 +--- a/notmuch-insert.c ++++ b/notmuch-insert.c +@@ -24,6 +24,21 @@ + #include + #include + ++static volatile sig_atomic_t interrupted; ++ ++static void ++handle_sigint (unused (int sig)) ++{ ++ static char msg[] = "Stopping... \n"; ++ ++ /* This write is "opportunistic", so it's okay to ignore the ++ * result. It is not required for correctness, and if it does ++ * fail or produce a short write, we want to get out of the signal ++ * handler as quickly as possible, not retry it. */ ++ IGNORE_RESULT (write (2, msg, sizeof (msg) - 1)); ++ interrupted = 1; ++} ++ + /* Like gethostname but guarantees that a null-terminated hostname is + * returned, even if it has to make one up. + * Returns true unless hostname contains a slash. */ +@@ -241,7 +256,7 @@ copy_fd_data (int fdin, int fdout) + ssize_t remain; + ssize_t written; + +- for (;;) { ++ while (! interrupted) { + remain = read (fdin, buf, sizeof(buf)); + if (remain == 0) + break; +@@ -270,7 +285,7 @@ copy_fd_data (int fdin, int fdout) + } while (remain > 0); + } + +- return TRUE; ++ return ! interrupted; + } + + /* Add the specified message file to the notmuch database, applying tags. +@@ -382,6 +397,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[]) + { + notmuch_config_t *config; + notmuch_database_t *notmuch; ++ struct sigaction action; + const char *db_path; + const char **new_tags; + size_t new_tags_length; +@@ -428,6 +444,13 @@ notmuch_insert_command (void *ctx, int argc, char *argv[]) + return 1; + } + ++ /* Setup our handler for SIGINT */ ++ memset (&action, 0, sizeof (struct sigaction)); ++ action.sa_handler = handle_sigint; ++ sigemptyset (&action.sa_mask); ++ action.sa_flags = SA_RESTART; ++ sigaction (SIGINT, &action, NULL); ++ + config = notmuch_config_open (ctx, NULL, NULL); + if (config == NULL) + return 1; +-- +1.7.12.1 +