Skip to content

bpo-46021: fcntl module update supporting FreeBSD's F_KINFO proposal. #30000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Modules/fcntlmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include <stropts.h>
#endif

#ifdef __FreeBSD__
#include <sys/user.h>
#endif
Comment on lines +18 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs an AC_CHECK_HEADERS check in configure.ac


/*[clinic input]
module fcntl
[clinic start generated code]*/
Expand Down Expand Up @@ -60,6 +64,13 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)

if (arg != NULL) {
int parse_result;
#ifdef F_KINFO
if (code == F_KINFO) {
PyErr_SetString(PyExc_ValueError,
"fnctl arg not permitted with F_KINFO");
return NULL;
}
#endif

if (PyArg_Parse(arg, "s#", &str, &len)) {
if ((size_t)len > sizeof buf) {
Expand Down Expand Up @@ -89,6 +100,33 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
}
}

#ifdef F_KINFO
if (code == F_KINFO) {
struct kinfo_file f = {.kf_structsize = KINFO_FILE_SIZE};
do {
Py_BEGIN_ALLOW_THREADS
ret = fcntl(fd, code, &f);
Py_END_ALLOW_THREADS
} while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
if (ret < 0) {
return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
}

PyObject *ret = PyDict_New();
if (ret) {
PyDict_SetItemString(ret, "status", PyLong_FromLong(f.kf_status));
PyDict_SetItemString(ret, "type", PyLong_FromLong(f.kf_type));
PyDict_SetItemString(ret, "offset", PyLong_FromLongLong(f.kf_offset));
PyDict_SetItemString(ret, "path", PyBytes_FromString(f.kf_path));
return ret;
Comment on lines +116 to +121
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fcntl.fcntl() returns either an integer or bytes. If F_KINFO needs a different return type, then it would be better to implement the feature as a new platform-specific function in the fcntl module, e.g. fcntl.kinfo(fd: int) -> dict.

} else {
PyErr_SetString(PyExc_ValueError,
"fcntl could not create F_KINFO data");
return NULL;
}
}
#endif

do {
Py_BEGIN_ALLOW_THREADS
ret = fcntl(fd, code, (int)int_arg);
Expand Down Expand Up @@ -588,6 +626,9 @@ all_ins(PyObject* m)
#ifdef F_DUP2FD_CLOEXEC
if (PyModule_AddIntMacro(m, F_DUP2FD_CLOEXEC)) return -1;
#endif
#ifdef F_KINFO
if (PyModule_AddIntMacro(m, F_KINFO)) return -1;
#endif

/* For F_{GET|SET}FL */
#ifdef FD_CLOEXEC
Expand Down