Error path does not close fd

Bart Warmerdam bartw at xs4all.nl
Tue Oct 13 21:58:04 UTC 2015



Hello,

In the ngx_daemon.c the dup2 result code is checked but the earlier
opened /dev/null handle is not closed in case of an error. Please
consider this path to add to the source base.

Regards,

B.


# HG changeset patch
# User Bart Warmerdam <bartw at xs4all.nl>
# Date 1444773372 -7200
#      Tue Oct 13 23:56:12 2015 +0200
# Branch close_fd_on_error
# Node ID e1e25db76cdf7583f1145b91b9dbcdff417d6f16
# Parent  2f34ea503ac4e015cc08f6efbb279b360eda609c
Close file handle on error as well

diff -r 2f34ea503ac4 -r e1e25db76cdf src/os/unix/ngx_daemon.c
--- a/src/os/unix/ngx_daemon.c	Wed Oct 07 22:19:42 2015 +0300
+++ b/src/os/unix/ngx_daemon.c	Tue Oct 13 23:56:12 2015 +0200
@@ -9,6 +9,20 @@
 #include <ngx_core.h>
 
 
+static ngx_int_t
+ngx_close_handle(ngx_log_t *log, int fd)
+{
+    if (fd > STDERR_FILENO) {
+        if (close(fd) == -1) {
+            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close()
failed");
+            return NGX_ERROR;
+        }
+    }
+
+    return NGX_OK;
+}
+
+
 ngx_int_t
 ngx_daemon(ngx_log_t *log)
 {
@@ -44,26 +58,26 @@
 
     if (dup2(fd, STDIN_FILENO) == -1) {
         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDIN)
failed");
+        ngx_close_handle(log, fd);
         return NGX_ERROR;
     }
 
     if (dup2(fd, STDOUT_FILENO) == -1) {
         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDOUT)
failed");
+        ngx_close_handle(log, fd);
         return NGX_ERROR;
     }
 
 #if 0
     if (dup2(fd, STDERR_FILENO) == -1) {
         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDERR)
failed");
+        ngx_close_handle(log, fd);
         return NGX_ERROR;
     }
 #endif
 
-    if (fd > STDERR_FILENO) {
-        if (close(fd) == -1) {
-            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close()
failed");
-            return NGX_ERROR;
-        }
+    if (ngx_close_handle(log, fd) == NGX_ERROR) {
+        return NGX_ERROR;
     }
 
     return NGX_OK;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20151013/26bee341/attachment.html>


More information about the nginx-devel mailing list