summaryrefslogtreecommitdiffstats
path: root/Modules/grpmodule.c
diff options
context:
space:
mode:
authorWilliam Grzybowski <wg@FreeBSD.org>2018-09-07 12:06:15 (GMT)
committerVictor Stinner <vstinner@redhat.com>2018-09-07 12:06:15 (GMT)
commit23e65b25557f957af840cf8fe68e80659ce28629 (patch)
tree2401a515712164f1536892efd8e5f7fb1c0b87d8 /Modules/grpmodule.c
parent25fa141487e61b94f15289619cb3af764cf65e58 (diff)
downloadcpython-23e65b25557f957af840cf8fe68e80659ce28629.zip
cpython-23e65b25557f957af840cf8fe68e80659ce28629.tar.gz
cpython-23e65b25557f957af840cf8fe68e80659ce28629.tar.bz2
bpo-33625: Release GIL for grp.getgr{nam,gid} and pwd.getpw{nam,uid} (GH-7081)
Release GIL on grp.getgrnam(), grp.getgrgid(), pwd.getpwnam() and pwd.getpwuid() if reentrant variants of these functions are available. Patch by William Grzybowski.
Diffstat (limited to 'Modules/grpmodule.c')
-rw-r--r--Modules/grpmodule.c103
1 files changed, 97 insertions, 6 deletions
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index f577fd3..ffdfa1b 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -37,6 +37,8 @@ static PyStructSequence_Desc struct_group_type_desc = {
static int initialized;
static PyTypeObject StructGrpType;
+#define DEFAULT_BUFFER_SIZE 1024
+
static PyObject *
mkgrent(struct group *p)
{
@@ -96,7 +98,9 @@ static PyObject *
grp_getgrgid_impl(PyObject *module, PyObject *id)
/*[clinic end generated code: output=30797c289504a1ba input=15fa0e2ccf5cda25]*/
{
- PyObject *py_int_id;
+ PyObject *py_int_id, *retval = NULL;
+ int nomem = 0;
+ char *buf = NULL, *buf2 = NULL;
gid_t gid;
struct group *p;
@@ -119,8 +123,48 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
}
Py_DECREF(py_int_id);
}
+#ifdef HAVE_GETGRGID_R
+ Py_BEGIN_ALLOW_THREADS
+ int status;
+ Py_ssize_t bufsize;
+ struct group grp;
+
+ bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (bufsize == -1) {
+ bufsize = DEFAULT_BUFFER_SIZE;
+ }
+
+ while (1) {
+ buf2 = PyMem_RawRealloc(buf, bufsize);
+ if (buf2 == NULL) {
+ p = NULL;
+ nomem = 1;
+ break;
+ }
+ buf = buf2;
+ status = getgrgid_r(gid, &grp, buf, bufsize, &p);
+ if (status != 0) {
+ p = NULL;
+ }
+ if (p != NULL || status != ERANGE) {
+ break;
+ }
+ if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
+ nomem = 1;
+ break;
+ }
+ bufsize <<= 1;
+ }
- if ((p = getgrgid(gid)) == NULL) {
+ Py_END_ALLOW_THREADS
+#else
+ p = getgrgid(gid);
+#endif
+ if (p == NULL) {
+ PyMem_RawFree(buf);
+ if (nomem == 1) {
+ return PyErr_NoMemory();
+ }
PyObject *gid_obj = _PyLong_FromGid(gid);
if (gid_obj == NULL)
return NULL;
@@ -128,7 +172,11 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
Py_DECREF(gid_obj);
return NULL;
}
- return mkgrent(p);
+ retval = mkgrent(p);
+#ifdef HAVE_GETGRGID_R
+ PyMem_RawFree(buf);
+#endif
+ return retval;
}
/*[clinic input]
@@ -145,7 +193,8 @@ static PyObject *
grp_getgrnam_impl(PyObject *module, PyObject *name)
/*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/
{
- char *name_chars;
+ char *buf = NULL, *buf2 = NULL, *name_chars;
+ int nomem = 0;
struct group *p;
PyObject *bytes, *retval = NULL;
@@ -154,13 +203,55 @@ grp_getgrnam_impl(PyObject *module, PyObject *name)
/* check for embedded null bytes */
if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
goto out;
+#ifdef HAVE_GETGRNAM_R
+ Py_BEGIN_ALLOW_THREADS
+ int status;
+ Py_ssize_t bufsize;
+ struct group grp;
+
+ bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (bufsize == -1) {
+ bufsize = DEFAULT_BUFFER_SIZE;
+ }
+
+ while(1) {
+ buf2 = PyMem_RawRealloc(buf, bufsize);
+ if (buf2 == NULL) {
+ p = NULL;
+ nomem = 1;
+ break;
+ }
+ buf = buf2;
+ status = getgrnam_r(name_chars, &grp, buf, bufsize, &p);
+ if (status != 0) {
+ p = NULL;
+ }
+ if (p != NULL || status != ERANGE) {
+ break;
+ }
+ if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
+ nomem = 1;
+ break;
+ }
+ bufsize <<= 1;
+ }
- if ((p = getgrnam(name_chars)) == NULL) {
- PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_chars);
+ Py_END_ALLOW_THREADS
+#else
+ p = getgrnam(name_chars);
+#endif
+ if (p == NULL) {
+ if (nomem == 1) {
+ PyErr_NoMemory();
+ }
+ else {
+ PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_chars);
+ }
goto out;
}
retval = mkgrent(p);
out:
+ PyMem_RawFree(buf);
Py_DECREF(bytes);
return retval;
}