UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » General
  • » How advisable is it to do socket calls from a signal handler?

#1 2009-05-05 05:23 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: How advisable is it to do socket calls from a signal handler?

Hi all,

My program currently uses a signal handler to catch CTRL-C events.  The signal handler simply sets a boolean variable.  The program's main event loop then polls the boolean every second or so, and if it sees that it has been set, it cleans up and exits.

This works, more or less, but it means that my program can never be completely idle, it must always wake up and poll every second, which seems like a wasteful/ugly way to do things.

On another discussion site I saw someone recommend setting up a socket pair, and having the signal handler close one of the sockets.  The main loop would then select() (or poll()) on that socket, and thus be notified when the signal handler had closed the other one.

My question is, will that work reliably?  I had been under the impression that signal handlers weren't allowed to execute socket routines; but perhaps simply closing a socket (or alternatively, send()-ing a byte on it) would be safe enough?

Bonus question:  Windows has a function somewhat similar to signals in its SetConsoleCtrlHandler() function.  Would a similar scheme work there?

Offline

#2 2009-05-05 05:33 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: How advisable is it to do socket calls from a signal handler?

You don't have to wake up and poll, that's what errno == EINTR is for.

Offline

#3 2009-05-05 05:46 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: How advisable is it to do socket calls from a signal handler?

Offline

#4 2009-05-05 08:46 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: How advisable is it to do socket calls from a signal handler?

Offline

#5 2009-05-06 12:28 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: How advisable is it to do socket calls from a signal handler?

Offline

#6 2009-05-06 12:39 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: How advisable is it to do socket calls from a signal handler?

Thanks for the help guys; I've rewritten my signal handler to send a byte on a socket instead of setting a boolean, and it appears to be working well.

-Jeremy

Offline

#7 2009-05-06 12:35 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: How advisable is it to do socket calls from a signal handler?

Fascinating...  I never knew it was impossible for select() (and company) to ever be
auto-restarted...  It makes perfect sense, now that I think about it, though...

Where'd you pull that quote from, i3839?  I'm just curious to see the rest of the list of
non-restartable functions...  I don't see it in my local sigaction man page, which was
the first place I looked for it...

Offline

#8 2009-05-06 01:16 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: How advisable is it to do socket calls from a signal handler?

signal(7).

Offline

#9 2009-05-06 08:13 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: How advisable is it to do socket calls from a signal handler?

It must be a newer version of the man page than I have on my system, because
mine doesn't mention it...  I found a copy online, though...

Offline

#10 2017-05-25 10:27 PM

Jason
Guest

Re: How advisable is it to do socket calls from a signal handler?

  • Index
  • » General
  • » How advisable is it to do socket calls from a signal handler?

Board footer

Powered by FluxBB