| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/cache_transport.cc |
| Date: | 2026-04-26 02:35:59 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 328 | 372 | 88.2% |
| Branches: | 168 | 228 | 73.7% |
| 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 | 313765 | cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() { | |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313765 times.
|
313765 | assert(msg_typed_ != NULL); |
| 36 |
1/2✓ Branch 0 taken 313765 times.
✗ Branch 1 not taken.
|
313765 | if (!is_wrapped_) |
| 37 | 313765 | WrapMsg(); | |
| 38 | 313765 | 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 | 392521 | google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() { | |
| 47 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 392521 times.
|
392521 | assert(msg_rpc_.IsInitialized()); |
| 48 |
2/2✓ Branch 0 taken 332540 times.
✓ Branch 1 taken 59981 times.
|
392521 | if (msg_typed_ == NULL) |
| 49 | 332540 | UnwrapMsg(); | |
| 50 | 392482 | return msg_typed_; | |
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | 392560 | CacheTransport::Frame::Frame() | |
| 55 | 392560 | : owns_msg_typed_(false) | |
| 56 | 392560 | , msg_typed_(NULL) | |
| 57 | 392560 | , attachment_(NULL) | |
| 58 | 392560 | , att_size_(0) | |
| 59 | 392560 | , is_wrapped_(false) | |
| 60 | 392560 | , is_msg_out_of_band_(false) { } | |
| 61 | |||
| 62 | |||
| 63 | 313765 | CacheTransport::Frame::Frame(google::protobuf::MessageLite *m) | |
| 64 | 313726 | : owns_msg_typed_(false) | |
| 65 | 313726 | , msg_typed_(m) | |
| 66 | 313726 | , attachment_(NULL) | |
| 67 | 313726 | , att_size_(0) | |
| 68 | 313726 | , is_wrapped_(false) | |
| 69 | 313765 | , is_msg_out_of_band_(false) { } | |
| 70 | |||
| 71 | |||
| 72 | 706325 | CacheTransport::Frame::~Frame() { Reset(0); } | |
| 73 | |||
| 74 | |||
| 75 | 59981 | bool CacheTransport::Frame::IsMsgOutOfBand() { | |
| 76 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 59981 times.
|
59981 | assert(msg_rpc_.IsInitialized()); |
| 77 |
1/2✓ Branch 0 taken 59981 times.
✗ Branch 1 not taken.
|
59981 | if (msg_typed_ == NULL) |
| 78 | 59981 | UnwrapMsg(); | |
| 79 | 59981 | return is_msg_out_of_band_; | |
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | 79092 | void CacheTransport::Frame::MergeFrom(const Frame &other) { | |
| 84 | 79092 | msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_); | |
| 85 | 79092 | owns_msg_typed_ = true; | |
| 86 |
2/2✓ Branch 0 taken 70200 times.
✓ Branch 1 taken 8892 times.
|
79092 | if (other.att_size_ > 0) { |
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 70200 times.
|
70200 | assert(att_size_ >= other.att_size_); |
| 88 | 70200 | memcpy(attachment_, other.attachment_, other.att_size_); | |
| 89 | 70200 | att_size_ = other.att_size_; | |
| 90 | } | ||
| 91 | 79092 | } | |
| 92 | |||
| 93 | |||
| 94 | 313429 | bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) { | |
| 95 | 313429 | const bool retval = msg_rpc_.ParseFromArray(buffer, size); | |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313390 times.
|
313390 | if (!retval) |
| 97 | ✗ | return false; | |
| 98 | |||
| 99 | // Cleanup typed message when Frame leaves scope | ||
| 100 | 313390 | owns_msg_typed_ = true; | |
| 101 | 313390 | return true; | |
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | 706364 | void CacheTransport::Frame::Release() { | |
| 106 |
2/2✓ Branch 0 taken 392560 times.
✓ Branch 1 taken 313804 times.
|
706364 | if (owns_msg_typed_) |
| 107 | 392560 | return; | |
| 108 | |||
| 109 | 313804 | msg_rpc_.release_msg_refcount_req(); | |
| 110 | 313804 | msg_rpc_.release_msg_refcount_reply(); | |
| 111 | 313804 | msg_rpc_.release_msg_read_req(); | |
| 112 | 313804 | msg_rpc_.release_msg_read_reply(); | |
| 113 | 313804 | msg_rpc_.release_msg_object_info_req(); | |
| 114 | 313804 | msg_rpc_.release_msg_object_info_reply(); | |
| 115 | 313804 | msg_rpc_.release_msg_store_req(); | |
| 116 | 313804 | msg_rpc_.release_msg_store_abort_req(); | |
| 117 | 313804 | msg_rpc_.release_msg_store_reply(); | |
| 118 | 313804 | msg_rpc_.release_msg_handshake(); | |
| 119 | 313804 | msg_rpc_.release_msg_handshake_ack(); | |
| 120 | 313804 | msg_rpc_.release_msg_quit(); | |
| 121 | 313804 | msg_rpc_.release_msg_ioctl(); | |
| 122 | 313804 | msg_rpc_.release_msg_info_req(); | |
| 123 | 313804 | msg_rpc_.release_msg_info_reply(); | |
| 124 | 313765 | msg_rpc_.release_msg_shrink_req(); | |
| 125 | 313765 | msg_rpc_.release_msg_shrink_reply(); | |
| 126 | 313765 | msg_rpc_.release_msg_list_req(); | |
| 127 | 313804 | msg_rpc_.release_msg_list_reply(); | |
| 128 | 313804 | msg_rpc_.release_msg_detach(); | |
| 129 | 313804 | msg_rpc_.release_msg_breadcrumb_store_req(); | |
| 130 | 313804 | msg_rpc_.release_msg_breadcrumb_load_req(); | |
| 131 | 313804 | msg_rpc_.release_msg_breadcrumb_reply(); | |
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | 706325 | void CacheTransport::Frame::Reset(uint32_t original_att_size) { | |
| 136 | 706325 | msg_typed_ = NULL; | |
| 137 | 706325 | att_size_ = original_att_size; | |
| 138 | 706325 | is_wrapped_ = false; | |
| 139 | 706325 | is_msg_out_of_band_ = false; | |
| 140 | 706325 | Release(); | |
| 141 | 706286 | msg_rpc_.Clear(); | |
| 142 | 706325 | owns_msg_typed_ = false; | |
| 143 | 706325 | } | |
| 144 | |||
| 145 | |||
| 146 | 313765 | void CacheTransport::Frame::WrapMsg() { | |
| 147 |
2/2✓ Branch 3 taken 728 times.
✓ Branch 4 taken 313037 times.
|
313765 | if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") { |
| 148 | 728 | msg_rpc_.set_allocated_msg_handshake( | |
| 149 | 728 | reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_)); | |
| 150 |
2/2✓ Branch 3 taken 702 times.
✓ Branch 4 taken 312335 times.
|
313037 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") { |
| 151 | 702 | msg_rpc_.set_allocated_msg_handshake_ack( | |
| 152 | 702 | reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_)); | |
| 153 |
2/2✓ Branch 3 taken 726 times.
✓ Branch 4 taken 311609 times.
|
312335 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") { |
| 154 | 726 | msg_rpc_.set_allocated_msg_quit( | |
| 155 | 726 | reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_)); | |
| 156 |
2/2✓ Branch 3 taken 156 times.
✓ Branch 4 taken 311453 times.
|
311609 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") { |
| 157 | 156 | msg_rpc_.set_allocated_msg_ioctl( | |
| 158 | 156 | reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_)); | |
| 159 |
2/2✓ Branch 3 taken 24500 times.
✓ Branch 4 taken 286953 times.
|
311453 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") { |
| 160 | 24500 | msg_rpc_.set_allocated_msg_refcount_req( | |
| 161 | 24500 | reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_)); | |
| 162 |
2/2✓ Branch 3 taken 21996 times.
✓ Branch 4 taken 264957 times.
|
286953 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") { |
| 163 | 21996 | msg_rpc_.set_allocated_msg_refcount_reply( | |
| 164 | 21996 | reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_)); | |
| 165 |
2/2✓ Branch 3 taken 648 times.
✓ Branch 4 taken 264309 times.
|
264957 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") { |
| 166 | 648 | msg_rpc_.set_allocated_msg_object_info_req( | |
| 167 | 648 | reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_)); | |
| 168 |
2/2✓ Branch 3 taken 624 times.
✓ Branch 4 taken 263685 times.
|
264309 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") { |
| 169 | 624 | msg_rpc_.set_allocated_msg_object_info_reply( | |
| 170 | 624 | reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_)); | |
| 171 |
2/2✓ Branch 3 taken 86934 times.
✓ Branch 4 taken 176751 times.
|
263685 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") { |
| 172 | 86934 | msg_rpc_.set_allocated_msg_read_req( | |
| 173 | 86934 | reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_)); | |
| 174 |
2/2✓ Branch 3 taken 86229 times.
✓ Branch 4 taken 90522 times.
|
176751 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") { |
| 175 | 86229 | msg_rpc_.set_allocated_msg_read_reply( | |
| 176 | 86229 | reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_)); | |
| 177 |
2/2✓ Branch 3 taken 26126 times.
✓ Branch 4 taken 64396 times.
|
90522 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") { |
| 178 | 26126 | msg_rpc_.set_allocated_msg_store_req( | |
| 179 | 26126 | reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_)); | |
| 180 |
2/2✓ Branch 3 taken 39 times.
✓ Branch 4 taken 64357 times.
|
64396 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") { |
| 181 | 39 | msg_rpc_.set_allocated_msg_store_abort_req( | |
| 182 | 39 | reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_)); | |
| 183 |
2/2✓ Branch 3 taken 23751 times.
✓ Branch 4 taken 40606 times.
|
64357 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") { |
| 184 | 23751 | msg_rpc_.set_allocated_msg_store_reply( | |
| 185 | 23751 | reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_)); | |
| 186 |
2/2✓ Branch 3 taken 180 times.
✓ Branch 4 taken 40426 times.
|
40606 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") { |
| 187 | 180 | msg_rpc_.set_allocated_msg_info_req( | |
| 188 | 180 | reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_)); | |
| 189 |
2/2✓ Branch 3 taken 156 times.
✓ Branch 4 taken 40270 times.
|
40426 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") { |
| 190 | 156 | msg_rpc_.set_allocated_msg_info_reply( | |
| 191 | 156 | reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_)); | |
| 192 |
2/2✓ Branch 3 taken 86 times.
✓ Branch 4 taken 40184 times.
|
40270 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") { |
| 193 | 86 | msg_rpc_.set_allocated_msg_shrink_req( | |
| 194 | 86 | reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_)); | |
| 195 |
2/2✓ Branch 3 taken 78 times.
✓ Branch 4 taken 40106 times.
|
40184 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") { |
| 196 | 78 | msg_rpc_.set_allocated_msg_shrink_reply( | |
| 197 | 78 | reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_)); | |
| 198 |
2/2✓ Branch 3 taken 398 times.
✓ Branch 4 taken 39708 times.
|
40106 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") { |
| 199 | 398 | msg_rpc_.set_allocated_msg_list_req( | |
| 200 | 398 | reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_)); | |
| 201 |
2/2✓ Branch 3 taken 390 times.
✓ Branch 4 taken 39318 times.
|
39708 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") { |
| 202 | 390 | msg_rpc_.set_allocated_msg_list_reply( | |
| 203 | 390 | reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_)); | |
| 204 |
2/2✓ Branch 3 taken 41 times.
✓ Branch 4 taken 39277 times.
|
39318 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") { |
| 205 | 41 | msg_rpc_.set_allocated_msg_breadcrumb_store_req( | |
| 206 | 41 | reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_)); | |
| 207 |
2/2✓ Branch 3 taken 82 times.
✓ Branch 4 taken 39195 times.
|
39277 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") { |
| 208 | 82 | msg_rpc_.set_allocated_msg_breadcrumb_load_req( | |
| 209 | 82 | reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_)); | |
| 210 |
2/2✓ Branch 3 taken 117 times.
✓ Branch 4 taken 39078 times.
|
39195 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") { |
| 211 | 117 | msg_rpc_.set_allocated_msg_breadcrumb_reply( | |
| 212 | 117 | reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_)); | |
| 213 |
1/2✓ Branch 3 taken 39078 times.
✗ Branch 4 not taken.
|
39078 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") { |
| 214 | 39078 | msg_rpc_.set_allocated_msg_detach( | |
| 215 | 39078 | reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_)); | |
| 216 | 39078 | is_msg_out_of_band_ = true; | |
| 217 | } else { | ||
| 218 | // Unexpected message type, should never happen | ||
| 219 | ✗ | PANIC(NULL); | |
| 220 | } | ||
| 221 | 313765 | is_wrapped_ = true; | |
| 222 | 313765 | } | |
| 223 | |||
| 224 | |||
| 225 | 392521 | void CacheTransport::Frame::UnwrapMsg() { | |
| 226 |
2/2✓ Branch 1 taken 702 times.
✓ Branch 2 taken 391741 times.
|
392521 | if (msg_rpc_.has_msg_handshake()) { |
| 227 | 702 | msg_typed_ = msg_rpc_.mutable_msg_handshake(); | |
| 228 |
2/2✓ Branch 1 taken 728 times.
✓ Branch 2 taken 391052 times.
|
391741 | } else if (msg_rpc_.has_msg_handshake_ack()) { |
| 229 | 728 | msg_typed_ = msg_rpc_.mutable_msg_handshake_ack(); | |
| 230 |
2/2✓ Branch 1 taken 468 times.
✓ Branch 2 taken 390623 times.
|
391052 | } else if (msg_rpc_.has_msg_quit()) { |
| 231 | 468 | msg_typed_ = msg_rpc_.mutable_msg_quit(); | |
| 232 |
2/2✓ Branch 1 taken 156 times.
✓ Branch 2 taken 390428 times.
|
390623 | } else if (msg_rpc_.has_msg_ioctl()) { |
| 233 | 156 | msg_typed_ = msg_rpc_.mutable_msg_ioctl(); | |
| 234 |
2/2✓ Branch 1 taken 21996 times.
✓ Branch 2 taken 368393 times.
|
390428 | } else if (msg_rpc_.has_msg_refcount_req()) { |
| 235 | 21996 | msg_typed_ = msg_rpc_.mutable_msg_refcount_req(); | |
| 236 |
2/2✓ Branch 1 taken 25241 times.
✓ Branch 2 taken 343113 times.
|
368393 | } else if (msg_rpc_.has_msg_refcount_reply()) { |
| 237 | 25241 | msg_typed_ = msg_rpc_.mutable_msg_refcount_reply(); | |
| 238 |
2/2✓ Branch 1 taken 624 times.
✓ Branch 2 taken 342489 times.
|
343113 | } else if (msg_rpc_.has_msg_object_info_req()) { |
| 239 | 624 | msg_typed_ = msg_rpc_.mutable_msg_object_info_req(); | |
| 240 |
2/2✓ Branch 1 taken 999 times.
✓ Branch 2 taken 341451 times.
|
342489 | } else if (msg_rpc_.has_msg_object_info_reply()) { |
| 241 | 999 | msg_typed_ = msg_rpc_.mutable_msg_object_info_reply(); | |
| 242 |
2/2✓ Branch 1 taken 86229 times.
✓ Branch 2 taken 255261 times.
|
341451 | } else if (msg_rpc_.has_msg_read_req()) { |
| 243 | 86229 | msg_typed_ = msg_rpc_.mutable_msg_read_req(); | |
| 244 |
2/2✓ Branch 1 taken 157134 times.
✓ Branch 2 taken 98283 times.
|
255261 | } else if (msg_rpc_.has_msg_read_reply()) { |
| 245 | 157134 | msg_typed_ = msg_rpc_.mutable_msg_read_reply(); | |
| 246 |
2/2✓ Branch 1 taken 23712 times.
✓ Branch 2 taken 74571 times.
|
98283 | } else if (msg_rpc_.has_msg_store_req()) { |
| 247 | 23712 | msg_typed_ = msg_rpc_.mutable_msg_store_req(); | |
| 248 |
2/2✓ Branch 1 taken 39 times.
✓ Branch 2 taken 74532 times.
|
74571 | } else if (msg_rpc_.has_msg_store_abort_req()) { |
| 249 | 39 | msg_typed_ = msg_rpc_.mutable_msg_store_abort_req(); | |
| 250 |
2/2✓ Branch 1 taken 33965 times.
✓ Branch 2 taken 40567 times.
|
74532 | } else if (msg_rpc_.has_msg_store_reply()) { |
| 251 | 33965 | msg_typed_ = msg_rpc_.mutable_msg_store_reply(); | |
| 252 |
2/2✓ Branch 1 taken 156 times.
✓ Branch 2 taken 40411 times.
|
40567 | } else if (msg_rpc_.has_msg_info_req()) { |
| 253 | 156 | msg_typed_ = msg_rpc_.mutable_msg_info_req(); | |
| 254 |
2/2✓ Branch 1 taken 180 times.
✓ Branch 2 taken 40231 times.
|
40411 | } else if (msg_rpc_.has_msg_info_reply()) { |
| 255 | 180 | msg_typed_ = msg_rpc_.mutable_msg_info_reply(); | |
| 256 |
2/2✓ Branch 1 taken 78 times.
✓ Branch 2 taken 40153 times.
|
40231 | } else if (msg_rpc_.has_msg_shrink_req()) { |
| 257 | 78 | msg_typed_ = msg_rpc_.mutable_msg_shrink_req(); | |
| 258 |
2/2✓ Branch 1 taken 86 times.
✓ Branch 2 taken 40067 times.
|
40153 | } else if (msg_rpc_.has_msg_shrink_reply()) { |
| 259 | 86 | msg_typed_ = msg_rpc_.mutable_msg_shrink_reply(); | |
| 260 |
2/2✓ Branch 1 taken 390 times.
✓ Branch 2 taken 39677 times.
|
40067 | } else if (msg_rpc_.has_msg_list_req()) { |
| 261 | 390 | msg_typed_ = msg_rpc_.mutable_msg_list_req(); | |
| 262 |
2/2✓ Branch 1 taken 398 times.
✓ Branch 2 taken 39279 times.
|
39677 | } else if (msg_rpc_.has_msg_list_reply()) { |
| 263 | 398 | msg_typed_ = msg_rpc_.mutable_msg_list_reply(); | |
| 264 |
2/2✓ Branch 1 taken 39 times.
✓ Branch 2 taken 39240 times.
|
39279 | } else if (msg_rpc_.has_msg_breadcrumb_store_req()) { |
| 265 | 39 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req(); | |
| 266 |
2/2✓ Branch 1 taken 78 times.
✓ Branch 2 taken 39162 times.
|
39240 | } else if (msg_rpc_.has_msg_breadcrumb_load_req()) { |
| 267 | 78 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req(); | |
| 268 |
2/2✓ Branch 1 taken 123 times.
✓ Branch 2 taken 39039 times.
|
39162 | } else if (msg_rpc_.has_msg_breadcrumb_reply()) { |
| 269 | 123 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply(); | |
| 270 |
1/2✓ Branch 1 taken 39039 times.
✗ Branch 2 not taken.
|
39039 | } else if (msg_rpc_.has_msg_detach()) { |
| 271 | 39039 | msg_typed_ = msg_rpc_.mutable_msg_detach(); | |
| 272 | 39039 | is_msg_out_of_band_ = true; | |
| 273 | } else { | ||
| 274 | // Unexpected message type, should never happen | ||
| 275 | ✗ | PANIC(NULL); | |
| 276 | } | ||
| 277 | 392560 | } | |
| 278 | |||
| 279 | |||
| 280 | //------------------------------------------------------------------------------ | ||
| 281 | |||
| 282 | |||
| 283 | 728 | CacheTransport::CacheTransport(int fd_connection) | |
| 284 | 728 | : fd_connection_(fd_connection), flags_(0) { | |
| 285 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
|
728 | assert(fd_connection_ >= 0); |
| 286 | 728 | } | |
| 287 | |||
| 288 | |||
| 289 | 173745 | CacheTransport::CacheTransport(int fd_connection, uint32_t flags) | |
| 290 | 173745 | : fd_connection_(fd_connection), flags_(flags) { | |
| 291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 173745 times.
|
173745 | assert(fd_connection_ >= 0); |
| 292 | 173745 | } | |
| 293 | |||
| 294 | |||
| 295 | 7852321 | void CacheTransport::FillMsgHash(const shash::Any &hash, | |
| 296 | cvmfs::MsgHash *msg_hash) { | ||
| 297 |
3/4✓ Branch 0 taken 7852217 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 92 times.
✗ Branch 3 not taken.
|
7852321 | switch (hash.algorithm) { |
| 298 | 7852217 | case shash::kSha1: | |
| 299 | 7852217 | msg_hash->set_algorithm(cvmfs::HASH_SHA1); | |
| 300 | 7852217 | break; | |
| 301 | 12 | case shash::kRmd160: | |
| 302 | 12 | msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160); | |
| 303 | 12 | break; | |
| 304 | 92 | case shash::kShake128: | |
| 305 | 92 | msg_hash->set_algorithm(cvmfs::HASH_SHAKE128); | |
| 306 | 92 | break; | |
| 307 | ✗ | default: | |
| 308 | ✗ | PANIC(NULL); | |
| 309 | } | ||
| 310 | 7852321 | msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]); | |
| 311 | 7852321 | } | |
| 312 | |||
| 313 | |||
| 314 | 26118 | void CacheTransport::FillObjectType(int object_flags, | |
| 315 | cvmfs::EnumObjectType *wire_type) { | ||
| 316 | 26118 | *wire_type = cvmfs::OBJECT_REGULAR; | |
| 317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26118 times.
|
26118 | if (object_flags & CacheManager::kLabelCatalog) |
| 318 | ✗ | *wire_type = cvmfs::OBJECT_CATALOG; | |
| 319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26118 times.
|
26118 | if (object_flags & CacheManager::kLabelVolatile) |
| 320 | ✗ | *wire_type = cvmfs::OBJECT_VOLATILE; | |
| 321 | 26118 | } | |
| 322 | |||
| 323 | |||
| 324 | 132641 | bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash, | |
| 325 | shash::Any *hash) { | ||
| 326 |
2/4✓ Branch 1 taken 132561 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
✗ Branch 4 not taken.
|
132641 | switch (msg_hash.algorithm()) { |
| 327 | 132561 | case cvmfs::HASH_SHA1: | |
| 328 | 132561 | hash->algorithm = shash::kSha1; | |
| 329 | 132561 | break; | |
| 330 | ✗ | case cvmfs::HASH_RIPEMD160: | |
| 331 | ✗ | hash->algorithm = shash::kRmd160; | |
| 332 | ✗ | break; | |
| 333 | 80 | case cvmfs::HASH_SHAKE128: | |
| 334 | 80 | hash->algorithm = shash::kShake128; | |
| 335 | 80 | break; | |
| 336 | ✗ | default: | |
| 337 | ✗ | return false; | |
| 338 | } | ||
| 339 | 132641 | const unsigned digest_size = shash::kDigestSizes[hash->algorithm]; | |
| 340 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 132641 times.
|
132641 | if (msg_hash.digest().length() != digest_size) |
| 341 | ✗ | return false; | |
| 342 | 132641 | memcpy(hash->digest, msg_hash.digest().data(), digest_size); | |
| 343 | 132641 | 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 | 313507 | bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) { | |
| 366 | uint32_t size; | ||
| 367 | bool has_attachment; | ||
| 368 |
1/2✓ Branch 1 taken 313507 times.
✗ Branch 2 not taken.
|
313507 | bool retval = RecvHeader(&size, &has_attachment); |
| 369 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 313468 times.
|
313507 | if (!retval) |
| 370 | 39 | return false; | |
| 371 | |||
| 372 | void *buffer; | ||
| 373 |
2/2✓ Branch 0 taken 203198 times.
✓ Branch 1 taken 110270 times.
|
313468 | if (size <= kMaxStackAlloc) |
| 374 | 203198 | buffer = alloca(size); | |
| 375 | else | ||
| 376 | 110270 | buffer = smalloc(size); | |
| 377 |
1/2✓ Branch 1 taken 313468 times.
✗ Branch 2 not taken.
|
313468 | const ssize_t nbytes = SafeRead(fd_connection_, buffer, size); |
| 378 |
3/4✓ Branch 0 taken 313429 times.
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 313429 times.
|
313468 | if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) { |
| 379 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (size > kMaxStackAlloc) { |
| 380 | ✗ | free(buffer); | |
| 381 | } | ||
| 382 | 39 | return false; | |
| 383 | } | ||
| 384 | |||
| 385 | 313429 | uint32_t msg_size = size; | |
| 386 |
2/2✓ Branch 0 taken 110562 times.
✓ Branch 1 taken 202867 times.
|
313429 | if (has_attachment) { |
| 387 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 110562 times.
|
110562 | 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 | 110562 | msg_size = (*reinterpret_cast<unsigned char *>(buffer)) | |
| 396 | 110562 | + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8); | |
| 397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 110562 times.
|
110562 | if ((msg_size + kInnerHeaderSize) > size) { |
| 398 | ✗ | if (size > kMaxStackAlloc) { | |
| 399 | ✗ | free(buffer); | |
| 400 | } | ||
| 401 | ✗ | return false; | |
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | 313429 | void *ptr_msg = has_attachment | |
| 406 |
2/2✓ Branch 0 taken 110562 times.
✓ Branch 1 taken 202867 times.
|
313429 | ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize) |
| 407 | : buffer; | ||
| 408 |
1/2✓ Branch 1 taken 313390 times.
✗ Branch 2 not taken.
|
313429 | retval = frame->ParseMsgRpc(ptr_msg, msg_size); |
| 409 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313390 times.
|
313390 | if (!retval) { |
| 410 | ✗ | if (size > kMaxStackAlloc) { | |
| 411 | ✗ | free(buffer); | |
| 412 | } | ||
| 413 | ✗ | return false; | |
| 414 | } | ||
| 415 | |||
| 416 |
2/2✓ Branch 0 taken 110562 times.
✓ Branch 1 taken 202828 times.
|
313390 | if (has_attachment) { |
| 417 | 110562 | const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize); | |
| 418 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 110562 times.
|
110562 | if (frame->att_size() < attachment_size) { |
| 419 | ✗ | if (size > kMaxStackAlloc) { | |
| 420 | ✗ | free(buffer); | |
| 421 | } | ||
| 422 | ✗ | return false; | |
| 423 | } | ||
| 424 | 110562 | void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize | |
| 425 | 110562 | + msg_size; | |
| 426 | 110562 | memcpy(frame->attachment(), ptr_attachment, attachment_size); | |
| 427 | 110562 | frame->set_att_size(attachment_size); | |
| 428 | } else { | ||
| 429 | 202828 | frame->set_att_size(0); | |
| 430 | } | ||
| 431 |
2/2✓ Branch 0 taken 110270 times.
✓ Branch 1 taken 203198 times.
|
313468 | if (size > kMaxStackAlloc) { |
| 432 | 110270 | free(buffer); | |
| 433 | } | ||
| 434 | 313468 | return true; | |
| 435 | } | ||
| 436 | |||
| 437 | |||
| 438 | 313507 | bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) { | |
| 439 | unsigned char header[kHeaderSize]; | ||
| 440 |
1/2✓ Branch 1 taken 313507 times.
✗ Branch 2 not taken.
|
313507 | const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize); |
| 441 |
3/4✓ Branch 0 taken 313507 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 313468 times.
|
313507 | if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize)) |
| 442 | 39 | return false; | |
| 443 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313468 times.
|
313468 | if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion) |
| 444 | ✗ | return false; | |
| 445 | 313468 | *has_attachment = header[0] & kFlagHasAttachment; | |
| 446 | 313468 | *size = header[1] + (header[2] << 8) + (header[3] << 16); | |
| 447 |
2/4✓ Branch 0 taken 313468 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 313468 times.
✗ Branch 3 not taken.
|
313468 | return (*size > 0) && (*size <= kMaxMsgSize); |
| 448 | } | ||
| 449 | |||
| 450 | |||
| 451 | 313765 | void CacheTransport::SendData(void *message, | |
| 452 | uint32_t msg_size, | ||
| 453 | void *attachment, | ||
| 454 | uint32_t att_size) { | ||
| 455 | 627530 | const uint32_t total_size = msg_size + att_size | |
| 456 |
2/2✓ Branch 0 taken 112275 times.
✓ Branch 1 taken 201490 times.
|
313765 | + ((att_size > 0) ? kInnerHeaderSize : 0); |
| 457 | |||
| 458 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313765 times.
|
313765 | assert(total_size > 0); |
| 459 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313765 times.
|
313765 | assert(total_size <= kMaxMsgSize); |
| 460 |
1/2✓ Branch 1 taken 308022 times.
✗ Branch 2 not taken.
|
308022 | 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 201490 times.
✓ Branch 1 taken 112275 times.
|
313765 | header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment); |
| 465 | 313765 | header[1] = (total_size & 0x000000FF); | |
| 466 | 313765 | header[2] = (total_size & 0x0000FF00) >> 8; | |
| 467 | 313765 | 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 | 313765 | iov[0].iov_base = header; | |
| 474 | 313765 | iov[0].iov_len = kHeaderSize; | |
| 475 | |||
| 476 |
2/2✓ Branch 0 taken 112275 times.
✓ Branch 1 taken 201490 times.
|
313765 | if (att_size > 0) { |
| 477 | 112275 | inner_header[0] = (msg_size & 0x000000FF); | |
| 478 | 112275 | inner_header[1] = (msg_size & 0x0000FF00) >> 8; | |
| 479 | 112275 | iov[1].iov_base = inner_header; | |
| 480 | 112275 | iov[1].iov_len = kInnerHeaderSize; | |
| 481 | 112275 | iov[2].iov_base = message; | |
| 482 | 112275 | iov[2].iov_len = msg_size; | |
| 483 | 112275 | iov[3].iov_base = attachment; | |
| 484 | 112275 | iov[3].iov_len = att_size; | |
| 485 | } else { | ||
| 486 | 201490 | iov[1].iov_base = message; | |
| 487 | 201490 | iov[1].iov_len = msg_size; | |
| 488 | } | ||
| 489 |
2/2✓ Branch 0 taken 39078 times.
✓ Branch 1 taken 274687 times.
|
313765 | if (flags_ & kFlagSendNonBlocking) { |
| 490 |
2/4✓ Branch 0 taken 39078 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 39078 times.
✗ Branch 4 not taken.
|
39078 | SendNonBlocking(iov, (att_size == 0) ? 2 : 4); |
| 491 | 39078 | return; | |
| 492 | } | ||
| 493 |
3/4✓ Branch 0 taken 162412 times.
✓ Branch 1 taken 112275 times.
✓ Branch 3 taken 274687 times.
✗ Branch 4 not taken.
|
274687 | const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4); |
| 494 | |||
| 495 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 274687 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
274687 | if (!retval && !(flags_ & kFlagSendIgnoreFailure)) { |
| 496 | ✗ | PANIC(kLogSyslogErr | kLogDebug, | |
| 497 | "failed to write to external cache transport (%d), aborting", errno); | ||
| 498 | } | ||
| 499 | } | ||
| 500 | |||
| 501 | 39078 | void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) { | |
| 502 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39078 times.
|
39078 | assert(iovcnt > 0); |
| 503 | 39078 | unsigned total_size = 0; | |
| 504 |
2/2✓ Branch 0 taken 78156 times.
✓ Branch 1 taken 39078 times.
|
117234 | for (unsigned i = 0; i < iovcnt; ++i) |
| 505 | 78156 | total_size += iov[i].iov_len; | |
| 506 | 39078 | unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size)); | |
| 507 | |||
| 508 | 39078 | unsigned pos = 0; | |
| 509 |
2/2✓ Branch 0 taken 78156 times.
✓ Branch 1 taken 39078 times.
|
117234 | for (unsigned i = 0; i < iovcnt; ++i) { |
| 510 | 78156 | memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len); | |
| 511 | 78156 | pos += iov[i].iov_len; | |
| 512 | } | ||
| 513 | |||
| 514 | 39078 | const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT); | |
| 515 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39078 times.
|
39078 | 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 | 39078 | } | |
| 524 | |||
| 525 | |||
| 526 | 313765 | void CacheTransport::SendFrame(CacheTransport::Frame *frame) { | |
| 527 | 313765 | cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc(); | |
| 528 | 313765 | const int32_t size = msg_rpc->ByteSize(); | |
| 529 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313726 times.
|
313726 | assert(size > 0); |
| 530 | #ifdef __APPLE__ | ||
| 531 | void *buffer = smalloc(size); | ||
| 532 | #else | ||
| 533 | 313726 | void *buffer = alloca(size); | |
| 534 | #endif | ||
| 535 | 313726 | const bool retval = msg_rpc->SerializeToArray(buffer, size); | |
| 536 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 313765 times.
|
313765 | assert(retval); |
| 537 | 313765 | SendData(buffer, size, frame->attachment(), frame->att_size()); | |
| 538 | #ifdef __APPLE__ | ||
| 539 | free(buffer); | ||
| 540 | #endif | ||
| 541 | 313648 | } | |
| 542 |