blob: 02505d304f32aecd196adf16a61b5a6ed4e7ad5a [file] [log] [blame]
Mark Salyzyn66ce3e02016-09-28 10:07:20 -07001/*
2 * Copyright (C) 2009-2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SocketClient"
18
San Mehatfa644ff2009-05-08 11:15:53 -070019#include <alloca.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070020#include <arpa/inet.h>
San Mehatfa644ff2009-05-08 11:15:53 -070021#include <errno.h>
Elliott Hughesa744b052015-01-28 11:37:57 -080022#include <malloc.h>
San Mehatfa644ff2009-05-08 11:15:53 -070023#include <pthread.h>
Mark Salyzyn43895882014-01-29 11:25:01 -080024#include <signal.h>
San Mehatd7680662009-05-12 11:16:59 -070025#include <string.h>
Mark Salyzyn43895882014-01-29 11:25:01 -080026#include <sys/socket.h>
27#include <sys/types.h>
San Mehatfa644ff2009-05-08 11:15:53 -070028
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070029#include <android/log.h>
San Mehatfa644ff2009-05-08 11:15:53 -070030#include <sysutils/SocketClient.h>
31
Robert Greenwalt8702bb12012-02-07 12:23:14 -080032SocketClient::SocketClient(int socket, bool owned) {
33 init(socket, owned, false);
34}
35
36SocketClient::SocketClient(int socket, bool owned, bool useCmdNum) {
37 init(socket, owned, useCmdNum);
38}
39
40void SocketClient::init(int socket, bool owned, bool useCmdNum) {
41 mSocket = socket;
42 mSocketOwned = owned;
43 mUseCmdNum = useCmdNum;
San Mehatfa644ff2009-05-08 11:15:53 -070044 pthread_mutex_init(&mWriteMutex, NULL);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070045 pthread_mutex_init(&mRefCountMutex, NULL);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080046 mPid = -1;
47 mUid = -1;
48 mGid = -1;
49 mRefCount = 1;
50 mCmdNum = 0;
Kenny Root30abb722010-09-14 14:26:12 -070051
52 struct ucred creds;
53 socklen_t szCreds = sizeof(creds);
54 memset(&creds, 0, szCreds);
55
56 int err = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
57 if (err == 0) {
58 mPid = creds.pid;
59 mUid = creds.uid;
60 mGid = creds.gid;
61 }
San Mehatfa644ff2009-05-08 11:15:53 -070062}
63
Mark Salyzyn43895882014-01-29 11:25:01 -080064SocketClient::~SocketClient() {
Xianzhu Wang45202462011-09-29 12:59:55 +080065 if (mSocketOwned) {
66 close(mSocket);
67 }
68}
69
San Mehatdb017542009-05-20 15:27:14 -070070int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080071 return sendMsg(code, msg, addErrno, mUseCmdNum);
72}
73
74int SocketClient::sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum) {
San Mehatd7680662009-05-12 11:16:59 -070075 char *buf;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080076 int ret = 0;
San Mehat03f0d272009-05-26 15:18:25 -070077
San Mehatd7680662009-05-12 11:16:59 -070078 if (addErrno) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080079 if (useCmdNum) {
80 ret = asprintf(&buf, "%d %d %s (%s)", code, getCmdNum(), msg, strerror(errno));
81 } else {
82 ret = asprintf(&buf, "%d %s (%s)", code, msg, strerror(errno));
83 }
San Mehatd7680662009-05-12 11:16:59 -070084 } else {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080085 if (useCmdNum) {
86 ret = asprintf(&buf, "%d %d %s", code, getCmdNum(), msg);
87 } else {
88 ret = asprintf(&buf, "%d %s", code, msg);
89 }
San Mehatd7680662009-05-12 11:16:59 -070090 }
Mark Salyzyn23f04102012-01-24 20:30:10 -080091 // Send the zero-terminated message
Robert Greenwalt8702bb12012-02-07 12:23:14 -080092 if (ret != -1) {
93 ret = sendMsg(buf);
94 free(buf);
95 }
96 return ret;
San Mehatd7680662009-05-12 11:16:59 -070097}
98
Mark Salyzyn23f04102012-01-24 20:30:10 -080099// send 3-digit code, null, binary-length, binary data
Selim Gurun7bf4c452012-02-27 16:04:37 -0800100int SocketClient::sendBinaryMsg(int code, const void *data, int len) {
101
Mark Salyzyn23f04102012-01-24 20:30:10 -0800102 // 4 bytes for the code & null + 4 bytes for the len
Robert Greenwalt7599bfc2012-03-08 16:10:06 -0800103 char buf[8];
Mark Salyzyn23f04102012-01-24 20:30:10 -0800104 // Write the code
Robert Greenwalt7599bfc2012-03-08 16:10:06 -0800105 snprintf(buf, 4, "%.3d", code);
Mark Salyzyn23f04102012-01-24 20:30:10 -0800106 // Write the len
Selim Gurun7bf4c452012-02-27 16:04:37 -0800107 uint32_t tmp = htonl(len);
Robert Greenwalt7599bfc2012-03-08 16:10:06 -0800108 memcpy(buf + 4, &tmp, sizeof(uint32_t));
Selim Gurun7bf4c452012-02-27 16:04:37 -0800109
Mark Salyzyn23f04102012-01-24 20:30:10 -0800110 struct iovec vec[2];
111 vec[0].iov_base = (void *) buf;
112 vec[0].iov_len = sizeof(buf);
113 vec[1].iov_base = (void *) data;
114 vec[1].iov_len = len;
115
Selim Gurun7bf4c452012-02-27 16:04:37 -0800116 pthread_mutex_lock(&mWriteMutex);
Mark Salyzyn23f04102012-01-24 20:30:10 -0800117 int result = sendDataLockedv(vec, (len > 0) ? 2 : 1);
Selim Gurun7bf4c452012-02-27 16:04:37 -0800118 pthread_mutex_unlock(&mWriteMutex);
119
120 return result;
121}
122
123// Sends the code (c-string null-terminated).
124int SocketClient::sendCode(int code) {
Robert Greenwalt7599bfc2012-03-08 16:10:06 -0800125 char buf[4];
126 snprintf(buf, sizeof(buf), "%.3d", code);
127 return sendData(buf, sizeof(buf));
Selim Gurun7bf4c452012-02-27 16:04:37 -0800128}
129
Robert Greenwalt59494772012-04-20 15:21:07 -0700130char *SocketClient::quoteArg(const char *arg) {
131 int len = strlen(arg);
132 char *result = (char *)malloc(len * 2 + 3);
133 char *current = result;
134 const char *end = arg + len;
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800135 char *oldresult;
136
137 if(result == NULL) {
138 SLOGW("malloc error (%s)", strerror(errno));
139 return NULL;
140 }
Robert Greenwalt59494772012-04-20 15:21:07 -0700141
142 *(current++) = '"';
143 while (arg < end) {
144 switch (*arg) {
145 case '\\':
146 case '"':
147 *(current++) = '\\'; // fallthrough
148 default:
149 *(current++) = *(arg++);
150 }
151 }
152 *(current++) = '"';
153 *(current++) = '\0';
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800154 oldresult = result; // save pointer in case realloc fails
Robert Greenwalt59494772012-04-20 15:21:07 -0700155 result = (char *)realloc(result, current-result);
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800156 return result ? result : oldresult;
Robert Greenwalt59494772012-04-20 15:21:07 -0700157}
158
159
San Mehatdb017542009-05-20 15:27:14 -0700160int SocketClient::sendMsg(const char *msg) {
San Mehatc73a3a52009-06-15 14:06:03 -0700161 // Send the message including null character
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700162 if (sendData(msg, strlen(msg) + 1) != 0) {
163 SLOGW("Unable to send msg '%s'", msg);
164 return -1;
165 }
166 return 0;
167}
168
Selim Gurun7bf4c452012-02-27 16:04:37 -0800169int SocketClient::sendData(const void *data, int len) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800170 struct iovec vec[1];
171 vec[0].iov_base = (void *) data;
172 vec[0].iov_len = len;
Selim Gurun7bf4c452012-02-27 16:04:37 -0800173
174 pthread_mutex_lock(&mWriteMutex);
Mark Salyzyn23f04102012-01-24 20:30:10 -0800175 int rc = sendDataLockedv(vec, 1);
Selim Gurun7bf4c452012-02-27 16:04:37 -0800176 pthread_mutex_unlock(&mWriteMutex);
177
178 return rc;
179}
180
Mark Salyzyn23f04102012-01-24 20:30:10 -0800181int SocketClient::sendDatav(struct iovec *iov, int iovcnt) {
182 pthread_mutex_lock(&mWriteMutex);
183 int rc = sendDataLockedv(iov, iovcnt);
184 pthread_mutex_unlock(&mWriteMutex);
185
186 return rc;
187}
188
189int SocketClient::sendDataLockedv(struct iovec *iov, int iovcnt) {
San Mehatd7680662009-05-12 11:16:59 -0700190
Mattias Falk2e5fcd02011-05-13 16:25:38 +0200191 if (mSocket < 0) {
192 errno = EHOSTUNREACH;
193 return -1;
194 }
195
Mark Salyzyn23f04102012-01-24 20:30:10 -0800196 if (iovcnt <= 0) {
Brad Fitzpatrick16ae4782010-11-02 10:55:52 -0700197 return 0;
198 }
199
Mark Salyzyn43895882014-01-29 11:25:01 -0800200 int ret = 0;
201 int e = 0; // SLOGW and sigaction are not inert regarding errno
Mark Salyzyn23f04102012-01-24 20:30:10 -0800202 int current = 0;
203
Mark Salyzyn43895882014-01-29 11:25:01 -0800204 struct sigaction new_action, old_action;
205 memset(&new_action, 0, sizeof(new_action));
206 new_action.sa_handler = SIG_IGN;
207 sigaction(SIGPIPE, &new_action, &old_action);
208
Mark Salyzyn23f04102012-01-24 20:30:10 -0800209 for (;;) {
Mark Salyzyn43895882014-01-29 11:25:01 -0800210 ssize_t rc = TEMP_FAILURE_RETRY(
211 writev(mSocket, iov + current, iovcnt - current));
212
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100213 if (rc > 0) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800214 size_t written = rc;
215 while ((current < iovcnt) && (written >= iov[current].iov_len)) {
216 written -= iov[current].iov_len;
217 current++;
218 }
219 if (current == iovcnt) {
220 break;
221 }
222 iov[current].iov_base = (char *)iov[current].iov_base + written;
223 iov[current].iov_len -= written;
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100224 continue;
225 }
226
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100227 if (rc == 0) {
Mark Salyzyn43895882014-01-29 11:25:01 -0800228 e = EIO;
San Mehat7e8529a2010-03-25 09:31:42 -0700229 SLOGW("0 length write :(");
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100230 } else {
Mark Salyzyn43895882014-01-29 11:25:01 -0800231 e = errno;
232 SLOGW("write error (%s)", strerror(e));
San Mehatd7680662009-05-12 11:16:59 -0700233 }
Mark Salyzyn43895882014-01-29 11:25:01 -0800234 ret = -1;
235 break;
San Mehatfa644ff2009-05-08 11:15:53 -0700236 }
Mark Salyzyn43895882014-01-29 11:25:01 -0800237
238 sigaction(SIGPIPE, &old_action, &new_action);
239
Elliott Hughesf6b62d02014-11-10 15:50:30 -0800240 if (e != 0) {
Bo Huang4df4dfe2014-09-29 14:54:02 +0800241 errno = e;
Elliott Hughesf6b62d02014-11-10 15:50:30 -0800242 }
Mark Salyzyn43895882014-01-29 11:25:01 -0800243 return ret;
San Mehatfa644ff2009-05-08 11:15:53 -0700244}
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700245
246void SocketClient::incRef() {
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700247 pthread_mutex_lock(&mRefCountMutex);
248 mRefCount++;
249 pthread_mutex_unlock(&mRefCountMutex);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700250}
251
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700252bool SocketClient::decRef() {
253 bool deleteSelf = false;
254 pthread_mutex_lock(&mRefCountMutex);
255 mRefCount--;
256 if (mRefCount == 0) {
257 deleteSelf = true;
258 } else if (mRefCount < 0) {
259 SLOGE("SocketClient refcount went negative!");
260 }
261 pthread_mutex_unlock(&mRefCountMutex);
262 if (deleteSelf) {
263 delete this;
264 }
265 return deleteSelf;
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700266}