GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2025-12-14 02:35:46
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 "crypto/hash.h"
16 #include "util/exception.h"
17 #include "util/logging.h"
18 #include "util/posix.h"
19 #include "util/smalloc.h"
20
21 // TODO(jblomer): Check for possible starvation of plugin by dying clients
22 // (blocking read). Probably only relevant for TCP sockets.
23
24 using namespace std; // NOLINT
25
26 const char
27 *CacheTransport::kEnvReadyNotifyFd = "__CVMFS_CACHE_EXTERNAL_PIPE_READY__";
28
29 /**
30 * Called on the sender side to wrap a message into a MsgRpc message for wire
31 * transfer.
32 */
33 305856 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305856 times.
305856 assert(msg_typed_ != NULL);
35
1/2
✓ Branch 0 taken 305856 times.
✗ Branch 1 not taken.
305856 if (!is_wrapped_)
36 305856 WrapMsg();
37 305856 return &msg_rpc_;
38 }
39
40
41 /**
42 * Called on the receiving end of an RPC to extract the actual message from the
43 * MsgRpc.
44 */
45 382782 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
46
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 382782 times.
382782 assert(msg_rpc_.IsInitialized());
47
2/2
✓ Branch 0 taken 324204 times.
✓ Branch 1 taken 58578 times.
382782 if (msg_typed_ == NULL)
48 324204 UnwrapMsg();
49 382744 return msg_typed_;
50 }
51
52
53 382858 CacheTransport::Frame::Frame()
54 382858 : owns_msg_typed_(false)
55 382858 , msg_typed_(NULL)
56 382858 , attachment_(NULL)
57 382858 , att_size_(0)
58 382858 , is_wrapped_(false)
59 382858 , is_msg_out_of_band_(false) { }
60
61
62 305818 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
63 305856 : owns_msg_typed_(false)
64 305856 , msg_typed_(m)
65 305856 , attachment_(NULL)
66 305856 , att_size_(0)
67 305856 , is_wrapped_(false)
68 305818 , is_msg_out_of_band_(false) { }
69
70
71 688676 CacheTransport::Frame::~Frame() { Reset(0); }
72
73
74 58578 bool CacheTransport::Frame::IsMsgOutOfBand() {
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 58578 times.
58578 assert(msg_rpc_.IsInitialized());
76
1/2
✓ Branch 0 taken 58578 times.
✗ Branch 1 not taken.
58578 if (msg_typed_ == NULL)
77 58578 UnwrapMsg();
78 58578 return is_msg_out_of_band_;
79 }
80
81
82 77064 void CacheTransport::Frame::MergeFrom(const Frame &other) {
83 77064 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
84 77064 owns_msg_typed_ = true;
85
2/2
✓ Branch 0 taken 68400 times.
✓ Branch 1 taken 8664 times.
77064 if (other.att_size_ > 0) {
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68400 times.
68400 assert(att_size_ >= other.att_size_);
87 68400 memcpy(attachment_, other.attachment_, other.att_size_);
88 68400 att_size_ = other.att_size_;
89 }
90 77064 }
91
92
93 305794 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
94 305794 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305680 times.
305680 if (!retval)
96 return false;
97
98 // Cleanup typed message when Frame leaves scope
99 305680 owns_msg_typed_ = true;
100 305680 return true;
101 }
102
103
104 688714 void CacheTransport::Frame::Release() {
105
2/2
✓ Branch 0 taken 382858 times.
✓ Branch 1 taken 305856 times.
688714 if (owns_msg_typed_)
106 382858 return;
107
108 305856 msg_rpc_.release_msg_refcount_req();
109 305894 msg_rpc_.release_msg_refcount_reply();
110 305894 msg_rpc_.release_msg_read_req();
111 305894 msg_rpc_.release_msg_read_reply();
112 305856 msg_rpc_.release_msg_object_info_req();
113 305894 msg_rpc_.release_msg_object_info_reply();
114 305894 msg_rpc_.release_msg_store_req();
115 305894 msg_rpc_.release_msg_store_abort_req();
116 305894 msg_rpc_.release_msg_store_reply();
117 305894 msg_rpc_.release_msg_handshake();
118 305894 msg_rpc_.release_msg_handshake_ack();
119 305894 msg_rpc_.release_msg_quit();
120 305894 msg_rpc_.release_msg_ioctl();
121 305894 msg_rpc_.release_msg_info_req();
122 305894 msg_rpc_.release_msg_info_reply();
123 305894 msg_rpc_.release_msg_shrink_req();
124 305856 msg_rpc_.release_msg_shrink_reply();
125 305894 msg_rpc_.release_msg_list_req();
126 305856 msg_rpc_.release_msg_list_reply();
127 305856 msg_rpc_.release_msg_detach();
128 305856 msg_rpc_.release_msg_breadcrumb_store_req();
129 305894 msg_rpc_.release_msg_breadcrumb_load_req();
130 305856 msg_rpc_.release_msg_breadcrumb_reply();
131 }
132
133
134 688714 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
135 688714 msg_typed_ = NULL;
136 688714 att_size_ = original_att_size;
137 688714 is_wrapped_ = false;
138 688714 is_msg_out_of_band_ = false;
139 688714 Release();
140 688638 msg_rpc_.Clear();
141 688562 owns_msg_typed_ = false;
142 688562 }
143
144
145 305856 void CacheTransport::Frame::WrapMsg() {
146
2/2
✓ Branch 3 taken 710 times.
✓ Branch 4 taken 305146 times.
305856 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
147 710 msg_rpc_.set_allocated_msg_handshake(
148 710 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
149
2/2
✓ Branch 3 taken 684 times.
✓ Branch 4 taken 304462 times.
305146 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
150 684 msg_rpc_.set_allocated_msg_handshake_ack(
151 684 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
152
2/2
✓ Branch 3 taken 708 times.
✓ Branch 4 taken 303754 times.
304462 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
153 708 msg_rpc_.set_allocated_msg_quit(
154 708 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
155
2/2
✓ Branch 3 taken 152 times.
✓ Branch 4 taken 303602 times.
303754 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
156 152 msg_rpc_.set_allocated_msg_ioctl(
157 152 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
158
2/2
✓ Branch 3 taken 23936 times.
✓ Branch 4 taken 279666 times.
303602 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
159 23936 msg_rpc_.set_allocated_msg_refcount_req(
160 23936 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
161
2/2
✓ Branch 3 taken 21432 times.
✓ Branch 4 taken 258234 times.
279666 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
162 21432 msg_rpc_.set_allocated_msg_refcount_reply(
163 21432 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
164
2/2
✓ Branch 3 taken 632 times.
✓ Branch 4 taken 257602 times.
258234 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
165 632 msg_rpc_.set_allocated_msg_object_info_req(
166 632 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
167
2/2
✓ Branch 3 taken 608 times.
✓ Branch 4 taken 256994 times.
257602 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
168 608 msg_rpc_.set_allocated_msg_object_info_reply(
169 608 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
170
2/2
✓ Branch 3 taken 84712 times.
✓ Branch 4 taken 172282 times.
256994 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
171 84712 msg_rpc_.set_allocated_msg_read_req(
172 84712 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
173
2/2
✓ Branch 3 taken 84018 times.
✓ Branch 4 taken 88264 times.
172282 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
174 84018 msg_rpc_.set_allocated_msg_read_reply(
175 84018 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
176
2/2
✓ Branch 3 taken 25518 times.
✓ Branch 4 taken 62746 times.
88264 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
177 25518 msg_rpc_.set_allocated_msg_store_req(
178 25518 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
179
2/2
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 62708 times.
62746 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
180 38 msg_rpc_.set_allocated_msg_store_abort_req(
181 38 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
182
2/2
✓ Branch 3 taken 23142 times.
✓ Branch 4 taken 39566 times.
62708 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
183 23142 msg_rpc_.set_allocated_msg_store_reply(
184 23142 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
185
2/2
✓ Branch 3 taken 176 times.
✓ Branch 4 taken 39390 times.
39566 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
186 176 msg_rpc_.set_allocated_msg_info_req(
187 176 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
188
2/2
✓ Branch 3 taken 152 times.
✓ Branch 4 taken 39238 times.
39390 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
189 152 msg_rpc_.set_allocated_msg_info_reply(
190 152 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
191
2/2
✓ Branch 3 taken 84 times.
✓ Branch 4 taken 39154 times.
39238 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
192 84 msg_rpc_.set_allocated_msg_shrink_req(
193 84 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
194
2/2
✓ Branch 3 taken 76 times.
✓ Branch 4 taken 39078 times.
39154 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
195 76 msg_rpc_.set_allocated_msg_shrink_reply(
196 76 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
197
2/2
✓ Branch 3 taken 388 times.
✓ Branch 4 taken 38690 times.
39078 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
198 388 msg_rpc_.set_allocated_msg_list_req(
199 388 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
200
2/2
✓ Branch 3 taken 380 times.
✓ Branch 4 taken 38310 times.
38690 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
201 380 msg_rpc_.set_allocated_msg_list_reply(
202 380 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
203
2/2
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 38270 times.
38310 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
204 40 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
205 40 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
206
2/2
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 38190 times.
38270 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
207 80 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
208 80 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
209
2/2
✓ Branch 3 taken 114 times.
✓ Branch 4 taken 38076 times.
38190 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
210 114 msg_rpc_.set_allocated_msg_breadcrumb_reply(
211 114 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
212
1/2
✓ Branch 3 taken 38076 times.
✗ Branch 4 not taken.
38076 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
213 38076 msg_rpc_.set_allocated_msg_detach(
214 38076 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
215 38076 is_msg_out_of_band_ = true;
216 } else {
217 // Unexpected message type, should never happen
218 PANIC(NULL);
219 }
220 305856 is_wrapped_ = true;
221 305856 }
222
223
224 382782 void CacheTransport::Frame::UnwrapMsg() {
225
2/2
✓ Branch 1 taken 684 times.
✓ Branch 2 taken 382098 times.
382782 if (msg_rpc_.has_msg_handshake()) {
226 684 msg_typed_ = msg_rpc_.mutable_msg_handshake();
227
2/2
✓ Branch 1 taken 710 times.
✓ Branch 2 taken 381388 times.
382098 } else if (msg_rpc_.has_msg_handshake_ack()) {
228 710 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
229
2/2
✓ Branch 1 taken 684 times.
✓ Branch 2 taken 380704 times.
381388 } else if (msg_rpc_.has_msg_quit()) {
230 684 msg_typed_ = msg_rpc_.mutable_msg_quit();
231
2/2
✓ Branch 1 taken 152 times.
✓ Branch 2 taken 380552 times.
380704 } else if (msg_rpc_.has_msg_ioctl()) {
232 152 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
233
2/2
✓ Branch 1 taken 21432 times.
✓ Branch 2 taken 359120 times.
380552 } else if (msg_rpc_.has_msg_refcount_req()) {
234 21432 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
235
2/2
✓ Branch 1 taken 24658 times.
✓ Branch 2 taken 334500 times.
359120 } else if (msg_rpc_.has_msg_refcount_reply()) {
236 24658 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
237
2/2
✓ Branch 1 taken 608 times.
✓ Branch 2 taken 333892 times.
334500 } else if (msg_rpc_.has_msg_object_info_req()) {
238 608 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
239
2/2
✓ Branch 1 taken 974 times.
✓ Branch 2 taken 332956 times.
333892 } else if (msg_rpc_.has_msg_object_info_reply()) {
240 974 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
241
2/2
✓ Branch 1 taken 84018 times.
✓ Branch 2 taken 248938 times.
332956 } else if (msg_rpc_.has_msg_read_req()) {
242 84018 msg_typed_ = msg_rpc_.mutable_msg_read_req();
243
2/2
✓ Branch 1 taken 153112 times.
✓ Branch 2 taken 95826 times.
248938 } else if (msg_rpc_.has_msg_read_reply()) {
244 153112 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
245
2/2
✓ Branch 1 taken 23104 times.
✓ Branch 2 taken 72722 times.
95826 } else if (msg_rpc_.has_msg_store_req()) {
246 23104 msg_typed_ = msg_rpc_.mutable_msg_store_req();
247
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 72684 times.
72722 } else if (msg_rpc_.has_msg_store_abort_req()) {
248 38 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
249
2/2
✓ Branch 1 taken 33156 times.
✓ Branch 2 taken 39528 times.
72684 } else if (msg_rpc_.has_msg_store_reply()) {
250 33156 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
251
2/2
✓ Branch 1 taken 152 times.
✓ Branch 2 taken 39376 times.
39528 } else if (msg_rpc_.has_msg_info_req()) {
252 152 msg_typed_ = msg_rpc_.mutable_msg_info_req();
253
2/2
✓ Branch 1 taken 176 times.
✓ Branch 2 taken 39200 times.
39376 } else if (msg_rpc_.has_msg_info_reply()) {
254 176 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
255
2/2
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 39124 times.
39200 } else if (msg_rpc_.has_msg_shrink_req()) {
256 76 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
257
2/2
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 39040 times.
39124 } else if (msg_rpc_.has_msg_shrink_reply()) {
258 84 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
259
2/2
✓ Branch 1 taken 380 times.
✓ Branch 2 taken 38660 times.
39040 } else if (msg_rpc_.has_msg_list_req()) {
260 380 msg_typed_ = msg_rpc_.mutable_msg_list_req();
261
2/2
✓ Branch 1 taken 388 times.
✓ Branch 2 taken 38272 times.
38660 } else if (msg_rpc_.has_msg_list_reply()) {
262 388 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
263
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 38234 times.
38272 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
264 38 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
265
2/2
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 38158 times.
38234 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
266 76 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
267
2/2
✓ Branch 1 taken 120 times.
✓ Branch 2 taken 38038 times.
38158 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
268 120 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
269
1/2
✓ Branch 1 taken 38038 times.
✗ Branch 2 not taken.
38038 } else if (msg_rpc_.has_msg_detach()) {
270 38038 msg_typed_ = msg_rpc_.mutable_msg_detach();
271 38038 is_msg_out_of_band_ = true;
272 } else {
273 // Unexpected message type, should never happen
274 PANIC(NULL);
275 }
276 382858 }
277
278
279 //------------------------------------------------------------------------------
280
281
282 710 CacheTransport::CacheTransport(int fd_connection)
283 710 : fd_connection_(fd_connection), flags_(0) {
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 710 times.
710 assert(fd_connection_ >= 0);
285 710 }
286
287
288 169518 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
289 169518 : fd_connection_(fd_connection), flags_(flags) {
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 169518 times.
169518 assert(fd_connection_ >= 0);
291 169518 }
292
293
294 7651107 void CacheTransport::FillMsgHash(const shash::Any &hash,
295 cvmfs::MsgHash *msg_hash) {
296
3/4
✓ Branch 0 taken 7651005 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
7651107 switch (hash.algorithm) {
297 7651005 case shash::kSha1:
298 7651005 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
299 7651005 break;
300 12 case shash::kRmd160:
301 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
302 12 break;
303 90 case shash::kShake128:
304 90 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
305 90 break;
306 default:
307 PANIC(NULL);
308 }
309 7651107 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
310 7651107 }
311
312
313 25510 void CacheTransport::FillObjectType(int object_flags,
314 cvmfs::EnumObjectType *wire_type) {
315 25510 *wire_type = cvmfs::OBJECT_REGULAR;
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25510 times.
25510 if (object_flags & CacheManager::kLabelCatalog)
317 *wire_type = cvmfs::OBJECT_CATALOG;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25510 times.
25510 if (object_flags & CacheManager::kLabelVolatile)
319 *wire_type = cvmfs::OBJECT_VOLATILE;
320 25510 }
321
322
323 129240 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
324 shash::Any *hash) {
325
2/4
✓ Branch 1 taken 129162 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 78 times.
✗ Branch 4 not taken.
129240 switch (msg_hash.algorithm()) {
326 129162 case cvmfs::HASH_SHA1:
327 129162 hash->algorithm = shash::kSha1;
328 129162 break;
329 case cvmfs::HASH_RIPEMD160:
330 hash->algorithm = shash::kRmd160;
331 break;
332 78 case cvmfs::HASH_SHAKE128:
333 78 hash->algorithm = shash::kShake128;
334 78 break;
335 default:
336 return false;
337 }
338 129240 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
339
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 129240 times.
129240 if (msg_hash.digest().length() != digest_size)
340 return false;
341 129240 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
342 129240 return true;
343 }
344
345
346 bool CacheTransport::ParseObjectType(cvmfs::EnumObjectType wire_type,
347 int *object_flags) {
348 *object_flags = 0;
349 switch (wire_type) {
350 case cvmfs::OBJECT_REGULAR:
351 return true;
352 case cvmfs::OBJECT_CATALOG:
353 *object_flags |= CacheManager::kLabelCatalog;
354 return true;
355 case cvmfs::OBJECT_VOLATILE:
356 *object_flags |= CacheManager::kLabelVolatile;
357 return true;
358 default:
359 return false;
360 }
361 }
362
363
364 305832 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
365 uint32_t size;
366 bool has_attachment;
367
1/2
✓ Branch 1 taken 305832 times.
✗ Branch 2 not taken.
305832 bool retval = RecvHeader(&size, &has_attachment);
368
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 305794 times.
305832 if (!retval)
369 38 return false;
370
371 void *buffer;
372
2/2
✓ Branch 0 taken 198345 times.
✓ Branch 1 taken 107449 times.
305794 if (size <= kMaxStackAlloc)
373 198345 buffer = alloca(size);
374 else
375 107449 buffer = smalloc(size);
376
1/2
✓ Branch 1 taken 305794 times.
✗ Branch 2 not taken.
305794 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
377
2/4
✓ Branch 0 taken 305794 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 305794 times.
305794 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
378 if (size > kMaxStackAlloc) {
379 free(buffer);
380 }
381 return false;
382 }
383
384 305794 uint32_t msg_size = size;
385
2/2
✓ Branch 0 taken 107734 times.
✓ Branch 1 taken 198060 times.
305794 if (has_attachment) {
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107734 times.
107734 if (size < 2) {
387 // kMaxStackAlloc is > 2 (of course!) but we'll leave the condition here
388 // for consistency.
389 if (size > kMaxStackAlloc) {
390 free(buffer);
391 }
392 return false;
393 }
394 107734 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
395 107734 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107734 times.
107734 if ((msg_size + kInnerHeaderSize) > size) {
397 if (size > kMaxStackAlloc) {
398 free(buffer);
399 }
400 return false;
401 }
402 }
403
404 305794 void *ptr_msg = has_attachment
405
2/2
✓ Branch 0 taken 107734 times.
✓ Branch 1 taken 198060 times.
305794 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
406 : buffer;
407
1/2
✓ Branch 1 taken 305680 times.
✗ Branch 2 not taken.
305794 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305680 times.
305680 if (!retval) {
409 if (size > kMaxStackAlloc) {
410 free(buffer);
411 }
412 return false;
413 }
414
415
2/2
✓ Branch 0 taken 107734 times.
✓ Branch 1 taken 197946 times.
305680 if (has_attachment) {
416 107734 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 107734 times.
107734 if (frame->att_size() < attachment_size) {
418 if (size > kMaxStackAlloc) {
419 free(buffer);
420 }
421 return false;
422 }
423 107734 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
424 107734 + msg_size;
425 107734 memcpy(frame->attachment(), ptr_attachment, attachment_size);
426 107734 frame->set_att_size(attachment_size);
427 } else {
428 197946 frame->set_att_size(0);
429 }
430
2/2
✓ Branch 0 taken 107449 times.
✓ Branch 1 taken 198307 times.
305756 if (size > kMaxStackAlloc) {
431 107449 free(buffer);
432 }
433 305756 return true;
434 }
435
436
437 305832 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
438 unsigned char header[kHeaderSize];
439
1/2
✓ Branch 1 taken 305832 times.
✗ Branch 2 not taken.
305832 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
440
3/4
✓ Branch 0 taken 305832 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 305794 times.
305832 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
441 38 return false;
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305794 times.
305794 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
443 return false;
444 305794 *has_attachment = header[0] & kFlagHasAttachment;
445 305794 *size = header[1] + (header[2] << 8) + (header[3] << 16);
446
2/4
✓ Branch 0 taken 305794 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 305794 times.
✗ Branch 3 not taken.
305794 return (*size > 0) && (*size <= kMaxMsgSize);
447 }
448
449
450 305856 void CacheTransport::SendData(void *message,
451 uint32_t msg_size,
452 void *attachment,
453 uint32_t att_size) {
454 611712 const uint32_t total_size = msg_size + att_size
455
2/2
✓ Branch 0 taken 109458 times.
✓ Branch 1 taken 196398 times.
305856 + ((att_size > 0) ? kInnerHeaderSize : 0);
456
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305856 times.
305856 assert(total_size > 0);
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305856 times.
305856 assert(total_size <= kMaxMsgSize);
459
1/2
✓ Branch 1 taken 300124 times.
✗ Branch 2 not taken.
300124 LogCvmfs(kLogCache, kLogDebug,
460 "sending message of size %u to cache transport", total_size);
461
462 unsigned char header[kHeaderSize];
463
2/2
✓ Branch 0 taken 196398 times.
✓ Branch 1 taken 109458 times.
305856 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
464 305856 header[1] = (total_size & 0x000000FF);
465 305856 header[2] = (total_size & 0x0000FF00) >> 8;
466 305856 header[3] = (total_size & 0x00FF0000) >> 16;
467 // Only transferred if an attachment is present. Otherwise the overall size
468 // is also the size of the protobuf message.
469 unsigned char inner_header[kInnerHeaderSize];
470
471 struct iovec iov[4];
472 305856 iov[0].iov_base = header;
473 305856 iov[0].iov_len = kHeaderSize;
474
475
2/2
✓ Branch 0 taken 109458 times.
✓ Branch 1 taken 196398 times.
305856 if (att_size > 0) {
476 109458 inner_header[0] = (msg_size & 0x000000FF);
477 109458 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
478 109458 iov[1].iov_base = inner_header;
479 109458 iov[1].iov_len = kInnerHeaderSize;
480 109458 iov[2].iov_base = message;
481 109458 iov[2].iov_len = msg_size;
482 109458 iov[3].iov_base = attachment;
483 109458 iov[3].iov_len = att_size;
484 } else {
485 196398 iov[1].iov_base = message;
486 196398 iov[1].iov_len = msg_size;
487 }
488
2/2
✓ Branch 0 taken 38076 times.
✓ Branch 1 taken 267780 times.
305856 if (flags_ & kFlagSendNonBlocking) {
489
2/4
✓ Branch 0 taken 38076 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 38076 times.
✗ Branch 4 not taken.
38076 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
490 38076 return;
491 }
492
3/4
✓ Branch 0 taken 158322 times.
✓ Branch 1 taken 109458 times.
✓ Branch 3 taken 267704 times.
✗ Branch 4 not taken.
267780 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
493
494
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 267704 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
267704 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
495 PANIC(kLogSyslogErr | kLogDebug,
496 "failed to write to external cache transport (%d), aborting", errno);
497 }
498 }
499
500 38076 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38076 times.
38076 assert(iovcnt > 0);
502 38076 unsigned total_size = 0;
503
2/2
✓ Branch 0 taken 76152 times.
✓ Branch 1 taken 38076 times.
114228 for (unsigned i = 0; i < iovcnt; ++i)
504 76152 total_size += iov[i].iov_len;
505 38076 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
506
507 38076 unsigned pos = 0;
508
2/2
✓ Branch 0 taken 76152 times.
✓ Branch 1 taken 38076 times.
114228 for (unsigned i = 0; i < iovcnt; ++i) {
509 76152 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
510 76152 pos += iov[i].iov_len;
511 }
512
513 38076 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38076 times.
38076 if (retval < 0) {
515 assert(errno != EMSGSIZE);
516 if (!(flags_ & kFlagSendIgnoreFailure)) {
517 PANIC(kLogSyslogErr | kLogDebug,
518 "failed to write to external cache transport (%d), aborting",
519 errno);
520 }
521 }
522 38076 }
523
524
525 305856 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
526 305856 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
527 305856 const int32_t size = msg_rpc->ByteSize();
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305856 times.
305856 assert(size > 0);
529 #ifdef __APPLE__
530 void *buffer = smalloc(size);
531 #else
532 305856 void *buffer = alloca(size);
533 #endif
534 305856 const bool retval = msg_rpc->SerializeToArray(buffer, size);
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305856 times.
305856 assert(retval);
536 305856 SendData(buffer, size, frame->attachment(), frame->att_size());
537 #ifdef __APPLE__
538 free(buffer);
539 #endif
540 305704 }
541