blob: d9713a6247c7dfa5c74795b629c20c31a0bee675 [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
Selim Gurun7bf4c452012-02-27 16:04:37 -080082
83int SocketClient::sendBinaryMsg(int code, const void *data, int len) {
84
85 /* 5 bytes for the code & space + 4 bytes for the len */
86 char buf[9];
87 /* Write the code */
88 snprintf(buf, 5, "%.3d ", code);
89 /* Write the len */
90 uint32_t tmp = htonl(len);
91 memcpy(buf + 5, &tmp, sizeof(uint32_t));
92
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) {
105 char buf[5];
106 snprintf(buf, 5, "%.3d ", code);
107 return sendData(buf, 5);
108}
109
San Mehatdb017542009-05-20 15:27:14 -0700110int SocketClient::sendMsg(const char *msg) {
San Mehatfa644ff2009-05-08 11:15:53 -0700111 if (mSocket < 0) {
112 errno = EHOSTUNREACH;
113 return -1;
114 }
115
San Mehatc73a3a52009-06-15 14:06:03 -0700116 // Send the message including null character
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700117 if (sendData(msg, strlen(msg) + 1) != 0) {
118 SLOGW("Unable to send msg '%s'", msg);
119 return -1;
120 }
121 return 0;
122}
123
Selim Gurun7bf4c452012-02-27 16:04:37 -0800124int SocketClient::sendData(const void *data, int len) {
125
126 pthread_mutex_lock(&mWriteMutex);
127 int rc = sendDataLocked(data, len);
128 pthread_mutex_unlock(&mWriteMutex);
129
130 return rc;
131}
132
133int SocketClient::sendDataLocked(const void *data, int len) {
San Mehatd7680662009-05-12 11:16:59 -0700134 int rc = 0;
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700135 const char *p = (const char*) data;
136 int brtw = len;
San Mehatd7680662009-05-12 11:16:59 -0700137
Brad Fitzpatrick16ae4782010-11-02 10:55:52 -0700138 if (len == 0) {
139 return 0;
140 }
141
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700142 while (brtw > 0) {
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100143 rc = write(mSocket, p, brtw);
144 if (rc > 0) {
145 p += rc;
146 brtw -= rc;
147 continue;
148 }
149
150 if (rc < 0 && errno == EINTR)
151 continue;
152
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100153 if (rc == 0) {
San Mehat7e8529a2010-03-25 09:31:42 -0700154 SLOGW("0 length write :(");
San Mehatd7680662009-05-12 11:16:59 -0700155 errno = EIO;
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100156 } else {
157 SLOGW("write error (%s)", strerror(errno));
San Mehatd7680662009-05-12 11:16:59 -0700158 }
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100159 return -1;
San Mehatfa644ff2009-05-08 11:15:53 -0700160 }
San Mehatfa644ff2009-05-08 11:15:53 -0700161 return 0;
162}
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700163
164void SocketClient::incRef() {
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700165 pthread_mutex_lock(&mRefCountMutex);
166 mRefCount++;
167 pthread_mutex_unlock(&mRefCountMutex);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700168}
169
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700170bool SocketClient::decRef() {
171 bool deleteSelf = false;
172 pthread_mutex_lock(&mRefCountMutex);
173 mRefCount--;
174 if (mRefCount == 0) {
175 deleteSelf = true;
176 } else if (mRefCount < 0) {
177 SLOGE("SocketClient refcount went negative!");
178 }
179 pthread_mutex_unlock(&mRefCountMutex);
180 if (deleteSelf) {
181 delete this;
182 }
183 return deleteSelf;
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700184}