Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Atomic batteries to power, turbines to speed." -- Robin, The Boy Wonder


devel / comp.unix.shell / "trap" and catching multiple signals

SubjectAuthor
* "trap" and catching multiple signalsKenny McCormack
+* Re: "trap" and catching multiple signalsjoerg
|+- Re: "trap" and catching multiple signalsKenny McCormack
|`* Re: "trap" and catching multiple signalsEd Morton
| `* Re: "trap" and catching multiple signalsKaz Kylheku
|  `* Re: "trap" and catching multiple signalsBen Bacarisse
|   `* Re: "trap" and catching multiple signalsKenny McCormack
|    `- Re: "trap" and catching multiple signalsWilliam Unruh
`- Re: "trap" and catching multiple signalsJanis Papanagnou

1
"trap" and catching multiple signals

<sak25t$2d07d$2@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3959&group=comp.unix.shell#3959

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: "trap" and catching multiple signals
Date: Sat, 19 Jun 2021 06:17:33 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sak25t$2d07d$2@news.xmission.com>
Injection-Date: Sat, 19 Jun 2021 06:17:33 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2523373"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 19 Jun 2021 06:17 UTC

Suppose you do:

handler() { ... }
trap handler INT QUIT

Any collection of signals would work; I am using INT and QUIT here for
convenience.

The point is that you have one handler for both (i.e., all of the) signals,
and you'd like to be able to distinguish, inside of handler(), which signal
you are handling. You'd like for there to be some variable that holds the
signal number.

In C, this is straightforward. It seems like it should be so in shell as
well, but I could not find anything in the man page about it.

Note: I'm specifically interested in bash, but would be interested to read
about any other shell(s) that readers have used.

--
The scent of awk programmers is a lot more attractive to women than
the scent of perl programmers.

(Mike Brennan, quoted in the "GAWK" manual)

Re: "trap" and catching multiple signals

<sakm7e$nn8$1@news.dns-netz.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3960&group=comp.unix.shell#3960

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!news.dns-netz.com!news.freedyn.net!.POSTED!not-for-mail
From: joe...@schily.net
Newsgroups: comp.unix.shell
Subject: Re: "trap" and catching multiple signals
Date: Sat, 19 Jun 2021 11:59:43 -0000 (UTC)
Message-ID: <sakm7e$nn8$1@news.dns-netz.com>
References: <sak25t$2d07d$2@news.xmission.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: eJxdjcEKwjAQBc/6FUtOCqY2sQppCRS8eVU/IKQhjaSbYraIfr3x6pxn3kP3ytWAmaOjT2XT1MGcMgX03FibFiTNHsk9fZ/tGOK7Kh7r1quYvP9JgyGjmWykOrEOJhMiLyNzNAEpc0qaLdmVpsf/IwZ7uBragVBwWRBkLQUI0R5V2xyA1wXY3G/n7Rc7FDRr
Cancel-Lock: sha1:o6xIYpf+ZNJ00NJ5Astacgiv02E=
X-Newsreader: trn 4.0-test76 (Apr 2, 2001)
X-Abuse-Contact: "abuse@dns-netz.com"
 by: joe...@schily.net - Sat, 19 Jun 2021 11:59 UTC

In article <sak25t$2d07d$2@news.xmission.com>,
Kenny McCormack <gazelle@shell.xmission.com> wrote:
>Suppose you do:
>
>handler() { ... }
>trap handler INT QUIT
>
>Any collection of signals would work; I am using INT and QUIT here for
>convenience.
>
>The point is that you have one handler for both (i.e., all of the) signals,
>and you'd like to be able to distinguish, inside of handler(), which signal
>you are handling. You'd like for there to be some variable that holds the
>signal number.

This has been discussed in the POSIX standardization, but there is currently
no enhancement request.

Recent versions of the Bourne Shell (bosh) since summer 2015 set
${.sh.signame} while excuting the trap handler.

This was part of the conversions to use waitid() from 1988 instead of the
historical wait() call in order to be able to support all 32 bits from the
exit code of programs.

--
EMail:joerg@schily.net Jörg Schilling D-13353 Berlin
Blog: http://schily.blogspot.com/
URL: http://cdrecord.org/private/ http://sourceforge.net/projects/schilytools/files/

Re: "trap" and catching multiple signals

<sakrkk$2ddln$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3961&group=comp.unix.shell#3961

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: "trap" and catching multiple signals
Date: Sat, 19 Jun 2021 13:32:04 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sakrkk$2ddln$1@news.xmission.com>
References: <sak25t$2d07d$2@news.xmission.com> <sakm7e$nn8$1@news.dns-netz.com>
Injection-Date: Sat, 19 Jun 2021 13:32:04 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2537143"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 19 Jun 2021 13:32 UTC

In article <sakm7e$nn8$1@news.dns-netz.com>, <joerg@schily.net> wrote:
>In article <sak25t$2d07d$2@news.xmission.com>,
>Kenny McCormack <gazelle@shell.xmission.com> wrote:
>>Suppose you do:
>>
>>handler() { ... }
>>trap handler INT QUIT
>>
>>Any collection of signals would work; I am using INT and QUIT here for
>>convenience.
>>
>>The point is that you have one handler for both (i.e., all of the) signals,
>>and you'd like to be able to distinguish, inside of handler(), which signal
>>you are handling. You'd like for there to be some variable that holds the
>>signal number.
>
>This has been discussed in the POSIX standardization, but there is currently
>no enhancement request.
>
>Recent versions of the Bourne Shell (bosh) since summer 2015 set
>${.sh.signame} while excuting the trap handler.
>
>This was part of the conversions to use waitid() from 1988 instead of the
>historical wait() call in order to be able to support all 32 bits from the
>exit code of programs.

Very interesting. Thanks.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/WeekendAwayFromHome

Re: "trap" and catching multiple signals

<saks5h$43h$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3962&group=comp.unix.shell#3962

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: mortons...@gmail.com (Ed Morton)
Newsgroups: comp.unix.shell
Subject: Re: "trap" and catching multiple signals
Date: Sat, 19 Jun 2021 08:41:05 -0500
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <saks5h$43h$1@dont-email.me>
References: <sak25t$2d07d$2@news.xmission.com>
<sakm7e$nn8$1@news.dns-netz.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 19 Jun 2021 13:41:05 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="381d56e484972f0063148a8ba406a24c";
logging-data="4209"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KYNZM98shE+hYlftTjqUq"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:x6RQmUXrCR6GKVlHL6wBQ+q/YM0=
In-Reply-To: <sakm7e$nn8$1@news.dns-netz.com>
X-Antivirus-Status: Clean
Content-Language: en-US
X-Antivirus: Avast (VPS 210618-4, 06/18/2021), Outbound message
 by: Ed Morton - Sat, 19 Jun 2021 13:41 UTC

On 6/19/2021 6:59 AM, joerg@schily.net wrote:
> In article <sak25t$2d07d$2@news.xmission.com>,
> Kenny McCormack <gazelle@shell.xmission.com> wrote:
>> Suppose you do:
>>
>> handler() { ... }
>> trap handler INT QUIT
>>
>> Any collection of signals would work; I am using INT and QUIT here for
>> convenience.
>>
>> The point is that you have one handler for both (i.e., all of the) signals,
>> and you'd like to be able to distinguish, inside of handler(), which signal
>> you are handling. You'd like for there to be some variable that holds the
>> signal number.

It just takes one small function to implement it yourself:

--------
trap_handler() {
printf 'handling signal %s\n' "$1"
....
}

set_traps() {
for sig; do
trap "trap_handler $sig" "$sig"
done
}

set_traps INT QUIT
--------

Massage to suit wrt whether or not to pass the handler name as an
argument to set_traps and whether to define set_traps within your script
or externally, e.g. in .profile.

Regards,

Ed.

Re: "trap" and catching multiple signals

<saktri$2vh$1@news-1.m-online.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3963&group=comp.unix.shell#3963

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!news.mixmin.net!news2.arglkargh.de!news.karotte.org!news.space.net!news.m-online.net!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: "trap" and catching multiple signals
Date: Sat, 19 Jun 2021 16:09:54 +0200
Organization: (posted via) M-net Telekommunikations GmbH
Lines: 72
Message-ID: <saktri$2vh$1@news-1.m-online.net>
References: <sak25t$2d07d$2@news.xmission.com>
NNTP-Posting-Host: 2001:a61:252a:da01:a17e:aa73:c82f:3f8b
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
X-Trace: news-1.m-online.net 1624111794 3057 2001:a61:252a:da01:a17e:aa73:c82f:3f8b (19 Jun 2021 14:09:54 GMT)
X-Complaints-To: news@news-1.m-online.net
NNTP-Posting-Date: Sat, 19 Jun 2021 14:09:54 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
X-Enigmail-Draft-Status: N1110
In-Reply-To: <sak25t$2d07d$2@news.xmission.com>
 by: Janis Papanagnou - Sat, 19 Jun 2021 14:09 UTC

On 19.06.2021 08:17, Kenny McCormack wrote:
> Suppose you do:
>
> handler() { ... }
> trap handler INT QUIT
>
> Any collection of signals would work; I am using INT and QUIT here for
> convenience.
>
> The point is that you have one handler for both (i.e., all of the) signals,
> and you'd like to be able to distinguish, inside of handler(), which signal
> you are handling. You'd like for there to be some variable that holds the
> signal number.

I don't know any feature or straightforward workaround to achieve
that with the prominent shells.

I observe - correct me if I am wrong - and want to note that this
would require some decision making in the handler function, like

case $1 in ## just an informal use of $1
(INT) echo processing INT ;;
(QUIT) echo processing QUIT ;;
(*) echo huh ;;
esac

thus explicitly enumerating the signals (all of them or a desired
subset). This appears to me to be an inherent suboptimal approach.
When might that have positive aspects? Maybe if we build subsets?

case $1 in
(INT|QUIT) echo processing type A ;;
(HUP|TERM) echo processing type B ;;
(*) echo huh ;;
esac

Is there an advantage to

trap process_handler_A INT QUIT
trap process_handler_B HUP TERM

We can think of common (complex) code passages that we want coded
just once (not repeated in every trap handler), but we can rely on
a separate common function to achieve that.

Of course we could also add auxiliary shell code to pass the signal
name to the trap handler function (supporting syntactic sugar here)

mytrap() {
for t do
trap "handle $t" $t
done
}

mytrap INT QUIT

or slightly adjust the mytrap code to support

mytrap handler INT QUIT

So, while the feature appeared potentially useful to me at first,
I'm now after pondering about it not sure in which cases it actually
has any (or even a significant) gain.

Can you provide an example or application cases to make the advantage
of such a feature more obvious?

Janis

> [...]

Re: "trap" and catching multiple signals

<20210619084849.706@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3965&group=comp.unix.shell#3965

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 563-365-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: "trap" and catching multiple signals
Date: Sat, 19 Jun 2021 15:56:27 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <20210619084849.706@kylheku.com>
References: <sak25t$2d07d$2@news.xmission.com>
<sakm7e$nn8$1@news.dns-netz.com> <saks5h$43h$1@dont-email.me>
Injection-Date: Sat, 19 Jun 2021 15:56:27 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="65ae2e2de3b35ce1dda3b029f8d181a7";
logging-data="15248"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19JmehyyjbVzE2Kd6xnuKVpR+uqx+Gfkms="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:PuCsK0hmSBZkcfSjag1xbk13D2g=
 by: Kaz Kylheku - Sat, 19 Jun 2021 15:56 UTC

On 2021-06-19, Ed Morton <mortonspam@gmail.com> wrote:
> On 6/19/2021 6:59 AM, joerg@schily.net wrote:
>> In article <sak25t$2d07d$2@news.xmission.com>,
>> Kenny McCormack <gazelle@shell.xmission.com> wrote:
>>> Suppose you do:
>>>
>>> handler() { ... }
>>> trap handler INT QUIT
>>>
>>> Any collection of signals would work; I am using INT and QUIT here for
>>> convenience.
>>>
>>> The point is that you have one handler for both (i.e., all of the) signals,
>>> and you'd like to be able to distinguish, inside of handler(), which signal
>>> you are handling. You'd like for there to be some variable that holds the
>>> signal number.
>
> It just takes one small function to implement it yourself:
>
> --------
> trap_handler() {
> printf 'handling signal %s\n' "$1"
> ....
> }
>
> set_traps() {
> for sig; do
> trap "trap_handler $sig" "$sig"
> done
> }

Good call.

The trap handler is a full command, not simply the name of a function,
and that command can bind a function to whatever captured arguments
it needs.

It's like partial function application that is entirely based on
character strings.

(Note that lambda calculus can be implemented using manipulation of
purely textual formulas represented as character strings, at least if
restricted to eager evaluation.)

Re: "trap" and catching multiple signals

<87lf75fum2.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3968&group=comp.unix.shell#3968

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.unix.shell
Subject: Re: "trap" and catching multiple signals
Date: Sat, 19 Jun 2021 21:49:09 +0100
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <87lf75fum2.fsf@bsb.me.uk>
References: <sak25t$2d07d$2@news.xmission.com>
<sakm7e$nn8$1@news.dns-netz.com> <saks5h$43h$1@dont-email.me>
<20210619084849.706@kylheku.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="a7f27449a8e1d29c2e31ba58df9df84b";
logging-data="5010"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+DYynUdfDB61b/+nghj0RxflIeRPxhirw="
Cancel-Lock: sha1:3oj/PfLi4iMGMSMOf+lu3G7u8p0=
sha1:MThGFrlxWT3awolaxgs1FjYR6KU=
X-BSB-Auth: 1.112fbc3a1bde24d8cdfc.20210619214909BST.87lf75fum2.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 19 Jun 2021 20:49 UTC

Kaz Kylheku <563-365-8930@kylheku.com> writes:

> On 2021-06-19, Ed Morton <mortonspam@gmail.com> wrote:
>> On 6/19/2021 6:59 AM, joerg@schily.net wrote:
>>> In article <sak25t$2d07d$2@news.xmission.com>,
>>> Kenny McCormack <gazelle@shell.xmission.com> wrote:
>>>> Suppose you do:
>>>>
>>>> handler() { ... }
>>>> trap handler INT QUIT
>>>>
>>>> Any collection of signals would work; I am using INT and QUIT here for
>>>> convenience.
>>>>
>>>> The point is that you have one handler for both (i.e., all of the) signals,
>>>> and you'd like to be able to distinguish, inside of handler(), which signal
>>>> you are handling. You'd like for there to be some variable that holds the
>>>> signal number.
>>
>> It just takes one small function to implement it yourself:
>>
>> --------
>> trap_handler() {
>> printf 'handling signal %s\n' "$1"
>> ....
>> }
>>
>> set_traps() {
>> for sig; do
>> trap "trap_handler $sig" "$sig"
>> done
>> }
>
> Good call.
>
> The trap handler is a full command, not simply the name of a function,
> and that command can bind a function to whatever captured arguments
> it needs.

Yes, and you can use the command to set a variable if that is more
convenient:

trap "MySIGNAME=$sig trap_handler" "$sig"

--
Ben.

Re: "trap" and catching multiple signals

<salloj$2dr98$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3969&group=comp.unix.shell#3969

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: "trap" and catching multiple signals
Date: Sat, 19 Jun 2021 20:57:55 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <salloj$2dr98$1@news.xmission.com>
References: <sak25t$2d07d$2@news.xmission.com> <saks5h$43h$1@dont-email.me> <20210619084849.706@kylheku.com> <87lf75fum2.fsf@bsb.me.uk>
Injection-Date: Sat, 19 Jun 2021 20:57:55 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2551080"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 19 Jun 2021 20:57 UTC

In article <87lf75fum2.fsf@bsb.me.uk>,
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
....
>Yes, and you can use the command to set a variable if that is more
>convenient:
>
> trap "MySIGNAME=$sig trap_handler" "$sig"

There seem to be endless workarounds, but no actual solution.

I'm OK with that.

--
In American politics, there are two things you just don't f*ck with:

1) Goldman Sachs
2) The military/industrial complex

Re: "trap" and catching multiple signals

<salp5u$smu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3970&group=comp.unix.shell#3970

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: unr...@invalid.ca (William Unruh)
Newsgroups: comp.unix.shell
Subject: Re: "trap" and catching multiple signals
Date: Sat, 19 Jun 2021 21:56:15 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <salp5u$smu$1@dont-email.me>
References: <sak25t$2d07d$2@news.xmission.com> <saks5h$43h$1@dont-email.me>
<20210619084849.706@kylheku.com> <87lf75fum2.fsf@bsb.me.uk>
<salloj$2dr98$1@news.xmission.com>
Injection-Date: Sat, 19 Jun 2021 21:56:15 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="e5127a5f10fddf94ec03fcb789613859";
logging-data="29406"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX197iumEiebKDL+h2nARD96P"
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:icnzWUI6Oy5N0q7oaIyfWojA+Bc=
 by: William Unruh - Sat, 19 Jun 2021 21:56 UTC

On 2021-06-19, Kenny McCormack <gazelle@shell.xmission.com> wrote:
> In article <87lf75fum2.fsf@bsb.me.uk>,
> Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> ...
>>Yes, and you can use the command to set a variable if that is more
>>convenient:
>>
>> trap "MySIGNAME=$sig trap_handler" "$sig"
>
> There seem to be endless workarounds, but no actual solution.

Why are the suggestions "workarounds" rather than "solutions". What is
the difference between a workaround and a solution?
>
> I'm OK with that.
>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor