fcntlmodule.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /* fcntl module */
  2. #define PY_SSIZE_T_CLEAN
  3. #include "Python.h"
  4. #ifdef HAVE_SYS_FILE_H
  5. #include <sys/file.h>
  6. #endif
  7. #ifdef HAVE_LINUX_FS_H
  8. #include <linux/fs.h>
  9. #endif
  10. #include <sys/ioctl.h>
  11. #include <fcntl.h>
  12. #ifdef HAVE_STROPTS_H
  13. #include <stropts.h>
  14. #endif
  15. /*[clinic input]
  16. module fcntl
  17. [clinic start generated code]*/
  18. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=124b58387c158179]*/
  19. #include "clinic/fcntlmodule.c.h"
  20. /*[clinic input]
  21. fcntl.fcntl
  22. fd: fildes
  23. cmd as code: int
  24. arg: object(c_default='NULL') = 0
  25. /
  26. Perform the operation `cmd` on file descriptor fd.
  27. The values used for `cmd` are operating system dependent, and are available
  28. as constants in the fcntl module, using the same names as used in
  29. the relevant C header files. The argument arg is optional, and
  30. defaults to 0; it may be an int or a string. If arg is given as a string,
  31. the return value of fcntl is a string of that length, containing the
  32. resulting value put in the arg buffer by the operating system. The length
  33. of the arg string is not allowed to exceed 1024 bytes. If the arg given
  34. is an integer or if none is specified, the result value is an integer
  35. corresponding to the return value of the fcntl call in the C code.
  36. [clinic start generated code]*/
  37. static PyObject *
  38. fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
  39. /*[clinic end generated code: output=888fc93b51c295bd input=7955340198e5f334]*/
  40. {
  41. unsigned int int_arg = 0;
  42. int ret;
  43. char *str;
  44. Py_ssize_t len;
  45. char buf[1024];
  46. int async_err = 0;
  47. if (PySys_Audit("fcntl.fcntl", "iiO", fd, code, arg ? arg : Py_None) < 0) {
  48. return NULL;
  49. }
  50. if (arg != NULL) {
  51. int parse_result;
  52. if (PyArg_Parse(arg, "s#", &str, &len)) {
  53. if ((size_t)len > sizeof buf) {
  54. PyErr_SetString(PyExc_ValueError,
  55. "fcntl string arg too long");
  56. return NULL;
  57. }
  58. memcpy(buf, str, len);
  59. do {
  60. Py_BEGIN_ALLOW_THREADS
  61. ret = fcntl(fd, code, buf);
  62. Py_END_ALLOW_THREADS
  63. } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  64. if (ret < 0) {
  65. return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
  66. }
  67. return PyBytes_FromStringAndSize(buf, len);
  68. }
  69. PyErr_Clear();
  70. parse_result = PyArg_Parse(arg,
  71. "I;fcntl requires a file or file descriptor,"
  72. " an integer and optionally a third integer or a string",
  73. &int_arg);
  74. if (!parse_result) {
  75. return NULL;
  76. }
  77. }
  78. do {
  79. Py_BEGIN_ALLOW_THREADS
  80. ret = fcntl(fd, code, (int)int_arg);
  81. Py_END_ALLOW_THREADS
  82. } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  83. if (ret < 0) {
  84. return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
  85. }
  86. return PyLong_FromLong((long)ret);
  87. }
  88. /*[clinic input]
  89. fcntl.ioctl
  90. fd: fildes
  91. request as code: unsigned_int(bitwise=True)
  92. arg as ob_arg: object(c_default='NULL') = 0
  93. mutate_flag as mutate_arg: bool = True
  94. /
  95. Perform the operation `request` on file descriptor `fd`.
  96. The values used for `request` are operating system dependent, and are available
  97. as constants in the fcntl or termios library modules, using the same names as
  98. used in the relevant C header files.
  99. The argument `arg` is optional, and defaults to 0; it may be an int or a
  100. buffer containing character data (most likely a string or an array).
  101. If the argument is a mutable buffer (such as an array) and if the
  102. mutate_flag argument (which is only allowed in this case) is true then the
  103. buffer is (in effect) passed to the operating system and changes made by
  104. the OS will be reflected in the contents of the buffer after the call has
  105. returned. The return value is the integer returned by the ioctl system
  106. call.
  107. If the argument is a mutable buffer and the mutable_flag argument is false,
  108. the behavior is as if a string had been passed.
  109. If the argument is an immutable buffer (most likely a string) then a copy
  110. of the buffer is passed to the operating system and the return value is a
  111. string of the same length containing whatever the operating system put in
  112. the buffer. The length of the arg buffer in this case is not allowed to
  113. exceed 1024 bytes.
  114. If the arg given is an integer or if none is specified, the result value is
  115. an integer corresponding to the return value of the ioctl call in the C
  116. code.
  117. [clinic start generated code]*/
  118. static PyObject *
  119. fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
  120. PyObject *ob_arg, int mutate_arg)
  121. /*[clinic end generated code: output=7f7f5840c65991be input=967b4a4cbeceb0a8]*/
  122. {
  123. #define IOCTL_BUFSZ 1024
  124. /* We use the unsigned non-checked 'I' format for the 'code' parameter
  125. because the system expects it to be a 32bit bit field value
  126. regardless of it being passed as an int or unsigned long on
  127. various platforms. See the termios.TIOCSWINSZ constant across
  128. platforms for an example of this.
  129. If any of the 64bit platforms ever decide to use more than 32bits
  130. in their unsigned long ioctl codes this will break and need
  131. special casing based on the platform being built on.
  132. */
  133. int arg = 0;
  134. int ret;
  135. Py_buffer pstr;
  136. char *str;
  137. Py_ssize_t len;
  138. char buf[IOCTL_BUFSZ+1]; /* argument plus NUL byte */
  139. if (PySys_Audit("fcntl.ioctl", "iIO", fd, code,
  140. ob_arg ? ob_arg : Py_None) < 0) {
  141. return NULL;
  142. }
  143. if (ob_arg != NULL) {
  144. if (PyArg_Parse(ob_arg, "w*:ioctl", &pstr)) {
  145. char *arg;
  146. str = pstr.buf;
  147. len = pstr.len;
  148. if (mutate_arg) {
  149. if (len <= IOCTL_BUFSZ) {
  150. memcpy(buf, str, len);
  151. buf[len] = '\0';
  152. arg = buf;
  153. }
  154. else {
  155. arg = str;
  156. }
  157. }
  158. else {
  159. if (len > IOCTL_BUFSZ) {
  160. PyBuffer_Release(&pstr);
  161. PyErr_SetString(PyExc_ValueError,
  162. "ioctl string arg too long");
  163. return NULL;
  164. }
  165. else {
  166. memcpy(buf, str, len);
  167. buf[len] = '\0';
  168. arg = buf;
  169. }
  170. }
  171. if (buf == arg) {
  172. Py_BEGIN_ALLOW_THREADS /* think array.resize() */
  173. ret = ioctl(fd, code, arg);
  174. Py_END_ALLOW_THREADS
  175. }
  176. else {
  177. ret = ioctl(fd, code, arg);
  178. }
  179. if (mutate_arg && (len <= IOCTL_BUFSZ)) {
  180. memcpy(str, buf, len);
  181. }
  182. if (ret < 0) {
  183. PyErr_SetFromErrno(PyExc_OSError);
  184. PyBuffer_Release(&pstr);
  185. return NULL;
  186. }
  187. PyBuffer_Release(&pstr);
  188. if (mutate_arg) {
  189. return PyLong_FromLong(ret);
  190. }
  191. else {
  192. return PyBytes_FromStringAndSize(buf, len);
  193. }
  194. }
  195. PyErr_Clear();
  196. if (PyArg_Parse(ob_arg, "s*:ioctl", &pstr)) {
  197. str = pstr.buf;
  198. len = pstr.len;
  199. if (len > IOCTL_BUFSZ) {
  200. PyBuffer_Release(&pstr);
  201. PyErr_SetString(PyExc_ValueError,
  202. "ioctl string arg too long");
  203. return NULL;
  204. }
  205. memcpy(buf, str, len);
  206. buf[len] = '\0';
  207. Py_BEGIN_ALLOW_THREADS
  208. ret = ioctl(fd, code, buf);
  209. Py_END_ALLOW_THREADS
  210. if (ret < 0) {
  211. PyErr_SetFromErrno(PyExc_OSError);
  212. PyBuffer_Release(&pstr);
  213. return NULL;
  214. }
  215. PyBuffer_Release(&pstr);
  216. return PyBytes_FromStringAndSize(buf, len);
  217. }
  218. PyErr_Clear();
  219. if (!PyArg_Parse(ob_arg,
  220. "i;ioctl requires a file or file descriptor,"
  221. " an integer and optionally an integer or buffer argument",
  222. &arg)) {
  223. return NULL;
  224. }
  225. // Fall-through to outside the 'if' statement.
  226. }
  227. Py_BEGIN_ALLOW_THREADS
  228. ret = ioctl(fd, code, arg);
  229. Py_END_ALLOW_THREADS
  230. if (ret < 0) {
  231. PyErr_SetFromErrno(PyExc_OSError);
  232. return NULL;
  233. }
  234. return PyLong_FromLong((long)ret);
  235. #undef IOCTL_BUFSZ
  236. }
  237. /*[clinic input]
  238. fcntl.flock
  239. fd: fildes
  240. operation as code: int
  241. /
  242. Perform the lock operation `operation` on file descriptor `fd`.
  243. See the Unix manual page for flock(2) for details (On some systems, this
  244. function is emulated using fcntl()).
  245. [clinic start generated code]*/
  246. static PyObject *
  247. fcntl_flock_impl(PyObject *module, int fd, int code)
  248. /*[clinic end generated code: output=84059e2b37d2fc64 input=0bfc00f795953452]*/
  249. {
  250. int ret;
  251. int async_err = 0;
  252. if (PySys_Audit("fcntl.flock", "ii", fd, code) < 0) {
  253. return NULL;
  254. }
  255. #ifdef HAVE_FLOCK
  256. do {
  257. Py_BEGIN_ALLOW_THREADS
  258. ret = flock(fd, code);
  259. Py_END_ALLOW_THREADS
  260. } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  261. #else
  262. #ifndef LOCK_SH
  263. #define LOCK_SH 1 /* shared lock */
  264. #define LOCK_EX 2 /* exclusive lock */
  265. #define LOCK_NB 4 /* don't block when locking */
  266. #define LOCK_UN 8 /* unlock */
  267. #endif
  268. {
  269. struct flock l;
  270. if (code == LOCK_UN)
  271. l.l_type = F_UNLCK;
  272. else if (code & LOCK_SH)
  273. l.l_type = F_RDLCK;
  274. else if (code & LOCK_EX)
  275. l.l_type = F_WRLCK;
  276. else {
  277. PyErr_SetString(PyExc_ValueError,
  278. "unrecognized flock argument");
  279. return NULL;
  280. }
  281. l.l_whence = l.l_start = l.l_len = 0;
  282. do {
  283. Py_BEGIN_ALLOW_THREADS
  284. ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
  285. Py_END_ALLOW_THREADS
  286. } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  287. }
  288. #endif /* HAVE_FLOCK */
  289. if (ret < 0) {
  290. return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
  291. }
  292. Py_RETURN_NONE;
  293. }
  294. /*[clinic input]
  295. fcntl.lockf
  296. fd: fildes
  297. cmd as code: int
  298. len as lenobj: object(c_default='NULL') = 0
  299. start as startobj: object(c_default='NULL') = 0
  300. whence: int = 0
  301. /
  302. A wrapper around the fcntl() locking calls.
  303. `fd` is the file descriptor of the file to lock or unlock, and operation is one
  304. of the following values:
  305. LOCK_UN - unlock
  306. LOCK_SH - acquire a shared lock
  307. LOCK_EX - acquire an exclusive lock
  308. When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with
  309. LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the
  310. lock cannot be acquired, an OSError will be raised and the exception will
  311. have an errno attribute set to EACCES or EAGAIN (depending on the operating
  312. system -- for portability, check for either value).
  313. `len` is the number of bytes to lock, with the default meaning to lock to
  314. EOF. `start` is the byte offset, relative to `whence`, to that the lock
  315. starts. `whence` is as with fileobj.seek(), specifically:
  316. 0 - relative to the start of the file (SEEK_SET)
  317. 1 - relative to the current buffer position (SEEK_CUR)
  318. 2 - relative to the end of the file (SEEK_END)
  319. [clinic start generated code]*/
  320. static PyObject *
  321. fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj,
  322. PyObject *startobj, int whence)
  323. /*[clinic end generated code: output=4985e7a172e7461a input=5480479fc63a04b8]*/
  324. {
  325. int ret;
  326. int async_err = 0;
  327. if (PySys_Audit("fcntl.lockf", "iiOOi", fd, code, lenobj ? lenobj : Py_None,
  328. startobj ? startobj : Py_None, whence) < 0) {
  329. return NULL;
  330. }
  331. #ifndef LOCK_SH
  332. #define LOCK_SH 1 /* shared lock */
  333. #define LOCK_EX 2 /* exclusive lock */
  334. #define LOCK_NB 4 /* don't block when locking */
  335. #define LOCK_UN 8 /* unlock */
  336. #endif /* LOCK_SH */
  337. {
  338. struct flock l;
  339. if (code == LOCK_UN)
  340. l.l_type = F_UNLCK;
  341. else if (code & LOCK_SH)
  342. l.l_type = F_RDLCK;
  343. else if (code & LOCK_EX)
  344. l.l_type = F_WRLCK;
  345. else {
  346. PyErr_SetString(PyExc_ValueError,
  347. "unrecognized lockf argument");
  348. return NULL;
  349. }
  350. l.l_start = l.l_len = 0;
  351. if (startobj != NULL) {
  352. #if !defined(HAVE_LARGEFILE_SUPPORT)
  353. l.l_start = PyLong_AsLong(startobj);
  354. #else
  355. l.l_start = PyLong_Check(startobj) ?
  356. PyLong_AsLongLong(startobj) :
  357. PyLong_AsLong(startobj);
  358. #endif
  359. if (PyErr_Occurred())
  360. return NULL;
  361. }
  362. if (lenobj != NULL) {
  363. #if !defined(HAVE_LARGEFILE_SUPPORT)
  364. l.l_len = PyLong_AsLong(lenobj);
  365. #else
  366. l.l_len = PyLong_Check(lenobj) ?
  367. PyLong_AsLongLong(lenobj) :
  368. PyLong_AsLong(lenobj);
  369. #endif
  370. if (PyErr_Occurred())
  371. return NULL;
  372. }
  373. l.l_whence = whence;
  374. do {
  375. Py_BEGIN_ALLOW_THREADS
  376. ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
  377. Py_END_ALLOW_THREADS
  378. } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  379. }
  380. if (ret < 0) {
  381. return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
  382. }
  383. Py_RETURN_NONE;
  384. }
  385. /* List of functions */
  386. static PyMethodDef fcntl_methods[] = {
  387. FCNTL_FCNTL_METHODDEF
  388. FCNTL_IOCTL_METHODDEF
  389. FCNTL_FLOCK_METHODDEF
  390. FCNTL_LOCKF_METHODDEF
  391. {NULL, NULL} /* sentinel */
  392. };
  393. PyDoc_STRVAR(module_doc,
  394. "This module performs file control and I/O control on file\n\
  395. descriptors. It is an interface to the fcntl() and ioctl() Unix\n\
  396. routines. File descriptors can be obtained with the fileno() method of\n\
  397. a file or socket object.");
  398. /* Module initialisation */
  399. static int
  400. all_ins(PyObject* m)
  401. {
  402. if (PyModule_AddIntMacro(m, LOCK_SH)) return -1;
  403. if (PyModule_AddIntMacro(m, LOCK_EX)) return -1;
  404. if (PyModule_AddIntMacro(m, LOCK_NB)) return -1;
  405. if (PyModule_AddIntMacro(m, LOCK_UN)) return -1;
  406. /* GNU extensions, as of glibc 2.2.4 */
  407. #ifdef LOCK_MAND
  408. if (PyModule_AddIntMacro(m, LOCK_MAND)) return -1;
  409. #endif
  410. #ifdef LOCK_READ
  411. if (PyModule_AddIntMacro(m, LOCK_READ)) return -1;
  412. #endif
  413. #ifdef LOCK_WRITE
  414. if (PyModule_AddIntMacro(m, LOCK_WRITE)) return -1;
  415. #endif
  416. #ifdef LOCK_RW
  417. if (PyModule_AddIntMacro(m, LOCK_RW)) return -1;
  418. #endif
  419. #ifdef F_DUPFD
  420. if (PyModule_AddIntMacro(m, F_DUPFD)) return -1;
  421. #endif
  422. #ifdef F_DUPFD_CLOEXEC
  423. if (PyModule_AddIntMacro(m, F_DUPFD_CLOEXEC)) return -1;
  424. #endif
  425. #ifdef F_GETFD
  426. if (PyModule_AddIntMacro(m, F_GETFD)) return -1;
  427. #endif
  428. #ifdef F_SETFD
  429. if (PyModule_AddIntMacro(m, F_SETFD)) return -1;
  430. #endif
  431. #ifdef F_GETFL
  432. if (PyModule_AddIntMacro(m, F_GETFL)) return -1;
  433. #endif
  434. #ifdef F_SETFL
  435. if (PyModule_AddIntMacro(m, F_SETFL)) return -1;
  436. #endif
  437. #ifdef F_GETLK
  438. if (PyModule_AddIntMacro(m, F_GETLK)) return -1;
  439. #endif
  440. #ifdef F_SETLK
  441. if (PyModule_AddIntMacro(m, F_SETLK)) return -1;
  442. #endif
  443. #ifdef F_SETLKW
  444. if (PyModule_AddIntMacro(m, F_SETLKW)) return -1;
  445. #endif
  446. #ifdef F_OFD_GETLK
  447. if (PyModule_AddIntMacro(m, F_OFD_GETLK)) return -1;
  448. #endif
  449. #ifdef F_OFD_SETLK
  450. if (PyModule_AddIntMacro(m, F_OFD_SETLK)) return -1;
  451. #endif
  452. #ifdef F_OFD_SETLKW
  453. if (PyModule_AddIntMacro(m, F_OFD_SETLKW)) return -1;
  454. #endif
  455. #ifdef F_GETOWN
  456. if (PyModule_AddIntMacro(m, F_GETOWN)) return -1;
  457. #endif
  458. #ifdef F_SETOWN
  459. if (PyModule_AddIntMacro(m, F_SETOWN)) return -1;
  460. #endif
  461. #ifdef F_GETPATH
  462. if (PyModule_AddIntMacro(m, F_GETPATH)) return -1;
  463. #endif
  464. #ifdef F_GETSIG
  465. if (PyModule_AddIntMacro(m, F_GETSIG)) return -1;
  466. #endif
  467. #ifdef F_SETSIG
  468. if (PyModule_AddIntMacro(m, F_SETSIG)) return -1;
  469. #endif
  470. #ifdef F_RDLCK
  471. if (PyModule_AddIntMacro(m, F_RDLCK)) return -1;
  472. #endif
  473. #ifdef F_WRLCK
  474. if (PyModule_AddIntMacro(m, F_WRLCK)) return -1;
  475. #endif
  476. #ifdef F_UNLCK
  477. if (PyModule_AddIntMacro(m, F_UNLCK)) return -1;
  478. #endif
  479. /* LFS constants */
  480. #ifdef F_GETLK64
  481. if (PyModule_AddIntMacro(m, F_GETLK64)) return -1;
  482. #endif
  483. #ifdef F_SETLK64
  484. if (PyModule_AddIntMacro(m, F_SETLK64)) return -1;
  485. #endif
  486. #ifdef F_SETLKW64
  487. if (PyModule_AddIntMacro(m, F_SETLKW64)) return -1;
  488. #endif
  489. /* GNU extensions, as of glibc 2.2.4. */
  490. #ifdef FASYNC
  491. if (PyModule_AddIntMacro(m, FASYNC)) return -1;
  492. #endif
  493. #ifdef F_SETLEASE
  494. if (PyModule_AddIntMacro(m, F_SETLEASE)) return -1;
  495. #endif
  496. #ifdef F_GETLEASE
  497. if (PyModule_AddIntMacro(m, F_GETLEASE)) return -1;
  498. #endif
  499. #ifdef F_NOTIFY
  500. if (PyModule_AddIntMacro(m, F_NOTIFY)) return -1;
  501. #endif
  502. /* Old BSD flock(). */
  503. #ifdef F_EXLCK
  504. if (PyModule_AddIntMacro(m, F_EXLCK)) return -1;
  505. #endif
  506. #ifdef F_SHLCK
  507. if (PyModule_AddIntMacro(m, F_SHLCK)) return -1;
  508. #endif
  509. /* Linux specifics */
  510. #ifdef F_SETPIPE_SZ
  511. if (PyModule_AddIntMacro(m, F_SETPIPE_SZ)) return -1;
  512. #endif
  513. #ifdef F_GETPIPE_SZ
  514. if (PyModule_AddIntMacro(m, F_GETPIPE_SZ)) return -1;
  515. #endif
  516. #ifdef FICLONE
  517. if (PyModule_AddIntMacro(m, FICLONE)) return -1;
  518. #endif
  519. #ifdef FICLONERANGE
  520. if (PyModule_AddIntMacro(m, FICLONERANGE)) return -1;
  521. #endif
  522. /* OS X specifics */
  523. #ifdef F_FULLFSYNC
  524. if (PyModule_AddIntMacro(m, F_FULLFSYNC)) return -1;
  525. #endif
  526. #ifdef F_NOCACHE
  527. if (PyModule_AddIntMacro(m, F_NOCACHE)) return -1;
  528. #endif
  529. /* FreeBSD specifics */
  530. #ifdef F_DUP2FD
  531. if (PyModule_AddIntMacro(m, F_DUP2FD)) return -1;
  532. #endif
  533. #ifdef F_DUP2FD_CLOEXEC
  534. if (PyModule_AddIntMacro(m, F_DUP2FD_CLOEXEC)) return -1;
  535. #endif
  536. /* For F_{GET|SET}FL */
  537. #ifdef FD_CLOEXEC
  538. if (PyModule_AddIntMacro(m, FD_CLOEXEC)) return -1;
  539. #endif
  540. /* For F_NOTIFY */
  541. #ifdef DN_ACCESS
  542. if (PyModule_AddIntMacro(m, DN_ACCESS)) return -1;
  543. #endif
  544. #ifdef DN_MODIFY
  545. if (PyModule_AddIntMacro(m, DN_MODIFY)) return -1;
  546. #endif
  547. #ifdef DN_CREATE
  548. if (PyModule_AddIntMacro(m, DN_CREATE)) return -1;
  549. #endif
  550. #ifdef DN_DELETE
  551. if (PyModule_AddIntMacro(m, DN_DELETE)) return -1;
  552. #endif
  553. #ifdef DN_RENAME
  554. if (PyModule_AddIntMacro(m, DN_RENAME)) return -1;
  555. #endif
  556. #ifdef DN_ATTRIB
  557. if (PyModule_AddIntMacro(m, DN_ATTRIB)) return -1;
  558. #endif
  559. #ifdef DN_MULTISHOT
  560. if (PyModule_AddIntMacro(m, DN_MULTISHOT)) return -1;
  561. #endif
  562. #ifdef HAVE_STROPTS_H
  563. /* Unix 98 guarantees that these are in stropts.h. */
  564. if (PyModule_AddIntMacro(m, I_PUSH)) return -1;
  565. if (PyModule_AddIntMacro(m, I_POP)) return -1;
  566. if (PyModule_AddIntMacro(m, I_LOOK)) return -1;
  567. if (PyModule_AddIntMacro(m, I_FLUSH)) return -1;
  568. if (PyModule_AddIntMacro(m, I_FLUSHBAND)) return -1;
  569. if (PyModule_AddIntMacro(m, I_SETSIG)) return -1;
  570. if (PyModule_AddIntMacro(m, I_GETSIG)) return -1;
  571. if (PyModule_AddIntMacro(m, I_FIND)) return -1;
  572. if (PyModule_AddIntMacro(m, I_PEEK)) return -1;
  573. if (PyModule_AddIntMacro(m, I_SRDOPT)) return -1;
  574. if (PyModule_AddIntMacro(m, I_GRDOPT)) return -1;
  575. if (PyModule_AddIntMacro(m, I_NREAD)) return -1;
  576. if (PyModule_AddIntMacro(m, I_FDINSERT)) return -1;
  577. if (PyModule_AddIntMacro(m, I_STR)) return -1;
  578. if (PyModule_AddIntMacro(m, I_SWROPT)) return -1;
  579. #ifdef I_GWROPT
  580. /* despite the comment above, old-ish glibcs miss a couple... */
  581. if (PyModule_AddIntMacro(m, I_GWROPT)) return -1;
  582. #endif
  583. if (PyModule_AddIntMacro(m, I_SENDFD)) return -1;
  584. if (PyModule_AddIntMacro(m, I_RECVFD)) return -1;
  585. if (PyModule_AddIntMacro(m, I_LIST)) return -1;
  586. if (PyModule_AddIntMacro(m, I_ATMARK)) return -1;
  587. if (PyModule_AddIntMacro(m, I_CKBAND)) return -1;
  588. if (PyModule_AddIntMacro(m, I_GETBAND)) return -1;
  589. if (PyModule_AddIntMacro(m, I_CANPUT)) return -1;
  590. if (PyModule_AddIntMacro(m, I_SETCLTIME)) return -1;
  591. #ifdef I_GETCLTIME
  592. if (PyModule_AddIntMacro(m, I_GETCLTIME)) return -1;
  593. #endif
  594. if (PyModule_AddIntMacro(m, I_LINK)) return -1;
  595. if (PyModule_AddIntMacro(m, I_UNLINK)) return -1;
  596. if (PyModule_AddIntMacro(m, I_PLINK)) return -1;
  597. if (PyModule_AddIntMacro(m, I_PUNLINK)) return -1;
  598. #endif
  599. #ifdef F_ADD_SEALS
  600. /* Linux: file sealing for memfd_create() */
  601. if (PyModule_AddIntMacro(m, F_ADD_SEALS)) return -1;
  602. if (PyModule_AddIntMacro(m, F_GET_SEALS)) return -1;
  603. if (PyModule_AddIntMacro(m, F_SEAL_SEAL)) return -1;
  604. if (PyModule_AddIntMacro(m, F_SEAL_SHRINK)) return -1;
  605. if (PyModule_AddIntMacro(m, F_SEAL_GROW)) return -1;
  606. if (PyModule_AddIntMacro(m, F_SEAL_WRITE)) return -1;
  607. #endif
  608. return 0;
  609. }
  610. static int
  611. fcntl_exec(PyObject *module)
  612. {
  613. if (all_ins(module) < 0) {
  614. return -1;
  615. }
  616. return 0;
  617. }
  618. static PyModuleDef_Slot fcntl_slots[] = {
  619. {Py_mod_exec, fcntl_exec},
  620. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  621. {0, NULL}
  622. };
  623. static struct PyModuleDef fcntlmodule = {
  624. PyModuleDef_HEAD_INIT,
  625. .m_name = "fcntl",
  626. .m_doc = module_doc,
  627. .m_size = 0,
  628. .m_methods = fcntl_methods,
  629. .m_slots = fcntl_slots,
  630. };
  631. PyMODINIT_FUNC
  632. PyInit_fcntl(void)
  633. {
  634. return PyModuleDef_Init(&fcntlmodule);
  635. }