2019-02-02 00:52:23 -04:00
|
|
|
/*
|
2019-02-24 00:08:16 -04:00
|
|
|
posixshmem - A Python extension that provides shm_open() and shm_unlink()
|
2019-02-02 00:52:23 -04:00
|
|
|
*/
|
|
|
|
|
2023-11-20 09:52:00 -04:00
|
|
|
#include "pyconfig.h" // Py_GIL_DISABLED
|
2023-10-30 13:06:09 -03:00
|
|
|
|
2023-11-20 09:52:00 -04:00
|
|
|
#ifndef Py_GIL_DISABLED
|
2023-11-01 00:24:33 -03:00
|
|
|
// Need limited C API version 3.12 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
|
|
|
|
#define Py_LIMITED_API 0x030c0000
|
2023-10-30 13:06:09 -03:00
|
|
|
#endif
|
2023-10-17 09:30:31 -03:00
|
|
|
|
2019-02-02 00:52:23 -04:00
|
|
|
#include <Python.h>
|
|
|
|
|
2023-10-19 19:23:49 -03:00
|
|
|
#include <errno.h> // EINTR
|
2019-02-24 00:08:16 -04:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
2023-10-19 19:23:49 -03:00
|
|
|
# include <sys/mman.h> // shm_open(), shm_unlink()
|
2019-02-02 00:52:23 -04:00
|
|
|
#endif
|
|
|
|
|
2023-10-19 19:23:49 -03:00
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
/*[clinic input]
|
|
|
|
module _posixshmem
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a416734e49164bf8]*/
|
2019-02-02 00:52:23 -04:00
|
|
|
|
|
|
|
/*
|
2019-02-24 00:08:16 -04:00
|
|
|
*
|
|
|
|
* Module-level functions & meta stuff
|
|
|
|
*
|
|
|
|
*/
|
2019-02-02 00:52:23 -04:00
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
#ifdef HAVE_SHM_OPEN
|
|
|
|
/*[clinic input]
|
|
|
|
_posixshmem.shm_open -> int
|
|
|
|
path: unicode
|
|
|
|
flags: int
|
|
|
|
mode: int = 0o777
|
2019-02-02 00:52:23 -04:00
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
# "shm_open(path, flags, mode=0o777)\n\n\
|
2019-02-02 00:52:23 -04:00
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
Open a shared memory object. Returns a file descriptor (integer).
|
2019-02-02 00:52:23 -04:00
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
[clinic start generated code]*/
|
2019-02-02 00:52:23 -04:00
|
|
|
|
|
|
|
static int
|
2019-02-24 00:08:16 -04:00
|
|
|
_posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
|
|
|
|
int mode)
|
|
|
|
/*[clinic end generated code: output=8d110171a4fa20df input=e83b58fa802fac25]*/
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int async_err = 0;
|
2023-11-07 18:36:13 -04:00
|
|
|
const char *name = PyUnicode_AsUTF8AndSize(path, NULL);
|
2019-02-24 00:08:16 -04:00
|
|
|
if (name == NULL) {
|
|
|
|
return -1;
|
2019-02-02 00:52:23 -04:00
|
|
|
}
|
2019-02-24 00:08:16 -04:00
|
|
|
do {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
fd = shm_open(name, flags, mode);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
} while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
if (!async_err)
|
|
|
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
|
|
|
|
return -1;
|
2019-02-02 00:52:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
return fd;
|
2019-02-02 00:52:23 -04:00
|
|
|
}
|
2019-02-24 00:08:16 -04:00
|
|
|
#endif /* HAVE_SHM_OPEN */
|
2019-02-02 00:52:23 -04:00
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
#ifdef HAVE_SHM_UNLINK
|
|
|
|
/*[clinic input]
|
|
|
|
_posixshmem.shm_unlink
|
|
|
|
path: unicode
|
2019-02-02 00:52:23 -04:00
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
Remove a shared memory object (similar to unlink()).
|
2019-02-02 00:52:23 -04:00
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
Remove a shared memory object name, and, once all processes have unmapped
|
|
|
|
the object, de-allocates and destroys the contents of the associated memory
|
|
|
|
region.
|
2019-02-02 00:52:23 -04:00
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
[clinic start generated code]*/
|
2019-02-02 00:52:23 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2019-02-24 00:08:16 -04:00
|
|
|
_posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)
|
|
|
|
/*[clinic end generated code: output=42f8b23d134b9ff5 input=8dc0f87143e3b300]*/
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int async_err = 0;
|
2023-11-07 18:36:13 -04:00
|
|
|
const char *name = PyUnicode_AsUTF8AndSize(path, NULL);
|
2019-02-24 00:08:16 -04:00
|
|
|
if (name == NULL) {
|
|
|
|
return NULL;
|
2019-02-02 00:52:23 -04:00
|
|
|
}
|
2019-02-24 00:08:16 -04:00
|
|
|
do {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
rv = shm_unlink(name);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
} while (rv < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
|
|
|
|
|
|
|
|
if (rv < 0) {
|
|
|
|
if (!async_err)
|
|
|
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
|
|
|
|
return NULL;
|
2019-02-02 00:52:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2019-02-24 00:08:16 -04:00
|
|
|
#endif /* HAVE_SHM_UNLINK */
|
2019-02-02 00:52:23 -04:00
|
|
|
|
2019-02-24 00:08:16 -04:00
|
|
|
#include "clinic/posixshmem.c.h"
|
2019-02-02 00:52:23 -04:00
|
|
|
|
|
|
|
static PyMethodDef module_methods[ ] = {
|
2019-02-24 00:08:16 -04:00
|
|
|
_POSIXSHMEM_SHM_OPEN_METHODDEF
|
|
|
|
_POSIXSHMEM_SHM_UNLINK_METHODDEF
|
2019-02-02 00:52:23 -04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-05-05 18:11:27 -03:00
|
|
|
static PyModuleDef_Slot module_slots[] = {
|
|
|
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-11-19 11:20:42 -04:00
|
|
|
static struct PyModuleDef _posixshmemmodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
.m_name = "_posixshmem",
|
|
|
|
.m_doc = "POSIX shared memory module",
|
|
|
|
.m_size = 0,
|
|
|
|
.m_methods = module_methods,
|
2023-05-05 18:11:27 -03:00
|
|
|
.m_slots = module_slots,
|
2019-02-02 00:52:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Module init function */
|
|
|
|
PyMODINIT_FUNC
|
2020-11-19 11:20:42 -04:00
|
|
|
PyInit__posixshmem(void)
|
|
|
|
{
|
|
|
|
return PyModuleDef_Init(&_posixshmemmodule);
|
2019-02-02 00:52:23 -04:00
|
|
|
}
|