iptables_linux.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // +build linux
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package iptables
  15. import (
  16. "fmt"
  17. "net"
  18. "os"
  19. "time"
  20. "golang.org/x/sys/unix"
  21. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  22. "k8s.io/apimachinery/pkg/util/wait"
  23. )
  24. type locker struct {
  25. lock16 *os.File
  26. lock14 *net.UnixListener
  27. }
  28. func (l *locker) Close() error {
  29. errList := []error{}
  30. if l.lock16 != nil {
  31. if err := l.lock16.Close(); err != nil {
  32. errList = append(errList, err)
  33. }
  34. }
  35. if l.lock14 != nil {
  36. if err := l.lock14.Close(); err != nil {
  37. errList = append(errList, err)
  38. }
  39. }
  40. return utilerrors.NewAggregate(errList)
  41. }
  42. func grabIptablesLocks(lockfilePath string) (iptablesLocker, error) {
  43. var err error
  44. var success bool
  45. l := &locker{}
  46. defer func(l *locker) {
  47. // Clean up immediately on failure
  48. if !success {
  49. l.Close()
  50. }
  51. }(l)
  52. // Grab both 1.6.x and 1.4.x-style locks; we don't know what the
  53. // iptables-restore version is if it doesn't support --wait, so we
  54. // can't assume which lock method it'll use.
  55. // Roughly duplicate iptables 1.6.x xtables_lock() function.
  56. l.lock16, err = os.OpenFile(lockfilePath, os.O_CREATE, 0600)
  57. if err != nil {
  58. return nil, fmt.Errorf("failed to open iptables lock %s: %v", lockfilePath, err)
  59. }
  60. if err := wait.PollImmediate(200*time.Millisecond, 2*time.Second, func() (bool, error) {
  61. if err := grabIptablesFileLock(l.lock16); err != nil {
  62. return false, nil
  63. }
  64. return true, nil
  65. }); err != nil {
  66. return nil, fmt.Errorf("failed to acquire new iptables lock: %v", err)
  67. }
  68. // Roughly duplicate iptables 1.4.x xtables_lock() function.
  69. if err := wait.PollImmediate(200*time.Millisecond, 2*time.Second, func() (bool, error) {
  70. l.lock14, err = net.ListenUnix("unix", &net.UnixAddr{Name: "@xtables", Net: "unix"})
  71. if err != nil {
  72. return false, nil
  73. }
  74. return true, nil
  75. }); err != nil {
  76. return nil, fmt.Errorf("failed to acquire old iptables lock: %v", err)
  77. }
  78. success = true
  79. return l, nil
  80. }
  81. func grabIptablesFileLock(f *os.File) error {
  82. return unix.Flock(int(f.Fd()), unix.LOCK_EX|unix.LOCK_NB)
  83. }