blob: 3d4984d3b808f801837e6404240c27e7a0aaaca8 [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;
115
116 *(current++) = '"';
117 while (arg < end) {
118 switch (*arg) {
119 case '\\':
120 case '"':
121 *(current++) = '\\'; // fallthrough
122 default:
123 *(current++) = *(arg++);
124 }
125 }
126 *(current++) = '"';
127 *(current++) = '\0';
128 result = (char *)realloc(result, current-result);
129 return result;
130}
131
132
San Mehatdb017542009-05-20 15:27:14 -0700133int SocketClient::sendMsg(const char *msg) {
San Mehatc73a3a52009-06-15 14:06:03 -0700134 // Send the message including null character
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700135 if (sendData(msg, strlen(msg) + 1) != 0) {
136 SLOGW("Unable to send msg '%s'", msg);
137 return -1;
138 }
139 return 0;
140}
141
Selim Gurun7bf4c452012-02-27 16:04:37 -0800142int SocketClient::sendData(const void *data, int len) {
143
144 pthread_mutex_lock(&mWriteMutex);
145 int rc = sendDataLocked(data, len);
146 pthread_mutex_unlock(&mWriteMutex);
147
148 return rc;
149}
150
151int SocketClient::sendDataLocked(const void *data, int len) {
San Mehatd7680662009-05-12 11:16:59 -0700152 int rc = 0;
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700153 const char *p = (const char*) data;
154 int brtw = len;
San Mehatd7680662009-05-12 11:16:59 -0700155
Mattias Falk2e5fcd02011-05-13 16:25:38 +0200156 if (mSocket < 0) {
157 errno = EHOSTUNREACH;
158 return -1;
159 }
160
Brad Fitzpatrick16ae4782010-11-02 10:55:52 -0700161 if (len == 0) {
162 return 0;
163 }
164
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700165 while (brtw > 0) {
Selim Gurun6ac770f2012-03-12 10:20:42 -0700166 rc = send(mSocket, p, brtw, MSG_NOSIGNAL);
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100167 if (rc > 0) {
168 p += rc;
169 brtw -= rc;
170 continue;
171 }
172
173 if (rc < 0 && errno == EINTR)
174 continue;
175
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100176 if (rc == 0) {
San Mehat7e8529a2010-03-25 09:31:42 -0700177 SLOGW("0 length write :(");
San Mehatd7680662009-05-12 11:16:59 -0700178 errno = EIO;
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100179 } else {
180 SLOGW("write error (%s)", strerror(errno));
San Mehatd7680662009-05-12 11:16:59 -0700181 }
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100182 return -1;
San Mehatfa644ff2009-05-08 11:15:53 -0700183 }
San Mehatfa644ff2009-05-08 11:15:53 -0700184 return 0;
185}
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700186
187void SocketClient::incRef() {
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700188 pthread_mutex_lock(&mRefCountMutex);
189 mRefCount++;
190 pthread_mutex_unlock(&mRefCountMutex);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700191}
192
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700193bool SocketClient::decRef() {
194 bool deleteSelf = false;
195 pthread_mutex_lock(&mRefCountMutex);
196 mRefCount--;
197 if (mRefCount == 0) {
198 deleteSelf = true;
199 } else if (mRefCount < 0) {
200 SLOGE("SocketClient refcount went negative!");
201 }
202 pthread_mutex_unlock(&mRefCountMutex);
203 if (deleteSelf) {
204 delete this;
205 }
206 return deleteSelf;
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700207}