Skip to content

pcap-file: skip setvbuf on non-seekable streams (v2) - #15562

Closed
ssam18 wants to merge 1 commit into
OISF:mainfrom
ssam18:pcap-file-setvbuf-stdin-v2
Closed

pcap-file: skip setvbuf on non-seekable streams (v2)#15562
ssam18 wants to merge 1 commit into
OISF:mainfrom
ssam18:pcap-file-setvbuf-stdin-v2

Conversation

@ssam18

@ssam18 ssam18 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Make sure these boxes are checked accordingly before submitting your Pull Request -- thank you.

Contribution style:

Our Contribution agreements:

Changes (if applicable):

Link to ticket: https://redmine.openinfosecfoundation.org/issues/8464

Replaces #15384 (auto-closed because the branch was updated after the PR was opened).

Changes since #15384

  • Parse-error handling: when pcap-file.buffer-size fails to parse, the default buffer size is now retained instead of falling through and silently setting it to 0. The branches are mutually exclusive (else if) so only one of the parse-error / accepted / out-of-range messages is logged. (Addresses @jlucovsky's review.)
  • User Guide updated: --pcap-file-buffer-size now documents 0 to 64 MiB (0 disables setvbuf), and pcap-file.rst notes that buffer-size: 0 is the explicit opt-out for non-seekable sources such as stdin and named pipes. (Addresses @jlucovsky's doc comment.)

Description

Reading a pcap from /dev/stdin or a named pipe regressed in 8.0.0 with the setvbuf change in 7b730c2 and currently fails with failed to get first packet timestamp. pcap_next_ex(): -1. The reason is that InitPcapFile calls setvbuf on the FILE* underlying the pcap handle after libpcap has already consumed the pcap header, and on a non seekable fd glibc cannot recover from that and the very next read returns -1. This change detects non regular files via fstat on the underlying fd and skips setvbuf for that handle, so reading from stdin, named pipes, and other non seekable sources keeps working. It also lowers PCAP_FILE_BUFFER_SIZE_MIN to 0 so pcap-file.buffer-size = 0 stays available as an explicit opt out, matching the workaround proposed on the ticket.

Describe changes:

  • In InitPcapFile, fstat the fd behind pcap_file(handle) and skip setvbuf when the file is not a regular file. An info log explains the skip.
  • Lower PCAP_FILE_BUFFER_SIZE_MIN from 4096 to 0 so users can opt out of setvbuf explicitly via pcap-file.buffer-size = 0.
  • On a buffer-size parse error, retain the default instead of setting it to 0.
  • Verified locally that tcpdump piping a pcap into /dev/stdin and reading from a named pipe both now complete with 2 packets read and 0 errors. Regular file reads are unchanged.

SV_BRANCH=OISF/suricata-verify#3108

Reading a pcap from /dev/stdin or a named pipe currently fails with
"failed to get first packet timestamp. pcap_next_ex(): -1" because
InitPcapFile calls setvbuf on the FILE* underlying the pcap handle
after libpcap has already consumed the pcap header. On a non-seekable
fd glibc cannot recover from that and the next read returns -1.

Detect non-regular files via fstat and skip setvbuf in that case so
the read keeps working on pipes, fifos and stdin. Also lower
PCAP_FILE_BUFFER_SIZE_MIN to 0 so pcap-file.buffer-size = 0 stays
available as an explicit opt-out.

When pcap-file.buffer-size fails to parse, retain the default buffer
size instead of falling through and setting it to 0. The branches are
now mutually exclusive so only one of the parse-error, accepted, or
out-of-range messages is logged.

Update the user guide: --pcap-file-buffer-size now documents 0 to
64 MiB (0 disables setvbuf), and pcap-file.rst notes that 0 is the
opt-out for non-seekable sources such as stdin and named pipes.

Bug: OISF#8464.

@jlucovsky jlucovsky left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the values accepted for the buffer size should be
0 - disable standard buffering
4096 - PCAP_FILE_BUFFER_SIZE_MAX: buffering length set

Comment thread src/source-pcap-file-helper.c
Comment thread src/source-pcap-file.c
@ssam18

ssam18 commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @jlucovsky. All three comments are addressed in the v3 PR #15629. Closing in favor of #15629.

@ssam18 ssam18 closed this Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants