blob: 0296910ce2fdda51111796ff31f7f32eac3aa3fc [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
San Mehatfa644ff2009-05-08 11:15:53 -070032SocketListener::SocketListener(const char *socketName, bool listen) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080033 init(socketName, -1, listen, false);
San Mehat168415b2009-05-06 11:14:21 -070034}
35
San Mehatfa644ff2009-05-08 11:15:53 -070036SocketListener::SocketListener(int socketFd, bool listen) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -080037 init(NULL, socketFd, listen, false);
38}
39
40SocketListener::SocketListener(const char *socketName, bool listen, bool useCmdNum) {
41 init(socketName, -1, listen, useCmdNum);
42}
43
44void SocketListener::init(const char *socketName, int socketFd, bool listen, bool useCmdNum) {
San Mehatfa644ff2009-05-08 11:15:53 -070045 mListen = listen;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080046 mSocketName = socketName;
San Mehat168415b2009-05-06 11:14:21 -070047 mSock = socketFd;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080048 mUseCmdNum = useCmdNum;
San Mehatfa644ff2009-05-08 11:15:53 -070049 pthread_mutex_init(&mClientsLock, NULL);
50 mClients = new SocketClientCollection();
San Mehat168415b2009-05-06 11:14:21 -070051}
52
San Mehatc4a895b2009-06-23 21:10:57 -070053SocketListener::~SocketListener() {
54 if (mSocketName && mSock > -1)
55 close(mSock);
56
57 if (mCtrlPipe[0] != -1) {
58 close(mCtrlPipe[0]);
59 close(mCtrlPipe[1]);
60 }
61 SocketClientCollection::iterator it;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +010062 for (it = mClients->begin(); it != mClients->end();) {
Brad Fitzpatrick13aa8ad2011-03-17 15:41:20 -070063 (*it)->decRef();
San Mehatc4a895b2009-06-23 21:10:57 -070064 it = mClients->erase(it);
65 }
66 delete mClients;
67}
68
San Mehatfa644ff2009-05-08 11:15:53 -070069int SocketListener::startListener() {
San Mehat168415b2009-05-06 11:14:21 -070070
71 if (!mSocketName && mSock == -1) {
San Mehat7e8529a2010-03-25 09:31:42 -070072 SLOGE("Failed to start unbound listener");
San Mehat168415b2009-05-06 11:14:21 -070073 errno = EINVAL;
74 return -1;
75 } else if (mSocketName) {
76 if ((mSock = android_get_control_socket(mSocketName)) < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070077 SLOGE("Obtaining file descriptor socket '%s' failed: %s",
San Mehat168415b2009-05-06 11:14:21 -070078 mSocketName, strerror(errno));
79 return -1;
80 }
Robert Greenwalt8702bb12012-02-07 12:23:14 -080081 SLOGV("got mSock = %d for %s", mSock, mSocketName);
San Mehat168415b2009-05-06 11:14:21 -070082 }
83
San Mehatfa644ff2009-05-08 11:15:53 -070084 if (mListen && listen(mSock, 4) < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070085 SLOGE("Unable to listen on socket (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -070086 return -1;
San Mehatdf6c1b92009-05-13 08:58:43 -070087 } else if (!mListen)
Robert Greenwalt8702bb12012-02-07 12:23:14 -080088 mClients->push_back(new SocketClient(mSock, false, mUseCmdNum));
San Mehat168415b2009-05-06 11:14:21 -070089
San Mehatc4a895b2009-06-23 21:10:57 -070090 if (pipe(mCtrlPipe)) {
San Mehat7e8529a2010-03-25 09:31:42 -070091 SLOGE("pipe failed (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -070092 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -070093 }
San Mehatfa644ff2009-05-08 11:15:53 -070094
San Mehatc4a895b2009-06-23 21:10:57 -070095 if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) {
San Mehat7e8529a2010-03-25 09:31:42 -070096 SLOGE("pthread_create (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -070097 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -070098 }
San Mehatfa644ff2009-05-08 11:15:53 -070099
100 return 0;
101}
102
103int SocketListener::stopListener() {
104 char c = 0;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100105 int rc;
San Mehatfa644ff2009-05-08 11:15:53 -0700106
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100107 rc = TEMP_FAILURE_RETRY(write(mCtrlPipe[1], &c, 1));
108 if (rc != 1) {
San Mehat7e8529a2010-03-25 09:31:42 -0700109 SLOGE("Error writing to control pipe (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700110 return -1;
111 }
112
San Mehatfa644ff2009-05-08 11:15:53 -0700113 void *ret;
114 if (pthread_join(mThread, &ret)) {
San Mehat7e8529a2010-03-25 09:31:42 -0700115 SLOGE("Error joining to listener thread (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700116 return -1;
117 }
San Mehatdbdb0db2009-05-12 15:50:26 -0700118 close(mCtrlPipe[0]);
119 close(mCtrlPipe[1]);
San Mehatc4a895b2009-06-23 21:10:57 -0700120 mCtrlPipe[0] = -1;
121 mCtrlPipe[1] = -1;
122
123 if (mSocketName && mSock > -1) {
124 close(mSock);
125 mSock = -1;
126 }
127
128 SocketClientCollection::iterator it;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100129 for (it = mClients->begin(); it != mClients->end();) {
San Mehatc4a895b2009-06-23 21:10:57 -0700130 delete (*it);
131 it = mClients->erase(it);
132 }
San Mehatfa644ff2009-05-08 11:15:53 -0700133 return 0;
134}
135
136void *SocketListener::threadStart(void *obj) {
137 SocketListener *me = reinterpret_cast<SocketListener *>(obj);
138
139 me->runListener();
San Mehatfa644ff2009-05-08 11:15:53 -0700140 pthread_exit(NULL);
141 return NULL;
142}
143
144void SocketListener::runListener() {
145
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100146 SocketClientCollection *pendingList = new SocketClientCollection();
147
San Mehat168415b2009-05-06 11:14:21 -0700148 while(1) {
San Mehatfa644ff2009-05-08 11:15:53 -0700149 SocketClientCollection::iterator it;
San Mehat168415b2009-05-06 11:14:21 -0700150 fd_set read_fds;
San Mehat168415b2009-05-06 11:14:21 -0700151 int rc = 0;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100152 int max = -1;
San Mehatfa644ff2009-05-08 11:15:53 -0700153
San Mehat168415b2009-05-06 11:14:21 -0700154 FD_ZERO(&read_fds);
155
San Mehatfa644ff2009-05-08 11:15:53 -0700156 if (mListen) {
San Mehat168415b2009-05-06 11:14:21 -0700157 max = mSock;
San Mehatfa644ff2009-05-08 11:15:53 -0700158 FD_SET(mSock, &read_fds);
San Mehat168415b2009-05-06 11:14:21 -0700159 }
160
San Mehatfa644ff2009-05-08 11:15:53 -0700161 FD_SET(mCtrlPipe[0], &read_fds);
162 if (mCtrlPipe[0] > max)
163 max = mCtrlPipe[0];
164
165 pthread_mutex_lock(&mClientsLock);
166 for (it = mClients->begin(); it != mClients->end(); ++it) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100167 int fd = (*it)->getSocket();
168 FD_SET(fd, &read_fds);
169 if (fd > max)
170 max = fd;
San Mehatfa644ff2009-05-08 11:15:53 -0700171 }
172 pthread_mutex_unlock(&mClientsLock);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800173 SLOGV("mListen=%d, max=%d, mSocketName=%s", mListen, max, mSocketName);
San Mehatdf6c1b92009-05-13 08:58:43 -0700174 if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100175 if (errno == EINTR)
176 continue;
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800177 SLOGE("select failed (%s) mListen=%d, max=%d", strerror(errno), mListen, max);
San Mehatfa644ff2009-05-08 11:15:53 -0700178 sleep(1);
San Mehat168415b2009-05-06 11:14:21 -0700179 continue;
San Mehatdf6c1b92009-05-13 08:58:43 -0700180 } else if (!rc)
San Mehatfa644ff2009-05-08 11:15:53 -0700181 continue;
San Mehat168415b2009-05-06 11:14:21 -0700182
San Mehatdbdb0db2009-05-12 15:50:26 -0700183 if (FD_ISSET(mCtrlPipe[0], &read_fds))
San Mehatfa644ff2009-05-08 11:15:53 -0700184 break;
San Mehatfa644ff2009-05-08 11:15:53 -0700185 if (mListen && FD_ISSET(mSock, &read_fds)) {
186 struct sockaddr addr;
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100187 socklen_t alen;
San Mehatfa644ff2009-05-08 11:15:53 -0700188 int c;
189
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100190 do {
191 alen = sizeof(addr);
192 c = accept(mSock, &addr, &alen);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800193 SLOGV("%s got %d from accept", mSocketName, c);
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100194 } while (c < 0 && errno == EINTR);
195 if (c < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -0700196 SLOGE("accept failed (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700197 sleep(1);
198 continue;
199 }
San Mehatfa644ff2009-05-08 11:15:53 -0700200 pthread_mutex_lock(&mClientsLock);
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800201 mClients->push_back(new SocketClient(c, true, mUseCmdNum));
San Mehatfa644ff2009-05-08 11:15:53 -0700202 pthread_mutex_unlock(&mClientsLock);
203 }
204
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100205 /* Add all active clients to the pending list first */
206 pendingList->clear();
207 pthread_mutex_lock(&mClientsLock);
208 for (it = mClients->begin(); it != mClients->end(); ++it) {
209 int fd = (*it)->getSocket();
210 if (FD_ISSET(fd, &read_fds)) {
211 pendingList->push_back(*it);
San Mehat168415b2009-05-06 11:14:21 -0700212 }
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100213 }
214 pthread_mutex_unlock(&mClientsLock);
215
216 /* Process the pending list, since it is owned by the thread,
217 * there is no need to lock it */
218 while (!pendingList->empty()) {
219 /* Pop the first item from the list */
220 it = pendingList->begin();
221 SocketClient* c = *it;
222 pendingList->erase(it);
Vernon Tang87950072011-04-27 14:01:27 +1000223 /* Process it, if false is returned and our sockets are
224 * connection-based, remove and destroy it */
225 if (!onDataAvailable(c) && mListen) {
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100226 /* Remove the client from our array */
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800227 SLOGV("going to zap %d for %s", c->getSocket(), mSocketName);
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100228 pthread_mutex_lock(&mClientsLock);
229 for (it = mClients->begin(); it != mClients->end(); ++it) {
230 if (*it == c) {
231 mClients->erase(it);
232 break;
233 }
234 }
235 pthread_mutex_unlock(&mClientsLock);
Xianzhu Wang45202462011-09-29 12:59:55 +0800236 /* Remove our reference to the client */
237 c->decRef();
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100238 }
239 }
San Mehat168415b2009-05-06 11:14:21 -0700240 }
David 'Digit' Turner100c0e22011-01-17 03:10:31 +0100241 delete pendingList;
San Mehat168415b2009-05-06 11:14:21 -0700242}
243
San Mehatdb017542009-05-20 15:27:14 -0700244void SocketListener::sendBroadcast(int code, const char *msg, bool addErrno) {
San Mehatd7680662009-05-12 11:16:59 -0700245 pthread_mutex_lock(&mClientsLock);
246 SocketClientCollection::iterator i;
247
248 for (i = mClients->begin(); i != mClients->end(); ++i) {
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800249 // broadcasts are unsolicited and should not include a cmd number
250 if ((*i)->sendMsg(code, msg, addErrno, false)) {
Guang Zhua8185a62012-02-07 19:13:28 -0800251 SLOGW("Error sending broadcast (%s)", strerror(errno));
252 }
253 }
254 pthread_mutex_unlock(&mClientsLock);
255}