blob: d9fc48c5f28336a879041d5d088af9f8384e2ff3 [file] [log] [blame]
San Mehatfa644ff2009-05-08 11:15:53 -07001#include <alloca.h>
2#include <errno.h>
Kenny Root30abb722010-09-14 14:26:12 -07003#include <sys/socket.h>
San Mehatfa644ff2009-05-08 11:15:53 -07004#include <sys/types.h>
5#include <pthread.h>
San Mehatd7680662009-05-12 11:16:59 -07006#include <string.h>
San Mehatfa644ff2009-05-08 11:15:53 -07007
8#define LOG_TAG "SocketClient"
9#include <cutils/log.h>
10
11#include <sysutils/SocketClient.h>
12
Robert Greenwalt8702bb12012-02-07 12:23:14 -080013SocketClient::SocketClient(int socket, bool owned) {
14 init(socket, owned, false);
15}
16
17SocketClient::SocketClient(int socket, bool owned, bool useCmdNum) {
18 init(socket, owned, useCmdNum);
19}
20
21void SocketClient::init(int socket, bool owned, bool useCmdNum) {
22 mSocket = socket;
23 mSocketOwned = owned;
24 mUseCmdNum = useCmdNum;
San Mehatfa644ff2009-05-08 11:15:53 -070025 pthread_mutex_init(&mWriteMutex, NULL);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070026 pthread_mutex_init(&mRefCountMutex, NULL);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080027 mPid = -1;
28 mUid = -1;
29 mGid = -1;
30 mRefCount = 1;
31 mCmdNum = 0;
Kenny Root30abb722010-09-14 14:26:12 -070032
33 struct ucred creds;
34 socklen_t szCreds = sizeof(creds);
35 memset(&creds, 0, szCreds);
36
37 int err = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
38 if (err == 0) {
39 mPid = creds.pid;
40 mUid = creds.uid;
41 mGid = creds.gid;
42 }
San Mehatfa644ff2009-05-08 11:15:53 -070043}
44
Xianzhu Wang45202462011-09-29 12:59:55 +080045SocketClient::~SocketClient()
46{
47 if (mSocketOwned) {
48 close(mSocket);
49 }
50}
51
San Mehatdb017542009-05-20 15:27:14 -070052int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080053 return sendMsg(code, msg, addErrno, mUseCmdNum);
54}
55
56int SocketClient::sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum) {
San Mehatd7680662009-05-12 11:16:59 -070057 char *buf;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080058 int ret = 0;
San Mehat03f0d272009-05-26 15:18:25 -070059
San Mehatd7680662009-05-12 11:16:59 -070060 if (addErrno) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080061 if (useCmdNum) {
62 ret = asprintf(&buf, "%d %d %s (%s)", code, getCmdNum(), msg, strerror(errno));
63 } else {
64 ret = asprintf(&buf, "%d %s (%s)", code, msg, strerror(errno));
65 }
San Mehatd7680662009-05-12 11:16:59 -070066 } else {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080067 if (useCmdNum) {
68 ret = asprintf(&buf, "%d %d %s", code, getCmdNum(), msg);
69 } else {
70 ret = asprintf(&buf, "%d %s", code, msg);
71 }
San Mehatd7680662009-05-12 11:16:59 -070072 }
David 'Digit' Turneraf615092011-01-17 02:34:15 +010073 /* Send the zero-terminated message */
Robert Greenwalt8702bb12012-02-07 12:23:14 -080074 if (ret != -1) {
75 ret = sendMsg(buf);
76 free(buf);
77 }
78 return ret;
San Mehatd7680662009-05-12 11:16:59 -070079}
80
San Mehatdb017542009-05-20 15:27:14 -070081int SocketClient::sendMsg(const char *msg) {
San Mehatfa644ff2009-05-08 11:15:53 -070082 if (mSocket < 0) {
83 errno = EHOSTUNREACH;
84 return -1;
85 }
86
San Mehatc73a3a52009-06-15 14:06:03 -070087 // Send the message including null character
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070088 if (sendData(msg, strlen(msg) + 1) != 0) {
89 SLOGW("Unable to send msg '%s'", msg);
90 return -1;
91 }
92 return 0;
93}
94
95int SocketClient::sendData(const void* data, int len) {
San Mehatd7680662009-05-12 11:16:59 -070096 int rc = 0;
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070097 const char *p = (const char*) data;
98 int brtw = len;
San Mehatd7680662009-05-12 11:16:59 -070099
Brad Fitzpatrick16ae4782010-11-02 10:55:52 -0700100 if (len == 0) {
101 return 0;
102 }
103
San Mehatfa644ff2009-05-08 11:15:53 -0700104 pthread_mutex_lock(&mWriteMutex);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700105 while (brtw > 0) {
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100106 rc = write(mSocket, p, brtw);
107 if (rc > 0) {
108 p += rc;
109 brtw -= rc;
110 continue;
111 }
112
113 if (rc < 0 && errno == EINTR)
114 continue;
115
116 pthread_mutex_unlock(&mWriteMutex);
117 if (rc == 0) {
San Mehat7e8529a2010-03-25 09:31:42 -0700118 SLOGW("0 length write :(");
San Mehatd7680662009-05-12 11:16:59 -0700119 errno = EIO;
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100120 } else {
121 SLOGW("write error (%s)", strerror(errno));
San Mehatd7680662009-05-12 11:16:59 -0700122 }
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100123 return -1;
San Mehatfa644ff2009-05-08 11:15:53 -0700124 }
125 pthread_mutex_unlock(&mWriteMutex);
126 return 0;
127}
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700128
129void SocketClient::incRef() {
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700130 pthread_mutex_lock(&mRefCountMutex);
131 mRefCount++;
132 pthread_mutex_unlock(&mRefCountMutex);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700133}
134
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700135bool SocketClient::decRef() {
136 bool deleteSelf = false;
137 pthread_mutex_lock(&mRefCountMutex);
138 mRefCount--;
139 if (mRefCount == 0) {
140 deleteSelf = true;
141 } else if (mRefCount < 0) {
142 SLOGE("SocketClient refcount went negative!");
143 }
144 pthread_mutex_unlock(&mRefCountMutex);
145 if (deleteSelf) {
146 delete this;
147 }
148 return deleteSelf;
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700149}