summaryrefslogtreecommitdiff
path: root/libsysutils/src/SocketClient.cpp
diff options
context:
space:
mode:
authorRobert Greenwalt <rgreenwalt@google.com>2012-03-06 11:30:13 -0800
committerAndroid (Google) Code Review <android-gerrit@google.com>2012-03-06 11:30:13 -0800
commit78f6bcf853db3dd9b9b37a0ca4d4ee0374f6835a (patch)
treec8b92d557e35b39bcccad77c12025d795d5b8757 /libsysutils/src/SocketClient.cpp
parent397cf1986448cedde2a17093884a0ee8bf0d0b3f (diff)
parent8702bb17f40022e970e8acd40b348d074e39afc7 (diff)
Merge "New NativeDaemonConnector protocol adds a seqnum."
Diffstat (limited to 'libsysutils/src/SocketClient.cpp')
-rw-r--r--libsysutils/src/SocketClient.cpp59
1 files changed, 37 insertions, 22 deletions
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp
index 722dcb291..d9fc48c5f 100644
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -10,16 +10,25 @@
#include <sysutils/SocketClient.h>
-SocketClient::SocketClient(int socket, bool owned)
- : mSocket(socket)
- , mSocketOwned(owned)
- , mPid(-1)
- , mUid(-1)
- , mGid(-1)
- , mRefCount(1)
-{
+SocketClient::SocketClient(int socket, bool owned) {
+ init(socket, owned, false);
+}
+
+SocketClient::SocketClient(int socket, bool owned, bool useCmdNum) {
+ init(socket, owned, useCmdNum);
+}
+
+void SocketClient::init(int socket, bool owned, bool useCmdNum) {
+ mSocket = socket;
+ mSocketOwned = owned;
+ mUseCmdNum = useCmdNum;
pthread_mutex_init(&mWriteMutex, NULL);
pthread_mutex_init(&mRefCountMutex, NULL);
+ mPid = -1;
+ mUid = -1;
+ mGid = -1;
+ mRefCount = 1;
+ mCmdNum = 0;
struct ucred creds;
socklen_t szCreds = sizeof(creds);
@@ -41,26 +50,32 @@ SocketClient::~SocketClient()
}
int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
+ return sendMsg(code, msg, addErrno, mUseCmdNum);
+}
+
+int SocketClient::sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum) {
char *buf;
- const char* arg;
- const char* fmt;
- char tmp[1];
- int len;
+ int ret = 0;
if (addErrno) {
- fmt = "%.3d %s (%s)";
- arg = strerror(errno);
+ if (useCmdNum) {
+ ret = asprintf(&buf, "%d %d %s (%s)", code, getCmdNum(), msg, strerror(errno));
+ } else {
+ ret = asprintf(&buf, "%d %s (%s)", code, msg, strerror(errno));
+ }
} else {
- fmt = "%.3d %s";
- arg = NULL;
+ if (useCmdNum) {
+ ret = asprintf(&buf, "%d %d %s", code, getCmdNum(), msg);
+ } else {
+ ret = asprintf(&buf, "%d %s", code, msg);
+ }
}
- /* Measure length of required buffer */
- len = snprintf(tmp, sizeof tmp, fmt, code, msg, arg);
- /* Allocate in the stack, then write to it */
- buf = (char*)alloca(len+1);
- snprintf(buf, len+1, fmt, code, msg, arg);
/* Send the zero-terminated message */
- return sendMsg(buf);
+ if (ret != -1) {
+ ret = sendMsg(buf);
+ free(buf);
+ }
+ return ret;
}
int SocketClient::sendMsg(const char *msg) {