gproc.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gproc implements management and communication for processes.
  7. package gproc
  8. import (
  9. "os"
  10. "runtime"
  11. "time"
  12. "github.com/gogf/gf/v2/os/genv"
  13. "github.com/gogf/gf/v2/os/gfile"
  14. "github.com/gogf/gf/v2/text/gstr"
  15. "github.com/gogf/gf/v2/util/gconv"
  16. )
  17. const (
  18. envKeyPPid = "GPROC_PPID"
  19. tracingInstrumentName = "github.com/gogf/gf/v2/os/gproc.Process"
  20. )
  21. var (
  22. processPid = os.Getpid() // processPid is the pid of current process.
  23. processStartTime = time.Now() // processStartTime is the start time of current process.
  24. )
  25. // Pid returns the pid of current process.
  26. func Pid() int {
  27. return processPid
  28. }
  29. // PPid returns the custom parent pid if exists, or else it returns the system parent pid.
  30. func PPid() int {
  31. if !IsChild() {
  32. return Pid()
  33. }
  34. ppidValue := os.Getenv(envKeyPPid)
  35. if ppidValue != "" && ppidValue != "0" {
  36. return gconv.Int(ppidValue)
  37. }
  38. return PPidOS()
  39. }
  40. // PPidOS returns the system parent pid of current process.
  41. // Note that the difference between PPidOS and PPid function is that the PPidOS returns
  42. // the system ppid, but the PPid functions may return the custom pid by gproc if the custom
  43. // ppid exists.
  44. func PPidOS() int {
  45. return os.Getppid()
  46. }
  47. // IsChild checks and returns whether current process is a child process.
  48. // A child process is forked by another gproc process.
  49. func IsChild() bool {
  50. ppidValue := os.Getenv(envKeyPPid)
  51. return ppidValue != "" && ppidValue != "0"
  52. }
  53. // SetPPid sets custom parent pid for current process.
  54. func SetPPid(ppid int) error {
  55. if ppid > 0 {
  56. return os.Setenv(envKeyPPid, gconv.String(ppid))
  57. } else {
  58. return os.Unsetenv(envKeyPPid)
  59. }
  60. }
  61. // StartTime returns the start time of current process.
  62. func StartTime() time.Time {
  63. return processStartTime
  64. }
  65. // Uptime returns the duration which current process has been running
  66. func Uptime() time.Duration {
  67. return time.Since(processStartTime)
  68. }
  69. // SearchBinary searches the binary `file` in current working folder and PATH environment.
  70. func SearchBinary(file string) string {
  71. // Check if it is absolute path of exists at current working directory.
  72. if gfile.Exists(file) {
  73. return file
  74. }
  75. return SearchBinaryPath(file)
  76. }
  77. // SearchBinaryPath searches the binary `file` in PATH environment.
  78. func SearchBinaryPath(file string) string {
  79. array := ([]string)(nil)
  80. switch runtime.GOOS {
  81. case "windows":
  82. envPath := genv.Get("PATH", genv.Get("Path")).String()
  83. if gstr.Contains(envPath, ";") {
  84. array = gstr.SplitAndTrim(envPath, ";")
  85. } else if gstr.Contains(envPath, ":") {
  86. array = gstr.SplitAndTrim(envPath, ":")
  87. }
  88. if gfile.Ext(file) != ".exe" {
  89. file += ".exe"
  90. }
  91. default:
  92. array = gstr.SplitAndTrim(genv.Get("PATH").String(), ":")
  93. }
  94. if len(array) > 0 {
  95. path := ""
  96. for _, v := range array {
  97. path = v + gfile.Separator + file
  98. if gfile.Exists(path) && gfile.IsFile(path) {
  99. return path
  100. }
  101. }
  102. }
  103. return ""
  104. }