You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
6.7 KiB

  1. // Package gracenet provides a family of Listen functions that either open a
  2. // fresh connection or provide an inherited connection from when the process
  3. // was started. The behave like their counterparts in the net package, but
  4. // transparently provide support for graceful restarts without dropping
  5. // connections. This is provided in a systemd socket activation compatible form
  6. // to allow using socket activation.
  7. //
  8. // BUG: Doesn't handle closing of listeners.
  9. package gracenet
  10. import (
  11. "fmt"
  12. "net"
  13. "os"
  14. "os/exec"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. )
  19. const (
  20. // Used to indicate a graceful restart in the new process.
  21. envCountKey = "LISTEN_FDS"
  22. envCountKeyPrefix = envCountKey + "="
  23. )
  24. // In order to keep the working directory the same as when we started we record
  25. // it at startup.
  26. var originalWD, _ = os.Getwd()
  27. // Net provides the family of Listen functions and maintains the associated
  28. // state. Typically you will have only once instance of Net per application.
  29. type Net struct {
  30. inherited []net.Listener
  31. active []net.Listener
  32. mutex sync.Mutex
  33. inheritOnce sync.Once
  34. // used in tests to override the default behavior of starting from fd 3.
  35. fdStart int
  36. }
  37. func (n *Net) inherit() error {
  38. var retErr error
  39. n.inheritOnce.Do(func() {
  40. n.mutex.Lock()
  41. defer n.mutex.Unlock()
  42. countStr := os.Getenv(envCountKey)
  43. if countStr == "" {
  44. return
  45. }
  46. count, err := strconv.Atoi(countStr)
  47. if err != nil {
  48. retErr = fmt.Errorf("found invalid count value: %s=%s", envCountKey, countStr)
  49. return
  50. }
  51. // In tests this may be overridden.
  52. fdStart := n.fdStart
  53. if fdStart == 0 {
  54. // In normal operations if we are inheriting, the listeners will begin at
  55. // fd 3.
  56. fdStart = 3
  57. }
  58. for i := fdStart; i < fdStart+count; i++ {
  59. file := os.NewFile(uintptr(i), "listener")
  60. l, err := net.FileListener(file)
  61. if err != nil {
  62. file.Close()
  63. retErr = fmt.Errorf("error inheriting socket fd %d: %s", i, err)
  64. return
  65. }
  66. if err := file.Close(); err != nil {
  67. retErr = fmt.Errorf("error closing inherited socket fd %d: %s", i, err)
  68. return
  69. }
  70. n.inherited = append(n.inherited, l)
  71. }
  72. })
  73. return retErr
  74. }
  75. // Listen announces on the local network address laddr. The network net must be
  76. // a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket". It
  77. // returns an inherited net.Listener for the matching network and address, or
  78. // creates a new one using net.Listen.
  79. func (n *Net) Listen(nett, laddr string) (net.Listener, error) {
  80. switch nett {
  81. default:
  82. return nil, net.UnknownNetworkError(nett)
  83. case "tcp", "tcp4", "tcp6":
  84. addr, err := net.ResolveTCPAddr(nett, laddr)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return n.ListenTCP(nett, addr)
  89. case "unix", "unixpacket", "invalid_unix_net_for_test":
  90. addr, err := net.ResolveUnixAddr(nett, laddr)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return n.ListenUnix(nett, addr)
  95. }
  96. }
  97. // ListenTCP announces on the local network address laddr. The network net must
  98. // be: "tcp", "tcp4" or "tcp6". It returns an inherited net.Listener for the
  99. // matching network and address, or creates a new one using net.ListenTCP.
  100. func (n *Net) ListenTCP(nett string, laddr *net.TCPAddr) (*net.TCPListener, error) {
  101. if err := n.inherit(); err != nil {
  102. return nil, err
  103. }
  104. n.mutex.Lock()
  105. defer n.mutex.Unlock()
  106. // look for an inherited listener
  107. for i, l := range n.inherited {
  108. if l == nil { // we nil used inherited listeners
  109. continue
  110. }
  111. if isSameAddr(l.Addr(), laddr) {
  112. n.inherited[i] = nil
  113. n.active = append(n.active, l)
  114. return l.(*net.TCPListener), nil
  115. }
  116. }
  117. // make a fresh listener
  118. l, err := net.ListenTCP(nett, laddr)
  119. if err != nil {
  120. return nil, err
  121. }
  122. n.active = append(n.active, l)
  123. return l, nil
  124. }
  125. // ListenUnix announces on the local network address laddr. The network net
  126. // must be a: "unix" or "unixpacket". It returns an inherited net.Listener for
  127. // the matching network and address, or creates a new one using net.ListenUnix.
  128. func (n *Net) ListenUnix(nett string, laddr *net.UnixAddr) (*net.UnixListener, error) {
  129. if err := n.inherit(); err != nil {
  130. return nil, err
  131. }
  132. n.mutex.Lock()
  133. defer n.mutex.Unlock()
  134. // look for an inherited listener
  135. for i, l := range n.inherited {
  136. if l == nil { // we nil used inherited listeners
  137. continue
  138. }
  139. if isSameAddr(l.Addr(), laddr) {
  140. n.inherited[i] = nil
  141. n.active = append(n.active, l)
  142. return l.(*net.UnixListener), nil
  143. }
  144. }
  145. // make a fresh listener
  146. l, err := net.ListenUnix(nett, laddr)
  147. if err != nil {
  148. return nil, err
  149. }
  150. n.active = append(n.active, l)
  151. return l, nil
  152. }
  153. // activeListeners returns a snapshot copy of the active listeners.
  154. func (n *Net) activeListeners() ([]net.Listener, error) {
  155. n.mutex.Lock()
  156. defer n.mutex.Unlock()
  157. ls := make([]net.Listener, len(n.active))
  158. copy(ls, n.active)
  159. return ls, nil
  160. }
  161. func isSameAddr(a1, a2 net.Addr) bool {
  162. if a1.Network() != a2.Network() {
  163. return false
  164. }
  165. a1s := a1.String()
  166. a2s := a2.String()
  167. if a1s == a2s {
  168. return true
  169. }
  170. // This allows for ipv6 vs ipv4 local addresses to compare as equal. This
  171. // scenario is common when listening on localhost.
  172. const ipv6prefix = "[::]"
  173. a1s = strings.TrimPrefix(a1s, ipv6prefix)
  174. a2s = strings.TrimPrefix(a2s, ipv6prefix)
  175. const ipv4prefix = "0.0.0.0"
  176. a1s = strings.TrimPrefix(a1s, ipv4prefix)
  177. a2s = strings.TrimPrefix(a2s, ipv4prefix)
  178. return a1s == a2s
  179. }
  180. // StartProcess starts a new process passing it the active listeners. It
  181. // doesn't fork, but starts a new process using the same environment and
  182. // arguments as when it was originally started. This allows for a newly
  183. // deployed binary to be started. It returns the pid of the newly started
  184. // process when successful.
  185. func (n *Net) StartProcess() (int, error) {
  186. listeners, err := n.activeListeners()
  187. if err != nil {
  188. return 0, err
  189. }
  190. // Extract the fds from the listeners.
  191. files := make([]*os.File, len(listeners))
  192. for i, l := range listeners {
  193. files[i], err = l.(filer).File()
  194. if err != nil {
  195. return 0, err
  196. }
  197. defer files[i].Close()
  198. }
  199. // Use the original binary location. This works with symlinks such that if
  200. // the file it points to has been changed we will use the updated symlink.
  201. argv0, err := exec.LookPath(os.Args[0])
  202. if err != nil {
  203. return 0, err
  204. }
  205. // Pass on the environment and replace the old count key with the new one.
  206. var env []string
  207. for _, v := range os.Environ() {
  208. if !strings.HasPrefix(v, envCountKeyPrefix) {
  209. env = append(env, v)
  210. }
  211. }
  212. env = append(env, fmt.Sprintf("%s%d", envCountKeyPrefix, len(listeners)))
  213. allFiles := append([]*os.File{os.Stdin, os.Stdout, os.Stderr}, files...)
  214. process, err := os.StartProcess(argv0, os.Args, &os.ProcAttr{
  215. Dir: originalWD,
  216. Env: env,
  217. Files: allFiles,
  218. })
  219. if err != nil {
  220. return 0, err
  221. }
  222. return process.Pid, nil
  223. }
  224. type filer interface {
  225. File() (*os.File, error)
  226. }