Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Fixed

- Prevent IPC socket fd leak into child processes spawned by `$(...)` config commands, which caused `Binding control port failed` errors after restart ([#388](https://github.com/houmain/keymapper/pull/388)).

## [Version 5.6.0] - 2026-06-14

### Added
Expand Down
22 changes: 19 additions & 3 deletions src/common/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ void make_non_blocking(Socket socket_fd) {

#else // !defined(_WIN32)

#include <features.h>
// accept4() requires _GNU_SOURCE on glibc >= 2.10.
// On musl it is available without any feature-test macro.
#if defined(__GLIBC__) && __GLIBC_PREREQ(2, 10)
#define HAVE_ACCEPT4 1
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#endif
#include <csignal>
#include <unistd.h>
#include <fcntl.h>
Expand Down Expand Up @@ -120,7 +129,7 @@ Host::~Host() {
}

bool Host::listen() {
m_listen_fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
m_listen_fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (m_listen_fd == invalid_socket)
return false;

Expand Down Expand Up @@ -158,9 +167,16 @@ Connection Host::accept(std::optional<Duration> timeout) {
if (!block_until_readable(m_listen_fd, timeout))
return { };

# if defined(HAVE_ACCEPT4)
auto socket_fd = ::accept4(m_listen_fd, nullptr, nullptr, SOCK_CLOEXEC);
# else
auto socket_fd = ::accept(m_listen_fd, nullptr, nullptr);
# endif
if (socket_fd == invalid_socket)
return { };
# if !defined(HAVE_ACCEPT4)
::fcntl(socket_fd, F_SETFD, ::fcntl(socket_fd, F_GETFD) | FD_CLOEXEC);
# endif
make_blocking(socket_fd);
auto connection = Connection(socket_fd);

Expand Down Expand Up @@ -190,7 +206,7 @@ Connection Host::connect(std::optional<Duration> timeout) {
const auto retry_until_timepoint = (timeout ?
std::make_optional(Clock::now() + *timeout) : std::nullopt);

auto socket_fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
auto socket_fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
for (;;) {
if (socket_fd == invalid_socket)
return { };
Expand All @@ -205,7 +221,7 @@ Connection Host::connect(std::optional<Duration> timeout) {
!connection.read(&versions_match)) {
// this fails regularly when reconnecting to a closing host
connection.disconnect();
socket_fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
socket_fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
continue;
}
if (!versions_match)
Expand Down