blob: 7e385465dbb6414225efd02362229cb444b13526 [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
San Mehatfa644ff2009-05-08 11:15:53 -070086 void *ret;
87 if (pthread_join(mThread, &ret)) {
88 LOGE("Error joining to listener thread (%s)", strerror(errno));
89 return -1;
90 }
San Mehatdbdb0db2009-05-12 15:50:26 -070091 close(mCtrlPipe[0]);
92 close(mCtrlPipe[1]);
San Mehatfa644ff2009-05-08 11:15:53 -070093 return 0;
94}
95
96void *SocketListener::threadStart(void *obj) {
97 SocketListener *me = reinterpret_cast<SocketListener *>(obj);
98
99 me->runListener();
San Mehatfa644ff2009-05-08 11:15:53 -0700100 pthread_exit(NULL);
101 return NULL;
102}
103
104void SocketListener::runListener() {
105
San Mehat168415b2009-05-06 11:14:21 -0700106 while(1) {
San Mehatfa644ff2009-05-08 11:15:53 -0700107 SocketClientCollection::iterator it;
San Mehat168415b2009-05-06 11:14:21 -0700108 fd_set read_fds;
109 struct timeval to;
San Mehat168415b2009-05-06 11:14:21 -0700110 int rc = 0;
111
112 to.tv_sec = 60 * 60;
113 to.tv_usec = 0;
114
San Mehatfa644ff2009-05-08 11:15:53 -0700115 int max = 0;
116
San Mehat168415b2009-05-06 11:14:21 -0700117 FD_ZERO(&read_fds);
118
San Mehatfa644ff2009-05-08 11:15:53 -0700119 if (mListen) {
San Mehat168415b2009-05-06 11:14:21 -0700120 max = mSock;
San Mehatfa644ff2009-05-08 11:15:53 -0700121 FD_SET(mSock, &read_fds);
San Mehat168415b2009-05-06 11:14:21 -0700122 }
123
San Mehatfa644ff2009-05-08 11:15:53 -0700124 FD_SET(mCtrlPipe[0], &read_fds);
125 if (mCtrlPipe[0] > max)
126 max = mCtrlPipe[0];
127
128 pthread_mutex_lock(&mClientsLock);
129 for (it = mClients->begin(); it != mClients->end(); ++it) {
130 FD_SET((*it)->getSocket(), &read_fds);
131 if ((*it)->getSocket() > max)
132 max = (*it)->getSocket();
133 }
134 pthread_mutex_unlock(&mClientsLock);
135
San Mehat168415b2009-05-06 11:14:21 -0700136 if ((rc = select(max + 1, &read_fds, NULL, NULL, &to)) < 0) {
137 LOGE("select failed (%s)", strerror(errno));
San Mehatfa644ff2009-05-08 11:15:53 -0700138 sleep(1);
San Mehat168415b2009-05-06 11:14:21 -0700139 continue;
San Mehatfa644ff2009-05-08 11:15:53 -0700140 } else if (!rc) {
141 LOGD("select timeout");
142 continue;
143 }
San Mehat168415b2009-05-06 11:14:21 -0700144
San Mehatdbdb0db2009-05-12 15:50:26 -0700145 if (FD_ISSET(mCtrlPipe[0], &read_fds))
San Mehatfa644ff2009-05-08 11:15:53 -0700146 break;
San Mehatfa644ff2009-05-08 11:15:53 -0700147 if (mListen && FD_ISSET(mSock, &read_fds)) {
148 struct sockaddr addr;
149 socklen_t alen = sizeof(addr);
150 int c;
151
152 if ((c = accept(mSock, &addr, &alen)) < 0) {
153 LOGE("accept failed (%s)", strerror(errno));
154 sleep(1);
155 continue;
156 }
San Mehatfa644ff2009-05-08 11:15:53 -0700157 pthread_mutex_lock(&mClientsLock);
158 mClients->push_back(new SocketClient(c));
159 pthread_mutex_unlock(&mClientsLock);
160 }
161
162 do {
163 pthread_mutex_lock(&mClientsLock);
164 for (it = mClients->begin(); it != mClients->end(); ++it) {
165 int fd = (*it)->getSocket();
166 if (FD_ISSET(fd, &read_fds)) {
167 pthread_mutex_unlock(&mClientsLock);
168 if (!onDataAvailable(*it)) {
169 LOGD("SocketListener closing client socket");
170 close(fd);
171 pthread_mutex_lock(&mClientsLock);
172 delete *it;
173 it = mClients->erase(it);
174 pthread_mutex_unlock(&mClientsLock);
175 }
176 FD_CLR(fd, &read_fds);
177 continue;
San Mehat168415b2009-05-06 11:14:21 -0700178 }
San Mehat168415b2009-05-06 11:14:21 -0700179 }
San Mehatfa644ff2009-05-08 11:15:53 -0700180 pthread_mutex_unlock(&mClientsLock);
181 } while (0);
San Mehat168415b2009-05-06 11:14:21 -0700182 }
San Mehat168415b2009-05-06 11:14:21 -0700183}
184
San Mehatd7680662009-05-12 11:16:59 -0700185void SocketListener::sendBroadcast(int code, char *msg, bool addErrno) {
186 pthread_mutex_lock(&mClientsLock);
187 SocketClientCollection::iterator i;
188
189 for (i = mClients->begin(); i != mClients->end(); ++i) {
190 if ((*i)->sendMsg(code, msg, addErrno)) {
191 LOGW("Error sending broadcast (%s)", strerror(errno));
192 }
193 }
194 pthread_mutex_unlock(&mClientsLock);
195}
196
San Mehatfa644ff2009-05-08 11:15:53 -0700197void SocketListener::sendBroadcast(char *msg) {
198 pthread_mutex_lock(&mClientsLock);
199 SocketClientCollection::iterator i;
200
201 for (i = mClients->begin(); i != mClients->end(); ++i) {
202 if ((*i)->sendMsg(msg)) {
203 LOGW("Error sending broadcast (%s)", strerror(errno));
204 }
205 }
206 pthread_mutex_unlock(&mClientsLock);
San Mehat168415b2009-05-06 11:14:21 -0700207}