GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2025-10-05 02:35:40
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 92614 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92614 times.
92614 assert(msg_typed_ != NULL);
35
1/2
✓ Branch 0 taken 92614 times.
✗ Branch 1 not taken.
92614 if (!is_wrapped_)
36 92614 WrapMsg();
37 92614 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 114876 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
46
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 114832 times.
114876 assert(msg_rpc_.IsInitialized());
47
2/2
✓ Branch 0 taken 93834 times.
✓ Branch 1 taken 20998 times.
114832 if (msg_typed_ == NULL)
48 93834 UnwrapMsg();
49 114733 return msg_typed_;
50 }
51
52
53 114887 CacheTransport::Frame::Frame()
54 114887 : owns_msg_typed_(false)
55 114887 , msg_typed_(NULL)
56 114887 , attachment_(NULL)
57 114887 , att_size_(0)
58 114887 , is_wrapped_(false)
59 114887 , is_msg_out_of_band_(false) { }
60
61
62 92614 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
63 92614 : owns_msg_typed_(false)
64 92614 , msg_typed_(m)
65 92614 , attachment_(NULL)
66 92614 , att_size_(0)
67 92614 , is_wrapped_(false)
68 92614 , is_msg_out_of_band_(false) { }
69
70
71 207479 CacheTransport::Frame::~Frame() { Reset(0); }
72
73
74 20998 bool CacheTransport::Frame::IsMsgOutOfBand() {
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20998 times.
20998 assert(msg_rpc_.IsInitialized());
76
1/2
✓ Branch 0 taken 20998 times.
✗ Branch 1 not taken.
20998 if (msg_typed_ == NULL)
77 20998 UnwrapMsg();
78 20998 return is_msg_out_of_band_;
79 }
80
81
82 22308 void CacheTransport::Frame::MergeFrom(const Frame &other) {
83 22308 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
84 22308 owns_msg_typed_ = true;
85
2/2
✓ Branch 0 taken 19800 times.
✓ Branch 1 taken 2508 times.
22308 if (other.att_size_ > 0) {
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19800 times.
19800 assert(att_size_ >= other.att_size_);
87 19800 memcpy(attachment_, other.attachment_, other.att_size_);
88 19800 att_size_ = other.att_size_;
89 }
90 22308 }
91
92
93 92579 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
94 92579 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92557 times.
92557 if (!retval)
96 return false;
97
98 // Cleanup typed message when Frame leaves scope
99 92557 owns_msg_typed_ = true;
100 92557 return true;
101 }
102
103
104 207490 void CacheTransport::Frame::Release() {
105
2/2
✓ Branch 0 taken 114876 times.
✓ Branch 1 taken 92614 times.
207490 if (owns_msg_typed_)
106 114876 return;
107
108 92614 msg_rpc_.release_msg_refcount_req();
109 92625 msg_rpc_.release_msg_refcount_reply();
110 92625 msg_rpc_.release_msg_read_req();
111 92625 msg_rpc_.release_msg_read_reply();
112 92625 msg_rpc_.release_msg_object_info_req();
113 92625 msg_rpc_.release_msg_object_info_reply();
114 92614 msg_rpc_.release_msg_store_req();
115 92614 msg_rpc_.release_msg_store_abort_req();
116 92614 msg_rpc_.release_msg_store_reply();
117 92625 msg_rpc_.release_msg_handshake();
118 92625 msg_rpc_.release_msg_handshake_ack();
119 92614 msg_rpc_.release_msg_quit();
120 92614 msg_rpc_.release_msg_ioctl();
121 92614 msg_rpc_.release_msg_info_req();
122 92614 msg_rpc_.release_msg_info_reply();
123 92614 msg_rpc_.release_msg_shrink_req();
124 92625 msg_rpc_.release_msg_shrink_reply();
125 92614 msg_rpc_.release_msg_list_req();
126 92614 msg_rpc_.release_msg_list_reply();
127 92603 msg_rpc_.release_msg_detach();
128 92603 msg_rpc_.release_msg_breadcrumb_store_req();
129 92603 msg_rpc_.release_msg_breadcrumb_load_req();
130 92603 msg_rpc_.release_msg_breadcrumb_reply();
131 }
132
133
134 207501 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
135 207501 msg_typed_ = NULL;
136 207501 att_size_ = original_att_size;
137 207501 is_wrapped_ = false;
138 207501 is_msg_out_of_band_ = false;
139 207501 Release();
140 207490 msg_rpc_.Clear();
141 207479 owns_msg_typed_ = false;
142 207479 }
143
144
145 92614 void CacheTransport::Frame::WrapMsg() {
146
2/2
✓ Branch 3 taken 224 times.
✓ Branch 4 taken 92390 times.
92614 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
147 224 msg_rpc_.set_allocated_msg_handshake(
148 224 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
149
2/2
✓ Branch 3 taken 198 times.
✓ Branch 4 taken 92192 times.
92390 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
150 198 msg_rpc_.set_allocated_msg_handshake_ack(
151 198 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
152
2/2
✓ Branch 3 taken 222 times.
✓ Branch 4 taken 91970 times.
92192 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
153 222 msg_rpc_.set_allocated_msg_quit(
154 222 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
155
2/2
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 91926 times.
91970 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
156 44 msg_rpc_.set_allocated_msg_ioctl(
157 44 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
158
2/2
✓ Branch 3 taken 8708 times.
✓ Branch 4 taken 83218 times.
91926 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
159 8708 msg_rpc_.set_allocated_msg_refcount_req(
160 8708 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
161
2/2
✓ Branch 3 taken 6204 times.
✓ Branch 4 taken 77014 times.
83218 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
162 6204 msg_rpc_.set_allocated_msg_refcount_reply(
163 6204 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
164
2/2
✓ Branch 3 taken 200 times.
✓ Branch 4 taken 76814 times.
77014 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
165 200 msg_rpc_.set_allocated_msg_object_info_req(
166 200 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
167
2/2
✓ Branch 3 taken 176 times.
✓ Branch 4 taken 76638 times.
76814 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
168 176 msg_rpc_.set_allocated_msg_object_info_reply(
169 176 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
170
2/2
✓ Branch 3 taken 25019 times.
✓ Branch 4 taken 51619 times.
76638 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
171 25019 msg_rpc_.set_allocated_msg_read_req(
172 25019 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
173
2/2
✓ Branch 3 taken 24321 times.
✓ Branch 4 taken 27298 times.
51619 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
174 24321 msg_rpc_.set_allocated_msg_read_reply(
175 24321 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
176
2/2
✓ Branch 3 taken 9102 times.
✓ Branch 4 taken 18196 times.
27298 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
177 9102 msg_rpc_.set_allocated_msg_store_req(
178 9102 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
179
2/2
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 18185 times.
18196 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
180 11 msg_rpc_.set_allocated_msg_store_abort_req(
181 11 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
182
2/2
✓ Branch 3 taken 6699 times.
✓ Branch 4 taken 11486 times.
18185 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
183 6699 msg_rpc_.set_allocated_msg_store_reply(
184 6699 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
185
2/2
✓ Branch 3 taken 68 times.
✓ Branch 4 taken 11418 times.
11486 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
186 68 msg_rpc_.set_allocated_msg_info_req(
187 68 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
188
2/2
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 11374 times.
11418 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
189 44 msg_rpc_.set_allocated_msg_info_reply(
190 44 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
191
2/2
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 11344 times.
11374 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
192 30 msg_rpc_.set_allocated_msg_shrink_req(
193 30 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
194
2/2
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 11322 times.
11344 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
195 22 msg_rpc_.set_allocated_msg_shrink_reply(
196 22 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
197
2/2
✓ Branch 3 taken 118 times.
✓ Branch 4 taken 11204 times.
11322 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
198 118 msg_rpc_.set_allocated_msg_list_req(
199 118 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
200
2/2
✓ Branch 3 taken 110 times.
✓ Branch 4 taken 11094 times.
11204 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
201 110 msg_rpc_.set_allocated_msg_list_reply(
202 110 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
203
2/2
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 11081 times.
11094 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
204 13 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
205 13 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
206
2/2
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 11055 times.
11081 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
207 26 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
208 26 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
209
2/2
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 11022 times.
11055 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
210 33 msg_rpc_.set_allocated_msg_breadcrumb_reply(
211 33 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
212
1/2
✓ Branch 3 taken 11022 times.
✗ Branch 4 not taken.
11022 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
213 11022 msg_rpc_.set_allocated_msg_detach(
214 11022 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
215 11022 is_msg_out_of_band_ = true;
216 } else {
217 // Unexpected message type, should never happen
218 PANIC(NULL);
219 }
220 92614 is_wrapped_ = true;
221 92614 }
222
223
224 114843 void CacheTransport::Frame::UnwrapMsg() {
225
2/2
✓ Branch 1 taken 198 times.
✓ Branch 2 taken 114612 times.
114843 if (msg_rpc_.has_msg_handshake()) {
226 198 msg_typed_ = msg_rpc_.mutable_msg_handshake();
227
2/2
✓ Branch 1 taken 224 times.
✓ Branch 2 taken 114366 times.
114612 } else if (msg_rpc_.has_msg_handshake_ack()) {
228 224 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
229
2/2
✓ Branch 1 taken 198 times.
✓ Branch 2 taken 114157 times.
114366 } else if (msg_rpc_.has_msg_quit()) {
230 198 msg_typed_ = msg_rpc_.mutable_msg_quit();
231
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 114135 times.
114157 } else if (msg_rpc_.has_msg_ioctl()) {
232 44 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
233
2/2
✓ Branch 1 taken 6204 times.
✓ Branch 2 taken 107931 times.
114135 } else if (msg_rpc_.has_msg_refcount_req()) {
234 6204 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
235
2/2
✓ Branch 1 taken 8906 times.
✓ Branch 2 taken 99080 times.
107931 } else if (msg_rpc_.has_msg_refcount_reply()) {
236 8906 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
237
2/2
✓ Branch 1 taken 176 times.
✓ Branch 2 taken 98882 times.
99080 } else if (msg_rpc_.has_msg_object_info_req()) {
238 176 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
239
2/2
✓ Branch 1 taken 299 times.
✓ Branch 2 taken 98572 times.
98882 } else if (msg_rpc_.has_msg_object_info_reply()) {
240 299 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
241
2/2
✓ Branch 1 taken 24321 times.
✓ Branch 2 taken 74229 times.
98572 } else if (msg_rpc_.has_msg_read_req()) {
242 24321 msg_typed_ = msg_rpc_.mutable_msg_read_req();
243
2/2
✓ Branch 1 taken 44819 times.
✓ Branch 2 taken 29487 times.
74229 } else if (msg_rpc_.has_msg_read_reply()) {
244 44819 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
245
2/2
✓ Branch 1 taken 6688 times.
✓ Branch 2 taken 22799 times.
29487 } else if (msg_rpc_.has_msg_store_req()) {
246 6688 msg_typed_ = msg_rpc_.mutable_msg_store_req();
247
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 22788 times.
22799 } else if (msg_rpc_.has_msg_store_abort_req()) {
248 11 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
249
2/2
✓ Branch 1 taken 11313 times.
✓ Branch 2 taken 11475 times.
22788 } else if (msg_rpc_.has_msg_store_reply()) {
250 11313 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
251
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 11431 times.
11475 } else if (msg_rpc_.has_msg_info_req()) {
252 44 msg_typed_ = msg_rpc_.mutable_msg_info_req();
253
2/2
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 11363 times.
11431 } else if (msg_rpc_.has_msg_info_reply()) {
254 68 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
255
2/2
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 11341 times.
11363 } else if (msg_rpc_.has_msg_shrink_req()) {
256 22 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
257
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 11311 times.
11341 } else if (msg_rpc_.has_msg_shrink_reply()) {
258 30 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
259
2/2
✓ Branch 1 taken 110 times.
✓ Branch 2 taken 11201 times.
11311 } else if (msg_rpc_.has_msg_list_req()) {
260 110 msg_typed_ = msg_rpc_.mutable_msg_list_req();
261
2/2
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 11083 times.
11201 } else if (msg_rpc_.has_msg_list_reply()) {
262 118 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
263
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 11072 times.
11083 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
264 11 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
265
2/2
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 11050 times.
11072 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
266 22 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
267
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 11011 times.
11050 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
268 39 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
269
1/2
✓ Branch 1 taken 11011 times.
✗ Branch 2 not taken.
11011 } else if (msg_rpc_.has_msg_detach()) {
270 11011 msg_typed_ = msg_rpc_.mutable_msg_detach();
271 11011 is_msg_out_of_band_ = true;
272 } else {
273 // Unexpected message type, should never happen
274 PANIC(NULL);
275 }
276 114876 }
277
278
279 //------------------------------------------------------------------------------
280
281
282 224 CacheTransport::CacheTransport(int fd_connection)
283 224 : fd_connection_(fd_connection), flags_(0) {
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
224 assert(fd_connection_ >= 0);
285 224 }
286
287
288 49071 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
289 49071 : fd_connection_(fd_connection), flags_(flags) {
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49071 times.
49071 assert(fd_connection_ >= 0);
291 49071 }
292
293
294 2218462 void CacheTransport::FillMsgHash(const shash::Any &hash,
295 cvmfs::MsgHash *msg_hash) {
296
3/4
✓ Branch 0 taken 2218414 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
2218462 switch (hash.algorithm) {
297 2218414 case shash::kSha1:
298 2218414 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
299 2218414 break;
300 12 case shash::kRmd160:
301 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
302 12 break;
303 36 case shash::kShake128:
304 36 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
305 36 break;
306 default:
307 PANIC(NULL);
308 }
309 2218462 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
310 2218462 }
311
312
313 9094 void CacheTransport::FillObjectType(int object_flags,
314 cvmfs::EnumObjectType *wire_type) {
315 9094 *wire_type = cvmfs::OBJECT_REGULAR;
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9094 times.
9094 if (object_flags & CacheManager::kLabelCatalog)
317 *wire_type = cvmfs::OBJECT_CATALOG;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9094 times.
9094 if (object_flags & CacheManager::kLabelVolatile)
319 *wire_type = cvmfs::OBJECT_VOLATILE;
320 9094 }
321
322
323 37413 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
324 shash::Any *hash) {
325
2/4
✓ Branch 1 taken 37389 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
37413 switch (msg_hash.algorithm()) {
326 37389 case cvmfs::HASH_SHA1:
327 37389 hash->algorithm = shash::kSha1;
328 37389 break;
329 case cvmfs::HASH_RIPEMD160:
330 hash->algorithm = shash::kRmd160;
331 break;
332 24 case cvmfs::HASH_SHAKE128:
333 24 hash->algorithm = shash::kShake128;
334 24 break;
335 default:
336 return false;
337 }
338 37413 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
339
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 37413 times.
37413 if (msg_hash.digest().length() != digest_size)
340 return false;
341 37413 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
342 37413 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 92590 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
365 uint32_t size;
366 bool has_attachment;
367
1/2
✓ Branch 1 taken 92590 times.
✗ Branch 2 not taken.
92590 bool retval = RecvHeader(&size, &has_attachment);
368
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 92579 times.
92590 if (!retval)
369 11 return false;
370
371 void *buffer;
372
2/2
✓ Branch 0 taken 61129 times.
✓ Branch 1 taken 31450 times.
92579 if (size <= kMaxStackAlloc)
373 61129 buffer = alloca(size);
374 else
375 31450 buffer = smalloc(size);
376
1/2
✓ Branch 1 taken 92579 times.
✗ Branch 2 not taken.
92579 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
377
2/4
✓ Branch 0 taken 92579 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 92579 times.
92579 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
378 if (size > kMaxStackAlloc) {
379 free(buffer);
380 }
381 return false;
382 }
383
384 92579 uint32_t msg_size = size;
385
2/2
✓ Branch 0 taken 31679 times.
✓ Branch 1 taken 60900 times.
92579 if (has_attachment) {
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31679 times.
31679 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 31679 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
395 31679 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31679 times.
31679 if ((msg_size + kInnerHeaderSize) > size) {
397 if (size > kMaxStackAlloc) {
398 free(buffer);
399 }
400 return false;
401 }
402 }
403
404 92579 void *ptr_msg = has_attachment
405
2/2
✓ Branch 0 taken 31679 times.
✓ Branch 1 taken 60900 times.
92579 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
406 : buffer;
407
1/2
✓ Branch 1 taken 92535 times.
✗ Branch 2 not taken.
92579 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92535 times.
92535 if (!retval) {
409 if (size > kMaxStackAlloc) {
410 free(buffer);
411 }
412 return false;
413 }
414
415
2/2
✓ Branch 0 taken 31679 times.
✓ Branch 1 taken 60856 times.
92535 if (has_attachment) {
416 31679 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31679 times.
31679 if (frame->att_size() < attachment_size) {
418 if (size > kMaxStackAlloc) {
419 free(buffer);
420 }
421 return false;
422 }
423 31679 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
424 31679 + msg_size;
425 31679 memcpy(frame->attachment(), ptr_attachment, attachment_size);
426 31679 frame->set_att_size(attachment_size);
427 } else {
428 60856 frame->set_att_size(0);
429 }
430
2/2
✓ Branch 0 taken 31450 times.
✓ Branch 1 taken 61129 times.
92579 if (size > kMaxStackAlloc) {
431 31450 free(buffer);
432 }
433 92579 return true;
434 }
435
436
437 92590 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
438 unsigned char header[kHeaderSize];
439
1/2
✓ Branch 1 taken 92590 times.
✗ Branch 2 not taken.
92590 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
440
3/4
✓ Branch 0 taken 92590 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 92579 times.
92590 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
441 11 return false;
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92579 times.
92579 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
443 return false;
444 92579 *has_attachment = header[0] & kFlagHasAttachment;
445 92579 *size = header[1] + (header[2] << 8) + (header[3] << 16);
446
2/4
✓ Branch 0 taken 92579 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 92579 times.
✗ Branch 3 not taken.
92579 return (*size > 0) && (*size <= kMaxMsgSize);
447 }
448
449
450 92614 void CacheTransport::SendData(void *message,
451 uint32_t msg_size,
452 void *attachment,
453 uint32_t att_size) {
454 185228 const uint32_t total_size = msg_size + att_size
455
2/2
✓ Branch 0 taken 33399 times.
✓ Branch 1 taken 59215 times.
92614 + ((att_size > 0) ? kInnerHeaderSize : 0);
456
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92614 times.
92614 assert(total_size > 0);
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92614 times.
92614 assert(total_size <= kMaxMsgSize);
459
1/2
✓ Branch 1 taken 86878 times.
✗ Branch 2 not taken.
86878 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 59215 times.
✓ Branch 1 taken 33399 times.
92614 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
464 92614 header[1] = (total_size & 0x000000FF);
465 92614 header[2] = (total_size & 0x0000FF00) >> 8;
466 92614 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 92614 iov[0].iov_base = header;
473 92614 iov[0].iov_len = kHeaderSize;
474
475
2/2
✓ Branch 0 taken 33399 times.
✓ Branch 1 taken 59215 times.
92614 if (att_size > 0) {
476 33399 inner_header[0] = (msg_size & 0x000000FF);
477 33399 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
478 33399 iov[1].iov_base = inner_header;
479 33399 iov[1].iov_len = kInnerHeaderSize;
480 33399 iov[2].iov_base = message;
481 33399 iov[2].iov_len = msg_size;
482 33399 iov[3].iov_base = attachment;
483 33399 iov[3].iov_len = att_size;
484 } else {
485 59215 iov[1].iov_base = message;
486 59215 iov[1].iov_len = msg_size;
487 }
488
2/2
✓ Branch 0 taken 11022 times.
✓ Branch 1 taken 81592 times.
92614 if (flags_ & kFlagSendNonBlocking) {
489
2/4
✓ Branch 0 taken 11022 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 11022 times.
✗ Branch 4 not taken.
11022 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
490 11022 return;
491 }
492
3/4
✓ Branch 0 taken 48193 times.
✓ Branch 1 taken 33399 times.
✓ Branch 3 taken 81581 times.
✗ Branch 4 not taken.
81592 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
493
494
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 81581 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
81581 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
495 PANIC(kLogSyslogErr | kLogDebug,
496 "failed to write to external cache transport (%d), aborting", errno);
497 }
498 }
499
500 11022 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11022 times.
11022 assert(iovcnt > 0);
502 11022 unsigned total_size = 0;
503
2/2
✓ Branch 0 taken 22044 times.
✓ Branch 1 taken 11022 times.
33066 for (unsigned i = 0; i < iovcnt; ++i)
504 22044 total_size += iov[i].iov_len;
505 11022 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
506
507 11022 unsigned pos = 0;
508
2/2
✓ Branch 0 taken 22044 times.
✓ Branch 1 taken 11022 times.
33066 for (unsigned i = 0; i < iovcnt; ++i) {
509 22044 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
510 22044 pos += iov[i].iov_len;
511 }
512
513 11022 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11022 times.
11022 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 11022 }
523
524
525 92614 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
526 92614 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
527 92614 const int32_t size = msg_rpc->ByteSize();
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92614 times.
92614 assert(size > 0);
529 #ifdef __APPLE__
530 void *buffer = smalloc(size);
531 #else
532 92614 void *buffer = alloca(size);
533 #endif
534 92614 const bool retval = msg_rpc->SerializeToArray(buffer, size);
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92614 times.
92614 assert(retval);
536 92614 SendData(buffer, size, frame->attachment(), frame->att_size());
537 #ifdef __APPLE__
538 free(buffer);
539 #endif
540 92559 }
541