mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-25 19:15:34 +00:00
c467ed0b62
In DeviceConfig, we did not close r after calling FromUAPI. If FromUAPI returned early due to an error, then it might not have read all the data that IpcGetOperation wanted to write. As a result, IpcGetOperation could hang, as in #3220. We were also closing the wrong end of the pipe after IpcSetOperation in ReconfigDevice. To ensure that we get all available information to diagnose such a situation, include all errors anytime something goes wrong. This should fix the immediate crashing problem in #3220. We'll then need to figure out why IpcGetOperation was failing. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package wgcfg
|
|
|
|
import (
|
|
"io"
|
|
"sort"
|
|
|
|
"golang.zx2c4.com/wireguard/device"
|
|
"tailscale.com/types/logger"
|
|
"tailscale.com/util/multierr"
|
|
)
|
|
|
|
func DeviceConfig(d *device.Device) (*Config, error) {
|
|
r, w := io.Pipe()
|
|
errc := make(chan error, 1)
|
|
go func() {
|
|
errc <- d.IpcGetOperation(w)
|
|
w.Close()
|
|
}()
|
|
cfg, fromErr := FromUAPI(r)
|
|
r.Close()
|
|
getErr := <-errc
|
|
err := multierr.New(getErr, fromErr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sort.Slice(cfg.Peers, func(i, j int) bool {
|
|
return cfg.Peers[i].PublicKey.Less(cfg.Peers[j].PublicKey)
|
|
})
|
|
return cfg, nil
|
|
}
|
|
|
|
// ReconfigDevice replaces the existing device configuration with cfg.
|
|
func ReconfigDevice(d *device.Device, cfg *Config, logf logger.Logf) (err error) {
|
|
defer func() {
|
|
if err != nil {
|
|
logf("wgcfg.Reconfig failed: %v", err)
|
|
}
|
|
}()
|
|
|
|
prev, err := DeviceConfig(d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r, w := io.Pipe()
|
|
errc := make(chan error, 1)
|
|
go func() {
|
|
errc <- d.IpcSetOperation(r)
|
|
r.Close()
|
|
}()
|
|
|
|
toErr := cfg.ToUAPI(w, prev)
|
|
w.Close()
|
|
setErr := <-errc
|
|
return multierr.New(setErr, toErr)
|
|
}
|