help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] readline does not always handle SIGTERM on reboot


From: john smith
Subject: Re: [Help-bash] readline does not always handle SIGTERM on reboot
Date: Sat, 7 Apr 2018 02:03:13 +0200

On Fri, Apr 6, 2018 at 8:53 PM, Chet Ramey <address@hidden> wrote:
> On 4/6/18 12:03 PM, john smith wrote:
>
>>> Then readline is not active when the SIGTERM arrives, or its signal
>>> handlers are not installed.
>>
>> Hmm, maybe I don't understand something.  I wrote the following tiny
>> program that does the same as busybox init:
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <sys/types.h>
>> #include <signal.h>
>> #include <unistd.h>
>> #include <sys/reboot.h>
>>
>> int main(void)
>> {
>>     kill(-1, SIGTERM);
>>
>>     return EXIT_SUCCESS;
>> }
>>
>> If I run it in the ttyS0 serial console I also can't see `logout'
>> string and other debug messages I added to rl_getc() what means that
>> readline does not handle SIGTERM.
>
> First of all, bash ignores SIGTERM when it's interactive, so if you don't
> happen to send SIGTERM while readline is reading a command, you're not
> going to get anything. (There is a recent post on bug-bash explaining why
> readline returns EOF when it gets SIGTERM.)

Thanks, you helped me understand something.  I discovered that the
above program would logout the current session I ran it in if I ran it
in the background from another program like that:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
  if(fork() == 0)
    {
      execv("/bin/kill-mine", NULL);
    }
}

Or put daemon() at the top:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

int main(void)
{
  daemon(0, 0);
  kill(-1, SIGTERM);

  return EXIT_SUCCESS;
}

Or simply run it with &:

$ kill-mine &

So the general conclusion is that it has be detached from the terminal
before sending SIGTERM to bash.  I still don't know answers for 2
questions:

1. Where is the above checked in bash?

2. Why does busybox reboot make bash logout occasionally even if it
theoretically shouldn't because it does not detach from terminal?  The
interesting thing is that reboot on my Slackware system behaves
differently - it always makes bash logout.  I think that is because
`reboot' command informs init daemon that reboot should be done via
/dev/initctl named pipe and this is init process with PID 1 that
actually delivers SIGTERM to bash.  As it's detached from terminal
readline returns EOF and bash correctly logs out.

(BTW, I've inadvertently sent a previous e-mail only to you and not a
mailing list, sorry).
-- 
<address@hidden>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]