|
|
|
@ -19,7 +19,9 @@ namespace framework {
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<FILE> shell_fopen(const std::string& path,
|
|
|
|
|
const std::string& mode) {
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#if defined _WIN32 || defined __APPLE__
|
|
|
|
|
return nullptr;
|
|
|
|
|
#else
|
|
|
|
|
if (shell_verbose()) {
|
|
|
|
|
LOG(INFO) << "Opening file[" << path << "] with mode[" << mode << "]";
|
|
|
|
|
}
|
|
|
|
@ -35,8 +37,6 @@ std::shared_ptr<FILE> shell_fopen(const std::string& path,
|
|
|
|
|
LOG(FATAL) << "fclose fail, path[" << path << "]";
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
#else
|
|
|
|
|
return nullptr;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -44,7 +44,9 @@ std::shared_ptr<FILE> shell_fopen(const std::string& path,
|
|
|
|
|
// The implementation is async signal safe
|
|
|
|
|
// Mostly copy from CPython code
|
|
|
|
|
static int close_open_fds_internal() {
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#if defined _WIN32 || defined __APPLE__
|
|
|
|
|
return 0;
|
|
|
|
|
#else
|
|
|
|
|
struct linux_dirent {
|
|
|
|
|
long d_ino = 0; // NOLINT
|
|
|
|
|
off_t d_off;
|
|
|
|
@ -91,13 +93,15 @@ static int close_open_fds_internal() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(dir_fd);
|
|
|
|
|
#endif
|
|
|
|
|
return 0;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int shell_popen_fork_internal(const char* real_cmd, bool do_read,
|
|
|
|
|
int parent_end, int child_end) {
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#if defined _WIN32 || defined __APPLE__
|
|
|
|
|
return 0;
|
|
|
|
|
#else
|
|
|
|
|
int child_pid = -1;
|
|
|
|
|
// Too frequent calls to fork() makes openmpi very slow. Use vfork() instead.
|
|
|
|
|
// But vfork() is very dangerous. Be careful.
|
|
|
|
@ -127,12 +131,13 @@ static int shell_popen_fork_internal(const char* real_cmd, bool do_read,
|
|
|
|
|
}
|
|
|
|
|
exit(127);
|
|
|
|
|
#endif
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<FILE> shell_popen(const std::string& cmd,
|
|
|
|
|
const std::string& mode, int* err_no) {
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#if defined _WIN32 || defined __APPLE__
|
|
|
|
|
return nullptr;
|
|
|
|
|
#else
|
|
|
|
|
bool do_read = mode == "r";
|
|
|
|
|
bool do_write = mode == "w";
|
|
|
|
|
if (!(do_read || do_write)) {
|
|
|
|
@ -197,7 +202,9 @@ std::shared_ptr<FILE> shell_popen(const std::string& cmd,
|
|
|
|
|
|
|
|
|
|
static int shell_p2open_fork_internal(const char* real_cmd, int pipein_fds[2],
|
|
|
|
|
int pipeout_fds[2]) {
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#if defined _WIN32 || defined __APPLE__
|
|
|
|
|
return 0;
|
|
|
|
|
#else
|
|
|
|
|
int child_pid = -1;
|
|
|
|
|
if ((child_pid = fork()) < 0) {
|
|
|
|
|
return -1;
|
|
|
|
@ -230,12 +237,13 @@ static int shell_p2open_fork_internal(const char* real_cmd, int pipein_fds[2],
|
|
|
|
|
}
|
|
|
|
|
exit(127);
|
|
|
|
|
#endif
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<std::shared_ptr<FILE>, std::shared_ptr<FILE>> shell_p2open(
|
|
|
|
|
const std::string& cmd) {
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#if defined _WIN32 || defined __APPLE__
|
|
|
|
|
return nullptr;
|
|
|
|
|
#else
|
|
|
|
|
if (shell_verbose()) {
|
|
|
|
|
LOG(INFO) << "Opening bidirectional pipe[" << cmd << "]";
|
|
|
|
|
}
|
|
|
|
@ -287,13 +295,13 @@ std::pair<std::shared_ptr<FILE>, std::shared_ptr<FILE>> shell_p2open(
|
|
|
|
|
PCHECK((out_fp = fdopen(pipeout_fds[1], "w")) != NULL);
|
|
|
|
|
return {{in_fp, [child_life](FILE* fp) { PCHECK(fclose(fp) == 0); }},
|
|
|
|
|
{out_fp, [child_life](FILE* fp) { PCHECK(fclose(fp) == 0); }}};
|
|
|
|
|
#else
|
|
|
|
|
return nullptr;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string shell_get_command_output(const std::string& cmd) {
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#if defined _WIN32 || defined __APPLE__
|
|
|
|
|
return "";
|
|
|
|
|
#else
|
|
|
|
|
int err_no = 0;
|
|
|
|
|
do {
|
|
|
|
|
err_no = 0;
|
|
|
|
@ -308,7 +316,6 @@ std::string shell_get_command_output(const std::string& cmd) {
|
|
|
|
|
}
|
|
|
|
|
} while (err_no == -1);
|
|
|
|
|
#endif
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
} // end namespace framework
|
|
|
|
|
} // end namespace paddle
|
|
|
|
|