8000 Fix hardcoded /tmp path for Unix socket in unix_connect by luckyabsoluter · Pull Request #288 · python-xlib/python-xlib · GitHub
[go: up one dir, main page]

Skip to content

Fix hardcoded /tmp path for Unix socket in unix_connect - #288

Open
luckyabsoluter wants to merge 1 commit into
python-xlib:masterfrom
luckyabsoluter:fix-hardcoded-tmp-path
Open

luckyabsoluter wants to merge 1 commit into
python-xlib:masterfrom
luckyabsoluter:fix-hardcoded-tmp-path

Conversation

@luckyabsoluter
Copy link
Copy Markdown

Problem:

  • The unix_connect function previously hardcoded /tmp as the directory for the Unix socket path.
  • This causes compatibility issues on platforms or specific environments that use a different temporary directory, or even lack access to /tmp entirely.

Actions:

  • Imported the tempfile module in unix_connect.
  • Updated the X11 Unix socket path generation to dynamically use tempfile.gettempdir().

Result:

  • The Unix socket path now correctly adapts to the system's runtime configuration (e.g., OS defaults or TMPDIR environment variables), improving cross-platform compatibility.

Problem:
- The unix_connect function previously hardcoded /tmp as the directory for the Unix socket path.
- This causes compatibility issues on platforms or specific environments that use a different temporary directory, or even lack access to /tmp entirely.

Actions:
- Imported the tempfile module in unix_connect.
- Updated the X11 Unix socket path generation to dynamically use tempfile.gettempdir().

Result:
- The Unix socket path now correctly adapts to the system's runtime configuration (e.g., OS defaults or TMPDIR environment variables), improving cross-platform compatibility.
@Avasam
Avasam commented Nov 1, 2026 •
Copy link
Copy Markdown
Contributor

Pretty sure the convention IS to use /tmp/.X11-unix/X as the socket path. This PR would just break things.

tempfile.gettempdir() reads TMPDIR, TEMP and TMP. Debian's libpam-tmpdir sets TMPDIR=/tmp/user/<uid> for every login, so this PR would look for /tmp/user/1000/.X11-unix/X0, which doesn't exist. The abstract socket fallback would also break, because this PR builds that name from the same path. The server listens on the abstract name @/tmp/.X11-unix/X0, which is also fixed, so both lookups would miss. Every other X client on that machine would still connect fine.

The one real exception is macOS XQuartz, whose socket lives under /private/tmp/com.apple.launchd.*. Though that path comes through DISPLAY, not TMPDIR.

@luckyabsoluter
luckyabsoluter commented Nov 1, 2026 •
Copy link
Copy Markdown
Author

That is a very valid point. While the goal here is to support Termux on Android (where X servers must place their sockets under $TMPDIR mainly because standard /tmp is inaccessible), relying unconditionally on tempfile.gettempdir() or TMPDIR across all environments is an overly broad fix.

To solve this without causing regressions on standard Linux, a few alternative approaches were considered:

  • Socket connection fallback (trying /tmp first, then $TMPDIR):

    • Automatically probing fallback directories upon failure carries a security risk of connecting to an unintended socket and exposing X11 authentication data.
  • Checking sys.platform == 'android':

    • Since 'android' was only introduced in Python 3.13, supporting Python < 3.13 would require a redundant fallback to TERMUX_VERSION anyway. Moreover, given how narrow the X11 ecosystem on Android is, rather than casting a wide net with a dual check, it is much cleaner and safer to just target Termux directly with a single check.

This narrows the practical solutions down to two main options:

1. Keep the project strictly platform-agnostic: Introduce a dedicated environment variable

  • For example, allowing users to explicitly override the socket directory via something like PYTHON_XLIB_SOCKET_DIR.
  • This avoids environment-specific checks in the codebase, though it introduces a new configuration variable that users must set manually.

2. Allow a Termux-specific branch: Detect TERMUX_VERSION (Recommended)

  • Narrow the target strictly to Termux by only using TMPDIR when TERMUX_VERSION is present, keeping /tmp as the default everywhere else.
  • This is the most pragmatic and minimal fix—it requires no new imports or user configuration, works out-of-the-box on Termux across all Python versions, and has zero impact on standard Linux environments.
  • However, since TERMUX_VERSION is a fairly common name combination, users who define it for other purposes—unaware that it triggers special behavior in python-xlib—could inadvertently expose themselves to the aforementioned TMPDIR security risks or connection issues. (To mitigate this risk, we could combine the check like if os.getenv('TERMUX_VERSION') and 'com.termux' in os.getenv('PREFIX', ''):.)
tmpdir = '/tmp'
if os.getenv('TERMUX_VERSION'):  # if os.getenv('TERMUX_VERSION') and 'com.termux' in os.getenv('PREFIX', ''):
    tmpdir = os.getenv('TMPDIR') or tmpdir
address = os.path.join(tmpdir, '.X11-unix', 'X%d' % dno)

Addressing this properly will likely require submitting a new PR. Before doing that, I would like to open this up for discussion and hear everyone's thoughts on which direction seems best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0