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
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),
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;
~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:
int closeFdAndSetErrno(int fd, int error) {
close(fd);
- errno = error;
+ errno = -error;
return -1;
}
} 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)) {
FwmarkCommand command = {FwmarkCommand::ON_CONNECT, 0};
int error = FwmarkClient().send(&command, sizeof(command), sockfd);
if (error) {
- errno = error;
+ errno = -error;
return -1;
}
}
socketFd = socket(AF_INET6, SOCK_DGRAM, 0);
}
if (socketFd < 0) {
- return errno;
+ return -errno;
}
int error = setNetworkForSocket(netId, socketFd);
if (!error) {
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;
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);
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);
__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);
bool FwmarkServer::onDataAvailable(SocketClient* client) {
int fd = -1;
- processClient(client, &fd);
- int error = errno;
+ int error = processClient(client, &fd);
if (fd >= 0) {
close(fd);
}
return false;
}
-void FwmarkServer::processClient(SocketClient* client, int* fd) {
+int FwmarkServer::processClient(SocketClient* client, int* fd) {
FwmarkCommand command;
iovec iov;
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);
}
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());
// 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;
}
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;
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;
}
// 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;