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