Replace RW lock with mutex, shared_mutex
Test: built, flashed, booted
system/netd/tests/runtests.sh passes
Change-Id: I42b52d815b6ba0ba6f93dc27e83a900d2abec715
diff --git a/server/BandwidthController.h b/server/BandwidthController.h
index 4b72f18..882dea4 100644
--- a/server/BandwidthController.h
+++ b/server/BandwidthController.h
@@ -21,14 +21,13 @@
#include <string>
#include <utility>
#include <vector>
-
-#include <utils/RWLock.h>
+#include <mutex>
#include "NetdConstants.h"
class BandwidthController {
public:
- android::RWLock lock;
+ std::mutex lock;
BandwidthController();
diff --git a/server/CommandListener.cpp b/server/CommandListener.cpp
index 0585bad..70302d9 100644
--- a/server/CommandListener.cpp
+++ b/server/CommandListener.cpp
@@ -84,25 +84,25 @@
class LockingFrameworkCommand : public FrameworkCommand {
public:
- LockingFrameworkCommand(FrameworkCommand *wrappedCmd, android::RWLock& lock) :
+ LockingFrameworkCommand(FrameworkCommand *wrappedCmd, std::mutex& lock) :
FrameworkCommand(wrappedCmd->getCommand()),
mWrappedCmd(wrappedCmd),
mLock(lock) {}
int runCommand(SocketClient *c, int argc, char **argv) {
- android::RWLock::AutoWLock lock(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
return mWrappedCmd->runCommand(c, argc, argv);
}
private:
FrameworkCommand *mWrappedCmd;
- android::RWLock& mLock;
+ std::mutex& mLock;
};
} // namespace
-void CommandListener::registerLockingCmd(FrameworkCommand *cmd, android::RWLock& lock) {
+void CommandListener::registerLockingCmd(FrameworkCommand *cmd, std::mutex& lock) {
registerCmd(new LockingFrameworkCommand(cmd, lock));
}
diff --git a/server/CommandListener.h b/server/CommandListener.h
index 95e5830..c23f17a 100644
--- a/server/CommandListener.h
+++ b/server/CommandListener.h
@@ -17,8 +17,9 @@
#ifndef _COMMANDLISTENER_H__
#define _COMMANDLISTENER_H__
+#include <mutex>
+
#include <sysutils/FrameworkListener.h>
-#include "utils/RWLock.h"
#include "NetdCommand.h"
#include "NetdConstants.h"
@@ -44,7 +45,7 @@
static constexpr const char* SOCKET_NAME = "netd";
private:
- void registerLockingCmd(FrameworkCommand *cmd, android::RWLock& lock);
+ void registerLockingCmd(FrameworkCommand *cmd, std::mutex& lock);
void registerLockingCmd(FrameworkCommand *cmd) {
registerLockingCmd(cmd, android::net::gBigNetdLock);
}
diff --git a/server/FirewallController.h b/server/FirewallController.h
index 8a222d3..9b0c013 100644
--- a/server/FirewallController.h
+++ b/server/FirewallController.h
@@ -17,12 +17,11 @@
#ifndef _FIREWALL_CONTROLLER_H
#define _FIREWALL_CONTROLLER_H
+#include <mutex>
#include <set>
#include <string>
#include <vector>
-#include <utils/RWLock.h>
-
#include "NetdConstants.h"
enum FirewallRule { DENY, ALLOW };
@@ -75,7 +74,7 @@
static const char* ICMPV6_TYPES[];
- android::RWLock lock;
+ std::mutex lock;
protected:
friend class FirewallControllerTest;
diff --git a/server/NetdConstants.h b/server/NetdConstants.h
index 4f7d923..8db9ba3 100644
--- a/server/NetdConstants.h
+++ b/server/NetdConstants.h
@@ -17,6 +17,7 @@
#ifndef _NETD_CONSTANTS_H
#define _NETD_CONSTANTS_H
+#include <mutex>
#include <string>
#include <list>
#include <ifaddrs.h>
@@ -27,8 +28,6 @@
#include <private/android_filesystem_config.h>
-#include "utils/RWLock.h"
-
const int PROTECT_MARK = 0x1;
const int MAX_SYSTEM_UID = AID_APP - 1;
@@ -93,7 +92,7 @@
* CommandListener has only one user (NetworkManagementService), which is connected through a
* FrameworkListener that passes in commands one at a time.
*/
-extern android::RWLock gBigNetdLock;
+extern std::mutex gBigNetdLock;
} // namespace net
} // namespace android
diff --git a/server/NetdHwService.cpp b/server/NetdHwService.cpp
index 65afe9b..06723cf 100644
--- a/server/NetdHwService.cpp
+++ b/server/NetdHwService.cpp
@@ -140,7 +140,7 @@
}
Return <StatusCode> NetdHwService::setIpForwardEnable(bool enable) {
- android::RWLock::AutoWLock _lock(gCtls->tetherCtrl.lock);
+ std::lock_guard<std::mutex> _lock(gCtls->tetherCtrl.lock);
bool success = enable ? gCtls->tetherCtrl.enableForwarding(FORWARDING_REQUESTER) :
gCtls->tetherCtrl.disableForwarding(FORWARDING_REQUESTER);
@@ -150,7 +150,7 @@
Return <StatusCode> NetdHwService::setForwardingBetweenInterfaces(
const hidl_string& inputIfName, const hidl_string& outputIfName, bool enable) {
- android::RWLock::AutoWLock _lock(gCtls->tetherCtrl.lock);
+ std::lock_guard<std::mutex> _lock(gCtls->tetherCtrl.lock);
// TODO: check that one interface is an OEM interface and the other is another OEM interface, an
// IPsec interface or a dummy interface.
diff --git a/server/NetdNativeService.cpp b/server/NetdNativeService.cpp
index 5253096..99c4452 100644
--- a/server/NetdNativeService.cpp
+++ b/server/NetdNativeService.cpp
@@ -88,7 +88,7 @@
#define NETD_LOCKING_RPC(permission, lock) \
ENFORCE_PERMISSION(permission); \
- android::RWLock::AutoWLock _lock(lock);
+ std::lock_guard<std::mutex> _lock(lock);
#define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
@@ -376,7 +376,7 @@
}
binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
- NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock)
+ NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
*ret = gCtls->tetherCtrl.applyDnsInterfaces();
return binder::Status::ok();
@@ -404,7 +404,7 @@
} // namespace
binder::Status NetdNativeService::tetherGetStats(PersistableBundle *bundle) {
- NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock)
+ NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
const auto& statsList = gCtls->tetherCtrl.getTetherStats();
if (!isOk(statsList)) {
diff --git a/server/NetworkController.cpp b/server/NetworkController.cpp
index ed5156f..ac709a0 100644
--- a/server/NetworkController.cpp
+++ b/server/NetworkController.cpp
@@ -149,12 +149,12 @@
}
unsigned NetworkController::getDefaultNetwork() const {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
return mDefaultNetId;
}
int NetworkController::setDefaultNetwork(unsigned netId) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
if (netId == mDefaultNetId) {
return 0;
@@ -241,14 +241,14 @@
}
uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
return getNetworkForDnsLocked(netId, uid);
}
// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
// the VPN that applies to the UID if any; otherwise, the default network.
unsigned NetworkController::getNetworkForUser(uid_t uid) const {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
return virtualNetwork->getNetId();
}
@@ -278,13 +278,13 @@
}
unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
return getNetworkForConnectLocked(uid);
}
void NetworkController::getNetworkContext(
unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
struct android_net_context nc = {
.app_netid = netId,
@@ -341,12 +341,12 @@
}
unsigned NetworkController::getNetworkForInterface(const char* interface) const {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
return getNetworkForInterfaceLocked(interface);
}
bool NetworkController::isVirtualNetwork(unsigned netId) const {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
return isVirtualNetworkLocked(netId);
}
@@ -382,7 +382,7 @@
}
int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
return createPhysicalNetworkLocked(netId, permission);
}
@@ -391,7 +391,7 @@
return -EINVAL;
}
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
if (!isValidNetworkLocked(*pNetId)) {
break;
@@ -413,7 +413,7 @@
}
int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
ALOGE("invalid netId %u", netId);
@@ -433,7 +433,7 @@
}
int NetworkController::destroyNetwork(unsigned netId) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
if (netId == LOCAL_NET_ID) {
ALOGE("cannot destroy local network");
@@ -486,7 +486,7 @@
}
int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
if (!isValidNetworkLocked(netId)) {
ALOGE("no such netId %u", netId);
@@ -513,7 +513,7 @@
}
int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
if (!isValidNetworkLocked(netId)) {
ALOGE("no such netId %u", netId);
@@ -524,26 +524,26 @@
}
Permission NetworkController::getPermissionForUser(uid_t uid) const {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
return getPermissionForUserLocked(uid);
}
void NetworkController::setPermissionForUsers(Permission permission,
const std::vector<uid_t>& uids) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
for (uid_t uid : uids) {
mUsers[uid] = permission;
}
}
int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
return checkUserNetworkAccessLocked(uid, netId);
}
int NetworkController::setPermissionForNetworks(Permission permission,
const std::vector<unsigned>& netIds) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
for (unsigned netId : netIds) {
Network* network = getNetworkLocked(netId);
if (!network) {
@@ -563,7 +563,7 @@
}
int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
Network* network = getNetworkLocked(netId);
if (!network) {
ALOGE("no such netId %u", netId);
@@ -580,7 +580,7 @@
}
int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
Network* network = getNetworkLocked(netId);
if (!network) {
ALOGE("no such netId %u", netId);
@@ -608,8 +608,7 @@
}
void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
- android::RWLock::AutoWLock lock(mRWLock);
-
+ ScopedWLock lock(mRWLock);
if (ifIndex == 0) {
ALOGE("Attempting to add address %s without ifindex", address);
return;
@@ -619,7 +618,7 @@
// Returns whether we should call SOCK_DESTROY on the removed address.
bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
// First, update mAddressToIfindices map
auto ifindicesIter = mAddressToIfindices.find(address);
if (ifindicesIter == mAddressToIfindices.end()) {
@@ -660,24 +659,24 @@
}
bool NetworkController::canProtect(uid_t uid) const {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
return canProtectLocked(uid);
}
void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
mProtectableUsers.insert(uids.begin(), uids.end());
}
void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
- android::RWLock::AutoWLock lock(mRWLock);
+ ScopedWLock lock(mRWLock);
for (uid_t uid : uids) {
mProtectableUsers.erase(uid);
}
}
void NetworkController::dump(DumpWriter& dw) {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
dw.incIndent();
dw.println("NetworkController");
@@ -782,7 +781,7 @@
int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
const char* nexthop, bool add, bool legacy, uid_t uid) {
- android::RWLock::AutoRLock lock(mRWLock);
+ ScopedRLock lock(mRWLock);
if (!isValidNetworkLocked(netId)) {
ALOGE("no such netId %u", netId);
diff --git a/server/NetworkController.h b/server/NetworkController.h
index 5e7af80..eb7d670 100644
--- a/server/NetworkController.h
+++ b/server/NetworkController.h
@@ -17,16 +17,18 @@
#ifndef NETD_SERVER_NETWORK_CONTROLLER_H
#define NETD_SERVER_NETWORK_CONTROLLER_H
+
+#include <android-base/thread_annotations.h>
#include <android/multinetwork.h>
+
#include "NetdConstants.h"
#include "Permission.h"
-#include "utils/RWLock.h"
-
+#include <sys/types.h>
#include <list>
#include <map>
#include <set>
-#include <sys/types.h>
+#include <shared_mutex>
#include <unordered_map>
#include <unordered_set>
#include <vector>
@@ -36,6 +38,9 @@
namespace android {
namespace net {
+typedef std::shared_lock<std::shared_mutex> ScopedRLock;
+typedef std::lock_guard<std::shared_mutex> ScopedWLock;
+
constexpr uint32_t kHandleMagic = 0xcafed00d;
// Utility to convert from netId to net_handle_t. Doing this here as opposed to exporting
@@ -146,7 +151,6 @@
unsigned getNetworkForInterfaceLocked(const char* interface) const;
bool canProtectLocked(uid_t uid) const;
bool isVirtualNetworkLocked(unsigned netId) const;
-
VirtualNetwork* getVirtualNetworkForUserLocked(uid_t uid) const;
Permission getPermissionForUserLocked(uid_t uid) const;
int checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const;
@@ -162,7 +166,7 @@
// mRWLock guards all accesses to mDefaultNetId, mNetworks, mUsers, mProtectableUsers,
// mIfindexToLastNetId and mAddressToIfindices.
- mutable android::RWLock mRWLock;
+ mutable std::shared_mutex mRWLock;
unsigned mDefaultNetId;
std::map<unsigned, Network*> mNetworks; // Map keys are NetIds.
std::map<uid_t, Permission> mUsers;
diff --git a/server/RouteController.cpp b/server/RouteController.cpp
index c78854d..ccec437 100644
--- a/server/RouteController.cpp
+++ b/server/RouteController.cpp
@@ -155,7 +155,7 @@
}
uint32_t RouteController::getIfIndex(const char* interface) {
- android::RWLock::AutoRLock lock(sInterfaceToTableLock);
+ std::shared_lock<std::shared_mutex> lock(sInterfaceToTableLock);
auto iter = sInterfaceToTable.find(interface);
if (iter == sInterfaceToTable.end()) {
@@ -167,7 +167,7 @@
}
uint32_t RouteController::getRouteTableForInterface(const char* interface) {
- android::RWLock::AutoRLock lock(sInterfaceToTableLock);
+ std::shared_lock<std::shared_mutex> lock(sInterfaceToTableLock);
return getRouteTableForInterfaceLocked(interface);
}
@@ -191,7 +191,7 @@
addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
- android::RWLock::AutoRLock lock(sInterfaceToTableLock);
+ std::shared_lock<std::shared_mutex> lock(sInterfaceToTableLock);
for (const auto& entry : sInterfaceToTable) {
addTableName(entry.second, entry.first, &contents);
}
@@ -927,7 +927,7 @@
// Returns 0 on success or negative errno on failure.
WARN_UNUSED_RESULT int RouteController::flushRoutes(const char* interface) {
- android::RWLock::AutoWLock lock(sInterfaceToTableLock);
+ std::lock_guard<std::shared_mutex> lock(sInterfaceToTableLock);
uint32_t table = getRouteTableForInterfaceLocked(interface);
if (table == RT_TABLE_UNSPEC) {
@@ -1089,9 +1089,9 @@
}
// Protects sInterfaceToTable.
-android::RWLock RouteController::sInterfaceToTableLock;
-
+std::shared_mutex RouteController::sInterfaceToTableLock;
std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
+
} // namespace net
} // namespace android
diff --git a/server/RouteController.h b/server/RouteController.h
index 6e10cce..57920d6 100644
--- a/server/RouteController.h
+++ b/server/RouteController.h
@@ -20,9 +20,10 @@
#include "NetdConstants.h"
#include "Permission.h"
-#include <map>
-#include <sys/types.h>
#include <linux/netlink.h>
+#include <sys/types.h>
+#include <map>
+#include <shared_mutex>
namespace android {
namespace net {
@@ -110,9 +111,8 @@
private:
friend class RouteControllerTest;
-
// Protects access to interfaceToTable.
- static android::RWLock sInterfaceToTableLock;
+ static std::shared_mutex sInterfaceToTableLock;
static std::map<std::string, uint32_t> sInterfaceToTable;
static int configureDummyNetwork();
diff --git a/server/TetherController.h b/server/TetherController.h
index ce2d964..d1d337e 100644
--- a/server/TetherController.h
+++ b/server/TetherController.h
@@ -26,6 +26,7 @@
#include "NetdConstants.h"
+
namespace android {
namespace net {
@@ -137,7 +138,7 @@
static constexpr const char* LOCAL_RAW_PREROUTING = "tetherctrl_raw_PREROUTING";
static constexpr const char* LOCAL_TETHER_COUNTERS_CHAIN = "tetherctrl_counters";
- android::RWLock lock;
+ std::mutex lock;
private:
bool setIpFwdEnabled();
diff --git a/server/main.cpp b/server/main.cpp
index 0f5716b..a14cd8b 100644
--- a/server/main.cpp
+++ b/server/main.cpp
@@ -19,6 +19,7 @@
#include <signal.h>
#include <errno.h>
#include <string.h>
+#include <mutex>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
@@ -29,7 +30,6 @@
#define LOG_TAG "Netd"
#include "log/log.h"
-#include "utils/RWLock.h"
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
@@ -64,7 +64,7 @@
const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
-android::RWLock android::net::gBigNetdLock;
+std::mutex android::net::gBigNetdLock;
int main() {
using android::net::gLog;