GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-09-27 02:40:09
Exec Total Coverage
Lines: 327 373 87.7%
Branches: 167 230 72.6%

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 211088 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211088 times.
211088 assert(msg_typed_ != NULL);
36
1/2
✓ Branch 0 taken 211088 times.
✗ Branch 1 not taken.
211088 if (!is_wrapped_)
37 211088 WrapMsg();
38 211088 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 263662 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 263714 times.
263662 assert(msg_rpc_.IsInitialized());
48
2/2
✓ Branch 0 taken 221832 times.
✓ Branch 1 taken 41882 times.
263714 if (msg_typed_ == NULL)
49 221832 UnwrapMsg();
50 263584 return msg_typed_;
51 }
52
53
54 263766 CacheTransport::Frame::Frame()
55 263766 : owns_msg_typed_(false)
56 263766 , msg_typed_(NULL)
57 263766 , attachment_(NULL)
58 263766 , att_size_(0)
59 263766 , is_wrapped_(false)
60 263766 , is_msg_out_of_band_(false) { }
61
62
63 211062 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
64 211062 : owns_msg_typed_(false)
65 211062 , msg_typed_(m)
66 211062 , attachment_(NULL)
67 211062 , att_size_(0)
68 211062 , is_wrapped_(false)
69 211062 , is_msg_out_of_band_(false) { }
70
71
72 474828 CacheTransport::Frame::~Frame() { Reset(0); }
73
74
75 41882 bool CacheTransport::Frame::IsMsgOutOfBand() {
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41882 times.
41882 assert(msg_rpc_.IsInitialized());
77
1/2
✓ Branch 0 taken 41882 times.
✗ Branch 1 not taken.
41882 if (msg_typed_ == NULL)
78 41882 UnwrapMsg();
79 41882 return is_msg_out_of_band_;
80 }
81
82
83 52728 void CacheTransport::Frame::MergeFrom(const Frame &other) {
84 52728 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
85 52728 owns_msg_typed_ = true;
86
2/2
✓ Branch 0 taken 46800 times.
✓ Branch 1 taken 5928 times.
52728 if (other.att_size_ > 0) {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46800 times.
46800 assert(att_size_ >= other.att_size_);
88 46800 memcpy(attachment_, other.attachment_, other.att_size_);
89 46800 att_size_ = other.att_size_;
90 }
91 52728 }
92
93
94 211038 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
95 211038 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211038 times.
211038 if (!retval)
97 ✗ return false;
98
99 // Cleanup typed message when Frame leaves scope
100 211038 owns_msg_typed_ = true;
101 211038 return true;
102 }
103
104
105 474854 void CacheTransport::Frame::Release() {
106
2/2
✓ Branch 0 taken 263766 times.
✓ Branch 1 taken 211088 times.
474854 if (owns_msg_typed_)
107 263766 return;
108
109 211088 (void)msg_rpc_.release_msg_refcount_req();
110 211114 (void)msg_rpc_.release_msg_refcount_reply();
111 211114 (void)msg_rpc_.release_msg_read_req();
112 211114 (void)msg_rpc_.release_msg_read_reply();
113 211114 (void)msg_rpc_.release_msg_object_info_req();
114 211114 (void)msg_rpc_.release_msg_object_info_reply();
115 211114 (void)msg_rpc_.release_msg_store_req();
116 211114 (void)msg_rpc_.release_msg_store_abort_req();
117 211114 (void)msg_rpc_.release_msg_store_reply();
118 211114 (void)msg_rpc_.release_msg_handshake();
119 211114 (void)msg_rpc_.release_msg_handshake_ack();
120 211114 (void)msg_rpc_.release_msg_quit();
121 211114 (void)msg_rpc_.release_msg_ioctl();
122 211114 (void)msg_rpc_.release_msg_info_req();
123 211114 (void)msg_rpc_.release_msg_info_reply();
124 211114 (void)msg_rpc_.release_msg_shrink_req();
125 211114 (void)msg_rpc_.release_msg_shrink_reply();
126 211114 (void)msg_rpc_.release_msg_list_req();
127 211114 (void)msg_rpc_.release_msg_list_reply();
128 211114 (void)msg_rpc_.release_msg_detach();
129 211114 (void)msg_rpc_.release_msg_breadcrumb_store_req();
130 211114 (void)msg_rpc_.release_msg_breadcrumb_load_req();
131 211114 (void)msg_rpc_.release_msg_breadcrumb_reply();
132 }
133
134
135 474854 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
136 474854 msg_typed_ = NULL;
137 474854 att_size_ = original_att_size;
138 474854 is_wrapped_ = false;
139 474854 is_msg_out_of_band_ = false;
140 474854 Release();
141 474880 msg_rpc_.Clear();
142 474854 owns_msg_typed_ = false;
143 474854 }
144
145
146 211088 void CacheTransport::Frame::WrapMsg() {
147
2/2
✓ Branch 3 taken 494 times.
✓ Branch 4 taken 210594 times.
211088 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
148 494 msg_rpc_.set_allocated_msg_handshake(
149 494 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
150
2/2
✓ Branch 3 taken 468 times.
✓ Branch 4 taken 210126 times.
210594 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
151 468 msg_rpc_.set_allocated_msg_handshake_ack(
152 468 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
153
2/2
✓ Branch 3 taken 492 times.
✓ Branch 4 taken 209582 times.
210126 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
154 492 msg_rpc_.set_allocated_msg_quit(
155 492 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
156
2/2
✓ Branch 3 taken 104 times.
✓ Branch 4 taken 209530 times.
209582 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
157 104 msg_rpc_.set_allocated_msg_ioctl(
158 104 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
159
2/2
✓ Branch 3 taken 17168 times.
✓ Branch 4 taken 192362 times.
209530 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
160 17168 msg_rpc_.set_allocated_msg_refcount_req(
161 17168 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
162
2/2
✓ Branch 3 taken 14664 times.
✓ Branch 4 taken 177698 times.
192362 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
163 14664 msg_rpc_.set_allocated_msg_refcount_reply(
164 14664 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
165
2/2
✓ Branch 3 taken 440 times.
✓ Branch 4 taken 177232 times.
177698 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
166 440 msg_rpc_.set_allocated_msg_object_info_req(
167 440 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
168
2/2
✓ Branch 3 taken 416 times.
✓ Branch 4 taken 176816 times.
177232 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
169 416 msg_rpc_.set_allocated_msg_object_info_reply(
170 416 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
171
2/2
✓ Branch 3 taken 58188 times.
✓ Branch 4 taken 118654 times.
176816 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
172 58188 msg_rpc_.set_allocated_msg_read_req(
173 58188 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
174
2/2
✓ Branch 3 taken 57486 times.
✓ Branch 4 taken 61168 times.
118654 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
175 57486 msg_rpc_.set_allocated_msg_read_reply(
176 57486 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
177
2/2
✓ Branch 3 taken 18222 times.
✓ Branch 4 taken 42946 times.
61168 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
178 18222 msg_rpc_.set_allocated_msg_store_req(
179 18222 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
180
2/2
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 42920 times.
42946 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
181 26 msg_rpc_.set_allocated_msg_store_abort_req(
182 26 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
183
2/2
✓ Branch 3 taken 15834 times.
✓ Branch 4 taken 27086 times.
42920 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
184 15834 msg_rpc_.set_allocated_msg_store_reply(
185 15834 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
186
2/2
✓ Branch 3 taken 128 times.
✓ Branch 4 taken 26958 times.
27086 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
187 128 msg_rpc_.set_allocated_msg_info_req(
188 128 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
189
2/2
✓ Branch 3 taken 104 times.
✓ Branch 4 taken 26854 times.
26958 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
190 104 msg_rpc_.set_allocated_msg_info_reply(
191 104 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
192
2/2
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 26794 times.
26854 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
193 60 msg_rpc_.set_allocated_msg_shrink_req(
194 60 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
195
2/2
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 26742 times.
26794 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
196 52 msg_rpc_.set_allocated_msg_shrink_reply(
197 52 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
198
2/2
✓ Branch 3 taken 268 times.
✓ Branch 4 taken 26474 times.
26742 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
199 268 msg_rpc_.set_allocated_msg_list_req(
200 268 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
201
2/2
✓ Branch 3 taken 260 times.
✓ Branch 4 taken 26214 times.
26474 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
202 260 msg_rpc_.set_allocated_msg_list_reply(
203 260 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
204
2/2
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 26186 times.
26214 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
205 28 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
206 28 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
207
2/2
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 26130 times.
26186 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
208 56 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
209 56 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
210
2/2
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 26052 times.
26130 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
211 78 msg_rpc_.set_allocated_msg_breadcrumb_reply(
212 78 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
213
1/2
✓ Branch 3 taken 26052 times.
✗ Branch 4 not taken.
26052 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
214 26052 msg_rpc_.set_allocated_msg_detach(
215 26052 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
216 26052 is_msg_out_of_band_ = true;
217 } else {
218 // Unexpected message type, should never happen
219 ✗ PANIC(NULL);
220 }
221 211088 is_wrapped_ = true;
222 211088 }
223
224
225 263688 void CacheTransport::Frame::UnwrapMsg() {
226
2/2
✓ Branch 1 taken 468 times.
✓ Branch 2 taken 263142 times.
263688 if (msg_rpc_.has_msg_handshake()) {
227 468 msg_typed_ = msg_rpc_.mutable_msg_handshake();
228
2/2
✓ Branch 1 taken 494 times.
✓ Branch 2 taken 262726 times.
263142 } else if (msg_rpc_.has_msg_handshake_ack()) {
229 494 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
230
2/2
✓ Branch 1 taken 468 times.
✓ Branch 2 taken 262232 times.
262726 } else if (msg_rpc_.has_msg_quit()) {
231 468 msg_typed_ = msg_rpc_.mutable_msg_quit();
232
2/2
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 262154 times.
262232 } else if (msg_rpc_.has_msg_ioctl()) {
233 104 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
234
2/2
✓ Branch 1 taken 14664 times.
✓ Branch 2 taken 247516 times.
262154 } else if (msg_rpc_.has_msg_refcount_req()) {
235 14664 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
236
2/2
✓ Branch 1 taken 17662 times.
✓ Branch 2 taken 229750 times.
247516 } else if (msg_rpc_.has_msg_refcount_reply()) {
237 17662 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
238
2/2
✓ Branch 1 taken 416 times.
✓ Branch 2 taken 229334 times.
229750 } else if (msg_rpc_.has_msg_object_info_req()) {
239 416 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
240
2/2
✓ Branch 1 taken 674 times.
✓ Branch 2 taken 228738 times.
229334 } else if (msg_rpc_.has_msg_object_info_reply()) {
241 674 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
242
2/2
✓ Branch 1 taken 57486 times.
✓ Branch 2 taken 171174 times.
228738 } else if (msg_rpc_.has_msg_read_req()) {
243 57486 msg_typed_ = msg_rpc_.mutable_msg_read_req();
244
2/2
✓ Branch 1 taken 104988 times.
✓ Branch 2 taken 66342 times.
171174 } else if (msg_rpc_.has_msg_read_reply()) {
245 104988 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
246
2/2
✓ Branch 1 taken 15808 times.
✓ Branch 2 taken 50534 times.
66342 } else if (msg_rpc_.has_msg_store_req()) {
247 15808 msg_typed_ = msg_rpc_.mutable_msg_store_req();
248
2/2
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 50508 times.
50534 } else if (msg_rpc_.has_msg_store_abort_req()) {
249 26 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
250
2/2
✓ Branch 1 taken 23448 times.
✓ Branch 2 taken 27060 times.
50508 } else if (msg_rpc_.has_msg_store_reply()) {
251 23448 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
252
2/2
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 26956 times.
27060 } else if (msg_rpc_.has_msg_info_req()) {
253 104 msg_typed_ = msg_rpc_.mutable_msg_info_req();
254
2/2
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 26828 times.
26956 } else if (msg_rpc_.has_msg_info_reply()) {
255 128 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
256
2/2
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 26776 times.
26828 } else if (msg_rpc_.has_msg_shrink_req()) {
257 52 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
258
2/2
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 26716 times.
26776 } else if (msg_rpc_.has_msg_shrink_reply()) {
259 60 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
260
2/2
✓ Branch 1 taken 260 times.
✓ Branch 2 taken 26456 times.
26716 } else if (msg_rpc_.has_msg_list_req()) {
261 260 msg_typed_ = msg_rpc_.mutable_msg_list_req();
262
2/2
✓ Branch 1 taken 268 times.
✓ Branch 2 taken 26188 times.
26456 } else if (msg_rpc_.has_msg_list_reply()) {
263 268 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
264
2/2
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 26162 times.
26188 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
265 26 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
266
2/2
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 26110 times.
26162 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
267 52 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
268
2/2
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 26026 times.
26110 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
269 84 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
270
1/2
✓ Branch 1 taken 26026 times.
✗ Branch 2 not taken.
26026 } else if (msg_rpc_.has_msg_detach()) {
271 26026 msg_typed_ = msg_rpc_.mutable_msg_detach();
272 26026 is_msg_out_of_band_ = true;
273 } else {
274 // Unexpected message type, should never happen
275 ✗ PANIC(NULL);
276 }
277 263766 }
278
279
280 //------------------------------------------------------------------------------
281
282
283 494 CacheTransport::CacheTransport(int fd_connection)
284 494 : fd_connection_(fd_connection), flags_(0) {
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 494 times.
494 assert(fd_connection_ >= 0);
286 494 }
287
288
289 115986 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
290 115986 : fd_connection_(fd_connection), flags_(flags) {
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115986 times.
115986 assert(fd_connection_ >= 0);
292 115986 }
293
294
295 5236604 void CacheTransport::FillMsgHash(const shash::Any &hash,
296 cvmfs::MsgHash *msg_hash) {
297
3/4
✓ Branch 0 taken 5236526 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
5236604 switch (hash.algorithm) {
298 5236526 case shash::kSha1:
299 5236526 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
300 5236526 break;
301 12 case shash::kRmd160:
302 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
303 12 break;
304 66 case shash::kShake128:
305 66 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
306 66 break;
307 ✗ default:
308 ✗ PANIC(NULL);
309 }
310 5236604 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
311 5236604 }
312
313
314 18214 void CacheTransport::FillObjectType(int object_flags,
315 cvmfs::EnumObjectType *wire_type) {
316 18214 *wire_type = cvmfs::OBJECT_REGULAR;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18214 times.
18214 if (object_flags & CacheManager::kLabelCatalog)
318 ✗ *wire_type = cvmfs::OBJECT_CATALOG;
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18214 times.
18214 if (object_flags & CacheManager::kLabelVolatile)
320 ✗ *wire_type = cvmfs::OBJECT_VOLATILE;
321 18214 }
322
323
324 88428 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
325 shash::Any *hash) {
326
2/4
✓ Branch 1 taken 88374 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
✗ Branch 4 not taken.
88428 switch (msg_hash.algorithm()) {
327 88374 case cvmfs::HASH_SHA1:
328 88374 hash->algorithm = shash::kSha1;
329 88374 break;
330 ✗ case cvmfs::HASH_RIPEMD160:
331 ✗ hash->algorithm = shash::kRmd160;
332 ✗ break;
333 54 case cvmfs::HASH_SHAKE128:
334 54 hash->algorithm = shash::kShake128;
335 54 break;
336 ✗ default:
337 ✗ return false;
338 }
339 88428 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
340
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 88428 times.
88428 if (msg_hash.digest().length() != digest_size)
341 ✗ return false;
342 88428 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
343 88428 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 211064 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
366 uint32_t size;
367 bool has_attachment;
368
1/2
✓ Branch 1 taken 211064 times.
✗ Branch 2 not taken.
211064 bool retval = RecvHeader(&size, &has_attachment);
369
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 211038 times.
211064 if (!retval)
370 26 return false;
371
372 void *buffer;
373
2/2
✓ Branch 0 taken 137366 times.
✓ Branch 1 taken 73672 times.
211038 if (size <= kMaxStackAlloc)
374 137366 buffer = alloca(size);
375 else
376 73672 buffer = smalloc(size);
377
1/2
✓ Branch 1 taken 211038 times.
✗ Branch 2 not taken.
211038 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
378
2/4
✓ Branch 0 taken 211038 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 211038 times.
211038 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
379 ✗ if (size > kMaxStackAlloc) {
380 ✗ free(buffer);
381 }
382 ✗ return false;
383 }
384
385 211038 uint32_t msg_size = size;
386
2/2
✓ Branch 0 taken 73938 times.
✓ Branch 1 taken 137100 times.
211038 if (has_attachment) {
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73938 times.
73938 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 73938 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
396 73938 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73938 times.
73938 if ((msg_size + kInnerHeaderSize) > size) {
398 ✗ if (size > kMaxStackAlloc) {
399 ✗ free(buffer);
400 }
401 ✗ return false;
402 }
403 }
404
405 211038 void *ptr_msg = has_attachment
406
2/2
✓ Branch 0 taken 73938 times.
✓ Branch 1 taken 137100 times.
211038 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
407 : buffer;
408
1/2
✓ Branch 1 taken 211038 times.
✗ Branch 2 not taken.
211038 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211038 times.
211038 if (!retval) {
410 ✗ if (size > kMaxStackAlloc) {
411 ✗ free(buffer);
412 }
413 ✗ return false;
414 }
415
416
2/2
✓ Branch 0 taken 73938 times.
✓ Branch 1 taken 137100 times.
211038 if (has_attachment) {
417 73938 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 73938 times.
73938 if (frame->att_size() < attachment_size) {
419 ✗ if (size > kMaxStackAlloc) {
420 ✗ free(buffer);
421 }
422 ✗ return false;
423 }
424 73938 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
425 73938 + msg_size;
426 73938 memcpy(frame->attachment(), ptr_attachment, attachment_size);
427 73938 frame->set_att_size(attachment_size);
428 } else {
429 137100 frame->set_att_size(0);
430 }
431
2/2
✓ Branch 0 taken 73672 times.
✓ Branch 1 taken 137288 times.
210960 if (size > kMaxStackAlloc) {
432 73672 free(buffer);
433 }
434 210960 return true;
435 }
436
437
438 211064 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
439 unsigned char header[kHeaderSize];
440
1/2
✓ Branch 1 taken 211064 times.
✗ Branch 2 not taken.
211064 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
441
3/4
✓ Branch 0 taken 211064 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 211038 times.
211064 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
442 26 return false;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211038 times.
211038 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
444 ✗ return false;
445 211038 *has_attachment = header[0] & kFlagHasAttachment;
446 211038 *size = header[1] + (header[2] << 8) + (header[3] << 16);
447
2/4
✓ Branch 0 taken 211038 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 211038 times.
✗ Branch 3 not taken.
211038 return (*size > 0) && (*size <= kMaxMsgSize);
448 }
449
450
451 211062 void CacheTransport::SendData(void *message,
452 uint32_t msg_size,
453 void *attachment,
454 uint32_t att_size) {
455 422124 const uint32_t total_size = msg_size + att_size
456
2/2
✓ Branch 0 taken 75654 times.
✓ Branch 1 taken 135408 times.
211062 + ((att_size > 0) ? kInnerHeaderSize : 0);
457
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211062 times.
211062 assert(total_size > 0);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211062 times.
211062 assert(total_size <= kMaxMsgSize);
460
1/2
✓ Branch 1 taken 205348 times.
✗ Branch 2 not taken.
205322 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 135434 times.
✓ Branch 1 taken 75654 times.
211088 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
465 211088 header[1] = (total_size & 0x000000FF);
466 211088 header[2] = (total_size & 0x0000FF00) >> 8;
467 211088 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 211088 iov[0].iov_base = header;
474 211088 iov[0].iov_len = kHeaderSize;
475
476
2/2
✓ Branch 0 taken 75654 times.
✓ Branch 1 taken 135434 times.
211088 if (att_size > 0) {
477 75654 inner_header[0] = (msg_size & 0x000000FF);
478 75654 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
479 75654 iov[1].iov_base = inner_header;
480 75654 iov[1].iov_len = kInnerHeaderSize;
481 75654 iov[2].iov_base = message;
482 75654 iov[2].iov_len = msg_size;
483 75654 iov[3].iov_base = attachment;
484 75654 iov[3].iov_len = att_size;
485 } else {
486 135434 iov[1].iov_base = message;
487 135434 iov[1].iov_len = msg_size;
488 }
489
2/2
✓ Branch 0 taken 26052 times.
✓ Branch 1 taken 185036 times.
211088 if (flags_ & kFlagSendNonBlocking) {
490
2/4
✓ Branch 0 taken 26052 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 26052 times.
✗ Branch 4 not taken.
26052 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
491 26052 return;
492 }
493
3/4
✓ Branch 0 taken 109382 times.
✓ Branch 1 taken 75654 times.
✓ Branch 3 taken 185010 times.
✗ Branch 4 not taken.
185036 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
494
495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 185010 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
185010 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
496 ✗ PANIC(kLogSyslogErr | kLogDebug,
497 "failed to write to external cache transport (%d), aborting", errno);
498 }
499 }
500
501 26052 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26052 times.
26052 assert(iovcnt > 0);
503 26052 unsigned total_size = 0;
504
2/2
✓ Branch 0 taken 52104 times.
✓ Branch 1 taken 26052 times.
78156 for (unsigned i = 0; i < iovcnt; ++i)
505 52104 total_size += iov[i].iov_len;
506 26052 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
507
508 26052 unsigned pos = 0;
509
2/2
✓ Branch 0 taken 52104 times.
✓ Branch 1 taken 26052 times.
78156 for (unsigned i = 0; i < iovcnt; ++i) {
510 52104 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
511 52104 pos += iov[i].iov_len;
512 }
513
514 26052 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26052 times.
26052 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 26052 }
524
525
526 211088 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
527 211088 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
528 211088 const size_t byte_size = msg_rpc->ByteSizeLong();
529
2/4
✓ Branch 0 taken 211088 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 211088 times.
✗ Branch 3 not taken.
211088 assert((byte_size > 0) && (byte_size <= kMaxMsgSize));
530 211088 const uint32_t size = static_cast<uint32_t>(byte_size);
531 #ifdef __APPLE__
532 void *buffer = smalloc(size);
533 #else
534 211088 void *buffer = alloca(size);
535 #endif
536 211088 const bool retval = msg_rpc->SerializeToArray(buffer, size);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211062 times.
211062 assert(retval);
538 211062 SendData(buffer, size, frame->attachment(), frame->att_size());
539 #ifdef __APPLE__
540 free(buffer);
541 #endif
542 211062 }
543