[OpenWrt-Devel] [PATCH 1/2] utils: implement fw3_lock_path() & fw3_unlock_path()
Paul Spooren
mail at aparcar.org
Fri May 17 05:14:45 EDT 2019
From: Alexander Couzens <lynxis at fe80.eu>
To lock a second lock file at the same time, introduce fw3_{un,}lock_path.
fw3_lock_path support the path as parameter in difference to fw3_lock which
only locks the fw3 lock file (/var/run/fw3.lock)
Signed-off-by: Alexander Couzens <lynxis at fe80.eu>
---
utils.c | 34 +++++++++++++++++++++++++---------
utils.h | 2 ++
2 files changed, 27 insertions(+), 9 deletions(-)
diff --git a/utils.c b/utils.c
index 7f09787..6360279 100644
--- a/utils.c
+++ b/utils.c
@@ -28,7 +28,7 @@
#include "ipsets.h"
-static int lock_fd = -1;
+static int fw3_lock_fd = -1;
static pid_t pipe_pid = -1;
static FILE *pipe_fd = NULL;
@@ -346,13 +346,13 @@ fw3_has_table(bool ipv6, const char *table)
bool
-fw3_lock(void)
+fw3_lock_path(int *fd, const char *path)
{
- lock_fd = open(FW3_LOCKFILE, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
+ int lock_fd = open(path, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
if (lock_fd < 0)
{
- warn("Cannot create lock file %s: %s", FW3_LOCKFILE, strerror(errno));
+ warn("Cannot create lock file %s: %s", path, strerror(errno));
return false;
}
@@ -362,22 +362,38 @@ fw3_lock(void)
return false;
}
+ *fd = lock_fd;
+
return true;
}
+bool
+fw3_lock()
+{
+ return fw3_lock_path(&fw3_lock_fd, FW3_LOCKFILE);
+}
+
+
void
-fw3_unlock(void)
+fw3_unlock_path(int *fd, const char *lockpath)
{
- if (lock_fd < 0)
+ if (*fd < 0)
return;
- if (flock(lock_fd, LOCK_UN))
+ if (flock(*fd, LOCK_UN))
warn("Cannot release exclusive lock: %s", strerror(errno));
- close(lock_fd);
+ close(*fd);
unlink(FW3_LOCKFILE);
- lock_fd = -1;
+ *fd = -1;
+}
+
+
+void
+fw3_unlock(void)
+{
+ fw3_unlock_path(&fw3_lock_fd, FW3_LOCKFILE);
}
diff --git a/utils.h b/utils.h
index 1ada0dd..2388072 100644
--- a/utils.h
+++ b/utils.h
@@ -102,6 +102,8 @@ bool fw3_has_table(bool ipv6, const char *table);
bool fw3_lock(void);
void fw3_unlock(void);
+bool fw3_lock_path(int *fw3_lock_fd, const char *path);
+void fw3_unlock_path(int *fw3_lock_fd, const char *path);
void fw3_write_statefile(void *state);
--
2.20.1
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel
More information about the openwrt-devel
mailing list