Skip to content

Add EINTR retry coverage to negotiation sniffer - #134

Merged
oferchen merged 1 commit into
masterfrom
codex/implement-rsync-3.4.1-functional-parity
Oct 15, 2025
Merged

oferchen merged 1 commit into
masterfrom
codex/implement-rsync-3.4.1-functional-parity

Conversation

@oferchen

@oferchen oferchen commented Oct 15, 2025 •

Copy link
Copy Markdown
Owner

Summary

  • add a test reader that injects EINTR during negotiation prologue sniffing
  • exercise the sniffer to prove it retries the read and preserves the buffered prefix

Testing

  • cargo test

@oferchen
oferchen merged commit 0289cc4 into master Oct 15, 2025
@oferchen
oferchen deleted the codex/implement-rsync-3.4.1-functional-parity branch October 15, 2025 23:03
oferchen added a commit that referenced this pull request Aug 15, 2026
…#7357)

Upstream has exactly one writer to the log file: `logit()` (log.c:126),
which hands every line to `filtered_fwrite(logfile_fp, buf, len, 0, 1,
trailing)` at log.c:132. Escaping in the sink is what makes it
unbypassable - a producer cannot forget it, because there is no other
way to reach the file.

oc-rsync escaped in the CLI out-format renderer instead. That covered
`--log-file` on the client and nothing else: the daemon writes its log
through an entirely different path, so a peer-supplied operand reached
the log with a raw ESC where rsync 3.5.0 writes `\#33`.

Move the escaper down to `logging_sink::logfile::LogFileWriter`, the one
type both the CLI (summary.rs) and the daemon (daemon.rs, privilege.rs,
hardening.rs, module_access) construct for a log file, and switch the
renderer to the new `EscapeStyle::passthrough`. Applying both would
double-escape: the sink's `\#ddd` guard (log.c:252-254) fires on the
renderer's own output and turns `\#33` into `\#134#033`.

Two consequences fall out of putting it in the sink:

- `EscapeStyle` gains an `escape_controls` switch. Upstream has no such
  switch because every `filtered_fwrite` call site escapes C0 controls;
  oc-rsync needs one because it now has a sink upstream does not - the
  passthrough style, whose whole job is to leave the bytes alone.

- The `Write` adapter now treats a newline as a record terminator only
  when it ends the write. A newline *inside* the message is escaped as
  `\#12`, matching log.c:128-129, which strips exactly one trailing
  newline. This is the injection itself: every line the sink emits gets
  a real timestamp and pid, so a name carrying `\n` would otherwise
  produce a second, entirely authentic-looking record.

Tests: a daemon integration test pins the half a renderer-side escaper
cannot reach (peer-supplied operand in the `client args:` line), with a
printable control operand so the assertions cannot pass vacuously. It
fails with a raw ESC in the log when the sink escaping is reverted. The
existing 3.5.0 oracle test for `--log-file` still passes unchanged,
which is what proves the move did not perturb the client half.
oferchen added a commit that referenced this pull request Aug 24, 2026
…#7357)

Upstream has exactly one writer to the log file: `logit()` (log.c:126),
which hands every line to `filtered_fwrite(logfile_fp, buf, len, 0, 1,
trailing)` at log.c:132. Escaping in the sink is what makes it
unbypassable - a producer cannot forget it, because there is no other
way to reach the file.

oc-rsync escaped in the CLI out-format renderer instead. That covered
`--log-file` on the client and nothing else: the daemon writes its log
through an entirely different path, so a peer-supplied operand reached
the log with a raw ESC where rsync 3.5.0 writes `\#33`.

Move the escaper down to `logging_sink::logfile::LogFileWriter`, the one
type both the CLI (summary.rs) and the daemon (daemon.rs, privilege.rs,
hardening.rs, module_access) construct for a log file, and switch the
renderer to the new `EscapeStyle::passthrough`. Applying both would
double-escape: the sink's `\#ddd` guard (log.c:252-254) fires on the
renderer's own output and turns `\#33` into `\#134#033`.

Two consequences fall out of putting it in the sink:

- `EscapeStyle` gains an `escape_controls` switch. Upstream has no such
  switch because every `filtered_fwrite` call site escapes C0 controls;
  oc-rsync needs one because it now has a sink upstream does not - the
  passthrough style, whose whole job is to leave the bytes alone.

- The `Write` adapter now treats a newline as a record terminator only
  when it ends the write. A newline *inside* the message is escaped as
  `\#12`, matching log.c:128-129, which strips exactly one trailing
  newline. This is the injection itself: every line the sink emits gets
  a real timestamp and pid, so a name carrying `\n` would otherwise
  produce a second, entirely authentic-looking record.

Tests: a daemon integration test pins the half a renderer-side escaper
cannot reach (peer-supplied operand in the `client args:` line), with a
printable control operand so the assertions cannot pass vacuously. It
fails with a raw ESC in the log when the sink escaping is reverted. The
existing 3.5.0 oracle test for `--log-file` still passes unchanged,
which is what proves the move did not perturb the client half.
oferchen added a commit that referenced this pull request Nov 27, 2026
## What rsync 3.5.2 does

`rwrite()` hands every FERROR/FWARNING/FINFO line to `filtered_fwrite()`
before it reaches the terminal:

- A client without `-8` opens `ic_chck = iconv_open(cs, cs)`
(rsync.c:96-98). `rwrite()` then validates the line in the locale
codeset (log.c:463-496). Each rejected byte is written as `\#ooo`
(log.c:487), and the valid characters go through `filtered_fwrite(f,
convbuf, len, 0, ..)` (log.c:476).
- A `-8` client or a server takes `filtered_fwrite(f, buf, len,
!allow_8bit_chars, ..)` (log.c:499).
- `filtered_char_len()` (log.c:276-293) escapes C0 except TAB, DEL,
0x80-0x9F, and the backslash of a literal `\#ddd` (log.c:288-291). With
`use_isprint` it also escapes every byte `isprint()` rejects.
- `rwrite()` peels one trailing newline and writes it raw (log.c:460).
- A refused filter rule is echoed through `rprintf(FERROR, ...)`:
`filter_rule_err()` at exclude.c:133-136 and the `invalid modifier` line
at exclude.c:1376. So a VT in the rule prints as `\#13`, both in the
`'%c'` detail and in the echoed rule.

## What oc did

oc's daemon log file already escaped its lines (`LogFileWriter`,
mirroring `logit()` at log.c:143), and so did lines forwarded from a
server in MSG frames. Lines the client printed itself were not escaped:

- A refused `--filter` rule (`write_refusal`).
- Every `Message` written through the client stderr `MessageSink`.
- The pre-formatted cause lines of a `ClientError` report.
- The local-copy `link_stat ... failed` / `file has vanished` operand
lines, which the engine printed with `eprintln!`.

## The fix

It reuses the existing escaper in `logging::escape`. There is no second
escaper.

- `logging::escape`:
- `may_need_escaping()` is the fast-path test `escape_for_output`
already ran, now split out so sinks can skip the copy.
- `write_terminal_line()` / `eprint_terminal_line()` write one escaped
stderr line. A clean line goes out with no copy.
- `MessageSink`:
- A `terminal_escaping` flag, set on the client stderr sink. A message
whose text is clean keeps the vectored, allocation-free render. Only a
text with something to escape is rendered into a buffer and escaped
once.
  - `write_escaped_line()` for pre-formatted lines.
- The style is read at write time, because `-8` and the locale are
parsed after the sink is built.
- `write_refusal` takes an `EscapeStyle`:
  - Client: the sink's style.
- Server argv: `terminal_style()`, as log.c:499 applies on a server
before multiplexing.
- Daemon: `passthrough()`. This is deliberate. The daemon's refusal
lines also go to its log sink, which escapes on its own as upstream's
`logit()` does (log.c:143). Escaping here as well would print
`\#134#013`, so each line is still escaped exactly once.
- `emit_error_report` sends its cause and program lines through
`write_escaped_line`. The remote shell's own stderr is still written as
received.

## Measured on Linux, stderr cause line (`od -c`) and exit code

| cell | rsync 3.5.2 | oc master | oc with this change |
|---|---|---|---|
| `--filter=$'+\vfoo'` | `invalid modifier '\#13' at position 1 in
filter rule: +\#013foo`, exit 1 | raw `\v` twice, exit 1 | `invalid
modifier '\#13' at position 1 in filter rule: +\#013foo`, exit 1 |
| `--filter=$'foo\vbar'` | `Unknown filter rule: foo\#013bar`, exit 1 |
raw `\v`, exit 1 | `Unknown filter rule: foo\#013bar`, exit 1 |
| missing operand `no\001pe` | `link_stat ".../no\#001pe" failed`, exit
23 | raw `\001` on both lines, exit 23 | `link_stat ".../no\#001pe"
failed` on both lines, exit 23 |
| same, forwarded from a remote server | `\#1`, exit 23 | `\#1`,
exit 23 | `\#1`, exit 23 (unchanged) |
| missing operand `no\tpe` | raw TAB, exit 23 | raw TAB, exit 23 | raw
TAB, exit 23 |
| operand `café\001`, `LC_ALL=C` | `caf\#303\#251\#1` | raw |
`caf\#303\#251\#1` |
| operand `café\001`, `-8` or `C.UTF-8` | `caf` + raw `é` + `\#1` |
raw | `caf` + raw `é` + `\#1` |

## Tests

- `crates/cli/tests/client_stderr_control_byte_escaping.rs`:
- Three oracle cells against rsync 3.5.2: VT in a refused modifier, the
same under `-8`, and a control byte in an unknown rule. Each compares
the cause line byte for byte, plus the exit code.
- One cell for a control byte in a missing operand. It checks the
escaping and exit 23, not the whole line, because the surrounding
wording still differs (see below).
- `MessageSink` unit tests, one per byte class:
  - C0 escaped and TAB raw.
  - DEL and high/C1 bytes.
  - The literal `\#ddd` guard.
  - An embedded newline staying on one line.
  - A raw trailing newline.
  - `write_escaped_line`.
- Unit tests for `write_refusal` (VT) and `write_terminal_line`.

Mutation: with the escaping switched off at each new site (the client
sink flag, the `write_refusal` style, `MessageSink::escape_style`,
`write_terminal_line`), all 12 new tests fail: 4/4 cli, 6/6
logging-sink, 1/1 core, 1/1 logging.

## Multi-line causes

Escaping showed that oc folded some multi-line diagnostics into one
line. Each such cause is now written one line per upstream `rprintf`
call:

- `Unexpected local arg: %s` and `If arg is a remote file/dir, prefix it
with a colon (:).` are two `rprintf(FERROR, ...)` calls in 3.5.2
(main.c:1591-1592). oc built one cause with an embedded newline, so
escaping printed `\#12` between the two parts. A `ClientError` cause
now holds one line per upstream call (`cause_lines()`), and the report
writes each line through the escaper. A newline inside the operand
itself is still escaped as `\#12`, as log.c:288-291 does.
- clap's parse error spans several lines. oc writes it line by line, so
its own line breaks stay raw and only bytes inside each line are
escaped. Upstream prints its option error as one popt line through
`option_error()` (options.c:2087-2090, options.c:933), then the trailer
(main.c:1916-1917). The wording differs, and this PR does not change it:
- 3.5.2: `rsync: --version=extra: option does not take an argument`,
then `rsync error: syntax or usage error (code 1) at main.c(1917)
[client=3.5.2]`
- oc: `rsync error: syntax or usage error: unexpected value 'extra' for
'--version...' found; no more were expected`, a blank line, `Usage:
rsync --version... [args]...`, then ` (code 1) [client=...]`
- The unknown-option refusal and the failed `--log-file` open line were
raw writes. They now go through the escaper too. Measured: 3.5.2 prints
`rsync: --bo\#001gus: unknown option`, and oc now prints the same line.

- A daemon that sends free text and then closes before acknowledging a
module listing used to be reported as one message, with the received
lines joined by newlines. Escaping would have printed `\#12` between
them. Upstream prints each received line with its own `rprintf`
(clientserver.c:428-429 for `@ERROR`, 438-439 for MOTD and listing
lines). Each received line is now its own escaped stderr line, followed
by the existing `daemon refused module listing` trailer. The trailer no
longer repeats the lines. The connect-failure lines on that path also go
through the escaper now.

New tests in `crates/cli/src/frontend/tests/stderr_lines.rs`. Each one
fails with the split reverted:
- the local-after-remote refusal prints 3 lines, and a newline in the
operand prints as `\#12`
- a parse error keeps its lines
- an unknown option is escaped
- a daemon refusal prints one stderr line per received line, with a
control byte escaped (plus a core `module_list` test that the refusal
keeps one cause line per received line)

## Gates (Linux host, on master 0c57d58)

| gate | exit |
|---|---|
| `cargo fmt --all -- --check` | 0 |
| `python3 tools/ci/check_rustfmt_all.py` | 0 |
| clippy on logging, logging-sink, core, cli, daemon, engine
(`--all-targets --all-features -D warnings`) | 0 |
| `cargo check --workspace --all-features --all-targets --target
x86_64-pc-windows-gnu` | 0 |
| `cargo clippy --workspace --all-features --all-targets --target
x86_64-pc-windows-gnu --no-deps -- -D warnings` | 0 |
| `cargo run --locked -p xtask -- no-placeholders` | 0 |
| `tools/ci/citation_drift_audit.py --ratchet` | 0 |
| new oracle file vs rsync 3.5.2 (`OC_RSYNC_UPSTREAM_COMPAT=1`) | 0
(4/4) |
| cli `--lib` (all frontend unit tests) | 0 (3142/3142) |
| cli `stderr_lines` + `clap` under `LC_ALL=C` and `LC_ALL=en_US.UTF-8`
| 0 (7/7 each) |
| cli summary/filters/argv_filters/refusal/filter_rule units | 0 (67/67)
|
| cli `module` (module listing) | 0 (20/20) |
| core `module_list` | 0 (269/269) |
| daemon `/list/` | 0 (118/118) |
| cli `client_output_locale_escaping`,
`upstream_compat_filter_rule_parse` | 0 (31/31) |
| bin `ssh_transport` | 0 (15/15) |
| test-support `daemon_pull_every_operand` | 0 (10/10) |
| core `filter_args`, `client::error` | 0 (357/357) |
| core `client::remote::invocation` | 0 (322/322) |
| logging-sink `message_sink` | 0 (63/63) |
| logging `escape` | 0 (50/50) |
| engine orchestration/link_stat/vanished | 0 (20/20) |
| daemon `/argv_filters/`, `/daemon_filter/` | 0 (39/39) |

## Remaining differences

- The missing-operand report is worded differently from 3.5.2. oc prints
a bare `link_stat ...` line, then an `rsync error: link_stat ... (code
23)` trailer. 3.5.2 prints `rsync: [sender] link_stat ...`, then `some
files/attrs were not transferred ... (code 23)`. That is a separate
text-parity gap; this change only fixes the escaping.
- Other raw `eprintln!` diagnostics in the local-copy engine carry names
that are not escaped yet. Examples: `symlink has no referent`,
`opendir`, `directory has vanished`, and the skip notices. They can move
to `eprint_terminal_line()` one by one.
- The same applies to the remaining raw stderr writes of an oc
`--server`, and to the copy of a daemon's refusal on its own stderr.
- Stdout escaping is untouched. The escaping of valid UTF-8 names on
stdout is tracked separately.

Closes #8884
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant