2016-03-12 13:50:43 -05:00
|
|
|
#include "Task.h"
|
|
|
|
#include "TaskStub.h"
|
|
|
|
#include "TaskTryCatch.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <libplatform/libplatform.h>
|
|
|
|
#include <uv.h>
|
|
|
|
#include <v8.h>
|
|
|
|
#include <v8-platform.h>
|
|
|
|
|
|
|
|
#if !defined (_WIN32) && !defined (__MACH__)
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/prctl.h>
|
2016-04-16 15:04:15 -04:00
|
|
|
#include <sys/resource.h>
|
2016-03-12 13:50:43 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
v8::Platform* gPlatform = 0;
|
|
|
|
|
2016-04-16 15:04:15 -04:00
|
|
|
void shedPrivileges() {
|
2016-04-17 08:56:46 -04:00
|
|
|
#if !defined (_WIN32)
|
2016-04-16 15:04:15 -04:00
|
|
|
struct rlimit zeroLimit;
|
|
|
|
zeroLimit.rlim_cur = 0;
|
|
|
|
zeroLimit.rlim_max = 0;
|
|
|
|
|
|
|
|
// RLIMIT_AS
|
|
|
|
// RLIMIT_CORE
|
|
|
|
// RLIMIT_CPU
|
|
|
|
// RLIMIT_DATA
|
|
|
|
// RLIMIT_FSIZE
|
|
|
|
// RLIMIT_RSS
|
|
|
|
// RLIMIT_RTPRIO
|
|
|
|
// RLIMIT_RTTIME
|
|
|
|
// RLIMIT_SIGPENDING
|
|
|
|
// RLIMIT_STACK
|
|
|
|
|
|
|
|
if (setrlimit(RLIMIT_FSIZE, &zeroLimit) != 0) {
|
|
|
|
perror("setrlimit(RLIMIT_FSIZE, {0, 0})");
|
2016-04-17 08:56:46 -04:00
|
|
|
exit(-1);
|
2016-04-16 15:04:15 -04:00
|
|
|
}
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &zeroLimit) != 0) {
|
|
|
|
perror("setrlimit(RLIMIT_NOFILE, {0, 0})");
|
2016-04-17 08:56:46 -04:00
|
|
|
exit(-1);
|
2016-04-16 15:04:15 -04:00
|
|
|
}
|
|
|
|
if (setrlimit(RLIMIT_NPROC, &zeroLimit) != 0) {
|
|
|
|
perror("setrlimit(RLIMIT_NPROC, {0, 0})");
|
2016-04-17 08:56:46 -04:00
|
|
|
exit(-1);
|
2016-04-16 15:04:15 -04:00
|
|
|
}
|
2016-04-17 08:56:46 -04:00
|
|
|
#if !defined (__MACH__)
|
|
|
|
if (setrlimit(RLIMIT_LOCKS, &zeroLimit) != 0) {
|
|
|
|
perror("setrlimit(RLIMIT_LOCKS, {0, 0})");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (setrlimit(RLIMIT_MSGQUEUE, &zeroLimit) != 0) {
|
|
|
|
perror("setrlimit(RLIMIT_MSGQUEUE, {0, 0})");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-16 15:04:15 -04:00
|
|
|
#endif
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2016-03-13 09:37:58 -04:00
|
|
|
int result = 0;
|
2016-03-12 13:50:43 -05:00
|
|
|
uv_setup_args(argc, argv);
|
|
|
|
TaskStub::initialize();
|
|
|
|
v8::V8::InitializeICU();
|
|
|
|
gPlatform = v8::platform::CreateDefaultPlatform();
|
|
|
|
v8::V8::InitializePlatform(gPlatform);
|
|
|
|
v8::V8::Initialize();
|
2016-07-24 13:02:45 -04:00
|
|
|
|
|
|
|
const char* kAsyncAwait = "--harmony-async-await";
|
|
|
|
v8::V8::SetFlagsFromString(kAsyncAwait, std::strlen(kAsyncAwait));
|
|
|
|
|
2016-03-12 13:50:43 -05:00
|
|
|
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
|
|
|
|
|
|
|
|
bool isChild = false;
|
|
|
|
const char* coreTask = "core/core.js";
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (!std::strcmp(argv[i], "--child")) {
|
|
|
|
isChild = true;
|
|
|
|
} else {
|
|
|
|
coreTask = argv[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined (_WIN32)
|
|
|
|
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
|
|
|
|
perror("signal");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (isChild) {
|
|
|
|
#if !defined (_WIN32) && !defined (__MACH__)
|
|
|
|
prctl(PR_SET_PDEATHSIG, SIGHUP);
|
|
|
|
#endif
|
|
|
|
Task task;
|
|
|
|
task.configureFromStdin();
|
2016-04-16 15:04:15 -04:00
|
|
|
shedPrivileges();
|
2016-03-12 13:50:43 -05:00
|
|
|
task.activate();
|
|
|
|
task.run();
|
|
|
|
} else {
|
|
|
|
#if !defined (_WIN32) && !defined (__MACH__)
|
|
|
|
setpgid(0, 0);
|
|
|
|
#endif
|
|
|
|
Task task;
|
|
|
|
task.setTrusted(true);
|
|
|
|
task.activate();
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolateScope(task.getIsolate());
|
|
|
|
v8::HandleScope handleScope(task.getIsolate());
|
|
|
|
v8::Context::Scope contextScope(task.getContext());
|
|
|
|
TaskTryCatch tryCatch(&task);
|
2016-03-13 09:37:58 -04:00
|
|
|
if (!task.execute(coreTask))
|
|
|
|
{
|
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
if (tryCatch.hasCaught())
|
|
|
|
{
|
|
|
|
result = -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
task.run();
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::V8::Dispose();
|
|
|
|
|
2016-03-13 09:37:58 -04:00
|
|
|
return result;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|