blob: ed8d96a4a81bd6180d342d681c59abc851a489ea [file] [log] [blame]
Brett Cannonb7299dd2014-11-09 20:22:01 -05001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(fcntl_fcntl__doc__,
Serhiy Storchaka17d3a582015-03-20 20:04:21 +02006"fcntl($module, fd, cmd, arg=0, /)\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -05007"--\n"
8"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +02009"Perform the operation `cmd` on file descriptor fd.\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050010"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020011"The values used for `cmd` are operating system dependent, and are available\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050012"as constants in the fcntl module, using the same names as used in\n"
13"the relevant C header files. The argument arg is optional, and\n"
14"defaults to 0; it may be an int or a string. If arg is given as a string,\n"
15"the return value of fcntl is a string of that length, containing the\n"
16"resulting value put in the arg buffer by the operating system. The length\n"
17"of the arg string is not allowed to exceed 1024 bytes. If the arg given\n"
18"is an integer or if none is specified, the result value is an integer\n"
19"corresponding to the return value of the fcntl call in the C code.");
20
21#define FCNTL_FCNTL_METHODDEF \
Victor Stinner259f0e42017-01-17 01:35:17 +010022 {"fcntl", (PyCFunction)fcntl_fcntl, METH_FASTCALL, fcntl_fcntl__doc__},
Brett Cannonb7299dd2014-11-09 20:22:01 -050023
24static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030025fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg);
Brett Cannonb7299dd2014-11-09 20:22:01 -050026
27static PyObject *
Victor Stinner259f0e42017-01-17 01:35:17 +010028fcntl_fcntl(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Brett Cannonb7299dd2014-11-09 20:22:01 -050029{
30 PyObject *return_value = NULL;
31 int fd;
32 int code;
33 PyObject *arg = NULL;
34
Victor Stinner259f0e42017-01-17 01:35:17 +010035 if (!_PyArg_ParseStack(args, nargs, "O&i|O:fcntl",
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030036 conv_descriptor, &fd, &code, &arg)) {
Brett Cannonb7299dd2014-11-09 20:22:01 -050037 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030038 }
Victor Stinner259f0e42017-01-17 01:35:17 +010039
40 if (!_PyArg_NoStackKeywords("fcntl", kwnames)) {
41 goto exit;
42 }
Brett Cannonb7299dd2014-11-09 20:22:01 -050043 return_value = fcntl_fcntl_impl(module, fd, code, arg);
44
45exit:
46 return return_value;
47}
48
49PyDoc_STRVAR(fcntl_ioctl__doc__,
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020050"ioctl($module, fd, request, arg=0, mutate_flag=True, /)\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050051"--\n"
52"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020053"Perform the operation `request` on file descriptor `fd`.\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050054"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020055"The values used for `request` are operating system dependent, and are available\n"
56"as constants in the fcntl or termios library modules, using the same names as\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050057"used in the relevant C header files.\n"
58"\n"
59"The argument `arg` is optional, and defaults to 0; it may be an int or a\n"
60"buffer containing character data (most likely a string or an array).\n"
61"\n"
62"If the argument is a mutable buffer (such as an array) and if the\n"
63"mutate_flag argument (which is only allowed in this case) is true then the\n"
64"buffer is (in effect) passed to the operating system and changes made by\n"
65"the OS will be reflected in the contents of the buffer after the call has\n"
66"returned. The return value is the integer returned by the ioctl system\n"
67"call.\n"
68"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020069"If the argument is a mutable buffer and the mutable_flag argument is false,\n"
70"the behavior is as if a string had been passed.\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050071"\n"
72"If the argument is an immutable buffer (most likely a string) then a copy\n"
73"of the buffer is passed to the operating system and the return value is a\n"
74"string of the same length containing whatever the operating system put in\n"
75"the buffer. The length of the arg buffer in this case is not allowed to\n"
76"exceed 1024 bytes.\n"
77"\n"
78"If the arg given is an integer or if none is specified, the result value is\n"
79"an integer corresponding to the return value of the ioctl call in the C\n"
80"code.");
81
82#define FCNTL_IOCTL_METHODDEF \
Victor Stinner259f0e42017-01-17 01:35:17 +010083 {"ioctl", (PyCFunction)fcntl_ioctl, METH_FASTCALL, fcntl_ioctl__doc__},
Brett Cannonb7299dd2014-11-09 20:22:01 -050084
85static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030086fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
Larry Hastings89964c42015-04-14 18:07:59 -040087 PyObject *ob_arg, int mutate_arg);
Brett Cannonb7299dd2014-11-09 20:22:01 -050088
89static PyObject *
Victor Stinner259f0e42017-01-17 01:35:17 +010090fcntl_ioctl(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Brett Cannonb7299dd2014-11-09 20:22:01 -050091{
92 PyObject *return_value = NULL;
93 int fd;
94 unsigned int code;
95 PyObject *ob_arg = NULL;
96 int mutate_arg = 1;
97
Victor Stinner259f0e42017-01-17 01:35:17 +010098 if (!_PyArg_ParseStack(args, nargs, "O&I|Op:ioctl",
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030099 conv_descriptor, &fd, &code, &ob_arg, &mutate_arg)) {
Brett Cannonb7299dd2014-11-09 20:22:01 -0500100 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300101 }
Victor Stinner259f0e42017-01-17 01:35:17 +0100102
103 if (!_PyArg_NoStackKeywords("ioctl", kwnames)) {
104 goto exit;
105 }
Brett Cannonb7299dd2014-11-09 20:22:01 -0500106 return_value = fcntl_ioctl_impl(module, fd, code, ob_arg, mutate_arg);
107
108exit:
109 return return_value;
110}
111
112PyDoc_STRVAR(fcntl_flock__doc__,
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200113"flock($module, fd, operation, /)\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500114"--\n"
115"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200116"Perform the lock operation `operation` on file descriptor `fd`.\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500117"\n"
118"See the Unix manual page for flock(2) for details (On some systems, this\n"
119"function is emulated using fcntl()).");
120
121#define FCNTL_FLOCK_METHODDEF \
Victor Stinner259f0e42017-01-17 01:35:17 +0100122 {"flock", (PyCFunction)fcntl_flock, METH_FASTCALL, fcntl_flock__doc__},
Brett Cannonb7299dd2014-11-09 20:22:01 -0500123
124static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300125fcntl_flock_impl(PyObject *module, int fd, int code);
Brett Cannonb7299dd2014-11-09 20:22:01 -0500126
127static PyObject *
Victor Stinner259f0e42017-01-17 01:35:17 +0100128fcntl_flock(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Brett Cannonb7299dd2014-11-09 20:22:01 -0500129{
130 PyObject *return_value = NULL;
131 int fd;
132 int code;
133
Victor Stinner259f0e42017-01-17 01:35:17 +0100134 if (!_PyArg_ParseStack(args, nargs, "O&i:flock",
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300135 conv_descriptor, &fd, &code)) {
Brett Cannonb7299dd2014-11-09 20:22:01 -0500136 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300137 }
Victor Stinner259f0e42017-01-17 01:35:17 +0100138
139 if (!_PyArg_NoStackKeywords("flock", kwnames)) {
140 goto exit;
141 }
Brett Cannonb7299dd2014-11-09 20:22:01 -0500142 return_value = fcntl_flock_impl(module, fd, code);
143
144exit:
145 return return_value;
146}
147
148PyDoc_STRVAR(fcntl_lockf__doc__,
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200149"lockf($module, fd, cmd, len=0, start=0, whence=0, /)\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500150"--\n"
151"\n"
152"A wrapper around the fcntl() locking calls.\n"
153"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200154"`fd` is the file descriptor of the file to lock or unlock, and operation is one\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500155"of the following values:\n"
156"\n"
157" LOCK_UN - unlock\n"
158" LOCK_SH - acquire a shared lock\n"
159" LOCK_EX - acquire an exclusive lock\n"
160"\n"
161"When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with\n"
162"LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n"
Serhiy Storchakad6117a42016-04-14 12:28:01 +0300163"lock cannot be acquired, an OSError will be raised and the exception will\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500164"have an errno attribute set to EACCES or EAGAIN (depending on the operating\n"
165"system -- for portability, check for either value).\n"
166"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200167"`len` is the number of bytes to lock, with the default meaning to lock to\n"
168"EOF. `start` is the byte offset, relative to `whence`, to that the lock\n"
169"starts. `whence` is as with fileobj.seek(), specifically:\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500170"\n"
171" 0 - relative to the start of the file (SEEK_SET)\n"
172" 1 - relative to the current buffer position (SEEK_CUR)\n"
173" 2 - relative to the end of the file (SEEK_END)");
174
175#define FCNTL_LOCKF_METHODDEF \
Victor Stinner259f0e42017-01-17 01:35:17 +0100176 {"lockf", (PyCFunction)fcntl_lockf, METH_FASTCALL, fcntl_lockf__doc__},
Brett Cannonb7299dd2014-11-09 20:22:01 -0500177
178static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300179fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj,
Larry Hastings89964c42015-04-14 18:07:59 -0400180 PyObject *startobj, int whence);
Brett Cannonb7299dd2014-11-09 20:22:01 -0500181
182static PyObject *
Victor Stinner259f0e42017-01-17 01:35:17 +0100183fcntl_lockf(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Brett Cannonb7299dd2014-11-09 20:22:01 -0500184{
185 PyObject *return_value = NULL;
186 int fd;
187 int code;
188 PyObject *lenobj = NULL;
189 PyObject *startobj = NULL;
190 int whence = 0;
191
Victor Stinner259f0e42017-01-17 01:35:17 +0100192 if (!_PyArg_ParseStack(args, nargs, "O&i|OOi:lockf",
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300193 conv_descriptor, &fd, &code, &lenobj, &startobj, &whence)) {
Brett Cannonb7299dd2014-11-09 20:22:01 -0500194 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300195 }
Victor Stinner259f0e42017-01-17 01:35:17 +0100196
197 if (!_PyArg_NoStackKeywords("lockf", kwnames)) {
198 goto exit;
199 }
Brett Cannonb7299dd2014-11-09 20:22:01 -0500200 return_value = fcntl_lockf_impl(module, fd, code, lenobj, startobj, whence);
201
202exit:
203 return return_value;
204}
Victor Stinner259f0e42017-01-17 01:35:17 +0100205/*[clinic end generated code: output=b67e9579722e6d4f input=a9049054013a1b77]*/