blob: c657526eeef9cad93299328b900aeca409e220c6 [file] [log] [blame]
San Mehatfa644ff2009-05-08 11:15:53 -07001#ifndef _SOCKET_CLIENT_H
2#define _SOCKET_CLIENT_H
3
San Mehatfa644ff2009-05-08 11:15:53 -07004#include <pthread.h>
Robert Greenwalt8702bb12012-02-07 12:23:14 -08005#include <cutils/atomic.h>
Kenny Root30abb722010-09-14 14:26:12 -07006#include <sys/types.h>
Mark Salyzyna6e96552012-01-24 20:30:10 -08007#include <sys/uio.h>
San Mehatfa644ff2009-05-08 11:15:53 -07008
9class SocketClient {
10 int mSocket;
Xianzhu Wang45202462011-09-29 12:59:55 +080011 bool mSocketOwned;
San Mehatfa644ff2009-05-08 11:15:53 -070012 pthread_mutex_t mWriteMutex;
13
Mark Salyzyna6e96552012-01-24 20:30:10 -080014 // Peer process ID
Kenny Root30abb722010-09-14 14:26:12 -070015 pid_t mPid;
16
Mark Salyzyna6e96552012-01-24 20:30:10 -080017 // Peer user ID
Kenny Root30abb722010-09-14 14:26:12 -070018 uid_t mUid;
19
Mark Salyzyna6e96552012-01-24 20:30:10 -080020 // Peer group ID
Kenny Root30abb722010-09-14 14:26:12 -070021 gid_t mGid;
22
Mark Salyzyna6e96552012-01-24 20:30:10 -080023 // Reference count (starts at 1)
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070024 pthread_mutex_t mRefCountMutex;
25 int mRefCount;
26
Robert Greenwalt8702bb12012-02-07 12:23:14 -080027 int mCmdNum;
28
29 bool mUseCmdNum;
30
San Mehatfa644ff2009-05-08 11:15:53 -070031public:
Xianzhu Wang45202462011-09-29 12:59:55 +080032 SocketClient(int sock, bool owned);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080033 SocketClient(int sock, bool owned, bool useCmdNum);
Xianzhu Wang45202462011-09-29 12:59:55 +080034 virtual ~SocketClient();
San Mehatfa644ff2009-05-08 11:15:53 -070035
Bernie Innocentica768592018-05-23 19:02:52 +090036 int getSocket() const { return mSocket; }
Kenny Root30abb722010-09-14 14:26:12 -070037 pid_t getPid() const { return mPid; }
38 uid_t getUid() const { return mUid; }
39 gid_t getGid() const { return mGid; }
Mark Salyzyna6e96552012-01-24 20:30:10 -080040 void setCmdNum(int cmdNum) {
41 android_atomic_release_store(cmdNum, &mCmdNum);
42 }
Robert Greenwalt8702bb12012-02-07 12:23:14 -080043 int getCmdNum() { return mCmdNum; }
San Mehatfa644ff2009-05-08 11:15:53 -070044
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070045 // Send null-terminated C strings:
San Mehatdb017542009-05-20 15:27:14 -070046 int sendMsg(int code, const char *msg, bool addErrno);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080047 int sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum);
Mark Salyzyna6e96552012-01-24 20:30:10 -080048 int sendMsg(const char *msg);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070049
Robert Greenwalt7599bfc2012-03-08 16:10:06 -080050 // Provides a mechanism to send a response code to the client.
51 // Sends the code and a null character.
Selim Gurun7bf4c452012-02-27 16:04:37 -080052 int sendCode(int code);
53
Robert Greenwalt7599bfc2012-03-08 16:10:06 -080054 // Provides a mechanism to send binary data to client.
55 // Sends the code and a null character, followed by 4 bytes of
Selim Gurun7bf4c452012-02-27 16:04:37 -080056 // big-endian length, and the data.
57 int sendBinaryMsg(int code, const void *data, int len);
58
59 // Sending binary data:
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070060 int sendData(const void *data, int len);
Mark Salyzyna6e96552012-01-24 20:30:10 -080061 // iovec contents not preserved through call
62 int sendDatav(struct iovec *iov, int iovcnt);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070063
64 // Optional reference counting. Reference count starts at 1. If
65 // it's decremented to 0, it deletes itself.
66 // SocketListener creates a SocketClient (at refcount 1) and calls
67 // decRef() when it's done with the client.
68 void incRef();
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -070069 bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
Robert Greenwalt8702bb12012-02-07 12:23:14 -080070
Mark Salyzyna6e96552012-01-24 20:30:10 -080071 // return a new string in quotes with '\\' and '\"' escaped for "my arg"
72 // transmissions
Robert Greenwalt59494772012-04-20 15:21:07 -070073 static char *quoteArg(const char *arg);
74
Robert Greenwalt8702bb12012-02-07 12:23:14 -080075private:
Robert Greenwalt8702bb12012-02-07 12:23:14 -080076 void init(int socket, bool owned, bool useCmdNum);
Selim Gurun7bf4c452012-02-27 16:04:37 -080077
Mark Salyzyna6e96552012-01-24 20:30:10 -080078 // Sending binary data. The caller should make sure this is protected
Selim Gurun7bf4c452012-02-27 16:04:37 -080079 // from multiple threads entering simultaneously.
Mark Salyzyna6e96552012-01-24 20:30:10 -080080 // returns 0 if successful, -1 if there is a 0 byte write or if any
81 // other error occurred (use errno to get the error)
82 int sendDataLockedv(struct iovec *iov, int iovcnt);
San Mehatfa644ff2009-05-08 11:15:53 -070083};
84
San Mehatfa644ff2009-05-08 11:15:53 -070085#endif