casaroli opened a new pull request, #21907:
URL: https://github.com/apache/nuttx/pull/21907
depends-on: apache/nuttx-apps/pull/4061
## Summary
NuttX keeps the current directory in the environment variable `PWD`.
`chdir()` is libc code that calls `setenv("PWD")`, `getcwd()` returns
`getenv("PWD")`, and the kernel joins `getenv("PWD")` to each relative path. So
a program that sets or unsets `PWD` moves its current directory (`env PWD=/etc
ls` lists `/etc`), a child started with an empty environment starts in `/`,
`chdir()` changes nothing with `CONFIG_DISABLE_ENVIRON`, and in protected and
kernel builds the kernel locks the environment, which is in the memory of the
process, for each relative path. POSIX makes the current directory an attribute
of the process; `PWD` is only information for the shell.
This PR makes the current directory kernel state of the task group
(`tg_cwd`):
- `chdir()` and `fchdir()` move to `fs/vfs/fs_cwd.c` and are system calls.
The kernel keeps the canonical absolute path in the kernel heap. A new task
group shares the path of its creator with a reference count until one of them
calls `chdir()`. `nx_getcwd()` is a new system call; `getcwd()` stays in libc,
so `getcwd(NULL, size)` allocates in the heap of the caller. Relative lookups
(`inode_search_setup()`) and the `*at()` functions with `AT_FDCWD` use
`tg_cwd`, not `PWD`. `chdir()` does not write `PWD` and `OLDPWD` any more: the
shell sets them.
- The next commit follows changes of the directory: after `rename()` of the
current directory or of a parent, `getcwd()` gives the new name; after
`rmdir()` of it, `getcwd()` and relative names give `ENOENT`; `umount()` of a
file system that holds a current directory gives `EBUSY`.
- The last commit adds `Documentation/os/filesystem/current_directory.rst`
and updates `chroot.rst`.
NSH sets `PWD` and `OLDPWD` after `cd` in apache/nuttx-apps#4061 (merge it
first; it works with today's master too). The ostest test is
apache/nuttx-apps#TBD.
## Impact
What a program sees, in every build mode (flat too):
- Setting, unsetting or clearing `PWD` does not move the current directory.
A child started with an empty environment (`execve()`, `posix_spawn()`) starts
in the directory of its parent, not in `/`.
- `chdir()` does not change `PWD` or `OLDPWD`. A program that reads `PWD`
after its own `chdir()` sees the old value; `getcwd()` gives the directory. The
value that `getcwd()` gives is the same as before (`chdir()` stored
`realpath()` of the path).
- After `rmdir()` of the current directory, `getcwd()` and relative names
fail with `ENOENT` (before: the old name, and a new directory of that name
became the current directory). After `rename()`, `getcwd()` gives the new name.
`umount()` of a file system that holds a current directory fails with `EBUSY`.
- `chdir()`, `fchdir()`, `getcwd()`, `get_current_dir_name()`, `nftw()` with
`FTW_CHDIR` and the `chdir("/")` of `daemon()` work with
`CONFIG_DISABLE_ENVIRON`. `fchdir()` of a descriptor that is not a directory
gives `ENOTDIR`.
- Not changed: the threads of a task group share one current directory, as
before (the environment was per task group too): a `chdir()` in one thread
changes it for all, as POSIX says. A task from `task_create()`,
`posix_spawn()`, `vfork()` or `fork()` starts in the directory of its creator
and then has its own, as before. The search permission check of `chdir()` is
the same code, moved from libc. The kernel threads share one current directory
of their own.
- The relative lookup no longer locks the environment of the process from
the kernel; it takes a kernel mutex instead.
- Protected and kernel builds: three new system calls (`chdir`, `fchdir`,
`nx_getcwd`), next to the other file system calls; the numbers after them move
by three.
- Size (text, Arm GNU 13.2 / xPack RISC-V 14.3): mps2-an500:nsh (Cortex-M4,
flat) +2600 B, data +24 B; rv-virt:knsh64 (kernel) +2856 B; rv-virt:pnsh64
(protected) +2848 B; ea3131:pgnsh +520 B. Two CI configurations did not fit any
more, so the first two commits make room: lm3s6965-ek:qemu-protected had 528 B
of kernel flash free and overflowed by 1728 B, so it excludes six small procfs
entries that the configuration does not use, `cpuinfo`, `iobinfo`, `meminfo`,
`tcbinfo`, `uptime` and `version` (`/proc/<pid>` and `ps` stay; 1212 B free
after); nucleo-l432kc:wgen (clang) had 656 B free and overflowed by 1576 B, so
it disables the NSH commands `cp`, `mv`, `rm` and `truncate` (5696 B free
after).
In-tree users: in nuttx, `getcwd()` is used by `lib_realpath()` and
`nftw()`, `chdir()` by `nftw()` and `daemon()`, and `PWD` is read only by the
code this PR changes (`setenv("PWD")` at boot in `nx_bringup()` stays). In
nuttx-apps, NSH reads `PWD` for `pwd`, `cd -` and relative arguments
(apache/nuttx-apps#4061 sets it after `cd` and `chroot`); `ftpc_lpwd()` reads
`PWD` once at connect, which NSH sets; thttpd, bas, readline, vi and the cmocka
fs and syscall suites call `chdir()` and `getcwd()` and need no change.
## Testing
QEMU with HVF, nuttx master 46066440966 against this PR, `CONFIG_PIPES`,
`CONFIG_FS_LINKS`, `CONFIG_PSEUDOFS_SOFTLINKS`, `CONFIG_FS_CHROOT` and
`CONFIG_FS_TMPFS` enabled:
| Test | master | this PR |
|---|---|---|
| ostest `cwd_test` (apache/nuttx-apps#TBD), qemu-armv8a:knsh (kernel) | 4
errors (PWD set, PWD unset, rename, rmdir), ostest asserts | PASS, ostest
status 0 |
| ostest `cwd_test`, qemu-armv8a:nsh (flat) | the same 4 errors, ostest
asserts | PASS, ostest status 0 |
| test program `cwdkernel.c` (29 cases), qemu-armv8a:knsh | 11 PASS, 18 FAIL
| 26 PASS, 3 FAIL |
| test program `cwdchroot.c` (5 cases), qemu-armv8a:knsh | 1 PASS, 4 FAIL |
4 PASS, 1 FAIL |
The cases that still fail: `at-dirfd` and `at-dirfd-moved` (the `*at()`
functions resolve a directory descriptor by its name, a separate change),
`chdir-eacces` (the test needs `setuid()`, which this configuration does not
have), and `fstatdir` (`fstat()` of a directory descriptor inside a `chroot()`
jail, a separate bug). They fail on master too. `cwdkernel` also shows that
1000 `chdir()` calls and 200 children leave the kernel heap as it was.
NSH on qemu-armv8a:nsh (flat) with this PR and apache/nuttx-apps#4061: `cd
/proc`, `pwd` (`/proc`), `echo $PWD $OLDPWD` (`/proc /`), `cat version`
(relative name), `cd -`, `cd /dev`, `ls null`, `cd ..`: all right. With this PR
and apps master, NSH keeps the old `PWD`: `pwd` gives `/` and `cat version`
fails with `ENOENT`, so the apps PR must merge first. The apps PR alone on
master gives the right results too.
Builds with `-Werror`, master and this PR: rv-virt:pnsh64 (protected, rv64),
rv-virt:knsh64 (kernel, rv64), mps2-an500:nsh, qemu-armv8a:nsh with
`CONFIG_DISABLE_ENVIRON`, ea3131:pgnsh (flat with paging: the locked region
still fits), lm3s6965-ek:qemu-protected, b-g431b-esc1:nsh,
b-g431b-esc1:foc_f32, nucleo-g431kb:nsh, nucleo-l432kc:nsh,
weact-stm32h750:sdcard: all link. qemu-armv8a:nsh also builds with CMake. In
the CI docker image (GNU 13.2, clang for the stm32l4 and stm32h7 boards, as the
CI lists), this PR links: lm3s6965-ek:qemu-protected (kflash 99.08%),
b-g431b-esc1:nsh (98.66%), nucleo-g431kb:nsh (98.63%), nucleo-l432kc:nsh
(97.75%), nucleo-l432kc:wgen (97.83%), weact-stm32h750:sdcard (95.95%),
ea3131:pgnsh.
`tools/checkpatch.sh -c -u -m -g` passes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]