github.com/camlistore/go4@v0.0.0-20200104003542-c7e774b10ea0/osutil/exec_solaris_amd64.go (about)

     1  // Copyright 2015 The go4 Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build amd64,solaris
    16  
    17  package osutil
    18  
    19  import (
    20  	"os"
    21  	"syscall"
    22  	"unsafe"
    23  )
    24  
    25  //go:cgo_import_dynamic libc_getexecname getexecname "libc.so"
    26  //go:linkname libc_getexecname libc_getexecname
    27  
    28  var libc_getexecname uintptr
    29  
    30  func getexecname() (path unsafe.Pointer, err error) {
    31  	r0, _, e1 := syscall.Syscall6(uintptr(unsafe.Pointer(&libc_getexecname)), 0, 0, 0, 0, 0, 0)
    32  	path = unsafe.Pointer(r0)
    33  	if e1 != 0 {
    34  		err = syscall.Errno(e1)
    35  	}
    36  	return
    37  }
    38  
    39  func syscallGetexecname() (path string, err error) {
    40  	ptr, err := getexecname()
    41  	if err != nil {
    42  		return "", err
    43  	}
    44  	bytes := (*[1 << 29]byte)(ptr)[:]
    45  	for i, b := range bytes {
    46  		if b == 0 {
    47  			return string(bytes[:i]), nil
    48  		}
    49  	}
    50  	panic("unreachable")
    51  }
    52  
    53  var initCwd, initCwdErr = os.Getwd()
    54  
    55  func executable() (string, error) {
    56  	path, err := syscallGetexecname()
    57  	if err != nil {
    58  		return path, err
    59  	}
    60  	if len(path) > 0 && path[0] != '/' {
    61  		if initCwdErr != nil {
    62  			return path, initCwdErr
    63  		}
    64  		if len(path) > 2 && path[0:2] == "./" {
    65  			// skip "./"
    66  			path = path[2:]
    67  		}
    68  		return initCwd + "/" + path, nil
    69  	}
    70  	return path, nil
    71  }