21 #ifndef NDB_PROCESS_HPP
22 #define NDB_PROCESS_HPP
24 #include <portlib/NdbSleep.h>
38 return GetCurrentProcessid();
49 void add(
const char* str)
51 m_args.push_back(str);
54 void add(
const char* str,
const char* str2)
57 tmp.
assfmt(
"%s%s", str, str2);
58 m_args.push_back(tmp);
61 void add(
const char* str,
int val)
64 tmp.
assfmt(
"%s%d", str, val);
65 m_args.push_back(tmp);
83 fprintf(stderr,
"Failed to allocate memory for new process\n");
88 if (cwd.
c_str() && access(cwd.
c_str(), F_OK) != 0)
91 "The specified working directory '%s' does not exist\n",
97 if (!start_process(proc->m_pid, path.
c_str(), cwd.
c_str(), args))
100 "Failed to create process '%s'\n", name.
c_str());
113 "Failed to kill process %d, ret: %d, errno: %d\n",
117 printf(
"Stopped process %d\n", m_pid);
121 bool wait(
int& ret,
int timeout = 0)
127 pid_t ret_pid = waitpid(m_pid, &status, WNOHANG);
131 "Error occured when waiting for process %d, ret: %d, errno: %d\n",
132 m_pid, status, errno);
136 if (ret_pid == m_pid)
138 if (WIFEXITED(status))
139 ret = WEXITSTATUS(status);
140 else if (WIFSIGNALED(status))
141 ret = WTERMSIG(status);
145 printf(
"Got process %d, status: %d, ret: %d\n", m_pid, status, ret);
152 if (retries++ > timeout*10)
155 "Timeout when waiting for process %d\n", m_pid);
158 NdbSleep_MilliSleep(10);
171 static bool start_process(pid_t& pid,
const char* path,
179 while ((tmp = fork()) == -1)
181 fprintf(stderr,
"Warning: 'fork' failed, errno: %d - ", errno);
184 fprintf(stderr,
"retrying in 1 second...\n");
185 NdbSleep_SecSleep(1);
188 fprintf(stderr,
"giving up...\n");
195 printf(
"Started process: %d\n", pid);
200 if (cwd && chdir(cwd) != 0)
202 fprintf(stderr,
"Failed to change directory to '%s', errno: %d\n", cwd, errno);
208 args_str.
assign(args.args(),
" ");
214 fprintf(stderr,
"execv failed, errno: %d\n", errno);