At 11:18 PM 3/18/2003, William A. Rowe, Jr. wrote:
>Thanks to the feedback from Joe and Bjoern, I have committed a ton and 1/2
>of bug fixes to apr's open.c and dupfile.c.
>
>After those changes, this patch is much easier to read. Please take a look
>and comment on anything you see that might be amiss.
I swear the cat must have walked across the keyboard... I never clicked
send that *I* remember. Anyways, patch is attached.
Index: file_io/unix/filedup.c
===================================================================
RCS file: /home/cvs/apr/file_io/unix/filedup.c,v
retrieving revision 1.59
diff -u -r1.59 filedup.c
--- file_io/unix/filedup.c 19 Mar 2003 03:34:47 -0000 1.59
+++ file_io/unix/filedup.c 19 Mar 2003 05:00:34 -0000
@@ -129,6 +129,19 @@
(*new_file)->flags = old_file->flags & ~APR_INHERIT;
}
+#ifdef FD_CLOEXEC
+ if (!((*new_file)->flags & APR_INHERIT)) {
+ int ffd = fcntl((*new_file)->filedes, F_GETFD);
+ if (ffd >= 0)
+ ffd = fcntl((*new_file)->filedes, F_SETFD, ffd | FD_CLOEXEC);
+ /* if (ffd < 0)
+ * XXX: What to do in this case? No good ideas.
+ * What exactly would the user do with this fault?
+ * We close with the child cleanup anyhow, given a chance.
+ */
+ }
+#endif
+
apr_pool_cleanup_register((*new_file)->pool, (void *)(*new_file),
apr_unix_file_cleanup,
((*new_file)->flags & APR_INHERIT)
Index: file_io/unix/open.c
===================================================================
RCS file: /home/cvs/apr/file_io/unix/open.c,v
retrieving revision 1.111
diff -u -r1.111 open.c
--- file_io/unix/open.c 19 Mar 2003 03:45:42 -0000 1.111
+++ file_io/unix/open.c 19 Mar 2003 05:00:34 -0000
@@ -190,6 +190,17 @@
(*new)->direction = 0;
if (!(flag & APR_FILE_NOCLEANUP)) {
+#ifdef FD_CLOEXEC
+ int ffd = fcntl((*new_file)->filedes, F_GETFD);
+ if (ffd >= 0)
+ ffd = fcntl((*new_file)->filedes, F_SETFD, ffd | FD_CLOEXEC);
+ /* if (ffd < 0)
+ * XXX: What to do in this case? No good ideas.
+ * What exactly would the user do with this fault?
+ * We close with the child cleanup anyhow, given a chance.
+ */
+#endif
+
apr_pool_cleanup_register((*new)->pool, (void *)(*new),
apr_unix_file_cleanup,
apr_unix_file_cleanup);
@@ -293,8 +304,8 @@
return apr_os_file_put(thefile, &fd, 0, pool);
}
-APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_unix_file_cleanup)
+APR_IMPLEMENT_INHERIT_SET(file, flags, filedes, pool, apr_unix_file_cleanup)
-APR_IMPLEMENT_INHERIT_UNSET(file, flags, pool, apr_unix_file_cleanup)
+APR_IMPLEMENT_INHERIT_UNSET(file, flags, filedes, pool, apr_unix_file_cleanup)
APR_POOL_IMPLEMENT_ACCESSOR(file)
Index: include/arch/unix/apr_arch_inherit.h
===================================================================
RCS file: /home/cvs/apr/include/arch/unix/apr_arch_inherit.h,v
retrieving revision 1.4
diff -u -r1.4 apr_arch_inherit.h
--- include/arch/unix/apr_arch_inherit.h 19 Mar 2003 04:35:11 -0000
1.4
+++ include/arch/unix/apr_arch_inherit.h 19 Mar 2003 05:00:34 -0000
@@ -59,7 +59,9 @@
#define APR_INHERIT (1 << 24) /* Must not conflict with other bits */
-#define APR_IMPLEMENT_INHERIT_SET(name, flag, pool, cleanup) \
+#ifndef FD_CLOEXEC
+
+#define APR_IMPLEMENT_INHERIT_SET(name, flag, fd, pool, cleanup) \
apr_status_t apr_##name##_inherit_set(apr_##name##_t *the##name) \
{ \
if (the##name->flag & APR_FILE_NOCLEANUP) \
@@ -78,7 +80,7 @@
apr_##name##_inherit_set(the##name); \
}
-#define APR_IMPLEMENT_INHERIT_UNSET(name, flag, pool, cleanup) \
+#define APR_IMPLEMENT_INHERIT_UNSET(name, flag, fd, pool, cleanup) \
apr_status_t apr_##name##_inherit_unset(apr_##name##_t *the##name) \
{ \
if (the##name->flag & APR_FILE_NOCLEANUP) \
@@ -96,5 +98,59 @@
{ \
apr_##name##_inherit_unset(the##name); \
}
+
+#else /* defined(FD_CLOEXEC) */
+
+#define APR_IMPLEMENT_INHERIT_SET(name, flag, fd, pool, cleanup) \
+apr_status_t apr_##name##_inherit_set(apr_##name##_t *the##name) \
+{ \
+ if (the##name->flag & APR_FILE_NOCLEANUP) \
+ return APR_EINVAL; \
+ if (!(the##name->flag & APR_INHERIT)) { \
+ int ffd; \
+ the##name->flag |= APR_INHERIT; \
+ apr_pool_child_cleanup_set(the##name->pool, \
+ (void *)the##name, \
+ cleanup, apr_pool_cleanup_null); \
+ ffd = fcntl(the##name->fd, F_GETFD); \
+ if (ffd >= 0) \
+ ffd = fcntl(the##name->fd, F_SETFD, ffd & ~FD_CLOEXEC); \
+ if (ffd < 0) \
+ return errno; \
+ } \
+ return APR_SUCCESS; \
+} \
+/* Deprecated */ \
+void apr_##name##_set_inherit(apr_##name##_t *the##name) \
+{ \
+ apr_##name##_inherit_set(the##name); \
+}
+
+#define APR_IMPLEMENT_INHERIT_UNSET(name, flag, fd, pool, cleanup) \
+apr_status_t apr_##name##_inherit_unset(apr_##name##_t *the##name) \
+{ \
+ if (the##name->flag & APR_FILE_NOCLEANUP) \
+ return APR_EINVAL; \
+ if (the##name->flag & APR_INHERIT) { \
+ int ffd; \
+ the##name->flag &= ~APR_INHERIT; \
+ apr_pool_child_cleanup_set(the##name->pool, \
+ (void *)the##name, \
+ cleanup, cleanup); \
+ ffd = fcntl(the##name->fd, F_GETFD); \
+ if (ffd >= 0) \
+ ffd = fcntl(the##name->fd, F_SETFD, ffd | FD_CLOEXEC); \
+ if (ffd < 0) \
+ return errno; \
+ } \
+ return APR_SUCCESS; \
+} \
+/* Deprecated */ \
+void apr_##name##_unset_inherit(apr_##name##_t *the##name) \
+{ \
+ apr_##name##_inherit_unset(the##name); \
+}
+
+#endif /* defined(FD_CLOEXEC) */
#endif /* ! INHERIT_H */
Index: network_io/unix/sockets.c
===================================================================
RCS file: /home/cvs/apr/network_io/unix/sockets.c,v
retrieving revision 1.107
diff -u -r1.107 sockets.c
--- network_io/unix/sockets.c 6 Jan 2003 23:44:35 -0000 1.107
+++ network_io/unix/sockets.c 19 Mar 2003 05:00:35 -0000
@@ -141,6 +141,20 @@
(*new)->timeout = -1;
(*new)->inherit = 0;
+
+#if defined(FD_CLOEXEC) && APR_FILES_AS_SOCKETS
+ {
+ int ffd = fcntl((*new)->socketdes, F_GETFD);
+ if (ffd >= 0)
+ ffd = fcntl((*new)->socketdes, F_SETFD, ffd | FD_CLOEXEC);
+ /* if (ffd < 0)
+ * XXX: What to do in this case? No good ideas.
+ * What exactly would the user do with this fault?
+ * We close with the child cleanup anyhow, given a chance.
+ */
+ }
+#endif
+
apr_pool_cleanup_register((*new)->cntxt, (void *)(*new), socket_cleanup,
socket_cleanup);
return APR_SUCCESS;
@@ -253,6 +267,20 @@
}
(*new)->inherit = 0;
+
+#if defined(FD_CLOEXEC) && APR_FILES_AS_SOCKETS
+ {
+ int ffd = fcntl((*new)->socketdes, F_GETFD);
+ if (ffd >= 0)
+ ffd = fcntl((*new)->socketdes, F_SETFD, ffd | FD_CLOEXEC);
+ /* if (ffd < 0)
+ * XXX: What to do in this case? No good ideas.
+ * What exactly would the user do with this fault?
+ * We close with the child cleanup anyhow, given a chance.
+ */
+ }
+#endif
+
apr_pool_cleanup_register((*new)->cntxt, (void *)(*new), socket_cleanup,
socket_cleanup);
return APR_SUCCESS;
@@ -369,8 +397,23 @@
}
(*apr_sock)->inherit = 0;
+
+#if defined(FD_CLOEXEC) && APR_FILES_AS_SOCKETS
+ {
+ int ffd = fcntl((*new)->socketdes, F_GETFD);
+ if (ffd >= 0)
+ ffd = fcntl((*new)->socketdes, F_SETFD, ffd | FD_CLOEXEC);
+ /* if (ffd < 0)
+ * XXX: What to do in this case? No good ideas.
+ * What exactly would the user do with this fault?
+ * We close with the child cleanup anyhow, given a chance.
+ */
+ }
+#endif
+
apr_pool_cleanup_register((*apr_sock)->cntxt, (void *)(*apr_sock),
socket_cleanup, socket_cleanup);
+
return APR_SUCCESS;
}
@@ -389,12 +432,14 @@
(*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
(*sock)->remote_addr_unknown = 1;
(*sock)->socketdes = *thesock;
+ /* We register no cleanup - inhibit apr_socket_inherit_[un]set */
+ (*apr_sock)->inherit = APR_FILE_NOCLEANUP;
return APR_SUCCESS;
}
-APR_IMPLEMENT_INHERIT_SET(socket, inherit, cntxt, socket_cleanup)
+APR_IMPLEMENT_INHERIT_SET(socket, inherit, socketdes, cntxt, socket_cleanup)
-APR_IMPLEMENT_INHERIT_UNSET(socket, inherit, cntxt, socket_cleanup)
+APR_IMPLEMENT_INHERIT_UNSET(socket, inherit, socketdes, cntxt, socket_cleanup)
/* deprecated */
apr_status_t apr_shutdown(apr_socket_t *thesocket, apr_shutdown_how_e how)