Branch data Line data Source code
1 : : /*[clinic input]
2 : : preserve
3 : : [clinic start generated code]*/
4 : :
5 : : #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 : : # include "pycore_gc.h" // PyGC_Head
7 : : # include "pycore_runtime.h" // _Py_ID()
8 : : #endif
9 : :
10 : :
11 : : PyDoc_STRVAR(fcntl_fcntl__doc__,
12 : : "fcntl($module, fd, cmd, arg=0, /)\n"
13 : : "--\n"
14 : : "\n"
15 : : "Perform the operation `cmd` on file descriptor fd.\n"
16 : : "\n"
17 : : "The values used for `cmd` are operating system dependent, and are available\n"
18 : : "as constants in the fcntl module, using the same names as used in\n"
19 : : "the relevant C header files. The argument arg is optional, and\n"
20 : : "defaults to 0; it may be an int or a string. If arg is given as a string,\n"
21 : : "the return value of fcntl is a string of that length, containing the\n"
22 : : "resulting value put in the arg buffer by the operating system. The length\n"
23 : : "of the arg string is not allowed to exceed 1024 bytes. If the arg given\n"
24 : : "is an integer or if none is specified, the result value is an integer\n"
25 : : "corresponding to the return value of the fcntl call in the C code.");
26 : :
27 : : #define FCNTL_FCNTL_METHODDEF \
28 : : {"fcntl", _PyCFunction_CAST(fcntl_fcntl), METH_FASTCALL, fcntl_fcntl__doc__},
29 : :
30 : : static PyObject *
31 : : fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg);
32 : :
33 : : static PyObject *
34 : 0 : fcntl_fcntl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
35 : : {
36 : 0 : PyObject *return_value = NULL;
37 : : int fd;
38 : : int code;
39 : 0 : PyObject *arg = NULL;
40 : :
41 [ # # # # : 0 : if (!_PyArg_CheckPositional("fcntl", nargs, 2, 3)) {
# # ]
42 : 0 : goto exit;
43 : : }
44 [ # # ]: 0 : if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
45 : 0 : goto exit;
46 : : }
47 : 0 : code = _PyLong_AsInt(args[1]);
48 [ # # # # ]: 0 : if (code == -1 && PyErr_Occurred()) {
49 : 0 : goto exit;
50 : : }
51 [ # # ]: 0 : if (nargs < 3) {
52 : 0 : goto skip_optional;
53 : : }
54 : 0 : arg = args[2];
55 : 0 : skip_optional:
56 : 0 : return_value = fcntl_fcntl_impl(module, fd, code, arg);
57 : :
58 : 0 : exit:
59 : 0 : return return_value;
60 : : }
61 : :
62 : : PyDoc_STRVAR(fcntl_ioctl__doc__,
63 : : "ioctl($module, fd, request, arg=0, mutate_flag=True, /)\n"
64 : : "--\n"
65 : : "\n"
66 : : "Perform the operation `request` on file descriptor `fd`.\n"
67 : : "\n"
68 : : "The values used for `request` are operating system dependent, and are available\n"
69 : : "as constants in the fcntl or termios library modules, using the same names as\n"
70 : : "used in the relevant C header files.\n"
71 : : "\n"
72 : : "The argument `arg` is optional, and defaults to 0; it may be an int or a\n"
73 : : "buffer containing character data (most likely a string or an array).\n"
74 : : "\n"
75 : : "If the argument is a mutable buffer (such as an array) and if the\n"
76 : : "mutate_flag argument (which is only allowed in this case) is true then the\n"
77 : : "buffer is (in effect) passed to the operating system and changes made by\n"
78 : : "the OS will be reflected in the contents of the buffer after the call has\n"
79 : : "returned. The return value is the integer returned by the ioctl system\n"
80 : : "call.\n"
81 : : "\n"
82 : : "If the argument is a mutable buffer and the mutable_flag argument is false,\n"
83 : : "the behavior is as if a string had been passed.\n"
84 : : "\n"
85 : : "If the argument is an immutable buffer (most likely a string) then a copy\n"
86 : : "of the buffer is passed to the operating system and the return value is a\n"
87 : : "string of the same length containing whatever the operating system put in\n"
88 : : "the buffer. The length of the arg buffer in this case is not allowed to\n"
89 : : "exceed 1024 bytes.\n"
90 : : "\n"
91 : : "If the arg given is an integer or if none is specified, the result value is\n"
92 : : "an integer corresponding to the return value of the ioctl call in the C\n"
93 : : "code.");
94 : :
95 : : #define FCNTL_IOCTL_METHODDEF \
96 : : {"ioctl", _PyCFunction_CAST(fcntl_ioctl), METH_FASTCALL, fcntl_ioctl__doc__},
97 : :
98 : : static PyObject *
99 : : fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
100 : : PyObject *ob_arg, int mutate_arg);
101 : :
102 : : static PyObject *
103 : 0 : fcntl_ioctl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
104 : : {
105 : 0 : PyObject *return_value = NULL;
106 : : int fd;
107 : : unsigned int code;
108 : 0 : PyObject *ob_arg = NULL;
109 : 0 : int mutate_arg = 1;
110 : :
111 [ # # # # : 0 : if (!_PyArg_CheckPositional("ioctl", nargs, 2, 4)) {
# # ]
112 : 0 : goto exit;
113 : : }
114 [ # # ]: 0 : if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
115 : 0 : goto exit;
116 : : }
117 : 0 : code = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
118 [ # # # # ]: 0 : if (code == (unsigned int)-1 && PyErr_Occurred()) {
119 : 0 : goto exit;
120 : : }
121 [ # # ]: 0 : if (nargs < 3) {
122 : 0 : goto skip_optional;
123 : : }
124 : 0 : ob_arg = args[2];
125 [ # # ]: 0 : if (nargs < 4) {
126 : 0 : goto skip_optional;
127 : : }
128 : 0 : mutate_arg = PyObject_IsTrue(args[3]);
129 [ # # ]: 0 : if (mutate_arg < 0) {
130 : 0 : goto exit;
131 : : }
132 : 0 : skip_optional:
133 : 0 : return_value = fcntl_ioctl_impl(module, fd, code, ob_arg, mutate_arg);
134 : :
135 : 0 : exit:
136 : 0 : return return_value;
137 : : }
138 : :
139 : : PyDoc_STRVAR(fcntl_flock__doc__,
140 : : "flock($module, fd, operation, /)\n"
141 : : "--\n"
142 : : "\n"
143 : : "Perform the lock operation `operation` on file descriptor `fd`.\n"
144 : : "\n"
145 : : "See the Unix manual page for flock(2) for details (On some systems, this\n"
146 : : "function is emulated using fcntl()).");
147 : :
148 : : #define FCNTL_FLOCK_METHODDEF \
149 : : {"flock", _PyCFunction_CAST(fcntl_flock), METH_FASTCALL, fcntl_flock__doc__},
150 : :
151 : : static PyObject *
152 : : fcntl_flock_impl(PyObject *module, int fd, int code);
153 : :
154 : : static PyObject *
155 : 0 : fcntl_flock(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
156 : : {
157 : 0 : PyObject *return_value = NULL;
158 : : int fd;
159 : : int code;
160 : :
161 [ # # # # : 0 : if (!_PyArg_CheckPositional("flock", nargs, 2, 2)) {
# # ]
162 : 0 : goto exit;
163 : : }
164 [ # # ]: 0 : if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
165 : 0 : goto exit;
166 : : }
167 : 0 : code = _PyLong_AsInt(args[1]);
168 [ # # # # ]: 0 : if (code == -1 && PyErr_Occurred()) {
169 : 0 : goto exit;
170 : : }
171 : 0 : return_value = fcntl_flock_impl(module, fd, code);
172 : :
173 : 0 : exit:
174 : 0 : return return_value;
175 : : }
176 : :
177 : : PyDoc_STRVAR(fcntl_lockf__doc__,
178 : : "lockf($module, fd, cmd, len=0, start=0, whence=0, /)\n"
179 : : "--\n"
180 : : "\n"
181 : : "A wrapper around the fcntl() locking calls.\n"
182 : : "\n"
183 : : "`fd` is the file descriptor of the file to lock or unlock, and operation is one\n"
184 : : "of the following values:\n"
185 : : "\n"
186 : : " LOCK_UN - unlock\n"
187 : : " LOCK_SH - acquire a shared lock\n"
188 : : " LOCK_EX - acquire an exclusive lock\n"
189 : : "\n"
190 : : "When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with\n"
191 : : "LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n"
192 : : "lock cannot be acquired, an OSError will be raised and the exception will\n"
193 : : "have an errno attribute set to EACCES or EAGAIN (depending on the operating\n"
194 : : "system -- for portability, check for either value).\n"
195 : : "\n"
196 : : "`len` is the number of bytes to lock, with the default meaning to lock to\n"
197 : : "EOF. `start` is the byte offset, relative to `whence`, to that the lock\n"
198 : : "starts. `whence` is as with fileobj.seek(), specifically:\n"
199 : : "\n"
200 : : " 0 - relative to the start of the file (SEEK_SET)\n"
201 : : " 1 - relative to the current buffer position (SEEK_CUR)\n"
202 : : " 2 - relative to the end of the file (SEEK_END)");
203 : :
204 : : #define FCNTL_LOCKF_METHODDEF \
205 : : {"lockf", _PyCFunction_CAST(fcntl_lockf), METH_FASTCALL, fcntl_lockf__doc__},
206 : :
207 : : static PyObject *
208 : : fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj,
209 : : PyObject *startobj, int whence);
210 : :
211 : : static PyObject *
212 : 0 : fcntl_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
213 : : {
214 : 0 : PyObject *return_value = NULL;
215 : : int fd;
216 : : int code;
217 : 0 : PyObject *lenobj = NULL;
218 : 0 : PyObject *startobj = NULL;
219 : 0 : int whence = 0;
220 : :
221 [ # # # # : 0 : if (!_PyArg_CheckPositional("lockf", nargs, 2, 5)) {
# # ]
222 : 0 : goto exit;
223 : : }
224 [ # # ]: 0 : if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
225 : 0 : goto exit;
226 : : }
227 : 0 : code = _PyLong_AsInt(args[1]);
228 [ # # # # ]: 0 : if (code == -1 && PyErr_Occurred()) {
229 : 0 : goto exit;
230 : : }
231 [ # # ]: 0 : if (nargs < 3) {
232 : 0 : goto skip_optional;
233 : : }
234 : 0 : lenobj = args[2];
235 [ # # ]: 0 : if (nargs < 4) {
236 : 0 : goto skip_optional;
237 : : }
238 : 0 : startobj = args[3];
239 [ # # ]: 0 : if (nargs < 5) {
240 : 0 : goto skip_optional;
241 : : }
242 : 0 : whence = _PyLong_AsInt(args[4]);
243 [ # # # # ]: 0 : if (whence == -1 && PyErr_Occurred()) {
244 : 0 : goto exit;
245 : : }
246 : 0 : skip_optional:
247 : 0 : return_value = fcntl_lockf_impl(module, fd, code, lenobj, startobj, whence);
248 : :
249 : 0 : exit:
250 : 0 : return return_value;
251 : : }
252 : : /*[clinic end generated code: output=1db859412172dd53 input=a9049054013a1b77]*/
|