Unix socket dir, an idea

From: Danylo Hlynskyi <abcz2(dot)uprola(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Unix socket dir, an idea
Date: 2019-03-29 20:37:44
Message-ID: CANZg+yd2OdqxM=HwnrkT-QecNMqLKNp+bGO_2SirJGznGK6h_w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Before writing a patch, I'd like to hear discussion first.

I've searched archives first and read following previous discussions on
this topic:
- https://www.postgresql.org/message-id/4FCF6040.5030408%40redhat.com
- https://www.postgresql.org/message-id/14899.974513046%40sss.pgh.pa.us

The problem (as I see it) is that everybody would like to move `/tmp`
socket dir to `/var/run`, or even `/var/run/postgresql` (or even
`/run/postgresql`), but compatibility with old clients (which connect to
/tmp by default) is a concern.

One reason to make this move is because any user can create PG socket in
`/tmp`, and thus local clients will use that PG, instead of system one
(which won't even start because it can't create socket - it is already
used).

I propose following 2 ideas:

1. Add a Makefile parameter CONFIG_PGSOCKET_DIR to explicitly switch to new
unix socket directory, and let distributions decide if they want this, and
if they want, they should handle socket dir change on their own. For
example, switch to `/var/run/postgresql` require `/var/run/postgresql`
directory to be created - an extra step compared to current situation.

This will allow remove some patches for many (many?) distributions.

By default (CONFIG_PGSOCKET_DIR undefined) unix socket dir should be set to
`/tmp` - backward compatibility.

2. The new socket directory shouldn't be hardcoded to single directory,
instead it should be detected dynamically.

For server:
- if unix_socket_directory specified, use it
- if not, check if /run/user/$(id -u) exists. If yes, use it as socket dir
- if doesn't exist, check CONFIG_PGSOCKET_DIR exists. If yes, use it as
socket dir
- else fail

For client:
- if host explicitly set, use it
- if not, check if /run/user/$(id -u) exists and socket file exists there.
If yes, use it as socket
- if doesn't exist, check CONFIG_PGSOCKET_DIR exists. If yes, use it as
socket dir
- else fail

What will be solved:
- no more local /tmp hijack
- `pg_ctl start` and psql, when run as single user, will use same socket
directory /run/user/$(id -u) - no need to create safe directory first
- psql will still be able to connect to "service" PG - if socket is not
found in user runtime dir, then lookup in system (runtime) dir

Drawbacks:
- running pg_ctl as root will no longer make server accessible by default
to other users, because /run/user/0 is readable only by root
- if `postgres` user, under which postgresql service runs, is "normal"
user, and has /run/user/XXX directory, pg will require start-time -k
/var/run/postgresql switch, to be accessible to other users' clients
- there will no longer be a "single" directory to lookup sockets, so an
instructions on nuances of unix socket dir resolution for newcomers is
required
- non-systemd distributions won't benefit from this logic
- /run/user/$(id -u) is opinionated. $XDG_RUNTIME_DIR would be better

Thoughts?

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2019-03-29 21:06:35 Re: PostgreSQL pollutes the file system
Previous Message Christoph Berg 2019-03-29 20:30:09 Re: PostgreSQL pollutes the file system