diff options
| author | Timo Weingärtner <timo@tiwe.de> | 2021-04-25 17:52:03 +0200 |
|---|---|---|
| committer | Timo Weingärtner <timo@tiwe.de> | 2026-03-07 22:25:16 +0100 |
| commit | 34781d9036c6e564209780154f781b04cd040805 (patch) | |
| tree | d0c76657d409bae8f682b47c07fc6f0890d27265 /ssh-agent-filter.C | |
| parent | 1ed4729f59343b8041387b8802610c25de599085 (diff) | |
| download | ssh-agent-filter-34781d9036c6e564209780154f781b04cd040805.tar.gz | |
factor out socket creation with CLOEXEC
Diffstat (limited to 'ssh-agent-filter.C')
| -rw-r--r-- | ssh-agent-filter.C | 27 |
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"); |
