GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-05-19 11:45:12
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 "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 242680 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242680 times.
242680 assert(msg_typed_ != NULL);
36
1/2
✓ Branch 0 taken 242680 times.
✗ Branch 1 not taken.
242680 if (!is_wrapped_)
37 242680 WrapMsg();
38 242620 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 303466 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 303346 times.
303466 assert(msg_rpc_.IsInitialized());
48
2/2
✓ Branch 0 taken 255896 times.
✓ Branch 1 taken 47450 times.
303346 if (msg_typed_ == NULL)
49 255896 UnwrapMsg();
50 303286 return msg_typed_;
51 }
52
53
54 303466 CacheTransport::Frame::Frame()
55 303466 : owns_msg_typed_(false)
56 303466 , msg_typed_(NULL)
57 303466 , attachment_(NULL)
58 303466 , att_size_(0)
59 303466 , is_wrapped_(false)
60 303466 , is_msg_out_of_band_(false) { }
61
62
63 242650 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
64 242680 : owns_msg_typed_(false)
65 242680 , msg_typed_(m)
66 242680 , attachment_(NULL)
67 242680 , att_size_(0)
68 242680 , is_wrapped_(false)
69 242650 , is_msg_out_of_band_(false) { }
70
71
72 546026 CacheTransport::Frame::~Frame() { Reset(0); }
73
74
75 47450 bool CacheTransport::Frame::IsMsgOutOfBand() {
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47450 times.
47450 assert(msg_rpc_.IsInitialized());
77
1/2
✓ Branch 0 taken 47450 times.
✗ Branch 1 not taken.
47450 if (msg_typed_ == NULL)
78 47450 UnwrapMsg();
79 47450 return is_msg_out_of_band_;
80 }
81
82
83 60840 void CacheTransport::Frame::MergeFrom(const Frame &other) {
84 60840 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
85 60840 owns_msg_typed_ = true;
86
2/2
✓ Branch 0 taken 54000 times.
✓ Branch 1 taken 6840 times.
60840 if (other.att_size_ > 0) {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54000 times.
54000 assert(att_size_ >= other.att_size_);
88 54000 memcpy(attachment_, other.attachment_, other.att_size_);
89 54000 att_size_ = other.att_size_;
90 }
91 60840 }
92
93
94 242626 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
95 242626 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242596 times.
242596 if (!retval)
97 return false;
98
99 // Cleanup typed message when Frame leaves scope
100 242596 owns_msg_typed_ = true;
101 242596 return true;
102 }
103
104
105 546056 void CacheTransport::Frame::Release() {
106
2/2
✓ Branch 0 taken 303376 times.
✓ Branch 1 taken 242680 times.
546056 if (owns_msg_typed_)
107 303376 return;
108
109 242680 msg_rpc_.release_msg_refcount_req();
110 242710 msg_rpc_.release_msg_refcount_reply();
111 242680 msg_rpc_.release_msg_read_req();
112 242650 msg_rpc_.release_msg_read_reply();
113 242650 msg_rpc_.release_msg_object_info_req();
114 242620 msg_rpc_.release_msg_object_info_reply();
115 242680 msg_rpc_.release_msg_store_req();
116 242680 msg_rpc_.release_msg_store_abort_req();
117 242650 msg_rpc_.release_msg_store_reply();
118 242650 msg_rpc_.release_msg_handshake();
119 242650 msg_rpc_.release_msg_handshake_ack();
120 242680 msg_rpc_.release_msg_quit();
121 242620 msg_rpc_.release_msg_ioctl();
122 242590 msg_rpc_.release_msg_info_req();
123 242620 msg_rpc_.release_msg_info_reply();
124 242590 msg_rpc_.release_msg_shrink_req();
125 242620 msg_rpc_.release_msg_shrink_reply();
126 242590 msg_rpc_.release_msg_list_req();
127 242620 msg_rpc_.release_msg_list_reply();
128 242590 msg_rpc_.release_msg_detach();
129 242590 msg_rpc_.release_msg_breadcrumb_store_req();
130 242590 msg_rpc_.release_msg_breadcrumb_load_req();
131 242560 msg_rpc_.release_msg_breadcrumb_reply();
132 }
133
134
135 546056 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
136 546056 msg_typed_ = NULL;
137 546056 att_size_ = original_att_size;
138 546056 is_wrapped_ = false;
139 546056 is_msg_out_of_band_ = false;
140 546056 Release();
141 545936 msg_rpc_.Clear();
142 546026 owns_msg_typed_ = false;
143 546026 }
144
145
146 242680 void CacheTransport::Frame::WrapMsg() {
147
2/2
✓ Branch 3 taken 566 times.
✓ Branch 4 taken 242054 times.
242680 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
148 566 msg_rpc_.set_allocated_msg_handshake(
149 566 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
150
2/2
✓ Branch 3 taken 540 times.
✓ Branch 4 taken 241544 times.
242054 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
151 540 msg_rpc_.set_allocated_msg_handshake_ack(
152 540 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
153
2/2
✓ Branch 3 taken 564 times.
✓ Branch 4 taken 240980 times.
241544 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
154 564 msg_rpc_.set_allocated_msg_quit(
155 564 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
156
2/2
✓ Branch 3 taken 120 times.
✓ Branch 4 taken 240860 times.
240980 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
157 120 msg_rpc_.set_allocated_msg_ioctl(
158 120 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
159
2/2
✓ Branch 3 taken 19424 times.
✓ Branch 4 taken 221466 times.
240860 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
160 19424 msg_rpc_.set_allocated_msg_refcount_req(
161 19424 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
162
2/2
✓ Branch 3 taken 16920 times.
✓ Branch 4 taken 204516 times.
221466 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
163 16920 msg_rpc_.set_allocated_msg_refcount_reply(
164 16920 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
165
2/2
✓ Branch 3 taken 504 times.
✓ Branch 4 taken 204042 times.
204516 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
166 504 msg_rpc_.set_allocated_msg_object_info_req(
167 504 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
168
2/2
✓ Branch 3 taken 480 times.
✓ Branch 4 taken 203532 times.
204042 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
169 480 msg_rpc_.set_allocated_msg_object_info_reply(
170 480 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
171
2/2
✓ Branch 3 taken 67032 times.
✓ Branch 4 taken 136530 times.
203532 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
172 67032 msg_rpc_.set_allocated_msg_read_req(
173 67032 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
174
2/2
✓ Branch 3 taken 66330 times.
✓ Branch 4 taken 70200 times.
136530 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
175 66330 msg_rpc_.set_allocated_msg_read_reply(
176 66330 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
177
2/2
✓ Branch 3 taken 20654 times.
✓ Branch 4 taken 49546 times.
70200 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
178 20654 msg_rpc_.set_allocated_msg_store_req(
179 20654 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
180
2/2
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 49516 times.
49546 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
181 30 msg_rpc_.set_allocated_msg_store_abort_req(
182 30 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
183
2/2
✓ Branch 3 taken 18270 times.
✓ Branch 4 taken 31246 times.
49516 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
184 18270 msg_rpc_.set_allocated_msg_store_reply(
185 18270 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
186
2/2
✓ Branch 3 taken 144 times.
✓ Branch 4 taken 31102 times.
31246 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
187 144 msg_rpc_.set_allocated_msg_info_req(
188 144 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
189
2/2
✓ Branch 3 taken 120 times.
✓ Branch 4 taken 30982 times.
31102 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
190 120 msg_rpc_.set_allocated_msg_info_reply(
191 120 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
192
2/2
✓ Branch 3 taken 68 times.
✓ Branch 4 taken 30914 times.
30982 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
193 68 msg_rpc_.set_allocated_msg_shrink_req(
194 68 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
195
2/2
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 30854 times.
30914 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
196 60 msg_rpc_.set_allocated_msg_shrink_reply(
197 60 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
198
2/2
✓ Branch 3 taken 308 times.
✓ Branch 4 taken 30546 times.
30854 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
199 308 msg_rpc_.set_allocated_msg_list_req(
200 308 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
201
2/2
✓ Branch 3 taken 300 times.
✓ Branch 4 taken 30246 times.
30546 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
202 300 msg_rpc_.set_allocated_msg_list_reply(
203 300 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
204
2/2
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 30214 times.
30246 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
205 32 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
206 32 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
207
2/2
✓ Branch 3 taken 64 times.
✓ Branch 4 taken 30150 times.
30214 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
208 64 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
209 64 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
210
2/2
✓ Branch 3 taken 90 times.
✓ Branch 4 taken 30060 times.
30150 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
211 90 msg_rpc_.set_allocated_msg_breadcrumb_reply(
212 90 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
213
1/2
✓ Branch 3 taken 30060 times.
✗ Branch 4 not taken.
30060 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
214 30060 msg_rpc_.set_allocated_msg_detach(
215 30060 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
216 30060 is_msg_out_of_band_ = true;
217 } else {
218 // Unexpected message type, should never happen
219 PANIC(NULL);
220 }
221 242680 is_wrapped_ = true;
222 242680 }
223
224
225 303346 void CacheTransport::Frame::UnwrapMsg() {
226
2/2
✓ Branch 1 taken 540 times.
✓ Branch 2 taken 302806 times.
303346 if (msg_rpc_.has_msg_handshake()) {
227 540 msg_typed_ = msg_rpc_.mutable_msg_handshake();
228
2/2
✓ Branch 1 taken 566 times.
✓ Branch 2 taken 302240 times.
302806 } else if (msg_rpc_.has_msg_handshake_ack()) {
229 566 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
230
2/2
✓ Branch 1 taken 540 times.
✓ Branch 2 taken 301640 times.
302240 } else if (msg_rpc_.has_msg_quit()) {
231 540 msg_typed_ = msg_rpc_.mutable_msg_quit();
232
2/2
✓ Branch 1 taken 120 times.
✓ Branch 2 taken 301490 times.
301640 } else if (msg_rpc_.has_msg_ioctl()) {
233 120 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
234
2/2
✓ Branch 1 taken 16920 times.
✓ Branch 2 taken 284630 times.
301490 } else if (msg_rpc_.has_msg_refcount_req()) {
235 16920 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
236
2/2
✓ Branch 1 taken 19994 times.
✓ Branch 2 taken 264456 times.
284630 } else if (msg_rpc_.has_msg_refcount_reply()) {
237 19994 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
238
2/2
✓ Branch 1 taken 480 times.
✓ Branch 2 taken 264066 times.
264456 } else if (msg_rpc_.has_msg_object_info_req()) {
239 480 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
240
2/2
✓ Branch 1 taken 714 times.
✓ Branch 2 taken 263352 times.
264066 } else if (msg_rpc_.has_msg_object_info_reply()) {
241 714 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
242
2/2
✓ Branch 1 taken 66330 times.
✓ Branch 2 taken 196992 times.
263352 } else if (msg_rpc_.has_msg_read_req()) {
243 66330 msg_typed_ = msg_rpc_.mutable_msg_read_req();
244
2/2
✓ Branch 1 taken 121002 times.
✓ Branch 2 taken 76170 times.
196992 } else if (msg_rpc_.has_msg_read_reply()) {
245 121002 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
246
2/2
✓ Branch 1 taken 18240 times.
✓ Branch 2 taken 57930 times.
76170 } else if (msg_rpc_.has_msg_store_req()) {
247 18240 msg_typed_ = msg_rpc_.mutable_msg_store_req();
248
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 57900 times.
57930 } else if (msg_rpc_.has_msg_store_abort_req()) {
249 30 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
250
2/2
✓ Branch 1 taken 26684 times.
✓ Branch 2 taken 31216 times.
57900 } else if (msg_rpc_.has_msg_store_reply()) {
251 26684 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
252
2/2
✓ Branch 1 taken 120 times.
✓ Branch 2 taken 31096 times.
31216 } else if (msg_rpc_.has_msg_info_req()) {
253 120 msg_typed_ = msg_rpc_.mutable_msg_info_req();
254
2/2
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 30952 times.
31096 } else if (msg_rpc_.has_msg_info_reply()) {
255 144 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
256
2/2
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 30892 times.
30952 } else if (msg_rpc_.has_msg_shrink_req()) {
257 60 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
258
2/2
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 30824 times.
30892 } else if (msg_rpc_.has_msg_shrink_reply()) {
259 68 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
260
2/2
✓ Branch 1 taken 300 times.
✓ Branch 2 taken 30524 times.
30824 } else if (msg_rpc_.has_msg_list_req()) {
261 300 msg_typed_ = msg_rpc_.mutable_msg_list_req();
262
2/2
✓ Branch 1 taken 308 times.
✓ Branch 2 taken 30216 times.
30524 } else if (msg_rpc_.has_msg_list_reply()) {
263 308 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
264
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30186 times.
30216 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
265 30 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
266
2/2
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 30126 times.
30186 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
267 60 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
268
2/2
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 30030 times.
30126 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
269 96 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
270
1/2
✓ Branch 1 taken 30030 times.
✗ Branch 2 not taken.
30030 } else if (msg_rpc_.has_msg_detach()) {
271 30030 msg_typed_ = msg_rpc_.mutable_msg_detach();
272 30030 is_msg_out_of_band_ = true;
273 } else {
274 // Unexpected message type, should never happen
275 PANIC(NULL);
276 }
277 303376 }
278
279
280 //------------------------------------------------------------------------------
281
282
283 566 CacheTransport::CacheTransport(int fd_connection)
284 566 : fd_connection_(fd_connection), flags_(0) {
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 566 times.
566 assert(fd_connection_ >= 0);
286 566 }
287
288
289 133830 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
290 133830 : fd_connection_(fd_connection), flags_(flags) {
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133830 times.
133830 assert(fd_connection_ >= 0);
292 133830 }
293
294
295 6041445 void CacheTransport::FillMsgHash(const shash::Any &hash,
296 cvmfs::MsgHash *msg_hash) {
297
3/4
✓ Branch 0 taken 6041359 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
6041445 switch (hash.algorithm) {
298 6041359 case shash::kSha1:
299 6041359 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
300 6041359 break;
301 12 case shash::kRmd160:
302 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
303 12 break;
304 74 case shash::kShake128:
305 74 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
306 74 break;
307 default:
308 PANIC(NULL);
309 }
310 6041445 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
311 6041445 }
312
313
314 20646 void CacheTransport::FillObjectType(int object_flags,
315 cvmfs::EnumObjectType *wire_type) {
316 20646 *wire_type = cvmfs::OBJECT_REGULAR;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20646 times.
20646 if (object_flags & CacheManager::kLabelCatalog)
318 *wire_type = cvmfs::OBJECT_CATALOG;
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20646 times.
20646 if (object_flags & CacheManager::kLabelVolatile)
320 *wire_type = cvmfs::OBJECT_VOLATILE;
321 20646 }
322
323
324 102032 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
325 shash::Any *hash) {
326
2/4
✓ Branch 1 taken 101970 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
✗ Branch 4 not taken.
102032 switch (msg_hash.algorithm()) {
327 101970 case cvmfs::HASH_SHA1:
328 101970 hash->algorithm = shash::kSha1;
329 101970 break;
330 case cvmfs::HASH_RIPEMD160:
331 hash->algorithm = shash::kRmd160;
332 break;
333 62 case cvmfs::HASH_SHAKE128:
334 62 hash->algorithm = shash::kShake128;
335 62 break;
336 default:
337 return false;
338 }
339 102032 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
340
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 102032 times.
102032 if (msg_hash.digest().length() != digest_size)
341 return false;
342 102032 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
343 102032 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 242656 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
366 uint32_t size;
367 bool has_attachment;
368
1/2
✓ Branch 1 taken 242656 times.
✗ Branch 2 not taken.
242656 bool retval = RecvHeader(&size, &has_attachment);
369
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 242626 times.
242656 if (!retval)
370 30 return false;
371
372 void *buffer;
373
2/2
✓ Branch 0 taken 157699 times.
✓ Branch 1 taken 84927 times.
242626 if (size <= kMaxStackAlloc)
374 157699 buffer = alloca(size);
375 else
376 84927 buffer = smalloc(size);
377
1/2
✓ Branch 1 taken 242626 times.
✗ Branch 2 not taken.
242626 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
378
2/4
✓ Branch 0 taken 242626 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 242626 times.
242626 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
379 if (size > kMaxStackAlloc) {
380 free(buffer);
381 }
382 return false;
383 }
384
385 242626 uint32_t msg_size = size;
386
2/2
✓ Branch 0 taken 85206 times.
✓ Branch 1 taken 157420 times.
242626 if (has_attachment) {
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85206 times.
85206 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 85206 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
396 85206 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85206 times.
85206 if ((msg_size + kInnerHeaderSize) > size) {
398 if (size > kMaxStackAlloc) {
399 free(buffer);
400 }
401 return false;
402 }
403 }
404
405 242626 void *ptr_msg = has_attachment
406
2/2
✓ Branch 0 taken 85206 times.
✓ Branch 1 taken 157420 times.
242626 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
407 : buffer;
408
1/2
✓ Branch 1 taken 242596 times.
✗ Branch 2 not taken.
242626 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242596 times.
242596 if (!retval) {
410 if (size > kMaxStackAlloc) {
411 free(buffer);
412 }
413 return false;
414 }
415
416
2/2
✓ Branch 0 taken 85206 times.
✓ Branch 1 taken 157390 times.
242596 if (has_attachment) {
417 85206 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 85206 times.
85206 if (frame->att_size() < attachment_size) {
419 if (size > kMaxStackAlloc) {
420 free(buffer);
421 }
422 return false;
423 }
424 85206 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
425 85206 + msg_size;
426 85206 memcpy(frame->attachment(), ptr_attachment, attachment_size);
427 85206 frame->set_att_size(attachment_size);
428 } else {
429 157390 frame->set_att_size(0);
430 }
431
2/2
✓ Branch 0 taken 84927 times.
✓ Branch 1 taken 157669 times.
242596 if (size > kMaxStackAlloc) {
432 84927 free(buffer);
433 }
434 242596 return true;
435 }
436
437
438 242656 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
439 unsigned char header[kHeaderSize];
440
1/2
✓ Branch 1 taken 242656 times.
✗ Branch 2 not taken.
242656 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
441
3/4
✓ Branch 0 taken 242656 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 242626 times.
242656 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
442 30 return false;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242626 times.
242626 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
444 return false;
445 242626 *has_attachment = header[0] & kFlagHasAttachment;
446 242626 *size = header[1] + (header[2] << 8) + (header[3] << 16);
447
2/4
✓ Branch 0 taken 242626 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 242626 times.
✗ Branch 3 not taken.
242626 return (*size > 0) && (*size <= kMaxMsgSize);
448 }
449
450
451 242680 void CacheTransport::SendData(void *message,
452 uint32_t msg_size,
453 void *attachment,
454 uint32_t att_size) {
455 485360 const uint32_t total_size = msg_size + att_size
456
2/2
✓ Branch 0 taken 86922 times.
✓ Branch 1 taken 155758 times.
242680 + ((att_size > 0) ? kInnerHeaderSize : 0);
457
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242680 times.
242680 assert(total_size > 0);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242680 times.
242680 assert(total_size <= kMaxMsgSize);
460
1/2
✓ Branch 1 taken 236940 times.
✗ Branch 2 not taken.
236940 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 155758 times.
✓ Branch 1 taken 86922 times.
242680 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
465 242680 header[1] = (total_size & 0x000000FF);
466 242680 header[2] = (total_size & 0x0000FF00) >> 8;
467 242680 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 242680 iov[0].iov_base = header;
474 242680 iov[0].iov_len = kHeaderSize;
475
476
2/2
✓ Branch 0 taken 86922 times.
✓ Branch 1 taken 155758 times.
242680 if (att_size > 0) {
477 86922 inner_header[0] = (msg_size & 0x000000FF);
478 86922 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
479 86922 iov[1].iov_base = inner_header;
480 86922 iov[1].iov_len = kInnerHeaderSize;
481 86922 iov[2].iov_base = message;
482 86922 iov[2].iov_len = msg_size;
483 86922 iov[3].iov_base = attachment;
484 86922 iov[3].iov_len = att_size;
485 } else {
486 155758 iov[1].iov_base = message;
487 155758 iov[1].iov_len = msg_size;
488 }
489
2/2
✓ Branch 0 taken 30060 times.
✓ Branch 1 taken 212620 times.
242680 if (flags_ & kFlagSendNonBlocking) {
490
2/4
✓ Branch 0 taken 30060 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 30060 times.
✗ Branch 4 not taken.
30060 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
491 30060 return;
492 }
493
3/4
✓ Branch 0 taken 125698 times.
✓ Branch 1 taken 86922 times.
✓ Branch 3 taken 212530 times.
✗ Branch 4 not taken.
212620 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
494
495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 212530 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
212530 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
496 PANIC(kLogSyslogErr | kLogDebug,
497 "failed to write to external cache transport (%d), aborting", errno);
498 }
499 }
500
501 30060 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30060 times.
30060 assert(iovcnt > 0);
503 30060 unsigned total_size = 0;
504
2/2
✓ Branch 0 taken 60120 times.
✓ Branch 1 taken 30060 times.
90180 for (unsigned i = 0; i < iovcnt; ++i)
505 60120 total_size += iov[i].iov_len;
506 30060 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
507
508 30060 unsigned pos = 0;
509
2/2
✓ Branch 0 taken 60120 times.
✓ Branch 1 taken 30060 times.
90180 for (unsigned i = 0; i < iovcnt; ++i) {
510 60120 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
511 60120 pos += iov[i].iov_len;
512 }
513
514 30060 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30060 times.
30060 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 30060 }
524
525
526 242680 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
527 242680 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
528 242620 const int32_t size = msg_rpc->ByteSize();
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242590 times.
242590 assert(size > 0);
530 #ifdef __APPLE__
531 void *buffer = smalloc(size);
532 #else
533 242590 void *buffer = alloca(size);
534 #endif
535 242590 const bool retval = msg_rpc->SerializeToArray(buffer, size);
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242650 times.
242650 assert(retval);
537 242650 SendData(buffer, size, frame->attachment(), frame->att_size());
538 #ifdef __APPLE__
539 free(buffer);
540 #endif
541 242620 }
542