[PATCH 1/1] ifupdown: add write lock around ifstate access
Danomi Manchego
danomimanchego123 at gmail.com
Thu Sep 29 03:04:40 UTC 2016
So that concurrent access on systems with multiple
network interfaces correctly update the ifstate file.
Signed-off-by: Danomi Manchego <danomimanchego123 at gmail.com>
---
networking/ifupdown.c | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index b0bc0d7..1902c42 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -1196,6 +1196,29 @@ static llist_t *find_iface_state(llist_t *state_list, const char *iface)
return NULL;
}
+static FILE *open_ifstate_lock_file(void)
+{
+ FILE *fp = fopen_for_write("/var/lock/ifupdown.lock");
+
+ if (fp) {
+ struct flock fl;
+ fl.l_type = F_WRLCK;
+ fl.l_whence = SEEK_SET;
+ fl.l_start = 0;
+ fl.l_len = 0;
+ fcntl(fileno(fp), F_SETLKW, &fl);
+ }
+
+ return fp;
+}
+
+static void close_ifstate_lock_file(FILE *fp)
+{
+ if (fp) {
+ fclose(fp);
+ }
+}
+
/* read the previous state from the state file */
static llist_t *read_iface_state(void)
{
@@ -1276,9 +1299,14 @@ int ifupdown_main(int argc UNUSED_PARAM, char **argv)
}
if (!FORCE) {
+
+ FILE *state_lock_fp = open_ifstate_lock_file();
+
llist_t *state_list = read_iface_state();
const llist_t *iface_state = find_iface_state(state_list, iface);
+ close_ifstate_lock_file(state_lock_fp);
+
if (cmds == iface_up) {
/* ifup */
if (iface_state) {
@@ -1348,6 +1376,7 @@ int ifupdown_main(int argc UNUSED_PARAM, char **argv)
any_failures = 1;
} else if (!NO_ACT) {
/* update the state file */
+ FILE *state_lock_fp = open_ifstate_lock_file();
FILE *state_fp;
llist_t *state;
llist_t *state_list = read_iface_state();
@@ -1378,6 +1407,8 @@ int ifupdown_main(int argc UNUSED_PARAM, char **argv)
}
fclose(state_fp);
llist_free(state_list, free);
+
+ close_ifstate_lock_file(state_lock_fp);
}
next:
free(iface);
--
1.9.1
More information about the busybox
mailing list