github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/sysutils/pid_path_darwin.go (about)

     1  // Copyright 2017 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package sysutils
     6  
     7  import (
     8  	"bytes"
     9  	"syscall"
    10  	"unsafe"
    11  )
    12  
    13  const (
    14  	procpidpathinfo     = 11
    15  	procpidpathinfosize = 1024
    16  	proccallnumpidinfo  = 2
    17  )
    18  
    19  // GetExecPathFromPID returns the process's executable path for given PID.
    20  func GetExecPathFromPID(pid uint32) (string, error) {
    21  	buf := make([]byte, procpidpathinfosize)
    22  	_, _, errno := syscall.Syscall6(
    23  		syscall.SYS_PROC_INFO,
    24  		proccallnumpidinfo,
    25  		uintptr(pid),
    26  		procpidpathinfo,
    27  		0,
    28  		uintptr(unsafe.Pointer(&buf[0])),
    29  		procpidpathinfosize)
    30  	if errno != 0 {
    31  		return "", errno
    32  	}
    33  	firstZero := bytes.IndexByte(buf, 0)
    34  	if firstZero <= 0 {
    35  		return "", nil
    36  	}
    37  	return string(buf[:firstZero]), nil
    38  }