]> git.openfabrics.org - ~shefty/librdmacm.git/commitdiff
Open files with "close on exec" flag
authorYann Droneaud <ydroneaud@opteya.com>
Tue, 16 Jul 2013 21:59:52 +0000 (23:59 +0200)
committerSean Hefty <sean.hefty@intel.com>
Tue, 16 Jul 2013 23:07:21 +0000 (16:07 -0700)
File opened by librdmacm are not supposed to be inherited across
exec*(), most of the files are of no use for another program, and
others cannot be used without the associated memory mapping.

This patch changes fopen() open() and socket() to always set
close on exec flag.

This patch also add checks to configure to guess if fopen() supports
"e" flag. If O_CLOEXEC and SOCK_CLOEXEC are supported, fopen() should
support "e". If not supported, its discarded according to POSIX. Many
operating systems have support for fopen("e").

You might find more information about close on exec in the following articles:

- "Excuse me son, but your code is leaking !!!" by Dan Walsh
  http://danwalsh.livejournal.com/53603.html

- "Secure File Descriptor Handling" by Ulrich Drepper
  http://udrepper.livejournal.com/20407.html

Note: this patch won't set close on exec flag on file descriptors
created by the kernel for completion channel and such.
This is addressed by another kernel patch.

Signed-off-by: Yann Droneaud <ydroneaud@opteya.com>
Signed-off-by: Sean Hefty <sean.hefty@intel.com>
configure.ac
src/acm.c
src/cma.c

index 9fd9e9e662261661c2e09c99e5236f9b14134f42..6163f564d053543f39fb425845bc71b8c6bc3fc2 100644 (file)
@@ -75,6 +75,30 @@ AC_CHECK_HEADER(infiniband/acm.h,
                AC_DEFINE(DEFINE_ACM_MSG, 1, [adding ACM message definition]),
                        [#include <infiniband/acm.h>]), [])
 
+dnl Checks close on exec support
+AC_CHECK_HEADERS([fcntl.h sys/socket.h])
+
+AC_CHECK_DECLS([O_CLOEXEC],,[AC_DEFINE([O_CLOEXEC],[0], [Defined to 0 if not provided])],
+[[
+#ifdef HAVE_FCNTL_H
+# include <fcntl.h>
+#endif
+]])
+AC_CHECK_DECLS([SOCK_CLOEXEC],,[AC_DEFINE([SOCK_CLOEXEC],[0],[Defined to 0 if not provided])],
+[[
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+]])
+
+AC_CACHE_CHECK(for close on exec modifier for fopen(), ac_cv_feature_stream_cloexec_flag,
+   [if test $ac_cv_have_decl_O_CLOEXEC = yes ; then
+        if test $ac_cv_have_decl_SOCK_CLOEXEC = yes ; then
+            ac_cv_feature_stream_cloexec_flag="e"
+        fi
+    fi])
+AC_DEFINE_UNQUOTED([STREAM_CLOEXEC], "$ac_cv_feature_stream_cloexec_flag", [fopen() modifier for setting close on exec flag])
+
 AC_CACHE_CHECK(whether ld accepts --version-script, ac_cv_version_script,
     if test -n "`$LD --help < /dev/null 2>/dev/null | grep version-script`"; then
         ac_cv_version_script=yes
index c423bb7290332a6f4395f72eede3c9c20d3e4276..dfc49599e72c86a228a67e227d039eaf7eb712d9 100755 (executable)
--- a/src/acm.c
+++ b/src/acm.c
@@ -79,7 +79,7 @@ static int ucma_set_server_port(void)
 {
        FILE *f;
 
-       if ((f = fopen("/var/run/ibacm.port", "r"))) {
+       if ((f = fopen("/var/run/ibacm.port", "r" STREAM_CLOEXEC))) {
                fscanf(f, "%hu", (unsigned short *) &server_port);
                fclose(f);
        }
@@ -99,7 +99,7 @@ void ucma_ib_init(void)
        if (!ucma_set_server_port())
                goto out;
 
-       sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+       sock = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
        if (sock < 0)
                goto out;
 
index 2fb9913ae6c69c29c74a27ff32955ca5fa7f3eea..baebecdd96322341bc59f20dc093554609ac9d5e 100755 (executable)
--- a/src/cma.c
+++ b/src/cma.c
@@ -328,7 +328,7 @@ struct rdma_event_channel *rdma_create_event_channel(void)
        if (!channel)
                return NULL;
 
-       channel->fd = open("/dev/infiniband/rdma_cm", O_RDWR);
+       channel->fd = open("/dev/infiniband/rdma_cm", O_RDWR | O_CLOEXEC);
        if (channel->fd < 0) {
                fprintf(stderr, PFX "Fatal: unable to open /dev/infiniband/rdma_cm\n");
                goto err;