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

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