Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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