wrowe 02/01/07 19:45:09
Modified: file_io/unix filedup.c
Log:
We cannot close-on-fork any fd 0 through 2 (stdin, stdout, stderr)!!!
This patch is possibly still borked, we probably should remove any
existing cleanup registered again (*new_file) if it was given.
This api really is dirty, should really have an apr_file_dup2() with
different conventions (passing apr_file_t* for both left and right args.)
I can see users 'forgetting' to null the target apr_file_t** destination.
Revision Changes Path
1.37 +13 -4 apr/file_io/unix/filedup.c
Index: filedup.c
===================================================================
RCS file: /home/cvs/apr/file_io/unix/filedup.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -r1.36 -r1.37
--- filedup.c 6 Dec 2001 13:43:45 -0000 1.36
+++ filedup.c 8 Jan 2002 03:45:09 -0000 1.37
@@ -92,11 +92,20 @@
/* make sure unget behavior is consistent */
(*new_file)->ungetchar = old_file->ungetchar;
/* apr_file_dup() clears the inherit attribute, user must call
- * apr_file_set_inherit() again on the dupped handle, as necessary.
+ * apr_file_set_inherit() again on the dupped handle, as necessary,
+ * unless you have dup2'ed fd 0-2 (stdin, stdout or stderr) which
+ * should never, never, never close on fork()
*/
- (*new_file)->flags = old_file->flags & ~APR_INHERIT;
- apr_pool_cleanup_register((*new_file)->cntxt, (void *)(*new_file),
- apr_unix_file_cleanup, apr_unix_file_cleanup);
+ if (have_file && old_file->filedes >= 0 and old_file->filedes <= 2) {
+ (*new_file)->flags = old_file->flags | APR_INHERIT;
+ apr_pool_cleanup_register((*new_file)->cntxt, (void *)(*in),
+ apr_unix_file_cleanup,
apr_pool_cleanup_null);
+ }
+ else {
+ (*new_file)->flags = old_file->flags & ~APR_INHERIT;
+ apr_pool_cleanup_register((*new_file)->cntxt, (void *)(*new_file),
+ apr_unix_file_cleanup,
apr_unix_file_cleanup);
+ }
return APR_SUCCESS;
}