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

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   ## Summary
   Fixes in the syslog write path and in the RPMsg syslog server:
   
   - **drivers/syslog: fix syslog write busy loop when a channel is full**: 
`syslog_write_foreach()` treated a zero-length channel write the same as a 
successful one. When a channel buffer is full its `sc_write()` returns 0, but 
the CR/LF path still emitted the trailing CR/LF and advanced `nwritten` past 
data that was never stored, and the tail path counted the blocked channel as 
having written 0 bytes and let it update `nwritten_max`. With every channel 
full the function returned 0, which the caller takes as "no progress" and 
retries, spinning the CPU. A return of 0 is now handled like a write error.
   - **drivers/syslog: keep the partial rpmsg line in a fixed buffer**: 
`syslog_rpmsg_ept_cb()` kept the unterminated tail of each transfer in a buffer 
that it grew with `kmm_realloc()` from the endpoint callback. The tail now goes 
to a `SYSLOG_RPMSG_MAXLEN` buffer inside `struct syslog_rpmsg_server_s`, so the 
callback never allocates. The part of a partial line that does not fit is 
dropped; `syslog_rpmsg_write_internal()` could merge at most 
`SYSLOG_RPMSG_MAXLEN` bytes with the rest of the line anyway.
   - **drivers/syslog: drop the NUL from the color reset write**: 
`sizeof("\e[0m")` counts the string terminator, so with 
`CONFIG_SYSLOG_COLOR_OUTPUT` every message ended with a 0x00 byte after its 
newline. Terminals ignore it, but tools that read the log byte by byte see the 
NUL at the start of the next line.
   - **drivers/syslog: check the CRLF write result**: the result of the CR/LF 
write was never used, so a channel filling up between the payload write and the 
CR/LF write had its failure discarded while `nwritten` still claimed the '\n' 
was consumed. The '\n' is now accounted only when its CR/LF expansion was 
stored; otherwise it is left to the tail write.
   - **drivers/syslog: keep the CR of an empty line**: with the first commit, 
the write of the empty text in front of the '\n' of an empty line (or of a '\n' 
at the start of the buffer) returns 0 and counts as a full channel, so that 
'\n' went out without its CR. The empty text is no longer written.
   
   ## Impact
   - A full syslog channel no longer makes `syslog_write_foreach()` spin, and 
no longer gets a CR/LF for text it did not store.
   - The RPMsg syslog server does not allocate in its endpoint callback. 
`struct syslog_rpmsg_server_s` grows by `SYSLOG_RPMSG_MAXLEN` (256) bytes per 
remote CPU, and a partial line longer than that is cut.
   - `CONFIG_SYSLOG_COLOR_OUTPUT`: no NUL byte in the output any more.
   - No API or Kconfig change.
   
   ## Testing
   Host: Ubuntu 22.04 x86_64, GCC 13.4.0, CMake 3.23 + Ninja. Base: master 
dc9481383ed. Targets: `sim:nsh` (it has the default `CONFIG_SYSLOG_CRLF=y`), 
`sim:nsh` with `CONFIG_SYSLOG_COLOR_OUTPUT=y`, and `sim:rpserver` + 
`sim:rpproxy`.
   
   `./tools/checkpatch.sh -c -u -m -g HEAD~5..HEAD` passes.
   
   A small test program (not part of this PR) calls `syslog(LOG_INFO, 
"slogA\n\nslogB\n")`, `syslog(LOG_INFO, "\nslogC\n")` and `syslog(LOG_INFO, 
"slogD\n")`. The bytes on the console from `slogA` to `slogD`:
   
   | Build | Output |
   |---|---|
   | master | `slogA\r\n\r\nslogB\r\n\r\nslogC\r\nslogD` |
   | the first four commits only | `slogA\r\n\nslogB\r\n\nslogC\r\nslogD` |
   | this PR | `slogA\r\n\r\nslogB\r\n\r\nslogC\r\nslogD` |
   
   With `CONFIG_SYSLOG_COLOR_OUTPUT=y`, master writes 3 NUL bytes in that 
range, this PR none:
   ```
   master:  
slogA\r\n\r\nslogB\r\n\x1b[0m\x00\x1b[0m\r\nslogC\r\n\x1b[0m\x00\x1b[0mslogD
   this PR: slogA\r\n\r\nslogB\r\n\x1b[0m\x1b[0m\r\nslogC\r\n\x1b[0m\x1b[0mslogD
   ```
   
   On `sim:rpserver` + `sim:rpproxy`, the same program run on the proxy 
(through `cu` on the server) still shows up on the server console through the 
RPMsg syslog server:
   ```
   proxy> upstest syslog
   syslog: done
   proxy> [    7.010636] [proxy] slogA
   
   slogB
   [    7.010710] [proxy]
   slogC
   [    7.010777] [proxy] slogD
   ```
   
   A channel that really fills up (the case of the first and fourth commits) 
was not reproduced here.
   


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