executable_procfs.mx raw

   1  // The following is copied from Go 1.17 official implementation.
   2  
   3  // Copyright 2016 The Go Authors. All rights reserved.
   4  // Use of this source code is governed by a BSD-style
   5  // license that can be found in the LICENSE file.
   6  
   7  //go:build linux && !baremetal
   8  
   9  package os
  10  
  11  func Executable() (string, error) {
  12  	path, err := Readlink("/proc/self/exe")
  13  
  14  	// When the executable has been deleted then Readlink returns a
  15  	// path appended with " (deleted)".
  16  	return stringsTrimSuffix(path, " (deleted)"), err
  17  }
  18  
  19  // stringsTrimSuffix is the same as strings.TrimSuffix.
  20  func stringsTrimSuffix(s, suffix string) string {
  21  	if len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix {
  22  		return s[:len(s)-len(suffix)]
  23  	}
  24  	return s
  25  }
  26