blob: 57f31f1f43a9f6396f67c4f22b5ca458d7d487e2 [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) {
33 mListen = listen;
San Mehat168415b2009-05-06 11:14:21 -070034 mSocketName = socketName;
35 mSock = -1;
San Mehatfa644ff2009-05-08 11:15:53 -070036 pthread_mutex_init(&mClientsLock, NULL);
37 mClients = new SocketClientCollection();
San Mehat168415b2009-05-06 11:14:21 -070038}
39
San Mehatfa644ff2009-05-08 11:15:53 -070040SocketListener::SocketListener(int socketFd, bool listen) {
41 mListen = listen;
San Mehat168415b2009-05-06 11:14:21 -070042 mSocketName = NULL;
43 mSock = socketFd;
San Mehatfa644ff2009-05-08 11:15:53 -070044 pthread_mutex_init(&mClientsLock, NULL);
45 mClients = new SocketClientCollection();
San Mehat168415b2009-05-06 11:14:21 -070046}
47
San Mehatfa644ff2009-05-08 11:15:53 -070048int SocketListener::startListener() {
San Mehat168415b2009-05-06 11:14:21 -070049
50 if (!mSocketName && mSock == -1) {
51 errno = EINVAL;
52 return -1;
53 } else if (mSocketName) {
54 if ((mSock = android_get_control_socket(mSocketName)) < 0) {
55 LOGE("Obtaining file descriptor socket '%s' failed: %s",
56 mSocketName, strerror(errno));
57 return -1;
58 }
59 }
60
San Mehatfa644ff2009-05-08 11:15:53 -070061 if (mListen && listen(mSock, 4) < 0) {
62 LOGE("Unable to listen on socket (%s)", strerror(errno));
63 return -1;
64 } else if (!mListen) {
65 mClients->push_back(new SocketClient(mSock));
66 LOGD("Created phantom client");
San Mehat168415b2009-05-06 11:14:21 -070067 }
68
San Mehatfa644ff2009-05-08 11:15:53 -070069 if (pipe(mCtrlPipe))
70 return -1;
71
72 if (pthread_create(&mThread, NULL, SocketListener::threadStart, this))
73 return -1;
74
75 return 0;
76}
77
78int SocketListener::stopListener() {
79 char c = 0;
80
81 if (write(mCtrlPipe[1], &c, 1) != 1) {
82 LOGE("Error writing to control pipe (%s)", strerror(errno));
83 return -1;
84 }
85
86 LOGD("Signaled listener thread - waiting for it to die");
87 void *ret;
88 if (pthread_join(mThread, &ret)) {
89 LOGE("Error joining to listener thread (%s)", strerror(errno));
90 return -1;
91 }
92 LOGD("Listener stopped");
93 return 0;
94}
95
96void *SocketListener::threadStart(void *obj) {
97 SocketListener *me = reinterpret_cast<SocketListener *>(obj);
98
99 me->runListener();
100 LOGD("Listener thread shutting down");
101 pthread_exit(NULL);
102 return NULL;
103}
104
105void SocketListener::runListener() {
106
San Mehat168415b2009-05-06 11:14:21 -0700107 while(1) {
San Mehatfa644ff2009-05-08 11:15:53 -0700108 SocketClientCollection::iterator it;
San Mehat168415b2009-05-06 11:14:21 -0700109 fd_set read_fds;
110 struct timeval to;
San Mehat168415b2009-05-06 11:14:21 -0700111 int rc = 0;
112
113 to.tv_sec = 60 * 60;
114 to.tv_usec = 0;
115
San Mehatfa644ff2009-05-08 11:15:53 -0700116 int max = 0;
117
San Mehat168415b2009-05-06 11:14:21 -0700118 FD_ZERO(&read_fds);
119
San Mehatfa644ff2009-05-08 11:15:53 -0700120 if (mListen) {
San Mehat168415b2009-05-06 11:14:21 -0700121 max = mSock;
San Mehatfa644ff2009-05-08 11:15:53 -0700122 FD_SET(mSock, &read_fds);
San Mehat168415b2009-05-06 11:14:21 -0700123 }
124
San Mehatfa644ff2009-05-08 11:15:53 -0700125 FD_SET(mCtrlPipe[0], &read_fds);
126 if (mCtrlPipe[0] > max)
127 max = mCtrlPipe[0];
128
129 pthread_mutex_lock(&mClientsLock);
130 for (it = mClients->begin(); it != mClients->end(); ++it) {
131 FD_SET((*it)->getSocket(), &read_fds);
132 if ((*it)->getSocket() > max)
133 max = (*it)->getSocket();
134 }
135 pthread_mutex_unlock(&mClientsLock);
136
San Mehat168415b2009-05-06 11:14:21 -0700137 if ((rc = select(max + 1, &read_fds, NULL, NULL, &to)) < 0) {
138 LOGE("select failed (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700139 sleep(1);
San Mehat168415b2009-05-06 11:14:21 -0700140 continue;
San Mehatfa644ff2009-05-08 11:15:53 -0700141 } else if (!rc) {
142 LOGD("select timeout");
143 continue;
144 }
San Mehat168415b2009-05-06 11:14:21 -0700145
San Mehatfa644ff2009-05-08 11:15:53 -0700146 if (FD_ISSET(mCtrlPipe[0], &read_fds)) {
147 LOGD("Control message received");
148 break;
149 }
150 if (mListen && FD_ISSET(mSock, &read_fds)) {
151 struct sockaddr addr;
152 socklen_t alen = sizeof(addr);
153 int c;
154
155 if ((c = accept(mSock, &addr, &alen)) < 0) {
156 LOGE("accept failed (%s)", strerror(errno));
157 sleep(1);
158 continue;
159 }
160 LOGD("SocketListener client connection accepted");
161 pthread_mutex_lock(&mClientsLock);
162 mClients->push_back(new SocketClient(c));
163 pthread_mutex_unlock(&mClientsLock);
164 }
165
166 do {
167 pthread_mutex_lock(&mClientsLock);
168 for (it = mClients->begin(); it != mClients->end(); ++it) {
169 int fd = (*it)->getSocket();
170 if (FD_ISSET(fd, &read_fds)) {
171 pthread_mutex_unlock(&mClientsLock);
172 if (!onDataAvailable(*it)) {
173 LOGD("SocketListener closing client socket");
174 close(fd);
175 pthread_mutex_lock(&mClientsLock);
176 delete *it;
177 it = mClients->erase(it);
178 pthread_mutex_unlock(&mClientsLock);
179 }
180 FD_CLR(fd, &read_fds);
181 continue;
San Mehat168415b2009-05-06 11:14:21 -0700182 }
San Mehat168415b2009-05-06 11:14:21 -0700183 }
San Mehatfa644ff2009-05-08 11:15:53 -0700184 pthread_mutex_unlock(&mClientsLock);
185 } while (0);
San Mehat168415b2009-05-06 11:14:21 -0700186 }
San Mehat168415b2009-05-06 11:14:21 -0700187}
188
San Mehatd7680662009-05-12 11:16:59 -0700189void SocketListener::sendBroadcast(int code, char *msg, bool addErrno) {
190 pthread_mutex_lock(&mClientsLock);
191 SocketClientCollection::iterator i;
192
193 for (i = mClients->begin(); i != mClients->end(); ++i) {
194 if ((*i)->sendMsg(code, msg, addErrno)) {
195 LOGW("Error sending broadcast (%s)", strerror(errno));
196 }
197 }
198 pthread_mutex_unlock(&mClientsLock);
199}
200
San Mehatfa644ff2009-05-08 11:15:53 -0700201void SocketListener::sendBroadcast(char *msg) {
202 pthread_mutex_lock(&mClientsLock);
203 SocketClientCollection::iterator i;
204
205 for (i = mClients->begin(); i != mClients->end(); ++i) {
206 if ((*i)->sendMsg(msg)) {
207 LOGW("Error sending broadcast (%s)", strerror(errno));
208 }
209 }
210 pthread_mutex_unlock(&mClientsLock);
San Mehat168415b2009-05-06 11:14:21 -0700211}