From 61b347f47a82564dc0ef52d3abc258b64801e355 Mon Sep 17 00:00:00 2001 From: Chiaki ISHIKAWA Date: Tue, 10 Mar 2015 21:21:06 +0900 Subject: [PATCH 1/8] Harden logging, perror() replaced with cc_log() and fatal(). unlink() failure recorded. --- util.c | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/util.c b/util.c index 4b8dd15..7e1bed5 100644 --- a/util.c +++ b/util.c @@ -107,16 +107,33 @@ path_max(const char *path) #endif } +/* + * warn the failure of logging function. + * exit then. + */ +static void +warn_log_fail() { + fprintf(stderr, + "Writing to logfile failed.\n" + "Check the permission and make sure there is enough room for writing to the filesystem.\n"); + /* Maybe we should exit here! */ + fatal("ccache log could not be written."); +} + static void vlog(const char *format, va_list ap, bool log_updated_time) { + int rc1, rc2; if (!init_log()) { return; } log_prefix(log_updated_time); - vfprintf(logfile, format, ap); - fprintf(logfile, "\n"); + rc1 = vfprintf(logfile, format, ap); + rc2 = fprintf(logfile, "\n"); + if (rc1 < 0 || rc2 < 0) { + warn_log_fail(); + } } /* @@ -153,6 +170,7 @@ cc_bulklog(const char *format, ...) void cc_log_argv(const char *prefix, char **argv) { + int rc; if (!init_log()) { return; } @@ -160,7 +178,9 @@ cc_log_argv(const char *prefix, char **argv) log_prefix(true); fputs(prefix, logfile); print_command(logfile, argv); - fflush(logfile); + rc = fflush(logfile); + if (rc) + warn_log_fail(); } /* something went badly wrong! */ @@ -827,7 +847,8 @@ traverse(const char *dir, void (*fn)(const char *, struct stat *)) fname = format("%s/%s", dir, de->d_name); if (lstat(fname, &st)) { if (errno != ENOENT) { - perror(fname); + cc_log("lstat on file %s failed.", fname); + fatal("lstat on file %s failed.", fname); } free(fname); continue; @@ -1413,8 +1434,13 @@ x_rename(const char *oldpath, const char *newpath) int tmp_unlink(const char *path) { - cc_log("Unlink %s", path); - return unlink(path); + int rc; + cc_log("tmp_unlink:Unlink %s", path); + rc = unlink(path); + if (rc) { + cc_log("tmp_unlink:Unlink failed: rc = %d", rc); + } + return rc; } /* @@ -1430,7 +1456,7 @@ x_unlink(const char *path) */ char *tmp_name = format("%s.rm.%s", path, tmp_string()); int result = 0; - cc_log("Unlink %s via %s", path, tmp_name); + cc_log("x_unlink: Unlink %s via %s", path, tmp_name); if (x_rename(path, tmp_name) == -1) { result = -1; goto out; @@ -1443,6 +1469,9 @@ x_unlink(const char *path) } out: free(tmp_name); + if (result) { + cc_log("x_unlink: failed result = %d", result); + } return result; } -- 2.1.4