Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fsevent_watch : Fix "zombie" processes not dying #62

Merged
merged 2 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions ext/fsevent_watch/TSICTString.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,6 @@ static inline CFStringRef TSICTStringCreateStringFromIntermediateRepresentation(
return string;
}

static inline CFDataRef TSICTStringCreateDataWithDataOfTypeAndFormat(CFDataRef data, TSITStringTag type, TSITStringFormat format)
{
CFRetain(data);

if (format == kTSITStringFormatDefault) {
format = TSICTStringGetDefaultFormat();
}

TStringIRep* rep = TSICTStringCreateWithDataOfTypeAndFormat(data, type, format);
if (rep == NULL) {
return NULL;
}

CFDataRef result = TSICTStringCreateDataFromIntermediateRepresentation(rep);

TSICTStringDestroy(rep);
CFRelease(data);

return result;
}

static inline void TSICTStringAppendObjectToMutableDataWithFormat(CFTypeRef object, CFMutableDataRef buffer, TSITStringFormat format)
{
if (object == NULL) {
Expand Down
2 changes: 2 additions & 0 deletions ext/fsevent_watch/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "common.h"
#include "signal_handlers.h"
#include "cli.h"
#include "FSEventsFix.h"

Expand Down Expand Up @@ -470,6 +471,7 @@ static void callback(__attribute__((unused)) FSEventStreamRef streamRef,

int main(int argc, const char* argv[])
{
install_signal_handlers();
parse_cli_settings(argc, argv);

if (needs_fsevents_fix) {
Expand Down
66 changes: 66 additions & 0 deletions ext/fsevent_watch/signal_handlers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#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);
}

16 changes: 16 additions & 0 deletions ext/fsevent_watch/signal_handlers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @headerfile signal_handlers.h
* Signal handlers to stop the zombie hordes
*
* Catch and handle signals better so that we die faster like a good meat puppet.
*/


#ifndef fsevent_watch_signal_handlers_h
#define fsevent_watch_signal_handlers_h


void install_signal_handlers(void);


#endif // fsevent_watch_signal_handlers_h