From 4a6fcb5963538e3d25fc6ab8d57095d6388ef563 Mon Sep 17 00:00:00 2001 From: William Robinet Date: Fri, 18 Jun 2021 17:33:48 +0200 Subject: [PATCH] Add support for pcap output to FIFO --- pcap/logpkt.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/pcap/logpkt.c b/pcap/logpkt.c index a8be86d..6320a16 100644 --- a/pcap/logpkt.c +++ b/pcap/logpkt.c @@ -219,25 +219,30 @@ logpkt_pcap_open_fd(int fd) { pcap_file_hdr_t hdr; off_t sz; ssize_t n; + struct stat st; - sz = lseek(fd, 0, SEEK_END); - if (sz == -1) + if(fstat(fd, &st)) return -1; - if (sz > 0) { - if (lseek(fd, 0, SEEK_SET) == -1) + if(!S_ISFIFO(st.st_mode)) { + sz = lseek(fd, 0, SEEK_END); + if (sz == -1) return -1; - n = read(fd, &hdr, sizeof(pcap_file_hdr_t)); - if (n != sizeof(pcap_file_hdr_t)) - return -1; - if (hdr.magic_number == PCAP_MAGIC) - return lseek(fd, 0, SEEK_END) == -1 ? -1 : 0; - if (lseek(fd, 0, SEEK_SET) == -1) - return -1; - if (ftruncate(fd, 0) == -1) - return -1; - } + if (sz > 0) { + if (lseek(fd, 0, SEEK_SET) == -1) + return -1; + n = read(fd, &hdr, sizeof(pcap_file_hdr_t)); + if (n != sizeof(pcap_file_hdr_t)) + return -1; + if (hdr.magic_number == PCAP_MAGIC) + return lseek(fd, 0, SEEK_END) == -1 ? -1 : 0; + if (lseek(fd, 0, SEEK_SET) == -1) + return -1; + if (ftruncate(fd, 0) == -1) + return -1; + } + } return logpkt_write_global_pcap_hdr(fd); }