forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpal_process.cpp
More file actions
419 lines (364 loc) · 12.7 KB
/
pal_process.cpp
File metadata and controls
419 lines (364 loc) · 12.7 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include "pal_config.h"
#include "pal_process.h"
#include "pal_utilities.h"
#include <assert.h>
#include <errno.h>
#include <limits>
#include <limits.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <syslog.h>
#include <unistd.h>
// Validate that our Signals enum values are correct for the platform
static_assert(PAL_SIGKILL == SIGKILL, "");
// Validate that our WaitPidOptions enum values are correct for the platform
static_assert(PAL_WNOHANG == WNOHANG, "");
static_assert(PAL_WUNTRACED == WUNTRACED, "");
// Validate that our SysLogPriority values are correct for the platform
static_assert(PAL_LOG_EMERG == LOG_EMERG, "");
static_assert(PAL_LOG_ALERT == LOG_ALERT, "");
static_assert(PAL_LOG_CRIT == LOG_CRIT, "");
static_assert(PAL_LOG_ERR == LOG_ERR, "");
static_assert(PAL_LOG_WARNING == LOG_WARNING, "");
static_assert(PAL_LOG_NOTICE == LOG_NOTICE, "");
static_assert(PAL_LOG_INFO == LOG_INFO, "");
static_assert(PAL_LOG_DEBUG == LOG_DEBUG, "");
static_assert(PAL_LOG_KERN == LOG_KERN, "");
static_assert(PAL_LOG_USER == LOG_USER, "");
static_assert(PAL_LOG_MAIL == LOG_MAIL, "");
static_assert(PAL_LOG_DAEMON == LOG_DAEMON, "");
static_assert(PAL_LOG_AUTH == LOG_AUTH, "");
static_assert(PAL_LOG_SYSLOG == LOG_SYSLOG, "");
static_assert(PAL_LOG_LPR == LOG_LPR, "");
static_assert(PAL_LOG_NEWS == LOG_NEWS, "");
static_assert(PAL_LOG_UUCP == LOG_UUCP, "");
static_assert(PAL_LOG_CRON == LOG_CRON, "");
static_assert(PAL_LOG_AUTHPRIV == LOG_AUTHPRIV, "");
static_assert(PAL_LOG_FTP == LOG_FTP, "");
static_assert(PAL_LOG_LOCAL0 == LOG_LOCAL0, "");
static_assert(PAL_LOG_LOCAL1 == LOG_LOCAL1, "");
static_assert(PAL_LOG_LOCAL2 == LOG_LOCAL2, "");
static_assert(PAL_LOG_LOCAL3 == LOG_LOCAL3, "");
static_assert(PAL_LOG_LOCAL4 == LOG_LOCAL4, "");
static_assert(PAL_LOG_LOCAL5 == LOG_LOCAL5, "");
static_assert(PAL_LOG_LOCAL6 == LOG_LOCAL6, "");
static_assert(PAL_LOG_LOCAL7 == LOG_LOCAL7, "");
// Validate that out PriorityWhich values are correct for the platform
static_assert(PAL_PRIO_PROCESS == static_cast<int>(PRIO_PROCESS), "");
static_assert(PAL_PRIO_PGRP == static_cast<int>(PRIO_PGRP), "");
static_assert(PAL_PRIO_USER == static_cast<int>(PRIO_USER), "");
enum
{
READ_END_OF_PIPE = 0,
WRITE_END_OF_PIPE = 1,
};
static void CloseIfOpen(int fd)
{
// Ignoring errors from close is a deliberate choice and we musn't
// let close during cleanup on a failure path disturb errno.
if (fd >= 0)
{
int priorErrno = errno;
close(fd);
errno = priorErrno;
}
}
extern "C" int32_t ForkAndExecProcess(const char* filename,
char* const argv[],
char* const envp[],
const char* cwd,
int32_t redirectStdin,
int32_t redirectStdout,
int32_t redirectStderr,
int32_t* childPid,
int32_t* stdinFd,
int32_t* stdoutFd,
int32_t* stderrFd)
{
int success = true;
int stdinFds[2] = {-1, -1}, stdoutFds[2] = {-1, -1}, stderrFds[2] = {-1, -1};
int processId = -1;
// Validate arguments
if (nullptr == filename || nullptr == argv || nullptr == envp || nullptr == stdinFd || nullptr == stdoutFd ||
nullptr == stderrFd || nullptr == childPid)
{
assert(false && "null argument.");
errno = EINVAL;
success = false;
goto done;
}
if ((redirectStdin & ~1) != 0 || (redirectStdout & ~1) != 0 || (redirectStderr & ~1) != 0)
{
assert(false && "Boolean redirect* inputs must be 0 or 1.");
errno = EINVAL;
success = false;
goto done;
}
// Open pipes for any requests to redirect stdin/stdout/stderr
if ((redirectStdin && pipe(stdinFds) != 0) || (redirectStdout && pipe(stdoutFds) != 0) ||
(redirectStderr && pipe(stderrFds) != 0))
{
assert(false && "pipe() failed.");
success = false;
goto done;
}
// Fork the child process
if ((processId = fork()) == -1)
{
assert(false && "fork() failed.");
success = false;
goto done;
}
if (processId == 0) // processId == 0 if this is child process
{
// Close the child's copy of the parent end of any open pipes
CloseIfOpen(stdinFds[WRITE_END_OF_PIPE]);
CloseIfOpen(stdoutFds[READ_END_OF_PIPE]);
CloseIfOpen(stderrFds[READ_END_OF_PIPE]);
// For any redirections that should happen, dup the pipe descriptors onto stdin/out/err.
// Then close out the old pipe descriptrs, which we no longer need.
if ((redirectStdin && dup2(stdinFds[READ_END_OF_PIPE], STDIN_FILENO) == -1) ||
(redirectStdout && dup2(stdoutFds[WRITE_END_OF_PIPE], STDOUT_FILENO) == -1) ||
(redirectStderr && dup2(stderrFds[WRITE_END_OF_PIPE], STDERR_FILENO) == -1))
{
_exit(errno != 0 ? errno : EXIT_FAILURE);
}
CloseIfOpen(stdinFds[READ_END_OF_PIPE]);
CloseIfOpen(stdoutFds[WRITE_END_OF_PIPE]);
CloseIfOpen(stderrFds[WRITE_END_OF_PIPE]);
// Change to the designated working directory, if one was specified
if (nullptr != cwd && chdir(cwd) == -1)
{
_exit(errno != 0 ? errno : EXIT_FAILURE);
}
// Finally, execute the new process. execve will not return if it's successful.
execve(filename, argv, envp);
_exit(errno != 0 ? errno : EXIT_FAILURE); // execve failed
}
// This is the parent process. processId == pid of the child
*childPid = processId;
*stdinFd = stdinFds[WRITE_END_OF_PIPE];
*stdoutFd = stdoutFds[READ_END_OF_PIPE];
*stderrFd = stderrFds[READ_END_OF_PIPE];
done:
// Regardless of success or failure, close the parent's copy of the child's end of
// any opened pipes. The parent doesn't need them anymore.
CloseIfOpen(stdinFds[READ_END_OF_PIPE]);
CloseIfOpen(stdoutFds[WRITE_END_OF_PIPE]);
CloseIfOpen(stderrFds[WRITE_END_OF_PIPE]);
// If we failed, close everything else and give back error values in all out arguments.
if (!success)
{
CloseIfOpen(stdinFds[WRITE_END_OF_PIPE]);
CloseIfOpen(stdoutFds[READ_END_OF_PIPE]);
CloseIfOpen(stderrFds[READ_END_OF_PIPE]);
*stdinFd = -1;
*stdoutFd = -1;
*stderrFd = -1;
*childPid = -1;
}
return success ? 0 : -1;
}
// Each platform type has it's own RLIMIT values but the same name, so we need
// to convert our standard types into the platform specific ones.
static int32_t ConvertRLimitResourcesPalToPlatform(RLimitResources value)
{
switch (value)
{
case PAL_RLIMIT_CPU:
return RLIMIT_CPU;
case PAL_RLIMIT_FSIZE:
return RLIMIT_FSIZE;
case PAL_RLIMIT_DATA:
return RLIMIT_DATA;
case PAL_RLIMIT_STACK:
return RLIMIT_STACK;
case PAL_RLIMIT_CORE:
return RLIMIT_CORE;
case PAL_RLIMIT_AS:
return RLIMIT_AS;
case PAL_RLIMIT_RSS:
return RLIMIT_RSS;
case PAL_RLIMIT_MEMLOCK:
return RLIMIT_MEMLOCK;
case PAL_RLIMIT_NPROC:
return RLIMIT_NPROC;
case PAL_RLIMIT_NOFILE:
return RLIMIT_NOFILE;
}
assert(false && "Unknown RLIMIT value");
return -1;
}
// Because RLIM_INFINITY is different per-platform, use the max value of a uint64 (which is RLIM_INFINITY on Ubuntu)
// to signify RLIM_INIFINITY; on OS X, where RLIM_INFINITY is slightly lower, we'll translate it to the correct value
// here.
static rlim_t ConvertFromManagedRLimitInfinityToPalIfNecessary(uint64_t value)
{
// rlim_t type can vary per platform, so we also treat anything outside its range as infinite.
if (value == UINT64_MAX || value > std::numeric_limits<rlim_t>::max())
return RLIM_INFINITY;
return static_cast<rlim_t>(value);
}
// Because RLIM_INFINITY is different per-platform, use the max value of a uint64 (which is RLIM_INFINITY on Ubuntu)
// to signify RLIM_INIFINITY; on OS X, where RLIM_INFINITY is slightly lower, we'll translate it to the correct value
// here.
static uint64_t ConvertFromNativeRLimitInfinityToManagedIfNecessary(rlim_t value)
{
if (value == RLIM_INFINITY)
return UINT64_MAX;
return UnsignedCast(value);
}
static void ConvertFromRLimitManagedToPal(const RLimit& pal, rlimit& native)
{
native.rlim_cur = ConvertFromManagedRLimitInfinityToPalIfNecessary(pal.CurrentLimit);
native.rlim_max = ConvertFromManagedRLimitInfinityToPalIfNecessary(pal.MaximumLimit);
}
static void ConvertFromPalRLimitToManaged(const rlimit& native, RLimit& pal)
{
pal.CurrentLimit = ConvertFromNativeRLimitInfinityToManagedIfNecessary(native.rlim_cur);
pal.MaximumLimit = ConvertFromNativeRLimitInfinityToManagedIfNecessary(native.rlim_max);
}
extern "C" int32_t GetRLimit(RLimitResources resourceType, RLimit* limits)
{
assert(limits != nullptr);
int32_t platformLimit = ConvertRLimitResourcesPalToPlatform(resourceType);
rlimit internalLimit;
int result = getrlimit(platformLimit, &internalLimit);
if (result == 0)
{
ConvertFromPalRLimitToManaged(internalLimit, *limits);
}
else
{
*limits = {};
}
return result;
}
extern "C" int32_t SetRLimit(RLimitResources resourceType, const RLimit* limits)
{
assert(limits != nullptr);
int32_t platformLimit = ConvertRLimitResourcesPalToPlatform(resourceType);
rlimit internalLimit;
ConvertFromRLimitManagedToPal(*limits, internalLimit);
return setrlimit(platformLimit, &internalLimit);
}
extern "C" int32_t Kill(int32_t pid, int32_t signal)
{
return kill(pid, signal);
}
extern "C" int32_t GetPid()
{
return getpid();
}
extern "C" int32_t GetSid(int32_t pid)
{
return getsid(pid);
}
extern "C" void SysLog(SysLogPriority priority, const char* message, const char* arg1)
{
syslog(static_cast<int>(priority), message, arg1);
}
extern "C" int32_t WaitPid(int32_t pid, int32_t* status, WaitPidOptions options)
{
assert(status != nullptr);
return waitpid(pid, status, static_cast<int>(options));
}
extern "C" int32_t WExitStatus(int32_t status)
{
return WEXITSTATUS(status);
}
extern "C" int32_t WIfExited(int32_t status)
{
return WIFEXITED(status);
}
extern "C" int32_t WIfSignaled(int32_t status)
{
return WIFSIGNALED(status);
}
extern "C" int32_t WTermSig(int32_t status)
{
return WTERMSIG(status);
}
extern "C" int64_t PathConf(const char* path, PathConfName name)
{
int32_t confValue = -1;
switch (name)
{
case PAL_PC_LINK_MAX:
confValue = _PC_LINK_MAX;
break;
case PAL_PC_MAX_CANON:
confValue = _PC_MAX_CANON;
break;
case PAL_PC_MAX_INPUT:
confValue = _PC_MAX_INPUT;
break;
case PAL_PC_NAME_MAX:
confValue = _PC_NAME_MAX;
break;
case PAL_PC_PATH_MAX:
confValue = _PC_PATH_MAX;
break;
case PAL_PC_PIPE_BUF:
confValue = _PC_PIPE_BUF;
break;
case PAL_PC_CHOWN_RESTRICTED:
confValue = _PC_CHOWN_RESTRICTED;
break;
case PAL_PC_NO_TRUNC:
confValue = _PC_NO_TRUNC;
break;
case PAL_PC_VDISABLE:
confValue = _PC_VDISABLE;
break;
}
if (confValue == -1)
{
assert(false && "Unknown PathConfName");
errno = EINVAL;
return -1;
}
return pathconf(path, confValue);
}
extern "C" int64_t GetMaximumPath()
{
int64_t result = pathconf("/", _PC_PATH_MAX);
if (result == -1)
{
result = PATH_MAX;
}
return result;
}
extern "C" int32_t GetPriority(PriorityWhich which, int32_t who)
{
// GetPriority uses errno 0 to show succes to make sure we don't have a stale value
errno = 0;
#if PRIORITY_REQUIRES_INT_WHO
return getpriority(which, who);
#else
return getpriority(which, static_cast<id_t>(who));
#endif
}
extern "C" int32_t SetPriority(PriorityWhich which, int32_t who, int32_t nice)
{
#if PRIORITY_REQUIRES_INT_WHO
return setpriority(which, who, nice);
#else
return setpriority(which, static_cast<id_t>(who), nice);
#endif
}
extern "C" char* GetCwd(char* buffer, int32_t bufferSize)
{
assert(bufferSize >= 0);
if (bufferSize < 0)
{
errno = EINVAL;
return nullptr;
}
return getcwd(buffer, UnsignedCast(bufferSize));
}