San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 1 | /* |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 2 | * Copyright (C) 2008-2014 The Android Open Source Project |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 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 | |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 32 | #define CtrlPipe_Shutdown 0 |
| 33 | #define CtrlPipe_Wakeup 1 |
| 34 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 35 | SocketListener::SocketListener(const char *socketName, bool listen) { |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 36 | init(socketName, -1, listen, false); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 37 | } |
| 38 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 39 | SocketListener::SocketListener(int socketFd, bool listen) { |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 40 | init(NULL, socketFd, listen, false); |
| 41 | } |
| 42 | |
| 43 | SocketListener::SocketListener(const char *socketName, bool listen, bool useCmdNum) { |
| 44 | init(socketName, -1, listen, useCmdNum); |
| 45 | } |
| 46 | |
| 47 | void SocketListener::init(const char *socketName, int socketFd, bool listen, bool useCmdNum) { |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 48 | mListen = listen; |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 49 | mSocketName = socketName; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 50 | mSock = socketFd; |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 51 | mUseCmdNum = useCmdNum; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 52 | pthread_mutex_init(&mClientsLock, NULL); |
| 53 | mClients = new SocketClientCollection(); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 54 | } |
| 55 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 56 | SocketListener::~SocketListener() { |
| 57 | if (mSocketName && mSock > -1) |
| 58 | close(mSock); |
| 59 | |
| 60 | if (mCtrlPipe[0] != -1) { |
| 61 | close(mCtrlPipe[0]); |
| 62 | close(mCtrlPipe[1]); |
| 63 | } |
| 64 | SocketClientCollection::iterator it; |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 65 | for (it = mClients->begin(); it != mClients->end();) { |
Brad Fitzpatrick | 13aa8ad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 66 | (*it)->decRef(); |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 67 | it = mClients->erase(it); |
| 68 | } |
| 69 | delete mClients; |
| 70 | } |
| 71 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 72 | int SocketListener::startListener() { |
Mark Salyzyn | 581edc1 | 2013-11-20 13:38:52 -0800 | [diff] [blame] | 73 | return startListener(4); |
| 74 | } |
| 75 | |
| 76 | int SocketListener::startListener(int backlog) { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 77 | |
| 78 | if (!mSocketName && mSock == -1) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 79 | SLOGE("Failed to start unbound listener"); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 80 | errno = EINVAL; |
| 81 | return -1; |
| 82 | } else if (mSocketName) { |
| 83 | if ((mSock = android_get_control_socket(mSocketName)) < 0) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 84 | SLOGE("Obtaining file descriptor socket '%s' failed: %s", |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 85 | mSocketName, strerror(errno)); |
| 86 | return -1; |
| 87 | } |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 88 | SLOGV("got mSock = %d for %s", mSock, mSocketName); |
Jeff Sharkey | 55fd316 | 2015-04-01 22:31:40 -0700 | [diff] [blame] | 89 | fcntl(mSock, F_SETFD, FD_CLOEXEC); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Mark Salyzyn | 581edc1 | 2013-11-20 13:38:52 -0800 | [diff] [blame] | 92 | if (mListen && listen(mSock, backlog) < 0) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 93 | SLOGE("Unable to listen on socket (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 94 | return -1; |
San Mehat | df6c1b9 | 2009-05-13 08:58:43 -0700 | [diff] [blame] | 95 | } else if (!mListen) |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 96 | mClients->push_back(new SocketClient(mSock, false, mUseCmdNum)); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 97 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 98 | if (pipe(mCtrlPipe)) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 99 | SLOGE("pipe failed (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 100 | return -1; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 101 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 102 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 103 | if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 104 | SLOGE("pthread_create (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 105 | return -1; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 106 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | int SocketListener::stopListener() { |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 112 | char c = CtrlPipe_Shutdown; |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 113 | int rc; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 114 | |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 115 | rc = TEMP_FAILURE_RETRY(write(mCtrlPipe[1], &c, 1)); |
| 116 | if (rc != 1) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 117 | SLOGE("Error writing to control pipe (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 118 | return -1; |
| 119 | } |
| 120 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 121 | void *ret; |
| 122 | if (pthread_join(mThread, &ret)) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 123 | SLOGE("Error joining to listener thread (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 124 | return -1; |
| 125 | } |
San Mehat | dbdb0db | 2009-05-12 15:50:26 -0700 | [diff] [blame] | 126 | close(mCtrlPipe[0]); |
| 127 | close(mCtrlPipe[1]); |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 128 | mCtrlPipe[0] = -1; |
| 129 | mCtrlPipe[1] = -1; |
| 130 | |
| 131 | if (mSocketName && mSock > -1) { |
| 132 | close(mSock); |
| 133 | mSock = -1; |
| 134 | } |
| 135 | |
| 136 | SocketClientCollection::iterator it; |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 137 | for (it = mClients->begin(); it != mClients->end();) { |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 138 | delete (*it); |
| 139 | it = mClients->erase(it); |
| 140 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | void *SocketListener::threadStart(void *obj) { |
| 145 | SocketListener *me = reinterpret_cast<SocketListener *>(obj); |
| 146 | |
| 147 | me->runListener(); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 148 | pthread_exit(NULL); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | void SocketListener::runListener() { |
| 153 | |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 154 | SocketClientCollection pendingList; |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 155 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 156 | while(1) { |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 157 | SocketClientCollection::iterator it; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 158 | fd_set read_fds; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 159 | int rc = 0; |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 160 | int max = -1; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 161 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 162 | FD_ZERO(&read_fds); |
| 163 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 164 | if (mListen) { |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 165 | max = mSock; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 166 | FD_SET(mSock, &read_fds); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 167 | } |
| 168 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 169 | FD_SET(mCtrlPipe[0], &read_fds); |
| 170 | if (mCtrlPipe[0] > max) |
| 171 | max = mCtrlPipe[0]; |
| 172 | |
| 173 | pthread_mutex_lock(&mClientsLock); |
| 174 | for (it = mClients->begin(); it != mClients->end(); ++it) { |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 175 | // NB: calling out to an other object with mClientsLock held (safe) |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 176 | int fd = (*it)->getSocket(); |
| 177 | FD_SET(fd, &read_fds); |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 178 | if (fd > max) { |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 179 | max = fd; |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 180 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 181 | } |
| 182 | pthread_mutex_unlock(&mClientsLock); |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 183 | SLOGV("mListen=%d, max=%d, mSocketName=%s", mListen, max, mSocketName); |
San Mehat | df6c1b9 | 2009-05-13 08:58:43 -0700 | [diff] [blame] | 184 | if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) { |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 185 | if (errno == EINTR) |
| 186 | continue; |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 187 | SLOGE("select failed (%s) mListen=%d, max=%d", strerror(errno), mListen, max); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 188 | sleep(1); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 189 | continue; |
San Mehat | df6c1b9 | 2009-05-13 08:58:43 -0700 | [diff] [blame] | 190 | } else if (!rc) |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 191 | continue; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 192 | |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 193 | if (FD_ISSET(mCtrlPipe[0], &read_fds)) { |
| 194 | char c = CtrlPipe_Shutdown; |
| 195 | TEMP_FAILURE_RETRY(read(mCtrlPipe[0], &c, 1)); |
| 196 | if (c == CtrlPipe_Shutdown) { |
| 197 | break; |
| 198 | } |
| 199 | continue; |
| 200 | } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 201 | if (mListen && FD_ISSET(mSock, &read_fds)) { |
| 202 | struct sockaddr addr; |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 203 | socklen_t alen; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 204 | int c; |
| 205 | |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 206 | do { |
| 207 | alen = sizeof(addr); |
| 208 | c = accept(mSock, &addr, &alen); |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 209 | SLOGV("%s got %d from accept", mSocketName, c); |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 210 | } while (c < 0 && errno == EINTR); |
| 211 | if (c < 0) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 212 | SLOGE("accept failed (%s)", strerror(errno)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 213 | sleep(1); |
| 214 | continue; |
| 215 | } |
Jeff Sharkey | 55fd316 | 2015-04-01 22:31:40 -0700 | [diff] [blame] | 216 | fcntl(c, F_SETFD, FD_CLOEXEC); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 217 | pthread_mutex_lock(&mClientsLock); |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 218 | mClients->push_back(new SocketClient(c, true, mUseCmdNum)); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 219 | pthread_mutex_unlock(&mClientsLock); |
| 220 | } |
| 221 | |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 222 | /* Add all active clients to the pending list first */ |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 223 | pendingList.clear(); |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 224 | pthread_mutex_lock(&mClientsLock); |
| 225 | for (it = mClients->begin(); it != mClients->end(); ++it) { |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 226 | SocketClient* c = *it; |
| 227 | // NB: calling out to an other object with mClientsLock held (safe) |
| 228 | int fd = c->getSocket(); |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 229 | if (FD_ISSET(fd, &read_fds)) { |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 230 | pendingList.push_back(c); |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 231 | c->incRef(); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 232 | } |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 233 | } |
| 234 | pthread_mutex_unlock(&mClientsLock); |
| 235 | |
| 236 | /* Process the pending list, since it is owned by the thread, |
| 237 | * there is no need to lock it */ |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 238 | while (!pendingList.empty()) { |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 239 | /* Pop the first item from the list */ |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 240 | it = pendingList.begin(); |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 241 | SocketClient* c = *it; |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 242 | pendingList.erase(it); |
| 243 | /* Process it, if false is returned, remove from list */ |
| 244 | if (!onDataAvailable(c)) { |
| 245 | release(c, false); |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 246 | } |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 247 | c->decRef(); |
David 'Digit' Turner | 100c0e2 | 2011-01-17 03:10:31 +0100 | [diff] [blame] | 248 | } |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 249 | } |
Mark Salyzyn | 44b99c2 | 2014-01-08 12:44:23 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | bool SocketListener::release(SocketClient* c, bool wakeup) { |
| 253 | bool ret = false; |
| 254 | /* if our sockets are connection-based, remove and destroy it */ |
| 255 | if (mListen && c) { |
| 256 | /* Remove the client from our array */ |
| 257 | SLOGV("going to zap %d for %s", c->getSocket(), mSocketName); |
| 258 | pthread_mutex_lock(&mClientsLock); |
| 259 | SocketClientCollection::iterator it; |
| 260 | for (it = mClients->begin(); it != mClients->end(); ++it) { |
| 261 | if (*it == c) { |
| 262 | mClients->erase(it); |
| 263 | ret = true; |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | pthread_mutex_unlock(&mClientsLock); |
| 268 | if (ret) { |
| 269 | ret = c->decRef(); |
| 270 | if (wakeup) { |
| 271 | char b = CtrlPipe_Wakeup; |
| 272 | TEMP_FAILURE_RETRY(write(mCtrlPipe[1], &b, 1)); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | return ret; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 277 | } |
| 278 | |
San Mehat | db01754 | 2009-05-20 15:27:14 -0700 | [diff] [blame] | 279 | void SocketListener::sendBroadcast(int code, const char *msg, bool addErrno) { |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 280 | SocketClientCollection safeList; |
| 281 | |
| 282 | /* Add all active clients to the safe list first */ |
| 283 | safeList.clear(); |
San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 284 | pthread_mutex_lock(&mClientsLock); |
| 285 | SocketClientCollection::iterator i; |
| 286 | |
| 287 | for (i = mClients->begin(); i != mClients->end(); ++i) { |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 288 | SocketClient* c = *i; |
| 289 | c->incRef(); |
| 290 | safeList.push_back(c); |
Guang Zhu | a8185a6 | 2012-02-07 19:13:28 -0800 | [diff] [blame] | 291 | } |
| 292 | pthread_mutex_unlock(&mClientsLock); |
Mark Salyzyn | 23f0410 | 2012-01-24 20:30:10 -0800 | [diff] [blame] | 293 | |
| 294 | while (!safeList.empty()) { |
| 295 | /* Pop the first item from the list */ |
| 296 | i = safeList.begin(); |
| 297 | SocketClient* c = *i; |
| 298 | safeList.erase(i); |
| 299 | // broadcasts are unsolicited and should not include a cmd number |
| 300 | if (c->sendMsg(code, msg, addErrno, false)) { |
| 301 | SLOGW("Error sending broadcast (%s)", strerror(errno)); |
| 302 | } |
| 303 | c->decRef(); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void SocketListener::runOnEachSocket(SocketClientCommand *command) { |
| 308 | SocketClientCollection safeList; |
| 309 | |
| 310 | /* Add all active clients to the safe list first */ |
| 311 | safeList.clear(); |
| 312 | pthread_mutex_lock(&mClientsLock); |
| 313 | SocketClientCollection::iterator i; |
| 314 | |
| 315 | for (i = mClients->begin(); i != mClients->end(); ++i) { |
| 316 | SocketClient* c = *i; |
| 317 | c->incRef(); |
| 318 | safeList.push_back(c); |
| 319 | } |
| 320 | pthread_mutex_unlock(&mClientsLock); |
| 321 | |
| 322 | while (!safeList.empty()) { |
| 323 | /* Pop the first item from the list */ |
| 324 | i = safeList.begin(); |
| 325 | SocketClient* c = *i; |
| 326 | safeList.erase(i); |
| 327 | command->runSocketCommand(c); |
| 328 | c->decRef(); |
| 329 | } |
Guang Zhu | a8185a6 | 2012-02-07 19:13:28 -0800 | [diff] [blame] | 330 | } |