GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-10-04 02:40:33
Exec Total Coverage
Lines: 327 373 87.7%
Branches: 167 230 72.6%

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 100490 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100490 times.
100490 assert(msg_typed_ != NULL);
36
1/2
✓ Branch 0 taken 100490 times.
✗ Branch 1 not taken.
100490 if (!is_wrapped_)
37 100490 WrapMsg();
38 100490 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 124766 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 124790 times.
124766 assert(msg_rpc_.IsInitialized());
48
2/2
✓ Branch 0 taken 102410 times.
✓ Branch 1 taken 22380 times.
124790 if (msg_typed_ == NULL)
49 102410 UnwrapMsg();
50 124694 return msg_typed_;
51 }
52
53
54 124802 CacheTransport::Frame::Frame()
55 124790 : owns_msg_typed_(false)
56 124790 , msg_typed_(NULL)
57 124790 , attachment_(NULL)
58 124790 , att_size_(0)
59 124790 , is_wrapped_(false)
60 124802 , is_msg_out_of_band_(false) { }
61
62
63 100502 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
64 100502 : owns_msg_typed_(false)
65 100502 , msg_typed_(m)
66 100502 , attachment_(NULL)
67 100502 , att_size_(0)
68 100502 , is_wrapped_(false)
69 100502 , is_msg_out_of_band_(false) { }
70
71
72 225292 CacheTransport::Frame::~Frame() { Reset(0); }
73
74
75 22380 bool CacheTransport::Frame::IsMsgOutOfBand() {
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22380 times.
22380 assert(msg_rpc_.IsInitialized());
77
1/2
✓ Branch 0 taken 22380 times.
✗ Branch 1 not taken.
22380 if (msg_typed_ == NULL)
78 22380 UnwrapMsg();
79 22380 return is_msg_out_of_band_;
80 }
81
82
83 24336 void CacheTransport::Frame::MergeFrom(const Frame &other) {
84 24336 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
85 24336 owns_msg_typed_ = true;
86
2/2
✓ Branch 0 taken 21600 times.
✓ Branch 1 taken 2736 times.
24336 if (other.att_size_ > 0) {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21600 times.
21600 assert(att_size_ >= other.att_size_);
88 21600 memcpy(attachment_, other.attachment_, other.att_size_);
89 21600 att_size_ = other.att_size_;
90 }
91 24336 }
92
93
94 100466 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
95 100466 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100466 times.
100466 if (!retval)
97 ✗ return false;
98
99 // Cleanup typed message when Frame leaves scope
100 100466 owns_msg_typed_ = true;
101 100466 return true;
102 }
103
104
105 225316 void CacheTransport::Frame::Release() {
106
2/2
✓ Branch 0 taken 124802 times.
✓ Branch 1 taken 100514 times.
225316 if (owns_msg_typed_)
107 124802 return;
108
109 100514 (void)msg_rpc_.release_msg_refcount_req();
110 100514 (void)msg_rpc_.release_msg_refcount_reply();
111 100514 (void)msg_rpc_.release_msg_read_req();
112 100514 (void)msg_rpc_.release_msg_read_reply();
113 100502 (void)msg_rpc_.release_msg_object_info_req();
114 100502 (void)msg_rpc_.release_msg_object_info_reply();
115 100502 (void)msg_rpc_.release_msg_store_req();
116 100502 (void)msg_rpc_.release_msg_store_abort_req();
117 100502 (void)msg_rpc_.release_msg_store_reply();
118 100502 (void)msg_rpc_.release_msg_handshake();
119 100502 (void)msg_rpc_.release_msg_handshake_ack();
120 100502 (void)msg_rpc_.release_msg_quit();
121 100502 (void)msg_rpc_.release_msg_ioctl();
122 100490 (void)msg_rpc_.release_msg_info_req();
123 100490 (void)msg_rpc_.release_msg_info_reply();
124 100502 (void)msg_rpc_.release_msg_shrink_req();
125 100502 (void)msg_rpc_.release_msg_shrink_reply();
126 100514 (void)msg_rpc_.release_msg_list_req();
127 100514 (void)msg_rpc_.release_msg_list_reply();
128 100514 (void)msg_rpc_.release_msg_detach();
129 100502 (void)msg_rpc_.release_msg_breadcrumb_store_req();
130 100502 (void)msg_rpc_.release_msg_breadcrumb_load_req();
131 100502 (void)msg_rpc_.release_msg_breadcrumb_reply();
132 }
133
134
135 225316 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
136 225316 msg_typed_ = NULL;
137 225316 att_size_ = original_att_size;
138 225316 is_wrapped_ = false;
139 225316 is_msg_out_of_band_ = false;
140 225316 Release();
141 225292 msg_rpc_.Clear();
142 225292 owns_msg_typed_ = false;
143 225292 }
144
145
146 100490 void CacheTransport::Frame::WrapMsg() {
147
2/2
✓ Branch 3 taken 242 times.
✓ Branch 4 taken 100260 times.
100490 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
148 242 msg_rpc_.set_allocated_msg_handshake(
149 242 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
150
2/2
✓ Branch 3 taken 216 times.
✓ Branch 4 taken 100044 times.
100260 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
151 216 msg_rpc_.set_allocated_msg_handshake_ack(
152 216 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
153
2/2
✓ Branch 3 taken 240 times.
✓ Branch 4 taken 99804 times.
100044 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
154 240 msg_rpc_.set_allocated_msg_quit(
155 240 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
156
2/2
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 99756 times.
99804 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
157 48 msg_rpc_.set_allocated_msg_ioctl(
158 48 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
159
2/2
✓ Branch 3 taken 9272 times.
✓ Branch 4 taken 90484 times.
99756 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
160 9272 msg_rpc_.set_allocated_msg_refcount_req(
161 9272 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
162
2/2
✓ Branch 3 taken 6768 times.
✓ Branch 4 taken 83716 times.
90484 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
163 6768 msg_rpc_.set_allocated_msg_refcount_reply(
164 6768 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
165
2/2
✓ Branch 3 taken 216 times.
✓ Branch 4 taken 83500 times.
83716 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
166 216 msg_rpc_.set_allocated_msg_object_info_req(
167 216 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
168
2/2
✓ Branch 3 taken 192 times.
✓ Branch 4 taken 83308 times.
83500 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
169 192 msg_rpc_.set_allocated_msg_object_info_reply(
170 192 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
171
2/2
✓ Branch 3 taken 27220 times.
✓ Branch 4 taken 56088 times.
83308 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
172 27220 msg_rpc_.set_allocated_msg_read_req(
173 27220 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
174
2/2
✓ Branch 3 taken 26532 times.
✓ Branch 4 taken 29556 times.
56088 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
175 26532 msg_rpc_.set_allocated_msg_read_reply(
176 26532 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
177
2/2
✓ Branch 3 taken 9710 times.
✓ Branch 4 taken 19846 times.
29556 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
178 9710 msg_rpc_.set_allocated_msg_store_req(
179 9710 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
180
2/2
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 19834 times.
19846 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
181 12 msg_rpc_.set_allocated_msg_store_abort_req(
182 12 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
183
2/2
✓ Branch 3 taken 7308 times.
✓ Branch 4 taken 12526 times.
19834 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
184 7308 msg_rpc_.set_allocated_msg_store_reply(
185 7308 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
186
2/2
✓ Branch 3 taken 72 times.
✓ Branch 4 taken 12454 times.
12526 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
187 72 msg_rpc_.set_allocated_msg_info_req(
188 72 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
189
2/2
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 12406 times.
12454 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
190 48 msg_rpc_.set_allocated_msg_info_reply(
191 48 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
192
2/2
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 12374 times.
12406 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
193 32 msg_rpc_.set_allocated_msg_shrink_req(
194 32 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
195
2/2
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 12350 times.
12374 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
196 24 msg_rpc_.set_allocated_msg_shrink_reply(
197 24 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
198
2/2
✓ Branch 3 taken 128 times.
✓ Branch 4 taken 12222 times.
12350 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
199 128 msg_rpc_.set_allocated_msg_list_req(
200 128 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
201
2/2
✓ Branch 3 taken 120 times.
✓ Branch 4 taken 12102 times.
12222 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
202 120 msg_rpc_.set_allocated_msg_list_reply(
203 120 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
204
2/2
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 12088 times.
12102 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
205 14 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
206 14 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
207
2/2
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 12060 times.
12088 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
208 28 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
209 28 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
210
2/2
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 12024 times.
12060 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
211 36 msg_rpc_.set_allocated_msg_breadcrumb_reply(
212 36 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
213
1/2
✓ Branch 3 taken 12024 times.
✗ Branch 4 not taken.
12024 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
214 12024 msg_rpc_.set_allocated_msg_detach(
215 12024 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
216 12024 is_msg_out_of_band_ = true;
217 } else {
218 // Unexpected message type, should never happen
219 ✗ PANIC(NULL);
220 }
221 100502 is_wrapped_ = true;
222 100502 }
223
224
225 124766 void CacheTransport::Frame::UnwrapMsg() {
226
2/2
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 124526 times.
124766 if (msg_rpc_.has_msg_handshake()) {
227 216 msg_typed_ = msg_rpc_.mutable_msg_handshake();
228
2/2
✓ Branch 1 taken 242 times.
✓ Branch 2 taken 124272 times.
124526 } else if (msg_rpc_.has_msg_handshake_ack()) {
229 242 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
230
2/2
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 124068 times.
124272 } else if (msg_rpc_.has_msg_quit()) {
231 216 msg_typed_ = msg_rpc_.mutable_msg_quit();
232
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 124080 times.
124068 } else if (msg_rpc_.has_msg_ioctl()) {
233 48 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
234
2/2
✓ Branch 1 taken 6768 times.
✓ Branch 2 taken 117264 times.
124080 } else if (msg_rpc_.has_msg_refcount_req()) {
235 6768 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
236
2/2
✓ Branch 1 taken 9500 times.
✓ Branch 2 taken 107740 times.
117264 } else if (msg_rpc_.has_msg_refcount_reply()) {
237 9500 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
238
2/2
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 107560 times.
107740 } else if (msg_rpc_.has_msg_object_info_req()) {
239 192 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
240
2/2
✓ Branch 1 taken 324 times.
✓ Branch 2 taken 107248 times.
107560 } else if (msg_rpc_.has_msg_object_info_reply()) {
241 324 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
242
2/2
✓ Branch 1 taken 26532 times.
✓ Branch 2 taken 80728 times.
107248 } else if (msg_rpc_.has_msg_read_req()) {
243 26532 msg_typed_ = msg_rpc_.mutable_msg_read_req();
244
2/2
✓ Branch 1 taken 48820 times.
✓ Branch 2 taken 31944 times.
80728 } else if (msg_rpc_.has_msg_read_reply()) {
245 48820 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
246
2/2
✓ Branch 1 taken 7296 times.
✓ Branch 2 taken 24648 times.
31944 } else if (msg_rpc_.has_msg_store_req()) {
247 7296 msg_typed_ = msg_rpc_.mutable_msg_store_req();
248
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 24636 times.
24648 } else if (msg_rpc_.has_msg_store_abort_req()) {
249 12 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
250
2/2
✓ Branch 1 taken 12122 times.
✓ Branch 2 taken 12514 times.
24636 } else if (msg_rpc_.has_msg_store_reply()) {
251 12122 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
252
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 12466 times.
12514 } else if (msg_rpc_.has_msg_info_req()) {
253 48 msg_typed_ = msg_rpc_.mutable_msg_info_req();
254
2/2
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 12394 times.
12466 } else if (msg_rpc_.has_msg_info_reply()) {
255 72 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
256
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 12370 times.
12394 } else if (msg_rpc_.has_msg_shrink_req()) {
257 24 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
258
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 12338 times.
12370 } else if (msg_rpc_.has_msg_shrink_reply()) {
259 32 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
260
2/2
✓ Branch 1 taken 120 times.
✓ Branch 2 taken 12218 times.
12338 } else if (msg_rpc_.has_msg_list_req()) {
261 120 msg_typed_ = msg_rpc_.mutable_msg_list_req();
262
2/2
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 12090 times.
12218 } else if (msg_rpc_.has_msg_list_reply()) {
263 128 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
264
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 12078 times.
12090 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
265 12 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
266
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 12054 times.
12078 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
267 24 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
268
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 12012 times.
12054 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
269 42 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
270
1/2
✓ Branch 1 taken 12012 times.
✗ Branch 2 not taken.
12012 } else if (msg_rpc_.has_msg_detach()) {
271 12012 msg_typed_ = msg_rpc_.mutable_msg_detach();
272 12012 is_msg_out_of_band_ = true;
273 } else {
274 // Unexpected message type, should never happen
275 ✗ PANIC(NULL);
276 }
277 124802 }
278
279
280 //------------------------------------------------------------------------------
281
282
283 242 CacheTransport::CacheTransport(int fd_connection)
284 242 : fd_connection_(fd_connection), flags_(0) {
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242 times.
242 assert(fd_connection_ >= 0);
286 242 }
287
288
289 53532 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
290 53532 : fd_connection_(fd_connection), flags_(flags) {
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53532 times.
53532 assert(fd_connection_ >= 0);
292 53532 }
293
294
295 2419667 void CacheTransport::FillMsgHash(const shash::Any &hash,
296 cvmfs::MsgHash *msg_hash) {
297
3/4
✓ Branch 0 taken 2419617 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
2419667 switch (hash.algorithm) {
298 2419617 case shash::kSha1:
299 2419617 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
300 2419617 break;
301 12 case shash::kRmd160:
302 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
303 12 break;
304 38 case shash::kShake128:
305 38 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
306 38 break;
307 ✗ default:
308 ✗ PANIC(NULL);
309 }
310 2419667 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
311 2419667 }
312
313
314 9702 void CacheTransport::FillObjectType(int object_flags,
315 cvmfs::EnumObjectType *wire_type) {
316 9702 *wire_type = cvmfs::OBJECT_REGULAR;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9702 times.
9702 if (object_flags & CacheManager::kLabelCatalog)
318 ✗ *wire_type = cvmfs::OBJECT_CATALOG;
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9702 times.
9702 if (object_flags & CacheManager::kLabelVolatile)
320 ✗ *wire_type = cvmfs::OBJECT_VOLATILE;
321 9702 }
322
323
324 40814 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
325 shash::Any *hash) {
326
2/4
✓ Branch 1 taken 40788 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
✗ Branch 4 not taken.
40814 switch (msg_hash.algorithm()) {
327 40788 case cvmfs::HASH_SHA1:
328 40788 hash->algorithm = shash::kSha1;
329 40788 break;
330 ✗ case cvmfs::HASH_RIPEMD160:
331 ✗ hash->algorithm = shash::kRmd160;
332 ✗ break;
333 26 case cvmfs::HASH_SHAKE128:
334 26 hash->algorithm = shash::kShake128;
335 26 break;
336 ✗ default:
337 ✗ return false;
338 }
339 40814 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
340
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 40814 times.
40814 if (msg_hash.digest().length() != digest_size)
341 ✗ return false;
342 40814 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
343 40814 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 100478 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
366 uint32_t size;
367 bool has_attachment;
368
1/2
✓ Branch 1 taken 100478 times.
✗ Branch 2 not taken.
100478 bool retval = RecvHeader(&size, &has_attachment);
369
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 100466 times.
100478 if (!retval)
370 12 return false;
371
372 void *buffer;
373
2/2
✓ Branch 0 taken 66207 times.
✓ Branch 1 taken 34259 times.
100466 if (size <= kMaxStackAlloc)
374 66207 buffer = alloca(size);
375 else
376 34259 buffer = smalloc(size);
377
1/2
✓ Branch 1 taken 100466 times.
✗ Branch 2 not taken.
100466 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
378
2/4
✓ Branch 0 taken 100466 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 100466 times.
100466 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
379 ✗ if (size > kMaxStackAlloc) {
380 ✗ free(buffer);
381 }
382 ✗ return false;
383 }
384
385 100466 uint32_t msg_size = size;
386
2/2
✓ Branch 0 taken 34486 times.
✓ Branch 1 taken 65980 times.
100466 if (has_attachment) {
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34486 times.
34486 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 34486 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
396 34486 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34486 times.
34486 if ((msg_size + kInnerHeaderSize) > size) {
398 ✗ if (size > kMaxStackAlloc) {
399 ✗ free(buffer);
400 }
401 ✗ return false;
402 }
403 }
404
405 100466 void *ptr_msg = has_attachment
406
2/2
✓ Branch 0 taken 34486 times.
✓ Branch 1 taken 65980 times.
100466 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
407 : buffer;
408
1/2
✓ Branch 1 taken 100466 times.
✗ Branch 2 not taken.
100466 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100466 times.
100466 if (!retval) {
410 ✗ if (size > kMaxStackAlloc) {
411 ✗ free(buffer);
412 }
413 ✗ return false;
414 }
415
416
2/2
✓ Branch 0 taken 34486 times.
✓ Branch 1 taken 65980 times.
100466 if (has_attachment) {
417 34486 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34486 times.
34486 if (frame->att_size() < attachment_size) {
419 ✗ if (size > kMaxStackAlloc) {
420 ✗ free(buffer);
421 }
422 ✗ return false;
423 }
424 34486 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
425 34486 + msg_size;
426 34486 memcpy(frame->attachment(), ptr_attachment, attachment_size);
427 34486 frame->set_att_size(attachment_size);
428 } else {
429 65980 frame->set_att_size(0);
430 }
431
2/2
✓ Branch 0 taken 34259 times.
✓ Branch 1 taken 66159 times.
100418 if (size > kMaxStackAlloc) {
432 34259 free(buffer);
433 }
434 100418 return true;
435 }
436
437
438 100478 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
439 unsigned char header[kHeaderSize];
440
1/2
✓ Branch 1 taken 100478 times.
✗ Branch 2 not taken.
100478 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
441
3/4
✓ Branch 0 taken 100478 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 100466 times.
100478 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
442 12 return false;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100466 times.
100466 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
444 ✗ return false;
445 100466 *has_attachment = header[0] & kFlagHasAttachment;
446 100466 *size = header[1] + (header[2] << 8) + (header[3] << 16);
447
2/4
✓ Branch 0 taken 100466 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 100466 times.
✗ Branch 3 not taken.
100466 return (*size > 0) && (*size <= kMaxMsgSize);
448 }
449
450
451 100502 void CacheTransport::SendData(void *message,
452 uint32_t msg_size,
453 void *attachment,
454 uint32_t att_size) {
455 201004 const uint32_t total_size = msg_size + att_size
456
2/2
✓ Branch 0 taken 36216 times.
✓ Branch 1 taken 64286 times.
100502 + ((att_size > 0) ? kInnerHeaderSize : 0);
457
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100502 times.
100502 assert(total_size > 0);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100502 times.
100502 assert(total_size <= kMaxMsgSize);
460
1/2
✓ Branch 1 taken 94776 times.
✗ Branch 2 not taken.
94776 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 64286 times.
✓ Branch 1 taken 36216 times.
100502 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
465 100502 header[1] = (total_size & 0x000000FF);
466 100502 header[2] = (total_size & 0x0000FF00) >> 8;
467 100502 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 100502 iov[0].iov_base = header;
474 100502 iov[0].iov_len = kHeaderSize;
475
476
2/2
✓ Branch 0 taken 36216 times.
✓ Branch 1 taken 64286 times.
100502 if (att_size > 0) {
477 36216 inner_header[0] = (msg_size & 0x000000FF);
478 36216 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
479 36216 iov[1].iov_base = inner_header;
480 36216 iov[1].iov_len = kInnerHeaderSize;
481 36216 iov[2].iov_base = message;
482 36216 iov[2].iov_len = msg_size;
483 36216 iov[3].iov_base = attachment;
484 36216 iov[3].iov_len = att_size;
485 } else {
486 64286 iov[1].iov_base = message;
487 64286 iov[1].iov_len = msg_size;
488 }
489
2/2
✓ Branch 0 taken 12024 times.
✓ Branch 1 taken 88478 times.
100502 if (flags_ & kFlagSendNonBlocking) {
490
2/4
✓ Branch 0 taken 12024 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 12024 times.
✗ Branch 4 not taken.
12024 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
491 12024 return;
492 }
493
3/4
✓ Branch 0 taken 52262 times.
✓ Branch 1 taken 36216 times.
✓ Branch 3 taken 88478 times.
✗ Branch 4 not taken.
88478 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
494
495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 88478 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
88478 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
496 ✗ PANIC(kLogSyslogErr | kLogDebug,
497 "failed to write to external cache transport (%d), aborting", errno);
498 }
499 }
500
501 12024 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12024 times.
12024 assert(iovcnt > 0);
503 12024 unsigned total_size = 0;
504
2/2
✓ Branch 0 taken 24048 times.
✓ Branch 1 taken 12024 times.
36072 for (unsigned i = 0; i < iovcnt; ++i)
505 24048 total_size += iov[i].iov_len;
506 12024 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
507
508 12024 unsigned pos = 0;
509
2/2
✓ Branch 0 taken 24048 times.
✓ Branch 1 taken 12024 times.
36072 for (unsigned i = 0; i < iovcnt; ++i) {
510 24048 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
511 24048 pos += iov[i].iov_len;
512 }
513
514 12024 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12024 times.
12024 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 12024 }
524
525
526 100490 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
527 100490 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
528 100490 const size_t byte_size = msg_rpc->ByteSizeLong();
529
2/4
✓ Branch 0 taken 100502 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 100502 times.
✗ Branch 3 not taken.
100502 assert((byte_size > 0) && (byte_size <= kMaxMsgSize));
530 100502 const uint32_t size = static_cast<uint32_t>(byte_size);
531 #ifdef __APPLE__
532 void *buffer = smalloc(size);
533 #else
534 100502 void *buffer = alloca(size);
535 #endif
536 100502 const bool retval = msg_rpc->SerializeToArray(buffer, size);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100502 times.
100502 assert(retval);
538 100502 SendData(buffer, size, frame->attachment(), frame->att_size());
539 #ifdef __APPLE__
540 free(buffer);
541 #endif
542 100502 }
543