blob: 84a004bb59686caabafc2de9619daa99987b20a6 [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 \
22 {"fcntl", (PyCFunction)fcntl_fcntl, METH_VARARGS, fcntl_fcntl__doc__},
23
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 *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030028fcntl_fcntl(PyObject *module, PyObject *args)
Brett Cannonb7299dd2014-11-09 20:22:01 -050029{
30 PyObject *return_value = NULL;
31 int fd;
32 int code;
33 PyObject *arg = NULL;
34
Serhiy Storchaka247789c2015-04-24 00:40:51 +030035 if (!PyArg_ParseTuple(args, "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 }
Brett Cannonb7299dd2014-11-09 20:22:01 -050039 return_value = fcntl_fcntl_impl(module, fd, code, arg);
40
41exit:
42 return return_value;
43}
44
45PyDoc_STRVAR(fcntl_ioctl__doc__,
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020046"ioctl($module, fd, request, arg=0, mutate_flag=True, /)\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050047"--\n"
48"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020049"Perform the operation `request` on file descriptor `fd`.\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050050"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020051"The values used for `request` are operating system dependent, and are available\n"
52"as constants in the fcntl or termios library modules, using the same names as\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050053"used in the relevant C header files.\n"
54"\n"
55"The argument `arg` is optional, and defaults to 0; it may be an int or a\n"
56"buffer containing character data (most likely a string or an array).\n"
57"\n"
58"If the argument is a mutable buffer (such as an array) and if the\n"
59"mutate_flag argument (which is only allowed in this case) is true then the\n"
60"buffer is (in effect) passed to the operating system and changes made by\n"
61"the OS will be reflected in the contents of the buffer after the call has\n"
62"returned. The return value is the integer returned by the ioctl system\n"
63"call.\n"
64"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020065"If the argument is a mutable buffer and the mutable_flag argument is false,\n"
66"the behavior is as if a string had been passed.\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050067"\n"
68"If the argument is an immutable buffer (most likely a string) then a copy\n"
69"of the buffer is passed to the operating system and the return value is a\n"
70"string of the same length containing whatever the operating system put in\n"
71"the buffer. The length of the arg buffer in this case is not allowed to\n"
72"exceed 1024 bytes.\n"
73"\n"
74"If the arg given is an integer or if none is specified, the result value is\n"
75"an integer corresponding to the return value of the ioctl call in the C\n"
76"code.");
77
78#define FCNTL_IOCTL_METHODDEF \
79 {"ioctl", (PyCFunction)fcntl_ioctl, METH_VARARGS, fcntl_ioctl__doc__},
80
81static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030082fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
Larry Hastings89964c42015-04-14 18:07:59 -040083 PyObject *ob_arg, int mutate_arg);
Brett Cannonb7299dd2014-11-09 20:22:01 -050084
85static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030086fcntl_ioctl(PyObject *module, PyObject *args)
Brett Cannonb7299dd2014-11-09 20:22:01 -050087{
88 PyObject *return_value = NULL;
89 int fd;
90 unsigned int code;
91 PyObject *ob_arg = NULL;
92 int mutate_arg = 1;
93
Serhiy Storchaka247789c2015-04-24 00:40:51 +030094 if (!PyArg_ParseTuple(args, "O&I|Op:ioctl",
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030095 conv_descriptor, &fd, &code, &ob_arg, &mutate_arg)) {
Brett Cannonb7299dd2014-11-09 20:22:01 -050096 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030097 }
Brett Cannonb7299dd2014-11-09 20:22:01 -050098 return_value = fcntl_ioctl_impl(module, fd, code, ob_arg, mutate_arg);
99
100exit:
101 return return_value;
102}
103
104PyDoc_STRVAR(fcntl_flock__doc__,
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200105"flock($module, fd, operation, /)\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500106"--\n"
107"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200108"Perform the lock operation `operation` on file descriptor `fd`.\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500109"\n"
110"See the Unix manual page for flock(2) for details (On some systems, this\n"
111"function is emulated using fcntl()).");
112
113#define FCNTL_FLOCK_METHODDEF \
114 {"flock", (PyCFunction)fcntl_flock, METH_VARARGS, fcntl_flock__doc__},
115
116static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300117fcntl_flock_impl(PyObject *module, int fd, int code);
Brett Cannonb7299dd2014-11-09 20:22:01 -0500118
119static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300120fcntl_flock(PyObject *module, PyObject *args)
Brett Cannonb7299dd2014-11-09 20:22:01 -0500121{
122 PyObject *return_value = NULL;
123 int fd;
124 int code;
125
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300126 if (!PyArg_ParseTuple(args, "O&i:flock",
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300127 conv_descriptor, &fd, &code)) {
Brett Cannonb7299dd2014-11-09 20:22:01 -0500128 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300129 }
Brett Cannonb7299dd2014-11-09 20:22:01 -0500130 return_value = fcntl_flock_impl(module, fd, code);
131
132exit:
133 return return_value;
134}
135
136PyDoc_STRVAR(fcntl_lockf__doc__,
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200137"lockf($module, fd, cmd, len=0, start=0, whence=0, /)\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500138"--\n"
139"\n"
140"A wrapper around the fcntl() locking calls.\n"
141"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200142"`fd` is the file descriptor of the file to lock or unlock, and operation is one\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500143"of the following values:\n"
144"\n"
145" LOCK_UN - unlock\n"
146" LOCK_SH - acquire a shared lock\n"
147" LOCK_EX - acquire an exclusive lock\n"
148"\n"
149"When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with\n"
150"LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n"
Serhiy Storchakad6117a42016-04-14 12:28:01 +0300151"lock cannot be acquired, an OSError will be raised and the exception will\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500152"have an errno attribute set to EACCES or EAGAIN (depending on the operating\n"
153"system -- for portability, check for either value).\n"
154"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200155"`len` is the number of bytes to lock, with the default meaning to lock to\n"
156"EOF. `start` is the byte offset, relative to `whence`, to that the lock\n"
157"starts. `whence` is as with fileobj.seek(), specifically:\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500158"\n"
159" 0 - relative to the start of the file (SEEK_SET)\n"
160" 1 - relative to the current buffer position (SEEK_CUR)\n"
161" 2 - relative to the end of the file (SEEK_END)");
162
163#define FCNTL_LOCKF_METHODDEF \
164 {"lockf", (PyCFunction)fcntl_lockf, METH_VARARGS, fcntl_lockf__doc__},
165
166static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300167fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj,
Larry Hastings89964c42015-04-14 18:07:59 -0400168 PyObject *startobj, int whence);
Brett Cannonb7299dd2014-11-09 20:22:01 -0500169
170static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300171fcntl_lockf(PyObject *module, PyObject *args)
Brett Cannonb7299dd2014-11-09 20:22:01 -0500172{
173 PyObject *return_value = NULL;
174 int fd;
175 int code;
176 PyObject *lenobj = NULL;
177 PyObject *startobj = NULL;
178 int whence = 0;
179
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300180 if (!PyArg_ParseTuple(args, "O&i|OOi:lockf",
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300181 conv_descriptor, &fd, &code, &lenobj, &startobj, &whence)) {
Brett Cannonb7299dd2014-11-09 20:22:01 -0500182 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300183 }
Brett Cannonb7299dd2014-11-09 20:22:01 -0500184 return_value = fcntl_lockf_impl(module, fd, code, lenobj, startobj, whence);
185
186exit:
187 return return_value;
188}
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300189/*[clinic end generated code: output=36cff76a8fb2c9a6 input=a9049054013a1b77]*/