Debian recvfrom() revisited

Alex Vrenios plug-devel@lists.PLUG.phoenix.az.us
Sun Oct 27 23:20:02 2002


All,

Debian has a bug in recvfrom() but it hasn't anything to do with the problem
I posted here a few days ago. The technique had to do with preventing a
process hang when an expected UDP packet doesn't arrive.

Instead of initializing the signal handlers with

   signal(SIGALRM, sig_alarm);

use sigaction. It requires a structure:

   struct sigaction newact, oldact;

and a slightly more complicated initialization procedure:

   sigemptyset(&newact.sa_mask);
   newact.sa_flags = SA_INTERRUPT;
   newact.sa_handler = sig_alarm;
   sigaction(SIGALRM, &newact, &oldact);

Note that the "SA_INTERRUPT" says don't restart that recvfrom, interrupt it!
Other kinds of interrupts should probably use "SA_RESTART" instead, but you
want your alarm signal to tell you that the data is NOT going to arrive.

This is tested on Red Hat 4.2, 7.0 and 8.0, and is likely to be the RIGHT
way to do this kind of thing, no matter which Linux you are using. See W.
Richard Stevens' "Advanced Programming in the Unix Environment" Program
10.13 for further details. (I'll of course be happy to discuss this with
anyone who may be interested.)


Best Regards,

Alex