From cdfed140128fa822d867658a74a10814dc4eff27 Mon Sep 17 00:00:00 2001 From: Graham Clark Date: Fri, 30 Jul 2021 16:19:22 -0400 Subject: [PATCH] Rearrange the dup() functions to compile on more platforms This fixes issue #127. Thanks @nmeum for the suggestion - this should provide a path forward for more OSes and architectures. --- go.mod | 1 - go.sum | 5 +---- system/dumpcapext.go | 4 ++-- system/dumpcapext_darwin.go | 40 +++++++++++++++++++++++++++++++++++++ system/dup.go | 15 ++++++++++++++ system/dup_linux_arm64.go | 11 ++++++++++ system/dup_linux_riscv64.go | 11 ++++++++++ 7 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 system/dumpcapext_darwin.go create mode 100644 system/dup.go create mode 100644 system/dup_linux_arm64.go create mode 100644 system/dup_linux_riscv64.go diff --git a/go.mod b/go.mod index 2bed9221..ec58386c 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,6 @@ require ( github.com/hashicorp/golang-lru v0.5.4 github.com/jessevdk/go-flags v1.4.0 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 - github.com/kr/pty v1.1.4 // indirect github.com/magiconair/properties v1.8.4 // indirect github.com/mattn/go-isatty v0.0.12 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index 8516d4f1..c25a67cc 100644 --- a/go.sum +++ b/go.sum @@ -44,6 +44,7 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/creack/pty v1.1.14 h1:55VbUWoBxE1iTAh3B6JztD6xyQ06CvW/31oD6rYwrtY= github.com/creack/pty v1.1.14/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -56,8 +57,6 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/gcla/deep v1.0.2 h1:qBOx6eepcOSRYnHJ+f2ih4hP4Vca1YnLtXxp73n5KWI= github.com/gcla/deep v1.0.2/go.mod h1:evE9pbpSGhItmFoBIk8hPOIC/keKTGYhFl6Le1Av+GE= -github.com/gcla/gowid v1.2.1-0.20210403174333-badb5d3b9c2e h1:1HY4w4hXm9Q7QlqOQBZD45GC3VRnD6oDcb2lEpoiv/Q= -github.com/gcla/gowid v1.2.1-0.20210403174333-badb5d3b9c2e/go.mod h1:uXYWVvhYMv/0pi2nZeIuogFporMhiivk/PPFmi+lQKw= github.com/gcla/gowid v1.2.1-0.20210730201604-e21cc1dff36b h1:mIGVx3Kq19ln5E25pwQtxCNmKi6rbrUhOgAtA1Vx+BE= github.com/gcla/gowid v1.2.1-0.20210730201604-e21cc1dff36b/go.mod h1:jQwZ6fAAnaylA+MkfgXLzjG0mTMStYixffw2jY0qG4k= github.com/gcla/tail v1.0.1-0.20190505190527-650e90873359 h1:3xEhacR7pIJV8daurdBygptxhzTJeYFqJp1V6SDl+pE= @@ -180,8 +179,6 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/pty v1.1.4 h1:5Myjjh3JY/NaAi4IsUbHADytDyl1VE1Y9PXDlL+P/VQ= -github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= diff --git a/system/dumpcapext.go b/system/dumpcapext.go index 74f03008..f1d92134 100644 --- a/system/dumpcapext.go +++ b/system/dumpcapext.go @@ -3,7 +3,7 @@ // file. // +build !windows -// +build !arm64 +// +build !darwin package system @@ -47,7 +47,7 @@ func DumpcapExt(dumpcapBin string, tsharkBin string, args ...string) error { if err != nil { log.Warnf("Unexpected error parsing %s: %v", args[1], err) } else { - err = syscall.Dup2(fd, 0) + err = Dup2(fd, 0) if err != nil { log.Warnf("Problem duplicating fd %d to 0: %v", fd, err) log.Warnf("Will not try to replace argument %s to tshark", args[1]) diff --git a/system/dumpcapext_darwin.go b/system/dumpcapext_darwin.go new file mode 100644 index 00000000..fec0d3b0 --- /dev/null +++ b/system/dumpcapext_darwin.go @@ -0,0 +1,40 @@ +// Copyright 2019-2021 Graham Clark. All rights reserved. Use of this source +// code is governed by the MIT license that can be found in the LICENSE +// file. + +package system + +import ( + "os" + "os/exec" + "syscall" + + log "github.com/sirupsen/logrus" +) + +//====================================================================== + +// DumpcapExt will run dumpcap first, but if it fails, run tshark. Intended as +// a special case to allow termshark -i to use dumpcap if possible, +// but if it fails (e.g. iface==randpkt), fall back to tshark. dumpcap is more +// efficient than tshark at just capturing, and will drop fewer packets, but +// tshark supports extcap interfaces. +func DumpcapExt(dumpcapBin string, tsharkBin string, args ...string) error { + var err error + + dumpcapCmd := exec.Command(dumpcapBin, args...) + log.Infof("Starting dumpcap command %v", dumpcapCmd) + dumpcapCmd.Stdin = os.Stdin + dumpcapCmd.Stdout = os.Stdout + dumpcapCmd.Stderr = os.Stderr + if dumpcapCmd.Run() != nil { + var tshark string + tshark, err = exec.LookPath(tsharkBin) + if err == nil { + log.Infof("Retrying with dumpcap command %v", append([]string{tshark}, args...)) + err = syscall.Exec(tshark, append([]string{tshark}, args...), os.Environ()) + } + } + + return err +} diff --git a/system/dup.go b/system/dup.go new file mode 100644 index 00000000..3429bf4b --- /dev/null +++ b/system/dup.go @@ -0,0 +1,15 @@ +// Copyright 2019-2021 Graham Clark. All rights reserved. Use of this source +// code is governed by the MIT license that can be found in the LICENSE +// file. + +// +build !windows +// +build !linux !arm64 +// +build !linux !riscv64 + +package system + +import "syscall" + +func Dup2(fd int, fd2 int) error { + return syscall.Dup2(fd, fd2) +} diff --git a/system/dup_linux_arm64.go b/system/dup_linux_arm64.go new file mode 100644 index 00000000..dffa4cd9 --- /dev/null +++ b/system/dup_linux_arm64.go @@ -0,0 +1,11 @@ +// Copyright 2019-2021 Graham Clark. All rights reserved. Use of this source +// code is governed by the MIT license that can be found in the LICENSE +// file. + +package system + +import "syscall" + +func Dup2(fd int, fd2 int) error { + return syscall.Dup3(fd, fd2, 0) +} diff --git a/system/dup_linux_riscv64.go b/system/dup_linux_riscv64.go new file mode 100644 index 00000000..dffa4cd9 --- /dev/null +++ b/system/dup_linux_riscv64.go @@ -0,0 +1,11 @@ +// Copyright 2019-2021 Graham Clark. All rights reserved. Use of this source +// code is governed by the MIT license that can be found in the LICENSE +// file. + +package system + +import "syscall" + +func Dup2(fd int, fd2 int) error { + return syscall.Dup3(fd, fd2, 0) +}