From 1ebf0b95c46c688b300f75a9a4a6887d7ea9c7c6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 21 Dec 2017 12:30:34 +0530 Subject: [PATCH] Implement reading of cwd of a process on macOS as well --- kitty/child.py | 11 ++++++++--- kitty/cocoa_window.m | 13 +++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/kitty/child.py b/kitty/child.py index 31a6aad1d..8d9df44a4 100644 --- a/kitty/child.py +++ b/kitty/child.py @@ -13,8 +13,11 @@ from .constants import terminfo_dir, is_macos def cwd_of_process(pid): if is_macos: - raise NotImplementedError('getting cwd of child processes not implemented') - return os.readlink('/proc/{}/cwd'.format(pid)) + from kitty.fast_data_types import cwd_of_process + ans = cwd_of_process(pid) + else: + ans = '/proc/{}/cwd'.format(pid) + return os.path.realpath(ans) def remove_cloexec(fd): @@ -34,7 +37,9 @@ class Child: except Exception: import traceback 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.stdin = stdin self.env = env or {} diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index 1c3d19a31..621c204b3 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -12,6 +12,8 @@ #include // Needed for _NSGetProgname #include +typedef void* rusage_info_t; // needed for libproc.h +#include #if (MAC_OS_X_VERSION_MAX_ALLOWED < 101200) #define NSWindowStyleMaskResizable NSResizableWindowMask @@ -166,8 +168,19 @@ cocoa_get_lang(PyObject UNUSED *self) { 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[] = { {"cocoa_get_lang", (PyCFunction)cocoa_get_lang, METH_NOARGS, ""}, + {"cwd_of_process", (PyCFunction)cwd_of_process, METH_O, ""}, {NULL, NULL, 0, NULL} /* Sentinel */ };