• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 
17 #pragma once
18 
19 #include <atomic>
20 #include <map>
21 #include <optional>
22 #include <shared_mutex>
23 #include <string>
24 #include <vector>
25 
26 #include <fruit/fruit.h>
27 
28 #include "cvd_server.pb.h"
29 
30 #include "common/libs/fs/epoll.h"
31 #include "common/libs/fs/shared_fd.h"
32 #include "common/libs/utils/subprocess.h"
33 #include "common/libs/utils/unix_sockets.h"
34 #include "host/commands/cvd/epoll_loop.h"
35 #include "host/commands/cvd/instance_manager.h"
36 #include "host/commands/cvd/logger.h"
37 // including "server_command/subcmd.h" causes cyclic dependency
38 #include "host/commands/cvd/server_command/host_tool_target_manager.h"
39 #include "host/commands/cvd/server_command/server_handler.h"
40 #include "host/libs/config/inject.h"
41 #include "host/libs/web/build_api.h"
42 
43 namespace cuttlefish {
44 
45 struct ServerMainParam {
46   SharedFD internal_server_fd;
47   SharedFD carryover_client_fd;
48   std::optional<SharedFD> memory_carryover_fd;
49   std::unique_ptr<ServerLogger> server_logger;
50   /* scoped logger that carries the stderr of the carried-over
51    * client. The client may have called "cvd restart-server."
52    *
53    * The scoped_logger should expire just after AcceptCarryoverClient()
54    */
55   std::unique_ptr<ServerLogger::ScopedLogger> scoped_logger;
56 };
57 Result<int> CvdServerMain(ServerMainParam&& fds);
58 
59 class CvdServer {
60   // for server_logger_.
61   // server_logger_ shouldn't be exposed to anything but CvdServerMain()
62   friend Result<int> CvdServerMain(ServerMainParam&& fds);
63 
64  public:
65   INJECT(CvdServer(BuildApi&, EpollPool&, InstanceManager&,
66                    HostToolTargetManager&, ServerLogger&));
67   ~CvdServer();
68 
69   Result<void> StartServer(SharedFD server);
70   struct ExecParam {
71     SharedFD new_exe;
72     SharedFD carryover_client_fd;  // the client that called cvd restart-server
73     std::optional<SharedFD>
74         in_memory_data_fd;  // fd to carry over in-memory data
75     SharedFD client_stderr_fd;
76     bool verbose;
77   };
78   Result<void> Exec(const ExecParam&);
79   Result<void> AcceptCarryoverClient(
80       SharedFD client,
81       std::unique_ptr<ServerLogger::ScopedLogger> scoped_logger);
82   void Stop();
83   void Join();
84   Result<void> InstanceDbFromJson(const std::string& json_string);
85 
86  private:
87   struct OngoingRequest {
88     CvdServerHandler* handler;
89     std::mutex mutex;
90     std::thread::id thread_id;
91   };
92 
93   /* this has to be static due to the way fruit includes components */
94   static fruit::Component<> RequestComponent(CvdServer*);
95 
96   Result<void> AcceptClient(EpollEvent);
97   Result<void> HandleMessage(EpollEvent);
98   Result<cvd::Response> HandleRequest(RequestWithStdio, SharedFD client);
99   Result<void> BestEffortWakeup();
100 
101   SharedFD server_fd_;
102   BuildApi& build_api_;
103   EpollPool& epoll_pool_;
104   InstanceManager& instance_manager_;
105   HostToolTargetManager& host_tool_target_manager_;
106   ServerLogger& server_logger_;
107   std::atomic_bool running_ = true;
108 
109   std::mutex ongoing_requests_mutex_;
110   std::set<std::shared_ptr<OngoingRequest>> ongoing_requests_;
111   // TODO(schuffelen): Move this thread pool to another class.
112   std::mutex threads_mutex_;
113   std::vector<std::thread> threads_;
114 
115   // translator optout
116   std::atomic<bool> optout_;
117 };
118 
119 Result<CvdServerHandler*> RequestHandler(
120     const RequestWithStdio& request,
121     const std::vector<CvdServerHandler*>& handlers);
122 
123 // Read all contents from the file
124 Result<std::string> ReadAllFromMemFd(const SharedFD& mem_fd);
125 
126 }  // namespace cuttlefish
127