2016-07-09 15:04:09 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
2016-10-22 18:25:43 +03:00
|
|
|
@import Foundation;
|
|
|
|
|
|
|
|
#import <sys/event.h>
|
|
|
|
#import <uv.h>
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
#import "NeoVimServer.h"
|
|
|
|
#import "server_globals.h"
|
2016-08-13 00:45:56 +03:00
|
|
|
#import "Logging.h"
|
2016-08-26 16:31:30 +03:00
|
|
|
#import "CocoaCategories.h"
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
NeoVimServer *_neovim_server;
|
|
|
|
|
2016-10-22 18:25:43 +03:00
|
|
|
// Ensure that no parent-less NeoVimServer processes are left when the main app crashes.
|
2016-07-10 12:32:54 +03:00
|
|
|
// From http://mac-os-x.10953.n7.nabble.com/Ensure-NSTask-terminates-when-parent-application-does-td31477.html
|
2016-07-17 16:59:01 +03:00
|
|
|
static void observe_parent_termination(void *arg) {
|
2016-07-10 12:32:54 +03:00
|
|
|
pid_t ppid = getppid(); // get parent pid
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
kevent(kq, &procEvent, 1, &procEvent, 1, 0);
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:25:43 +03:00
|
|
|
ILOG("Exiting NeoVimServer: Parent terminated.");
|
2016-07-10 12:32:54 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
int main(int argc, const char *argv[]) {
|
2016-07-10 12:32:54 +03:00
|
|
|
uv_thread_t parent_observer_thread;
|
2016-07-17 16:59:01 +03:00
|
|
|
uv_thread_create(&parent_observer_thread, observe_parent_termination, NULL);
|
2016-07-10 12:32:54 +03:00
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
@autoreleasepool {
|
|
|
|
NSArray<NSString *> *arguments = [NSProcessInfo processInfo].arguments;
|
2016-07-10 15:43:26 +03:00
|
|
|
NSString *remoteServerName = arguments[1];
|
|
|
|
NSString *localServerName = arguments[2];
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-10 15:43:26 +03:00
|
|
|
_neovim_server = [[NeoVimServer alloc] initWithLocalServerName:localServerName remoteServerName:remoteServerName];
|
2016-08-26 16:31:30 +03:00
|
|
|
DLOG("Started neovim server '%s' and connected it with the remote agent '%s'.",
|
|
|
|
localServerName.cstr, remoteServerName.cstr);
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
[_neovim_server notifyReadiness];
|
|
|
|
}
|
|
|
|
|
2016-07-10 14:51:00 +03:00
|
|
|
CFRunLoopRun();
|
2016-07-09 15:04:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|