github.com/wangyougui/gf/v2@v2.6.5/os/gproc/gproc.go (about)

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