Re: getcwd() returns with stale errno value upon error
From:
Richard Weinberger
Subject:
Re: getcwd() returns with stale errno value upon error
Date:
Wed, 25 Jun 2025 21:00:19 +0200
Hello Oğuz,
On Mittwoch, 25. Juni 2025 20:34 Oğuz wrote:
> On Wednesday, June 25, 2025, Richard Weinberger <richard@sigma-star.at>
> wrote:
> >
> > Having different results for d_ino and st_ino is not that uncommon.
> >
>
> POSIX defines both fields to contain the unique serial number of a file in
> a filesystem. I don't think Bash is at fault here for not assuming
> otherwise.
I'm not asking you to implement a workaround, I'm asking you to make sure Bash
fails more gracefully.
Currently the error message very misleading since errno is undefined.
Here some examples from my test bed:
shell-init: error retrieving current directory: getcwd: cannot access parent
directories: No such file or directory
shell-init: error retrieving current directory: getcwd: cannot access parent
directories: Success
shell-init: error retrieving current directory: getcwd: cannot access parent
directories: Inappropriate ioctl for device
If Bash's getcwd() does not find a matching directory entry it could state this
more clearly instead
of showing a wrong errno value.
Speaking of POSIX, I don't think POSIX requires that readdir() has to offer a
matching entry *after*
you have called stat(). This is a TOCTOU problem and can easily reproduced on
any file system.
The file system can change between stat() and readdir().
Here on my Linux system with btrfs:
$ mkdir -p a/b/c
$ cd a/b/c
$ pwd /home/rw/bash/a/b/c
/home/rw/bash/a/b/c
$ rmdir /home/rw/bash/a/b/c
$ unset PWD; ~/bash/bash -c 'true'
shell-init: error retrieving current directory: getcwd: cannot access parent
directories: No such file or directory
Thanks,
//richard
--
sigma star gmbh | Eduard-Bodem-Gasse 6, 6020 Innsbruck, AUT UID/VAT Nr:
ATU 66964118 | FN: 374287y