/* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include #include "cvd_server.pb.h" #include "common/libs/fs/epoll.h" #include "common/libs/fs/shared_fd.h" #include "common/libs/utils/result.h" #include "common/libs/utils/subprocess.h" #include "common/libs/utils/unix_sockets.h" #include "host/commands/cvd/epoll_loop.h" #include "host/commands/cvd/instance_manager.h" #include "host/commands/cvd/server_client.h" namespace cuttlefish { class CvdServerHandler { public: virtual ~CvdServerHandler() = default; virtual Result CanHandle(const RequestWithStdio&) const = 0; virtual Result Handle(const RequestWithStdio&) = 0; virtual Result Interrupt() = 0; }; class CvdServer { public: INJECT(CvdServer(EpollPool&, InstanceManager&)); ~CvdServer(); Result StartServer(SharedFD server); void Stop(); void Join(); private: struct OngoingRequest { CvdServerHandler* handler; std::mutex mutex; std::thread::id thread_id; }; Result AcceptClient(EpollEvent); Result HandleMessage(EpollEvent); Result HandleRequest(RequestWithStdio, SharedFD client); Result BestEffortWakeup(); EpollPool& epoll_pool_; InstanceManager& instance_manager_; std::atomic_bool running_ = true; std::mutex ongoing_requests_mutex_; std::set> ongoing_requests_; // TODO(schuffelen): Move this thread pool to another class. std::mutex threads_mutex_; std::vector threads_; }; class CvdCommandHandler : public CvdServerHandler { public: INJECT(CvdCommandHandler(InstanceManager& instance_manager)); Result CanHandle(const RequestWithStdio&) const override; Result Handle(const RequestWithStdio&) override; Result Interrupt() override; private: InstanceManager& instance_manager_; std::optional subprocess_; std::mutex interruptible_; bool interrupted_ = false; }; fruit::Component> cvdCommandComponent(); fruit::Component> cvdShutdownComponent(); fruit::Component<> cvdVersionComponent(); fruit::Component> AcloudCommandComponent(); struct CommandInvocation { std::string command; std::vector arguments; }; CommandInvocation ParseInvocation(const cvd::Request& request); Result CvdServerMain(SharedFD server_fd); } // namespace cuttlefish