GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/posix.cc
Date: 2026-02-01 02:35:56
Exec Total Coverage
Lines: 899 1154 77.9%
Branches: 671 1265 53.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Some common functions.
5 */
6
7 #ifndef __STDC_FORMAT_MACROS
8 // NOLINTNEXTLINE
9 #define __STDC_FORMAT_MACROS
10 #endif
11
12 #include <cctype>
13 #include <stdlib.h>
14 #include <cstdlib>
15 #include <dirent.h>
16 #include <sys/file.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <sched.h>
20 #include <sys/select.h>
21 #include <sys/uio.h>
22
23 #include "posix.h"
24
25 #include <arpa/inet.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <grp.h>
29 #include <inttypes.h>
30 #include <netinet/in.h>
31 #include <pthread.h>
32 #include <pwd.h>
33 #include <signal.h>
34 #include <stdint.h>
35 #include <sys/resource.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #ifdef __APPLE__
39 #include <sys/mount.h> // for statfs()
40 #else
41 #include <sys/statfs.h>
42 #endif
43 #include <sys/time.h>
44 #include <sys/types.h>
45 #include <sys/un.h>
46 #include <sys/utsname.h>
47 #include <sys/wait.h>
48 #include <unistd.h>
49 // If valgrind headers are present on the build system, then we can detect
50 // valgrind at runtime.
51 #ifdef HAS_VALGRIND_HEADERS
52 #include <valgrind/valgrind.h>
53 #endif
54
55 #include <algorithm>
56 #include <cassert>
57 #include <cstdio>
58 #include <cstring>
59 #include <map>
60 #include <set>
61 #include <string>
62 #include <vector>
63
64 #include "util/algorithm.h"
65 #include "util/exception.h"
66 #include "util/fs_traversal.h"
67 #include "util/logging.h"
68 #include "util/pipe.h"
69 #include "util/string.h"
70 #include "util/smalloc.h"
71 #include "util/smalloc.h"
72 #include "util/smalloc.h"
73 #include "util/mutex.h"
74 #include "util/mutex.h"
75 #include "util/mutex.h"
76
77 // using namespace std; // NOLINT
78
79 #ifndef ST_RDONLY
80 // On Linux, this is in sys/statvfs.h
81 // On macOS, this flag is called MNT_RDONLY /usr/include/sys/mount.h
82 #define ST_RDONLY 1
83 #endif
84
85 // Older Linux glibc versions do not provide the f_flags member in struct statfs
86 #define CVMFS_HAS_STATFS_F_FLAGS
87 #ifndef __APPLE__
88 #ifdef __GLIBC_MINOR__
89 #if __GLIBC_MINOR__ < 12
90 #undef CVMFS_HAS_STATFS_F_FLAGS
91 #endif
92 #endif
93 #endif
94
95 // Work around missing clearenv()
96 #ifdef __APPLE__
97 extern "C" {
98 extern char **environ;
99 }
100 #endif
101
102 #ifdef CVMFS_NAMESPACE_GUARD
103 namespace CVMFS_NAMESPACE_GUARD {
104 #endif
105
106 static pthread_mutex_t getumask_mutex = PTHREAD_MUTEX_INITIALIZER;
107
108
109 /**
110 * Removes a trailing "/" from a path.
111 */
112 9745 std::string MakeCanonicalPath(const std::string &path) {
113
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 9734 times.
9745 if (path.length() == 0)
114 11 return path;
115
116
2/2
✓ Branch 2 taken 132 times.
✓ Branch 3 taken 9602 times.
9734 if (path[path.length() - 1] == '/') {
117 132 return path.substr(0, path.length() - 1);
118 } else {
119 9602 return path;
120 }
121 }
122
123 /**
124 * Return both the file and directory name for a given path.
125 *
126 * NOTE: If only a filename is given, the directory is returned as "."
127 */
128 698 void SplitPath(const std::string &path,
129 std::string *dirname,
130 std::string *filename) {
131 698 const size_t dir_sep = path.rfind('/');
132
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 122 times.
698 if (dir_sep != std::string::npos) {
133 576 *dirname = path.substr(0, dir_sep);
134 576 *filename = path.substr(dir_sep + 1);
135 } else {
136 122 *dirname = ".";
137 122 *filename = path;
138 }
139 698 }
140
141
142 /**
143 * Gets the directory part of a path.
144 */
145 10909 std::string GetParentPath(const std::string &path) {
146 10909 const std::string::size_type idx = path.find_last_of('/');
147
2/2
✓ Branch 0 taken 10860 times.
✓ Branch 1 taken 49 times.
10909 if (idx != std::string::npos) {
148 10860 return path.substr(0, idx);
149 } else {
150
1/2
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
49 return "";
151 }
152 }
153
154
155 /**
156 * Gets the file name part of a path.
157 */
158 329494 std::string GetFileName(const std::string &path) {
159 329494 const std::string::size_type idx = path.find_last_of('/');
160
2/2
✓ Branch 0 taken 329033 times.
✓ Branch 1 taken 461 times.
329494 if (idx != std::string::npos) {
161 329033 return path.substr(idx + 1);
162 } else {
163 461 return path;
164 }
165 }
166
167
168 352 bool IsAbsolutePath(const std::string &path) {
169
4/4
✓ Branch 1 taken 335 times.
✓ Branch 2 taken 17 times.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 302 times.
352 return (!path.empty() && path[0] == '/');
170 }
171
172
173 301 std::string GetAbsolutePath(const std::string &path) {
174
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 285 times.
301 if (IsAbsolutePath(path))
175 16 return path;
176
177
2/4
✓ Branch 2 taken 285 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 285 times.
✗ Branch 6 not taken.
570 return GetCurrentWorkingDirectory() + "/" + path;
178 }
179
180
181 2617 bool IsHttpUrl(const std::string &path) {
182
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 2593 times.
2617 if (path.length() < 7) {
183 24 return false;
184 }
185
186
1/2
✓ Branch 1 taken 2593 times.
✗ Branch 2 not taken.
2593 std::string prefix = path.substr(0, 8);
187 2593 std::transform(prefix.begin(), prefix.end(), prefix.begin(), ::tolower);
188
189
6/12
✓ Branch 1 taken 2593 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2533 times.
✓ Branch 5 taken 60 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 60 times.
✓ Branch 8 taken 2473 times.
✓ Branch 9 taken 2593 times.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
2593 return prefix.substr(0, 7) == "http://" || prefix == "https://";
190 2593 }
191
192
193 5148 FileSystemInfo GetFileSystemInfo(const std::string &path) {
194 5148 FileSystemInfo result;
195
196 struct statfs info;
197 5148 const int retval = statfs(path.c_str(), &info);
198
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 5019 times.
5148 if (retval != 0)
199 129 return result;
200
201
3/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 88 times.
✓ Branch 5 taken 4914 times.
5019 switch (info.f_type) {
202 case kFsTypeAutofs:
203 result.type = kFsTypeAutofs;
204 break;
205 case kFsTypeNFS:
206 result.type = kFsTypeNFS;
207 break;
208 17 case kFsTypeProc:
209 17 result.type = kFsTypeProc;
210 17 break;
211 case kFsTypeBeeGFS:
212 result.type = kFsTypeBeeGFS;
213 break;
214 88 case kFsTypeTmpfs:
215 88 result.type = kFsTypeTmpfs;
216 88 break;
217 4914 default:
218 4914 result.type = kFsTypeUnknown;
219 }
220
221 #ifdef CVMFS_HAS_STATFS_F_FLAGS
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5019 times.
5019 if (info.f_flags & ST_RDONLY)
223 result.is_rdonly = true;
224 #else
225 // On old Linux systems, fall back to access()
226 retval = access(path.c_str(), W_OK);
227 result.is_rdonly = (retval != 0);
228 #endif
229
230
231 5019 return result;
232 }
233
234
235 503 std::string ReadSymlink(const std::string &path) {
236 // TODO(jblomer): avoid PATH_MAX
237 char buf[PATH_MAX + 1];
238 503 const ssize_t nchars = readlink(path.c_str(), buf, PATH_MAX);
239
2/2
✓ Branch 0 taken 472 times.
✓ Branch 1 taken 31 times.
503 if (nchars >= 0) {
240 472 buf[nchars] = '\0';
241
1/2
✓ Branch 2 taken 472 times.
✗ Branch 3 not taken.
472 return std::string(buf);
242 }
243
1/2
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
31 return "";
244 }
245
246
247 /**
248 * Follow all symlinks if possible. Equivalent to
249 * `readlink --canonicalize-missing`
250 */
251 498 std::string ResolvePath(const std::string &path) {
252
6/6
✓ Branch 1 taken 372 times.
✓ Branch 2 taken 126 times.
✓ Branch 4 taken 39 times.
✓ Branch 5 taken 333 times.
✓ Branch 6 taken 165 times.
✓ Branch 7 taken 333 times.
498 if (path.empty() || (path == "/"))
253
1/2
✓ Branch 2 taken 165 times.
✗ Branch 3 not taken.
165 return "/";
254
1/2
✓ Branch 1 taken 333 times.
✗ Branch 2 not taken.
333 const std::string name = GetFileName(path);
255
1/2
✓ Branch 1 taken 333 times.
✗ Branch 2 not taken.
333 std::string result = name;
256
2/2
✓ Branch 1 taken 285 times.
✓ Branch 2 taken 48 times.
333 if (name != path) {
257 // There is a parent path of 'path'
258
2/4
✓ Branch 1 taken 285 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 285 times.
✗ Branch 5 not taken.
285 const std::string parent = ResolvePath(GetParentPath(path));
259
4/6
✓ Branch 1 taken 183 times.
✓ Branch 2 taken 102 times.
✓ Branch 4 taken 285 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 285 times.
✗ Branch 8 not taken.
285 result = parent + (parent == "/" ? "" : "/") + name;
260 285 }
261 333 char *real_result = realpath(result.c_str(), NULL);
262
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 141 times.
333 if (real_result) {
263
1/2
✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
192 result = real_result;
264 192 free(real_result);
265 }
266
3/4
✓ Branch 1 taken 333 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 309 times.
333 if (SymlinkExists(result)) {
267 char buf[PATH_MAX + 1];
268 24 const ssize_t nchars = readlink(result.c_str(), buf, PATH_MAX);
269
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (nchars >= 0) {
270 24 buf[nchars] = '\0';
271
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 result = buf;
272 }
273 }
274 333 return result;
275 333 }
276
277
278 45 bool IsMountPoint(const std::string &path) {
279
1/2
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
45 std::vector<std::string> mount_list = platform_mountlist();
280
1/2
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
45 const std::string resolved_path = ResolvePath(path);
281
2/2
✓ Branch 1 taken 3690 times.
✓ Branch 2 taken 15 times.
3705 for (unsigned i = 0; i < mount_list.size(); ++i) {
282
2/2
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 3660 times.
3690 if (mount_list[i] == resolved_path)
283 30 return true;
284 }
285 15 return false;
286 45 }
287
288
289 /**
290 * By default PANIC(NULL) on failure
291 */
292 1425 void CreateFile(const std::string &path,
293 const int mode,
294 const bool ignore_failure) {
295 1425 const int fd = open(path.c_str(), O_CREAT, mode);
296
2/2
✓ Branch 0 taken 1402 times.
✓ Branch 1 taken 23 times.
1425 if (fd >= 0) {
297 1402 close(fd);
298 1402 return;
299 }
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ignore_failure)
301 return;
302 23 PANIC(NULL);
303 }
304
305
306 /**
307 * Symlinks /tmp/cvmfs.XYZ/l --> ParentPath(path) to make it shorter
308 */
309 89 static std::string MakeShortSocketLink(const std::string &path) {
310 struct sockaddr_un sock_addr;
311 89 const unsigned max_length = sizeof(sock_addr.sun_path);
312
313 89 std::string result;
314
2/4
✓ Branch 2 taken 89 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 89 times.
✗ Branch 6 not taken.
178 const std::string tmp_path = CreateTempDir("/tmp/cvmfs");
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 89 times.
89 if (tmp_path.empty())
316 return "";
317
1/2
✓ Branch 1 taken 89 times.
✗ Branch 2 not taken.
89 const std::string link = tmp_path + "/l";
318
3/6
✓ Branch 1 taken 89 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 89 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 89 times.
✗ Branch 8 not taken.
89 result = link + "/" + GetFileName(path);
319
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 54 times.
89 if (result.length() >= max_length) {
320 35 rmdir(tmp_path.c_str());
321
1/2
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
35 return "";
322 }
323
1/2
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
54 const int retval = symlink(GetParentPath(path).c_str(), link.c_str());
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (retval != 0) {
325 rmdir(tmp_path.c_str());
326 return "";
327 }
328 54 return result;
329 89 }
330
331 54 static void RemoveShortSocketLink(const std::string &short_path) {
332
1/2
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
54 const std::string link = GetParentPath(short_path);
333 54 unlink(link.c_str());
334
1/2
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
54 rmdir(GetParentPath(link).c_str());
335 54 }
336
337
338 /**
339 * Creates and binds to a named socket.
340 */
341 1586 int MakeSocket(const std::string &path, const int mode) {
342
1/2
✓ Branch 1 taken 1586 times.
✗ Branch 2 not taken.
1586 std::string short_path(path);
343 struct sockaddr_un sock_addr;
344
2/2
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 1535 times.
1586 if (path.length() >= sizeof(sock_addr.sun_path)) {
345 // Socket paths are limited to 108 bytes (on some systems to 92 bytes),
346 // try working around
347
1/2
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
51 short_path = MakeShortSocketLink(path);
348
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 35 times.
51 if (short_path.empty())
349 16 return -1;
350 }
351 1570 sock_addr.sun_family = AF_UNIX;
352 1570 strncpy(sock_addr.sun_path, short_path.c_str(), sizeof(sock_addr.sun_path));
353
354 1570 const int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1570 times.
1570 assert(socket_fd != -1);
356
357 #ifndef __APPLE__
358 // fchmod on a socket is not allowed under Mac OS X
359 // using default 0770 here
360
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1570 times.
1570 if (fchmod(socket_fd, mode) != 0)
361 goto make_socket_failure;
362 #endif
363
364 1570 if (bind(socket_fd, reinterpret_cast<struct sockaddr *>(&sock_addr),
365 sizeof(sock_addr.sun_family) + sizeof(sock_addr.sun_path))
366
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1554 times.
1570 < 0) {
367
3/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
16 if ((errno == EADDRINUSE) && (unlink(path.c_str()) == 0)) {
368 // Second try, perhaps the file was left over
369 16 if (bind(socket_fd, reinterpret_cast<struct sockaddr *>(&sock_addr),
370 sizeof(sock_addr.sun_family) + sizeof(sock_addr.sun_path))
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 < 0) {
372 LogCvmfs(kLogCvmfs, kLogDebug, "binding socket failed (%d)", errno);
373 goto make_socket_failure;
374 }
375 } else {
376 LogCvmfs(kLogCvmfs, kLogDebug, "binding socket failed (%d)", errno);
377 goto make_socket_failure;
378 }
379 }
380
381
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 1535 times.
1570 if (short_path != path)
382
1/2
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
35 RemoveShortSocketLink(short_path);
383
384 1570 return socket_fd;
385
386 make_socket_failure:
387 close(socket_fd);
388 if (short_path != path)
389 RemoveShortSocketLink(short_path);
390 return -1;
391 1586 }
392
393
394 /**
395 * Creates and binds a TCP/IPv4 socket. An empty address binds to the "any"
396 * address.
397 */
398 1416 int MakeTcpEndpoint(const std::string &ipv4_address, int portno) {
399 1416 const int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1416 times.
1416 assert(socket_fd != -1);
401 1416 const int on = 1;
402 1416 int retval = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1416 times.
1416 assert(retval == 0);
404
405 struct sockaddr_in endpoint_addr;
406 1416 memset(&endpoint_addr, 0, sizeof(endpoint_addr));
407 1416 endpoint_addr.sin_family = AF_INET;
408
2/2
✓ Branch 1 taken 1383 times.
✓ Branch 2 taken 33 times.
1416 if (ipv4_address.empty()) {
409 1383 endpoint_addr.sin_addr.s_addr = INADDR_ANY;
410 } else {
411 33 retval = inet_aton(ipv4_address.c_str(), &(endpoint_addr.sin_addr));
412
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 22 times.
33 if (retval == 0) {
413
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 LogCvmfs(kLogCvmfs, kLogDebug, "invalid IPv4 address");
414
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 close(socket_fd);
415 11 return -1;
416 }
417 }
418 1405 endpoint_addr.sin_port = htons(portno);
419
420 1405 retval = bind(socket_fd, reinterpret_cast<struct sockaddr *>(&endpoint_addr),
421 sizeof(endpoint_addr));
422
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1394 times.
1405 if (retval < 0) {
423
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 LogCvmfs(kLogCvmfs, kLogDebug, "binding TCP endpoint failed (%d)", errno);
424
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 close(socket_fd);
425 11 return -1;
426 }
427 1394 return socket_fd;
428 }
429
430
431 /**
432 * Connects to a named socket.
433 *
434 * \return socket file descriptor on success, -1 else
435 */
436 578 int ConnectSocket(const std::string &path) {
437
1/2
✓ Branch 1 taken 578 times.
✗ Branch 2 not taken.
578 std::string short_path(path);
438 struct sockaddr_un sock_addr;
439
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 540 times.
578 if (path.length() >= sizeof(sock_addr.sun_path)) {
440 // Socket paths are limited to 108 bytes (on some systems to 92 bytes),
441 // try working around
442
1/2
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
38 short_path = MakeShortSocketLink(path);
443
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 19 times.
38 if (short_path.empty())
444 19 return -1;
445 }
446 559 sock_addr.sun_family = AF_UNIX;
447 559 strncpy(sock_addr.sun_path, short_path.c_str(), sizeof(sock_addr.sun_path));
448
449 559 const int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 559 times.
559 assert(socket_fd != -1);
451
452
1/2
✓ Branch 1 taken 559 times.
✗ Branch 2 not taken.
559 const int retval = connect(
453 socket_fd, reinterpret_cast<struct sockaddr *>(&sock_addr),
454 sizeof(sock_addr.sun_family) + sizeof(sock_addr.sun_path));
455
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 540 times.
559 if (short_path != path)
456
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
19 RemoveShortSocketLink(short_path);
457
458
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 540 times.
559 if (retval < 0) {
459
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
19 close(socket_fd);
460 19 return -1;
461 }
462
463 540 return socket_fd;
464 578 }
465
466
467 /**
468 * Connects to a (remote) TCP server
469 */
470 59 int ConnectTcpEndpoint(const std::string &ipv4_address, int portno) {
471 59 const int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 assert(socket_fd != -1);
473
474 struct sockaddr_in endpoint_addr;
475 59 memset(&endpoint_addr, 0, sizeof(endpoint_addr));
476 59 endpoint_addr.sin_family = AF_INET;
477 59 int retval = inet_aton(ipv4_address.c_str(), &(endpoint_addr.sin_addr));
478
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 48 times.
59 if (retval == 0) {
479
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 LogCvmfs(kLogCvmfs, kLogDebug, "invalid IPv4 address");
480
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 close(socket_fd);
481 11 return -1;
482 }
483 48 endpoint_addr.sin_port = htons(portno);
484
485
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 retval = connect(socket_fd,
486 reinterpret_cast<struct sockaddr *>(&endpoint_addr),
487 sizeof(endpoint_addr));
488
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 37 times.
48 if (retval != 0) {
489 11 LogCvmfs(kLogCvmfs, kLogDebug, "failed to connect to TCP endpoint (%d)",
490
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 errno);
491
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 close(socket_fd);
492 11 return -1;
493 }
494 37 return socket_fd;
495 }
496
497
498 /**
499 * Creating a pipe should always succeed.
500 */
501 35436 void MakePipe(int pipe_fd[2]) {
502 35436 const int retval = pipe(pipe_fd);
503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35436 times.
35436 assert(retval == 0);
504 35436 }
505
506
507 /**
508 * Writes to a pipe should always succeed.
509 */
510 9592807 void WritePipe(int fd, const void *buf, size_t nbyte) {
511 ssize_t num_bytes;
512 do {
513 9592807 num_bytes = write(fd, buf, nbyte);
514
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9592807 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9592807 } while ((num_bytes < 0) && (errno == EINTR));
515
2/4
✓ Branch 0 taken 9592807 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9592807 times.
✗ Branch 3 not taken.
9592807 assert((num_bytes >= 0) && (static_cast<size_t>(num_bytes) == nbyte));
516 9592807 }
517
518
519 /**
520 * Reads from a pipe should always succeed.
521 */
522 71575612 void ReadPipe(int fd, void *buf, size_t nbyte) {
523 ssize_t num_bytes;
524 do {
525 71575612 num_bytes = read(fd, buf, nbyte);
526
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 71575612 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
71575612 } while ((num_bytes < 0) && (errno == EINTR));
527
2/4
✓ Branch 0 taken 71575612 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71575612 times.
✗ Branch 3 not taken.
71575612 assert((num_bytes >= 0) && (static_cast<size_t>(num_bytes) == nbyte));
528 71575612 }
529
530
531 /**
532 * Reads from a pipe where writer's end is not yet necessarily connected
533 */
534 2703499 bool ReadHalfPipe(int fd, void *buf, size_t nbyte, unsigned timeout_ms) {
535 ssize_t num_bytes;
536 2703499 unsigned i = 0;
537 2703499 unsigned backoff_ms = 1;
538 2703499 uint64_t duration_ms = 0;
539 2703499 uint64_t timestamp = 0;
540
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2703484 times.
2703499 if (timeout_ms != 0)
541 15 timestamp = platform_monotonic_time_ns();
542
543 2703499 const unsigned max_backoff_ms = 256;
544 do {
545 // When the writer is not connected, this takes ~200-300ns per call as per
546 // micro benchmarks
547 2703499 num_bytes = read(fd, buf, nbyte);
548
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2703499 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2703499 if ((num_bytes < 0) && (errno == EINTR))
549 continue;
550 2703499 i++;
551 // Start backing off when the busy loop reaches the ballpark of 1ms
552
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2703499 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2703499 if ((i > 3000) && (num_bytes == 0)) {
553 // The BackoffThrottle would pull in too many dependencies
554 SafeSleepMs(backoff_ms);
555 if (backoff_ms < max_backoff_ms)
556 backoff_ms *= 2;
557 }
558
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2703484 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
2703499 if ((timeout_ms != 0) && (num_bytes == 0)) {
559 duration_ms = (platform_monotonic_time_ns() - timestamp)
560 / (1000UL * 1000UL);
561 if (duration_ms > timeout_ms)
562 return false;
563 }
564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2703499 times.
2703499 } while (num_bytes == 0);
565
2/4
✓ Branch 0 taken 2703499 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2703499 times.
✗ Branch 3 not taken.
2703499 assert((num_bytes >= 0) && (static_cast<size_t>(num_bytes) == nbyte));
566 2703499 return true;
567 }
568
569
570 /**
571 * Closes both ends of a pipe
572 */
573 32279 void ClosePipe(int pipe_fd[2]) {
574 32279 close(pipe_fd[0]);
575 32279 close(pipe_fd[1]);
576 32279 }
577
578
579 /**
580 * Compares two directory trees on the meta-data level. Returns true iff the
581 * trees have identical content.
582 */
583 156285 bool DiffTree(const std::string &path_a, const std::string &path_b) {
584 int retval;
585 156285 std::vector<std::string> ls_a;
586 156285 std::vector<std::string> ls_b;
587 156285 std::vector<std::string> subdirs;
588
589
1/2
✓ Branch 2 taken 156285 times.
✗ Branch 3 not taken.
156285 DIR *dirp_a = opendir(path_a.c_str());
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156285 times.
156285 if (dirp_a == NULL)
591 return false;
592
1/2
✓ Branch 2 taken 156285 times.
✗ Branch 3 not taken.
156285 DIR *dirp_b = opendir(path_b.c_str());
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156285 times.
156285 if (dirp_b == NULL) {
594 closedir(dirp_a);
595 return false;
596 }
597
598 platform_dirent64 *dirent;
599
3/4
✓ Branch 1 taken 633397 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 477112 times.
✓ Branch 4 taken 156285 times.
633397 while ((dirent = platform_readdir(dirp_a))) {
600
1/2
✓ Branch 2 taken 477112 times.
✗ Branch 3 not taken.
477112 const std::string name(dirent->d_name);
601
6/6
✓ Branch 1 taken 320827 times.
✓ Branch 2 taken 156285 times.
✓ Branch 4 taken 156285 times.
✓ Branch 5 taken 164542 times.
✓ Branch 6 taken 312570 times.
✓ Branch 7 taken 164542 times.
477112 if ((name == ".") || (name == ".."))
602 312570 continue;
603
2/4
✓ Branch 1 taken 164542 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 164542 times.
✗ Branch 5 not taken.
164542 const std::string path = path_a + "/" + name;
604
1/2
✓ Branch 1 taken 164542 times.
✗ Branch 2 not taken.
164542 ls_a.push_back(path);
605
606 platform_stat64 info;
607 164542 retval = platform_lstat(path.c_str(), &info);
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 164542 times.
164542 if (retval != 0) {
609 closedir(dirp_a);
610 closedir(dirp_b);
611 return false;
612 }
613
2/2
✓ Branch 0 taken 156952 times.
✓ Branch 1 taken 7590 times.
164542 if (S_ISDIR(info.st_mode))
614
1/2
✓ Branch 1 taken 156952 times.
✗ Branch 2 not taken.
156952 subdirs.push_back(name);
615
3/5
✓ Branch 1 taken 164542 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 164542 times.
✓ Branch 5 taken 312570 times.
✗ Branch 6 not taken.
477112 }
616
3/4
✓ Branch 1 taken 632776 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 476491 times.
✓ Branch 4 taken 156285 times.
632776 while ((dirent = platform_readdir(dirp_b))) {
617
1/2
✓ Branch 2 taken 476491 times.
✗ Branch 3 not taken.
476491 const std::string name(dirent->d_name);
618
6/6
✓ Branch 1 taken 320206 times.
✓ Branch 2 taken 156285 times.
✓ Branch 4 taken 156285 times.
✓ Branch 5 taken 163921 times.
✓ Branch 6 taken 312570 times.
✓ Branch 7 taken 163921 times.
476491 if ((name == ".") || (name == ".."))
619 312570 continue;
620
2/4
✓ Branch 1 taken 163921 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 163921 times.
✗ Branch 5 not taken.
163921 const std::string path = path_b + "/" + name;
621
1/2
✓ Branch 1 taken 163921 times.
✗ Branch 2 not taken.
163921 ls_b.push_back(path);
622
2/2
✓ Branch 2 taken 163921 times.
✓ Branch 3 taken 312570 times.
476491 }
623
1/2
✓ Branch 1 taken 156285 times.
✗ Branch 2 not taken.
156285 closedir(dirp_a);
624
1/2
✓ Branch 1 taken 156285 times.
✗ Branch 2 not taken.
156285 closedir(dirp_b);
625
626
1/2
✓ Branch 3 taken 156285 times.
✗ Branch 4 not taken.
156285 sort(ls_a.begin(), ls_a.end());
627
1/2
✓ Branch 3 taken 156285 times.
✗ Branch 4 not taken.
156285 sort(ls_b.begin(), ls_b.end());
628
2/2
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 156262 times.
156285 if (ls_a.size() != ls_b.size())
629 23 return false;
630
2/2
✓ Branch 1 taken 163438 times.
✓ Branch 2 taken 156262 times.
319700 for (unsigned i = 0; i < ls_a.size(); ++i) {
631
3/7
✓ Branch 2 taken 163438 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 163438 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 163438 times.
163438 if (GetFileName(ls_a[i]) != GetFileName(ls_b[i]))
632 return false;
633 platform_stat64 info_a;
634 platform_stat64 info_b;
635 163438 retval = platform_lstat(ls_a[i].c_str(), &info_a);
636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163438 times.
163438 if (retval != 0)
637 return false;
638 163438 retval = platform_lstat(ls_b[i].c_str(), &info_b);
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163438 times.
163438 if (retval != 0)
640 return false;
641
2/4
✓ Branch 0 taken 163438 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 163438 times.
✗ Branch 3 not taken.
163438 if ((info_a.st_mode != info_b.st_mode) || (info_a.st_uid != info_b.st_uid)
642
1/2
✓ Branch 0 taken 163438 times.
✗ Branch 1 not taken.
163438 || (info_a.st_gid != info_b.st_gid)
643
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 163438 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
163438 || ((info_a.st_size != info_b.st_size) && !S_ISDIR(info_a.st_mode))) {
644 return false;
645 }
646 }
647
648
2/2
✓ Branch 1 taken 156216 times.
✓ Branch 2 taken 156262 times.
312478 for (unsigned i = 0; i < subdirs.size(); ++i) {
649
3/6
✓ Branch 2 taken 156216 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 156216 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 156216 times.
✗ Branch 9 not taken.
156216 const bool retval_subtree = DiffTree(path_a + "/" + subdirs[i],
650
2/4
✓ Branch 2 taken 156216 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 156216 times.
✗ Branch 6 not taken.
312432 path_b + "/" + subdirs[i]);
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156216 times.
156216 if (!retval_subtree)
652 return false;
653 }
654
655 156262 return true;
656 156285 }
657
658
659 /**
660 * Changes a non-blocking file descriptor to a blocking one.
661 */
662 132 void Nonblock2Block(int filedes) {
663 132 const int flags = fcntl(filedes, F_GETFL);
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 assert(flags != -1);
665 132 const int retval = fcntl(filedes, F_SETFL, flags & ~O_NONBLOCK);
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 assert(retval != -1);
667 132 }
668
669
670 /**
671 * Changes a blocking file descriptor to a non-blocking one.
672 */
673 122 void Block2Nonblock(int filedes) {
674 122 const int flags = fcntl(filedes, F_GETFL);
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
122 assert(flags != -1);
676 122 const int retval = fcntl(filedes, F_SETFL, flags | O_NONBLOCK);
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
122 assert(retval != -1);
678 122 }
679
680
681 /**
682 * Drops the characters of string to a socket. It doesn't matter
683 * if the other side has hung up.
684 */
685 386 void SendMsg2Socket(const int fd, const std::string &msg) {
686 386 (void)send(fd, &msg[0], msg.length(), MSG_NOSIGNAL);
687 386 }
688
689 /**
690 * Sends the file descriptor passing_fd to the socket socket_fd. Can be used
691 * to transfer an open file descriptor from one process to another. Use
692 * ConnectSocket() to get the socket_fd.
693 */
694 17 bool SendFd2Socket(int socket_fd, int passing_fd) {
695 union {
696 // Make sure that ctrl_msg is properly aligned.
697 struct cmsghdr align;
698 // Buffer large enough to store the file descriptor (ancillary data)
699 unsigned char buf[CMSG_SPACE(sizeof(int))];
700 } ctrl_msg;
701
702 17 memset(ctrl_msg.buf, 0, sizeof(ctrl_msg.buf));
703
704 struct msghdr msgh;
705 17 msgh.msg_name = NULL;
706 17 msgh.msg_namelen = 0;
707
708 17 unsigned char dummy = 0;
709 struct iovec iov;
710 17 iov.iov_base = &dummy;
711 17 iov.iov_len = 1;
712 17 msgh.msg_iov = &iov;
713 17 msgh.msg_iovlen = 1;
714
715 17 msgh.msg_control = ctrl_msg.buf;
716 17 msgh.msg_controllen = sizeof(ctrl_msg.buf);
717
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 struct cmsghdr *cmsgp = CMSG_FIRSTHDR(&msgh);
718 17 cmsgp->cmsg_len = CMSG_LEN(sizeof(int));
719 17 cmsgp->cmsg_level = SOL_SOCKET;
720 17 cmsgp->cmsg_type = SCM_RIGHTS;
721 17 memcpy(CMSG_DATA(cmsgp), &passing_fd, sizeof(int));
722
723
1/2
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
17 const ssize_t retval = sendmsg(socket_fd, &msgh, 0);
724 17 return (retval != -1);
725 }
726
727
728 /**
729 * Returns the file descriptor that has been sent with SendFd2Socket. The
730 * msg_fd file descriptor needs to come from a call to accept() on the socket
731 * where the passing file descriptor has been sent to.
732 * Returns -errno on error.
733 */
734 int RecvFdFromSocket(int msg_fd) {
735 union {
736 // Make sure that ctrl_msg is properly aligned.
737 struct cmsghdr align;
738 // Buffer large enough to store the file descriptor (ancillary data)
739 unsigned char buf[CMSG_SPACE(sizeof(int))];
740 } ctrl_msg;
741
742 memset(ctrl_msg.buf, 0, sizeof(ctrl_msg.buf));
743
744 struct msghdr msgh;
745 msgh.msg_name = NULL;
746 msgh.msg_namelen = 0;
747
748 unsigned char dummy;
749 struct iovec iov;
750 iov.iov_base = &dummy;
751 iov.iov_len = 1;
752 msgh.msg_iov = &iov;
753 msgh.msg_iovlen = 1;
754
755 msgh.msg_control = ctrl_msg.buf;
756 msgh.msg_controllen = sizeof(ctrl_msg.buf);
757
758 const ssize_t retval = recvmsg(msg_fd, &msgh, 0);
759 if (retval == -1)
760 return -errno;
761
762 struct cmsghdr *cmsgp = CMSG_FIRSTHDR(&msgh);
763 assert(cmsgp != NULL);
764 if (cmsgp->cmsg_len != CMSG_LEN(sizeof(int)))
765 return -ERANGE;
766 assert(cmsgp->cmsg_level == SOL_SOCKET);
767 assert(cmsgp->cmsg_type == SCM_RIGHTS);
768
769 int passing_fd;
770 memcpy(&passing_fd, CMSG_DATA(cmsgp), sizeof(int));
771 assert(passing_fd >= 0);
772 return passing_fd;
773 }
774
775
776 17 std::string GetHostname() {
777 char name[HOST_NAME_MAX + 1];
778 17 const int retval = gethostname(name, HOST_NAME_MAX);
779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 assert(retval == 0);
780
1/2
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 return name;
781 }
782
783
784 /**
785 * set(e){g/u}id wrapper.
786 *
787 * avoid_mutexes: if true, means that we are a forked child and must not use
788 * mutexes.
789 */
790 52 bool SwitchCredentials(const uid_t uid, const gid_t gid, const bool temporarily,
791 const bool avoid_mutexes) {
792
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 28 times.
52 if (!avoid_mutexes) {
793 24 LogCvmfs(kLogCvmfs, kLogDebug,
794 "current credentials uid %d gid %d "
795 "euid %d egid %d, switching to %d %d (temp: %d)",
796 getuid(), getgid(), geteuid(), getegid(), uid, gid, temporarily);
797 }
798 52 int retval = 0;
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (temporarily) {
800 if (gid != getegid())
801 retval = setegid(gid);
802 if ((retval == 0) && (uid != geteuid()))
803 retval = seteuid(uid);
804 } else {
805 // If effective uid is not root, we must first gain root access back
806
2/6
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 52 times.
52 if ((getuid() == 0) && (getuid() != geteuid())) {
807 retval = SwitchCredentials(0, getgid(), true);
808 if (!retval)
809 return false;
810 }
811
3/4
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 28 times.
52 retval = setgid(gid) || setuid(uid);
812 }
813
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 28 times.
52 if (!avoid_mutexes) {
814 24 LogCvmfs(kLogCvmfs, kLogDebug, "switch credentials result %d (%d)", retval,
815 24 errno);
816 }
817 52 return retval == 0;
818 }
819
820
821 /**
822 * Checks if the regular file path exists.
823 */
824 108794 bool FileExists(const std::string &path) {
825 platform_stat64 info;
826
4/4
✓ Branch 2 taken 65653 times.
✓ Branch 3 taken 43141 times.
✓ Branch 4 taken 64415 times.
✓ Branch 5 taken 1238 times.
108794 return ((platform_lstat(path.c_str(), &info) == 0) && S_ISREG(info.st_mode));
827 }
828
829
830 /**
831 * Returns -1 on failure.
832 */
833 14509597 int64_t GetFileSize(const std::string &path) {
834 platform_stat64 info;
835 14509597 const int retval = platform_stat(path.c_str(), &info);
836
2/2
✓ Branch 0 taken 14437978 times.
✓ Branch 1 taken 118809 times.
14556787 if (retval != 0)
837 14437978 return -1;
838 118809 return info.st_size;
839 }
840
841
842 /**
843 * Checks if the directory (not symlink) path exists.
844 */
845 4670 bool DirectoryExists(const std::string &path) {
846 platform_stat64 info;
847
3/4
✓ Branch 2 taken 4608 times.
✓ Branch 3 taken 62 times.
✓ Branch 4 taken 4608 times.
✗ Branch 5 not taken.
4670 return ((platform_lstat(path.c_str(), &info) == 0) && S_ISDIR(info.st_mode));
848 }
849
850
851 /**
852 * Checks if the symlink file path exists.
853 */
854 1785 bool SymlinkExists(const std::string &path) {
855 platform_stat64 info;
856
4/4
✓ Branch 2 taken 1645 times.
✓ Branch 3 taken 140 times.
✓ Branch 4 taken 1453 times.
✓ Branch 5 taken 192 times.
1785 return ((platform_lstat(path.c_str(), &info) == 0) && S_ISLNK(info.st_mode));
857 }
858
859
860 /**
861 * Equivalent of `ln -sf $src $dest`
862 */
863 120 bool SymlinkForced(const std::string &src, const std::string &dest) {
864 120 int retval = unlink(dest.c_str());
865
3/4
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 110 times.
120 if ((retval != 0) && (errno != ENOENT))
866 return false;
867 120 retval = symlink(src.c_str(), dest.c_str());
868 120 return retval == 0;
869 }
870
871
872 /**
873 * The mkdir -p command. Additionally checks if the directory is writable
874 * if it exists.
875 */
876 1633993 bool MkdirDeep(const std::string &path,
877 const mode_t mode,
878 bool verify_writable) {
879
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 1633975 times.
1633993 if (path == "")
880 18 return false;
881
882 1633975 int retval = mkdir(path.c_str(), mode);
883
2/2
✓ Branch 0 taken 1628474 times.
✓ Branch 1 taken 5501 times.
1633975 if (retval == 0)
884 1628474 return true;
885
886 11002 if ((errno == ENOENT)
887
9/15
✓ Branch 0 taken 3536 times.
✓ Branch 1 taken 1965 times.
✓ Branch 3 taken 3536 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 3536 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3536 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3536 times.
✓ Branch 11 taken 1965 times.
✓ Branch 13 taken 3536 times.
✓ Branch 14 taken 1965 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
5501 && (MkdirDeep(GetParentPath(path), mode, verify_writable))) {
888 3536 return MkdirDeep(path, mode, verify_writable);
889 }
890
891
2/2
✓ Branch 0 taken 1877 times.
✓ Branch 1 taken 88 times.
1965 if (errno == EEXIST) {
892 platform_stat64 info;
893
5/6
✓ Branch 2 taken 1877 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1860 times.
✓ Branch 5 taken 17 times.
✓ Branch 6 taken 1860 times.
✓ Branch 7 taken 17 times.
1877 if ((platform_stat(path.c_str(), &info) == 0) && S_ISDIR(info.st_mode)) {
894
2/2
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 1378 times.
1860 if (verify_writable) {
895 482 retval = utimes(path.c_str(), NULL);
896
1/2
✓ Branch 0 taken 482 times.
✗ Branch 1 not taken.
482 if (retval == 0)
897 1860 return true;
898 } else {
899 1378 return true;
900 }
901 }
902 }
903
904 105 return false;
905 }
906
907
908 /**
909 * Creates the "hash cache" directory structure in path.
910 */
911 6405 bool MakeCacheDirectories(const std::string &path, const mode_t mode) {
912
1/2
✓ Branch 1 taken 6405 times.
✗ Branch 2 not taken.
6405 const std::string canonical_path = MakeCanonicalPath(path);
913
914
1/2
✓ Branch 1 taken 6405 times.
✗ Branch 2 not taken.
6405 std::string this_path = canonical_path + "/quarantaine";
915
3/4
✓ Branch 1 taken 6405 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 88 times.
✓ Branch 4 taken 6317 times.
6405 if (!MkdirDeep(this_path, mode, false))
916 88 return false;
917
918
1/2
✓ Branch 1 taken 6317 times.
✗ Branch 2 not taken.
6317 this_path = canonical_path + "/ff";
919
920 platform_stat64 stat_info;
921
2/2
✓ Branch 2 taken 5063 times.
✓ Branch 3 taken 1254 times.
6317 if (platform_stat(this_path.c_str(), &stat_info) != 0) {
922
1/2
✓ Branch 1 taken 5063 times.
✗ Branch 2 not taken.
5063 this_path = canonical_path + "/txn";
923
2/4
✓ Branch 1 taken 5063 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5063 times.
5063 if (!MkdirDeep(this_path, mode, false))
924 return false;
925
2/2
✓ Branch 0 taken 1296128 times.
✓ Branch 1 taken 5063 times.
1301191 for (int i = 0; i <= 0xff; i++) {
926 char hex[4];
927 1296128 snprintf(hex, sizeof(hex), "%02x", i);
928
3/6
✓ Branch 2 taken 1296128 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1296128 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1296128 times.
✗ Branch 9 not taken.
1296128 this_path = canonical_path + "/" + std::string(hex);
929
2/4
✓ Branch 1 taken 1296128 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1296128 times.
1296128 if (!MkdirDeep(this_path, mode, false))
930 return false;
931 }
932 }
933 6317 return true;
934 6405 }
935
936
937 /**
938 * Tries to locks file path, return an error if file is already locked.
939 * Creates path if required.
940 *
941 * \return file descriptor, -1 on error, -2 if it would block
942 */
943 1987 int TryLockFile(const std::string &path) {
944 1987 const int fd_lockfile = open(path.c_str(), O_RDONLY | O_CREAT, 0600);
945
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1970 times.
1987 if (fd_lockfile < 0)
946 17 return -1;
947
948
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 1935 times.
1970 if (flock(fd_lockfile, LOCK_EX | LOCK_NB) != 0) {
949 35 close(fd_lockfile);
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (errno != EWOULDBLOCK)
951 return -1;
952 35 return -2;
953 }
954
955 1935 return fd_lockfile;
956 }
957
958
959 /**
960 * Tries to write the process id in a /var/run/progname.pid like file. Returns
961 * the same as TryLockFile.
962 *
963 * \return file descriptor, -1 on error, -2 if it would block
964 */
965 28 int WritePidFile(const std::string &path) {
966
1/2
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 const int fd = open(path.c_str(), O_CREAT | O_RDWR, 0600);
967
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 21 times.
28 if (fd < 0)
968 7 return -1;
969
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 14 times.
21 if (flock(fd, LOCK_EX | LOCK_NB) != 0) {
970
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 close(fd);
971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (errno != EWOULDBLOCK)
972 return -1;
973 7 return -2;
974 }
975
976 // Don't leak the file descriptor to exec'd children
977
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 int flags = fcntl(fd, F_GETFD);
978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 assert(flags != -1);
979 14 flags |= FD_CLOEXEC;
980
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 flags = fcntl(fd, F_SETFD, flags);
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 assert(flags != -1);
982
983 char buf[64];
984
985 14 snprintf(buf, sizeof(buf), "%" PRId64 "\n", static_cast<uint64_t>(getpid()));
986 14 const bool retval = (ftruncate(fd, 0) == 0)
987
3/6
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
✗ Branch 6 not taken.
14 && SafeWrite(fd, buf, strlen(buf));
988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!retval) {
989 UnlockFile(fd);
990 return -1;
991 }
992 14 return fd;
993 }
994
995
996 /**
997 * Locks file path, blocks if file is already locked. Creates path if required.
998 *
999 * \return file descriptor, -1 on error
1000 */
1001 2647 int LockFile(const std::string &path) {
1002 2647 const int fd_lockfile = open(path.c_str(), O_RDONLY | O_CREAT, 0600);
1003
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 2566 times.
2647 if (fd_lockfile < 0)
1004 81 return -1;
1005
1006
1007
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2556 times.
2566 if (flock(fd_lockfile, LOCK_EX | LOCK_NB) != 0) {
1008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (errno != EWOULDBLOCK) {
1009 close(fd_lockfile);
1010 return -1;
1011 }
1012 10 LogCvmfs(kLogCvmfs, kLogSyslog, "another process holds %s, waiting.",
1013 path.c_str());
1014
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (flock(fd_lockfile, LOCK_EX) != 0) {
1015 close(fd_lockfile);
1016 return -1;
1017 }
1018 10 LogCvmfs(kLogCvmfs, kLogSyslog, "lock %s acquired", path.c_str());
1019 }
1020
1021 2566 return fd_lockfile;
1022 }
1023
1024
1025 4457 void UnlockFile(const int filedes) {
1026 4457 const int retval = flock(filedes, LOCK_UN);
1027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4457 times.
4457 assert(retval == 0);
1028 4457 close(filedes);
1029 4457 }
1030
1031
1032 /**
1033 * Wrapper around mkstemp.
1034 */
1035 66213 FILE *CreateTempFile(const std::string &path_prefix, const int mode,
1036 const char *open_flags, std::string *final_path) {
1037 66213 *final_path = path_prefix + ".XXXXXX";
1038 66213 char *tmp_file = strdupa(final_path->c_str());
1039 66213 const int tmp_fd = mkstemp(tmp_file);
1040
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 66178 times.
66213 if (tmp_fd < 0) {
1041 35 return NULL;
1042 }
1043
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 66178 times.
66178 if (fchmod(tmp_fd, mode) != 0) {
1044 close(tmp_fd);
1045 return NULL;
1046 }
1047
1048 66178 *final_path = tmp_file;
1049 66178 FILE *tmp_fp = fdopen(tmp_fd, open_flags);
1050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66178 times.
66178 if (!tmp_fp) {
1051 close(tmp_fd);
1052 unlink(tmp_file);
1053 return NULL;
1054 }
1055
1056 66178 return tmp_fp;
1057 }
1058
1059
1060 /**
1061 * Create the file but don't open. Use only in non-public tmp directories.
1062 */
1063 18095 std::string CreateTempPath(const std::string &path_prefix, const int mode) {
1064 18095 std::string result;
1065
1/2
✓ Branch 1 taken 18095 times.
✗ Branch 2 not taken.
18095 FILE *f = CreateTempFile(path_prefix, mode, "w", &result);
1066
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 18079 times.
18095 if (!f)
1067
1/2
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 return "";
1068
1/2
✓ Branch 1 taken 18079 times.
✗ Branch 2 not taken.
18079 fclose(f);
1069 18079 return result;
1070 18095 }
1071
1072
1073 /**
1074 * Create a directory with a unique name.
1075 */
1076 5897 std::string CreateTempDir(const std::string &path_prefix) {
1077
1/2
✓ Branch 1 taken 5897 times.
✗ Branch 2 not taken.
5897 const std::string dir = path_prefix + ".XXXXXX";
1078 5897 char *tmp_dir = strdupa(dir.c_str());
1079 5897 tmp_dir = mkdtemp(tmp_dir);
1080
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 5885 times.
5897 if (tmp_dir == NULL)
1081
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 return "";
1082
1/2
✓ Branch 2 taken 5885 times.
✗ Branch 3 not taken.
5885 return std::string(tmp_dir);
1083 5897 }
1084
1085
1086 /**
1087 * Get the current working directory of the running process
1088 */
1089 4739 std::string GetCurrentWorkingDirectory() {
1090 char cwd[PATH_MAX];
1091
3/9
✓ Branch 1 taken 4739 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 4739 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 4739 times.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
4739 return (getcwd(cwd, sizeof(cwd)) != NULL) ? std::string(cwd) : std::string();
1092 }
1093
1094
1095 /**
1096 * Helper class that provides callback functions for the file system traversal.
1097 */
1098 class RemoveTreeHelper {
1099 public:
1100 bool success;
1101 15104 RemoveTreeHelper() { success = true; }
1102 100277 void RemoveFile(const std::string &parent_path, const std::string &name) {
1103
1/2
✓ Branch 2 taken 100277 times.
✗ Branch 3 not taken.
100277 const int retval = unlink((parent_path + "/" + name).c_str());
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100277 times.
100277 if (retval != 0)
1105 success = false;
1106 100277 }
1107 44020 void RemoveDir(const std::string &parent_path, const std::string &name) {
1108
1/2
✓ Branch 2 taken 44020 times.
✗ Branch 3 not taken.
44020 const int retval = rmdir((parent_path + "/" + name).c_str());
1109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44020 times.
44020 if (retval != 0)
1110 success = false;
1111 44020 }
1112 1552995 bool TryRemoveDir(const std::string &parent_path, const std::string &name) {
1113
1/2
✓ Branch 2 taken 1552995 times.
✗ Branch 3 not taken.
1552995 const int retval = rmdir((parent_path + "/" + name).c_str());
1114 1552995 return (retval != 0);
1115 }
1116 };
1117
1118
1119 /**
1120 * Does rm -rf on path.
1121 */
1122 16197 bool RemoveTree(const std::string &path) {
1123 platform_stat64 info;
1124 16197 const int retval = platform_lstat(path.c_str(), &info);
1125
2/2
✓ Branch 0 taken 1093 times.
✓ Branch 1 taken 15104 times.
16197 if (retval != 0)
1126 1093 return errno == ENOENT;
1127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15104 times.
15104 if (!S_ISDIR(info.st_mode))
1128 return false;
1129
1130
1/2
✓ Branch 1 taken 15104 times.
✗ Branch 2 not taken.
15104 RemoveTreeHelper *remove_tree_helper = new RemoveTreeHelper();
1131
2/4
✓ Branch 2 taken 15104 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 15104 times.
✗ Branch 6 not taken.
30208 FileSystemTraversal<RemoveTreeHelper> traversal(remove_tree_helper, "", true);
1132 15104 traversal.fn_new_file = &RemoveTreeHelper::RemoveFile;
1133 15104 traversal.fn_new_character_dev = &RemoveTreeHelper::RemoveFile;
1134 15104 traversal.fn_new_symlink = &RemoveTreeHelper::RemoveFile;
1135 15104 traversal.fn_new_socket = &RemoveTreeHelper::RemoveFile;
1136 15104 traversal.fn_new_fifo = &RemoveTreeHelper::RemoveFile;
1137 15104 traversal.fn_leave_dir = &RemoveTreeHelper::RemoveDir;
1138 15104 traversal.fn_new_dir_prefix = &RemoveTreeHelper::TryRemoveDir;
1139
1/2
✓ Branch 1 taken 15104 times.
✗ Branch 2 not taken.
15104 traversal.Recurse(path);
1140 15104 const bool result = remove_tree_helper->success;
1141
1/2
✓ Branch 0 taken 15104 times.
✗ Branch 1 not taken.
15104 delete remove_tree_helper;
1142
1143 15104 return result;
1144 15104 }
1145
1146
1147 /**
1148 * Returns ls $dir/GLOB$suffix
1149 */
1150 304 std::vector<std::string> FindFilesBySuffix(const std::string &dir,
1151 const std::string &suffix) {
1152 304 std::vector<std::string> result;
1153
1/2
✓ Branch 2 taken 304 times.
✗ Branch 3 not taken.
304 DIR *dirp = opendir(dir.c_str());
1154
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 103 times.
304 if (!dirp)
1155 201 return result;
1156
1157 platform_dirent64 *dirent;
1158
3/4
✓ Branch 1 taken 613 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 510 times.
✓ Branch 4 taken 103 times.
613 while ((dirent = platform_readdir(dirp))) {
1159
1/2
✓ Branch 2 taken 510 times.
✗ Branch 3 not taken.
510 const std::string name(dirent->d_name);
1160 510 if ((name.length() >= suffix.length())
1161
9/14
✓ Branch 1 taken 396 times.
✓ Branch 2 taken 114 times.
✓ Branch 6 taken 396 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 225 times.
✓ Branch 10 taken 171 times.
✓ Branch 11 taken 396 times.
✓ Branch 12 taken 114 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 225 times.
✓ Branch 15 taken 285 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
1020 && (name.substr(name.length() - suffix.length()) == suffix)) {
1162
3/6
✓ Branch 1 taken 225 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 225 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 225 times.
✗ Branch 8 not taken.
225 result.push_back(dir + "/" + name);
1163 }
1164 510 }
1165
1/2
✓ Branch 1 taken 103 times.
✗ Branch 2 not taken.
103 closedir(dirp);
1166
1/2
✓ Branch 3 taken 103 times.
✗ Branch 4 not taken.
103 std::sort(result.begin(), result.end());
1167 103 return result;
1168 }
1169
1170
1171 /**
1172 * Returns ls $dir/$prefixGLOB
1173 */
1174 85 std::vector<std::string> FindFilesByPrefix(const std::string &dir,
1175 const std::string &prefix) {
1176 85 std::vector<std::string> result;
1177
1/2
✓ Branch 2 taken 85 times.
✗ Branch 3 not taken.
85 DIR *dirp = opendir(dir.c_str());
1178
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 68 times.
85 if (!dirp)
1179 17 return result;
1180
1181 platform_dirent64 *dirent;
1182
3/4
✓ Branch 1 taken 476 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 408 times.
✓ Branch 4 taken 68 times.
476 while ((dirent = platform_readdir(dirp))) {
1183
1/2
✓ Branch 2 taken 408 times.
✗ Branch 3 not taken.
408 const std::string name(dirent->d_name);
1184 408 if ((name.length() >= prefix.length())
1185
9/14
✓ Branch 1 taken 340 times.
✓ Branch 2 taken 68 times.
✓ Branch 5 taken 340 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 187 times.
✓ Branch 9 taken 153 times.
✓ Branch 10 taken 340 times.
✓ Branch 11 taken 68 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 187 times.
✓ Branch 14 taken 221 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
816 && (name.substr(0, prefix.length()) == prefix)) {
1186
3/6
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 187 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 187 times.
✗ Branch 8 not taken.
187 result.push_back(dir + "/" + name);
1187 }
1188 408 }
1189
1/2
✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
68 closedir(dirp);
1190
1/2
✓ Branch 3 taken 68 times.
✗ Branch 4 not taken.
68 std::sort(result.begin(), result.end());
1191 68 return result;
1192 }
1193
1194
1195 /**
1196 * Finds all direct subdirectories under parent_dir (except ., ..). Used,
1197 * for instance, to parse /etc/cvmfs/repositories.d/<reponoame>
1198 */
1199 68 std::vector<std::string> FindDirectories(const std::string &parent_dir) {
1200 68 std::vector<std::string> result;
1201
1/2
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
68 DIR *dirp = opendir(parent_dir.c_str());
1202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (!dirp)
1203 return result;
1204
1205 platform_dirent64 *dirent;
1206
3/4
✓ Branch 1 taken 357 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 289 times.
✓ Branch 4 taken 68 times.
357 while ((dirent = platform_readdir(dirp))) {
1207
1/2
✓ Branch 2 taken 289 times.
✗ Branch 3 not taken.
289 const std::string name(dirent->d_name);
1208
6/6
✓ Branch 1 taken 221 times.
✓ Branch 2 taken 68 times.
✓ Branch 4 taken 68 times.
✓ Branch 5 taken 153 times.
✓ Branch 6 taken 136 times.
✓ Branch 7 taken 153 times.
289 if ((name == ".") || (name == ".."))
1209 136 continue;
1210
2/4
✓ Branch 1 taken 153 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 153 times.
✗ Branch 5 not taken.
153 const std::string path = parent_dir + "/" + name;
1211
1212 platform_stat64 info;
1213 153 const int retval = platform_stat(path.c_str(), &info);
1214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
153 if (retval != 0)
1215 continue;
1216
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 34 times.
153 if (S_ISDIR(info.st_mode))
1217
1/2
✓ Branch 1 taken 119 times.
✗ Branch 2 not taken.
119 result.push_back(path);
1218
3/4
✓ Branch 1 taken 153 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 153 times.
✓ Branch 5 taken 136 times.
289 }
1219
1/2
✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
68 closedir(dirp);
1220
1/2
✓ Branch 3 taken 68 times.
✗ Branch 4 not taken.
68 sort(result.begin(), result.end());
1221 68 return result;
1222 }
1223
1224
1225 /**
1226 * Finds all files and direct subdirectories under directory (except ., ..).
1227 */
1228 121 bool ListDirectory(const std::string &directory,
1229 std::vector<std::string> *names,
1230 std::vector<mode_t> *modes) {
1231 121 DIR *dirp = opendir(directory.c_str());
1232
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 90 times.
121 if (!dirp)
1233 31 return false;
1234
1235 platform_dirent64 *dirent;
1236
2/2
✓ Branch 1 taken 1032 times.
✓ Branch 2 taken 90 times.
1122 while ((dirent = platform_readdir(dirp))) {
1237
1/2
✓ Branch 2 taken 1032 times.
✗ Branch 3 not taken.
1032 const std::string name(dirent->d_name);
1238
6/6
✓ Branch 1 taken 942 times.
✓ Branch 2 taken 90 times.
✓ Branch 4 taken 90 times.
✓ Branch 5 taken 852 times.
✓ Branch 6 taken 180 times.
✓ Branch 7 taken 852 times.
1032 if ((name == ".") || (name == ".."))
1239 180 continue;
1240
2/4
✓ Branch 1 taken 852 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 852 times.
✗ Branch 5 not taken.
852 const std::string path = directory + "/" + name;
1241
1242 platform_stat64 info;
1243 852 const int retval = platform_lstat(path.c_str(), &info);
1244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 852 times.
852 if (retval != 0) {
1245 closedir(dirp);
1246 return false;
1247 }
1248
1249
1/2
✓ Branch 1 taken 852 times.
✗ Branch 2 not taken.
852 names->push_back(name);
1250
1/2
✓ Branch 1 taken 852 times.
✗ Branch 2 not taken.
852 modes->push_back(info.st_mode);
1251
3/5
✓ Branch 1 taken 852 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 852 times.
✓ Branch 5 taken 180 times.
✗ Branch 6 not taken.
1032 }
1252 90 closedir(dirp);
1253
1254 90 SortTeam(names, modes);
1255 90 return true;
1256 }
1257
1258
1259 /**
1260 * Looks whether exe is an executable file. If exe is not an absolute path,
1261 * searches the PATH environment.
1262 */
1263 51 std::string FindExecutable(const std::string &exe) {
1264
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if (exe.empty())
1265 return "";
1266
1267 51 std::vector<std::string> search_paths;
1268
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 34 times.
51 if (exe[0] == '/') {
1269
2/4
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
17 search_paths.push_back(GetParentPath(exe));
1270 } else {
1271 34 char *path_env = getenv("PATH");
1272
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (path_env) {
1273
2/4
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 34 times.
✗ Branch 6 not taken.
34 search_paths = SplitString(path_env, ':');
1274 }
1275 }
1276
1277
2/2
✓ Branch 1 taken 187 times.
✓ Branch 2 taken 17 times.
204 for (unsigned i = 0; i < search_paths.size(); ++i) {
1278
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 187 times.
187 if (search_paths[i].empty())
1279 153 continue;
1280
2/4
✓ Branch 2 taken 187 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 187 times.
187 if (search_paths[i][0] != '/')
1281 continue;
1282
1283
3/6
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 187 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 187 times.
✗ Branch 9 not taken.
374 std::string path = search_paths[i] + "/" + GetFileName(exe);
1284 platform_stat64 info;
1285 187 int retval = platform_stat(path.c_str(), &info);
1286
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 34 times.
187 if (retval != 0)
1287 153 continue;
1288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (!S_ISREG(info.st_mode))
1289 continue;
1290 34 retval = access(path.c_str(), X_OK);
1291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (retval != 0)
1292 continue;
1293
1294 34 return path;
1295
2/2
✓ Branch 1 taken 153 times.
✓ Branch 2 taken 34 times.
187 }
1296
1297
1/2
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 return "";
1298 51 }
1299
1300
1301 17 std::string GetUserName() {
1302 struct passwd pwd;
1303 17 struct passwd *result = NULL;
1304 17 int bufsize = 16 * 1024;
1305 17 char *buf = static_cast<char *>(smalloc(bufsize));
1306
2/4
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 17 times.
17 while (getpwuid_r(geteuid(), &pwd, buf, bufsize, &result) == ERANGE) {
1307 bufsize *= 2;
1308 buf = static_cast<char *>(srealloc(buf, bufsize));
1309 }
1310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (result == NULL) {
1311 free(buf);
1312 return "";
1313 }
1314
1/2
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 std::string user_name = pwd.pw_name;
1315 17 free(buf);
1316 17 return user_name;
1317 17 }
1318
1319 std::string GetShell() {
1320 struct passwd pwd;
1321 struct passwd *result = NULL;
1322 int bufsize = 16 * 1024;
1323 char *buf = static_cast<char *>(smalloc(bufsize));
1324 while (getpwuid_r(geteuid(), &pwd, buf, bufsize, &result) == ERANGE) {
1325 bufsize *= 2;
1326 buf = static_cast<char *>(srealloc(buf, bufsize));
1327 }
1328 if (result == NULL) {
1329 free(buf);
1330 return "";
1331 }
1332 std::string shell = pwd.pw_shell;
1333 free(buf);
1334 return shell;
1335 }
1336
1337 /**
1338 * UID -> Name from passwd database
1339 */
1340 16 bool GetUserNameOf(uid_t uid, std::string *username) {
1341 struct passwd pwd;
1342 16 struct passwd *result = NULL;
1343 16 int bufsize = 16 * 1024;
1344 16 char *buf = static_cast<char *>(smalloc(bufsize));
1345
2/4
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
16 while (getpwuid_r(uid, &pwd, buf, bufsize, &result) == ERANGE) {
1346 bufsize *= 2;
1347 buf = static_cast<char *>(srealloc(buf, bufsize));
1348 }
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (result == NULL) {
1350 free(buf);
1351 return false;
1352 }
1353
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (username)
1354
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 *username = result->pw_name;
1355 16 free(buf);
1356 16 return true;
1357 }
1358
1359
1360 /**
1361 * Name -> UID from passwd database
1362 */
1363 257 bool GetUidOf(const std::string &username, uid_t *uid, gid_t *main_gid) {
1364 struct passwd pwd;
1365 257 struct passwd *result = NULL;
1366 257 int bufsize = 16 * 1024;
1367 257 char *buf = static_cast<char *>(smalloc(bufsize));
1368
2/4
✓ Branch 2 taken 257 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 257 times.
257 while (getpwnam_r(username.c_str(), &pwd, buf, bufsize, &result) == ERANGE) {
1369 bufsize *= 2;
1370 buf = static_cast<char *>(srealloc(buf, bufsize));
1371 }
1372
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 246 times.
257 if (result == NULL) {
1373 11 free(buf);
1374 11 return false;
1375 }
1376 246 *uid = result->pw_uid;
1377 246 *main_gid = result->pw_gid;
1378 246 free(buf);
1379 246 return true;
1380 }
1381
1382
1383 /**
1384 * Name -> GID from groups database
1385 */
1386 136 bool GetGidOf(const std::string &groupname, gid_t *gid) {
1387 struct group grp;
1388 136 struct group *result = NULL;
1389 136 int bufsize = 16 * 1024;
1390 136 char *buf = static_cast<char *>(smalloc(bufsize));
1391
2/4
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 136 times.
136 while (getgrnam_r(groupname.c_str(), &grp, buf, bufsize, &result) == ERANGE) {
1392 bufsize *= 2;
1393 buf = static_cast<char *>(srealloc(buf, bufsize));
1394 }
1395
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 115 times.
136 if (result == NULL) {
1396 21 free(buf);
1397 21 return false;
1398 }
1399 115 *gid = result->gr_gid;
1400 115 free(buf);
1401 115 return true;
1402 }
1403
1404 /**
1405 * read the current umask of this process
1406 * Note: umask query is guarded by a global mutex. Hence, always use
1407 * this function and beware of scalability bottlenecks
1408 */
1409 2500 mode_t GetUmask() {
1410 2500 const MutexLockGuard m(&getumask_mutex);
1411 2500 const mode_t my_umask = umask(0);
1412 2500 umask(my_umask);
1413 5000 return my_umask;
1414 2500 }
1415
1416
1417 /**
1418 * Adds gid to the list of supplementary groups
1419 */
1420 bool AddGroup2Persona(const gid_t gid) {
1421 const int ngroups = getgroups(0, NULL);
1422 if (ngroups < 0)
1423 return false;
1424 gid_t *groups = static_cast<gid_t *>(smalloc((ngroups + 1) * sizeof(gid_t)));
1425 int retval = getgroups(ngroups, groups);
1426 if (retval < 0) {
1427 free(groups);
1428 return false;
1429 }
1430 for (int i = 0; i < ngroups; ++i) {
1431 if (groups[i] == gid) {
1432 free(groups);
1433 return true;
1434 }
1435 }
1436 groups[ngroups] = gid;
1437 retval = setgroups(ngroups + 1, groups);
1438 free(groups);
1439 return retval == 0;
1440 }
1441
1442
1443 46 std::string GetHomeDirectory() {
1444 46 const uid_t uid = getuid();
1445 struct passwd pwd;
1446 46 struct passwd *result = NULL;
1447 46 int bufsize = 16 * 1024;
1448 46 char *buf = static_cast<char *>(smalloc(bufsize));
1449
2/4
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 46 times.
46 while (getpwuid_r(uid, &pwd, buf, bufsize, &result) == ERANGE) {
1450 bufsize *= 2;
1451 buf = static_cast<char *>(srealloc(buf, bufsize));
1452 }
1453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (result == NULL) {
1454 free(buf);
1455 return "";
1456 }
1457
1/2
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
46 std::string home_dir = result->pw_dir;
1458 46 free(buf);
1459 46 return home_dir;
1460 46 }
1461
1462 /**
1463 * Returns the output of `uname -m`
1464 */
1465 7 std::string GetArch() {
1466 struct utsname info;
1467 7 const int retval = uname(&info);
1468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 assert(retval == 0);
1469
1/2
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 return info.machine;
1470 }
1471
1472
1473 /**
1474 * Sets soft and hard limit for maximum number of open file descriptors.
1475 * Returns 0 on success, -1 on failure, -2 if running under valgrind.
1476 */
1477 23 int SetLimitNoFile(unsigned limit_nofile) {
1478 struct rlimit rpl;
1479 23 memset(&rpl, 0, sizeof(rpl));
1480 23 getrlimit(RLIMIT_NOFILE, &rpl);
1481
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11 times.
23 if (rpl.rlim_max < limit_nofile)
1482 12 rpl.rlim_max = limit_nofile;
1483 23 rpl.rlim_cur = limit_nofile;
1484 23 const int retval = setrlimit(RLIMIT_NOFILE, &rpl);
1485
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 12 times.
23 if (retval == 0)
1486 11 return 0;
1487
1488 #ifdef HAS_VALGRIND_HEADERS
1489
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 return RUNNING_ON_VALGRIND ? -2 : -1;
1490 #else
1491 return -1;
1492 #endif
1493 }
1494
1495
1496 /**
1497 * Get the file descriptor limits
1498 */
1499 502 void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit) {
1500 502 *soft_limit = 0;
1501 502 *hard_limit = 0;
1502
1503 struct rlimit rpl;
1504 502 memset(&rpl, 0, sizeof(rpl));
1505 502 getrlimit(RLIMIT_NOFILE, &rpl);
1506 502 *soft_limit = rpl.rlim_cur;
1507
1508 #ifdef __APPLE__
1509 int value = sysconf(_SC_OPEN_MAX);
1510 assert(value > 0);
1511 *hard_limit = value;
1512 #else
1513 502 *hard_limit = rpl.rlim_max;
1514 #endif
1515 502 }
1516
1517
1518 7 std::vector<LsofEntry> Lsof(const std::string &path) {
1519 7 std::vector<LsofEntry> result;
1520
1521 7 std::vector<std::string> proc_names;
1522 7 std::vector<mode_t> proc_modes;
1523
2/4
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7 times.
✗ Branch 6 not taken.
7 ListDirectory("/proc", &proc_names, &proc_modes);
1524
1525
2/2
✓ Branch 1 taken 455 times.
✓ Branch 2 taken 7 times.
462 for (unsigned i = 0; i < proc_names.size(); ++i) {
1526
2/2
✓ Branch 1 taken 343 times.
✓ Branch 2 taken 112 times.
455 if (!S_ISDIR(proc_modes[i]))
1527 413 continue;
1528
2/2
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 42 times.
112 if (proc_names[i].find_first_not_of("1234567890") != std::string::npos)
1529 70 continue;
1530
1531 42 std::vector<std::string> fd_names;
1532 42 std::vector<mode_t> fd_modes;
1533
1/2
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 const std::string proc_dir = "/proc/" + proc_names[i];
1534
1/2
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
42 const std::string fd_dir = proc_dir + "/fd";
1535
1/2
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
42 const bool rvb = ListDirectory(fd_dir, &fd_names, &fd_modes);
1536 42 uid_t proc_uid = 0;
1537
1538 // The working directory of the process requires special handling
1539
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 7 times.
42 if (rvb) {
1540 platform_stat64 info;
1541 35 platform_stat(proc_dir.c_str(), &info);
1542 35 proc_uid = info.st_uid;
1543
1544
2/4
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
35 const std::string cwd = ReadSymlink(proc_dir + "/cwd");
1545
5/9
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 35 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 7 times.
✓ Branch 12 taken 28 times.
35 if (HasPrefix(cwd + "/", path + "/", false /* ignore_case */)) {
1546 7 LsofEntry entry;
1547
1/2
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 entry.pid = static_cast<pid_t>(String2Uint64(proc_names[i]));
1548 7 entry.owner = proc_uid;
1549 7 entry.read_only = true; // A bit sloppy but good enough for the moment
1550
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 entry.executable = ReadSymlink(proc_dir + "/exe");
1551
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 entry.path = cwd;
1552
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 result.push_back(entry);
1553 7 }
1554 35 }
1555
1556
2/2
✓ Branch 1 taken 301 times.
✓ Branch 2 taken 42 times.
343 for (unsigned j = 0; j < fd_names.size(); ++j) {
1557
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 301 times.
301 if (!S_ISLNK(fd_modes[j]))
1558 203 continue;
1559
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 301 times.
301 if (fd_names[j].find_first_not_of("1234567890") != std::string::npos)
1560 continue;
1561
1562
3/6
✓ Branch 2 taken 301 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 301 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 301 times.
✗ Branch 9 not taken.
602 const std::string target = ReadSymlink(fd_dir + "/" + fd_names[j]);
1563
5/9
✓ Branch 1 taken 301 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 301 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 301 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 203 times.
✓ Branch 12 taken 98 times.
301 if (!HasPrefix(target + "/", path + "/", false /* ignore_case */))
1564 203 continue;
1565
1566 98 LsofEntry entry;
1567
1/2
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
98 entry.pid = static_cast<pid_t>(String2Uint64(proc_names[i]));
1568 98 entry.owner = proc_uid;
1569 98 entry.read_only = !((fd_modes[j] & S_IWUSR) == S_IWUSR);
1570
2/4
✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
98 entry.executable = ReadSymlink(proc_dir + "/exe");
1571
1/2
✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
98 entry.path = target;
1572
1/2
✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
98 result.push_back(entry);
1573
2/2
✓ Branch 2 taken 98 times.
✓ Branch 3 taken 203 times.
301 }
1574 42 }
1575
1576 14 return result;
1577 7 }
1578
1579
1580 36 bool ProcessExists(pid_t pid) {
1581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 assert(pid > 0);
1582 36 const int retval = kill(pid, 0);
1583
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
36 if (retval == 0)
1584 24 return true;
1585 12 return (errno != ESRCH);
1586 }
1587
1588
1589 /**
1590 * Blocks a signal for the calling thread.
1591 */
1592 34 void BlockSignal(int signum) {
1593 sigset_t sigset;
1594 34 int retval = sigemptyset(&sigset);
1595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 assert(retval == 0);
1596 34 retval = sigaddset(&sigset, signum);
1597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 assert(retval == 0);
1598 34 retval = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 assert(retval == 0);
1600 34 }
1601
1602
1603 /**
1604 * Waits for a signal. The signal should be blocked before for all threads.
1605 * Threads inherit their parent's signal mask.
1606 */
1607 12 void WaitForSignal(int signum) {
1608 int retval;
1609 do {
1610 12 retval = platform_sigwait(signum);
1611
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12 } while ((retval != signum) && (errno == EINTR));
1612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 assert(retval == signum);
1613 12 }
1614
1615
1616 /**
1617 * Returns -1 if the child crashed or the exit code otherwise.
1618 * @param pid Process identifier.
1619 * @param sig_ok List of signals that are still considered a successful
1620 * termination.
1621 */
1622 98 int WaitForChild(pid_t pid, const std::vector<int> &sig_ok) {
1623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 assert(pid > 0);
1624 int statloc;
1625 while (true) {
1626
1/2
✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
98 const pid_t retval = waitpid(pid, &statloc, 0);
1627
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 82 times.
98 if (retval == -1) {
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (errno == EINTR)
1629 continue;
1630 16 PANIC(kLogSyslogErr | kLogDebug, "waitpid failed with errno %d", errno);
1631 }
1632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 assert(retval == pid);
1633 82 break;
1634 }
1635
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 16 times.
82 if (WIFEXITED(statloc))
1636 66 return WEXITSTATUS(statloc);
1637 32 if (WIFSIGNALED(statloc)
1638
3/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 5 taken 16 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 16 times.
48 && (std::find(sig_ok.begin(), sig_ok.end(), WTERMSIG(statloc))
1639
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
48 != sig_ok.end()))
1640 return 0;
1641 16 return -1;
1642 }
1643
1644 /**
1645 * Exec a command as a daemon.
1646 */
1647
1648 bool ExecAsDaemon(const std::vector<std::string> &command_line,
1649 pid_t *child_pid) {
1650 assert(command_line.size() >= 1);
1651
1652 Pipe<kPipeDetachedChild> pipe_fork;
1653 const pid_t pid = fork();
1654 assert(pid >= 0);
1655 if (pid == 0) {
1656 pid_t pid_grand_child;
1657
1658 const char *argv[command_line.size() + 1];
1659 for (unsigned i = 0; i < command_line.size(); ++i)
1660 argv[i] = command_line[i].c_str();
1661 argv[command_line.size()] = NULL;
1662 int retval = setsid();
1663 assert(retval != -1);
1664
1665 pid_grand_child = fork();
1666 assert(pid_grand_child >= 0);
1667
1668 if (pid_grand_child != 0) {
1669 pipe_fork.Write<pid_t>(pid_grand_child);
1670 _exit(0);
1671 } else {
1672 const int null_read = open("/dev/null", O_RDONLY);
1673 const int null_write = open("/dev/null", O_WRONLY);
1674 assert((null_read >= 0) && (null_write >= 0));
1675 retval = dup2(null_read, 0);
1676 assert(retval == 0);
1677 retval = dup2(null_write, 1);
1678 assert(retval == 1);
1679 retval = dup2(null_write, 2);
1680 assert(retval == 2);
1681 close(null_read);
1682 close(null_write);
1683
1684 execvp(command_line[0].c_str(), const_cast<char **>(argv));
1685
1686 pipe_fork.CloseWriteFd();
1687 }
1688 }
1689 int statloc;
1690 waitpid(pid, &statloc, 0);
1691 pid_t buf_child_pid = 0;
1692 pipe_fork.Read(&buf_child_pid);
1693 if (child_pid != NULL)
1694 *child_pid = buf_child_pid;
1695 pipe_fork.CloseReadFd();
1696
1697 LogCvmfs(kLogCvmfs, kLogDebug, "exec'd as daemon %s (PID: %d)",
1698 command_line[0].c_str(), static_cast<int>(*child_pid));
1699 return true;
1700 }
1701
1702
1703 /**
1704 * Makes a daemon. The daemon() call is deprecated on OS X
1705 */
1706 void Daemonize() {
1707 pid_t pid;
1708 int statloc;
1709 if ((pid = fork()) == 0) {
1710 int retval = setsid();
1711 assert(retval != -1);
1712 if ((pid = fork()) == 0) {
1713
1/2
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
49 const int null_read = open("/dev/null", O_RDONLY);
1714
1/2
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
49 const int null_write = open("/dev/null", O_WRONLY);
1715
2/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
49 assert((null_read >= 0) && (null_write >= 0));
1716 49 retval = dup2(null_read, 0);
1717
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 assert(retval == 0);
1718 49 retval = dup2(null_write, 1);
1719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 assert(retval == 1);
1720 49 retval = dup2(null_write, 2);
1721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 assert(retval == 2);
1722
1/2
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
49 close(null_read);
1723
1/2
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
49 close(null_write);
1724
1/2
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
49 LogCvmfs(kLogCvmfs, kLogDebug, "daemonized");
1725 } else {
1726 assert(pid > 0);
1727 _exit(0);
1728 }
1729 } else {
1730 assert(pid > 0);
1731 waitpid(pid, &statloc, 0);
1732 _exit(0);
1733 }
1734 49 }
1735
1736
1737 678 bool ExecuteBinary(int *fd_stdin,
1738 int *fd_stdout,
1739 int *fd_stderr,
1740 const std::string &binary_path,
1741 const std::vector<std::string> &argv,
1742 const bool double_fork,
1743 pid_t *child_pid) {
1744 int pipe_stdin[2];
1745 int pipe_stdout[2];
1746 int pipe_stderr[2];
1747
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 MakePipe(pipe_stdin);
1748
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 MakePipe(pipe_stdout);
1749
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 MakePipe(pipe_stderr);
1750
1751 678 std::set<int> preserve_fildes;
1752
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 preserve_fildes.insert(0);
1753
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 preserve_fildes.insert(1);
1754
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 preserve_fildes.insert(2);
1755 678 std::map<int, int> map_fildes;
1756
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 map_fildes[pipe_stdin[0]] = 0; // Reading end of pipe_stdin
1757
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 map_fildes[pipe_stdout[1]] = 1; // Writing end of pipe_stdout
1758
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 map_fildes[pipe_stderr[1]] = 2; // Writing end of pipe_stderr
1759 678 std::vector<std::string> cmd_line;
1760
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 cmd_line.push_back(binary_path);
1761
1/2
✓ Branch 5 taken 678 times.
✗ Branch 6 not taken.
678 cmd_line.insert(cmd_line.end(), argv.begin(), argv.end());
1762
1763
2/4
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 678 times.
678 if (!ManagedExec(cmd_line,
1764 preserve_fildes,
1765 map_fildes,
1766 true /* drop_credentials */,
1767 false /* clear_env */,
1768 double_fork,
1769 child_pid)) {
1770 ClosePipe(pipe_stdin);
1771 ClosePipe(pipe_stdout);
1772 ClosePipe(pipe_stderr);
1773 return false;
1774 }
1775
1776
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 close(pipe_stdin[0]);
1777
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 close(pipe_stdout[1]);
1778
1/2
✓ Branch 1 taken 678 times.
✗ Branch 2 not taken.
678 close(pipe_stderr[1]);
1779 678 *fd_stdin = pipe_stdin[1];
1780 678 *fd_stdout = pipe_stdout[0];
1781 678 *fd_stderr = pipe_stderr[0];
1782 678 return true;
1783 678 }
1784
1785
1786 /**
1787 * Opens /bin/sh and provides file descriptors to write into stdin and
1788 * read from stdout. Quit shell simply by closing stderr, stdout, and stdin.
1789 */
1790 628 bool Shell(int *fd_stdin, int *fd_stdout, int *fd_stderr) {
1791 628 const bool double_fork = true;
1792
2/4
✓ Branch 2 taken 628 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 628 times.
✗ Branch 6 not taken.
1256 return ExecuteBinary(fd_stdin, fd_stdout, fd_stderr, "/bin/sh",
1793 1884 std::vector<std::string>(), double_fork);
1794 }
1795
1796 struct ForkFailures { // TODO(rmeusel): C++11 (type safe enum)
1797 enum Names {
1798 kSendPid,
1799 kUnknown,
1800 kFailDupFd,
1801 kFailCloseFds,
1802 kFailGetFdFlags,
1803 kFailSetFdFlags,
1804 kFailDropCredentials,
1805 kFailExec,
1806 };
1807
1808 static std::string ToString(const Names name) {
1809 switch (name) {
1810 case kSendPid:
1811 return "Sending PID";
1812
1813 default:
1814 case kUnknown:
1815 return "Unknown Status";
1816 case kFailDupFd:
1817 return "Duplicate File Descriptor";
1818 case kFailCloseFds:
1819 return "Close File Descriptors";
1820 case kFailGetFdFlags:
1821 return "Read File Descriptor Flags";
1822 case kFailSetFdFlags:
1823 return "Set File Descriptor Flags";
1824 case kFailDropCredentials:
1825 return "Lower User Permissions";
1826 case kFailExec:
1827 return "Invoking execvp()";
1828 }
1829 }
1830 };
1831
1832 /**
1833 * Loop through all possible FDs and close them.
1834 */
1835 static bool CloseAllFildesUntilMaxFD(const std::set<int> &preserve_fildes,
1836 int max_fd) {
1837 for (int fd = 0; fd < max_fd; fd++) {
1838 if (preserve_fildes.count(fd) == 0) {
1839 close(fd);
1840 }
1841 }
1842
1843 return true;
1844 }
1845
1846 /**
1847 * Loop through /proc/self/fd and close the listed FDs.
1848 * Not used on macOS.
1849 */
1850 #ifndef __APPLE__
1851 51 static bool CloseAllFildesInProcSelfFd(const std::set<int> &preserve_fildes) {
1852 51 DIR *dirp = opendir("/proc/self/fd");
1853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (!dirp)
1854 return false;
1855
1856 platform_dirent64 *dirent;
1857
1858
2/2
✓ Branch 1 taken 1158 times.
✓ Branch 2 taken 51 times.
1209 while ((dirent = platform_readdir(dirp))) {
1859
1/2
✓ Branch 2 taken 1158 times.
✗ Branch 3 not taken.
1158 const std::string name(dirent->d_name);
1860 uint64_t name_uint64;
1861
1862 // Make sure the dir name is digits only (skips ".", ".." and similar).
1863
3/4
✓ Branch 1 taken 1158 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 102 times.
✓ Branch 4 taken 1056 times.
1158 if (!String2Uint64Parse(name, &name_uint64)) {
1864 102 continue;
1865 }
1866
1867 1056 const int fd = static_cast<int>(name_uint64);
1868
3/4
✓ Branch 1 taken 1056 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 253 times.
✓ Branch 4 taken 803 times.
1056 if (preserve_fildes.count(fd)) {
1869 253 continue;
1870 }
1871
1872
1/2
✓ Branch 1 taken 803 times.
✗ Branch 2 not taken.
803 close(fd);
1873
2/2
✓ Branch 1 taken 803 times.
✓ Branch 2 taken 355 times.
1158 }
1874
1875 51 closedir(dirp);
1876
1877 51 return true;
1878 }
1879 #endif
1880
1881 /**
1882 * Closes all file descriptors except the ones in preserve_fildes.
1883 * To be used after fork but before exec.
1884 */
1885 51 bool CloseAllFildes(const std::set<int> &preserve_fildes) {
1886 51 const int max_fd = static_cast<int>(sysconf(_SC_OPEN_MAX));
1887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (max_fd < 0) {
1888 return false;
1889 }
1890
1891 #ifdef __APPLE__
1892 return CloseAllFildesUntilMaxFD(preserve_fildes, max_fd);
1893 #else // ifdef __APPLE__
1894
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (max_fd > 100000) {
1895 // CloseAllFildesUntilMaxFD is inefficient with very large max_fd.
1896 // Looping through /proc/self/fd performs better.
1897 51 return CloseAllFildesInProcSelfFd(preserve_fildes);
1898 }
1899
1900 return CloseAllFildesUntilMaxFD(preserve_fildes, max_fd);
1901 #endif // #ifdef __APPLE__
1902 }
1903
1904 /**
1905 * Execve to the given command line, preserving the given file descriptors.
1906 * If stdin, stdout, stderr should be preserved, add 0, 1, 2.
1907 * File descriptors from the parent process can also be mapped to the new
1908 * process (dup2) using map_fildes. Can be useful for
1909 * stdout/in/err redirection.
1910 * NOTE: The destination fildes have to be preserved!
1911 * NOTE: For forked child, held mutexes have invalid state, avoid operations.
1912 * Does a double fork to detach child.
1913 * The command_line parameter contains the binary at index 0 and the arguments
1914 * in the rest of the vector.
1915 * Using the optional parameter *pid it is possible to retrieve the process ID
1916 * of the spawned process.
1917 */
1918 729 bool ManagedExec(const std::vector<std::string> &command_line,
1919 const std::set<int> &preserve_fildes,
1920 const std::map<int, int> &map_fildes,
1921 const bool drop_credentials,
1922 const bool clear_env,
1923 const bool double_fork,
1924 pid_t *child_pid) {
1925
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 729 times.
729 assert(command_line.size() >= 1);
1926
1927
1/2
✓ Branch 1 taken 729 times.
✗ Branch 2 not taken.
729 Pipe<kPipeDetachedChild> pipe_fork;
1928 729 const pid_t pid = fork();
1929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 731 times.
731 assert(pid >= 0);
1930
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 729 times.
731 if (pid == 0) {
1931 pid_t pid_grand_child;
1932 int fd_flags;
1933 2 ForkFailures::Names failed = ForkFailures::kUnknown;
1934
1935 2 std::set<int> skip_fds = preserve_fildes;
1936
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 skip_fds.insert(pipe_fork.GetWriteFd());
1937
1938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (clear_env) {
1939 #ifdef __APPLE__
1940 environ = NULL;
1941 #else
1942 const int retval = clearenv();
1943 assert(retval == 0);
1944 #endif
1945 }
1946
1947 2 const char *argv[command_line.size() + 1];
1948
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
5 for (unsigned i = 0; i < command_line.size(); ++i)
1949 3 argv[i] = command_line[i].c_str();
1950 2 argv[command_line.size()] = NULL;
1951
1952 // Child, map file descriptors
1953 4 for (std::map<int, int>::const_iterator i = map_fildes.begin(),
1954 2 iEnd = map_fildes.end();
1955
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
8 i != iEnd;
1956 6 ++i) {
1957 6 const int retval = dup2(i->first, i->second);
1958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (retval == -1) {
1959 failed = ForkFailures::kFailDupFd;
1960 goto fork_failure;
1961 }
1962 }
1963
1964 // Child, close file descriptors
1965
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if (!CloseAllFildes(skip_fds)) {
1966 failed = ForkFailures::kFailCloseFds;
1967 goto fork_failure;
1968 }
1969
1970 // Double fork to disconnect from parent
1971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (double_fork) {
1972 pid_grand_child = fork();
1973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 assert(pid_grand_child >= 0);
1974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (pid_grand_child != 0)
1975 _exit(0);
1976 }
1977
1978
1/2
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
29 fd_flags = fcntl(pipe_fork.GetWriteFd(), F_GETFD);
1979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (fd_flags < 0) {
1980 failed = ForkFailures::kFailGetFdFlags;
1981 goto fork_failure;
1982 }
1983 29 fd_flags |= FD_CLOEXEC;
1984
2/4
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
29 if (fcntl(pipe_fork.GetWriteFd(), F_SETFD, fd_flags) < 0) {
1985 failed = ForkFailures::kFailSetFdFlags;
1986 goto fork_failure;
1987 }
1988
1989 #ifdef DEBUGMSG
1990
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 assert(setenv("__CVMFS_DEBUG_MODE__", "yes", 1) == 0);
1991 #endif
1992
5/8
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1 times.
✓ Branch 5 taken 28 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 28 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 29 times.
29 if (drop_credentials && !SwitchCredentials(geteuid(), getegid(), false, true)) {
1993 failed = ForkFailures::kFailDropCredentials;
1994 goto fork_failure;
1995 }
1996
1997 // retrieve the PID of the new (grand) child process and send it to the
1998 // grand father
1999 29 pid_grand_child = getpid();
2000 29 failed = ForkFailures::kSendPid;
2001
1/2
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
29 pipe_fork.Write<ForkFailures::Names>(failed);
2002
1/2
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
29 pipe_fork.Write<pid_t>(pid_grand_child);
2003
2004 29 execvp(command_line[0].c_str(), const_cast<char **>(argv));
2005
2006 failed = ForkFailures::kFailExec;
2007
2008 fork_failure:
2009 pipe_fork.Write<ForkFailures::Names>(failed);
2010 _exit(1);
2011
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 }
2012
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 33 times.
729 if (double_fork) {
2013 int statloc;
2014
1/2
✓ Branch 1 taken 696 times.
✗ Branch 2 not taken.
696 waitpid(pid, &statloc, 0);
2015 }
2016
2017
1/2
✓ Branch 1 taken 729 times.
✗ Branch 2 not taken.
729 pipe_fork.CloseWriteFd();
2018
2019 // Either the PID or a return value is sent
2020 ForkFailures::Names status_code;
2021
1/2
✓ Branch 1 taken 729 times.
✗ Branch 2 not taken.
729 pipe_fork.Read<ForkFailures::Names>(&status_code);
2022
2023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 729 times.
729 if (status_code != ForkFailures::kSendPid) {
2024 pipe_fork.CloseReadFd();
2025 LogCvmfs(kLogCvmfs, kLogDebug, "managed execve failed (%s)",
2026 ForkFailures::ToString(status_code).c_str());
2027 return false;
2028 }
2029
2030 // read the PID of the spawned process if requested
2031 // (the actual read needs to be done in any case!)
2032 729 pid_t buf_child_pid = 0;
2033
1/2
✓ Branch 1 taken 729 times.
✗ Branch 2 not taken.
729 pipe_fork.Read(&buf_child_pid);
2034
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 628 times.
729 if (child_pid != NULL)
2035 101 *child_pid = buf_child_pid;
2036
1/2
✓ Branch 1 taken 729 times.
✗ Branch 2 not taken.
729 pipe_fork.CloseReadFd();
2037
1/2
✓ Branch 2 taken 729 times.
✗ Branch 3 not taken.
729 LogCvmfs(kLogCvmfs, kLogDebug, "execve'd %s (PID: %d)",
2038 729 command_line[0].c_str(), static_cast<int>(buf_child_pid));
2039 729 return true;
2040 729 }
2041
2042
2043 /**
2044 * Sleeps using select. This is without signals and doesn't interfere with
2045 * other uses of the ALRM signal.
2046 */
2047 2284 void SafeSleepMs(const unsigned ms) {
2048 struct timeval wait_for;
2049 2284 wait_for.tv_sec = ms / 1000;
2050 2284 wait_for.tv_usec = (ms % 1000) * 1000;
2051
2/2
✓ Branch 1 taken 2240 times.
✓ Branch 2 taken 44 times.
2284 select(0, NULL, NULL, NULL, &wait_for);
2052 2240 }
2053
2054
2055 /**
2056 * Deal with EINTR and partial writes.
2057 */
2058 5747 bool SafeWrite(int fd, const void *buf, size_t nbyte) {
2059
2/2
✓ Branch 0 taken 5747 times.
✓ Branch 1 taken 5714 times.
11461 while (nbyte) {
2060 5747 const ssize_t retval = write(fd, buf, nbyte);
2061
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 5714 times.
5747 if (retval < 0) {
2062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (errno == EINTR)
2063 continue;
2064 33 return false;
2065 }
2066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5714 times.
5714 assert(static_cast<size_t>(retval) <= nbyte);
2067 5714 buf = reinterpret_cast<const char *>(buf) + retval;
2068 5714 nbyte -= retval;
2069 }
2070 5714 return true;
2071 }
2072
2073 /**
2074 * The contents of the iov vector might be modified by the function.
2075 */
2076 185083 bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt) {
2077 185083 unsigned nbytes = 0;
2078
2/2
✓ Branch 0 taken 521491 times.
✓ Branch 1 taken 185083 times.
706574 for (unsigned i = 0; i < iovcnt; ++i)
2079 521491 nbytes += iov[i].iov_len;
2080 185083 unsigned iov_idx = 0;
2081
2082
2/2
✓ Branch 0 taken 185083 times.
✓ Branch 1 taken 26 times.
185109 while (nbytes) {
2083 370166 const ssize_t retval = writev(fd, &iov[iov_idx],
2084 185083 static_cast<int>(iovcnt - iov_idx));
2085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185083 times.
185083 if (retval < 0) {
2086 if (errno == EINTR)
2087 continue;
2088 return false;
2089 }
2090
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185083 times.
185083 assert(static_cast<size_t>(retval) <= nbytes);
2091 185083 nbytes -= retval;
2092
2093 185083 unsigned sum_written_blocks = 0;
2094 185083 while ((sum_written_blocks + iov[iov_idx].iov_len)
2095
2/2
✓ Branch 0 taken 521387 times.
✓ Branch 1 taken 26 times.
521413 <= static_cast<size_t>(retval)) {
2096 521387 sum_written_blocks += iov[iov_idx].iov_len;
2097 521387 iov_idx++;
2098
2/2
✓ Branch 0 taken 185057 times.
✓ Branch 1 taken 336330 times.
521387 if (iov_idx == iovcnt) {
2099
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185057 times.
185057 assert(sum_written_blocks == static_cast<size_t>(retval));
2100 185057 return true;
2101 }
2102 }
2103 26 const unsigned offset = retval - sum_written_blocks;
2104 26 iov[iov_idx].iov_len -= offset;
2105 26 iov[iov_idx].iov_base = reinterpret_cast<char *>(iov[iov_idx].iov_base)
2106 26 + offset;
2107 }
2108
2109 26 return true;
2110 }
2111
2112
2113 /**
2114 * Deal with EINTR and partial reads.
2115 */
2116 22102694 ssize_t SafeRead(int fd, void *buf, size_t nbyte) {
2117 22102694 ssize_t total_bytes = 0;
2118
2/2
✓ Branch 0 taken 22396229 times.
✓ Branch 1 taken 22062559 times.
44458788 while (nbyte) {
2119 22396229 const ssize_t retval = read(fd, buf, nbyte);
2120
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22411183 times.
22411205 if (retval < 0) {
2121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (errno == EINTR)
2122 continue;
2123 22 return -1;
2124
2/2
✓ Branch 0 taken 55089 times.
✓ Branch 1 taken 22356094 times.
22411183 } else if (retval == 0) {
2125 55089 return total_bytes;
2126 }
2127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22356094 times.
22356094 assert(static_cast<size_t>(retval) <= nbyte);
2128 22356094 buf = reinterpret_cast<char *>(buf) + retval;
2129 22356094 nbyte -= retval;
2130 22356094 total_bytes += retval;
2131 }
2132 22062559 return total_bytes;
2133 }
2134
2135
2136 /**
2137 * Pull file contents into a string
2138 */
2139 1378 bool SafeReadToString(int fd, std::string *final_result) {
2140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1378 times.
1378 if (!final_result) {
2141 return false;
2142 }
2143
2144 1378 std::string tmp_result;
2145 static const int buf_size = 4096;
2146 char buf[4096];
2147 1378 ssize_t total_bytes = -1;
2148 do {
2149
1/2
✓ Branch 1 taken 15982 times.
✗ Branch 2 not taken.
15982 total_bytes = SafeRead(fd, buf, buf_size);
2150
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 15971 times.
15982 if (total_bytes < 0) {
2151 11 return false;
2152 }
2153
1/2
✓ Branch 1 taken 15971 times.
✗ Branch 2 not taken.
15971 tmp_result.append(buf, total_bytes);
2154
2/2
✓ Branch 0 taken 14604 times.
✓ Branch 1 taken 1367 times.
15971 } while (total_bytes == buf_size);
2155 1367 final_result->swap(tmp_result);
2156 1367 return true;
2157 1378 }
2158
2159 3937 bool SafeWriteToFile(const std::string &content,
2160 const std::string &path,
2161 int mode) {
2162 3937 const int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode);
2163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3937 times.
3937 if (fd < 0)
2164 return false;
2165 3937 const bool retval = SafeWrite(fd, content.data(), content.size());
2166 3937 close(fd);
2167 3937 return retval;
2168 }
2169
2170
2171 #ifdef CVMFS_NAMESPACE_GUARD
2172 } // namespace CVMFS_NAMESPACE_GUARD
2173 #endif
2174