Skip to content

Commit

Permalink
Fix up stderr for all external dumpcap routines
Browse files Browse the repository at this point in the history
Termshark uses a custom approach to capturing, preferring dumpcap but
then launching tshark if dumpcap fails (e.g. on extcap interfaces). This
fixes up stderr for the non-Linux builds so that if capture fails
altogether, the stderr printed out on the terminal is more easily
digestible.
  • Loading branch information
gcla committed Jul 4, 2022
1 parent 0bc1d46 commit 949dbf7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/termshark/termshark.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ func cmain() int {
}

fmt.Fprintf(os.Stderr, "Standard error stream from %s:\n", cbin)
fmt.Fprintf(os.Stderr, "\n%s\n", stderr.String())
fmt.Fprintf(os.Stderr, "------\n%s\n------\n", stderr.String())
}
if runtime.GOOS == "linux" && os.Geteuid() != 0 {
fmt.Fprintf(os.Stderr, "You might need: sudo setcap cap_net_raw,cap_net_admin+eip %s\n", termshark.PrivilegedBin())
Expand Down
2 changes: 1 addition & 1 deletion system/dumpcapext.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func DumpcapExt(dumpcapBin string, tsharkBin string, args ...string) error {

fmt.Fprintf(os.Stderr, "Starting termshark's custom live capture procedure.\n")
dumpcapCmd := exec.Command(dumpcapBin, args...)
fmt.Fprintf(os.Stderr, "First, trying dumpcap command %v\n", dumpcapCmd)
fmt.Fprintf(os.Stderr, "Trying dumpcap command %v\n", dumpcapCmd)
dumpcapCmd.Stdin = os.Stdin
dumpcapCmd.Stdout = os.Stdout
dumpcapCmd.Stderr = os.Stderr
Expand Down
8 changes: 4 additions & 4 deletions system/dumpcapext_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
package system

import (
"fmt"
"os"
"os/exec"
"syscall"

log "github.com/sirupsen/logrus"
)

//======================================================================
Expand All @@ -26,15 +25,16 @@ func DumpcapExt(dumpcapBin string, tsharkBin string, args ...string) error {
var err error

dumpcapCmd := exec.Command(dumpcapBin, args...)
log.Infof("Starting dumpcap command %v", dumpcapCmd)
fmt.Fprintf(os.Stderr, "Starting termshark's custom live capture procedure.\n")
fmt.Fprintf(os.Stderr, "Trying dumpcap command %v\n", 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...))
fmt.Fprintf(os.Stderr, "Retrying with dumpcap command %v\n", append([]string{tshark}, args...))
err = syscall.Exec(tshark, append([]string{tshark}, args...), os.Environ())
}
}
Expand Down
8 changes: 4 additions & 4 deletions system/dumpcapext_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
package system

import (
"fmt"
"os"
"os/exec"
"syscall"

log "github.com/sirupsen/logrus"
)

//======================================================================
Expand All @@ -23,15 +22,16 @@ func DumpcapExt(dumpcapBin string, tsharkBin string, args ...string) error {
var err error

dumpcapCmd := exec.Command(dumpcapBin, args...)
log.Infof("Starting dumpcap command %v", dumpcapCmd)
fmt.Fprintf(os.Stderr, "Starting termshark's custom live capture procedure.\n")
fmt.Fprintf(os.Stderr, "Trying dumpcap command %v\n", 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...))
fmt.Fprintf(os.Stderr, "Retrying with dumpcap command %v\n", append([]string{tshark}, args...))
err = syscall.Exec(tshark, append([]string{tshark}, args...), os.Environ())
}
}
Expand Down
8 changes: 4 additions & 4 deletions system/dumpcapext_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
package system

import (
"fmt"
"os"
"os/exec"

log "github.com/sirupsen/logrus"
)

//======================================================================
Expand All @@ -20,7 +19,8 @@ import (
// tshark supports extcap interfaces.
func DumpcapExt(dumpcapBin string, tsharkBin string, args ...string) error {
dumpcapCmd := exec.Command(dumpcapBin, args...)
log.Infof("Starting dumpcap command %v", dumpcapCmd)
fmt.Fprintf(os.Stderr, "Starting termshark's custom live capture procedure.\n")
fmt.Fprintf(os.Stderr, "Trying dumpcap command %v\n", dumpcapCmd)
dumpcapCmd.Stdin = os.Stdin
dumpcapCmd.Stdout = os.Stdout
dumpcapCmd.Stderr = os.Stderr
Expand All @@ -29,7 +29,7 @@ func DumpcapExt(dumpcapBin string, tsharkBin string, args ...string) error {
}

tsharkCmd := exec.Command(tsharkBin, args...)
log.Infof("Retrying with dumpcap command %v", tsharkCmd)
fmt.Fprintf(os.Stderr, "Retrying with dumpcap command %v\n", tsharkCmd)
tsharkCmd.Stdin = os.Stdin
tsharkCmd.Stdout = os.Stdout
tsharkCmd.Stderr = os.Stderr
Expand Down

0 comments on commit 949dbf7

Please sign in to comment.