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.

restart_unix.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // +build !windows
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. // This code is heavily inspired by the archived gofacebook/gracenet/net.go handler
  6. package graceful
  7. import (
  8. "fmt"
  9. "net"
  10. "os"
  11. "os/exec"
  12. "strings"
  13. "sync"
  14. "syscall"
  15. )
  16. var killParent sync.Once
  17. // KillParent sends the kill signal to the parent process if we are a child
  18. func KillParent() {
  19. killParent.Do(func() {
  20. if GetManager().IsChild() {
  21. ppid := syscall.Getppid()
  22. if ppid > 1 {
  23. _ = syscall.Kill(ppid, syscall.SIGTERM)
  24. }
  25. }
  26. })
  27. }
  28. // RestartProcess starts a new process passing it the active listeners. It
  29. // doesn't fork, but starts a new process using the same environment and
  30. // arguments as when it was originally started. This allows for a newly
  31. // deployed binary to be started. It returns the pid of the newly started
  32. // process when successful.
  33. func RestartProcess() (int, error) {
  34. listeners := getActiveListeners()
  35. // Extract the fds from the listeners.
  36. files := make([]*os.File, len(listeners))
  37. for i, l := range listeners {
  38. var err error
  39. // Now, all our listeners actually have File() functions so instead of
  40. // individually casting we just use a hacky interface
  41. files[i], err = l.(filer).File()
  42. if err != nil {
  43. return 0, err
  44. }
  45. if unixListener, ok := l.(*net.UnixListener); ok {
  46. unixListener.SetUnlinkOnClose(false)
  47. }
  48. // Remember to close these at the end.
  49. defer files[i].Close()
  50. }
  51. // Use the original binary location. This works with symlinks such that if
  52. // the file it points to has been changed we will use the updated symlink.
  53. argv0, err := exec.LookPath(os.Args[0])
  54. if err != nil {
  55. return 0, err
  56. }
  57. // Pass on the environment and replace the old count key with the new one.
  58. var env []string
  59. for _, v := range os.Environ() {
  60. if !strings.HasPrefix(v, listenFDs+"=") {
  61. env = append(env, v)
  62. }
  63. }
  64. env = append(env, fmt.Sprintf("%s=%d", listenFDs, len(listeners)))
  65. allFiles := append([]*os.File{os.Stdin, os.Stdout, os.Stderr}, files...)
  66. process, err := os.StartProcess(argv0, os.Args, &os.ProcAttr{
  67. Dir: originalWD,
  68. Env: env,
  69. Files: allFiles,
  70. })
  71. if err != nil {
  72. return 0, err
  73. }
  74. return process.Pid, nil
  75. }