From 342e35c0b867713e5c1eb5724c3f258b61c2fbd0 Mon Sep 17 00:00:00 2001 From: Jia Zhouyang Date: Thu, 1 Mar 2018 15:17:58 +0800 Subject: [PATCH 1/3] Add error handling for external APIs Add error handling code for chdir and pipe. When those APIs fail, print error message and return. --- base/utils.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/base/utils.c b/base/utils.c index 606e682e3..976a225f4 100644 --- a/base/utils.c +++ b/base/utils.c @@ -480,7 +480,10 @@ int my_system_r(icinga_macros *mac, char *cmd, int timeout, int *early_timeout, #endif /* create a pipe */ - pipe(fd); + if (pipe(fd) < 0) { + logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: pipe() in my_system_r() failed for command \"%s\"\n", cmd); + return STATE_UNKNOWN; + } /* make the pipe non-blocking */ fcntl(fd[0], F_SETFL, O_NONBLOCK); @@ -2507,9 +2510,14 @@ int daemon_init(void) { /* change working directory. scuttle home if we're dumping core */ homedir = getenv("HOME"); if (daemon_dumps_core == TRUE && homedir != NULL) - chdir(homedir); + val = chdir(homedir); else - chdir("/"); + val = chdir("/"); + if (val < 0) { + logit(NSLOG_RUNTIME_ERROR, TRUE, "Failed to change dir: %s\n", strerror(errno)); + cleanup(); + exit(ERROR); + } umask(S_IWGRP | S_IWOTH); From c723ba0ecd820a8a803257bfeaad67fd50a7b24f Mon Sep 17 00:00:00 2001 From: Jia Zhouyang Date: Thu, 1 Mar 2018 15:23:32 +0800 Subject: [PATCH 2/3] Add error handling for chown When chown fails, print error message and return. --- base/logging.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/base/logging.c b/base/logging.c index cc0245086..a222415aa 100644 --- a/base/logging.c +++ b/base/logging.c @@ -606,7 +606,10 @@ int rotate_log_file(time_t rotation_time) { if (stat_result == 0) { chmod(log_file, log_file_stat.st_mode); - chown(log_file, log_file_stat.st_uid, log_file_stat.st_gid); + if (chown(log_file, log_file_stat.st_uid, log_file_stat.st_gid) < 0) { + perror("chown failed"); + return ERROR; + } } /* log current host and service state if activated*/ From 3beb2ed1c6fef73c81653809f44c938f7b7d1300 Mon Sep 17 00:00:00 2001 From: Jia Zhouyang Date: Thu, 1 Mar 2018 15:28:32 +0800 Subject: [PATCH 3/3] Add error handling for chown When chown fails, print error message and return. --- cgi/cgiutils.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cgi/cgiutils.c b/cgi/cgiutils.c index 694ba383b..bc8765601 100644 --- a/cgi/cgiutils.c +++ b/cgi/cgiutils.c @@ -3047,7 +3047,10 @@ int rotate_cgi_log_file() { if (stat_result == 0) { chmod(cgi_log_file, log_file_stat.st_mode); - chown(cgi_log_file, log_file_stat.st_uid, log_file_stat.st_gid); + if (chown(cgi_log_file, log_file_stat.st_uid, log_file_stat.st_gid) < 0) { + perror("chown failed"); + return ERROR; + } } my_free(log_archive);