Implement reading of cwd of a process on macOS as well

This commit is contained in:
Kovid Goyal 2017-12-21 12:30:34 +05:30
parent c5acd94456
commit 1ebf0b95c4
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 21 additions and 3 deletions

View File

@ -13,8 +13,11 @@ from .constants import terminfo_dir, is_macos
def cwd_of_process(pid): def cwd_of_process(pid):
if is_macos: if is_macos:
raise NotImplementedError('getting cwd of child processes not implemented') from kitty.fast_data_types import cwd_of_process
return os.readlink('/proc/{}/cwd'.format(pid)) ans = cwd_of_process(pid)
else:
ans = '/proc/{}/cwd'.format(pid)
return os.path.realpath(ans)
def remove_cloexec(fd): def remove_cloexec(fd):
@ -34,7 +37,9 @@ class Child:
except Exception: except Exception:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
self.cwd = os.path.abspath(os.path.expandvars(os.path.expanduser(cwd or os.getcwd()))) else:
cwd = os.path.expandvars(os.path.expanduser(cwd or os.getcwd()))
self.cwd = os.path.abspath(cwd)
self.opts = opts self.opts = opts
self.stdin = stdin self.stdin = stdin
self.env = env or {} self.env = env or {}

View File

@ -12,6 +12,8 @@
#include <AvailabilityMacros.h> #include <AvailabilityMacros.h>
// Needed for _NSGetProgname // Needed for _NSGetProgname
#include <crt_externs.h> #include <crt_externs.h>
typedef void* rusage_info_t; // needed for libproc.h
#include <libproc.h>
#if (MAC_OS_X_VERSION_MAX_ALLOWED < 101200) #if (MAC_OS_X_VERSION_MAX_ALLOWED < 101200)
#define NSWindowStyleMaskResizable NSResizableWindowMask #define NSWindowStyleMaskResizable NSResizableWindowMask
@ -166,8 +168,19 @@ cocoa_get_lang(PyObject UNUSED *self) {
return Py_BuildValue("s", [locale UTF8String]); return Py_BuildValue("s", [locale UTF8String]);
} }
PyObject*
cwd_of_process(PyObject *self UNUSED, PyObject *pid_) {
long pid = PyLong_AsLong(pid_);
struct proc_vnodepathinfo vpi;
int ret = proc_pidinfo(pid, PROC_PIDVNODEPATHINFO, 0, &vpi, sizeof(vpi));
if (ret < 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
return PyUnicode_FromString(vpi.pvi_cdir.vip_path);
}
static PyMethodDef module_methods[] = { static PyMethodDef module_methods[] = {
{"cocoa_get_lang", (PyCFunction)cocoa_get_lang, METH_NOARGS, ""}, {"cocoa_get_lang", (PyCFunction)cocoa_get_lang, METH_NOARGS, ""},
{"cwd_of_process", (PyCFunction)cwd_of_process, METH_O, ""},
{NULL, NULL, 0, NULL} /* Sentinel */ {NULL, NULL, 0, NULL} /* Sentinel */
}; };