Skip to content

Commit

Permalink
wip: refactor to use poll
Browse files Browse the repository at this point in the history
Signed-off-by: thediveo <thediveo@gmx.eu>
  • Loading branch information
thediveo committed Dec 15, 2023
1 parent 2f45f96 commit 7fa651f
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions pipe/checker_notwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,30 @@ import (
// This implementation leverages [syscall.Select].
func WaitTillBreak(fifo *os.File) {
log.Debug("constantly monitoring packet capture fifo status...")
fds := unix.FdSet{}
fifoFdNo := int(fifo.Fd())
fds := []unix.PollFd{
{
Fd: int32(fifo.Fd()),
Events: unix.POLLIN + unix.POLLERR,
},
}
for {
// Check the fifo becomming readable, which signals that it has been
// closed. In this case, ex-termi-nate ;) Oh, and remember to correctly
// initialize the fdset each time before calling select() ... well, just
// because that's a good idea to do. :(
fds.Zero()
fds.Set(fifoFdNo)
_, err := unix.Select(
fifoFdNo+1, // highest fd is our file descriptor.
&fds, nil, nil, // only watch readable.
nil, // no timeout, ever.
)
n, err := unix.Poll(fds, 1000 /*ms*/)
if err != nil {
if err == unix.EINTR {
continue
}
log.Debugf("capture fifo broken, reason: %s", err.Error())
return
}
if fds.IsSet(fifoFdNo) {
if n <= 0 {
continue
}
log.Debugf("poll: %+v", fds)
if fds[0].Revents&unix.POLLERR != 0 {
// Either the pipe was broken by Wireshark, or we did break it on
// purpose in the piping process. Anyway, we're done.
log.Debug("capture fifo broken, stopped monitoring.")
Expand Down

0 comments on commit 7fa651f

Please sign in to comment.