blob: 608abaea51f8ea20c4242038e7f3b4d0ae8bba68 [file] [log] [blame]
San Mehat168415b2009-05-06 11:14:21 -07001/*
Mark Salyzyn23f04102012-01-24 20:30:10 -08002 * Copyright (C) 2008-2014 The Android Open Source Project
San Mehat168415b2009-05-06 11:14:21 -07003 *
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#include <stdio.h>
17#include <errno.h>
18#include <stdlib.h>
19#include <sys/socket.h>
20#include <sys/select.h>
21#include <sys/time.h>
22#include <sys/types.h>
23#include <sys/un.h>
24
25#define LOG_TAG "SocketListener"
26#include <cutils/log.h>
San Mehat168415b2009-05-06 11:14:21 -070027#include <cutils/sockets.h>
28
29#include <sysutils/SocketListener.h>
San Mehatfa644ff2009-05-08 11:15:53 -070030#include <sysutils/SocketClient.h>
San Mehat168415b2009-05-06 11:14:21 -070031
Mark Salyzyn44b99c22014-01-08 12:44:23 -080032#define CtrlPipe_Shutdown 0
33#define CtrlPipe_Wakeup 1
34
San Mehatfa644ff2009-05-08 11:15:53 -070035SocketListener::SocketListener(const char *socketName, bool listen) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080036 init(socketName, -1, listen, false);
San Mehat168415b2009-05-06 11:14:21 -070037}
38
San Mehatfa644ff2009-05-08 11:15:53 -070039SocketListener::SocketListener(int socketFd, bool listen) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080040 init(NULL, socketFd, listen, false);
41}
42
43SocketListener::SocketListener(const char *socketName, bool listen, bool useCmdNum) {
44 init(socketName, -1, listen, useCmdNum);
45}
46
47void SocketListener::init(const char *socketName, int socketFd, bool listen, bool useCmdNum) {
San Mehatfa644ff2009-05-08 11:15:53 -070048 mListen = listen;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080049 mSocketName = socketName;
San Mehat168415b2009-05-06 11:14:21 -070050 mSock = socketFd;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080051 mUseCmdNum = useCmdNum;
San Mehatfa644ff2009-05-08 11:15:53 -070052 pthread_mutex_init(&mClientsLock, NULL);
53 mClients = new SocketClientCollection();
San Mehat168415b2009-05-06 11:14:21 -070054}
55
San Mehatc4a895b2009-06-23 21:10:57 -070056SocketListener::~SocketListener() {
57 if (mSocketName && mSock > -1)
58 close(mSock);
59
60 if (mCtrlPipe[0] != -1) {
61 close(mCtrlPipe[0]);
62 close(mCtrlPipe[1]);
63 }
64 SocketClientCollection::iterator it;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +010065 for (it = mClients->begin(); it != mClients->end();) {
Brad Fitzpatrick13aa8ad2011-03-17 15:41:20 -070066 (*it)->decRef();
San Mehatc4a895b2009-06-23 21:10:57 -070067 it = mClients->erase(it);
68 }
69 delete mClients;
70}
71
San Mehatfa644ff2009-05-08 11:15:53 -070072int SocketListener::startListener() {
Mark Salyzyn581edc12013-11-20 13:38:52 -080073 return startListener(4);
74}
75
76int SocketListener::startListener(int backlog) {
San Mehat168415b2009-05-06 11:14:21 -070077
78 if (!mSocketName && mSock == -1) {
San Mehat7e8529a2010-03-25 09:31:42 -070079 SLOGE("Failed to start unbound listener");
San Mehat168415b2009-05-06 11:14:21 -070080 errno = EINVAL;
81 return -1;
82 } else if (mSocketName) {
83 if ((mSock = android_get_control_socket(mSocketName)) < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070084 SLOGE("Obtaining file descriptor socket '%s' failed: %s",
San Mehat168415b2009-05-06 11:14:21 -070085 mSocketName, strerror(errno));
86 return -1;
87 }
Robert Greenwalt8702bb12012-02-07 12:23:14 -080088 SLOGV("got mSock = %d for %s", mSock, mSocketName);
Jeff Sharkey55fd3162015-04-01 22:31:40 -070089 fcntl(mSock, F_SETFD, FD_CLOEXEC);
San Mehat168415b2009-05-06 11:14:21 -070090 }
91
Mark Salyzyn581edc12013-11-20 13:38:52 -080092 if (mListen && listen(mSock, backlog) < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070093 SLOGE("Unable to listen on socket (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -070094 return -1;
San Mehatdf6c1b92009-05-13 08:58:43 -070095 } else if (!mListen)
Robert Greenwalt8702bb12012-02-07 12:23:14 -080096 mClients->push_back(new SocketClient(mSock, false, mUseCmdNum));
San Mehat168415b2009-05-06 11:14:21 -070097
San Mehatc4a895b2009-06-23 21:10:57 -070098 if (pipe(mCtrlPipe)) {
San Mehat7e8529a2010-03-25 09:31:42 -070099 SLOGE("pipe failed (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700100 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -0700101 }
San Mehatfa644ff2009-05-08 11:15:53 -0700102
San Mehatc4a895b2009-06-23 21:10:57 -0700103 if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) {
San Mehat7e8529a2010-03-25 09:31:42 -0700104 SLOGE("pthread_create (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700105 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -0700106 }
San Mehatfa644ff2009-05-08 11:15:53 -0700107
108 return 0;
109}
110
111int SocketListener::stopListener() {
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800112 char c = CtrlPipe_Shutdown;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100113 int rc;
San Mehatfa644ff2009-05-08 11:15:53 -0700114
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100115 rc = TEMP_FAILURE_RETRY(write(mCtrlPipe[1], &c, 1));
116 if (rc != 1) {
San Mehat7e8529a2010-03-25 09:31:42 -0700117 SLOGE("Error writing to control pipe (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700118 return -1;
119 }
120
San Mehatfa644ff2009-05-08 11:15:53 -0700121 void *ret;
122 if (pthread_join(mThread, &ret)) {
San Mehat7e8529a2010-03-25 09:31:42 -0700123 SLOGE("Error joining to listener thread (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700124 return -1;
125 }
San Mehatdbdb0db2009-05-12 15:50:26 -0700126 close(mCtrlPipe[0]);
127 close(mCtrlPipe[1]);
San Mehatc4a895b2009-06-23 21:10:57 -0700128 mCtrlPipe[0] = -1;
129 mCtrlPipe[1] = -1;
130
131 if (mSocketName && mSock > -1) {
132 close(mSock);
133 mSock = -1;
134 }
135
136 SocketClientCollection::iterator it;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100137 for (it = mClients->begin(); it != mClients->end();) {
San Mehatc4a895b2009-06-23 21:10:57 -0700138 delete (*it);
139 it = mClients->erase(it);
140 }
San Mehatfa644ff2009-05-08 11:15:53 -0700141 return 0;
142}
143
144void *SocketListener::threadStart(void *obj) {
145 SocketListener *me = reinterpret_cast<SocketListener *>(obj);
146
147 me->runListener();
San Mehatfa644ff2009-05-08 11:15:53 -0700148 pthread_exit(NULL);
149 return NULL;
150}
151
152void SocketListener::runListener() {
153
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800154 SocketClientCollection pendingList;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100155
San Mehat168415b2009-05-06 11:14:21 -0700156 while(1) {
San Mehatfa644ff2009-05-08 11:15:53 -0700157 SocketClientCollection::iterator it;
San Mehat168415b2009-05-06 11:14:21 -0700158 fd_set read_fds;
San Mehat168415b2009-05-06 11:14:21 -0700159 int rc = 0;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100160 int max = -1;
San Mehatfa644ff2009-05-08 11:15:53 -0700161
San Mehat168415b2009-05-06 11:14:21 -0700162 FD_ZERO(&read_fds);
163
San Mehatfa644ff2009-05-08 11:15:53 -0700164 if (mListen) {
San Mehat168415b2009-05-06 11:14:21 -0700165 max = mSock;
San Mehatfa644ff2009-05-08 11:15:53 -0700166 FD_SET(mSock, &read_fds);
San Mehat168415b2009-05-06 11:14:21 -0700167 }
168
San Mehatfa644ff2009-05-08 11:15:53 -0700169 FD_SET(mCtrlPipe[0], &read_fds);
170 if (mCtrlPipe[0] > max)
171 max = mCtrlPipe[0];
172
173 pthread_mutex_lock(&mClientsLock);
174 for (it = mClients->begin(); it != mClients->end(); ++it) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800175 // NB: calling out to an other object with mClientsLock held (safe)
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100176 int fd = (*it)->getSocket();
177 FD_SET(fd, &read_fds);
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800178 if (fd > max) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100179 max = fd;
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800180 }
San Mehatfa644ff2009-05-08 11:15:53 -0700181 }
182 pthread_mutex_unlock(&mClientsLock);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800183 SLOGV("mListen=%d, max=%d, mSocketName=%s", mListen, max, mSocketName);
San Mehatdf6c1b92009-05-13 08:58:43 -0700184 if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100185 if (errno == EINTR)
186 continue;
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800187 SLOGE("select failed (%s) mListen=%d, max=%d", strerror(errno), mListen, max);
San Mehatfa644ff2009-05-08 11:15:53 -0700188 sleep(1);
San Mehat168415b2009-05-06 11:14:21 -0700189 continue;
San Mehatdf6c1b92009-05-13 08:58:43 -0700190 } else if (!rc)
San Mehatfa644ff2009-05-08 11:15:53 -0700191 continue;
San Mehat168415b2009-05-06 11:14:21 -0700192
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800193 if (FD_ISSET(mCtrlPipe[0], &read_fds)) {
194 char c = CtrlPipe_Shutdown;
195 TEMP_FAILURE_RETRY(read(mCtrlPipe[0], &c, 1));
196 if (c == CtrlPipe_Shutdown) {
197 break;
198 }
199 continue;
200 }
San Mehatfa644ff2009-05-08 11:15:53 -0700201 if (mListen && FD_ISSET(mSock, &read_fds)) {
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700202 int c = TEMP_FAILURE_RETRY(accept4(mSock, nullptr, nullptr, SOCK_CLOEXEC));
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100203 if (c < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -0700204 SLOGE("accept failed (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700205 sleep(1);
206 continue;
207 }
San Mehatfa644ff2009-05-08 11:15:53 -0700208 pthread_mutex_lock(&mClientsLock);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800209 mClients->push_back(new SocketClient(c, true, mUseCmdNum));
San Mehatfa644ff2009-05-08 11:15:53 -0700210 pthread_mutex_unlock(&mClientsLock);
211 }
212
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100213 /* Add all active clients to the pending list first */
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800214 pendingList.clear();
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100215 pthread_mutex_lock(&mClientsLock);
216 for (it = mClients->begin(); it != mClients->end(); ++it) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800217 SocketClient* c = *it;
218 // NB: calling out to an other object with mClientsLock held (safe)
219 int fd = c->getSocket();
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100220 if (FD_ISSET(fd, &read_fds)) {
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800221 pendingList.push_back(c);
Mark Salyzyn23f04102012-01-24 20:30:10 -0800222 c->incRef();
San Mehat168415b2009-05-06 11:14:21 -0700223 }
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100224 }
225 pthread_mutex_unlock(&mClientsLock);
226
227 /* Process the pending list, since it is owned by the thread,
228 * there is no need to lock it */
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800229 while (!pendingList.empty()) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100230 /* Pop the first item from the list */
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800231 it = pendingList.begin();
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100232 SocketClient* c = *it;
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800233 pendingList.erase(it);
234 /* Process it, if false is returned, remove from list */
235 if (!onDataAvailable(c)) {
236 release(c, false);
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100237 }
Mark Salyzyn23f04102012-01-24 20:30:10 -0800238 c->decRef();
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100239 }
San Mehat168415b2009-05-06 11:14:21 -0700240 }
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800241}
242
243bool SocketListener::release(SocketClient* c, bool wakeup) {
244 bool ret = false;
245 /* if our sockets are connection-based, remove and destroy it */
246 if (mListen && c) {
247 /* Remove the client from our array */
248 SLOGV("going to zap %d for %s", c->getSocket(), mSocketName);
249 pthread_mutex_lock(&mClientsLock);
250 SocketClientCollection::iterator it;
251 for (it = mClients->begin(); it != mClients->end(); ++it) {
252 if (*it == c) {
253 mClients->erase(it);
254 ret = true;
255 break;
256 }
257 }
258 pthread_mutex_unlock(&mClientsLock);
259 if (ret) {
260 ret = c->decRef();
261 if (wakeup) {
262 char b = CtrlPipe_Wakeup;
263 TEMP_FAILURE_RETRY(write(mCtrlPipe[1], &b, 1));
264 }
265 }
266 }
267 return ret;
San Mehat168415b2009-05-06 11:14:21 -0700268}
269
San Mehatdb017542009-05-20 15:27:14 -0700270void SocketListener::sendBroadcast(int code, const char *msg, bool addErrno) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800271 SocketClientCollection safeList;
272
273 /* Add all active clients to the safe list first */
274 safeList.clear();
San Mehatd7680662009-05-12 11:16:59 -0700275 pthread_mutex_lock(&mClientsLock);
276 SocketClientCollection::iterator i;
277
278 for (i = mClients->begin(); i != mClients->end(); ++i) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800279 SocketClient* c = *i;
280 c->incRef();
281 safeList.push_back(c);
Guang Zhua8185a62012-02-07 19:13:28 -0800282 }
283 pthread_mutex_unlock(&mClientsLock);
Mark Salyzyn23f04102012-01-24 20:30:10 -0800284
285 while (!safeList.empty()) {
286 /* Pop the first item from the list */
287 i = safeList.begin();
288 SocketClient* c = *i;
289 safeList.erase(i);
290 // broadcasts are unsolicited and should not include a cmd number
291 if (c->sendMsg(code, msg, addErrno, false)) {
292 SLOGW("Error sending broadcast (%s)", strerror(errno));
293 }
294 c->decRef();
295 }
296}
297
298void SocketListener::runOnEachSocket(SocketClientCommand *command) {
299 SocketClientCollection safeList;
300
301 /* Add all active clients to the safe list first */
302 safeList.clear();
303 pthread_mutex_lock(&mClientsLock);
304 SocketClientCollection::iterator i;
305
306 for (i = mClients->begin(); i != mClients->end(); ++i) {
307 SocketClient* c = *i;
308 c->incRef();
309 safeList.push_back(c);
310 }
311 pthread_mutex_unlock(&mClientsLock);
312
313 while (!safeList.empty()) {
314 /* Pop the first item from the list */
315 i = safeList.begin();
316 SocketClient* c = *i;
317 safeList.erase(i);
318 command->runSocketCommand(c);
319 c->decRef();
320 }
Guang Zhua8185a62012-02-07 19:13:28 -0800321}