Home | History | Annotate | Download | only in monitor
      1 /*
      2  * Copyright (C) 2017 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 #pragma once
     17 
     18 #include <string>
     19 
     20 #include "common/libs/fs/shared_fd.h"
     21 #include "common/libs/fs/shared_select.h"
     22 //#include "host/libs/monitor/kernel_log_client.h"
     23 
     24 namespace monitor {
     25 // KernelLogServer manages incoming kernel log connection from QEmu. Only accept
     26 // one connection.
     27 class KernelLogServer {
     28  public:
     29   KernelLogServer(const std::string& socket_name,
     30                   const std::string& log_name,
     31                   bool deprecated_boot_completed);
     32 
     33   ~KernelLogServer() = default;
     34 
     35   // Initialize this instance of Server.
     36   // Returns true, if initialization was successful.
     37   bool Init();
     38 
     39   // BeforeSelect is Called right before Select() to populate interesting
     40   // SharedFDs.
     41   void BeforeSelect(cvd::SharedFDSet* fd_read) const;
     42 
     43   // AfterSelect is Called right after Select() to detect and respond to changes
     44   // on affected SharedFDs.
     45   void AfterSelect(const cvd::SharedFDSet& fd_read);
     46 
     47  private:
     48   // Create kernel log server socket.
     49   // Returns true, if socket was successfully created.
     50   bool CreateServerSocket();
     51 
     52   // Handle new client connection. Only accept one connection.
     53   void HandleIncomingConnection();
     54 
     55   // Respond to message from remote client.
     56   // Returns false, if client disconnected.
     57   bool HandleIncomingMessage();
     58 
     59   std::string name_;
     60   cvd::SharedFD server_fd_;
     61   cvd::SharedFD client_fd_;
     62   cvd::SharedFD log_fd_;
     63   std::string line_;
     64   bool deprecated_boot_completed_;
     65 
     66   KernelLogServer(const KernelLogServer&) = delete;
     67   KernelLogServer& operator=(const KernelLogServer&) = delete;
     68 };
     69 
     70 }  // namespace monitor
     71