• 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 #include "host/commands/cvd/server.h"
18 
19 #include <fruit/fruit.h>
20 
21 #include "cvd_server.pb.h"
22 
23 #include "common/libs/fs/shared_buf.h"
24 #include "common/libs/fs/shared_fd.h"
25 #include "common/libs/utils/result.h"
26 #include "host/commands/cvd/instance_manager.h"
27 
28 namespace cuttlefish {
29 namespace {
30 
31 class CvdShutdownHandler : public CvdServerHandler {
32  public:
INJECT(CvdShutdownHandler (CvdServer & server,InstanceManager & instance_manager))33   INJECT(CvdShutdownHandler(CvdServer& server,
34                             InstanceManager& instance_manager))
35       : server_(server), instance_manager_(instance_manager) {}
36 
CanHandle(const RequestWithStdio & request) const37   Result<bool> CanHandle(const RequestWithStdio& request) const override {
38     return request.Message().contents_case() ==
39            cvd::Request::ContentsCase::kShutdownRequest;
40   }
41 
Handle(const RequestWithStdio & request)42   Result<cvd::Response> Handle(const RequestWithStdio& request) override {
43     CF_EXPECT(CanHandle(request));
44     cvd::Response response;
45     response.mutable_shutdown_response();
46 
47     if (!request.Extra()) {
48       response.mutable_status()->set_code(cvd::Status::FAILED_PRECONDITION);
49       response.mutable_status()->set_message(
50           "Missing extra SharedFD for shutdown");
51       return response;
52     }
53 
54     if (request.Message().shutdown_request().clear()) {
55       *response.mutable_status() =
56           instance_manager_.CvdClear(request.Out(), request.Err());
57       if (response.status().code() != cvd::Status::OK) {
58         return response;
59       }
60     }
61 
62     if (instance_manager_.HasInstanceGroups()) {
63       response.mutable_status()->set_code(cvd::Status::FAILED_PRECONDITION);
64       response.mutable_status()->set_message(
65           "Cannot shut down cvd_server while devices are being tracked. "
66           "Try `cvd kill-server`.");
67       return response;
68     }
69 
70     // Intentionally leak the write_pipe fd so that it only closes
71     // when this process fully exits.
72     (*request.Extra())->UNMANAGED_Dup();
73 
74     WriteAll(request.Out(), "Stopping the cvd_server.\n");
75     server_.Stop();
76 
77     response.mutable_status()->set_code(cvd::Status::OK);
78     return response;
79   }
80 
Interrupt()81   Result<void> Interrupt() override { return CF_ERR("Can't interrupt"); }
82 
83  private:
84   CvdServer& server_;
85   InstanceManager& instance_manager_;
86 };
87 
88 }  // namespace
89 
90 fruit::Component<fruit::Required<CvdServer, InstanceManager>>
cvdShutdownComponent()91 cvdShutdownComponent() {
92   return fruit::createComponent()
93       .addMultibinding<CvdServerHandler, CvdShutdownHandler>();
94 }
95 
96 }  // namespace cuttlefish
97