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