GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-08-30 02:40:36
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 305835 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305835 times.
305835 assert(msg_typed_ != NULL);
36
1/2
✓ Branch 0 taken 305835 times.
✗ Branch 1 not taken.
305835 if (!is_wrapped_)
37 305835 WrapMsg();
38 305873 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 382647 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 382837 times.
382647 assert(msg_rpc_.IsInitialized());
48
2/2
✓ Branch 0 taken 324242 times.
✓ Branch 1 taken 58595 times.
382837 if (msg_typed_ == NULL)
49 324242 UnwrapMsg();
50 382647 return msg_typed_;
51 }
52
53
54 382875 CacheTransport::Frame::Frame()
55 382837 : owns_msg_typed_(false)
56 382837 , msg_typed_(NULL)
57 382837 , attachment_(NULL)
58 382837 , att_size_(0)
59 382837 , is_wrapped_(false)
60 382875 , is_msg_out_of_band_(false) { }
61
62
63 305873 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
64 305873 : owns_msg_typed_(false)
65 305873 , msg_typed_(m)
66 305873 , attachment_(NULL)
67 305873 , att_size_(0)
68 305873 , is_wrapped_(false)
69 305873 , is_msg_out_of_band_(false) { }
70
71
72 688748 CacheTransport::Frame::~Frame() { Reset(0); }
73
74
75 58595 bool CacheTransport::Frame::IsMsgOutOfBand() {
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 58595 times.
58595 assert(msg_rpc_.IsInitialized());
77
1/2
✓ Branch 0 taken 58595 times.
✗ Branch 1 not taken.
58595 if (msg_typed_ == NULL)
78 58595 UnwrapMsg();
79 58595 return is_msg_out_of_band_;
80 }
81
82
83 77064 void CacheTransport::Frame::MergeFrom(const Frame &other) {
84 77064 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
85 77064 owns_msg_typed_ = true;
86
2/2
✓ Branch 0 taken 68400 times.
✓ Branch 1 taken 8664 times.
77064 if (other.att_size_ > 0) {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68400 times.
68400 assert(att_size_ >= other.att_size_);
88 68400 memcpy(attachment_, other.attachment_, other.att_size_);
89 68400 att_size_ = other.att_size_;
90 }
91 77064 }
92
93
94 305811 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
95 305811 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305811 times.
305811 if (!retval)
97 return false;
98
99 // Cleanup typed message when Frame leaves scope
100 305811 owns_msg_typed_ = true;
101 305811 return true;
102 }
103
104
105 688786 void CacheTransport::Frame::Release() {
106
2/2
✓ Branch 0 taken 382875 times.
✓ Branch 1 taken 305911 times.
688786 if (owns_msg_typed_)
107 382875 return;
108
109 305911 (void)msg_rpc_.release_msg_refcount_req();
110 305911 (void)msg_rpc_.release_msg_refcount_reply();
111 305911 (void)msg_rpc_.release_msg_read_req();
112 305873 (void)msg_rpc_.release_msg_read_reply();
113 305873 (void)msg_rpc_.release_msg_object_info_req();
114 305873 (void)msg_rpc_.release_msg_object_info_reply();
115 305873 (void)msg_rpc_.release_msg_store_req();
116 305873 (void)msg_rpc_.release_msg_store_abort_req();
117 305873 (void)msg_rpc_.release_msg_store_reply();
118 305873 (void)msg_rpc_.release_msg_handshake();
119 305873 (void)msg_rpc_.release_msg_handshake_ack();
120 305873 (void)msg_rpc_.release_msg_quit();
121 305873 (void)msg_rpc_.release_msg_ioctl();
122 305911 (void)msg_rpc_.release_msg_info_req();
123 305911 (void)msg_rpc_.release_msg_info_reply();
124 305911 (void)msg_rpc_.release_msg_shrink_req();
125 305911 (void)msg_rpc_.release_msg_shrink_reply();
126 305911 (void)msg_rpc_.release_msg_list_req();
127 305911 (void)msg_rpc_.release_msg_list_reply();
128 305911 (void)msg_rpc_.release_msg_detach();
129 305911 (void)msg_rpc_.release_msg_breadcrumb_store_req();
130 305911 (void)msg_rpc_.release_msg_breadcrumb_load_req();
131 305873 (void)msg_rpc_.release_msg_breadcrumb_reply();
132 }
133
134
135 688786 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
136 688786 msg_typed_ = NULL;
137 688786 att_size_ = original_att_size;
138 688786 is_wrapped_ = false;
139 688786 is_msg_out_of_band_ = false;
140 688786 Release();
141 688710 msg_rpc_.Clear();
142 688748 owns_msg_typed_ = false;
143 688748 }
144
145
146 305835 void CacheTransport::Frame::WrapMsg() {
147
2/2
✓ Branch 3 taken 710 times.
✓ Branch 4 taken 305125 times.
305835 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
148 710 msg_rpc_.set_allocated_msg_handshake(
149 710 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
150
2/2
✓ Branch 3 taken 684 times.
✓ Branch 4 taken 304479 times.
305125 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
151 684 msg_rpc_.set_allocated_msg_handshake_ack(
152 684 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
153
2/2
✓ Branch 3 taken 708 times.
✓ Branch 4 taken 303771 times.
304479 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
154 708 msg_rpc_.set_allocated_msg_quit(
155 708 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
156
2/2
✓ Branch 3 taken 152 times.
✓ Branch 4 taken 303619 times.
303771 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
157 152 msg_rpc_.set_allocated_msg_ioctl(
158 152 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
159
2/2
✓ Branch 3 taken 23936 times.
✓ Branch 4 taken 279645 times.
303619 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
160 23936 msg_rpc_.set_allocated_msg_refcount_req(
161 23936 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
162
2/2
✓ Branch 3 taken 21432 times.
✓ Branch 4 taken 258251 times.
279645 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
163 21432 msg_rpc_.set_allocated_msg_refcount_reply(
164 21432 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
165
2/2
✓ Branch 3 taken 632 times.
✓ Branch 4 taken 257581 times.
258251 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
166 632 msg_rpc_.set_allocated_msg_object_info_req(
167 632 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
168
2/2
✓ Branch 3 taken 608 times.
✓ Branch 4 taken 257011 times.
257581 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
169 608 msg_rpc_.set_allocated_msg_object_info_reply(
170 608 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
171
2/2
✓ Branch 3 taken 84729 times.
✓ Branch 4 taken 172282 times.
257011 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
172 84729 msg_rpc_.set_allocated_msg_read_req(
173 84729 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
174
2/2
✓ Branch 3 taken 84018 times.
✓ Branch 4 taken 88264 times.
172282 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
175 84018 msg_rpc_.set_allocated_msg_read_reply(
176 84018 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
177
2/2
✓ Branch 3 taken 25518 times.
✓ Branch 4 taken 62746 times.
88264 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
178 25518 msg_rpc_.set_allocated_msg_store_req(
179 25518 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
180
2/2
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 62708 times.
62746 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
181 38 msg_rpc_.set_allocated_msg_store_abort_req(
182 38 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
183
2/2
✓ Branch 3 taken 23142 times.
✓ Branch 4 taken 39566 times.
62708 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
184 23142 msg_rpc_.set_allocated_msg_store_reply(
185 23142 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
186
2/2
✓ Branch 3 taken 176 times.
✓ Branch 4 taken 39390 times.
39566 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
187 176 msg_rpc_.set_allocated_msg_info_req(
188 176 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
189
2/2
✓ Branch 3 taken 152 times.
✓ Branch 4 taken 39238 times.
39390 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
190 152 msg_rpc_.set_allocated_msg_info_reply(
191 152 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
192
2/2
✓ Branch 3 taken 84 times.
✓ Branch 4 taken 39154 times.
39238 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
193 84 msg_rpc_.set_allocated_msg_shrink_req(
194 84 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
195
2/2
✓ Branch 3 taken 76 times.
✓ Branch 4 taken 39078 times.
39154 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
196 76 msg_rpc_.set_allocated_msg_shrink_reply(
197 76 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
198
2/2
✓ Branch 3 taken 388 times.
✓ Branch 4 taken 38690 times.
39078 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
199 388 msg_rpc_.set_allocated_msg_list_req(
200 388 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
201
2/2
✓ Branch 3 taken 380 times.
✓ Branch 4 taken 38310 times.
38690 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
202 380 msg_rpc_.set_allocated_msg_list_reply(
203 380 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
204
2/2
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 38270 times.
38310 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
205 40 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
206 40 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
207
2/2
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 38190 times.
38270 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
208 80 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
209 80 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
210
2/2
✓ Branch 3 taken 114 times.
✓ Branch 4 taken 38076 times.
38190 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
211 114 msg_rpc_.set_allocated_msg_breadcrumb_reply(
212 114 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
213
1/2
✓ Branch 3 taken 38076 times.
✗ Branch 4 not taken.
38076 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
214 38076 msg_rpc_.set_allocated_msg_detach(
215 38076 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
216 38076 is_msg_out_of_band_ = true;
217 } else {
218 // Unexpected message type, should never happen
219 PANIC(NULL);
220 }
221 305873 is_wrapped_ = true;
222 305873 }
223
224
225 382799 void CacheTransport::Frame::UnwrapMsg() {
226
2/2
✓ Branch 1 taken 684 times.
✓ Branch 2 taken 382001 times.
382799 if (msg_rpc_.has_msg_handshake()) {
227 684 msg_typed_ = msg_rpc_.mutable_msg_handshake();
228
2/2
✓ Branch 1 taken 710 times.
✓ Branch 2 taken 381405 times.
382001 } else if (msg_rpc_.has_msg_handshake_ack()) {
229 710 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
230
2/2
✓ Branch 1 taken 684 times.
✓ Branch 2 taken 380607 times.
381405 } else if (msg_rpc_.has_msg_quit()) {
231 684 msg_typed_ = msg_rpc_.mutable_msg_quit();
232
2/2
✓ Branch 1 taken 152 times.
✓ Branch 2 taken 380531 times.
380607 } else if (msg_rpc_.has_msg_ioctl()) {
233 152 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
234
2/2
✓ Branch 1 taken 21432 times.
✓ Branch 2 taken 358985 times.
380531 } else if (msg_rpc_.has_msg_refcount_req()) {
235 21432 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
236
2/2
✓ Branch 1 taken 24658 times.
✓ Branch 2 taken 334441 times.
358985 } else if (msg_rpc_.has_msg_refcount_reply()) {
237 24658 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
238
2/2
✓ Branch 1 taken 608 times.
✓ Branch 2 taken 333681 times.
334441 } else if (msg_rpc_.has_msg_object_info_req()) {
239 608 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
240
2/2
✓ Branch 1 taken 974 times.
✓ Branch 2 taken 332821 times.
333681 } else if (msg_rpc_.has_msg_object_info_reply()) {
241 974 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
242
2/2
✓ Branch 1 taken 84018 times.
✓ Branch 2 taken 248727 times.
332821 } else if (msg_rpc_.has_msg_read_req()) {
243 84018 msg_typed_ = msg_rpc_.mutable_msg_read_req();
244
2/2
✓ Branch 1 taken 153129 times.
✓ Branch 2 taken 95826 times.
248727 } else if (msg_rpc_.has_msg_read_reply()) {
245 153129 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
246
2/2
✓ Branch 1 taken 23104 times.
✓ Branch 2 taken 72722 times.
95826 } else if (msg_rpc_.has_msg_store_req()) {
247 23104 msg_typed_ = msg_rpc_.mutable_msg_store_req();
248
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 72684 times.
72722 } else if (msg_rpc_.has_msg_store_abort_req()) {
249 38 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
250
2/2
✓ Branch 1 taken 33156 times.
✓ Branch 2 taken 39528 times.
72684 } else if (msg_rpc_.has_msg_store_reply()) {
251 33156 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
252
2/2
✓ Branch 1 taken 152 times.
✓ Branch 2 taken 39376 times.
39528 } else if (msg_rpc_.has_msg_info_req()) {
253 152 msg_typed_ = msg_rpc_.mutable_msg_info_req();
254
2/2
✓ Branch 1 taken 176 times.
✓ Branch 2 taken 39200 times.
39376 } else if (msg_rpc_.has_msg_info_reply()) {
255 176 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
256
2/2
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 39124 times.
39200 } else if (msg_rpc_.has_msg_shrink_req()) {
257 76 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
258
2/2
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 39040 times.
39124 } else if (msg_rpc_.has_msg_shrink_reply()) {
259 84 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
260
2/2
✓ Branch 1 taken 380 times.
✓ Branch 2 taken 38660 times.
39040 } else if (msg_rpc_.has_msg_list_req()) {
261 380 msg_typed_ = msg_rpc_.mutable_msg_list_req();
262
2/2
✓ Branch 1 taken 388 times.
✓ Branch 2 taken 38272 times.
38660 } else if (msg_rpc_.has_msg_list_reply()) {
263 388 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
264
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 38234 times.
38272 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
265 38 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
266
2/2
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 38158 times.
38234 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
267 76 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
268
2/2
✓ Branch 1 taken 120 times.
✓ Branch 2 taken 38038 times.
38158 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
269 120 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
270
1/2
✓ Branch 1 taken 38038 times.
✗ Branch 2 not taken.
38038 } else if (msg_rpc_.has_msg_detach()) {
271 38038 msg_typed_ = msg_rpc_.mutable_msg_detach();
272 38038 is_msg_out_of_band_ = true;
273 } else {
274 // Unexpected message type, should never happen
275 PANIC(NULL);
276 }
277 382875 }
278
279
280 //------------------------------------------------------------------------------
281
282
283 710 CacheTransport::CacheTransport(int fd_connection)
284 710 : fd_connection_(fd_connection), flags_(0) {
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 710 times.
710 assert(fd_connection_ >= 0);
286 710 }
287
288
289 169518 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
290 169518 : fd_connection_(fd_connection), flags_(flags) {
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 169518 times.
169518 assert(fd_connection_ >= 0);
292 169518 }
293
294
295 7651122 void CacheTransport::FillMsgHash(const shash::Any &hash,
296 cvmfs::MsgHash *msg_hash) {
297
3/4
✓ Branch 0 taken 7651020 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
7651122 switch (hash.algorithm) {
298 7651020 case shash::kSha1:
299 7651020 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
300 7651020 break;
301 12 case shash::kRmd160:
302 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
303 12 break;
304 90 case shash::kShake128:
305 90 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
306 90 break;
307 default:
308 PANIC(NULL);
309 }
310 7651122 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
311 7651122 }
312
313
314 25510 void CacheTransport::FillObjectType(int object_flags,
315 cvmfs::EnumObjectType *wire_type) {
316 25510 *wire_type = cvmfs::OBJECT_REGULAR;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25510 times.
25510 if (object_flags & CacheManager::kLabelCatalog)
318 *wire_type = cvmfs::OBJECT_CATALOG;
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25510 times.
25510 if (object_flags & CacheManager::kLabelVolatile)
320 *wire_type = cvmfs::OBJECT_VOLATILE;
321 25510 }
322
323
324 129240 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
325 shash::Any *hash) {
326
2/4
✓ Branch 1 taken 129162 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 78 times.
✗ Branch 4 not taken.
129240 switch (msg_hash.algorithm()) {
327 129162 case cvmfs::HASH_SHA1:
328 129162 hash->algorithm = shash::kSha1;
329 129162 break;
330 case cvmfs::HASH_RIPEMD160:
331 hash->algorithm = shash::kRmd160;
332 break;
333 78 case cvmfs::HASH_SHAKE128:
334 78 hash->algorithm = shash::kShake128;
335 78 break;
336 default:
337 return false;
338 }
339 129240 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
340
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 129240 times.
129240 if (msg_hash.digest().length() != digest_size)
341 return false;
342 129240 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
343 129240 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 305849 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
366 uint32_t size;
367 bool has_attachment;
368
1/2
✓ Branch 1 taken 305849 times.
✗ Branch 2 not taken.
305849 bool retval = RecvHeader(&size, &has_attachment);
369
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 305811 times.
305849 if (!retval)
370 38 return false;
371
372 void *buffer;
373
2/2
✓ Branch 0 taken 198360 times.
✓ Branch 1 taken 107451 times.
305811 if (size <= kMaxStackAlloc)
374 198360 buffer = alloca(size);
375 else
376 107451 buffer = smalloc(size);
377
1/2
✓ Branch 1 taken 305811 times.
✗ Branch 2 not taken.
305811 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
378
2/4
✓ Branch 0 taken 305811 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 305811 times.
305811 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
379 if (size > kMaxStackAlloc) {
380 free(buffer);
381 }
382 return false;
383 }
384
385 305811 uint32_t msg_size = size;
386
2/2
✓ Branch 0 taken 107751 times.
✓ Branch 1 taken 198060 times.
305811 if (has_attachment) {
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107751 times.
107751 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 107751 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
396 107751 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107751 times.
107751 if ((msg_size + kInnerHeaderSize) > size) {
398 if (size > kMaxStackAlloc) {
399 free(buffer);
400 }
401 return false;
402 }
403 }
404
405 305811 void *ptr_msg = has_attachment
406
2/2
✓ Branch 0 taken 107751 times.
✓ Branch 1 taken 198060 times.
305811 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
407 : buffer;
408
1/2
✓ Branch 1 taken 305811 times.
✗ Branch 2 not taken.
305811 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305811 times.
305811 if (!retval) {
410 if (size > kMaxStackAlloc) {
411 free(buffer);
412 }
413 return false;
414 }
415
416
2/2
✓ Branch 0 taken 107751 times.
✓ Branch 1 taken 198060 times.
305811 if (has_attachment) {
417 107751 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 107751 times.
107751 if (frame->att_size() < attachment_size) {
419 if (size > kMaxStackAlloc) {
420 free(buffer);
421 }
422 return false;
423 }
424 107751 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
425 107751 + msg_size;
426 107751 memcpy(frame->attachment(), ptr_attachment, attachment_size);
427 107751 frame->set_att_size(attachment_size);
428 } else {
429 198060 frame->set_att_size(0);
430 }
431
2/2
✓ Branch 0 taken 107451 times.
✓ Branch 1 taken 198360 times.
305811 if (size > kMaxStackAlloc) {
432 107451 free(buffer);
433 }
434 305811 return true;
435 }
436
437
438 305849 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
439 unsigned char header[kHeaderSize];
440
1/2
✓ Branch 1 taken 305849 times.
✗ Branch 2 not taken.
305849 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
441
3/4
✓ Branch 0 taken 305849 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 305811 times.
305849 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
442 38 return false;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305811 times.
305811 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
444 return false;
445 305811 *has_attachment = header[0] & kFlagHasAttachment;
446 305811 *size = header[1] + (header[2] << 8) + (header[3] << 16);
447
2/4
✓ Branch 0 taken 305811 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 305811 times.
✗ Branch 3 not taken.
305811 return (*size > 0) && (*size <= kMaxMsgSize);
448 }
449
450
451 305759 void CacheTransport::SendData(void *message,
452 uint32_t msg_size,
453 void *attachment,
454 uint32_t att_size) {
455 611518 const uint32_t total_size = msg_size + att_size
456
2/2
✓ Branch 0 taken 109458 times.
✓ Branch 1 taken 196301 times.
305759 + ((att_size > 0) ? kInnerHeaderSize : 0);
457
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305759 times.
305759 assert(total_size > 0);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305759 times.
305759 assert(total_size <= kMaxMsgSize);
460
1/2
✓ Branch 1 taken 300124 times.
✗ Branch 2 not taken.
300010 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 196415 times.
✓ Branch 1 taken 109458 times.
305873 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
465 305873 header[1] = (total_size & 0x000000FF);
466 305873 header[2] = (total_size & 0x0000FF00) >> 8;
467 305873 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 305873 iov[0].iov_base = header;
474 305873 iov[0].iov_len = kHeaderSize;
475
476
2/2
✓ Branch 0 taken 109458 times.
✓ Branch 1 taken 196415 times.
305873 if (att_size > 0) {
477 109458 inner_header[0] = (msg_size & 0x000000FF);
478 109458 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
479 109458 iov[1].iov_base = inner_header;
480 109458 iov[1].iov_len = kInnerHeaderSize;
481 109458 iov[2].iov_base = message;
482 109458 iov[2].iov_len = msg_size;
483 109458 iov[3].iov_base = attachment;
484 109458 iov[3].iov_len = att_size;
485 } else {
486 196415 iov[1].iov_base = message;
487 196415 iov[1].iov_len = msg_size;
488 }
489
2/2
✓ Branch 0 taken 38076 times.
✓ Branch 1 taken 267797 times.
305873 if (flags_ & kFlagSendNonBlocking) {
490
2/4
✓ Branch 0 taken 38076 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 38076 times.
✗ Branch 4 not taken.
38076 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
491 38076 return;
492 }
493
3/4
✓ Branch 0 taken 158339 times.
✓ Branch 1 taken 109458 times.
✓ Branch 3 taken 267797 times.
✗ Branch 4 not taken.
267797 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
494
495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 267797 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
267797 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
496 PANIC(kLogSyslogErr | kLogDebug,
497 "failed to write to external cache transport (%d), aborting", errno);
498 }
499 }
500
501 38076 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38076 times.
38076 assert(iovcnt > 0);
503 38076 unsigned total_size = 0;
504
2/2
✓ Branch 0 taken 76152 times.
✓ Branch 1 taken 38076 times.
114228 for (unsigned i = 0; i < iovcnt; ++i)
505 76152 total_size += iov[i].iov_len;
506 38076 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
507
508 38076 unsigned pos = 0;
509
2/2
✓ Branch 0 taken 76152 times.
✓ Branch 1 taken 38076 times.
114228 for (unsigned i = 0; i < iovcnt; ++i) {
510 76152 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
511 76152 pos += iov[i].iov_len;
512 }
513
514 38076 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38076 times.
38076 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 38076 }
524
525
526 305835 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
527 305835 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
528 305873 const size_t byte_size = msg_rpc->ByteSizeLong();
529
2/4
✓ Branch 0 taken 305873 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 305873 times.
✗ Branch 3 not taken.
305873 assert((byte_size > 0) && (byte_size <= kMaxMsgSize));
530 305873 const uint32_t size = static_cast<uint32_t>(byte_size);
531 #ifdef __APPLE__
532 void *buffer = smalloc(size);
533 #else
534 305873 void *buffer = alloca(size);
535 #endif
536 305873 const bool retval = msg_rpc->SerializeToArray(buffer, size);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305759 times.
305759 assert(retval);
538 305759 SendData(buffer, size, frame->attachment(), frame->att_size());
539 #ifdef __APPLE__
540 free(buffer);
541 #endif
542 305873 }
543