posixshmem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. posixshmem - A Python extension that provides shm_open() and shm_unlink()
  3. */
  4. #define PY_SSIZE_T_CLEAN
  5. #include <Python.h>
  6. // for shm_open() and shm_unlink()
  7. #ifdef HAVE_SYS_MMAN_H
  8. #include <sys/mman.h>
  9. #endif
  10. /*[clinic input]
  11. module _posixshmem
  12. [clinic start generated code]*/
  13. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=a416734e49164bf8]*/
  14. /*
  15. *
  16. * Module-level functions & meta stuff
  17. *
  18. */
  19. #ifdef HAVE_SHM_OPEN
  20. /*[clinic input]
  21. _posixshmem.shm_open -> int
  22. path: unicode
  23. flags: int
  24. mode: int = 0o777
  25. # "shm_open(path, flags, mode=0o777)\n\n\
  26. Open a shared memory object. Returns a file descriptor (integer).
  27. [clinic start generated code]*/
  28. static int
  29. _posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
  30. int mode)
  31. /*[clinic end generated code: output=8d110171a4fa20df input=e83b58fa802fac25]*/
  32. {
  33. int fd;
  34. int async_err = 0;
  35. Py_ssize_t name_size;
  36. const char *name = PyUnicode_AsUTF8AndSize(path, &name_size);
  37. if (name == NULL) {
  38. return -1;
  39. }
  40. if (strlen(name) != (size_t)name_size) {
  41. PyErr_SetString(PyExc_ValueError, "embedded null character");
  42. return -1;
  43. }
  44. do {
  45. Py_BEGIN_ALLOW_THREADS
  46. fd = shm_open(name, flags, mode);
  47. Py_END_ALLOW_THREADS
  48. } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  49. if (fd < 0) {
  50. if (!async_err)
  51. PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
  52. return -1;
  53. }
  54. return fd;
  55. }
  56. #endif /* HAVE_SHM_OPEN */
  57. #ifdef HAVE_SHM_UNLINK
  58. /*[clinic input]
  59. _posixshmem.shm_unlink
  60. path: unicode
  61. Remove a shared memory object (similar to unlink()).
  62. Remove a shared memory object name, and, once all processes have unmapped
  63. the object, de-allocates and destroys the contents of the associated memory
  64. region.
  65. [clinic start generated code]*/
  66. static PyObject *
  67. _posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)
  68. /*[clinic end generated code: output=42f8b23d134b9ff5 input=8dc0f87143e3b300]*/
  69. {
  70. int rv;
  71. int async_err = 0;
  72. Py_ssize_t name_size;
  73. const char *name = PyUnicode_AsUTF8AndSize(path, &name_size);
  74. if (name == NULL) {
  75. return NULL;
  76. }
  77. if (strlen(name) != (size_t)name_size) {
  78. PyErr_SetString(PyExc_ValueError, "embedded null character");
  79. return NULL;
  80. }
  81. do {
  82. Py_BEGIN_ALLOW_THREADS
  83. rv = shm_unlink(name);
  84. Py_END_ALLOW_THREADS
  85. } while (rv < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  86. if (rv < 0) {
  87. if (!async_err)
  88. PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
  89. return NULL;
  90. }
  91. Py_RETURN_NONE;
  92. }
  93. #endif /* HAVE_SHM_UNLINK */
  94. #include "clinic/posixshmem.c.h"
  95. static PyMethodDef module_methods[ ] = {
  96. _POSIXSHMEM_SHM_OPEN_METHODDEF
  97. _POSIXSHMEM_SHM_UNLINK_METHODDEF
  98. {NULL} /* Sentinel */
  99. };
  100. static PyModuleDef_Slot module_slots[] = {
  101. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  102. {0, NULL}
  103. };
  104. static struct PyModuleDef _posixshmemmodule = {
  105. PyModuleDef_HEAD_INIT,
  106. .m_name = "_posixshmem",
  107. .m_doc = "POSIX shared memory module",
  108. .m_size = 0,
  109. .m_methods = module_methods,
  110. .m_slots = module_slots,
  111. };
  112. /* Module init function */
  113. PyMODINIT_FUNC
  114. PyInit__posixshmem(void)
  115. {
  116. return PyModuleDef_Init(&_posixshmemmodule);
  117. }