Disable core dumps and tracing on *nix.

But only when built in release mode.

Closes #4
This commit is contained in:
Felix Geyer 2012-10-13 11:05:50 +02:00
parent 807924c0bc
commit 169e6327ea
5 changed files with 66 additions and 0 deletions

View file

@ -37,6 +37,16 @@
#include <time.h> // for nanosleep()
#endif
#if defined(HAVE_PR_SET_DUMPABLE)
#include <sys/prctl.h>
#elif defined(HAVE_RLIMIT_CORE)
#include <sys/resource.h>
#endif
#ifdef HAVE_PT_DENY_ATTACH
#include <sys/ptrace.h>
#endif
namespace Tools {
QString humanReadableFileSize(qint64 bytes)
@ -203,4 +213,29 @@ QString platform()
#endif
}
void disableCoreDumps()
{
bool success = false;
// prefer PR_SET_DUMPABLE since that also prevents ptrace
#if defined(HAVE_PR_SET_DUMPABLE)
success = (prctl(PR_SET_DUMPABLE, 0) == 0);
#elif defined(HAVE_RLIMIT_CORE)
struct rlimit limit;
limit.rlim_cur = 0;
limit.rlim_max = 0;
success = (setrlimit(RLIMIT_CORE, &limit) == 0);
#endif
// Mac OS X
#ifdef HAVE_PT_DENY_ATTACH
// make sure setrlimit() and ptrace() succeeded
success = success && (ptrace(PT_DENY_ATTACH, 0, 0, 0) == 0);
#endif
if (!success) {
qWarning("Unable to disable core dumps.");
}
}
} // namespace Tools