blob: 85b58efcc660762d76003ebc7c3b93a9f72501ab [file] [log] [blame]
San Mehatfa644ff2009-05-08 11:15:53 -07001#ifndef _SOCKET_CLIENT_H
2#define _SOCKET_CLIENT_H
3
Mathias Agopianb7286aa2012-03-05 16:45:55 -08004#include "List.h"
San Mehatfa644ff2009-05-08 11:15:53 -07005
6#include <pthread.h>
Robert Greenwalt8702bb12012-02-07 12:23:14 -08007#include <cutils/atomic.h>
Kenny Root30abb722010-09-14 14:26:12 -07008#include <sys/types.h>
San Mehatfa644ff2009-05-08 11:15:53 -07009
10class SocketClient {
11 int mSocket;
Xianzhu Wang45202462011-09-29 12:59:55 +080012 bool mSocketOwned;
San Mehatfa644ff2009-05-08 11:15:53 -070013 pthread_mutex_t mWriteMutex;
14
Kenny Root30abb722010-09-14 14:26:12 -070015 /* 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
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070024 /* Reference count (starts at 1) */
25 pthread_mutex_t mRefCountMutex;
26 int mRefCount;
27
Robert Greenwalt8702bb12012-02-07 12:23:14 -080028 int mCmdNum;
29
30 bool mUseCmdNum;
31
San Mehatfa644ff2009-05-08 11:15:53 -070032public:
Xianzhu Wang45202462011-09-29 12:59:55 +080033 SocketClient(int sock, bool owned);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080034 SocketClient(int sock, bool owned, bool useCmdNum);
Xianzhu Wang45202462011-09-29 12:59:55 +080035 virtual ~SocketClient();
San Mehatfa644ff2009-05-08 11:15:53 -070036
37 int getSocket() { return mSocket; }
Kenny Root30abb722010-09-14 14:26:12 -070038 pid_t getPid() const { return mPid; }
39 uid_t getUid() const { return mUid; }
40 gid_t getGid() const { return mGid; }
Robert Greenwalt8702bb12012-02-07 12:23:14 -080041 void setCmdNum(int cmdNum) { android_atomic_release_store(cmdNum, &mCmdNum); }
42 int getCmdNum() { return mCmdNum; }
San Mehatfa644ff2009-05-08 11:15:53 -070043
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070044 // Send null-terminated C strings:
San Mehatdb017542009-05-20 15:27:14 -070045 int sendMsg(int code, const char *msg, bool addErrno);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080046 int sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070047
Robert Greenwalt7599bfc2012-03-08 16:10:06 -080048 // Provides a mechanism to send a response code to the client.
49 // Sends the code and a null character.
Selim Gurun7bf4c452012-02-27 16:04:37 -080050 int sendCode(int code);
51
Robert Greenwalt7599bfc2012-03-08 16:10:06 -080052 // Provides a mechanism to send binary data to client.
53 // Sends the code and a null character, followed by 4 bytes of
Selim Gurun7bf4c452012-02-27 16:04:37 -080054 // big-endian length, and the data.
55 int sendBinaryMsg(int code, const void *data, int len);
56
57 // Sending binary data:
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070058 int sendData(const void *data, int len);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070059
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();
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -070065 bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
Robert Greenwalt8702bb12012-02-07 12:23:14 -080066
Robert Greenwalt59494772012-04-20 15:21:07 -070067 // return a new string in quotes with '\\' and '\"' escaped for "my arg" transmissions
68 static char *quoteArg(const char *arg);
69
Robert Greenwalt8702bb12012-02-07 12:23:14 -080070private:
71 // Send null-terminated C strings
72 int sendMsg(const char *msg);
73 void init(int socket, bool owned, bool useCmdNum);
Selim Gurun7bf4c452012-02-27 16:04:37 -080074
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);
San Mehatfa644ff2009-05-08 11:15:53 -070080};
81
Mathias Agopianb7286aa2012-03-05 16:45:55 -080082typedef android::sysutils::List<SocketClient *> SocketClientCollection;
San Mehatfa644ff2009-05-08 11:15:53 -070083#endif