36 #include <sys/types.h> 38 #include <sys/resource.h> 40 #include <sys/statvfs.h> 45 #define UNUSUAL_CHARS ":/\\!*" 46 #define REPLACEMENT_CHAR '_' 48 #define COREDUMP_FILESYSTEM "/var" 49 #define COREDUMP_FILESYSTEM_MIN_SIZE_MB 40 50 #define COREDUMP_HANDLER_PRIORITY -19 66 memset(p_proc->
name, 0,
sizeof(p_proc->
name));
104 syslog(LOG_ERR,
"Usage: cdh timestamp pid signal procname");
110 if (sscanf(argv[1],
"%u", &proc->
timestamp) != 1)
112 syslog(LOG_ERR,
"Unable to read timestamp argument <%s>. Closing", argv[1]);
116 if (sscanf(argv[2],
"%d", &proc->
pid) != 1)
118 syslog(LOG_ERR,
"Unable to read pid argument <%s>. Closing", argv[2]);
122 if (sscanf(argv[3],
"%d", &proc->
signal) != 1)
124 syslog(LOG_ERR,
"Unable to read signal argument <%s>. Closing", argv[3]);
132 strncpy(proc->
name, argv[4],
sizeof(proc->
name) - 1);
148 unsigned int l_char_index = 0;
150 for (l_char_index = 0; l_char_index <
sizeof(
UNUSUAL_CHARS) - 1; l_char_index++)
152 char* l_str_pointer = p_string;
156 l_str_pointer = strchr(l_str_pointer,
UNUSUAL_CHARS[l_char_index]);
158 if (l_str_pointer !=
NULL)
164 while (l_str_pointer !=
NULL);
179 unsigned long free_size = 0;
183 syslog(LOG_ERR,
"ERR cannot stat disk space on %s: %s",
COREDUMP_FILESYSTEM, strerror(errno));
188 free_size = (stat.f_bsize * stat.f_bavail) >> 20;
191 syslog(LOG_WARNING,
"ERR insufficient disk space for coredump: %ld MB.", free_size);
195 syslog(LOG_INFO,
"INFO disk space for coredump: %ld MB.", free_size);
202 struct dirent *dir =
NULL;
208 while ((dir = readdir(d)) !=
NULL)
210 struct stat unused_stat;
215 if (stat(lockfilepath, &unused_stat) != 0)
244 int l_need_create = 0;
245 int l_need_delete = 0;
249 if (lstat(p_dirname, &l_stat) < 0)
253 else if (!S_ISDIR(l_stat.st_mode))
259 if (l_need_delete > 0)
261 syslog(LOG_WARNING,
"WARN core directory '%s' is not a directory => removing it", p_dirname);
263 if (unlink(p_dirname) == -1)
265 syslog(LOG_ERR,
"ERR core directory '%s' cannot be unlinked: %s", p_dirname, strerror(errno));
269 if (l_need_create > 0)
271 if (create_silently == 0)
272 syslog(LOG_WARNING,
"WARN core directory '%s' does not exist => creation", p_dirname);
274 if (mkdir(p_dirname, 0666) == -1)
276 syslog(LOG_ERR,
"ERR core directory '%s' cannot be created: %s", p_dirname, strerror(errno));
325 unsigned int pattern_num = 0;
330 for (pattern_num = 0; pattern_num <
sizeof(patterns) /
sizeof(
char*); pattern_num++)
336 snprintf(l_src_filename,
sizeof(l_src_filename), patterns[pattern_num],
339 snprintf(l_dst_filename,
sizeof(l_dst_filename), patterns[pattern_num],
342 syslog(LOG_INFO,
"Moving coredump from %s to %s", l_src_filename, l_dst_filename);
344 if (rename(l_src_filename, l_dst_filename) < 0)
345 syslog(LOG_ERR,
"Moving failed: %s", strerror(errno));
360 int main(
int argc,
char* argv[])
365 openlog(
"CoredumpHandler", 0, LOG_DAEMON);
367 if (
read_args(argc, argv, &l_proc_info) < 0)
371 syslog(LOG_ERR,
"Failed to get executable name");
373 syslog(LOG_NOTICE,
"Handling coredump procname:%s pid:%d timest:%d signal:%d",
381 syslog(LOG_WARNING,
"Failed to change CDH priority");
418 unsigned int pattern_num = 0;
419 int fd_lockfile = -1;
424 for (pattern_num = 0; pattern_num <
sizeof(patterns) /
sizeof(
char*); pattern_num++)
426 snprintf(l_lockfilepath,
sizeof(l_lockfilepath), patterns[pattern_num],
433 unlink(l_lockfilepath);
439 if ((fd_lockfile = open(l_lockfilepath, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) >= 0)
441 if (write(fd_lockfile,
"1", 1) < 0)
442 syslog(LOG_WARNING,
"Failed to write lockfile %d: %s", fd_lockfile, strerror(errno));
448 syslog(LOG_WARNING,
"Failed to open lockfile %s: %s", l_lockfilepath, strerror(errno));
unsigned int m_note_page_size
cdh_status_t get_exec_name(unsigned int p_pid_str, char *p_exec_name, int p_exec_name_maxsize)
cdh_status_t check_disk_space()
#define CORE_LOCK_DIRECTORY
cdh_status_t treat_coredump(proc_info_t *p_proc)
char name[MAX_PROC_NAME_LENGTH]
void remove_unusual_chars(char *p_string)
unsigned char m_crashid[CRASH_ID_LEN]
#define COREDUMP_FILESYSTEM_MIN_SIZE_MB
int main(int argc, char *argv[])
uint64_t m_crashid_phase1
cdh_status_t read_args(int argc, char **argv, proc_info_t *proc)
#define CONTEXT_FILE_PATTERN
#define COREDUMP_HANDLER_PRIORITY
#define COREDUMP_FILESYSTEM
#define CORE_TMP_DIRECTORY
void init_proc_info(proc_info_t *p_proc)
cdh_status_t check_core_directory()
cdh_status_t move_to_core_directory(proc_info_t *p_proc)
cdh_status_t treat_crash_data(proc_info_t *p_proc)
cdh_registers_t m_registers
cdh_status_t write_proc_context(const proc_info_t *)
cdh_status_t check_and_create_directory(const char *p_dirname, int create_silently)
#define CORE_FILE_PATTERN
void clean_core_tmp_dir()
char threadname[MAX_PROC_NAME_LENGTH]
void core_locks(const proc_info_t *p_proc, int action)
#define CORE_MAX_FILENAME_LENGTH