From 096983712809f4bda2b1b334c754e13962db9c8f Mon Sep 17 00:00:00 2001
From: Lukas Slebodnik <lslebodn@redhat.com>
Date: Fri, 8 Mar 2013 15:27:14 +0100
Subject: [PATCH 2/2] More generalized function open_debug_file_ex()

Function open_debug_file_ex() set flag FD_CLOEXEC to opened
file according to the value of third parameter.

Removed duplicity of unsetting FD_CLOEXEC after calling function
open_debug_file_ex()
---
 src/providers/krb5/krb5_init_shared.c   |  5 +----
 src/providers/ldap/sdap_child_helpers.c |  5 +----
 src/util/debug.c                        | 10 ++++++----
 src/util/util.h                         |  2 +-
 4 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/src/providers/krb5/krb5_init_shared.c b/src/providers/krb5/krb5_init_shared.c
index 312c695ea1c8b8e03701db4185ba6f168d80cc8b..b30e9d209bb4ec16e8b2367ca0acf9819628edec 100644
--- a/src/providers/krb5/krb5_init_shared.c
+++ b/src/providers/krb5/krb5_init_shared.c
@@ -71,7 +71,7 @@ errno_t krb5_child_init(struct krb5_ctx *krb5_auth_ctx,
     }
 
     if (debug_to_file != 0) {
-        ret = open_debug_file_ex(KRB5_CHILD_LOG_FILE, &debug_filep);
+        ret = open_debug_file_ex(KRB5_CHILD_LOG_FILE, &debug_filep, false);
         if (ret != EOK) {
             DEBUG(0, ("Error setting up logging (%d) [%s]\n",
                     ret, strerror(ret)));
@@ -84,9 +84,6 @@ errno_t krb5_child_init(struct krb5_ctx *krb5_auth_ctx,
             ret = errno;
             goto done;
         }
-
-        v = fcntl(krb5_auth_ctx->child_debug_fd, F_GETFD, 0);
-        fcntl(krb5_auth_ctx->child_debug_fd, F_SETFD, v & ~FD_CLOEXEC);
     }
 
 done:
diff --git a/src/providers/ldap/sdap_child_helpers.c b/src/providers/ldap/sdap_child_helpers.c
index f2412f9e59a8ee3c6d1651b6b96ce2fd36c4fdd1..cc265e55b29a6c787859439b68d3c5869b826c49 100644
--- a/src/providers/ldap/sdap_child_helpers.c
+++ b/src/providers/ldap/sdap_child_helpers.c
@@ -457,7 +457,7 @@ int setup_child(struct sdap_id_ctx *ctx)
     FILE *debug_filep;
 
     if (debug_to_file != 0 && ldap_child_debug_fd == -1) {
-        ret = open_debug_file_ex(LDAP_CHILD_LOG_FILE, &debug_filep);
+        ret = open_debug_file_ex(LDAP_CHILD_LOG_FILE, &debug_filep, false);
         if (ret != EOK) {
             DEBUG(0, ("Error setting up logging (%d) [%s]\n",
                         ret, strerror(ret)));
@@ -470,9 +470,6 @@ int setup_child(struct sdap_id_ctx *ctx)
             ret = errno;
             return ret;
         }
-
-        v = fcntl(ldap_child_debug_fd, F_GETFD, 0);
-        fcntl(ldap_child_debug_fd, F_SETFD, v & ~FD_CLOEXEC);
     }
 
     return EOK;
diff --git a/src/util/debug.c b/src/util/debug.c
index 54fb8011be7a4c787b201314f1f08d1b46d3b66c..3bf5e75af0d7024ee7322f0c3574669a3e8bb4c0 100644
--- a/src/util/debug.c
+++ b/src/util/debug.c
@@ -173,7 +173,7 @@ void ldb_debug_messages(void *context, enum ldb_debug_level level,
     free(message);
 }
 
-int open_debug_file_ex(const char *filename, FILE **filep)
+int open_debug_file_ex(const char *filename, FILE **filep, bool want_cloexec)
 {
     FILE *f = NULL;
     char *logpath;
@@ -214,8 +214,10 @@ int open_debug_file_ex(const char *filename, FILE **filep)
         return EIO;
     }
 
-    flags = fcntl(debug_fd, F_GETFD, 0);
-    (void) fcntl(debug_fd, F_SETFD, flags | FD_CLOEXEC);
+    if(want_cloexec) {
+        flags = fcntl(debug_fd, F_GETFD, 0);
+        (void) fcntl(debug_fd, F_SETFD, flags | FD_CLOEXEC);
+    }
 
     if (filep == NULL) {
         debug_file = f;
@@ -228,7 +230,7 @@ int open_debug_file_ex(const char *filename, FILE **filep)
 
 int open_debug_file(void)
 {
-    return open_debug_file_ex(NULL, NULL);
+    return open_debug_file_ex(NULL, NULL, true);
 }
 
 int rotate_debug_files(void)
diff --git a/src/util/util.h b/src/util/util.h
index 2adc8cb4180ae802afee63d084c3d1d67f273862..351a29a4626a765ae44954646de9701754168e89 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -336,7 +336,7 @@ safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter)
 /* From debug.c */
 void ldb_debug_messages(void *context, enum ldb_debug_level level,
                         const char *fmt, va_list ap);
-int open_debug_file_ex(const char *filename, FILE **filep);
+int open_debug_file_ex(const char *filename, FILE **filep, bool want_cloexec);
 int open_debug_file(void);
 int rotate_debug_files(void);
 void talloc_log_fn(const char *msg);
-- 
1.8.1.4

