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 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 86 | void *ret; |
| 87 | if (pthread_join(mThread, &ret)) { |
| 88 | LOGE("Error joining to listener thread (%s)", strerror(errno)); |
| 89 | return -1; |
| 90 | } |
San Mehat | dbdb0db | 2009-05-12 15:50:26 -0700 | [diff] [blame^] | 91 | close(mCtrlPipe[0]); |
| 92 | close(mCtrlPipe[1]); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | void *SocketListener::threadStart(void *obj) { |
| 97 | SocketListener *me = reinterpret_cast<SocketListener *>(obj); |
| 98 | |
| 99 | me->runListener(); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 100 | pthread_exit(NULL); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | void SocketListener::runListener() { |
| 105 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 106 | while(1) { |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 107 | SocketClientCollection::iterator it; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 108 | fd_set read_fds; |
| 109 | struct timeval to; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 110 | int rc = 0; |
| 111 | |
| 112 | to.tv_sec = 60 * 60; |
| 113 | to.tv_usec = 0; |
| 114 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 115 | int max = 0; |
| 116 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 117 | FD_ZERO(&read_fds); |
| 118 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 119 | if (mListen) { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 120 | max = mSock; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 121 | FD_SET(mSock, &read_fds); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 122 | } |
| 123 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 124 | 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 Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 136 | if ((rc = select(max + 1, &read_fds, NULL, NULL, &to)) < 0) { |
| 137 | LOGE("select failed (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 138 | sleep(1); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 139 | continue; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 140 | } else if (!rc) { |
| 141 | LOGD("select timeout"); |
| 142 | continue; |
| 143 | } |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 144 | |
San Mehat | dbdb0db | 2009-05-12 15:50:26 -0700 | [diff] [blame^] | 145 | if (FD_ISSET(mCtrlPipe[0], &read_fds)) |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 146 | break; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 147 | 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 Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 157 | 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 Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 178 | } |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 179 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 180 | pthread_mutex_unlock(&mClientsLock); |
| 181 | } while (0); |
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 | } |
| 184 | |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 185 | void 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 Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 197 | void 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 Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 207 | } |