Set errno in NetworkController commands.

Set errno in the NetworkController functions that are called by
CommandListener and where failure causes an operationError. This
is because operationError always appends errno to its messages,
and

"400 481 addInterfaceToNetwork() failed (Device or resource busy)"

is more useful than:

"400 481 addInterfaceToNetwork() failed (Success)"

We already call ALOGE to put descriptive messages in the system
log, but the bugreport already conveniently aggregates all netd
commands in one place in the network_management service dump, and
it would help if the errno in those messages were accurate.

Unfortunately many of the route and iptables commands shell out
to /sbin/ip and we can't return meaningful values, but we'll
fix that when (if?) we replace that with a proper netlink
implementation.

Bug: 15316764
Change-Id: Ia47f451029d611491aa72bca602de77333f2c6a0
diff --git a/server/NetworkController.cpp b/server/NetworkController.cpp
index 9504dc0..a26821f 100644
--- a/server/NetworkController.cpp
+++ b/server/NetworkController.cpp
@@ -69,6 +69,7 @@
     // asking for there to be no default network, which is a request we support.
     if (newNetId != NETID_UNSET && !isValidNetwork(newNetId)) {
         ALOGE("invalid netId %u", newNetId);
+        errno = EINVAL;
         return false;
     }
 
@@ -113,8 +114,10 @@
 
 bool NetworkController::setNetworkForUidRange(int uid_start, int uid_end, unsigned netId,
                                               bool forward_dns) {
-    if (uid_start > uid_end || !isValidNetwork(netId))
+    if (uid_start > uid_end || !isValidNetwork(netId)) {
+        errno = EINVAL;
         return false;
+    }
 
     android::RWLock::AutoWLock lock(mRWLock);
     for (std::list<UidEntry>::iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
@@ -129,8 +132,10 @@
 }
 
 bool NetworkController::clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId) {
-    if (uid_start > uid_end || !isValidNetwork(netId))
+    if (uid_start > uid_end || !isValidNetwork(netId)) {
+        errno = EINVAL;
         return false;
+    }
 
     android::RWLock::AutoWLock lock(mRWLock);
     for (std::list<UidEntry>::iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
@@ -139,6 +144,8 @@
         mUidMap.erase(it);
         return true;
     }
+
+    errno = ENOENT;
     return false;
 }
 
@@ -169,6 +176,7 @@
 bool NetworkController::createNetwork(unsigned netId, Permission permission) {
     if (netId < MIN_NET_ID || netId > MAX_NET_ID) {
         ALOGE("invalid netId %u", netId);
+        errno = EINVAL;
         return false;
     }
 
@@ -176,6 +184,7 @@
         android::RWLock::AutoWLock lock(mRWLock);
         if (!mValidNetworks.insert(netId).second) {
             ALOGE("duplicate netId %u", netId);
+            errno = EEXIST;
             return false;
         }
     }
@@ -187,12 +196,14 @@
 bool NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
     if (!isValidNetwork(netId) || !interface) {
         ALOGE("invalid netId %u or interface null", netId);
+        errno = EINVAL;
         return false;
     }
 
     unsigned existingNetId = getNetworkId(interface);
     if (existingNetId != NETID_UNSET) {
         ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
+        errno = EBUSY;
         return false;
     }
 
@@ -216,6 +227,7 @@
 bool NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
     if (!isValidNetwork(netId) || !interface) {
         ALOGE("invalid netId %u or interface null", netId);
+        errno = EINVAL;
         return false;
     }
 
@@ -230,6 +242,7 @@
     }
     if (!status) {
         ALOGE("interface %s not assigned to netId %u", interface, netId);
+        errno = ENOENT;
     }
 
     Permission permission = mPermissionsController->getPermissionForNetwork(netId);
@@ -250,6 +263,7 @@
 bool NetworkController::destroyNetwork(unsigned netId) {
     if (!isValidNetwork(netId)) {
         ALOGE("invalid netId %u", netId);
+        errno = EINVAL;
         return false;
     }
 
@@ -296,6 +310,7 @@
     for (size_t i = 0; i < netId.size(); ++i) {
         if (!isValidNetwork(netId[i])) {
             ALOGE("invalid netId %u", netId[i]);
+            errno = EINVAL;
             status = false;
             continue;
         }
@@ -347,11 +362,13 @@
                                     const char* nexthop, bool add) {
     if (!isValidNetwork(netId)) {
         ALOGE("invalid netId %u", netId);
+        errno = EINVAL;
         return false;
     }
 
     if (getNetworkId(interface) != netId) {
         ALOGE("netId %u has no such interface %s", netId, interface);
+        errno = ENOENT;
         return false;
     }