From: Joe Orton Date: Fri, 17 Aug 2012 13:41:15 +0000 (+0000) Subject: * support/rotatelogs.c (truncate_and_write_error): Factor out from X-Git-Tag: 2.5.0-alpha~6419 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4f1e2bb5f8c907338efbbb574c79d6d1e5ad57d9;p=thirdparty%2Fapache%2Fhttpd.git * support/rotatelogs.c (truncate_and_write_error): Factor out from doRotate, main; make error handling consistant and print the actual error to stderr. (doRotate, main): Use it. PR: 45084 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1374247 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/support/rotatelogs.c b/support/rotatelogs.c index 5ed8befaa7c..8c611f88d0c 100644 --- a/support/rotatelogs.c +++ b/support/rotatelogs.c @@ -356,6 +356,24 @@ static void post_rotate(apr_pool_t *pool, struct logfile *newlog, } } +/* After a error, truncate the current file and write out an error + * message, which must be contained in status->errbuf. The process is + * terminated on failure. */ +static void truncate_and_write_error(rotate_status_t *status) +{ + apr_size_t buflen = strlen(status->errbuf); + + if (apr_file_trunc(status->current.fd, 0) != APR_SUCCESS) { + fprintf(stderr, "Error truncating the file %s\n", status->current.name); + exit(2); + } + if (apr_file_write_full(status->current.fd, status->errbuf, buflen, NULL) != APR_SUCCESS) { + fprintf(stderr, "Error writing error (%s) to the file %s\n", + status->errbuf, status->current.name); + exit(2); + } +} + /* * Open a new log file, and if successful * also close the old one. @@ -432,7 +450,6 @@ static void doRotate(rotate_config_t *config, rotate_status_t *status) } else { char error[120]; - apr_size_t nWrite; apr_strerror(rv, error, sizeof error); @@ -453,16 +470,8 @@ static void doRotate(rotate_config_t *config, rotate_status_t *status) "Resetting log file due to error opening " "new log file, %10d messages lost: %-25.25s\n", status->nMessCount, error); - nWrite = strlen(status->errbuf); - if (apr_file_trunc(status->current.fd, 0) != APR_SUCCESS) { - fprintf(stderr, "Error truncating the file %s\n", status->current.name); - exit(2); - } - if (apr_file_write_full(status->current.fd, status->errbuf, nWrite, NULL) != APR_SUCCESS) { - fprintf(stderr, "Error writing to the file %s\n", status->current.name); - exit(2); - } + truncate_and_write_error(status); } status->nMessCount = 0; @@ -702,12 +711,8 @@ int main (int argc, const char * const argv[]) "Error %d writing to log file at offset %" APR_OFF_T_FMT ". " "%10d messages lost (%s)\n", rv, cur_offset, status.nMessCount, strerrbuf); - nWrite = strlen(status.errbuf); - apr_file_trunc(status.current.fd, 0); - if (apr_file_write_full(status.current.fd, status.errbuf, nWrite, NULL) != APR_SUCCESS) { - fprintf(stderr, "Error writing to the file %s\n", status.current.name); - exit(2); - } + + truncate_and_write_error(&status); } else { status.nMessCount++;