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 2E3B0429E34 for ; Sat, 19 Jan 2013 16:52:11 -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 Zr0FaIggHcyU for ; Sat, 19 Jan 2013 16:52:10 -0800 (PST) Received: from mail-pa0-f42.google.com (mail-pa0-f42.google.com [209.85.220.42]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 13A53429E3C for ; Sat, 19 Jan 2013 16:51:52 -0800 (PST) Received: by mail-pa0-f42.google.com with SMTP id rl6so2773792pac.29 for ; Sat, 19 Jan 2013 16:51:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=Ff7jWpxkQw2Fq70JM+kXXndzZkMnJ0zG1LWai5ZLAIQ=; b=CKl1iaRRFnBkTxHLpUs4p9XzqN9/305LTTHnrL4p7cVzeSM3i1P4jFbkbSmu/ZU8YF UliVeZrV2eg6ln9skDBzyAQBYK/oEaH0bFz+9v0HFh8lCK9SfHf+ERcK/SN16Y25E5h5 GDQTp6LTKg2hfyFRP1gmD2ugsNyBKIr9PlDgh/yzcpcQI8Sv4la1p0Mrapx5KmynxD/6 Gpm1AQjImyNnhYucnBL3YNXhPLOLVW3Y5zFKCnmd1aL0hIEmgCi+pgXwJPhwkJ9maXBL 39zaiTMYrgSGCxOMh3zxD3kkRB0XZTrZD8zLX0BKx/DXwQffTqoZJsoZ8zWRsrse+V7N sg+w== X-Received: by 10.66.75.66 with SMTP id a2mr36252872paw.65.1358643111159; Sat, 19 Jan 2013 16:51:51 -0800 (PST) Received: from localhost (215.42.233.220.static.exetel.com.au. [220.233.42.215]) by mx.google.com with ESMTPS id nw9sm5801009pbb.42.2013.01.19.16.51.49 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 19 Jan 2013 16:51:50 -0800 (PST) From: Peter Wang To: notmuch@notmuchmail.org Subject: [PATCH v3 16/20] insert: trap SIGINT and clean up Date: Sun, 20 Jan 2013 11:50:00 +1100 Message-Id: <1358643004-14522-17-git-send-email-novalazy@gmail.com> X-Mailer: git-send-email 1.7.12.1 In-Reply-To: <1358643004-14522-1-git-send-email-novalazy@gmail.com> References: <1358643004-14522-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, 20 Jan 2013 00:52:11 -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 | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index 8012eb4..494a7b0 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -25,6 +25,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. */ @@ -258,7 +273,7 @@ copy_stdin (int fdin, int fdout) ssize_t remain; ssize_t written; - for (;;) { + while (! interrupted) { remain = read (fdin, buf, sizeof (buf)); if (remain == 0) break; @@ -287,7 +302,7 @@ copy_stdin (int fdin, int fdout) } while (remain > 0); } - return TRUE; + return ! interrupted; } /* Add the specified message file to the notmuch database, applying tags. @@ -372,6 +387,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; @@ -443,6 +459,14 @@ notmuch_insert_command (void *ctx, int argc, char *argv[]) return 1; } + /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying + * from standard input may be interrupted. */ + memset (&action, 0, sizeof (struct sigaction)); + action.sa_handler = handle_sigint; + sigemptyset (&action.sa_mask); + action.sa_flags = 0; + sigaction (SIGINT, &action, NULL); + if (notmuch_database_open (notmuch_config_get_database_path (config), NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) return 1; -- 1.7.12.1