summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/sysutils/FrameworkListener.h4
-rw-r--r--include/sysutils/SocketClient.h12
-rw-r--r--include/sysutils/SocketListener.h1
-rw-r--r--libsysutils/src/FrameworkListener.cpp20
-rw-r--r--libsysutils/src/SocketClient.cpp25
-rw-r--r--libsysutils/src/SocketListener.cpp21
6 files changed, 59 insertions, 24 deletions
diff --git a/include/sysutils/FrameworkListener.h b/include/sysutils/FrameworkListener.h
index 142f50c..d8c4258 100644
--- a/include/sysutils/FrameworkListener.h
+++ b/include/sysutils/FrameworkListener.h
@@ -24,7 +24,11 @@ class SocketClient;
class FrameworkListener : public SocketListener {
public:
static const int CMD_ARGS_MAX = 16;
+
+ /* 1 out of errorRate will be dropped */
+ int errorRate;
private:
+ int mCommandCount;
FrameworkCommandCollection *mCommands;
public:
diff --git a/include/sysutils/SocketClient.h b/include/sysutils/SocketClient.h
index 7d2b1d6..96323ac 100644
--- a/include/sysutils/SocketClient.h
+++ b/include/sysutils/SocketClient.h
@@ -24,6 +24,9 @@ class SocketClient {
pthread_mutex_t mRefCountMutex;
int mRefCount;
+ pthread_mutex_t mCmdNumMutex;
+ int mCmdNum;
+
public:
SocketClient(int sock, bool owned);
virtual ~SocketClient();
@@ -32,12 +35,13 @@ public:
pid_t getPid() const { return mPid; }
uid_t getUid() const { return mUid; }
gid_t getGid() const { return mGid; }
+ void setCmdNum(int cmdNum);
+ int getCmdNum();
// Send null-terminated C strings:
int sendMsg(int code, const char *msg, bool addErrno);
- int sendMsg(const char *msg);
- // Sending binary data:
+ //Sending binary data:
int sendData(const void *data, int len);
// Optional reference counting. Reference count starts at 1. If
@@ -46,6 +50,10 @@ public:
// decRef() when it's done with the client.
void incRef();
bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
+
+private:
+ // Send null-terminated C strings
+ int sendMsg(const char *msg);
};
typedef android::List<SocketClient *> SocketClientCollection;
diff --git a/include/sysutils/SocketListener.h b/include/sysutils/SocketListener.h
index 6592b01..64a52f4 100644
--- a/include/sysutils/SocketListener.h
+++ b/include/sysutils/SocketListener.h
@@ -38,7 +38,6 @@ public:
int stopListener();
void sendBroadcast(int code, const char *msg, bool addErrno);
- void sendBroadcast(const char *msg);
protected:
virtual bool onDataAvailable(SocketClient *c) = 0;
diff --git a/libsysutils/src/FrameworkListener.cpp b/libsysutils/src/FrameworkListener.cpp
index 90be754..6b56bb5 100644
--- a/libsysutils/src/FrameworkListener.cpp
+++ b/libsysutils/src/FrameworkListener.cpp
@@ -28,6 +28,8 @@
FrameworkListener::FrameworkListener(const char *socketName) :
SocketListener(socketName, true) {
mCommands = new FrameworkCommandCollection();
+ errorRate = 0;
+ mCommandCount = 0;
}
bool FrameworkListener::onDataAvailable(SocketClient *c) {
@@ -69,6 +71,7 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
bool esc = false;
bool quote = false;
int k;
+ bool haveCmdNum = false;
memset(argv, 0, sizeof(argv));
memset(tmp, 0, sizeof(tmp));
@@ -115,9 +118,14 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
*q = *p++;
if (!quote && *q == ' ') {
*q = '\0';
- if (argc >= CMD_ARGS_MAX)
- goto overflow;
- argv[argc++] = strdup(tmp);
+ if (!haveCmdNum) {
+ cli->setCmdNum(atoi(tmp));
+ haveCmdNum = true;
+ } else {
+ if (argc >= CMD_ARGS_MAX)
+ goto overflow;
+ argv[argc++] = strdup(tmp);
+ }
memset(tmp, 0, sizeof(tmp));
q = tmp;
continue;
@@ -140,6 +148,12 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
goto out;
}
+ if (errorRate && (++mCommandCount % errorRate == 0)) {
+ /* ignore this command - let the timeout handler handle it */
+ SLOGE("Faking a timeout");
+ goto out;
+ }
+
for (i = mCommands->begin(); i != mCommands->end(); ++i) {
FrameworkCommand *c = *i;
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp
index 722dcb2..6e270bd 100644
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -17,9 +17,11 @@ SocketClient::SocketClient(int socket, bool owned)
, mUid(-1)
, mGid(-1)
, mRefCount(1)
+ , mCmdNum(0)
{
pthread_mutex_init(&mWriteMutex, NULL);
pthread_mutex_init(&mRefCountMutex, NULL);
+ pthread_mutex_init(&mCmdNumMutex, NULL);
struct ucred creds;
socklen_t szCreds = sizeof(creds);
@@ -46,19 +48,20 @@ int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
const char* fmt;
char tmp[1];
int len;
+ int cmdNum = getCmdNum();
if (addErrno) {
- fmt = "%.3d %s (%s)";
+ fmt = "%d %.3d %s (%s)";
arg = strerror(errno);
} else {
- fmt = "%.3d %s";
+ fmt = "%d %.3d %s";
arg = NULL;
}
/* Measure length of required buffer */
- len = snprintf(tmp, sizeof tmp, fmt, code, msg, arg);
+ len = snprintf(tmp, sizeof tmp, fmt, cmdNum, code, msg, arg);
/* Allocate in the stack, then write to it */
buf = (char*)alloca(len+1);
- snprintf(buf, len+1, fmt, code, msg, arg);
+ snprintf(buf, len+1, fmt, cmdNum, code, msg, arg);
/* Send the zero-terminated message */
return sendMsg(buf);
}
@@ -132,3 +135,17 @@ bool SocketClient::decRef() {
}
return deleteSelf;
}
+
+void SocketClient::setCmdNum(int cmdNum) {
+ pthread_mutex_lock(&mCmdNumMutex);
+ mCmdNum = cmdNum;
+ pthread_mutex_unlock(&mCmdNumMutex);
+}
+
+int SocketClient::getCmdNum() {
+ int ret;
+ pthread_mutex_lock(&mCmdNumMutex);
+ ret = mCmdNum;
+ pthread_mutex_unlock(&mCmdNumMutex);
+ return ret;
+}
diff --git a/libsysutils/src/SocketListener.cpp b/libsysutils/src/SocketListener.cpp
index 3f871ea..8f5329c 100644
--- a/libsysutils/src/SocketListener.cpp
+++ b/libsysutils/src/SocketListener.cpp
@@ -29,6 +29,8 @@
#include <sysutils/SocketListener.h>
#include <sysutils/SocketClient.h>
+#define DBG 0
+
SocketListener::SocketListener(const char *socketName, bool listen) {
mListen = listen;
mSocketName = socketName;
@@ -73,6 +75,7 @@ int SocketListener::startListener() {
mSocketName, strerror(errno));
return -1;
}
+ if (DBG) SLOGE("got mSock = %d for %s", mSock, mSocketName);
}
if (mListen && listen(mSock, 4) < 0) {
@@ -164,11 +167,11 @@ void SocketListener::runListener() {
max = fd;
}
pthread_mutex_unlock(&mClientsLock);
-
+ if (DBG) SLOGE("mListen=%d, max=%d, mSocketName=%s", mListen, max, mSocketName);
if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
if (errno == EINTR)
continue;
- SLOGE("select failed (%s)", strerror(errno));
+ SLOGE("select failed (%s) mListen=%d, max=%d", strerror(errno), mListen, max);
sleep(1);
continue;
} else if (!rc)
@@ -184,6 +187,7 @@ void SocketListener::runListener() {
do {
alen = sizeof(addr);
c = accept(mSock, &addr, &alen);
+ if (DBG) SLOGE("%s got %d from accept", mSocketName, c);
} while (c < 0 && errno == EINTR);
if (c < 0) {
SLOGE("accept failed (%s)", strerror(errno));
@@ -217,6 +221,7 @@ void SocketListener::runListener() {
* connection-based, remove and destroy it */
if (!onDataAvailable(c) && mListen) {
/* Remove the client from our array */
+ if (DBG) SLOGE("going to zap %d for %s", c->getSocket(), mSocketName);
pthread_mutex_lock(&mClientsLock);
for (it = mClients->begin(); it != mClients->end(); ++it) {
if (*it == c) {
@@ -244,15 +249,3 @@ void SocketListener::sendBroadcast(int code, const char *msg, bool addErrno) {
}
pthread_mutex_unlock(&mClientsLock);
}
-
-void SocketListener::sendBroadcast(const char *msg) {
- pthread_mutex_lock(&mClientsLock);
- SocketClientCollection::iterator i;
-
- for (i = mClients->begin(); i != mClients->end(); ++i) {
- if ((*i)->sendMsg(msg)) {
- SLOGW("Error sending broadcast (%s)", strerror(errno));
- }
- }
- pthread_mutex_unlock(&mClientsLock);
-}