1 /*
2 * Copyright (C) 2023 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/libs/process_monitor/process_monitor_channel.h"
18
19 #include <string>
20
21 #include "common/libs/fs/shared_buf.h"
22 #include "common/libs/fs/shared_fd.h"
23 #include "common/libs/utils/result.h"
24
25 namespace cuttlefish {
26 namespace process_monitor_impl {
27
ParentToChildMessage(const ParentToChildMessageType type)28 ParentToChildMessage::ParentToChildMessage(const ParentToChildMessageType type)
29 : type_(type) {}
30
Write(const SharedFD & fd)31 Result<void> ParentToChildMessage::Write(const SharedFD& fd) {
32 CF_EXPECTF(fd->IsOpen(), "File descriptor to write ParentToChildMessage",
33 " is closed.");
34 const auto n_bytes = WriteAllBinary(fd, &type_);
35 std::string err_msg("Failed to communicate with monitor socket");
36 CF_EXPECTF(n_bytes == sizeof(type_),
37 "{} : {}. Expected to write {} bytes but wrote {} bytes.", err_msg,
38 fd->StrError(), sizeof(type_), n_bytes);
39 return {};
40 }
41
Read(const SharedFD & fd)42 Result<ParentToChildMessage> ParentToChildMessage::Read(const SharedFD& fd) {
43 ParentToChildMessageType type = ParentToChildMessageType::kError;
44 CF_EXPECTF(fd->IsOpen(), "File descriptor to read ParentToChildMessage",
45 "from is closed.");
46 std::string err_msg("Could not read message from parent");
47 const auto n_bytes = ReadExactBinary(fd, &type);
48 CF_EXPECTF(n_bytes == sizeof(type),
49 "{} : {}. Expected To read {} bytes but actually read {} bytes",
50 err_msg, fd->StrError(), sizeof(type), n_bytes);
51 return ParentToChildMessage{type};
52 }
53
ChildToParentResponse(const ChildToParentResponseType type)54 ChildToParentResponse::ChildToParentResponse(
55 const ChildToParentResponseType type)
56 : type_(type) {}
57
Write(const SharedFD & fd)58 Result<void> ChildToParentResponse::Write(const SharedFD& fd) {
59 CF_EXPECTF(fd->IsOpen(), "File descriptor to write ChildToParentResponse",
60 " is closed.");
61 const auto n_bytes = WriteAllBinary(fd, &type_);
62 std::string err_msg("Failed to communicate with monitor socket");
63 CF_EXPECTF(n_bytes == sizeof(type_),
64 "{} : {}. Expected to write {} bytes but wrote {} bytes.", err_msg,
65 fd->StrError(), sizeof(type_), n_bytes);
66 return {};
67 }
68
Read(const SharedFD & fd)69 Result<ChildToParentResponse> ChildToParentResponse::Read(const SharedFD& fd) {
70 ChildToParentResponseType type = ChildToParentResponseType::kFailure;
71 CF_EXPECTF(fd->IsOpen(), "File descriptor to read ChildToParentResponse",
72 "from is closed.");
73 std::string err_msg("Could not read response from parent");
74 const auto n_bytes = ReadExactBinary(fd, &type);
75 CF_EXPECTF(n_bytes == sizeof(type),
76 "{} : {}. Expected To read {} bytes but actually read {} bytes",
77 err_msg, fd->StrError(), sizeof(type), n_bytes);
78 return ChildToParentResponse{type};
79 }
80
81 } // namespace process_monitor_impl
82 } // namespace cuttlefish
83