Home | History | Annotate | Download | only in sysutils
      1 #ifndef _SOCKET_CLIENT_H
      2 #define _SOCKET_CLIENT_H
      3 
      4 #include "List.h"
      5 
      6 #include <pthread.h>
      7 #include <cutils/atomic.h>
      8 #include <sys/types.h>
      9 
     10 class SocketClient {
     11     int             mSocket;
     12     bool            mSocketOwned;
     13     pthread_mutex_t mWriteMutex;
     14 
     15     /* Peer process ID */
     16     pid_t mPid;
     17 
     18     /* Peer user ID */
     19     uid_t mUid;
     20 
     21     /* Peer group ID */
     22     gid_t mGid;
     23 
     24     /* Reference count (starts at 1) */
     25     pthread_mutex_t mRefCountMutex;
     26     int mRefCount;
     27 
     28     int mCmdNum;
     29 
     30     bool mUseCmdNum;
     31 
     32 public:
     33     SocketClient(int sock, bool owned);
     34     SocketClient(int sock, bool owned, bool useCmdNum);
     35     virtual ~SocketClient();
     36 
     37     int getSocket() { return mSocket; }
     38     pid_t getPid() const { return mPid; }
     39     uid_t getUid() const { return mUid; }
     40     gid_t getGid() const { return mGid; }
     41     void setCmdNum(int cmdNum) { android_atomic_release_store(cmdNum, &mCmdNum); }
     42     int getCmdNum() { return mCmdNum; }
     43 
     44     // Send null-terminated C strings:
     45     int sendMsg(int code, const char *msg, bool addErrno);
     46     int sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum);
     47 
     48     // Provides a mechanism to send a response code to the client.
     49     // Sends the code and a null character.
     50     int sendCode(int code);
     51 
     52     // Provides a mechanism to send binary data to client.
     53     // Sends the code and a null character, followed by 4 bytes of
     54     // big-endian length, and the data.
     55     int sendBinaryMsg(int code, const void *data, int len);
     56 
     57     // Sending binary data:
     58     int sendData(const void *data, int len);
     59 
     60     // Optional reference counting.  Reference count starts at 1.  If
     61     // it's decremented to 0, it deletes itself.
     62     // SocketListener creates a SocketClient (at refcount 1) and calls
     63     // decRef() when it's done with the client.
     64     void incRef();
     65     bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
     66 
     67     // return a new string in quotes with '\\' and '\"' escaped for "my arg" transmissions
     68     static char *quoteArg(const char *arg);
     69 
     70 private:
     71     // Send null-terminated C strings
     72     int sendMsg(const char *msg);
     73     void init(int socket, bool owned, bool useCmdNum);
     74 
     75     // Sending binary data. The caller should use make sure this is protected
     76     // from multiple threads entering simultaneously.
     77     // returns 0 if successful, -1 if there is a 0 byte write and -2 if any other
     78     // error occurred (use errno to get the error)
     79     int sendDataLocked(const void *data, int len);
     80 };
     81 
     82 typedef android::sysutils::List<SocketClient *> SocketClientCollection;
     83 #endif
     84