/*
 * Copyright 2001 Michael Grant. All rights reserved. 
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  1.Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  2.Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY MICHAEL GRANT ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MICHAEL GRANT OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and
 * documentation are those of the authors and should not be
 * interpreted as representing official policies, either expressed or
 * implied, of Michael Grant.
 *
 * named-reload sends a kill -1 to the named process then reads the log
 * file to see if any named errors were produced.  Lines from the log file
 * are printed out.  No messages at all indicates apparent success.
 */

#define LOGFILE "/var/log/messages"
#define PIDFILE "/var/run/named.pid"

#include <stdio.h>
#include <signal.h>
#include <string.h>

main() {
  FILE *pidf;
  FILE *logf;
  char s[1024];
  pid_t pid;
  int gotone = 0;

  pidf = fopen(PIDFILE, "r");

  if (!pidf) {
    perror(PIDFILE);
    exit(1);
  }

  fgets(s, sizeof(s), pidf);
  fclose(pidf);

  pid = atoi(s);

  logf = fopen(LOGFILE, "r");

  if (!logf) {
    perror(LOGFILE);
    exit(1);
  }

  fseek(logf, 0, SEEK_END);

  if (kill(pid, 1) == -1) {
    perror("kill");
    exit(1);
  }

  sleep(2);

  while (fgets(s, sizeof(s), logf) != NULL) {
    if (strstr(s, "named")) {
      char *cp;
      cp = strstr(s, "]: ");
      if (cp) {
	cp+=3;
	printf("%s", cp);
	gotone = 1;
      }
    }
  }
  if (! gotone)
    printf("named apparently reloaded successfully.\n");
}
