Make getpeerid re-useable

This commit is contained in:
Kovid Goyal
2024-08-26 18:55:10 +05:30
parent 0594033b31
commit eedf01fbe3
2 changed files with 15 additions and 8 deletions

View File

@@ -360,19 +360,25 @@ locale_is_valid(PyObject *self UNUSED, PyObject *args) {
Py_RETURN_TRUE;
}
bool
getpeerid(int fd, uid_t *euid, gid_t *egid) {
#ifdef __linux__
struct ucred cr;
socklen_t sz = sizeof(cr);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &sz) != 0) return false;
*euid = cr.uid; *egid = cr.gid;
#else
if (getpeereid(fd, euid, egid) != 0) return false;
#endif
return true;
}
static PyObject*
py_getpeereid(PyObject *self UNUSED, PyObject *args) {
int fd;
if (!PyArg_ParseTuple(args, "i", &fd)) return NULL;
uid_t euid = 0; gid_t egid = 0;
#ifdef __linux__
struct ucred cr;
socklen_t sz = sizeof(cr);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &sz) != 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
euid = cr.uid; egid = cr.gid;
#else
if (getpeereid(fd, &euid, &egid) != 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
#endif
if (!getpeerid(fd, &euid, &egid)) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
int u = euid, g = egid;
return Py_BuildValue("ii", u, g);
}

View File

@@ -423,6 +423,7 @@ void play_canberra_sound(const char *which_sound, const char *event_id, bool is_
SPRITE_MAP_HANDLE alloc_sprite_map(unsigned int, unsigned int);
SPRITE_MAP_HANDLE free_sprite_map(SPRITE_MAP_HANDLE);
const char* get_hyperlink_for_id(const HYPERLINK_POOL_HANDLE, hyperlink_id_type id, bool only_url);
bool getpeerid(int fd, uid_t *euid, gid_t *egid);
#define memset_array(array, val, count) if ((count) > 0) { \
(array)[0] = (val); \