SocketListener.cpp revision 168415b822cae1f8b54ef09c41c11a9b97b87f40
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> 27 28#include <cutils/sockets.h> 29 30#include <sysutils/SocketListener.h> 31 32SocketListener::SocketListener(const char *socketName, bool acceptClients) { 33 mAcceptClients = acceptClients; 34 mCsock = -1; 35 mSocketName = socketName; 36 mSock = -1; 37} 38 39SocketListener::SocketListener(int socketFd, bool acceptClients) { 40 mAcceptClients = acceptClients; 41 mCsock = -1; 42 mSocketName = NULL; 43 mSock = socketFd; 44} 45 46int SocketListener::run() { 47 48 if (!mSocketName && mSock == -1) { 49 errno = EINVAL; 50 return -1; 51 } else if (mSocketName) { 52 if ((mSock = android_get_control_socket(mSocketName)) < 0) { 53 LOGE("Obtaining file descriptor socket '%s' failed: %s", 54 mSocketName, strerror(errno)); 55 return -1; 56 } 57 } 58 59 if (mAcceptClients) { 60 if (listen(mSock, 4) < 0) { 61 LOGE("Unable to listen on socket (%s)", strerror(errno)); 62 return -1; 63 } 64 } 65 66 while(1) { 67 fd_set read_fds; 68 struct timeval to; 69 int max = 0; 70 int rc = 0; 71 72 to.tv_sec = 60 * 60; 73 to.tv_usec = 0; 74 75 FD_ZERO(&read_fds); 76 77 if ((mAcceptClients == false) || 78 (mAcceptClients == true && mCsock == -1)) { 79 FD_SET(mSock, &read_fds); 80 max = mSock; 81 } else if (mCsock != -1) { 82 FD_SET(mCsock, &read_fds); 83 max = mCsock; 84 } 85 86 if ((rc = select(max + 1, &read_fds, NULL, NULL, &to)) < 0) { 87 LOGE("select failed (%s)", strerror(errno)); 88 return -errno; 89 } else if (!rc) 90 continue; 91 else if (FD_ISSET(mSock, &read_fds)) { 92 /* 93 * If we're accepting client connections then 94 * accept and gobble the event. Otherwise 95 * pass it on to the handlers. 96 */ 97 if (mAcceptClients) { 98 struct sockaddr addr; 99 socklen_t alen = sizeof(addr); 100 101 if ((mCsock = accept(mSock, &addr, &alen)) < 0) { 102 LOGE("accept failed (%s)", strerror(errno)); 103 return -errno; 104 } 105 LOGD("SocketListener client connection accepted"); 106 } else if (!onDataAvailable(mSock)) { 107 LOGW("SocketListener closing listening socket (Will shut down)"); 108 close(mSock); 109 return -ESHUTDOWN; 110 } 111 } else if ((FD_ISSET(mCsock, &read_fds)) && 112 !onDataAvailable(mCsock)) { 113 /* 114 * Once mCsock == -1, we'll start 115 * accepting connections on mSock again. 116 */ 117 LOGD("SocketListener closing client socket"); 118 close(mCsock); 119 mCsock = -1; 120 } 121 } 122 return 0; 123} 124 125bool SocketListener::onDataAvailable(int socket) { 126 return false; 127} 128