Can we avoid chdir'ing in resolve_symlinks() ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Can we avoid chdir'ing in resolve_symlinks() ?
Date: 2022-09-01 23:39:33
Message-ID: 797232.1662075573@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

find_my_exec() wants to obtain an absolute, symlink-free path
to the program's own executable, for what seem to me good
reasons. However, chasing down symlinks is left to its
subroutine resolve_symlinks(), which does this:

* To resolve a symlink properly, we have to chdir into its directory and
* then chdir to where the symlink points; otherwise we may fail to
* resolve relative links correctly (consider cases involving mount
* points, for example). After following the final symlink, we use
* getcwd() to figure out where the heck we're at.

and then afterwards it has to chdir back to the original cwd.
That last step is a bit of a sore spot, because sometimes
(especially in sudo situations) we may not have the privileges
necessary to do that; I think this is the cause of the complaint
at [1]. Anyway the whole thing seems a bit excessively Rube
Goldbergian. I'm wondering why we couldn't just read the
symlink(s), concatenate them together, and use canonicalize_path()
to clean up any mess.

This code was mine originally (336969e49), but I sure don't
remember why I wrote it like that. I know we didn't have a
robust version of canonicalize_path() then, and that may have
been the main issue, but that offhand comment about mount
points bothers me. But I can't reconstruct precisely what
I was worried about there. The only contemporaneous discussion
thread I can find is [2], which doesn't go into coding details.

Thoughts?

regards, tom lane

[1] https://www.postgresql.org/message-id/flat/CAH8yC8kOj0pmHF1RbK2Gb2t4YCcNG-5h0TwZ7yxk3Hzw6C0Otg%40mail.gmail.com
[2] https://www.postgresql.org/message-id/flat/4973.1099605411%40sss.pgh.pa.us

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Rowley 2022-09-02 00:27:45 Re: Fix possible bogus array out of bonds (src/backend/access/brin/brin_minmax_multi.c)
Previous Message Nathan Bossart 2022-09-01 23:31:20 Re: postgres_fdw hint messages