14#include "arccore/common/internal/Process.h"
16#include "arccore/base/NotImplementedException.h"
17#include "arccore/base/FixedArray.h"
26#ifdef ARCCORE_OS_LINUX
41ProcessExecArgs::ExecStatus Process::
42execute(ProcessExecArgs& args)
44#ifdef ARCCORE_OS_LINUX
45 args.m_output_bytes.clear();
52 int r0 = pipe2(pipefd_out, O_CLOEXEC);
54 return ProcessExecArgs::ExecStatus::CanNotCreatePipe;
55 r0 = pipe2(pipefd_in, O_CLOEXEC);
57 return ProcessExecArgs::ExecStatus::CanNotCreatePipe;
58 pid_t cpid = ::fork();
60 return ProcessExecArgs::ExecStatus::CanNotFork;
62 ProcessExecArgs::ExecStatus exec_status = ProcessExecArgs::ExecStatus::OK;
69 ::close(STDOUT_FILENO);
70 ::close(pipefd_out[0]);
71 ::dup2(pipefd_out[1], STDOUT_FILENO);
74 ::close(STDIN_FILENO);
75 ::close(pipefd_in[1]);
76 ::dup2(pipefd_in[0], STDIN_FILENO);
78 const char* cmd_name = args.command().localstr();
81 Integer nb_arg = arguments.size();
85 for (
Integer i = 0; i < nb_arg; ++i)
86 command_args[i + 1] = arguments[i].localstr();
87 command_args[0] = cmd_name;
88 command_args[nb_arg + 1] =
nullptr;
90 const char*
const newenviron[] = { NULL };
91 ::execve(cmd_name, (
char*
const*)command_args.data(), (
char*
const*)newenviron);
95 ::close(pipefd_out[1]);
96 ::close(pipefd_in[0]);
98 Int64 nb_wanted_write = input_bytes.size();
99 Int64 nb_written = ::write(pipefd_in[1], input_bytes.data(), nb_wanted_write);
100 if (nb_written != nb_wanted_write)
101 std::cerr <<
"Error writing to pipe\n";
102 ::close(pipefd_in[1]);
103 const int BUF_SIZE = 4096;
105 buf[BUF_SIZE] =
'\0';
106 Int32 max_iteration = 1000000;
107 Int32 current_iteration = 0;
109 for (
Int32 i = 0; i < max_iteration; ++i) {
110 ssize_t nb_read = ::read(pipefd_out[0], buf.data(), BUF_SIZE);
111 if (nb_read == EINTR)
115 Int32 i_nb_read =
static_cast<Int32>(nb_read);
116 buf[i_nb_read] =
'\0';
117 args.m_output_bytes.addRange(buf.view().subView(0, i_nb_read));
125 child_pid = ::waitpid(cpid, &status, 0);
126 }
while (child_pid == -1 && errno == EINTR);
128 if (WIFEXITED(status)) {
129 args.m_exit_code = WEXITSTATUS(status);
133 exec_status = ProcessExecArgs::ExecStatus::AbnormalExit;
135 close(pipefd_out[0]);
138 args.m_output_bytes.add(
'\0');
Constant view of an array of type T.
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
ConstArrayView< Byte > ByteConstArrayView
C equivalent of a 1D array of characters.
std::int32_t Int32
Signed integer type of 32 bits.