osext_procfs.go raw

   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  
   5  // +build !go1.8,android !go1.8,linux !go1.8,netbsd !go1.8,solaris !go1.8,dragonfly
   6  
   7  package osext
   8  
   9  import (
  10  	"errors"
  11  	"fmt"
  12  	"os"
  13  	"runtime"
  14  	"strings"
  15  )
  16  
  17  func executable() (string, error) {
  18  	switch runtime.GOOS {
  19  	case "linux", "android":
  20  		const deletedTag = " (deleted)"
  21  		execpath, err := os.Readlink("/proc/self/exe")
  22  		if err != nil {
  23  			return execpath, err
  24  		}
  25  		execpath = strings.TrimSuffix(execpath, deletedTag)
  26  		execpath = strings.TrimPrefix(execpath, deletedTag)
  27  		return execpath, nil
  28  	case "netbsd":
  29  		return os.Readlink("/proc/curproc/exe")
  30  	case "dragonfly":
  31  		return os.Readlink("/proc/curproc/file")
  32  	case "solaris":
  33  		return os.Readlink(fmt.Sprintf("/proc/%d/path/a.out", os.Getpid()))
  34  	}
  35  	return "", errors.New("ExecPath not implemented for " + runtime.GOOS)
  36  }
  37