mirror of
https://github.com/qvacua/vimr.git
synced 2024-12-24 22:33:52 +03:00
Use dispatch source for parent process monitoring
This commit is contained in:
parent
9c46099b81
commit
0a94c43a9b
@ -5,9 +5,6 @@
|
||||
|
||||
@import Foundation;
|
||||
|
||||
#import <sys/event.h>
|
||||
#import <uv.h>
|
||||
|
||||
#import "NeoVimServer.h"
|
||||
#import "server_globals.h"
|
||||
#import "Logging.h"
|
||||
@ -15,35 +12,33 @@
|
||||
|
||||
|
||||
NeoVimServer *_neovim_server;
|
||||
CFRunLoopRef _mainRunLoop;
|
||||
|
||||
// Ensure that no parent-less NeoVimServer processes are left when the main app crashes.
|
||||
// From http://mac-os-x.10953.n7.nabble.com/Ensure-NSTask-terminates-when-parent-application-does-td31477.html
|
||||
static void observe_parent_termination(void *arg) {
|
||||
pid_t ppid = getppid(); // get parent pid
|
||||
void observe_parent_termination() {
|
||||
pid_t parentPID = getppid();
|
||||
|
||||
int kq = kqueue();
|
||||
if (kq != -1) {
|
||||
struct kevent procEvent; // wait for parent to exit
|
||||
EV_SET(
|
||||
&procEvent, // kevent
|
||||
ppid, // ident
|
||||
EVFILT_PROC, // filter
|
||||
EV_ADD, // flags
|
||||
NOTE_EXIT, // fflags
|
||||
0, // data
|
||||
0 // udata
|
||||
);
|
||||
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
||||
dispatch_source_t source = dispatch_source_create(
|
||||
DISPATCH_SOURCE_TYPE_PROC, (uintptr_t) parentPID, DISPATCH_PROC_EXIT, queue
|
||||
);
|
||||
|
||||
kevent(kq, &procEvent, 1, &procEvent, 1, 0);
|
||||
if (source == NULL) {
|
||||
WLOG("No parent process monitoring...");
|
||||
return;
|
||||
}
|
||||
|
||||
ILOG("Exiting NeoVimServer: Parent terminated.");
|
||||
exit(0);
|
||||
dispatch_source_set_event_handler(source, ^{
|
||||
WLOG("Exiting neovim server due to parent termination.");
|
||||
CFRunLoopStop(_mainRunLoop);
|
||||
dispatch_source_cancel(source);
|
||||
});
|
||||
|
||||
dispatch_resume(source);
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
uv_thread_t parent_observer_thread;
|
||||
uv_thread_create(&parent_observer_thread, observe_parent_termination, NULL);
|
||||
_mainRunLoop = CFRunLoopGetCurrent();
|
||||
observe_parent_termination();
|
||||
|
||||
@autoreleasepool {
|
||||
NSArray<NSString *> *arguments = [NSProcessInfo processInfo].arguments;
|
||||
@ -58,5 +53,6 @@ int main(int argc, const char *argv[]) {
|
||||
}
|
||||
|
||||
CFRunLoopRun();
|
||||
WLOG("Returning neovim server");
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user