aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ssh-agent-filter.C27
1 files changed, 12 insertions, 15 deletions
diff --git a/ssh-agent-filter.C b/ssh-agent-filter.C
index 750ae60..e94425b 100644
--- a/ssh-agent-filter.C
+++ b/ssh-agent-filter.C
@@ -126,7 +126,16 @@ void cloexec (int fd) {
throw system_error(errno, system_category(), "fcntl");
}
-void arm(std::ios & stream) {
+int make_cloexec_socket (int const address_family, int const type, int const protocol) {
+ int sock;
+ std::lock_guard lock{fd_fork_mutex};
+ if ((sock = socket(address_family, type | SOCK_CLOEXEC, protocol)) == -1)
+ throw system_error(errno, system_category(), "socket");
+ cloexec(sock);
+ return sock;
+}
+
+void arm (std::ios & stream) {
stream.exceptions(stream.badbit | stream.failbit);
}
@@ -136,13 +145,7 @@ int make_upstream_agent_conn () {
if (!(path = getenv("SSH_AUTH_SOCK")))
throw invalid_argument("no $SSH_AUTH_SOCK");
- int sock;
- {
- std::lock_guard lock{fd_fork_mutex};
- if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1)
- throw system_error(errno, system_category(), "socket");
- cloexec(sock);
- }
+ int const sock = make_cloexec_socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un addr{AF_UNIX, {}};
@@ -158,13 +161,7 @@ int make_upstream_agent_conn () {
}
int make_listen_sock () {
- int sock;
- {
- std::lock_guard lock{fd_fork_mutex};
- if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1)
- throw system_error(errno, system_category(), "socket");
- cloexec(sock);
- }
+ int const sock = make_cloexec_socket(AF_UNIX, SOCK_STREAM, 0);
if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK))
throw system_error(errno, system_category(), "fcntl");