wgengine/router: simplify some cmd invocations.

Signed-off-by: David Anderson <danderson@tailscale.com>
reviewable/pr359/r1
David Anderson 5 years ago
parent e3559d1c45
commit 7618d7e677

@ -6,9 +6,9 @@ package router
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -49,28 +49,30 @@ func newUserspaceRouter(logf logger.Logf, _ *device.Device, tunDev tun.Device) (
}, nil }, nil
} }
func cmd(args ...string) *exec.Cmd { func cmd(args ...string) error {
if len(args) == 0 { if len(args) == 0 {
log.Fatalf("exec.Cmd(%#v) invalid; need argv[0]\n", args) return errors.New("cmd: no argv[0]")
} }
return exec.Command(args[0], args[1:]...)
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
if err != nil {
return fmt.Errorf("running %q failed: %v\n%s", strings.Join(args, " "), err, out)
}
return nil
} }
func (r *linuxRouter) Up() error { func (r *linuxRouter) Up() error {
out, err := cmd("ip", "link", "set", r.tunname, "up").CombinedOutput() if err := cmd("ip", "link", "set", r.tunname, "up"); err != nil {
if err != nil { return err
// TODO: this should return an error; why is it calling log.Fatalf?
// Audit callers to make sure they're handling errors.
log.Fatalf("running ip link failed: %v\n%s", err, out)
} }
err = r.ipt4.AppendUnique("filter", "FORWARD", r.forwardRule()...) if err := r.ipt4.AppendUnique("filter", "FORWARD", r.forwardRule()...); err != nil {
if err != nil { return err
r.logf("iptables forward failed: %v", err)
} }
err = r.ipt4.AppendUnique("nat", "POSTROUTING", r.natRule()...)
if err != nil { if err := r.ipt4.AppendUnique("nat", "POSTROUTING", r.natRule()...); err != nil {
r.logf("iptables nat failed: %v", err) return err
} }
return nil return nil
} }
@ -83,9 +85,8 @@ func (r *linuxRouter) SetRoutes(rs RouteSettings) error {
addrdel := []string{"ip", "addr", addrdel := []string{"ip", "addr",
"del", r.local.String(), "del", r.local.String(),
"dev", r.tunname} "dev", r.tunname}
out, err := cmd(addrdel...).CombinedOutput() if err := cmd(addrdel...); err != nil {
if err != nil { r.logf("addr del failed: %v", err)
r.logf("addr del failed: %v: %v\n%s", addrdel, err, out)
if errq == nil { if errq == nil {
errq = err errq = err
} }
@ -94,9 +95,8 @@ func (r *linuxRouter) SetRoutes(rs RouteSettings) error {
addradd := []string{"ip", "addr", addradd := []string{"ip", "addr",
"add", rs.LocalAddr.String(), "add", rs.LocalAddr.String(),
"dev", r.tunname} "dev", r.tunname}
out, err := cmd(addradd...).CombinedOutput() if err := cmd(addradd...); err != nil {
if err != nil { r.logf("addr add failed: %v", err)
r.logf("addr add failed: %v: %v\n%s", addradd, err, out)
if errq == nil { if errq == nil {
errq = err errq = err
} }
@ -118,9 +118,8 @@ func (r *linuxRouter) SetRoutes(rs RouteSettings) error {
"del", nstr, "del", nstr,
"via", r.local.IP.String(), "via", r.local.IP.String(),
"dev", r.tunname} "dev", r.tunname}
out, err := cmd(addrdel...).CombinedOutput() if err := cmd(addrdel...); err != nil {
if err != nil { r.logf("addr del failed: %v", err)
r.logf("addr del failed: %v: %v\n%s", addrdel, err, out)
if errq == nil { if errq == nil {
errq = err errq = err
} }
@ -136,9 +135,8 @@ func (r *linuxRouter) SetRoutes(rs RouteSettings) error {
"add", nstr, "add", nstr,
"via", rs.LocalAddr.IP.String(), "via", rs.LocalAddr.IP.String(),
"dev", r.tunname} "dev", r.tunname}
out, err := cmd(addradd...).CombinedOutput() if err := cmd(addradd...); err != nil {
if err != nil { r.logf("addr add failed: %v", err)
r.logf("addr add failed: %v: %v\n%s", addradd, err, out)
if errq == nil { if errq == nil {
errq = err errq = err
} }

Loading…
Cancel
Save