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.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // This code is heavily inspired by the archived gofacebook/gracenet/net.go handler
  5. //go:build !windows
  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 func(i int) {
  50. _ = files[i].Close()
  51. }(i)
  52. }
  53. // Use the original binary location. This works with symlinks such that if
  54. // the file it points to has been changed we will use the updated symlink.
  55. argv0, err := exec.LookPath(os.Args[0])
  56. if err != nil {
  57. return 0, err
  58. }
  59. // Pass on the environment and replace the old count key with the new one.
  60. var env []string
  61. for _, v := range os.Environ() {
  62. if !strings.HasPrefix(v, listenFDs+"=") {
  63. env = append(env, v)
  64. }
  65. }
  66. env = append(env, fmt.Sprintf("%s=%d", listenFDs, len(listeners)))
  67. allFiles := append([]*os.File{os.Stdin, os.Stdout, os.Stderr}, files...)
  68. process, err := os.StartProcess(argv0, os.Args, &os.ProcAttr{
  69. Dir: originalWD,
  70. Env: env,
  71. Files: allFiles,
  72. })
  73. if err != nil {
  74. return 0, err
  75. }
  76. return process.Pid, nil
  77. }