Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 1 | /* |
| 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 Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 19 | #include <alloca.h> |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 20 | #include <arpa/inet.h> |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 21 | #include <errno.h> |
Elliott Hughes | a744b05 | 2015-01-28 11:37:57 -0800 | [diff] [blame] | 22 | #include <malloc.h> |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 23 | #include <pthread.h> |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 24 | #include <signal.h> |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 25 | #include <string.h> |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 26 | #include <sys/socket.h> |
| 27 | #include <sys/types.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 29 | |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 30 | #include <log/log.h> |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 31 | #include <sysutils/SocketClient.h> |
| 32 | |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 33 | SocketClient::SocketClient(int socket, bool owned) { |
| 34 | init(socket, owned, false); |
| 35 | } |
| 36 | |
| 37 | SocketClient::SocketClient(int socket, bool owned, bool useCmdNum) { |
| 38 | init(socket, owned, useCmdNum); |
| 39 | } |
| 40 | |
| 41 | void SocketClient::init(int socket, bool owned, bool useCmdNum) { |
| 42 | mSocket = socket; |
| 43 | mSocketOwned = owned; |
| 44 | mUseCmdNum = useCmdNum; |
Yi Kong | 4888525 | 2018-07-24 16:34:27 -0700 | [diff] [blame] | 45 | pthread_mutex_init(&mWriteMutex, nullptr); |
| 46 | pthread_mutex_init(&mRefCountMutex, nullptr); |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 47 | mPid = -1; |
| 48 | mUid = -1; |
| 49 | mGid = -1; |
| 50 | mRefCount = 1; |
| 51 | mCmdNum = 0; |
Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 52 | |
| 53 | struct ucred creds; |
| 54 | socklen_t szCreds = sizeof(creds); |
| 55 | memset(&creds, 0, szCreds); |
| 56 | |
| 57 | int err = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds); |
| 58 | if (err == 0) { |
| 59 | mPid = creds.pid; |
| 60 | mUid = creds.uid; |
| 61 | mGid = creds.gid; |
| 62 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 65 | SocketClient::~SocketClient() { |
Xianzhu Wang | 4520246 | 2011-09-29 12:59:55 +0800 | [diff] [blame] | 66 | if (mSocketOwned) { |
| 67 | close(mSocket); |
| 68 | } |
| 69 | } |
| 70 | |
San Mehat | db01754 | 2009-05-20 15:27:14 -0700 | [diff] [blame] | 71 | int SocketClient::sendMsg(int code, const char *msg, bool addErrno) { |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 72 | return sendMsg(code, msg, addErrno, mUseCmdNum); |
| 73 | } |
| 74 | |
| 75 | int SocketClient::sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum) { |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 76 | char *buf; |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 77 | int ret = 0; |
San Mehat | 03f0d27 | 2009-05-26 15:18:25 -0700 | [diff] [blame] | 78 | |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 79 | if (addErrno) { |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 80 | if (useCmdNum) { |
| 81 | ret = asprintf(&buf, "%d %d %s (%s)", code, getCmdNum(), msg, strerror(errno)); |
| 82 | } else { |
| 83 | ret = asprintf(&buf, "%d %s (%s)", code, msg, strerror(errno)); |
| 84 | } |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 85 | } else { |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 86 | if (useCmdNum) { |
| 87 | ret = asprintf(&buf, "%d %d %s", code, getCmdNum(), msg); |
| 88 | } else { |
| 89 | ret = asprintf(&buf, "%d %s", code, msg); |
| 90 | } |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 91 | } |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 92 | // Send the zero-terminated message |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 93 | if (ret != -1) { |
| 94 | ret = sendMsg(buf); |
| 95 | free(buf); |
| 96 | } |
| 97 | return ret; |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 100 | // send 3-digit code, null, binary-length, binary data |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 101 | int SocketClient::sendBinaryMsg(int code, const void *data, int len) { |
| 102 | |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 103 | // 4 bytes for the code & null + 4 bytes for the len |
Robert Greenwalt | 7599bfc | 2012-03-08 16:10:06 -0800 | [diff] [blame] | 104 | char buf[8]; |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 105 | // Write the code |
Robert Greenwalt | 7599bfc | 2012-03-08 16:10:06 -0800 | [diff] [blame] | 106 | snprintf(buf, 4, "%.3d", code); |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 107 | // Write the len |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 108 | uint32_t tmp = htonl(len); |
Robert Greenwalt | 7599bfc | 2012-03-08 16:10:06 -0800 | [diff] [blame] | 109 | memcpy(buf + 4, &tmp, sizeof(uint32_t)); |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 110 | |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 111 | struct iovec vec[2]; |
| 112 | vec[0].iov_base = (void *) buf; |
| 113 | vec[0].iov_len = sizeof(buf); |
| 114 | vec[1].iov_base = (void *) data; |
| 115 | vec[1].iov_len = len; |
| 116 | |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 117 | pthread_mutex_lock(&mWriteMutex); |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 118 | int result = sendDataLockedv(vec, (len > 0) ? 2 : 1); |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 119 | pthread_mutex_unlock(&mWriteMutex); |
| 120 | |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | // Sends the code (c-string null-terminated). |
| 125 | int SocketClient::sendCode(int code) { |
Robert Greenwalt | 7599bfc | 2012-03-08 16:10:06 -0800 | [diff] [blame] | 126 | char buf[4]; |
| 127 | snprintf(buf, sizeof(buf), "%.3d", code); |
| 128 | return sendData(buf, sizeof(buf)); |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Robert Greenwalt | 5949477 | 2012-04-20 15:21:07 -0700 | [diff] [blame] | 131 | char *SocketClient::quoteArg(const char *arg) { |
| 132 | int len = strlen(arg); |
| 133 | char *result = (char *)malloc(len * 2 + 3); |
| 134 | char *current = result; |
| 135 | const char *end = arg + len; |
Hong-Mei Li | 544a7f7 | 2013-04-01 11:24:44 +0800 | [diff] [blame] | 136 | char *oldresult; |
| 137 | |
Yi Kong | 4888525 | 2018-07-24 16:34:27 -0700 | [diff] [blame] | 138 | if(result == nullptr) { |
Hong-Mei Li | 544a7f7 | 2013-04-01 11:24:44 +0800 | [diff] [blame] | 139 | SLOGW("malloc error (%s)", strerror(errno)); |
Yi Kong | 4888525 | 2018-07-24 16:34:27 -0700 | [diff] [blame] | 140 | return nullptr; |
Hong-Mei Li | 544a7f7 | 2013-04-01 11:24:44 +0800 | [diff] [blame] | 141 | } |
Robert Greenwalt | 5949477 | 2012-04-20 15:21:07 -0700 | [diff] [blame] | 142 | |
| 143 | *(current++) = '"'; |
| 144 | while (arg < end) { |
| 145 | switch (*arg) { |
| 146 | case '\\': |
| 147 | case '"': |
| 148 | *(current++) = '\\'; // fallthrough |
| 149 | default: |
| 150 | *(current++) = *(arg++); |
| 151 | } |
| 152 | } |
| 153 | *(current++) = '"'; |
| 154 | *(current++) = '\0'; |
Hong-Mei Li | 544a7f7 | 2013-04-01 11:24:44 +0800 | [diff] [blame] | 155 | oldresult = result; // save pointer in case realloc fails |
Robert Greenwalt | 5949477 | 2012-04-20 15:21:07 -0700 | [diff] [blame] | 156 | result = (char *)realloc(result, current-result); |
Hong-Mei Li | 544a7f7 | 2013-04-01 11:24:44 +0800 | [diff] [blame] | 157 | return result ? result : oldresult; |
Robert Greenwalt | 5949477 | 2012-04-20 15:21:07 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
San Mehat | db01754 | 2009-05-20 15:27:14 -0700 | [diff] [blame] | 161 | int SocketClient::sendMsg(const char *msg) { |
San Mehat | c73a3a5 | 2009-06-15 14:06:03 -0700 | [diff] [blame] | 162 | // Send the message including null character |
Brad Fitzpatrick | 8c5669f | 2010-10-27 10:23:16 -0700 | [diff] [blame] | 163 | if (sendData(msg, strlen(msg) + 1) != 0) { |
| 164 | SLOGW("Unable to send msg '%s'", msg); |
| 165 | return -1; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 170 | int SocketClient::sendData(const void *data, int len) { |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 171 | struct iovec vec[1]; |
| 172 | vec[0].iov_base = (void *) data; |
| 173 | vec[0].iov_len = len; |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 174 | |
| 175 | pthread_mutex_lock(&mWriteMutex); |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 176 | int rc = sendDataLockedv(vec, 1); |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 177 | pthread_mutex_unlock(&mWriteMutex); |
| 178 | |
| 179 | return rc; |
| 180 | } |
| 181 | |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 182 | int SocketClient::sendDatav(struct iovec *iov, int iovcnt) { |
| 183 | pthread_mutex_lock(&mWriteMutex); |
| 184 | int rc = sendDataLockedv(iov, iovcnt); |
| 185 | pthread_mutex_unlock(&mWriteMutex); |
| 186 | |
| 187 | return rc; |
| 188 | } |
| 189 | |
| 190 | int SocketClient::sendDataLockedv(struct iovec *iov, int iovcnt) { |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 191 | |
Mattias Falk | 2e5fcd0 | 2011-05-13 16:25:38 +0200 | [diff] [blame] | 192 | if (mSocket < 0) { |
| 193 | errno = EHOSTUNREACH; |
| 194 | return -1; |
| 195 | } |
| 196 | |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 197 | if (iovcnt <= 0) { |
Brad Fitzpatrick | 16ae478 | 2010-11-02 10:55:52 -0700 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 201 | int ret = 0; |
| 202 | int e = 0; // SLOGW and sigaction are not inert regarding errno |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 203 | int current = 0; |
| 204 | |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 205 | struct sigaction new_action, old_action; |
| 206 | memset(&new_action, 0, sizeof(new_action)); |
| 207 | new_action.sa_handler = SIG_IGN; |
| 208 | sigaction(SIGPIPE, &new_action, &old_action); |
| 209 | |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 210 | for (;;) { |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 211 | ssize_t rc = TEMP_FAILURE_RETRY( |
| 212 | writev(mSocket, iov + current, iovcnt - current)); |
| 213 | |
David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 214 | if (rc > 0) { |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 215 | size_t written = rc; |
| 216 | while ((current < iovcnt) && (written >= iov[current].iov_len)) { |
| 217 | written -= iov[current].iov_len; |
| 218 | current++; |
| 219 | } |
| 220 | if (current == iovcnt) { |
| 221 | break; |
| 222 | } |
| 223 | iov[current].iov_base = (char *)iov[current].iov_base + written; |
| 224 | iov[current].iov_len -= written; |
David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 225 | continue; |
| 226 | } |
| 227 | |
David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 228 | if (rc == 0) { |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 229 | e = EIO; |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 230 | SLOGW("0 length write :("); |
David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 231 | } else { |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 232 | e = errno; |
| 233 | SLOGW("write error (%s)", strerror(e)); |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 234 | } |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 235 | ret = -1; |
| 236 | break; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 237 | } |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 238 | |
| 239 | sigaction(SIGPIPE, &old_action, &new_action); |
| 240 | |
Elliott Hughes | f6b62d0 | 2014-11-10 15:50:30 -0800 | [diff] [blame] | 241 | if (e != 0) { |
Bo Huang | 4df4dfe | 2014-09-29 14:54:02 +0800 | [diff] [blame] | 242 | errno = e; |
Elliott Hughes | f6b62d0 | 2014-11-10 15:50:30 -0800 | [diff] [blame] | 243 | } |
Mark Salyzyn | 4389588 | 2014-01-29 11:25:01 -0800 | [diff] [blame] | 244 | return ret; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 245 | } |
Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 246 | |
| 247 | void SocketClient::incRef() { |
Brad Fitzpatrick | 4be4e69 | 2011-03-17 17:14:46 -0700 | [diff] [blame] | 248 | pthread_mutex_lock(&mRefCountMutex); |
| 249 | mRefCount++; |
| 250 | pthread_mutex_unlock(&mRefCountMutex); |
Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Brad Fitzpatrick | 4be4e69 | 2011-03-17 17:14:46 -0700 | [diff] [blame] | 253 | bool SocketClient::decRef() { |
| 254 | bool deleteSelf = false; |
| 255 | pthread_mutex_lock(&mRefCountMutex); |
| 256 | mRefCount--; |
| 257 | if (mRefCount == 0) { |
| 258 | deleteSelf = true; |
| 259 | } else if (mRefCount < 0) { |
| 260 | SLOGE("SocketClient refcount went negative!"); |
| 261 | } |
| 262 | pthread_mutex_unlock(&mRefCountMutex); |
| 263 | if (deleteSelf) { |
| 264 | delete this; |
| 265 | } |
| 266 | return deleteSelf; |
Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 267 | } |