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 | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 48 | int SocketListener::startListener() { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 49 | |
| 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 Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 61 | 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 Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 67 | } |
| 68 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 69 | 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 | |
| 78 | int 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 | |
| 96 | void *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 | |
| 105 | void SocketListener::runListener() { |
| 106 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 107 | while(1) { |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 108 | SocketClientCollection::iterator it; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 109 | fd_set read_fds; |
| 110 | struct timeval to; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 111 | int rc = 0; |
| 112 | |
| 113 | to.tv_sec = 60 * 60; |
| 114 | to.tv_usec = 0; |
| 115 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 116 | int max = 0; |
| 117 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 118 | FD_ZERO(&read_fds); |
| 119 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 120 | if (mListen) { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 121 | max = mSock; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 122 | FD_SET(mSock, &read_fds); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 123 | } |
| 124 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 125 | 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 Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 137 | if ((rc = select(max + 1, &read_fds, NULL, NULL, &to)) < 0) { |
| 138 | LOGE("select failed (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 139 | sleep(1); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 140 | continue; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 141 | } else if (!rc) { |
| 142 | LOGD("select timeout"); |
| 143 | continue; |
| 144 | } |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 145 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 146 | 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 Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 182 | } |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 183 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 184 | pthread_mutex_unlock(&mClientsLock); |
| 185 | } while (0); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 186 | } |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 187 | } |
| 188 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 189 | void SocketListener::sendBroadcast(char *msg) { |
| 190 | pthread_mutex_lock(&mClientsLock); |
| 191 | SocketClientCollection::iterator i; |
| 192 | |
| 193 | for (i = mClients->begin(); i != mClients->end(); ++i) { |
| 194 | if ((*i)->sendMsg(msg)) { |
| 195 | LOGW("Error sending broadcast (%s)", strerror(errno)); |
| 196 | } |
| 197 | } |
| 198 | pthread_mutex_unlock(&mClientsLock); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 199 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame^] | 200 | |
| 201 | void SocketListener::sendBroadcast(char *msg, char *data) { |
| 202 | pthread_mutex_lock(&mClientsLock); |
| 203 | SocketClientCollection::iterator i; |
| 204 | |
| 205 | for (i = mClients->begin(); i != mClients->end(); ++i) { |
| 206 | if ((*i)->sendMsg(msg, data)) { |
| 207 | LOGW("Error sending broadcast (%s)", strerror(errno)); |
| 208 | } |
| 209 | } |
| 210 | pthread_mutex_unlock(&mClientsLock); |
| 211 | } |
| 212 | |