forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdbProcess.hpp
More file actions
220 lines (187 loc) · 4.79 KB
/
NdbProcess.hpp
File metadata and controls
220 lines (187 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright 2009 Sun Microsystems, Inc.
All rights reserved. Use is subject to license terms.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef NDB_PROCESS_HPP
#define NDB_PROCESS_HPP
#include <portlib/NdbSleep.h>
class NdbProcess
{
#ifdef _WIN32
tyepdef DWORD pid_t;
#endif
pid_t m_pid;
BaseString m_name;
public:
static pid_t getpid()
{
#ifdef _WIN32
return GetCurrentProcessid();
#else
return ::getpid();
#endif
}
class Args
{
Vector<BaseString> m_args;
public:
void add(const char* str)
{
m_args.push_back(str);
}
void add(const char* str, const char* str2)
{
BaseString tmp;
tmp.assfmt("%s%s", str, str2);
m_args.push_back(tmp);
}
void add(const char* str, int val)
{
BaseString tmp;
tmp.assfmt("%s%d", str, val);
m_args.push_back(tmp);
}
const Vector<BaseString>& args(void) const
{
return m_args;
}
};
static NdbProcess* create(const BaseString& name,
const BaseString& path,
const BaseString& cwd,
const Args& args)
{
NdbProcess* proc = new NdbProcess(name);
if (!proc)
{
fprintf(stderr, "Failed to allocate memory for new process\n");
return NULL;
}
// Check existence of cwd
if (cwd.c_str() && access(cwd.c_str(), F_OK) != 0)
{
fprintf(stderr,
"The specified working directory '%s' does not exist\n",
cwd.c_str());
delete proc;
return NULL;
}
if (!start_process(proc->m_pid, path.c_str(), cwd.c_str(), args))
{
fprintf(stderr,
"Failed to create process '%s'\n", name.c_str());
delete proc;
return NULL;
}
return proc;
}
bool stop(void)
{
int ret = kill(m_pid, 9);
if (ret != 0)
{
fprintf(stderr,
"Failed to kill process %d, ret: %d, errno: %d\n",
m_pid, ret, errno);
return false;
}
printf("Stopped process %d\n", m_pid);
return true;
}
bool wait(int& ret, int timeout = 0)
{
int retries = 0;
int status;
while (true)
{
pid_t ret_pid = waitpid(m_pid, &status, WNOHANG);
if (ret_pid == -1)
{
fprintf(stderr,
"Error occured when waiting for process %d, ret: %d, errno: %d\n",
m_pid, status, errno);
return false;
}
if (ret_pid == m_pid)
{
if (WIFEXITED(status))
ret = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
ret = WTERMSIG(status);
else
ret = 37; // Unknown exit status
printf("Got process %d, status: %d, ret: %d\n", m_pid, status, ret);
return true;
}
if (timeout == 0)
return false;
if (retries++ > timeout*10)
{
fprintf(stderr,
"Timeout when waiting for process %d\n", m_pid);
return false;
}
NdbSleep_MilliSleep(10);
}
assert(false); // Never reached
}
private:
NdbProcess(BaseString name) :
m_name(name)
{
m_pid = -1;
}
static bool start_process(pid_t& pid, const char* path,
const char* cwd,
const Args& args)
{
#ifdef _WIN32
#else
int retries = 5;
pid_t tmp;
while ((tmp = fork()) == -1)
{
fprintf(stderr, "Warning: 'fork' failed, errno: %d - ", errno);
if (retries--)
{
fprintf(stderr, "retrying in 1 second...\n");
NdbSleep_SecSleep(1);
continue;
}
fprintf(stderr, "giving up...\n");
return false;
}
if (tmp)
{
pid = tmp;
printf("Started process: %d\n", pid);
return true;
}
assert(tmp == 0);
if (cwd && chdir(cwd) != 0)
{
fprintf(stderr, "Failed to change directory to '%s', errno: %d\n", cwd, errno);
exit(1);
}
// Concatenate arguments
BaseString args_str;
args_str.assign(args.args(), " ");
char **argv = BaseString::argify(path, args_str.c_str());
//printf("name: %s\n", path);
execv(path, argv);
fprintf(stderr, "execv failed, errno: %d\n", errno);
exit(1);
#endif
}
};
#endif