blob: ae0e0770d33d0e5dd71c7dde48bcc33f5b5f96c9 [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>
Selim Gurun7bf4c452012-02-27 16:04:37 -08007#include <arpa/inet.h>
San Mehatfa644ff2009-05-08 11:15:53 -07008
9#define LOG_TAG "SocketClient"
10#include <cutils/log.h>
11
12#include <sysutils/SocketClient.h>
13
Robert Greenwalt8702bb12012-02-07 12:23:14 -080014SocketClient::SocketClient(int socket, bool owned) {
15 init(socket, owned, false);
16}
17
18SocketClient::SocketClient(int socket, bool owned, bool useCmdNum) {
19 init(socket, owned, useCmdNum);
20}
21
22void SocketClient::init(int socket, bool owned, bool useCmdNum) {
23 mSocket = socket;
24 mSocketOwned = owned;
25 mUseCmdNum = useCmdNum;
San Mehatfa644ff2009-05-08 11:15:53 -070026 pthread_mutex_init(&mWriteMutex, NULL);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070027 pthread_mutex_init(&mRefCountMutex, NULL);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080028 mPid = -1;
29 mUid = -1;
30 mGid = -1;
31 mRefCount = 1;
32 mCmdNum = 0;
Kenny Root30abb722010-09-14 14:26:12 -070033
34 struct ucred creds;
35 socklen_t szCreds = sizeof(creds);
36 memset(&creds, 0, szCreds);
37
38 int err = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
39 if (err == 0) {
40 mPid = creds.pid;
41 mUid = creds.uid;
42 mGid = creds.gid;
43 }
San Mehatfa644ff2009-05-08 11:15:53 -070044}
45
Xianzhu Wang45202462011-09-29 12:59:55 +080046SocketClient::~SocketClient()
47{
48 if (mSocketOwned) {
49 close(mSocket);
50 }
51}
52
San Mehatdb017542009-05-20 15:27:14 -070053int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080054 return sendMsg(code, msg, addErrno, mUseCmdNum);
55}
56
57int SocketClient::sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum) {
San Mehatd7680662009-05-12 11:16:59 -070058 char *buf;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080059 int ret = 0;
San Mehat03f0d272009-05-26 15:18:25 -070060
San Mehatd7680662009-05-12 11:16:59 -070061 if (addErrno) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080062 if (useCmdNum) {
63 ret = asprintf(&buf, "%d %d %s (%s)", code, getCmdNum(), msg, strerror(errno));
64 } else {
65 ret = asprintf(&buf, "%d %s (%s)", code, msg, strerror(errno));
66 }
San Mehatd7680662009-05-12 11:16:59 -070067 } else {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080068 if (useCmdNum) {
69 ret = asprintf(&buf, "%d %d %s", code, getCmdNum(), msg);
70 } else {
71 ret = asprintf(&buf, "%d %s", code, msg);
72 }
San Mehatd7680662009-05-12 11:16:59 -070073 }
David 'Digit' Turneraf615092011-01-17 02:34:15 +010074 /* Send the zero-terminated message */
Robert Greenwalt8702bb12012-02-07 12:23:14 -080075 if (ret != -1) {
76 ret = sendMsg(buf);
77 free(buf);
78 }
79 return ret;
San Mehatd7680662009-05-12 11:16:59 -070080}
81
Robert Greenwalt7599bfc2012-03-08 16:10:06 -080082/** send 3-digit code, null, binary-length, binary data */
Selim Gurun7bf4c452012-02-27 16:04:37 -080083int SocketClient::sendBinaryMsg(int code, const void *data, int len) {
84
Robert Greenwalt7599bfc2012-03-08 16:10:06 -080085 /* 4 bytes for the code & null + 4 bytes for the len */
86 char buf[8];
Selim Gurun7bf4c452012-02-27 16:04:37 -080087 /* Write the code */
Robert Greenwalt7599bfc2012-03-08 16:10:06 -080088 snprintf(buf, 4, "%.3d", code);
Selim Gurun7bf4c452012-02-27 16:04:37 -080089 /* Write the len */
90 uint32_t tmp = htonl(len);
Robert Greenwalt7599bfc2012-03-08 16:10:06 -080091 memcpy(buf + 4, &tmp, sizeof(uint32_t));
Selim Gurun7bf4c452012-02-27 16:04:37 -080092
93 pthread_mutex_lock(&mWriteMutex);
94 int result = sendDataLocked(buf, sizeof(buf));
95 if (result == 0 && len > 0) {
96 result = sendDataLocked(data, len);
97 }
98 pthread_mutex_unlock(&mWriteMutex);
99
100 return result;
101}
102
103// Sends the code (c-string null-terminated).
104int SocketClient::sendCode(int code) {
Robert Greenwalt7599bfc2012-03-08 16:10:06 -0800105 char buf[4];
106 snprintf(buf, sizeof(buf), "%.3d", code);
107 return sendData(buf, sizeof(buf));
Selim Gurun7bf4c452012-02-27 16:04:37 -0800108}
109
Robert Greenwalt59494772012-04-20 15:21:07 -0700110char *SocketClient::quoteArg(const char *arg) {
111 int len = strlen(arg);
112 char *result = (char *)malloc(len * 2 + 3);
113 char *current = result;
114 const char *end = arg + len;
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800115 char *oldresult;
116
117 if(result == NULL) {
118 SLOGW("malloc error (%s)", strerror(errno));
119 return NULL;
120 }
Robert Greenwalt59494772012-04-20 15:21:07 -0700121
122 *(current++) = '"';
123 while (arg < end) {
124 switch (*arg) {
125 case '\\':
126 case '"':
127 *(current++) = '\\'; // fallthrough
128 default:
129 *(current++) = *(arg++);
130 }
131 }
132 *(current++) = '"';
133 *(current++) = '\0';
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800134 oldresult = result; // save pointer in case realloc fails
Robert Greenwalt59494772012-04-20 15:21:07 -0700135 result = (char *)realloc(result, current-result);
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800136 return result ? result : oldresult;
Robert Greenwalt59494772012-04-20 15:21:07 -0700137}
138
139
San Mehatdb017542009-05-20 15:27:14 -0700140int SocketClient::sendMsg(const char *msg) {
San Mehatc73a3a52009-06-15 14:06:03 -0700141 // Send the message including null character
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700142 if (sendData(msg, strlen(msg) + 1) != 0) {
143 SLOGW("Unable to send msg '%s'", msg);
144 return -1;
145 }
146 return 0;
147}
148
Selim Gurun7bf4c452012-02-27 16:04:37 -0800149int SocketClient::sendData(const void *data, int len) {
150
151 pthread_mutex_lock(&mWriteMutex);
152 int rc = sendDataLocked(data, len);
153 pthread_mutex_unlock(&mWriteMutex);
154
155 return rc;
156}
157
158int SocketClient::sendDataLocked(const void *data, int len) {
San Mehatd7680662009-05-12 11:16:59 -0700159 int rc = 0;
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700160 const char *p = (const char*) data;
161 int brtw = len;
San Mehatd7680662009-05-12 11:16:59 -0700162
Mattias Falk2e5fcd02011-05-13 16:25:38 +0200163 if (mSocket < 0) {
164 errno = EHOSTUNREACH;
165 return -1;
166 }
167
Brad Fitzpatrick16ae4782010-11-02 10:55:52 -0700168 if (len == 0) {
169 return 0;
170 }
171
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700172 while (brtw > 0) {
Selim Gurun6ac770f2012-03-12 10:20:42 -0700173 rc = send(mSocket, p, brtw, MSG_NOSIGNAL);
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100174 if (rc > 0) {
175 p += rc;
176 brtw -= rc;
177 continue;
178 }
179
180 if (rc < 0 && errno == EINTR)
181 continue;
182
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100183 if (rc == 0) {
San Mehat7e8529a2010-03-25 09:31:42 -0700184 SLOGW("0 length write :(");
San Mehatd7680662009-05-12 11:16:59 -0700185 errno = EIO;
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100186 } else {
187 SLOGW("write error (%s)", strerror(errno));
San Mehatd7680662009-05-12 11:16:59 -0700188 }
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100189 return -1;
San Mehatfa644ff2009-05-08 11:15:53 -0700190 }
San Mehatfa644ff2009-05-08 11:15:53 -0700191 return 0;
192}
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700193
194void SocketClient::incRef() {
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700195 pthread_mutex_lock(&mRefCountMutex);
196 mRefCount++;
197 pthread_mutex_unlock(&mRefCountMutex);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700198}
199
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700200bool SocketClient::decRef() {
201 bool deleteSelf = false;
202 pthread_mutex_lock(&mRefCountMutex);
203 mRefCount--;
204 if (mRefCount == 0) {
205 deleteSelf = true;
206 } else if (mRefCount < 0) {
207 SLOGE("SocketClient refcount went negative!");
208 }
209 pthread_mutex_unlock(&mRefCountMutex);
210 if (deleteSelf) {
211 delete this;
212 }
213 return deleteSelf;
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700214}