OSDN Git Service

Return negative errno (instead of positive) on failure.
authorSreeram Ramachandran <sreeram@google.com>
Sun, 22 Jun 2014 18:02:57 +0000 (11:02 -0700)
committerSreeram Ramachandran <sreeram@google.com>
Mon, 23 Jun 2014 16:32:42 +0000 (09:32 -0700)
http://ag/489245 changed some return values from bools to errno values. However,
in forthcoming CLs, @lorenzo uses the convention of negative errno to indicate
failure. So, be consistent with that style.

Change-Id: I3eac8f142c36a2e779cda289c07ee374c49e2f6b

client/FwmarkClient.cpp
client/FwmarkClient.h
client/NetdClient.cpp
include/NetdClient.h
server/FwmarkServer.cpp
server/FwmarkServer.h

index 03cd5fb..db2009f 100644 (file)
@@ -43,7 +43,7 @@ FwmarkClient::~FwmarkClient() {
 int FwmarkClient::send(void* data, size_t len, int fd) {
     mChannel = socket(AF_UNIX, SOCK_STREAM, 0);
     if (mChannel == -1) {
-        return errno;
+        return -errno;
     }
 
     if (TEMP_FAILURE_RETRY(connect(mChannel, reinterpret_cast<const sockaddr*>(&FWMARK_SERVER_PATH),
@@ -78,13 +78,13 @@ int FwmarkClient::send(void* data, size_t len, int fd) {
     memcpy(CMSG_DATA(cmsgh), &fd, sizeof(fd));
 
     if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) {
-        return errno;
+        return -errno;
     }
 
     int error = 0;
 
     if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) {
-        return errno;
+        return -errno;
     }
 
     return error;
index 37f89bc..620275e 100644 (file)
@@ -29,7 +29,7 @@ public:
     ~FwmarkClient();
 
     // Sends |data| to the fwmark server, along with |fd| as ancillary data using cmsg(3).
-    // Returns 0 on success or an errno value on failure.
+    // Returns 0 on success or a negative errno value on failure.
     int send(void* data, size_t len, int fd);
 
 private:
index 7380224..f499f2a 100644 (file)
@@ -43,7 +43,7 @@ SocketFunctionType libcSocket = 0;
 
 int closeFdAndSetErrno(int fd, int error) {
     close(fd);
-    errno = error;
+    errno = -error;
     return -1;
 }
 
@@ -58,7 +58,7 @@ int netdClientAccept4(int sockfd, sockaddr* addr, socklen_t* addrlen, int flags)
     } else {
         socklen_t familyLen = sizeof(family);
         if (getsockopt(acceptedSocket, SOL_SOCKET, SO_DOMAIN, &family, &familyLen) == -1) {
-            return closeFdAndSetErrno(acceptedSocket, errno);
+            return closeFdAndSetErrno(acceptedSocket, -errno);
         }
     }
     if (FwmarkClient::shouldSetFwmark(family)) {
@@ -76,7 +76,7 @@ int netdClientConnect(int sockfd, const sockaddr* addr, socklen_t addrlen) {
         FwmarkCommand command = {FwmarkCommand::ON_CONNECT, 0};
         int error = FwmarkClient().send(&command, sizeof(command), sockfd);
         if (error) {
-            errno = error;
+            errno = -error;
             return -1;
         }
     }
@@ -124,7 +124,7 @@ int setNetworkForTarget(unsigned netId, std::atomic_uint* target) {
         socketFd = socket(AF_INET6, SOCK_DGRAM, 0);
     }
     if (socketFd < 0) {
-        return errno;
+        return -errno;
     }
     int error = setNetworkForSocket(netId, socketFd);
     if (!error) {
@@ -166,12 +166,12 @@ extern "C" void netdClientInitNetIdForResolv(NetIdForResolvFunctionType* functio
 
 extern "C" int getNetworkForSocket(unsigned* netId, int socketFd) {
     if (!netId || socketFd < 0) {
-        return EBADF;
+        return -EBADF;
     }
     Fwmark fwmark;
     socklen_t fwmarkLen = sizeof(fwmark.intValue);
     if (getsockopt(socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) {
-        return errno;
+        return -errno;
     }
     *netId = fwmark.netId;
     return 0;
@@ -183,7 +183,7 @@ extern "C" unsigned getNetworkForProcess() {
 
 extern "C" int setNetworkForSocket(unsigned netId, int socketFd) {
     if (socketFd < 0) {
-        return EBADF;
+        return -EBADF;
     }
     FwmarkCommand command = {FwmarkCommand::SELECT_NETWORK, netId};
     return FwmarkClient().send(&command, sizeof(command), socketFd);
@@ -199,7 +199,7 @@ extern "C" int setNetworkForResolv(unsigned netId) {
 
 extern "C" int protectFromVpn(int socketFd) {
     if (socketFd < 0) {
-        return EBADF;
+        return -EBADF;
     }
     FwmarkCommand command = {FwmarkCommand::PROTECT_FROM_VPN, 0};
     return FwmarkClient().send(&command, sizeof(command), socketFd);
index 1a2f8be..742902f 100644 (file)
@@ -22,7 +22,7 @@
 
 __BEGIN_DECLS
 
-// All functions below that return an int return 0 on success or an errno value on failure.
+// All functions below that return an int return 0 on success or a negative errno value on failure.
 
 int getNetworkForSocket(unsigned* netId, int socketFd);
 int setNetworkForSocket(unsigned netId, int socketFd);
index 605e724..b5a5872 100644 (file)
@@ -34,8 +34,7 @@ FwmarkServer::FwmarkServer(NetworkController* networkController,
 
 bool FwmarkServer::onDataAvailable(SocketClient* client) {
     int fd = -1;
-    processClient(client, &fd);
-    int error = errno;
+    int error = processClient(client, &fd);
     if (fd >= 0) {
         close(fd);
     }
@@ -50,7 +49,7 @@ bool FwmarkServer::onDataAvailable(SocketClient* client) {
     return false;
 }
 
-void FwmarkServer::processClient(SocketClient* client, int* fd) {
+int FwmarkServer::processClient(SocketClient* client, int* fd) {
     FwmarkCommand command;
 
     iovec iov;
@@ -73,12 +72,11 @@ void FwmarkServer::processClient(SocketClient* client, int* fd) {
 
     int messageLength = TEMP_FAILURE_RETRY(recvmsg(client->getSocket(), &message, 0));
     if (messageLength <= 0) {
-        return;
+        return -errno;
     }
 
     if (messageLength != sizeof(command)) {
-        errno = EINVAL;
-        return;
+        return -EBADMSG;
     }
 
     cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message);
@@ -88,14 +86,13 @@ void FwmarkServer::processClient(SocketClient* client, int* fd) {
     }
 
     if (*fd < 0) {
-        errno = EBADF;
-        return;
+        return -EBADF;
     }
 
     Fwmark fwmark;
     socklen_t fwmarkLen = sizeof(fwmark.intValue);
     if (getsockopt(*fd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) {
-        return;
+        return -errno;
     }
 
     fwmark.permission = mPermissionsController->getPermissionForUser(client->getUid());
@@ -105,8 +102,7 @@ void FwmarkServer::processClient(SocketClient* client, int* fd) {
             // Called after a socket accept(). The kernel would've marked the netId into the socket
             // already, so we just need to check permissions here.
             if (!mPermissionsController->isUserPermittedOnNetwork(client->getUid(), fwmark.netId)) {
-                errno = EPERM;
-                return;
+                return -EPERM;
             }
             break;
         }
@@ -132,13 +128,11 @@ void FwmarkServer::processClient(SocketClient* client, int* fd) {
                     fwmark.protectedFromVpn = true;
                 }
                 if (!mNetworkController->isValidNetwork(command.netId)) {
-                    errno = ENONET;
-                    return;
+                    return -ENONET;
                 }
                 if (!mPermissionsController->isUserPermittedOnNetwork(client->getUid(),
                                                                       command.netId)) {
-                    errno = EPERM;
-                    return;
+                    return -EPERM;
                 }
             }
             break;
@@ -152,14 +146,13 @@ void FwmarkServer::processClient(SocketClient* client, int* fd) {
 
         default: {
             // unknown command
-            errno = EINVAL;
-            return;
+            return -EPROTO;
         }
     }
 
     if (setsockopt(*fd, SOL_SOCKET, SO_MARK, &fwmark.intValue, sizeof(fwmark.intValue)) == -1) {
-        return;
+        return -errno;
     }
 
-    errno = 0;
+    return 0;
 }
index 5629bf9..04fb280 100644 (file)
@@ -31,8 +31,8 @@ private:
     // Overridden from SocketListener:
     bool onDataAvailable(SocketClient* client);
 
-    // Returns success / failure implicitly via errno.
-    void processClient(SocketClient* client, int* fd);
+    // Returns 0 on success or a negative errno value on failure.
+    int processClient(SocketClient* client, int* fd);
 
     NetworkController* const mNetworkController;
     PermissionsController* const mPermissionsController;