From patchwork Tue Dec 8 09:51:36 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Perkin X-Patchwork-Id: 7796161 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-dash@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id BCDF9BEEE1 for ; Tue, 8 Dec 2015 09:52:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BE4DA203E6 for ; Tue, 8 Dec 2015 09:52:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 77A12203F3 for ; Tue, 8 Dec 2015 09:52:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933366AbbLHJwA (ORCPT ); Tue, 8 Dec 2015 04:52:00 -0500 Received: from mail-wm0-f43.google.com ([74.125.82.43]:32922 "EHLO mail-wm0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933419AbbLHJvy (ORCPT ); Tue, 8 Dec 2015 04:51:54 -0500 Received: by wmec201 with SMTP id c201so204401805wme.0 for ; Tue, 08 Dec 2015 01:51:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joyent.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=tlQi/A53FObHzpJzoimTutbFQxIbBjAjbNHOJQWC1Cc=; b=kjrWkiqKMZXFQite9ldx/Lfd9f+baoTfX8IFA7Xtb14ImPC6E3ZC1w7DDBOL7mDFDt mT9NLkLrOUFtSUnyyvO94paiCGA8NrO5MT7IH0znks6D8Nap3vjBukRhQeei+asIGSVX orzixJgES/nfFLQCMA7xhZCYVR6EJMOduuNlY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-type:content-disposition:in-reply-to :user-agent; bh=tlQi/A53FObHzpJzoimTutbFQxIbBjAjbNHOJQWC1Cc=; b=hZiVKW1E6LqRjjqO/tZUEazcGEkUi2VIOHIaGh7IolNMZs7vDesx5DAoj3F87Z7X+A T0NcvRna7oXdU0ji1iUOHqGaDosZuY67CnvOH2mM53/u6XpqHM4SXLXAEe3kJ4U50CiT l7h/xkJZxM14mTkVhx4N+Zm5JxTiV/okP8F/2fam2X8yWTjLdLSv/g7mTxxjdS/dreDI R0ukUCt5SkI9OnmqPdmbFZOQixMiALzqe6F5Qow7VGsOzrS7w8K1Xz4fJS2vML4I9jM4 iqkZgRwiH8qYuZdcQVuZThO767zNEUta8BNdpGV7OjjgqIxLxEKQia+ClP4XTHHnB3uE 4hFA== X-Gm-Message-State: ALoCoQkptV20+4K0zmUbieaxsI8ARgJV/xyrcqWiN3KBnu7aG8mtDMH4eyZsiqtUIRYafRvbSCzizfOW0cR1peUKIwaN0vdthQ== X-Received: by 10.194.223.39 with SMTP id qr7mr2636356wjc.63.1449568312692; Tue, 08 Dec 2015 01:51:52 -0800 (PST) Received: from joyent.com (host86-144-234-20.range86-144.btcentralplus.com. [86.144.234.20]) by smtp.gmail.com with ESMTPSA id b84sm2644164wmh.15.2015.12.08.01.51.51 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 08 Dec 2015 01:51:52 -0800 (PST) Date: Tue, 8 Dec 2015 09:51:36 +0000 From: Jonathan Perkin To: Herbert Xu Cc: dash@vger.kernel.org Subject: Re: [PATCH] trap: Implement POSIX.1-2008 trap reset behaviour (#2) Message-ID: <20151208095136.GA96236@joyent.com> References: <20151207175047.GG735@joyent.com> <20151208082451.GA17236@gondor.apana.org.au> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20151208082451.GA17236@gondor.apana.org.au> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID,T_RP_MATCHES_RCVD,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP * On 2015-12-08 at 08:25 GMT, Herbert Xu wrote: > Jonathan Perkin wrote: > > Clarifies a couple of issues with the previous patch, and expands on > > the rationale in the commit message. Sorry for the noise -- jperkin > > > > POSIX.1-2008 for trap adds the following behaviour: > > > > If the first operand is an unsigned decimal integer, the shell shall > > treat all operands as conditions, and shall reset each condition to > > the default value. > > > > The interpretation of this behaviour differs among other shells. In the > > case where the first operand is an invalid signo: > > > > bash-4.3.39(1), zsh-5.1.1: > > > > $ trap echo 1 2 3 > > $ trap 100 1 2 > > $ trap > > trap -- '100' SIGHUP > > trap -- '100' SIGINT > > trap -- 'echo' SIGQUIT > > So dash's behaviour is the same as bash. I'm not changing this. Ok, that's fair enough. New patch below which supports the new behaviour but matches bash for all cases, as well as retaining historical dash behaviour for invalid signos. 1. First operand an integer but invalid signo: bash-4.3.39(1), dash (without patch), dash (with patch) $ trap 100 1 2; trap trap -- '100' HUP trap -- '100' INT 2. First operand not an integer but valid signame: bash-4.3.39(1), dash (without patch), dash (with patch) $ trap HUP 2; trap trap -- '100' HUP trap -- 'HUP' INT 3. First operand an integer and valid signo: dash (without patch) $ trap 1 2; trap trap -- '100' HUP trap -- '1' INT bash-4.3.39(1), dash (with patch) $ trap 1 2; trap [no output] Hopefully that's acceptable. I've inlined most of the format-patch below, I didn't know if attachments are welcome to this list. Cheers, diff --git a/src/jobs.c b/src/jobs.c index c2c2332..0b1ae79 100644 --- a/src/jobs.c +++ b/src/jobs.c @@ -259,7 +259,7 @@ usage: } if (**++argv == '-') { - signo = decode_signal(*argv + 1, 1); + signo = decode_signal(*argv + 1, 1, 0); if (signo < 0) { int c; @@ -273,7 +273,7 @@ usage: list = 1; break; case 's': - signo = decode_signal(optionarg, 1); + signo = decode_signal(optionarg, 1, 0); if (signo < 0) { sh_error( "invalid signal number or name: %s", diff --git a/src/trap.c b/src/trap.c index 82d4263..f2988bc 100644 --- a/src/trap.c +++ b/src/trap.c @@ -112,12 +112,12 @@ trapcmd(int argc, char **argv) } return 0; } - if (!ap[1]) + if ((!ap[1]) || (decode_signal(*ap, 0, 1)) >= 0) action = NULL; else action = *ap++; while (*ap) { - if ((signo = decode_signal(*ap, 0)) < 0) { + if ((signo = decode_signal(*ap, 0, 0)) < 0) { outfmt(out2, "trap: %s: bad trap\n", *ap); return 1; } @@ -400,7 +400,7 @@ out: /* NOTREACHED */ } -int decode_signal(const char *string, int minsig) +int decode_signal(const char *string, int minsig, int numonly) { int signo; @@ -410,7 +410,8 @@ int decode_signal(const char *string, int minsig) return -1; } return signo; - } + } else if (numonly) + return -1; for (signo = minsig; signo < NSIG; signo++) { if (!strcasecmp(string, signal_names[signo])) { diff --git a/src/trap.h b/src/trap.h index 7573fd7..edbdb45 100644 --- a/src/trap.h +++ b/src/trap.h @@ -49,7 +49,7 @@ void onsig(int); void dotrap(void); void setinteractive(int); void exitshell(void) __attribute__((__noreturn__)); -int decode_signal(const char *, int); +int decode_signal(const char *, int, int); static inline int have_traps(void) {