From 30f53ca100f039189cc631e47fd07e4130f2c93b Mon Sep 17 00:00:00 2001 From: Felix Kratz Date: Sun, 20 Feb 2022 13:54:35 +0100 Subject: [PATCH] acquire lockfile --- src/main.m | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main.m b/src/main.m index f67c398..6b1800b 100644 --- a/src/main.m +++ b/src/main.m @@ -6,9 +6,26 @@ void* g_workspace; +static void acquire_lockfile(void) { + int handle = open("/tmp/svim.lock", O_CREAT | O_WRONLY, 0600); + if (handle == -1) printf("Error: Could not create lock-file.\n"), exit(1); + + struct flock lockfd = { + .l_start = 0, + .l_len = 0, + .l_pid = getpid(), + .l_type = F_WRLCK, + .l_whence = SEEK_SET + }; + + if (fcntl(handle, F_SETLK, &lockfd) == -1) + printf("Error: Could not acquire lock-file.\n"), exit(1); +} + int main (int argc, char *argv[]) { NSApplicationLoad(); + acquire_lockfile(); ax_begin(&g_ax); event_tap_begin(&g_event_tap); workspace_begin(&g_workspace);