/*
 * Copyright 1988, 1989, 1990, 1991 Massachusetts Institute of Technology
 */
#include"structs.h"

static int sig_sent;

rebuildvolumes()
{
  struct sockaddr from;
  struct sockaddr_in *from_in;
  char temp[100];
  int cc, fromlen;
  if (need_to_rebuild_list == 1) {
    bsdsyslog(LOG_INFO, "rebuild was forced internally");
  }
  else if (need_to_rebuild_list == 2) {
    bsdsyslog(LOG_INFO, "rebuilding on signal %d",sig_sent);
  }
  else {
    fromlen = sizeof(struct sockaddr);
    cc = recvfrom(notice_socket, temp, 100, 0, &from, &fromlen);
    from_in = (struct sockaddr_in *)&from;
    if (cc < 1) return;
    if (!strcmp(temp,QUERY)) {   /* a query */
      sendto(notice_socket,RESPONSE,strlen(RESPONSE)+1,0,&from,
	     fromlen);
      return;
    }
    strcpy(temp,inet_ntoa(from_in->sin_addr/*.s_addr*/));
    if (strcmp(temp,localaddress)) {
      bsdsyslog(LOG_INFO, "rebuilding on notification from %s",temp);
    }
    else {
      if (DEBUG) printf("> I refuse to accept notification from myself.\n");
      return;
    }
  }
  build_volumes(1);
  notify_hosts();
  need_to_rebuild_list = 0;
  if (DEBUG) printf("Finished rebuilding.\n");
}

sigrebuild(sig)
     int sig;
{
  sig_sent = sig;
  need_to_rebuild_list = 2;
#ifdef SYSV
  signal(sig, sigrebuild);
#endif /* SYSV */
}
