Add check for iptables xlock support

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-05-23 14:18:50 -07:00
parent cc706701b0
commit 1c07f75e6f

View file

@ -20,6 +20,7 @@ const (
var ( var (
ErrIptablesNotFound = errors.New("Iptables not found") ErrIptablesNotFound = errors.New("Iptables not found")
nat = []string{"-t", "nat"} nat = []string{"-t", "nat"}
supportsXlock = false
) )
type Chain struct { type Chain struct {
@ -27,6 +28,10 @@ type Chain struct {
Bridge string Bridge string
} }
func init() {
supportsXlock = exec.Command("iptables", "--wait", "-L", "-n").Run() == nil
}
func NewChain(name, bridge string) (*Chain, error) { func NewChain(name, bridge string) (*Chain, error) {
if output, err := Raw("-t", "nat", "-N", name); err != nil { if output, err := Raw("-t", "nat", "-N", name); err != nil {
return nil, err return nil, err
@ -147,12 +152,19 @@ func Raw(args ...string) ([]byte, error) {
if err != nil { if err != nil {
return nil, ErrIptablesNotFound return nil, ErrIptablesNotFound
} }
if supportsXlock {
args = append([]string{"--wait"}, args...)
}
if os.Getenv("DEBUG") != "" { if os.Getenv("DEBUG") != "" {
fmt.Printf("[DEBUG] [iptables]: %s, %v\n", path, args) fmt.Printf("[DEBUG] [iptables]: %s, %v\n", path, args)
} }
output, err := exec.Command(path, append([]string{"--wait"}, args...)...).CombinedOutput()
output, err := exec.Command(path, args...).CombinedOutput()
if err != nil { if err != nil {
return nil, fmt.Errorf("iptables failed: iptables %v: %s (%s)", strings.Join(args, " "), output, err) return nil, fmt.Errorf("iptables failed: iptables %v: %s (%s)", strings.Join(args, " "), output, err)
} }
return output, err return output, err
} }