Re: thread-safety: getpwuid_r()

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Peter Eisentraut <peter(at)eisentraut(dot)org>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: thread-safety: getpwuid_r()
Date: 2024-08-24 13:55:06
Message-ID: d45fabed-8c24-403b-922f-15556868ebf2@iki.fi
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 24/08/2024 11:42, Peter Eisentraut wrote:
> Here is a patch to replace a getpwuid() call in the backend, for
> thread-safety.
>
> This is AFAICT the only call in the getpw*() family that needs to be
> dealt with.
>
> (There is also a getgrnam() call, but that is called very early in the
> postmaster, before multiprocessing, so we can leave that as is.)
>
> The solution here is actually quite attractive: We can replace the
> getpwuid() call by the existing wrapper function pg_get_user_name(),
> which internally uses getpwuid_r(). This also makes the code a bit
> simpler. The same function is already used in libpq for a purpose that
> mirrors the backend use, so it's also nicer to use the same function for
> consistency.

Makes sense.

The temporary buffers are a bit funky. pg_get_user_name() internally
uses a BUFSIZE-sized area to hold the result of getpwuid_r(). If the
pg_get_user_name() caller passes a buffer smaller than BUFSIZE, the user
id might get truncated. I don't think that's a concern on any real-world
system, and the callers do pass a large-enough buffer so truncation
can't happen. At a minimum, it would be good to add a comment to
pg_get_user_name() along the lines of "if 'buflen' is smaller than
BUFSIZE, the result might be truncated".

Come to think of it, the pg_get_user_name() function is just a thin
wrapper around getpwuid_r(). It doesn't provide a lot of value. Perhaps
we should remove pg_get_user_name() and pg_get_user_home_dir()
altogether and call getpwuid_r() directly.

But no objection to committing this as it is, either.

--
Heikki Linnakangas
Neon (https://neon.tech)

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Junwang Zhao 2024-08-24 14:18:58 Re: Official devcontainer config
Previous Message Peter Eisentraut 2024-08-24 13:47:23 Re: Official devcontainer config