GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-05-03 02:36:16
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 84719 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84719 times.
84719 assert(msg_typed_ != NULL);
36
1/2
✓ Branch 0 taken 84719 times.
✗ Branch 1 not taken.
84719 if (!is_wrapped_)
37 84719 WrapMsg();
38 84719 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 104945 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 104895 times.
104945 assert(msg_rpc_.IsInitialized());
48
2/2
✓ Branch 0 taken 85296 times.
✓ Branch 1 taken 19599 times.
104895 if (msg_typed_ == NULL)
49 85296 UnwrapMsg();
50 104925 return msg_typed_;
51 }
52
53
54 104965 CacheTransport::Frame::Frame()
55 104965 : owns_msg_typed_(false)
56 104965 , msg_typed_(NULL)
57 104965 , attachment_(NULL)
58 104965 , att_size_(0)
59 104965 , is_wrapped_(false)
60 104965 , is_msg_out_of_band_(false) { }
61
62
63 84719 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
64 84719 : owns_msg_typed_(false)
65 84719 , msg_typed_(m)
66 84719 , attachment_(NULL)
67 84719 , att_size_(0)
68 84719 , is_wrapped_(false)
69 84719 , is_msg_out_of_band_(false) { }
70
71
72 189674 CacheTransport::Frame::~Frame() { Reset(0); }
73
74
75 19609 bool CacheTransport::Frame::IsMsgOutOfBand() {
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19609 times.
19609 assert(msg_rpc_.IsInitialized());
77
1/2
✓ Branch 0 taken 19609 times.
✗ Branch 1 not taken.
19609 if (msg_typed_ == NULL)
78 19609 UnwrapMsg();
79 19609 return is_msg_out_of_band_;
80 }
81
82
83 20280 void CacheTransport::Frame::MergeFrom(const Frame &other) {
84 20280 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
85 20280 owns_msg_typed_ = true;
86
2/2
✓ Branch 0 taken 18000 times.
✓ Branch 1 taken 2280 times.
20280 if (other.att_size_ > 0) {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18000 times.
18000 assert(att_size_ >= other.att_size_);
88 18000 memcpy(attachment_, other.attachment_, other.att_size_);
89 18000 att_size_ = other.att_size_;
90 }
91 20280 }
92
93
94 84655 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
95 84655 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84675 times.
84675 if (!retval)
97 return false;
98
99 // Cleanup typed message when Frame leaves scope
100 84675 owns_msg_typed_ = true;
101 84675 return true;
102 }
103
104
105 189684 void CacheTransport::Frame::Release() {
106
2/2
✓ Branch 0 taken 104965 times.
✓ Branch 1 taken 84719 times.
189684 if (owns_msg_typed_)
107 104965 return;
108
109 84719 msg_rpc_.release_msg_refcount_req();
110 84729 msg_rpc_.release_msg_refcount_reply();
111 84729 msg_rpc_.release_msg_read_req();
112 84719 msg_rpc_.release_msg_read_reply();
113 84719 msg_rpc_.release_msg_object_info_req();
114 84719 msg_rpc_.release_msg_object_info_reply();
115 84729 msg_rpc_.release_msg_store_req();
116 84719 msg_rpc_.release_msg_store_abort_req();
117 84719 msg_rpc_.release_msg_store_reply();
118 84729 msg_rpc_.release_msg_handshake();
119 84719 msg_rpc_.release_msg_handshake_ack();
120 84709 msg_rpc_.release_msg_quit();
121 84719 msg_rpc_.release_msg_ioctl();
122 84719 msg_rpc_.release_msg_info_req();
123 84719 msg_rpc_.release_msg_info_reply();
124 84719 msg_rpc_.release_msg_shrink_req();
125 84709 msg_rpc_.release_msg_shrink_reply();
126 84719 msg_rpc_.release_msg_list_req();
127 84709 msg_rpc_.release_msg_list_reply();
128 84709 msg_rpc_.release_msg_detach();
129 84709 msg_rpc_.release_msg_breadcrumb_store_req();
130 84709 msg_rpc_.release_msg_breadcrumb_load_req();
131 84709 msg_rpc_.release_msg_breadcrumb_reply();
132 }
133
134
135 189684 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
136 189684 msg_typed_ = NULL;
137 189684 att_size_ = original_att_size;
138 189684 is_wrapped_ = false;
139 189684 is_msg_out_of_band_ = false;
140 189684 Release();
141 189674 msg_rpc_.Clear();
142 189664 owns_msg_typed_ = false;
143 189664 }
144
145
146 84719 void CacheTransport::Frame::WrapMsg() {
147
2/2
✓ Branch 3 taken 206 times.
✓ Branch 4 taken 84513 times.
84719 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
148 206 msg_rpc_.set_allocated_msg_handshake(
149 206 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
150
2/2
✓ Branch 3 taken 180 times.
✓ Branch 4 taken 84333 times.
84513 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
151 180 msg_rpc_.set_allocated_msg_handshake_ack(
152 180 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
153
2/2
✓ Branch 3 taken 204 times.
✓ Branch 4 taken 84129 times.
84333 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
154 204 msg_rpc_.set_allocated_msg_quit(
155 204 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
156
2/2
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 84089 times.
84129 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
157 40 msg_rpc_.set_allocated_msg_ioctl(
158 40 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
159
2/2
✓ Branch 3 taken 8144 times.
✓ Branch 4 taken 75945 times.
84089 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
160 8144 msg_rpc_.set_allocated_msg_refcount_req(
161 8144 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
162
2/2
✓ Branch 3 taken 5640 times.
✓ Branch 4 taken 70305 times.
75945 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
163 5640 msg_rpc_.set_allocated_msg_refcount_reply(
164 5640 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
165
2/2
✓ Branch 3 taken 184 times.
✓ Branch 4 taken 70121 times.
70305 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
166 184 msg_rpc_.set_allocated_msg_object_info_req(
167 184 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
168
2/2
✓ Branch 3 taken 160 times.
✓ Branch 4 taken 69961 times.
70121 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
169 160 msg_rpc_.set_allocated_msg_object_info_reply(
170 160 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
171
2/2
✓ Branch 3 taken 22811 times.
✓ Branch 4 taken 47150 times.
69961 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
172 22811 msg_rpc_.set_allocated_msg_read_req(
173 22811 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
174
2/2
✓ Branch 3 taken 22110 times.
✓ Branch 4 taken 25040 times.
47150 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
175 22110 msg_rpc_.set_allocated_msg_read_reply(
176 22110 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
177
2/2
✓ Branch 3 taken 8494 times.
✓ Branch 4 taken 16546 times.
25040 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
178 8494 msg_rpc_.set_allocated_msg_store_req(
179 8494 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
180
2/2
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 16536 times.
16546 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
181 10 msg_rpc_.set_allocated_msg_store_abort_req(
182 10 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
183
2/2
✓ Branch 3 taken 6090 times.
✓ Branch 4 taken 10446 times.
16536 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
184 6090 msg_rpc_.set_allocated_msg_store_reply(
185 6090 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
186
2/2
✓ Branch 3 taken 64 times.
✓ Branch 4 taken 10382 times.
10446 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
187 64 msg_rpc_.set_allocated_msg_info_req(
188 64 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
189
2/2
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 10342 times.
10382 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
190 40 msg_rpc_.set_allocated_msg_info_reply(
191 40 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
192
2/2
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 10314 times.
10342 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
193 28 msg_rpc_.set_allocated_msg_shrink_req(
194 28 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
195
2/2
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 10294 times.
10314 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
196 20 msg_rpc_.set_allocated_msg_shrink_reply(
197 20 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
198
2/2
✓ Branch 3 taken 108 times.
✓ Branch 4 taken 10186 times.
10294 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
199 108 msg_rpc_.set_allocated_msg_list_req(
200 108 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
201
2/2
✓ Branch 3 taken 100 times.
✓ Branch 4 taken 10086 times.
10186 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
202 100 msg_rpc_.set_allocated_msg_list_reply(
203 100 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
204
2/2
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 10074 times.
10086 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
205 12 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
206 12 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
207
2/2
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 10050 times.
10074 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
208 24 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
209 24 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
210
2/2
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 10020 times.
10050 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
211 30 msg_rpc_.set_allocated_msg_breadcrumb_reply(
212 30 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
213
1/2
✓ Branch 3 taken 10020 times.
✗ Branch 4 not taken.
10020 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
214 10020 msg_rpc_.set_allocated_msg_detach(
215 10020 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
216 10020 is_msg_out_of_band_ = true;
217 } else {
218 // Unexpected message type, should never happen
219 PANIC(NULL);
220 }
221 84719 is_wrapped_ = true;
222 84719 }
223
224
225 104905 void CacheTransport::Frame::UnwrapMsg() {
226
2/2
✓ Branch 1 taken 180 times.
✓ Branch 2 taken 104715 times.
104905 if (msg_rpc_.has_msg_handshake()) {
227 180 msg_typed_ = msg_rpc_.mutable_msg_handshake();
228
2/2
✓ Branch 1 taken 206 times.
✓ Branch 2 taken 104489 times.
104715 } else if (msg_rpc_.has_msg_handshake_ack()) {
229 206 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
230
2/2
✓ Branch 1 taken 180 times.
✓ Branch 2 taken 104329 times.
104489 } else if (msg_rpc_.has_msg_quit()) {
231 180 msg_typed_ = msg_rpc_.mutable_msg_quit();
232
2/2
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 104299 times.
104329 } else if (msg_rpc_.has_msg_ioctl()) {
233 40 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
234
2/2
✓ Branch 1 taken 5640 times.
✓ Branch 2 taken 98649 times.
104299 } else if (msg_rpc_.has_msg_refcount_req()) {
235 5640 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
236
2/2
✓ Branch 1 taken 8334 times.
✓ Branch 2 taken 90335 times.
98649 } else if (msg_rpc_.has_msg_refcount_reply()) {
237 8334 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
238
2/2
✓ Branch 1 taken 160 times.
✓ Branch 2 taken 90195 times.
90335 } else if (msg_rpc_.has_msg_object_info_req()) {
239 160 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
240
2/2
✓ Branch 1 taken 274 times.
✓ Branch 2 taken 89921 times.
90195 } else if (msg_rpc_.has_msg_object_info_reply()) {
241 274 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
242
2/2
✓ Branch 1 taken 22110 times.
✓ Branch 2 taken 67811 times.
89921 } else if (msg_rpc_.has_msg_read_req()) {
243 22110 msg_typed_ = msg_rpc_.mutable_msg_read_req();
244
2/2
✓ Branch 1 taken 40811 times.
✓ Branch 2 taken 27030 times.
67811 } else if (msg_rpc_.has_msg_read_reply()) {
245 40811 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
246
2/2
✓ Branch 1 taken 6080 times.
✓ Branch 2 taken 20950 times.
27030 } else if (msg_rpc_.has_msg_store_req()) {
247 6080 msg_typed_ = msg_rpc_.mutable_msg_store_req();
248
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 20940 times.
20950 } else if (msg_rpc_.has_msg_store_abort_req()) {
249 10 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
250
2/2
✓ Branch 1 taken 10504 times.
✓ Branch 2 taken 10436 times.
20940 } else if (msg_rpc_.has_msg_store_reply()) {
251 10504 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
252
2/2
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 10396 times.
10436 } else if (msg_rpc_.has_msg_info_req()) {
253 40 msg_typed_ = msg_rpc_.mutable_msg_info_req();
254
2/2
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 10332 times.
10396 } else if (msg_rpc_.has_msg_info_reply()) {
255 64 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
256
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 10312 times.
10332 } else if (msg_rpc_.has_msg_shrink_req()) {
257 20 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
258
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 10284 times.
10312 } else if (msg_rpc_.has_msg_shrink_reply()) {
259 28 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
260
2/2
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 10184 times.
10284 } else if (msg_rpc_.has_msg_list_req()) {
261 100 msg_typed_ = msg_rpc_.mutable_msg_list_req();
262
2/2
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 10076 times.
10184 } else if (msg_rpc_.has_msg_list_reply()) {
263 108 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
264
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10066 times.
10076 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
265 10 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
266
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 10046 times.
10066 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
267 20 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
268
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 10010 times.
10046 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
269 36 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
270
1/2
✓ Branch 1 taken 10010 times.
✗ Branch 2 not taken.
10010 } else if (msg_rpc_.has_msg_detach()) {
271 10010 msg_typed_ = msg_rpc_.mutable_msg_detach();
272 10010 is_msg_out_of_band_ = true;
273 } else {
274 // Unexpected message type, should never happen
275 PANIC(NULL);
276 }
277 104965 }
278
279
280 //------------------------------------------------------------------------------
281
282
283 206 CacheTransport::CacheTransport(int fd_connection)
284 206 : fd_connection_(fd_connection), flags_(0) {
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 assert(fd_connection_ >= 0);
286 206 }
287
288
289 44610 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
290 44610 : fd_connection_(fd_connection), flags_(flags) {
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44610 times.
44610 assert(fd_connection_ >= 0);
292 44610 }
293
294
295 2017255 void CacheTransport::FillMsgHash(const shash::Any &hash,
296 cvmfs::MsgHash *msg_hash) {
297
3/4
✓ Branch 0 taken 2017209 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
2017255 switch (hash.algorithm) {
298 2017209 case shash::kSha1:
299 2017209 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
300 2017209 break;
301 12 case shash::kRmd160:
302 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
303 12 break;
304 34 case shash::kShake128:
305 34 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
306 34 break;
307 default:
308 PANIC(NULL);
309 }
310 2017255 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
311 2017255 }
312
313
314 8486 void CacheTransport::FillObjectType(int object_flags,
315 cvmfs::EnumObjectType *wire_type) {
316 8486 *wire_type = cvmfs::OBJECT_REGULAR;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8486 times.
8486 if (object_flags & CacheManager::kLabelCatalog)
318 *wire_type = cvmfs::OBJECT_CATALOG;
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8486 times.
8486 if (object_flags & CacheManager::kLabelVolatile)
320 *wire_type = cvmfs::OBJECT_VOLATILE;
321 8486 }
322
323
324 34012 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
325 shash::Any *hash) {
326
2/4
✓ Branch 1 taken 33990 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
✗ Branch 4 not taken.
34012 switch (msg_hash.algorithm()) {
327 33990 case cvmfs::HASH_SHA1:
328 33990 hash->algorithm = shash::kSha1;
329 33990 break;
330 case cvmfs::HASH_RIPEMD160:
331 hash->algorithm = shash::kRmd160;
332 break;
333 22 case cvmfs::HASH_SHAKE128:
334 22 hash->algorithm = shash::kShake128;
335 22 break;
336 default:
337 return false;
338 }
339 34012 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
340
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 34012 times.
34012 if (msg_hash.digest().length() != digest_size)
341 return false;
342 34012 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
343 34012 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 84695 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
366 uint32_t size;
367 bool has_attachment;
368
1/2
✓ Branch 1 taken 84695 times.
✗ Branch 2 not taken.
84695 bool retval = RecvHeader(&size, &has_attachment);
369
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 84685 times.
84695 if (!retval)
370 10 return false;
371
372 void *buffer;
373
2/2
✓ Branch 0 taken 56049 times.
✓ Branch 1 taken 28636 times.
84685 if (size <= kMaxStackAlloc)
374 56049 buffer = alloca(size);
375 else
376 28636 buffer = smalloc(size);
377
1/2
✓ Branch 1 taken 84685 times.
✗ Branch 2 not taken.
84685 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
378
2/4
✓ Branch 0 taken 84685 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 84685 times.
84685 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
379 if (size > kMaxStackAlloc) {
380 free(buffer);
381 }
382 return false;
383 }
384
385 84685 uint32_t msg_size = size;
386
2/2
✓ Branch 0 taken 28865 times.
✓ Branch 1 taken 55820 times.
84685 if (has_attachment) {
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28865 times.
28865 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 28865 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
396 28865 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28865 times.
28865 if ((msg_size + kInnerHeaderSize) > size) {
398 if (size > kMaxStackAlloc) {
399 free(buffer);
400 }
401 return false;
402 }
403 }
404
405 84685 void *ptr_msg = has_attachment
406
2/2
✓ Branch 0 taken 28865 times.
✓ Branch 1 taken 55820 times.
84685 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
407 : buffer;
408
1/2
✓ Branch 1 taken 84675 times.
✗ Branch 2 not taken.
84685 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84675 times.
84675 if (!retval) {
410 if (size > kMaxStackAlloc) {
411 free(buffer);
412 }
413 return false;
414 }
415
416
2/2
✓ Branch 0 taken 28865 times.
✓ Branch 1 taken 55810 times.
84675 if (has_attachment) {
417 28865 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28865 times.
28865 if (frame->att_size() < attachment_size) {
419 if (size > kMaxStackAlloc) {
420 free(buffer);
421 }
422 return false;
423 }
424 28865 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
425 28865 + msg_size;
426 28865 memcpy(frame->attachment(), ptr_attachment, attachment_size);
427 28865 frame->set_att_size(attachment_size);
428 } else {
429 55810 frame->set_att_size(0);
430 }
431
2/2
✓ Branch 0 taken 28636 times.
✓ Branch 1 taken 56049 times.
84685 if (size > kMaxStackAlloc) {
432 28636 free(buffer);
433 }
434 84685 return true;
435 }
436
437
438 84695 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
439 unsigned char header[kHeaderSize];
440
1/2
✓ Branch 1 taken 84695 times.
✗ Branch 2 not taken.
84695 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
441
3/4
✓ Branch 0 taken 84695 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 84685 times.
84695 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
442 10 return false;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84685 times.
84685 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
444 return false;
445 84685 *has_attachment = header[0] & kFlagHasAttachment;
446 84685 *size = header[1] + (header[2] << 8) + (header[3] << 16);
447
2/4
✓ Branch 0 taken 84685 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84685 times.
✗ Branch 3 not taken.
84685 return (*size > 0) && (*size <= kMaxMsgSize);
448 }
449
450
451 84719 void CacheTransport::SendData(void *message,
452 uint32_t msg_size,
453 void *attachment,
454 uint32_t att_size) {
455 169438 const uint32_t total_size = msg_size + att_size
456
2/2
✓ Branch 0 taken 30582 times.
✓ Branch 1 taken 54137 times.
84719 + ((att_size > 0) ? kInnerHeaderSize : 0);
457
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84719 times.
84719 assert(total_size > 0);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84719 times.
84719 assert(total_size <= kMaxMsgSize);
460
1/2
✓ Branch 1 taken 78980 times.
✗ Branch 2 not taken.
78980 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 54137 times.
✓ Branch 1 taken 30582 times.
84719 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
465 84719 header[1] = (total_size & 0x000000FF);
466 84719 header[2] = (total_size & 0x0000FF00) >> 8;
467 84719 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 84719 iov[0].iov_base = header;
474 84719 iov[0].iov_len = kHeaderSize;
475
476
2/2
✓ Branch 0 taken 30582 times.
✓ Branch 1 taken 54137 times.
84719 if (att_size > 0) {
477 30582 inner_header[0] = (msg_size & 0x000000FF);
478 30582 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
479 30582 iov[1].iov_base = inner_header;
480 30582 iov[1].iov_len = kInnerHeaderSize;
481 30582 iov[2].iov_base = message;
482 30582 iov[2].iov_len = msg_size;
483 30582 iov[3].iov_base = attachment;
484 30582 iov[3].iov_len = att_size;
485 } else {
486 54137 iov[1].iov_base = message;
487 54137 iov[1].iov_len = msg_size;
488 }
489
2/2
✓ Branch 0 taken 10020 times.
✓ Branch 1 taken 74699 times.
84719 if (flags_ & kFlagSendNonBlocking) {
490
2/4
✓ Branch 0 taken 10020 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 10020 times.
✗ Branch 4 not taken.
10020 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
491 10020 return;
492 }
493
3/4
✓ Branch 0 taken 44117 times.
✓ Branch 1 taken 30582 times.
✓ Branch 3 taken 74699 times.
✗ Branch 4 not taken.
74699 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
494
495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 74699 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
74699 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
496 PANIC(kLogSyslogErr | kLogDebug,
497 "failed to write to external cache transport (%d), aborting", errno);
498 }
499 }
500
501 10020 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10020 times.
10020 assert(iovcnt > 0);
503 10020 unsigned total_size = 0;
504
2/2
✓ Branch 0 taken 20040 times.
✓ Branch 1 taken 10020 times.
30060 for (unsigned i = 0; i < iovcnt; ++i)
505 20040 total_size += iov[i].iov_len;
506 10020 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
507
508 10020 unsigned pos = 0;
509
2/2
✓ Branch 0 taken 20040 times.
✓ Branch 1 taken 10020 times.
30060 for (unsigned i = 0; i < iovcnt; ++i) {
510 20040 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
511 20040 pos += iov[i].iov_len;
512 }
513
514 10020 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10020 times.
10020 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 10020 }
524
525
526 84719 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
527 84719 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
528 84719 const int32_t size = msg_rpc->ByteSize();
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84699 times.
84699 assert(size > 0);
530 #ifdef __APPLE__
531 void *buffer = smalloc(size);
532 #else
533 84699 void *buffer = alloca(size);
534 #endif
535 84699 const bool retval = msg_rpc->SerializeToArray(buffer, size);
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84719 times.
84719 assert(retval);
537 84719 SendData(buffer, size, frame->attachment(), frame->att_size());
538 #ifdef __APPLE__
539 free(buffer);
540 #endif
541 84699 }
542