Ejemplo n.º 1
0
void olsr_reconfigure(int signo __attribute__ ((unused))) {
  /* if we are started with -nofork, we do not weant to go into the
   * background here. So we can simply stop on -HUP
   */
  olsr_syslog(OLSR_LOG_INFO, "sot: olsr_reconfigure()\n");
  if (!olsr_cnf->no_fork) {
    if (!fork()) {
      int i;
      sigset_t sigs;
      /* New process */
      sleep(3);
      sigemptyset(&sigs);
      sigaddset(&sigs, SIGHUP);
      sigprocmask(SIG_UNBLOCK, &sigs, NULL);
      for (i = sysconf(_SC_OPEN_MAX); --i > STDERR_FILENO;) {
        close(i);
      }
      printf("Restarting %s\n", olsr_argv[0]);
      olsr_syslog(OLSR_LOG_INFO, "Restarting %s\n", olsr_argv[0]);
      execv(olsr_argv[0], olsr_argv);
      olsr_syslog(OLSR_LOG_ERR, "execv(%s) fails: %s!\n", olsr_argv[0],
          strerror(errno));
    } else {
      olsr_syslog(OLSR_LOG_INFO, "RECONFIGURING!\n");
    }
  }
  olsr_shutdown(0);
}
Ejemplo n.º 2
0
/**
 * Write the current PID to the configured PID file (if one is configured)
 */
static void writePidFile(void) {
  if (olsr_cnf->pidfile) {
    char buf[PATH_MAX + 256];

    /* create / open the PID file */
#ifdef __WIN32
    mode_t mode = S_IRUSR | S_IWUSR;
#else
    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
#endif
    int fd = open(olsr_cnf->pidfile, O_CREAT | O_WRONLY, mode);
    if (fd < 0) {
      snprintf(buf, sizeof(buf), "Could not open PID file %s", olsr_cnf->pidfile);
      perror(buf);
      olsr_shutdown(0);
    }

    /* write the PID */
    {
      pid_t pid = getpid();
      int chars = snprintf(buf, sizeof(buf), "%d", pid);
      ssize_t chars_written = write(fd, buf, chars);
      if (chars_written != chars) {
        close(fd);
        snprintf(buf, sizeof(buf), "Could not write the PID %d to the PID file %s", pid, olsr_cnf->pidfile);
        perror(buf);
        if (remove(olsr_cnf->pidfile) < 0) {
          snprintf(buf, sizeof(buf), "Could not remove the PID file %s", olsr_cnf->pidfile);
          perror(buf);
        }
        olsr_shutdown(0);
      }
    }

    if (close(fd) < 0) {
      snprintf(buf, sizeof(buf), "Could not close PID file %s", olsr_cnf->pidfile);
      perror(buf);
      if (remove(olsr_cnf->pidfile) < 0) {
        snprintf(buf, sizeof(buf), "Could not remove the PID file %s", olsr_cnf->pidfile);
        perror(buf);
      }
      olsr_shutdown(0);
    }
  }
}