fix: use golang.org/x/sys/execabs (#224)

Closes https://github.com/ooni/probe-engine/issues/1195
This commit is contained in:
Simone Basso 2021-02-10 07:40:48 +01:00 committed by GitHub
parent f53b3be66e
commit 18ca6d5f35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 20 deletions

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"strconv" "strconv"
"strings" "strings"
"text/template" "text/template"
@ -14,6 +13,7 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils" "github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils"
"github.com/ooni/probe-cli/v3/internal/engine/shellx" "github.com/ooni/probe-cli/v3/internal/engine/shellx"
"golang.org/x/sys/execabs"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -55,7 +55,7 @@ func runQuiteQuietly(name string, arg ...string) error {
} }
func darwinVersionMajor() (int, error) { func darwinVersionMajor() (int, error) {
out, err := exec.Command("uname", "-r").Output() out, err := execabs.Command("uname", "-r").Output()
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -129,7 +129,7 @@ func (m managerDarwin) Start() error {
} }
func (managerDarwin) stop() error { func (managerDarwin) stop() error {
var failure *exec.ExitError var failure *execabs.ExitError
err := runQuiteQuietly("launchctl", "bootout", serviceTarget) err := runQuiteQuietly("launchctl", "bootout", serviceTarget)
if errors.As(err, &failure) && failure.ExitCode() == int(unix.ESRCH) { if errors.As(err, &failure) && failure.ExitCode() == int(unix.ESRCH) {
err = nil err = nil
@ -158,7 +158,7 @@ func (m managerDarwin) Stop() error {
func (m managerDarwin) Status() (string, error) { func (m managerDarwin) Status() (string, error) {
err := runQuiteQuietly("launchctl", "kill", "SIGINFO", serviceTarget) err := runQuiteQuietly("launchctl", "kill", "SIGINFO", serviceTarget)
var failure *exec.ExitError var failure *execabs.ExitError
if errors.As(err, &failure) { if errors.As(err, &failure) {
switch failure.ExitCode() { switch failure.ExitCode() {
case int(unix.ESRCH): case int(unix.ESRCH):

View File

@ -6,12 +6,13 @@ import (
"bytes" "bytes"
"errors" "errors"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"golang.org/x/sys/execabs"
) )
// DisableCache will disable caching of the home directory. Caching is enabled // DisableCache will disable caching of the home directory. Caching is enabled
@ -103,7 +104,7 @@ func dirDarwin() (string, error) {
var stdout bytes.Buffer var stdout bytes.Buffer
// If that fails, try OS specific commands // If that fails, try OS specific commands
cmd := exec.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`) cmd := execabs.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`)
cmd.Stdout = &stdout cmd.Stdout = &stdout
if err := cmd.Run(); err == nil { if err := cmd.Run(); err == nil {
result := strings.TrimSpace(stdout.String()) result := strings.TrimSpace(stdout.String())
@ -114,7 +115,7 @@ func dirDarwin() (string, error) {
// try the shell // try the shell
stdout.Reset() stdout.Reset()
cmd = exec.Command("sh", "-c", "cd && pwd") cmd = execabs.Command("sh", "-c", "cd && pwd")
cmd.Stdout = &stdout cmd.Stdout = &stdout
if err := cmd.Run(); err == nil { if err := cmd.Run(); err == nil {
result := strings.TrimSpace(stdout.String()) result := strings.TrimSpace(stdout.String())
@ -125,7 +126,7 @@ func dirDarwin() (string, error) {
// try to figure out the user and check the default location // try to figure out the user and check the default location
stdout.Reset() stdout.Reset()
cmd = exec.Command("whoami") cmd = execabs.Command("whoami")
cmd.Stdout = &stdout cmd.Stdout = &stdout
if err := cmd.Run(); err == nil { if err := cmd.Run(); err == nil {
user := strings.TrimSpace(stdout.String()) user := strings.TrimSpace(stdout.String())
@ -150,7 +151,7 @@ func dirUnix() (string, error) {
var stdout bytes.Buffer var stdout bytes.Buffer
// If that fails, try OS specific commands // If that fails, try OS specific commands
cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid())) cmd := execabs.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
cmd.Stdout = &stdout cmd.Stdout = &stdout
if err := cmd.Run(); err == nil { if err := cmd.Run(); err == nil {
if passwd := strings.TrimSpace(stdout.String()); passwd != "" { if passwd := strings.TrimSpace(stdout.String()); passwd != "" {
@ -164,7 +165,7 @@ func dirUnix() (string, error) {
// If all else fails, try the shell // If all else fails, try the shell
stdout.Reset() stdout.Reset()
cmd = exec.Command("sh", "-c", "cd && pwd") cmd = execabs.Command("sh", "-c", "cd && pwd")
cmd.Stdout = &stdout cmd.Stdout = &stdout
if err := cmd.Run(); err == nil { if err := cmd.Run(); err == nil {
result := strings.TrimSpace(stdout.String()) result := strings.TrimSpace(stdout.String())
@ -175,7 +176,7 @@ func dirUnix() (string, error) {
// try to figure out the user and check the default location // try to figure out the user and check the default location
stdout.Reset() stdout.Reset()
cmd = exec.Command("whoami") cmd = execabs.Command("whoami")
cmd.Stdout = &stdout cmd.Stdout = &stdout
if err := cmd.Run(); err == nil { if err := cmd.Run(); err == nil {
user := strings.TrimSpace(stdout.String()) user := strings.TrimSpace(stdout.String())

View File

@ -7,9 +7,10 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"golang.org/x/sys/execabs"
) )
func fatalOnError(err error) { func fatalOnError(err error) {
@ -70,7 +71,7 @@ func main() {
options = append(options, *entry.Input) options = append(options, *entry.Input)
} }
log.Printf("run: go %s", strings.Join(options, " ")) log.Printf("run: go %s", strings.Join(options, " "))
cmd := exec.Command("go", options...) cmd := execabs.Command("go", options...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
err = cmd.Run() err = cmd.Run()
fatalOnError(err) fatalOnError(err)

View File

@ -7,12 +7,13 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"os/exec"
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
"time" "time"
"golang.org/x/sys/execabs"
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/cmd/jafar/resolver" "github.com/ooni/probe-cli/v3/internal/cmd/jafar/resolver"
"github.com/ooni/probe-cli/v3/internal/cmd/jafar/uncensored" "github.com/ooni/probe-cli/v3/internal/cmd/jafar/uncensored"
@ -295,7 +296,7 @@ func TestHijackHTTP(t *testing.T) {
if err == nil { if err == nil {
t.Fatal("expected an error here") t.Fatal("expected an error here")
} }
var exitErr *exec.ExitError var exitErr *execabs.ExitError
if !errors.As(err, &exitErr) { if !errors.As(err, &exitErr) {
t.Fatal("not the error type we expected") t.Fatal("not the error type we expected")
} }
@ -335,7 +336,7 @@ func TestHijackHTTPS(t *testing.T) {
t.Fatal("expected an error here") t.Fatal("expected an error here")
} }
t.Log(err) t.Log(err)
var exitErr *exec.ExitError var exitErr *execabs.ExitError
if !errors.As(err, &exitErr) { if !errors.As(err, &exitErr) {
t.Fatal("not the error type we expected") t.Fatal("not the error type we expected")
} }

View File

@ -10,11 +10,12 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"os/exec"
"os/signal" "os/signal"
"strings" "strings"
"syscall" "syscall"
"golang.org/x/sys/execabs"
"github.com/apex/log" "github.com/apex/log"
"github.com/apex/log/handlers/cli" "github.com/apex/log/handlers/cli"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -243,7 +244,7 @@ func mustx(err error, message string, osExit func(int)) {
if err != nil { if err != nil {
var ( var (
exitcode = 1 exitcode = 1
exiterr *exec.ExitError exiterr *execabs.ExitError
) )
if errors.As(err, &exiterr) { if errors.As(err, &exiterr) {
exitcode = exiterr.ExitCode() exitcode = exiterr.ExitCode()

View File

@ -4,9 +4,10 @@ package shellx
import ( import (
"errors" "errors"
"os" "os"
"os/exec"
"strings" "strings"
"golang.org/x/sys/execabs"
"github.com/apex/log" "github.com/apex/log"
"github.com/google/shlex" "github.com/google/shlex"
"github.com/ooni/probe-cli/v3/internal/engine/model" "github.com/ooni/probe-cli/v3/internal/engine/model"
@ -22,7 +23,7 @@ type runconfig struct {
func run(config runconfig) error { func run(config runconfig) error {
config.loginfof("exec: %s %s", config.name, strings.Join(config.args, " ")) config.loginfof("exec: %s %s", config.name, strings.Join(config.args, " "))
cmd := exec.Command(config.name, config.args...) cmd := execabs.Command(config.name, config.args...)
cmd.Stdout = config.stdout cmd.Stdout = config.stdout
cmd.Stderr = config.stderr cmd.Stderr = config.stderr
err := cmd.Run() err := cmd.Run()