GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2025-07-27 02:42:09
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 132110 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132110 times.
132110 assert(msg_typed_ != NULL);
35
1/2
✓ Branch 0 taken 132110 times.
✗ Branch 1 not taken.
132110 if (!is_wrapped_)
36 132110 WrapMsg();
37 132110 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 164518 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
46
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 164470 times.
164518 assert(msg_rpc_.IsInitialized());
47
2/2
✓ Branch 0 taken 136506 times.
✓ Branch 1 taken 27964 times.
164470 if (msg_typed_ == NULL)
48 136506 UnwrapMsg();
49 164470 return msg_typed_;
50 }
51
52
53 164518 CacheTransport::Frame::Frame()
54 164518 : owns_msg_typed_(false)
55 164518 , msg_typed_(NULL)
56 164518 , attachment_(NULL)
57 164518 , att_size_(0)
58 164518 , is_wrapped_(false)
59 164518 , is_msg_out_of_band_(false) { }
60
61
62 132110 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
63 132094 : owns_msg_typed_(false)
64 132094 , msg_typed_(m)
65 132094 , attachment_(NULL)
66 132094 , att_size_(0)
67 132094 , is_wrapped_(false)
68 132110 , is_msg_out_of_band_(false) { }
69
70
71 296628 CacheTransport::Frame::~Frame() { Reset(0); }
72
73
74 27964 bool CacheTransport::Frame::IsMsgOutOfBand() {
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27964 times.
27964 assert(msg_rpc_.IsInitialized());
76
1/2
✓ Branch 0 taken 27964 times.
✗ Branch 1 not taken.
27964 if (msg_typed_ == NULL)
77 27964 UnwrapMsg();
78 27964 return is_msg_out_of_band_;
79 }
80
81
82 32448 void CacheTransport::Frame::MergeFrom(const Frame &other) {
83 32448 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
84 32448 owns_msg_typed_ = true;
85
2/2
✓ Branch 0 taken 28800 times.
✓ Branch 1 taken 3648 times.
32448 if (other.att_size_ > 0) {
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28800 times.
28800 assert(att_size_ >= other.att_size_);
87 28800 memcpy(attachment_, other.attachment_, other.att_size_);
88 28800 att_size_ = other.att_size_;
89 }
90 32448 }
91
92
93 132038 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
94 132038 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132054 times.
132054 if (!retval)
96 return false;
97
98 // Cleanup typed message when Frame leaves scope
99 132054 owns_msg_typed_ = true;
100 132054 return true;
101 }
102
103
104 296644 void CacheTransport::Frame::Release() {
105
2/2
✓ Branch 0 taken 164518 times.
✓ Branch 1 taken 132126 times.
296644 if (owns_msg_typed_)
106 164518 return;
107
108 132126 msg_rpc_.release_msg_refcount_req();
109 132126 msg_rpc_.release_msg_refcount_reply();
110 132126 msg_rpc_.release_msg_read_req();
111 132126 msg_rpc_.release_msg_read_reply();
112 132126 msg_rpc_.release_msg_object_info_req();
113 132126 msg_rpc_.release_msg_object_info_reply();
114 132126 msg_rpc_.release_msg_store_req();
115 132126 msg_rpc_.release_msg_store_abort_req();
116 132126 msg_rpc_.release_msg_store_reply();
117 132126 msg_rpc_.release_msg_handshake();
118 132126 msg_rpc_.release_msg_handshake_ack();
119 132110 msg_rpc_.release_msg_quit();
120 132126 msg_rpc_.release_msg_ioctl();
121 132126 msg_rpc_.release_msg_info_req();
122 132110 msg_rpc_.release_msg_info_reply();
123 132110 msg_rpc_.release_msg_shrink_req();
124 132110 msg_rpc_.release_msg_shrink_reply();
125 132110 msg_rpc_.release_msg_list_req();
126 132126 msg_rpc_.release_msg_list_reply();
127 132110 msg_rpc_.release_msg_detach();
128 132094 msg_rpc_.release_msg_breadcrumb_store_req();
129 132094 msg_rpc_.release_msg_breadcrumb_load_req();
130 132110 msg_rpc_.release_msg_breadcrumb_reply();
131 }
132
133
134 296644 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
135 296644 msg_typed_ = NULL;
136 296644 att_size_ = original_att_size;
137 296644 is_wrapped_ = false;
138 296644 is_msg_out_of_band_ = false;
139 296644 Release();
140 296612 msg_rpc_.Clear();
141 296564 owns_msg_typed_ = false;
142 296564 }
143
144
145 132110 void CacheTransport::Frame::WrapMsg() {
146
2/2
✓ Branch 3 taken 314 times.
✓ Branch 4 taken 131796 times.
132110 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
147 314 msg_rpc_.set_allocated_msg_handshake(
148 314 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
149
2/2
✓ Branch 3 taken 288 times.
✓ Branch 4 taken 131508 times.
131796 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
150 288 msg_rpc_.set_allocated_msg_handshake_ack(
151 288 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
152
2/2
✓ Branch 3 taken 312 times.
✓ Branch 4 taken 131196 times.
131508 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
153 312 msg_rpc_.set_allocated_msg_quit(
154 312 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
155
2/2
✓ Branch 3 taken 64 times.
✓ Branch 4 taken 131132 times.
131196 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
156 64 msg_rpc_.set_allocated_msg_ioctl(
157 64 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
158
2/2
✓ Branch 3 taken 11528 times.
✓ Branch 4 taken 119604 times.
131132 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
159 11528 msg_rpc_.set_allocated_msg_refcount_req(
160 11528 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
161
2/2
✓ Branch 3 taken 9024 times.
✓ Branch 4 taken 110580 times.
119604 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
162 9024 msg_rpc_.set_allocated_msg_refcount_reply(
163 9024 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
164
2/2
✓ Branch 3 taken 280 times.
✓ Branch 4 taken 110300 times.
110580 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
165 280 msg_rpc_.set_allocated_msg_object_info_req(
166 280 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
167
2/2
✓ Branch 3 taken 256 times.
✓ Branch 4 taken 110044 times.
110300 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
168 256 msg_rpc_.set_allocated_msg_object_info_reply(
169 256 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
170
2/2
✓ Branch 3 taken 36080 times.
✓ Branch 4 taken 73964 times.
110044 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
171 36080 msg_rpc_.set_allocated_msg_read_req(
172 36080 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
173
2/2
✓ Branch 3 taken 35376 times.
✓ Branch 4 taken 38588 times.
73964 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
174 35376 msg_rpc_.set_allocated_msg_read_reply(
175 35376 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
176
2/2
✓ Branch 3 taken 12142 times.
✓ Branch 4 taken 26446 times.
38588 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
177 12142 msg_rpc_.set_allocated_msg_store_req(
178 12142 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
179
2/2
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 26430 times.
26446 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
180 16 msg_rpc_.set_allocated_msg_store_abort_req(
181 16 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
182
2/2
✓ Branch 3 taken 9744 times.
✓ Branch 4 taken 16686 times.
26430 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
183 9744 msg_rpc_.set_allocated_msg_store_reply(
184 9744 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
185
2/2
✓ Branch 3 taken 88 times.
✓ Branch 4 taken 16598 times.
16686 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
186 88 msg_rpc_.set_allocated_msg_info_req(
187 88 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
188
2/2
✓ Branch 3 taken 64 times.
✓ Branch 4 taken 16534 times.
16598 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
189 64 msg_rpc_.set_allocated_msg_info_reply(
190 64 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
191
2/2
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 16494 times.
16534 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
192 40 msg_rpc_.set_allocated_msg_shrink_req(
193 40 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
194
2/2
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 16462 times.
16494 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
195 32 msg_rpc_.set_allocated_msg_shrink_reply(
196 32 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
197
2/2
✓ Branch 3 taken 168 times.
✓ Branch 4 taken 16294 times.
16462 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
198 168 msg_rpc_.set_allocated_msg_list_req(
199 168 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
200
2/2
✓ Branch 3 taken 160 times.
✓ Branch 4 taken 16134 times.
16294 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
201 160 msg_rpc_.set_allocated_msg_list_reply(
202 160 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
203
2/2
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 16116 times.
16134 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
204 18 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
205 18 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
206
2/2
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 16080 times.
16116 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
207 36 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
208 36 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
209
2/2
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 16032 times.
16080 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
210 48 msg_rpc_.set_allocated_msg_breadcrumb_reply(
211 48 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
212
1/2
✓ Branch 3 taken 16032 times.
✗ Branch 4 not taken.
16032 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
213 16032 msg_rpc_.set_allocated_msg_detach(
214 16032 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
215 16032 is_msg_out_of_band_ = true;
216 } else {
217 // Unexpected message type, should never happen
218 PANIC(NULL);
219 }
220 132110 is_wrapped_ = true;
221 132110 }
222
223
224 164454 void CacheTransport::Frame::UnwrapMsg() {
225
2/2
✓ Branch 1 taken 288 times.
✓ Branch 2 taken 164150 times.
164454 if (msg_rpc_.has_msg_handshake()) {
226 288 msg_typed_ = msg_rpc_.mutable_msg_handshake();
227
2/2
✓ Branch 1 taken 314 times.
✓ Branch 2 taken 163868 times.
164150 } else if (msg_rpc_.has_msg_handshake_ack()) {
228 314 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
229
2/2
✓ Branch 1 taken 288 times.
✓ Branch 2 taken 163612 times.
163868 } else if (msg_rpc_.has_msg_quit()) {
230 288 msg_typed_ = msg_rpc_.mutable_msg_quit();
231
2/2
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 163516 times.
163612 } else if (msg_rpc_.has_msg_ioctl()) {
232 64 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
233
2/2
✓ Branch 1 taken 9024 times.
✓ Branch 2 taken 154492 times.
163516 } else if (msg_rpc_.has_msg_refcount_req()) {
234 9024 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
235
2/2
✓ Branch 1 taken 11816 times.
✓ Branch 2 taken 142676 times.
154492 } else if (msg_rpc_.has_msg_refcount_reply()) {
236 11816 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
237
2/2
✓ Branch 1 taken 256 times.
✓ Branch 2 taken 142436 times.
142676 } else if (msg_rpc_.has_msg_object_info_req()) {
238 256 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
239
2/2
✓ Branch 1 taken 424 times.
✓ Branch 2 taken 141996 times.
142436 } else if (msg_rpc_.has_msg_object_info_reply()) {
240 424 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
241
2/2
✓ Branch 1 taken 35376 times.
✓ Branch 2 taken 106636 times.
141996 } else if (msg_rpc_.has_msg_read_req()) {
242 35376 msg_typed_ = msg_rpc_.mutable_msg_read_req();
243
2/2
✓ Branch 1 taken 64880 times.
✓ Branch 2 taken 41772 times.
106636 } else if (msg_rpc_.has_msg_read_reply()) {
244 64880 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
245
2/2
✓ Branch 1 taken 9728 times.
✓ Branch 2 taken 32044 times.
41772 } else if (msg_rpc_.has_msg_store_req()) {
246 9728 msg_typed_ = msg_rpc_.mutable_msg_store_req();
247
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 32028 times.
32044 } else if (msg_rpc_.has_msg_store_abort_req()) {
248 16 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
249
2/2
✓ Branch 1 taken 15358 times.
✓ Branch 2 taken 16670 times.
32028 } else if (msg_rpc_.has_msg_store_reply()) {
250 15358 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
251
2/2
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 16606 times.
16670 } else if (msg_rpc_.has_msg_info_req()) {
252 64 msg_typed_ = msg_rpc_.mutable_msg_info_req();
253
2/2
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 16518 times.
16606 } else if (msg_rpc_.has_msg_info_reply()) {
254 88 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
255
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 16486 times.
16518 } else if (msg_rpc_.has_msg_shrink_req()) {
256 32 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
257
2/2
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 16446 times.
16486 } else if (msg_rpc_.has_msg_shrink_reply()) {
258 40 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
259
2/2
✓ Branch 1 taken 160 times.
✓ Branch 2 taken 16286 times.
16446 } else if (msg_rpc_.has_msg_list_req()) {
260 160 msg_typed_ = msg_rpc_.mutable_msg_list_req();
261
2/2
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 16118 times.
16286 } else if (msg_rpc_.has_msg_list_reply()) {
262 168 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
263
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 16102 times.
16118 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
264 16 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
265
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 16070 times.
16102 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
266 32 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
267
2/2
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 16016 times.
16070 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
268 54 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
269
1/2
✓ Branch 1 taken 16016 times.
✗ Branch 2 not taken.
16016 } else if (msg_rpc_.has_msg_detach()) {
270 16016 msg_typed_ = msg_rpc_.mutable_msg_detach();
271 16016 is_msg_out_of_band_ = true;
272 } else {
273 // Unexpected message type, should never happen
274 PANIC(NULL);
275 }
276 164518 }
277
278
279 //------------------------------------------------------------------------------
280
281
282 314 CacheTransport::CacheTransport(int fd_connection)
283 314 : fd_connection_(fd_connection), flags_(0) {
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 314 times.
314 assert(fd_connection_ >= 0);
285 314 }
286
287
288 71376 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
289 71376 : fd_connection_(fd_connection), flags_(flags) {
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71376 times.
71376 assert(fd_connection_ >= 0);
291 71376 }
292
293
294 3224519 void CacheTransport::FillMsgHash(const shash::Any &hash,
295 cvmfs::MsgHash *msg_hash) {
296
3/4
✓ Branch 0 taken 3224461 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
3224519 switch (hash.algorithm) {
297 3224461 case shash::kSha1:
298 3224461 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
299 3224461 break;
300 12 case shash::kRmd160:
301 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
302 12 break;
303 46 case shash::kShake128:
304 46 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
305 46 break;
306 default:
307 PANIC(NULL);
308 }
309 3224519 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
310 3224519 }
311
312
313 12134 void CacheTransport::FillObjectType(int object_flags,
314 cvmfs::EnumObjectType *wire_type) {
315 12134 *wire_type = cvmfs::OBJECT_REGULAR;
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12134 times.
12134 if (object_flags & CacheManager::kLabelCatalog)
317 *wire_type = cvmfs::OBJECT_CATALOG;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12134 times.
12134 if (object_flags & CacheManager::kLabelVolatile)
319 *wire_type = cvmfs::OBJECT_VOLATILE;
320 12134 }
321
322
323 54418 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
324 shash::Any *hash) {
325
2/4
✓ Branch 1 taken 54384 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
54418 switch (msg_hash.algorithm()) {
326 54384 case cvmfs::HASH_SHA1:
327 54384 hash->algorithm = shash::kSha1;
328 54384 break;
329 case cvmfs::HASH_RIPEMD160:
330 hash->algorithm = shash::kRmd160;
331 break;
332 34 case cvmfs::HASH_SHAKE128:
333 34 hash->algorithm = shash::kShake128;
334 34 break;
335 default:
336 return false;
337 }
338 54418 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
339
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 54418 times.
54418 if (msg_hash.digest().length() != digest_size)
340 return false;
341 54418 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
342 54418 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 132086 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
365 uint32_t size;
366 bool has_attachment;
367
1/2
✓ Branch 1 taken 132086 times.
✗ Branch 2 not taken.
132086 bool retval = RecvHeader(&size, &has_attachment);
368
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 132070 times.
132086 if (!retval)
369 16 return false;
370
371 void *buffer;
372
2/2
✓ Branch 0 taken 86551 times.
✓ Branch 1 taken 45519 times.
132070 if (size <= kMaxStackAlloc)
373 86551 buffer = alloca(size);
374 else
375 45519 buffer = smalloc(size);
376
1/2
✓ Branch 1 taken 132070 times.
✗ Branch 2 not taken.
132070 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
377
2/4
✓ Branch 0 taken 132070 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 132070 times.
132070 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
378 if (size > kMaxStackAlloc) {
379 free(buffer);
380 }
381 return false;
382 }
383
384 132070 uint32_t msg_size = size;
385
2/2
✓ Branch 0 taken 45770 times.
✓ Branch 1 taken 86300 times.
132070 if (has_attachment) {
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45770 times.
45770 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 45770 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
395 45770 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45770 times.
45770 if ((msg_size + kInnerHeaderSize) > size) {
397 if (size > kMaxStackAlloc) {
398 free(buffer);
399 }
400 return false;
401 }
402 }
403
404 132070 void *ptr_msg = has_attachment
405
2/2
✓ Branch 0 taken 45770 times.
✓ Branch 1 taken 86300 times.
132070 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
406 : buffer;
407
1/2
✓ Branch 1 taken 132054 times.
✗ Branch 2 not taken.
132070 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132054 times.
132054 if (!retval) {
409 if (size > kMaxStackAlloc) {
410 free(buffer);
411 }
412 return false;
413 }
414
415
2/2
✓ Branch 0 taken 45770 times.
✓ Branch 1 taken 86284 times.
132054 if (has_attachment) {
416 45770 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45770 times.
45770 if (frame->att_size() < attachment_size) {
418 if (size > kMaxStackAlloc) {
419 free(buffer);
420 }
421 return false;
422 }
423 45770 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
424 45770 + msg_size;
425 45770 memcpy(frame->attachment(), ptr_attachment, attachment_size);
426 45770 frame->set_att_size(attachment_size);
427 } else {
428 86284 frame->set_att_size(0);
429 }
430
2/2
✓ Branch 0 taken 45519 times.
✓ Branch 1 taken 86551 times.
132070 if (size > kMaxStackAlloc) {
431 45519 free(buffer);
432 }
433 132070 return true;
434 }
435
436
437 132086 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
438 unsigned char header[kHeaderSize];
439
1/2
✓ Branch 1 taken 132086 times.
✗ Branch 2 not taken.
132086 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
440
3/4
✓ Branch 0 taken 132086 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 132070 times.
132086 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
441 16 return false;
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132070 times.
132070 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
443 return false;
444 132070 *has_attachment = header[0] & kFlagHasAttachment;
445 132070 *size = header[1] + (header[2] << 8) + (header[3] << 16);
446
2/4
✓ Branch 0 taken 132070 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 132070 times.
✗ Branch 3 not taken.
132070 return (*size > 0) && (*size <= kMaxMsgSize);
447 }
448
449
450 132110 void CacheTransport::SendData(void *message,
451 uint32_t msg_size,
452 void *attachment,
453 uint32_t att_size) {
454 264220 const uint32_t total_size = msg_size + att_size
455
2/2
✓ Branch 0 taken 47484 times.
✓ Branch 1 taken 84626 times.
132110 + ((att_size > 0) ? kInnerHeaderSize : 0);
456
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132110 times.
132110 assert(total_size > 0);
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132110 times.
132110 assert(total_size <= kMaxMsgSize);
459
1/2
✓ Branch 1 taken 126368 times.
✗ Branch 2 not taken.
126368 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 84626 times.
✓ Branch 1 taken 47484 times.
132110 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
464 132110 header[1] = (total_size & 0x000000FF);
465 132110 header[2] = (total_size & 0x0000FF00) >> 8;
466 132110 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 132110 iov[0].iov_base = header;
473 132110 iov[0].iov_len = kHeaderSize;
474
475
2/2
✓ Branch 0 taken 47484 times.
✓ Branch 1 taken 84626 times.
132110 if (att_size > 0) {
476 47484 inner_header[0] = (msg_size & 0x000000FF);
477 47484 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
478 47484 iov[1].iov_base = inner_header;
479 47484 iov[1].iov_len = kInnerHeaderSize;
480 47484 iov[2].iov_base = message;
481 47484 iov[2].iov_len = msg_size;
482 47484 iov[3].iov_base = attachment;
483 47484 iov[3].iov_len = att_size;
484 } else {
485 84626 iov[1].iov_base = message;
486 84626 iov[1].iov_len = msg_size;
487 }
488
2/2
✓ Branch 0 taken 16032 times.
✓ Branch 1 taken 116078 times.
132110 if (flags_ & kFlagSendNonBlocking) {
489
2/4
✓ Branch 0 taken 16032 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 16032 times.
✗ Branch 4 not taken.
16032 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
490 16032 return;
491 }
492
3/4
✓ Branch 0 taken 68594 times.
✓ Branch 1 taken 47484 times.
✓ Branch 3 taken 116078 times.
✗ Branch 4 not taken.
116078 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
493
494
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 116078 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
116078 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
495 PANIC(kLogSyslogErr | kLogDebug,
496 "failed to write to external cache transport (%d), aborting", errno);
497 }
498 }
499
500 16032 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16032 times.
16032 assert(iovcnt > 0);
502 16032 unsigned total_size = 0;
503
2/2
✓ Branch 0 taken 32064 times.
✓ Branch 1 taken 16032 times.
48096 for (unsigned i = 0; i < iovcnt; ++i)
504 32064 total_size += iov[i].iov_len;
505 16032 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
506
507 16032 unsigned pos = 0;
508
2/2
✓ Branch 0 taken 32064 times.
✓ Branch 1 taken 16032 times.
48096 for (unsigned i = 0; i < iovcnt; ++i) {
509 32064 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
510 32064 pos += iov[i].iov_len;
511 }
512
513 16032 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16032 times.
16032 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 16032 }
523
524
525 132110 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
526 132110 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
527 132110 const int32_t size = msg_rpc->ByteSize();
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132110 times.
132110 assert(size > 0);
529 #ifdef __APPLE__
530 void *buffer = smalloc(size);
531 #else
532 132110 void *buffer = alloca(size);
533 #endif
534 132110 const bool retval = msg_rpc->SerializeToArray(buffer, size);
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132110 times.
132110 assert(retval);
536 132110 SendData(buffer, size, frame->attachment(), frame->att_size());
537 #ifdef __APPLE__
538 free(buffer);
539 #endif
540 132062 }
541