Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
L
libzmq
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
libzmq
Commits
44da0e7e
Commit
44da0e7e
authored
May 23, 2018
by
Simon Giesecke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Problem: code duplication in making sockets non-inheritable
Solution: extracted make_socket_noninheritable function
parent
c432aada
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
54 deletions
+32
-54
ip.cpp
src/ip.cpp
+25
-27
ip.hpp
src/ip.hpp
+4
-0
ipc_listener.cpp
src/ipc_listener.cpp
+1
-7
tcp_listener.cpp
src/tcp_listener.cpp
+2
-20
No files found.
src/ip.cpp
View file @
44da0e7e
...
...
@@ -84,19 +84,7 @@ zmq::fd_t zmq::open_socket (int domain_, int type_, int protocol_)
return
retired_fd
;
}
// If there's no SOCK_CLOEXEC, let's try the second best option. Note that
// race condition can cause socket not to be closed (if fork happens
// between socket creation and this point).
#if !defined ZMQ_HAVE_SOCK_CLOEXEC && defined FD_CLOEXEC
rc
=
fcntl
(
s
,
F_SETFD
,
FD_CLOEXEC
);
errno_assert
(
rc
!=
-
1
);
#endif
// On Windows, preventing sockets to be inherited by child processes.
#if defined ZMQ_HAVE_WINDOWS && defined HANDLE_FLAG_INHERIT
BOOL
brc
=
SetHandleInformation
((
HANDLE
)
s
,
HANDLE_FLAG_INHERIT
,
0
);
win_assert
(
brc
);
#endif
make_socket_noninheritable
(
s
);
// Socket is not yet connected so EINVAL is not a valid networking error
rc
=
zmq
::
set_nosigpipe
(
s
);
...
...
@@ -521,11 +509,7 @@ int zmq::make_fdpair (fd_t *r_, fd_t *w_)
}
if
(
*
r_
!=
INVALID_SOCKET
)
{
#if !defined _WIN32_WCE && !defined ZMQ_HAVE_WINDOWS_UWP
// On Windows, preventing sockets to be inherited by child processes.
BOOL
brc
=
SetHandleInformation
((
HANDLE
)
*
r_
,
HANDLE_FLAG_INHERIT
,
0
);
win_assert
(
brc
);
#endif
make_socket_noninheritable
(
*
r_
);
return
0
;
}
else
{
// Cleanup writer if connection failed
...
...
@@ -650,18 +634,32 @@ int zmq::make_fdpair (fd_t *r_, fd_t *w_)
*
w_
=
*
r_
=
-
1
;
return
-
1
;
}
else
{
// If there's no SOCK_CLOEXEC, let's try the second best option. Note that
// race condition can cause socket not to be closed (if fork happens
// between socket creation and this point).
#if !defined ZMQ_HAVE_SOCK_CLOEXEC && defined FD_CLOEXEC
rc
=
fcntl
(
sv
[
0
],
F_SETFD
,
FD_CLOEXEC
);
errno_assert
(
rc
!=
-
1
);
rc
=
fcntl
(
sv
[
1
],
F_SETFD
,
FD_CLOEXEC
);
errno_assert
(
rc
!=
-
1
);
#endif
make_socket_noninheritable
(
sv
[
0
]);
make_socket_noninheritable
(
sv
[
1
]);
*
w_
=
sv
[
0
];
*
r_
=
sv
[
1
];
return
0
;
}
#endif
}
void
zmq
::
make_socket_noninheritable
(
fd_t
sock
)
{
#if defined ZMQ_HAVE_WINDOWS && !defined _WIN32_WCE \
&& !defined ZMQ_HAVE_WINDOWS_UWP
// On Windows, preventing sockets to be inherited by child processes.
const
BOOL
brc
=
SetHandleInformation
(
reinterpret_cast
<
HANDLE
>
(
sock
),
HANDLE_FLAG_INHERIT
,
0
);
win_assert
(
brc
);
#endif
#if (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) \
&& defined FD_CLOEXEC
// If there 's no SOCK_CLOEXEC, let's try the second best option.
// Race condition can cause socket not to be closed (if fork happens
// between accept and this point).
const
int
rc
=
fcntl
(
sock
,
F_SETFD
,
FD_CLOEXEC
);
errno_assert
(
rc
!=
-
1
);
#endif
}
src/ip.hpp
View file @
44da0e7e
...
...
@@ -67,6 +67,10 @@ void shutdown_network ();
// Creates a pair of sockets (using signaler_port on OS using TCP sockets).
// Returns -1 if we could not make the socket pair successfully
int
make_fdpair
(
fd_t
*
r_
,
fd_t
*
w_
);
// Makes a socket non-inheritable to child processes.
// Asserts on any failure.
void
make_socket_noninheritable
(
fd_t
sock
);
}
#endif
src/ipc_listener.cpp
View file @
44da0e7e
...
...
@@ -403,13 +403,7 @@ zmq::fd_t zmq::ipc_listener_t::accept ()
return
retired_fd
;
}
#if (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) \
&& defined FD_CLOEXEC
// Race condition can cause socket not to be closed (if fork happens
// between accept and this point).
int
rc
=
fcntl
(
sock
,
F_SETFD
,
FD_CLOEXEC
);
errno_assert
(
rc
!=
-
1
);
#endif
make_socket_noninheritable
(
sock
);
// IPC accept() filters
#if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
...
...
src/tcp_listener.cpp
View file @
44da0e7e
...
...
@@ -198,12 +198,7 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
if
(
s
==
retired_fd
)
{
return
-
1
;
}
#if defined ZMQ_HAVE_WINDOWS && !defined _WIN32_WCE \
&& !defined ZMQ_HAVE_WINDOWS_UWP
// On Windows, preventing sockets to be inherited by child processes.
BOOL
brc
=
SetHandleInformation
((
HANDLE
)
s
,
HANDLE_FLAG_INHERIT
,
0
);
win_assert
(
brc
);
#endif
make_socket_noninheritable
(
s
);
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// Switch it on in such cases.
...
...
@@ -315,20 +310,7 @@ zmq::fd_t zmq::tcp_listener_t::accept ()
return
retired_fd
;
}
#if defined ZMQ_HAVE_WINDOWS && !defined _WIN32_WCE \
&& !defined ZMQ_HAVE_WINDOWS_UWP
// On Windows, preventing sockets to be inherited by child processes.
BOOL
brc
=
SetHandleInformation
((
HANDLE
)
sock
,
HANDLE_FLAG_INHERIT
,
0
);
win_assert
(
brc
);
#endif
#if (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) \
&& defined FD_CLOEXEC
// Race condition can cause socket not to be closed (if fork happens
// between accept and this point).
int
rc
=
fcntl
(
sock
,
F_SETFD
,
FD_CLOEXEC
);
errno_assert
(
rc
!=
-
1
);
#endif
make_socket_noninheritable
(
sock
);
if
(
!
options
.
tcp_accept_filters
.
empty
())
{
bool
matched
=
false
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment