Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/libutil/current-process.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <algorithm>
#include <cstring>

#include "current-process.hh"
#include "namespaces.hh"
#include "util.hh"
Expand Down Expand Up @@ -49,20 +52,27 @@ unsigned int getMaxCPU()
//////////////////////////////////////////////////////////////////////


#if __linux__
rlim_t savedStackSize = 0;
#endif

void setStackSize(size_t stackSize)
void setStackSize(rlim_t stackSize)
{
#if __linux__
struct rlimit limit;
if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) {
savedStackSize = limit.rlim_cur;
limit.rlim_cur = stackSize;
setrlimit(RLIMIT_STACK, &limit);
limit.rlim_cur = std::min(stackSize, limit.rlim_max);
if (setrlimit(RLIMIT_STACK, &limit) != 0) {
logger->log(
lvlError,
hintfmt(
"Failed to increase stack size from %1% to %2% (maximum allowed stack size: %3%): %4%",
savedStackSize,
stackSize,
limit.rlim_max,
std::strerror(errno)
).str()
);
}
}
#endif
}

void restoreProcessContext(bool restoreMounts)
Expand All @@ -72,15 +82,13 @@ void restoreProcessContext(bool restoreMounts)
restoreMountNamespace();
}

#if __linux__
if (savedStackSize) {
struct rlimit limit;
if (getrlimit(RLIMIT_STACK, &limit) == 0) {
limit.rlim_cur = savedStackSize;
setrlimit(RLIMIT_STACK, &limit);
}
}
#endif
}


Expand Down
2 changes: 1 addition & 1 deletion src/libutil/current-process.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ unsigned int getMaxCPU();
/**
* Change the stack size.
*/
void setStackSize(size_t stackSize);
void setStackSize(rlim_t stackSize);

/**
* Restore the original inherited Unix process context (such as signal
Expand Down