blob: 0361641e21ed285734bc2a1fba7671cbe4d7a73a [file] [log] [blame]
San Mehat168415b2009-05-06 11:14:21 -07001/*
2 * Copyright (C) 2008 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#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
Robert Greenwalt8702bb12012-02-07 12:23:14 -080032#define LOG_NDEBUG 0
33
San Mehatfa644ff2009-05-08 11:15:53 -070034SocketListener::SocketListener(const char *socketName, bool listen) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080035 init(socketName, -1, listen, false);
San Mehat168415b2009-05-06 11:14:21 -070036}
37
San Mehatfa644ff2009-05-08 11:15:53 -070038SocketListener::SocketListener(int socketFd, bool listen) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080039 init(NULL, socketFd, listen, false);
40}
41
42SocketListener::SocketListener(const char *socketName, bool listen, bool useCmdNum) {
43 init(socketName, -1, listen, useCmdNum);
44}
45
46void SocketListener::init(const char *socketName, int socketFd, bool listen, bool useCmdNum) {
San Mehatfa644ff2009-05-08 11:15:53 -070047 mListen = listen;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080048 mSocketName = socketName;
San Mehat168415b2009-05-06 11:14:21 -070049 mSock = socketFd;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080050 mUseCmdNum = useCmdNum;
San Mehatfa644ff2009-05-08 11:15:53 -070051 pthread_mutex_init(&mClientsLock, NULL);
52 mClients = new SocketClientCollection();
San Mehat168415b2009-05-06 11:14:21 -070053}
54
San Mehatc4a895b2009-06-23 21:10:57 -070055SocketListener::~SocketListener() {
56 if (mSocketName && mSock > -1)
57 close(mSock);
58
59 if (mCtrlPipe[0] != -1) {
60 close(mCtrlPipe[0]);
61 close(mCtrlPipe[1]);
62 }
63 SocketClientCollection::iterator it;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +010064 for (it = mClients->begin(); it != mClients->end();) {
Brad Fitzpatrick13aa8ad2011-03-17 15:41:20 -070065 (*it)->decRef();
San Mehatc4a895b2009-06-23 21:10:57 -070066 it = mClients->erase(it);
67 }
68 delete mClients;
69}
70
San Mehatfa644ff2009-05-08 11:15:53 -070071int SocketListener::startListener() {
San Mehat168415b2009-05-06 11:14:21 -070072
73 if (!mSocketName && mSock == -1) {
San Mehat7e8529a2010-03-25 09:31:42 -070074 SLOGE("Failed to start unbound listener");
San Mehat168415b2009-05-06 11:14:21 -070075 errno = EINVAL;
76 return -1;
77 } else if (mSocketName) {
78 if ((mSock = android_get_control_socket(mSocketName)) < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070079 SLOGE("Obtaining file descriptor socket '%s' failed: %s",
San Mehat168415b2009-05-06 11:14:21 -070080 mSocketName, strerror(errno));
81 return -1;
82 }
Robert Greenwalt8702bb12012-02-07 12:23:14 -080083 SLOGV("got mSock = %d for %s", mSock, mSocketName);
San Mehat168415b2009-05-06 11:14:21 -070084 }
85
San Mehatfa644ff2009-05-08 11:15:53 -070086 if (mListen && listen(mSock, 4) < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070087 SLOGE("Unable to listen on socket (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -070088 return -1;
San Mehatdf6c1b92009-05-13 08:58:43 -070089 } else if (!mListen)
Robert Greenwalt8702bb12012-02-07 12:23:14 -080090 mClients->push_back(new SocketClient(mSock, false, mUseCmdNum));
San Mehat168415b2009-05-06 11:14:21 -070091
San Mehatc4a895b2009-06-23 21:10:57 -070092 if (pipe(mCtrlPipe)) {
San Mehat7e8529a2010-03-25 09:31:42 -070093 SLOGE("pipe failed (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -070094 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -070095 }
San Mehatfa644ff2009-05-08 11:15:53 -070096
San Mehatc4a895b2009-06-23 21:10:57 -070097 if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) {
San Mehat7e8529a2010-03-25 09:31:42 -070098 SLOGE("pthread_create (%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
102 return 0;
103}
104
105int SocketListener::stopListener() {
106 char c = 0;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100107 int rc;
San Mehatfa644ff2009-05-08 11:15:53 -0700108
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100109 rc = TEMP_FAILURE_RETRY(write(mCtrlPipe[1], &c, 1));
110 if (rc != 1) {
San Mehat7e8529a2010-03-25 09:31:42 -0700111 SLOGE("Error writing to control pipe (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700112 return -1;
113 }
114
San Mehatfa644ff2009-05-08 11:15:53 -0700115 void *ret;
116 if (pthread_join(mThread, &ret)) {
San Mehat7e8529a2010-03-25 09:31:42 -0700117 SLOGE("Error joining to listener thread (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700118 return -1;
119 }
San Mehatdbdb0db2009-05-12 15:50:26 -0700120 close(mCtrlPipe[0]);
121 close(mCtrlPipe[1]);
San Mehatc4a895b2009-06-23 21:10:57 -0700122 mCtrlPipe[0] = -1;
123 mCtrlPipe[1] = -1;
124
125 if (mSocketName && mSock > -1) {
126 close(mSock);
127 mSock = -1;
128 }
129
130 SocketClientCollection::iterator it;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100131 for (it = mClients->begin(); it != mClients->end();) {
San Mehatc4a895b2009-06-23 21:10:57 -0700132 delete (*it);
133 it = mClients->erase(it);
134 }
San Mehatfa644ff2009-05-08 11:15:53 -0700135 return 0;
136}
137
138void *SocketListener::threadStart(void *obj) {
139 SocketListener *me = reinterpret_cast<SocketListener *>(obj);
140
141 me->runListener();
San Mehatfa644ff2009-05-08 11:15:53 -0700142 pthread_exit(NULL);
143 return NULL;
144}
145
146void SocketListener::runListener() {
147
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100148 SocketClientCollection *pendingList = new SocketClientCollection();
149
San Mehat168415b2009-05-06 11:14:21 -0700150 while(1) {
San Mehatfa644ff2009-05-08 11:15:53 -0700151 SocketClientCollection::iterator it;
San Mehat168415b2009-05-06 11:14:21 -0700152 fd_set read_fds;
San Mehat168415b2009-05-06 11:14:21 -0700153 int rc = 0;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100154 int max = -1;
San Mehatfa644ff2009-05-08 11:15:53 -0700155
San Mehat168415b2009-05-06 11:14:21 -0700156 FD_ZERO(&read_fds);
157
San Mehatfa644ff2009-05-08 11:15:53 -0700158 if (mListen) {
San Mehat168415b2009-05-06 11:14:21 -0700159 max = mSock;
San Mehatfa644ff2009-05-08 11:15:53 -0700160 FD_SET(mSock, &read_fds);
San Mehat168415b2009-05-06 11:14:21 -0700161 }
162
San Mehatfa644ff2009-05-08 11:15:53 -0700163 FD_SET(mCtrlPipe[0], &read_fds);
164 if (mCtrlPipe[0] > max)
165 max = mCtrlPipe[0];
166
167 pthread_mutex_lock(&mClientsLock);
168 for (it = mClients->begin(); it != mClients->end(); ++it) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100169 int fd = (*it)->getSocket();
170 FD_SET(fd, &read_fds);
171 if (fd > max)
172 max = fd;
San Mehatfa644ff2009-05-08 11:15:53 -0700173 }
174 pthread_mutex_unlock(&mClientsLock);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800175 SLOGV("mListen=%d, max=%d, mSocketName=%s", mListen, max, mSocketName);
San Mehatdf6c1b92009-05-13 08:58:43 -0700176 if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100177 if (errno == EINTR)
178 continue;
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800179 SLOGE("select failed (%s) mListen=%d, max=%d", strerror(errno), mListen, max);
San Mehatfa644ff2009-05-08 11:15:53 -0700180 sleep(1);
San Mehat168415b2009-05-06 11:14:21 -0700181 continue;
San Mehatdf6c1b92009-05-13 08:58:43 -0700182 } else if (!rc)
San Mehatfa644ff2009-05-08 11:15:53 -0700183 continue;
San Mehat168415b2009-05-06 11:14:21 -0700184
San Mehatdbdb0db2009-05-12 15:50:26 -0700185 if (FD_ISSET(mCtrlPipe[0], &read_fds))
San Mehatfa644ff2009-05-08 11:15:53 -0700186 break;
San Mehatfa644ff2009-05-08 11:15:53 -0700187 if (mListen && FD_ISSET(mSock, &read_fds)) {
188 struct sockaddr addr;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100189 socklen_t alen;
San Mehatfa644ff2009-05-08 11:15:53 -0700190 int c;
191
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100192 do {
193 alen = sizeof(addr);
194 c = accept(mSock, &addr, &alen);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800195 SLOGV("%s got %d from accept", mSocketName, c);
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100196 } while (c < 0 && errno == EINTR);
197 if (c < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -0700198 SLOGE("accept failed (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700199 sleep(1);
200 continue;
201 }
San Mehatfa644ff2009-05-08 11:15:53 -0700202 pthread_mutex_lock(&mClientsLock);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800203 mClients->push_back(new SocketClient(c, true, mUseCmdNum));
San Mehatfa644ff2009-05-08 11:15:53 -0700204 pthread_mutex_unlock(&mClientsLock);
205 }
206
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100207 /* Add all active clients to the pending list first */
208 pendingList->clear();
209 pthread_mutex_lock(&mClientsLock);
210 for (it = mClients->begin(); it != mClients->end(); ++it) {
211 int fd = (*it)->getSocket();
212 if (FD_ISSET(fd, &read_fds)) {
213 pendingList->push_back(*it);
San Mehat168415b2009-05-06 11:14:21 -0700214 }
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100215 }
216 pthread_mutex_unlock(&mClientsLock);
217
218 /* Process the pending list, since it is owned by the thread,
219 * there is no need to lock it */
220 while (!pendingList->empty()) {
221 /* Pop the first item from the list */
222 it = pendingList->begin();
223 SocketClient* c = *it;
224 pendingList->erase(it);
Vernon Tang87950072011-04-27 14:01:27 +1000225 /* Process it, if false is returned and our sockets are
226 * connection-based, remove and destroy it */
227 if (!onDataAvailable(c) && mListen) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100228 /* Remove the client from our array */
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800229 SLOGV("going to zap %d for %s", c->getSocket(), mSocketName);
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100230 pthread_mutex_lock(&mClientsLock);
231 for (it = mClients->begin(); it != mClients->end(); ++it) {
232 if (*it == c) {
233 mClients->erase(it);
234 break;
235 }
236 }
237 pthread_mutex_unlock(&mClientsLock);
Xianzhu Wang45202462011-09-29 12:59:55 +0800238 /* Remove our reference to the client */
239 c->decRef();
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100240 }
241 }
San Mehat168415b2009-05-06 11:14:21 -0700242 }
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100243 delete pendingList;
San Mehat168415b2009-05-06 11:14:21 -0700244}
245
San Mehatdb017542009-05-20 15:27:14 -0700246void SocketListener::sendBroadcast(int code, const char *msg, bool addErrno) {
San Mehatd7680662009-05-12 11:16:59 -0700247 pthread_mutex_lock(&mClientsLock);
248 SocketClientCollection::iterator i;
249
250 for (i = mClients->begin(); i != mClients->end(); ++i) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800251 // broadcasts are unsolicited and should not include a cmd number
252 if ((*i)->sendMsg(code, msg, addErrno, false)) {
Guang Zhua8185a62012-02-07 19:13:28 -0800253 SLOGW("Error sending broadcast (%s)", strerror(errno));
254 }
255 }
256 pthread_mutex_unlock(&mClientsLock);
257}