github.com/equinox-io/equinox@v1.2.1-0.20200723040547-60ffe7f858fe/internal/osext/osext_procfs.go (about)

     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 linux netbsd openbsd solaris 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":
    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 "openbsd", "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  }