[distcc] Ooops!

Oscar Esteban flesteban at mi.madritel.es
Mon Oct 7 19:15:01 GMT 2002


While I just modified the previously written and tested code, something
bad is happening. There seems to be some problem with calling signal
repeated times (which is done due to the 1..50 loop which, I think, could
somehow be avoided).
Worst thing happening will be that the timeout will be innefective all but
the first time.

I don't really like doing this, but will, with the hope that someone has
had a similar experience. I'm doing some tests with the code following.

Sorry for the inconvenience...
---------------------------------------------

#define DNS_TIMEOUT     1000   /* Time given in milliseconds, but
				  it'd better be multiple of 1000 */
    
#include <stdio.h>
#include <unistd.h>
#include <setjmp.h>
#include <signal.h>

/**
 * alarm handler for implementing gethostbyname timeout
 **/
static jmp_buf time_to_wakeup;
static int cuenta;

static void dcc_alarm_handler()
{
  cuenta++;
  longjmp(time_to_wakeup, 1);
}

void dcc_gethostbyname(int spend_long_time)
{
  void *prev_signal;

  alarm(0); /* Let's be sure we do not get a signal before setjmp */

  prev_signal = signal(SIGALRM, dcc_alarm_handler);
  if (!setjmp(time_to_wakeup)) {
    /* we came here from the setjmp, proceed normally */
    alarm(DNS_TIMEOUT / 1000); /* Program the alarm */

    if (spend_long_time > 1) {
      while (1); /* Endless loop, should exit due to signal */
    } else {
      /* Nothing, will end immediately -> no signal */
    }

    alarm(0);             /* Finished in time, disconnect the alarm */
    signal(SIGALRM, prev_signal); /* and restore previous behaviour */
    printf("Ok for time = %d\n", spend_long_time);

  } else {
    /* we came here from a longjump -> we got the SIGALRM */
    signal(SIGALRM, prev_signal); /* restore previous behaviour */
    printf("Error for time = %d\n", spend_long_time);
  }

  return;
}

int main()
{
  dcc_gethostbyname(1); // Ok
  dcc_gethostbyname(1); // Ok
  dcc_gethostbyname(1); // Ok
  dcc_gethostbyname(3); // Ok
  dcc_gethostbyname(3); // Gets stuck
  dcc_gethostbyname(3);

  return 0;
}




More information about the distcc mailing list