Merge pull request #17447 from tchaikov/wip-freebsd-coredump

test/coredumpctl: support freebsd

Reviewed-by: Willem Jan Withagen <wjw@digiware.nl>
This commit is contained in:
Kefu Chai 2017-09-06 11:16:08 +08:00 committed by GitHub
commit 0717fc3488

View File

@ -1,13 +1,24 @@
#pragma once #pragma once
#include "acconfig.h"
#ifdef HAVE_SYS_PRCTL_H #ifdef HAVE_SYS_PRCTL_H
#include <iostream> #include <iostream>
#include <sys/prctl.h> #include <sys/prctl.h>
#include "common/errno.h" #include "common/errno.h"
struct PrCtl { class PrCtl {
int saved_state = -1; int saved_state = -1;
int set_dumpable(int new_state) { static int get_dumpable() {
int r = prctl(PR_GET_DUMPABLE);
if (r == -1) {
r = errno;
std::cerr << "warning: unable to get dumpable flag: " << cpp_strerror(r)
<< std::endl;
}
return r;
}
static int set_dumpable(bool new_state) {
int r = prctl(PR_SET_DUMPABLE, new_state); int r = prctl(PR_SET_DUMPABLE, new_state);
if (r) { if (r) {
r = -errno; r = -errno;
@ -17,13 +28,13 @@ struct PrCtl {
} }
return r; return r;
} }
public:
PrCtl(int new_state = 0) { PrCtl(int new_state = 0) {
int r = prctl(PR_GET_DUMPABLE); int r = get_dumpable();
if (r == -1) { if (r == -1) {
r = errno; return;
std::cerr << "warning: unable to get dumpable flag: " << cpp_strerror(r) }
<< std::endl; if (r != new_state) {
} else if (r != new_state) {
if (!set_dumpable(new_state)) { if (!set_dumpable(new_state)) {
saved_state = r; saved_state = r;
} }
@ -38,10 +49,57 @@ struct PrCtl {
}; };
#else #else
#include <sys/resource.h>
#ifdef RLIMIT_CORE
#include <iostream>
#include <sys/resource.h>
#include "common/errno.h"
class PrCtl {
rlimit saved_lim;
static int get_dumpable(rlimit* saved) {
int r = getrlimit(RLIMIT_CORE, saved);
if (r) {
r = errno;
std::cerr << "warning: unable to getrlimit(): " << cpp_strerror(r)
<< std::endl;
}
return r;
}
static void set_dumpable(const rlimit& rlim) {
int r = setrlimit(RLIMIT_CORE, &rlim);
if (r) {
r = -errno;
std::cerr << "warning: unable to setrlimit(): " << cpp_strerror(r)
<< std::endl;
}
}
public:
PrCtl(int new_state = 0) {
int r = get_dumpable(&saved_lim);
if (r == -1) {
return;
}
rlimit new_lim;
if (new_state) {
new_lim.rlim_cur = saved_lim.rlim_max;
} else {
new_lim.rlim_cur = new_lim.rlim_max = 0;
}
if (new_lim.rlim_cur == saved_lim.rlim_cur) {
return;
}
set_dumpable(new_lim);
}
~PrCtl() {
set_dumpable(saved_lim);
}
};
#else
struct PrCtl { struct PrCtl {
// to silence the Wunused-variable warning // to silence the Wunused-variable warning
PrCtl() {} PrCtl() {}
}; };
#endif // RLIMIT_CORE
#endif #endif