github.com/keysonzzz/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgSys/GetCurrentExecutePath_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 kmgSys
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"os"
    13  	"runtime"
    14  	"strings"
    15  )
    16  
    17  func getCurrentExecutePath() (string, error) {
    18  	switch runtime.GOOS {
    19  	case "linux":
    20  		const deletedSuffix = " (deleted)"
    21  		execpath, err := os.Readlink("/proc/self/exe")
    22  		if err != nil {
    23  			return execpath, err
    24  		}
    25  		return strings.TrimSuffix(execpath, deletedSuffix), nil
    26  	case "netbsd":
    27  		return os.Readlink("/proc/curproc/exe")
    28  	case "openbsd", "dragonfly":
    29  		return os.Readlink("/proc/curproc/file")
    30  	case "solaris":
    31  		return os.Readlink(fmt.Sprintf("/proc/%d/path/a.out", os.Getpid()))
    32  	}
    33  	return "", errors.New("ExecPath not implemented for " + runtime.GOOS)
    34  }