diff options
author | Lorenzo Colitti <lorenzo@google.com> | 2017-03-24 06:34:54 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-03-24 06:34:54 +0000 |
commit | 332c59fdc6cb27457c7f803761e150c92bdc1d9b (patch) | |
tree | d44d064df2e9a1a5d867d170cfab1d96b27bdaed | |
parent | ab592e369dd35339f5ef68bf7d8c76473cf61951 (diff) | |
parent | 2c22058a3d26a4644ac7aaad70282cb8aa837eba (diff) | |
download | netd-332c59fdc6cb27457c7f803761e150c92bdc1d9b.tar.gz |
Merge "Update test code to match new iptables behaviour." am: a10b28e080 am: d3b4f5ba35
am: 2c22058a3d
Change-Id: Ib6dc71856a5ce9f9b9d105a5db460e4493e81a37
-rw-r--r-- | server/IptablesRestoreControllerTest.cpp | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/server/IptablesRestoreControllerTest.cpp b/server/IptablesRestoreControllerTest.cpp index a5d8a7bb..96394c19 100644 --- a/server/IptablesRestoreControllerTest.cpp +++ b/server/IptablesRestoreControllerTest.cpp @@ -16,6 +16,7 @@ #include <string> #include <fcntl.h> +#include <sys/file.h> #include <sys/socket.h> #include <sys/un.h> @@ -29,7 +30,9 @@ #include "IptablesRestoreController.h" #include "NetdConstants.h" -#define XTABLES_LOCK "@xtables" +#define XT_LOCK_NAME "/system/etc/xtables.lock" +#define XT_LOCK_ATTEMPTS 10 +#define XT_LOCK_POLL_INTERVAL_MS 100 using android::base::Join; using android::base::StringPrintf; @@ -110,19 +113,19 @@ public: } int acquireIptablesLock() { - mIptablesLock = socket(AF_UNIX, SOCK_STREAM, 0); - if (mIptablesLock == -1) { - return -errno; + mIptablesLock = open(XT_LOCK_NAME, O_CREAT, 0600); + if (mIptablesLock == -1) return mIptablesLock; + int attempts; + for (attempts = 0; attempts < XT_LOCK_ATTEMPTS; attempts++) { + if (flock(mIptablesLock, LOCK_EX | LOCK_NB) == 0) { + return 0; + } + usleep(XT_LOCK_POLL_INTERVAL_MS * 1000); } - sockaddr_un sun = { AF_UNIX, XTABLES_LOCK }; - sun.sun_path[0] = '\0'; - size_t len = offsetof(struct sockaddr_un, sun_path) + sizeof(XTABLES_LOCK) - 1; - if (int ret = bind(mIptablesLock, reinterpret_cast<sockaddr *>(&sun), len) == -1) { - ret = -errno; - close(mIptablesLock); - return ret; - } - return 0; + EXPECT_LT(attempts, XT_LOCK_ATTEMPTS) << + "Could not acquire iptables lock after " << XT_LOCK_ATTEMPTS << " attempts " << + XT_LOCK_POLL_INTERVAL_MS << "ms apart"; + return -1; } void releaseIptablesLock() { |