7 #ifndef __STDC_FORMAT_MACROS
9 #define __STDC_FORMAT_MACROS
12 #include "cvmfs_config.h"
15 #include <arpa/inet.h>
20 #include <netinet/in.h>
25 #include <sys/resource.h>
26 #include <sys/socket.h>
29 #include <sys/mount.h>
31 #include <sys/statfs.h>
34 #include <sys/types.h>
40 #ifdef HAS_VALGRIND_HEADERS
41 #include <valgrind/valgrind.h>
53 #include "fs_traversal.h"
70 #define CVMFS_HAS_STATFS_F_FLAGS
72 #ifdef __GLIBC_MINOR__
73 #if __GLIBC_MINOR__ < 12
74 #undef CVMFS_HAS_STATFS_F_FLAGS
82 extern char **environ;
86 #ifdef CVMFS_NAMESPACE_GUARD
87 namespace CVMFS_NAMESPACE_GUARD {
97 if (path.length() == 0)
return path;
99 if (path[path.length()-1] ==
'/') {
100 return path.substr(0, path.length()-1);
112 const std::string &path,
113 std::string *dirname,
114 std::string *filename)
116 size_t dir_sep = path.rfind(
'/');
117 if (dir_sep != std::string::npos) {
118 *dirname = path.substr(0, dir_sep);
119 *filename = path.substr(dir_sep+1);
131 const std::string::size_type idx = path.find_last_of(
'/');
132 if (idx != std::string::npos) {
133 return path.substr(0, idx);
144 int length =
static_cast<int>(path.
GetLength());
147 const char *chars = path.
GetChars();
149 for (
int i = length-1; i >= 0; --i) {
162 const std::string::size_type idx = path.find_last_of(
'/');
163 if (idx != std::string::npos) {
164 return path.substr(idx+1);
173 int length =
static_cast<int>(path.
GetLength());
174 const char *chars = path.
GetChars();
177 for (i = length-1; i >= 0; --i) {
183 name.
Append(chars+i, length-i);
191 return (!path.empty() && path[0] ==
'/');
204 if (path.length() < 7) {
208 std::string prefix = path.substr(0, 8);
209 std::transform(prefix.begin(), prefix.end(), prefix.begin(), ::tolower);
211 return prefix.substr(0, 7) ==
"http://" || prefix ==
"https://";
219 int retval = statfs(path.c_str(), &info);
223 switch (info.f_type) {
240 #ifdef CVMFS_HAS_STATFS_F_FLAGS
245 retval = access(path.c_str(), W_OK);
257 char buf[PATH_MAX + 1];
258 ssize_t nchars = readlink(path.c_str(), buf, PATH_MAX);
261 return std::string(buf);
272 if (path.empty() || (path ==
"/"))
275 std::string result = name;
279 result = parent + (parent ==
"/" ?
"" :
"/") + name;
281 char *real_result = realpath(result.c_str(), NULL);
283 result = real_result;
287 char buf[PATH_MAX + 1];
288 ssize_t nchars = readlink(result.c_str(), buf, PATH_MAX);
301 for (
unsigned i = 0; i < mount_list.size(); ++i) {
302 if (mount_list[i] == resolved_path)
313 const std::string &path,
315 const bool ignore_failure)
317 int fd = open(path.c_str(), O_CREAT, mode);
332 struct sockaddr_un sock_addr;
333 unsigned max_length =
sizeof(sock_addr.sun_path);
337 if (tmp_path.empty())
339 std::string link = tmp_path +
"/l";
341 if (result.length() >= max_length) {
342 rmdir(tmp_path.c_str());
345 int retval = symlink(
GetParentPath(path).c_str(), link.c_str());
347 rmdir(tmp_path.c_str());
355 unlink(link.c_str());
364 std::string short_path(path);
365 struct sockaddr_un sock_addr;
366 if (path.length() >=
sizeof(sock_addr.sun_path)) {
370 if (short_path.empty())
373 sock_addr.sun_family = AF_UNIX;
374 strncpy(sock_addr.sun_path, short_path.c_str(),
375 sizeof(sock_addr.sun_path));
377 const int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
383 if (fchmod(socket_fd, mode) != 0)
384 goto make_socket_failure;
387 if (bind(socket_fd, reinterpret_cast<struct sockaddr *>(&sock_addr),
388 sizeof(sock_addr.sun_family) +
sizeof(sock_addr.sun_path)) < 0)
390 if ((errno == EADDRINUSE) && (unlink(path.c_str()) == 0)) {
392 if (bind(socket_fd, reinterpret_cast<struct sockaddr *>(&sock_addr),
393 sizeof(sock_addr.sun_family) +
sizeof(sock_addr.sun_path)) < 0)
396 goto make_socket_failure;
400 goto make_socket_failure;
404 if (short_path != path)
411 if (short_path != path)
422 const int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
425 int retval = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on));
428 struct sockaddr_in endpoint_addr;
429 memset(&endpoint_addr, 0,
sizeof(endpoint_addr));
430 endpoint_addr.sin_family = AF_INET;
431 if (ipv4_address.empty()) {
432 endpoint_addr.sin_addr.s_addr = INADDR_ANY;
434 retval = inet_aton(ipv4_address.c_str(), &(endpoint_addr.sin_addr));
441 endpoint_addr.sin_port = htons(portno);
443 retval = bind(socket_fd, reinterpret_cast<struct sockaddr *>(&endpoint_addr),
444 sizeof(endpoint_addr));
460 std::string short_path(path);
461 struct sockaddr_un sock_addr;
462 if (path.length() >=
sizeof(sock_addr.sun_path)) {
466 if (short_path.empty())
469 sock_addr.sun_family = AF_UNIX;
470 strncpy(sock_addr.sun_path, short_path.c_str(),
sizeof(sock_addr.sun_path));
472 const int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
476 connect(socket_fd, reinterpret_cast<struct sockaddr *>(&sock_addr),
477 sizeof(sock_addr.sun_family) +
sizeof(sock_addr.sun_path));
478 if (short_path != path)
494 const int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
497 struct sockaddr_in endpoint_addr;
498 memset(&endpoint_addr, 0,
sizeof(endpoint_addr));
499 endpoint_addr.sin_family = AF_INET;
500 int retval = inet_aton(ipv4_address.c_str(), &(endpoint_addr.sin_addr));
506 endpoint_addr.sin_port = htons(portno);
509 connect(socket_fd, reinterpret_cast<struct sockaddr *>(&endpoint_addr),
510 sizeof(endpoint_addr));
525 int retval = pipe(pipe_fd);
536 num_bytes = write(fd, buf, nbyte);
537 }
while ((num_bytes < 0) && (errno == EINTR));
538 assert((num_bytes >= 0) && (static_cast<size_t>(num_bytes) == nbyte));
548 num_bytes = read(fd, buf, nbyte);
549 }
while ((num_bytes < 0) && (errno == EINTR));
550 assert((num_bytes >= 0) && (static_cast<size_t>(num_bytes) == nbyte));
560 unsigned backoff_ms = 1;
561 const unsigned max_backoff_ms = 256;
565 num_bytes = read(fd, buf, nbyte);
566 if ((num_bytes < 0) && (errno == EINTR))
570 if ((i > 3000) && (num_bytes == 0)) {
573 if (backoff_ms < max_backoff_ms) backoff_ms *= 2;
575 }
while (num_bytes == 0);
576 assert((num_bytes >= 0) && (static_cast<size_t>(num_bytes) == nbyte));
593 bool DiffTree(
const std::string &path_a,
const std::string &path_b) {
595 std::vector<std::string> ls_a;
596 std::vector<std::string> ls_b;
597 std::vector<std::string> subdirs;
599 DIR *dirp_a = opendir(path_a.c_str());
600 if (dirp_a == NULL)
return false;
601 DIR *dirp_b = opendir(path_b.c_str());
602 if (dirp_b == NULL) {
609 const std::string name(dirent->d_name);
610 if ((name ==
".") || (name ==
".."))
612 const std::string path = path_a +
"/" + name;
613 ls_a.push_back(path);
622 if (S_ISDIR(info.st_mode)) subdirs.push_back(name);
625 const std::string name(dirent->d_name);
626 if ((name ==
".") || (name ==
".."))
628 const std::string path = path_b +
"/" + name;
629 ls_b.push_back(path);
634 sort(ls_a.begin(), ls_a.end());
635 sort(ls_b.begin(), ls_b.end());
636 if (ls_a.size() != ls_b.size())
638 for (
unsigned i = 0; i < ls_a.size(); ++i) {
643 if (retval != 0)
return false;
645 if (retval != 0)
return false;
646 if ((info_a.st_mode != info_b.st_mode) ||
647 (info_a.st_uid != info_b.st_uid) ||
648 (info_a.st_gid != info_b.st_gid) ||
649 (info_a.st_size != info_b.st_size))
655 for (
unsigned i = 0; i < subdirs.size(); ++i) {
656 bool retval_subtree =
DiffTree(path_a +
"/" + subdirs[i],
657 path_b +
"/" + subdirs[i]);
658 if (!retval_subtree)
return false;
669 int flags = fcntl(filedes, F_GETFL);
671 int retval = fcntl(filedes, F_SETFL, flags & ~O_NONBLOCK);
680 int flags = fcntl(filedes, F_GETFL);
682 int retval = fcntl(filedes, F_SETFL, flags | O_NONBLOCK);
703 struct cmsghdr align;
705 unsigned char buf[CMSG_SPACE(
sizeof(
int))];
708 memset(ctrl_msg.buf, 0,
sizeof(ctrl_msg.buf));
711 msgh.msg_name = NULL;
712 msgh.msg_namelen = 0;
714 unsigned char dummy = 0;
716 iov.iov_base = &dummy;
721 msgh.msg_control = ctrl_msg.buf;
722 msgh.msg_controllen =
sizeof(ctrl_msg.buf);
723 struct cmsghdr *cmsgp = CMSG_FIRSTHDR(&msgh);
724 cmsgp->cmsg_len = CMSG_LEN(
sizeof(
int));
725 cmsgp->cmsg_level = SOL_SOCKET;
726 cmsgp->cmsg_type = SCM_RIGHTS;
727 memcpy(CMSG_DATA(cmsgp), &passing_fd,
sizeof(
int));
729 ssize_t retval = sendmsg(socket_fd, &msgh, 0);
730 return (retval != -1);
743 struct cmsghdr align;
745 unsigned char buf[CMSG_SPACE(
sizeof(
int))];
748 memset(ctrl_msg.buf, 0,
sizeof(ctrl_msg.buf));
751 msgh.msg_name = NULL;
752 msgh.msg_namelen = 0;
756 iov.iov_base = &dummy;
761 msgh.msg_control = ctrl_msg.buf;
762 msgh.msg_controllen =
sizeof(ctrl_msg.buf);
764 ssize_t retval = recvmsg(msg_fd, &msgh, 0);
768 struct cmsghdr *cmsgp = CMSG_FIRSTHDR(&msgh);
770 if (cmsgp->cmsg_len != CMSG_LEN(
sizeof(
int)))
772 assert(cmsgp->cmsg_level == SOL_SOCKET);
773 assert(cmsgp->cmsg_type == SCM_RIGHTS);
776 memcpy(&passing_fd, CMSG_DATA(cmsgp),
sizeof(
int));
786 const bool temporarily)
789 "euid %d egid %d, switching to %d %d (temp: %d)",
790 getuid(), getgid(), geteuid(), getegid(), uid, gid, temporarily);
793 if (gid != getegid())
794 retval = setegid(gid);
795 if ((retval == 0) && (uid != geteuid()))
796 retval = seteuid(uid);
799 if ((getuid() == 0) && (getuid() != geteuid())) {
804 retval = setgid(gid) || setuid(uid);
818 S_ISREG(info.st_mode));
840 S_ISDIR(info.st_mode));
850 S_ISLNK(info.st_mode));
858 int retval = unlink(dest.c_str());
859 if ((retval != 0) && (errno != ENOENT))
861 retval = symlink(src.c_str(), dest.c_str());
871 const std::string &path,
873 bool verify_writable)
875 if (path ==
"")
return false;
877 int retval = mkdir(path.c_str(), mode);
878 if (retval == 0)
return true;
880 if ((errno == ENOENT) &&
883 return MkdirDeep(path, mode, verify_writable);
886 if (errno == EEXIST) {
888 if ((
platform_stat(path.c_str(), &info) == 0) && S_ISDIR(info.st_mode)) {
889 if (verify_writable) {
890 retval = utimes(path.c_str(), NULL);
909 std::string this_path = canonical_path +
"/quarantaine";
910 if (!
MkdirDeep(this_path, mode,
false))
return false;
912 this_path = canonical_path +
"/ff";
916 this_path = canonical_path +
"/txn";
919 for (
int i = 0; i <= 0xff; i++) {
921 snprintf(hex,
sizeof(hex),
"%02x", i);
922 this_path = canonical_path +
"/" + std::string(hex);
938 const int fd_lockfile = open(path.c_str(), O_RDONLY | O_CREAT, 0600);
942 if (flock(fd_lockfile, LOCK_EX | LOCK_NB) != 0) {
944 if (errno != EWOULDBLOCK)
960 const int fd = open(path.c_str(), O_CREAT | O_RDWR, 0600);
963 if (flock(fd, LOCK_EX | LOCK_NB) != 0) {
965 if (errno != EWOULDBLOCK)
971 int flags = fcntl(fd, F_GETFD);
974 flags = fcntl(fd, F_SETFD, flags);
979 snprintf(buf,
sizeof(buf),
"%" PRId64
"\n", static_cast<uint64_t>(getpid()));
981 (ftruncate(fd, 0) == 0) &&
SafeWrite(fd, buf, strlen(buf));
996 const int fd_lockfile = open(path.c_str(), O_RDONLY | O_CREAT, 0600);
1001 if (flock(fd_lockfile, LOCK_EX | LOCK_NB) != 0) {
1002 if (errno != EWOULDBLOCK) {
1008 if (flock(fd_lockfile, LOCK_EX) != 0) {
1020 int retval = flock(filedes, LOCK_UN);
1030 const char *open_flags, std::string *final_path)
1032 *final_path = path_prefix +
".XXXXXX";
1033 char *tmp_file =
strdupa(final_path->c_str());
1034 int tmp_fd = mkstemp(tmp_file);
1038 if (fchmod(tmp_fd, mode) != 0) {
1043 *final_path = tmp_file;
1044 FILE *tmp_fp = fdopen(tmp_fd, open_flags);
1072 std::string dir = path_prefix +
".XXXXXX";
1073 char *tmp_dir =
strdupa(dir.c_str());
1074 tmp_dir = mkdtemp(tmp_dir);
1075 if (tmp_dir == NULL)
1077 return std::string(tmp_dir);
1086 return (getcwd(cwd,
sizeof(cwd)) != NULL) ? std::string(cwd) : std::string();
1099 void RemoveFile(
const std::string &parent_path,
const std::string &name) {
1100 int retval = unlink((parent_path +
"/" + name).c_str());
1104 void RemoveDir(
const std::string &parent_path,
const std::string &name) {
1105 int retval = rmdir((parent_path +
"/" + name).c_str());
1109 bool TryRemoveDir(
const std::string &parent_path,
const std::string &name) {
1110 int retval = rmdir((parent_path +
"/" + name).c_str());
1111 return (retval != 0);
1123 return errno == ENOENT;
1124 if (!S_ISDIR(info.st_mode))
1138 bool result = remove_tree_helper->
success;
1139 delete remove_tree_helper;
1149 const std::string &dir,
1150 const std::string &suffix)
1152 std::vector<std::string> result;
1153 DIR *dirp = opendir(dir.c_str());
1159 const std::string name(dirent->d_name);
1160 if ((name.length() >= suffix.length()) &&
1161 (name.substr(name.length()-suffix.length()) == suffix))
1163 result.push_back(dir +
"/" + name);
1167 std::sort(result.begin(), result.end());
1176 const std::string &dir,
1177 const std::string &prefix)
1179 std::vector<std::string> result;
1180 DIR *dirp = opendir(dir.c_str());
1186 const std::string name(dirent->d_name);
1187 if ((name.length() >= prefix.length()) &&
1188 (name.substr(0, prefix.length()) == prefix))
1190 result.push_back(dir +
"/" + name);
1194 std::sort(result.begin(), result.end());
1204 std::vector<std::string> result;
1205 DIR *dirp = opendir(parent_dir.c_str());
1211 const std::string name(dirent->d_name);
1212 if ((name ==
".") || (name ==
".."))
1214 const std::string path = parent_dir +
"/" + name;
1220 if (S_ISDIR(info.st_mode))
1221 result.push_back(path);
1224 sort(result.begin(), result.end());
1233 std::vector<std::string> *names,
1234 std::vector<mode_t> *modes)
1236 DIR *dirp = opendir(directory.c_str());
1242 const std::string name(dirent->d_name);
1243 if ((name ==
".") || (name ==
".."))
1245 const std::string path = directory +
"/" + name;
1254 names->push_back(name);
1255 modes->push_back(info.st_mode);
1272 std::vector<std::string> search_paths;
1273 if (exe[0] ==
'/') {
1276 char *path_env = getenv(
"PATH");
1282 for (
unsigned i = 0; i < search_paths.size(); ++i) {
1283 if (search_paths[i].empty())
1285 if (search_paths[i][0] !=
'/')
1288 std::string path = search_paths[i] +
"/" +
GetFileName(exe);
1293 if (!S_ISREG(info.st_mode))
1295 retval = access(path.c_str(), X_OK);
1308 struct passwd *result = NULL;
1309 int bufsize = 16 * 1024;
1310 char *buf =
static_cast<char *
>(smalloc(bufsize));
1311 while (getpwuid_r(geteuid(), &pwd, buf, bufsize, &result) == ERANGE) {
1313 buf =
static_cast<char *
>(srealloc(buf, bufsize));
1315 if (result == NULL) {
1319 std::string user_name = pwd.pw_name;
1326 struct passwd *result = NULL;
1327 int bufsize = 16 * 1024;
1328 char *buf =
static_cast<char *
>(smalloc(bufsize));
1329 while (getpwuid_r(geteuid(), &pwd, buf, bufsize, &result) == ERANGE) {
1331 buf =
static_cast<char *
>(srealloc(buf, bufsize));
1333 if (result == NULL) {
1337 std::string shell = pwd.pw_shell;
1347 struct passwd *result = NULL;
1348 int bufsize = 16 * 1024;
1349 char *buf =
static_cast<char *
>(smalloc(bufsize));
1350 while (getpwuid_r(uid, &pwd, buf, bufsize, &result) == ERANGE) {
1352 buf =
static_cast<char *
>(srealloc(buf, bufsize));
1354 if (result == NULL) {
1359 *username = result->pw_name;
1368 bool GetUidOf(
const std::string &username, uid_t *uid, gid_t *main_gid) {
1370 struct passwd *result = NULL;
1371 int bufsize = 16 * 1024;
1372 char *buf =
static_cast<char *
>(smalloc(bufsize));
1373 while (getpwnam_r(username.c_str(), &pwd, buf, bufsize, &result) == ERANGE) {
1375 buf =
static_cast<char *
>(srealloc(buf, bufsize));
1377 if (result == NULL) {
1381 *uid = result->pw_uid;
1382 *main_gid = result->pw_gid;
1391 bool GetGidOf(
const std::string &groupname, gid_t *gid) {
1393 struct group *result = NULL;
1394 int bufsize = 16 * 1024;
1395 char *buf =
static_cast<char *
>(smalloc(bufsize));
1396 while (getgrnam_r(groupname.c_str(), &grp, buf, bufsize, &result) == ERANGE) {
1398 buf =
static_cast<char *
>(srealloc(buf, bufsize));
1400 if (result == NULL) {
1404 *gid = result->gr_gid;
1416 const mode_t my_umask = umask(0);
1426 int ngroups = getgroups(0, NULL);
1429 gid_t *groups =
static_cast<gid_t *
>(smalloc((ngroups+1) *
sizeof(gid_t)));
1430 int retval = getgroups(ngroups, groups);
1435 for (
int i = 0; i < ngroups; ++i) {
1436 if (groups[i] == gid) {
1441 groups[ngroups] = gid;
1442 retval = setgroups(ngroups+1, groups);
1449 uid_t uid = getuid();
1451 struct passwd *result = NULL;
1452 int bufsize = 16 * 1024;
1453 char *buf =
static_cast<char *
>(smalloc(bufsize));
1454 while (getpwuid_r(uid, &pwd, buf, bufsize, &result) == ERANGE) {
1456 buf =
static_cast<char *
>(srealloc(buf, bufsize));
1458 if (result == NULL) {
1462 std::string home_dir = result->pw_dir;
1474 memset(&rpl, 0,
sizeof(rpl));
1475 getrlimit(RLIMIT_NOFILE, &rpl);
1476 if (rpl.rlim_max < limit_nofile)
1477 rpl.rlim_max = limit_nofile;
1478 rpl.rlim_cur = limit_nofile;
1479 int retval = setrlimit(RLIMIT_NOFILE, &rpl);
1483 #ifdef HAS_VALGRIND_HEADERS
1484 return RUNNING_ON_VALGRIND ? -2 : -1;
1499 memset(&rpl, 0,
sizeof(rpl));
1500 getrlimit(RLIMIT_NOFILE, &rpl);
1501 *soft_limit = rpl.rlim_cur;
1504 int value = sysconf(_SC_OPEN_MAX);
1506 *hard_limit = value;
1508 *hard_limit = rpl.rlim_max;
1513 std::vector<LsofEntry>
Lsof(
const std::string &path) {
1514 std::vector<LsofEntry> result;
1516 std::vector<std::string> proc_names;
1517 std::vector<mode_t> proc_modes;
1520 for (
unsigned i = 0; i < proc_names.size(); ++i) {
1521 if (!S_ISDIR(proc_modes[i]))
1523 if (proc_names[i].find_first_not_of(
"1234567890") != std::string::npos)
1526 std::vector<std::string> fd_names;
1527 std::vector<mode_t> fd_modes;
1528 std::string proc_dir =
"/proc/" + proc_names[i];
1529 std::string fd_dir = proc_dir +
"/fd";
1537 proc_uid = info.st_uid;
1540 if (
HasPrefix(cwd +
"/", path +
"/",
false )) {
1543 entry.
owner = proc_uid;
1547 result.push_back(entry);
1551 for (
unsigned j = 0; j < fd_names.size(); ++j) {
1552 if (!S_ISLNK(fd_modes[j]))
1554 if (fd_names[j].find_first_not_of(
"1234567890") != std::string::npos)
1557 std::string target =
ReadSymlink(fd_dir +
"/" + fd_names[j]);
1558 if (!
HasPrefix(target +
"/", path +
"/",
false ))
1563 entry.
owner = proc_uid;
1564 entry.
read_only = !((fd_modes[j] & S_IWUSR) == S_IWUSR);
1566 entry.
path = target;
1567 result.push_back(entry);
1577 int retval = kill(pid, 0);
1580 return (errno != ESRCH);
1589 int retval = sigemptyset(&sigset);
1591 retval = sigaddset(&sigset, signum);
1593 retval = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1606 }
while ((retval != signum) && (errno == EINTR));
1607 assert(retval == signum);
1620 pid_t retval = waitpid(pid, &statloc, 0);
1629 if (WIFEXITED(statloc))
1630 return WEXITSTATUS(statloc);
1631 if (WIFSIGNALED(statloc) && (std::find(sig_ok.begin(), sig_ok.end(),
1632 WTERMSIG(statloc)) != sig_ok.end()))
1644 if ((pid = fork()) == 0) {
1645 int retval = setsid();
1647 if ((pid = fork()) == 0) {
1648 int null_read = open(
"/dev/null", O_RDONLY);
1649 int null_write = open(
"/dev/null", O_WRONLY);
1650 assert((null_read >= 0) && (null_write >= 0));
1651 retval = dup2(null_read, 0);
1653 retval = dup2(null_write, 1);
1655 retval = dup2(null_write, 2);
1666 waitpid(pid, &statloc, 0);
1676 const std::string &binary_path,
1677 const std::vector<std::string> &argv,
1678 const bool double_fork,
1688 std::set<int> preserve_fildes;
1689 preserve_fildes.insert(0);
1690 preserve_fildes.insert(1);
1691 preserve_fildes.insert(2);
1692 std::map<int, int> map_fildes;
1693 map_fildes[pipe_stdin[0]] = 0;
1694 map_fildes[pipe_stdout[1]] = 1;
1695 map_fildes[pipe_stderr[1]] = 2;
1696 std::vector<std::string> cmd_line;
1697 cmd_line.push_back(binary_path);
1698 cmd_line.insert(cmd_line.end(), argv.begin(), argv.end());
1714 close(pipe_stdin[0]);
1715 close(pipe_stdout[1]);
1716 close(pipe_stderr[1]);
1717 *fd_stdin = pipe_stdin[1];
1718 *fd_stdout = pipe_stdout[0];
1719 *fd_stderr = pipe_stderr[0];
1728 bool Shell(
int *fd_stdin,
int *fd_stdout,
int *fd_stderr) {
1729 const bool double_fork =
true;
1730 return ExecuteBinary(fd_stdin, fd_stdout, fd_stderr,
"/bin/sh",
1731 std::vector<std::string>(), double_fork);
1749 return "Sending PID";
1753 return "Unknown Status";
1755 return "Duplicate File Descriptor";
1757 return "Read maximal File Descriptor";
1758 case kFailGetFdFlags:
1759 return "Read File Descriptor Flags";
1760 case kFailSetFdFlags:
1761 return "Set File Descriptor Flags";
1762 case kFailDropCredentials:
1763 return "Lower User Permissions";
1765 return "Invoking execvp()";
1784 const std::set<int> &preserve_fildes,
1785 const std::map<int, int> &map_fildes,
1786 const bool drop_credentials,
1787 const bool clear_env,
1788 const bool double_fork,
1791 assert(command_line.size() >= 1);
1797 pid_t pid_grand_child;
1806 int retval = clearenv();
1811 const char *argv[command_line.size() + 1];
1812 for (
unsigned i = 0; i < command_line.size(); ++i)
1813 argv[i] = command_line[i].c_str();
1814 argv[command_line.size()] = NULL;
1817 for (std::map<int, int>::const_iterator i = map_fildes.begin(),
1818 iEnd = map_fildes.end(); i != iEnd; ++i)
1820 int retval = dup2(i->first, i->second);
1828 max_fd =
static_cast<int>(sysconf(_SC_OPEN_MAX));
1833 for (
int fd = 0; fd < max_fd; fd++) {
1834 if ((fd != pipe_fork.write_end) && (preserve_fildes.count(fd) == 0)) {
1841 pid_grand_child = fork();
1842 assert(pid_grand_child >= 0);
1843 if (pid_grand_child != 0) _exit(0);
1846 fd_flags = fcntl(pipe_fork.write_end, F_GETFD);
1851 fd_flags |= FD_CLOEXEC;
1852 if (fcntl(pipe_fork.write_end, F_SETFD, fd_flags) < 0) {
1858 assert(setenv(
"__CVMFS_DEBUG_MODE__",
"yes", 1) == 0);
1867 pid_grand_child = getpid();
1869 pipe_fork.Write(&failed,
sizeof(failed));
1870 pipe_fork.Write(pid_grand_child);
1872 execvp(command_line[0].c_str(), const_cast<char **>(argv));
1877 pipe_fork.Write(&failed,
sizeof(failed));
1882 waitpid(pid, &statloc, 0);
1885 close(pipe_fork.write_end);
1889 bool retcode = pipe_fork.Read(&status_code,
sizeof(status_code));
1892 close(pipe_fork.read_end);
1900 pid_t buf_child_pid = 0;
1901 pipe_fork.Read(&buf_child_pid);
1902 if (child_pid != NULL)
1903 *child_pid = buf_child_pid;
1904 close(pipe_fork.read_end);
1906 command_line[0].c_str(),
1907 static_cast<int>(buf_child_pid));
1917 struct timeval wait_for;
1918 wait_for.tv_sec = ms / 1000;
1919 wait_for.tv_usec = (ms % 1000) * 1000;
1920 select(0, NULL, NULL, NULL, &wait_for);
1929 ssize_t retval = write(fd, buf, nbyte);
1935 assert(static_cast<size_t>(retval) <= nbyte);
1936 buf =
reinterpret_cast<const char *
>(buf) + retval;
1946 unsigned nbytes = 0;
1947 for (
unsigned i = 0; i < iovcnt; ++i)
1948 nbytes += iov[i].iov_len;
1949 unsigned iov_idx = 0;
1953 writev(fd, &iov[iov_idx], static_cast<int>(iovcnt - iov_idx));
1959 assert(static_cast<size_t>(retval) <= nbytes);
1962 unsigned sum_written_blocks = 0;
1963 while ((sum_written_blocks + iov[iov_idx].iov_len) <=
1964 static_cast<size_t>(retval))
1966 sum_written_blocks += iov[iov_idx].iov_len;
1968 if (iov_idx == iovcnt) {
1969 assert(sum_written_blocks == static_cast<size_t>(retval));
1973 unsigned offset = retval - sum_written_blocks;
1974 iov[iov_idx].iov_len -= offset;
1975 iov[iov_idx].iov_base =
1976 reinterpret_cast<char *
>(iov[iov_idx].iov_base) + offset;
1987 ssize_t total_bytes = 0;
1989 ssize_t retval = read(fd, buf, nbyte);
1994 }
else if (retval == 0) {
1997 assert(static_cast<size_t>(retval) <= nbyte);
1998 buf =
reinterpret_cast<char *
>(buf) + retval;
2000 total_bytes += retval;
2010 if (!final_result) {
return false;}
2012 std::string tmp_result;
2013 static const int buf_size = 4096;
2015 ssize_t total_bytes = -1;
2017 total_bytes =
SafeRead(fd, buf, buf_size);
2018 if (total_bytes < 0) {
return false;}
2019 tmp_result.append(buf, total_bytes);
2020 }
while (total_bytes == buf_size);
2021 final_result->swap(tmp_result);
2026 const std::string &path,
2028 int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode);
2029 if (fd < 0)
return false;
2030 bool retval =
SafeWrite(fd, content.data(), content.size());
2036 #ifdef CVMFS_NAMESPACE_GUARD
bool MakeCacheDirectories(const std::string &path, const mode_t mode)
#define LogCvmfs(source, mask,...)
void RemoveFile(const std::string &parent_path, const std::string &name)
int MakeSocket(const std::string &path, const int mode)
bool SymlinkForced(const std::string &src, const std::string &dest)
std::string GetFileName(const std::string &path)
int MakeTcpEndpoint(const std::string &ipv4_address, int portno)
vector< string > SplitString(const string &str, const char delim, const unsigned max_chunks)
std::string GetUserName()
VoidCallback fn_new_symlink
VoidCallback fn_new_character_dev
void Recurse(const std::string &dir_path) const
void CreateFile(const std::string &path, const int mode, const bool ignore_failure)
bool GetUserNameOf(uid_t uid, std::string *username)
FILE * CreateTempFile(const std::string &path_prefix, const int mode, const char *open_flags, std::string *final_path)
int ConnectTcpEndpoint(const std::string &ipv4_address, int portno)
void RemoveDir(const std::string &parent_path, const std::string &name)
bool Shell(int *fd_stdin, int *fd_stdout, int *fd_stderr)
bool IsHttpUrl(const std::string &path)
bool ManagedExec(const std::vector< std::string > &command_line, const std::set< int > &preserve_fildes, const std::map< int, int > &map_fildes, const bool drop_credentials, const bool clear_env, const bool double_fork, pid_t *child_pid)
static void RemoveShortSocketLink(const std::string &short_path)
static pthread_mutex_t getumask_mutex
std::string CreateTempPath(const std::string &path_prefix, const int mode)
A simple recursion engine to abstract the recursion of directories. It provides several callback hook...
bool SafeWrite(int fd, const void *buf, size_t nbyte)
void SendMsg2Socket(const int fd, const std::string &msg)
assert((mem||(size==0))&&"Out Of Memory")
bool SafeWriteToFile(const std::string &content, const std::string &path, int mode)
std::string FindExecutable(const std::string &exe)
bool SendFd2Socket(int socket_fd, int passing_fd)
VoidCallback fn_leave_dir
int WaitForChild(pid_t pid, const std::vector< int > &sig_ok)
bool TryRemoveDir(const std::string &parent_path, const std::string &name)
std::string GetParentPath(const std::string &path)
bool AddGroup2Persona(const gid_t gid)
void MakePipe(int pipe_fd[2])
std::vector< std::string > FindDirectories(const std::string &parent_dir)
int SetLimitNoFile(unsigned limit_nofile)
bool SymlinkExists(const std::string &path)
bool FileExists(const std::string &path)
std::string GetAbsolutePath(const std::string &path)
void SplitPath(const std::string &path, std::string *dirname, std::string *filename)
void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit)
void ReadHalfPipe(int fd, void *buf, size_t nbyte)
ssize_t SafeRead(int fd, void *buf, size_t nbyte)
FileSystemInfo GetFileSystemInfo(const std::string &path)
void Nonblock2Block(int filedes)
static std::string MakeShortSocketLink(const std::string &path)
int TryLockFile(const std::string &path)
bool MkdirDeep(const std::string &path, const mode_t mode, bool verify_writable)
int LockFile(const std::string &path)
string ResolvePath(const std::string &path)
std::string GetHomeDirectory()
void WaitForSignal(int signum)
void Append(const char *chars, const unsigned length)
bool GetGidOf(const std::string &groupname, gid_t *gid)
std::string CreateTempDir(const std::string &path_prefix)
bool HasPrefix(const string &str, const string &prefix, const bool ignore_case)
bool DirectoryExists(const std::string &path)
bool ExecuteBinary(int *fd_stdin, int *fd_stdout, int *fd_stderr, const std::string &binary_path, const std::vector< std::string > &argv, const bool double_fork, pid_t *child_pid)
bool RemoveTree(const std::string &path)
bool SafeReadToString(int fd, std::string *final_result)
int WritePidFile(const std::string &path)
int ConnectSocket(const std::string &path)
uint64_t String2Uint64(const string &value)
VoidCallback fn_new_socket
bool GetUidOf(const std::string &username, uid_t *uid, gid_t *main_gid)
ShortString< kDefaultMaxPath, 0 > PathString
std::vector< std::string > FindFilesByPrefix(const std::string &dir, const std::string &prefix)
bool SwitchCredentials(const uid_t uid, const gid_t gid, const bool temporarily)
std::string ReadSymlink(const std::string &path)
std::vector< LsofEntry > Lsof(const std::string &path)
BoolCallback fn_new_dir_prefix
bool ListDirectory(const std::string &directory, std::vector< std::string > *names, std::vector< mode_t > *modes)
int64_t GetFileSize(const std::string &path)
void SafeSleepMs(const unsigned ms)
bool DiffTree(const std::string &path_a, const std::string &path_b)
void SortTeam(std::vector< T > *tractor, std::vector< U > *towed)
static std::string ToString(const Names name)
void Block2Nonblock(int filedes)
unsigned GetLength() const
bool IsAbsolutePath(const std::string &path)
bool ProcessExists(pid_t pid)
std::string MakeCanonicalPath(const std::string &path)
const char * GetChars() const
void WritePipe(int fd, const void *buf, size_t nbyte)
std::string GetCurrentWorkingDirectory()
void ReadPipe(int fd, void *buf, size_t nbyte)
std::vector< std::string > FindFilesBySuffix(const std::string &dir, const std::string &suffix)
void ClosePipe(int pipe_fd[2])
bool IsMountPoint(const std::string &path)
int RecvFdFromSocket(int msg_fd)
bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt)
void UnlockFile(const int filedes)
void BlockSignal(int signum)