From f8a90d609fe3b05b3d41875537f3be03095b0e8f Mon Sep 17 00:00:00 2001 From: "Kevin M. Rosenberg" Date: Mon, 12 Feb 2018 12:53:49 -0700 Subject: [PATCH] Automatically remove snark.lock file on application exit using atexit() registration --- src/snark/snark.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/snark/snark.cpp b/src/snark/snark.cpp index 08e1fec..07b8521 100644 --- a/src/snark/snark.cpp +++ b/src/snark/snark.cpp @@ -220,6 +220,9 @@ static const INTEGER main_codes[NUMBER_OF_COMMANDS] = CHAR2INT('s','u','p','e') }; +static const char* kLockFilename = "snark.lock"; +static int gLockFd = -1; + int FindKeywordCode(int word) { for(int i = 0; i < NUMBER_OF_COMMANDS; i++) { // find keyword code @@ -260,6 +263,16 @@ INTEGER GetCommandPhase(INTEGER code) return Phase[code]; } +void unlinkLockfile(void) { +#ifndef __CYGWIN__ + if (gLockFd >= 0) { + close(gLockFd); + if (unlink(kLockFilename) != 0) { + fprintf(stderr, "Error unlinking lock file\n"); + } + } +#endif +} int snark(int argc, char *argv[]) { @@ -285,19 +298,22 @@ int snark(int argc, char *argv[]) // bug 179 - swr - 10/30/05 #ifndef __CYGWIN__ // bug 167 - swr - 9/24/05 - int lfp=open("snark.lock",O_RDWR|O_CREAT,0640); - if (lfp<0) { + gLockFd = open(kLockFilename,O_RDWR|O_CREAT,0640); + if (gLockFd < 0) { fprintf(stderr, "can't create lock file in current directory - check directory permissions\n"); exit(1); /* can not open */ } - if (lockf(lfp,F_TLOCK,0)<0) { + if (lockf(gLockFd,F_TLOCK,0)<0) { fprintf(stderr, "multiple snark executions in the same directory are not allowed!\n"); exit(0); /* can not lock */ } /* only first instance continues */ char str[10]; sprintf(str,"%d\n",getpid()); - write(lfp,str,strlen(str)); /* record pid to lockfile */ + write(gLockFd,str,strlen(str)); /* record pid to lockfile */ + + atexit(unlinkLockfile); + #endif // bug 187 - allow input filename on command line - swr - 11/11/05 @@ -490,6 +506,7 @@ int snark(int argc, char *argv[]) }; fprintf(output, "\n"); + return 0; } -- 2.34.1