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 fcntl_fcntl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
35 {
36 PyObject *return_value = NULL;
37 int fd;
38 int code;
39 PyObject *arg = NULL;
40
41 if (!_PyArg_CheckPositional("fcntl", nargs, 2, 3)) {
42 goto exit;
43 }
44 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
45 goto exit;
46 }
47 code = _PyLong_AsInt(args[1]);
48 if (code == -1 && PyErr_Occurred()) {
49 goto exit;
50 }
51 if (nargs < 3) {
52 goto skip_optional;
53 }
54 arg = args[2];
55 skip_optional:
56 return_value = fcntl_fcntl_impl(module, fd, code, arg);
57
58 exit:
59 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 fcntl_ioctl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
104 {
105 PyObject *return_value = NULL;
106 int fd;
107 unsigned int code;
108 PyObject *ob_arg = NULL;
109 int mutate_arg = 1;
110
111 if (!_PyArg_CheckPositional("ioctl", nargs, 2, 4)) {
112 goto exit;
113 }
114 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
115 goto exit;
116 }
117 code = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
118 if (code == (unsigned int)-1 && PyErr_Occurred()) {
119 goto exit;
120 }
121 if (nargs < 3) {
122 goto skip_optional;
123 }
124 ob_arg = args[2];
125 if (nargs < 4) {
126 goto skip_optional;
127 }
128 mutate_arg = PyObject_IsTrue(args[3]);
129 if (mutate_arg < 0) {
130 goto exit;
131 }
132 skip_optional:
133 return_value = fcntl_ioctl_impl(module, fd, code, ob_arg, mutate_arg);
134
135 exit:
136 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 fcntl_flock(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
156 {
157 PyObject *return_value = NULL;
158 int fd;
159 int code;
160
161 if (!_PyArg_CheckPositional("flock", nargs, 2, 2)) {
162 goto exit;
163 }
164 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
165 goto exit;
166 }
167 code = _PyLong_AsInt(args[1]);
168 if (code == -1 && PyErr_Occurred()) {
169 goto exit;
170 }
171 return_value = fcntl_flock_impl(module, fd, code);
172
173 exit:
174 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 fcntl_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
213 {
214 PyObject *return_value = NULL;
215 int fd;
216 int code;
217 PyObject *lenobj = NULL;
218 PyObject *startobj = NULL;
219 int whence = 0;
220
221 if (!_PyArg_CheckPositional("lockf", nargs, 2, 5)) {
222 goto exit;
223 }
224 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
225 goto exit;
226 }
227 code = _PyLong_AsInt(args[1]);
228 if (code == -1 && PyErr_Occurred()) {
229 goto exit;
230 }
231 if (nargs < 3) {
232 goto skip_optional;
233 }
234 lenobj = args[2];
235 if (nargs < 4) {
236 goto skip_optional;
237 }
238 startobj = args[3];
239 if (nargs < 5) {
240 goto skip_optional;
241 }
242 whence = _PyLong_AsInt(args[4]);
243 if (whence == -1 && PyErr_Occurred()) {
244 goto exit;
245 }
246 skip_optional:
247 return_value = fcntl_lockf_impl(module, fd, code, lenobj, startobj, whence);
248
249 exit:
250 return return_value;
251 }
252 /*[clinic end generated code: output=1db859412172dd53 input=a9049054013a1b77]*/