| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/cache_transport.cc |
| Date: | 2026-06-14 02:36:34 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 326 | 372 | 87.6% |
| Branches: | 166 | 228 | 72.8% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "cache_transport.h" | ||
| 6 | |||
| 7 | #include <alloca.h> | ||
| 8 | #include <errno.h> | ||
| 9 | #include <sys/socket.h> | ||
| 10 | |||
| 11 | #include <cassert> | ||
| 12 | #include <cstdlib> | ||
| 13 | #include <cstring> | ||
| 14 | |||
| 15 | #include "cache.h" | ||
| 16 | #include "crypto/hash.h" | ||
| 17 | #include "util/exception.h" | ||
| 18 | #include "util/logging.h" | ||
| 19 | #include "util/posix.h" | ||
| 20 | #include "util/smalloc.h" | ||
| 21 | |||
| 22 | // TODO(jblomer): Check for possible starvation of plugin by dying clients | ||
| 23 | // (blocking read). Probably only relevant for TCP sockets. | ||
| 24 | |||
| 25 | using namespace std; // NOLINT | ||
| 26 | |||
| 27 | const char | ||
| 28 | *CacheTransport::kEnvReadyNotifyFd = "__CVMFS_CACHE_EXTERNAL_PIPE_READY__"; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * Called on the sender side to wrap a message into a MsgRpc message for wire | ||
| 32 | * transfer. | ||
| 33 | */ | ||
| 34 | 368767 | cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() { | |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 368767 times.
|
368767 | assert(msg_typed_ != NULL); |
| 36 |
1/2✓ Branch 0 taken 368767 times.
✗ Branch 1 not taken.
|
368767 | if (!is_wrapped_) |
| 37 | 368767 | WrapMsg(); | |
| 38 | 368905 | return &msg_rpc_; | |
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | /** | ||
| 43 | * Called on the receiving end of an RPC to extract the actual message from the | ||
| 44 | * MsgRpc. | ||
| 45 | */ | ||
| 46 | 462123 | google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() { | |
| 47 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 462031 times.
|
462123 | assert(msg_rpc_.IsInitialized()); |
| 48 |
2/2✓ Branch 0 taken 392314 times.
✓ Branch 1 taken 69717 times.
|
462031 | if (msg_typed_ == NULL) |
| 49 | 392314 | UnwrapMsg(); | |
| 50 | 461617 | return msg_typed_; | |
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | 462215 | CacheTransport::Frame::Frame() | |
| 55 | 462123 | : owns_msg_typed_(false) | |
| 56 | 462123 | , msg_typed_(NULL) | |
| 57 | 462123 | , attachment_(NULL) | |
| 58 | 462123 | , att_size_(0) | |
| 59 | 462123 | , is_wrapped_(false) | |
| 60 | 462215 | , is_msg_out_of_band_(false) { } | |
| 61 | |||
| 62 | |||
| 63 | 369043 | CacheTransport::Frame::Frame(google::protobuf::MessageLite *m) | |
| 64 | 369043 | : owns_msg_typed_(false) | |
| 65 | 369043 | , msg_typed_(m) | |
| 66 | 369043 | , attachment_(NULL) | |
| 67 | 369043 | , att_size_(0) | |
| 68 | 369043 | , is_wrapped_(false) | |
| 69 | 369043 | , is_msg_out_of_band_(false) { } | |
| 70 | |||
| 71 | |||
| 72 | 831258 | CacheTransport::Frame::~Frame() { Reset(0); } | |
| 73 | |||
| 74 | |||
| 75 | 69717 | bool CacheTransport::Frame::IsMsgOutOfBand() { | |
| 76 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 69717 times.
|
69717 | assert(msg_rpc_.IsInitialized()); |
| 77 |
1/2✓ Branch 0 taken 69717 times.
✗ Branch 1 not taken.
|
69717 | if (msg_typed_ == NULL) |
| 78 | 69717 | UnwrapMsg(); | |
| 79 | 69717 | return is_msg_out_of_band_; | |
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | 93288 | void CacheTransport::Frame::MergeFrom(const Frame &other) { | |
| 84 | 93288 | msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_); | |
| 85 | 93288 | owns_msg_typed_ = true; | |
| 86 |
2/2✓ Branch 0 taken 82800 times.
✓ Branch 1 taken 10488 times.
|
93288 | if (other.att_size_ > 0) { |
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 82800 times.
|
82800 | assert(att_size_ >= other.att_size_); |
| 88 | 82800 | memcpy(attachment_, other.attachment_, other.att_size_); | |
| 89 | 82800 | att_size_ = other.att_size_; | |
| 90 | } | ||
| 91 | 93288 | } | |
| 92 | |||
| 93 | |||
| 94 | 368881 | bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) { | |
| 95 | 368881 | const bool retval = msg_rpc_.ParseFromArray(buffer, size); | |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 368927 times.
|
368927 | if (!retval) |
| 97 | ✗ | return false; | |
| 98 | |||
| 99 | // Cleanup typed message when Frame leaves scope | ||
| 100 | 368927 | owns_msg_typed_ = true; | |
| 101 | 368927 | return true; | |
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | 831258 | void CacheTransport::Frame::Release() { | |
| 106 |
2/2✓ Branch 0 taken 462215 times.
✓ Branch 1 taken 369043 times.
|
831258 | if (owns_msg_typed_) |
| 107 | 462215 | return; | |
| 108 | |||
| 109 | 369043 | msg_rpc_.release_msg_refcount_req(); | |
| 110 | 369089 | msg_rpc_.release_msg_refcount_reply(); | |
| 111 | 369043 | msg_rpc_.release_msg_read_req(); | |
| 112 | 369089 | msg_rpc_.release_msg_read_reply(); | |
| 113 | 369089 | msg_rpc_.release_msg_object_info_req(); | |
| 114 | 369089 | msg_rpc_.release_msg_object_info_reply(); | |
| 115 | 369089 | msg_rpc_.release_msg_store_req(); | |
| 116 | 369043 | msg_rpc_.release_msg_store_abort_req(); | |
| 117 | 369043 | msg_rpc_.release_msg_store_reply(); | |
| 118 | 369043 | msg_rpc_.release_msg_handshake(); | |
| 119 | 369043 | msg_rpc_.release_msg_handshake_ack(); | |
| 120 | 369043 | msg_rpc_.release_msg_quit(); | |
| 121 | 369043 | msg_rpc_.release_msg_ioctl(); | |
| 122 | 369043 | msg_rpc_.release_msg_info_req(); | |
| 123 | 369043 | msg_rpc_.release_msg_info_reply(); | |
| 124 | 368997 | msg_rpc_.release_msg_shrink_req(); | |
| 125 | 368997 | msg_rpc_.release_msg_shrink_reply(); | |
| 126 | 368997 | msg_rpc_.release_msg_list_req(); | |
| 127 | 368997 | msg_rpc_.release_msg_list_reply(); | |
| 128 | 368997 | msg_rpc_.release_msg_detach(); | |
| 129 | 368905 | msg_rpc_.release_msg_breadcrumb_store_req(); | |
| 130 | 368951 | msg_rpc_.release_msg_breadcrumb_load_req(); | |
| 131 | 368997 | msg_rpc_.release_msg_breadcrumb_reply(); | |
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | 831258 | void CacheTransport::Frame::Reset(uint32_t original_att_size) { | |
| 136 | 831258 | msg_typed_ = NULL; | |
| 137 | 831258 | att_size_ = original_att_size; | |
| 138 | 831258 | is_wrapped_ = false; | |
| 139 | 831258 | is_msg_out_of_band_ = false; | |
| 140 | 831258 | Release(); | |
| 141 | 831212 | msg_rpc_.Clear(); | |
| 142 | 831074 | owns_msg_typed_ = false; | |
| 143 | 831074 | } | |
| 144 | |||
| 145 | |||
| 146 | 368721 | void CacheTransport::Frame::WrapMsg() { | |
| 147 |
2/2✓ Branch 3 taken 854 times.
✓ Branch 4 taken 368097 times.
|
368721 | if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") { |
| 148 | 854 | msg_rpc_.set_allocated_msg_handshake( | |
| 149 | 854 | reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_)); | |
| 150 |
2/2✓ Branch 3 taken 828 times.
✓ Branch 4 taken 367315 times.
|
368097 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") { |
| 151 | 828 | msg_rpc_.set_allocated_msg_handshake_ack( | |
| 152 | 828 | reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_)); | |
| 153 |
2/2✓ Branch 3 taken 852 times.
✓ Branch 4 taken 366325 times.
|
367315 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") { |
| 154 | 852 | msg_rpc_.set_allocated_msg_quit( | |
| 155 | 852 | reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_)); | |
| 156 |
2/2✓ Branch 3 taken 184 times.
✓ Branch 4 taken 366325 times.
|
366325 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") { |
| 157 | 184 | msg_rpc_.set_allocated_msg_ioctl( | |
| 158 | 184 | reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_)); | |
| 159 |
2/2✓ Branch 3 taken 28448 times.
✓ Branch 4 taken 337785 times.
|
366325 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") { |
| 160 | 28448 | msg_rpc_.set_allocated_msg_refcount_req( | |
| 161 | 28448 | reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_)); | |
| 162 |
2/2✓ Branch 3 taken 25944 times.
✓ Branch 4 taken 311749 times.
|
337785 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") { |
| 163 | 25944 | msg_rpc_.set_allocated_msg_refcount_reply( | |
| 164 | 25944 | reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_)); | |
| 165 |
2/2✓ Branch 3 taken 760 times.
✓ Branch 4 taken 311081 times.
|
311749 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") { |
| 166 | 760 | msg_rpc_.set_allocated_msg_object_info_req( | |
| 167 | 760 | reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_)); | |
| 168 |
2/2✓ Branch 3 taken 736 times.
✓ Branch 4 taken 310391 times.
|
311081 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") { |
| 169 | 736 | msg_rpc_.set_allocated_msg_object_info_reply( | |
| 170 | 736 | reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_)); | |
| 171 |
2/2✓ Branch 3 taken 102403 times.
✓ Branch 4 taken 208034 times.
|
310391 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") { |
| 172 | 102403 | msg_rpc_.set_allocated_msg_read_req( | |
| 173 | 102403 | reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_)); | |
| 174 |
2/2✓ Branch 3 taken 101706 times.
✓ Branch 4 taken 106328 times.
|
208034 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") { |
| 175 | 101706 | msg_rpc_.set_allocated_msg_read_reply( | |
| 176 | 101706 | reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_)); | |
| 177 |
2/2✓ Branch 3 taken 30382 times.
✓ Branch 4 taken 75946 times.
|
106328 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") { |
| 178 | 30382 | msg_rpc_.set_allocated_msg_store_req( | |
| 179 | 30382 | reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_)); | |
| 180 |
2/2✓ Branch 3 taken 46 times.
✓ Branch 4 taken 75900 times.
|
75946 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") { |
| 181 | 46 | msg_rpc_.set_allocated_msg_store_abort_req( | |
| 182 | 46 | reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_)); | |
| 183 |
2/2✓ Branch 3 taken 28014 times.
✓ Branch 4 taken 47886 times.
|
75900 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") { |
| 184 | 28014 | msg_rpc_.set_allocated_msg_store_reply( | |
| 185 | 28014 | reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_)); | |
| 186 |
2/2✓ Branch 3 taken 208 times.
✓ Branch 4 taken 47678 times.
|
47886 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") { |
| 187 | 208 | msg_rpc_.set_allocated_msg_info_req( | |
| 188 | 208 | reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_)); | |
| 189 |
2/2✓ Branch 3 taken 184 times.
✓ Branch 4 taken 47494 times.
|
47678 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") { |
| 190 | 184 | msg_rpc_.set_allocated_msg_info_reply( | |
| 191 | 184 | reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_)); | |
| 192 |
2/2✓ Branch 3 taken 100 times.
✓ Branch 4 taken 47394 times.
|
47494 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") { |
| 193 | 100 | msg_rpc_.set_allocated_msg_shrink_req( | |
| 194 | 100 | reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_)); | |
| 195 |
2/2✓ Branch 3 taken 92 times.
✓ Branch 4 taken 47302 times.
|
47394 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") { |
| 196 | 92 | msg_rpc_.set_allocated_msg_shrink_reply( | |
| 197 | 92 | reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_)); | |
| 198 |
2/2✓ Branch 3 taken 468 times.
✓ Branch 4 taken 46834 times.
|
47302 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") { |
| 199 | 468 | msg_rpc_.set_allocated_msg_list_req( | |
| 200 | 468 | reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_)); | |
| 201 |
2/2✓ Branch 3 taken 460 times.
✓ Branch 4 taken 46374 times.
|
46834 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") { |
| 202 | 460 | msg_rpc_.set_allocated_msg_list_reply( | |
| 203 | 460 | reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_)); | |
| 204 |
2/2✓ Branch 3 taken 48 times.
✓ Branch 4 taken 46326 times.
|
46374 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") { |
| 205 | 48 | msg_rpc_.set_allocated_msg_breadcrumb_store_req( | |
| 206 | 48 | reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_)); | |
| 207 |
2/2✓ Branch 3 taken 96 times.
✓ Branch 4 taken 46230 times.
|
46326 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") { |
| 208 | 96 | msg_rpc_.set_allocated_msg_breadcrumb_load_req( | |
| 209 | 96 | reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_)); | |
| 210 |
2/2✓ Branch 3 taken 138 times.
✓ Branch 4 taken 46092 times.
|
46230 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") { |
| 211 | 138 | msg_rpc_.set_allocated_msg_breadcrumb_reply( | |
| 212 | 138 | reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_)); | |
| 213 |
1/2✓ Branch 3 taken 46092 times.
✗ Branch 4 not taken.
|
46092 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") { |
| 214 | 46092 | msg_rpc_.set_allocated_msg_detach( | |
| 215 | 46092 | reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_)); | |
| 216 | 46092 | is_msg_out_of_band_ = true; | |
| 217 | } else { | ||
| 218 | // Unexpected message type, should never happen | ||
| 219 | ✗ | PANIC(NULL); | |
| 220 | } | ||
| 221 | 369043 | is_wrapped_ = true; | |
| 222 | 369043 | } | |
| 223 | |||
| 224 | |||
| 225 | 462077 | void CacheTransport::Frame::UnwrapMsg() { | |
| 226 |
2/2✓ Branch 1 taken 828 times.
✓ Branch 2 taken 461157 times.
|
462077 | if (msg_rpc_.has_msg_handshake()) { |
| 227 | 828 | msg_typed_ = msg_rpc_.mutable_msg_handshake(); | |
| 228 |
2/2✓ Branch 1 taken 854 times.
✓ Branch 2 taken 460303 times.
|
461157 | } else if (msg_rpc_.has_msg_handshake_ack()) { |
| 229 | 854 | msg_typed_ = msg_rpc_.mutable_msg_handshake_ack(); | |
| 230 |
2/2✓ Branch 1 taken 828 times.
✓ Branch 2 taken 459475 times.
|
460303 | } else if (msg_rpc_.has_msg_quit()) { |
| 231 | 828 | msg_typed_ = msg_rpc_.mutable_msg_quit(); | |
| 232 |
2/2✓ Branch 1 taken 184 times.
✓ Branch 2 taken 459061 times.
|
459475 | } else if (msg_rpc_.has_msg_ioctl()) { |
| 233 | 184 | msg_typed_ = msg_rpc_.mutable_msg_ioctl(); | |
| 234 |
2/2✓ Branch 1 taken 25944 times.
✓ Branch 2 taken 433117 times.
|
459061 | } else if (msg_rpc_.has_msg_refcount_req()) { |
| 235 | 25944 | msg_typed_ = msg_rpc_.mutable_msg_refcount_req(); | |
| 236 |
2/2✓ Branch 1 taken 29322 times.
✓ Branch 2 taken 403749 times.
|
433117 | } else if (msg_rpc_.has_msg_refcount_reply()) { |
| 237 | 29322 | msg_typed_ = msg_rpc_.mutable_msg_refcount_reply(); | |
| 238 |
2/2✓ Branch 1 taken 736 times.
✓ Branch 2 taken 403013 times.
|
403749 | } else if (msg_rpc_.has_msg_object_info_req()) { |
| 239 | 736 | msg_typed_ = msg_rpc_.mutable_msg_object_info_req(); | |
| 240 |
2/2✓ Branch 1 taken 1174 times.
✓ Branch 2 taken 401793 times.
|
403013 | } else if (msg_rpc_.has_msg_object_info_reply()) { |
| 241 | 1174 | msg_typed_ = msg_rpc_.mutable_msg_object_info_reply(); | |
| 242 |
2/2✓ Branch 1 taken 101706 times.
✓ Branch 2 taken 300225 times.
|
401793 | } else if (msg_rpc_.has_msg_read_req()) { |
| 243 | 101706 | msg_typed_ = msg_rpc_.mutable_msg_read_req(); | |
| 244 |
2/2✓ Branch 1 taken 185157 times.
✓ Branch 2 taken 115482 times.
|
300225 | } else if (msg_rpc_.has_msg_read_reply()) { |
| 245 | 185157 | msg_typed_ = msg_rpc_.mutable_msg_read_reply(); | |
| 246 |
2/2✓ Branch 1 taken 27968 times.
✓ Branch 2 taken 87514 times.
|
115482 | } else if (msg_rpc_.has_msg_store_req()) { |
| 247 | 27968 | msg_typed_ = msg_rpc_.mutable_msg_store_req(); | |
| 248 |
2/2✓ Branch 1 taken 46 times.
✓ Branch 2 taken 87468 times.
|
87514 | } else if (msg_rpc_.has_msg_store_abort_req()) { |
| 249 | 46 | msg_typed_ = msg_rpc_.mutable_msg_store_abort_req(); | |
| 250 |
2/2✓ Branch 1 taken 39628 times.
✓ Branch 2 taken 47840 times.
|
87468 | } else if (msg_rpc_.has_msg_store_reply()) { |
| 251 | 39628 | msg_typed_ = msg_rpc_.mutable_msg_store_reply(); | |
| 252 |
2/2✓ Branch 1 taken 184 times.
✓ Branch 2 taken 47656 times.
|
47840 | } else if (msg_rpc_.has_msg_info_req()) { |
| 253 | 184 | msg_typed_ = msg_rpc_.mutable_msg_info_req(); | |
| 254 |
2/2✓ Branch 1 taken 208 times.
✓ Branch 2 taken 47448 times.
|
47656 | } else if (msg_rpc_.has_msg_info_reply()) { |
| 255 | 208 | msg_typed_ = msg_rpc_.mutable_msg_info_reply(); | |
| 256 |
2/2✓ Branch 1 taken 92 times.
✓ Branch 2 taken 47356 times.
|
47448 | } else if (msg_rpc_.has_msg_shrink_req()) { |
| 257 | 92 | msg_typed_ = msg_rpc_.mutable_msg_shrink_req(); | |
| 258 |
2/2✓ Branch 1 taken 100 times.
✓ Branch 2 taken 47256 times.
|
47356 | } else if (msg_rpc_.has_msg_shrink_reply()) { |
| 259 | 100 | msg_typed_ = msg_rpc_.mutable_msg_shrink_reply(); | |
| 260 |
2/2✓ Branch 1 taken 460 times.
✓ Branch 2 taken 46796 times.
|
47256 | } else if (msg_rpc_.has_msg_list_req()) { |
| 261 | 460 | msg_typed_ = msg_rpc_.mutable_msg_list_req(); | |
| 262 |
2/2✓ Branch 1 taken 468 times.
✓ Branch 2 taken 46328 times.
|
46796 | } else if (msg_rpc_.has_msg_list_reply()) { |
| 263 | 468 | msg_typed_ = msg_rpc_.mutable_msg_list_reply(); | |
| 264 |
2/2✓ Branch 1 taken 46 times.
✓ Branch 2 taken 46282 times.
|
46328 | } else if (msg_rpc_.has_msg_breadcrumb_store_req()) { |
| 265 | 46 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req(); | |
| 266 |
2/2✓ Branch 1 taken 92 times.
✓ Branch 2 taken 46190 times.
|
46282 | } else if (msg_rpc_.has_msg_breadcrumb_load_req()) { |
| 267 | 92 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req(); | |
| 268 |
2/2✓ Branch 1 taken 144 times.
✓ Branch 2 taken 46046 times.
|
46190 | } else if (msg_rpc_.has_msg_breadcrumb_reply()) { |
| 269 | 144 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply(); | |
| 270 |
1/2✓ Branch 1 taken 46046 times.
✗ Branch 2 not taken.
|
46046 | } else if (msg_rpc_.has_msg_detach()) { |
| 271 | 46046 | msg_typed_ = msg_rpc_.mutable_msg_detach(); | |
| 272 | 46046 | is_msg_out_of_band_ = true; | |
| 273 | } else { | ||
| 274 | // Unexpected message type, should never happen | ||
| 275 | ✗ | PANIC(NULL); | |
| 276 | } | ||
| 277 | 462215 | } | |
| 278 | |||
| 279 | |||
| 280 | //------------------------------------------------------------------------------ | ||
| 281 | |||
| 282 | |||
| 283 | 854 | CacheTransport::CacheTransport(int fd_connection) | |
| 284 | 854 | : fd_connection_(fd_connection), flags_(0) { | |
| 285 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 854 times.
|
854 | assert(fd_connection_ >= 0); |
| 286 | 854 | } | |
| 287 | |||
| 288 | |||
| 289 | 205206 | CacheTransport::CacheTransport(int fd_connection, uint32_t flags) | |
| 290 | 205206 | : fd_connection_(fd_connection), flags_(flags) { | |
| 291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 205206 times.
|
205206 | assert(fd_connection_ >= 0); |
| 292 | 205206 | } | |
| 293 | |||
| 294 | |||
| 295 | 9260775 | void CacheTransport::FillMsgHash(const shash::Any &hash, | |
| 296 | cvmfs::MsgHash *msg_hash) { | ||
| 297 |
3/4✓ Branch 0 taken 9260657 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 106 times.
✗ Branch 3 not taken.
|
9260775 | switch (hash.algorithm) { |
| 298 | 9260657 | case shash::kSha1: | |
| 299 | 9260657 | msg_hash->set_algorithm(cvmfs::HASH_SHA1); | |
| 300 | 9260657 | break; | |
| 301 | 12 | case shash::kRmd160: | |
| 302 | 12 | msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160); | |
| 303 | 12 | break; | |
| 304 | 106 | case shash::kShake128: | |
| 305 | 106 | msg_hash->set_algorithm(cvmfs::HASH_SHAKE128); | |
| 306 | 106 | break; | |
| 307 | ✗ | default: | |
| 308 | ✗ | PANIC(NULL); | |
| 309 | } | ||
| 310 | 9260775 | msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]); | |
| 311 | 9260775 | } | |
| 312 | |||
| 313 | |||
| 314 | 30374 | void CacheTransport::FillObjectType(int object_flags, | |
| 315 | cvmfs::EnumObjectType *wire_type) { | ||
| 316 | 30374 | *wire_type = cvmfs::OBJECT_REGULAR; | |
| 317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30374 times.
|
30374 | if (object_flags & CacheManager::kLabelCatalog) |
| 318 | ✗ | *wire_type = cvmfs::OBJECT_CATALOG; | |
| 319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30374 times.
|
30374 | if (object_flags & CacheManager::kLabelVolatile) |
| 320 | ✗ | *wire_type = cvmfs::OBJECT_VOLATILE; | |
| 321 | 30374 | } | |
| 322 | |||
| 323 | |||
| 324 | 156448 | bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash, | |
| 325 | shash::Any *hash) { | ||
| 326 |
2/4✓ Branch 1 taken 156354 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 94 times.
✗ Branch 4 not taken.
|
156448 | switch (msg_hash.algorithm()) { |
| 327 | 156354 | case cvmfs::HASH_SHA1: | |
| 328 | 156354 | hash->algorithm = shash::kSha1; | |
| 329 | 156354 | break; | |
| 330 | ✗ | case cvmfs::HASH_RIPEMD160: | |
| 331 | ✗ | hash->algorithm = shash::kRmd160; | |
| 332 | ✗ | break; | |
| 333 | 94 | case cvmfs::HASH_SHAKE128: | |
| 334 | 94 | hash->algorithm = shash::kShake128; | |
| 335 | 94 | break; | |
| 336 | ✗ | default: | |
| 337 | ✗ | return false; | |
| 338 | } | ||
| 339 | 156448 | const unsigned digest_size = shash::kDigestSizes[hash->algorithm]; | |
| 340 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 156448 times.
|
156448 | if (msg_hash.digest().length() != digest_size) |
| 341 | ✗ | return false; | |
| 342 | 156448 | memcpy(hash->digest, msg_hash.digest().data(), digest_size); | |
| 343 | 156448 | return true; | |
| 344 | } | ||
| 345 | |||
| 346 | |||
| 347 | ✗ | bool CacheTransport::ParseObjectType(cvmfs::EnumObjectType wire_type, | |
| 348 | int *object_flags) { | ||
| 349 | ✗ | *object_flags = 0; | |
| 350 | ✗ | switch (wire_type) { | |
| 351 | ✗ | case cvmfs::OBJECT_REGULAR: | |
| 352 | ✗ | return true; | |
| 353 | ✗ | case cvmfs::OBJECT_CATALOG: | |
| 354 | ✗ | *object_flags |= CacheManager::kLabelCatalog; | |
| 355 | ✗ | return true; | |
| 356 | ✗ | case cvmfs::OBJECT_VOLATILE: | |
| 357 | ✗ | *object_flags |= CacheManager::kLabelVolatile; | |
| 358 | ✗ | return true; | |
| 359 | ✗ | default: | |
| 360 | ✗ | return false; | |
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | |||
| 365 | 369019 | bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) { | |
| 366 | uint32_t size; | ||
| 367 | bool has_attachment; | ||
| 368 |
1/2✓ Branch 1 taken 369019 times.
✗ Branch 2 not taken.
|
369019 | bool retval = RecvHeader(&size, &has_attachment); |
| 369 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 368973 times.
|
369019 | if (!retval) |
| 370 | 46 | return false; | |
| 371 | |||
| 372 | void *buffer; | ||
| 373 |
2/2✓ Branch 0 taken 238997 times.
✓ Branch 1 taken 129976 times.
|
368973 | if (size <= kMaxStackAlloc) |
| 374 | 238997 | buffer = alloca(size); | |
| 375 | else | ||
| 376 | 129976 | buffer = smalloc(size); | |
| 377 |
1/2✓ Branch 1 taken 368927 times.
✗ Branch 2 not taken.
|
368973 | const ssize_t nbytes = SafeRead(fd_connection_, buffer, size); |
| 378 |
2/4✓ Branch 0 taken 368927 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 368927 times.
|
368927 | if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) { |
| 379 | ✗ | if (size > kMaxStackAlloc) { | |
| 380 | ✗ | free(buffer); | |
| 381 | } | ||
| 382 | ✗ | return false; | |
| 383 | } | ||
| 384 | |||
| 385 | 368927 | uint32_t msg_size = size; | |
| 386 |
2/2✓ Branch 0 taken 130273 times.
✓ Branch 1 taken 238654 times.
|
368927 | if (has_attachment) { |
| 387 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 130273 times.
|
130273 | if (size < 2) { |
| 388 | // kMaxStackAlloc is > 2 (of course!) but we'll leave the condition here | ||
| 389 | // for consistency. | ||
| 390 | ✗ | if (size > kMaxStackAlloc) { | |
| 391 | ✗ | free(buffer); | |
| 392 | } | ||
| 393 | ✗ | return false; | |
| 394 | } | ||
| 395 | 130273 | msg_size = (*reinterpret_cast<unsigned char *>(buffer)) | |
| 396 | 130273 | + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8); | |
| 397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 130273 times.
|
130273 | if ((msg_size + kInnerHeaderSize) > size) { |
| 398 | ✗ | if (size > kMaxStackAlloc) { | |
| 399 | ✗ | free(buffer); | |
| 400 | } | ||
| 401 | ✗ | return false; | |
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | 368927 | void *ptr_msg = has_attachment | |
| 406 |
2/2✓ Branch 0 taken 130273 times.
✓ Branch 1 taken 238654 times.
|
368927 | ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize) |
| 407 | : buffer; | ||
| 408 |
1/2✓ Branch 1 taken 368927 times.
✗ Branch 2 not taken.
|
368927 | retval = frame->ParseMsgRpc(ptr_msg, msg_size); |
| 409 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 368927 times.
|
368927 | if (!retval) { |
| 410 | ✗ | if (size > kMaxStackAlloc) { | |
| 411 | ✗ | free(buffer); | |
| 412 | } | ||
| 413 | ✗ | return false; | |
| 414 | } | ||
| 415 | |||
| 416 |
2/2✓ Branch 0 taken 130273 times.
✓ Branch 1 taken 238654 times.
|
368927 | if (has_attachment) { |
| 417 | 130273 | const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize); | |
| 418 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 130273 times.
|
130273 | if (frame->att_size() < attachment_size) { |
| 419 | ✗ | if (size > kMaxStackAlloc) { | |
| 420 | ✗ | free(buffer); | |
| 421 | } | ||
| 422 | ✗ | return false; | |
| 423 | } | ||
| 424 | 130273 | void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize | |
| 425 | 130273 | + msg_size; | |
| 426 | 130273 | memcpy(frame->attachment(), ptr_attachment, attachment_size); | |
| 427 | 130273 | frame->set_att_size(attachment_size); | |
| 428 | } else { | ||
| 429 | 238654 | frame->set_att_size(0); | |
| 430 | } | ||
| 431 |
2/2✓ Branch 0 taken 129976 times.
✓ Branch 1 taken 238951 times.
|
368927 | if (size > kMaxStackAlloc) { |
| 432 | 129976 | free(buffer); | |
| 433 | } | ||
| 434 | 368927 | return true; | |
| 435 | } | ||
| 436 | |||
| 437 | |||
| 438 | 369019 | bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) { | |
| 439 | unsigned char header[kHeaderSize]; | ||
| 440 |
1/2✓ Branch 1 taken 369019 times.
✗ Branch 2 not taken.
|
369019 | const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize); |
| 441 |
3/4✓ Branch 0 taken 369019 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 368973 times.
|
369019 | if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize)) |
| 442 | 46 | return false; | |
| 443 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 368973 times.
|
368973 | if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion) |
| 444 | ✗ | return false; | |
| 445 | 368973 | *has_attachment = header[0] & kFlagHasAttachment; | |
| 446 | 368973 | *size = header[1] + (header[2] << 8) + (header[3] << 16); | |
| 447 |
2/4✓ Branch 0 taken 368973 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 368973 times.
✗ Branch 3 not taken.
|
368973 | return (*size > 0) && (*size <= kMaxMsgSize); |
| 448 | } | ||
| 449 | |||
| 450 | |||
| 451 | 368951 | void CacheTransport::SendData(void *message, | |
| 452 | uint32_t msg_size, | ||
| 453 | void *attachment, | ||
| 454 | uint32_t att_size) { | ||
| 455 | 737902 | const uint32_t total_size = msg_size + att_size | |
| 456 |
2/2✓ Branch 0 taken 131994 times.
✓ Branch 1 taken 236957 times.
|
368951 | + ((att_size > 0) ? kInnerHeaderSize : 0); |
| 457 | |||
| 458 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 368951 times.
|
368951 | assert(total_size > 0); |
| 459 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 368951 times.
|
368951 | assert(total_size <= kMaxMsgSize); |
| 460 |
1/2✓ Branch 1 taken 363308 times.
✗ Branch 2 not taken.
|
363216 | LogCvmfs(kLogCache, kLogDebug, |
| 461 | "sending message of size %u to cache transport", total_size); | ||
| 462 | |||
| 463 | unsigned char header[kHeaderSize]; | ||
| 464 |
2/2✓ Branch 0 taken 237049 times.
✓ Branch 1 taken 131994 times.
|
369043 | header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment); |
| 465 | 369043 | header[1] = (total_size & 0x000000FF); | |
| 466 | 369043 | header[2] = (total_size & 0x0000FF00) >> 8; | |
| 467 | 369043 | header[3] = (total_size & 0x00FF0000) >> 16; | |
| 468 | // Only transferred if an attachment is present. Otherwise the overall size | ||
| 469 | // is also the size of the protobuf message. | ||
| 470 | unsigned char inner_header[kInnerHeaderSize]; | ||
| 471 | |||
| 472 | struct iovec iov[4]; | ||
| 473 | 369043 | iov[0].iov_base = header; | |
| 474 | 369043 | iov[0].iov_len = kHeaderSize; | |
| 475 | |||
| 476 |
2/2✓ Branch 0 taken 131994 times.
✓ Branch 1 taken 237049 times.
|
369043 | if (att_size > 0) { |
| 477 | 131994 | inner_header[0] = (msg_size & 0x000000FF); | |
| 478 | 131994 | inner_header[1] = (msg_size & 0x0000FF00) >> 8; | |
| 479 | 131994 | iov[1].iov_base = inner_header; | |
| 480 | 131994 | iov[1].iov_len = kInnerHeaderSize; | |
| 481 | 131994 | iov[2].iov_base = message; | |
| 482 | 131994 | iov[2].iov_len = msg_size; | |
| 483 | 131994 | iov[3].iov_base = attachment; | |
| 484 | 131994 | iov[3].iov_len = att_size; | |
| 485 | } else { | ||
| 486 | 237049 | iov[1].iov_base = message; | |
| 487 | 237049 | iov[1].iov_len = msg_size; | |
| 488 | } | ||
| 489 |
2/2✓ Branch 0 taken 46092 times.
✓ Branch 1 taken 322951 times.
|
369043 | if (flags_ & kFlagSendNonBlocking) { |
| 490 |
2/4✓ Branch 0 taken 46092 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 46092 times.
✗ Branch 4 not taken.
|
46092 | SendNonBlocking(iov, (att_size == 0) ? 2 : 4); |
| 491 | 46092 | return; | |
| 492 | } | ||
| 493 |
3/4✓ Branch 0 taken 190957 times.
✓ Branch 1 taken 131994 times.
✓ Branch 3 taken 322951 times.
✗ Branch 4 not taken.
|
322951 | const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4); |
| 494 | |||
| 495 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 322951 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
322951 | if (!retval && !(flags_ & kFlagSendIgnoreFailure)) { |
| 496 | ✗ | PANIC(kLogSyslogErr | kLogDebug, | |
| 497 | "failed to write to external cache transport (%d), aborting", errno); | ||
| 498 | } | ||
| 499 | } | ||
| 500 | |||
| 501 | 46092 | void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) { | |
| 502 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46092 times.
|
46092 | assert(iovcnt > 0); |
| 503 | 46092 | unsigned total_size = 0; | |
| 504 |
2/2✓ Branch 0 taken 92184 times.
✓ Branch 1 taken 46092 times.
|
138276 | for (unsigned i = 0; i < iovcnt; ++i) |
| 505 | 92184 | total_size += iov[i].iov_len; | |
| 506 | 46092 | unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size)); | |
| 507 | |||
| 508 | 46092 | unsigned pos = 0; | |
| 509 |
2/2✓ Branch 0 taken 92184 times.
✓ Branch 1 taken 46092 times.
|
138276 | for (unsigned i = 0; i < iovcnt; ++i) { |
| 510 | 92184 | memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len); | |
| 511 | 92184 | pos += iov[i].iov_len; | |
| 512 | } | ||
| 513 | |||
| 514 | 46092 | const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT); | |
| 515 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46092 times.
|
46092 | if (retval < 0) { |
| 516 | ✗ | assert(errno != EMSGSIZE); | |
| 517 | ✗ | if (!(flags_ & kFlagSendIgnoreFailure)) { | |
| 518 | ✗ | PANIC(kLogSyslogErr | kLogDebug, | |
| 519 | "failed to write to external cache transport (%d), aborting", | ||
| 520 | errno); | ||
| 521 | } | ||
| 522 | } | ||
| 523 | 46092 | } | |
| 524 | |||
| 525 | |||
| 526 | 368905 | void CacheTransport::SendFrame(CacheTransport::Frame *frame) { | |
| 527 | 368905 | cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc(); | |
| 528 | 368905 | const int32_t size = msg_rpc->ByteSize(); | |
| 529 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 368261 times.
|
368261 | assert(size > 0); |
| 530 | #ifdef __APPLE__ | ||
| 531 | void *buffer = smalloc(size); | ||
| 532 | #else | ||
| 533 | 368261 | void *buffer = alloca(size); | |
| 534 | #endif | ||
| 535 | 368261 | const bool retval = msg_rpc->SerializeToArray(buffer, size); | |
| 536 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 368951 times.
|
368951 | assert(retval); |
| 537 | 368951 | SendData(buffer, size, frame->attachment(), frame->att_size()); | |
| 538 | #ifdef __APPLE__ | ||
| 539 | free(buffer); | ||
| 540 | #endif | ||
| 541 | 369043 | } | |
| 542 |