-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
/
Copy pathposixshmem.c
221 lines (187 loc) · 5.57 KB
/
posixshmem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
posixshmem - A Python extension that provides shm_open() and shm_unlink()
*/
// Need limited C API version 3.13 for Py_mod_gil
#include "pyconfig.h" // Py_GIL_DISABLED
#ifndef Py_GIL_DISABLED
# define Py_LIMITED_API 0x030d0000
#endif
#include <Python.h>
#include <string.h> // strlen()
#include <errno.h> // EINTR
#ifdef HAVE_SYS_MMAN_H
# include <sys/mman.h> // shm_open(), shm_unlink()
#endif
/*[clinic input]
module _posixshmem
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a416734e49164bf8]*/
/*
*
* Module-level functions & meta stuff
*
*/
#ifdef HAVE_SHM_OPEN
/*[clinic input]
_posixshmem.shm_open -> int
path: unicode
flags: int
mode: int = 0o777
# "shm_open(path, flags, mode=0o777)\n\n\
Open a shared memory object. Returns a file descriptor (integer).
[clinic start generated code]*/
static int
_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;
Py_ssize_t name_size;
const char *name = PyUnicode_AsUTF8AndSize(path, &name_size);
if (name == NULL) {
return -1;
}
if (strlen(name) != (size_t)name_size) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
return -1;
}
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;
}
return fd;
}
#endif /* HAVE_SHM_OPEN */
#ifdef HAVE_SHM_RENAME
/*[clinic input]
_posixshmem.shm_rename -> int
path_from: unicode
path_to: unicode
flags: int
/
Rename a shared memory object.
Remove a shared memory object and relink to another path.
By default, if the destination path already exist, it will be unlinked.
With the SHM_RENAME_EXCHANGE flag, source and destination paths
will be exchanged.
With the SHM_RENAME_NOREPLACE flag, an error will be triggered
if the destination alredady exists.
[clinic start generated code]*/
static int
_posixshmem_shm_rename_impl(PyObject *module, PyObject *path_from,
PyObject *path_to, int flags)
/*[clinic end generated code: output=d9a710c512166e18 input=5fb42d1ce077caec]*/
{
int rv;
int async_err = 0;
Py_ssize_t from_size;
Py_ssize_t to_size;
const char *from = PyUnicode_AsUTF8AndSize(path_from, &from_size);
const char *to = PyUnicode_AsUTF8AndSize(path_to, &to_size);
if (from == NULL || to == NULL) {
return -1;
}
if (strlen(from) != (size_t)from_size || strlen(to) != (size_t)to_size) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
return -1;
}
do {
Py_BEGIN_ALLOW_THREADS
rv = shm_rename(from, to, flags);
Py_END_ALLOW_THREADS
} while (rv < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
if (rv < 0) {
if (!async_err)
PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path_from, path_to);
return -1;
}
return rv;
}
#endif /* HAVE_SHM_RENAME */
#ifdef HAVE_SHM_UNLINK
/*[clinic input]
_posixshmem.shm_unlink
path: unicode
/
Remove a shared memory object (similar to unlink()).
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.
[clinic start generated code]*/
static PyObject *
_posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)
/*[clinic end generated code: output=42f8b23d134b9ff5 input=298369d013dcad63]*/
{
int rv;
int async_err = 0;
Py_ssize_t name_size;
const char *name = PyUnicode_AsUTF8AndSize(path, &name_size);
if (name == NULL) {
return NULL;
}
if (strlen(name) != (size_t)name_size) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
return NULL;
}
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;
}
Py_RETURN_NONE;
}
#endif /* HAVE_SHM_UNLINK */
#include "clinic/posixshmem.c.h"
static PyMethodDef module_methods[ ] = {
_POSIXSHMEM_SHM_OPEN_METHODDEF
#if defined(HAVE_SHM_RENAME)
_POSIXSHMEM_SHM_RENAME_METHODDEF
#endif
_POSIXSHMEM_SHM_UNLINK_METHODDEF
{NULL} /* Sentinel */
};
static int
posixshmem_exec(PyObject *m)
{
#ifdef HAVE_SHM_RENAME
#ifdef SHM_RENAME_EXCHANGE
if (PyModule_AddIntMacro(m, SHM_RENAME_EXCHANGE)) return -1;
#endif
#ifdef SHM_RENAME_NOREPLACE
if (PyModule_AddIntMacro(m, SHM_RENAME_NOREPLACE)) return -1;
#endif
#endif /* HAVE_SHM_RENAME */
return 0;
}
static PyModuleDef_Slot module_slots[] = {
{Py_mod_exec, posixshmem_exec},
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
{0, NULL}
};
static struct PyModuleDef _posixshmemmodule = {
PyModuleDef_HEAD_INIT,
.m_name = "_posixshmem",
.m_doc = "POSIX shared memory module",
.m_size = 0,
.m_methods = module_methods,
.m_slots = module_slots,
};
/* Module init function */
PyMODINIT_FUNC
PyInit__posixshmem(void)
{
return PyModuleDef_Init(&_posixshmemmodule);
}