Home | History | Annotate | Download | only in kernel_log_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 <stdint.h>
     19 
     20 #include <functional>
     21 #include <string>
     22 #include <vector>
     23 
     24 #include "common/libs/fs/shared_fd.h"
     25 #include "common/libs/fs/shared_select.h"
     26 
     27 namespace monitor {
     28 
     29 enum BootEvent : int32_t {
     30   BootStarted = 0,
     31   BootCompleted = 1,
     32   BootFailed = 2,
     33   WifiNetworkConnected = 3,
     34   MobileNetworkConnected = 4,
     35   AdbdStarted = 5,
     36 };
     37 
     38 enum class SubscriptionAction {
     39   ContinueSubscription,
     40   CancelSubscription,
     41 };
     42 
     43 using BootEventCallback = std::function<SubscriptionAction(BootEvent)>;
     44 
     45 // KernelLogServer manages incoming kernel log connection from QEmu. Only accept
     46 // one connection.
     47 class KernelLogServer {
     48  public:
     49   KernelLogServer(cvd::SharedFD server_socket,
     50                   const std::string& log_name,
     51                   bool deprecated_boot_completed);
     52 
     53   ~KernelLogServer() = default;
     54 
     55   // BeforeSelect is Called right before Select() to populate interesting
     56   // SharedFDs.
     57   void BeforeSelect(cvd::SharedFDSet* fd_read) const;
     58 
     59   // AfterSelect is Called right after Select() to detect and respond to changes
     60   // on affected SharedFDs.
     61   void AfterSelect(const cvd::SharedFDSet& fd_read);
     62 
     63   void SubscribeToBootEvents(BootEventCallback callback);
     64  private:
     65   // Handle new client connection. Only accept one connection.
     66   void HandleIncomingConnection();
     67 
     68   // Respond to message from remote client.
     69   // Returns false, if client disconnected.
     70   bool HandleIncomingMessage();
     71 
     72   cvd::SharedFD server_fd_;
     73   cvd::SharedFD client_fd_;
     74   cvd::SharedFD log_fd_;
     75   std::string line_;
     76   bool deprecated_boot_completed_;
     77   std::vector<BootEventCallback> subscribers_;
     78 
     79   KernelLogServer(const KernelLogServer&) = delete;
     80   KernelLogServer& operator=(const KernelLogServer&) = delete;
     81 };
     82 
     83 }  // namespace monitor
     84