From 76531d2f7ac448103cb5d00b47c8a791656a9139 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 5 Jun 2022 15:36:05 +0530 Subject: [PATCH] Python wrapper function to establish controlling tty --- kitty/child.c | 19 +++++++++++++++++++ kitty/fast_data_types.pyi | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/kitty/child.c b/kitty/child.c index 267c4361d..7d1c6e4a1 100644 --- a/kitty/child.c +++ b/kitty/child.c @@ -180,8 +180,27 @@ spawn(PyObject *self UNUSED, PyObject *args) { return PyLong_FromLong(pid); } +static PyObject* +establish_controlling_tty(PyObject *self UNUSED, PyObject *args) { + const char *ttyname; + int stdin_fd, stdout_fd, stderr_fd; + if (!PyArg_ParseTuple(args, "siii", &ttyname, &stdin_fd, &stdout_fd, &stderr_fd)) return NULL; + int tfd = safe_open(ttyname, O_RDWR, 0); + if (tfd == -1) return PyErr_SetFromErrnoWithFilename(PyExc_OSError, ttyname); +#ifdef TIOCSCTTY + // On BSD open() does not establish the controlling terminal + if (ioctl(tfd, TIOCSCTTY, 0) == -1) return PyErr_SetFromErrno(PyExc_OSError); +#endif + if (dup2(tfd, stdin_fd) == -1) return PyErr_SetFromErrno(PyExc_OSError); + if (dup2(tfd, stdout_fd) == -1) return PyErr_SetFromErrno(PyExc_OSError); + if (dup2(tfd, stderr_fd) == -1) return PyErr_SetFromErrno(PyExc_OSError); + safe_close(tfd, __FILE__, __LINE__); + Py_RETURN_NONE; +} + static PyMethodDef module_methods[] = { METHODB(spawn, METH_VARARGS), + METHODB(establish_controlling_tty, METH_VARARGS), {NULL, NULL, 0, NULL} /* Sentinel */ }; diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index c1126dc08..ef7c9e126 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -1379,3 +1379,7 @@ def shm_unlink(name: str) -> None: def sigqueue(pid: int, signal: int, value: int) -> None: pass + + +def establish_controlling_tty(ttyname: str, stdin: int, stdout: int, stderr: int) -> None: + pass