blob: 1004f061159290c4ffeb546b017d5bb366909170 [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>
Mark Salyzyn23f04102012-01-24 20:30:10 -08009#include <sys/uio.h>
San Mehatfa644ff2009-05-08 11:15:53 -070010
11class SocketClient {
12 int mSocket;
Xianzhu Wang45202462011-09-29 12:59:55 +080013 bool mSocketOwned;
San Mehatfa644ff2009-05-08 11:15:53 -070014 pthread_mutex_t mWriteMutex;
15
Mark Salyzyn23f04102012-01-24 20:30:10 -080016 // Peer process ID
Kenny Root30abb722010-09-14 14:26:12 -070017 pid_t mPid;
18
Mark Salyzyn23f04102012-01-24 20:30:10 -080019 // Peer user ID
Kenny Root30abb722010-09-14 14:26:12 -070020 uid_t mUid;
21
Mark Salyzyn23f04102012-01-24 20:30:10 -080022 // Peer group ID
Kenny Root30abb722010-09-14 14:26:12 -070023 gid_t mGid;
24
Mark Salyzyn23f04102012-01-24 20:30:10 -080025 // Reference count (starts at 1)
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070026 pthread_mutex_t mRefCountMutex;
27 int mRefCount;
28
Robert Greenwalt8702bb12012-02-07 12:23:14 -080029 int mCmdNum;
30
31 bool mUseCmdNum;
32
San Mehatfa644ff2009-05-08 11:15:53 -070033public:
Xianzhu Wang45202462011-09-29 12:59:55 +080034 SocketClient(int sock, bool owned);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080035 SocketClient(int sock, bool owned, bool useCmdNum);
Xianzhu Wang45202462011-09-29 12:59:55 +080036 virtual ~SocketClient();
San Mehatfa644ff2009-05-08 11:15:53 -070037
38 int getSocket() { return mSocket; }
Kenny Root30abb722010-09-14 14:26:12 -070039 pid_t getPid() const { return mPid; }
40 uid_t getUid() const { return mUid; }
41 gid_t getGid() const { return mGid; }
Mark Salyzyn23f04102012-01-24 20:30:10 -080042 void setCmdNum(int cmdNum) {
43 android_atomic_release_store(cmdNum, &mCmdNum);
44 }
Robert Greenwalt8702bb12012-02-07 12:23:14 -080045 int getCmdNum() { return mCmdNum; }
San Mehatfa644ff2009-05-08 11:15:53 -070046
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070047 // Send null-terminated C strings:
San Mehatdb017542009-05-20 15:27:14 -070048 int sendMsg(int code, const char *msg, bool addErrno);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080049 int sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum);
Mark Salyzyn23f04102012-01-24 20:30:10 -080050 int sendMsg(const char *msg);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070051
Robert Greenwalt7599bfc2012-03-08 16:10:06 -080052 // Provides a mechanism to send a response code to the client.
53 // Sends the code and a null character.
Selim Gurun7bf4c452012-02-27 16:04:37 -080054 int sendCode(int code);
55
Robert Greenwalt7599bfc2012-03-08 16:10:06 -080056 // Provides a mechanism to send binary data to client.
57 // Sends the code and a null character, followed by 4 bytes of
Selim Gurun7bf4c452012-02-27 16:04:37 -080058 // big-endian length, and the data.
59 int sendBinaryMsg(int code, const void *data, int len);
60
61 // Sending binary data:
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070062 int sendData(const void *data, int len);
Mark Salyzyn23f04102012-01-24 20:30:10 -080063 // iovec contents not preserved through call
64 int sendDatav(struct iovec *iov, int iovcnt);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070065
66 // Optional reference counting. Reference count starts at 1. If
67 // it's decremented to 0, it deletes itself.
68 // SocketListener creates a SocketClient (at refcount 1) and calls
69 // decRef() when it's done with the client.
70 void incRef();
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -070071 bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
Robert Greenwalt8702bb12012-02-07 12:23:14 -080072
Mark Salyzyn23f04102012-01-24 20:30:10 -080073 // return a new string in quotes with '\\' and '\"' escaped for "my arg"
74 // transmissions
Robert Greenwalt59494772012-04-20 15:21:07 -070075 static char *quoteArg(const char *arg);
76
Robert Greenwalt8702bb12012-02-07 12:23:14 -080077private:
Robert Greenwalt8702bb12012-02-07 12:23:14 -080078 void init(int socket, bool owned, bool useCmdNum);
Selim Gurun7bf4c452012-02-27 16:04:37 -080079
Mark Salyzyn23f04102012-01-24 20:30:10 -080080 // Sending binary data. The caller should make sure this is protected
Selim Gurun7bf4c452012-02-27 16:04:37 -080081 // from multiple threads entering simultaneously.
Mark Salyzyn23f04102012-01-24 20:30:10 -080082 // returns 0 if successful, -1 if there is a 0 byte write or if any
83 // other error occurred (use errno to get the error)
84 int sendDataLockedv(struct iovec *iov, int iovcnt);
San Mehatfa644ff2009-05-08 11:15:53 -070085};
86
Mathias Agopianb7286aa2012-03-05 16:45:55 -080087typedef android::sysutils::List<SocketClient *> SocketClientCollection;
San Mehatfa644ff2009-05-08 11:15:53 -070088#endif