daemon.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package daemon
  2. import (
  3. "errors"
  4. "os"
  5. )
  6. var errNotSupported = errors.New("daemon: Non-POSIX OS is not supported")
  7. // Mark of daemon process - system environment variable _GO_DAEMON=1
  8. const (
  9. MARK_NAME = "_GO_DAEMON"
  10. MARK_VALUE = "1"
  11. )
  12. // Default file permissions for log and pid files.
  13. const FILE_PERM = os.FileMode(0640)
  14. // WasReborn returns true in child process (daemon) and false in parent process.
  15. func WasReborn() bool {
  16. return os.Getenv(MARK_NAME) == MARK_VALUE
  17. }
  18. // Reborn runs second copy of current process in the given context.
  19. // function executes separate parts of code in child process and parent process
  20. // and provides demonization of child process. It look similar as the
  21. // fork-daemonization, but goroutine-safe.
  22. // In success returns *os.Process in parent process and nil in child process.
  23. // Otherwise returns error.
  24. func (d *Context) Reborn() (child *os.Process, err error) {
  25. return d.reborn()
  26. }
  27. // Search searches daemons process by given in context pid file name.
  28. // If success returns pointer on daemons os.Process structure,
  29. // else returns error. Returns nil if filename is empty.
  30. func (d *Context) Search() (daemon *os.Process, err error) {
  31. return d.search()
  32. }
  33. // Release provides correct pid-file release in daemon.
  34. func (d *Context) Release() (err error) {
  35. return d.release()
  36. }