Contract process resource limits. (#243)

This commit is contained in:
Ravin Perera
2021-02-15 16:36:10 +05:30
committed by GitHub
parent 3783c687a2
commit 4c4e109233
7 changed files with 83 additions and 8 deletions

View File

@@ -98,6 +98,13 @@ namespace sc
// Set up the process environment and overlay the contract binary program with execv().
// Set process resource limits.
if (set_process_rlimits() == -1)
{
std::cerr << errno << ": Failed to set contract process resource limits." << (ctx.args.readonly ? " (rdonly)" : "") << "\n";
exit(1);
}
// Close all fds unused by SC process.
close_unused_fds(ctx, false);
@@ -153,6 +160,33 @@ namespace sc
return ret;
}
int set_process_rlimits()
{
rlimit lim;
if (conf::cfg.contract.round_limits.proc_cpu_seconds > 0)
{
lim.rlim_cur = lim.rlim_max = conf::cfg.contract.round_limits.proc_cpu_seconds;
if (setrlimit(RLIMIT_CPU, &lim) == -1)
return -1;
}
if (conf::cfg.contract.round_limits.proc_mem_bytes > 0)
{
lim.rlim_cur = lim.rlim_max = conf::cfg.contract.round_limits.proc_mem_bytes;
if (setrlimit(RLIMIT_DATA, &lim) == -1)
return -1;
}
if (conf::cfg.contract.round_limits.proc_ofd_count > 0)
{
lim.rlim_cur = lim.rlim_max = conf::cfg.contract.round_limits.proc_ofd_count;
if (setrlimit(RLIMIT_NOFILE, &lim) == -1)
return -1;
}
return 0;
}
/**
* Checks whether the contract process has exited.
* @param ctx Contract execution context.
@@ -185,7 +219,7 @@ namespace sc
}
else
{
LOG_ERROR << "Contract process" << (ctx.args.readonly ? " (rdonly)" : "") << " ended with code " << WEXITSTATUS(scstatus);
LOG_ERROR << "Contract process" << (ctx.args.readonly ? " (rdonly)" : "") << " ended prematurely with code " << WEXITSTATUS(scstatus);
return -1;
}
}