All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gems.rb-fsevent-0.10.2.ext.fsevent_watch.signal_handlers.c Maven / Gradle / Ivy

There is a newer version: 3.7.2
Show newest version
#include "signal_handlers.h"
#include 
#include 
#include 
#include 
#include 
#include 


#define PPID_ALARM_INTERVAL 2 // send SIGALRM every this seconds


static pid_t orig_ppid;


static void signal_handler(int _) {
  exit(EXIT_FAILURE);
}

static void check_ppid(void) {
  if (getppid() != orig_ppid) {
    exit(EXIT_FAILURE);
  }
}

static void check_stdout_open(void) {
  if (fcntl(STDOUT_FILENO, F_GETFD) < 0) {
    exit(EXIT_FAILURE);
  }
}

static void alarm_handler(int _) {
  check_ppid();
  check_stdout_open();
  alarm(PPID_ALARM_INTERVAL);
  signal(SIGALRM, alarm_handler);
}

static void die(const char *msg) {
  fprintf(stderr, "\nFATAL: %s\n", msg);
  abort();
}

static void install_signal_handler(int sig, void (*handler)(int)) {
  if (signal(sig, handler) == SIG_ERR) {
    die("Could not install signal handler");
  }
}

void install_signal_handlers(void) {
  // check pipe is still connected
  check_stdout_open();

  // watch getppid() every PPID_ALARM_INTERVAL seconds 
  orig_ppid = getppid();
  if (orig_ppid <= 1) {
    die("prematurely zombied");
  }
  install_signal_handler(SIGALRM, alarm_handler);
  alarm(PPID_ALARM_INTERVAL);

  // be sure to exit on SIGHUP, SIGPIPE
  install_signal_handler(SIGHUP,  signal_handler);
  install_signal_handler(SIGPIPE, signal_handler);
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy