From 85ca83a7caf31cbe741c579e05ed9ffdd3aeb974 Mon Sep 17 00:00:00 2001
From: Pavel Modilaynen <pavelmn@axis.com>
Date: Fri, 26 Apr 2019 09:12:18 +0200
Subject: [PATCH] xargs: use GNU_FINDUTILS_FD_LEAK_CHECK as for find

Utilize GNU_FINDUTILS_FD_LEAK_CHECK environment variable
to enable/disable fd leak check for xargs the same way as
for find.

* find/defs.h: Remove prototype for fd_leak_check_is_enabled().
* find/util.c: Remove implementation of fd_leak_check_is_enabled().
* lib/fdleak.c: Add implementation of fd_leak_check_is_enabled().
* lib/fdleak.h: Add prototype for fd_leak_check_is_enabled().
* xargs/testsuite/config/unix.exp: Enable GNU_FINDUTILS_FD_LEAK_CHECK
for all tests.
* xargs/xargs.c: Execute complain_about_leaky_fds when
fd_leak_check_is_enabled returns true.
* NEWS (Improvements): Document support of GNU_FINDUTILS_FD_LEAK_CHECK
by xargs.

Change-Id: I0686340e36ac38addc2508df7e3b660cd6eb5985
---
 NEWS                            |  3 +++
 find/defs.h                     |  1 -
 find/util.c                     | 12 ------------
 lib/fdleak.c                    | 11 +++++++++++
 lib/fdleak.h                    |  3 +++
 xargs/testsuite/config/unix.exp |  2 ++
 xargs/xargs.c                   |  5 ++++-
 7 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/NEWS b/NEWS
index 47338232..0d161a58 100644
--- a/NEWS
+++ b/NEWS
@@ -49,6 +49,9 @@ xargs now supports the -o, --open-tty option to reopen stdin as /dev/tty
 in the child process before executing the command; useful to run an
 interactive application.  Added for compatibility with BSD.
 
+xargs now supports GNU_FINDUTILS_FD_LEAK_CHECK environment variable to
+enable/disable fd leak check.
+
 ** Documentation Changes
 
 Prefer https:// over http:// links where possible, e.g. for '*.gnu.org' servers.
diff --git a/find/defs.h b/find/defs.h
index 6e79ee00..9b5e506a 100644
--- a/find/defs.h
+++ b/find/defs.h
@@ -498,7 +498,6 @@ struct predicate *get_new_pred_chk_op (const struct parser_table *entry,
 float  calculate_derived_rates (struct predicate *p);
 
 /* util.c */
-bool fd_leak_check_is_enabled (void);
 struct predicate *insert_primary (const struct parser_table *entry, const char *arg);
 struct predicate *insert_primary_noarg (const struct parser_table *entry);
 struct predicate *insert_primary_withpred (const struct parser_table *entry, PRED_FUNC fptr, const char *arg);
diff --git a/find/util.c b/find/util.c
index 7d8e9677..aa387a47 100644
--- a/find/util.c
+++ b/find/util.c
@@ -545,18 +545,6 @@ undangle_file_pointers (struct predicate *p)
     }
 }
 
-/* Return nonzero if file descriptor leak-checking is enabled.
- */
-bool
-fd_leak_check_is_enabled (void)
-{
-  if (getenv ("GNU_FINDUTILS_FD_LEAK_CHECK"))
-    return true;
-  else
-    return false;
-
-}
-
 /* Complete any outstanding commands.
  * Flush and close any open files.
  */
diff --git a/lib/fdleak.c b/lib/fdleak.c
index 68061d56..9a6ab42a 100644
--- a/lib/fdleak.c
+++ b/lib/fdleak.c
@@ -366,6 +366,17 @@ forget_non_cloexec_fds (void)
   num_cloexec_fds = 0;
 }
 
+/* Return nonzero if file descriptor leak-checking is enabled.
+ */
+bool
+fd_leak_check_is_enabled (void)
+{
+  if (getenv ("GNU_FINDUTILS_FD_LEAK_CHECK"))
+    return true;
+  else
+    return false;
+
+}
 
 void
 complain_about_leaky_fds (void)
diff --git a/lib/fdleak.h b/lib/fdleak.h
index a3c8c3a4..2979dc31 100644
--- a/lib/fdleak.h
+++ b/lib/fdleak.h
@@ -17,9 +17,12 @@
 #ifndef FDLEAK_H
 # define FDLEAK_H
 
+# include <stdbool.h>		/* for bool */
+
 void remember_non_cloexec_fds (void);
 void forget_non_cloexec_fds (void);
 void complain_about_leaky_fds (void);
+bool fd_leak_check_is_enabled (void);
 
 int open_cloexec(const char *path, int flags, ...);
 
diff --git a/xargs/testsuite/config/unix.exp b/xargs/testsuite/config/unix.exp
index 177235eb..44fee292 100644
--- a/xargs/testsuite/config/unix.exp
+++ b/xargs/testsuite/config/unix.exp
@@ -21,6 +21,8 @@
 
 verbose "base_dir is $base_dir" 2
 
+set env(GNU_FINDUTILS_FD_LEAK_CHECK) "1"
+
 set objfile "xargs.o"
 set dir "$base_dir/.."
 set path "$dir/$objfile"
diff --git a/xargs/xargs.c b/xargs/xargs.c
index 3d00080d..913328b8 100644
--- a/xargs/xargs.c
+++ b/xargs/xargs.c
@@ -1165,7 +1165,10 @@ set_slot_var (unsigned int n)
 static void
 prep_child_for_exec (void)
 {
-  complain_about_leaky_fds ();
+  if (fd_leak_check_is_enabled ())
+    {
+      complain_about_leaky_fds ();
+    }
 
   /* The parent will call add_proc to allocate a slot.  We do the same in the
      child to make sure we get the same value.
-- 
2.11.0

