1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef APPSPAWN_SOCKET_SERVER_H 17 #define APPSPAWN_SOCKET_SERVER_H 18 19 #include <mutex> 20 #include <sys/stat.h> 21 #include <unistd.h> 22 #include <vector> 23 24 #include "appspawn_socket.h" 25 #include "nocopyable.h" 26 27 namespace OHOS { 28 namespace AppSpawn { 29 class ServerSocket : public AppSpawnSocket { 30 public: 31 /** 32 * Constructor used to create a ServerSocket 33 */ 34 explicit ServerSocket(const std::string &server); 35 36 /** 37 * Destructor used to destory a ServerSocket 38 */ 39 virtual ~ServerSocket(); 40 41 /** 42 * Disables copying and moving for ServerSocket. 43 */ 44 DISALLOW_COPY_AND_MOVE(ServerSocket); 45 46 /** 47 * Closes a socket connection. 48 * 49 * @param connectFd Indicates the connection's file descriptor (FD). 50 */ 51 virtual void CloseConnection(int connectFd); 52 53 /** 54 * Saves the connection's FD. 55 * 56 * @param connectFd Indicates the connection's file descriptor (FD). 57 */ 58 virtual void SaveConnection(int connectFd); 59 60 /** 61 * Closes a server socket. 62 */ 63 virtual void CloseServer(); 64 65 /** 66 * Closes the server monitor. 67 */ 68 virtual void CloseServerMonitor(); 69 70 /** 71 * Creates server socket, binds socket and listens it. 72 * 73 * @return -1:failed; 0:success 74 */ 75 virtual int RegisterServerSocket(); 76 77 /** 78 * Sets socket option and waits for connection. 79 * 80 * @return -1:failed;other means connection FD. 81 */ 82 virtual int WaitForConnection(); 83 84 /** 85 * Verifies the connection's FD. 86 * 87 * @return -1:failed; 0:success 88 */ 89 virtual int VerifyConnection(int connectFd); 90 91 /** 92 * Uses functions of the parent class. 93 */ 94 using AppSpawnSocket::CloseSocket; 95 using AppSpawnSocket::CreateSocket; 96 using AppSpawnSocket::GetSocketFd; 97 using AppSpawnSocket::ReadSocketMessage; 98 using AppSpawnSocket::WriteSocketMessage; 99 100 static constexpr uid_t APPSPAWN_ID_ROOT = 0; // chown owner 101 static constexpr gid_t APPSPAWN_GROUP_ID = 4000; // chown group 102 static constexpr mode_t SOCKET_PERM = 0660; // root system can read and write appspawn socket 103 104 private: 105 /** 106 * Binds a socket and sets socket attributes. 107 * 108 * @param connectFd Indicates the connection's FD. 109 */ 110 int BindSocket(int connectFd); 111 112 /** 113 * Creates a socket and binds it. 114 * 115 * @param connectFd Indicates the connection's FD. 116 */ 117 int RegisterServerSocket(int &connectFd); 118 119 /** 120 * Accepts a socket. 121 * 122 * @param connectFd Indicates the connection's FD. 123 */ 124 int WaitForConnection(int connectFd); 125 126 private: 127 std::vector<int> connectFds_ = {}; 128 std::mutex mutexConnect_; 129 }; 130 } // namespace AppSpawn 131 } // namespace OHOS 132 #endif 133