osext_procfs.go 910 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build linux netbsd openbsd solaris dragonfly
  5. package osext
  6. import (
  7. "errors"
  8. "fmt"
  9. "os"
  10. "runtime"
  11. "strings"
  12. )
  13. func executable() (string, error) {
  14. switch runtime.GOOS {
  15. case "linux":
  16. const deletedTag = " (deleted)"
  17. execpath, err := os.Readlink("/proc/self/exe")
  18. if err != nil {
  19. return execpath, err
  20. }
  21. execpath = strings.TrimSuffix(execpath, deletedTag)
  22. execpath = strings.TrimPrefix(execpath, deletedTag)
  23. return execpath, nil
  24. case "netbsd":
  25. return os.Readlink("/proc/curproc/exe")
  26. case "openbsd", "dragonfly":
  27. return os.Readlink("/proc/curproc/file")
  28. case "solaris":
  29. return os.Readlink(fmt.Sprintf("/proc/%d/path/a.out", os.Getpid()))
  30. }
  31. return "", errors.New("ExecPath not implemented for " + runtime.GOOS)
  32. }