---

** [tickets:#1337] Opensaf : Optimize  fcntl() call for  sckts/fd  O_NONBLOCK & 
FD_CLOEXEC **

**Status:** assigned
**Milestone:** future
**Created:** Fri Apr 24, 2015 05:02 AM UTC by A V Mahesh (AVM)
**Last Updated:** Fri Apr 24, 2015 05:02 AM UTC
**Owner:** A V Mahesh (AVM)

Opensaf : Optimize  fcntl() call for  sckts/fd  O_NONBLOCK & FD_CLOEXEC

Now all the kernel supports  SOCK_CLOEXEC | SOCK_NONBLOCK while creating 
socket()
so it is not required to have explicit setting of  FD_CLOEXEC &  O_NONBLOCK  
using fcntl()
it cleanup some code in opensaf.

Example changes :
===================

diff --git a/opensaf/osaf/libs/core/mds/mds_dt_tcp.c 
b/opensaf/osaf/libs/core/mds/mds_dt_tcp.c
--- a/opensaf/osaf/libs/core/mds/mds_dt_tcp.c
+++ b/opensaf/osaf/libs/core/mds/mds_dt_tcp.c
@@ -81,7 +81,6 @@ uint32_t mdtm_process_recv_events_tcp(vo
  */
 uint32_t mds_mdtm_init_tcp(NODE_ID nodeid, uint32_t *mds_tcp_ref)
 {
-       uint32_t flags;
        uint32_t sndbuf_size = 0; /* Send buffer size */
        uint32_t rcvbuf_size = 0;  /* Receive buffer size */
        socklen_t optlen; /* Option length */
@@ -125,7 +124,7 @@ uint32_t mds_mdtm_init_tcp(NODE_ID nodei
 
        /* Create the sockets required for Binding, Send, receive and Discovery 
*/
 
-       tcp_cb->DBSRsock = socket(mds_socket_domain, SOCK_STREAM, 0);
+       tcp_cb->DBSRsock = socket(mds_socket_domain, 
SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
        if (tcp_cb->DBSRsock < 0) {
                syslog(LOG_ERR, "MDS:MDTM:TCP DBSRsock Socket creation failed 
in MDTM_INIT err :%s", strerror(errno));
                return NCSCC_RC_FAILURE;
@@ -177,19 +176,6 @@ uint32_t mds_mdtm_init_tcp(NODE_ID nodei
                return NCSCC_RC_FAILURE;
        }
 
-       flags = fcntl(tcp_cb->DBSRsock, F_GETFD, 0);
-       if ((flags < 0) || (flags > 1)) {
-               syslog(LOG_ERR, "MDS:MDTM:TCP Unable to get the CLOEXEC Flag on 
DBSRsock  err :%s", strerror(errno));
-               close(tcp_cb->DBSRsock);
-               return NCSCC_RC_FAILURE;
-       } else {
-               if (fcntl(tcp_cb->DBSRsock, F_SETFD, (flags | FD_CLOEXEC)) == 
(-1)) {
-                       syslog(LOG_ERR, "MDS:MDTM:TCP Unable to set the CLOEXEC 
Flag on DBSRsock err :%s", strerror(errno));
-                       close(tcp_cb->DBSRsock);
-                       return NCSCC_RC_FAILURE;
-               }
-       }
-


@@ -256,18 +242,6 @@ uint32_t mds_mdtm_init_tcp(NODE_ID nodei
                return NCSCC_RC_FAILURE;
        }
 
-       /* Convert the socket to a Non-blocking socket */
-       if ((flags = fcntl(tcp_cb->DBSRsock, F_GETFL, NULL)) < 0) {
-               syslog(LOG_ERR, "MDS:MDTM:TCP Unable to set the  F_GETFL 
O_NONBLOCK Flag on DBSRsock  err :%s", strerror(errno));
-               close(tcp_cb->DBSRsock);
-               return NCSCC_RC_FAILURE;
-       }
-       flags |= O_NONBLOCK;
-       if (fcntl(tcp_cb->DBSRsock, F_SETFL, flags) < 0) {
-               syslog(LOG_ERR, "MDS:MDTM:TCP Unable to set the F_SETFL  
O_NONBLOCK Flag on DBSRsock  err :%s", strerror(errno));
-               close(tcp_cb->DBSRsock);
-               return NCSCC_RC_FAILURE;
-       }




--- a/opensaf/osaf/libs/core/mds/mds_dt_tipc.c
+++ b/opensaf/osaf/libs/core/mds/mds_dt_tipc.c
@@ -149,7 +149,6 @@ uint32_t mdtm_global_frag_num;
 uint32_t mdtm_tipc_init(NODE_ID nodeid, uint32_t *mds_tipc_ref)
 {
        uint32_t tipc_node_id = 0;
-       int flags;
 
        NCS_PATRICIA_PARAMS pat_tree_params;
 
@@ -174,46 +173,27 @@ uint32_t mdtm_tipc_init(NODE_ID nodeid, 
 
        /* Create the sockets required for Binding, Send, receive and Discovery 
*/
 
-       tipc_cb.Dsock = socket(AF_TIPC, SOCK_SEQPACKET, 0);
+       tipc_cb.Dsock = socket(AF_TIPC, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
        if (tipc_cb.Dsock < 0) {
                syslog(LOG_ERR, "MDS:MDTM:TIPC Dsock Socket creation failed in 
MDTM_INIT err :%s", strerror(errno));
                return NCSCC_RC_FAILURE;
        }
-       tipc_cb.BSRsock = socket(AF_TIPC, SOCK_RDM, 0);
+       tipc_cb.BSRsock = socket(AF_TIPC, SOCK_RDM|SOCK_CLOEXEC, 0);
        if (tipc_cb.BSRsock < 0) {
                syslog(LOG_ERR, "MDS:MDTM:TIPC BSRsock Socket creation failed 
in MDTM_INIT err :%s", strerror(errno));
                return NCSCC_RC_FAILURE;
        }
 
-       flags = fcntl(tipc_cb.Dsock, F_GETFD, 0);
-       if ((flags < 0) || (flags > 1)) {
-               syslog(LOG_ERR, "MDS:MDTM:TIPC Unable to get the CLOEXEC Flag 
on Dsock err :%s", strerror(errno));



--- a/opensaf/osaf/libs/core/leap/os_defs.c
+++ b/opensaf/osaf/libs/core/leap/os_defs.c
@@ -1059,19 +1059,23 @@ uint32_t ncs_sel_obj_create(NCS_SEL_OBJ 
 {
        int s_pair[2];
        int flags = 0;
+       uint64_t retval = 0;
 
        osaf_mutex_lock_ordie(&s_cloexec_mutex);
-       if (0 != socketpair(AF_UNIX, SOCK_STREAM, 0, s_pair)) {
+       retval = socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, s_pair);
+       if (retval < 0 && (errno == EINVAL || errno == EPROTOTYPE)) {
                syslog(LOG_ERR, "%s: socketpair failed - %s", __FUNCTION__, 
strerror(errno));
                osaf_mutex_unlock_ordie(&s_cloexec_mutex);
                return NCSCC_RC_FAILURE;
        }
-
-       flags = fcntl(s_pair[0], F_GETFD, 0);
-       fcntl(s_pair[0], F_SETFD, (flags | FD_CLOEXEC));
-
-       flags = fcntl(s_pair[1], F_GETFD, 0);
-       fcntl(s_pair[1], F_SETFD, (flags | FD_CLOEXEC));
+       int fl1 = fcntl(s_pair[0], F_GETFD);
+       int fl2 = fcntl(s_pair[1], F_GETFD);
+       if ((fl1 & FD_CLOEXEC) == 0 || (fl2 & FD_CLOEXEC) == 0) {
+               syslog(LOG_ERR, "socketpair(SOCK_CLOEXEC) did not set CLOEXEC");
+               close (s_pair[0]);
+               close (s_pair[1]);
+               return NCSCC_RC_FAILURE;
+       }


---

Sent from sourceforge.net because [email protected] is 
subscribed to https://sourceforge.net/p/opensaf/tickets/

To unsubscribe from further messages, a project admin can change settings at 
https://sourceforge.net/p/opensaf/admin/tickets/options.  Or, if this is a 
mailing list, you can unsubscribe from the mailing list.
------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Opensaf-tickets mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensaf-tickets

Reply via email to