changeset 1523:e5b52720f539

Use O_CLOEXEC instead of O_RDONLY to signal loopfiles_rw() to close filehandles.
author Rob Landley <rob@landley.net>
date Tue, 14 Oct 2014 14:16:34 -0500
parents fc2200f927af
children 2ce0f52103a9
files lib/lib.c toys/other/truncate.c toys/posix/cmp.c
diffstat 3 files changed, 7 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.c	Tue Oct 14 11:40:03 2014 -0500
+++ b/lib/lib.c	Tue Oct 14 14:16:34 2014 -0500
@@ -421,8 +421,8 @@
 // flags is O_RDONLY, stdout otherwise.  An empty argument list calls
 // function() on just stdin/stdout.
 //
-// Note: read only filehandles are automatically closed when function()
-// returns, but writeable filehandles must be close by function()
+// Note: pass O_CLOEXEC to automatically close filehandles when function()
+// returns, otherwise filehandles must be closed by function()
 void loopfiles_rw(char **argv, int flags, int permissions, int failok,
   void (*function)(int fd, char *name))
 {
@@ -441,14 +441,14 @@
       continue;
     }
     function(fd, *argv);
-    if (flags == O_RDONLY) close(fd);
+    if (flags & O_CLOEXEC) close(fd);
   } while (*++argv);
 }
 
-// Call loopfiles_rw with O_RDONLY and !failok (common case).
+// Call loopfiles_rw with O_RDONLY|O_CLOEXEC and !failok (common case).
 void loopfiles(char **argv, void (*function)(int fd, char *name))
 {
-  loopfiles_rw(argv, O_RDONLY, 0, 0, function);
+  loopfiles_rw(argv, O_RDONLY|O_CLOEXEC, 0, 0, function);
 }
 
 // Slow, but small.
--- a/toys/other/truncate.c	Tue Oct 14 11:40:03 2014 -0500
+++ b/toys/other/truncate.c	Tue Oct 14 14:16:34 2014 -0500
@@ -35,6 +35,6 @@
 
   // Create files with mask rwrwrw.
   // Nonexistent files are only an error if we're supposed to create them.
-  loopfiles_rw(toys.optargs, O_WRONLY|(cr ? O_CREAT : 0), 0666, cr,
+  loopfiles_rw(toys.optargs, O_WRONLY|O_CLOEXEC|(cr ? O_CREAT : 0), 0666, cr,
     do_truncate);
 }
--- a/toys/posix/cmp.c	Tue Oct 14 11:40:03 2014 -0500
+++ b/toys/posix/cmp.c	Tue Oct 14 14:16:34 2014 -0500
@@ -79,6 +79,6 @@
 
 void cmp_main(void)
 {
-  loopfiles_rw(toys.optargs, O_RDONLY, 0, toys.optflags&FLAG_s, do_cmp);
+  loopfiles_rw(toys.optargs, O_CLOEXEC, 0, toys.optflags&FLAG_s, do_cmp);
 }