mirror of
https://github.com/python/cpython.git
synced 2025-11-25 21:11:09 +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
|
|
@ -50,6 +50,8 @@ exception is raised if the entry asked for cannot be found.");
|
|||
static int initialized;
|
||||
static PyTypeObject StructPwdType;
|
||||
|
||||
#define DEFAULT_BUFFER_SIZE 1024
|
||||
|
||||
static void
|
||||
sets(PyObject *v, int i, const char* val)
|
||||
{
|
||||
|
|
@ -116,8 +118,11 @@ static PyObject *
|
|||
pwd_getpwuid(PyObject *module, PyObject *uidobj)
|
||||
/*[clinic end generated code: output=c4ee1d4d429b86c4 input=ae64d507a1c6d3e8]*/
|
||||
{
|
||||
PyObject *retval = NULL;
|
||||
uid_t uid;
|
||||
int nomem = 0;
|
||||
struct passwd *p;
|
||||
char *buf = NULL, *buf2 = NULL;
|
||||
|
||||
if (!_Py_Uid_Converter(uidobj, &uid)) {
|
||||
if (PyErr_ExceptionMatches(PyExc_OverflowError))
|
||||
|
|
@ -125,7 +130,47 @@ pwd_getpwuid(PyObject *module, PyObject *uidobj)
|
|||
"getpwuid(): uid not found");
|
||||
return NULL;
|
||||
}
|
||||
if ((p = getpwuid(uid)) == NULL) {
|
||||
#ifdef HAVE_GETPWUID_R
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
int status;
|
||||
Py_ssize_t bufsize;
|
||||
struct passwd pwd;
|
||||
|
||||
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
|
||||
if (bufsize == -1) {
|
||||
bufsize = DEFAULT_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
buf2 = PyMem_RawRealloc(buf, bufsize);
|
||||
if (buf2 == NULL) {
|
||||
nomem = 1;
|
||||
break;
|
||||
}
|
||||
buf = buf2;
|
||||
status = getpwuid_r(uid, &pwd, 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 = getpwuid(uid);
|
||||
#endif
|
||||
if (p == NULL) {
|
||||
PyMem_RawFree(buf);
|
||||
if (nomem == 1) {
|
||||
return PyErr_NoMemory();
|
||||
}
|
||||
PyObject *uid_obj = _PyLong_FromUid(uid);
|
||||
if (uid_obj == NULL)
|
||||
return NULL;
|
||||
|
|
@ -134,7 +179,11 @@ pwd_getpwuid(PyObject *module, PyObject *uidobj)
|
|||
Py_DECREF(uid_obj);
|
||||
return NULL;
|
||||
}
|
||||
return mkpwent(p);
|
||||
retval = mkpwent(p);
|
||||
#ifdef HAVE_GETPWUID_R
|
||||
PyMem_RawFree(buf);
|
||||
#endif
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
|
|
@ -152,7 +201,8 @@ static PyObject *
|
|||
pwd_getpwnam_impl(PyObject *module, PyObject *arg)
|
||||
/*[clinic end generated code: output=6abeee92430e43d2 input=d5f7e700919b02d3]*/
|
||||
{
|
||||
char *name;
|
||||
char *buf = NULL, *buf2 = NULL, *name;
|
||||
int nomem = 0;
|
||||
struct passwd *p;
|
||||
PyObject *bytes, *retval = NULL;
|
||||
|
||||
|
|
@ -161,13 +211,55 @@ pwd_getpwnam_impl(PyObject *module, PyObject *arg)
|
|||
/* check for embedded null bytes */
|
||||
if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
|
||||
goto out;
|
||||
if ((p = getpwnam(name)) == NULL) {
|
||||
PyErr_Format(PyExc_KeyError,
|
||||
"getpwnam(): name not found: %s", name);
|
||||
#ifdef HAVE_GETPWNAM_R
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
int status;
|
||||
Py_ssize_t bufsize;
|
||||
struct passwd pwd;
|
||||
|
||||
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
|
||||
if (bufsize == -1) {
|
||||
bufsize = DEFAULT_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
buf2 = PyMem_RawRealloc(buf, bufsize);
|
||||
if (buf2 == NULL) {
|
||||
nomem = 1;
|
||||
break;
|
||||
}
|
||||
buf = buf2;
|
||||
status = getpwnam_r(name, &pwd, 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 = getpwnam(name);
|
||||
#endif
|
||||
if (p == NULL) {
|
||||
if (nomem == 1) {
|
||||
PyErr_NoMemory();
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_KeyError,
|
||||
"getpwnam(): name not found: %s", name);
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
retval = mkpwent(p);
|
||||
out:
|
||||
PyMem_RawFree(buf);
|
||||
Py_DECREF(bytes);
|
||||
return retval;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue