From d0d575bcd5201eab1aacdf682f6fc11d1f46c682 Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Thu, 20 Nov 2008 17:23:08 +0000 Subject: [PATCH] r2303 merge: compatibility with glibc 2.3, warn_unused_result attribute for write() --- src/core/nginx.c | 31 ++++++++++++++++++++----------- src/core/ngx_conf_file.c | 18 ++++++++++++++++-- src/core/ngx_cycle.c | 21 ++++++++++++++++++--- src/core/ngx_log.c | 2 +- src/os/unix/ngx_files.h | 12 +++++++++++- 5 files changed, 66 insertions(+), 18 deletions(-) diff --git a/src/core/nginx.c b/src/core/nginx.c index 48e085ba3..28624db76 100644 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -192,6 +192,8 @@ static char **ngx_os_environ; int ngx_cdecl main(int argc, char *const *argv) { + char *p; + ssize_t n; ngx_int_t i; ngx_log_t *log; ngx_cycle_t *cycle, init_cycle; @@ -241,23 +243,30 @@ main(int argc, char *const *argv) } if (ngx_show_version) { - ngx_write_fd(ngx_stderr_fileno, "nginx version: " NGINX_VER CRLF, - sizeof("nginx version: " NGINX_VER CRLF) - 1); + + p = "nginx version: " NGINX_VER CRLF; + n = sizeof("nginx version: " NGINX_VER CRLF) - 1; + + if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) { + return 1; + } if (ngx_show_configure) { #ifdef NGX_COMPILER - ngx_write_fd(ngx_stderr_fileno, "built by " NGX_COMPILER CRLF, - sizeof("built by " NGX_COMPILER CRLF) - 1); + p = "built by " NGX_COMPILER CRLF; + n = sizeof("built by " NGX_COMPILER CRLF) - 1; + + if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) { + return 1; + } #endif -#ifndef __WATCOMC__ + p = "configure arguments: " NGX_CONFIGURE CRLF; + n = sizeof("configure arguments :" NGX_CONFIGURE CRLF) - 1; - /* OpenWatcomC could not build the long NGX_CONFIGURE string */ - - ngx_write_fd(ngx_stderr_fileno, - "configure arguments: " NGX_CONFIGURE CRLF, - sizeof("configure arguments :" NGX_CONFIGURE CRLF) - 1); -#endif + if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) { + return 1; + } } if (!ngx_test_config) { diff --git a/src/core/ngx_conf_file.c b/src/core/ngx_conf_file.c index 6efcd8b7d..54ae237f3 100644 --- a/src/core/ngx_conf_file.c +++ b/src/core/ngx_conf_file.c @@ -806,6 +806,7 @@ ngx_conf_open_file(ngx_cycle_t *cycle, ngx_str_t *name) static void ngx_conf_flush_files(ngx_cycle_t *cycle) { + ssize_t n, len; ngx_uint_t i; ngx_list_part_t *part; ngx_open_file_t *file; @@ -826,11 +827,24 @@ ngx_conf_flush_files(ngx_cycle_t *cycle) i = 0; } - if (file[i].buffer == NULL || file[i].pos - file[i].buffer == 0) { + len = file[i].pos - file[i].buffer; + + if (file[i].buffer == NULL || len == 0) { continue; } - ngx_write_fd(file[i].fd, file[i].buffer, file[i].pos - file[i].buffer); + n = ngx_write_fd(file[i].fd, file[i].buffer, len); + + if (n == NGX_FILE_ERROR) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, + ngx_write_fd_n " to \"%s\" failed", + file[i].name.data); + + } else if (n != len) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, + ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz", + file[i].name.data, n, len); + } } } diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c index 9cdbb2d95..f8c0d3e85 100644 --- a/src/core/ngx_cycle.c +++ b/src/core/ngx_cycle.c @@ -970,6 +970,7 @@ ngx_test_lockfile(u_char *file, ngx_log_t *log) void ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user) { + ssize_t n, len; ngx_fd_t fd; ngx_uint_t i; ngx_list_part_t *part; @@ -993,9 +994,23 @@ ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user) continue; } - if (file[i].buffer && file[i].pos - file[i].buffer != 0) { - ngx_write_fd(file[i].fd, file[i].buffer, - file[i].pos - file[i].buffer); + len = file[i].pos - file[i].buffer; + + if (file[i].buffer && len != 0) { + + n = ngx_write_fd(file[i].fd, file[i].buffer, len); + + if (n == NGX_FILE_ERROR) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, + ngx_write_fd_n " to \"%s\" failed", + file[i].name.data); + + } else if (n != len) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, + ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz", + file[i].name.data, n, len); + } + file[i].pos = file[i].buffer; } diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c index 1059acf43..e3abcd780 100644 --- a/src/core/ngx_log.c +++ b/src/core/ngx_log.c @@ -158,7 +158,7 @@ ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, ngx_linefeed(p); - ngx_write_fd(log->file->fd, errstr, p - errstr); + (void) ngx_write_fd(log->file->fd, errstr, p - errstr); } diff --git a/src/os/unix/ngx_files.h b/src/os/unix/ngx_files.h index a223b34e8..419f2fba0 100644 --- a/src/os/unix/ngx_files.h +++ b/src/os/unix/ngx_files.h @@ -68,7 +68,17 @@ ssize_t ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *ce, #define ngx_read_fd read #define ngx_read_fd_n "read()" -#define ngx_write_fd write +/* + * we use inlined function instead of simple #define + * because glibc 2.3 sets warn_unused_result attribute for write() + * and in this case gcc 4.3 ignores (void) cast + */ +static ngx_inline ssize_t +ngx_write_fd(ngx_fd_t fd, void *buf, size_t n) +{ + return write(fd, buf, n); +} + #define ngx_write_fd_n "write()" #define ngx_linefeed(p) *p++ = LF;