GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-05-24 02:35:55
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 211080 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211080 times.
211080 assert(msg_typed_ != NULL);
36
1/2
✓ Branch 0 taken 211080 times.
✗ Branch 1 not taken.
211080 if (!is_wrapped_)
37 211080 WrapMsg();
38 211080 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 263732 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 263654 times.
263732 assert(msg_rpc_.IsInitialized());
48
2/2
✓ Branch 0 taken 221832 times.
✓ Branch 1 taken 41822 times.
263654 if (msg_typed_ == NULL)
49 221832 UnwrapMsg();
50 263628 return msg_typed_;
51 }
52
53
54 263758 CacheTransport::Frame::Frame()
55 263758 : owns_msg_typed_(false)
56 263758 , msg_typed_(NULL)
57 263758 , attachment_(NULL)
58 263758 , att_size_(0)
59 263758 , is_wrapped_(false)
60 263758 , is_msg_out_of_band_(false) { }
61
62
63 211080 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
64 211080 : owns_msg_typed_(false)
65 211080 , msg_typed_(m)
66 211080 , attachment_(NULL)
67 211080 , att_size_(0)
68 211080 , is_wrapped_(false)
69 211080 , is_msg_out_of_band_(false) { }
70
71
72 474760 CacheTransport::Frame::~Frame() { Reset(0); }
73
74
75 41874 bool CacheTransport::Frame::IsMsgOutOfBand() {
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41874 times.
41874 assert(msg_rpc_.IsInitialized());
77
1/2
✓ Branch 0 taken 41874 times.
✗ Branch 1 not taken.
41874 if (msg_typed_ == NULL)
78 41874 UnwrapMsg();
79 41874 return is_msg_out_of_band_;
80 }
81
82
83 52728 void CacheTransport::Frame::MergeFrom(const Frame &other) {
84 52728 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
85 52728 owns_msg_typed_ = true;
86
2/2
✓ Branch 0 taken 46800 times.
✓ Branch 1 taken 5928 times.
52728 if (other.att_size_ > 0) {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46800 times.
46800 assert(att_size_ >= other.att_size_);
88 46800 memcpy(attachment_, other.attachment_, other.att_size_);
89 46800 att_size_ = other.att_size_;
90 }
91 52728 }
92
93
94 210978 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
95 210978 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210952 times.
210952 if (!retval)
97 return false;
98
99 // Cleanup typed message when Frame leaves scope
100 210952 owns_msg_typed_ = true;
101 210952 return true;
102 }
103
104
105 474786 void CacheTransport::Frame::Release() {
106
2/2
✓ Branch 0 taken 263758 times.
✓ Branch 1 taken 211028 times.
474786 if (owns_msg_typed_)
107 263758 return;
108
109 211028 msg_rpc_.release_msg_refcount_req();
110 211080 msg_rpc_.release_msg_refcount_reply();
111 211080 msg_rpc_.release_msg_read_req();
112 211106 msg_rpc_.release_msg_read_reply();
113 211080 msg_rpc_.release_msg_object_info_req();
114 211080 msg_rpc_.release_msg_object_info_reply();
115 211106 msg_rpc_.release_msg_store_req();
116 211080 msg_rpc_.release_msg_store_abort_req();
117 211080 msg_rpc_.release_msg_store_reply();
118 211080 msg_rpc_.release_msg_handshake();
119 211080 msg_rpc_.release_msg_handshake_ack();
120 211080 msg_rpc_.release_msg_quit();
121 211106 msg_rpc_.release_msg_ioctl();
122 211080 msg_rpc_.release_msg_info_req();
123 211054 msg_rpc_.release_msg_info_reply();
124 211054 msg_rpc_.release_msg_shrink_req();
125 211054 msg_rpc_.release_msg_shrink_reply();
126 211054 msg_rpc_.release_msg_list_req();
127 211080 msg_rpc_.release_msg_list_reply();
128 211106 msg_rpc_.release_msg_detach();
129 211106 msg_rpc_.release_msg_breadcrumb_store_req();
130 211106 msg_rpc_.release_msg_breadcrumb_load_req();
131 211054 msg_rpc_.release_msg_breadcrumb_reply();
132 }
133
134
135 474760 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
136 474760 msg_typed_ = NULL;
137 474760 att_size_ = original_att_size;
138 474760 is_wrapped_ = false;
139 474760 is_msg_out_of_band_ = false;
140 474760 Release();
141 474838 msg_rpc_.Clear();
142 474682 owns_msg_typed_ = false;
143 474682 }
144
145
146 211080 void CacheTransport::Frame::WrapMsg() {
147
2/2
✓ Branch 3 taken 494 times.
✓ Branch 4 taken 210586 times.
211080 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
148 494 msg_rpc_.set_allocated_msg_handshake(
149 494 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
150
2/2
✓ Branch 3 taken 468 times.
✓ Branch 4 taken 210118 times.
210586 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
151 468 msg_rpc_.set_allocated_msg_handshake_ack(
152 468 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
153
2/2
✓ Branch 3 taken 492 times.
✓ Branch 4 taken 209600 times.
210118 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
154 492 msg_rpc_.set_allocated_msg_quit(
155 492 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
156
2/2
✓ Branch 3 taken 104 times.
✓ Branch 4 taken 209496 times.
209600 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
157 104 msg_rpc_.set_allocated_msg_ioctl(
158 104 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
159
2/2
✓ Branch 3 taken 17168 times.
✓ Branch 4 taken 192354 times.
209496 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
160 17168 msg_rpc_.set_allocated_msg_refcount_req(
161 17168 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
162
2/2
✓ Branch 3 taken 14664 times.
✓ Branch 4 taken 177690 times.
192354 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
163 14664 msg_rpc_.set_allocated_msg_refcount_reply(
164 14664 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
165
2/2
✓ Branch 3 taken 440 times.
✓ Branch 4 taken 177250 times.
177690 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
166 440 msg_rpc_.set_allocated_msg_object_info_req(
167 440 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
168
2/2
✓ Branch 3 taken 416 times.
✓ Branch 4 taken 176834 times.
177250 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
169 416 msg_rpc_.set_allocated_msg_object_info_reply(
170 416 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
171
2/2
✓ Branch 3 taken 58180 times.
✓ Branch 4 taken 118654 times.
176834 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
172 58180 msg_rpc_.set_allocated_msg_read_req(
173 58180 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
174
2/2
✓ Branch 3 taken 57486 times.
✓ Branch 4 taken 61168 times.
118654 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
175 57486 msg_rpc_.set_allocated_msg_read_reply(
176 57486 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
177
2/2
✓ Branch 3 taken 18222 times.
✓ Branch 4 taken 42946 times.
61168 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
178 18222 msg_rpc_.set_allocated_msg_store_req(
179 18222 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
180
2/2
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 42920 times.
42946 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
181 26 msg_rpc_.set_allocated_msg_store_abort_req(
182 26 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
183
2/2
✓ Branch 3 taken 15834 times.
✓ Branch 4 taken 27086 times.
42920 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
184 15834 msg_rpc_.set_allocated_msg_store_reply(
185 15834 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
186
2/2
✓ Branch 3 taken 128 times.
✓ Branch 4 taken 26958 times.
27086 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
187 128 msg_rpc_.set_allocated_msg_info_req(
188 128 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
189
2/2
✓ Branch 3 taken 104 times.
✓ Branch 4 taken 26854 times.
26958 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
190 104 msg_rpc_.set_allocated_msg_info_reply(
191 104 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
192
2/2
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 26794 times.
26854 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
193 60 msg_rpc_.set_allocated_msg_shrink_req(
194 60 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
195
2/2
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 26742 times.
26794 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
196 52 msg_rpc_.set_allocated_msg_shrink_reply(
197 52 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
198
2/2
✓ Branch 3 taken 268 times.
✓ Branch 4 taken 26474 times.
26742 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
199 268 msg_rpc_.set_allocated_msg_list_req(
200 268 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
201
2/2
✓ Branch 3 taken 260 times.
✓ Branch 4 taken 26214 times.
26474 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
202 260 msg_rpc_.set_allocated_msg_list_reply(
203 260 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
204
2/2
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 26186 times.
26214 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
205 28 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
206 28 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
207
2/2
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 26130 times.
26186 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
208 56 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
209 56 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
210
2/2
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 26052 times.
26130 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
211 78 msg_rpc_.set_allocated_msg_breadcrumb_reply(
212 78 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
213
1/2
✓ Branch 3 taken 26052 times.
✗ Branch 4 not taken.
26052 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
214 26052 msg_rpc_.set_allocated_msg_detach(
215 26052 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
216 26052 is_msg_out_of_band_ = true;
217 } else {
218 // Unexpected message type, should never happen
219 PANIC(NULL);
220 }
221 211080 is_wrapped_ = true;
222 211080 }
223
224
225 263706 void CacheTransport::Frame::UnwrapMsg() {
226
2/2
✓ Branch 1 taken 468 times.
✓ Branch 2 taken 263264 times.
263706 if (msg_rpc_.has_msg_handshake()) {
227 468 msg_typed_ = msg_rpc_.mutable_msg_handshake();
228
2/2
✓ Branch 1 taken 494 times.
✓ Branch 2 taken 262718 times.
263264 } else if (msg_rpc_.has_msg_handshake_ack()) {
229 494 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
230
2/2
✓ Branch 1 taken 468 times.
✓ Branch 2 taken 262198 times.
262718 } else if (msg_rpc_.has_msg_quit()) {
231 468 msg_typed_ = msg_rpc_.mutable_msg_quit();
232
2/2
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 262146 times.
262198 } else if (msg_rpc_.has_msg_ioctl()) {
233 104 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
234
2/2
✓ Branch 1 taken 14664 times.
✓ Branch 2 taken 247482 times.
262146 } else if (msg_rpc_.has_msg_refcount_req()) {
235 14664 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
236
2/2
✓ Branch 1 taken 17636 times.
✓ Branch 2 taken 229872 times.
247482 } else if (msg_rpc_.has_msg_refcount_reply()) {
237 17636 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
238
2/2
✓ Branch 1 taken 416 times.
✓ Branch 2 taken 229482 times.
229872 } else if (msg_rpc_.has_msg_object_info_req()) {
239 416 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
240
2/2
✓ Branch 1 taken 648 times.
✓ Branch 2 taken 228808 times.
229482 } else if (msg_rpc_.has_msg_object_info_reply()) {
241 648 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
242
2/2
✓ Branch 1 taken 57486 times.
✓ Branch 2 taken 171322 times.
228808 } else if (msg_rpc_.has_msg_read_req()) {
243 57486 msg_typed_ = msg_rpc_.mutable_msg_read_req();
244
2/2
✓ Branch 1 taken 104980 times.
✓ Branch 2 taken 66342 times.
171322 } else if (msg_rpc_.has_msg_read_reply()) {
245 104980 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
246
2/2
✓ Branch 1 taken 15808 times.
✓ Branch 2 taken 50534 times.
66342 } else if (msg_rpc_.has_msg_store_req()) {
247 15808 msg_typed_ = msg_rpc_.mutable_msg_store_req();
248
2/2
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 50508 times.
50534 } else if (msg_rpc_.has_msg_store_abort_req()) {
249 26 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
250
2/2
✓ Branch 1 taken 23448 times.
✓ Branch 2 taken 27060 times.
50508 } else if (msg_rpc_.has_msg_store_reply()) {
251 23448 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
252
2/2
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 26956 times.
27060 } else if (msg_rpc_.has_msg_info_req()) {
253 104 msg_typed_ = msg_rpc_.mutable_msg_info_req();
254
2/2
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 26828 times.
26956 } else if (msg_rpc_.has_msg_info_reply()) {
255 128 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
256
2/2
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 26776 times.
26828 } else if (msg_rpc_.has_msg_shrink_req()) {
257 52 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
258
2/2
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 26716 times.
26776 } else if (msg_rpc_.has_msg_shrink_reply()) {
259 60 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
260
2/2
✓ Branch 1 taken 260 times.
✓ Branch 2 taken 26456 times.
26716 } else if (msg_rpc_.has_msg_list_req()) {
261 260 msg_typed_ = msg_rpc_.mutable_msg_list_req();
262
2/2
✓ Branch 1 taken 268 times.
✓ Branch 2 taken 26188 times.
26456 } else if (msg_rpc_.has_msg_list_reply()) {
263 268 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
264
2/2
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 26162 times.
26188 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
265 26 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
266
2/2
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 26110 times.
26162 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
267 52 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
268
2/2
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 26026 times.
26110 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
269 84 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
270
1/2
✓ Branch 1 taken 26026 times.
✗ Branch 2 not taken.
26026 } else if (msg_rpc_.has_msg_detach()) {
271 26026 msg_typed_ = msg_rpc_.mutable_msg_detach();
272 26026 is_msg_out_of_band_ = true;
273 } else {
274 // Unexpected message type, should never happen
275 PANIC(NULL);
276 }
277 263732 }
278
279
280 //------------------------------------------------------------------------------
281
282
283 494 CacheTransport::CacheTransport(int fd_connection)
284 494 : fd_connection_(fd_connection), flags_(0) {
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 494 times.
494 assert(fd_connection_ >= 0);
286 494 }
287
288
289 115986 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
290 115986 : fd_connection_(fd_connection), flags_(flags) {
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115986 times.
115986 assert(fd_connection_ >= 0);
292 115986 }
293
294
295 5236593 void CacheTransport::FillMsgHash(const shash::Any &hash,
296 cvmfs::MsgHash *msg_hash) {
297
3/4
✓ Branch 0 taken 5236515 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
5236593 switch (hash.algorithm) {
298 5236515 case shash::kSha1:
299 5236515 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
300 5236515 break;
301 12 case shash::kRmd160:
302 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
303 12 break;
304 66 case shash::kShake128:
305 66 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
306 66 break;
307 default:
308 PANIC(NULL);
309 }
310 5236593 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
311 5236593 }
312
313
314 18214 void CacheTransport::FillObjectType(int object_flags,
315 cvmfs::EnumObjectType *wire_type) {
316 18214 *wire_type = cvmfs::OBJECT_REGULAR;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18214 times.
18214 if (object_flags & CacheManager::kLabelCatalog)
318 *wire_type = cvmfs::OBJECT_CATALOG;
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18214 times.
18214 if (object_flags & CacheManager::kLabelVolatile)
320 *wire_type = cvmfs::OBJECT_VOLATILE;
321 18214 }
322
323
324 88428 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
325 shash::Any *hash) {
326
2/4
✓ Branch 1 taken 88374 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
✗ Branch 4 not taken.
88428 switch (msg_hash.algorithm()) {
327 88374 case cvmfs::HASH_SHA1:
328 88374 hash->algorithm = shash::kSha1;
329 88374 break;
330 case cvmfs::HASH_RIPEMD160:
331 hash->algorithm = shash::kRmd160;
332 break;
333 54 case cvmfs::HASH_SHAKE128:
334 54 hash->algorithm = shash::kShake128;
335 54 break;
336 default:
337 return false;
338 }
339 88428 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
340
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 88428 times.
88428 if (msg_hash.digest().length() != digest_size)
341 return false;
342 88428 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
343 88428 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 211056 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
366 uint32_t size;
367 bool has_attachment;
368
1/2
✓ Branch 1 taken 211056 times.
✗ Branch 2 not taken.
211056 bool retval = RecvHeader(&size, &has_attachment);
369
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 211030 times.
211056 if (!retval)
370 26 return false;
371
372 void *buffer;
373
2/2
✓ Branch 0 taken 137355 times.
✓ Branch 1 taken 73675 times.
211030 if (size <= kMaxStackAlloc)
374 137355 buffer = alloca(size);
375 else
376 73675 buffer = smalloc(size);
377
1/2
✓ Branch 1 taken 211030 times.
✗ Branch 2 not taken.
211030 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
378
2/4
✓ Branch 0 taken 211030 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 211030 times.
211030 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
379 if (size > kMaxStackAlloc) {
380 free(buffer);
381 }
382 return false;
383 }
384
385 211030 uint32_t msg_size = size;
386
2/2
✓ Branch 0 taken 73930 times.
✓ Branch 1 taken 137100 times.
211030 if (has_attachment) {
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73930 times.
73930 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 73930 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
396 73930 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73930 times.
73930 if ((msg_size + kInnerHeaderSize) > size) {
398 if (size > kMaxStackAlloc) {
399 free(buffer);
400 }
401 return false;
402 }
403 }
404
405 211030 void *ptr_msg = has_attachment
406
2/2
✓ Branch 0 taken 73930 times.
✓ Branch 1 taken 137100 times.
211030 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
407 : buffer;
408
1/2
✓ Branch 1 taken 210952 times.
✗ Branch 2 not taken.
211030 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210952 times.
210952 if (!retval) {
410 if (size > kMaxStackAlloc) {
411 free(buffer);
412 }
413 return false;
414 }
415
416
2/2
✓ Branch 0 taken 73930 times.
✓ Branch 1 taken 137022 times.
210952 if (has_attachment) {
417 73930 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 73930 times.
73930 if (frame->att_size() < attachment_size) {
419 if (size > kMaxStackAlloc) {
420 free(buffer);
421 }
422 return false;
423 }
424 73930 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
425 73930 + msg_size;
426 73930 memcpy(frame->attachment(), ptr_attachment, attachment_size);
427 73930 frame->set_att_size(attachment_size);
428 } else {
429 137022 frame->set_att_size(0);
430 }
431
2/2
✓ Branch 0 taken 73675 times.
✓ Branch 1 taken 137355 times.
211030 if (size > kMaxStackAlloc) {
432 73675 free(buffer);
433 }
434 211030 return true;
435 }
436
437
438 211056 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
439 unsigned char header[kHeaderSize];
440
1/2
✓ Branch 1 taken 211056 times.
✗ Branch 2 not taken.
211056 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
441
3/4
✓ Branch 0 taken 211056 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 211030 times.
211056 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
442 26 return false;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211030 times.
211030 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
444 return false;
445 211030 *has_attachment = header[0] & kFlagHasAttachment;
446 211030 *size = header[1] + (header[2] << 8) + (header[3] << 16);
447
2/4
✓ Branch 0 taken 211030 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 211030 times.
✗ Branch 3 not taken.
211030 return (*size > 0) && (*size <= kMaxMsgSize);
448 }
449
450
451 211080 void CacheTransport::SendData(void *message,
452 uint32_t msg_size,
453 void *attachment,
454 uint32_t att_size) {
455 422160 const uint32_t total_size = msg_size + att_size
456
2/2
✓ Branch 0 taken 75654 times.
✓ Branch 1 taken 135426 times.
211080 + ((att_size > 0) ? kInnerHeaderSize : 0);
457
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211080 times.
211080 assert(total_size > 0);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211080 times.
211080 assert(total_size <= kMaxMsgSize);
460
1/2
✓ Branch 1 taken 205348 times.
✗ Branch 2 not taken.
205348 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 135426 times.
✓ Branch 1 taken 75654 times.
211080 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
465 211080 header[1] = (total_size & 0x000000FF);
466 211080 header[2] = (total_size & 0x0000FF00) >> 8;
467 211080 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 211080 iov[0].iov_base = header;
474 211080 iov[0].iov_len = kHeaderSize;
475
476
2/2
✓ Branch 0 taken 75654 times.
✓ Branch 1 taken 135426 times.
211080 if (att_size > 0) {
477 75654 inner_header[0] = (msg_size & 0x000000FF);
478 75654 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
479 75654 iov[1].iov_base = inner_header;
480 75654 iov[1].iov_len = kInnerHeaderSize;
481 75654 iov[2].iov_base = message;
482 75654 iov[2].iov_len = msg_size;
483 75654 iov[3].iov_base = attachment;
484 75654 iov[3].iov_len = att_size;
485 } else {
486 135426 iov[1].iov_base = message;
487 135426 iov[1].iov_len = msg_size;
488 }
489
2/2
✓ Branch 0 taken 26052 times.
✓ Branch 1 taken 185028 times.
211080 if (flags_ & kFlagSendNonBlocking) {
490
2/4
✓ Branch 0 taken 26052 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 26052 times.
✗ Branch 4 not taken.
26052 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
491 26052 return;
492 }
493
3/4
✓ Branch 0 taken 109374 times.
✓ Branch 1 taken 75654 times.
✓ Branch 3 taken 184976 times.
✗ Branch 4 not taken.
185028 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
494
495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 184976 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
184976 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
496 PANIC(kLogSyslogErr | kLogDebug,
497 "failed to write to external cache transport (%d), aborting", errno);
498 }
499 }
500
501 26052 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26052 times.
26052 assert(iovcnt > 0);
503 26052 unsigned total_size = 0;
504
2/2
✓ Branch 0 taken 52104 times.
✓ Branch 1 taken 26052 times.
78156 for (unsigned i = 0; i < iovcnt; ++i)
505 52104 total_size += iov[i].iov_len;
506 26052 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
507
508 26052 unsigned pos = 0;
509
2/2
✓ Branch 0 taken 52104 times.
✓ Branch 1 taken 26052 times.
78156 for (unsigned i = 0; i < iovcnt; ++i) {
510 52104 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
511 52104 pos += iov[i].iov_len;
512 }
513
514 26052 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26052 times.
26052 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 26052 }
524
525
526 211080 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
527 211080 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
528 211080 const int32_t size = msg_rpc->ByteSize();
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211080 times.
211080 assert(size > 0);
530 #ifdef __APPLE__
531 void *buffer = smalloc(size);
532 #else
533 211080 void *buffer = alloca(size);
534 #endif
535 211080 const bool retval = msg_rpc->SerializeToArray(buffer, size);
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211080 times.
211080 assert(retval);
537 211080 SendData(buffer, size, frame->attachment(), frame->att_size());
538 #ifdef __APPLE__
539 free(buffer);
540 #endif
541 211028 }
542