Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8702bb17 authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

New NativeDaemonConnector protocol adds a seqnum.

Allows for one socket to be multiplexed for multiple requests.
Doesn't use command sequence numbers for broadcasts - would make no sense.
Doesn't alter current default behavior so OEM's using these classes
won't notice a difference.
bug:5864209

Change-Id: Ie3b19c4f81eea868569229a365c8cb7de249c2dd
parent ca3bf255
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -24,11 +24,17 @@ 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;
    bool mWithSeq;
    FrameworkCommandCollection *mCommands;

public:
    FrameworkListener(const char *socketName);
    FrameworkListener(const char *socketName, bool withSeq);
    virtual ~FrameworkListener() {}

protected:
@@ -37,5 +43,6 @@ protected:

private:
    void dispatchCommand(SocketClient *c, char *data);
    void init(const char *socketName, bool withSeq);
};
#endif
+15 −2
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
#include "../../../frameworks/base/include/utils/List.h"

#include <pthread.h>
#include <cutils/atomic.h>
#include <sys/types.h>

class SocketClient {
@@ -24,18 +25,25 @@ class SocketClient {
    pthread_mutex_t mRefCountMutex;
    int mRefCount;

    int mCmdNum;

    bool mUseCmdNum;

public:
    SocketClient(int sock, bool owned);
    SocketClient(int sock, bool owned, bool useCmdNum);
    virtual ~SocketClient();

    int getSocket() { return mSocket; }
    pid_t getPid() const { return mPid; }
    uid_t getUid() const { return mUid; }
    gid_t getGid() const { return mGid; }
    void setCmdNum(int cmdNum) { android_atomic_release_store(cmdNum, &mCmdNum); }
    int getCmdNum() { return mCmdNum; }

    // Send null-terminated C strings:
    int sendMsg(int code, const char *msg, bool addErrno);
    int sendMsg(const char *msg);
    int sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum);

    //Sending binary data:
    int sendData(const void *data, int len);
@@ -46,6 +54,11 @@ 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);
    void init(int socket, bool owned, bool useCmdNum);
};

typedef android::List<SocketClient *> SocketClientCollection;
+5 −3
Original line number Diff line number Diff line
@@ -21,16 +21,18 @@
#include <sysutils/SocketClient.h>

class SocketListener {
    int                     mSock;
    bool                    mListen;
    const char              *mSocketName;
    int                     mSock;
    SocketClientCollection  *mClients;
    pthread_mutex_t         mClientsLock;
    bool                    mListen;
    int                     mCtrlPipe[2];
    pthread_t               mThread;
    bool                    mUseCmdNum;

public:
    SocketListener(const char *socketName, bool listen);
    SocketListener(const char *socketName, bool listen, bool useCmdNum);
    SocketListener(int socketFd, bool listen);

    virtual ~SocketListener();
@@ -38,7 +40,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;
@@ -46,5 +47,6 @@ protected:
private:
    static void *threadStart(void *obj);
    void runListener();
    void init(const char *socketName, int socketFd, bool listen, bool useCmdNum);
};
#endif
+34 −4
Original line number Diff line number Diff line
@@ -25,9 +25,21 @@
#include <sysutils/FrameworkCommand.h>
#include <sysutils/SocketClient.h>

FrameworkListener::FrameworkListener(const char *socketName, bool withSeq) :
                            SocketListener(socketName, true, withSeq) {
    init(socketName, withSeq);
}

FrameworkListener::FrameworkListener(const char *socketName) :
                            SocketListener(socketName, true) {
                            SocketListener(socketName, true, false) {
    init(socketName, false);
}

void FrameworkListener::init(const char *socketName, bool withSeq) {
    mCommands = new FrameworkCommandCollection();
    errorRate = 0;
    mCommandCount = 0;
    mWithSeq = withSeq;
}

bool FrameworkListener::onDataAvailable(SocketClient *c) {
@@ -69,6 +81,7 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
    bool esc = false;
    bool quote = false;
    int k;
    bool haveCmdNum = !mWithSeq;

    memset(argv, 0, sizeof(argv));
    memset(tmp, 0, sizeof(tmp));
@@ -115,9 +128,20 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
        *q = *p++;
        if (!quote && *q == ' ') {
            *q = '\0';
            if (!haveCmdNum) {
                char *endptr;
                int cmdNum = (int)strtol(tmp, &endptr, 0);
                if (endptr == NULL || *endptr != '\0') {
                    cli->sendMsg(500, "Invalid sequence number", false);
                    goto out;
                }
                cli->setCmdNum(cmdNum);
                haveCmdNum = true;
            } else {
                if (argc >= CMD_ARGS_MAX)
                    goto overflow;
                argv[argc++] = strdup(tmp);
            }
            memset(tmp, 0, sizeof(tmp));
            q = tmp;
            continue;
@@ -140,6 +164,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;

+37 −22
Original line number Diff line number Diff line
@@ -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 {
        fmt = "%.3d %s";
        arg = NULL;
    }
    /* 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);
            ret = asprintf(&buf, "%d %s (%s)", code, msg, strerror(errno));
        }
    } else {
        if (useCmdNum) {
            ret = asprintf(&buf, "%d %d %s", code, getCmdNum(), msg);
        } else {
            ret = asprintf(&buf, "%d %s", code, msg);
        }
    }
    /* Send the zero-terminated message */
    return sendMsg(buf);
    if (ret != -1) {
        ret = sendMsg(buf);
        free(buf);
    }
    return ret;
}

int SocketClient::sendMsg(const char *msg) {
Loading