blob: e90afcd189e00d8ae0471a4824f2cbc36de7279f [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>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070028#include <unistd.h>
San Mehatfa644ff2009-05-08 11:15:53 -070029
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -070030#include <android-base/file.h>
31#include <android-base/macros.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070032#include <log/log.h>
San Mehatfa644ff2009-05-08 11:15:53 -070033#include <sysutils/SocketClient.h>
34
Robert Greenwalt8702bb12012-02-07 12:23:14 -080035SocketClient::SocketClient(int socket, bool owned) {
36 init(socket, owned, false);
37}
38
39SocketClient::SocketClient(int socket, bool owned, bool useCmdNum) {
40 init(socket, owned, useCmdNum);
41}
42
43void SocketClient::init(int socket, bool owned, bool useCmdNum) {
44 mSocket = socket;
45 mSocketOwned = owned;
46 mUseCmdNum = useCmdNum;
Yi Kong48885252018-07-24 16:34:27 -070047 pthread_mutex_init(&mWriteMutex, nullptr);
48 pthread_mutex_init(&mRefCountMutex, nullptr);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080049 mPid = -1;
50 mUid = -1;
51 mGid = -1;
52 mRefCount = 1;
53 mCmdNum = 0;
Kenny Root30abb722010-09-14 14:26:12 -070054
55 struct ucred creds;
56 socklen_t szCreds = sizeof(creds);
57 memset(&creds, 0, szCreds);
58
59 int err = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
60 if (err == 0) {
61 mPid = creds.pid;
62 mUid = creds.uid;
63 mGid = creds.gid;
64 }
San Mehatfa644ff2009-05-08 11:15:53 -070065}
66
Mark Salyzyn43895882014-01-29 11:25:01 -080067SocketClient::~SocketClient() {
Xianzhu Wang45202462011-09-29 12:59:55 +080068 if (mSocketOwned) {
69 close(mSocket);
70 }
71}
72
San Mehatdb017542009-05-20 15:27:14 -070073int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080074 return sendMsg(code, msg, addErrno, mUseCmdNum);
75}
76
77int SocketClient::sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum) {
San Mehatd7680662009-05-12 11:16:59 -070078 char *buf;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080079 int ret = 0;
San Mehat03f0d272009-05-26 15:18:25 -070080
San Mehatd7680662009-05-12 11:16:59 -070081 if (addErrno) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080082 if (useCmdNum) {
83 ret = asprintf(&buf, "%d %d %s (%s)", code, getCmdNum(), msg, strerror(errno));
84 } else {
85 ret = asprintf(&buf, "%d %s (%s)", code, msg, strerror(errno));
86 }
San Mehatd7680662009-05-12 11:16:59 -070087 } else {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080088 if (useCmdNum) {
89 ret = asprintf(&buf, "%d %d %s", code, getCmdNum(), msg);
90 } else {
91 ret = asprintf(&buf, "%d %s", code, msg);
92 }
San Mehatd7680662009-05-12 11:16:59 -070093 }
Mark Salyzyn23f04102012-01-24 20:30:10 -080094 // Send the zero-terminated message
Robert Greenwalt8702bb12012-02-07 12:23:14 -080095 if (ret != -1) {
96 ret = sendMsg(buf);
97 free(buf);
98 }
99 return ret;
San Mehatd7680662009-05-12 11:16:59 -0700100}
101
Mark Salyzyn23f04102012-01-24 20:30:10 -0800102// send 3-digit code, null, binary-length, binary data
Selim Gurun7bf4c452012-02-27 16:04:37 -0800103int SocketClient::sendBinaryMsg(int code, const void *data, int len) {
104
Mark Salyzyn23f04102012-01-24 20:30:10 -0800105 // 4 bytes for the code & null + 4 bytes for the len
Robert Greenwalt7599bfc2012-03-08 16:10:06 -0800106 char buf[8];
Mark Salyzyn23f04102012-01-24 20:30:10 -0800107 // Write the code
Robert Greenwalt7599bfc2012-03-08 16:10:06 -0800108 snprintf(buf, 4, "%.3d", code);
Mark Salyzyn23f04102012-01-24 20:30:10 -0800109 // Write the len
Selim Gurun7bf4c452012-02-27 16:04:37 -0800110 uint32_t tmp = htonl(len);
Robert Greenwalt7599bfc2012-03-08 16:10:06 -0800111 memcpy(buf + 4, &tmp, sizeof(uint32_t));
Selim Gurun7bf4c452012-02-27 16:04:37 -0800112
Mark Salyzyn23f04102012-01-24 20:30:10 -0800113 struct iovec vec[2];
114 vec[0].iov_base = (void *) buf;
115 vec[0].iov_len = sizeof(buf);
116 vec[1].iov_base = (void *) data;
117 vec[1].iov_len = len;
118
Selim Gurun7bf4c452012-02-27 16:04:37 -0800119 pthread_mutex_lock(&mWriteMutex);
Mark Salyzyn23f04102012-01-24 20:30:10 -0800120 int result = sendDataLockedv(vec, (len > 0) ? 2 : 1);
Selim Gurun7bf4c452012-02-27 16:04:37 -0800121 pthread_mutex_unlock(&mWriteMutex);
122
123 return result;
124}
125
126// Sends the code (c-string null-terminated).
127int SocketClient::sendCode(int code) {
Robert Greenwalt7599bfc2012-03-08 16:10:06 -0800128 char buf[4];
129 snprintf(buf, sizeof(buf), "%.3d", code);
130 return sendData(buf, sizeof(buf));
Selim Gurun7bf4c452012-02-27 16:04:37 -0800131}
132
Robert Greenwalt59494772012-04-20 15:21:07 -0700133char *SocketClient::quoteArg(const char *arg) {
134 int len = strlen(arg);
135 char *result = (char *)malloc(len * 2 + 3);
136 char *current = result;
137 const char *end = arg + len;
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800138 char *oldresult;
139
Yi Kong48885252018-07-24 16:34:27 -0700140 if(result == nullptr) {
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800141 SLOGW("malloc error (%s)", strerror(errno));
Yi Kong48885252018-07-24 16:34:27 -0700142 return nullptr;
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800143 }
Robert Greenwalt59494772012-04-20 15:21:07 -0700144
145 *(current++) = '"';
146 while (arg < end) {
147 switch (*arg) {
148 case '\\':
149 case '"':
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700150 *(current++) = '\\';
151 FALLTHROUGH_INTENDED;
Robert Greenwalt59494772012-04-20 15:21:07 -0700152 default:
153 *(current++) = *(arg++);
154 }
155 }
156 *(current++) = '"';
157 *(current++) = '\0';
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800158 oldresult = result; // save pointer in case realloc fails
Robert Greenwalt59494772012-04-20 15:21:07 -0700159 result = (char *)realloc(result, current-result);
Hong-Mei Li544a7f72013-04-01 11:24:44 +0800160 return result ? result : oldresult;
Robert Greenwalt59494772012-04-20 15:21:07 -0700161}
162
163
San Mehatdb017542009-05-20 15:27:14 -0700164int SocketClient::sendMsg(const char *msg) {
San Mehatc73a3a52009-06-15 14:06:03 -0700165 // Send the message including null character
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -0700166 if (sendData(msg, strlen(msg) + 1) != 0) {
167 SLOGW("Unable to send msg '%s'", msg);
168 return -1;
169 }
170 return 0;
171}
172
Selim Gurun7bf4c452012-02-27 16:04:37 -0800173int SocketClient::sendData(const void *data, int len) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800174 struct iovec vec[1];
175 vec[0].iov_base = (void *) data;
176 vec[0].iov_len = len;
Selim Gurun7bf4c452012-02-27 16:04:37 -0800177
178 pthread_mutex_lock(&mWriteMutex);
Mark Salyzyn23f04102012-01-24 20:30:10 -0800179 int rc = sendDataLockedv(vec, 1);
Selim Gurun7bf4c452012-02-27 16:04:37 -0800180 pthread_mutex_unlock(&mWriteMutex);
181
182 return rc;
183}
184
Mark Salyzyn23f04102012-01-24 20:30:10 -0800185int SocketClient::sendDatav(struct iovec *iov, int iovcnt) {
186 pthread_mutex_lock(&mWriteMutex);
187 int rc = sendDataLockedv(iov, iovcnt);
188 pthread_mutex_unlock(&mWriteMutex);
189
190 return rc;
191}
192
193int SocketClient::sendDataLockedv(struct iovec *iov, int iovcnt) {
San Mehatd7680662009-05-12 11:16:59 -0700194
Mattias Falk2e5fcd02011-05-13 16:25:38 +0200195 if (mSocket < 0) {
196 errno = EHOSTUNREACH;
197 return -1;
198 }
199
Mark Salyzyn23f04102012-01-24 20:30:10 -0800200 if (iovcnt <= 0) {
Brad Fitzpatrick16ae4782010-11-02 10:55:52 -0700201 return 0;
202 }
203
Mark Salyzyn23f04102012-01-24 20:30:10 -0800204 int current = 0;
205
206 for (;;) {
Tom Cherry2b3ebaf2020-07-31 15:21:54 -0700207 ssize_t rc = TEMP_FAILURE_RETRY(writev(mSocket, iov + current, iovcnt - current));
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100208
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100209 if (rc == 0) {
Tom Cherry2b3ebaf2020-07-31 15:21:54 -0700210 errno = EIO;
San Mehat7e8529a2010-03-25 09:31:42 -0700211 SLOGW("0 length write :(");
Tom Cherry2b3ebaf2020-07-31 15:21:54 -0700212 return -1;
213 } else if (rc < 0) {
214 SLOGW("write error (%s)", strerror(errno));
215 return -1;
San Mehatd7680662009-05-12 11:16:59 -0700216 }
Mark Salyzyn43895882014-01-29 11:25:01 -0800217
Tom Cherry2b3ebaf2020-07-31 15:21:54 -0700218 size_t written = rc;
219 while (current < iovcnt && written >= iov[current].iov_len) {
220 written -= iov[current].iov_len;
221 current++;
222 }
223 if (current == iovcnt) {
224 return 0;
225 }
226 iov[current].iov_base = (char*)iov[current].iov_base + written;
227 iov[current].iov_len -= written;
Elliott Hughesf6b62d02014-11-10 15:50:30 -0800228 }
San Mehatfa644ff2009-05-08 11:15:53 -0700229}
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700230
231void SocketClient::incRef() {
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700232 pthread_mutex_lock(&mRefCountMutex);
233 mRefCount++;
234 pthread_mutex_unlock(&mRefCountMutex);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700235}
236
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700237bool SocketClient::decRef() {
238 bool deleteSelf = false;
239 pthread_mutex_lock(&mRefCountMutex);
240 mRefCount--;
241 if (mRefCount == 0) {
242 deleteSelf = true;
243 } else if (mRefCount < 0) {
244 SLOGE("SocketClient refcount went negative!");
245 }
246 pthread_mutex_unlock(&mRefCountMutex);
247 if (deleteSelf) {
248 delete this;
249 }
250 return deleteSelf;
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700251}