San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 27 | #include <cutils/sockets.h> |
| 28 | |
| 29 | #include <sysutils/SocketListener.h> |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 30 | #include <sysutils/SocketClient.h> |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 31 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 32 | SocketListener::SocketListener(const char *socketName, bool listen) { |
| 33 | mListen = listen; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 34 | mSocketName = socketName; |
| 35 | mSock = -1; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 36 | pthread_mutex_init(&mClientsLock, NULL); |
| 37 | mClients = new SocketClientCollection(); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 38 | } |
| 39 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 40 | SocketListener::SocketListener(int socketFd, bool listen) { |
| 41 | mListen = listen; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 42 | mSocketName = NULL; |
| 43 | mSock = socketFd; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 44 | pthread_mutex_init(&mClientsLock, NULL); |
| 45 | mClients = new SocketClientCollection(); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 46 | } |
| 47 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 48 | SocketListener::~SocketListener() { |
| 49 | if (mSocketName && mSock > -1) |
| 50 | close(mSock); |
| 51 | |
| 52 | if (mCtrlPipe[0] != -1) { |
| 53 | close(mCtrlPipe[0]); |
| 54 | close(mCtrlPipe[1]); |
| 55 | } |
| 56 | SocketClientCollection::iterator it; |
| 57 | for (it = mClients->begin(); it != mClients->end(); ++it) { |
| 58 | delete (*it); |
| 59 | it = mClients->erase(it); |
| 60 | } |
| 61 | delete mClients; |
| 62 | } |
| 63 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 64 | int SocketListener::startListener() { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 65 | |
| 66 | if (!mSocketName && mSock == -1) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 67 | SLOGE("Failed to start unbound listener"); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 68 | errno = EINVAL; |
| 69 | return -1; |
| 70 | } else if (mSocketName) { |
| 71 | if ((mSock = android_get_control_socket(mSocketName)) < 0) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 72 | SLOGE("Obtaining file descriptor socket '%s' failed: %s", |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 73 | mSocketName, strerror(errno)); |
| 74 | return -1; |
| 75 | } |
| 76 | } |
| 77 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 78 | if (mListen && listen(mSock, 4) < 0) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 79 | SLOGE("Unable to listen on socket (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 80 | return -1; |
San Mehat | df6c1b9 | 2009-05-13 08:58:43 -0700 | [diff] [blame] | 81 | } else if (!mListen) |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 82 | mClients->push_back(new SocketClient(mSock)); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 83 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 84 | if (pipe(mCtrlPipe)) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 85 | SLOGE("pipe failed (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 86 | return -1; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 87 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 88 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 89 | if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 90 | SLOGE("pthread_create (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 91 | return -1; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 92 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | int SocketListener::stopListener() { |
| 98 | char c = 0; |
| 99 | |
| 100 | if (write(mCtrlPipe[1], &c, 1) != 1) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 101 | SLOGE("Error writing to control pipe (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 102 | return -1; |
| 103 | } |
| 104 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 105 | void *ret; |
| 106 | if (pthread_join(mThread, &ret)) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 107 | SLOGE("Error joining to listener thread (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 108 | return -1; |
| 109 | } |
San Mehat | dbdb0db | 2009-05-12 15:50:26 -0700 | [diff] [blame] | 110 | close(mCtrlPipe[0]); |
| 111 | close(mCtrlPipe[1]); |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 112 | mCtrlPipe[0] = -1; |
| 113 | mCtrlPipe[1] = -1; |
| 114 | |
| 115 | if (mSocketName && mSock > -1) { |
| 116 | close(mSock); |
| 117 | mSock = -1; |
| 118 | } |
| 119 | |
| 120 | SocketClientCollection::iterator it; |
| 121 | for (it = mClients->begin(); it != mClients->end(); ++it) { |
| 122 | delete (*it); |
| 123 | it = mClients->erase(it); |
| 124 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | void *SocketListener::threadStart(void *obj) { |
| 129 | SocketListener *me = reinterpret_cast<SocketListener *>(obj); |
| 130 | |
| 131 | me->runListener(); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 132 | pthread_exit(NULL); |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | void SocketListener::runListener() { |
| 137 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 138 | while(1) { |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 139 | SocketClientCollection::iterator it; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 140 | fd_set read_fds; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 141 | int rc = 0; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 142 | int max = 0; |
| 143 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 144 | FD_ZERO(&read_fds); |
| 145 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 146 | if (mListen) { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 147 | max = mSock; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 148 | FD_SET(mSock, &read_fds); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 149 | } |
| 150 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 151 | FD_SET(mCtrlPipe[0], &read_fds); |
| 152 | if (mCtrlPipe[0] > max) |
| 153 | max = mCtrlPipe[0]; |
| 154 | |
| 155 | pthread_mutex_lock(&mClientsLock); |
| 156 | for (it = mClients->begin(); it != mClients->end(); ++it) { |
| 157 | FD_SET((*it)->getSocket(), &read_fds); |
| 158 | if ((*it)->getSocket() > max) |
| 159 | max = (*it)->getSocket(); |
| 160 | } |
| 161 | pthread_mutex_unlock(&mClientsLock); |
San Mehat | 03f0d27 | 2009-05-26 15:18:25 -0700 | [diff] [blame] | 162 | |
San Mehat | df6c1b9 | 2009-05-13 08:58:43 -0700 | [diff] [blame] | 163 | if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 164 | SLOGE("select failed (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 165 | sleep(1); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 166 | continue; |
San Mehat | df6c1b9 | 2009-05-13 08:58:43 -0700 | [diff] [blame] | 167 | } else if (!rc) |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 168 | continue; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 169 | |
San Mehat | dbdb0db | 2009-05-12 15:50:26 -0700 | [diff] [blame] | 170 | if (FD_ISSET(mCtrlPipe[0], &read_fds)) |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 171 | break; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 172 | if (mListen && FD_ISSET(mSock, &read_fds)) { |
| 173 | struct sockaddr addr; |
| 174 | socklen_t alen = sizeof(addr); |
| 175 | int c; |
| 176 | |
| 177 | if ((c = accept(mSock, &addr, &alen)) < 0) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 178 | SLOGE("accept failed (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 179 | sleep(1); |
| 180 | continue; |
| 181 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 182 | pthread_mutex_lock(&mClientsLock); |
| 183 | mClients->push_back(new SocketClient(c)); |
| 184 | pthread_mutex_unlock(&mClientsLock); |
| 185 | } |
| 186 | |
| 187 | do { |
| 188 | pthread_mutex_lock(&mClientsLock); |
| 189 | for (it = mClients->begin(); it != mClients->end(); ++it) { |
| 190 | int fd = (*it)->getSocket(); |
| 191 | if (FD_ISSET(fd, &read_fds)) { |
| 192 | pthread_mutex_unlock(&mClientsLock); |
| 193 | if (!onDataAvailable(*it)) { |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 194 | close(fd); |
| 195 | pthread_mutex_lock(&mClientsLock); |
| 196 | delete *it; |
| 197 | it = mClients->erase(it); |
| 198 | pthread_mutex_unlock(&mClientsLock); |
| 199 | } |
| 200 | FD_CLR(fd, &read_fds); |
Brad Fitzpatrick | 86d01de | 2010-09-14 10:22:14 -0700 | [diff] [blame] | 201 | pthread_mutex_lock(&mClientsLock); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 202 | continue; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 203 | } |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 204 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 205 | pthread_mutex_unlock(&mClientsLock); |
| 206 | } while (0); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 207 | } |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 208 | } |
| 209 | |
San Mehat | db01754 | 2009-05-20 15:27:14 -0700 | [diff] [blame] | 210 | void SocketListener::sendBroadcast(int code, const char *msg, bool addErrno) { |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 211 | pthread_mutex_lock(&mClientsLock); |
| 212 | SocketClientCollection::iterator i; |
| 213 | |
| 214 | for (i = mClients->begin(); i != mClients->end(); ++i) { |
| 215 | if ((*i)->sendMsg(code, msg, addErrno)) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 216 | SLOGW("Error sending broadcast (%s)", strerror(errno)); |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | pthread_mutex_unlock(&mClientsLock); |
| 220 | } |
| 221 | |
San Mehat | db01754 | 2009-05-20 15:27:14 -0700 | [diff] [blame] | 222 | void SocketListener::sendBroadcast(const char *msg) { |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 223 | pthread_mutex_lock(&mClientsLock); |
| 224 | SocketClientCollection::iterator i; |
| 225 | |
| 226 | for (i = mClients->begin(); i != mClients->end(); ++i) { |
| 227 | if ((*i)->sendMsg(msg)) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 228 | SLOGW("Error sending broadcast (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | pthread_mutex_unlock(&mClientsLock); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 232 | } |