blob: 17f5a3ddecf5baa69081d044b795a9b9e1e4d692 [file] [log] [blame]
// Copyright 2016 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build linux || netbsd || openbsd
// +build linux netbsd openbsd
package osutil
import (
"os"
"path/filepath"
"runtime"
"strings"
)
// Executable returns an absolute path to the currently executing program.
func Executable() (string, error) {
var path string
var err error
switch runtime.GOOS {
case "linux":
const deletedTag = " (deleted)"
path, err = os.Readlink("/proc/self/exe")
if err != nil {
return "", err
}
path = strings.TrimSuffix(path, deletedTag)
path = strings.TrimPrefix(path, deletedTag)
case "netbsd":
path, err = os.Readlink("/proc/curproc/exe")
case "openbsd":
path, err = os.Readlink("/proc/curproc/file")
}
return filepath.Clean(path), err
}