How can I get the the path of a process on macOS? ...
# kotlin-native
a
How can I get the the path of a process on macOS? On Linux I can use
/proc
and `platform.posix.realpath`:
Copy code
import kotlinx.cinterop.refTo
import kotlinx.cinterop.toKString
import platform.posix.PATH_MAX
import platform.posix.realpath

actual fun processPath(pid: UInt): String? {
  val buffer = ByteArray(PATH_MAX)
  val procPath = "/proc/$pid/exe"
  if (realpath(procPath, buffer.refTo(0)) == null) {
    return null
  }
  return buffer.toKString()
}
But iiuc
/proc
doesn't exist on macOS. Instead there's
proc_pidpath()
example, but K/N doesn't have a generated function for that. Is there another way?
Never mind! Kotlin does have a binding for
proc_pidpath()
. IntelliJ sync wasn't working properly, so autocomplete wasn't working. I cleaned out all of the
build/
,
.kotlin/
and
.gradle/
dirs and resynced, and it works now:
Copy code
import kotlinx.cinterop.refTo
import kotlinx.cinterop.toKString
import platform.osx.proc_pidpath
import platform.posix.PATH_MAX


actual fun processPath(pid: UInt): String? {
  val buffer = ByteArray(PATH_MAX)
  val ret = proc_pidpath(pid = pid.toInt(), buffer.refTo(0), PATH_MAX.toUInt())
  return if (ret <= 0) {
    null
  } else {
    buffer.toKString()
  }
}
🙌 1