GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-10-25 02:40:14
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 266241 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266241 times.
266241 assert(msg_typed_ != NULL);
36
1/2
✓ Branch 0 taken 266241 times.
✗ Branch 1 not taken.
266241 if (!is_wrapped_)
37 266241 WrapMsg();
38 266307 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 333207 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 333207 times.
333207 assert(msg_rpc_.IsInitialized());
48
2/2
✓ Branch 0 taken 281582 times.
✓ Branch 1 taken 51625 times.
333207 if (msg_typed_ == NULL)
49 281582 UnwrapMsg();
50 333042 return msg_typed_;
51 }
52
53
54 333240 CacheTransport::Frame::Frame()
55 333174 : owns_msg_typed_(false)
56 333174 , msg_typed_(NULL)
57 333174 , attachment_(NULL)
58 333174 , att_size_(0)
59 333174 , is_wrapped_(false)
60 333240 , is_msg_out_of_band_(false) { }
61
62
63 266373 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
64 266373 : owns_msg_typed_(false)
65 266373 , msg_typed_(m)
66 266373 , attachment_(NULL)
67 266373 , att_size_(0)
68 266373 , is_wrapped_(false)
69 266373 , is_msg_out_of_band_(false) { }
70
71
72 599613 CacheTransport::Frame::~Frame() { Reset(0); }
73
74
75 51625 bool CacheTransport::Frame::IsMsgOutOfBand() {
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51625 times.
51625 assert(msg_rpc_.IsInitialized());
77
1/2
✓ Branch 0 taken 51625 times.
✗ Branch 1 not taken.
51625 if (msg_typed_ == NULL)
78 51625 UnwrapMsg();
79 51625 return is_msg_out_of_band_;
80 }
81
82
83 66924 void CacheTransport::Frame::MergeFrom(const Frame &other) {
84 66924 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
85 66924 owns_msg_typed_ = true;
86
2/2
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 7524 times.
66924 if (other.att_size_ > 0) {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59400 times.
59400 assert(att_size_ >= other.att_size_);
88 59400 memcpy(attachment_, other.attachment_, other.att_size_);
89 59400 att_size_ = other.att_size_;
90 }
91 66924 }
92
93
94 266316 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
95 266316 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266316 times.
266316 if (!retval)
97 ✗ return false;
98
99 // Cleanup typed message when Frame leaves scope
100 266316 owns_msg_typed_ = true;
101 266316 return true;
102 }
103
104
105 599646 void CacheTransport::Frame::Release() {
106
2/2
✓ Branch 0 taken 333240 times.
✓ Branch 1 taken 266406 times.
599646 if (owns_msg_typed_)
107 333240 return;
108
109 266406 (void)msg_rpc_.release_msg_refcount_req();
110 266373 (void)msg_rpc_.release_msg_refcount_reply();
111 266373 (void)msg_rpc_.release_msg_read_req();
112 266406 (void)msg_rpc_.release_msg_read_reply();
113 266406 (void)msg_rpc_.release_msg_object_info_req();
114 266406 (void)msg_rpc_.release_msg_object_info_reply();
115 266406 (void)msg_rpc_.release_msg_store_req();
116 266406 (void)msg_rpc_.release_msg_store_abort_req();
117 266406 (void)msg_rpc_.release_msg_store_reply();
118 266406 (void)msg_rpc_.release_msg_handshake();
119 266406 (void)msg_rpc_.release_msg_handshake_ack();
120 266406 (void)msg_rpc_.release_msg_quit();
121 266406 (void)msg_rpc_.release_msg_ioctl();
122 266406 (void)msg_rpc_.release_msg_info_req();
123 266406 (void)msg_rpc_.release_msg_info_reply();
124 266406 (void)msg_rpc_.release_msg_shrink_req();
125 266406 (void)msg_rpc_.release_msg_shrink_reply();
126 266406 (void)msg_rpc_.release_msg_list_req();
127 266406 (void)msg_rpc_.release_msg_list_reply();
128 266406 (void)msg_rpc_.release_msg_detach();
129 266406 (void)msg_rpc_.release_msg_breadcrumb_store_req();
130 266406 (void)msg_rpc_.release_msg_breadcrumb_load_req();
131 266406 (void)msg_rpc_.release_msg_breadcrumb_reply();
132 }
133
134
135 599646 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
136 599646 msg_typed_ = NULL;
137 599646 att_size_ = original_att_size;
138 599646 is_wrapped_ = false;
139 599646 is_msg_out_of_band_ = false;
140 599646 Release();
141 599580 msg_rpc_.Clear();
142 599547 owns_msg_typed_ = false;
143 599547 }
144
145
146 266274 void CacheTransport::Frame::WrapMsg() {
147
2/2
✓ Branch 3 taken 620 times.
✓ Branch 4 taken 265588 times.
266274 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
148 620 msg_rpc_.set_allocated_msg_handshake(
149 620 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
150
2/2
✓ Branch 3 taken 594 times.
✓ Branch 4 taken 265027 times.
265588 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
151 594 msg_rpc_.set_allocated_msg_handshake_ack(
152 594 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
153
2/2
✓ Branch 3 taken 618 times.
✓ Branch 4 taken 264508 times.
265027 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
154 618 msg_rpc_.set_allocated_msg_quit(
155 618 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
156
2/2
✓ Branch 3 taken 132 times.
✓ Branch 4 taken 264409 times.
264508 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
157 132 msg_rpc_.set_allocated_msg_ioctl(
158 132 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
159
2/2
✓ Branch 3 taken 21116 times.
✓ Branch 4 taken 243227 times.
264409 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
160 21116 msg_rpc_.set_allocated_msg_refcount_req(
161 21116 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
162
2/2
✓ Branch 3 taken 18612 times.
✓ Branch 4 taken 224615 times.
243227 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
163 18612 msg_rpc_.set_allocated_msg_refcount_reply(
164 18612 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
165
2/2
✓ Branch 3 taken 552 times.
✓ Branch 4 taken 224096 times.
224615 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
166 552 msg_rpc_.set_allocated_msg_object_info_req(
167 552 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
168
2/2
✓ Branch 3 taken 528 times.
✓ Branch 4 taken 223568 times.
224096 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
169 528 msg_rpc_.set_allocated_msg_object_info_reply(
170 528 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
171
2/2
✓ Branch 3 taken 73664 times.
✓ Branch 4 taken 149937 times.
223568 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
172 73664 msg_rpc_.set_allocated_msg_read_req(
173 73664 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
174
2/2
✓ Branch 3 taken 72963 times.
✓ Branch 4 taken 76974 times.
149937 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
175 72963 msg_rpc_.set_allocated_msg_read_reply(
176 72963 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
177
2/2
✓ Branch 3 taken 22478 times.
✓ Branch 4 taken 54496 times.
76974 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
178 22478 msg_rpc_.set_allocated_msg_store_req(
179 22478 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
180
2/2
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 54463 times.
54496 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
181 33 msg_rpc_.set_allocated_msg_store_abort_req(
182 33 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
183
2/2
✓ Branch 3 taken 20097 times.
✓ Branch 4 taken 34366 times.
54463 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
184 20097 msg_rpc_.set_allocated_msg_store_reply(
185 20097 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
186
2/2
✓ Branch 3 taken 156 times.
✓ Branch 4 taken 34210 times.
34366 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
187 156 msg_rpc_.set_allocated_msg_info_req(
188 156 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
189
2/2
✓ Branch 3 taken 132 times.
✓ Branch 4 taken 34078 times.
34210 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
190 132 msg_rpc_.set_allocated_msg_info_reply(
191 132 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
192
2/2
✓ Branch 3 taken 74 times.
✓ Branch 4 taken 34004 times.
34078 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
193 74 msg_rpc_.set_allocated_msg_shrink_req(
194 74 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
195
2/2
✓ Branch 3 taken 66 times.
✓ Branch 4 taken 33938 times.
34004 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
196 66 msg_rpc_.set_allocated_msg_shrink_reply(
197 66 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
198
2/2
✓ Branch 3 taken 338 times.
✓ Branch 4 taken 33600 times.
33938 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
199 338 msg_rpc_.set_allocated_msg_list_req(
200 338 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
201
2/2
✓ Branch 3 taken 330 times.
✓ Branch 4 taken 33270 times.
33600 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
202 330 msg_rpc_.set_allocated_msg_list_reply(
203 330 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
204
2/2
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 33235 times.
33270 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
205 35 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
206 35 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
207
2/2
✓ Branch 3 taken 70 times.
✓ Branch 4 taken 33165 times.
33235 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
208 70 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
209 70 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
210
2/2
✓ Branch 3 taken 99 times.
✓ Branch 4 taken 33066 times.
33165 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
211 99 msg_rpc_.set_allocated_msg_breadcrumb_reply(
212 99 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
213
1/2
✓ Branch 3 taken 33066 times.
✗ Branch 4 not taken.
33066 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
214 33066 msg_rpc_.set_allocated_msg_detach(
215 33066 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
216 33066 is_msg_out_of_band_ = true;
217 } else {
218 // Unexpected message type, should never happen
219 ✗ PANIC(NULL);
220 }
221 266373 is_wrapped_ = true;
222 266373 }
223
224
225 333207 void CacheTransport::Frame::UnwrapMsg() {
226
2/2
✓ Branch 1 taken 594 times.
✓ Branch 2 taken 332514 times.
333207 if (msg_rpc_.has_msg_handshake()) {
227 594 msg_typed_ = msg_rpc_.mutable_msg_handshake();
228
2/2
✓ Branch 1 taken 620 times.
✓ Branch 2 taken 331828 times.
332514 } else if (msg_rpc_.has_msg_handshake_ack()) {
229 620 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
230
2/2
✓ Branch 1 taken 594 times.
✓ Branch 2 taken 331267 times.
331828 } else if (msg_rpc_.has_msg_quit()) {
231 594 msg_typed_ = msg_rpc_.mutable_msg_quit();
232
2/2
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 331267 times.
331267 } else if (msg_rpc_.has_msg_ioctl()) {
233 132 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
234
2/2
✓ Branch 1 taken 18612 times.
✓ Branch 2 taken 312457 times.
331267 } else if (msg_rpc_.has_msg_refcount_req()) {
235 18612 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
236
2/2
✓ Branch 1 taken 21743 times.
✓ Branch 2 taken 290780 times.
312457 } else if (msg_rpc_.has_msg_refcount_reply()) {
237 21743 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
238
2/2
✓ Branch 1 taken 528 times.
✓ Branch 2 taken 290318 times.
290780 } else if (msg_rpc_.has_msg_object_info_req()) {
239 528 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
240
2/2
✓ Branch 1 taken 849 times.
✓ Branch 2 taken 289436 times.
290318 } else if (msg_rpc_.has_msg_object_info_reply()) {
241 849 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
242
2/2
✓ Branch 1 taken 72963 times.
✓ Branch 2 taken 216440 times.
289436 } else if (msg_rpc_.has_msg_read_req()) {
243 72963 msg_typed_ = msg_rpc_.mutable_msg_read_req();
244
2/2
✓ Branch 1 taken 133064 times.
✓ Branch 2 taken 83541 times.
216440 } else if (msg_rpc_.has_msg_read_reply()) {
245 133064 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
246
2/2
✓ Branch 1 taken 20064 times.
✓ Branch 2 taken 63477 times.
83541 } else if (msg_rpc_.has_msg_store_req()) {
247 20064 msg_typed_ = msg_rpc_.mutable_msg_store_req();
248
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 63444 times.
63477 } else if (msg_rpc_.has_msg_store_abort_req()) {
249 33 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
250
2/2
✓ Branch 1 taken 29111 times.
✓ Branch 2 taken 34333 times.
63444 } else if (msg_rpc_.has_msg_store_reply()) {
251 29111 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
252
2/2
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 34201 times.
34333 } else if (msg_rpc_.has_msg_info_req()) {
253 132 msg_typed_ = msg_rpc_.mutable_msg_info_req();
254
2/2
✓ Branch 1 taken 156 times.
✓ Branch 2 taken 34045 times.
34201 } else if (msg_rpc_.has_msg_info_reply()) {
255 156 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
256
2/2
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 33979 times.
34045 } else if (msg_rpc_.has_msg_shrink_req()) {
257 66 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
258
2/2
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 33905 times.
33979 } else if (msg_rpc_.has_msg_shrink_reply()) {
259 74 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
260
2/2
✓ Branch 1 taken 330 times.
✓ Branch 2 taken 33575 times.
33905 } else if (msg_rpc_.has_msg_list_req()) {
261 330 msg_typed_ = msg_rpc_.mutable_msg_list_req();
262
2/2
✓ Branch 1 taken 338 times.
✓ Branch 2 taken 33237 times.
33575 } else if (msg_rpc_.has_msg_list_reply()) {
263 338 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
264
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 33204 times.
33237 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
265 33 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
266
2/2
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 33138 times.
33204 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
267 66 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
268
2/2
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 33033 times.
33138 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
269 105 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
270
1/2
✓ Branch 1 taken 33033 times.
✗ Branch 2 not taken.
33033 } else if (msg_rpc_.has_msg_detach()) {
271 33033 msg_typed_ = msg_rpc_.mutable_msg_detach();
272 33033 is_msg_out_of_band_ = true;
273 } else {
274 // Unexpected message type, should never happen
275 ✗ PANIC(NULL);
276 }
277 333240 }
278
279
280 //------------------------------------------------------------------------------
281
282
283 620 CacheTransport::CacheTransport(int fd_connection)
284 620 : fd_connection_(fd_connection), flags_(0) {
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 620 times.
620 assert(fd_connection_ >= 0);
286 620 }
287
288
289 147213 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
290 147213 : fd_connection_(fd_connection), flags_(flags) {
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 147213 times.
147213 assert(fd_connection_ >= 0);
292 147213 }
293
294
295 6645070 void CacheTransport::FillMsgHash(const shash::Any &hash,
296 cvmfs::MsgHash *msg_hash) {
297
3/4
✓ Branch 0 taken 6644978 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
6645070 switch (hash.algorithm) {
298 6644978 case shash::kSha1:
299 6644978 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
300 6644978 break;
301 12 case shash::kRmd160:
302 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
303 12 break;
304 80 case shash::kShake128:
305 80 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
306 80 break;
307 ✗ default:
308 ✗ PANIC(NULL);
309 }
310 6645070 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
311 6645070 }
312
313
314 22470 void CacheTransport::FillObjectType(int object_flags,
315 cvmfs::EnumObjectType *wire_type) {
316 22470 *wire_type = cvmfs::OBJECT_REGULAR;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22470 times.
22470 if (object_flags & CacheManager::kLabelCatalog)
318 ✗ *wire_type = cvmfs::OBJECT_CATALOG;
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22470 times.
22470 if (object_flags & CacheManager::kLabelVolatile)
320 ✗ *wire_type = cvmfs::OBJECT_VOLATILE;
321 22470 }
322
323
324 112235 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
325 shash::Any *hash) {
326
2/4
✓ Branch 1 taken 112167 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 68 times.
✗ Branch 4 not taken.
112235 switch (msg_hash.algorithm()) {
327 112167 case cvmfs::HASH_SHA1:
328 112167 hash->algorithm = shash::kSha1;
329 112167 break;
330 ✗ case cvmfs::HASH_RIPEMD160:
331 ✗ hash->algorithm = shash::kRmd160;
332 ✗ break;
333 68 case cvmfs::HASH_SHAKE128:
334 68 hash->algorithm = shash::kShake128;
335 68 break;
336 ✗ default:
337 ✗ return false;
338 }
339 112235 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
340
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 112235 times.
112235 if (msg_hash.digest().length() != digest_size)
341 ✗ return false;
342 112235 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
343 112235 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 266349 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
366 uint32_t size;
367 bool has_attachment;
368
1/2
✓ Branch 1 taken 266349 times.
✗ Branch 2 not taken.
266349 bool retval = RecvHeader(&size, &has_attachment);
369
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 266316 times.
266349 if (!retval)
370 33 return false;
371
372 void *buffer;
373
2/2
✓ Branch 0 taken 172943 times.
✓ Branch 1 taken 93373 times.
266316 if (size <= kMaxStackAlloc)
374 172943 buffer = alloca(size);
375 else
376 93373 buffer = smalloc(size);
377
1/2
✓ Branch 1 taken 266316 times.
✗ Branch 2 not taken.
266316 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
378
2/4
✓ Branch 0 taken 266316 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 266316 times.
266316 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
379 ✗ if (size > kMaxStackAlloc) {
380 ✗ free(buffer);
381 }
382 ✗ return false;
383 }
384
385 266316 uint32_t msg_size = size;
386
2/2
✓ Branch 0 taken 93656 times.
✓ Branch 1 taken 172660 times.
266316 if (has_attachment) {
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93656 times.
93656 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 93656 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
396 93656 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93656 times.
93656 if ((msg_size + kInnerHeaderSize) > size) {
398 ✗ if (size > kMaxStackAlloc) {
399 ✗ free(buffer);
400 }
401 ✗ return false;
402 }
403 }
404
405 266316 void *ptr_msg = has_attachment
406
2/2
✓ Branch 0 taken 93656 times.
✓ Branch 1 taken 172660 times.
266316 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
407 : buffer;
408
1/2
✓ Branch 1 taken 266316 times.
✗ Branch 2 not taken.
266316 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266316 times.
266316 if (!retval) {
410 ✗ if (size > kMaxStackAlloc) {
411 ✗ free(buffer);
412 }
413 ✗ return false;
414 }
415
416
2/2
✓ Branch 0 taken 93656 times.
✓ Branch 1 taken 172660 times.
266316 if (has_attachment) {
417 93656 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 93656 times.
93656 if (frame->att_size() < attachment_size) {
419 ✗ if (size > kMaxStackAlloc) {
420 ✗ free(buffer);
421 }
422 ✗ return false;
423 }
424 93656 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
425 93656 + msg_size;
426 93656 memcpy(frame->attachment(), ptr_attachment, attachment_size);
427 93656 frame->set_att_size(attachment_size);
428 } else {
429 172660 frame->set_att_size(0);
430 }
431
2/2
✓ Branch 0 taken 93373 times.
✓ Branch 1 taken 172811 times.
266184 if (size > kMaxStackAlloc) {
432 93373 free(buffer);
433 }
434 266184 return true;
435 }
436
437
438 266349 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
439 unsigned char header[kHeaderSize];
440
1/2
✓ Branch 1 taken 266349 times.
✗ Branch 2 not taken.
266349 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
441
3/4
✓ Branch 0 taken 266349 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 266316 times.
266349 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
442 33 return false;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266316 times.
266316 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
444 ✗ return false;
445 266316 *has_attachment = header[0] & kFlagHasAttachment;
446 266316 *size = header[1] + (header[2] << 8) + (header[3] << 16);
447
2/4
✓ Branch 0 taken 266316 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 266316 times.
✗ Branch 3 not taken.
266316 return (*size > 0) && (*size <= kMaxMsgSize);
448 }
449
450
451 266307 void CacheTransport::SendData(void *message,
452 uint32_t msg_size,
453 void *attachment,
454 uint32_t att_size) {
455 532614 const uint32_t total_size = msg_size + att_size
456
2/2
✓ Branch 0 taken 95373 times.
✓ Branch 1 taken 170934 times.
266307 + ((att_size > 0) ? kInnerHeaderSize : 0);
457
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266307 times.
266307 assert(total_size > 0);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266307 times.
266307 assert(total_size <= kMaxMsgSize);
460
1/2
✓ Branch 1 taken 260634 times.
✗ Branch 2 not taken.
260568 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 171000 times.
✓ Branch 1 taken 95373 times.
266373 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
465 266373 header[1] = (total_size & 0x000000FF);
466 266373 header[2] = (total_size & 0x0000FF00) >> 8;
467 266373 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 266373 iov[0].iov_base = header;
474 266373 iov[0].iov_len = kHeaderSize;
475
476
2/2
✓ Branch 0 taken 95373 times.
✓ Branch 1 taken 171000 times.
266373 if (att_size > 0) {
477 95373 inner_header[0] = (msg_size & 0x000000FF);
478 95373 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
479 95373 iov[1].iov_base = inner_header;
480 95373 iov[1].iov_len = kInnerHeaderSize;
481 95373 iov[2].iov_base = message;
482 95373 iov[2].iov_len = msg_size;
483 95373 iov[3].iov_base = attachment;
484 95373 iov[3].iov_len = att_size;
485 } else {
486 171000 iov[1].iov_base = message;
487 171000 iov[1].iov_len = msg_size;
488 }
489
2/2
✓ Branch 0 taken 33066 times.
✓ Branch 1 taken 233307 times.
266373 if (flags_ & kFlagSendNonBlocking) {
490
2/4
✓ Branch 0 taken 33066 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 33066 times.
✗ Branch 4 not taken.
33066 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
491 33066 return;
492 }
493
3/4
✓ Branch 0 taken 137934 times.
✓ Branch 1 taken 95373 times.
✓ Branch 3 taken 233307 times.
✗ Branch 4 not taken.
233307 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
494
495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 233307 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
233307 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
496 ✗ PANIC(kLogSyslogErr | kLogDebug,
497 "failed to write to external cache transport (%d), aborting", errno);
498 }
499 }
500
501 33066 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33066 times.
33066 assert(iovcnt > 0);
503 33066 unsigned total_size = 0;
504
2/2
✓ Branch 0 taken 66132 times.
✓ Branch 1 taken 33066 times.
99198 for (unsigned i = 0; i < iovcnt; ++i)
505 66132 total_size += iov[i].iov_len;
506 33066 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
507
508 33066 unsigned pos = 0;
509
2/2
✓ Branch 0 taken 66132 times.
✓ Branch 1 taken 33066 times.
99198 for (unsigned i = 0; i < iovcnt; ++i) {
510 66132 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
511 66132 pos += iov[i].iov_len;
512 }
513
514 33066 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33066 times.
33066 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 33066 }
524
525
526 266340 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
527 266340 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
528 266274 const size_t byte_size = msg_rpc->ByteSizeLong();
529
2/4
✓ Branch 0 taken 265977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 265977 times.
✗ Branch 3 not taken.
265977 assert((byte_size > 0) && (byte_size <= kMaxMsgSize));
530 265977 const uint32_t size = static_cast<uint32_t>(byte_size);
531 #ifdef __APPLE__
532 void *buffer = smalloc(size);
533 #else
534 265977 void *buffer = alloca(size);
535 #endif
536 265977 const bool retval = msg_rpc->SerializeToArray(buffer, size);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266307 times.
266307 assert(retval);
538 266307 SendData(buffer, size, frame->attachment(), frame->att_size());
539 #ifdef __APPLE__
540 free(buffer);
541 #endif
542 266373 }
543