GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-01-11 02:35:46
Exec Total Coverage
Lines: 328 372 88.2%
Branches: 168 228 73.7%

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 "crypto/hash.h"
16 #include "util/exception.h"
17 #include "util/logging.h"
18 #include "util/posix.h"
19 #include "util/smalloc.h"
20
21 // TODO(jblomer): Check for possible starvation of plugin by dying clients
22 // (blocking read). Probably only relevant for TCP sockets.
23
24 using namespace std; // NOLINT
25
26 const char
27 *CacheTransport::kEnvReadyNotifyFd = "__CVMFS_CACHE_EXTERNAL_PIPE_READY__";
28
29 /**
30 * Called on the sender side to wrap a message into a MsgRpc message for wire
31 * transfer.
32 */
33 29439 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29439 times.
29439 assert(msg_typed_ != NULL);
35
1/2
✓ Branch 0 taken 29439 times.
✗ Branch 1 not taken.
29439 if (!is_wrapped_)
36 29439 WrapMsg();
37 29439 return &msg_rpc_;
38 }
39
40
41 /**
42 * Called on the receiving end of an RPC to extract the actual message from the
43 * MsgRpc.
44 */
45 35490 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
46
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35478 times.
35490 assert(msg_rpc_.IsInitialized());
47
2/2
✓ Branch 0 taken 25613 times.
✓ Branch 1 taken 9865 times.
35478 if (msg_typed_ == NULL)
48 25613 UnwrapMsg();
49 35469 return msg_typed_;
50 }
51
52
53 35493 CacheTransport::Frame::Frame()
54 35493 : owns_msg_typed_(false)
55 35493 , msg_typed_(NULL)
56 35493 , attachment_(NULL)
57 35493 , att_size_(0)
58 35493 , is_wrapped_(false)
59 35493 , is_msg_out_of_band_(false) { }
60
61
62 29436 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
63 29436 : owns_msg_typed_(false)
64 29436 , msg_typed_(m)
65 29436 , attachment_(NULL)
66 29436 , att_size_(0)
67 29436 , is_wrapped_(false)
68 29436 , is_msg_out_of_band_(false) { }
69
70
71 64929 CacheTransport::Frame::~Frame() { Reset(0); }
72
73
74 9871 bool CacheTransport::Frame::IsMsgOutOfBand() {
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9871 times.
9871 assert(msg_rpc_.IsInitialized());
76
1/2
✓ Branch 0 taken 9871 times.
✗ Branch 1 not taken.
9871 if (msg_typed_ == NULL)
77 9871 UnwrapMsg();
78 9871 return is_msg_out_of_band_;
79 }
80
81
82 6084 void CacheTransport::Frame::MergeFrom(const Frame &other) {
83 6084 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
84 6084 owns_msg_typed_ = true;
85
2/2
✓ Branch 0 taken 5400 times.
✓ Branch 1 taken 684 times.
6084 if (other.att_size_ > 0) {
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5400 times.
5400 assert(att_size_ >= other.att_size_);
87 5400 memcpy(attachment_, other.attachment_, other.att_size_);
88 5400 att_size_ = other.att_size_;
89 }
90 6084 }
91
92
93 29370 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
94 29370 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29388 times.
29388 if (!retval)
96 return false;
97
98 // Cleanup typed message when Frame leaves scope
99 29388 owns_msg_typed_ = true;
100 29388 return true;
101 }
102
103
104 64932 void CacheTransport::Frame::Release() {
105
2/2
✓ Branch 0 taken 35493 times.
✓ Branch 1 taken 29439 times.
64932 if (owns_msg_typed_)
106 35493 return;
107
108 29439 msg_rpc_.release_msg_refcount_req();
109 29433 msg_rpc_.release_msg_refcount_reply();
110 29436 msg_rpc_.release_msg_read_req();
111 29436 msg_rpc_.release_msg_read_reply();
112 29436 msg_rpc_.release_msg_object_info_req();
113 29436 msg_rpc_.release_msg_object_info_reply();
114 29436 msg_rpc_.release_msg_store_req();
115 29436 msg_rpc_.release_msg_store_abort_req();
116 29436 msg_rpc_.release_msg_store_reply();
117 29433 msg_rpc_.release_msg_handshake();
118 29433 msg_rpc_.release_msg_handshake_ack();
119 29436 msg_rpc_.release_msg_quit();
120 29436 msg_rpc_.release_msg_ioctl();
121 29433 msg_rpc_.release_msg_info_req();
122 29433 msg_rpc_.release_msg_info_reply();
123 29430 msg_rpc_.release_msg_shrink_req();
124 29433 msg_rpc_.release_msg_shrink_reply();
125 29430 msg_rpc_.release_msg_list_req();
126 29433 msg_rpc_.release_msg_list_reply();
127 29430 msg_rpc_.release_msg_detach();
128 29427 msg_rpc_.release_msg_breadcrumb_store_req();
129 29430 msg_rpc_.release_msg_breadcrumb_load_req();
130 29430 msg_rpc_.release_msg_breadcrumb_reply();
131 }
132
133
134 64929 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
135 64929 msg_typed_ = NULL;
136 64929 att_size_ = original_att_size;
137 64929 is_wrapped_ = false;
138 64929 is_msg_out_of_band_ = false;
139 64929 Release();
140 64926 msg_rpc_.Clear();
141 64914 owns_msg_typed_ = false;
142 64914 }
143
144
145 29439 void CacheTransport::Frame::WrapMsg() {
146
2/2
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 29359 times.
29439 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
147 80 msg_rpc_.set_allocated_msg_handshake(
148 80 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
149
2/2
✓ Branch 3 taken 54 times.
✓ Branch 4 taken 29305 times.
29359 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
150 54 msg_rpc_.set_allocated_msg_handshake_ack(
151 54 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
152
2/2
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 29227 times.
29305 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
153 78 msg_rpc_.set_allocated_msg_quit(
154 78 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
155
2/2
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 29215 times.
29227 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
156 12 msg_rpc_.set_allocated_msg_ioctl(
157 12 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
158
2/2
✓ Branch 3 taken 4196 times.
✓ Branch 4 taken 25019 times.
29215 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
159 4196 msg_rpc_.set_allocated_msg_refcount_req(
160 4196 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
161
2/2
✓ Branch 3 taken 1692 times.
✓ Branch 4 taken 23327 times.
25019 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
162 1692 msg_rpc_.set_allocated_msg_refcount_reply(
163 1692 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
164
2/2
✓ Branch 3 taken 72 times.
✓ Branch 4 taken 23255 times.
23327 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
165 72 msg_rpc_.set_allocated_msg_object_info_req(
166 72 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
167
2/2
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 23207 times.
23255 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
168 48 msg_rpc_.set_allocated_msg_object_info_reply(
169 48 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
170
2/2
✓ Branch 3 taken 7340 times.
✓ Branch 4 taken 15867 times.
23207 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
171 7340 msg_rpc_.set_allocated_msg_read_req(
172 7340 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
173
2/2
✓ Branch 3 taken 6633 times.
✓ Branch 4 taken 9234 times.
15867 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
174 6633 msg_rpc_.set_allocated_msg_read_reply(
175 6633 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
176
2/2
✓ Branch 3 taken 4238 times.
✓ Branch 4 taken 4996 times.
9234 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
177 4238 msg_rpc_.set_allocated_msg_store_req(
178 4238 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
179
2/2
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 4993 times.
4996 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
180 3 msg_rpc_.set_allocated_msg_store_abort_req(
181 3 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
182
2/2
✓ Branch 3 taken 1827 times.
✓ Branch 4 taken 3166 times.
4993 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
183 1827 msg_rpc_.set_allocated_msg_store_reply(
184 1827 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
185
2/2
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 3130 times.
3166 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
186 36 msg_rpc_.set_allocated_msg_info_req(
187 36 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
188
2/2
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 3118 times.
3130 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
189 12 msg_rpc_.set_allocated_msg_info_reply(
190 12 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
191
2/2
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 3104 times.
3118 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
192 14 msg_rpc_.set_allocated_msg_shrink_req(
193 14 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
194
2/2
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 3098 times.
3104 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
195 6 msg_rpc_.set_allocated_msg_shrink_reply(
196 6 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
197
2/2
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 3060 times.
3098 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
198 38 msg_rpc_.set_allocated_msg_list_req(
199 38 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
200
2/2
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 3030 times.
3060 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
201 30 msg_rpc_.set_allocated_msg_list_reply(
202 30 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
203
2/2
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 3025 times.
3030 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
204 5 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
205 5 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
206
2/2
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 3015 times.
3025 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
207 10 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
208 10 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
209
2/2
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 3006 times.
3015 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
210 9 msg_rpc_.set_allocated_msg_breadcrumb_reply(
211 9 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
212
1/2
✓ Branch 3 taken 3006 times.
✗ Branch 4 not taken.
3006 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
213 3006 msg_rpc_.set_allocated_msg_detach(
214 3006 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
215 3006 is_msg_out_of_band_ = true;
216 } else {
217 // Unexpected message type, should never happen
218 PANIC(NULL);
219 }
220 29439 is_wrapped_ = true;
221 29439 }
222
223
224 35487 void CacheTransport::Frame::UnwrapMsg() {
225
2/2
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 35436 times.
35487 if (msg_rpc_.has_msg_handshake()) {
226 54 msg_typed_ = msg_rpc_.mutable_msg_handshake();
227
2/2
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 35353 times.
35436 } else if (msg_rpc_.has_msg_handshake_ack()) {
228 80 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
229
2/2
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 35299 times.
35353 } else if (msg_rpc_.has_msg_quit()) {
230 54 msg_typed_ = msg_rpc_.mutable_msg_quit();
231
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 35284 times.
35299 } else if (msg_rpc_.has_msg_ioctl()) {
232 12 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
233
2/2
✓ Branch 1 taken 1692 times.
✓ Branch 2 taken 33577 times.
35284 } else if (msg_rpc_.has_msg_refcount_req()) {
234 1692 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
235
2/2
✓ Branch 1 taken 4244 times.
✓ Branch 2 taken 29333 times.
33577 } else if (msg_rpc_.has_msg_refcount_reply()) {
236 4244 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
237
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 29294 times.
29333 } else if (msg_rpc_.has_msg_object_info_req()) {
238 48 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
239
2/2
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 29198 times.
29294 } else if (msg_rpc_.has_msg_object_info_reply()) {
240 99 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
241
2/2
✓ Branch 1 taken 6633 times.
✓ Branch 2 taken 22559 times.
29198 } else if (msg_rpc_.has_msg_read_req()) {
242 6633 msg_typed_ = msg_rpc_.mutable_msg_read_req();
243
2/2
✓ Branch 1 taken 12740 times.
✓ Branch 2 taken 9831 times.
22559 } else if (msg_rpc_.has_msg_read_reply()) {
244 12740 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
245
2/2
✓ Branch 1 taken 1824 times.
✓ Branch 2 taken 8007 times.
9831 } else if (msg_rpc_.has_msg_store_req()) {
246 1824 msg_typed_ = msg_rpc_.mutable_msg_store_req();
247
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 8004 times.
8007 } else if (msg_rpc_.has_msg_store_abort_req()) {
248 3 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
249
2/2
✓ Branch 1 taken 4841 times.
✓ Branch 2 taken 3163 times.
8004 } else if (msg_rpc_.has_msg_store_reply()) {
250 4841 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
251
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 3151 times.
3163 } else if (msg_rpc_.has_msg_info_req()) {
252 12 msg_typed_ = msg_rpc_.mutable_msg_info_req();
253
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 3115 times.
3151 } else if (msg_rpc_.has_msg_info_reply()) {
254 36 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
255
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3109 times.
3115 } else if (msg_rpc_.has_msg_shrink_req()) {
256 6 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
257
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 3095 times.
3109 } else if (msg_rpc_.has_msg_shrink_reply()) {
258 14 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
259
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 3065 times.
3095 } else if (msg_rpc_.has_msg_list_req()) {
260 30 msg_typed_ = msg_rpc_.mutable_msg_list_req();
261
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 3027 times.
3065 } else if (msg_rpc_.has_msg_list_reply()) {
262 38 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
263
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3024 times.
3027 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
264 3 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
265
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3018 times.
3024 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
266 6 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
267
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 3003 times.
3018 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
268 15 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
269
1/2
✓ Branch 1 taken 3003 times.
✗ Branch 2 not taken.
3003 } else if (msg_rpc_.has_msg_detach()) {
270 3003 msg_typed_ = msg_rpc_.mutable_msg_detach();
271 3003 is_msg_out_of_band_ = true;
272 } else {
273 // Unexpected message type, should never happen
274 PANIC(NULL);
275 }
276 35487 }
277
278
279 //------------------------------------------------------------------------------
280
281
282 80 CacheTransport::CacheTransport(int fd_connection)
283 80 : fd_connection_(fd_connection), flags_(0) {
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 assert(fd_connection_ >= 0);
285 80 }
286
287
288 13383 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
289 13383 : fd_connection_(fd_connection), flags_(flags) {
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13383 times.
13383 assert(fd_connection_ >= 0);
291 13383 }
292
293
294 608804 void CacheTransport::FillMsgHash(const shash::Any &hash,
295 cvmfs::MsgHash *msg_hash) {
296
3/4
✓ Branch 0 taken 608772 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
608804 switch (hash.algorithm) {
297 608772 case shash::kSha1:
298 608772 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
299 608772 break;
300 12 case shash::kRmd160:
301 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
302 12 break;
303 20 case shash::kShake128:
304 20 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
305 20 break;
306 default:
307 PANIC(NULL);
308 }
309 608804 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
310 608804 }
311
312
313 4230 void CacheTransport::FillObjectType(int object_flags,
314 cvmfs::EnumObjectType *wire_type) {
315 4230 *wire_type = cvmfs::OBJECT_REGULAR;
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4230 times.
4230 if (object_flags & CacheManager::kLabelCatalog)
317 *wire_type = cvmfs::OBJECT_CATALOG;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4230 times.
4230 if (object_flags & CacheManager::kLabelVolatile)
319 *wire_type = cvmfs::OBJECT_VOLATILE;
320 4230 }
321
322
323 10205 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
324 shash::Any *hash) {
325
2/4
✓ Branch 1 taken 10197 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
10205 switch (msg_hash.algorithm()) {
326 10197 case cvmfs::HASH_SHA1:
327 10197 hash->algorithm = shash::kSha1;
328 10197 break;
329 case cvmfs::HASH_RIPEMD160:
330 hash->algorithm = shash::kRmd160;
331 break;
332 8 case cvmfs::HASH_SHAKE128:
333 8 hash->algorithm = shash::kShake128;
334 8 break;
335 default:
336 return false;
337 }
338 10205 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
339
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 10205 times.
10205 if (msg_hash.digest().length() != digest_size)
340 return false;
341 10205 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
342 10205 return true;
343 }
344
345
346 bool CacheTransport::ParseObjectType(cvmfs::EnumObjectType wire_type,
347 int *object_flags) {
348 *object_flags = 0;
349 switch (wire_type) {
350 case cvmfs::OBJECT_REGULAR:
351 return true;
352 case cvmfs::OBJECT_CATALOG:
353 *object_flags |= CacheManager::kLabelCatalog;
354 return true;
355 case cvmfs::OBJECT_VOLATILE:
356 *object_flags |= CacheManager::kLabelVolatile;
357 return true;
358 default:
359 return false;
360 }
361 }
362
363
364 29415 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
365 uint32_t size;
366 bool has_attachment;
367
1/2
✓ Branch 1 taken 29415 times.
✗ Branch 2 not taken.
29415 bool retval = RecvHeader(&size, &has_attachment);
368
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 29412 times.
29415 if (!retval)
369 3 return false;
370
371 void *buffer;
372
2/2
✓ Branch 0 taken 20487 times.
✓ Branch 1 taken 8925 times.
29412 if (size <= kMaxStackAlloc)
373 20487 buffer = alloca(size);
374 else
375 8925 buffer = smalloc(size);
376
1/2
✓ Branch 1 taken 29409 times.
✗ Branch 2 not taken.
29412 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
377
3/4
✓ Branch 0 taken 29385 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 29385 times.
29409 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (size > kMaxStackAlloc) {
379 free(buffer);
380 }
381 24 return false;
382 }
383
384 29385 uint32_t msg_size = size;
385
2/2
✓ Branch 0 taken 9152 times.
✓ Branch 1 taken 20233 times.
29385 if (has_attachment) {
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9152 times.
9152 if (size < 2) {
387 // kMaxStackAlloc is > 2 (of course!) but we'll leave the condition here
388 // for consistency.
389 if (size > kMaxStackAlloc) {
390 free(buffer);
391 }
392 return false;
393 }
394 9152 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
395 9152 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9152 times.
9152 if ((msg_size + kInnerHeaderSize) > size) {
397 if (size > kMaxStackAlloc) {
398 free(buffer);
399 }
400 return false;
401 }
402 }
403
404 29385 void *ptr_msg = has_attachment
405
2/2
✓ Branch 0 taken 9152 times.
✓ Branch 1 taken 20233 times.
29385 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
406 : buffer;
407
1/2
✓ Branch 1 taken 29391 times.
✗ Branch 2 not taken.
29385 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29391 times.
29391 if (!retval) {
409 if (size > kMaxStackAlloc) {
410 free(buffer);
411 }
412 return false;
413 }
414
415
2/2
✓ Branch 0 taken 9152 times.
✓ Branch 1 taken 20239 times.
29391 if (has_attachment) {
416 9152 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9152 times.
9152 if (frame->att_size() < attachment_size) {
418 if (size > kMaxStackAlloc) {
419 free(buffer);
420 }
421 return false;
422 }
423 9152 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
424 9152 + msg_size;
425 9152 memcpy(frame->attachment(), ptr_attachment, attachment_size);
426 9152 frame->set_att_size(attachment_size);
427 } else {
428 20239 frame->set_att_size(0);
429 }
430
2/2
✓ Branch 0 taken 8925 times.
✓ Branch 1 taken 20481 times.
29406 if (size > kMaxStackAlloc) {
431 8925 free(buffer);
432 }
433 29406 return true;
434 }
435
436
437 29415 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
438 unsigned char header[kHeaderSize];
439
1/2
✓ Branch 1 taken 29415 times.
✗ Branch 2 not taken.
29415 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
440
3/4
✓ Branch 0 taken 29415 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 29412 times.
29415 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
441 3 return false;
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29412 times.
29412 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
443 return false;
444 29412 *has_attachment = header[0] & kFlagHasAttachment;
445 29412 *size = header[1] + (header[2] << 8) + (header[3] << 16);
446
2/4
✓ Branch 0 taken 29412 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29412 times.
✗ Branch 3 not taken.
29412 return (*size > 0) && (*size <= kMaxMsgSize);
447 }
448
449
450 29433 void CacheTransport::SendData(void *message,
451 uint32_t msg_size,
452 void *attachment,
453 uint32_t att_size) {
454 58866 const uint32_t total_size = msg_size + att_size
455
2/2
✓ Branch 0 taken 10863 times.
✓ Branch 1 taken 18570 times.
29433 + ((att_size > 0) ? kInnerHeaderSize : 0);
456
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29433 times.
29433 assert(total_size > 0);
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29433 times.
29433 assert(total_size <= kMaxMsgSize);
459
1/2
✓ Branch 1 taken 23694 times.
✗ Branch 2 not taken.
23688 LogCvmfs(kLogCache, kLogDebug,
460 "sending message of size %u to cache transport", total_size);
461
462 unsigned char header[kHeaderSize];
463
2/2
✓ Branch 0 taken 18576 times.
✓ Branch 1 taken 10863 times.
29439 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
464 29439 header[1] = (total_size & 0x000000FF);
465 29439 header[2] = (total_size & 0x0000FF00) >> 8;
466 29439 header[3] = (total_size & 0x00FF0000) >> 16;
467 // Only transferred if an attachment is present. Otherwise the overall size
468 // is also the size of the protobuf message.
469 unsigned char inner_header[kInnerHeaderSize];
470
471 struct iovec iov[4];
472 29439 iov[0].iov_base = header;
473 29439 iov[0].iov_len = kHeaderSize;
474
475
2/2
✓ Branch 0 taken 10863 times.
✓ Branch 1 taken 18576 times.
29439 if (att_size > 0) {
476 10863 inner_header[0] = (msg_size & 0x000000FF);
477 10863 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
478 10863 iov[1].iov_base = inner_header;
479 10863 iov[1].iov_len = kInnerHeaderSize;
480 10863 iov[2].iov_base = message;
481 10863 iov[2].iov_len = msg_size;
482 10863 iov[3].iov_base = attachment;
483 10863 iov[3].iov_len = att_size;
484 } else {
485 18576 iov[1].iov_base = message;
486 18576 iov[1].iov_len = msg_size;
487 }
488
2/2
✓ Branch 0 taken 3006 times.
✓ Branch 1 taken 26433 times.
29439 if (flags_ & kFlagSendNonBlocking) {
489
2/4
✓ Branch 0 taken 3006 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3006 times.
✗ Branch 4 not taken.
3006 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
490 3006 return;
491 }
492
3/4
✓ Branch 0 taken 15570 times.
✓ Branch 1 taken 10863 times.
✓ Branch 3 taken 26433 times.
✗ Branch 4 not taken.
26433 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
493
494
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26433 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
26433 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
495 PANIC(kLogSyslogErr | kLogDebug,
496 "failed to write to external cache transport (%d), aborting", errno);
497 }
498 }
499
500 3006 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3006 times.
3006 assert(iovcnt > 0);
502 3006 unsigned total_size = 0;
503
2/2
✓ Branch 0 taken 6012 times.
✓ Branch 1 taken 3006 times.
9018 for (unsigned i = 0; i < iovcnt; ++i)
504 6012 total_size += iov[i].iov_len;
505 3006 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
506
507 3006 unsigned pos = 0;
508
2/2
✓ Branch 0 taken 6012 times.
✓ Branch 1 taken 3006 times.
9018 for (unsigned i = 0; i < iovcnt; ++i) {
509 6012 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
510 6012 pos += iov[i].iov_len;
511 }
512
513 3006 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3006 times.
3006 if (retval < 0) {
515 assert(errno != EMSGSIZE);
516 if (!(flags_ & kFlagSendIgnoreFailure)) {
517 PANIC(kLogSyslogErr | kLogDebug,
518 "failed to write to external cache transport (%d), aborting",
519 errno);
520 }
521 }
522 3006 }
523
524
525 29439 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
526 29439 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
527 29439 const int32_t size = msg_rpc->ByteSize();
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29439 times.
29439 assert(size > 0);
529 #ifdef __APPLE__
530 void *buffer = smalloc(size);
531 #else
532 29439 void *buffer = alloca(size);
533 #endif
534 29439 const bool retval = msg_rpc->SerializeToArray(buffer, size);
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29436 times.
29436 assert(retval);
536 29436 SendData(buffer, size, frame->attachment(), frame->att_size());
537 #ifdef __APPLE__
538 free(buffer);
539 #endif
540 29436 }
541