mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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.
This commit is contained in:
parent
25fa141487
commit
23e65b2555
6 changed files with 228 additions and 15 deletions
|
@ -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;
|
||||
|
||||
if ((p = getgrgid(gid)) == NULL) {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if ((p = getgrnam(name_chars)) == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_chars);
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue