blob: 527a6a0a3c7887a917f8a9a32c9f90af58fa4566 [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);
San Mehat168415b2009-05-06 11:14:21 -070089 }
90
Mark Salyzyn581edc12013-11-20 13:38:52 -080091 if (mListen && listen(mSock, backlog) < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070092 SLOGE("Unable to listen on socket (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -070093 return -1;
San Mehatdf6c1b92009-05-13 08:58:43 -070094 } else if (!mListen)
Robert Greenwalt8702bb12012-02-07 12:23:14 -080095 mClients->push_back(new SocketClient(mSock, false, mUseCmdNum));
San Mehat168415b2009-05-06 11:14:21 -070096
San Mehatc4a895b2009-06-23 21:10:57 -070097 if (pipe(mCtrlPipe)) {
San Mehat7e8529a2010-03-25 09:31:42 -070098 SLOGE("pipe failed (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -070099 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -0700100 }
San Mehatfa644ff2009-05-08 11:15:53 -0700101
San Mehatc4a895b2009-06-23 21:10:57 -0700102 if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) {
San Mehat7e8529a2010-03-25 09:31:42 -0700103 SLOGE("pthread_create (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700104 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -0700105 }
San Mehatfa644ff2009-05-08 11:15:53 -0700106
107 return 0;
108}
109
110int SocketListener::stopListener() {
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800111 char c = CtrlPipe_Shutdown;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100112 int rc;
San Mehatfa644ff2009-05-08 11:15:53 -0700113
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100114 rc = TEMP_FAILURE_RETRY(write(mCtrlPipe[1], &c, 1));
115 if (rc != 1) {
San Mehat7e8529a2010-03-25 09:31:42 -0700116 SLOGE("Error writing to control pipe (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700117 return -1;
118 }
119
San Mehatfa644ff2009-05-08 11:15:53 -0700120 void *ret;
121 if (pthread_join(mThread, &ret)) {
San Mehat7e8529a2010-03-25 09:31:42 -0700122 SLOGE("Error joining to listener thread (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700123 return -1;
124 }
San Mehatdbdb0db2009-05-12 15:50:26 -0700125 close(mCtrlPipe[0]);
126 close(mCtrlPipe[1]);
San Mehatc4a895b2009-06-23 21:10:57 -0700127 mCtrlPipe[0] = -1;
128 mCtrlPipe[1] = -1;
129
130 if (mSocketName && mSock > -1) {
131 close(mSock);
132 mSock = -1;
133 }
134
135 SocketClientCollection::iterator it;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100136 for (it = mClients->begin(); it != mClients->end();) {
San Mehatc4a895b2009-06-23 21:10:57 -0700137 delete (*it);
138 it = mClients->erase(it);
139 }
San Mehatfa644ff2009-05-08 11:15:53 -0700140 return 0;
141}
142
143void *SocketListener::threadStart(void *obj) {
144 SocketListener *me = reinterpret_cast<SocketListener *>(obj);
145
146 me->runListener();
San Mehatfa644ff2009-05-08 11:15:53 -0700147 pthread_exit(NULL);
148 return NULL;
149}
150
151void SocketListener::runListener() {
152
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800153 SocketClientCollection pendingList;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100154
San Mehat168415b2009-05-06 11:14:21 -0700155 while(1) {
San Mehatfa644ff2009-05-08 11:15:53 -0700156 SocketClientCollection::iterator it;
San Mehat168415b2009-05-06 11:14:21 -0700157 fd_set read_fds;
San Mehat168415b2009-05-06 11:14:21 -0700158 int rc = 0;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100159 int max = -1;
San Mehatfa644ff2009-05-08 11:15:53 -0700160
San Mehat168415b2009-05-06 11:14:21 -0700161 FD_ZERO(&read_fds);
162
San Mehatfa644ff2009-05-08 11:15:53 -0700163 if (mListen) {
San Mehat168415b2009-05-06 11:14:21 -0700164 max = mSock;
San Mehatfa644ff2009-05-08 11:15:53 -0700165 FD_SET(mSock, &read_fds);
San Mehat168415b2009-05-06 11:14:21 -0700166 }
167
San Mehatfa644ff2009-05-08 11:15:53 -0700168 FD_SET(mCtrlPipe[0], &read_fds);
169 if (mCtrlPipe[0] > max)
170 max = mCtrlPipe[0];
171
172 pthread_mutex_lock(&mClientsLock);
173 for (it = mClients->begin(); it != mClients->end(); ++it) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800174 // NB: calling out to an other object with mClientsLock held (safe)
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100175 int fd = (*it)->getSocket();
176 FD_SET(fd, &read_fds);
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800177 if (fd > max) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100178 max = fd;
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800179 }
San Mehatfa644ff2009-05-08 11:15:53 -0700180 }
181 pthread_mutex_unlock(&mClientsLock);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800182 SLOGV("mListen=%d, max=%d, mSocketName=%s", mListen, max, mSocketName);
San Mehatdf6c1b92009-05-13 08:58:43 -0700183 if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100184 if (errno == EINTR)
185 continue;
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800186 SLOGE("select failed (%s) mListen=%d, max=%d", strerror(errno), mListen, max);
San Mehatfa644ff2009-05-08 11:15:53 -0700187 sleep(1);
San Mehat168415b2009-05-06 11:14:21 -0700188 continue;
San Mehatdf6c1b92009-05-13 08:58:43 -0700189 } else if (!rc)
San Mehatfa644ff2009-05-08 11:15:53 -0700190 continue;
San Mehat168415b2009-05-06 11:14:21 -0700191
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800192 if (FD_ISSET(mCtrlPipe[0], &read_fds)) {
193 char c = CtrlPipe_Shutdown;
194 TEMP_FAILURE_RETRY(read(mCtrlPipe[0], &c, 1));
195 if (c == CtrlPipe_Shutdown) {
196 break;
197 }
198 continue;
199 }
San Mehatfa644ff2009-05-08 11:15:53 -0700200 if (mListen && FD_ISSET(mSock, &read_fds)) {
201 struct sockaddr addr;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100202 socklen_t alen;
San Mehatfa644ff2009-05-08 11:15:53 -0700203 int c;
204
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100205 do {
206 alen = sizeof(addr);
207 c = accept(mSock, &addr, &alen);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800208 SLOGV("%s got %d from accept", mSocketName, c);
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100209 } while (c < 0 && errno == EINTR);
210 if (c < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -0700211 SLOGE("accept failed (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700212 sleep(1);
213 continue;
214 }
San Mehatfa644ff2009-05-08 11:15:53 -0700215 pthread_mutex_lock(&mClientsLock);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800216 mClients->push_back(new SocketClient(c, true, mUseCmdNum));
San Mehatfa644ff2009-05-08 11:15:53 -0700217 pthread_mutex_unlock(&mClientsLock);
218 }
219
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100220 /* Add all active clients to the pending list first */
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800221 pendingList.clear();
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100222 pthread_mutex_lock(&mClientsLock);
223 for (it = mClients->begin(); it != mClients->end(); ++it) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800224 SocketClient* c = *it;
225 // NB: calling out to an other object with mClientsLock held (safe)
226 int fd = c->getSocket();
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100227 if (FD_ISSET(fd, &read_fds)) {
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800228 pendingList.push_back(c);
Mark Salyzyn23f04102012-01-24 20:30:10 -0800229 c->incRef();
San Mehat168415b2009-05-06 11:14:21 -0700230 }
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100231 }
232 pthread_mutex_unlock(&mClientsLock);
233
234 /* Process the pending list, since it is owned by the thread,
235 * there is no need to lock it */
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800236 while (!pendingList.empty()) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100237 /* Pop the first item from the list */
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800238 it = pendingList.begin();
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100239 SocketClient* c = *it;
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800240 pendingList.erase(it);
241 /* Process it, if false is returned, remove from list */
242 if (!onDataAvailable(c)) {
243 release(c, false);
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100244 }
Mark Salyzyn23f04102012-01-24 20:30:10 -0800245 c->decRef();
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100246 }
San Mehat168415b2009-05-06 11:14:21 -0700247 }
Mark Salyzyn44b99c22014-01-08 12:44:23 -0800248}
249
250bool SocketListener::release(SocketClient* c, bool wakeup) {
251 bool ret = false;
252 /* if our sockets are connection-based, remove and destroy it */
253 if (mListen && c) {
254 /* Remove the client from our array */
255 SLOGV("going to zap %d for %s", c->getSocket(), mSocketName);
256 pthread_mutex_lock(&mClientsLock);
257 SocketClientCollection::iterator it;
258 for (it = mClients->begin(); it != mClients->end(); ++it) {
259 if (*it == c) {
260 mClients->erase(it);
261 ret = true;
262 break;
263 }
264 }
265 pthread_mutex_unlock(&mClientsLock);
266 if (ret) {
267 ret = c->decRef();
268 if (wakeup) {
269 char b = CtrlPipe_Wakeup;
270 TEMP_FAILURE_RETRY(write(mCtrlPipe[1], &b, 1));
271 }
272 }
273 }
274 return ret;
San Mehat168415b2009-05-06 11:14:21 -0700275}
276
San Mehatdb017542009-05-20 15:27:14 -0700277void SocketListener::sendBroadcast(int code, const char *msg, bool addErrno) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800278 SocketClientCollection safeList;
279
280 /* Add all active clients to the safe list first */
281 safeList.clear();
San Mehatd7680662009-05-12 11:16:59 -0700282 pthread_mutex_lock(&mClientsLock);
283 SocketClientCollection::iterator i;
284
285 for (i = mClients->begin(); i != mClients->end(); ++i) {
Mark Salyzyn23f04102012-01-24 20:30:10 -0800286 SocketClient* c = *i;
287 c->incRef();
288 safeList.push_back(c);
Guang Zhua8185a62012-02-07 19:13:28 -0800289 }
290 pthread_mutex_unlock(&mClientsLock);
Mark Salyzyn23f04102012-01-24 20:30:10 -0800291
292 while (!safeList.empty()) {
293 /* Pop the first item from the list */
294 i = safeList.begin();
295 SocketClient* c = *i;
296 safeList.erase(i);
297 // broadcasts are unsolicited and should not include a cmd number
298 if (c->sendMsg(code, msg, addErrno, false)) {
299 SLOGW("Error sending broadcast (%s)", strerror(errno));
300 }
301 c->decRef();
302 }
303}
304
305void SocketListener::runOnEachSocket(SocketClientCommand *command) {
306 SocketClientCollection safeList;
307
308 /* Add all active clients to the safe list first */
309 safeList.clear();
310 pthread_mutex_lock(&mClientsLock);
311 SocketClientCollection::iterator i;
312
313 for (i = mClients->begin(); i != mClients->end(); ++i) {
314 SocketClient* c = *i;
315 c->incRef();
316 safeList.push_back(c);
317 }
318 pthread_mutex_unlock(&mClientsLock);
319
320 while (!safeList.empty()) {
321 /* Pop the first item from the list */
322 i = safeList.begin();
323 SocketClient* c = *i;
324 safeList.erase(i);
325 command->runSocketCommand(c);
326 c->decRef();
327 }
Guang Zhua8185a62012-02-07 19:13:28 -0800328}