Source file src/os/executable_procfs.go

     1  // Copyright 2016 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  //go:build linux || netbsd || (js && wasm)
     6  
     7  package os
     8  
     9  import (
    10  	"errors"
    11  	"runtime"
    12  )
    13  
    14  func executable() (string, error) {
    15  	var procfn string
    16  	switch runtime.GOOS {
    17  	default:
    18  		return "", errors.New("Executable not implemented for " + runtime.GOOS)
    19  	case "linux", "android":
    20  		procfn = "/proc/self/exe"
    21  	case "netbsd":
    22  		procfn = "/proc/curproc/exe"
    23  	}
    24  	path, err := Readlink(procfn)
    25  
    26  	// When the executable has been deleted then Readlink returns a
    27  	// path appended with " (deleted)".
    28  	return stringsTrimSuffix(path, " (deleted)"), err
    29  }
    30  
    31  // stringsTrimSuffix is the same as strings.TrimSuffix.
    32  func stringsTrimSuffix(s, suffix string) string {
    33  	if len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix {
    34  		return s[:len(s)-len(suffix)]
    35  	}
    36  	return s
    37  }
    38  

View as plain text