29 #ifdef HAVE_SYS_RESOURCE_H
30 #include <sys/resource.h>
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
45 SCLogInfo(
"Configuring core dump is not yet supported on Windows.");
55 static bool unlimited =
false;
56 static rlim_t max_dump = 0;
64 if (!unlimited && !max_dump) {
70 dumpable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
72 SCLogNotice(
"Failed to get dumpable state of process, "
73 "core dumps may not be enabled: %s",
76 else if (unlimited || max_dump > 0) {
78 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
79 SCLogInfo(
"Unable to make this process dumpable.");
97 #ifdef HAVE_SYS_RESOURCE_H
100 uint64_t max_dump64 = 0;
101 uint32_t max_dump32 = 0;
102 const char *dump_size_config = NULL;
103 size_t rlim_size =
sizeof(rlim_t);
105 if (
SCConfGet(
"coredump.max-dump", &dump_size_config) == 0) {
109 if (dump_size_config == NULL) {
110 SCLogError(
"malformed value for coredump.max-dump: NULL");
113 if (strcasecmp (dump_size_config,
"unlimited") == 0) {
118 if (strchr (dump_size_config,
'-') != NULL) {
119 SCLogInfo (
"Negative value for core dump size; ignored.");
124 SCLogInfo (
"Unexpected type for rlim_t");
127 if (rlim_size == 8) {
129 &max_dump64, 10, strlen(dump_size_config), dump_size_config);
131 else if (rlim_size == 4) {
133 &max_dump32, 10, strlen(dump_size_config), dump_size_config);
136 SCLogInfo (
"Illegal core dump size: %s.", dump_size_config);
140 max_dump = rlim_size == 8 ? (rlim_t)max_dump64 : (rlim_t)max_dump32;
142 SCLogInfo(
"Max dump is 0, disable core dumping.");
144 SCLogInfo(
"Max dump is %" PRIu64, (uint64_t)max_dump);
151 struct rlimit new_lim;
154 if (getrlimit (RLIMIT_CORE, &lim) == -1) {
155 SCLogInfo (
"Can't read coredump limit for this process.");
161 if (lim.rlim_max == RLIM_INFINITY && lim.rlim_cur == RLIM_INFINITY) {
166 new_lim.rlim_max = RLIM_INFINITY;
167 new_lim.rlim_cur = RLIM_INFINITY;
168 if (setrlimit (RLIMIT_CORE, &new_lim) == 0) {
172 if (errno == EPERM) {
175 if (lim.rlim_cur < lim.rlim_max) {
176 new_lim.rlim_cur = lim.rlim_max;
177 if (setrlimit (RLIMIT_CORE, & new_lim) == 0) {
178 SCLogInfo (
"Could not set core dump size to unlimited; core dump size set to the hard limit.");
182 SCLogInfo (
"Failed to set core dump size to unlimited or to the hard limit.");
186 SCLogInfo (
"Could not set core dump size to unlimited; it's set to the hard limit.");
193 new_lim.rlim_cur = max_dump;
196 if (lim.rlim_max == RLIM_INFINITY) {
198 new_lim.rlim_max = lim.rlim_max;
200 #ifdef RLIM_SAVED_MAX
201 else if (lim.rlim_max == RLIM_SAVED_MAX) {
203 new_lim.rlim_max = lim.rlim_max;
206 else if (lim.rlim_max < max_dump) {
208 new_lim.rlim_max = max_dump;
212 new_lim.rlim_max = lim.rlim_max;
214 if (setrlimit (RLIMIT_CORE, &new_lim) == 0) {
215 SCLogInfo (
"Core dump setting attempted is %"PRIu64, (uint64_t) new_lim.rlim_cur);
216 struct rlimit actual_lim;
217 if (getrlimit (RLIMIT_CORE, &actual_lim) == 0) {
218 if (actual_lim.rlim_cur == RLIM_INFINITY) {
221 #ifdef RLIM_SAVED_CUR
222 else if (actual_lim.rlim_cur == RLIM_SAVED_CUR) {
223 SCLogInfo (
"Core dump size set to soft limit.");
227 SCLogInfo (
"Core dump size set to %"PRIu64, (uint64_t) actual_lim.rlim_cur);
233 if (errno == EINVAL || errno == EPERM) {
236 if ((lim.rlim_cur < max_dump && lim.rlim_cur < lim.rlim_max)
237 #ifdef RLIM_SAVED_CUR
238 || (lim.rlim_cur == RLIM_SAVED_CUR)
241 new_lim.rlim_max = lim.rlim_max;
242 new_lim.rlim_cur = lim.rlim_max;
243 if (setrlimit (RLIMIT_CORE, &new_lim) == 0) {
244 SCLogInfo(
"Core dump size set to the hard limit.");
251 SCLogInfo(
"Couldn't set coredump size to %s.", dump_size_config);
256 #if defined UNITTESTS && defined HAVE_SYS_RESOURCE_H
259 static void ResetCoredumpConfig(
void)
269 static int CoredumpConfigTest01(
void)
275 max-dump: unlimited\n\
279 ResetCoredumpConfig();
285 FAIL_IF(errno != EPERM && ret != 1);
299 static int CoredumpConfigTest02(
void)
309 ResetCoredumpConfig();
315 FAIL_IF(errno != EPERM && ret != 1);
329 static int CoredumpConfigTest03(
void)
335 max-dump: -1000000\n\
338 ResetCoredumpConfig();
357 static int CoredumpConfigTest04(
void)
366 ResetCoredumpConfig();
385 static int CoredumpConfigTest05(
void)
395 ResetCoredumpConfig();
402 FAIL_IF(errno != EPERM && ret != 1);
415 #if defined UNITTESTS && defined HAVE_SYS_RESOURCE_H