www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - System signals

reply "Andrea Fontana" <nospam example.com> writes:
How can i catch kill signal to gracefully shutdown my app?
Apr 28 2012
next sibling parent Sean Kelly <sean invisibleduck.org> writes:
On Apr 28, 2012, at 1:04 PM, "Andrea Fontana" <nospam example.com> wrote:

 How can i catch kill signal to gracefully shutdown my app?
Gracefully? That's tough. I'd set a flag, then exit the signal handler. All= threads should periodically check the flag and throw an Error if they see i= t. Or an Exception you're sure won't be caught. You really can't do much in= a signal handler, so it is t safe to do something fancier there.=20=
Apr 28 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Apr 28, 2012 at 02:08:06PM -0700, Sean Kelly wrote:
 On Apr 28, 2012, at 1:04 PM, "Andrea Fontana" <nospam example.com>
 wrote:
 
 How can i catch kill signal to gracefully shutdown my app?
Gracefully? That's tough. I'd set a flag, then exit the signal handler. All threads should periodically check the flag and throw an Error if they see it. Or an Exception you're sure won't be caught. You really can't do much in a signal handler, so it is t safe to do something fancier there.
My favorite solution to this is to use the self-pipe trick: http://evbergen.home.xs4all.nl/unix-signals.html MUCH safer than setting a flag, or trying to do too much in a signal handler, and can integrate seamlessly if your program is already using a select loop. Of course, if you're using threads then things may be a bit more complicated. But still, moving the actual handling out of the signal handler is a good thing; it lets you do more complex stuff (like communicate with threads to stop, etc.). Note, however, that SIGKILL is not catchable, ever. T -- MAS = Mana Ada Sistem?
Apr 28 2012
prev sibling parent reply deadalnix <deadalnix gmail.com> writes:
Le 28/04/2012 22:04, Andrea Fontana a écrit :
 How can i catch kill signal to gracefully shutdown my app?
This can be used for other signals : http://www.deadalnix.me/2012/03/24/get-an-exception-from-a-segfault-on-linux-x86-and-x86_64-using-some-black-magic/ But still, you can't catch kill and it is linux specific.
Apr 28 2012
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sun, Apr 29, 2012 at 12:16:15AM +0200, deadalnix wrote:
 Le 28/04/2012 22:04, Andrea Fontana a écrit :
How can i catch kill signal to gracefully shutdown my app?
This can be used for other signals : http://www.deadalnix.me/2012/03/24/get-an-exception-from-a-segfault-on-linux-x86-and-x86_64-using-some-black-magic/ But still, you can't catch kill and it is linux specific.
AFAIK, SIGKILL cannot be caught on any posix system. T -- The easy way is the wrong way, and the hard way is the stupid way. Pick one.
Apr 28 2012
next sibling parent "Andrea Fontana" <nospam example.com> writes:
Of course I mean sigterm.
I mixed sigterm and kill (not -9) utility and i wrote sigkill :)

On Saturday, 28 April 2012 at 22:22:00 UTC, H. S. Teoh wrote:
 On Sun, Apr 29, 2012 at 12:16:15AM +0200, deadalnix wrote:
 But still, you can't catch kill and it is linux specific.
AFAIK, SIGKILL cannot be caught on any posix system. T
Apr 28 2012
prev sibling parent reply deadalnix <deadalnix gmail.com> writes:
Le 29/04/2012 00:23, H. S. Teoh a écrit :
 On Sun, Apr 29, 2012 at 12:16:15AM +0200, deadalnix wrote:
 Le 28/04/2012 22:04, Andrea Fontana a écrit :
 How can i catch kill signal to gracefully shutdown my app?
This can be used for other signals : http://www.deadalnix.me/2012/03/24/get-an-exception-from-a-segfault-on-linux-x86-and-x86_64-using-some-black-magic/ But still, you can't catch kill and it is linux specific.
AFAIK, SIGKILL cannot be caught on any posix system. T
You misunderstood me (the sentence was not clear). I mean, you cannot catch kill and the trick mentioned int he link is linux specific. Obviously, the fact that you cannot catch kill isn't linux specific ;)
Apr 29 2012
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sun, Apr 29, 2012 at 11:47:24PM +0200, deadalnix wrote:
[...]
 You misunderstood me (the sentence was not clear). I mean, you cannot
 catch kill and the trick mentioned int he link is linux specific.
 Obviously, the fact that you cannot catch kill isn't linux specific ;)
Ah, I see. But really? I thought the self-pipe trick is portable across Posix platforms. But then again, my last contact with a non-Linux Posix system was, oh, at least 8 years ago with a Solaris workstation, so I could be totally wrong. T -- They pretend to pay us, and we pretend to work. -- Russian saying
Apr 29 2012
prev sibling next sibling parent Sean Kelly <sean invisibleduck.org> writes:
On Apr 29, 2012, at 9:28 PM, H. S. Teoh wrote:

 On Sun, Apr 29, 2012 at 11:47:24PM +0200, deadalnix wrote:
 [...]
 You misunderstood me (the sentence was not clear). I mean, you cannot
 catch kill and the trick mentioned int he link is linux specific.
 Obviously, the fact that you cannot catch kill isn't linux specific =
;)
=20
 Ah, I see.
=20
 But really? I thought the self-pipe trick is portable across Posix
 platforms. But then again, my last contact with a non-Linux Posix =
system
 was, oh, at least 8 years ago with a Solaris workstation, so I could =
be
 totally wrong.
It's legal to call write() in a signal handler, so if the socket is = calling select() or poll() or whatever, something along those lines = should work just fine. As for catching kill, I thought the signal = handler would fire but when it exited the app would still terminate. = No?=
Apr 30 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Apr 30, 2012 at 06:50:50AM -0700, Sean Kelly wrote:
 On Apr 29, 2012, at 9:28 PM, H. S. Teoh wrote:
 
 On Sun, Apr 29, 2012 at 11:47:24PM +0200, deadalnix wrote:
 [...]
 You misunderstood me (the sentence was not clear). I mean, you
 cannot catch kill and the trick mentioned int he link is linux
 specific.  Obviously, the fact that you cannot catch kill isn't
 linux specific ;)
Ah, I see. But really? I thought the self-pipe trick is portable across Posix platforms. But then again, my last contact with a non-Linux Posix system was, oh, at least 8 years ago with a Solaris workstation, so I could be totally wrong.
It's legal to call write() in a signal handler, so if the socket is calling select() or poll() or whatever, something along those lines should work just fine. As for catching kill, I thought the signal handler would fire but when it exited the app would still terminate. No?
IIRC, catching SIGTERM allows you to not terminate (that's how SIG_IGN is implemented, I think). But SIGKILL isn't even catchable to begin with, you'll just terminate no matter what. But again, my impression may be biased by exclusive use of Linux for the last 8 years or so. T -- GEEK = Gatherer of Extremely Enlightening Knowledge
Apr 30 2012
prev sibling parent Sean Kelly <sean invisibleduck.org> writes:
On Apr 30, 2012, at 8:42 AM, "H. S. Teoh" <hsteoh quickfur.ath.cx> wrote:

 On Mon, Apr 30, 2012 at 06:50:50AM -0700, Sean Kelly wrote:
 On Apr 29, 2012, at 9:28 PM, H. S. Teoh wrote:
=20
 On Sun, Apr 29, 2012 at 11:47:24PM +0200, deadalnix wrote:
 [...]
 You misunderstood me (the sentence was not clear). I mean, you
 cannot catch kill and the trick mentioned int he link is linux
 specific.  Obviously, the fact that you cannot catch kill isn't
 linux specific ;)
=20 Ah, I see. =20 But really? I thought the self-pipe trick is portable across Posix platforms. But then again, my last contact with a non-Linux Posix system was, oh, at least 8 years ago with a Solaris workstation, so I could be totally wrong.
=20 It's legal to call write() in a signal handler, so if the socket is calling select() or poll() or whatever, something along those lines should work just fine. As for catching kill, I thought the signal handler would fire but when it exited the app would still terminate. No?
=20 IIRC, catching SIGTERM allows you to not terminate (that's how SIG_IGN is implemented, I think). But SIGKILL isn't even catchable to begin with, you'll just terminate no matter what. =20 But again, my impression may be biased by exclusive use of Linux for the last 8 years or so.
You're probably right. So SIGKILL is defined so you can send the signal only= . I've never tried to trap it.=20=
Apr 30 2012