Sha256: 832915ef0c3a4961eaff8690bcf3a23eff1e7cd9b512c3f353b69550b3e634a2

Contents?: true

Size: 1.33 KB

Versions: 153

Compression:

Stored size: 1.33 KB

Contents

#include "signal_handlers.h"
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>


#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);
}

Version data entries

153 entries across 145 versions & 15 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/rb-fsevent-0.11.2/ext/fsevent_watch/signal_handlers.c