casaroli opened a new pull request, #22523:
URL: https://github.com/apache/nuttx/pull/22523

   ## Summary
   
   This PR fixes three defects in the life of a pseudo-terminal pair. Each 
commit fixes one.
   
   1. Pipes. The first open of a pair makes its two pipes, with the ends of 
both sides. The last close of a side closes only its own ends. When a master is 
closed before its slave is opened, the two ends of the slave stay open, so each 
`posix_openpt()` and `close()` loses two pipes (1056 bytes and 6 heap chunks 
per round on qemu-armv8a). A BSD pair (`CONFIG_PSEUDOTERM_SUSV1=n`) stays after 
its last close, and its next open made new pipes over the ends that were still 
open: the same loss. Now `pty_destroy()`, and the last close of a pair that 
stays, close the ends of both sides.
   
   2. Unlink of a pair that no file has open. `pty_unlink()` frees such a pair 
with `pty_destroy()`. `pty_destroy()` removes the devices with 
`unregister_driver()`, and `nx_unlink()` calls the unlink method of a device 
before it removes the node. So `pty_unlink()` and `pty_destroy()` called each 
other without end. Example with BSD pairs: `posix_openpt()`, `close()`, 
`unlink("/dev/ptyN")` stops the kernel. Now `pty_destroy()` counts an open 
while it removes the devices, so that `pty_unlink()` only marks the pair. It 
also does not remove the device that `pty_unlink()` is called for: 
`nx_unlink()` removes that node itself, and a second removal made `unlink()` 
fail with `ENOENT`.
   
   3. `ptmx_open()` did not handle a failed open of the new master or a failed 
`file_dup2()`, for example when the pipes of the pair cannot be allocated. Now 
it removes the pair and returns the error. This commit needs commit 2: the 
removal frees a pair that is not open.
   
   ## Impact
   
   Only `drivers/serial/pty.c` and `drivers/serial/ptmx.c`. The same code runs 
in flat, protected and kernel builds, on all architectures. No API or ABI 
change. Nothing changes when `CONFIG_PSEUDOTERM` is off. `Documentation/` has 
no text about the life of a pair, so no documentation change.
   
   ## Testing
   
   Test program `ptyfree` (local, not part of this PR). Modes: `probe` = 
`posix_openpt()` and `close()`; `plain` = also `grantpt()`, `unlockpt()`, open 
and close of the slave; `bsdunlink` = BSD pairs: `posix_openpt()`, `close()`, 
`unlink("/dev/ptyN")`, then `/dev/ptyN` and `/dev/ttypN` must be gone; `lowmem` 
= flat build: fill the heap, then free one small block at a time and try 
`posix_openpt()` after each step. PASS = the used bytes and chunks of 
`/proc/meminfo` are the same after the rounds, no `/dev/pts` entry is left, and 
every call returns what it should.
   
   | Configuration (qemu-armv8a, `CONFIG_PSEUDOTERM=y`) | Mode | master 
06dfc8794e9 | this PR |
   |---|---|---|---|
   | `nsh` (flat), SUSv1, HVF | probe, 50 rounds | FAIL: 45080 -> 97880 bytes, 
73 -> 373 chunks | PASS: 44024 -> 44024 bytes, 67 -> 67 chunks |
   | | plain, 50 rounds | PASS | PASS |
   | | lowmem | the kernel stops at ptmx.c:210 | PASS: 7 tries fail with 
ENOMEM, the 8th opens; heap the same after |
   | `nsh` (flat), BSD pairs | probe, 50 rounds | FAIL: 45400 -> 98200 bytes | 
PASS: 44344 -> 44344 bytes |
   | | plain, 50 rounds | PASS | PASS |
   | | bsdunlink, 20 rounds | the kernel stops at the first `unlink()` (data 
abort in `mm_malloc()`) | PASS: 43832 -> 43832 bytes |
   | `nsh` (flat), SUSv1, `CONFIG_FS_PERMISSION=y` | probe, 50 rounds | FAIL: 
46296 -> 105496 bytes | PASS |
   | | plain / lowmem | PASS / - | PASS / PASS |
   | `knsh` (kernel), SUSv1, TCG | probe, 50 rounds | FAIL: 46088 -> 98888 
bytes | PASS: 45032 -> 45032 bytes |
   | | plain, 50 rounds | PASS | PASS |
   
   ostest with this PR: qemu-armv8a:knsh (TCG) and qemu-armv8a:nsh (HVF) both 
end with `Exiting with status 0`.
   
   Builds with `EXTRAFLAGS="-Wno-cpp -Werror"` and `CONFIG_PSEUDOTERM=y`, 0 
warnings from the changed files:
   - arm64 (Arm GNU 13.2.Rel1): qemu-armv8a:nsh (SUSv1, BSD, SUSv1 + 
FS_PERMISSION), qemu-armv8a:knsh, fvp-armv8r:pnsh (protected; SUSv1 and BSD).
   - risc-v 64 (CI image): rv-virt:nsh64 (SUSv1, BSD, SUSv1 + FS_PERMISSION), 
rv-virt:pnsh64 (protected), rv-virt:knsh64 (kernel).
   
   BSD pairs are tested in a flat build only: with BSD pairs, the libc 
`posix_openpt()` calls `pty_register()`, which a protected or kernel build does 
not export to user space.
   


-- 
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]

Reply via email to