Skip to content

Commit

Permalink
Rearrange the dup() functions to compile on more platforms
Browse files Browse the repository at this point in the history
This fixes issue #127. Thanks @nmeum for the suggestion - this should
provide a path forward for more OSes and architectures.
  • Loading branch information
gcla committed Jul 30, 2021
1 parent feea76b commit cdfed14
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 7 deletions.
1 change: 0 additions & 1 deletion go.mod
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions go.sum
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions system/dumpcapext.go
Expand Up @@ -3,7 +3,7 @@
// file.

// +build !windows
// +build !arm64
// +build !darwin

package system

Expand Down Expand Up @@ -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])
Expand Down
40 changes: 40 additions & 0 deletions 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 <iface> 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
}
15 changes: 15 additions & 0 deletions 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)
}
11 changes: 11 additions & 0 deletions 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)
}
11 changes: 11 additions & 0 deletions 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)
}

0 comments on commit cdfed14

Please sign in to comment.