GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-08-09 02:40:25
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 140014 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140014 times.
140014 assert(msg_typed_ != NULL);
36
1/2
✓ Branch 0 taken 140014 times.
✗ Branch 1 not taken.
140014 if (!is_wrapped_)
37 140014 WrapMsg();
38 140014 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 174449 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 174432 times.
174449 assert(msg_rpc_.IsInitialized());
48
2/2
✓ Branch 0 taken 145070 times.
✓ Branch 1 taken 29362 times.
174432 if (msg_typed_ == NULL)
49 145070 UnwrapMsg();
50 174381 return msg_typed_;
51 }
52
53
54 174432 CacheTransport::Frame::Frame()
55 174432 : owns_msg_typed_(false)
56 174432 , msg_typed_(NULL)
57 174432 , attachment_(NULL)
58 174432 , att_size_(0)
59 174432 , is_wrapped_(false)
60 174432 , is_msg_out_of_band_(false) { }
61
62
63 140014 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
64 140014 : owns_msg_typed_(false)
65 140014 , msg_typed_(m)
66 140014 , attachment_(NULL)
67 140014 , att_size_(0)
68 140014 , is_wrapped_(false)
69 140014 , is_msg_out_of_band_(false) { }
70
71
72 314463 CacheTransport::Frame::~Frame() { Reset(0); }
73
74
75 29362 bool CacheTransport::Frame::IsMsgOutOfBand() {
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29362 times.
29362 assert(msg_rpc_.IsInitialized());
77
1/2
✓ Branch 0 taken 29362 times.
✗ Branch 1 not taken.
29362 if (msg_typed_ == NULL)
78 29362 UnwrapMsg();
79 29362 return is_msg_out_of_band_;
80 }
81
82
83 34476 void CacheTransport::Frame::MergeFrom(const Frame &other) {
84 34476 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
85 34476 owns_msg_typed_ = true;
86
2/2
✓ Branch 0 taken 30600 times.
✓ Branch 1 taken 3876 times.
34476 if (other.att_size_ > 0) {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30600 times.
30600 assert(att_size_ >= other.att_size_);
88 30600 memcpy(attachment_, other.attachment_, other.att_size_);
89 30600 att_size_ = other.att_size_;
90 }
91 34476 }
92
93
94 139939 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
95 139939 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139956 times.
139956 if (!retval)
97 return false;
98
99 // Cleanup typed message when Frame leaves scope
100 139956 owns_msg_typed_ = true;
101 139956 return true;
102 }
103
104
105 314480 void CacheTransport::Frame::Release() {
106
2/2
✓ Branch 0 taken 174449 times.
✓ Branch 1 taken 140031 times.
314480 if (owns_msg_typed_)
107 174449 return;
108
109 140031 msg_rpc_.release_msg_refcount_req();
110 140014 msg_rpc_.release_msg_refcount_reply();
111 140031 msg_rpc_.release_msg_read_req();
112 140031 msg_rpc_.release_msg_read_reply();
113 140031 msg_rpc_.release_msg_object_info_req();
114 140031 msg_rpc_.release_msg_object_info_reply();
115 140031 msg_rpc_.release_msg_store_req();
116 140031 msg_rpc_.release_msg_store_abort_req();
117 140031 msg_rpc_.release_msg_store_reply();
118 140031 msg_rpc_.release_msg_handshake();
119 140031 msg_rpc_.release_msg_handshake_ack();
120 140031 msg_rpc_.release_msg_quit();
121 140031 msg_rpc_.release_msg_ioctl();
122 140031 msg_rpc_.release_msg_info_req();
123 140031 msg_rpc_.release_msg_info_reply();
124 140014 msg_rpc_.release_msg_shrink_req();
125 140014 msg_rpc_.release_msg_shrink_reply();
126 140014 msg_rpc_.release_msg_list_req();
127 140014 msg_rpc_.release_msg_list_reply();
128 140031 msg_rpc_.release_msg_detach();
129 140031 msg_rpc_.release_msg_breadcrumb_store_req();
130 140031 msg_rpc_.release_msg_breadcrumb_load_req();
131 140031 msg_rpc_.release_msg_breadcrumb_reply();
132 }
133
134
135 314480 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
136 314480 msg_typed_ = NULL;
137 314480 att_size_ = original_att_size;
138 314480 is_wrapped_ = false;
139 314480 is_msg_out_of_band_ = false;
140 314480 Release();
141 314463 msg_rpc_.Clear();
142 314463 owns_msg_typed_ = false;
143 314463 }
144
145
146 140014 void CacheTransport::Frame::WrapMsg() {
147
2/2
✓ Branch 3 taken 332 times.
✓ Branch 4 taken 139682 times.
140014 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
148 332 msg_rpc_.set_allocated_msg_handshake(
149 332 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
150
2/2
✓ Branch 3 taken 306 times.
✓ Branch 4 taken 139376 times.
139682 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
151 306 msg_rpc_.set_allocated_msg_handshake_ack(
152 306 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
153
2/2
✓ Branch 3 taken 330 times.
✓ Branch 4 taken 139046 times.
139376 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
154 330 msg_rpc_.set_allocated_msg_quit(
155 330 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
156
2/2
✓ Branch 3 taken 68 times.
✓ Branch 4 taken 138978 times.
139046 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
157 68 msg_rpc_.set_allocated_msg_ioctl(
158 68 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
159
2/2
✓ Branch 3 taken 12092 times.
✓ Branch 4 taken 126886 times.
138978 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
160 12092 msg_rpc_.set_allocated_msg_refcount_req(
161 12092 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
162
2/2
✓ Branch 3 taken 9588 times.
✓ Branch 4 taken 117298 times.
126886 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
163 9588 msg_rpc_.set_allocated_msg_refcount_reply(
164 9588 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
165
2/2
✓ Branch 3 taken 296 times.
✓ Branch 4 taken 117002 times.
117298 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
166 296 msg_rpc_.set_allocated_msg_object_info_req(
167 296 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
168
2/2
✓ Branch 3 taken 272 times.
✓ Branch 4 taken 116730 times.
117002 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
169 272 msg_rpc_.set_allocated_msg_object_info_reply(
170 272 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
171
2/2
✓ Branch 3 taken 38297 times.
✓ Branch 4 taken 78433 times.
116730 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
172 38297 msg_rpc_.set_allocated_msg_read_req(
173 38297 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
174
2/2
✓ Branch 3 taken 37587 times.
✓ Branch 4 taken 40846 times.
78433 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
175 37587 msg_rpc_.set_allocated_msg_read_reply(
176 37587 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
177
2/2
✓ Branch 3 taken 12750 times.
✓ Branch 4 taken 28096 times.
40846 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
178 12750 msg_rpc_.set_allocated_msg_store_req(
179 12750 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
180
2/2
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 28079 times.
28096 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
181 17 msg_rpc_.set_allocated_msg_store_abort_req(
182 17 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
183
2/2
✓ Branch 3 taken 10353 times.
✓ Branch 4 taken 17726 times.
28079 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
184 10353 msg_rpc_.set_allocated_msg_store_reply(
185 10353 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
186
2/2
✓ Branch 3 taken 92 times.
✓ Branch 4 taken 17634 times.
17726 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
187 92 msg_rpc_.set_allocated_msg_info_req(
188 92 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
189
2/2
✓ Branch 3 taken 68 times.
✓ Branch 4 taken 17566 times.
17634 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
190 68 msg_rpc_.set_allocated_msg_info_reply(
191 68 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
192
2/2
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 17524 times.
17566 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
193 42 msg_rpc_.set_allocated_msg_shrink_req(
194 42 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
195
2/2
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 17490 times.
17524 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
196 34 msg_rpc_.set_allocated_msg_shrink_reply(
197 34 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
198
2/2
✓ Branch 3 taken 178 times.
✓ Branch 4 taken 17312 times.
17490 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
199 178 msg_rpc_.set_allocated_msg_list_req(
200 178 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
201
2/2
✓ Branch 3 taken 170 times.
✓ Branch 4 taken 17142 times.
17312 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
202 170 msg_rpc_.set_allocated_msg_list_reply(
203 170 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
204
2/2
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 17123 times.
17142 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
205 19 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
206 19 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
207
2/2
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 17085 times.
17123 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
208 38 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
209 38 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
210
2/2
✓ Branch 3 taken 51 times.
✓ Branch 4 taken 17034 times.
17085 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
211 51 msg_rpc_.set_allocated_msg_breadcrumb_reply(
212 51 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
213
1/2
✓ Branch 3 taken 17034 times.
✗ Branch 4 not taken.
17034 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
214 17034 msg_rpc_.set_allocated_msg_detach(
215 17034 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
216 17034 is_msg_out_of_band_ = true;
217 } else {
218 // Unexpected message type, should never happen
219 PANIC(NULL);
220 }
221 140014 is_wrapped_ = true;
222 140014 }
223
224
225 174432 void CacheTransport::Frame::UnwrapMsg() {
226
2/2
✓ Branch 1 taken 306 times.
✓ Branch 2 taken 174092 times.
174432 if (msg_rpc_.has_msg_handshake()) {
227 306 msg_typed_ = msg_rpc_.mutable_msg_handshake();
228
2/2
✓ Branch 1 taken 332 times.
✓ Branch 2 taken 173777 times.
174092 } else if (msg_rpc_.has_msg_handshake_ack()) {
229 332 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
230
2/2
✓ Branch 1 taken 306 times.
✓ Branch 2 taken 173437 times.
173777 } else if (msg_rpc_.has_msg_quit()) {
231 306 msg_typed_ = msg_rpc_.mutable_msg_quit();
232
2/2
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 173335 times.
173437 } else if (msg_rpc_.has_msg_ioctl()) {
233 68 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
234
2/2
✓ Branch 1 taken 9588 times.
✓ Branch 2 taken 163781 times.
173335 } else if (msg_rpc_.has_msg_refcount_req()) {
235 9588 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
236
2/2
✓ Branch 1 taken 12415 times.
✓ Branch 2 taken 151383 times.
163781 } else if (msg_rpc_.has_msg_refcount_reply()) {
237 12415 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
238
2/2
✓ Branch 1 taken 272 times.
✓ Branch 2 taken 151060 times.
151383 } else if (msg_rpc_.has_msg_object_info_req()) {
239 272 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
240
2/2
✓ Branch 1 taken 449 times.
✓ Branch 2 taken 150611 times.
151060 } else if (msg_rpc_.has_msg_object_info_reply()) {
241 449 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
242
2/2
✓ Branch 1 taken 37587 times.
✓ Branch 2 taken 113058 times.
150611 } else if (msg_rpc_.has_msg_read_req()) {
243 37587 msg_typed_ = msg_rpc_.mutable_msg_read_req();
244
2/2
✓ Branch 1 taken 68897 times.
✓ Branch 2 taken 44229 times.
113058 } else if (msg_rpc_.has_msg_read_reply()) {
245 68897 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
246
2/2
✓ Branch 1 taken 10336 times.
✓ Branch 2 taken 33893 times.
44229 } else if (msg_rpc_.has_msg_store_req()) {
247 10336 msg_typed_ = msg_rpc_.mutable_msg_store_req();
248
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 33876 times.
33893 } else if (msg_rpc_.has_msg_store_abort_req()) {
249 17 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
250
2/2
✓ Branch 1 taken 16167 times.
✓ Branch 2 taken 17709 times.
33876 } else if (msg_rpc_.has_msg_store_reply()) {
251 16167 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
252
2/2
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 17641 times.
17709 } else if (msg_rpc_.has_msg_info_req()) {
253 68 msg_typed_ = msg_rpc_.mutable_msg_info_req();
254
2/2
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 17549 times.
17641 } else if (msg_rpc_.has_msg_info_reply()) {
255 92 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
256
2/2
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 17515 times.
17549 } else if (msg_rpc_.has_msg_shrink_req()) {
257 34 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
258
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 17473 times.
17515 } else if (msg_rpc_.has_msg_shrink_reply()) {
259 42 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
260
2/2
✓ Branch 1 taken 170 times.
✓ Branch 2 taken 17303 times.
17473 } else if (msg_rpc_.has_msg_list_req()) {
261 170 msg_typed_ = msg_rpc_.mutable_msg_list_req();
262
2/2
✓ Branch 1 taken 178 times.
✓ Branch 2 taken 17125 times.
17303 } else if (msg_rpc_.has_msg_list_reply()) {
263 178 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
264
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 17108 times.
17125 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
265 17 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
266
2/2
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 17074 times.
17108 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
267 34 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
268
2/2
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 17017 times.
17074 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
269 57 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
270
1/2
✓ Branch 1 taken 17017 times.
✗ Branch 2 not taken.
17017 } else if (msg_rpc_.has_msg_detach()) {
271 17017 msg_typed_ = msg_rpc_.mutable_msg_detach();
272 17017 is_msg_out_of_band_ = true;
273 } else {
274 // Unexpected message type, should never happen
275 PANIC(NULL);
276 }
277 174449 }
278
279
280 //------------------------------------------------------------------------------
281
282
283 332 CacheTransport::CacheTransport(int fd_connection)
284 332 : fd_connection_(fd_connection), flags_(0) {
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 332 times.
332 assert(fd_connection_ >= 0);
286 332 }
287
288
289 75837 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
290 75837 : fd_connection_(fd_connection), flags_(flags) {
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75837 times.
75837 assert(fd_connection_ >= 0);
292 75837 }
293
294
295 3425735 void CacheTransport::FillMsgHash(const shash::Any &hash,
296 cvmfs::MsgHash *msg_hash) {
297
3/4
✓ Branch 0 taken 3425675 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
3425735 switch (hash.algorithm) {
298 3425675 case shash::kSha1:
299 3425675 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
300 3425675 break;
301 12 case shash::kRmd160:
302 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
303 12 break;
304 48 case shash::kShake128:
305 48 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
306 48 break;
307 default:
308 PANIC(NULL);
309 }
310 3425735 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
311 3425735 }
312
313
314 12742 void CacheTransport::FillObjectType(int object_flags,
315 cvmfs::EnumObjectType *wire_type) {
316 12742 *wire_type = cvmfs::OBJECT_REGULAR;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12742 times.
12742 if (object_flags & CacheManager::kLabelCatalog)
318 *wire_type = cvmfs::OBJECT_CATALOG;
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12742 times.
12742 if (object_flags & CacheManager::kLabelVolatile)
320 *wire_type = cvmfs::OBJECT_VOLATILE;
321 12742 }
322
323
324 57819 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
325 shash::Any *hash) {
326
2/4
✓ Branch 1 taken 57783 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
✗ Branch 4 not taken.
57819 switch (msg_hash.algorithm()) {
327 57783 case cvmfs::HASH_SHA1:
328 57783 hash->algorithm = shash::kSha1;
329 57783 break;
330 case cvmfs::HASH_RIPEMD160:
331 hash->algorithm = shash::kRmd160;
332 break;
333 36 case cvmfs::HASH_SHAKE128:
334 36 hash->algorithm = shash::kShake128;
335 36 break;
336 default:
337 return false;
338 }
339 57819 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
340
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 57819 times.
57819 if (msg_hash.digest().length() != digest_size)
341 return false;
342 57819 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
343 57819 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 139973 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
366 uint32_t size;
367 bool has_attachment;
368
1/2
✓ Branch 1 taken 139990 times.
✗ Branch 2 not taken.
139973 bool retval = RecvHeader(&size, &has_attachment);
369
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 139973 times.
139990 if (!retval)
370 17 return false;
371
372 void *buffer;
373
2/2
✓ Branch 0 taken 91640 times.
✓ Branch 1 taken 48333 times.
139973 if (size <= kMaxStackAlloc)
374 91640 buffer = alloca(size);
375 else
376 48333 buffer = smalloc(size);
377
1/2
✓ Branch 1 taken 139956 times.
✗ Branch 2 not taken.
139973 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
378
2/4
✓ Branch 0 taken 139956 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 139956 times.
139956 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
379 if (size > kMaxStackAlloc) {
380 free(buffer);
381 }
382 return false;
383 }
384
385 139956 uint32_t msg_size = size;
386
2/2
✓ Branch 0 taken 48593 times.
✓ Branch 1 taken 91363 times.
139956 if (has_attachment) {
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48593 times.
48593 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 48593 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
396 48593 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48593 times.
48593 if ((msg_size + kInnerHeaderSize) > size) {
398 if (size > kMaxStackAlloc) {
399 free(buffer);
400 }
401 return false;
402 }
403 }
404
405 139956 void *ptr_msg = has_attachment
406
2/2
✓ Branch 0 taken 48593 times.
✓ Branch 1 taken 91363 times.
139956 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
407 : buffer;
408
1/2
✓ Branch 1 taken 139956 times.
✗ Branch 2 not taken.
139956 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139956 times.
139956 if (!retval) {
410 if (size > kMaxStackAlloc) {
411 free(buffer);
412 }
413 return false;
414 }
415
416
2/2
✓ Branch 0 taken 48593 times.
✓ Branch 1 taken 91363 times.
139956 if (has_attachment) {
417 48593 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 48593 times.
48593 if (frame->att_size() < attachment_size) {
419 if (size > kMaxStackAlloc) {
420 free(buffer);
421 }
422 return false;
423 }
424 48593 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
425 48593 + msg_size;
426 48593 memcpy(frame->attachment(), ptr_attachment, attachment_size);
427 48593 frame->set_att_size(attachment_size);
428 } else {
429 91363 frame->set_att_size(0);
430 }
431
2/2
✓ Branch 0 taken 48333 times.
✓ Branch 1 taken 91640 times.
139973 if (size > kMaxStackAlloc) {
432 48333 free(buffer);
433 }
434 139973 return true;
435 }
436
437
438 139973 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
439 unsigned char header[kHeaderSize];
440
1/2
✓ Branch 1 taken 139990 times.
✗ Branch 2 not taken.
139973 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
441
3/4
✓ Branch 0 taken 139990 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 139973 times.
139990 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
442 17 return false;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139973 times.
139973 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
444 return false;
445 139973 *has_attachment = header[0] & kFlagHasAttachment;
446 139973 *size = header[1] + (header[2] << 8) + (header[3] << 16);
447
2/4
✓ Branch 0 taken 139973 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 139973 times.
✗ Branch 3 not taken.
139973 return (*size > 0) && (*size <= kMaxMsgSize);
448 }
449
450
451 140014 void CacheTransport::SendData(void *message,
452 uint32_t msg_size,
453 void *attachment,
454 uint32_t att_size) {
455 280028 const uint32_t total_size = msg_size + att_size
456
2/2
✓ Branch 0 taken 50301 times.
✓ Branch 1 taken 89713 times.
140014 + ((att_size > 0) ? kInnerHeaderSize : 0);
457
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140014 times.
140014 assert(total_size > 0);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140014 times.
140014 assert(total_size <= kMaxMsgSize);
460
1/2
✓ Branch 1 taken 134266 times.
✗ Branch 2 not taken.
134266 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 89713 times.
✓ Branch 1 taken 50301 times.
140014 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
465 140014 header[1] = (total_size & 0x000000FF);
466 140014 header[2] = (total_size & 0x0000FF00) >> 8;
467 140014 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 140014 iov[0].iov_base = header;
474 140014 iov[0].iov_len = kHeaderSize;
475
476
2/2
✓ Branch 0 taken 50301 times.
✓ Branch 1 taken 89713 times.
140014 if (att_size > 0) {
477 50301 inner_header[0] = (msg_size & 0x000000FF);
478 50301 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
479 50301 iov[1].iov_base = inner_header;
480 50301 iov[1].iov_len = kInnerHeaderSize;
481 50301 iov[2].iov_base = message;
482 50301 iov[2].iov_len = msg_size;
483 50301 iov[3].iov_base = attachment;
484 50301 iov[3].iov_len = att_size;
485 } else {
486 89713 iov[1].iov_base = message;
487 89713 iov[1].iov_len = msg_size;
488 }
489
2/2
✓ Branch 0 taken 17034 times.
✓ Branch 1 taken 122980 times.
140014 if (flags_ & kFlagSendNonBlocking) {
490
2/4
✓ Branch 0 taken 17034 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17034 times.
✗ Branch 4 not taken.
17034 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
491 17034 return;
492 }
493
3/4
✓ Branch 0 taken 72679 times.
✓ Branch 1 taken 50301 times.
✓ Branch 3 taken 122980 times.
✗ Branch 4 not taken.
122980 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
494
495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 122980 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
122980 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
496 PANIC(kLogSyslogErr | kLogDebug,
497 "failed to write to external cache transport (%d), aborting", errno);
498 }
499 }
500
501 17034 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17034 times.
17034 assert(iovcnt > 0);
503 17034 unsigned total_size = 0;
504
2/2
✓ Branch 0 taken 34068 times.
✓ Branch 1 taken 17034 times.
51102 for (unsigned i = 0; i < iovcnt; ++i)
505 34068 total_size += iov[i].iov_len;
506 17034 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
507
508 17034 unsigned pos = 0;
509
2/2
✓ Branch 0 taken 34068 times.
✓ Branch 1 taken 17034 times.
51102 for (unsigned i = 0; i < iovcnt; ++i) {
510 34068 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
511 34068 pos += iov[i].iov_len;
512 }
513
514 17034 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17034 times.
17034 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 17034 }
524
525
526 140014 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
527 140014 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
528 140014 const int32_t size = msg_rpc->ByteSize();
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140014 times.
140014 assert(size > 0);
530 #ifdef __APPLE__
531 void *buffer = smalloc(size);
532 #else
533 140014 void *buffer = alloca(size);
534 #endif
535 140014 const bool retval = msg_rpc->SerializeToArray(buffer, size);
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140014 times.
140014 assert(retval);
537 140014 SendData(buffer, size, frame->attachment(), frame->att_size());
538 #ifdef __APPLE__
539 free(buffer);
540 #endif
541 140014 }
542