blob: 5e3b1c06f69a19b132ad5cfbf71324055755c06f [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 *
25fcntl_fcntl_impl(PyModuleDef *module, int fd, int code, PyObject *arg);
26
27static PyObject *
28fcntl_fcntl(PyModuleDef *module, PyObject *args)
29{
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",
Brett Cannonb7299dd2014-11-09 20:22:01 -050036 conv_descriptor, &fd, &code, &arg))
37 goto exit;
38 return_value = fcntl_fcntl_impl(module, fd, code, arg);
39
40exit:
41 return return_value;
42}
43
44PyDoc_STRVAR(fcntl_ioctl__doc__,
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020045"ioctl($module, fd, request, arg=0, mutate_flag=True, /)\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050046"--\n"
47"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020048"Perform the operation `request` on file descriptor `fd`.\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050049"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020050"The values used for `request` are operating system dependent, and are available\n"
51"as constants in the fcntl or termios library modules, using the same names as\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050052"used in the relevant C header files.\n"
53"\n"
54"The argument `arg` is optional, and defaults to 0; it may be an int or a\n"
55"buffer containing character data (most likely a string or an array).\n"
56"\n"
57"If the argument is a mutable buffer (such as an array) and if the\n"
58"mutate_flag argument (which is only allowed in this case) is true then the\n"
59"buffer is (in effect) passed to the operating system and changes made by\n"
60"the OS will be reflected in the contents of the buffer after the call has\n"
61"returned. The return value is the integer returned by the ioctl system\n"
62"call.\n"
63"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +020064"If the argument is a mutable buffer and the mutable_flag argument is false,\n"
65"the behavior is as if a string had been passed.\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -050066"\n"
67"If the argument is an immutable buffer (most likely a string) then a copy\n"
68"of the buffer is passed to the operating system and the return value is a\n"
69"string of the same length containing whatever the operating system put in\n"
70"the buffer. The length of the arg buffer in this case is not allowed to\n"
71"exceed 1024 bytes.\n"
72"\n"
73"If the arg given is an integer or if none is specified, the result value is\n"
74"an integer corresponding to the return value of the ioctl call in the C\n"
75"code.");
76
77#define FCNTL_IOCTL_METHODDEF \
78 {"ioctl", (PyCFunction)fcntl_ioctl, METH_VARARGS, fcntl_ioctl__doc__},
79
80static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -040081fcntl_ioctl_impl(PyModuleDef *module, int fd, unsigned int code,
82 PyObject *ob_arg, int mutate_arg);
Brett Cannonb7299dd2014-11-09 20:22:01 -050083
84static PyObject *
85fcntl_ioctl(PyModuleDef *module, PyObject *args)
86{
87 PyObject *return_value = NULL;
88 int fd;
89 unsigned int code;
90 PyObject *ob_arg = NULL;
91 int mutate_arg = 1;
92
Serhiy Storchaka247789c2015-04-24 00:40:51 +030093 if (!PyArg_ParseTuple(args, "O&I|Op:ioctl",
Brett Cannonb7299dd2014-11-09 20:22:01 -050094 conv_descriptor, &fd, &code, &ob_arg, &mutate_arg))
95 goto exit;
96 return_value = fcntl_ioctl_impl(module, fd, code, ob_arg, mutate_arg);
97
98exit:
99 return return_value;
100}
101
102PyDoc_STRVAR(fcntl_flock__doc__,
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200103"flock($module, fd, operation, /)\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500104"--\n"
105"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200106"Perform the lock operation `operation` on file descriptor `fd`.\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500107"\n"
108"See the Unix manual page for flock(2) for details (On some systems, this\n"
109"function is emulated using fcntl()).");
110
111#define FCNTL_FLOCK_METHODDEF \
112 {"flock", (PyCFunction)fcntl_flock, METH_VARARGS, fcntl_flock__doc__},
113
114static PyObject *
115fcntl_flock_impl(PyModuleDef *module, int fd, int code);
116
117static PyObject *
118fcntl_flock(PyModuleDef *module, PyObject *args)
119{
120 PyObject *return_value = NULL;
121 int fd;
122 int code;
123
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300124 if (!PyArg_ParseTuple(args, "O&i:flock",
Brett Cannonb7299dd2014-11-09 20:22:01 -0500125 conv_descriptor, &fd, &code))
126 goto exit;
127 return_value = fcntl_flock_impl(module, fd, code);
128
129exit:
130 return return_value;
131}
132
133PyDoc_STRVAR(fcntl_lockf__doc__,
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200134"lockf($module, fd, cmd, len=0, start=0, whence=0, /)\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500135"--\n"
136"\n"
137"A wrapper around the fcntl() locking calls.\n"
138"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200139"`fd` is the file descriptor of the file to lock or unlock, and operation is one\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500140"of the following values:\n"
141"\n"
142" LOCK_UN - unlock\n"
143" LOCK_SH - acquire a shared lock\n"
144" LOCK_EX - acquire an exclusive lock\n"
145"\n"
146"When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with\n"
147"LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n"
148"lock cannot be acquired, an IOError will be raised and the exception will\n"
149"have an errno attribute set to EACCES or EAGAIN (depending on the operating\n"
150"system -- for portability, check for either value).\n"
151"\n"
Serhiy Storchaka17d3a582015-03-20 20:04:21 +0200152"`len` is the number of bytes to lock, with the default meaning to lock to\n"
153"EOF. `start` is the byte offset, relative to `whence`, to that the lock\n"
154"starts. `whence` is as with fileobj.seek(), specifically:\n"
Brett Cannonb7299dd2014-11-09 20:22:01 -0500155"\n"
156" 0 - relative to the start of the file (SEEK_SET)\n"
157" 1 - relative to the current buffer position (SEEK_CUR)\n"
158" 2 - relative to the end of the file (SEEK_END)");
159
160#define FCNTL_LOCKF_METHODDEF \
161 {"lockf", (PyCFunction)fcntl_lockf, METH_VARARGS, fcntl_lockf__doc__},
162
163static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400164fcntl_lockf_impl(PyModuleDef *module, int fd, int code, PyObject *lenobj,
165 PyObject *startobj, int whence);
Brett Cannonb7299dd2014-11-09 20:22:01 -0500166
167static PyObject *
168fcntl_lockf(PyModuleDef *module, PyObject *args)
169{
170 PyObject *return_value = NULL;
171 int fd;
172 int code;
173 PyObject *lenobj = NULL;
174 PyObject *startobj = NULL;
175 int whence = 0;
176
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300177 if (!PyArg_ParseTuple(args, "O&i|OOi:lockf",
Brett Cannonb7299dd2014-11-09 20:22:01 -0500178 conv_descriptor, &fd, &code, &lenobj, &startobj, &whence))
179 goto exit;
180 return_value = fcntl_lockf_impl(module, fd, code, lenobj, startobj, whence);
181
182exit:
183 return return_value;
184}
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300185/*[clinic end generated code: output=92963b631d00f0fe input=a9049054013a1b77]*/