Skip to content

Commit c2fd40c

Browse files
dmcbrideFather Chrysostomos
authored and
Father Chrysostomos
committed
[perl #112990] Simplify kill implementation and docs
Clean up kill implementation and clear up the docs in perlfunc to be less ambiguous and encompass more of its behaviour. a) kill -INT => $pid; was surprisingly doing a "kill 0, $pid" instead of being the same as "kill -2, $pid" (killing the process group for $pid with an interrupt). Now negative signal names will be allowed and be the same as if the name was replaced with the signal number it represents. b) remove all calls to killpg() as killpg is defined in terms of kill anyway. c) Clarify the use of signal names vs numbers in perlfunc so that using names is not so well hidden, as well as explaining the usage of negative signal numbers as well as negative process IDs.
1 parent 0f9db00 commit c2fd40c

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

doio.c

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,7 @@ Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
15791579
const char *s;
15801580
STRLEN len;
15811581
SV ** const oldmark = mark;
1582+
bool killgp = false;
15821583

15831584
PERL_ARGS_ASSERT_APPLY;
15841585

@@ -1689,6 +1690,12 @@ nothing in the core.
16891690
if (mark == sp)
16901691
break;
16911692
s = SvPVx_const(*++mark, len);
1693+
if (*s == '-' && isALPHA(s[1]))
1694+
{
1695+
s++;
1696+
len--;
1697+
killgp = true;
1698+
}
16921699
if (isALPHA(*s)) {
16931700
if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
16941701
s += 3;
@@ -1698,12 +1705,18 @@ nothing in the core.
16981705
Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark));
16991706
}
17001707
else
1708+
{
17011709
val = SvIV(*mark);
1710+
if (val < 0)
1711+
{
1712+
killgp = true;
1713+
val = -val;
1714+
}
1715+
}
17021716
APPLY_TAINT_PROPER();
17031717
tot = sp - mark;
17041718
#ifdef VMS
17051719
/* kill() doesn't do process groups (job trees?) under VMS */
1706-
if (val < 0) val = -val;
17071720
if (val == SIGKILL) {
17081721
/* Use native sys$delprc() to insure that target process is
17091722
* deleted; supervisor-mode images don't pay attention to
@@ -1736,34 +1749,19 @@ nothing in the core.
17361749
break;
17371750
}
17381751
#endif
1739-
if (val < 0) {
1740-
val = -val;
1741-
while (++mark <= sp) {
1742-
I32 proc;
1743-
SvGETMAGIC(*mark);
1744-
if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1745-
Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1746-
proc = SvIV_nomg(*mark);
1747-
APPLY_TAINT_PROPER();
1748-
#ifdef HAS_KILLPG
1749-
if (PerlProc_killpg(proc,val)) /* BSD */
1750-
#else
1751-
if (PerlProc_kill(-proc,val)) /* SYSV */
1752-
#endif
1753-
tot--;
1754-
}
1755-
}
1756-
else {
1757-
while (++mark <= sp) {
1758-
I32 proc;
1759-
SvGETMAGIC(*mark);
1760-
if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1761-
Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1762-
proc = SvIV_nomg(*mark);
1763-
APPLY_TAINT_PROPER();
1764-
if (PerlProc_kill(proc, val))
1765-
tot--;
1752+
while (++mark <= sp) {
1753+
I32 proc;
1754+
SvGETMAGIC(*mark);
1755+
if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1756+
Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1757+
proc = SvIV_nomg(*mark);
1758+
if (killgp)
1759+
{
1760+
proc = -proc;
17661761
}
1762+
APPLY_TAINT_PROPER();
1763+
if (PerlProc_kill(proc, val))
1764+
tot--;
17671765
}
17681766
PERL_ASYNC_CHECK();
17691767
break;

pod/perlfunc.pod

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3178,11 +3178,20 @@ L<perlport> for notes on the portability of this construct.
31783178
Unlike in the shell, if SIGNAL is negative, it kills process groups instead
31793179
of processes. That means you usually
31803180
want to use positive not negative signals.
3181-
You may also use a signal name in quotes.
3181+
3182+
You may also use a signal name in quotes. A negative signal name is the
3183+
same as a negative signal number, killing process groups instead of processes.
3184+
For example, C<kill -KILL, $pgrp> will send C<SIGKILL> to the entire process
3185+
group specified.
31823186

31833187
The behavior of kill when a I<PROCESS> number is zero or negative depends on
31843188
the operating system. For example, on POSIX-conforming systems, zero will
3185-
signal the current process group and -1 will signal all processes.
3189+
signal the current process group, -1 will signal all processes, and any
3190+
other negative PROCESS number will act as a negative signal number and
3191+
kill the entire process group specified.
3192+
3193+
If both the SIGNAL and the PROCESS are negative, the results are undefined.
3194+
A warning may be produced in a future version.
31863195

31873196
See L<perlipc/"Signals"> for more details.
31883197

0 commit comments

Comments
 (0)