diff options
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r-- | Modules/posixmodule.c | 331 |
1 files changed, 206 insertions, 125 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 776c7c6fb6..a06ebd10a3 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -27,6 +27,9 @@ #include "Python.h" #include "structseq.h" +#ifndef MS_WINDOWS +#include "posixmodule.h" +#endif #if defined(__VMS) # include <unixio.h> @@ -347,6 +350,134 @@ extern int lstat(const char *, struct stat *); #endif #endif + +#ifndef MS_WINDOWS +PyObject * +_PyInt_FromUid(uid_t uid) +{ + if (uid <= LONG_MAX) + return PyInt_FromLong(uid); + return PyLong_FromUnsignedLong(uid); +} + +PyObject * +_PyInt_FromGid(gid_t gid) +{ + if (gid <= LONG_MAX) + return PyInt_FromLong(gid); + return PyLong_FromUnsignedLong(gid); +} + +int +_Py_Uid_Converter(PyObject *obj, void *p) +{ + int overflow; + long result; + if (PyFloat_Check(obj)) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float"); + return 0; + } + result = PyLong_AsLongAndOverflow(obj, &overflow); + if (overflow < 0) + goto OverflowDown; + if (!overflow && result == -1) { + /* error or -1 */ + if (PyErr_Occurred()) + return 0; + *(uid_t *)p = (uid_t)-1; + } + else { + /* unsigned uid_t */ + unsigned long uresult; + if (overflow > 0) { + uresult = PyLong_AsUnsignedLong(obj); + if (PyErr_Occurred()) { + if (PyErr_ExceptionMatches(PyExc_OverflowError)) + goto OverflowUp; + return 0; + } + if ((uid_t)uresult == (uid_t)-1) + goto OverflowUp; + } else { + if (result < 0) + goto OverflowDown; + uresult = result; + } + if (sizeof(uid_t) < sizeof(long) && + (unsigned long)(uid_t)uresult != uresult) + goto OverflowUp; + *(uid_t *)p = (uid_t)uresult; + } + return 1; + +OverflowDown: + PyErr_SetString(PyExc_OverflowError, + "user id is less than minimum"); + return 0; + +OverflowUp: + PyErr_SetString(PyExc_OverflowError, + "user id is greater than maximum"); + return 0; +} + +int +_Py_Gid_Converter(PyObject *obj, void *p) +{ + int overflow; + long result; + if (PyFloat_Check(obj)) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float"); + return 0; + } + result = PyLong_AsLongAndOverflow(obj, &overflow); + if (overflow < 0) + goto OverflowDown; + if (!overflow && result == -1) { + /* error or -1 */ + if (PyErr_Occurred()) + return 0; + *(gid_t *)p = (gid_t)-1; + } + else { + /* unsigned gid_t */ + unsigned long uresult; + if (overflow > 0) { + uresult = PyLong_AsUnsignedLong(obj); + if (PyErr_Occurred()) { + if (PyErr_ExceptionMatches(PyExc_OverflowError)) + goto OverflowUp; + return 0; + } + if ((gid_t)uresult == (gid_t)-1) + goto OverflowUp; + } else { + if (result < 0) + goto OverflowDown; + uresult = result; + } + if (sizeof(gid_t) < sizeof(long) && + (unsigned long)(gid_t)uresult != uresult) + goto OverflowUp; + *(gid_t *)p = (gid_t)uresult; + } + return 1; + +OverflowDown: + PyErr_SetString(PyExc_OverflowError, + "group id is less than minimum"); + return 0; + +OverflowUp: + PyErr_SetString(PyExc_OverflowError, + "group id is greater than maximum"); + return 0; +} +#endif /* MS_WINDOWS */ + + #if defined _MSC_VER && _MSC_VER >= 1400 /* Microsoft CRT in VS2005 and higher will verify that a filehandle is * valid and raise an assertion if it isn't. @@ -1306,8 +1437,13 @@ _pystat_fromstructstat(STRUCT_STAT *st) PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); #endif PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); - PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); - PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); +#if defined(MS_WINDOWS) + PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong(0)); + PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong(0)); +#else + PyStructSequence_SET_ITEM(v, 4, _PyInt_FromUid(st->st_uid)); + PyStructSequence_SET_ITEM(v, 5, _PyInt_FromGid(st->st_gid)); +#endif #ifdef HAVE_LARGEFILE_SUPPORT PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); @@ -1884,14 +2020,16 @@ static PyObject * posix_chown(PyObject *self, PyObject *args) { char *path = NULL; - long uid, gid; + uid_t uid; + gid_t gid; int res; - if (!PyArg_ParseTuple(args, "etll:chown", + if (!PyArg_ParseTuple(args, "etO&O&:chown", Py_FileSystemDefaultEncoding, &path, - &uid, &gid)) + _Py_Uid_Converter, &uid, + _Py_Gid_Converter, &gid)) return NULL; Py_BEGIN_ALLOW_THREADS - res = chown(path, (uid_t) uid, (gid_t) gid); + res = chown(path, uid, gid); Py_END_ALLOW_THREADS if (res < 0) return posix_error_with_allocated_filename(path); @@ -1911,12 +2049,15 @@ static PyObject * posix_fchown(PyObject *self, PyObject *args) { int fd; - long uid, gid; + uid_t uid; + gid_t gid; int res; - if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid)) + if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd, + _Py_Uid_Converter, &uid, + _Py_Gid_Converter, &gid)) return NULL; Py_BEGIN_ALLOW_THREADS - res = fchown(fd, (uid_t) uid, (gid_t) gid); + res = fchown(fd, uid, gid); Py_END_ALLOW_THREADS if (res < 0) return posix_error(); @@ -1934,14 +2075,16 @@ static PyObject * posix_lchown(PyObject *self, PyObject *args) { char *path = NULL; - long uid, gid; + uid_t uid; + gid_t gid; int res; - if (!PyArg_ParseTuple(args, "etll:lchown", + if (!PyArg_ParseTuple(args, "etO&O&:lchown", Py_FileSystemDefaultEncoding, &path, - &uid, &gid)) + _Py_Uid_Converter, &uid, + _Py_Gid_Converter, &gid)) return NULL; Py_BEGIN_ALLOW_THREADS - res = lchown(path, (uid_t) uid, (gid_t) gid); + res = lchown(path, uid, gid); Py_END_ALLOW_THREADS if (res < 0) return posix_error_with_allocated_filename(path); @@ -3844,7 +3987,7 @@ Return the current process's effective group id."); static PyObject * posix_getegid(PyObject *self, PyObject *noargs) { - return PyInt_FromLong((long)getegid()); + return _PyInt_FromGid(getegid()); } #endif @@ -3857,7 +4000,7 @@ Return the current process's effective user id."); static PyObject * posix_geteuid(PyObject *self, PyObject *noargs) { - return PyInt_FromLong((long)geteuid()); + return _PyInt_FromUid(geteuid()); } #endif @@ -3870,7 +4013,7 @@ Return the current process's group id."); static PyObject * posix_getgid(PyObject *self, PyObject *noargs) { - return PyInt_FromLong((long)getgid()); + return _PyInt_FromGid(getgid()); } #endif @@ -3945,7 +4088,7 @@ posix_getgroups(PyObject *self, PyObject *noargs) if (result != NULL) { int i; for (i = 0; i < n; ++i) { - PyObject *o = PyInt_FromLong((long)alt_grouplist[i]); + PyObject *o = _PyInt_FromGid(alt_grouplist[i]); if (o == NULL) { Py_DECREF(result); result = NULL; @@ -3974,12 +4117,22 @@ static PyObject * posix_initgroups(PyObject *self, PyObject *args) { char *username; - long gid; +#ifdef __APPLE__ + int gid; +#else + gid_t gid; +#endif - if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid)) +#ifdef __APPLE__ + if (!PyArg_ParseTuple(args, "si:initgroups", &username, + &gid)) +#else + if (!PyArg_ParseTuple(args, "sO&:initgroups", &username, + _Py_Gid_Converter, &gid)) +#endif return NULL; - if (initgroups(username, (gid_t) gid) == -1) + if (initgroups(username, gid) == -1) return PyErr_SetFromErrno(PyExc_OSError); Py_INCREF(Py_None); @@ -4093,7 +4246,7 @@ Return the current process's user id."); static PyObject * posix_getuid(PyObject *self, PyObject *noargs) { - return PyInt_FromLong((long)getuid()); + return _PyInt_FromUid(getuid()); } #endif @@ -5740,15 +5893,9 @@ Set the current process's user id."); static PyObject * posix_setuid(PyObject *self, PyObject *args) { - long uid_arg; uid_t uid; - if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) - return NULL; - uid = uid_arg; - if (uid != uid_arg) { - PyErr_SetString(PyExc_OverflowError, "user id too big"); + if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid)) return NULL; - } if (setuid(uid) < 0) return posix_error(); Py_INCREF(Py_None); @@ -5765,15 +5912,9 @@ Set the current process's effective user id."); static PyObject * posix_seteuid (PyObject *self, PyObject *args) { - long euid_arg; uid_t euid; - if (!PyArg_ParseTuple(args, "l", &euid_arg)) + if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid)) return NULL; - euid = euid_arg; - if (euid != euid_arg) { - PyErr_SetString(PyExc_OverflowError, "user id too big"); - return NULL; - } if (seteuid(euid) < 0) { return posix_error(); } else { @@ -5791,15 +5932,9 @@ Set the current process's effective group id."); static PyObject * posix_setegid (PyObject *self, PyObject *args) { - long egid_arg; gid_t egid; - if (!PyArg_ParseTuple(args, "l", &egid_arg)) + if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid)) return NULL; - egid = egid_arg; - if (egid != egid_arg) { - PyErr_SetString(PyExc_OverflowError, "group id too big"); - return NULL; - } if (setegid(egid) < 0) { return posix_error(); } else { @@ -5817,23 +5952,11 @@ Set the current process's real and effective user ids."); static PyObject * posix_setreuid (PyObject *self, PyObject *args) { - long ruid_arg, euid_arg; uid_t ruid, euid; - if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) - return NULL; - if (ruid_arg == -1) - ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ - else - ruid = ruid_arg; /* otherwise, assign from our long */ - if (euid_arg == -1) - euid = (uid_t)-1; - else - euid = euid_arg; - if ((euid_arg != -1 && euid != euid_arg) || - (ruid_arg != -1 && ruid != ruid_arg)) { - PyErr_SetString(PyExc_OverflowError, "user id too big"); + if (!PyArg_ParseTuple(args, "O&O&:setreuid", + _Py_Uid_Converter, &ruid, + _Py_Uid_Converter, &euid)) return NULL; - } if (setreuid(ruid, euid) < 0) { return posix_error(); } else { @@ -5851,23 +5974,11 @@ Set the current process's real and effective group ids."); static PyObject * posix_setregid (PyObject *self, PyObject *args) { - long rgid_arg, egid_arg; gid_t rgid, egid; - if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) + if (!PyArg_ParseTuple(args, "O&O&:setregid", + _Py_Gid_Converter, &rgid, + _Py_Gid_Converter, &egid)) return NULL; - if (rgid_arg == -1) - rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ - else - rgid = rgid_arg; /* otherwise, assign from our long */ - if (egid_arg == -1) - egid = (gid_t)-1; - else - egid = egid_arg; - if ((egid_arg != -1 && egid != egid_arg) || - (rgid_arg != -1 && rgid != rgid_arg)) { - PyErr_SetString(PyExc_OverflowError, "group id too big"); - return NULL; - } if (setregid(rgid, egid) < 0) { return posix_error(); } else { @@ -5885,15 +5996,9 @@ Set the current process's group id."); static PyObject * posix_setgid(PyObject *self, PyObject *args) { - long gid_arg; gid_t gid; - if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) - return NULL; - gid = gid_arg; - if (gid != gid_arg) { - PyErr_SetString(PyExc_OverflowError, "group id too big"); + if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid)) return NULL; - } if (setgid(gid) < 0) return posix_error(); Py_INCREF(Py_None); @@ -5926,35 +6031,13 @@ posix_setgroups(PyObject *self, PyObject *groups) elem = PySequence_GetItem(groups, i); if (!elem) return NULL; - if (!PyInt_Check(elem)) { - if (!PyLong_Check(elem)) { - PyErr_SetString(PyExc_TypeError, - "groups must be integers"); - Py_DECREF(elem); - return NULL; - } else { - unsigned long x = PyLong_AsUnsignedLong(elem); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, - "group id too big"); - Py_DECREF(elem); - return NULL; - } - grouplist[i] = x; - /* read back to see if it fits in gid_t */ - if (grouplist[i] != x) { - PyErr_SetString(PyExc_TypeError, - "group id too big"); - Py_DECREF(elem); - return NULL; - } - } + if (!PyInt_Check(elem) && !PyLong_Check(elem)) { + PyErr_SetString(PyExc_TypeError, + "groups must be integers"); + Py_DECREF(elem); + return NULL; } else { - long x = PyInt_AsLong(elem); - grouplist[i] = x; - if (grouplist[i] != x) { - PyErr_SetString(PyExc_TypeError, - "group id too big"); + if (!_Py_Gid_Converter(elem, &grouplist[i])) { Py_DECREF(elem); return NULL; } @@ -8580,9 +8663,11 @@ Set the current process's real, effective, and saved user ids."); static PyObject* posix_setresuid (PyObject *self, PyObject *args) { - /* We assume uid_t is no larger than a long. */ - long ruid, euid, suid; - if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) + uid_t ruid, euid, suid; + if (!PyArg_ParseTuple(args, "O&O&O&:setresuid", + _Py_Uid_Converter, &ruid, + _Py_Uid_Converter, &euid, + _Py_Uid_Converter, &suid)) return NULL; if (setresuid(ruid, euid, suid) < 0) return posix_error(); @@ -8598,9 +8683,11 @@ Set the current process's real, effective, and saved group ids."); static PyObject* posix_setresgid (PyObject *self, PyObject *args) { - /* We assume uid_t is no larger than a long. */ - long rgid, egid, sgid; - if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) + gid_t rgid, egid, sgid; + if (!PyArg_ParseTuple(args, "O&O&O&:setresgid", + _Py_Gid_Converter, &rgid, + _Py_Gid_Converter, &egid, + _Py_Gid_Converter, &sgid)) return NULL; if (setresgid(rgid, egid, sgid) < 0) return posix_error(); @@ -8617,14 +8704,11 @@ static PyObject* posix_getresuid (PyObject *self, PyObject *noargs) { uid_t ruid, euid, suid; - long l_ruid, l_euid, l_suid; if (getresuid(&ruid, &euid, &suid) < 0) return posix_error(); - /* Force the values into long's as we don't know the size of uid_t. */ - l_ruid = ruid; - l_euid = euid; - l_suid = suid; - return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); + return Py_BuildValue("(NNN)", _PyInt_FromUid(ruid), + _PyInt_FromUid(euid), + _PyInt_FromUid(suid)); } #endif @@ -8637,14 +8721,11 @@ static PyObject* posix_getresgid (PyObject *self, PyObject *noargs) { uid_t rgid, egid, sgid; - long l_rgid, l_egid, l_sgid; if (getresgid(&rgid, &egid, &sgid) < 0) return posix_error(); - /* Force the values into long's as we don't know the size of uid_t. */ - l_rgid = rgid; - l_egid = egid; - l_sgid = sgid; - return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); + return Py_BuildValue("(NNN)", _PyInt_FromGid(rgid), + _PyInt_FromGid(egid), + _PyInt_FromGid(sgid)); } #endif |