Merge pull request #411 from nodogsplash/ndsctl-fix

ndsctl: prevent deadlock with multiple concurrent instances
This commit is contained in:
Rob White
2019-08-10 13:14:04 +01:00
committed by GitHub
2 changed files with 23 additions and 1 deletions

View File

@@ -681,6 +681,12 @@ config_read(const char *filename)
char line[MAX_BUF], *s, *p1, *p2;
int linenum = 0, opcode, value;
struct stat sb;
char lockfile[] = "/tmp/ndsctl.lock";
//Remove ndsctl lock file if it exists
if (fd = fopen(lockfile, "r")) {
remove(lockfile);
}
debug(LOG_INFO, "Reading configuration file '%s'", filename);

View File

@@ -161,6 +161,21 @@ ndsctl_do(const char *socket, const struct argument *arg, const char *param)
char request[128];
int len, rlen;
int ret;
char lockfile[] = "/tmp/ndsctl.lock";
FILE *fd;
setlogmask(LOG_UPTO (LOG_NOTICE));
if (fd = fopen(lockfile, "r")) {
openlog ("ndsctl", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
syslog (LOG_NOTICE, "ndsctl is locked by another process - try again later...");
closelog ();
fclose(fd);
return -1;
} else {
//Create lock
fd = fopen(lockfile, "w");
}
sock = connect_to_server(socket);
if (sock < 0) {
@@ -206,7 +221,8 @@ ndsctl_do(const char *socket, const struct argument *arg, const char *param)
shutdown(sock, 2);
close(sock);
fclose(fd);
remove(lockfile);
return ret;
}