GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-05-17 02:35:27
Exec Total Coverage
Lines: 326 372 87.6%
Branches: 166 228 72.8%

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