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
6 years ago
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_)
...
@@ -84,19 +84,7 @@ zmq::fd_t zmq::open_socket (int domain_, int type_, int protocol_)
return
retired_fd
;
return
retired_fd
;
}
}
// If there's no SOCK_CLOEXEC, let's try the second best option. Note that
make_socket_noninheritable
(
s
);
// 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
// Socket is not yet connected so EINVAL is not a valid networking error
// Socket is not yet connected so EINVAL is not a valid networking error
rc
=
zmq
::
set_nosigpipe
(
s
);
rc
=
zmq
::
set_nosigpipe
(
s
);
...
@@ -521,11 +509,7 @@ int zmq::make_fdpair (fd_t *r_, fd_t *w_)
...
@@ -521,11 +509,7 @@ int zmq::make_fdpair (fd_t *r_, fd_t *w_)
}
}
if
(
*
r_
!=
INVALID_SOCKET
)
{
if
(
*
r_
!=
INVALID_SOCKET
)
{
#if !defined _WIN32_WCE && !defined ZMQ_HAVE_WINDOWS_UWP
make_socket_noninheritable
(
*
r_
);
// On Windows, preventing sockets to be inherited by child processes.
BOOL
brc
=
SetHandleInformation
((
HANDLE
)
*
r_
,
HANDLE_FLAG_INHERIT
,
0
);
win_assert
(
brc
);
#endif
return
0
;
return
0
;
}
else
{
}
else
{
// Cleanup writer if connection failed
// Cleanup writer if connection failed
...
@@ -650,18 +634,32 @@ int zmq::make_fdpair (fd_t *r_, fd_t *w_)
...
@@ -650,18 +634,32 @@ int zmq::make_fdpair (fd_t *r_, fd_t *w_)
*
w_
=
*
r_
=
-
1
;
*
w_
=
*
r_
=
-
1
;
return
-
1
;
return
-
1
;
}
else
{
}
else
{
// If there's no SOCK_CLOEXEC, let's try the second best option. Note that
make_socket_noninheritable
(
sv
[
0
]);
// race condition can cause socket not to be closed (if fork happens
make_socket_noninheritable
(
sv
[
1
]);
// 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
*
w_
=
sv
[
0
];
*
w_
=
sv
[
0
];
*
r_
=
sv
[
1
];
*
r_
=
sv
[
1
];
return
0
;
return
0
;
}
}
#endif
#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
}
This diff is collapsed.
Click to expand it.
src/ip.hpp
View file @
44da0e7e
...
@@ -67,6 +67,10 @@ void shutdown_network ();
...
@@ -67,6 +67,10 @@ void shutdown_network ();
// Creates a pair of sockets (using signaler_port on OS using TCP sockets).
// Creates a pair of sockets (using signaler_port on OS using TCP sockets).
// Returns -1 if we could not make the socket pair successfully
// Returns -1 if we could not make the socket pair successfully
int
make_fdpair
(
fd_t
*
r_
,
fd_t
*
w_
);
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
#endif
This diff is collapsed.
Click to expand it.
src/ipc_listener.cpp
View file @
44da0e7e
...
@@ -403,13 +403,7 @@ zmq::fd_t zmq::ipc_listener_t::accept ()
...
@@ -403,13 +403,7 @@ zmq::fd_t zmq::ipc_listener_t::accept ()
return
retired_fd
;
return
retired_fd
;
}
}
#if (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) \
make_socket_noninheritable
(
sock
);
&& 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
// IPC accept() filters
// IPC accept() filters
#if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
#if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
...
...
This diff is collapsed.
Click to expand it.
src/tcp_listener.cpp
View file @
44da0e7e
...
@@ -198,12 +198,7 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
...
@@ -198,12 +198,7 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
if
(
s
==
retired_fd
)
{
if
(
s
==
retired_fd
)
{
return
-
1
;
return
-
1
;
}
}
#if defined ZMQ_HAVE_WINDOWS && !defined _WIN32_WCE \
make_socket_noninheritable
(
s
);
&& !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
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// Switch it on in such cases.
// Switch it on in such cases.
...
@@ -315,20 +310,7 @@ zmq::fd_t zmq::tcp_listener_t::accept ()
...
@@ -315,20 +310,7 @@ zmq::fd_t zmq::tcp_listener_t::accept ()
return
retired_fd
;
return
retired_fd
;
}
}
#if defined ZMQ_HAVE_WINDOWS && !defined _WIN32_WCE \
make_socket_noninheritable
(
sock
);
&& !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
if
(
!
options
.
tcp_accept_filters
.
empty
())
{
if
(
!
options
.
tcp_accept_filters
.
empty
())
{
bool
matched
=
false
;
bool
matched
=
false
;
...
...
This diff is collapsed.
Click to expand it.
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