Skip to content

Commit

Permalink
Merge pull request #411 from nodogsplash/ndsctl-fix
Browse files Browse the repository at this point in the history
ndsctl: prevent deadlock with multiple concurrent instances
  • Loading branch information
bluewavenet committed Aug 10, 2019
2 parents 5385752 + 1a94200 commit 1eda1bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 17 additions & 1 deletion src/ndsctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 1eda1bf

Please sign in to comment.