github.com/btwiuse/jiri@v0.0.0-20191125065820-53353bcfef54/osutil/exec_procfs.go (about)

     1  // Copyright 2016 The Fuchsia 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
     6  
     7  package osutil
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  	"runtime"
    13  	"strings"
    14  )
    15  
    16  // Executable returns an absolute path to the currently executing program.
    17  func Executable() (string, error) {
    18  	var path string
    19  	var err error
    20  	switch runtime.GOOS {
    21  	case "linux":
    22  		const deletedTag = " (deleted)"
    23  		path, err = os.Readlink("/proc/self/exe")
    24  		if err != nil {
    25  			return "", err
    26  		}
    27  		path = strings.TrimSuffix(path, deletedTag)
    28  		path = strings.TrimPrefix(path, deletedTag)
    29  	case "netbsd":
    30  		path, err = os.Readlink("/proc/curproc/exe")
    31  	case "openbsd":
    32  		path, err = os.Readlink("/proc/curproc/file")
    33  	}
    34  	return filepath.Clean(path), err
    35  }