From a2a1f3149fb32d95bc81ddfd713d5bba9d6cbc61 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 27 Oct 2010 10:23:16 -0700 Subject: [PATCH 1/2] Let SocketClient users write binary data to clients. This is a dependency for the DNS proxy CLs. This CL also adds a new socket for the netd process to inherit which is owned by the inet group. (so only apps with the INTERNET permission can use the DNS proxy...) Change-Id: I8a51924e0ed56c6066f77e6f1b02d39bdadac51e --- include/sysutils/SocketClient.h | 4 ++++ libsysutils/src/SocketClient.cpp | 18 +++++++++++++----- rootdir/init.rc | 1 + 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/sysutils/SocketClient.h b/include/sysutils/SocketClient.h index e7fb17756..2fcc3311c 100644 --- a/include/sysutils/SocketClient.h +++ b/include/sysutils/SocketClient.h @@ -28,8 +28,12 @@ public: uid_t getUid() const { return mUid; } gid_t getGid() const { return mGid; } + // Send null-terminated C strings: int sendMsg(int code, const char *msg, bool addErrno); int sendMsg(const char *msg); + + // Sending binary data: + int sendData(const void *data, int len); }; typedef android::List SocketClientCollection; diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp index 8e5f1545a..ff2315b6f 100644 --- a/libsysutils/src/SocketClient.cpp +++ b/libsysutils/src/SocketClient.cpp @@ -50,14 +50,22 @@ int SocketClient::sendMsg(const char *msg) { } // Send the message including null character + if (sendData(msg, strlen(msg) + 1) != 0) { + SLOGW("Unable to send msg '%s'", msg); + return -1; + } + return 0; +} + +int SocketClient::sendData(const void* data, int len) { int rc = 0; - const char *p = msg; - int brtw = strlen(msg) + 1; + const char *p = (const char*) data; + int brtw = len; pthread_mutex_lock(&mWriteMutex); - while(brtw) { - if ((rc = write(mSocket,p, brtw)) < 0) { - SLOGW("Unable to send msg '%s' (%s)", msg, strerror(errno)); + while (brtw > 0) { + if ((rc = write(mSocket, p, brtw)) < 0) { + SLOGW("write error (%s)", strerror(errno)); pthread_mutex_unlock(&mWriteMutex); return -1; } else if (!rc) { diff --git a/rootdir/init.rc b/rootdir/init.rc index d1b27d080..90c017bb6 100644 --- a/rootdir/init.rc +++ b/rootdir/init.rc @@ -320,6 +320,7 @@ service vold /system/bin/vold service netd /system/bin/netd socket netd stream 0660 root system + socket dnsproxyd stream 0660 root inet service debuggerd /system/bin/debuggerd From b9634d05cf4fedb2ee49b0a41aeb129d35337ce9 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 2 Nov 2010 10:55:52 -0700 Subject: [PATCH 2/2] Permit 0 length writes. Change-Id: I087d0074c8d9e13ce814187475966da94f693fc0 --- libsysutils/src/SocketClient.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp index ff2315b6f..c9c7417f1 100644 --- a/libsysutils/src/SocketClient.cpp +++ b/libsysutils/src/SocketClient.cpp @@ -62,6 +62,10 @@ int SocketClient::sendData(const void* data, int len) { const char *p = (const char*) data; int brtw = len; + if (len == 0) { + return 0; + } + pthread_mutex_lock(&mWriteMutex); while (brtw > 0) { if ((rc = write(mSocket, p, brtw)) < 0) {