diff --git a/features/src/docker-out/docker-init.sh.tmpl b/features/src/docker-out/docker-init.sh.tmpl index b42e33b..2eed0d1 100755 --- a/features/src/docker-out/docker-init.sh.tmpl +++ b/features/src/docker-out/docker-init.sh.tmpl @@ -58,6 +58,12 @@ else else # Use socat echo "Using socat to bridge socket from GID $SOCKET_GID to docker GID $DOCKER_GID." + # A bridge onto its own source would delete it, then the listener would connect to itself. + # A string compare suffices: two spellings of one file already failed at the ln -s above. + if [ "${SOURCE_SOCKET}" = "${TARGET_SOCKET}" ]; then + echo "Error: SOURCE_SOCKET and TARGET_SOCKET are both ${SOURCE_SOCKET}, but its GID ${SOCKET_GID} belongs to group ${EXISTING_GROUP}, not docker. Mount the host socket at a different path than TARGET_SOCKET." >&2 + exit 1 + fi sudoIf rm -rf ${TARGET_SOCKET} # Check if socat is installed if command -v socat > /dev/null 2>&1; then @@ -75,8 +81,22 @@ else exit 1 fi fi - # Create the socat bridge - sudoIf socat UNIX-CONNECT:${SOURCE_SOCKET} UNIX-LISTEN:${TARGET_SOCKET},fork,user=${USERNAME},group=docker,mode=660 + # Create the socat bridge. The listener must be the first address: socat opens the first + # address once, so with UNIX-CONNECT first every forked client shared one daemon connection. + # Backgrounded so the script reaches `exec "$@"` below instead of blocking on socat. + sudoIf socat UNIX-LISTEN:${TARGET_SOCKET},fork,user=${USERNAME},group=docker,mode=660 UNIX-CONNECT:${SOURCE_SOCKET} & + # Probe by connecting: the path exists from bind(), before listen(), so [ -S ] passes too early. + # sudoIf because this process predates the usermod above and lacks the docker group. + BRIDGE_READY=false + for _ in $(seq 1 50); do + if sudoIf socat -u /dev/null UNIX-CONNECT:"${TARGET_SOCKET}" 2>/dev/null; then BRIDGE_READY=true; break; fi + sleep 0.1 + done + # set -e cannot see a backgrounded socat fail, so check the result explicitly. + if [ "${BRIDGE_READY}" != true ]; then + echo "Error: socat bridge at ${TARGET_SOCKET} accepted no connection within 5s." >&2 + exit 1 + fi fi fi