blob: 89f9757c515bb80436d81f4f8da7768421b49f9f [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Jesus Ceaab70e2a2012-10-05 01:48:08 +02004/* This file is also used for Windows NT/MS-Win. In that case the
5 module actually calls itself 'nt', not 'posix', and a few
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00006 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Victor Stinnerf427a142014-10-22 12:33:23 +02009 test macro, e.g. '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010
Larry Hastings31826802013-10-19 00:09:25 -070011
12
Thomas Wouters477c8d52006-05-27 19:21:47 +000013#ifdef __APPLE__
14 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000015 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000016 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
17 * at the end of this file for more information.
18 */
19# pragma weak lchown
20# pragma weak statvfs
21# pragma weak fstatvfs
22
23#endif /* __APPLE__ */
24
Thomas Wouters68bc4f92006-03-01 01:05:10 +000025#define PY_SSIZE_T_CLEAN
26
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000027#include "Python.h"
Victor Stinnerd5d9e812019-05-13 12:35:37 +020028#ifdef MS_WINDOWS
29 /* include <windows.h> early to avoid conflict with pycore_condvar.h:
30
31 #define WIN32_LEAN_AND_MEAN
32 #include <windows.h>
33
34 FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */
35# include <windows.h>
36#endif
37
Victor Stinner4a21e572020-04-15 02:35:41 +020038#include "pycore_ceval.h" // _PyEval_ReInitThreads()
39#include "pycore_import.h" // _PyImport_ReInitLock()
40#include "pycore_pystate.h" // _PyInterpreterState_GET()
41#include "structmember.h" // PyMemberDef
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020042#ifndef MS_WINDOWS
Victor Stinnerd5d9e812019-05-13 12:35:37 +020043# include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010044#else
Victor Stinnerd5d9e812019-05-13 12:35:37 +020045# include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020046#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000047
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020048/* On android API level 21, 'AT_EACCESS' is not declared although
49 * HAVE_FACCESSAT is defined. */
50#ifdef __ANDROID__
51#undef HAVE_FACCESSAT
52#endif
53
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000054#include <stdio.h> /* needed for ctermid() */
55
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056#ifdef __cplusplus
57extern "C" {
58#endif
59
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000060PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000061"This module provides access to operating system functionality that is\n\
62standardized by the C Standard and the POSIX standard (a thinly\n\
63disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000064corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000066
Ross Lagerwall4d076da2011-03-18 06:56:53 +020067#ifdef HAVE_SYS_UIO_H
68#include <sys/uio.h>
69#endif
70
Christian Heimes75b96182017-09-05 15:53:09 +020071#ifdef HAVE_SYS_SYSMACROS_H
72/* GNU C Library: major(), minor(), makedev() */
73#include <sys/sysmacros.h>
74#endif
75
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000077#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#endif /* HAVE_SYS_TYPES_H */
79
80#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000081#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083
Guido van Rossum36bc6801995-06-14 22:54:23 +000084#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000085#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000086#endif
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080087#ifdef HAVE_LINUX_WAIT_H
88#include <linux/wait.h> // For P_PIDFD
89#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000090
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000092#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000094
Guido van Rossumb6775db1994-08-01 11:34:53 +000095#ifdef HAVE_FCNTL_H
96#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000097#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000098
Guido van Rossuma6535fd2001-10-18 19:44:10 +000099#ifdef HAVE_GRP_H
100#include <grp.h>
101#endif
102
Barry Warsaw5676bd12003-01-07 20:57:09 +0000103#ifdef HAVE_SYSEXITS_H
104#include <sysexits.h>
105#endif /* HAVE_SYSEXITS_H */
106
Anthony Baxter8a560de2004-10-13 15:30:56 +0000107#ifdef HAVE_SYS_LOADAVG_H
108#include <sys/loadavg.h>
109#endif
110
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000111#ifdef HAVE_SYS_SENDFILE_H
112#include <sys/sendfile.h>
113#endif
114
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200115#if defined(__APPLE__)
116#include <copyfile.h>
117#endif
118
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500119#ifdef HAVE_SCHED_H
120#include <sched.h>
121#endif
122
Pablo Galindoaac4d032019-05-31 19:39:47 +0100123#ifdef HAVE_COPY_FILE_RANGE
124#include <unistd.h>
125#endif
126
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500127#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500128#undef HAVE_SCHED_SETAFFINITY
129#endif
130
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200131#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400132#define USE_XATTRS
133#endif
134
135#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400136#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400137#endif
138
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000139#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
140#ifdef HAVE_SYS_SOCKET_H
141#include <sys/socket.h>
142#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000143#endif
144
Victor Stinner8b905bd2011-10-25 13:34:04 +0200145#ifdef HAVE_DLFCN_H
146#include <dlfcn.h>
147#endif
148
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200149#ifdef __hpux
150#include <sys/mpctl.h>
151#endif
152
153#if defined(__DragonFly__) || \
154 defined(__OpenBSD__) || \
155 defined(__FreeBSD__) || \
156 defined(__NetBSD__) || \
157 defined(__APPLE__)
158#include <sys/sysctl.h>
159#endif
160
Victor Stinner9b1f4742016-09-06 16:18:52 -0700161#ifdef HAVE_LINUX_RANDOM_H
162# include <linux/random.h>
163#endif
164#ifdef HAVE_GETRANDOM_SYSCALL
165# include <sys/syscall.h>
166#endif
167
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100168#if defined(MS_WINDOWS)
169# define TERMSIZE_USE_CONIO
170#elif defined(HAVE_SYS_IOCTL_H)
171# include <sys/ioctl.h>
172# if defined(HAVE_TERMIOS_H)
173# include <termios.h>
174# endif
175# if defined(TIOCGWINSZ)
176# define TERMSIZE_USE_IOCTL
177# endif
178#endif /* MS_WINDOWS */
179
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000181/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000182#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000183#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000184#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000185#include <process.h>
186#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000187#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000188#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000189#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000190#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000191#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700192#define HAVE_WSPAWNV 1
193#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000194#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000195#define HAVE_SYSTEM 1
196#define HAVE_CWAIT 1
197#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000198#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000199#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000200/* Unix functions that the configure script doesn't check for */
pxinwrf2d7ac72019-05-21 18:46:37 +0800201#ifndef __VXWORKS__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000202#define HAVE_EXECV 1
203#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000204#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000205#define HAVE_FORK1 1
206#endif
pxinwrf2d7ac72019-05-21 18:46:37 +0800207#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000208#define HAVE_GETEGID 1
209#define HAVE_GETEUID 1
210#define HAVE_GETGID 1
211#define HAVE_GETPPID 1
212#define HAVE_GETUID 1
213#define HAVE_KILL 1
214#define HAVE_OPENDIR 1
215#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000216#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000217#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000218#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000219#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000220#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000221
Eddie Elizondob3966632019-11-05 07:16:14 -0800222_Py_IDENTIFIER(__fspath__);
Victor Stinnera2f7c002012-02-08 03:36:25 +0100223
Larry Hastings61272b72014-01-07 12:41:53 -0800224/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000225# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800226module os
Larry Hastings61272b72014-01-07 12:41:53 -0800227[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000228/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100229
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000230#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000231
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000232#if defined(__sgi)&&_COMPILER_VERSION>=700
233/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
234 (default) */
235extern char *ctermid_r(char *);
236#endif
237
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239
pxinwrf2d7ac72019-05-21 18:46:37 +0800240#if defined(__VXWORKS__)
241#include <vxCpuLib.h>
242#include <rtpLib.h>
243#include <wait.h>
244#include <taskLib.h>
245#ifndef _P_WAIT
246#define _P_WAIT 0
247#define _P_NOWAIT 1
248#define _P_NOWAITO 1
249#endif
250#endif /* __VXWORKS__ */
251
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000252#ifdef HAVE_POSIX_SPAWN
253#include <spawn.h>
254#endif
255
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256#ifdef HAVE_UTIME_H
257#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000258#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000260#ifdef HAVE_SYS_UTIME_H
261#include <sys/utime.h>
262#define HAVE_UTIME_H /* pretend we do for the rest of this file */
263#endif /* HAVE_SYS_UTIME_H */
264
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265#ifdef HAVE_SYS_TIMES_H
266#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000267#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268
269#ifdef HAVE_SYS_PARAM_H
270#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000271#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272
273#ifdef HAVE_SYS_UTSNAME_H
274#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000275#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000276
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000277#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000279#define NAMLEN(dirent) strlen((dirent)->d_name)
280#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000281#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000282#include <direct.h>
283#define NAMLEN(dirent) strlen((dirent)->d_name)
284#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000286#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000287#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000288#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000289#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000290#endif
291#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000292#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000293#endif
294#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000296#endif
297#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000299#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000300#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302#endif
303#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000304#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000305#endif
306#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000308#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000309#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000310#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000311#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100312#ifndef IO_REPARSE_TAG_MOUNT_POINT
313#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
314#endif
Victor Stinner361dcdc2020-04-15 03:24:57 +0200315#include "osdefs.h" // SEP
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000316#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000317#include <windows.h>
Victor Stinner361dcdc2020-04-15 03:24:57 +0200318#include <shellapi.h> // ShellExecute()
319#include <lmcons.h> // UNLEN
Brian Curtin52173d42010-12-02 18:29:18 +0000320#define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000321#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000322
Tim Petersbc2e10e2002-03-03 23:17:02 +0000323#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000324#if defined(PATH_MAX) && PATH_MAX > 1024
325#define MAXPATHLEN PATH_MAX
326#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000327#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000328#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000329#endif /* MAXPATHLEN */
330
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000331#ifdef UNION_WAIT
332/* Emulate some macros on systems that have a union instead of macros */
333
334#ifndef WIFEXITED
335#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
336#endif
337
338#ifndef WEXITSTATUS
339#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
340#endif
341
342#ifndef WTERMSIG
343#define WTERMSIG(u_wait) ((u_wait).w_termsig)
344#endif
345
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346#define WAIT_TYPE union wait
347#define WAIT_STATUS_INT(s) (s.w_status)
348
349#else /* !UNION_WAIT */
350#define WAIT_TYPE int
351#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000352#endif /* UNION_WAIT */
353
Greg Wardb48bc172000-03-01 21:51:56 +0000354/* Don't use the "_r" form if we don't need it (also, won't have a
355 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200356#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000357#define USE_CTERMID_R
358#endif
359
Fred Drake699f3522000-06-29 21:12:41 +0000360/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000361#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000362#undef FSTAT
363#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200364#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000365# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700366# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200367# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800368# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000369#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000370# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700371# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000372# define FSTAT fstat
373# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000374#endif
375
Tim Peters11b23062003-04-23 02:39:17 +0000376#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000377#include <sys/mkdev.h>
378#else
379#if defined(MAJOR_IN_SYSMACROS)
380#include <sys/sysmacros.h>
381#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000382#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
383#include <sys/mkdev.h>
384#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000385#endif
Fred Drake699f3522000-06-29 21:12:41 +0000386
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200387#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100388#define INITFUNC PyInit_nt
389#define MODNAME "nt"
390#else
391#define INITFUNC PyInit_posix
392#define MODNAME "posix"
393#endif
394
jcea6c51d512018-01-28 14:00:08 +0100395#if defined(__sun)
396/* Something to implement in autoconf, not present in autoconf 2.69 */
397#define HAVE_STRUCT_STAT_ST_FSTYPE 1
398#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200399
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600400/* memfd_create is either defined in sys/mman.h or sys/memfd.h
401 * linux/memfd.h defines additional flags
402 */
403#ifdef HAVE_SYS_MMAN_H
404#include <sys/mman.h>
405#endif
406#ifdef HAVE_SYS_MEMFD_H
407#include <sys/memfd.h>
408#endif
409#ifdef HAVE_LINUX_MEMFD_H
410#include <linux/memfd.h>
411#endif
412
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800413#ifdef _Py_MEMORY_SANITIZER
414# include <sanitizer/msan_interface.h>
415#endif
416
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200417#ifdef HAVE_FORK
418static void
419run_at_forkers(PyObject *lst, int reverse)
420{
421 Py_ssize_t i;
422 PyObject *cpy;
423
424 if (lst != NULL) {
425 assert(PyList_CheckExact(lst));
426
427 /* Use a list copy in case register_at_fork() is called from
428 * one of the callbacks.
429 */
430 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
431 if (cpy == NULL)
432 PyErr_WriteUnraisable(lst);
433 else {
434 if (reverse)
435 PyList_Reverse(cpy);
436 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
437 PyObject *func, *res;
438 func = PyList_GET_ITEM(cpy, i);
Jeroen Demeyer7f41c8e2019-07-04 12:35:31 +0200439 res = _PyObject_CallNoArg(func);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200440 if (res == NULL)
441 PyErr_WriteUnraisable(func);
442 else
443 Py_DECREF(res);
444 }
445 Py_DECREF(cpy);
446 }
447 }
448}
449
450void
451PyOS_BeforeFork(void)
452{
Victor Stinner81a7be32020-04-14 15:14:01 +0200453 run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200454
455 _PyImport_AcquireLock();
456}
457
458void
459PyOS_AfterFork_Parent(void)
460{
461 if (_PyImport_ReleaseLock() <= 0)
462 Py_FatalError("failed releasing import lock after fork");
463
Victor Stinner81a7be32020-04-14 15:14:01 +0200464 run_at_forkers(_PyInterpreterState_GET()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200465}
466
467void
468PyOS_AfterFork_Child(void)
469{
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200470 _PyRuntimeState *runtime = &_PyRuntime;
471 _PyGILState_Reinit(runtime);
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200472 _PyEval_ReInitThreads(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200473 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200474 _PySignal_AfterFork();
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200475 _PyRuntimeState_ReInitThreads(runtime);
Victor Stinnerb49858b2019-05-24 15:20:23 +0200476 _PyInterpreterState_DeleteExceptMain(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200477
Victor Stinner81a7be32020-04-14 15:14:01 +0200478 run_at_forkers(_PyInterpreterState_GET()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200479}
480
481static int
482register_at_forker(PyObject **lst, PyObject *func)
483{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700484 if (func == NULL) /* nothing to register? do nothing. */
485 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200486 if (*lst == NULL) {
487 *lst = PyList_New(0);
488 if (*lst == NULL)
489 return -1;
490 }
491 return PyList_Append(*lst, func);
492}
Victor Stinner87255be2020-04-07 23:11:49 +0200493#endif /* HAVE_FORK */
494
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200495
496/* Legacy wrapper */
497void
498PyOS_AfterFork(void)
499{
500#ifdef HAVE_FORK
501 PyOS_AfterFork_Child();
502#endif
503}
504
505
Victor Stinner6036e442015-03-08 01:58:04 +0100506#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200507/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700508void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
509void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200510 ULONG, struct _Py_stat_struct *);
511#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700512
Larry Hastings9cf065c2012-06-22 16:30:09 -0700513
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200514#ifndef MS_WINDOWS
515PyObject *
516_PyLong_FromUid(uid_t uid)
517{
518 if (uid == (uid_t)-1)
519 return PyLong_FromLong(-1);
520 return PyLong_FromUnsignedLong(uid);
521}
522
523PyObject *
524_PyLong_FromGid(gid_t gid)
525{
526 if (gid == (gid_t)-1)
527 return PyLong_FromLong(-1);
528 return PyLong_FromUnsignedLong(gid);
529}
530
531int
532_Py_Uid_Converter(PyObject *obj, void *p)
533{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700534 uid_t uid;
535 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200536 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200537 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700538 unsigned long uresult;
539
540 index = PyNumber_Index(obj);
541 if (index == NULL) {
542 PyErr_Format(PyExc_TypeError,
543 "uid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800544 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200545 return 0;
546 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700547
548 /*
549 * Handling uid_t is complicated for two reasons:
550 * * Although uid_t is (always?) unsigned, it still
551 * accepts -1.
552 * * We don't know its size in advance--it may be
553 * bigger than an int, or it may be smaller than
554 * a long.
555 *
556 * So a bit of defensive programming is in order.
557 * Start with interpreting the value passed
558 * in as a signed long and see if it works.
559 */
560
561 result = PyLong_AsLongAndOverflow(index, &overflow);
562
563 if (!overflow) {
564 uid = (uid_t)result;
565
566 if (result == -1) {
567 if (PyErr_Occurred())
568 goto fail;
569 /* It's a legitimate -1, we're done. */
570 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200571 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700572
573 /* Any other negative number is disallowed. */
574 if (result < 0)
575 goto underflow;
576
577 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200578 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700579 (long)uid != result)
580 goto underflow;
581 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200582 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700583
584 if (overflow < 0)
585 goto underflow;
586
587 /*
588 * Okay, the value overflowed a signed long. If it
589 * fits in an *unsigned* long, it may still be okay,
590 * as uid_t may be unsigned long on this platform.
591 */
592 uresult = PyLong_AsUnsignedLong(index);
593 if (PyErr_Occurred()) {
594 if (PyErr_ExceptionMatches(PyExc_OverflowError))
595 goto overflow;
596 goto fail;
597 }
598
599 uid = (uid_t)uresult;
600
601 /*
602 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
603 * but this value would get interpreted as (uid_t)-1 by chown
604 * and its siblings. That's not what the user meant! So we
605 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100606 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700607 */
608 if (uid == (uid_t)-1)
609 goto overflow;
610
611 /* Ensure the value wasn't truncated. */
612 if (sizeof(uid_t) < sizeof(long) &&
613 (unsigned long)uid != uresult)
614 goto overflow;
615 /* fallthrough */
616
617success:
618 Py_DECREF(index);
619 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200620 return 1;
621
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700622underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200623 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700624 "uid is less than minimum");
625 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200626
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700627overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200628 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700629 "uid is greater than maximum");
630 /* fallthrough */
631
632fail:
633 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200634 return 0;
635}
636
637int
638_Py_Gid_Converter(PyObject *obj, void *p)
639{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700640 gid_t gid;
641 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200642 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200643 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700644 unsigned long uresult;
645
646 index = PyNumber_Index(obj);
647 if (index == NULL) {
648 PyErr_Format(PyExc_TypeError,
649 "gid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800650 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200651 return 0;
652 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700653
654 /*
655 * Handling gid_t is complicated for two reasons:
656 * * Although gid_t is (always?) unsigned, it still
657 * accepts -1.
658 * * We don't know its size in advance--it may be
659 * bigger than an int, or it may be smaller than
660 * a long.
661 *
662 * So a bit of defensive programming is in order.
663 * Start with interpreting the value passed
664 * in as a signed long and see if it works.
665 */
666
667 result = PyLong_AsLongAndOverflow(index, &overflow);
668
669 if (!overflow) {
670 gid = (gid_t)result;
671
672 if (result == -1) {
673 if (PyErr_Occurred())
674 goto fail;
675 /* It's a legitimate -1, we're done. */
676 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200677 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700678
679 /* Any other negative number is disallowed. */
680 if (result < 0) {
681 goto underflow;
682 }
683
684 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200685 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700686 (long)gid != result)
687 goto underflow;
688 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200689 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700690
691 if (overflow < 0)
692 goto underflow;
693
694 /*
695 * Okay, the value overflowed a signed long. If it
696 * fits in an *unsigned* long, it may still be okay,
697 * as gid_t may be unsigned long on this platform.
698 */
699 uresult = PyLong_AsUnsignedLong(index);
700 if (PyErr_Occurred()) {
701 if (PyErr_ExceptionMatches(PyExc_OverflowError))
702 goto overflow;
703 goto fail;
704 }
705
706 gid = (gid_t)uresult;
707
708 /*
709 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
710 * but this value would get interpreted as (gid_t)-1 by chown
711 * and its siblings. That's not what the user meant! So we
712 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100713 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700714 */
715 if (gid == (gid_t)-1)
716 goto overflow;
717
718 /* Ensure the value wasn't truncated. */
719 if (sizeof(gid_t) < sizeof(long) &&
720 (unsigned long)gid != uresult)
721 goto overflow;
722 /* fallthrough */
723
724success:
725 Py_DECREF(index);
726 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200727 return 1;
728
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700729underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200730 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700731 "gid is less than minimum");
732 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200733
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700734overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200735 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700736 "gid is greater than maximum");
737 /* fallthrough */
738
739fail:
740 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200741 return 0;
742}
743#endif /* MS_WINDOWS */
744
745
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700746#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800747
748
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200749#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
750static int
751_Py_Dev_Converter(PyObject *obj, void *p)
752{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200753 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200754 if (PyErr_Occurred())
755 return 0;
756 return 1;
757}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800758#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200759
760
Larry Hastings9cf065c2012-06-22 16:30:09 -0700761#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400762/*
763 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
764 * without the int cast, the value gets interpreted as uint (4291925331),
765 * which doesn't play nicely with all the initializer lines in this file that
766 * look like this:
767 * int dir_fd = DEFAULT_DIR_FD;
768 */
769#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700770#else
771#define DEFAULT_DIR_FD (-100)
772#endif
773
774static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300775_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200776{
777 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700778 long long_value;
779
780 PyObject *index = PyNumber_Index(o);
781 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700782 return 0;
783 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700784
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300785 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700786 long_value = PyLong_AsLongAndOverflow(index, &overflow);
787 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300788 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200789 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700790 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700791 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700792 return 0;
793 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200794 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700795 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700796 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700797 return 0;
798 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700799
Larry Hastings9cf065c2012-06-22 16:30:09 -0700800 *p = (int)long_value;
801 return 1;
802}
803
804static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200805dir_fd_converter(PyObject *o, void *p)
806{
807 if (o == Py_None) {
808 *(int *)p = DEFAULT_DIR_FD;
809 return 1;
810 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300811 else if (PyIndex_Check(o)) {
812 return _fd_converter(o, (int *)p);
813 }
814 else {
815 PyErr_Format(PyExc_TypeError,
816 "argument should be integer or None, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800817 _PyType_Name(Py_TYPE(o)));
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300818 return 0;
819 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700820}
821
Eddie Elizondob3966632019-11-05 07:16:14 -0800822typedef struct {
823 PyObject *billion;
Eddie Elizondob3966632019-11-05 07:16:14 -0800824 PyObject *DirEntryType;
825 PyObject *ScandirIteratorType;
826#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
827 PyObject *SchedParamType;
828#endif
829 PyObject *StatResultType;
830 PyObject *StatVFSResultType;
831 PyObject *TerminalSizeType;
832 PyObject *TimesResultType;
833 PyObject *UnameResultType;
834#if defined(HAVE_WAITID) && !defined(__APPLE__)
835 PyObject *WaitidResultType;
836#endif
837#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
838 PyObject *struct_rusage;
839#endif
840 PyObject *st_mode;
841} _posixstate;
842
843static struct PyModuleDef posixmodule;
844
Hai Shif707d942020-03-16 21:15:01 +0800845static inline _posixstate*
846get_posix_state(PyObject *module)
847{
848 void *state = PyModule_GetState(module);
849 assert(state != NULL);
850 return (_posixstate *)state;
851}
852
Eddie Elizondob3966632019-11-05 07:16:14 -0800853#define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule)))
Larry Hastings9cf065c2012-06-22 16:30:09 -0700854
Larry Hastings9cf065c2012-06-22 16:30:09 -0700855/*
856 * A PyArg_ParseTuple "converter" function
857 * that handles filesystem paths in the manner
858 * preferred by the os module.
859 *
860 * path_converter accepts (Unicode) strings and their
861 * subclasses, and bytes and their subclasses. What
862 * it does with the argument depends on the platform:
863 *
864 * * On Windows, if we get a (Unicode) string we
865 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700866 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700867 *
868 * * On all other platforms, strings are encoded
869 * to bytes using PyUnicode_FSConverter, then we
870 * extract the char * from the bytes object and
871 * return that.
872 *
873 * path_converter also optionally accepts signed
874 * integers (representing open file descriptors) instead
875 * of path strings.
876 *
877 * Input fields:
878 * path.nullable
879 * If nonzero, the path is permitted to be None.
880 * path.allow_fd
881 * If nonzero, the path is permitted to be a file handle
882 * (a signed int) instead of a string.
883 * path.function_name
884 * If non-NULL, path_converter will use that as the name
885 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700886 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700887 * path.argument_name
888 * If non-NULL, path_converter will use that as the name
889 * of the parameter in error messages.
890 * (If path.argument_name is NULL it uses "path".)
891 *
892 * Output fields:
893 * path.wide
894 * Points to the path if it was expressed as Unicode
895 * and was not encoded. (Only used on Windows.)
896 * path.narrow
897 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700898 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000899 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700900 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700901 * path.fd
902 * Contains a file descriptor if path.accept_fd was true
903 * and the caller provided a signed integer instead of any
904 * sort of string.
905 *
906 * WARNING: if your "path" parameter is optional, and is
907 * unspecified, path_converter will never get called.
908 * So if you set allow_fd, you *MUST* initialize path.fd = -1
909 * yourself!
910 * path.length
911 * The length of the path in characters, if specified as
912 * a string.
913 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800914 * The original object passed in (if get a PathLike object,
915 * the result of PyOS_FSPath() is treated as the original object).
916 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700917 * path.cleanup
918 * For internal use only. May point to a temporary object.
919 * (Pay no attention to the man behind the curtain.)
920 *
921 * At most one of path.wide or path.narrow will be non-NULL.
922 * If path was None and path.nullable was set,
923 * or if path was an integer and path.allow_fd was set,
924 * both path.wide and path.narrow will be NULL
925 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200926 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700927 * path_converter takes care to not write to the path_t
928 * unless it's successful. However it must reset the
929 * "cleanup" field each time it's called.
930 *
931 * Use as follows:
932 * path_t path;
933 * memset(&path, 0, sizeof(path));
934 * PyArg_ParseTuple(args, "O&", path_converter, &path);
935 * // ... use values from path ...
936 * path_cleanup(&path);
937 *
938 * (Note that if PyArg_Parse fails you don't need to call
939 * path_cleanup(). However it is safe to do so.)
940 */
941typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100942 const char *function_name;
943 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700944 int nullable;
945 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300946 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700947#ifdef MS_WINDOWS
948 BOOL narrow;
949#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300950 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700951#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700952 int fd;
953 Py_ssize_t length;
954 PyObject *object;
955 PyObject *cleanup;
956} path_t;
957
Steve Dowercc16be82016-09-08 10:35:16 -0700958#ifdef MS_WINDOWS
959#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
960 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
961#else
Larry Hastings2f936352014-08-05 14:04:04 +1000962#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
963 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700964#endif
Larry Hastings31826802013-10-19 00:09:25 -0700965
Larry Hastings9cf065c2012-06-22 16:30:09 -0700966static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800967path_cleanup(path_t *path)
968{
969 Py_CLEAR(path->object);
970 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700971}
972
973static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300974path_converter(PyObject *o, void *p)
975{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700976 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800977 PyObject *bytes = NULL;
978 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700979 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300980 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700981#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800982 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700983 const wchar_t *wide;
984#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700985
986#define FORMAT_EXCEPTION(exc, fmt) \
987 PyErr_Format(exc, "%s%s" fmt, \
988 path->function_name ? path->function_name : "", \
989 path->function_name ? ": " : "", \
990 path->argument_name ? path->argument_name : "path")
991
992 /* Py_CLEANUP_SUPPORTED support */
993 if (o == NULL) {
994 path_cleanup(path);
995 return 1;
996 }
997
Brett Cannon3f9183b2016-08-26 14:44:48 -0700998 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800999 path->object = path->cleanup = NULL;
1000 /* path->object owns a reference to the original object */
1001 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001002
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001003 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001004 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001005#ifdef MS_WINDOWS
1006 path->narrow = FALSE;
1007#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001008 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001009#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001010 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001011 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001012 }
1013
Brett Cannon3f9183b2016-08-26 14:44:48 -07001014 /* Only call this here so that we don't treat the return value of
1015 os.fspath() as an fd or buffer. */
1016 is_index = path->allow_fd && PyIndex_Check(o);
1017 is_buffer = PyObject_CheckBuffer(o);
1018 is_bytes = PyBytes_Check(o);
1019 is_unicode = PyUnicode_Check(o);
1020
1021 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
1022 /* Inline PyOS_FSPath() for better error messages. */
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001023 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001024
1025 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
1026 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001027 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001028 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001029 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001030 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001031 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001032 goto error_exit;
1033 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001034 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001035 is_unicode = 1;
1036 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001037 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001038 is_bytes = 1;
1039 }
1040 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001041 PyErr_Format(PyExc_TypeError,
1042 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -08001043 "not %.200s", _PyType_Name(Py_TYPE(o)),
1044 _PyType_Name(Py_TYPE(res)));
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001045 Py_DECREF(res);
1046 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001047 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001048
1049 /* still owns a reference to the original object */
1050 Py_DECREF(o);
1051 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001052 }
1053
1054 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001055#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001056 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001057 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001058 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001059 }
Victor Stinner59799a82013-11-13 14:17:30 +01001060 if (length > 32767) {
1061 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001062 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001063 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001064 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001065 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001066 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001067 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001068
1069 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001070 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001071 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001072 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001073#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001074 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001075 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001076 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001077#endif
1078 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001079 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001080 bytes = o;
1081 Py_INCREF(bytes);
1082 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001083 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001084 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001085 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001086 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1087 "%s%s%s should be %s, not %.200s",
1088 path->function_name ? path->function_name : "",
1089 path->function_name ? ": " : "",
1090 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001091 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1092 "integer or None" :
1093 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1094 path->nullable ? "string, bytes, os.PathLike or None" :
1095 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001096 _PyType_Name(Py_TYPE(o)))) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001097 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001098 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001099 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001100 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001101 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001102 }
1103 }
Steve Dowercc16be82016-09-08 10:35:16 -07001104 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001105 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001106 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001107 }
1108 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001109#ifdef MS_WINDOWS
1110 path->narrow = FALSE;
1111#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001112 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001113#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001114 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001115 }
1116 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001117 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001118 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1119 path->function_name ? path->function_name : "",
1120 path->function_name ? ": " : "",
1121 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001122 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1123 "integer or None" :
1124 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1125 path->nullable ? "string, bytes, os.PathLike or None" :
1126 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001127 _PyType_Name(Py_TYPE(o)));
Xiang Zhang04316c42017-01-08 23:26:57 +08001128 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001129 }
1130
Larry Hastings9cf065c2012-06-22 16:30:09 -07001131 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001132 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001133 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001134 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001135 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001136 }
1137
Steve Dowercc16be82016-09-08 10:35:16 -07001138#ifdef MS_WINDOWS
1139 wo = PyUnicode_DecodeFSDefaultAndSize(
1140 narrow,
1141 length
1142 );
1143 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001144 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001145 }
1146
Xiang Zhang04316c42017-01-08 23:26:57 +08001147 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001148 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001149 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001150 }
1151 if (length > 32767) {
1152 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001153 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001154 }
1155 if (wcslen(wide) != length) {
1156 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001157 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001158 }
1159 path->wide = wide;
1160 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001161 path->cleanup = wo;
1162 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001163#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001164 path->wide = NULL;
1165 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001166 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001167 /* Still a reference owned by path->object, don't have to
1168 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001169 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001170 }
1171 else {
1172 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001173 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001174#endif
1175 path->fd = -1;
1176
1177 success_exit:
1178 path->length = length;
1179 path->object = o;
1180 return Py_CLEANUP_SUPPORTED;
1181
1182 error_exit:
1183 Py_XDECREF(o);
1184 Py_XDECREF(bytes);
1185#ifdef MS_WINDOWS
1186 Py_XDECREF(wo);
1187#endif
1188 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001189}
1190
1191static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001192argument_unavailable_error(const char *function_name, const char *argument_name)
1193{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001194 PyErr_Format(PyExc_NotImplementedError,
1195 "%s%s%s unavailable on this platform",
1196 (function_name != NULL) ? function_name : "",
1197 (function_name != NULL) ? ": ": "",
1198 argument_name);
1199}
1200
1201static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001202dir_fd_unavailable(PyObject *o, void *p)
1203{
1204 int dir_fd;
1205 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001206 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001207 if (dir_fd != DEFAULT_DIR_FD) {
1208 argument_unavailable_error(NULL, "dir_fd");
1209 return 0;
1210 }
1211 *(int *)p = dir_fd;
1212 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001213}
1214
1215static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001216fd_specified(const char *function_name, int fd)
1217{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001218 if (fd == -1)
1219 return 0;
1220
1221 argument_unavailable_error(function_name, "fd");
1222 return 1;
1223}
1224
1225static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001226follow_symlinks_specified(const char *function_name, int follow_symlinks)
1227{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001228 if (follow_symlinks)
1229 return 0;
1230
1231 argument_unavailable_error(function_name, "follow_symlinks");
1232 return 1;
1233}
1234
1235static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001236path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1237{
Steve Dowercc16be82016-09-08 10:35:16 -07001238 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1239#ifndef MS_WINDOWS
1240 && !path->narrow
1241#endif
1242 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001243 PyErr_Format(PyExc_ValueError,
1244 "%s: can't specify dir_fd without matching path",
1245 function_name);
1246 return 1;
1247 }
1248 return 0;
1249}
1250
1251static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001252dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1253{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001254 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1255 PyErr_Format(PyExc_ValueError,
1256 "%s: can't specify both dir_fd and fd",
1257 function_name);
1258 return 1;
1259 }
1260 return 0;
1261}
1262
1263static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001264fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1265 int follow_symlinks)
1266{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001267 if ((fd > 0) && (!follow_symlinks)) {
1268 PyErr_Format(PyExc_ValueError,
1269 "%s: cannot use fd and follow_symlinks together",
1270 function_name);
1271 return 1;
1272 }
1273 return 0;
1274}
1275
1276static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001277dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1278 int follow_symlinks)
1279{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001280 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1281 PyErr_Format(PyExc_ValueError,
1282 "%s: cannot use dir_fd and follow_symlinks together",
1283 function_name);
1284 return 1;
1285 }
1286 return 0;
1287}
1288
Larry Hastings2f936352014-08-05 14:04:04 +10001289#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001290 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001291#else
Larry Hastings2f936352014-08-05 14:04:04 +10001292 typedef off_t Py_off_t;
1293#endif
1294
1295static int
1296Py_off_t_converter(PyObject *arg, void *addr)
1297{
1298#ifdef HAVE_LARGEFILE_SUPPORT
1299 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1300#else
1301 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001302#endif
1303 if (PyErr_Occurred())
1304 return 0;
1305 return 1;
1306}
Larry Hastings2f936352014-08-05 14:04:04 +10001307
1308static PyObject *
1309PyLong_FromPy_off_t(Py_off_t offset)
1310{
1311#ifdef HAVE_LARGEFILE_SUPPORT
1312 return PyLong_FromLongLong(offset);
1313#else
1314 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001315#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001316}
1317
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001318#ifdef HAVE_SIGSET_T
1319/* Convert an iterable of integers to a sigset.
1320 Return 1 on success, return 0 and raise an exception on error. */
1321int
1322_Py_Sigset_Converter(PyObject *obj, void *addr)
1323{
1324 sigset_t *mask = (sigset_t *)addr;
1325 PyObject *iterator, *item;
1326 long signum;
1327 int overflow;
1328
Rémi Lapeyref0900192019-05-04 01:30:53 +02001329 // The extra parens suppress the unreachable-code warning with clang on MacOS
1330 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001331 /* Probably only if mask == NULL. */
1332 PyErr_SetFromErrno(PyExc_OSError);
1333 return 0;
1334 }
1335
1336 iterator = PyObject_GetIter(obj);
1337 if (iterator == NULL) {
1338 return 0;
1339 }
1340
1341 while ((item = PyIter_Next(iterator)) != NULL) {
1342 signum = PyLong_AsLongAndOverflow(item, &overflow);
1343 Py_DECREF(item);
1344 if (signum <= 0 || signum >= NSIG) {
1345 if (overflow || signum != -1 || !PyErr_Occurred()) {
1346 PyErr_Format(PyExc_ValueError,
1347 "signal number %ld out of range", signum);
1348 }
1349 goto error;
1350 }
1351 if (sigaddset(mask, (int)signum)) {
1352 if (errno != EINVAL) {
1353 /* Probably impossible */
1354 PyErr_SetFromErrno(PyExc_OSError);
1355 goto error;
1356 }
1357 /* For backwards compatibility, allow idioms such as
1358 * `range(1, NSIG)` but warn about invalid signal numbers
1359 */
1360 const char msg[] =
1361 "invalid signal number %ld, please use valid_signals()";
1362 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1363 goto error;
1364 }
1365 }
1366 }
1367 if (!PyErr_Occurred()) {
1368 Py_DECREF(iterator);
1369 return 1;
1370 }
1371
1372error:
1373 Py_DECREF(iterator);
1374 return 0;
1375}
1376#endif /* HAVE_SIGSET_T */
1377
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001378#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001379
1380static int
Brian Curtind25aef52011-06-13 15:16:04 -05001381win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001382{
Martin Panter70214ad2016-08-04 02:38:59 +00001383 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1384 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001385 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001386
1387 if (0 == DeviceIoControl(
1388 reparse_point_handle,
1389 FSCTL_GET_REPARSE_POINT,
1390 NULL, 0, /* in buffer */
1391 target_buffer, sizeof(target_buffer),
1392 &n_bytes_returned,
1393 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001394 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001395
1396 if (reparse_tag)
1397 *reparse_tag = rdb->ReparseTag;
1398
Brian Curtind25aef52011-06-13 15:16:04 -05001399 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001400}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001401
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001402#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001403
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001404/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001405#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001406/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001407** environ directly, we must obtain it with _NSGetEnviron(). See also
1408** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001409*/
1410#include <crt_externs.h>
pxinwrf2d7ac72019-05-21 18:46:37 +08001411#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001412extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001413#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001414
Barry Warsaw53699e91996-12-10 23:23:01 +00001415static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001416convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001417{
Victor Stinner8c62be82010-05-06 00:08:46 +00001418 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001419#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001421#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001423#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001424
Victor Stinner8c62be82010-05-06 00:08:46 +00001425 d = PyDict_New();
1426 if (d == NULL)
1427 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001428#ifdef MS_WINDOWS
1429 /* _wenviron must be initialized in this way if the program is started
1430 through main() instead of wmain(). */
1431 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001432 e = _wenviron;
Benoit Hudson723f71a2019-12-06 14:15:03 -05001433#elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1434 /* environ is not accessible as an extern in a shared object on OSX; use
1435 _NSGetEnviron to resolve it. The value changes if you add environment
1436 variables between calls to Py_Initialize, so don't cache the value. */
1437 e = *_NSGetEnviron();
Victor Stinner8c62be82010-05-06 00:08:46 +00001438#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001439 e = environ;
1440#endif
1441 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001442 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001443 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001444 PyObject *k;
1445 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001446#ifdef MS_WINDOWS
1447 const wchar_t *p = wcschr(*e, L'=');
1448#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001449 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001450#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001451 if (p == NULL)
1452 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001453#ifdef MS_WINDOWS
1454 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1455#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001456 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001457#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001458 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001459 Py_DECREF(d);
1460 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001461 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001462#ifdef MS_WINDOWS
1463 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1464#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001465 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001466#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001467 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001468 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001469 Py_DECREF(d);
1470 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001472 if (PyDict_GetItemWithError(d, k) == NULL) {
1473 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1474 Py_DECREF(v);
1475 Py_DECREF(k);
1476 Py_DECREF(d);
1477 return NULL;
1478 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001479 }
1480 Py_DECREF(k);
1481 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001482 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001483 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001484}
1485
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001486/* Set a POSIX-specific error from errno, and return NULL */
1487
Barry Warsawd58d7641998-07-23 16:14:40 +00001488static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001489posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001490{
Victor Stinner8c62be82010-05-06 00:08:46 +00001491 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001492}
Mark Hammondef8b6542001-05-13 08:04:26 +00001493
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001494#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001495static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001496win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001497{
Victor Stinner8c62be82010-05-06 00:08:46 +00001498 /* XXX We should pass the function name along in the future.
1499 (winreg.c also wants to pass the function name.)
1500 This would however require an additional param to the
1501 Windows error object, which is non-trivial.
1502 */
1503 errno = GetLastError();
1504 if (filename)
1505 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1506 else
1507 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001508}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001509
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001510static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001511win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001512{
1513 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001514 if (filename)
1515 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001516 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001517 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001518 filename);
1519 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001520 return PyErr_SetFromWindowsErr(err);
1521}
1522
1523static PyObject *
1524win32_error_object(const char* function, PyObject* filename)
1525{
1526 errno = GetLastError();
1527 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001528}
1529
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001530#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001531
Larry Hastings9cf065c2012-06-22 16:30:09 -07001532static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001533posix_path_object_error(PyObject *path)
1534{
1535 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1536}
1537
1538static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001539path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001540{
1541#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001542 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1543 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001544#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001545 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001546#endif
1547}
1548
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001549static PyObject *
1550path_object_error2(PyObject *path, PyObject *path2)
1551{
1552#ifdef MS_WINDOWS
1553 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1554 PyExc_OSError, 0, path, path2);
1555#else
1556 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1557#endif
1558}
1559
1560static PyObject *
1561path_error(path_t *path)
1562{
1563 return path_object_error(path->object);
1564}
Larry Hastings31826802013-10-19 00:09:25 -07001565
Larry Hastingsb0827312014-02-09 22:05:19 -08001566static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001567posix_path_error(path_t *path)
1568{
1569 return posix_path_object_error(path->object);
1570}
1571
1572static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001573path_error2(path_t *path, path_t *path2)
1574{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001575 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001576}
1577
1578
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001579/* POSIX generic methods */
1580
Larry Hastings2f936352014-08-05 14:04:04 +10001581static int
1582fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001583{
Victor Stinner8c62be82010-05-06 00:08:46 +00001584 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001585 int *pointer = (int *)p;
1586 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001587 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001588 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001589 *pointer = fd;
1590 return 1;
1591}
1592
1593static PyObject *
1594posix_fildes_fd(int fd, int (*func)(int))
1595{
1596 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001597 int async_err = 0;
1598
1599 do {
1600 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001601 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001602 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001603 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001604 Py_END_ALLOW_THREADS
1605 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1606 if (res != 0)
1607 return (!async_err) ? posix_error() : NULL;
1608 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001609}
Guido van Rossum21142a01999-01-08 21:05:37 +00001610
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001611
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001612#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001613/* This is a reimplementation of the C library's chdir function,
1614 but one that produces Win32 errors instead of DOS error codes.
1615 chdir is essentially a wrapper around SetCurrentDirectory; however,
1616 it also needs to set "magic" environment variables indicating
1617 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001618static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001619win32_wchdir(LPCWSTR path)
1620{
Victor Stinnered537822015-12-13 21:40:26 +01001621 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 int result;
1623 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624
Victor Stinner8c62be82010-05-06 00:08:46 +00001625 if(!SetCurrentDirectoryW(path))
1626 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001627 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001628 if (!result)
1629 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001630 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001631 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001632 if (!new_path) {
1633 SetLastError(ERROR_OUTOFMEMORY);
1634 return FALSE;
1635 }
1636 result = GetCurrentDirectoryW(result, new_path);
1637 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001638 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001639 return FALSE;
1640 }
1641 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001642 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1643 wcsncmp(new_path, L"//", 2) == 0);
1644 if (!is_unc_like_path) {
1645 env[1] = new_path[0];
1646 result = SetEnvironmentVariableW(env, new_path);
1647 }
Victor Stinnered537822015-12-13 21:40:26 +01001648 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001649 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001650 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001651}
1652#endif
1653
Martin v. Löwis14694662006-02-03 12:54:16 +00001654#ifdef MS_WINDOWS
1655/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1656 - time stamps are restricted to second resolution
1657 - file modification times suffer from forth-and-back conversions between
1658 UTC and local time
1659 Therefore, we implement our own stat, based on the Win32 API directly.
1660*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001661#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001662#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001663#define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001664
Victor Stinner6036e442015-03-08 01:58:04 +01001665static void
Steve Dowercc16be82016-09-08 10:35:16 -07001666find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1667 BY_HANDLE_FILE_INFORMATION *info,
1668 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001669{
1670 memset(info, 0, sizeof(*info));
1671 info->dwFileAttributes = pFileData->dwFileAttributes;
1672 info->ftCreationTime = pFileData->ftCreationTime;
1673 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1674 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1675 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1676 info->nFileSizeLow = pFileData->nFileSizeLow;
1677/* info->nNumberOfLinks = 1; */
1678 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1679 *reparse_tag = pFileData->dwReserved0;
1680 else
1681 *reparse_tag = 0;
1682}
1683
Guido van Rossumd8faa362007-04-27 19:54:29 +00001684static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001685attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001686{
Victor Stinner8c62be82010-05-06 00:08:46 +00001687 HANDLE hFindFile;
1688 WIN32_FIND_DATAW FileData;
1689 hFindFile = FindFirstFileW(pszFile, &FileData);
1690 if (hFindFile == INVALID_HANDLE_VALUE)
1691 return FALSE;
1692 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001693 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001694 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001695}
1696
Brian Curtind25aef52011-06-13 15:16:04 -05001697static int
Steve Dowercc16be82016-09-08 10:35:16 -07001698win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001699 BOOL traverse)
1700{
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001701 HANDLE hFile;
1702 BY_HANDLE_FILE_INFORMATION fileInfo;
1703 FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
1704 DWORD fileType, error;
1705 BOOL isUnhandledTag = FALSE;
1706 int retval = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001707
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001708 DWORD access = FILE_READ_ATTRIBUTES;
1709 DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
1710 if (!traverse) {
1711 flags |= FILE_FLAG_OPEN_REPARSE_POINT;
1712 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001713
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001714 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001715 if (hFile == INVALID_HANDLE_VALUE) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001716 /* Either the path doesn't exist, or the caller lacks access. */
1717 error = GetLastError();
1718 switch (error) {
1719 case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */
1720 case ERROR_SHARING_VIOLATION: /* It's a paging file. */
1721 /* Try reading the parent directory. */
1722 if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
1723 /* Cannot read the parent directory. */
1724 SetLastError(error);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001725 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001726 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001727 if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1728 if (traverse ||
1729 !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1730 /* The stat call has to traverse but cannot, so fail. */
1731 SetLastError(error);
Brian Curtind25aef52011-06-13 15:16:04 -05001732 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001733 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001734 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001735 break;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001736
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001737 case ERROR_INVALID_PARAMETER:
1738 /* \\.\con requires read or write access. */
1739 hFile = CreateFileW(path, access | GENERIC_READ,
1740 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1741 OPEN_EXISTING, flags, NULL);
1742 if (hFile == INVALID_HANDLE_VALUE) {
1743 SetLastError(error);
1744 return -1;
1745 }
1746 break;
1747
1748 case ERROR_CANT_ACCESS_FILE:
1749 /* bpo37834: open unhandled reparse points if traverse fails. */
1750 if (traverse) {
1751 traverse = FALSE;
1752 isUnhandledTag = TRUE;
1753 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
1754 flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
1755 }
1756 if (hFile == INVALID_HANDLE_VALUE) {
1757 SetLastError(error);
1758 return -1;
1759 }
1760 break;
1761
1762 default:
1763 return -1;
1764 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001765 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001766
1767 if (hFile != INVALID_HANDLE_VALUE) {
1768 /* Handle types other than files on disk. */
1769 fileType = GetFileType(hFile);
1770 if (fileType != FILE_TYPE_DISK) {
1771 if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1772 retval = -1;
1773 goto cleanup;
1774 }
1775 DWORD fileAttributes = GetFileAttributesW(path);
1776 memset(result, 0, sizeof(*result));
1777 if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1778 fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1779 /* \\.\pipe\ or \\.\mailslot\ */
1780 result->st_mode = _S_IFDIR;
1781 } else if (fileType == FILE_TYPE_CHAR) {
1782 /* \\.\nul */
1783 result->st_mode = _S_IFCHR;
1784 } else if (fileType == FILE_TYPE_PIPE) {
1785 /* \\.\pipe\spam */
1786 result->st_mode = _S_IFIFO;
1787 }
1788 /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1789 goto cleanup;
1790 }
1791
1792 /* Query the reparse tag, and traverse a non-link. */
1793 if (!traverse) {
1794 if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
1795 &tagInfo, sizeof(tagInfo))) {
1796 /* Allow devices that do not support FileAttributeTagInfo. */
1797 switch (GetLastError()) {
1798 case ERROR_INVALID_PARAMETER:
1799 case ERROR_INVALID_FUNCTION:
1800 case ERROR_NOT_SUPPORTED:
1801 tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1802 tagInfo.ReparseTag = 0;
1803 break;
1804 default:
1805 retval = -1;
1806 goto cleanup;
1807 }
1808 } else if (tagInfo.FileAttributes &
1809 FILE_ATTRIBUTE_REPARSE_POINT) {
1810 if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1811 if (isUnhandledTag) {
1812 /* Traversing previously failed for either this link
1813 or its target. */
1814 SetLastError(ERROR_CANT_ACCESS_FILE);
1815 retval = -1;
1816 goto cleanup;
1817 }
1818 /* Traverse a non-link, but not if traversing already failed
1819 for an unhandled tag. */
1820 } else if (!isUnhandledTag) {
1821 CloseHandle(hFile);
1822 return win32_xstat_impl(path, result, TRUE);
1823 }
1824 }
1825 }
1826
1827 if (!GetFileInformationByHandle(hFile, &fileInfo)) {
1828 switch (GetLastError()) {
1829 case ERROR_INVALID_PARAMETER:
1830 case ERROR_INVALID_FUNCTION:
1831 case ERROR_NOT_SUPPORTED:
Steve Dower772ec0f2019-09-04 14:42:54 -07001832 /* Volumes and physical disks are block devices, e.g.
1833 \\.\C: and \\.\PhysicalDrive0. */
1834 memset(result, 0, sizeof(*result));
1835 result->st_mode = 0x6000; /* S_IFBLK */
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001836 goto cleanup;
1837 }
Steve Dower772ec0f2019-09-04 14:42:54 -07001838 retval = -1;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001839 goto cleanup;
1840 }
1841 }
1842
1843 _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result);
1844
1845 if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
1846 /* Fix the file execute permissions. This hack sets S_IEXEC if
1847 the filename has an extension that is commonly used by files
1848 that CreateProcessW can execute. A real implementation calls
1849 GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
1850 AccessCheck to check for generic read, write, and execute
1851 access. */
1852 const wchar_t *fileExtension = wcsrchr(path, '.');
1853 if (fileExtension) {
1854 if (_wcsicmp(fileExtension, L".exe") == 0 ||
1855 _wcsicmp(fileExtension, L".bat") == 0 ||
1856 _wcsicmp(fileExtension, L".cmd") == 0 ||
1857 _wcsicmp(fileExtension, L".com") == 0) {
1858 result->st_mode |= 0111;
1859 }
1860 }
1861 }
1862
1863cleanup:
1864 if (hFile != INVALID_HANDLE_VALUE) {
Steve Dower772ec0f2019-09-04 14:42:54 -07001865 /* Preserve last error if we are failing */
1866 error = retval ? GetLastError() : 0;
1867 if (!CloseHandle(hFile)) {
1868 retval = -1;
1869 } else if (retval) {
1870 /* Restore last error */
1871 SetLastError(error);
1872 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001873 }
1874
1875 return retval;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001876}
1877
1878static int
Steve Dowercc16be82016-09-08 10:35:16 -07001879win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001880{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001881 /* Protocol violation: we explicitly clear errno, instead of
1882 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001883 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001884 errno = 0;
1885 return code;
1886}
Brian Curtind25aef52011-06-13 15:16:04 -05001887/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001888
1889 In Posix, stat automatically traverses symlinks and returns the stat
1890 structure for the target. In Windows, the equivalent GetFileAttributes by
1891 default does not traverse symlinks and instead returns attributes for
1892 the symlink.
1893
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001894 Instead, we will open the file (which *does* traverse symlinks by default)
1895 and GetFileInformationByHandle(). */
Brian Curtind40e6f72010-07-08 21:39:08 +00001896
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001897static int
Steve Dowercc16be82016-09-08 10:35:16 -07001898win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001899{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001900 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001901}
1902
Victor Stinner8c62be82010-05-06 00:08:46 +00001903static int
Steve Dowercc16be82016-09-08 10:35:16 -07001904win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001905{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001906 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001907}
1908
Martin v. Löwis14694662006-02-03 12:54:16 +00001909#endif /* MS_WINDOWS */
1910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001911PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001912"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001913This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001914 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001915or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1916\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001917Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1918or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001919\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001920See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001921
1922static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001923 {"st_mode", "protection bits"},
1924 {"st_ino", "inode"},
1925 {"st_dev", "device"},
1926 {"st_nlink", "number of hard links"},
1927 {"st_uid", "user ID of owner"},
1928 {"st_gid", "group ID of owner"},
1929 {"st_size", "total size, in bytes"},
1930 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1931 {NULL, "integer time of last access"},
1932 {NULL, "integer time of last modification"},
1933 {NULL, "integer time of last change"},
1934 {"st_atime", "time of last access"},
1935 {"st_mtime", "time of last modification"},
1936 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001937 {"st_atime_ns", "time of last access in nanoseconds"},
1938 {"st_mtime_ns", "time of last modification in nanoseconds"},
1939 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001940#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001941 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001942#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001943#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001944 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001945#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001946#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001947 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001948#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001949#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001950 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001951#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001952#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001953 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001954#endif
1955#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001956 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001957#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001958#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1959 {"st_file_attributes", "Windows file attribute bits"},
1960#endif
jcea6c51d512018-01-28 14:00:08 +01001961#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1962 {"st_fstype", "Type of filesystem"},
1963#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001964#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1965 {"st_reparse_tag", "Windows reparse tag"},
1966#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001967 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001968};
1969
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001970#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001971#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001972#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001973#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001974#endif
1975
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001976#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001977#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1978#else
1979#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1980#endif
1981
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001982#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001983#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1984#else
1985#define ST_RDEV_IDX ST_BLOCKS_IDX
1986#endif
1987
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001988#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1989#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1990#else
1991#define ST_FLAGS_IDX ST_RDEV_IDX
1992#endif
1993
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001994#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001995#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001996#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001997#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001998#endif
1999
2000#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2001#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
2002#else
2003#define ST_BIRTHTIME_IDX ST_GEN_IDX
2004#endif
2005
Zachary Ware63f277b2014-06-19 09:46:37 -05002006#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2007#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
2008#else
2009#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
2010#endif
2011
jcea6c51d512018-01-28 14:00:08 +01002012#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2013#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
2014#else
2015#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
2016#endif
2017
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002018#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2019#define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
2020#else
2021#define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
2022#endif
2023
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002024static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002025 "stat_result", /* name */
2026 stat_result__doc__, /* doc */
2027 stat_result_fields,
2028 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002029};
2030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002031PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002032"statvfs_result: Result from statvfs or fstatvfs.\n\n\
2033This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002034 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00002035or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002036\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002037See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002038
2039static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002040 {"f_bsize", },
2041 {"f_frsize", },
2042 {"f_blocks", },
2043 {"f_bfree", },
2044 {"f_bavail", },
2045 {"f_files", },
2046 {"f_ffree", },
2047 {"f_favail", },
2048 {"f_flag", },
2049 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01002050 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00002051 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002052};
2053
2054static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002055 "statvfs_result", /* name */
2056 statvfs_result__doc__, /* doc */
2057 statvfs_result_fields,
2058 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002059};
2060
Ross Lagerwall7807c352011-03-17 20:20:30 +02002061#if defined(HAVE_WAITID) && !defined(__APPLE__)
2062PyDoc_STRVAR(waitid_result__doc__,
2063"waitid_result: Result from waitid.\n\n\
2064This object may be accessed either as a tuple of\n\
2065 (si_pid, si_uid, si_signo, si_status, si_code),\n\
2066or via the attributes si_pid, si_uid, and so on.\n\
2067\n\
2068See os.waitid for more information.");
2069
2070static PyStructSequence_Field waitid_result_fields[] = {
2071 {"si_pid", },
2072 {"si_uid", },
2073 {"si_signo", },
2074 {"si_status", },
2075 {"si_code", },
2076 {0}
2077};
2078
2079static PyStructSequence_Desc waitid_result_desc = {
2080 "waitid_result", /* name */
2081 waitid_result__doc__, /* doc */
2082 waitid_result_fields,
2083 5
2084};
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002085#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002086static newfunc structseq_new;
2087
2088static PyObject *
2089statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2090{
Victor Stinner8c62be82010-05-06 00:08:46 +00002091 PyStructSequence *result;
2092 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002093
Victor Stinner8c62be82010-05-06 00:08:46 +00002094 result = (PyStructSequence*)structseq_new(type, args, kwds);
2095 if (!result)
2096 return NULL;
2097 /* If we have been initialized from a tuple,
2098 st_?time might be set to None. Initialize it
2099 from the int slots. */
2100 for (i = 7; i <= 9; i++) {
2101 if (result->ob_item[i+3] == Py_None) {
2102 Py_DECREF(Py_None);
2103 Py_INCREF(result->ob_item[i]);
2104 result->ob_item[i+3] = result->ob_item[i];
2105 }
2106 }
2107 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002108}
2109
Eddie Elizondob3966632019-11-05 07:16:14 -08002110static int
2111_posix_clear(PyObject *module)
2112{
Hai Shif707d942020-03-16 21:15:01 +08002113 Py_CLEAR(get_posix_state(module)->billion);
2114 Py_CLEAR(get_posix_state(module)->DirEntryType);
2115 Py_CLEAR(get_posix_state(module)->ScandirIteratorType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002116#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Hai Shif707d942020-03-16 21:15:01 +08002117 Py_CLEAR(get_posix_state(module)->SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002118#endif
Hai Shif707d942020-03-16 21:15:01 +08002119 Py_CLEAR(get_posix_state(module)->StatResultType);
2120 Py_CLEAR(get_posix_state(module)->StatVFSResultType);
2121 Py_CLEAR(get_posix_state(module)->TerminalSizeType);
2122 Py_CLEAR(get_posix_state(module)->TimesResultType);
2123 Py_CLEAR(get_posix_state(module)->UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002124#if defined(HAVE_WAITID) && !defined(__APPLE__)
Hai Shif707d942020-03-16 21:15:01 +08002125 Py_CLEAR(get_posix_state(module)->WaitidResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002126#endif
2127#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Hai Shif707d942020-03-16 21:15:01 +08002128 Py_CLEAR(get_posix_state(module)->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08002129#endif
Hai Shif707d942020-03-16 21:15:01 +08002130 Py_CLEAR(get_posix_state(module)->st_mode);
Eddie Elizondob3966632019-11-05 07:16:14 -08002131 return 0;
2132}
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002133
Eddie Elizondob3966632019-11-05 07:16:14 -08002134static int
2135_posix_traverse(PyObject *module, visitproc visit, void *arg)
2136{
Hai Shif707d942020-03-16 21:15:01 +08002137 Py_VISIT(get_posix_state(module)->billion);
2138 Py_VISIT(get_posix_state(module)->DirEntryType);
2139 Py_VISIT(get_posix_state(module)->ScandirIteratorType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002140#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Hai Shif707d942020-03-16 21:15:01 +08002141 Py_VISIT(get_posix_state(module)->SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002142#endif
Hai Shif707d942020-03-16 21:15:01 +08002143 Py_VISIT(get_posix_state(module)->StatResultType);
2144 Py_VISIT(get_posix_state(module)->StatVFSResultType);
2145 Py_VISIT(get_posix_state(module)->TerminalSizeType);
2146 Py_VISIT(get_posix_state(module)->TimesResultType);
2147 Py_VISIT(get_posix_state(module)->UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002148#if defined(HAVE_WAITID) && !defined(__APPLE__)
Hai Shif707d942020-03-16 21:15:01 +08002149 Py_VISIT(get_posix_state(module)->WaitidResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002150#endif
2151#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Hai Shif707d942020-03-16 21:15:01 +08002152 Py_VISIT(get_posix_state(module)->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08002153#endif
Hai Shif707d942020-03-16 21:15:01 +08002154 Py_VISIT(get_posix_state(module)->st_mode);
Eddie Elizondob3966632019-11-05 07:16:14 -08002155 return 0;
2156}
2157
2158static void
2159_posix_free(void *module)
2160{
2161 _posix_clear((PyObject *)module);
2162}
Larry Hastings6fe20b32012-04-19 15:07:49 -07002163
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002164static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002165fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002166{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002167 PyObject *s = _PyLong_FromTime_t(sec);
2168 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2169 PyObject *s_in_ns = NULL;
2170 PyObject *ns_total = NULL;
2171 PyObject *float_s = NULL;
2172
2173 if (!(s && ns_fractional))
2174 goto exit;
2175
Eddie Elizondob3966632019-11-05 07:16:14 -08002176 s_in_ns = PyNumber_Multiply(s, _posixstate_global->billion);
Larry Hastings6fe20b32012-04-19 15:07:49 -07002177 if (!s_in_ns)
2178 goto exit;
2179
2180 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2181 if (!ns_total)
2182 goto exit;
2183
Victor Stinner01b5aab2017-10-24 02:02:00 -07002184 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2185 if (!float_s) {
2186 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002187 }
2188
2189 PyStructSequence_SET_ITEM(v, index, s);
2190 PyStructSequence_SET_ITEM(v, index+3, float_s);
2191 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2192 s = NULL;
2193 float_s = NULL;
2194 ns_total = NULL;
2195exit:
2196 Py_XDECREF(s);
2197 Py_XDECREF(ns_fractional);
2198 Py_XDECREF(s_in_ns);
2199 Py_XDECREF(ns_total);
2200 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002201}
2202
Tim Peters5aa91602002-01-30 05:46:57 +00002203/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002204 (used by posix_stat() and posix_fstat()) */
2205static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002206_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002207{
Victor Stinner8c62be82010-05-06 00:08:46 +00002208 unsigned long ansec, mnsec, cnsec;
Eddie Elizondob3966632019-11-05 07:16:14 -08002209 PyObject *StatResultType = _posixstate_global->StatResultType;
2210 PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002211 if (v == NULL)
2212 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002213
Victor Stinner8c62be82010-05-06 00:08:46 +00002214 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002215 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002216 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002217#ifdef MS_WINDOWS
2218 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002219#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002220 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002221#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002222 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002223#if defined(MS_WINDOWS)
2224 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2225 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2226#else
2227 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2228 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2229#endif
xdegaye50e86032017-05-22 11:15:08 +02002230 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2231 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002232
Martin v. Löwis14694662006-02-03 12:54:16 +00002233#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002234 ansec = st->st_atim.tv_nsec;
2235 mnsec = st->st_mtim.tv_nsec;
2236 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002237#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002238 ansec = st->st_atimespec.tv_nsec;
2239 mnsec = st->st_mtimespec.tv_nsec;
2240 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002241#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002242 ansec = st->st_atime_nsec;
2243 mnsec = st->st_mtime_nsec;
2244 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002245#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002246 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002247#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002248 fill_time(v, 7, st->st_atime, ansec);
2249 fill_time(v, 8, st->st_mtime, mnsec);
2250 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002251
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002252#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002253 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2254 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002255#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002256#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002257 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2258 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002259#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002260#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002261 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2262 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002263#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002264#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002265 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2266 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002267#endif
2268#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002269 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002270 PyObject *val;
2271 unsigned long bsec,bnsec;
2272 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002273#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002274 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002275#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002276 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002277#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002278 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002279 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2280 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002281 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002282#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002283#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002284 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2285 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002286#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002287#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2288 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2289 PyLong_FromUnsignedLong(st->st_file_attributes));
2290#endif
jcea6c51d512018-01-28 14:00:08 +01002291#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2292 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2293 PyUnicode_FromString(st->st_fstype));
2294#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002295#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2296 PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX,
2297 PyLong_FromUnsignedLong(st->st_reparse_tag));
2298#endif
Fred Drake699f3522000-06-29 21:12:41 +00002299
Victor Stinner8c62be82010-05-06 00:08:46 +00002300 if (PyErr_Occurred()) {
2301 Py_DECREF(v);
2302 return NULL;
2303 }
Fred Drake699f3522000-06-29 21:12:41 +00002304
Victor Stinner8c62be82010-05-06 00:08:46 +00002305 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002306}
2307
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002308/* POSIX methods */
2309
Guido van Rossum94f6f721999-01-06 18:42:14 +00002310
2311static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002312posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002313 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002314{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002315 STRUCT_STAT st;
2316 int result;
2317
2318#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2319 if (follow_symlinks_specified(function_name, follow_symlinks))
2320 return NULL;
2321#endif
2322
2323 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2324 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2325 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2326 return NULL;
2327
2328 Py_BEGIN_ALLOW_THREADS
2329 if (path->fd != -1)
2330 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002331#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002332 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002333 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002334 else
Steve Dowercc16be82016-09-08 10:35:16 -07002335 result = win32_lstat(path->wide, &st);
2336#else
2337 else
2338#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002339 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2340 result = LSTAT(path->narrow, &st);
2341 else
Steve Dowercc16be82016-09-08 10:35:16 -07002342#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002343#ifdef HAVE_FSTATAT
2344 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2345 result = fstatat(dir_fd, path->narrow, &st,
2346 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2347 else
Steve Dowercc16be82016-09-08 10:35:16 -07002348#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002349 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002350#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002351 Py_END_ALLOW_THREADS
2352
Victor Stinner292c8352012-10-30 02:17:38 +01002353 if (result != 0) {
2354 return path_error(path);
2355 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002356
2357 return _pystat_fromstructstat(&st);
2358}
2359
Larry Hastings2f936352014-08-05 14:04:04 +10002360/*[python input]
2361
2362for s in """
2363
2364FACCESSAT
2365FCHMODAT
2366FCHOWNAT
2367FSTATAT
2368LINKAT
2369MKDIRAT
2370MKFIFOAT
2371MKNODAT
2372OPENAT
2373READLINKAT
2374SYMLINKAT
2375UNLINKAT
2376
2377""".strip().split():
2378 s = s.strip()
2379 print("""
2380#ifdef HAVE_{s}
2381 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002382#else
Larry Hastings2f936352014-08-05 14:04:04 +10002383 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002384#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002385""".rstrip().format(s=s))
2386
2387for s in """
2388
2389FCHDIR
2390FCHMOD
2391FCHOWN
2392FDOPENDIR
2393FEXECVE
2394FPATHCONF
2395FSTATVFS
2396FTRUNCATE
2397
2398""".strip().split():
2399 s = s.strip()
2400 print("""
2401#ifdef HAVE_{s}
2402 #define PATH_HAVE_{s} 1
2403#else
2404 #define PATH_HAVE_{s} 0
2405#endif
2406
2407""".rstrip().format(s=s))
2408[python start generated code]*/
2409
2410#ifdef HAVE_FACCESSAT
2411 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2412#else
2413 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2414#endif
2415
2416#ifdef HAVE_FCHMODAT
2417 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2418#else
2419 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2420#endif
2421
2422#ifdef HAVE_FCHOWNAT
2423 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2424#else
2425 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2426#endif
2427
2428#ifdef HAVE_FSTATAT
2429 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2430#else
2431 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2432#endif
2433
2434#ifdef HAVE_LINKAT
2435 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2436#else
2437 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2438#endif
2439
2440#ifdef HAVE_MKDIRAT
2441 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2442#else
2443 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2444#endif
2445
2446#ifdef HAVE_MKFIFOAT
2447 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2448#else
2449 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2450#endif
2451
2452#ifdef HAVE_MKNODAT
2453 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2454#else
2455 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2456#endif
2457
2458#ifdef HAVE_OPENAT
2459 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2460#else
2461 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2462#endif
2463
2464#ifdef HAVE_READLINKAT
2465 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2466#else
2467 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2468#endif
2469
2470#ifdef HAVE_SYMLINKAT
2471 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2472#else
2473 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2474#endif
2475
2476#ifdef HAVE_UNLINKAT
2477 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2478#else
2479 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2480#endif
2481
2482#ifdef HAVE_FCHDIR
2483 #define PATH_HAVE_FCHDIR 1
2484#else
2485 #define PATH_HAVE_FCHDIR 0
2486#endif
2487
2488#ifdef HAVE_FCHMOD
2489 #define PATH_HAVE_FCHMOD 1
2490#else
2491 #define PATH_HAVE_FCHMOD 0
2492#endif
2493
2494#ifdef HAVE_FCHOWN
2495 #define PATH_HAVE_FCHOWN 1
2496#else
2497 #define PATH_HAVE_FCHOWN 0
2498#endif
2499
2500#ifdef HAVE_FDOPENDIR
2501 #define PATH_HAVE_FDOPENDIR 1
2502#else
2503 #define PATH_HAVE_FDOPENDIR 0
2504#endif
2505
2506#ifdef HAVE_FEXECVE
2507 #define PATH_HAVE_FEXECVE 1
2508#else
2509 #define PATH_HAVE_FEXECVE 0
2510#endif
2511
2512#ifdef HAVE_FPATHCONF
2513 #define PATH_HAVE_FPATHCONF 1
2514#else
2515 #define PATH_HAVE_FPATHCONF 0
2516#endif
2517
2518#ifdef HAVE_FSTATVFS
2519 #define PATH_HAVE_FSTATVFS 1
2520#else
2521 #define PATH_HAVE_FSTATVFS 0
2522#endif
2523
2524#ifdef HAVE_FTRUNCATE
2525 #define PATH_HAVE_FTRUNCATE 1
2526#else
2527 #define PATH_HAVE_FTRUNCATE 0
2528#endif
2529/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002530
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002531#ifdef MS_WINDOWS
2532 #undef PATH_HAVE_FTRUNCATE
2533 #define PATH_HAVE_FTRUNCATE 1
2534#endif
Larry Hastings31826802013-10-19 00:09:25 -07002535
Larry Hastings61272b72014-01-07 12:41:53 -08002536/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002537
2538class path_t_converter(CConverter):
2539
2540 type = "path_t"
2541 impl_by_reference = True
2542 parse_by_reference = True
2543
2544 converter = 'path_converter'
2545
2546 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002547 # right now path_t doesn't support default values.
2548 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002549 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002550 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002551
Larry Hastings2f936352014-08-05 14:04:04 +10002552 if self.c_default not in (None, 'Py_None'):
2553 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002554
2555 self.nullable = nullable
2556 self.allow_fd = allow_fd
2557
Larry Hastings7726ac92014-01-31 22:03:12 -08002558 def pre_render(self):
2559 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002560 if isinstance(value, str):
2561 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002562 return str(int(bool(value)))
2563
2564 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002565 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002566 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002567 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002568 strify(self.nullable),
2569 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002570 )
2571
2572 def cleanup(self):
2573 return "path_cleanup(&" + self.name + ");\n"
2574
2575
2576class dir_fd_converter(CConverter):
2577 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002578
Larry Hastings2f936352014-08-05 14:04:04 +10002579 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002580 if self.default in (unspecified, None):
2581 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002582 if isinstance(requires, str):
2583 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2584 else:
2585 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002586
Larry Hastings2f936352014-08-05 14:04:04 +10002587class fildes_converter(CConverter):
2588 type = 'int'
2589 converter = 'fildes_converter'
2590
2591class uid_t_converter(CConverter):
2592 type = "uid_t"
2593 converter = '_Py_Uid_Converter'
2594
2595class gid_t_converter(CConverter):
2596 type = "gid_t"
2597 converter = '_Py_Gid_Converter'
2598
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002599class dev_t_converter(CConverter):
2600 type = 'dev_t'
2601 converter = '_Py_Dev_Converter'
2602
2603class dev_t_return_converter(unsigned_long_return_converter):
2604 type = 'dev_t'
2605 conversion_fn = '_PyLong_FromDev'
2606 unsigned_cast = '(dev_t)'
2607
Larry Hastings2f936352014-08-05 14:04:04 +10002608class FSConverter_converter(CConverter):
2609 type = 'PyObject *'
2610 converter = 'PyUnicode_FSConverter'
2611 def converter_init(self):
2612 if self.default is not unspecified:
2613 fail("FSConverter_converter does not support default values")
2614 self.c_default = 'NULL'
2615
2616 def cleanup(self):
2617 return "Py_XDECREF(" + self.name + ");\n"
2618
2619class pid_t_converter(CConverter):
2620 type = 'pid_t'
2621 format_unit = '" _Py_PARSE_PID "'
2622
2623class idtype_t_converter(int_converter):
2624 type = 'idtype_t'
2625
2626class id_t_converter(CConverter):
2627 type = 'id_t'
2628 format_unit = '" _Py_PARSE_PID "'
2629
Benjamin Petersonca470632016-09-06 13:47:26 -07002630class intptr_t_converter(CConverter):
2631 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002632 format_unit = '" _Py_PARSE_INTPTR "'
2633
2634class Py_off_t_converter(CConverter):
2635 type = 'Py_off_t'
2636 converter = 'Py_off_t_converter'
2637
2638class Py_off_t_return_converter(long_return_converter):
2639 type = 'Py_off_t'
2640 conversion_fn = 'PyLong_FromPy_off_t'
2641
2642class path_confname_converter(CConverter):
2643 type="int"
2644 converter="conv_path_confname"
2645
2646class confstr_confname_converter(path_confname_converter):
2647 converter='conv_confstr_confname'
2648
2649class sysconf_confname_converter(path_confname_converter):
2650 converter="conv_sysconf_confname"
2651
2652class sched_param_converter(CConverter):
2653 type = 'struct sched_param'
2654 converter = 'convert_sched_param'
2655 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002656
Larry Hastings61272b72014-01-07 12:41:53 -08002657[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002658/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002659
Larry Hastings61272b72014-01-07 12:41:53 -08002660/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002661
Larry Hastings2a727912014-01-16 11:32:01 -08002662os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002663
2664 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002665 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002666 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002667
2668 *
2669
Larry Hastings2f936352014-08-05 14:04:04 +10002670 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002671 If not None, it should be a file descriptor open to a directory,
2672 and path should be a relative string; path will then be relative to
2673 that directory.
2674
2675 follow_symlinks: bool = True
2676 If False, and the last element of the path is a symbolic link,
2677 stat will examine the symbolic link itself instead of the file
2678 the link points to.
2679
2680Perform a stat system call on the given path.
2681
2682dir_fd and follow_symlinks may not be implemented
2683 on your platform. If they are unavailable, using them will raise a
2684 NotImplementedError.
2685
2686It's an error to use dir_fd or follow_symlinks when specifying path as
2687 an open file descriptor.
2688
Larry Hastings61272b72014-01-07 12:41:53 -08002689[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002690
Larry Hastings31826802013-10-19 00:09:25 -07002691static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002692os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002693/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002694{
2695 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2696}
2697
Larry Hastings2f936352014-08-05 14:04:04 +10002698
2699/*[clinic input]
2700os.lstat
2701
2702 path : path_t
2703
2704 *
2705
2706 dir_fd : dir_fd(requires='fstatat') = None
2707
2708Perform a stat system call on the given path, without following symbolic links.
2709
2710Like stat(), but do not follow symbolic links.
2711Equivalent to stat(path, follow_symlinks=False).
2712[clinic start generated code]*/
2713
Larry Hastings2f936352014-08-05 14:04:04 +10002714static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002715os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2716/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002717{
2718 int follow_symlinks = 0;
2719 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2720}
Larry Hastings31826802013-10-19 00:09:25 -07002721
Larry Hastings2f936352014-08-05 14:04:04 +10002722
Larry Hastings61272b72014-01-07 12:41:53 -08002723/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002724os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002725
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002726 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002727 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002728
2729 mode: int
2730 Operating-system mode bitfield. Can be F_OK to test existence,
2731 or the inclusive-OR of R_OK, W_OK, and X_OK.
2732
2733 *
2734
Larry Hastings2f936352014-08-05 14:04:04 +10002735 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002736 If not None, it should be a file descriptor open to a directory,
2737 and path should be relative; path will then be relative to that
2738 directory.
2739
2740 effective_ids: bool = False
2741 If True, access will use the effective uid/gid instead of
2742 the real uid/gid.
2743
2744 follow_symlinks: bool = True
2745 If False, and the last element of the path is a symbolic link,
2746 access will examine the symbolic link itself instead of the file
2747 the link points to.
2748
2749Use the real uid/gid to test for access to a path.
2750
2751{parameters}
2752dir_fd, effective_ids, and follow_symlinks may not be implemented
2753 on your platform. If they are unavailable, using them will raise a
2754 NotImplementedError.
2755
2756Note that most operations will use the effective uid/gid, therefore this
2757 routine can be used in a suid/sgid environment to test if the invoking user
2758 has the specified access to the path.
2759
Larry Hastings61272b72014-01-07 12:41:53 -08002760[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002761
Larry Hastings2f936352014-08-05 14:04:04 +10002762static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002763os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002764 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002765/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002766{
Larry Hastings2f936352014-08-05 14:04:04 +10002767 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002768
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002769#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002770 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002771#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002772 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002773#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002774
Larry Hastings9cf065c2012-06-22 16:30:09 -07002775#ifndef HAVE_FACCESSAT
2776 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002777 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002778
2779 if (effective_ids) {
2780 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002781 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002782 }
2783#endif
2784
2785#ifdef MS_WINDOWS
2786 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002787 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002788 Py_END_ALLOW_THREADS
2789
2790 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002791 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002792 * * we didn't get a -1, and
2793 * * write access wasn't requested,
2794 * * or the file isn't read-only,
2795 * * or it's a directory.
2796 * (Directories cannot be read-only on Windows.)
2797 */
Larry Hastings2f936352014-08-05 14:04:04 +10002798 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002799 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002800 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002801 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002802#else
2803
2804 Py_BEGIN_ALLOW_THREADS
2805#ifdef HAVE_FACCESSAT
2806 if ((dir_fd != DEFAULT_DIR_FD) ||
2807 effective_ids ||
2808 !follow_symlinks) {
2809 int flags = 0;
2810 if (!follow_symlinks)
2811 flags |= AT_SYMLINK_NOFOLLOW;
2812 if (effective_ids)
2813 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002814 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002815 }
2816 else
2817#endif
Larry Hastings31826802013-10-19 00:09:25 -07002818 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002819 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002820 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002821#endif
2822
Larry Hastings9cf065c2012-06-22 16:30:09 -07002823 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002824}
2825
Guido van Rossumd371ff11999-01-25 16:12:23 +00002826#ifndef F_OK
2827#define F_OK 0
2828#endif
2829#ifndef R_OK
2830#define R_OK 4
2831#endif
2832#ifndef W_OK
2833#define W_OK 2
2834#endif
2835#ifndef X_OK
2836#define X_OK 1
2837#endif
2838
Larry Hastings31826802013-10-19 00:09:25 -07002839
Guido van Rossumd371ff11999-01-25 16:12:23 +00002840#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002841/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002842os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002843
2844 fd: int
2845 Integer file descriptor handle.
2846
2847 /
2848
2849Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002850[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002851
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002852static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002853os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002854/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002855{
Guido van Rossum94f6f721999-01-06 18:42:14 +00002856
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002857 long size = sysconf(_SC_TTY_NAME_MAX);
2858 if (size == -1) {
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002859 return posix_error();
2860 }
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002861 char *buffer = (char *)PyMem_RawMalloc(size);
2862 if (buffer == NULL) {
2863 return PyErr_NoMemory();
2864 }
2865 int ret = ttyname_r(fd, buffer, size);
2866 if (ret != 0) {
2867 PyMem_RawFree(buffer);
2868 errno = ret;
2869 return posix_error();
2870 }
2871 PyObject *res = PyUnicode_DecodeFSDefault(buffer);
2872 PyMem_RawFree(buffer);
2873 return res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002874}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002875#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002876
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002877#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002878/*[clinic input]
2879os.ctermid
2880
2881Return the name of the controlling terminal for this process.
2882[clinic start generated code]*/
2883
Larry Hastings2f936352014-08-05 14:04:04 +10002884static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002885os_ctermid_impl(PyObject *module)
2886/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002887{
Victor Stinner8c62be82010-05-06 00:08:46 +00002888 char *ret;
2889 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002890
Greg Wardb48bc172000-03-01 21:51:56 +00002891#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002892 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002893#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002894 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002895#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002896 if (ret == NULL)
2897 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002898 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002899}
Larry Hastings2f936352014-08-05 14:04:04 +10002900#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002901
Larry Hastings2f936352014-08-05 14:04:04 +10002902
2903/*[clinic input]
2904os.chdir
2905
2906 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2907
2908Change the current working directory to the specified path.
2909
2910path may always be specified as a string.
2911On some platforms, path may also be specified as an open file descriptor.
2912 If this functionality is unavailable, using it raises an exception.
2913[clinic start generated code]*/
2914
Larry Hastings2f936352014-08-05 14:04:04 +10002915static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002916os_chdir_impl(PyObject *module, path_t *path)
2917/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002918{
2919 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002920
Saiyang Gou7514f4f2020-02-12 23:47:42 -08002921 if (PySys_Audit("os.chdir", "(O)", path->object) < 0) {
2922 return NULL;
2923 }
2924
Larry Hastings9cf065c2012-06-22 16:30:09 -07002925 Py_BEGIN_ALLOW_THREADS
2926#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002927 /* on unix, success = 0, on windows, success = !0 */
2928 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002929#else
2930#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002931 if (path->fd != -1)
2932 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002933 else
2934#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002935 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002936#endif
2937 Py_END_ALLOW_THREADS
2938
2939 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002940 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002941 }
2942
Larry Hastings2f936352014-08-05 14:04:04 +10002943 Py_RETURN_NONE;
2944}
2945
2946
2947#ifdef HAVE_FCHDIR
2948/*[clinic input]
2949os.fchdir
2950
2951 fd: fildes
2952
2953Change to the directory of the given file descriptor.
2954
2955fd must be opened on a directory, not a file.
2956Equivalent to os.chdir(fd).
2957
2958[clinic start generated code]*/
2959
Fred Drake4d1e64b2002-04-15 19:40:07 +00002960static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002961os_fchdir_impl(PyObject *module, int fd)
2962/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002963{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08002964 if (PySys_Audit("os.chdir", "(i)", fd) < 0) {
2965 return NULL;
2966 }
Larry Hastings2f936352014-08-05 14:04:04 +10002967 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002968}
2969#endif /* HAVE_FCHDIR */
2970
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002971
Larry Hastings2f936352014-08-05 14:04:04 +10002972/*[clinic input]
2973os.chmod
2974
2975 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002976 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002977 On some platforms, path may also be specified as an open file descriptor.
2978 If this functionality is unavailable, using it raises an exception.
2979
2980 mode: int
2981 Operating-system mode bitfield.
2982
2983 *
2984
2985 dir_fd : dir_fd(requires='fchmodat') = None
2986 If not None, it should be a file descriptor open to a directory,
2987 and path should be relative; path will then be relative to that
2988 directory.
2989
2990 follow_symlinks: bool = True
2991 If False, and the last element of the path is a symbolic link,
2992 chmod will modify the symbolic link itself instead of the file
2993 the link points to.
2994
2995Change the access permissions of a file.
2996
2997It is an error to use dir_fd or follow_symlinks when specifying path as
2998 an open file descriptor.
2999dir_fd and follow_symlinks may not be implemented on your platform.
3000 If they are unavailable, using them will raise a NotImplementedError.
3001
3002[clinic start generated code]*/
3003
Larry Hastings2f936352014-08-05 14:04:04 +10003004static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003005os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003006 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003007/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003008{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003009 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003010
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003011#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003012 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003013#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003014
Larry Hastings9cf065c2012-06-22 16:30:09 -07003015#ifdef HAVE_FCHMODAT
3016 int fchmodat_nofollow_unsupported = 0;
3017#endif
3018
Larry Hastings9cf065c2012-06-22 16:30:09 -07003019#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
3020 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003021 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003022#endif
3023
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003024 if (PySys_Audit("os.chmod", "Oii", path->object, mode,
3025 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3026 return NULL;
3027 }
3028
Larry Hastings9cf065c2012-06-22 16:30:09 -07003029#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003030 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003031 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01003032 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003033 result = 0;
3034 else {
3035 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00003036 attr &= ~FILE_ATTRIBUTE_READONLY;
3037 else
3038 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07003039 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003040 }
3041 Py_END_ALLOW_THREADS
3042
3043 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10003044 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003045 }
3046#else /* MS_WINDOWS */
3047 Py_BEGIN_ALLOW_THREADS
3048#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003049 if (path->fd != -1)
3050 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003051 else
3052#endif
3053#ifdef HAVE_LCHMOD
3054 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003055 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003056 else
3057#endif
3058#ifdef HAVE_FCHMODAT
3059 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
3060 /*
3061 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
3062 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07003063 * and then says it isn't implemented yet.
3064 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003065 *
3066 * Once it is supported, os.chmod will automatically
3067 * support dir_fd and follow_symlinks=False. (Hopefully.)
3068 * Until then, we need to be careful what exception we raise.
3069 */
Larry Hastings2f936352014-08-05 14:04:04 +10003070 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003071 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3072 /*
3073 * But wait! We can't throw the exception without allowing threads,
3074 * and we can't do that in this nested scope. (Macro trickery, sigh.)
3075 */
3076 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07003077 result &&
3078 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
3079 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00003080 }
3081 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00003082#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003083 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003084 Py_END_ALLOW_THREADS
3085
3086 if (result) {
3087#ifdef HAVE_FCHMODAT
3088 if (fchmodat_nofollow_unsupported) {
3089 if (dir_fd != DEFAULT_DIR_FD)
3090 dir_fd_and_follow_symlinks_invalid("chmod",
3091 dir_fd, follow_symlinks);
3092 else
3093 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08003094 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003095 }
3096 else
3097#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003098 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003099 }
3100#endif
3101
Larry Hastings2f936352014-08-05 14:04:04 +10003102 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003103}
3104
Larry Hastings9cf065c2012-06-22 16:30:09 -07003105
Christian Heimes4e30a842007-11-30 22:12:06 +00003106#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003107/*[clinic input]
3108os.fchmod
3109
3110 fd: int
3111 mode: int
3112
3113Change the access permissions of the file given by file descriptor fd.
3114
3115Equivalent to os.chmod(fd, mode).
3116[clinic start generated code]*/
3117
Larry Hastings2f936352014-08-05 14:04:04 +10003118static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003119os_fchmod_impl(PyObject *module, int fd, int mode)
3120/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003121{
3122 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003123 int async_err = 0;
3124
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003125 if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) {
3126 return NULL;
3127 }
3128
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003129 do {
3130 Py_BEGIN_ALLOW_THREADS
3131 res = fchmod(fd, mode);
3132 Py_END_ALLOW_THREADS
3133 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3134 if (res != 0)
3135 return (!async_err) ? posix_error() : NULL;
3136
Victor Stinner8c62be82010-05-06 00:08:46 +00003137 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003138}
3139#endif /* HAVE_FCHMOD */
3140
Larry Hastings2f936352014-08-05 14:04:04 +10003141
Christian Heimes4e30a842007-11-30 22:12:06 +00003142#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003143/*[clinic input]
3144os.lchmod
3145
3146 path: path_t
3147 mode: int
3148
3149Change the access permissions of a file, without following symbolic links.
3150
3151If path is a symlink, this affects the link itself rather than the target.
3152Equivalent to chmod(path, mode, follow_symlinks=False)."
3153[clinic start generated code]*/
3154
Larry Hastings2f936352014-08-05 14:04:04 +10003155static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003156os_lchmod_impl(PyObject *module, path_t *path, int mode)
3157/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003158{
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003160 if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) {
3161 return NULL;
3162 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003164 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003165 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003166 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003167 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003168 return NULL;
3169 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003170 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003171}
3172#endif /* HAVE_LCHMOD */
3173
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003174
Thomas Wouterscf297e42007-02-23 15:07:44 +00003175#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003176/*[clinic input]
3177os.chflags
3178
3179 path: path_t
3180 flags: unsigned_long(bitwise=True)
3181 follow_symlinks: bool=True
3182
3183Set file flags.
3184
3185If follow_symlinks is False, and the last element of the path is a symbolic
3186 link, chflags will change flags on the symbolic link itself instead of the
3187 file the link points to.
3188follow_symlinks may not be implemented on your platform. If it is
3189unavailable, using it will raise a NotImplementedError.
3190
3191[clinic start generated code]*/
3192
Larry Hastings2f936352014-08-05 14:04:04 +10003193static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003194os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003195 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003196/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003197{
3198 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003199
3200#ifndef HAVE_LCHFLAGS
3201 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003202 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003203#endif
3204
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003205 if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3206 return NULL;
3207 }
3208
Victor Stinner8c62be82010-05-06 00:08:46 +00003209 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003210#ifdef HAVE_LCHFLAGS
3211 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003212 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003213 else
3214#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003215 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003216 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003217
Larry Hastings2f936352014-08-05 14:04:04 +10003218 if (result)
3219 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003220
Larry Hastings2f936352014-08-05 14:04:04 +10003221 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003222}
3223#endif /* HAVE_CHFLAGS */
3224
Larry Hastings2f936352014-08-05 14:04:04 +10003225
Thomas Wouterscf297e42007-02-23 15:07:44 +00003226#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003227/*[clinic input]
3228os.lchflags
3229
3230 path: path_t
3231 flags: unsigned_long(bitwise=True)
3232
3233Set file flags.
3234
3235This function will not follow symbolic links.
3236Equivalent to chflags(path, flags, follow_symlinks=False).
3237[clinic start generated code]*/
3238
Larry Hastings2f936352014-08-05 14:04:04 +10003239static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003240os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3241/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003242{
Victor Stinner8c62be82010-05-06 00:08:46 +00003243 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003244 if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3245 return NULL;
3246 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003247 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003248 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003250 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003251 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003252 }
Victor Stinner292c8352012-10-30 02:17:38 +01003253 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003254}
3255#endif /* HAVE_LCHFLAGS */
3256
Larry Hastings2f936352014-08-05 14:04:04 +10003257
Martin v. Löwis244edc82001-10-04 22:44:26 +00003258#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003259/*[clinic input]
3260os.chroot
3261 path: path_t
3262
3263Change root directory to path.
3264
3265[clinic start generated code]*/
3266
Larry Hastings2f936352014-08-05 14:04:04 +10003267static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003268os_chroot_impl(PyObject *module, path_t *path)
3269/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003270{
3271 int res;
3272 Py_BEGIN_ALLOW_THREADS
3273 res = chroot(path->narrow);
3274 Py_END_ALLOW_THREADS
3275 if (res < 0)
3276 return path_error(path);
3277 Py_RETURN_NONE;
3278}
3279#endif /* HAVE_CHROOT */
3280
Martin v. Löwis244edc82001-10-04 22:44:26 +00003281
Guido van Rossum21142a01999-01-08 21:05:37 +00003282#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003283/*[clinic input]
3284os.fsync
3285
3286 fd: fildes
3287
3288Force write of fd to disk.
3289[clinic start generated code]*/
3290
Larry Hastings2f936352014-08-05 14:04:04 +10003291static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003292os_fsync_impl(PyObject *module, int fd)
3293/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003294{
3295 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003296}
3297#endif /* HAVE_FSYNC */
3298
Larry Hastings2f936352014-08-05 14:04:04 +10003299
Ross Lagerwall7807c352011-03-17 20:20:30 +02003300#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003301/*[clinic input]
3302os.sync
3303
3304Force write of everything to disk.
3305[clinic start generated code]*/
3306
Larry Hastings2f936352014-08-05 14:04:04 +10003307static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003308os_sync_impl(PyObject *module)
3309/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003310{
3311 Py_BEGIN_ALLOW_THREADS
3312 sync();
3313 Py_END_ALLOW_THREADS
3314 Py_RETURN_NONE;
3315}
Larry Hastings2f936352014-08-05 14:04:04 +10003316#endif /* HAVE_SYNC */
3317
Ross Lagerwall7807c352011-03-17 20:20:30 +02003318
Guido van Rossum21142a01999-01-08 21:05:37 +00003319#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003320#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003321extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3322#endif
3323
Larry Hastings2f936352014-08-05 14:04:04 +10003324/*[clinic input]
3325os.fdatasync
3326
3327 fd: fildes
3328
3329Force write of fd to disk without forcing update of metadata.
3330[clinic start generated code]*/
3331
Larry Hastings2f936352014-08-05 14:04:04 +10003332static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003333os_fdatasync_impl(PyObject *module, int fd)
3334/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003335{
3336 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003337}
3338#endif /* HAVE_FDATASYNC */
3339
3340
Fredrik Lundh10723342000-07-10 16:38:09 +00003341#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003342/*[clinic input]
3343os.chown
3344
3345 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003346 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003347
3348 uid: uid_t
3349
3350 gid: gid_t
3351
3352 *
3353
3354 dir_fd : dir_fd(requires='fchownat') = None
3355 If not None, it should be a file descriptor open to a directory,
3356 and path should be relative; path will then be relative to that
3357 directory.
3358
3359 follow_symlinks: bool = True
3360 If False, and the last element of the path is a symbolic link,
3361 stat will examine the symbolic link itself instead of the file
3362 the link points to.
3363
3364Change the owner and group id of path to the numeric uid and gid.\
3365
3366path may always be specified as a string.
3367On some platforms, path may also be specified as an open file descriptor.
3368 If this functionality is unavailable, using it raises an exception.
3369If dir_fd is not None, it should be a file descriptor open to a directory,
3370 and path should be relative; path will then be relative to that directory.
3371If follow_symlinks is False, and the last element of the path is a symbolic
3372 link, chown will modify the symbolic link itself instead of the file the
3373 link points to.
3374It is an error to use dir_fd or follow_symlinks when specifying path as
3375 an open file descriptor.
3376dir_fd and follow_symlinks may not be implemented on your platform.
3377 If they are unavailable, using them will raise a NotImplementedError.
3378
3379[clinic start generated code]*/
3380
Larry Hastings2f936352014-08-05 14:04:04 +10003381static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003382os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003383 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003384/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003385{
3386 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003387
3388#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3389 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003390 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003391#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003392 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3393 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3394 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003395
3396#ifdef __APPLE__
3397 /*
3398 * This is for Mac OS X 10.3, which doesn't have lchown.
3399 * (But we still have an lchown symbol because of weak-linking.)
3400 * It doesn't have fchownat either. So there's no possibility
3401 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003402 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003403 if ((!follow_symlinks) && (lchown == NULL)) {
3404 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003405 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003406 }
3407#endif
3408
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003409 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid,
3410 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3411 return NULL;
3412 }
3413
Victor Stinner8c62be82010-05-06 00:08:46 +00003414 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003415#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003416 if (path->fd != -1)
3417 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003418 else
3419#endif
3420#ifdef HAVE_LCHOWN
3421 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003422 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003423 else
3424#endif
3425#ifdef HAVE_FCHOWNAT
3426 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003427 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003428 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3429 else
3430#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003431 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003432 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003433
Larry Hastings2f936352014-08-05 14:04:04 +10003434 if (result)
3435 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003436
Larry Hastings2f936352014-08-05 14:04:04 +10003437 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003438}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003439#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003440
Larry Hastings2f936352014-08-05 14:04:04 +10003441
Christian Heimes4e30a842007-11-30 22:12:06 +00003442#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003443/*[clinic input]
3444os.fchown
3445
3446 fd: int
3447 uid: uid_t
3448 gid: gid_t
3449
3450Change the owner and group id of the file specified by file descriptor.
3451
3452Equivalent to os.chown(fd, uid, gid).
3453
3454[clinic start generated code]*/
3455
Larry Hastings2f936352014-08-05 14:04:04 +10003456static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003457os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3458/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003459{
Victor Stinner8c62be82010-05-06 00:08:46 +00003460 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003461 int async_err = 0;
3462
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003463 if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) {
3464 return NULL;
3465 }
3466
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003467 do {
3468 Py_BEGIN_ALLOW_THREADS
3469 res = fchown(fd, uid, gid);
3470 Py_END_ALLOW_THREADS
3471 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3472 if (res != 0)
3473 return (!async_err) ? posix_error() : NULL;
3474
Victor Stinner8c62be82010-05-06 00:08:46 +00003475 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003476}
3477#endif /* HAVE_FCHOWN */
3478
Larry Hastings2f936352014-08-05 14:04:04 +10003479
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003480#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003481/*[clinic input]
3482os.lchown
3483
3484 path : path_t
3485 uid: uid_t
3486 gid: gid_t
3487
3488Change the owner and group id of path to the numeric uid and gid.
3489
3490This function will not follow symbolic links.
3491Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3492[clinic start generated code]*/
3493
Larry Hastings2f936352014-08-05 14:04:04 +10003494static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003495os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3496/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003497{
Victor Stinner8c62be82010-05-06 00:08:46 +00003498 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003499 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) {
3500 return NULL;
3501 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003502 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003503 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003504 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003505 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003506 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003507 }
Larry Hastings2f936352014-08-05 14:04:04 +10003508 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003509}
3510#endif /* HAVE_LCHOWN */
3511
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003512
Barry Warsaw53699e91996-12-10 23:23:01 +00003513static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003514posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003515{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003516#ifdef MS_WINDOWS
Victor Stinner689830e2019-06-26 17:31:12 +02003517 wchar_t wbuf[MAXPATHLEN];
3518 wchar_t *wbuf2 = wbuf;
3519 DWORD len;
3520
3521 Py_BEGIN_ALLOW_THREADS
3522 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3523 /* If the buffer is large enough, len does not include the
3524 terminating \0. If the buffer is too small, len includes
3525 the space needed for the terminator. */
3526 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerec3e20a2019-06-28 18:01:59 +02003527 if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003528 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003529 }
Victor Stinner689830e2019-06-26 17:31:12 +02003530 else {
3531 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003532 }
Victor Stinner689830e2019-06-26 17:31:12 +02003533 if (wbuf2) {
3534 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003535 }
Victor Stinner689830e2019-06-26 17:31:12 +02003536 }
3537 Py_END_ALLOW_THREADS
3538
3539 if (!wbuf2) {
3540 PyErr_NoMemory();
3541 return NULL;
3542 }
3543 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003544 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003545 PyMem_RawFree(wbuf2);
Victor Stinner689830e2019-06-26 17:31:12 +02003546 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003548
Victor Stinner689830e2019-06-26 17:31:12 +02003549 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3550 if (wbuf2 != wbuf) {
3551 PyMem_RawFree(wbuf2);
3552 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003553
Victor Stinner689830e2019-06-26 17:31:12 +02003554 if (use_bytes) {
3555 if (resobj == NULL) {
3556 return NULL;
3557 }
3558 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3559 }
3560
3561 return resobj;
3562#else
3563 const size_t chunk = 1024;
3564
3565 char *buf = NULL;
3566 char *cwd = NULL;
3567 size_t buflen = 0;
3568
Victor Stinner8c62be82010-05-06 00:08:46 +00003569 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003570 do {
Victor Stinner689830e2019-06-26 17:31:12 +02003571 char *newbuf;
3572 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3573 buflen += chunk;
3574 newbuf = PyMem_RawRealloc(buf, buflen);
3575 }
3576 else {
3577 newbuf = NULL;
3578 }
3579 if (newbuf == NULL) {
3580 PyMem_RawFree(buf);
3581 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003582 break;
3583 }
Victor Stinner689830e2019-06-26 17:31:12 +02003584 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003585
Victor Stinner4403d7d2015-04-25 00:16:10 +02003586 cwd = getcwd(buf, buflen);
3587 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003588 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003589
Victor Stinner689830e2019-06-26 17:31:12 +02003590 if (buf == NULL) {
3591 return PyErr_NoMemory();
3592 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003593 if (cwd == NULL) {
3594 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003595 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003596 }
3597
Victor Stinner689830e2019-06-26 17:31:12 +02003598 PyObject *obj;
3599 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003600 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner689830e2019-06-26 17:31:12 +02003601 }
3602 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003603 obj = PyUnicode_DecodeFSDefault(buf);
Victor Stinner689830e2019-06-26 17:31:12 +02003604 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003605 PyMem_RawFree(buf);
3606
3607 return obj;
Victor Stinner689830e2019-06-26 17:31:12 +02003608#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003609}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003610
Larry Hastings2f936352014-08-05 14:04:04 +10003611
3612/*[clinic input]
3613os.getcwd
3614
3615Return a unicode string representing the current working directory.
3616[clinic start generated code]*/
3617
Larry Hastings2f936352014-08-05 14:04:04 +10003618static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003619os_getcwd_impl(PyObject *module)
3620/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003621{
3622 return posix_getcwd(0);
3623}
3624
Larry Hastings2f936352014-08-05 14:04:04 +10003625
3626/*[clinic input]
3627os.getcwdb
3628
3629Return a bytes string representing the current working directory.
3630[clinic start generated code]*/
3631
Larry Hastings2f936352014-08-05 14:04:04 +10003632static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003633os_getcwdb_impl(PyObject *module)
3634/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003635{
3636 return posix_getcwd(1);
3637}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003638
Larry Hastings2f936352014-08-05 14:04:04 +10003639
Larry Hastings9cf065c2012-06-22 16:30:09 -07003640#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3641#define HAVE_LINK 1
3642#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003643
Guido van Rossumb6775db1994-08-01 11:34:53 +00003644#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003645/*[clinic input]
3646
3647os.link
3648
3649 src : path_t
3650 dst : path_t
3651 *
3652 src_dir_fd : dir_fd = None
3653 dst_dir_fd : dir_fd = None
3654 follow_symlinks: bool = True
3655
3656Create a hard link to a file.
3657
3658If either src_dir_fd or dst_dir_fd is not None, it should be a file
3659 descriptor open to a directory, and the respective path string (src or dst)
3660 should be relative; the path will then be relative to that directory.
3661If follow_symlinks is False, and the last element of src is a symbolic
3662 link, link will create a link to the symbolic link itself instead of the
3663 file the link points to.
3664src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3665 platform. If they are unavailable, using them will raise a
3666 NotImplementedError.
3667[clinic start generated code]*/
3668
Larry Hastings2f936352014-08-05 14:04:04 +10003669static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003670os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003671 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003672/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003673{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003674#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003675 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003676#else
3677 int result;
3678#endif
3679
Larry Hastings9cf065c2012-06-22 16:30:09 -07003680#ifndef HAVE_LINKAT
3681 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3682 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003683 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003684 }
3685#endif
3686
Steve Dowercc16be82016-09-08 10:35:16 -07003687#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003688 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003689 PyErr_SetString(PyExc_NotImplementedError,
3690 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003691 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003692 }
Steve Dowercc16be82016-09-08 10:35:16 -07003693#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003694
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003695 if (PySys_Audit("os.link", "OOii", src->object, dst->object,
3696 src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
3697 dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
3698 return NULL;
3699 }
3700
Brian Curtin1b9df392010-11-24 20:24:31 +00003701#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003702 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003703 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003704 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003705
Larry Hastings2f936352014-08-05 14:04:04 +10003706 if (!result)
3707 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003708#else
3709 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003710#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003711 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3712 (dst_dir_fd != DEFAULT_DIR_FD) ||
3713 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003714 result = linkat(src_dir_fd, src->narrow,
3715 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003716 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3717 else
Steve Dowercc16be82016-09-08 10:35:16 -07003718#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003719 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003720 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003721
Larry Hastings2f936352014-08-05 14:04:04 +10003722 if (result)
3723 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003724#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003725
Larry Hastings2f936352014-08-05 14:04:04 +10003726 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003727}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003728#endif
3729
Brian Curtin1b9df392010-11-24 20:24:31 +00003730
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003731#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003732static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003733_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003734{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003735 PyObject *v;
3736 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3737 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003738 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003739 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003740 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003741 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003742
Steve Dowercc16be82016-09-08 10:35:16 -07003743 WIN32_FIND_DATAW wFileData;
3744 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003745
Steve Dowercc16be82016-09-08 10:35:16 -07003746 if (!path->wide) { /* Default arg: "." */
3747 po_wchars = L".";
3748 len = 1;
3749 } else {
3750 po_wchars = path->wide;
3751 len = wcslen(path->wide);
3752 }
3753 /* The +5 is so we can append "\\*.*\0" */
3754 wnamebuf = PyMem_New(wchar_t, len + 5);
3755 if (!wnamebuf) {
3756 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003757 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003758 }
Steve Dowercc16be82016-09-08 10:35:16 -07003759 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003760 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003761 wchar_t wch = wnamebuf[len-1];
3762 if (wch != SEP && wch != ALTSEP && wch != L':')
3763 wnamebuf[len++] = SEP;
3764 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003765 }
Steve Dowercc16be82016-09-08 10:35:16 -07003766 if ((list = PyList_New(0)) == NULL) {
3767 goto exit;
3768 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003769 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003770 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003771 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003772 if (hFindFile == INVALID_HANDLE_VALUE) {
3773 int error = GetLastError();
3774 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003775 goto exit;
3776 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003777 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003778 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003779 }
3780 do {
3781 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003782 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3783 wcscmp(wFileData.cFileName, L"..") != 0) {
3784 v = PyUnicode_FromWideChar(wFileData.cFileName,
3785 wcslen(wFileData.cFileName));
3786 if (path->narrow && v) {
3787 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3788 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003789 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003790 Py_DECREF(list);
3791 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003792 break;
3793 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003794 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003795 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003796 Py_DECREF(list);
3797 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003798 break;
3799 }
3800 Py_DECREF(v);
3801 }
3802 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003803 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003804 Py_END_ALLOW_THREADS
3805 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3806 it got to the end of the directory. */
3807 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003808 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003809 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003810 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003811 }
3812 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003813
Larry Hastings9cf065c2012-06-22 16:30:09 -07003814exit:
3815 if (hFindFile != INVALID_HANDLE_VALUE) {
3816 if (FindClose(hFindFile) == FALSE) {
3817 if (list != NULL) {
3818 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003819 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003820 }
3821 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003822 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003823 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003824
Larry Hastings9cf065c2012-06-22 16:30:09 -07003825 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003826} /* end of _listdir_windows_no_opendir */
3827
3828#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3829
3830static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003831_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003832{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003833 PyObject *v;
3834 DIR *dirp = NULL;
3835 struct dirent *ep;
3836 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003837#ifdef HAVE_FDOPENDIR
3838 int fd = -1;
3839#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003840
Victor Stinner8c62be82010-05-06 00:08:46 +00003841 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003842#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003843 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003844 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003845 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003846 if (fd == -1)
3847 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003848
Larry Hastingsfdaea062012-06-25 04:42:23 -07003849 return_str = 1;
3850
Larry Hastings9cf065c2012-06-22 16:30:09 -07003851 Py_BEGIN_ALLOW_THREADS
3852 dirp = fdopendir(fd);
3853 Py_END_ALLOW_THREADS
3854 }
3855 else
3856#endif
3857 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003858 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003859 if (path->narrow) {
3860 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003861 /* only return bytes if they specified a bytes-like object */
3862 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003863 }
3864 else {
3865 name = ".";
3866 return_str = 1;
3867 }
3868
Larry Hastings9cf065c2012-06-22 16:30:09 -07003869 Py_BEGIN_ALLOW_THREADS
3870 dirp = opendir(name);
3871 Py_END_ALLOW_THREADS
3872 }
3873
3874 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003875 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003876#ifdef HAVE_FDOPENDIR
3877 if (fd != -1) {
3878 Py_BEGIN_ALLOW_THREADS
3879 close(fd);
3880 Py_END_ALLOW_THREADS
3881 }
3882#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003883 goto exit;
3884 }
3885 if ((list = PyList_New(0)) == NULL) {
3886 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003887 }
3888 for (;;) {
3889 errno = 0;
3890 Py_BEGIN_ALLOW_THREADS
3891 ep = readdir(dirp);
3892 Py_END_ALLOW_THREADS
3893 if (ep == NULL) {
3894 if (errno == 0) {
3895 break;
3896 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003897 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003898 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003899 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003900 }
3901 }
3902 if (ep->d_name[0] == '.' &&
3903 (NAMLEN(ep) == 1 ||
3904 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3905 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003906 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003907 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3908 else
3909 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003910 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003911 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003912 break;
3913 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003914 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003915 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003916 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003917 break;
3918 }
3919 Py_DECREF(v);
3920 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003921
Larry Hastings9cf065c2012-06-22 16:30:09 -07003922exit:
3923 if (dirp != NULL) {
3924 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003925#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003926 if (fd > -1)
3927 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003928#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003929 closedir(dirp);
3930 Py_END_ALLOW_THREADS
3931 }
3932
Larry Hastings9cf065c2012-06-22 16:30:09 -07003933 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003934} /* end of _posix_listdir */
3935#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003936
Larry Hastings2f936352014-08-05 14:04:04 +10003937
3938/*[clinic input]
3939os.listdir
3940
3941 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3942
3943Return a list containing the names of the files in the directory.
3944
BNMetricsb9427072018-11-02 15:20:19 +00003945path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003946 the filenames returned will also be bytes; in all other circumstances
3947 the filenames returned will be str.
3948If path is None, uses the path='.'.
3949On some platforms, path may also be specified as an open file descriptor;\
3950 the file descriptor must refer to a directory.
3951 If this functionality is unavailable, using it raises NotImplementedError.
3952
3953The list is in arbitrary order. It does not include the special
3954entries '.' and '..' even if they are present in the directory.
3955
3956
3957[clinic start generated code]*/
3958
Larry Hastings2f936352014-08-05 14:04:04 +10003959static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003960os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003961/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003962{
Steve Dower60419a72019-06-24 08:42:54 -07003963 if (PySys_Audit("os.listdir", "O",
3964 path->object ? path->object : Py_None) < 0) {
3965 return NULL;
3966 }
Larry Hastings2f936352014-08-05 14:04:04 +10003967#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3968 return _listdir_windows_no_opendir(path, NULL);
3969#else
3970 return _posix_listdir(path, NULL);
3971#endif
3972}
3973
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003974#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003975/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003976/*[clinic input]
3977os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003978
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003979 path: path_t
3980 /
3981
3982[clinic start generated code]*/
3983
3984static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003985os__getfullpathname_impl(PyObject *module, path_t *path)
3986/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003987{
Victor Stinner3939c322019-06-25 15:02:43 +02003988 wchar_t *abspath;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003989
Victor Stinner3939c322019-06-25 15:02:43 +02003990 /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */
3991 if (_Py_abspath(path->wide, &abspath) < 0) {
3992 return win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00003993 }
Victor Stinner3939c322019-06-25 15:02:43 +02003994 if (abspath == NULL) {
3995 return PyErr_NoMemory();
3996 }
3997
3998 PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
3999 PyMem_RawFree(abspath);
4000 if (str == NULL) {
4001 return NULL;
4002 }
4003 if (path->narrow) {
4004 Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
4005 }
4006 return str;
Larry Hastings2f936352014-08-05 14:04:04 +10004007}
Brian Curtind40e6f72010-07-08 21:39:08 +00004008
Brian Curtind25aef52011-06-13 15:16:04 -05004009
Larry Hastings2f936352014-08-05 14:04:04 +10004010/*[clinic input]
4011os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00004012
Steve Dower23ad6d02018-02-22 10:39:10 -08004013 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004014 /
4015
4016A helper function for samepath on windows.
4017[clinic start generated code]*/
4018
Larry Hastings2f936352014-08-05 14:04:04 +10004019static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004020os__getfinalpathname_impl(PyObject *module, path_t *path)
4021/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00004022{
4023 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004024 wchar_t buf[MAXPATHLEN], *target_path = buf;
4025 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00004026 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10004027 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004028
Steve Dower23ad6d02018-02-22 10:39:10 -08004029 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004030 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08004031 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00004032 0, /* desired access */
4033 0, /* share mode */
4034 NULL, /* security attributes */
4035 OPEN_EXISTING,
4036 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
4037 FILE_FLAG_BACKUP_SEMANTICS,
4038 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004039 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004040
Steve Dower23ad6d02018-02-22 10:39:10 -08004041 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004042 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08004043 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004044
4045 /* We have a good handle to the target, use it to determine the
4046 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004047 while (1) {
4048 Py_BEGIN_ALLOW_THREADS
4049 result_length = GetFinalPathNameByHandleW(hFile, target_path,
4050 buf_size, VOLUME_NAME_DOS);
4051 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004052
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004053 if (!result_length) {
4054 result = win32_error_object("GetFinalPathNameByHandleW",
4055 path->object);
4056 goto cleanup;
4057 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004058
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004059 if (result_length < buf_size) {
4060 break;
4061 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004062
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004063 wchar_t *tmp;
4064 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
4065 result_length * sizeof(*tmp));
4066 if (!tmp) {
4067 result = PyErr_NoMemory();
4068 goto cleanup;
4069 }
4070
4071 buf_size = result_length;
4072 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08004073 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004074
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004075 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004076 if (result && path->narrow) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004077 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004078 }
Steve Dower23ad6d02018-02-22 10:39:10 -08004079
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004080cleanup:
4081 if (target_path != buf) {
4082 PyMem_Free(target_path);
4083 }
4084 CloseHandle(hFile);
4085 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10004086}
Brian Curtin62857742010-09-06 17:07:27 +00004087
Tim Golden6b528062013-08-01 12:44:00 +01004088
Larry Hastings2f936352014-08-05 14:04:04 +10004089/*[clinic input]
4090os._getvolumepathname
4091
Steve Dower23ad6d02018-02-22 10:39:10 -08004092 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004093
4094A helper function for ismount on Win32.
4095[clinic start generated code]*/
4096
Larry Hastings2f936352014-08-05 14:04:04 +10004097static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004098os__getvolumepathname_impl(PyObject *module, path_t *path)
4099/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004100{
4101 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004102 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01004103 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01004104 BOOL ret;
4105
Tim Golden6b528062013-08-01 12:44:00 +01004106 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08004107 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01004108
Victor Stinner850a18e2017-10-24 16:53:32 -07004109 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01004110 PyErr_SetString(PyExc_OverflowError, "path too long");
4111 return NULL;
4112 }
4113
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004114 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01004115 if (mountpath == NULL)
4116 return PyErr_NoMemory();
4117
4118 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08004119 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01004120 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01004121 Py_END_ALLOW_THREADS
4122
4123 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004124 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01004125 goto exit;
4126 }
4127 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08004128 if (path->narrow)
4129 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01004130
4131exit:
4132 PyMem_Free(mountpath);
4133 return result;
4134}
Tim Golden6b528062013-08-01 12:44:00 +01004135
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004136#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004137
Larry Hastings2f936352014-08-05 14:04:04 +10004138
4139/*[clinic input]
4140os.mkdir
4141
4142 path : path_t
4143
4144 mode: int = 0o777
4145
4146 *
4147
4148 dir_fd : dir_fd(requires='mkdirat') = None
4149
4150# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4151
4152Create a directory.
4153
4154If dir_fd is not None, it should be a file descriptor open to a directory,
4155 and path should be relative; path will then be relative to that directory.
4156dir_fd may not be implemented on your platform.
4157 If it is unavailable, using it will raise a NotImplementedError.
4158
4159The mode argument is ignored on Windows.
4160[clinic start generated code]*/
4161
Larry Hastings2f936352014-08-05 14:04:04 +10004162static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004163os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4164/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004165{
4166 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004167
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004168 if (PySys_Audit("os.mkdir", "Oii", path->object, mode,
4169 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4170 return NULL;
4171 }
4172
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004173#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004174 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004175 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004176 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004177
Larry Hastings2f936352014-08-05 14:04:04 +10004178 if (!result)
4179 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004180#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004181 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004182#if HAVE_MKDIRAT
4183 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004184 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004185 else
4186#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004187#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004188 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004189#else
Larry Hastings2f936352014-08-05 14:04:04 +10004190 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004191#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004193 if (result < 0)
4194 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004195#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004196 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004197}
4198
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004199
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004200/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4201#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004202#include <sys/resource.h>
4203#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004204
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004205
4206#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004207/*[clinic input]
4208os.nice
4209
4210 increment: int
4211 /
4212
4213Add increment to the priority of process and return the new priority.
4214[clinic start generated code]*/
4215
Larry Hastings2f936352014-08-05 14:04:04 +10004216static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004217os_nice_impl(PyObject *module, int increment)
4218/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004219{
4220 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004221
Victor Stinner8c62be82010-05-06 00:08:46 +00004222 /* There are two flavours of 'nice': one that returns the new
4223 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004224 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004225 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004226
Victor Stinner8c62be82010-05-06 00:08:46 +00004227 If we are of the nice family that returns the new priority, we
4228 need to clear errno before the call, and check if errno is filled
4229 before calling posix_error() on a returnvalue of -1, because the
4230 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004231
Victor Stinner8c62be82010-05-06 00:08:46 +00004232 errno = 0;
4233 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004234#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004235 if (value == 0)
4236 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004237#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004238 if (value == -1 && errno != 0)
4239 /* either nice() or getpriority() returned an error */
4240 return posix_error();
4241 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004242}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004243#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004244
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004245
4246#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004247/*[clinic input]
4248os.getpriority
4249
4250 which: int
4251 who: int
4252
4253Return program scheduling priority.
4254[clinic start generated code]*/
4255
Larry Hastings2f936352014-08-05 14:04:04 +10004256static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004257os_getpriority_impl(PyObject *module, int which, int who)
4258/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004259{
4260 int retval;
4261
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004262 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004263 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004264 if (errno != 0)
4265 return posix_error();
4266 return PyLong_FromLong((long)retval);
4267}
4268#endif /* HAVE_GETPRIORITY */
4269
4270
4271#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004272/*[clinic input]
4273os.setpriority
4274
4275 which: int
4276 who: int
4277 priority: int
4278
4279Set program scheduling priority.
4280[clinic start generated code]*/
4281
Larry Hastings2f936352014-08-05 14:04:04 +10004282static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004283os_setpriority_impl(PyObject *module, int which, int who, int priority)
4284/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004285{
4286 int retval;
4287
4288 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004289 if (retval == -1)
4290 return posix_error();
4291 Py_RETURN_NONE;
4292}
4293#endif /* HAVE_SETPRIORITY */
4294
4295
Barry Warsaw53699e91996-12-10 23:23:01 +00004296static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004297internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is_replace)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004298{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004299 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004300 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004301
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004302#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004303 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004304 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004305#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004306 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004307#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004308
Larry Hastings9cf065c2012-06-22 16:30:09 -07004309 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4310 (dst_dir_fd != DEFAULT_DIR_FD);
4311#ifndef HAVE_RENAMEAT
4312 if (dir_fd_specified) {
4313 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004314 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004315 }
4316#endif
4317
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004318 if (PySys_Audit("os.rename", "OOii", src->object, dst->object,
4319 src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
4320 dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
4321 return NULL;
4322 }
4323
Larry Hastings9cf065c2012-06-22 16:30:09 -07004324#ifdef MS_WINDOWS
4325 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004326 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004327 Py_END_ALLOW_THREADS
4328
Larry Hastings2f936352014-08-05 14:04:04 +10004329 if (!result)
4330 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004331
4332#else
Steve Dowercc16be82016-09-08 10:35:16 -07004333 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4334 PyErr_Format(PyExc_ValueError,
4335 "%s: src and dst must be the same type", function_name);
4336 return NULL;
4337 }
4338
Larry Hastings9cf065c2012-06-22 16:30:09 -07004339 Py_BEGIN_ALLOW_THREADS
4340#ifdef HAVE_RENAMEAT
4341 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004342 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004343 else
4344#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004345 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004346 Py_END_ALLOW_THREADS
4347
Larry Hastings2f936352014-08-05 14:04:04 +10004348 if (result)
4349 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004350#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004351 Py_RETURN_NONE;
4352}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004353
Larry Hastings2f936352014-08-05 14:04:04 +10004354
4355/*[clinic input]
4356os.rename
4357
4358 src : path_t
4359 dst : path_t
4360 *
4361 src_dir_fd : dir_fd = None
4362 dst_dir_fd : dir_fd = None
4363
4364Rename a file or directory.
4365
4366If either src_dir_fd or dst_dir_fd is not None, it should be a file
4367 descriptor open to a directory, and the respective path string (src or dst)
4368 should be relative; the path will then be relative to that directory.
4369src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4370 If they are unavailable, using them will raise a NotImplementedError.
4371[clinic start generated code]*/
4372
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004373static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004374os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004375 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004376/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004377{
Larry Hastings2f936352014-08-05 14:04:04 +10004378 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004379}
4380
Larry Hastings2f936352014-08-05 14:04:04 +10004381
4382/*[clinic input]
4383os.replace = os.rename
4384
4385Rename a file or directory, overwriting the destination.
4386
4387If either src_dir_fd or dst_dir_fd is not None, it should be a file
4388 descriptor open to a directory, and the respective path string (src or dst)
4389 should be relative; the path will then be relative to that directory.
4390src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004391 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004392[clinic start generated code]*/
4393
Larry Hastings2f936352014-08-05 14:04:04 +10004394static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004395os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4396 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004397/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004398{
4399 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4400}
4401
4402
4403/*[clinic input]
4404os.rmdir
4405
4406 path: path_t
4407 *
4408 dir_fd: dir_fd(requires='unlinkat') = None
4409
4410Remove a directory.
4411
4412If dir_fd is not None, it should be a file descriptor open to a directory,
4413 and path should be relative; path will then be relative to that directory.
4414dir_fd may not be implemented on your platform.
4415 If it is unavailable, using it will raise a NotImplementedError.
4416[clinic start generated code]*/
4417
Larry Hastings2f936352014-08-05 14:04:04 +10004418static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004419os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4420/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004421{
4422 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004423
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004424 if (PySys_Audit("os.rmdir", "Oi", path->object,
4425 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4426 return NULL;
4427 }
4428
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004429 Py_BEGIN_ALLOW_THREADS
4430#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004431 /* Windows, success=1, UNIX, success=0 */
4432 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004433#else
4434#ifdef HAVE_UNLINKAT
4435 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004436 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004437 else
4438#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004439 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004440#endif
4441 Py_END_ALLOW_THREADS
4442
Larry Hastings2f936352014-08-05 14:04:04 +10004443 if (result)
4444 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004445
Larry Hastings2f936352014-08-05 14:04:04 +10004446 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004447}
4448
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004449
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004450#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004451#ifdef MS_WINDOWS
4452/*[clinic input]
4453os.system -> long
4454
4455 command: Py_UNICODE
4456
4457Execute the command in a subshell.
4458[clinic start generated code]*/
4459
Larry Hastings2f936352014-08-05 14:04:04 +10004460static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004461os_system_impl(PyObject *module, const Py_UNICODE *command)
4462/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004463{
4464 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004465
Steve Dowerfbe3c762019-10-18 00:52:15 -07004466 if (PySys_Audit("os.system", "(u)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004467 return -1;
4468 }
4469
Victor Stinner8c62be82010-05-06 00:08:46 +00004470 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004471 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004472 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004473 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004474 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004475 return result;
4476}
4477#else /* MS_WINDOWS */
4478/*[clinic input]
4479os.system -> long
4480
4481 command: FSConverter
4482
4483Execute the command in a subshell.
4484[clinic start generated code]*/
4485
Larry Hastings2f936352014-08-05 14:04:04 +10004486static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004487os_system_impl(PyObject *module, PyObject *command)
4488/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004489{
4490 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004491 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004492
Steve Dowerfbe3c762019-10-18 00:52:15 -07004493 if (PySys_Audit("os.system", "(O)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004494 return -1;
4495 }
4496
Larry Hastings2f936352014-08-05 14:04:04 +10004497 Py_BEGIN_ALLOW_THREADS
4498 result = system(bytes);
4499 Py_END_ALLOW_THREADS
4500 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004501}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004502#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004503#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004504
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004505
Larry Hastings2f936352014-08-05 14:04:04 +10004506/*[clinic input]
4507os.umask
4508
4509 mask: int
4510 /
4511
4512Set the current numeric umask and return the previous umask.
4513[clinic start generated code]*/
4514
Larry Hastings2f936352014-08-05 14:04:04 +10004515static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004516os_umask_impl(PyObject *module, int mask)
4517/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004518{
4519 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004520 if (i < 0)
4521 return posix_error();
4522 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004523}
4524
Brian Curtind40e6f72010-07-08 21:39:08 +00004525#ifdef MS_WINDOWS
4526
4527/* override the default DeleteFileW behavior so that directory
4528symlinks can be removed with this function, the same as with
4529Unix symlinks */
4530BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4531{
4532 WIN32_FILE_ATTRIBUTE_DATA info;
4533 WIN32_FIND_DATAW find_data;
4534 HANDLE find_data_handle;
4535 int is_directory = 0;
4536 int is_link = 0;
4537
4538 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4539 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004540
Brian Curtind40e6f72010-07-08 21:39:08 +00004541 /* Get WIN32_FIND_DATA structure for the path to determine if
4542 it is a symlink */
4543 if(is_directory &&
4544 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4545 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4546
4547 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004548 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4549 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4550 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4551 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004552 FindClose(find_data_handle);
4553 }
4554 }
4555 }
4556
4557 if (is_directory && is_link)
4558 return RemoveDirectoryW(lpFileName);
4559
4560 return DeleteFileW(lpFileName);
4561}
4562#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004563
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004564
Larry Hastings2f936352014-08-05 14:04:04 +10004565/*[clinic input]
4566os.unlink
4567
4568 path: path_t
4569 *
4570 dir_fd: dir_fd(requires='unlinkat')=None
4571
4572Remove a file (same as remove()).
4573
4574If dir_fd is not None, it should be a file descriptor open to a directory,
4575 and path should be relative; path will then be relative to that directory.
4576dir_fd may not be implemented on your platform.
4577 If it is unavailable, using it will raise a NotImplementedError.
4578
4579[clinic start generated code]*/
4580
Larry Hastings2f936352014-08-05 14:04:04 +10004581static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004582os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4583/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004584{
4585 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004586
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004587 if (PySys_Audit("os.remove", "Oi", path->object,
4588 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4589 return NULL;
4590 }
4591
Larry Hastings9cf065c2012-06-22 16:30:09 -07004592 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004593 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004594#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004595 /* Windows, success=1, UNIX, success=0 */
4596 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004597#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004598#ifdef HAVE_UNLINKAT
4599 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004600 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004601 else
4602#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004603 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004604#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004605 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004606 Py_END_ALLOW_THREADS
4607
Larry Hastings2f936352014-08-05 14:04:04 +10004608 if (result)
4609 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004610
Larry Hastings2f936352014-08-05 14:04:04 +10004611 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004612}
4613
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004614
Larry Hastings2f936352014-08-05 14:04:04 +10004615/*[clinic input]
4616os.remove = os.unlink
4617
4618Remove a file (same as unlink()).
4619
4620If dir_fd is not None, it should be a file descriptor open to a directory,
4621 and path should be relative; path will then be relative to that directory.
4622dir_fd may not be implemented on your platform.
4623 If it is unavailable, using it will raise a NotImplementedError.
4624[clinic start generated code]*/
4625
Larry Hastings2f936352014-08-05 14:04:04 +10004626static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004627os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4628/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004629{
4630 return os_unlink_impl(module, path, dir_fd);
4631}
4632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004633
Larry Hastings605a62d2012-06-24 04:33:36 -07004634static PyStructSequence_Field uname_result_fields[] = {
4635 {"sysname", "operating system name"},
4636 {"nodename", "name of machine on network (implementation-defined)"},
4637 {"release", "operating system release"},
4638 {"version", "operating system version"},
4639 {"machine", "hardware identifier"},
4640 {NULL}
4641};
4642
4643PyDoc_STRVAR(uname_result__doc__,
4644"uname_result: Result from os.uname().\n\n\
4645This object may be accessed either as a tuple of\n\
4646 (sysname, nodename, release, version, machine),\n\
4647or via the attributes sysname, nodename, release, version, and machine.\n\
4648\n\
4649See os.uname for more information.");
4650
4651static PyStructSequence_Desc uname_result_desc = {
Eddie Elizondob3966632019-11-05 07:16:14 -08004652 MODNAME ".uname_result", /* name */
Larry Hastings605a62d2012-06-24 04:33:36 -07004653 uname_result__doc__, /* doc */
4654 uname_result_fields,
4655 5
4656};
4657
Larry Hastings605a62d2012-06-24 04:33:36 -07004658#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004659/*[clinic input]
4660os.uname
4661
4662Return an object identifying the current operating system.
4663
4664The object behaves like a named tuple with the following fields:
4665 (sysname, nodename, release, version, machine)
4666
4667[clinic start generated code]*/
4668
Larry Hastings2f936352014-08-05 14:04:04 +10004669static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004670os_uname_impl(PyObject *module)
4671/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004672{
Victor Stinner8c62be82010-05-06 00:08:46 +00004673 struct utsname u;
4674 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004675 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004676
Victor Stinner8c62be82010-05-06 00:08:46 +00004677 Py_BEGIN_ALLOW_THREADS
4678 res = uname(&u);
4679 Py_END_ALLOW_THREADS
4680 if (res < 0)
4681 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004682
Hai Shif707d942020-03-16 21:15:01 +08004683 PyObject *UnameResultType = get_posix_state(module)->UnameResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08004684 value = PyStructSequence_New((PyTypeObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004685 if (value == NULL)
4686 return NULL;
4687
4688#define SET(i, field) \
4689 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004690 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004691 if (!o) { \
4692 Py_DECREF(value); \
4693 return NULL; \
4694 } \
4695 PyStructSequence_SET_ITEM(value, i, o); \
4696 } \
4697
4698 SET(0, u.sysname);
4699 SET(1, u.nodename);
4700 SET(2, u.release);
4701 SET(3, u.version);
4702 SET(4, u.machine);
4703
4704#undef SET
4705
4706 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004707}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004708#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004709
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004710
Larry Hastings9cf065c2012-06-22 16:30:09 -07004711
4712typedef struct {
4713 int now;
4714 time_t atime_s;
4715 long atime_ns;
4716 time_t mtime_s;
4717 long mtime_ns;
4718} utime_t;
4719
4720/*
Victor Stinner484df002014-10-09 13:52:31 +02004721 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004722 * they also intentionally leak the declaration of a pointer named "time"
4723 */
4724#define UTIME_TO_TIMESPEC \
4725 struct timespec ts[2]; \
4726 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004727 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004728 time = NULL; \
4729 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004730 ts[0].tv_sec = ut->atime_s; \
4731 ts[0].tv_nsec = ut->atime_ns; \
4732 ts[1].tv_sec = ut->mtime_s; \
4733 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004734 time = ts; \
4735 } \
4736
4737#define UTIME_TO_TIMEVAL \
4738 struct timeval tv[2]; \
4739 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004740 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004741 time = NULL; \
4742 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004743 tv[0].tv_sec = ut->atime_s; \
4744 tv[0].tv_usec = ut->atime_ns / 1000; \
4745 tv[1].tv_sec = ut->mtime_s; \
4746 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004747 time = tv; \
4748 } \
4749
4750#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004751 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004752 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004753 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004754 time = NULL; \
4755 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004756 u.actime = ut->atime_s; \
4757 u.modtime = ut->mtime_s; \
4758 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004759 }
4760
4761#define UTIME_TO_TIME_T \
4762 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004763 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004764 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004765 time = NULL; \
4766 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004767 timet[0] = ut->atime_s; \
4768 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004769 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004770 } \
4771
4772
Victor Stinner528a9ab2015-09-03 21:30:26 +02004773#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004774
4775static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004776utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004777{
4778#ifdef HAVE_UTIMENSAT
4779 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4780 UTIME_TO_TIMESPEC;
4781 return utimensat(dir_fd, path, time, flags);
4782#elif defined(HAVE_FUTIMESAT)
4783 UTIME_TO_TIMEVAL;
4784 /*
4785 * follow_symlinks will never be false here;
4786 * we only allow !follow_symlinks and dir_fd together
4787 * if we have utimensat()
4788 */
4789 assert(follow_symlinks);
4790 return futimesat(dir_fd, path, time);
4791#endif
4792}
4793
Larry Hastings2f936352014-08-05 14:04:04 +10004794 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4795#else
4796 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004797#endif
4798
Victor Stinner528a9ab2015-09-03 21:30:26 +02004799#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004800
4801static int
Victor Stinner484df002014-10-09 13:52:31 +02004802utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004803{
4804#ifdef HAVE_FUTIMENS
4805 UTIME_TO_TIMESPEC;
4806 return futimens(fd, time);
4807#else
4808 UTIME_TO_TIMEVAL;
4809 return futimes(fd, time);
4810#endif
4811}
4812
Larry Hastings2f936352014-08-05 14:04:04 +10004813 #define PATH_UTIME_HAVE_FD 1
4814#else
4815 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004816#endif
4817
Victor Stinner5ebae872015-09-22 01:29:33 +02004818#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4819# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4820#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004821
Victor Stinner4552ced2015-09-21 22:37:15 +02004822#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004823
4824static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004825utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004826{
4827#ifdef HAVE_UTIMENSAT
4828 UTIME_TO_TIMESPEC;
4829 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4830#else
4831 UTIME_TO_TIMEVAL;
4832 return lutimes(path, time);
4833#endif
4834}
4835
4836#endif
4837
4838#ifndef MS_WINDOWS
4839
4840static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004841utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004842{
4843#ifdef HAVE_UTIMENSAT
4844 UTIME_TO_TIMESPEC;
4845 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4846#elif defined(HAVE_UTIMES)
4847 UTIME_TO_TIMEVAL;
4848 return utimes(path, time);
4849#elif defined(HAVE_UTIME_H)
4850 UTIME_TO_UTIMBUF;
4851 return utime(path, time);
4852#else
4853 UTIME_TO_TIME_T;
4854 return utime(path, time);
4855#endif
4856}
4857
4858#endif
4859
Larry Hastings76ad59b2012-05-03 00:30:07 -07004860static int
4861split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4862{
4863 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004864 PyObject *divmod;
Eddie Elizondob3966632019-11-05 07:16:14 -08004865 divmod = PyNumber_Divmod(py_long, _posixstate_global->billion);
Larry Hastings76ad59b2012-05-03 00:30:07 -07004866 if (!divmod)
4867 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004868 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4869 PyErr_Format(PyExc_TypeError,
4870 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -08004871 _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod)));
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004872 goto exit;
4873 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004874 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4875 if ((*s == -1) && PyErr_Occurred())
4876 goto exit;
4877 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004878 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004879 goto exit;
4880
4881 result = 1;
4882exit:
4883 Py_XDECREF(divmod);
4884 return result;
4885}
4886
Larry Hastings2f936352014-08-05 14:04:04 +10004887
4888/*[clinic input]
4889os.utime
4890
4891 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004892 times: object = None
Larry Hastings2f936352014-08-05 14:04:04 +10004893 *
4894 ns: object = NULL
4895 dir_fd: dir_fd(requires='futimensat') = None
4896 follow_symlinks: bool=True
4897
Martin Panter0ff89092015-09-09 01:56:53 +00004898# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004899
4900Set the access and modified time of path.
4901
4902path may always be specified as a string.
4903On some platforms, path may also be specified as an open file descriptor.
4904 If this functionality is unavailable, using it raises an exception.
4905
4906If times is not None, it must be a tuple (atime, mtime);
4907 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004908If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004909 atime_ns and mtime_ns should be expressed as integer nanoseconds
4910 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004911If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004912Specifying tuples for both times and ns is an error.
4913
4914If dir_fd is not None, it should be a file descriptor open to a directory,
4915 and path should be relative; path will then be relative to that directory.
4916If follow_symlinks is False, and the last element of the path is a symbolic
4917 link, utime will modify the symbolic link itself instead of the file the
4918 link points to.
4919It is an error to use dir_fd or follow_symlinks when specifying path
4920 as an open file descriptor.
4921dir_fd and follow_symlinks may not be available on your platform.
4922 If they are unavailable, using them will raise a NotImplementedError.
4923
4924[clinic start generated code]*/
4925
Larry Hastings2f936352014-08-05 14:04:04 +10004926static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004927os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4928 int dir_fd, int follow_symlinks)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004929/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004930{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004931#ifdef MS_WINDOWS
4932 HANDLE hFile;
4933 FILETIME atime, mtime;
4934#else
4935 int result;
4936#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004937
Larry Hastings2f936352014-08-05 14:04:04 +10004938 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004939
Christian Heimesb3c87242013-08-01 00:08:16 +02004940 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004941
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004942 if (times != Py_None && ns) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004943 PyErr_SetString(PyExc_ValueError,
4944 "utime: you may specify either 'times'"
4945 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004946 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004947 }
4948
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004949 if (times != Py_None) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004950 time_t a_sec, m_sec;
4951 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004952 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004953 PyErr_SetString(PyExc_TypeError,
4954 "utime: 'times' must be either"
4955 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004956 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004957 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004958 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004959 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004960 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004961 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004962 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004963 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004964 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004965 utime.atime_s = a_sec;
4966 utime.atime_ns = a_nsec;
4967 utime.mtime_s = m_sec;
4968 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004969 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004970 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004971 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004972 PyErr_SetString(PyExc_TypeError,
4973 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004974 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004975 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004976 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004977 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004978 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004979 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004980 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004981 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004982 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004983 }
4984 else {
4985 /* times and ns are both None/unspecified. use "now". */
4986 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004987 }
4988
Victor Stinner4552ced2015-09-21 22:37:15 +02004989#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004990 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004991 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004992#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004993
Larry Hastings2f936352014-08-05 14:04:04 +10004994 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4995 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4996 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004997 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004998
Larry Hastings9cf065c2012-06-22 16:30:09 -07004999#if !defined(HAVE_UTIMENSAT)
5000 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02005001 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005002 "utime: cannot use dir_fd and follow_symlinks "
5003 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005004 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07005005 }
5006#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07005007
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005008 if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None,
5009 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
5010 return NULL;
5011 }
5012
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005013#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005014 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07005015 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
5016 NULL, OPEN_EXISTING,
5017 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005018 Py_END_ALLOW_THREADS
5019 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10005020 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005021 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07005022 }
5023
Larry Hastings9cf065c2012-06-22 16:30:09 -07005024 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01005025 GetSystemTimeAsFileTime(&mtime);
5026 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00005027 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005028 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08005029 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
5030 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00005031 }
5032 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
5033 /* Avoid putting the file name into the error here,
5034 as that may confuse the user into believing that
5035 something is wrong with the file, when it also
5036 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01005037 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005038 CloseHandle(hFile);
5039 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005040 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005041 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005042#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005043 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00005044
Victor Stinner4552ced2015-09-21 22:37:15 +02005045#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005046 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10005047 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005048 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07005049#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07005050
Victor Stinner528a9ab2015-09-03 21:30:26 +02005051#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005052 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10005053 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005054 else
5055#endif
5056
Victor Stinner528a9ab2015-09-03 21:30:26 +02005057#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10005058 if (path->fd != -1)
5059 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005060 else
5061#endif
5062
Larry Hastings2f936352014-08-05 14:04:04 +10005063 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005064
5065 Py_END_ALLOW_THREADS
5066
5067 if (result < 0) {
5068 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005069 posix_error();
5070 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005071 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07005072
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005073#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005074
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005075 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005076}
5077
Guido van Rossum3b066191991-06-04 19:40:25 +00005078/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005079
Larry Hastings2f936352014-08-05 14:04:04 +10005080
5081/*[clinic input]
5082os._exit
5083
5084 status: int
5085
5086Exit to the system with specified status, without normal exit processing.
5087[clinic start generated code]*/
5088
Larry Hastings2f936352014-08-05 14:04:04 +10005089static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005090os__exit_impl(PyObject *module, int status)
5091/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005092{
5093 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00005094 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005095}
5096
Steve Dowercc16be82016-09-08 10:35:16 -07005097#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5098#define EXECV_CHAR wchar_t
5099#else
5100#define EXECV_CHAR char
5101#endif
5102
pxinwrf2d7ac72019-05-21 18:46:37 +08005103#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005104static void
Steve Dowercc16be82016-09-08 10:35:16 -07005105free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005106{
Victor Stinner8c62be82010-05-06 00:08:46 +00005107 Py_ssize_t i;
5108 for (i = 0; i < count; i++)
5109 PyMem_Free(array[i]);
5110 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005111}
Martin v. Löwis011e8422009-05-05 04:43:17 +00005112
Berker Peksag81816462016-09-15 20:19:47 +03005113static int
5114fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00005115{
Victor Stinner8c62be82010-05-06 00:08:46 +00005116 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03005117 PyObject *ub;
5118 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07005119#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03005120 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07005121 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005122 *out = PyUnicode_AsWideCharString(ub, &size);
5123 if (*out)
5124 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07005125#else
Berker Peksag81816462016-09-15 20:19:47 +03005126 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00005127 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005128 size = PyBytes_GET_SIZE(ub);
5129 *out = PyMem_Malloc(size + 1);
5130 if (*out) {
5131 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
5132 result = 1;
5133 } else
Victor Stinner50abf222013-11-07 23:56:10 +01005134 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07005135#endif
Berker Peksag81816462016-09-15 20:19:47 +03005136 Py_DECREF(ub);
5137 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005138}
Martin v. Löwis114619e2002-10-07 06:44:21 +00005139#endif
5140
pxinwrf2d7ac72019-05-21 18:46:37 +08005141#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07005142static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00005143parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
5144{
Victor Stinner8c62be82010-05-06 00:08:46 +00005145 Py_ssize_t i, pos, envc;
5146 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03005147 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07005148 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005149
Victor Stinner8c62be82010-05-06 00:08:46 +00005150 i = PyMapping_Size(env);
5151 if (i < 0)
5152 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07005153 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005154 if (envlist == NULL) {
5155 PyErr_NoMemory();
5156 return NULL;
5157 }
5158 envc = 0;
5159 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005160 if (!keys)
5161 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005162 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005163 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005164 goto error;
5165 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5166 PyErr_Format(PyExc_TypeError,
5167 "env.keys() or env.values() is not a list");
5168 goto error;
5169 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005170
Victor Stinner8c62be82010-05-06 00:08:46 +00005171 for (pos = 0; pos < i; pos++) {
5172 key = PyList_GetItem(keys, pos);
5173 val = PyList_GetItem(vals, pos);
5174 if (!key || !val)
5175 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005176
Berker Peksag81816462016-09-15 20:19:47 +03005177#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5178 if (!PyUnicode_FSDecoder(key, &key2))
5179 goto error;
5180 if (!PyUnicode_FSDecoder(val, &val2)) {
5181 Py_DECREF(key2);
5182 goto error;
5183 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005184 /* Search from index 1 because on Windows starting '=' is allowed for
5185 defining hidden environment variables. */
5186 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5187 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5188 {
5189 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005190 Py_DECREF(key2);
5191 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005192 goto error;
5193 }
Berker Peksag81816462016-09-15 20:19:47 +03005194 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5195#else
5196 if (!PyUnicode_FSConverter(key, &key2))
5197 goto error;
5198 if (!PyUnicode_FSConverter(val, &val2)) {
5199 Py_DECREF(key2);
5200 goto error;
5201 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005202 if (PyBytes_GET_SIZE(key2) == 0 ||
5203 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5204 {
5205 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005206 Py_DECREF(key2);
5207 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005208 goto error;
5209 }
Berker Peksag81816462016-09-15 20:19:47 +03005210 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5211 PyBytes_AS_STRING(val2));
5212#endif
5213 Py_DECREF(key2);
5214 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005215 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005216 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005217
5218 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5219 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005220 goto error;
5221 }
Berker Peksag81816462016-09-15 20:19:47 +03005222
Steve Dowercc16be82016-09-08 10:35:16 -07005223 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005224 }
5225 Py_DECREF(vals);
5226 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005227
Victor Stinner8c62be82010-05-06 00:08:46 +00005228 envlist[envc] = 0;
5229 *envc_ptr = envc;
5230 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005231
5232error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005233 Py_XDECREF(keys);
5234 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005235 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005236 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005237}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005238
Steve Dowercc16be82016-09-08 10:35:16 -07005239static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005240parse_arglist(PyObject* argv, Py_ssize_t *argc)
5241{
5242 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005243 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005244 if (argvlist == NULL) {
5245 PyErr_NoMemory();
5246 return NULL;
5247 }
5248 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005249 PyObject* item = PySequence_ITEM(argv, i);
5250 if (item == NULL)
5251 goto fail;
5252 if (!fsconvert_strdup(item, &argvlist[i])) {
5253 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005254 goto fail;
5255 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005256 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005257 }
5258 argvlist[*argc] = NULL;
5259 return argvlist;
5260fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005261 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005262 free_string_array(argvlist, *argc);
5263 return NULL;
5264}
Steve Dowercc16be82016-09-08 10:35:16 -07005265
Ross Lagerwall7807c352011-03-17 20:20:30 +02005266#endif
5267
Larry Hastings2f936352014-08-05 14:04:04 +10005268
Ross Lagerwall7807c352011-03-17 20:20:30 +02005269#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005270/*[clinic input]
5271os.execv
5272
Steve Dowercc16be82016-09-08 10:35:16 -07005273 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005274 Path of executable file.
5275 argv: object
5276 Tuple or list of strings.
5277 /
5278
5279Execute an executable path with arguments, replacing current process.
5280[clinic start generated code]*/
5281
Larry Hastings2f936352014-08-05 14:04:04 +10005282static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005283os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5284/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005285{
Steve Dowercc16be82016-09-08 10:35:16 -07005286 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005287 Py_ssize_t argc;
5288
5289 /* execv has two arguments: (path, argv), where
5290 argv is a list or tuple of strings. */
5291
Ross Lagerwall7807c352011-03-17 20:20:30 +02005292 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5293 PyErr_SetString(PyExc_TypeError,
5294 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005295 return NULL;
5296 }
5297 argc = PySequence_Size(argv);
5298 if (argc < 1) {
5299 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005300 return NULL;
5301 }
5302
5303 argvlist = parse_arglist(argv, &argc);
5304 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005305 return NULL;
5306 }
Steve Dowerbce26262016-11-19 19:17:26 -08005307 if (!argvlist[0][0]) {
5308 PyErr_SetString(PyExc_ValueError,
5309 "execv() arg 2 first element cannot be empty");
5310 free_string_array(argvlist, argc);
5311 return NULL;
5312 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005313
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005314 if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005315 free_string_array(argvlist, argc);
5316 return NULL;
5317 }
5318
Steve Dowerbce26262016-11-19 19:17:26 -08005319 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005320#ifdef HAVE_WEXECV
5321 _wexecv(path->wide, argvlist);
5322#else
5323 execv(path->narrow, argvlist);
5324#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005325 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005326
5327 /* If we get here it's definitely an error */
5328
5329 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005330 return posix_error();
5331}
5332
Larry Hastings2f936352014-08-05 14:04:04 +10005333
5334/*[clinic input]
5335os.execve
5336
5337 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5338 Path of executable file.
5339 argv: object
5340 Tuple or list of strings.
5341 env: object
5342 Dictionary of strings mapping to strings.
5343
5344Execute an executable path with arguments, replacing current process.
5345[clinic start generated code]*/
5346
Larry Hastings2f936352014-08-05 14:04:04 +10005347static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005348os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5349/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005350{
Steve Dowercc16be82016-09-08 10:35:16 -07005351 EXECV_CHAR **argvlist = NULL;
5352 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005353 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005354
Victor Stinner8c62be82010-05-06 00:08:46 +00005355 /* execve has three arguments: (path, argv, env), where
5356 argv is a list or tuple of strings and env is a dictionary
5357 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005358
Ross Lagerwall7807c352011-03-17 20:20:30 +02005359 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005360 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005361 "execve: argv must be a tuple or list");
Saiyang Gou95f60012020-02-04 16:15:00 -08005362 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005363 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005364 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005365 if (argc < 1) {
5366 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5367 return NULL;
5368 }
5369
Victor Stinner8c62be82010-05-06 00:08:46 +00005370 if (!PyMapping_Check(env)) {
5371 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005372 "execve: environment must be a mapping object");
Saiyang Gou95f60012020-02-04 16:15:00 -08005373 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005374 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005375
Ross Lagerwall7807c352011-03-17 20:20:30 +02005376 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005377 if (argvlist == NULL) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005378 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005379 }
Steve Dowerbce26262016-11-19 19:17:26 -08005380 if (!argvlist[0][0]) {
5381 PyErr_SetString(PyExc_ValueError,
5382 "execve: argv first element cannot be empty");
Saiyang Gou95f60012020-02-04 16:15:00 -08005383 goto fail_0;
Steve Dowerbce26262016-11-19 19:17:26 -08005384 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005385
Victor Stinner8c62be82010-05-06 00:08:46 +00005386 envlist = parse_envlist(env, &envc);
5387 if (envlist == NULL)
Saiyang Gou95f60012020-02-04 16:15:00 -08005388 goto fail_0;
5389
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005390 if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005391 goto fail_1;
5392 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005393
Steve Dowerbce26262016-11-19 19:17:26 -08005394 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005395#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005396 if (path->fd > -1)
5397 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005398 else
5399#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005400#ifdef HAVE_WEXECV
5401 _wexecve(path->wide, argvlist, envlist);
5402#else
Larry Hastings2f936352014-08-05 14:04:04 +10005403 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005404#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005405 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005406
5407 /* If we get here it's definitely an error */
5408
Alexey Izbyshev83460312018-10-20 03:28:22 +03005409 posix_path_error(path);
Saiyang Gou95f60012020-02-04 16:15:00 -08005410 fail_1:
Steve Dowercc16be82016-09-08 10:35:16 -07005411 free_string_array(envlist, envc);
Saiyang Gou95f60012020-02-04 16:15:00 -08005412 fail_0:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005413 if (argvlist)
5414 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005415 return NULL;
5416}
Steve Dowercc16be82016-09-08 10:35:16 -07005417
Larry Hastings9cf065c2012-06-22 16:30:09 -07005418#endif /* HAVE_EXECV */
5419
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005420#ifdef HAVE_POSIX_SPAWN
5421
5422enum posix_spawn_file_actions_identifier {
5423 POSIX_SPAWN_OPEN,
5424 POSIX_SPAWN_CLOSE,
5425 POSIX_SPAWN_DUP2
5426};
5427
William Orr81574b82018-10-01 22:19:56 -07005428#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005429static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005430convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005431#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005432
5433static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005434parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5435 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005436 PyObject *setsigdef, PyObject *scheduler,
5437 posix_spawnattr_t *attrp)
5438{
5439 long all_flags = 0;
5440
5441 errno = posix_spawnattr_init(attrp);
5442 if (errno) {
5443 posix_error();
5444 return -1;
5445 }
5446
5447 if (setpgroup) {
5448 pid_t pgid = PyLong_AsPid(setpgroup);
5449 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5450 goto fail;
5451 }
5452 errno = posix_spawnattr_setpgroup(attrp, pgid);
5453 if (errno) {
5454 posix_error();
5455 goto fail;
5456 }
5457 all_flags |= POSIX_SPAWN_SETPGROUP;
5458 }
5459
5460 if (resetids) {
5461 all_flags |= POSIX_SPAWN_RESETIDS;
5462 }
5463
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005464 if (setsid) {
5465#ifdef POSIX_SPAWN_SETSID
5466 all_flags |= POSIX_SPAWN_SETSID;
5467#elif defined(POSIX_SPAWN_SETSID_NP)
5468 all_flags |= POSIX_SPAWN_SETSID_NP;
5469#else
5470 argument_unavailable_error(func_name, "setsid");
5471 return -1;
5472#endif
5473 }
5474
Pablo Galindo254a4662018-09-07 16:44:24 +01005475 if (setsigmask) {
5476 sigset_t set;
5477 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5478 goto fail;
5479 }
5480 errno = posix_spawnattr_setsigmask(attrp, &set);
5481 if (errno) {
5482 posix_error();
5483 goto fail;
5484 }
5485 all_flags |= POSIX_SPAWN_SETSIGMASK;
5486 }
5487
5488 if (setsigdef) {
5489 sigset_t set;
5490 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5491 goto fail;
5492 }
5493 errno = posix_spawnattr_setsigdefault(attrp, &set);
5494 if (errno) {
5495 posix_error();
5496 goto fail;
5497 }
5498 all_flags |= POSIX_SPAWN_SETSIGDEF;
5499 }
5500
5501 if (scheduler) {
5502#ifdef POSIX_SPAWN_SETSCHEDULER
5503 PyObject *py_schedpolicy;
5504 struct sched_param schedparam;
5505
5506 if (!PyArg_ParseTuple(scheduler, "OO&"
5507 ";A scheduler tuple must have two elements",
5508 &py_schedpolicy, convert_sched_param, &schedparam)) {
5509 goto fail;
5510 }
5511 if (py_schedpolicy != Py_None) {
5512 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5513
5514 if (schedpolicy == -1 && PyErr_Occurred()) {
5515 goto fail;
5516 }
5517 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5518 if (errno) {
5519 posix_error();
5520 goto fail;
5521 }
5522 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5523 }
5524 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5525 if (errno) {
5526 posix_error();
5527 goto fail;
5528 }
5529 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5530#else
5531 PyErr_SetString(PyExc_NotImplementedError,
5532 "The scheduler option is not supported in this system.");
5533 goto fail;
5534#endif
5535 }
5536
5537 errno = posix_spawnattr_setflags(attrp, all_flags);
5538 if (errno) {
5539 posix_error();
5540 goto fail;
5541 }
5542
5543 return 0;
5544
5545fail:
5546 (void)posix_spawnattr_destroy(attrp);
5547 return -1;
5548}
5549
5550static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005551parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005552 posix_spawn_file_actions_t *file_actionsp,
5553 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005554{
5555 PyObject *seq;
5556 PyObject *file_action = NULL;
5557 PyObject *tag_obj;
5558
5559 seq = PySequence_Fast(file_actions,
5560 "file_actions must be a sequence or None");
5561 if (seq == NULL) {
5562 return -1;
5563 }
5564
5565 errno = posix_spawn_file_actions_init(file_actionsp);
5566 if (errno) {
5567 posix_error();
5568 Py_DECREF(seq);
5569 return -1;
5570 }
5571
Zackery Spytzd52a83a2019-06-26 14:54:20 -06005572 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03005573 file_action = PySequence_Fast_GET_ITEM(seq, i);
5574 Py_INCREF(file_action);
5575 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5576 PyErr_SetString(PyExc_TypeError,
5577 "Each file_actions element must be a non-empty tuple");
5578 goto fail;
5579 }
5580 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5581 if (tag == -1 && PyErr_Occurred()) {
5582 goto fail;
5583 }
5584
5585 /* Populate the file_actions object */
5586 switch (tag) {
5587 case POSIX_SPAWN_OPEN: {
5588 int fd, oflag;
5589 PyObject *path;
5590 unsigned long mode;
5591 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5592 ";A open file_action tuple must have 5 elements",
5593 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5594 &oflag, &mode))
5595 {
5596 goto fail;
5597 }
Pablo Galindocb970732018-06-19 09:19:50 +01005598 if (PyList_Append(temp_buffer, path)) {
5599 Py_DECREF(path);
5600 goto fail;
5601 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005602 errno = posix_spawn_file_actions_addopen(file_actionsp,
5603 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005604 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005605 if (errno) {
5606 posix_error();
5607 goto fail;
5608 }
5609 break;
5610 }
5611 case POSIX_SPAWN_CLOSE: {
5612 int fd;
5613 if (!PyArg_ParseTuple(file_action, "Oi"
5614 ";A close file_action tuple must have 2 elements",
5615 &tag_obj, &fd))
5616 {
5617 goto fail;
5618 }
5619 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5620 if (errno) {
5621 posix_error();
5622 goto fail;
5623 }
5624 break;
5625 }
5626 case POSIX_SPAWN_DUP2: {
5627 int fd1, fd2;
5628 if (!PyArg_ParseTuple(file_action, "Oii"
5629 ";A dup2 file_action tuple must have 3 elements",
5630 &tag_obj, &fd1, &fd2))
5631 {
5632 goto fail;
5633 }
5634 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5635 fd1, fd2);
5636 if (errno) {
5637 posix_error();
5638 goto fail;
5639 }
5640 break;
5641 }
5642 default: {
5643 PyErr_SetString(PyExc_TypeError,
5644 "Unknown file_actions identifier");
5645 goto fail;
5646 }
5647 }
5648 Py_DECREF(file_action);
5649 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005650
Serhiy Storchakaef347532018-05-01 16:45:04 +03005651 Py_DECREF(seq);
5652 return 0;
5653
5654fail:
5655 Py_DECREF(seq);
5656 Py_DECREF(file_action);
5657 (void)posix_spawn_file_actions_destroy(file_actionsp);
5658 return -1;
5659}
5660
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005661
5662static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005663py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5664 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005665 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005666 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005667{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005668 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005669 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005670 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005671 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005672 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005673 posix_spawnattr_t attr;
5674 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005675 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005676 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005677 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005678 pid_t pid;
5679 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005680
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005681 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005682 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005683 like posix.environ. */
5684
Serhiy Storchakaef347532018-05-01 16:45:04 +03005685 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005686 PyErr_Format(PyExc_TypeError,
5687 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005688 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005689 }
5690 argc = PySequence_Size(argv);
5691 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005692 PyErr_Format(PyExc_ValueError,
5693 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005694 return NULL;
5695 }
5696
5697 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005698 PyErr_Format(PyExc_TypeError,
5699 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005700 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005701 }
5702
5703 argvlist = parse_arglist(argv, &argc);
5704 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005705 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005706 }
5707 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005708 PyErr_Format(PyExc_ValueError,
5709 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005710 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005711 }
5712
5713 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005714 if (envlist == NULL) {
5715 goto exit;
5716 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005717
Anthony Shaw948ed8c2019-05-10 12:00:06 +10005718 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01005719 /* There is a bug in old versions of glibc that makes some of the
5720 * helper functions for manipulating file actions not copy the provided
5721 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5722 * copy the value of path for some old versions of glibc (<2.20).
5723 * The use of temp_buffer here is a workaround that keeps the
5724 * python objects that own the buffers alive until posix_spawn gets called.
5725 * Check https://bugs.python.org/issue33630 and
5726 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5727 temp_buffer = PyList_New(0);
5728 if (!temp_buffer) {
5729 goto exit;
5730 }
5731 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005732 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005733 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005734 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005735 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005736
Victor Stinner325e4ba2019-02-01 15:47:24 +01005737 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5738 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005739 goto exit;
5740 }
5741 attrp = &attr;
5742
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005743 if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005744 goto exit;
5745 }
5746
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005747 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005748#ifdef HAVE_POSIX_SPAWNP
5749 if (use_posix_spawnp) {
5750 err_code = posix_spawnp(&pid, path->narrow,
5751 file_actionsp, attrp, argvlist, envlist);
5752 }
5753 else
5754#endif /* HAVE_POSIX_SPAWNP */
5755 {
5756 err_code = posix_spawn(&pid, path->narrow,
5757 file_actionsp, attrp, argvlist, envlist);
5758 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005759 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005760
Serhiy Storchakaef347532018-05-01 16:45:04 +03005761 if (err_code) {
5762 errno = err_code;
5763 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005764 goto exit;
5765 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005766#ifdef _Py_MEMORY_SANITIZER
5767 __msan_unpoison(&pid, sizeof(pid));
5768#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005769 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005770
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005771exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005772 if (file_actionsp) {
5773 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005774 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005775 if (attrp) {
5776 (void)posix_spawnattr_destroy(attrp);
5777 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005778 if (envlist) {
5779 free_string_array(envlist, envc);
5780 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005781 if (argvlist) {
5782 free_string_array(argvlist, argc);
5783 }
Pablo Galindocb970732018-06-19 09:19:50 +01005784 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005785 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005786}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005787
5788
5789/*[clinic input]
5790
5791os.posix_spawn
5792 path: path_t
5793 Path of executable file.
5794 argv: object
5795 Tuple or list of strings.
5796 env: object
5797 Dictionary of strings mapping to strings.
5798 /
5799 *
5800 file_actions: object(c_default='NULL') = ()
5801 A sequence of file action tuples.
5802 setpgroup: object = NULL
5803 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5804 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005805 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5806 setsid: bool(accept={int}) = False
5807 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005808 setsigmask: object(c_default='NULL') = ()
5809 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5810 setsigdef: object(c_default='NULL') = ()
5811 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5812 scheduler: object = NULL
5813 A tuple with the scheduler policy (optional) and parameters.
5814
5815Execute the program specified by path in a new process.
5816[clinic start generated code]*/
5817
5818static PyObject *
5819os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5820 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005821 PyObject *setpgroup, int resetids, int setsid,
5822 PyObject *setsigmask, PyObject *setsigdef,
5823 PyObject *scheduler)
5824/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005825{
5826 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005827 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005828 scheduler);
5829}
5830 #endif /* HAVE_POSIX_SPAWN */
5831
5832
5833
5834#ifdef HAVE_POSIX_SPAWNP
5835/*[clinic input]
5836
5837os.posix_spawnp
5838 path: path_t
5839 Path of executable file.
5840 argv: object
5841 Tuple or list of strings.
5842 env: object
5843 Dictionary of strings mapping to strings.
5844 /
5845 *
5846 file_actions: object(c_default='NULL') = ()
5847 A sequence of file action tuples.
5848 setpgroup: object = NULL
5849 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5850 resetids: bool(accept={int}) = False
5851 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005852 setsid: bool(accept={int}) = False
5853 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005854 setsigmask: object(c_default='NULL') = ()
5855 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5856 setsigdef: object(c_default='NULL') = ()
5857 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5858 scheduler: object = NULL
5859 A tuple with the scheduler policy (optional) and parameters.
5860
5861Execute the program specified by path in a new process.
5862[clinic start generated code]*/
5863
5864static PyObject *
5865os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5866 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005867 PyObject *setpgroup, int resetids, int setsid,
5868 PyObject *setsigmask, PyObject *setsigdef,
5869 PyObject *scheduler)
5870/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005871{
5872 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005873 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005874 scheduler);
5875}
5876#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005877
pxinwrf2d7ac72019-05-21 18:46:37 +08005878#ifdef HAVE_RTPSPAWN
5879static intptr_t
5880_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5881 const char *envp[])
5882{
5883 RTP_ID rtpid;
5884 int status;
5885 pid_t res;
5886 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005887
pxinwrf2d7ac72019-05-21 18:46:37 +08005888 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5889 uStackSize=0 cannot be used, the default stack size is too small for
5890 Python. */
5891 if (envp) {
5892 rtpid = rtpSpawn(rtpFileName, argv, envp,
5893 100, 0x1000000, 0, VX_FP_TASK);
5894 }
5895 else {
5896 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5897 100, 0x1000000, 0, VX_FP_TASK);
5898 }
5899 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5900 do {
5901 res = waitpid((pid_t)rtpid, &status, 0);
5902 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5903
5904 if (res < 0)
5905 return RTP_ID_ERROR;
5906 return ((intptr_t)status);
5907 }
5908 return ((intptr_t)rtpid);
5909}
5910#endif
5911
5912#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10005913/*[clinic input]
5914os.spawnv
5915
5916 mode: int
5917 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005918 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005919 Path of executable file.
5920 argv: object
5921 Tuple or list of strings.
5922 /
5923
5924Execute the program specified by path in a new process.
5925[clinic start generated code]*/
5926
Larry Hastings2f936352014-08-05 14:04:04 +10005927static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005928os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5929/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005930{
Steve Dowercc16be82016-09-08 10:35:16 -07005931 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005932 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005933 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005934 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005935 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005936
Victor Stinner8c62be82010-05-06 00:08:46 +00005937 /* spawnv has three arguments: (mode, path, argv), where
5938 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005939
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 if (PyList_Check(argv)) {
5941 argc = PyList_Size(argv);
5942 getitem = PyList_GetItem;
5943 }
5944 else if (PyTuple_Check(argv)) {
5945 argc = PyTuple_Size(argv);
5946 getitem = PyTuple_GetItem;
5947 }
5948 else {
5949 PyErr_SetString(PyExc_TypeError,
5950 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005951 return NULL;
5952 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005953 if (argc == 0) {
5954 PyErr_SetString(PyExc_ValueError,
5955 "spawnv() arg 2 cannot be empty");
5956 return NULL;
5957 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005958
Steve Dowercc16be82016-09-08 10:35:16 -07005959 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005960 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005961 return PyErr_NoMemory();
5962 }
5963 for (i = 0; i < argc; i++) {
5964 if (!fsconvert_strdup((*getitem)(argv, i),
5965 &argvlist[i])) {
5966 free_string_array(argvlist, i);
5967 PyErr_SetString(
5968 PyExc_TypeError,
5969 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005970 return NULL;
5971 }
Steve Dower93ff8722016-11-19 19:03:54 -08005972 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005973 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005974 PyErr_SetString(
5975 PyExc_ValueError,
5976 "spawnv() arg 2 first element cannot be empty");
5977 return NULL;
5978 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005979 }
5980 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005981
pxinwrf2d7ac72019-05-21 18:46:37 +08005982#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005983 if (mode == _OLD_P_OVERLAY)
5984 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005985#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005986
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005987 if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv,
Saiyang Gou95f60012020-02-04 16:15:00 -08005988 Py_None) < 0) {
5989 free_string_array(argvlist, argc);
5990 return NULL;
5991 }
5992
Victor Stinner8c62be82010-05-06 00:08:46 +00005993 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005994 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005995#ifdef HAVE_WSPAWNV
5996 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005997#elif defined(HAVE_RTPSPAWN)
5998 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07005999#else
6000 spawnval = _spawnv(mode, path->narrow, argvlist);
6001#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006002 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006003 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00006004
Victor Stinner8c62be82010-05-06 00:08:46 +00006005 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00006006
Victor Stinner8c62be82010-05-06 00:08:46 +00006007 if (spawnval == -1)
6008 return posix_error();
6009 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006010 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006011}
6012
Larry Hastings2f936352014-08-05 14:04:04 +10006013/*[clinic input]
6014os.spawnve
6015
6016 mode: int
6017 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07006018 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10006019 Path of executable file.
6020 argv: object
6021 Tuple or list of strings.
6022 env: object
6023 Dictionary of strings mapping to strings.
6024 /
6025
6026Execute the program specified by path in a new process.
6027[clinic start generated code]*/
6028
Larry Hastings2f936352014-08-05 14:04:04 +10006029static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07006030os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006031 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07006032/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006033{
Steve Dowercc16be82016-09-08 10:35:16 -07006034 EXECV_CHAR **argvlist;
6035 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006036 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00006037 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07006038 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00006039 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006040 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00006041
Victor Stinner8c62be82010-05-06 00:08:46 +00006042 /* spawnve has four arguments: (mode, path, argv, env), where
6043 argv is a list or tuple of strings and env is a dictionary
6044 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00006045
Victor Stinner8c62be82010-05-06 00:08:46 +00006046 if (PyList_Check(argv)) {
6047 argc = PyList_Size(argv);
6048 getitem = PyList_GetItem;
6049 }
6050 else if (PyTuple_Check(argv)) {
6051 argc = PyTuple_Size(argv);
6052 getitem = PyTuple_GetItem;
6053 }
6054 else {
6055 PyErr_SetString(PyExc_TypeError,
6056 "spawnve() arg 2 must be a tuple or list");
6057 goto fail_0;
6058 }
Steve Dower859fd7b2016-11-19 18:53:19 -08006059 if (argc == 0) {
6060 PyErr_SetString(PyExc_ValueError,
6061 "spawnve() arg 2 cannot be empty");
6062 goto fail_0;
6063 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006064 if (!PyMapping_Check(env)) {
6065 PyErr_SetString(PyExc_TypeError,
6066 "spawnve() arg 3 must be a mapping object");
6067 goto fail_0;
6068 }
Guido van Rossuma1065681999-01-25 23:20:23 +00006069
Steve Dowercc16be82016-09-08 10:35:16 -07006070 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00006071 if (argvlist == NULL) {
6072 PyErr_NoMemory();
6073 goto fail_0;
6074 }
6075 for (i = 0; i < argc; i++) {
6076 if (!fsconvert_strdup((*getitem)(argv, i),
6077 &argvlist[i]))
6078 {
6079 lastarg = i;
6080 goto fail_1;
6081 }
Steve Dowerbce26262016-11-19 19:17:26 -08006082 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006083 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08006084 PyErr_SetString(
6085 PyExc_ValueError,
6086 "spawnv() arg 2 first element cannot be empty");
6087 goto fail_1;
6088 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006089 }
6090 lastarg = argc;
6091 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00006092
Victor Stinner8c62be82010-05-06 00:08:46 +00006093 envlist = parse_envlist(env, &envc);
6094 if (envlist == NULL)
6095 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00006096
pxinwrf2d7ac72019-05-21 18:46:37 +08006097#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00006098 if (mode == _OLD_P_OVERLAY)
6099 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08006100#endif
Tim Peters25059d32001-12-07 20:35:43 +00006101
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006102 if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08006103 goto fail_2;
6104 }
6105
Victor Stinner8c62be82010-05-06 00:08:46 +00006106 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07006107 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07006108#ifdef HAVE_WSPAWNV
6109 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08006110#elif defined(HAVE_RTPSPAWN)
6111 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
6112 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07006113#else
6114 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
6115#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006116 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006117 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00006118
Victor Stinner8c62be82010-05-06 00:08:46 +00006119 if (spawnval == -1)
6120 (void) posix_error();
6121 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006122 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006123
Saiyang Gou95f60012020-02-04 16:15:00 -08006124 fail_2:
Victor Stinner8c62be82010-05-06 00:08:46 +00006125 while (--envc >= 0)
6126 PyMem_DEL(envlist[envc]);
6127 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00006128 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006129 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00006130 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00006131 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00006132}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006133
Guido van Rossuma1065681999-01-25 23:20:23 +00006134#endif /* HAVE_SPAWNV */
6135
6136
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006137#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07006138
6139/* Helper function to validate arguments.
6140 Returns 0 on success. non-zero on failure with a TypeError raised.
6141 If obj is non-NULL it must be callable. */
6142static int
6143check_null_or_callable(PyObject *obj, const char* obj_name)
6144{
6145 if (obj && !PyCallable_Check(obj)) {
6146 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
Eddie Elizondob3966632019-11-05 07:16:14 -08006147 obj_name, _PyType_Name(Py_TYPE(obj)));
Gregory P. Smith163468a2017-05-29 10:03:41 -07006148 return -1;
6149 }
6150 return 0;
6151}
6152
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006153/*[clinic input]
6154os.register_at_fork
6155
Gregory P. Smith163468a2017-05-29 10:03:41 -07006156 *
6157 before: object=NULL
6158 A callable to be called in the parent before the fork() syscall.
6159 after_in_child: object=NULL
6160 A callable to be called in the child after fork().
6161 after_in_parent: object=NULL
6162 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006163
Gregory P. Smith163468a2017-05-29 10:03:41 -07006164Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006165
Gregory P. Smith163468a2017-05-29 10:03:41 -07006166'before' callbacks are called in reverse order.
6167'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006168
6169[clinic start generated code]*/
6170
6171static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07006172os_register_at_fork_impl(PyObject *module, PyObject *before,
6173 PyObject *after_in_child, PyObject *after_in_parent)
6174/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006175{
6176 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006177
Gregory P. Smith163468a2017-05-29 10:03:41 -07006178 if (!before && !after_in_child && !after_in_parent) {
6179 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
6180 return NULL;
6181 }
6182 if (check_null_or_callable(before, "before") ||
6183 check_null_or_callable(after_in_child, "after_in_child") ||
6184 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006185 return NULL;
6186 }
Victor Stinner81a7be32020-04-14 15:14:01 +02006187 interp = _PyInterpreterState_GET();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006188
Gregory P. Smith163468a2017-05-29 10:03:41 -07006189 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006190 return NULL;
6191 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07006192 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006193 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07006194 }
6195 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6196 return NULL;
6197 }
6198 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006199}
6200#endif /* HAVE_FORK */
6201
6202
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006203#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006204/*[clinic input]
6205os.fork1
6206
6207Fork a child process with a single multiplexed (i.e., not bound) thread.
6208
6209Return 0 to child process and PID of child to parent process.
6210[clinic start generated code]*/
6211
Larry Hastings2f936352014-08-05 14:04:04 +10006212static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006213os_fork1_impl(PyObject *module)
6214/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006215{
Victor Stinner8c62be82010-05-06 00:08:46 +00006216 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006217
Victor Stinner81a7be32020-04-14 15:14:01 +02006218 if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006219 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6220 return NULL;
6221 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006222 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006223 pid = fork1();
6224 if (pid == 0) {
6225 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006226 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 } else {
6228 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006229 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006230 }
6231 if (pid == -1)
6232 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006233 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006234}
Larry Hastings2f936352014-08-05 14:04:04 +10006235#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006236
6237
Guido van Rossumad0ee831995-03-01 10:34:45 +00006238#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006239/*[clinic input]
6240os.fork
6241
6242Fork a child process.
6243
6244Return 0 to child process and PID of child to parent process.
6245[clinic start generated code]*/
6246
Larry Hastings2f936352014-08-05 14:04:04 +10006247static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006248os_fork_impl(PyObject *module)
6249/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006250{
Victor Stinner8c62be82010-05-06 00:08:46 +00006251 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006252
Victor Stinner81a7be32020-04-14 15:14:01 +02006253 if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006254 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6255 return NULL;
6256 }
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006257 if (PySys_Audit("os.fork", NULL) < 0) {
6258 return NULL;
6259 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006260 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006261 pid = fork();
6262 if (pid == 0) {
6263 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006264 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006265 } else {
6266 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006267 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006268 }
6269 if (pid == -1)
6270 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006271 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006272}
Larry Hastings2f936352014-08-05 14:04:04 +10006273#endif /* HAVE_FORK */
6274
Guido van Rossum85e3b011991-06-03 12:42:10 +00006275
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006276#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006277#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006278/*[clinic input]
6279os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006280
Larry Hastings2f936352014-08-05 14:04:04 +10006281 policy: int
6282
6283Get the maximum scheduling priority for policy.
6284[clinic start generated code]*/
6285
Larry Hastings2f936352014-08-05 14:04:04 +10006286static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006287os_sched_get_priority_max_impl(PyObject *module, int policy)
6288/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006289{
6290 int max;
6291
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006292 max = sched_get_priority_max(policy);
6293 if (max < 0)
6294 return posix_error();
6295 return PyLong_FromLong(max);
6296}
6297
Larry Hastings2f936352014-08-05 14:04:04 +10006298
6299/*[clinic input]
6300os.sched_get_priority_min
6301
6302 policy: int
6303
6304Get the minimum scheduling priority for policy.
6305[clinic start generated code]*/
6306
Larry Hastings2f936352014-08-05 14:04:04 +10006307static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006308os_sched_get_priority_min_impl(PyObject *module, int policy)
6309/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006310{
6311 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006312 if (min < 0)
6313 return posix_error();
6314 return PyLong_FromLong(min);
6315}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006316#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6317
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006318
Larry Hastings2f936352014-08-05 14:04:04 +10006319#ifdef HAVE_SCHED_SETSCHEDULER
6320/*[clinic input]
6321os.sched_getscheduler
6322 pid: pid_t
6323 /
6324
Min ho Kimc4cacc82019-07-31 08:16:13 +10006325Get the scheduling policy for the process identified by pid.
Larry Hastings2f936352014-08-05 14:04:04 +10006326
6327Passing 0 for pid returns the scheduling policy for the calling process.
6328[clinic start generated code]*/
6329
Larry Hastings2f936352014-08-05 14:04:04 +10006330static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006331os_sched_getscheduler_impl(PyObject *module, pid_t pid)
Min ho Kimc4cacc82019-07-31 08:16:13 +10006332/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006333{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006334 int policy;
6335
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006336 policy = sched_getscheduler(pid);
6337 if (policy < 0)
6338 return posix_error();
6339 return PyLong_FromLong(policy);
6340}
Larry Hastings2f936352014-08-05 14:04:04 +10006341#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006342
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006343
William Orr81574b82018-10-01 22:19:56 -07006344#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006345/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006346class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006347
6348@classmethod
6349os.sched_param.__new__
6350
6351 sched_priority: object
6352 A scheduling parameter.
6353
Eddie Elizondob3966632019-11-05 07:16:14 -08006354Currently has only one field: sched_priority
Larry Hastings2f936352014-08-05 14:04:04 +10006355[clinic start generated code]*/
6356
Larry Hastings2f936352014-08-05 14:04:04 +10006357static PyObject *
6358os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondob3966632019-11-05 07:16:14 -08006359/*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006360{
6361 PyObject *res;
6362
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006363 res = PyStructSequence_New(type);
6364 if (!res)
6365 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006366 Py_INCREF(sched_priority);
6367 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006368 return res;
6369}
6370
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006371PyDoc_VAR(os_sched_param__doc__);
6372
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006373static PyStructSequence_Field sched_param_fields[] = {
6374 {"sched_priority", "the scheduling priority"},
6375 {0}
6376};
6377
6378static PyStructSequence_Desc sched_param_desc = {
6379 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006380 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006381 sched_param_fields,
6382 1
6383};
6384
6385static int
6386convert_sched_param(PyObject *param, struct sched_param *res)
6387{
6388 long priority;
6389
Andy Lester55728702020-03-06 16:53:17 -06006390 if (!Py_IS_TYPE(param, (PyTypeObject *)_posixstate_global->SchedParamType)) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006391 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6392 return 0;
6393 }
6394 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6395 if (priority == -1 && PyErr_Occurred())
6396 return 0;
6397 if (priority > INT_MAX || priority < INT_MIN) {
6398 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6399 return 0;
6400 }
6401 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6402 return 1;
6403}
William Orr81574b82018-10-01 22:19:56 -07006404#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006405
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006406
6407#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006408/*[clinic input]
6409os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006410
Larry Hastings2f936352014-08-05 14:04:04 +10006411 pid: pid_t
6412 policy: int
6413 param: sched_param
6414 /
6415
6416Set the scheduling policy for the process identified by pid.
6417
6418If pid is 0, the calling process is changed.
6419param is an instance of sched_param.
6420[clinic start generated code]*/
6421
Larry Hastings2f936352014-08-05 14:04:04 +10006422static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006423os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006424 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006425/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006426{
Jesus Cea9c822272011-09-10 01:40:52 +02006427 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006428 ** sched_setscheduler() returns 0 in Linux, but the previous
6429 ** scheduling policy under Solaris/Illumos, and others.
6430 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006431 */
Larry Hastings2f936352014-08-05 14:04:04 +10006432 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006433 return posix_error();
6434 Py_RETURN_NONE;
6435}
Larry Hastings2f936352014-08-05 14:04:04 +10006436#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006437
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006438
6439#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006440/*[clinic input]
6441os.sched_getparam
6442 pid: pid_t
6443 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006444
Larry Hastings2f936352014-08-05 14:04:04 +10006445Returns scheduling parameters for the process identified by pid.
6446
6447If pid is 0, returns parameters for the calling process.
6448Return value is an instance of sched_param.
6449[clinic start generated code]*/
6450
Larry Hastings2f936352014-08-05 14:04:04 +10006451static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006452os_sched_getparam_impl(PyObject *module, pid_t pid)
6453/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006454{
6455 struct sched_param param;
6456 PyObject *result;
6457 PyObject *priority;
6458
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006459 if (sched_getparam(pid, &param))
6460 return posix_error();
Eddie Elizondob3966632019-11-05 07:16:14 -08006461 PyObject *SchedParamType = _posixstate_global->SchedParamType;
6462 result = PyStructSequence_New((PyTypeObject *)SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006463 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006464 return NULL;
6465 priority = PyLong_FromLong(param.sched_priority);
6466 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006467 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006468 return NULL;
6469 }
Larry Hastings2f936352014-08-05 14:04:04 +10006470 PyStructSequence_SET_ITEM(result, 0, priority);
6471 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006472}
6473
Larry Hastings2f936352014-08-05 14:04:04 +10006474
6475/*[clinic input]
6476os.sched_setparam
6477 pid: pid_t
6478 param: sched_param
6479 /
6480
6481Set scheduling parameters for the process identified by pid.
6482
6483If pid is 0, sets parameters for the calling process.
6484param should be an instance of sched_param.
6485[clinic start generated code]*/
6486
Larry Hastings2f936352014-08-05 14:04:04 +10006487static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006488os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006489 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006490/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006491{
6492 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006493 return posix_error();
6494 Py_RETURN_NONE;
6495}
Larry Hastings2f936352014-08-05 14:04:04 +10006496#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006497
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006498
6499#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006500/*[clinic input]
6501os.sched_rr_get_interval -> double
6502 pid: pid_t
6503 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006504
Larry Hastings2f936352014-08-05 14:04:04 +10006505Return the round-robin quantum for the process identified by pid, in seconds.
6506
6507Value returned is a float.
6508[clinic start generated code]*/
6509
Larry Hastings2f936352014-08-05 14:04:04 +10006510static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006511os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6512/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006513{
6514 struct timespec interval;
6515 if (sched_rr_get_interval(pid, &interval)) {
6516 posix_error();
6517 return -1.0;
6518 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006519#ifdef _Py_MEMORY_SANITIZER
6520 __msan_unpoison(&interval, sizeof(interval));
6521#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006522 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6523}
6524#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006525
Larry Hastings2f936352014-08-05 14:04:04 +10006526
6527/*[clinic input]
6528os.sched_yield
6529
6530Voluntarily relinquish the CPU.
6531[clinic start generated code]*/
6532
Larry Hastings2f936352014-08-05 14:04:04 +10006533static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006534os_sched_yield_impl(PyObject *module)
6535/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006536{
6537 if (sched_yield())
6538 return posix_error();
6539 Py_RETURN_NONE;
6540}
6541
Benjamin Peterson2740af82011-08-02 17:41:34 -05006542#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006543/* The minimum number of CPUs allocated in a cpu_set_t */
6544static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006545
Larry Hastings2f936352014-08-05 14:04:04 +10006546/*[clinic input]
6547os.sched_setaffinity
6548 pid: pid_t
6549 mask : object
6550 /
6551
6552Set the CPU affinity of the process identified by pid to mask.
6553
6554mask should be an iterable of integers identifying CPUs.
6555[clinic start generated code]*/
6556
Larry Hastings2f936352014-08-05 14:04:04 +10006557static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006558os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6559/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006560{
Antoine Pitrou84869872012-08-04 16:16:35 +02006561 int ncpus;
6562 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006563 cpu_set_t *cpu_set = NULL;
6564 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006565
Larry Hastings2f936352014-08-05 14:04:04 +10006566 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006567 if (iterator == NULL)
6568 return NULL;
6569
6570 ncpus = NCPUS_START;
6571 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006572 cpu_set = CPU_ALLOC(ncpus);
6573 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006574 PyErr_NoMemory();
6575 goto error;
6576 }
Larry Hastings2f936352014-08-05 14:04:04 +10006577 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006578
6579 while ((item = PyIter_Next(iterator))) {
6580 long cpu;
6581 if (!PyLong_Check(item)) {
6582 PyErr_Format(PyExc_TypeError,
6583 "expected an iterator of ints, "
6584 "but iterator yielded %R",
6585 Py_TYPE(item));
6586 Py_DECREF(item);
6587 goto error;
6588 }
6589 cpu = PyLong_AsLong(item);
6590 Py_DECREF(item);
6591 if (cpu < 0) {
6592 if (!PyErr_Occurred())
6593 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6594 goto error;
6595 }
6596 if (cpu > INT_MAX - 1) {
6597 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6598 goto error;
6599 }
6600 if (cpu >= ncpus) {
6601 /* Grow CPU mask to fit the CPU number */
6602 int newncpus = ncpus;
6603 cpu_set_t *newmask;
6604 size_t newsetsize;
6605 while (newncpus <= cpu) {
6606 if (newncpus > INT_MAX / 2)
6607 newncpus = cpu + 1;
6608 else
6609 newncpus = newncpus * 2;
6610 }
6611 newmask = CPU_ALLOC(newncpus);
6612 if (newmask == NULL) {
6613 PyErr_NoMemory();
6614 goto error;
6615 }
6616 newsetsize = CPU_ALLOC_SIZE(newncpus);
6617 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006618 memcpy(newmask, cpu_set, setsize);
6619 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006620 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006621 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006622 ncpus = newncpus;
6623 }
Larry Hastings2f936352014-08-05 14:04:04 +10006624 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006625 }
Brandt Bucher45a30af2019-06-27 09:10:57 -07006626 if (PyErr_Occurred()) {
6627 goto error;
6628 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006629 Py_CLEAR(iterator);
6630
Larry Hastings2f936352014-08-05 14:04:04 +10006631 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006632 posix_error();
6633 goto error;
6634 }
Larry Hastings2f936352014-08-05 14:04:04 +10006635 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006636 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006637
6638error:
Larry Hastings2f936352014-08-05 14:04:04 +10006639 if (cpu_set)
6640 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006641 Py_XDECREF(iterator);
6642 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006643}
6644
Larry Hastings2f936352014-08-05 14:04:04 +10006645
6646/*[clinic input]
6647os.sched_getaffinity
6648 pid: pid_t
6649 /
6650
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006651Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006652
6653The affinity is returned as a set of CPU identifiers.
6654[clinic start generated code]*/
6655
Larry Hastings2f936352014-08-05 14:04:04 +10006656static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006657os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006658/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006659{
Antoine Pitrou84869872012-08-04 16:16:35 +02006660 int cpu, ncpus, count;
6661 size_t setsize;
6662 cpu_set_t *mask = NULL;
6663 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006664
Antoine Pitrou84869872012-08-04 16:16:35 +02006665 ncpus = NCPUS_START;
6666 while (1) {
6667 setsize = CPU_ALLOC_SIZE(ncpus);
6668 mask = CPU_ALLOC(ncpus);
6669 if (mask == NULL)
6670 return PyErr_NoMemory();
6671 if (sched_getaffinity(pid, setsize, mask) == 0)
6672 break;
6673 CPU_FREE(mask);
6674 if (errno != EINVAL)
6675 return posix_error();
6676 if (ncpus > INT_MAX / 2) {
6677 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6678 "a large enough CPU set");
6679 return NULL;
6680 }
6681 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006682 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006683
6684 res = PySet_New(NULL);
6685 if (res == NULL)
6686 goto error;
6687 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6688 if (CPU_ISSET_S(cpu, setsize, mask)) {
6689 PyObject *cpu_num = PyLong_FromLong(cpu);
6690 --count;
6691 if (cpu_num == NULL)
6692 goto error;
6693 if (PySet_Add(res, cpu_num)) {
6694 Py_DECREF(cpu_num);
6695 goto error;
6696 }
6697 Py_DECREF(cpu_num);
6698 }
6699 }
6700 CPU_FREE(mask);
6701 return res;
6702
6703error:
6704 if (mask)
6705 CPU_FREE(mask);
6706 Py_XDECREF(res);
6707 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006708}
6709
Benjamin Peterson2740af82011-08-02 17:41:34 -05006710#endif /* HAVE_SCHED_SETAFFINITY */
6711
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006712#endif /* HAVE_SCHED_H */
6713
Larry Hastings2f936352014-08-05 14:04:04 +10006714
Neal Norwitzb59798b2003-03-21 01:43:31 +00006715/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006716/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6717#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006718#define DEV_PTY_FILE "/dev/ptc"
6719#define HAVE_DEV_PTMX
6720#else
6721#define DEV_PTY_FILE "/dev/ptmx"
6722#endif
6723
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006724#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006725#ifdef HAVE_PTY_H
6726#include <pty.h>
6727#else
6728#ifdef HAVE_LIBUTIL_H
6729#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006730#else
6731#ifdef HAVE_UTIL_H
6732#include <util.h>
6733#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006734#endif /* HAVE_LIBUTIL_H */
6735#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006736#ifdef HAVE_STROPTS_H
6737#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006738#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006739#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006740
Larry Hastings2f936352014-08-05 14:04:04 +10006741
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006742#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006743/*[clinic input]
6744os.openpty
6745
6746Open a pseudo-terminal.
6747
6748Return a tuple of (master_fd, slave_fd) containing open file descriptors
6749for both the master and slave ends.
6750[clinic start generated code]*/
6751
Larry Hastings2f936352014-08-05 14:04:04 +10006752static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006753os_openpty_impl(PyObject *module)
6754/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006755{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006756 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006757#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006758 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006759#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006760#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006761 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006762#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006764#endif
6765#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006766
Thomas Wouters70c21a12000-07-14 14:28:33 +00006767#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006768 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006769 goto posix_error;
6770
6771 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6772 goto error;
6773 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6774 goto error;
6775
Neal Norwitzb59798b2003-03-21 01:43:31 +00006776#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006777 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6778 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006779 goto posix_error;
6780 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6781 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006782
Victor Stinnerdaf45552013-08-28 00:53:59 +02006783 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006784 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006785 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006786
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006787#else
Victor Stinner000de532013-11-25 23:19:58 +01006788 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006789 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006790 goto posix_error;
6791
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006793
Victor Stinner8c62be82010-05-06 00:08:46 +00006794 /* change permission of slave */
6795 if (grantpt(master_fd) < 0) {
6796 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006797 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006799
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 /* unlock slave */
6801 if (unlockpt(master_fd) < 0) {
6802 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006803 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006804 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006805
Victor Stinner8c62be82010-05-06 00:08:46 +00006806 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006807
Victor Stinner8c62be82010-05-06 00:08:46 +00006808 slave_name = ptsname(master_fd); /* get name of slave */
6809 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006810 goto posix_error;
6811
6812 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006813 if (slave_fd == -1)
6814 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006815
6816 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6817 goto posix_error;
6818
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006819#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006820 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6821 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006822#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006823 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006824#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006825#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006826#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006827
Victor Stinner8c62be82010-05-06 00:08:46 +00006828 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006829
Victor Stinnerdaf45552013-08-28 00:53:59 +02006830posix_error:
6831 posix_error();
6832error:
6833 if (master_fd != -1)
6834 close(master_fd);
6835 if (slave_fd != -1)
6836 close(slave_fd);
6837 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006838}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006839#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006840
Larry Hastings2f936352014-08-05 14:04:04 +10006841
Fred Drake8cef4cf2000-06-28 16:40:38 +00006842#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006843/*[clinic input]
6844os.forkpty
6845
6846Fork a new process with a new pseudo-terminal as controlling tty.
6847
6848Returns a tuple of (pid, master_fd).
6849Like fork(), return pid of 0 to the child process,
6850and pid of child to the parent process.
6851To both, return fd of newly opened pseudo-terminal.
6852[clinic start generated code]*/
6853
Larry Hastings2f936352014-08-05 14:04:04 +10006854static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006855os_forkpty_impl(PyObject *module)
6856/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006857{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006858 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006860
Victor Stinner81a7be32020-04-14 15:14:01 +02006861 if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006862 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6863 return NULL;
6864 }
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006865 if (PySys_Audit("os.forkpty", NULL) < 0) {
6866 return NULL;
6867 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006868 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006869 pid = forkpty(&master_fd, NULL, NULL, NULL);
6870 if (pid == 0) {
6871 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006872 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006873 } else {
6874 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006875 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006876 }
6877 if (pid == -1)
6878 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006880}
Larry Hastings2f936352014-08-05 14:04:04 +10006881#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006882
Ross Lagerwall7807c352011-03-17 20:20:30 +02006883
Guido van Rossumad0ee831995-03-01 10:34:45 +00006884#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006885/*[clinic input]
6886os.getegid
6887
6888Return the current process's effective group id.
6889[clinic start generated code]*/
6890
Larry Hastings2f936352014-08-05 14:04:04 +10006891static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006892os_getegid_impl(PyObject *module)
6893/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006894{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006895 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006896}
Larry Hastings2f936352014-08-05 14:04:04 +10006897#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006898
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006899
Guido van Rossumad0ee831995-03-01 10:34:45 +00006900#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006901/*[clinic input]
6902os.geteuid
6903
6904Return the current process's effective user id.
6905[clinic start generated code]*/
6906
Larry Hastings2f936352014-08-05 14:04:04 +10006907static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006908os_geteuid_impl(PyObject *module)
6909/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006910{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006911 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006912}
Larry Hastings2f936352014-08-05 14:04:04 +10006913#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006914
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006915
Guido van Rossumad0ee831995-03-01 10:34:45 +00006916#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006917/*[clinic input]
6918os.getgid
6919
6920Return the current process's group id.
6921[clinic start generated code]*/
6922
Larry Hastings2f936352014-08-05 14:04:04 +10006923static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006924os_getgid_impl(PyObject *module)
6925/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006926{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006927 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006928}
Larry Hastings2f936352014-08-05 14:04:04 +10006929#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006930
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006931
Berker Peksag39404992016-09-15 20:45:16 +03006932#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006933/*[clinic input]
6934os.getpid
6935
6936Return the current process id.
6937[clinic start generated code]*/
6938
Larry Hastings2f936352014-08-05 14:04:04 +10006939static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006940os_getpid_impl(PyObject *module)
6941/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006942{
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006944}
Berker Peksag39404992016-09-15 20:45:16 +03006945#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006946
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006947#ifdef NGROUPS_MAX
6948#define MAX_GROUPS NGROUPS_MAX
6949#else
6950 /* defined to be 16 on Solaris7, so this should be a small number */
6951#define MAX_GROUPS 64
6952#endif
6953
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006954#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006955
6956/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006957PyDoc_STRVAR(posix_getgrouplist__doc__,
6958"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6959Returns a list of groups to which a user belongs.\n\n\
6960 user: username to lookup\n\
6961 group: base group id of the user");
6962
6963static PyObject *
6964posix_getgrouplist(PyObject *self, PyObject *args)
6965{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006966 const char *user;
6967 int i, ngroups;
6968 PyObject *list;
6969#ifdef __APPLE__
6970 int *groups, basegid;
6971#else
6972 gid_t *groups, basegid;
6973#endif
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006974
6975 /*
6976 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6977 * number of supplimental groups a users can belong to.
6978 * We have to increment it by one because
6979 * getgrouplist() returns both the supplemental groups
6980 * and the primary group, i.e. all of the groups the
6981 * user belongs to.
6982 */
6983 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006984
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006985#ifdef __APPLE__
6986 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006987 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006988#else
6989 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6990 _Py_Gid_Converter, &basegid))
6991 return NULL;
6992#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006993
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01006994 while (1) {
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006995#ifdef __APPLE__
Victor Stinner8ec73702020-03-23 20:00:57 +01006996 groups = PyMem_New(int, ngroups);
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01006997#else
6998 groups = PyMem_New(gid_t, ngroups);
6999#endif
Victor Stinner8ec73702020-03-23 20:00:57 +01007000 if (groups == NULL) {
7001 return PyErr_NoMemory();
7002 }
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007003
7004 int old_ngroups = ngroups;
7005 if (getgrouplist(user, basegid, groups, &ngroups) != -1) {
7006 /* Success */
7007 break;
7008 }
7009
7010 /* getgrouplist() fails if the group list is too small */
7011 PyMem_Free(groups);
7012
7013 if (ngroups > old_ngroups) {
7014 /* If the group list is too small, the glibc implementation of
7015 getgrouplist() sets ngroups to the total number of groups and
7016 returns -1. */
7017 }
7018 else {
7019 /* Double the group list size */
7020 if (ngroups > INT_MAX / 2) {
7021 return PyErr_NoMemory();
7022 }
7023 ngroups *= 2;
7024 }
7025
7026 /* Retry getgrouplist() with a larger group list */
Victor Stinner8ec73702020-03-23 20:00:57 +01007027 }
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007028
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08007029#ifdef _Py_MEMORY_SANITIZER
7030 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
7031 __msan_unpoison(&ngroups, sizeof(ngroups));
7032 __msan_unpoison(groups, ngroups*sizeof(*groups));
7033#endif
7034
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007035 list = PyList_New(ngroups);
7036 if (list == NULL) {
7037 PyMem_Del(groups);
7038 return NULL;
7039 }
7040
7041 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007042#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007043 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007044#else
7045 PyObject *o = _PyLong_FromGid(groups[i]);
7046#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007047 if (o == NULL) {
7048 Py_DECREF(list);
7049 PyMem_Del(groups);
7050 return NULL;
7051 }
7052 PyList_SET_ITEM(list, i, o);
7053 }
7054
7055 PyMem_Del(groups);
7056
7057 return list;
7058}
Larry Hastings2f936352014-08-05 14:04:04 +10007059#endif /* HAVE_GETGROUPLIST */
7060
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007061
Fred Drakec9680921999-12-13 16:37:25 +00007062#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007063/*[clinic input]
7064os.getgroups
7065
7066Return list of supplemental group IDs for the process.
7067[clinic start generated code]*/
7068
Larry Hastings2f936352014-08-05 14:04:04 +10007069static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007070os_getgroups_impl(PyObject *module)
7071/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00007072{
7073 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007075
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007076 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007077 * This is a helper variable to store the intermediate result when
7078 * that happens.
7079 *
7080 * To keep the code readable the OSX behaviour is unconditional,
7081 * according to the POSIX spec this should be safe on all unix-y
7082 * systems.
7083 */
7084 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00007085 int n;
Fred Drakec9680921999-12-13 16:37:25 +00007086
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007087#ifdef __APPLE__
7088 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
7089 * there are more groups than can fit in grouplist. Therefore, on OS X
7090 * always first call getgroups with length 0 to get the actual number
7091 * of groups.
7092 */
7093 n = getgroups(0, NULL);
7094 if (n < 0) {
7095 return posix_error();
7096 } else if (n <= MAX_GROUPS) {
7097 /* groups will fit in existing array */
7098 alt_grouplist = grouplist;
7099 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007100 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007101 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007102 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007103 }
7104 }
7105
7106 n = getgroups(n, alt_grouplist);
7107 if (n == -1) {
7108 if (alt_grouplist != grouplist) {
7109 PyMem_Free(alt_grouplist);
7110 }
7111 return posix_error();
7112 }
7113#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007115 if (n < 0) {
7116 if (errno == EINVAL) {
7117 n = getgroups(0, NULL);
7118 if (n == -1) {
7119 return posix_error();
7120 }
7121 if (n == 0) {
7122 /* Avoid malloc(0) */
7123 alt_grouplist = grouplist;
7124 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007125 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007126 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007127 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007128 }
7129 n = getgroups(n, alt_grouplist);
7130 if (n == -1) {
7131 PyMem_Free(alt_grouplist);
7132 return posix_error();
7133 }
7134 }
7135 } else {
7136 return posix_error();
7137 }
7138 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007139#endif
7140
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007141 result = PyList_New(n);
7142 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007143 int i;
7144 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007145 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007147 Py_DECREF(result);
7148 result = NULL;
7149 break;
Fred Drakec9680921999-12-13 16:37:25 +00007150 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007151 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00007152 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007153 }
7154
7155 if (alt_grouplist != grouplist) {
7156 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00007157 }
Neal Norwitze241ce82003-02-17 18:17:05 +00007158
Fred Drakec9680921999-12-13 16:37:25 +00007159 return result;
7160}
Larry Hastings2f936352014-08-05 14:04:04 +10007161#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00007162
Antoine Pitroub7572f02009-12-02 20:46:48 +00007163#ifdef HAVE_INITGROUPS
7164PyDoc_STRVAR(posix_initgroups__doc__,
7165"initgroups(username, gid) -> None\n\n\
7166Call the system initgroups() to initialize the group access list with all of\n\
7167the groups of which the specified username is a member, plus the specified\n\
7168group id.");
7169
Larry Hastings2f936352014-08-05 14:04:04 +10007170/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007171static PyObject *
7172posix_initgroups(PyObject *self, PyObject *args)
7173{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007174 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007175 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007176 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007177#ifdef __APPLE__
7178 int gid;
7179#else
7180 gid_t gid;
7181#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00007182
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007183#ifdef __APPLE__
7184 if (!PyArg_ParseTuple(args, "O&i:initgroups",
7185 PyUnicode_FSConverter, &oname,
7186 &gid))
7187#else
7188 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
7189 PyUnicode_FSConverter, &oname,
7190 _Py_Gid_Converter, &gid))
7191#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007193 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007194
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007195 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007196 Py_DECREF(oname);
7197 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007199
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007200 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00007201}
Larry Hastings2f936352014-08-05 14:04:04 +10007202#endif /* HAVE_INITGROUPS */
7203
Antoine Pitroub7572f02009-12-02 20:46:48 +00007204
Martin v. Löwis606edc12002-06-13 21:09:11 +00007205#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007206/*[clinic input]
7207os.getpgid
7208
7209 pid: pid_t
7210
7211Call the system call getpgid(), and return the result.
7212[clinic start generated code]*/
7213
Larry Hastings2f936352014-08-05 14:04:04 +10007214static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007215os_getpgid_impl(PyObject *module, pid_t pid)
7216/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007217{
7218 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 if (pgid < 0)
7220 return posix_error();
7221 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00007222}
7223#endif /* HAVE_GETPGID */
7224
7225
Guido van Rossumb6775db1994-08-01 11:34:53 +00007226#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007227/*[clinic input]
7228os.getpgrp
7229
7230Return the current process group id.
7231[clinic start generated code]*/
7232
Larry Hastings2f936352014-08-05 14:04:04 +10007233static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007234os_getpgrp_impl(PyObject *module)
7235/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007236{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007237#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007238 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007239#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007241#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007242}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007243#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007244
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007245
Guido van Rossumb6775db1994-08-01 11:34:53 +00007246#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007247/*[clinic input]
7248os.setpgrp
7249
7250Make the current process the leader of its process group.
7251[clinic start generated code]*/
7252
Larry Hastings2f936352014-08-05 14:04:04 +10007253static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007254os_setpgrp_impl(PyObject *module)
7255/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007256{
Guido van Rossum64933891994-10-20 21:56:42 +00007257#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007259#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007260 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007261#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007262 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007263 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007264}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007265#endif /* HAVE_SETPGRP */
7266
Guido van Rossumad0ee831995-03-01 10:34:45 +00007267#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007268
7269#ifdef MS_WINDOWS
7270#include <tlhelp32.h>
7271
7272static PyObject*
7273win32_getppid()
7274{
7275 HANDLE snapshot;
7276 pid_t mypid;
7277 PyObject* result = NULL;
7278 BOOL have_record;
7279 PROCESSENTRY32 pe;
7280
7281 mypid = getpid(); /* This function never fails */
7282
7283 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7284 if (snapshot == INVALID_HANDLE_VALUE)
7285 return PyErr_SetFromWindowsErr(GetLastError());
7286
7287 pe.dwSize = sizeof(pe);
7288 have_record = Process32First(snapshot, &pe);
7289 while (have_record) {
7290 if (mypid == (pid_t)pe.th32ProcessID) {
7291 /* We could cache the ulong value in a static variable. */
7292 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7293 break;
7294 }
7295
7296 have_record = Process32Next(snapshot, &pe);
7297 }
7298
7299 /* If our loop exits and our pid was not found (result will be NULL)
7300 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7301 * error anyway, so let's raise it. */
7302 if (!result)
7303 result = PyErr_SetFromWindowsErr(GetLastError());
7304
7305 CloseHandle(snapshot);
7306
7307 return result;
7308}
7309#endif /*MS_WINDOWS*/
7310
Larry Hastings2f936352014-08-05 14:04:04 +10007311
7312/*[clinic input]
7313os.getppid
7314
7315Return the parent's process id.
7316
7317If the parent process has already exited, Windows machines will still
7318return its id; others systems will return the id of the 'init' process (1).
7319[clinic start generated code]*/
7320
Larry Hastings2f936352014-08-05 14:04:04 +10007321static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007322os_getppid_impl(PyObject *module)
7323/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007324{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007325#ifdef MS_WINDOWS
7326 return win32_getppid();
7327#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007328 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007329#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007330}
7331#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007332
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007333
Fred Drake12c6e2d1999-12-14 21:25:03 +00007334#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007335/*[clinic input]
7336os.getlogin
7337
7338Return the actual login name.
7339[clinic start generated code]*/
7340
Larry Hastings2f936352014-08-05 14:04:04 +10007341static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007342os_getlogin_impl(PyObject *module)
7343/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007344{
Victor Stinner8c62be82010-05-06 00:08:46 +00007345 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007346#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007347 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007348 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007349
7350 if (GetUserNameW(user_name, &num_chars)) {
7351 /* num_chars is the number of unicode chars plus null terminator */
7352 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007353 }
7354 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007355 result = PyErr_SetFromWindowsErr(GetLastError());
7356#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007357 char *name;
7358 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007359
Victor Stinner8c62be82010-05-06 00:08:46 +00007360 errno = 0;
7361 name = getlogin();
7362 if (name == NULL) {
7363 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007364 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007365 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007366 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007367 }
7368 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007369 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007370 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007371#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007372 return result;
7373}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007374#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007375
Larry Hastings2f936352014-08-05 14:04:04 +10007376
Guido van Rossumad0ee831995-03-01 10:34:45 +00007377#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007378/*[clinic input]
7379os.getuid
7380
7381Return the current process's user id.
7382[clinic start generated code]*/
7383
Larry Hastings2f936352014-08-05 14:04:04 +10007384static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007385os_getuid_impl(PyObject *module)
7386/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007387{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007388 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007389}
Larry Hastings2f936352014-08-05 14:04:04 +10007390#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007391
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007392
Brian Curtineb24d742010-04-12 17:16:38 +00007393#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007394#define HAVE_KILL
7395#endif /* MS_WINDOWS */
7396
7397#ifdef HAVE_KILL
7398/*[clinic input]
7399os.kill
7400
7401 pid: pid_t
7402 signal: Py_ssize_t
7403 /
7404
7405Kill a process with a signal.
7406[clinic start generated code]*/
7407
Larry Hastings2f936352014-08-05 14:04:04 +10007408static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007409os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7410/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007411{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007412 if (PySys_Audit("os.kill", "in", pid, signal) < 0) {
7413 return NULL;
7414 }
7415#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007416 if (kill(pid, (int)signal) == -1)
7417 return posix_error();
7418 Py_RETURN_NONE;
Larry Hastings2f936352014-08-05 14:04:04 +10007419#else /* !MS_WINDOWS */
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007420 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007421 DWORD sig = (DWORD)signal;
7422 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007423 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007424
Victor Stinner8c62be82010-05-06 00:08:46 +00007425 /* Console processes which share a common console can be sent CTRL+C or
7426 CTRL+BREAK events, provided they handle said events. */
7427 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007428 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007429 err = GetLastError();
7430 PyErr_SetFromWindowsErr(err);
7431 }
7432 else
7433 Py_RETURN_NONE;
7434 }
Brian Curtineb24d742010-04-12 17:16:38 +00007435
Victor Stinner8c62be82010-05-06 00:08:46 +00007436 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7437 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007438 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007439 if (handle == NULL) {
7440 err = GetLastError();
7441 return PyErr_SetFromWindowsErr(err);
7442 }
Brian Curtineb24d742010-04-12 17:16:38 +00007443
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 if (TerminateProcess(handle, sig) == 0) {
7445 err = GetLastError();
7446 result = PyErr_SetFromWindowsErr(err);
7447 } else {
7448 Py_INCREF(Py_None);
7449 result = Py_None;
7450 }
Brian Curtineb24d742010-04-12 17:16:38 +00007451
Victor Stinner8c62be82010-05-06 00:08:46 +00007452 CloseHandle(handle);
7453 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10007454#endif /* !MS_WINDOWS */
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007455}
Larry Hastings2f936352014-08-05 14:04:04 +10007456#endif /* HAVE_KILL */
7457
7458
7459#ifdef HAVE_KILLPG
7460/*[clinic input]
7461os.killpg
7462
7463 pgid: pid_t
7464 signal: int
7465 /
7466
7467Kill a process group with a signal.
7468[clinic start generated code]*/
7469
Larry Hastings2f936352014-08-05 14:04:04 +10007470static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007471os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7472/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007473{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007474 if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) {
7475 return NULL;
7476 }
Larry Hastings2f936352014-08-05 14:04:04 +10007477 /* XXX some man pages make the `pgid` parameter an int, others
7478 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7479 take the same type. Moreover, pid_t is always at least as wide as
7480 int (else compilation of this module fails), which is safe. */
7481 if (killpg(pgid, signal) == -1)
7482 return posix_error();
7483 Py_RETURN_NONE;
7484}
7485#endif /* HAVE_KILLPG */
7486
Brian Curtineb24d742010-04-12 17:16:38 +00007487
Guido van Rossumc0125471996-06-28 18:55:32 +00007488#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007489#ifdef HAVE_SYS_LOCK_H
7490#include <sys/lock.h>
7491#endif
7492
Larry Hastings2f936352014-08-05 14:04:04 +10007493/*[clinic input]
7494os.plock
7495 op: int
7496 /
7497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007498Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007499[clinic start generated code]*/
7500
Larry Hastings2f936352014-08-05 14:04:04 +10007501static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007502os_plock_impl(PyObject *module, int op)
7503/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007504{
Victor Stinner8c62be82010-05-06 00:08:46 +00007505 if (plock(op) == -1)
7506 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007507 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007508}
Larry Hastings2f936352014-08-05 14:04:04 +10007509#endif /* HAVE_PLOCK */
7510
Guido van Rossumc0125471996-06-28 18:55:32 +00007511
Guido van Rossumb6775db1994-08-01 11:34:53 +00007512#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007513/*[clinic input]
7514os.setuid
7515
7516 uid: uid_t
7517 /
7518
7519Set the current process's user id.
7520[clinic start generated code]*/
7521
Larry Hastings2f936352014-08-05 14:04:04 +10007522static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007523os_setuid_impl(PyObject *module, uid_t uid)
7524/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007525{
Victor Stinner8c62be82010-05-06 00:08:46 +00007526 if (setuid(uid) < 0)
7527 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007528 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007529}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007530#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007531
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007532
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007533#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007534/*[clinic input]
7535os.seteuid
7536
7537 euid: uid_t
7538 /
7539
7540Set the current process's effective user id.
7541[clinic start generated code]*/
7542
Larry Hastings2f936352014-08-05 14:04:04 +10007543static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007544os_seteuid_impl(PyObject *module, uid_t euid)
7545/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007546{
7547 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007548 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007549 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007550}
7551#endif /* HAVE_SETEUID */
7552
Larry Hastings2f936352014-08-05 14:04:04 +10007553
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007554#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007555/*[clinic input]
7556os.setegid
7557
7558 egid: gid_t
7559 /
7560
7561Set the current process's effective group id.
7562[clinic start generated code]*/
7563
Larry Hastings2f936352014-08-05 14:04:04 +10007564static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007565os_setegid_impl(PyObject *module, gid_t egid)
7566/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007567{
7568 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007569 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007570 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007571}
7572#endif /* HAVE_SETEGID */
7573
Larry Hastings2f936352014-08-05 14:04:04 +10007574
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007575#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007576/*[clinic input]
7577os.setreuid
7578
7579 ruid: uid_t
7580 euid: uid_t
7581 /
7582
7583Set the current process's real and effective user ids.
7584[clinic start generated code]*/
7585
Larry Hastings2f936352014-08-05 14:04:04 +10007586static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007587os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7588/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007589{
Victor Stinner8c62be82010-05-06 00:08:46 +00007590 if (setreuid(ruid, euid) < 0) {
7591 return posix_error();
7592 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007593 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007594 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007595}
7596#endif /* HAVE_SETREUID */
7597
Larry Hastings2f936352014-08-05 14:04:04 +10007598
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007599#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007600/*[clinic input]
7601os.setregid
7602
7603 rgid: gid_t
7604 egid: gid_t
7605 /
7606
7607Set the current process's real and effective group ids.
7608[clinic start generated code]*/
7609
Larry Hastings2f936352014-08-05 14:04:04 +10007610static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007611os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7612/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007613{
7614 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007615 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007616 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007617}
7618#endif /* HAVE_SETREGID */
7619
Larry Hastings2f936352014-08-05 14:04:04 +10007620
Guido van Rossumb6775db1994-08-01 11:34:53 +00007621#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007622/*[clinic input]
7623os.setgid
7624 gid: gid_t
7625 /
7626
7627Set the current process's group id.
7628[clinic start generated code]*/
7629
Larry Hastings2f936352014-08-05 14:04:04 +10007630static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007631os_setgid_impl(PyObject *module, gid_t gid)
7632/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007633{
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 if (setgid(gid) < 0)
7635 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007636 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007637}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007638#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007639
Larry Hastings2f936352014-08-05 14:04:04 +10007640
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007641#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007642/*[clinic input]
7643os.setgroups
7644
7645 groups: object
7646 /
7647
7648Set the groups of the current process to list.
7649[clinic start generated code]*/
7650
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007651static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007652os_setgroups(PyObject *module, PyObject *groups)
7653/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007654{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007655 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007656 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007657
Victor Stinner8c62be82010-05-06 00:08:46 +00007658 if (!PySequence_Check(groups)) {
7659 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7660 return NULL;
7661 }
7662 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007663 if (len < 0) {
7664 return NULL;
7665 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007666 if (len > MAX_GROUPS) {
7667 PyErr_SetString(PyExc_ValueError, "too many groups");
7668 return NULL;
7669 }
7670 for(i = 0; i < len; i++) {
7671 PyObject *elem;
7672 elem = PySequence_GetItem(groups, i);
7673 if (!elem)
7674 return NULL;
7675 if (!PyLong_Check(elem)) {
7676 PyErr_SetString(PyExc_TypeError,
7677 "groups must be integers");
7678 Py_DECREF(elem);
7679 return NULL;
7680 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007681 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007682 Py_DECREF(elem);
7683 return NULL;
7684 }
7685 }
7686 Py_DECREF(elem);
7687 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007688
Victor Stinner8c62be82010-05-06 00:08:46 +00007689 if (setgroups(len, grouplist) < 0)
7690 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007691 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007692}
7693#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007694
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007695#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7696static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007697wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007698{
Victor Stinner8c62be82010-05-06 00:08:46 +00007699 PyObject *result;
Eddie Elizondob3966632019-11-05 07:16:14 -08007700 PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007701
Victor Stinner8c62be82010-05-06 00:08:46 +00007702 if (pid == -1)
7703 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007704
Zackery Spytz682107c2019-09-09 09:48:32 -06007705 // If wait succeeded but no child was ready to report status, ru will not
7706 // have been populated.
7707 if (pid == 0) {
7708 memset(ru, 0, sizeof(*ru));
7709 }
7710
Eddie Elizondob3966632019-11-05 07:16:14 -08007711 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7712 if (m == NULL)
7713 return NULL;
7714 struct_rusage = PyObject_GetAttr(m, _posixstate_global->struct_rusage);
7715 Py_DECREF(m);
7716 if (struct_rusage == NULL)
7717 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007718
Victor Stinner8c62be82010-05-06 00:08:46 +00007719 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7720 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
Eddie Elizondoe4db1f02019-11-25 19:07:37 -08007721 Py_DECREF(struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007722 if (!result)
7723 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007724
7725#ifndef doubletime
7726#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7727#endif
7728
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007730 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007732 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007733#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7735 SET_INT(result, 2, ru->ru_maxrss);
7736 SET_INT(result, 3, ru->ru_ixrss);
7737 SET_INT(result, 4, ru->ru_idrss);
7738 SET_INT(result, 5, ru->ru_isrss);
7739 SET_INT(result, 6, ru->ru_minflt);
7740 SET_INT(result, 7, ru->ru_majflt);
7741 SET_INT(result, 8, ru->ru_nswap);
7742 SET_INT(result, 9, ru->ru_inblock);
7743 SET_INT(result, 10, ru->ru_oublock);
7744 SET_INT(result, 11, ru->ru_msgsnd);
7745 SET_INT(result, 12, ru->ru_msgrcv);
7746 SET_INT(result, 13, ru->ru_nsignals);
7747 SET_INT(result, 14, ru->ru_nvcsw);
7748 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007749#undef SET_INT
7750
Victor Stinner8c62be82010-05-06 00:08:46 +00007751 if (PyErr_Occurred()) {
7752 Py_DECREF(result);
7753 return NULL;
7754 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007755
Victor Stinner8c62be82010-05-06 00:08:46 +00007756 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007757}
7758#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7759
Larry Hastings2f936352014-08-05 14:04:04 +10007760
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007761#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007762/*[clinic input]
7763os.wait3
7764
7765 options: int
7766Wait for completion of a child process.
7767
7768Returns a tuple of information about the child process:
7769 (pid, status, rusage)
7770[clinic start generated code]*/
7771
Larry Hastings2f936352014-08-05 14:04:04 +10007772static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007773os_wait3_impl(PyObject *module, int options)
7774/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007775{
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007777 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007778 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007779 WAIT_TYPE status;
7780 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007781
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007782 do {
7783 Py_BEGIN_ALLOW_THREADS
7784 pid = wait3(&status, options, &ru);
7785 Py_END_ALLOW_THREADS
7786 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7787 if (pid < 0)
7788 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007789
Victor Stinner4195b5c2012-02-08 23:03:19 +01007790 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007791}
7792#endif /* HAVE_WAIT3 */
7793
Larry Hastings2f936352014-08-05 14:04:04 +10007794
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007795#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007796/*[clinic input]
7797
7798os.wait4
7799
7800 pid: pid_t
7801 options: int
7802
7803Wait for completion of a specific child process.
7804
7805Returns a tuple of information about the child process:
7806 (pid, status, rusage)
7807[clinic start generated code]*/
7808
Larry Hastings2f936352014-08-05 14:04:04 +10007809static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007810os_wait4_impl(PyObject *module, pid_t pid, int options)
7811/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007812{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007813 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007814 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007815 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007816 WAIT_TYPE status;
7817 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007818
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007819 do {
7820 Py_BEGIN_ALLOW_THREADS
7821 res = wait4(pid, &status, options, &ru);
7822 Py_END_ALLOW_THREADS
7823 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7824 if (res < 0)
7825 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007826
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007827 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007828}
7829#endif /* HAVE_WAIT4 */
7830
Larry Hastings2f936352014-08-05 14:04:04 +10007831
Ross Lagerwall7807c352011-03-17 20:20:30 +02007832#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007833/*[clinic input]
7834os.waitid
7835
7836 idtype: idtype_t
7837 Must be one of be P_PID, P_PGID or P_ALL.
7838 id: id_t
7839 The id to wait on.
7840 options: int
7841 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7842 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7843 /
7844
7845Returns the result of waiting for a process or processes.
7846
7847Returns either waitid_result or None if WNOHANG is specified and there are
7848no children in a waitable state.
7849[clinic start generated code]*/
7850
Larry Hastings2f936352014-08-05 14:04:04 +10007851static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007852os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7853/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007854{
7855 PyObject *result;
7856 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007857 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007858 siginfo_t si;
7859 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007860
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007861 do {
7862 Py_BEGIN_ALLOW_THREADS
7863 res = waitid(idtype, id, &si, options);
7864 Py_END_ALLOW_THREADS
7865 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7866 if (res < 0)
7867 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007868
7869 if (si.si_pid == 0)
7870 Py_RETURN_NONE;
7871
Hai Shif707d942020-03-16 21:15:01 +08007872 PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08007873 result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007874 if (!result)
7875 return NULL;
7876
7877 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007878 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007879 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7880 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7881 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7882 if (PyErr_Occurred()) {
7883 Py_DECREF(result);
7884 return NULL;
7885 }
7886
7887 return result;
7888}
Larry Hastings2f936352014-08-05 14:04:04 +10007889#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007890
Larry Hastings2f936352014-08-05 14:04:04 +10007891
7892#if defined(HAVE_WAITPID)
7893/*[clinic input]
7894os.waitpid
7895 pid: pid_t
7896 options: int
7897 /
7898
7899Wait for completion of a given child process.
7900
7901Returns a tuple of information regarding the child process:
7902 (pid, status)
7903
7904The options argument is ignored on Windows.
7905[clinic start generated code]*/
7906
Larry Hastings2f936352014-08-05 14:04:04 +10007907static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007908os_waitpid_impl(PyObject *module, pid_t pid, int options)
7909/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007910{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007911 pid_t res;
7912 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007913 WAIT_TYPE status;
7914 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007915
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007916 do {
7917 Py_BEGIN_ALLOW_THREADS
7918 res = waitpid(pid, &status, options);
7919 Py_END_ALLOW_THREADS
7920 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7921 if (res < 0)
7922 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007923
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007924 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007925}
Tim Petersab034fa2002-02-01 11:27:43 +00007926#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007927/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007928/*[clinic input]
7929os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007930 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007931 options: int
7932 /
7933
7934Wait for completion of a given process.
7935
7936Returns a tuple of information regarding the process:
7937 (pid, status << 8)
7938
7939The options argument is ignored on Windows.
7940[clinic start generated code]*/
7941
Larry Hastings2f936352014-08-05 14:04:04 +10007942static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007943os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007944/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007945{
7946 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007947 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007948 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007949
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007950 do {
7951 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007952 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007953 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007954 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007955 Py_END_ALLOW_THREADS
7956 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007957 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007958 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007959
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007961 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007962}
Larry Hastings2f936352014-08-05 14:04:04 +10007963#endif
7964
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007965
Guido van Rossumad0ee831995-03-01 10:34:45 +00007966#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007967/*[clinic input]
7968os.wait
7969
7970Wait for completion of a child process.
7971
7972Returns a tuple of information about the child process:
7973 (pid, status)
7974[clinic start generated code]*/
7975
Larry Hastings2f936352014-08-05 14:04:04 +10007976static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007977os_wait_impl(PyObject *module)
7978/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007979{
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007981 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 WAIT_TYPE status;
7983 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007984
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007985 do {
7986 Py_BEGIN_ALLOW_THREADS
7987 pid = wait(&status);
7988 Py_END_ALLOW_THREADS
7989 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7990 if (pid < 0)
7991 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007992
Victor Stinner8c62be82010-05-06 00:08:46 +00007993 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007994}
Larry Hastings2f936352014-08-05 14:04:04 +10007995#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007996
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -08007997#if defined(__linux__) && defined(__NR_pidfd_open)
7998/*[clinic input]
7999os.pidfd_open
8000 pid: pid_t
8001 flags: unsigned_int = 0
8002
8003Return a file descriptor referring to the process *pid*.
8004
8005The descriptor can be used to perform process management without races and
8006signals.
8007[clinic start generated code]*/
8008
8009static PyObject *
8010os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
8011/*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/
8012{
8013 int fd = syscall(__NR_pidfd_open, pid, flags);
8014 if (fd < 0) {
8015 return posix_error();
8016 }
8017 return PyLong_FromLong(fd);
8018}
8019#endif
8020
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008021
Larry Hastings9cf065c2012-06-22 16:30:09 -07008022#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008023/*[clinic input]
8024os.readlink
8025
8026 path: path_t
8027 *
8028 dir_fd: dir_fd(requires='readlinkat') = None
8029
8030Return a string representing the path to which the symbolic link points.
8031
8032If dir_fd is not None, it should be a file descriptor open to a directory,
8033and path should be relative; path will then be relative to that directory.
8034
8035dir_fd may not be implemented on your platform. If it is unavailable,
8036using it will raise a NotImplementedError.
8037[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008038
Barry Warsaw53699e91996-12-10 23:23:01 +00008039static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008040os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
8041/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008042{
Berker Peksage0b5b202018-08-15 13:03:41 +03008043#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02008044 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07008045 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008046
8047 Py_BEGIN_ALLOW_THREADS
8048#ifdef HAVE_READLINKAT
8049 if (dir_fd != DEFAULT_DIR_FD)
8050 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
8051 else
8052#endif
8053 length = readlink(path->narrow, buffer, MAXPATHLEN);
8054 Py_END_ALLOW_THREADS
8055
8056 if (length < 0) {
8057 return path_error(path);
8058 }
8059 buffer[length] = '\0';
8060
8061 if (PyUnicode_Check(path->object))
8062 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
8063 else
8064 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03008065#elif defined(MS_WINDOWS)
8066 DWORD n_bytes_returned;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008067 DWORD io_result = 0;
Berker Peksage0b5b202018-08-15 13:03:41 +03008068 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03008069 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
8070 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Steve Dower993ac922019-09-03 12:50:51 -07008071 PyObject *result = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00008072
Larry Hastings2f936352014-08-05 14:04:04 +10008073 /* First get a handle to the reparse point */
8074 Py_BEGIN_ALLOW_THREADS
8075 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008076 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10008077 0,
8078 0,
8079 0,
8080 OPEN_EXISTING,
8081 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
8082 0);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008083 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
8084 /* New call DeviceIoControl to read the reparse point */
8085 io_result = DeviceIoControl(
8086 reparse_point_handle,
8087 FSCTL_GET_REPARSE_POINT,
8088 0, 0, /* in buffer */
8089 target_buffer, sizeof(target_buffer),
8090 &n_bytes_returned,
8091 0 /* we're not using OVERLAPPED_IO */
8092 );
8093 CloseHandle(reparse_point_handle);
Berker Peksage0b5b202018-08-15 13:03:41 +03008094 }
Larry Hastings2f936352014-08-05 14:04:04 +10008095 Py_END_ALLOW_THREADS
8096
Berker Peksage0b5b202018-08-15 13:03:41 +03008097 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008098 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03008099 }
Larry Hastings2f936352014-08-05 14:04:04 +10008100
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008101 wchar_t *name = NULL;
8102 Py_ssize_t nameLen = 0;
8103 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Larry Hastings2f936352014-08-05 14:04:04 +10008104 {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008105 name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
8106 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
8107 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
Larry Hastings2f936352014-08-05 14:04:04 +10008108 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008109 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
8110 {
8111 name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
8112 rdb->MountPointReparseBuffer.SubstituteNameOffset);
8113 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
8114 }
8115 else
8116 {
8117 PyErr_SetString(PyExc_ValueError, "not a symbolic link");
8118 }
8119 if (name) {
8120 if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
8121 /* Our buffer is mutable, so this is okay */
8122 name[1] = L'\\';
8123 }
8124 result = PyUnicode_FromWideChar(name, nameLen);
Steve Dower993ac922019-09-03 12:50:51 -07008125 if (result && path->narrow) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008126 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
8127 }
Berker Peksage0b5b202018-08-15 13:03:41 +03008128 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008129 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03008130#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008131}
Berker Peksage0b5b202018-08-15 13:03:41 +03008132#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008133
Larry Hastings9cf065c2012-06-22 16:30:09 -07008134#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07008135
8136#if defined(MS_WINDOWS)
8137
Steve Dower6921e732018-03-05 14:26:08 -08008138/* Remove the last portion of the path - return 0 on success */
8139static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008140_dirnameW(WCHAR *path)
8141{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008142 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08008143 size_t length = wcsnlen_s(path, MAX_PATH);
8144 if (length == MAX_PATH) {
8145 return -1;
8146 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008147
8148 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08008149 for(ptr = path + length; ptr != path; ptr--) {
8150 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04008151 break;
Steve Dower6921e732018-03-05 14:26:08 -08008152 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008153 }
8154 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08008155 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008156}
8157
Victor Stinner31b3b922013-06-05 01:49:17 +02008158/* Is this path absolute? */
8159static int
8160_is_absW(const WCHAR *path)
8161{
Steve Dower6921e732018-03-05 14:26:08 -08008162 return path[0] == L'\\' || path[0] == L'/' ||
8163 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04008164}
8165
Steve Dower6921e732018-03-05 14:26:08 -08008166/* join root and rest with a backslash - return 0 on success */
8167static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008168_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
8169{
Victor Stinner31b3b922013-06-05 01:49:17 +02008170 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08008171 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008172 }
8173
Steve Dower6921e732018-03-05 14:26:08 -08008174 if (wcscpy_s(dest_path, MAX_PATH, root)) {
8175 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008176 }
Steve Dower6921e732018-03-05 14:26:08 -08008177
8178 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
8179 return -1;
8180 }
8181
8182 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008183}
8184
Victor Stinner31b3b922013-06-05 01:49:17 +02008185/* Return True if the path at src relative to dest is a directory */
8186static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03008187_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04008188{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008189 WIN32_FILE_ATTRIBUTE_DATA src_info;
8190 WCHAR dest_parent[MAX_PATH];
8191 WCHAR src_resolved[MAX_PATH] = L"";
8192
8193 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08008194 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
8195 _dirnameW(dest_parent)) {
8196 return 0;
8197 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008198 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08008199 if (_joinW(src_resolved, dest_parent, src)) {
8200 return 0;
8201 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008202 return (
8203 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
8204 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
8205 );
8206}
Larry Hastings9cf065c2012-06-22 16:30:09 -07008207#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008208
Larry Hastings2f936352014-08-05 14:04:04 +10008209
8210/*[clinic input]
8211os.symlink
8212 src: path_t
8213 dst: path_t
8214 target_is_directory: bool = False
8215 *
8216 dir_fd: dir_fd(requires='symlinkat')=None
8217
8218# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
8219
8220Create a symbolic link pointing to src named dst.
8221
8222target_is_directory is required on Windows if the target is to be
8223 interpreted as a directory. (On Windows, symlink requires
8224 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
8225 target_is_directory is ignored on non-Windows platforms.
8226
8227If dir_fd is not None, it should be a file descriptor open to a directory,
8228 and path should be relative; path will then be relative to that directory.
8229dir_fd may not be implemented on your platform.
8230 If it is unavailable, using it will raise a NotImplementedError.
8231
8232[clinic start generated code]*/
8233
Larry Hastings2f936352014-08-05 14:04:04 +10008234static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008235os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04008236 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008237/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008238{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008239#ifdef MS_WINDOWS
8240 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008241 DWORD flags = 0;
8242
8243 /* Assumed true, set to false if detected to not be available. */
8244 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008245#else
8246 int result;
8247#endif
8248
Saiyang Gou7514f4f2020-02-12 23:47:42 -08008249 if (PySys_Audit("os.symlink", "OOi", src->object, dst->object,
8250 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
8251 return NULL;
8252 }
8253
Larry Hastings9cf065c2012-06-22 16:30:09 -07008254#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008255
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008256 if (windows_has_symlink_unprivileged_flag) {
8257 /* Allow non-admin symlinks if system allows it. */
8258 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8259 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008260
Larry Hastings9cf065c2012-06-22 16:30:09 -07008261 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08008262 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008263 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8264 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8265 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8266 }
8267
8268 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08008269 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07008270 Py_END_ALLOW_THREADS
8271
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008272 if (windows_has_symlink_unprivileged_flag && !result &&
8273 ERROR_INVALID_PARAMETER == GetLastError()) {
8274
8275 Py_BEGIN_ALLOW_THREADS
8276 _Py_BEGIN_SUPPRESS_IPH
8277 /* This error might be caused by
8278 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8279 Try again, and update windows_has_symlink_unprivileged_flag if we
8280 are successful this time.
8281
8282 NOTE: There is a risk of a race condition here if there are other
8283 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8284 another process (or thread) changes that condition in between our
8285 calls to CreateSymbolicLink.
8286 */
8287 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8288 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8289 _Py_END_SUPPRESS_IPH
8290 Py_END_ALLOW_THREADS
8291
8292 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8293 windows_has_symlink_unprivileged_flag = FALSE;
8294 }
8295 }
8296
Larry Hastings2f936352014-08-05 14:04:04 +10008297 if (!result)
8298 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008299
8300#else
8301
Steve Dower6921e732018-03-05 14:26:08 -08008302 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8303 PyErr_SetString(PyExc_ValueError,
8304 "symlink: src and dst must be the same type");
8305 return NULL;
8306 }
8307
Larry Hastings9cf065c2012-06-22 16:30:09 -07008308 Py_BEGIN_ALLOW_THREADS
8309#if HAVE_SYMLINKAT
8310 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008311 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008312 else
8313#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008314 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008315 Py_END_ALLOW_THREADS
8316
Larry Hastings2f936352014-08-05 14:04:04 +10008317 if (result)
8318 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008319#endif
8320
Larry Hastings2f936352014-08-05 14:04:04 +10008321 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008322}
8323#endif /* HAVE_SYMLINK */
8324
Larry Hastings9cf065c2012-06-22 16:30:09 -07008325
Brian Curtind40e6f72010-07-08 21:39:08 +00008326
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008327
Larry Hastings605a62d2012-06-24 04:33:36 -07008328static PyStructSequence_Field times_result_fields[] = {
8329 {"user", "user time"},
8330 {"system", "system time"},
8331 {"children_user", "user time of children"},
8332 {"children_system", "system time of children"},
8333 {"elapsed", "elapsed time since an arbitrary point in the past"},
8334 {NULL}
8335};
8336
8337PyDoc_STRVAR(times_result__doc__,
8338"times_result: Result from os.times().\n\n\
8339This object may be accessed either as a tuple of\n\
8340 (user, system, children_user, children_system, elapsed),\n\
8341or via the attributes user, system, children_user, children_system,\n\
8342and elapsed.\n\
8343\n\
8344See os.times for more information.");
8345
8346static PyStructSequence_Desc times_result_desc = {
8347 "times_result", /* name */
8348 times_result__doc__, /* doc */
8349 times_result_fields,
8350 5
8351};
8352
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008353#ifdef MS_WINDOWS
8354#define HAVE_TIMES /* mandatory, for the method table */
8355#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008356
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008357#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008358
8359static PyObject *
8360build_times_result(double user, double system,
8361 double children_user, double children_system,
8362 double elapsed)
8363{
Eddie Elizondob3966632019-11-05 07:16:14 -08008364 PyObject *TimesResultType = _posixstate_global->TimesResultType;
8365 PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008366 if (value == NULL)
8367 return NULL;
8368
8369#define SET(i, field) \
8370 { \
8371 PyObject *o = PyFloat_FromDouble(field); \
8372 if (!o) { \
8373 Py_DECREF(value); \
8374 return NULL; \
8375 } \
8376 PyStructSequence_SET_ITEM(value, i, o); \
8377 } \
8378
8379 SET(0, user);
8380 SET(1, system);
8381 SET(2, children_user);
8382 SET(3, children_system);
8383 SET(4, elapsed);
8384
8385#undef SET
8386
8387 return value;
8388}
8389
Larry Hastings605a62d2012-06-24 04:33:36 -07008390
Larry Hastings2f936352014-08-05 14:04:04 +10008391#ifndef MS_WINDOWS
8392#define NEED_TICKS_PER_SECOND
8393static long ticks_per_second = -1;
8394#endif /* MS_WINDOWS */
8395
8396/*[clinic input]
8397os.times
8398
8399Return a collection containing process timing information.
8400
8401The object returned behaves like a named tuple with these fields:
8402 (utime, stime, cutime, cstime, elapsed_time)
8403All fields are floating point numbers.
8404[clinic start generated code]*/
8405
Larry Hastings2f936352014-08-05 14:04:04 +10008406static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008407os_times_impl(PyObject *module)
8408/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008409#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008410{
Victor Stinner8c62be82010-05-06 00:08:46 +00008411 FILETIME create, exit, kernel, user;
8412 HANDLE hProc;
8413 hProc = GetCurrentProcess();
8414 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8415 /* The fields of a FILETIME structure are the hi and lo part
8416 of a 64-bit value expressed in 100 nanosecond units.
8417 1e7 is one second in such units; 1e-7 the inverse.
8418 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8419 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008420 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008421 (double)(user.dwHighDateTime*429.4967296 +
8422 user.dwLowDateTime*1e-7),
8423 (double)(kernel.dwHighDateTime*429.4967296 +
8424 kernel.dwLowDateTime*1e-7),
8425 (double)0,
8426 (double)0,
8427 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008428}
Larry Hastings2f936352014-08-05 14:04:04 +10008429#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008430{
Larry Hastings2f936352014-08-05 14:04:04 +10008431
8432
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008433 struct tms t;
8434 clock_t c;
8435 errno = 0;
8436 c = times(&t);
8437 if (c == (clock_t) -1)
8438 return posix_error();
8439 return build_times_result(
8440 (double)t.tms_utime / ticks_per_second,
8441 (double)t.tms_stime / ticks_per_second,
8442 (double)t.tms_cutime / ticks_per_second,
8443 (double)t.tms_cstime / ticks_per_second,
8444 (double)c / ticks_per_second);
8445}
Larry Hastings2f936352014-08-05 14:04:04 +10008446#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008447#endif /* HAVE_TIMES */
8448
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008449
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008450#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008451/*[clinic input]
8452os.getsid
8453
8454 pid: pid_t
8455 /
8456
8457Call the system call getsid(pid) and return the result.
8458[clinic start generated code]*/
8459
Larry Hastings2f936352014-08-05 14:04:04 +10008460static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008461os_getsid_impl(PyObject *module, pid_t pid)
8462/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008463{
Victor Stinner8c62be82010-05-06 00:08:46 +00008464 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 sid = getsid(pid);
8466 if (sid < 0)
8467 return posix_error();
8468 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008469}
8470#endif /* HAVE_GETSID */
8471
8472
Guido van Rossumb6775db1994-08-01 11:34:53 +00008473#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008474/*[clinic input]
8475os.setsid
8476
8477Call the system call setsid().
8478[clinic start generated code]*/
8479
Larry Hastings2f936352014-08-05 14:04:04 +10008480static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008481os_setsid_impl(PyObject *module)
8482/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008483{
Victor Stinner8c62be82010-05-06 00:08:46 +00008484 if (setsid() < 0)
8485 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008486 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008487}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008488#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008489
Larry Hastings2f936352014-08-05 14:04:04 +10008490
Guido van Rossumb6775db1994-08-01 11:34:53 +00008491#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008492/*[clinic input]
8493os.setpgid
8494
8495 pid: pid_t
8496 pgrp: pid_t
8497 /
8498
8499Call the system call setpgid(pid, pgrp).
8500[clinic start generated code]*/
8501
Larry Hastings2f936352014-08-05 14:04:04 +10008502static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008503os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8504/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008505{
Victor Stinner8c62be82010-05-06 00:08:46 +00008506 if (setpgid(pid, pgrp) < 0)
8507 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008508 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008509}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008510#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008511
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008512
Guido van Rossumb6775db1994-08-01 11:34:53 +00008513#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008514/*[clinic input]
8515os.tcgetpgrp
8516
8517 fd: int
8518 /
8519
8520Return the process group associated with the terminal specified by fd.
8521[clinic start generated code]*/
8522
Larry Hastings2f936352014-08-05 14:04:04 +10008523static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008524os_tcgetpgrp_impl(PyObject *module, int fd)
8525/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008526{
8527 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008528 if (pgid < 0)
8529 return posix_error();
8530 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008531}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008532#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008533
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008534
Guido van Rossumb6775db1994-08-01 11:34:53 +00008535#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008536/*[clinic input]
8537os.tcsetpgrp
8538
8539 fd: int
8540 pgid: pid_t
8541 /
8542
8543Set the process group associated with the terminal specified by fd.
8544[clinic start generated code]*/
8545
Larry Hastings2f936352014-08-05 14:04:04 +10008546static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008547os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8548/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008549{
Victor Stinner8c62be82010-05-06 00:08:46 +00008550 if (tcsetpgrp(fd, pgid) < 0)
8551 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008552 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008553}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008554#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008555
Guido van Rossum687dd131993-05-17 08:34:16 +00008556/* Functions acting on file descriptors */
8557
Victor Stinnerdaf45552013-08-28 00:53:59 +02008558#ifdef O_CLOEXEC
8559extern int _Py_open_cloexec_works;
8560#endif
8561
Larry Hastings2f936352014-08-05 14:04:04 +10008562
8563/*[clinic input]
8564os.open -> int
8565 path: path_t
8566 flags: int
8567 mode: int = 0o777
8568 *
8569 dir_fd: dir_fd(requires='openat') = None
8570
8571# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8572
8573Open a file for low level IO. Returns a file descriptor (integer).
8574
8575If dir_fd is not None, it should be a file descriptor open to a directory,
8576 and path should be relative; path will then be relative to that directory.
8577dir_fd may not be implemented on your platform.
8578 If it is unavailable, using it will raise a NotImplementedError.
8579[clinic start generated code]*/
8580
Larry Hastings2f936352014-08-05 14:04:04 +10008581static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008582os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8583/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008584{
8585 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008586 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008587
Victor Stinnerdaf45552013-08-28 00:53:59 +02008588#ifdef O_CLOEXEC
8589 int *atomic_flag_works = &_Py_open_cloexec_works;
8590#elif !defined(MS_WINDOWS)
8591 int *atomic_flag_works = NULL;
8592#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008593
Victor Stinnerdaf45552013-08-28 00:53:59 +02008594#ifdef MS_WINDOWS
8595 flags |= O_NOINHERIT;
8596#elif defined(O_CLOEXEC)
8597 flags |= O_CLOEXEC;
8598#endif
8599
Steve Dowerb82e17e2019-05-23 08:45:22 -07008600 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8601 return -1;
8602 }
8603
Steve Dower8fc89802015-04-12 00:26:27 -04008604 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008605 do {
8606 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008607#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008608 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008609#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008610#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008611 if (dir_fd != DEFAULT_DIR_FD)
8612 fd = openat(dir_fd, path->narrow, flags, mode);
8613 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008614#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008615 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008616#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008617 Py_END_ALLOW_THREADS
8618 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008619 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008620
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008621 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008622 if (!async_err)
8623 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008624 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008625 }
8626
Victor Stinnerdaf45552013-08-28 00:53:59 +02008627#ifndef MS_WINDOWS
8628 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8629 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008630 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008631 }
8632#endif
8633
Larry Hastings2f936352014-08-05 14:04:04 +10008634 return fd;
8635}
8636
8637
8638/*[clinic input]
8639os.close
8640
8641 fd: int
8642
8643Close a file descriptor.
8644[clinic start generated code]*/
8645
Barry Warsaw53699e91996-12-10 23:23:01 +00008646static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008647os_close_impl(PyObject *module, int fd)
8648/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008649{
Larry Hastings2f936352014-08-05 14:04:04 +10008650 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008651 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8652 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8653 * for more details.
8654 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008655 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008656 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008657 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008658 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008659 Py_END_ALLOW_THREADS
8660 if (res < 0)
8661 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008662 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008663}
8664
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008665
Jakub Kulíke20134f2019-09-11 17:11:57 +02008666#ifdef HAVE_FDWALK
8667static int
8668_fdwalk_close_func(void *lohi, int fd)
8669{
8670 int lo = ((int *)lohi)[0];
8671 int hi = ((int *)lohi)[1];
8672
8673 if (fd >= hi)
8674 return 1;
8675 else if (fd >= lo)
8676 close(fd);
8677 return 0;
8678}
8679#endif /* HAVE_FDWALK */
8680
Larry Hastings2f936352014-08-05 14:04:04 +10008681/*[clinic input]
8682os.closerange
8683
8684 fd_low: int
8685 fd_high: int
8686 /
8687
8688Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8689[clinic start generated code]*/
8690
Larry Hastings2f936352014-08-05 14:04:04 +10008691static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008692os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8693/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008694{
Jakub Kulíke20134f2019-09-11 17:11:57 +02008695#ifdef HAVE_FDWALK
8696 int lohi[2];
8697#else
Larry Hastings2f936352014-08-05 14:04:04 +10008698 int i;
Jakub Kulíke20134f2019-09-11 17:11:57 +02008699#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008700 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008701 _Py_BEGIN_SUPPRESS_IPH
Jakub Kulíke20134f2019-09-11 17:11:57 +02008702#ifdef HAVE_FDWALK
8703 lohi[0] = Py_MAX(fd_low, 0);
8704 lohi[1] = fd_high;
8705 fdwalk(_fdwalk_close_func, lohi);
8706#else
Benjamin Peterson207116b2016-09-08 11:28:06 -07008707 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008708 close(i);
Jakub Kulíke20134f2019-09-11 17:11:57 +02008709#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008710 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008711 Py_END_ALLOW_THREADS
8712 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008713}
8714
8715
Larry Hastings2f936352014-08-05 14:04:04 +10008716/*[clinic input]
8717os.dup -> int
8718
8719 fd: int
8720 /
8721
8722Return a duplicate of a file descriptor.
8723[clinic start generated code]*/
8724
Larry Hastings2f936352014-08-05 14:04:04 +10008725static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008726os_dup_impl(PyObject *module, int fd)
8727/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008728{
8729 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008730}
8731
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008732
Larry Hastings2f936352014-08-05 14:04:04 +10008733/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008734os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008735 fd: int
8736 fd2: int
8737 inheritable: bool=True
8738
8739Duplicate file descriptor.
8740[clinic start generated code]*/
8741
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008742static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008743os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008744/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008745{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008746 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008747#if defined(HAVE_DUP3) && \
8748 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8749 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008750 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008751#endif
8752
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008753 if (fd < 0 || fd2 < 0) {
8754 posix_error();
8755 return -1;
8756 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008757
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008758 /* dup2() can fail with EINTR if the target FD is already open, because it
8759 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8760 * upon close(), and therefore below.
8761 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008762#ifdef MS_WINDOWS
8763 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008764 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008765 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008766 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008767 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008768 if (res < 0) {
8769 posix_error();
8770 return -1;
8771 }
8772 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008773
8774 /* Character files like console cannot be make non-inheritable */
8775 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8776 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008777 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008778 }
8779
8780#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8781 Py_BEGIN_ALLOW_THREADS
8782 if (!inheritable)
8783 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8784 else
8785 res = dup2(fd, fd2);
8786 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008787 if (res < 0) {
8788 posix_error();
8789 return -1;
8790 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008791
8792#else
8793
8794#ifdef HAVE_DUP3
8795 if (!inheritable && dup3_works != 0) {
8796 Py_BEGIN_ALLOW_THREADS
8797 res = dup3(fd, fd2, O_CLOEXEC);
8798 Py_END_ALLOW_THREADS
8799 if (res < 0) {
8800 if (dup3_works == -1)
8801 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008802 if (dup3_works) {
8803 posix_error();
8804 return -1;
8805 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008806 }
8807 }
8808
8809 if (inheritable || dup3_works == 0)
8810 {
8811#endif
8812 Py_BEGIN_ALLOW_THREADS
8813 res = dup2(fd, fd2);
8814 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008815 if (res < 0) {
8816 posix_error();
8817 return -1;
8818 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008819
8820 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8821 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008822 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008823 }
8824#ifdef HAVE_DUP3
8825 }
8826#endif
8827
8828#endif
8829
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008830 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008831}
8832
Larry Hastings2f936352014-08-05 14:04:04 +10008833
Ross Lagerwall7807c352011-03-17 20:20:30 +02008834#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008835/*[clinic input]
8836os.lockf
8837
8838 fd: int
8839 An open file descriptor.
8840 command: int
8841 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8842 length: Py_off_t
8843 The number of bytes to lock, starting at the current position.
8844 /
8845
8846Apply, test or remove a POSIX lock on an open file descriptor.
8847
8848[clinic start generated code]*/
8849
Larry Hastings2f936352014-08-05 14:04:04 +10008850static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008851os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8852/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008853{
8854 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008855
Saiyang Gou7514f4f2020-02-12 23:47:42 -08008856 if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) {
8857 return NULL;
8858 }
8859
Ross Lagerwall7807c352011-03-17 20:20:30 +02008860 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008861 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008862 Py_END_ALLOW_THREADS
8863
8864 if (res < 0)
8865 return posix_error();
8866
8867 Py_RETURN_NONE;
8868}
Larry Hastings2f936352014-08-05 14:04:04 +10008869#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008870
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008871
Larry Hastings2f936352014-08-05 14:04:04 +10008872/*[clinic input]
8873os.lseek -> Py_off_t
8874
8875 fd: int
8876 position: Py_off_t
8877 how: int
8878 /
8879
8880Set the position of a file descriptor. Return the new position.
8881
8882Return the new cursor position in number of bytes
8883relative to the beginning of the file.
8884[clinic start generated code]*/
8885
Larry Hastings2f936352014-08-05 14:04:04 +10008886static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008887os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8888/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008889{
8890 Py_off_t result;
8891
Guido van Rossum687dd131993-05-17 08:34:16 +00008892#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008893 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8894 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008895 case 0: how = SEEK_SET; break;
8896 case 1: how = SEEK_CUR; break;
8897 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008898 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008899#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008900
Victor Stinner8c62be82010-05-06 00:08:46 +00008901 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008902 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008903#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008904 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008905#else
Larry Hastings2f936352014-08-05 14:04:04 +10008906 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008907#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008908 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008909 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008910 if (result < 0)
8911 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008912
Larry Hastings2f936352014-08-05 14:04:04 +10008913 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008914}
8915
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008916
Larry Hastings2f936352014-08-05 14:04:04 +10008917/*[clinic input]
8918os.read
8919 fd: int
8920 length: Py_ssize_t
8921 /
8922
8923Read from a file descriptor. Returns a bytes object.
8924[clinic start generated code]*/
8925
Larry Hastings2f936352014-08-05 14:04:04 +10008926static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008927os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8928/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008929{
Victor Stinner8c62be82010-05-06 00:08:46 +00008930 Py_ssize_t n;
8931 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008932
8933 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008934 errno = EINVAL;
8935 return posix_error();
8936 }
Larry Hastings2f936352014-08-05 14:04:04 +10008937
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008938 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008939
8940 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008941 if (buffer == NULL)
8942 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008943
Victor Stinner66aab0c2015-03-19 22:53:20 +01008944 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8945 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008946 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008947 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008948 }
Larry Hastings2f936352014-08-05 14:04:04 +10008949
8950 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008951 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008952
Victor Stinner8c62be82010-05-06 00:08:46 +00008953 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008954}
8955
Ross Lagerwall7807c352011-03-17 20:20:30 +02008956#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008957 || defined(__APPLE__))) \
8958 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8959 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8960static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008961iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008962{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008963 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008964
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008965 *iov = PyMem_New(struct iovec, cnt);
8966 if (*iov == NULL) {
8967 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008968 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008969 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008970
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008971 *buf = PyMem_New(Py_buffer, cnt);
8972 if (*buf == NULL) {
8973 PyMem_Del(*iov);
8974 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008975 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008976 }
8977
8978 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008979 PyObject *item = PySequence_GetItem(seq, i);
8980 if (item == NULL)
8981 goto fail;
8982 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8983 Py_DECREF(item);
8984 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008985 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008986 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008987 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008988 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008989 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008990 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008991
8992fail:
8993 PyMem_Del(*iov);
8994 for (j = 0; j < i; j++) {
8995 PyBuffer_Release(&(*buf)[j]);
8996 }
8997 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008998 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008999}
9000
9001static void
9002iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
9003{
9004 int i;
9005 PyMem_Del(iov);
9006 for (i = 0; i < cnt; i++) {
9007 PyBuffer_Release(&buf[i]);
9008 }
9009 PyMem_Del(buf);
9010}
9011#endif
9012
Larry Hastings2f936352014-08-05 14:04:04 +10009013
Ross Lagerwall7807c352011-03-17 20:20:30 +02009014#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10009015/*[clinic input]
9016os.readv -> Py_ssize_t
9017
9018 fd: int
9019 buffers: object
9020 /
9021
9022Read from a file descriptor fd into an iterable of buffers.
9023
9024The buffers should be mutable buffers accepting bytes.
9025readv will transfer data into each buffer until it is full
9026and then move on to the next buffer in the sequence to hold
9027the rest of the data.
9028
9029readv returns the total number of bytes read,
9030which may be less than the total capacity of all the buffers.
9031[clinic start generated code]*/
9032
Larry Hastings2f936352014-08-05 14:04:04 +10009033static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009034os_readv_impl(PyObject *module, int fd, PyObject *buffers)
9035/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009036{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009037 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009038 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009039 struct iovec *iov;
9040 Py_buffer *buf;
9041
Larry Hastings2f936352014-08-05 14:04:04 +10009042 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009043 PyErr_SetString(PyExc_TypeError,
9044 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009045 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009046 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02009047
Larry Hastings2f936352014-08-05 14:04:04 +10009048 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009049 if (cnt < 0)
9050 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10009051
9052 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
9053 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009054
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009055 do {
9056 Py_BEGIN_ALLOW_THREADS
9057 n = readv(fd, iov, cnt);
9058 Py_END_ALLOW_THREADS
9059 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009060
9061 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10009062 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009063 if (!async_err)
9064 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10009065 return -1;
9066 }
Victor Stinner57ddf782014-01-08 15:21:28 +01009067
Larry Hastings2f936352014-08-05 14:04:04 +10009068 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009069}
Larry Hastings2f936352014-08-05 14:04:04 +10009070#endif /* HAVE_READV */
9071
Ross Lagerwall7807c352011-03-17 20:20:30 +02009072
9073#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10009074/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10009075os.pread
9076
9077 fd: int
Dong-hee Naad7736f2019-09-25 14:47:04 +09009078 length: Py_ssize_t
Larry Hastings2f936352014-08-05 14:04:04 +10009079 offset: Py_off_t
9080 /
9081
9082Read a number of bytes from a file descriptor starting at a particular offset.
9083
9084Read length bytes from file descriptor fd, starting at offset bytes from
9085the beginning of the file. The file offset remains unchanged.
9086[clinic start generated code]*/
9087
Larry Hastings2f936352014-08-05 14:04:04 +10009088static PyObject *
Dong-hee Naad7736f2019-09-25 14:47:04 +09009089os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
9090/*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009091{
Ross Lagerwall7807c352011-03-17 20:20:30 +02009092 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009093 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009094 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009095
Larry Hastings2f936352014-08-05 14:04:04 +10009096 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009097 errno = EINVAL;
9098 return posix_error();
9099 }
Larry Hastings2f936352014-08-05 14:04:04 +10009100 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009101 if (buffer == NULL)
9102 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009103
9104 do {
9105 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009106 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009107 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009108 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009109 Py_END_ALLOW_THREADS
9110 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9111
Ross Lagerwall7807c352011-03-17 20:20:30 +02009112 if (n < 0) {
9113 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009114 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009115 }
Larry Hastings2f936352014-08-05 14:04:04 +10009116 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02009117 _PyBytes_Resize(&buffer, n);
9118 return buffer;
9119}
Larry Hastings2f936352014-08-05 14:04:04 +10009120#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009121
Pablo Galindo4defba32018-01-27 16:16:37 +00009122#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
9123/*[clinic input]
9124os.preadv -> Py_ssize_t
9125
9126 fd: int
9127 buffers: object
9128 offset: Py_off_t
9129 flags: int = 0
9130 /
9131
9132Reads from a file descriptor into a number of mutable bytes-like objects.
9133
9134Combines the functionality of readv() and pread(). As readv(), it will
9135transfer data into each buffer until it is full and then move on to the next
9136buffer in the sequence to hold the rest of the data. Its fourth argument,
9137specifies the file offset at which the input operation is to be performed. It
9138will return the total number of bytes read (which can be less than the total
9139capacity of all the objects).
9140
9141The flags argument contains a bitwise OR of zero or more of the following flags:
9142
9143- RWF_HIPRI
9144- RWF_NOWAIT
9145
9146Using non-zero flags requires Linux 4.6 or newer.
9147[clinic start generated code]*/
9148
9149static Py_ssize_t
9150os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9151 int flags)
9152/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
9153{
9154 Py_ssize_t cnt, n;
9155 int async_err = 0;
9156 struct iovec *iov;
9157 Py_buffer *buf;
9158
9159 if (!PySequence_Check(buffers)) {
9160 PyErr_SetString(PyExc_TypeError,
9161 "preadv2() arg 2 must be a sequence");
9162 return -1;
9163 }
9164
9165 cnt = PySequence_Size(buffers);
9166 if (cnt < 0) {
9167 return -1;
9168 }
9169
9170#ifndef HAVE_PREADV2
9171 if(flags != 0) {
9172 argument_unavailable_error("preadv2", "flags");
9173 return -1;
9174 }
9175#endif
9176
9177 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
9178 return -1;
9179 }
9180#ifdef HAVE_PREADV2
9181 do {
9182 Py_BEGIN_ALLOW_THREADS
9183 _Py_BEGIN_SUPPRESS_IPH
9184 n = preadv2(fd, iov, cnt, offset, flags);
9185 _Py_END_SUPPRESS_IPH
9186 Py_END_ALLOW_THREADS
9187 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9188#else
9189 do {
9190 Py_BEGIN_ALLOW_THREADS
9191 _Py_BEGIN_SUPPRESS_IPH
9192 n = preadv(fd, iov, cnt, offset);
9193 _Py_END_SUPPRESS_IPH
9194 Py_END_ALLOW_THREADS
9195 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9196#endif
9197
9198 iov_cleanup(iov, buf, cnt);
9199 if (n < 0) {
9200 if (!async_err) {
9201 posix_error();
9202 }
9203 return -1;
9204 }
9205
9206 return n;
9207}
9208#endif /* HAVE_PREADV */
9209
Larry Hastings2f936352014-08-05 14:04:04 +10009210
9211/*[clinic input]
9212os.write -> Py_ssize_t
9213
9214 fd: int
9215 data: Py_buffer
9216 /
9217
9218Write a bytes object to a file descriptor.
9219[clinic start generated code]*/
9220
Larry Hastings2f936352014-08-05 14:04:04 +10009221static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009222os_write_impl(PyObject *module, int fd, Py_buffer *data)
9223/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009224{
Victor Stinner66aab0c2015-03-19 22:53:20 +01009225 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009226}
9227
9228#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009229PyDoc_STRVAR(posix_sendfile__doc__,
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009230"sendfile(out_fd, in_fd, offset, count) -> byteswritten\n\
9231sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009232 -> byteswritten\n\
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009233Copy count bytes from file descriptor in_fd to file descriptor out_fd.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009234
Larry Hastings2f936352014-08-05 14:04:04 +10009235/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009236static PyObject *
9237posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
9238{
9239 int in, out;
9240 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009241 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009242 off_t offset;
9243
9244#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9245#ifndef __APPLE__
9246 Py_ssize_t len;
9247#endif
9248 PyObject *headers = NULL, *trailers = NULL;
9249 Py_buffer *hbuf, *tbuf;
9250 off_t sbytes;
9251 struct sf_hdtr sf;
9252 int flags = 0;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009253 static char *keywords[] = {"out_fd", "in_fd",
Benjamin Petersond8a43b42011-02-26 21:35:16 +00009254 "offset", "count",
9255 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009256
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02009257 sf.headers = NULL;
9258 sf.trailers = NULL;
9259
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009260#ifdef __APPLE__
9261 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009262 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009263#else
9264 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009265 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009266#endif
9267 &headers, &trailers, &flags))
9268 return NULL;
9269 if (headers != NULL) {
9270 if (!PySequence_Check(headers)) {
9271 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009272 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009273 return NULL;
9274 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009275 Py_ssize_t i = PySequence_Size(headers);
9276 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009277 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009278 if (i > INT_MAX) {
9279 PyErr_SetString(PyExc_OverflowError,
9280 "sendfile() header is too large");
9281 return NULL;
9282 }
9283 if (i > 0) {
9284 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009285 if (iov_setup(&(sf.headers), &hbuf,
9286 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009287 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009288#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009289 for (i = 0; i < sf.hdr_cnt; i++) {
9290 Py_ssize_t blen = sf.headers[i].iov_len;
9291# define OFF_T_MAX 0x7fffffffffffffff
9292 if (sbytes >= OFF_T_MAX - blen) {
9293 PyErr_SetString(PyExc_OverflowError,
9294 "sendfile() header is too large");
9295 return NULL;
9296 }
9297 sbytes += blen;
9298 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009299#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009300 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009301 }
9302 }
9303 if (trailers != NULL) {
9304 if (!PySequence_Check(trailers)) {
9305 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009306 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009307 return NULL;
9308 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009309 Py_ssize_t i = PySequence_Size(trailers);
9310 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009311 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009312 if (i > INT_MAX) {
9313 PyErr_SetString(PyExc_OverflowError,
9314 "sendfile() trailer is too large");
9315 return NULL;
9316 }
9317 if (i > 0) {
9318 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009319 if (iov_setup(&(sf.trailers), &tbuf,
9320 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009321 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009322 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009323 }
9324 }
9325
Steve Dower8fc89802015-04-12 00:26:27 -04009326 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009327 do {
9328 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009329#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009330 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009331#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009332 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009333#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009334 Py_END_ALLOW_THREADS
9335 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009336 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009337
9338 if (sf.headers != NULL)
9339 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9340 if (sf.trailers != NULL)
9341 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9342
9343 if (ret < 0) {
9344 if ((errno == EAGAIN) || (errno == EBUSY)) {
9345 if (sbytes != 0) {
9346 // some data has been sent
9347 goto done;
9348 }
9349 else {
9350 // no data has been sent; upper application is supposed
9351 // to retry on EAGAIN or EBUSY
9352 return posix_error();
9353 }
9354 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009355 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009356 }
9357 goto done;
9358
9359done:
9360 #if !defined(HAVE_LARGEFILE_SUPPORT)
9361 return Py_BuildValue("l", sbytes);
9362 #else
9363 return Py_BuildValue("L", sbytes);
9364 #endif
9365
9366#else
9367 Py_ssize_t count;
9368 PyObject *offobj;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009369 static char *keywords[] = {"out_fd", "in_fd",
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009370 "offset", "count", NULL};
9371 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9372 keywords, &out, &in, &offobj, &count))
9373 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009374#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009375 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009376 do {
9377 Py_BEGIN_ALLOW_THREADS
9378 ret = sendfile(out, in, NULL, count);
9379 Py_END_ALLOW_THREADS
9380 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009381 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009382 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009383 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009384 }
9385#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009386 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009387 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009388
9389 do {
9390 Py_BEGIN_ALLOW_THREADS
9391 ret = sendfile(out, in, &offset, count);
9392 Py_END_ALLOW_THREADS
9393 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009394 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009395 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009396 return Py_BuildValue("n", ret);
9397#endif
9398}
Larry Hastings2f936352014-08-05 14:04:04 +10009399#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009400
Larry Hastings2f936352014-08-05 14:04:04 +10009401
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009402#if defined(__APPLE__)
9403/*[clinic input]
9404os._fcopyfile
9405
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009406 in_fd: int
9407 out_fd: int
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009408 flags: int
9409 /
9410
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009411Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009412[clinic start generated code]*/
9413
9414static PyObject *
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009415os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
9416/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009417{
9418 int ret;
9419
9420 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009421 ret = fcopyfile(in_fd, out_fd, NULL, flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009422 Py_END_ALLOW_THREADS
9423 if (ret < 0)
9424 return posix_error();
9425 Py_RETURN_NONE;
9426}
9427#endif
9428
9429
Larry Hastings2f936352014-08-05 14:04:04 +10009430/*[clinic input]
9431os.fstat
9432
9433 fd : int
9434
9435Perform a stat system call on the given file descriptor.
9436
9437Like stat(), but for an open file descriptor.
9438Equivalent to os.stat(fd).
9439[clinic start generated code]*/
9440
Larry Hastings2f936352014-08-05 14:04:04 +10009441static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009442os_fstat_impl(PyObject *module, int fd)
9443/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009444{
Victor Stinner8c62be82010-05-06 00:08:46 +00009445 STRUCT_STAT st;
9446 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009447 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009448
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009449 do {
9450 Py_BEGIN_ALLOW_THREADS
9451 res = FSTAT(fd, &st);
9452 Py_END_ALLOW_THREADS
9453 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009454 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009455#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009456 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009457#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009458 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009459#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009460 }
Tim Peters5aa91602002-01-30 05:46:57 +00009461
Victor Stinner4195b5c2012-02-08 23:03:19 +01009462 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009463}
9464
Larry Hastings2f936352014-08-05 14:04:04 +10009465
9466/*[clinic input]
9467os.isatty -> bool
9468 fd: int
9469 /
9470
9471Return True if the fd is connected to a terminal.
9472
9473Return True if the file descriptor is an open file descriptor
9474connected to the slave end of a terminal.
9475[clinic start generated code]*/
9476
Larry Hastings2f936352014-08-05 14:04:04 +10009477static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009478os_isatty_impl(PyObject *module, int fd)
9479/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009480{
Steve Dower8fc89802015-04-12 00:26:27 -04009481 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009482 _Py_BEGIN_SUPPRESS_IPH
9483 return_value = isatty(fd);
9484 _Py_END_SUPPRESS_IPH
9485 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009486}
9487
9488
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009489#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009490/*[clinic input]
9491os.pipe
9492
9493Create a pipe.
9494
9495Returns a tuple of two file descriptors:
9496 (read_fd, write_fd)
9497[clinic start generated code]*/
9498
Larry Hastings2f936352014-08-05 14:04:04 +10009499static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009500os_pipe_impl(PyObject *module)
9501/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009502{
Victor Stinner8c62be82010-05-06 00:08:46 +00009503 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009504#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009505 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009506 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009507 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009508#else
9509 int res;
9510#endif
9511
9512#ifdef MS_WINDOWS
9513 attr.nLength = sizeof(attr);
9514 attr.lpSecurityDescriptor = NULL;
9515 attr.bInheritHandle = FALSE;
9516
9517 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009518 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009519 ok = CreatePipe(&read, &write, &attr, 0);
9520 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009521 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9522 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009523 if (fds[0] == -1 || fds[1] == -1) {
9524 CloseHandle(read);
9525 CloseHandle(write);
9526 ok = 0;
9527 }
9528 }
Steve Dowerc3630612016-11-19 18:41:16 -08009529 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009530 Py_END_ALLOW_THREADS
9531
Victor Stinner8c62be82010-05-06 00:08:46 +00009532 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009533 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009534#else
9535
9536#ifdef HAVE_PIPE2
9537 Py_BEGIN_ALLOW_THREADS
9538 res = pipe2(fds, O_CLOEXEC);
9539 Py_END_ALLOW_THREADS
9540
9541 if (res != 0 && errno == ENOSYS)
9542 {
9543#endif
9544 Py_BEGIN_ALLOW_THREADS
9545 res = pipe(fds);
9546 Py_END_ALLOW_THREADS
9547
9548 if (res == 0) {
9549 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9550 close(fds[0]);
9551 close(fds[1]);
9552 return NULL;
9553 }
9554 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9555 close(fds[0]);
9556 close(fds[1]);
9557 return NULL;
9558 }
9559 }
9560#ifdef HAVE_PIPE2
9561 }
9562#endif
9563
9564 if (res != 0)
9565 return PyErr_SetFromErrno(PyExc_OSError);
9566#endif /* !MS_WINDOWS */
9567 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009568}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009569#endif /* HAVE_PIPE */
9570
Larry Hastings2f936352014-08-05 14:04:04 +10009571
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009572#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009573/*[clinic input]
9574os.pipe2
9575
9576 flags: int
9577 /
9578
9579Create a pipe with flags set atomically.
9580
9581Returns a tuple of two file descriptors:
9582 (read_fd, write_fd)
9583
9584flags can be constructed by ORing together one or more of these values:
9585O_NONBLOCK, O_CLOEXEC.
9586[clinic start generated code]*/
9587
Larry Hastings2f936352014-08-05 14:04:04 +10009588static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009589os_pipe2_impl(PyObject *module, int flags)
9590/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009591{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009592 int fds[2];
9593 int res;
9594
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009595 res = pipe2(fds, flags);
9596 if (res != 0)
9597 return posix_error();
9598 return Py_BuildValue("(ii)", fds[0], fds[1]);
9599}
9600#endif /* HAVE_PIPE2 */
9601
Larry Hastings2f936352014-08-05 14:04:04 +10009602
Ross Lagerwall7807c352011-03-17 20:20:30 +02009603#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009604/*[clinic input]
9605os.writev -> Py_ssize_t
9606 fd: int
9607 buffers: object
9608 /
9609
9610Iterate over buffers, and write the contents of each to a file descriptor.
9611
9612Returns the total number of bytes written.
9613buffers must be a sequence of bytes-like objects.
9614[clinic start generated code]*/
9615
Larry Hastings2f936352014-08-05 14:04:04 +10009616static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009617os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9618/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009619{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009620 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009621 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009622 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009623 struct iovec *iov;
9624 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009625
9626 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009627 PyErr_SetString(PyExc_TypeError,
9628 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009629 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009630 }
Larry Hastings2f936352014-08-05 14:04:04 +10009631 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009632 if (cnt < 0)
9633 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009634
Larry Hastings2f936352014-08-05 14:04:04 +10009635 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9636 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009637 }
9638
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009639 do {
9640 Py_BEGIN_ALLOW_THREADS
9641 result = writev(fd, iov, cnt);
9642 Py_END_ALLOW_THREADS
9643 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009644
9645 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009646 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009647 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009648
Georg Brandl306336b2012-06-24 12:55:33 +02009649 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009650}
Larry Hastings2f936352014-08-05 14:04:04 +10009651#endif /* HAVE_WRITEV */
9652
9653
9654#ifdef HAVE_PWRITE
9655/*[clinic input]
9656os.pwrite -> Py_ssize_t
9657
9658 fd: int
9659 buffer: Py_buffer
9660 offset: Py_off_t
9661 /
9662
9663Write bytes to a file descriptor starting at a particular offset.
9664
9665Write buffer to fd, starting at offset bytes from the beginning of
9666the file. Returns the number of bytes writte. Does not change the
9667current file offset.
9668[clinic start generated code]*/
9669
Larry Hastings2f936352014-08-05 14:04:04 +10009670static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009671os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9672/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009673{
9674 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009675 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009676
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009677 do {
9678 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009679 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009680 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009681 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009682 Py_END_ALLOW_THREADS
9683 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009684
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009685 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009686 posix_error();
9687 return size;
9688}
9689#endif /* HAVE_PWRITE */
9690
Pablo Galindo4defba32018-01-27 16:16:37 +00009691#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9692/*[clinic input]
9693os.pwritev -> Py_ssize_t
9694
9695 fd: int
9696 buffers: object
9697 offset: Py_off_t
9698 flags: int = 0
9699 /
9700
9701Writes the contents of bytes-like objects to a file descriptor at a given offset.
9702
9703Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9704of bytes-like objects. Buffers are processed in array order. Entire contents of first
9705buffer is written before proceeding to second, and so on. The operating system may
9706set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9707This function writes the contents of each object to the file descriptor and returns
9708the total number of bytes written.
9709
9710The flags argument contains a bitwise OR of zero or more of the following flags:
9711
9712- RWF_DSYNC
9713- RWF_SYNC
9714
9715Using non-zero flags requires Linux 4.7 or newer.
9716[clinic start generated code]*/
9717
9718static Py_ssize_t
9719os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9720 int flags)
9721/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9722{
9723 Py_ssize_t cnt;
9724 Py_ssize_t result;
9725 int async_err = 0;
9726 struct iovec *iov;
9727 Py_buffer *buf;
9728
9729 if (!PySequence_Check(buffers)) {
9730 PyErr_SetString(PyExc_TypeError,
9731 "pwritev() arg 2 must be a sequence");
9732 return -1;
9733 }
9734
9735 cnt = PySequence_Size(buffers);
9736 if (cnt < 0) {
9737 return -1;
9738 }
9739
9740#ifndef HAVE_PWRITEV2
9741 if(flags != 0) {
9742 argument_unavailable_error("pwritev2", "flags");
9743 return -1;
9744 }
9745#endif
9746
9747 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9748 return -1;
9749 }
9750#ifdef HAVE_PWRITEV2
9751 do {
9752 Py_BEGIN_ALLOW_THREADS
9753 _Py_BEGIN_SUPPRESS_IPH
9754 result = pwritev2(fd, iov, cnt, offset, flags);
9755 _Py_END_SUPPRESS_IPH
9756 Py_END_ALLOW_THREADS
9757 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9758#else
9759 do {
9760 Py_BEGIN_ALLOW_THREADS
9761 _Py_BEGIN_SUPPRESS_IPH
9762 result = pwritev(fd, iov, cnt, offset);
9763 _Py_END_SUPPRESS_IPH
9764 Py_END_ALLOW_THREADS
9765 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9766#endif
9767
9768 iov_cleanup(iov, buf, cnt);
9769 if (result < 0) {
9770 if (!async_err) {
9771 posix_error();
9772 }
9773 return -1;
9774 }
9775
9776 return result;
9777}
9778#endif /* HAVE_PWRITEV */
9779
Pablo Galindoaac4d032019-05-31 19:39:47 +01009780#ifdef HAVE_COPY_FILE_RANGE
9781/*[clinic input]
9782
9783os.copy_file_range
9784 src: int
9785 Source file descriptor.
9786 dst: int
9787 Destination file descriptor.
9788 count: Py_ssize_t
9789 Number of bytes to copy.
9790 offset_src: object = None
9791 Starting offset in src.
9792 offset_dst: object = None
9793 Starting offset in dst.
9794
9795Copy count bytes from one file descriptor to another.
9796
9797If offset_src is None, then src is read from the current position;
9798respectively for offset_dst.
9799[clinic start generated code]*/
9800
9801static PyObject *
9802os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9803 PyObject *offset_src, PyObject *offset_dst)
9804/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9805{
9806 off_t offset_src_val, offset_dst_val;
9807 off_t *p_offset_src = NULL;
9808 off_t *p_offset_dst = NULL;
9809 Py_ssize_t ret;
9810 int async_err = 0;
9811 /* The flags argument is provided to allow
9812 * for future extensions and currently must be to 0. */
9813 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009814
9815
Pablo Galindoaac4d032019-05-31 19:39:47 +01009816 if (count < 0) {
9817 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9818 return NULL;
9819 }
9820
9821 if (offset_src != Py_None) {
9822 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9823 return NULL;
9824 }
9825 p_offset_src = &offset_src_val;
9826 }
9827
9828 if (offset_dst != Py_None) {
9829 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9830 return NULL;
9831 }
9832 p_offset_dst = &offset_dst_val;
9833 }
9834
9835 do {
9836 Py_BEGIN_ALLOW_THREADS
9837 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9838 Py_END_ALLOW_THREADS
9839 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9840
9841 if (ret < 0) {
9842 return (!async_err) ? posix_error() : NULL;
9843 }
9844
9845 return PyLong_FromSsize_t(ret);
9846}
9847#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009848
9849#ifdef HAVE_MKFIFO
9850/*[clinic input]
9851os.mkfifo
9852
9853 path: path_t
9854 mode: int=0o666
9855 *
9856 dir_fd: dir_fd(requires='mkfifoat')=None
9857
9858Create a "fifo" (a POSIX named pipe).
9859
9860If dir_fd is not None, it should be a file descriptor open to a directory,
9861 and path should be relative; path will then be relative to that directory.
9862dir_fd may not be implemented on your platform.
9863 If it is unavailable, using it will raise a NotImplementedError.
9864[clinic start generated code]*/
9865
Larry Hastings2f936352014-08-05 14:04:04 +10009866static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009867os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9868/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009869{
9870 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009871 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009872
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009873 do {
9874 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009875#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009876 if (dir_fd != DEFAULT_DIR_FD)
9877 result = mkfifoat(dir_fd, path->narrow, mode);
9878 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009879#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009880 result = mkfifo(path->narrow, mode);
9881 Py_END_ALLOW_THREADS
9882 } while (result != 0 && errno == EINTR &&
9883 !(async_err = PyErr_CheckSignals()));
9884 if (result != 0)
9885 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009886
9887 Py_RETURN_NONE;
9888}
9889#endif /* HAVE_MKFIFO */
9890
9891
9892#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9893/*[clinic input]
9894os.mknod
9895
9896 path: path_t
9897 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009898 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009899 *
9900 dir_fd: dir_fd(requires='mknodat')=None
9901
9902Create a node in the file system.
9903
9904Create a node in the file system (file, device special file or named pipe)
9905at path. mode specifies both the permissions to use and the
9906type of node to be created, being combined (bitwise OR) with one of
9907S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9908device defines the newly created device special file (probably using
9909os.makedev()). Otherwise device is ignored.
9910
9911If dir_fd is not None, it should be a file descriptor open to a directory,
9912 and path should be relative; path will then be relative to that directory.
9913dir_fd may not be implemented on your platform.
9914 If it is unavailable, using it will raise a NotImplementedError.
9915[clinic start generated code]*/
9916
Larry Hastings2f936352014-08-05 14:04:04 +10009917static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009918os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009919 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009920/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009921{
9922 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009923 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009924
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009925 do {
9926 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009927#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009928 if (dir_fd != DEFAULT_DIR_FD)
9929 result = mknodat(dir_fd, path->narrow, mode, device);
9930 else
Larry Hastings2f936352014-08-05 14:04:04 +10009931#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009932 result = mknod(path->narrow, mode, device);
9933 Py_END_ALLOW_THREADS
9934 } while (result != 0 && errno == EINTR &&
9935 !(async_err = PyErr_CheckSignals()));
9936 if (result != 0)
9937 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009938
9939 Py_RETURN_NONE;
9940}
9941#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9942
9943
9944#ifdef HAVE_DEVICE_MACROS
9945/*[clinic input]
9946os.major -> unsigned_int
9947
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009948 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009949 /
9950
9951Extracts a device major number from a raw device number.
9952[clinic start generated code]*/
9953
Larry Hastings2f936352014-08-05 14:04:04 +10009954static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009955os_major_impl(PyObject *module, dev_t device)
9956/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009957{
9958 return major(device);
9959}
9960
9961
9962/*[clinic input]
9963os.minor -> unsigned_int
9964
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009965 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009966 /
9967
9968Extracts a device minor number from a raw device number.
9969[clinic start generated code]*/
9970
Larry Hastings2f936352014-08-05 14:04:04 +10009971static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009972os_minor_impl(PyObject *module, dev_t device)
9973/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009974{
9975 return minor(device);
9976}
9977
9978
9979/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009980os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009981
9982 major: int
9983 minor: int
9984 /
9985
9986Composes a raw device number from the major and minor device numbers.
9987[clinic start generated code]*/
9988
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009989static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009990os_makedev_impl(PyObject *module, int major, int minor)
9991/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009992{
9993 return makedev(major, minor);
9994}
9995#endif /* HAVE_DEVICE_MACROS */
9996
9997
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009998#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009999/*[clinic input]
10000os.ftruncate
10001
10002 fd: int
10003 length: Py_off_t
10004 /
10005
10006Truncate a file, specified by file descriptor, to a specific length.
10007[clinic start generated code]*/
10008
Larry Hastings2f936352014-08-05 14:04:04 +100010009static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010010os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
10011/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010012{
10013 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010014 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010015
Steve Dowerb82e17e2019-05-23 08:45:22 -070010016 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
10017 return NULL;
10018 }
10019
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010020 do {
10021 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010022 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010023#ifdef MS_WINDOWS
10024 result = _chsize_s(fd, length);
10025#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010026 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010027#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010028 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010029 Py_END_ALLOW_THREADS
10030 } while (result != 0 && errno == EINTR &&
10031 !(async_err = PyErr_CheckSignals()));
10032 if (result != 0)
10033 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010034 Py_RETURN_NONE;
10035}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010036#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010037
10038
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010039#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010040/*[clinic input]
10041os.truncate
10042 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
10043 length: Py_off_t
10044
10045Truncate a file, specified by path, to a specific length.
10046
10047On some platforms, path may also be specified as an open file descriptor.
10048 If this functionality is unavailable, using it raises an exception.
10049[clinic start generated code]*/
10050
Larry Hastings2f936352014-08-05 14:04:04 +100010051static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010052os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
10053/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010054{
10055 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010056#ifdef MS_WINDOWS
10057 int fd;
10058#endif
10059
10060 if (path->fd != -1)
10061 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010062
Steve Dowerb82e17e2019-05-23 08:45:22 -070010063 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
10064 return NULL;
10065 }
10066
Larry Hastings2f936352014-08-05 14:04:04 +100010067 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010068 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010069#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070010070 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +020010071 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010072 result = -1;
10073 else {
10074 result = _chsize_s(fd, length);
10075 close(fd);
10076 if (result < 0)
10077 errno = result;
10078 }
10079#else
10080 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010081#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010082 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +100010083 Py_END_ALLOW_THREADS
10084 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +030010085 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +100010086
10087 Py_RETURN_NONE;
10088}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010089#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010090
Ross Lagerwall7807c352011-03-17 20:20:30 +020010091
Victor Stinnerd6b17692014-09-30 12:20:05 +020010092/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
10093 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
10094 defined, which is the case in Python on AIX. AIX bug report:
10095 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
10096#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
10097# define POSIX_FADVISE_AIX_BUG
10098#endif
10099
Victor Stinnerec39e262014-09-30 12:35:58 +020010100
Victor Stinnerd6b17692014-09-30 12:20:05 +020010101#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010102/*[clinic input]
10103os.posix_fallocate
10104
10105 fd: int
10106 offset: Py_off_t
10107 length: Py_off_t
10108 /
10109
10110Ensure a file has allocated at least a particular number of bytes on disk.
10111
10112Ensure that the file specified by fd encompasses a range of bytes
10113starting at offset bytes from the beginning and continuing for length bytes.
10114[clinic start generated code]*/
10115
Larry Hastings2f936352014-08-05 14:04:04 +100010116static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010117os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010118 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010119/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010120{
10121 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010122 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010123
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010124 do {
10125 Py_BEGIN_ALLOW_THREADS
10126 result = posix_fallocate(fd, offset, length);
10127 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010128 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10129
10130 if (result == 0)
10131 Py_RETURN_NONE;
10132
10133 if (async_err)
10134 return NULL;
10135
10136 errno = result;
10137 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010138}
Victor Stinnerec39e262014-09-30 12:35:58 +020010139#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +100010140
Ross Lagerwall7807c352011-03-17 20:20:30 +020010141
Victor Stinnerd6b17692014-09-30 12:20:05 +020010142#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010143/*[clinic input]
10144os.posix_fadvise
10145
10146 fd: int
10147 offset: Py_off_t
10148 length: Py_off_t
10149 advice: int
10150 /
10151
10152Announce an intention to access data in a specific pattern.
10153
10154Announce an intention to access data in a specific pattern, thus allowing
10155the kernel to make optimizations.
10156The advice applies to the region of the file specified by fd starting at
10157offset and continuing for length bytes.
10158advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
10159POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
10160POSIX_FADV_DONTNEED.
10161[clinic start generated code]*/
10162
Larry Hastings2f936352014-08-05 14:04:04 +100010163static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010164os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010165 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010166/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010167{
10168 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010169 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010170
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010171 do {
10172 Py_BEGIN_ALLOW_THREADS
10173 result = posix_fadvise(fd, offset, length, advice);
10174 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010175 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10176
10177 if (result == 0)
10178 Py_RETURN_NONE;
10179
10180 if (async_err)
10181 return NULL;
10182
10183 errno = result;
10184 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010185}
Victor Stinnerec39e262014-09-30 12:35:58 +020010186#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010187
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000010188
Thomas Hellerf78f12a2007-11-08 19:33:05 +000010189#ifdef MS_WINDOWS
Victor Stinner161e7b32020-01-24 11:53:44 +010010190static PyObject*
10191win32_putenv(PyObject *name, PyObject *value)
10192{
10193 /* Search from index 1 because on Windows starting '=' is allowed for
10194 defining hidden environment variables. */
10195 if (PyUnicode_GET_LENGTH(name) == 0 ||
10196 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
10197 {
10198 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10199 return NULL;
10200 }
10201 PyObject *unicode;
10202 if (value != NULL) {
10203 unicode = PyUnicode_FromFormat("%U=%U", name, value);
10204 }
10205 else {
10206 unicode = PyUnicode_FromFormat("%U=", name);
10207 }
10208 if (unicode == NULL) {
10209 return NULL;
10210 }
10211
10212 Py_ssize_t size;
10213 /* PyUnicode_AsWideCharString() rejects embedded null characters */
10214 wchar_t *env = PyUnicode_AsWideCharString(unicode, &size);
10215 Py_DECREF(unicode);
10216
10217 if (env == NULL) {
10218 return NULL;
10219 }
10220 if (size > _MAX_ENV) {
10221 PyErr_Format(PyExc_ValueError,
10222 "the environment variable is longer than %u characters",
10223 _MAX_ENV);
10224 PyMem_Free(env);
10225 return NULL;
10226 }
10227
10228 /* _wputenv() and SetEnvironmentVariableW() update the environment in the
10229 Process Environment Block (PEB). _wputenv() also updates CRT 'environ'
10230 and '_wenviron' variables, whereas SetEnvironmentVariableW() does not.
10231
10232 Prefer _wputenv() to be compatible with C libraries using CRT
10233 variables and CRT functions using these variables (ex: getenv()). */
10234 int err = _wputenv(env);
10235 PyMem_Free(env);
10236
10237 if (err) {
10238 posix_error();
10239 return NULL;
10240 }
10241
10242 Py_RETURN_NONE;
10243}
10244#endif
10245
10246
10247#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010248/*[clinic input]
10249os.putenv
10250
10251 name: unicode
10252 value: unicode
10253 /
10254
10255Change or add an environment variable.
10256[clinic start generated code]*/
10257
Larry Hastings2f936352014-08-05 14:04:04 +100010258static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010259os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10260/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010261{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010262 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10263 return NULL;
10264 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010265 return win32_putenv(name, value);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010266}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010267#else
Larry Hastings2f936352014-08-05 14:04:04 +100010268/*[clinic input]
10269os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010270
Larry Hastings2f936352014-08-05 14:04:04 +100010271 name: FSConverter
10272 value: FSConverter
10273 /
10274
10275Change or add an environment variable.
10276[clinic start generated code]*/
10277
Larry Hastings2f936352014-08-05 14:04:04 +100010278static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010279os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10280/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010281{
Serhiy Storchaka77703942017-06-25 07:33:01 +030010282 const char *name_string = PyBytes_AS_STRING(name);
10283 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010284
Serhiy Storchaka77703942017-06-25 07:33:01 +030010285 if (strchr(name_string, '=') != NULL) {
10286 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10287 return NULL;
10288 }
Victor Stinnerb477d192020-01-22 22:48:16 +010010289
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010290 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10291 return NULL;
10292 }
10293
Victor Stinnerb477d192020-01-22 22:48:16 +010010294 if (setenv(name_string, value_string, 1)) {
10295 return posix_error();
10296 }
Larry Hastings2f936352014-08-05 14:04:04 +100010297 Py_RETURN_NONE;
10298}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010299#endif /* !defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100010300
10301
Victor Stinner161e7b32020-01-24 11:53:44 +010010302#ifdef MS_WINDOWS
10303/*[clinic input]
10304os.unsetenv
10305 name: unicode
10306 /
10307
10308Delete an environment variable.
10309[clinic start generated code]*/
10310
10311static PyObject *
10312os_unsetenv_impl(PyObject *module, PyObject *name)
10313/*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/
10314{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010315 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10316 return NULL;
10317 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010318 return win32_putenv(name, NULL);
10319}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010320#else
Larry Hastings2f936352014-08-05 14:04:04 +100010321/*[clinic input]
10322os.unsetenv
10323 name: FSConverter
10324 /
10325
10326Delete an environment variable.
10327[clinic start generated code]*/
10328
Larry Hastings2f936352014-08-05 14:04:04 +100010329static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010330os_unsetenv_impl(PyObject *module, PyObject *name)
10331/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010332{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010333 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10334 return NULL;
10335 }
Victor Stinner984890f2011-11-24 13:53:38 +010010336#ifdef HAVE_BROKEN_UNSETENV
10337 unsetenv(PyBytes_AS_STRING(name));
10338#else
Victor Stinner161e7b32020-01-24 11:53:44 +010010339 int err = unsetenv(PyBytes_AS_STRING(name));
10340 if (err) {
Victor Stinner60b385e2011-11-22 22:01:28 +010010341 return posix_error();
Victor Stinner161e7b32020-01-24 11:53:44 +010010342 }
Victor Stinner984890f2011-11-24 13:53:38 +010010343#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010344
Victor Stinner84ae1182010-05-06 22:05:07 +000010345 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010346}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010347#endif /* !MS_WINDOWS */
Guido van Rossumc524d952001-10-19 01:31:59 +000010348
Larry Hastings2f936352014-08-05 14:04:04 +100010349
10350/*[clinic input]
10351os.strerror
10352
10353 code: int
10354 /
10355
10356Translate an error code to a message string.
10357[clinic start generated code]*/
10358
Larry Hastings2f936352014-08-05 14:04:04 +100010359static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010360os_strerror_impl(PyObject *module, int code)
10361/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010362{
10363 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010364 if (message == NULL) {
10365 PyErr_SetString(PyExc_ValueError,
10366 "strerror() argument out of range");
10367 return NULL;
10368 }
Victor Stinner1b579672011-12-17 05:47:23 +010010369 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010370}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010371
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010372
Guido van Rossumc9641791998-08-04 15:26:23 +000010373#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010374#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010375/*[clinic input]
10376os.WCOREDUMP -> bool
10377
10378 status: int
10379 /
10380
10381Return True if the process returning status was dumped to a core file.
10382[clinic start generated code]*/
10383
Larry Hastings2f936352014-08-05 14:04:04 +100010384static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010385os_WCOREDUMP_impl(PyObject *module, int status)
10386/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010387{
10388 WAIT_TYPE wait_status;
10389 WAIT_STATUS_INT(wait_status) = status;
10390 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010391}
10392#endif /* WCOREDUMP */
10393
Larry Hastings2f936352014-08-05 14:04:04 +100010394
Fred Drake106c1a02002-04-23 15:58:02 +000010395#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010396/*[clinic input]
10397os.WIFCONTINUED -> bool
10398
10399 status: int
10400
10401Return True if a particular process was continued from a job control stop.
10402
10403Return True if the process returning status was continued from a
10404job control stop.
10405[clinic start generated code]*/
10406
Larry Hastings2f936352014-08-05 14:04:04 +100010407static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010408os_WIFCONTINUED_impl(PyObject *module, int status)
10409/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010410{
10411 WAIT_TYPE wait_status;
10412 WAIT_STATUS_INT(wait_status) = status;
10413 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010414}
10415#endif /* WIFCONTINUED */
10416
Larry Hastings2f936352014-08-05 14:04:04 +100010417
Guido van Rossumc9641791998-08-04 15:26:23 +000010418#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010419/*[clinic input]
10420os.WIFSTOPPED -> bool
10421
10422 status: int
10423
10424Return True if the process returning status was stopped.
10425[clinic start generated code]*/
10426
Larry Hastings2f936352014-08-05 14:04:04 +100010427static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010428os_WIFSTOPPED_impl(PyObject *module, int status)
10429/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010430{
10431 WAIT_TYPE wait_status;
10432 WAIT_STATUS_INT(wait_status) = status;
10433 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010434}
10435#endif /* WIFSTOPPED */
10436
Larry Hastings2f936352014-08-05 14:04:04 +100010437
Guido van Rossumc9641791998-08-04 15:26:23 +000010438#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010439/*[clinic input]
10440os.WIFSIGNALED -> bool
10441
10442 status: int
10443
10444Return True if the process returning status was terminated by a signal.
10445[clinic start generated code]*/
10446
Larry Hastings2f936352014-08-05 14:04:04 +100010447static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010448os_WIFSIGNALED_impl(PyObject *module, int status)
10449/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010450{
10451 WAIT_TYPE wait_status;
10452 WAIT_STATUS_INT(wait_status) = status;
10453 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010454}
10455#endif /* WIFSIGNALED */
10456
Larry Hastings2f936352014-08-05 14:04:04 +100010457
Guido van Rossumc9641791998-08-04 15:26:23 +000010458#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010459/*[clinic input]
10460os.WIFEXITED -> bool
10461
10462 status: int
10463
10464Return True if the process returning status exited via the exit() system call.
10465[clinic start generated code]*/
10466
Larry Hastings2f936352014-08-05 14:04:04 +100010467static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010468os_WIFEXITED_impl(PyObject *module, int status)
10469/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010470{
10471 WAIT_TYPE wait_status;
10472 WAIT_STATUS_INT(wait_status) = status;
10473 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010474}
10475#endif /* WIFEXITED */
10476
Larry Hastings2f936352014-08-05 14:04:04 +100010477
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010478#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010479/*[clinic input]
10480os.WEXITSTATUS -> int
10481
10482 status: int
10483
10484Return the process return code from status.
10485[clinic start generated code]*/
10486
Larry Hastings2f936352014-08-05 14:04:04 +100010487static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010488os_WEXITSTATUS_impl(PyObject *module, int status)
10489/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010490{
10491 WAIT_TYPE wait_status;
10492 WAIT_STATUS_INT(wait_status) = status;
10493 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010494}
10495#endif /* WEXITSTATUS */
10496
Larry Hastings2f936352014-08-05 14:04:04 +100010497
Guido van Rossumc9641791998-08-04 15:26:23 +000010498#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010499/*[clinic input]
10500os.WTERMSIG -> int
10501
10502 status: int
10503
10504Return the signal that terminated the process that provided the status value.
10505[clinic start generated code]*/
10506
Larry Hastings2f936352014-08-05 14:04:04 +100010507static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010508os_WTERMSIG_impl(PyObject *module, int status)
10509/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010510{
10511 WAIT_TYPE wait_status;
10512 WAIT_STATUS_INT(wait_status) = status;
10513 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010514}
10515#endif /* WTERMSIG */
10516
Larry Hastings2f936352014-08-05 14:04:04 +100010517
Guido van Rossumc9641791998-08-04 15:26:23 +000010518#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010519/*[clinic input]
10520os.WSTOPSIG -> int
10521
10522 status: int
10523
10524Return the signal that stopped the process that provided the status value.
10525[clinic start generated code]*/
10526
Larry Hastings2f936352014-08-05 14:04:04 +100010527static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010528os_WSTOPSIG_impl(PyObject *module, int status)
10529/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010530{
10531 WAIT_TYPE wait_status;
10532 WAIT_STATUS_INT(wait_status) = status;
10533 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010534}
10535#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010536#endif /* HAVE_SYS_WAIT_H */
10537
10538
Thomas Wouters477c8d52006-05-27 19:21:47 +000010539#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010540#ifdef _SCO_DS
10541/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10542 needed definitions in sys/statvfs.h */
10543#define _SVID3
10544#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010545#include <sys/statvfs.h>
10546
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010547static PyObject*
10548_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondob3966632019-11-05 07:16:14 -080010549 PyObject *StatVFSResultType = _posixstate_global->StatVFSResultType;
10550 PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010551 if (v == NULL)
10552 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010553
10554#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010555 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10556 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10557 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10558 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10559 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10560 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10561 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10562 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10563 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10564 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010565#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010566 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10567 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10568 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010569 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010570 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010571 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010572 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010573 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010574 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010575 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010576 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010577 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010579 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010580 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10581 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010582#endif
Michael Felt502d5512018-01-05 13:01:58 +010010583/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10584 * (issue #32390). */
10585#if defined(_AIX) && defined(_ALL_SOURCE)
10586 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10587#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010588 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010589#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010590 if (PyErr_Occurred()) {
10591 Py_DECREF(v);
10592 return NULL;
10593 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010594
Victor Stinner8c62be82010-05-06 00:08:46 +000010595 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010596}
10597
Larry Hastings2f936352014-08-05 14:04:04 +100010598
10599/*[clinic input]
10600os.fstatvfs
10601 fd: int
10602 /
10603
10604Perform an fstatvfs system call on the given fd.
10605
10606Equivalent to statvfs(fd).
10607[clinic start generated code]*/
10608
Larry Hastings2f936352014-08-05 14:04:04 +100010609static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010610os_fstatvfs_impl(PyObject *module, int fd)
10611/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010612{
10613 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010614 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010615 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010616
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010617 do {
10618 Py_BEGIN_ALLOW_THREADS
10619 result = fstatvfs(fd, &st);
10620 Py_END_ALLOW_THREADS
10621 } while (result != 0 && errno == EINTR &&
10622 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010623 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010624 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010625
Victor Stinner8c62be82010-05-06 00:08:46 +000010626 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010627}
Larry Hastings2f936352014-08-05 14:04:04 +100010628#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010629
10630
Thomas Wouters477c8d52006-05-27 19:21:47 +000010631#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010632#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010633/*[clinic input]
10634os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010635
Larry Hastings2f936352014-08-05 14:04:04 +100010636 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10637
10638Perform a statvfs system call on the given path.
10639
10640path may always be specified as a string.
10641On some platforms, path may also be specified as an open file descriptor.
10642 If this functionality is unavailable, using it raises an exception.
10643[clinic start generated code]*/
10644
Larry Hastings2f936352014-08-05 14:04:04 +100010645static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010646os_statvfs_impl(PyObject *module, path_t *path)
10647/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010648{
10649 int result;
10650 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010651
10652 Py_BEGIN_ALLOW_THREADS
10653#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010654 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010655#ifdef __APPLE__
10656 /* handle weak-linking on Mac OS X 10.3 */
10657 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010658 fd_specified("statvfs", path->fd);
10659 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010660 }
10661#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010662 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010663 }
10664 else
10665#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010666 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010667 Py_END_ALLOW_THREADS
10668
10669 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010670 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010671 }
10672
Larry Hastings2f936352014-08-05 14:04:04 +100010673 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010674}
Larry Hastings2f936352014-08-05 14:04:04 +100010675#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10676
Guido van Rossum94f6f721999-01-06 18:42:14 +000010677
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010678#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010679/*[clinic input]
10680os._getdiskusage
10681
Steve Dower23ad6d02018-02-22 10:39:10 -080010682 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010683
10684Return disk usage statistics about the given path as a (total, free) tuple.
10685[clinic start generated code]*/
10686
Larry Hastings2f936352014-08-05 14:04:04 +100010687static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010688os__getdiskusage_impl(PyObject *module, path_t *path)
10689/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010690{
10691 BOOL retval;
10692 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010693 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010694
10695 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010696 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010697 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010698 if (retval == 0) {
10699 if (GetLastError() == ERROR_DIRECTORY) {
10700 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010701
Joe Pamerc8c02492018-09-25 10:57:36 -040010702 dir_path = PyMem_New(wchar_t, path->length + 1);
10703 if (dir_path == NULL) {
10704 return PyErr_NoMemory();
10705 }
10706
10707 wcscpy_s(dir_path, path->length + 1, path->wide);
10708
10709 if (_dirnameW(dir_path) != -1) {
10710 Py_BEGIN_ALLOW_THREADS
10711 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10712 Py_END_ALLOW_THREADS
10713 }
10714 /* Record the last error in case it's modified by PyMem_Free. */
10715 err = GetLastError();
10716 PyMem_Free(dir_path);
10717 if (retval) {
10718 goto success;
10719 }
10720 }
10721 return PyErr_SetFromWindowsErr(err);
10722 }
10723
10724success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010725 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10726}
Larry Hastings2f936352014-08-05 14:04:04 +100010727#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010728
10729
Fred Drakec9680921999-12-13 16:37:25 +000010730/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10731 * It maps strings representing configuration variable names to
10732 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010733 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010734 * rarely-used constants. There are three separate tables that use
10735 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010736 *
10737 * This code is always included, even if none of the interfaces that
10738 * need it are included. The #if hackery needed to avoid it would be
10739 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010740 */
10741struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010742 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010743 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010744};
10745
Fred Drake12c6e2d1999-12-14 21:25:03 +000010746static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010747conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010748 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010749{
Christian Heimes217cfd12007-12-02 14:31:20 +000010750 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010751 int value = _PyLong_AsInt(arg);
10752 if (value == -1 && PyErr_Occurred())
10753 return 0;
10754 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010755 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010756 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010757 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010758 /* look up the value in the table using a binary search */
10759 size_t lo = 0;
10760 size_t mid;
10761 size_t hi = tablesize;
10762 int cmp;
10763 const char *confname;
10764 if (!PyUnicode_Check(arg)) {
10765 PyErr_SetString(PyExc_TypeError,
10766 "configuration names must be strings or integers");
10767 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010768 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010769 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010770 if (confname == NULL)
10771 return 0;
10772 while (lo < hi) {
10773 mid = (lo + hi) / 2;
10774 cmp = strcmp(confname, table[mid].name);
10775 if (cmp < 0)
10776 hi = mid;
10777 else if (cmp > 0)
10778 lo = mid + 1;
10779 else {
10780 *valuep = table[mid].value;
10781 return 1;
10782 }
10783 }
10784 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10785 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010786 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010787}
10788
10789
10790#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10791static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010792#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010793 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010794#endif
10795#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010796 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010797#endif
Fred Drakec9680921999-12-13 16:37:25 +000010798#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010799 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010800#endif
10801#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010802 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010803#endif
10804#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010805 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010806#endif
10807#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010808 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010809#endif
10810#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010811 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010812#endif
10813#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010814 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010815#endif
10816#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010817 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010818#endif
10819#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010820 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010821#endif
10822#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010823 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010824#endif
10825#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010826 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010827#endif
10828#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010829 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010830#endif
10831#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010832 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010833#endif
10834#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010835 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010836#endif
10837#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010838 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010839#endif
10840#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010841 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010842#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010843#ifdef _PC_ACL_ENABLED
10844 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10845#endif
10846#ifdef _PC_MIN_HOLE_SIZE
10847 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10848#endif
10849#ifdef _PC_ALLOC_SIZE_MIN
10850 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10851#endif
10852#ifdef _PC_REC_INCR_XFER_SIZE
10853 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10854#endif
10855#ifdef _PC_REC_MAX_XFER_SIZE
10856 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10857#endif
10858#ifdef _PC_REC_MIN_XFER_SIZE
10859 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10860#endif
10861#ifdef _PC_REC_XFER_ALIGN
10862 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10863#endif
10864#ifdef _PC_SYMLINK_MAX
10865 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10866#endif
10867#ifdef _PC_XATTR_ENABLED
10868 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10869#endif
10870#ifdef _PC_XATTR_EXISTS
10871 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10872#endif
10873#ifdef _PC_TIMESTAMP_RESOLUTION
10874 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10875#endif
Fred Drakec9680921999-12-13 16:37:25 +000010876};
10877
Fred Drakec9680921999-12-13 16:37:25 +000010878static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010879conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010880{
10881 return conv_confname(arg, valuep, posix_constants_pathconf,
10882 sizeof(posix_constants_pathconf)
10883 / sizeof(struct constdef));
10884}
10885#endif
10886
Larry Hastings2f936352014-08-05 14:04:04 +100010887
Fred Drakec9680921999-12-13 16:37:25 +000010888#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010889/*[clinic input]
10890os.fpathconf -> long
10891
10892 fd: int
10893 name: path_confname
10894 /
10895
10896Return the configuration limit name for the file descriptor fd.
10897
10898If there is no limit, return -1.
10899[clinic start generated code]*/
10900
Larry Hastings2f936352014-08-05 14:04:04 +100010901static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010902os_fpathconf_impl(PyObject *module, int fd, int name)
10903/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010904{
10905 long limit;
10906
10907 errno = 0;
10908 limit = fpathconf(fd, name);
10909 if (limit == -1 && errno != 0)
10910 posix_error();
10911
10912 return limit;
10913}
10914#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010915
10916
10917#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010918/*[clinic input]
10919os.pathconf -> long
10920 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10921 name: path_confname
10922
10923Return the configuration limit name for the file or directory path.
10924
10925If there is no limit, return -1.
10926On some platforms, path may also be specified as an open file descriptor.
10927 If this functionality is unavailable, using it raises an exception.
10928[clinic start generated code]*/
10929
Larry Hastings2f936352014-08-05 14:04:04 +100010930static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010931os_pathconf_impl(PyObject *module, path_t *path, int name)
10932/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010933{
Victor Stinner8c62be82010-05-06 00:08:46 +000010934 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010935
Victor Stinner8c62be82010-05-06 00:08:46 +000010936 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010937#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010938 if (path->fd != -1)
10939 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010940 else
10941#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010942 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010943 if (limit == -1 && errno != 0) {
10944 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010945 /* could be a path or name problem */
10946 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010947 else
Larry Hastings2f936352014-08-05 14:04:04 +100010948 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010949 }
Larry Hastings2f936352014-08-05 14:04:04 +100010950
10951 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010952}
Larry Hastings2f936352014-08-05 14:04:04 +100010953#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010954
10955#ifdef HAVE_CONFSTR
10956static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010957#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010958 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010959#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010960#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010961 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010962#endif
10963#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010964 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010965#endif
Fred Draked86ed291999-12-15 15:34:33 +000010966#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010967 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010968#endif
10969#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010970 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010971#endif
10972#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010973 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010974#endif
10975#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010976 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010977#endif
Fred Drakec9680921999-12-13 16:37:25 +000010978#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010979 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010980#endif
10981#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010982 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010983#endif
10984#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010985 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010986#endif
10987#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010988 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010989#endif
10990#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010991 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010992#endif
10993#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010994 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010995#endif
10996#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010997 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010998#endif
10999#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011000 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011001#endif
Fred Draked86ed291999-12-15 15:34:33 +000011002#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000011003 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000011004#endif
Fred Drakec9680921999-12-13 16:37:25 +000011005#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000011006 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000011007#endif
Fred Draked86ed291999-12-15 15:34:33 +000011008#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011009 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000011010#endif
11011#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011012 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000011013#endif
11014#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011015 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000011016#endif
11017#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011018 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000011019#endif
Fred Drakec9680921999-12-13 16:37:25 +000011020#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011021 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011022#endif
11023#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011024 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011025#endif
11026#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011027 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011028#endif
11029#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011030 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011031#endif
11032#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011033 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011034#endif
11035#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011036 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011037#endif
11038#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011039 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011040#endif
11041#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011042 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011043#endif
11044#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011045 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011046#endif
11047#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011048 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011049#endif
11050#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011051 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011052#endif
11053#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011054 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011055#endif
11056#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011057 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011058#endif
11059#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011060 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011061#endif
11062#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011063 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011064#endif
11065#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011066 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011067#endif
Fred Draked86ed291999-12-15 15:34:33 +000011068#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011069 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011070#endif
11071#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011072 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000011073#endif
11074#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000011075 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000011076#endif
11077#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011079#endif
11080#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011081 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011082#endif
11083#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000011084 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000011085#endif
11086#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011087 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000011088#endif
11089#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000011090 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000011091#endif
11092#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011093 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011094#endif
11095#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000011096 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000011097#endif
11098#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011100#endif
11101#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011102 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011103#endif
11104#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000011106#endif
Fred Drakec9680921999-12-13 16:37:25 +000011107};
11108
11109static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011110conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011111{
11112 return conv_confname(arg, valuep, posix_constants_confstr,
11113 sizeof(posix_constants_confstr)
11114 / sizeof(struct constdef));
11115}
11116
Larry Hastings2f936352014-08-05 14:04:04 +100011117
11118/*[clinic input]
11119os.confstr
11120
11121 name: confstr_confname
11122 /
11123
11124Return a string-valued system configuration variable.
11125[clinic start generated code]*/
11126
Larry Hastings2f936352014-08-05 14:04:04 +100011127static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011128os_confstr_impl(PyObject *module, int name)
11129/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000011130{
11131 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000011132 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011133 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000011134
Victor Stinnercb043522010-09-10 23:49:04 +000011135 errno = 0;
11136 len = confstr(name, buffer, sizeof(buffer));
11137 if (len == 0) {
11138 if (errno) {
11139 posix_error();
11140 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000011141 }
11142 else {
Victor Stinnercb043522010-09-10 23:49:04 +000011143 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000011144 }
11145 }
Victor Stinnercb043522010-09-10 23:49:04 +000011146
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011147 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010011148 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000011149 char *buf = PyMem_Malloc(len);
11150 if (buf == NULL)
11151 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010011152 len2 = confstr(name, buf, len);
11153 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020011154 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000011155 PyMem_Free(buf);
11156 }
11157 else
11158 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000011159 return result;
11160}
Larry Hastings2f936352014-08-05 14:04:04 +100011161#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000011162
11163
11164#ifdef HAVE_SYSCONF
11165static struct constdef posix_constants_sysconf[] = {
11166#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011167 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000011168#endif
11169#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000011170 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000011171#endif
11172#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011173 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011174#endif
11175#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011176 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011177#endif
11178#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011179 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011180#endif
11181#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000011182 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000011183#endif
11184#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000011185 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000011186#endif
11187#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011188 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011189#endif
11190#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000011191 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000011192#endif
11193#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011194 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011195#endif
Fred Draked86ed291999-12-15 15:34:33 +000011196#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011197 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011198#endif
11199#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000011200 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000011201#endif
Fred Drakec9680921999-12-13 16:37:25 +000011202#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011203 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011204#endif
Fred Drakec9680921999-12-13 16:37:25 +000011205#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011206 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011207#endif
11208#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011209 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011210#endif
11211#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011212 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011213#endif
11214#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011215 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011216#endif
11217#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011218 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011219#endif
Fred Draked86ed291999-12-15 15:34:33 +000011220#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011221 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000011222#endif
Fred Drakec9680921999-12-13 16:37:25 +000011223#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011224 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011225#endif
11226#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011227 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011228#endif
11229#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011230 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011231#endif
11232#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011233 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011234#endif
11235#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011236 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011237#endif
Fred Draked86ed291999-12-15 15:34:33 +000011238#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000011239 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000011240#endif
Fred Drakec9680921999-12-13 16:37:25 +000011241#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011242 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011243#endif
11244#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011245 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011246#endif
11247#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011248 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011249#endif
11250#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011251 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011252#endif
11253#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011254 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011255#endif
11256#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011257 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011258#endif
11259#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011260 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011261#endif
11262#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011263 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011264#endif
11265#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011266 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011267#endif
11268#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011269 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011270#endif
11271#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011272 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011273#endif
11274#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011275 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011276#endif
11277#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011278 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011279#endif
11280#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011281 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011282#endif
11283#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011284 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011285#endif
11286#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011287 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011288#endif
11289#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011290 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011291#endif
11292#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011293 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011294#endif
11295#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011296 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011297#endif
11298#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011299 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011300#endif
11301#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011302 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011303#endif
11304#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011305 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011306#endif
11307#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011308 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011309#endif
Fred Draked86ed291999-12-15 15:34:33 +000011310#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011311 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011312#endif
Fred Drakec9680921999-12-13 16:37:25 +000011313#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011314 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011315#endif
11316#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011317 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011318#endif
11319#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011320 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011321#endif
Fred Draked86ed291999-12-15 15:34:33 +000011322#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011323 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011324#endif
Fred Drakec9680921999-12-13 16:37:25 +000011325#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011326 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011327#endif
Fred Draked86ed291999-12-15 15:34:33 +000011328#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011329 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011330#endif
11331#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011332 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011333#endif
Fred Drakec9680921999-12-13 16:37:25 +000011334#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011335 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011336#endif
11337#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011338 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011339#endif
11340#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011341 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011342#endif
11343#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011344 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011345#endif
Fred Draked86ed291999-12-15 15:34:33 +000011346#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011347 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011348#endif
Fred Drakec9680921999-12-13 16:37:25 +000011349#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011350 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011351#endif
11352#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011353 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011354#endif
11355#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011356 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011357#endif
11358#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011359 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011360#endif
11361#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011362 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011363#endif
11364#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011365 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011366#endif
11367#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011368 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011369#endif
Fred Draked86ed291999-12-15 15:34:33 +000011370#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011371 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011372#endif
Fred Drakec9680921999-12-13 16:37:25 +000011373#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011374 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011375#endif
11376#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011377 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011378#endif
Fred Draked86ed291999-12-15 15:34:33 +000011379#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011380 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011381#endif
Fred Drakec9680921999-12-13 16:37:25 +000011382#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011383 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011384#endif
11385#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011386 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011387#endif
11388#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011389 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011390#endif
11391#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011392 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011393#endif
11394#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011395 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011396#endif
11397#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011398 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011399#endif
11400#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011401 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011402#endif
11403#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011404 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011405#endif
11406#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011407 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011408#endif
Fred Draked86ed291999-12-15 15:34:33 +000011409#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011410 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011411#endif
11412#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011413 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011414#endif
Fred Drakec9680921999-12-13 16:37:25 +000011415#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011416 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011417#endif
11418#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011419 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011420#endif
11421#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011422 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011423#endif
11424#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011425 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011426#endif
Batuhan Taşkaya909f4a32020-04-05 03:40:49 +030011427#ifdef _SC_AIX_REALMEM
11428 {"SC_AIX_REALMEM", _SC_AIX_REALMEM},
11429#endif
Fred Drakec9680921999-12-13 16:37:25 +000011430#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011431 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011432#endif
11433#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011434 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011435#endif
11436#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011437 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011438#endif
11439#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011440 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011441#endif
11442#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011443 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011444#endif
11445#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011446 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011447#endif
11448#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011449 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011450#endif
11451#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011452 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011453#endif
11454#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011455 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011456#endif
11457#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011458 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011459#endif
11460#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011461 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011462#endif
11463#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011464 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011465#endif
11466#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011467 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011468#endif
11469#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011470 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011471#endif
11472#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011473 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011474#endif
11475#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011476 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011477#endif
11478#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011479 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011480#endif
11481#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011482 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011483#endif
11484#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011485 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011486#endif
11487#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011488 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011489#endif
11490#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011491 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011492#endif
11493#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011494 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011495#endif
11496#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011497 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011498#endif
11499#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011500 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011501#endif
11502#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011503 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011504#endif
11505#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011506 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011507#endif
11508#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011509 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011510#endif
11511#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011512 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011513#endif
11514#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011515 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011516#endif
11517#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011518 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011519#endif
11520#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011521 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011522#endif
Fred Draked86ed291999-12-15 15:34:33 +000011523#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011524 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011525#endif
Fred Drakec9680921999-12-13 16:37:25 +000011526#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011527 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011528#endif
11529#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011530 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011531#endif
11532#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011533 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011534#endif
11535#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011536 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011537#endif
11538#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011539 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011540#endif
11541#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011542 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011543#endif
11544#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011545 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011546#endif
11547#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011548 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011549#endif
11550#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011551 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011552#endif
11553#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011554 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011555#endif
11556#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011557 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011558#endif
11559#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011560 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011561#endif
11562#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011563 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011564#endif
11565#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011566 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011567#endif
11568#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011569 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011570#endif
11571#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011572 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011573#endif
11574#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011575 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011576#endif
11577#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011578 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011579#endif
11580#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011581 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011582#endif
11583#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011584 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011585#endif
11586#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011587 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011588#endif
11589#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011590 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011591#endif
11592#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011593 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011594#endif
11595#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011596 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011597#endif
11598#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011599 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011600#endif
11601#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011602 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011603#endif
11604#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011605 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011606#endif
11607#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011608 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011609#endif
11610#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011611 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011612#endif
11613#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011614 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011615#endif
11616#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011617 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011618#endif
11619#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011620 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011621#endif
11622#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011623 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011624#endif
11625#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011626 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011627#endif
11628#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011629 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011630#endif
11631#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011632 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011633#endif
11634#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011635 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011636#endif
11637#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011638 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011639#endif
11640#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011641 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011642#endif
11643#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011644 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011645#endif
11646#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011647 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011648#endif
11649#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011650 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011651#endif
11652#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011653 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011654#endif
11655#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011656 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011657#endif
11658#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011659 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011660#endif
11661};
11662
11663static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011664conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011665{
11666 return conv_confname(arg, valuep, posix_constants_sysconf,
11667 sizeof(posix_constants_sysconf)
11668 / sizeof(struct constdef));
11669}
11670
Larry Hastings2f936352014-08-05 14:04:04 +100011671
11672/*[clinic input]
11673os.sysconf -> long
11674 name: sysconf_confname
11675 /
11676
11677Return an integer-valued system configuration variable.
11678[clinic start generated code]*/
11679
Larry Hastings2f936352014-08-05 14:04:04 +100011680static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011681os_sysconf_impl(PyObject *module, int name)
11682/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011683{
11684 long value;
11685
11686 errno = 0;
11687 value = sysconf(name);
11688 if (value == -1 && errno != 0)
11689 posix_error();
11690 return value;
11691}
11692#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011693
11694
Fred Drakebec628d1999-12-15 18:31:10 +000011695/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011696 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011697 * the exported dictionaries that are used to publish information about the
11698 * names available on the host platform.
11699 *
11700 * Sorting the table at runtime ensures that the table is properly ordered
11701 * when used, even for platforms we're not able to test on. It also makes
11702 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011703 */
Fred Drakebec628d1999-12-15 18:31:10 +000011704
11705static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011706cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011707{
11708 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011709 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011710 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011711 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011712
11713 return strcmp(c1->name, c2->name);
11714}
11715
11716static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011717setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011718 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011719{
Fred Drakebec628d1999-12-15 18:31:10 +000011720 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011721 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011722
11723 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11724 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011725 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011726 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011727
Barry Warsaw3155db32000-04-13 15:20:40 +000011728 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011729 PyObject *o = PyLong_FromLong(table[i].value);
11730 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11731 Py_XDECREF(o);
11732 Py_DECREF(d);
11733 return -1;
11734 }
11735 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011736 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011737 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011738}
11739
Fred Drakebec628d1999-12-15 18:31:10 +000011740/* Return -1 on failure, 0 on success. */
11741static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011742setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011743{
11744#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011745 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011746 sizeof(posix_constants_pathconf)
11747 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011748 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011749 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011750#endif
11751#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011752 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011753 sizeof(posix_constants_confstr)
11754 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011755 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011756 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011757#endif
11758#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011759 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011760 sizeof(posix_constants_sysconf)
11761 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011762 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011763 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011764#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011765 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011766}
Fred Draked86ed291999-12-15 15:34:33 +000011767
11768
Larry Hastings2f936352014-08-05 14:04:04 +100011769/*[clinic input]
11770os.abort
11771
11772Abort the interpreter immediately.
11773
11774This function 'dumps core' or otherwise fails in the hardest way possible
11775on the hosting operating system. This function never returns.
11776[clinic start generated code]*/
11777
Larry Hastings2f936352014-08-05 14:04:04 +100011778static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011779os_abort_impl(PyObject *module)
11780/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011781{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011782 abort();
11783 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011784#ifndef __clang__
11785 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11786 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11787 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011788 Py_FatalError("abort() called from Python code didn't abort!");
11789 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011790#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011791}
Fred Drakebec628d1999-12-15 18:31:10 +000011792
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011793#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011794/* Grab ShellExecute dynamically from shell32 */
11795static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011796static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11797 LPCWSTR, INT);
11798static int
11799check_ShellExecute()
11800{
11801 HINSTANCE hShell32;
11802
11803 /* only recheck */
11804 if (-1 == has_ShellExecute) {
11805 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011806 /* Security note: this call is not vulnerable to "DLL hijacking".
11807 SHELL32 is part of "KnownDLLs" and so Windows always load
11808 the system SHELL32.DLL, even if there is another SHELL32.DLL
11809 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011810 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011811 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011812 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11813 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011814 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011815 } else {
11816 has_ShellExecute = 0;
11817 }
Tony Roberts4860f012019-02-02 18:16:42 +010011818 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011819 }
11820 return has_ShellExecute;
11821}
11822
11823
Steve Dowercc16be82016-09-08 10:35:16 -070011824/*[clinic input]
11825os.startfile
11826 filepath: path_t
11827 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011828
Steve Dowercc16be82016-09-08 10:35:16 -070011829Start a file with its associated application.
11830
11831When "operation" is not specified or "open", this acts like
11832double-clicking the file in Explorer, or giving the file name as an
11833argument to the DOS "start" command: the file is opened with whatever
11834application (if any) its extension is associated.
11835When another "operation" is given, it specifies what should be done with
11836the file. A typical operation is "print".
11837
11838startfile returns as soon as the associated application is launched.
11839There is no option to wait for the application to close, and no way
11840to retrieve the application's exit status.
11841
11842The filepath is relative to the current directory. If you want to use
11843an absolute path, make sure the first character is not a slash ("/");
11844the underlying Win32 ShellExecute function doesn't work if it is.
11845[clinic start generated code]*/
11846
11847static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011848os_startfile_impl(PyObject *module, path_t *filepath,
11849 const Py_UNICODE *operation)
Serhiy Storchaka279f4462019-09-14 12:24:05 +030011850/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011851{
11852 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011853
11854 if(!check_ShellExecute()) {
11855 /* If the OS doesn't have ShellExecute, return a
11856 NotImplementedError. */
11857 return PyErr_Format(PyExc_NotImplementedError,
11858 "startfile not available on this platform");
11859 }
11860
Saiyang Gou7514f4f2020-02-12 23:47:42 -080011861 if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -080011862 return NULL;
11863 }
11864
Victor Stinner8c62be82010-05-06 00:08:46 +000011865 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011866 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011867 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011868 Py_END_ALLOW_THREADS
11869
Victor Stinner8c62be82010-05-06 00:08:46 +000011870 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011871 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011872 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011873 }
Steve Dowercc16be82016-09-08 10:35:16 -070011874 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011875}
Larry Hastings2f936352014-08-05 14:04:04 +100011876#endif /* MS_WINDOWS */
11877
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011878
Martin v. Löwis438b5342002-12-27 10:16:42 +000011879#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011880/*[clinic input]
11881os.getloadavg
11882
11883Return average recent system load information.
11884
11885Return the number of processes in the system run queue averaged over
11886the last 1, 5, and 15 minutes as a tuple of three floats.
11887Raises OSError if the load average was unobtainable.
11888[clinic start generated code]*/
11889
Larry Hastings2f936352014-08-05 14:04:04 +100011890static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011891os_getloadavg_impl(PyObject *module)
11892/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011893{
11894 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011895 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011896 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11897 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011898 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011899 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011900}
Larry Hastings2f936352014-08-05 14:04:04 +100011901#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011902
Larry Hastings2f936352014-08-05 14:04:04 +100011903
11904/*[clinic input]
11905os.device_encoding
11906 fd: int
11907
11908Return a string describing the encoding of a terminal's file descriptor.
11909
11910The file descriptor must be attached to a terminal.
11911If the device is not a terminal, return None.
11912[clinic start generated code]*/
11913
Larry Hastings2f936352014-08-05 14:04:04 +100011914static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011915os_device_encoding_impl(PyObject *module, int fd)
11916/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011917{
Brett Cannonefb00c02012-02-29 18:31:31 -050011918 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011919}
11920
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011921
Larry Hastings2f936352014-08-05 14:04:04 +100011922#ifdef HAVE_SETRESUID
11923/*[clinic input]
11924os.setresuid
11925
11926 ruid: uid_t
11927 euid: uid_t
11928 suid: uid_t
11929 /
11930
11931Set the current process's real, effective, and saved user ids.
11932[clinic start generated code]*/
11933
Larry Hastings2f936352014-08-05 14:04:04 +100011934static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011935os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11936/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011937{
Victor Stinner8c62be82010-05-06 00:08:46 +000011938 if (setresuid(ruid, euid, suid) < 0)
11939 return posix_error();
11940 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011941}
Larry Hastings2f936352014-08-05 14:04:04 +100011942#endif /* HAVE_SETRESUID */
11943
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011944
11945#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011946/*[clinic input]
11947os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011948
Larry Hastings2f936352014-08-05 14:04:04 +100011949 rgid: gid_t
11950 egid: gid_t
11951 sgid: gid_t
11952 /
11953
11954Set the current process's real, effective, and saved group ids.
11955[clinic start generated code]*/
11956
Larry Hastings2f936352014-08-05 14:04:04 +100011957static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011958os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11959/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011960{
Victor Stinner8c62be82010-05-06 00:08:46 +000011961 if (setresgid(rgid, egid, sgid) < 0)
11962 return posix_error();
11963 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011964}
Larry Hastings2f936352014-08-05 14:04:04 +100011965#endif /* HAVE_SETRESGID */
11966
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011967
11968#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011969/*[clinic input]
11970os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011971
Larry Hastings2f936352014-08-05 14:04:04 +100011972Return a tuple of the current process's real, effective, and saved user ids.
11973[clinic start generated code]*/
11974
Larry Hastings2f936352014-08-05 14:04:04 +100011975static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011976os_getresuid_impl(PyObject *module)
11977/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011978{
Victor Stinner8c62be82010-05-06 00:08:46 +000011979 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011980 if (getresuid(&ruid, &euid, &suid) < 0)
11981 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011982 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11983 _PyLong_FromUid(euid),
11984 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011985}
Larry Hastings2f936352014-08-05 14:04:04 +100011986#endif /* HAVE_GETRESUID */
11987
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011988
11989#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011990/*[clinic input]
11991os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011992
Larry Hastings2f936352014-08-05 14:04:04 +100011993Return a tuple of the current process's real, effective, and saved group ids.
11994[clinic start generated code]*/
11995
Larry Hastings2f936352014-08-05 14:04:04 +100011996static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011997os_getresgid_impl(PyObject *module)
11998/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011999{
12000 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000012001 if (getresgid(&rgid, &egid, &sgid) < 0)
12002 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020012003 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
12004 _PyLong_FromGid(egid),
12005 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012006}
Larry Hastings2f936352014-08-05 14:04:04 +100012007#endif /* HAVE_GETRESGID */
12008
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012009
Benjamin Peterson9428d532011-09-14 11:45:52 -040012010#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100012011/*[clinic input]
12012os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040012013
Larry Hastings2f936352014-08-05 14:04:04 +100012014 path: path_t(allow_fd=True)
12015 attribute: path_t
12016 *
12017 follow_symlinks: bool = True
12018
12019Return the value of extended attribute attribute on path.
12020
BNMetricsb9427072018-11-02 15:20:19 +000012021path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012022If follow_symlinks is False, and the last element of the path is a symbolic
12023 link, getxattr will examine the symbolic link itself instead of the file
12024 the link points to.
12025
12026[clinic start generated code]*/
12027
Larry Hastings2f936352014-08-05 14:04:04 +100012028static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012029os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012030 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012031/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012032{
12033 Py_ssize_t i;
12034 PyObject *buffer = NULL;
12035
12036 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
12037 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012038
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012039 if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) {
12040 return NULL;
12041 }
12042
Larry Hastings9cf065c2012-06-22 16:30:09 -070012043 for (i = 0; ; i++) {
12044 void *ptr;
12045 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012046 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070012047 Py_ssize_t buffer_size = buffer_sizes[i];
12048 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100012049 path_error(path);
12050 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012051 }
12052 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
12053 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100012054 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012055 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012056
Larry Hastings9cf065c2012-06-22 16:30:09 -070012057 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012058 if (path->fd >= 0)
12059 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012060 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012061 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012062 else
Larry Hastings2f936352014-08-05 14:04:04 +100012063 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012064 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012065
Larry Hastings9cf065c2012-06-22 16:30:09 -070012066 if (result < 0) {
12067 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012068 if (errno == ERANGE)
12069 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100012070 path_error(path);
12071 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012072 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012073
Larry Hastings9cf065c2012-06-22 16:30:09 -070012074 if (result != buffer_size) {
12075 /* Can only shrink. */
12076 _PyBytes_Resize(&buffer, result);
12077 }
12078 break;
12079 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012080
Larry Hastings9cf065c2012-06-22 16:30:09 -070012081 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012082}
12083
Larry Hastings2f936352014-08-05 14:04:04 +100012084
12085/*[clinic input]
12086os.setxattr
12087
12088 path: path_t(allow_fd=True)
12089 attribute: path_t
12090 value: Py_buffer
12091 flags: int = 0
12092 *
12093 follow_symlinks: bool = True
12094
12095Set extended attribute attribute on path to value.
12096
BNMetricsb9427072018-11-02 15:20:19 +000012097path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012098If follow_symlinks is False, and the last element of the path is a symbolic
12099 link, setxattr will modify the symbolic link itself instead of the file
12100 the link points to.
12101
12102[clinic start generated code]*/
12103
Benjamin Peterson799bd802011-08-31 22:15:17 -040012104static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012105os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012106 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012107/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040012108{
Larry Hastings2f936352014-08-05 14:04:04 +100012109 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012110
Larry Hastings2f936352014-08-05 14:04:04 +100012111 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040012112 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012113
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012114 if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object,
12115 value->buf, value->len, flags) < 0) {
12116 return NULL;
12117 }
12118
Benjamin Peterson799bd802011-08-31 22:15:17 -040012119 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012120 if (path->fd > -1)
12121 result = fsetxattr(path->fd, attribute->narrow,
12122 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012123 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012124 result = setxattr(path->narrow, attribute->narrow,
12125 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012126 else
Larry Hastings2f936352014-08-05 14:04:04 +100012127 result = lsetxattr(path->narrow, attribute->narrow,
12128 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012129 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012130
Larry Hastings9cf065c2012-06-22 16:30:09 -070012131 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100012132 path_error(path);
12133 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012134 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012135
Larry Hastings2f936352014-08-05 14:04:04 +100012136 Py_RETURN_NONE;
12137}
12138
12139
12140/*[clinic input]
12141os.removexattr
12142
12143 path: path_t(allow_fd=True)
12144 attribute: path_t
12145 *
12146 follow_symlinks: bool = True
12147
12148Remove extended attribute attribute on path.
12149
BNMetricsb9427072018-11-02 15:20:19 +000012150path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012151If follow_symlinks is False, and the last element of the path is a symbolic
12152 link, removexattr will modify the symbolic link itself instead of the file
12153 the link points to.
12154
12155[clinic start generated code]*/
12156
Larry Hastings2f936352014-08-05 14:04:04 +100012157static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012158os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012159 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012160/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012161{
12162 ssize_t result;
12163
12164 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
12165 return NULL;
12166
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012167 if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) {
12168 return NULL;
12169 }
12170
Larry Hastings2f936352014-08-05 14:04:04 +100012171 Py_BEGIN_ALLOW_THREADS;
12172 if (path->fd > -1)
12173 result = fremovexattr(path->fd, attribute->narrow);
12174 else if (follow_symlinks)
12175 result = removexattr(path->narrow, attribute->narrow);
12176 else
12177 result = lremovexattr(path->narrow, attribute->narrow);
12178 Py_END_ALLOW_THREADS;
12179
12180 if (result) {
12181 return path_error(path);
12182 }
12183
12184 Py_RETURN_NONE;
12185}
12186
12187
12188/*[clinic input]
12189os.listxattr
12190
12191 path: path_t(allow_fd=True, nullable=True) = None
12192 *
12193 follow_symlinks: bool = True
12194
12195Return a list of extended attributes on path.
12196
BNMetricsb9427072018-11-02 15:20:19 +000012197path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012198if path is None, listxattr will examine the current directory.
12199If follow_symlinks is False, and the last element of the path is a symbolic
12200 link, listxattr will examine the symbolic link itself instead of the file
12201 the link points to.
12202[clinic start generated code]*/
12203
Larry Hastings2f936352014-08-05 14:04:04 +100012204static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012205os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012206/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012207{
Larry Hastings9cf065c2012-06-22 16:30:09 -070012208 Py_ssize_t i;
12209 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100012210 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012211 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012212
Larry Hastings2f936352014-08-05 14:04:04 +100012213 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070012214 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012215
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012216 if (PySys_Audit("os.listxattr", "(O)",
12217 path->object ? path->object : Py_None) < 0) {
12218 return NULL;
12219 }
12220
Larry Hastings2f936352014-08-05 14:04:04 +100012221 name = path->narrow ? path->narrow : ".";
12222
Larry Hastings9cf065c2012-06-22 16:30:09 -070012223 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012224 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012225 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012226 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070012227 Py_ssize_t buffer_size = buffer_sizes[i];
12228 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020012229 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100012230 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012231 break;
12232 }
12233 buffer = PyMem_MALLOC(buffer_size);
12234 if (!buffer) {
12235 PyErr_NoMemory();
12236 break;
12237 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012238
Larry Hastings9cf065c2012-06-22 16:30:09 -070012239 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012240 if (path->fd > -1)
12241 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012242 else if (follow_symlinks)
12243 length = listxattr(name, buffer, buffer_size);
12244 else
12245 length = llistxattr(name, buffer, buffer_size);
12246 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012247
Larry Hastings9cf065c2012-06-22 16:30:09 -070012248 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020012249 if (errno == ERANGE) {
12250 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050012251 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012252 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020012253 }
Larry Hastings2f936352014-08-05 14:04:04 +100012254 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012255 break;
12256 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012257
Larry Hastings9cf065c2012-06-22 16:30:09 -070012258 result = PyList_New(0);
12259 if (!result) {
12260 goto exit;
12261 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012262
Larry Hastings9cf065c2012-06-22 16:30:09 -070012263 end = buffer + length;
12264 for (trace = start = buffer; trace != end; trace++) {
12265 if (!*trace) {
12266 int error;
12267 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12268 trace - start);
12269 if (!attribute) {
12270 Py_DECREF(result);
12271 result = NULL;
12272 goto exit;
12273 }
12274 error = PyList_Append(result, attribute);
12275 Py_DECREF(attribute);
12276 if (error) {
12277 Py_DECREF(result);
12278 result = NULL;
12279 goto exit;
12280 }
12281 start = trace + 1;
12282 }
12283 }
12284 break;
12285 }
12286exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012287 if (buffer)
12288 PyMem_FREE(buffer);
12289 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012290}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012291#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012292
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012293
Larry Hastings2f936352014-08-05 14:04:04 +100012294/*[clinic input]
12295os.urandom
12296
12297 size: Py_ssize_t
12298 /
12299
12300Return a bytes object containing random bytes suitable for cryptographic use.
12301[clinic start generated code]*/
12302
Larry Hastings2f936352014-08-05 14:04:04 +100012303static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012304os_urandom_impl(PyObject *module, Py_ssize_t size)
12305/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012306{
12307 PyObject *bytes;
12308 int result;
12309
Georg Brandl2fb477c2012-02-21 00:33:36 +010012310 if (size < 0)
12311 return PyErr_Format(PyExc_ValueError,
12312 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012313 bytes = PyBytes_FromStringAndSize(NULL, size);
12314 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012315 return NULL;
12316
Victor Stinnere66987e2016-09-06 16:33:52 -070012317 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012318 if (result == -1) {
12319 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012320 return NULL;
12321 }
Larry Hastings2f936352014-08-05 14:04:04 +100012322 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012323}
12324
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012325#ifdef HAVE_MEMFD_CREATE
12326/*[clinic input]
12327os.memfd_create
12328
12329 name: FSConverter
12330 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12331
12332[clinic start generated code]*/
12333
12334static PyObject *
12335os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12336/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12337{
12338 int fd;
12339 const char *bytes = PyBytes_AS_STRING(name);
12340 Py_BEGIN_ALLOW_THREADS
12341 fd = memfd_create(bytes, flags);
12342 Py_END_ALLOW_THREADS
12343 if (fd == -1) {
12344 return PyErr_SetFromErrno(PyExc_OSError);
12345 }
12346 return PyLong_FromLong(fd);
12347}
12348#endif
12349
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012350/* Terminal size querying */
12351
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012352PyDoc_STRVAR(TerminalSize_docstring,
12353 "A tuple of (columns, lines) for holding terminal window size");
12354
12355static PyStructSequence_Field TerminalSize_fields[] = {
12356 {"columns", "width of the terminal window in characters"},
12357 {"lines", "height of the terminal window in characters"},
12358 {NULL, NULL}
12359};
12360
12361static PyStructSequence_Desc TerminalSize_desc = {
12362 "os.terminal_size",
12363 TerminalSize_docstring,
12364 TerminalSize_fields,
12365 2,
12366};
12367
12368#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100012369/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012370PyDoc_STRVAR(termsize__doc__,
12371 "Return the size of the terminal window as (columns, lines).\n" \
12372 "\n" \
12373 "The optional argument fd (default standard output) specifies\n" \
12374 "which file descriptor should be queried.\n" \
12375 "\n" \
12376 "If the file descriptor is not connected to a terminal, an OSError\n" \
12377 "is thrown.\n" \
12378 "\n" \
12379 "This function will only be defined if an implementation is\n" \
12380 "available for this system.\n" \
12381 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080012382 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012383 "normally be used, os.get_terminal_size is the low-level implementation.");
12384
12385static PyObject*
12386get_terminal_size(PyObject *self, PyObject *args)
12387{
12388 int columns, lines;
12389 PyObject *termsize;
12390
12391 int fd = fileno(stdout);
12392 /* Under some conditions stdout may not be connected and
12393 * fileno(stdout) may point to an invalid file descriptor. For example
12394 * GUI apps don't have valid standard streams by default.
12395 *
12396 * If this happens, and the optional fd argument is not present,
12397 * the ioctl below will fail returning EBADF. This is what we want.
12398 */
12399
12400 if (!PyArg_ParseTuple(args, "|i", &fd))
12401 return NULL;
12402
12403#ifdef TERMSIZE_USE_IOCTL
12404 {
12405 struct winsize w;
12406 if (ioctl(fd, TIOCGWINSZ, &w))
12407 return PyErr_SetFromErrno(PyExc_OSError);
12408 columns = w.ws_col;
12409 lines = w.ws_row;
12410 }
12411#endif /* TERMSIZE_USE_IOCTL */
12412
12413#ifdef TERMSIZE_USE_CONIO
12414 {
12415 DWORD nhandle;
12416 HANDLE handle;
12417 CONSOLE_SCREEN_BUFFER_INFO csbi;
12418 switch (fd) {
12419 case 0: nhandle = STD_INPUT_HANDLE;
12420 break;
12421 case 1: nhandle = STD_OUTPUT_HANDLE;
12422 break;
12423 case 2: nhandle = STD_ERROR_HANDLE;
12424 break;
12425 default:
12426 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12427 }
12428 handle = GetStdHandle(nhandle);
12429 if (handle == NULL)
12430 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12431 if (handle == INVALID_HANDLE_VALUE)
12432 return PyErr_SetFromWindowsErr(0);
12433
12434 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12435 return PyErr_SetFromWindowsErr(0);
12436
12437 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12438 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12439 }
12440#endif /* TERMSIZE_USE_CONIO */
12441
Hai Shif707d942020-03-16 21:15:01 +080012442 PyObject *TerminalSizeType = get_posix_state(self)->TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080012443 termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012444 if (termsize == NULL)
12445 return NULL;
12446 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12447 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12448 if (PyErr_Occurred()) {
12449 Py_DECREF(termsize);
12450 return NULL;
12451 }
12452 return termsize;
12453}
12454#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12455
Larry Hastings2f936352014-08-05 14:04:04 +100012456
12457/*[clinic input]
12458os.cpu_count
12459
Charles-François Natali80d62e62015-08-13 20:37:08 +010012460Return the number of CPUs in the system; return None if indeterminable.
12461
12462This number is not equivalent to the number of CPUs the current process can
12463use. The number of usable CPUs can be obtained with
12464``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012465[clinic start generated code]*/
12466
Larry Hastings2f936352014-08-05 14:04:04 +100012467static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012468os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012469/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012470{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012471 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012472#ifdef MS_WINDOWS
Steve Doweraa929272019-09-11 16:15:39 +010012473 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012474#elif defined(__hpux)
12475 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12476#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12477 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012478#elif defined(__DragonFly__) || \
12479 defined(__OpenBSD__) || \
12480 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012481 defined(__NetBSD__) || \
12482 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012483 int mib[2];
12484 size_t len = sizeof(ncpu);
12485 mib[0] = CTL_HW;
12486 mib[1] = HW_NCPU;
12487 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12488 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012489#endif
12490 if (ncpu >= 1)
12491 return PyLong_FromLong(ncpu);
12492 else
12493 Py_RETURN_NONE;
12494}
12495
Victor Stinnerdaf45552013-08-28 00:53:59 +020012496
Larry Hastings2f936352014-08-05 14:04:04 +100012497/*[clinic input]
12498os.get_inheritable -> bool
12499
12500 fd: int
12501 /
12502
12503Get the close-on-exe flag of the specified file descriptor.
12504[clinic start generated code]*/
12505
Larry Hastings2f936352014-08-05 14:04:04 +100012506static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012507os_get_inheritable_impl(PyObject *module, int fd)
12508/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012509{
Steve Dower8fc89802015-04-12 00:26:27 -040012510 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012511 _Py_BEGIN_SUPPRESS_IPH
12512 return_value = _Py_get_inheritable(fd);
12513 _Py_END_SUPPRESS_IPH
12514 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012515}
12516
12517
12518/*[clinic input]
12519os.set_inheritable
12520 fd: int
12521 inheritable: int
12522 /
12523
12524Set the inheritable flag of the specified file descriptor.
12525[clinic start generated code]*/
12526
Larry Hastings2f936352014-08-05 14:04:04 +100012527static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012528os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12529/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012530{
Steve Dower8fc89802015-04-12 00:26:27 -040012531 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012532
Steve Dower8fc89802015-04-12 00:26:27 -040012533 _Py_BEGIN_SUPPRESS_IPH
12534 result = _Py_set_inheritable(fd, inheritable, NULL);
12535 _Py_END_SUPPRESS_IPH
12536 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012537 return NULL;
12538 Py_RETURN_NONE;
12539}
12540
12541
12542#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012543/*[clinic input]
12544os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012545 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012546 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012547
Larry Hastings2f936352014-08-05 14:04:04 +100012548Get the close-on-exe flag of the specified file descriptor.
12549[clinic start generated code]*/
12550
Larry Hastings2f936352014-08-05 14:04:04 +100012551static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012552os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012553/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012554{
12555 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012556
12557 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12558 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012559 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012560 }
12561
Larry Hastings2f936352014-08-05 14:04:04 +100012562 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012563}
12564
Victor Stinnerdaf45552013-08-28 00:53:59 +020012565
Larry Hastings2f936352014-08-05 14:04:04 +100012566/*[clinic input]
12567os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012568 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012569 inheritable: bool
12570 /
12571
12572Set the inheritable flag of the specified handle.
12573[clinic start generated code]*/
12574
Larry Hastings2f936352014-08-05 14:04:04 +100012575static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012576os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012577 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012578/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012579{
12580 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012581 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12582 PyErr_SetFromWindowsErr(0);
12583 return NULL;
12584 }
12585 Py_RETURN_NONE;
12586}
Larry Hastings2f936352014-08-05 14:04:04 +100012587#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012588
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012589#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012590/*[clinic input]
12591os.get_blocking -> bool
12592 fd: int
12593 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012594
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012595Get the blocking mode of the file descriptor.
12596
12597Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12598[clinic start generated code]*/
12599
12600static int
12601os_get_blocking_impl(PyObject *module, int fd)
12602/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012603{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012604 int blocking;
12605
Steve Dower8fc89802015-04-12 00:26:27 -040012606 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012607 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012608 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012609 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012610}
12611
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012612/*[clinic input]
12613os.set_blocking
12614 fd: int
12615 blocking: bool(accept={int})
12616 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012617
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012618Set the blocking mode of the specified file descriptor.
12619
12620Set the O_NONBLOCK flag if blocking is False,
12621clear the O_NONBLOCK flag otherwise.
12622[clinic start generated code]*/
12623
12624static PyObject *
12625os_set_blocking_impl(PyObject *module, int fd, int blocking)
12626/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012627{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012628 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012629
Steve Dower8fc89802015-04-12 00:26:27 -040012630 _Py_BEGIN_SUPPRESS_IPH
12631 result = _Py_set_blocking(fd, blocking);
12632 _Py_END_SUPPRESS_IPH
12633 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012634 return NULL;
12635 Py_RETURN_NONE;
12636}
12637#endif /* !MS_WINDOWS */
12638
12639
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012640/*[clinic input]
Eddie Elizondob3966632019-11-05 07:16:14 -080012641class os.DirEntry "DirEntry *" "DirEntryType"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012642[clinic start generated code]*/
Eddie Elizondob3966632019-11-05 07:16:14 -080012643/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012644
12645typedef struct {
12646 PyObject_HEAD
12647 PyObject *name;
12648 PyObject *path;
12649 PyObject *stat;
12650 PyObject *lstat;
12651#ifdef MS_WINDOWS
12652 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012653 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012654 int got_file_index;
12655#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012656#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012657 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012658#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012659 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012660 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012661#endif
12662} DirEntry;
12663
Eddie Elizondob3966632019-11-05 07:16:14 -080012664static PyObject *
12665_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
12666{
12667 PyErr_Format(PyExc_TypeError,
12668 "cannot create '%.100s' instances", _PyType_Name(type));
12669 return NULL;
12670}
12671
Victor Stinner6036e442015-03-08 01:58:04 +010012672static void
12673DirEntry_dealloc(DirEntry *entry)
12674{
Eddie Elizondob3966632019-11-05 07:16:14 -080012675 PyTypeObject *tp = Py_TYPE(entry);
Victor Stinner6036e442015-03-08 01:58:04 +010012676 Py_XDECREF(entry->name);
12677 Py_XDECREF(entry->path);
12678 Py_XDECREF(entry->stat);
12679 Py_XDECREF(entry->lstat);
Eddie Elizondob3966632019-11-05 07:16:14 -080012680 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
12681 free_func(entry);
12682 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010012683}
12684
12685/* Forward reference */
12686static int
12687DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12688
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012689/*[clinic input]
12690os.DirEntry.is_symlink -> bool
12691
12692Return True if the entry is a symbolic link; cached per entry.
12693[clinic start generated code]*/
12694
Victor Stinner6036e442015-03-08 01:58:04 +010012695static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012696os_DirEntry_is_symlink_impl(DirEntry *self)
12697/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012698{
12699#ifdef MS_WINDOWS
12700 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012701#elif defined(HAVE_DIRENT_D_TYPE)
12702 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012703 if (self->d_type != DT_UNKNOWN)
12704 return self->d_type == DT_LNK;
12705 else
12706 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012707#else
12708 /* POSIX without d_type */
12709 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012710#endif
12711}
12712
12713static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012714DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12715{
12716 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012717 STRUCT_STAT st;
12718 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012719
12720#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012721 if (!PyUnicode_FSDecoder(self->path, &ub))
12722 return NULL;
12723 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012724#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012725 if (!PyUnicode_FSConverter(self->path, &ub))
12726 return NULL;
12727 const char *path = PyBytes_AS_STRING(ub);
12728 if (self->dir_fd != DEFAULT_DIR_FD) {
12729#ifdef HAVE_FSTATAT
12730 result = fstatat(self->dir_fd, path, &st,
12731 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12732#else
12733 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12734 return NULL;
12735#endif /* HAVE_FSTATAT */
12736 }
12737 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012738#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012739 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012740 if (follow_symlinks)
12741 result = STAT(path, &st);
12742 else
12743 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012744 }
12745 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012746
12747 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012748 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012749
12750 return _pystat_fromstructstat(&st);
12751}
12752
12753static PyObject *
12754DirEntry_get_lstat(DirEntry *self)
12755{
12756 if (!self->lstat) {
12757#ifdef MS_WINDOWS
12758 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12759#else /* POSIX */
12760 self->lstat = DirEntry_fetch_stat(self, 0);
12761#endif
12762 }
12763 Py_XINCREF(self->lstat);
12764 return self->lstat;
12765}
12766
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012767/*[clinic input]
12768os.DirEntry.stat
12769 *
12770 follow_symlinks: bool = True
12771
12772Return stat_result object for the entry; cached per entry.
12773[clinic start generated code]*/
12774
Victor Stinner6036e442015-03-08 01:58:04 +010012775static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012776os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12777/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012778{
12779 if (!follow_symlinks)
12780 return DirEntry_get_lstat(self);
12781
12782 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012783 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012784 if (result == -1)
12785 return NULL;
12786 else if (result)
12787 self->stat = DirEntry_fetch_stat(self, 1);
12788 else
12789 self->stat = DirEntry_get_lstat(self);
12790 }
12791
12792 Py_XINCREF(self->stat);
12793 return self->stat;
12794}
12795
Victor Stinner6036e442015-03-08 01:58:04 +010012796/* Set exception and return -1 on error, 0 for False, 1 for True */
12797static int
12798DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12799{
12800 PyObject *stat = NULL;
12801 PyObject *st_mode = NULL;
12802 long mode;
12803 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012804#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012805 int is_symlink;
12806 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012807#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012808#ifdef MS_WINDOWS
12809 unsigned long dir_bits;
12810#endif
12811
12812#ifdef MS_WINDOWS
12813 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12814 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012815#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012816 is_symlink = self->d_type == DT_LNK;
12817 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12818#endif
12819
Victor Stinner35a97c02015-03-08 02:59:09 +010012820#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012821 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012822#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012823 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012824 if (!stat) {
12825 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12826 /* If file doesn't exist (anymore), then return False
12827 (i.e., say it's not a file/directory) */
12828 PyErr_Clear();
12829 return 0;
12830 }
12831 goto error;
12832 }
Eddie Elizondob3966632019-11-05 07:16:14 -080012833 st_mode = PyObject_GetAttr(stat, _posixstate_global->st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012834 if (!st_mode)
12835 goto error;
12836
12837 mode = PyLong_AsLong(st_mode);
12838 if (mode == -1 && PyErr_Occurred())
12839 goto error;
12840 Py_CLEAR(st_mode);
12841 Py_CLEAR(stat);
12842 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012843#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012844 }
12845 else if (is_symlink) {
12846 assert(mode_bits != S_IFLNK);
12847 result = 0;
12848 }
12849 else {
12850 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12851#ifdef MS_WINDOWS
12852 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12853 if (mode_bits == S_IFDIR)
12854 result = dir_bits != 0;
12855 else
12856 result = dir_bits == 0;
12857#else /* POSIX */
12858 if (mode_bits == S_IFDIR)
12859 result = self->d_type == DT_DIR;
12860 else
12861 result = self->d_type == DT_REG;
12862#endif
12863 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012864#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012865
12866 return result;
12867
12868error:
12869 Py_XDECREF(st_mode);
12870 Py_XDECREF(stat);
12871 return -1;
12872}
12873
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012874/*[clinic input]
12875os.DirEntry.is_dir -> bool
12876 *
12877 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012878
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012879Return True if the entry is a directory; cached per entry.
12880[clinic start generated code]*/
12881
12882static int
12883os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12884/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12885{
12886 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012887}
12888
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012889/*[clinic input]
12890os.DirEntry.is_file -> bool
12891 *
12892 follow_symlinks: bool = True
12893
12894Return True if the entry is a file; cached per entry.
12895[clinic start generated code]*/
12896
12897static int
12898os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12899/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012900{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012901 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012902}
12903
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012904/*[clinic input]
12905os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012906
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012907Return inode of the entry; cached per entry.
12908[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012909
12910static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012911os_DirEntry_inode_impl(DirEntry *self)
12912/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012913{
12914#ifdef MS_WINDOWS
12915 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012916 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012917 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012918 STRUCT_STAT stat;
12919 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012920
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012921 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012922 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012923 path = PyUnicode_AsUnicode(unicode);
12924 result = LSTAT(path, &stat);
12925 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012926
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012927 if (result != 0)
12928 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012929
12930 self->win32_file_index = stat.st_ino;
12931 self->got_file_index = 1;
12932 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012933 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12934 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012935#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012936 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12937 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012938#endif
12939}
12940
12941static PyObject *
12942DirEntry_repr(DirEntry *self)
12943{
12944 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12945}
12946
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012947/*[clinic input]
12948os.DirEntry.__fspath__
12949
12950Returns the path for the entry.
12951[clinic start generated code]*/
12952
Brett Cannon96881cd2016-06-10 14:37:21 -070012953static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012954os_DirEntry___fspath___impl(DirEntry *self)
12955/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012956{
12957 Py_INCREF(self->path);
12958 return self->path;
12959}
12960
Victor Stinner6036e442015-03-08 01:58:04 +010012961static PyMemberDef DirEntry_members[] = {
12962 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12963 "the entry's base filename, relative to scandir() \"path\" argument"},
12964 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12965 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12966 {NULL}
12967};
12968
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012969#include "clinic/posixmodule.c.h"
12970
Victor Stinner6036e442015-03-08 01:58:04 +010012971static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012972 OS_DIRENTRY_IS_DIR_METHODDEF
12973 OS_DIRENTRY_IS_FILE_METHODDEF
12974 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12975 OS_DIRENTRY_STAT_METHODDEF
12976 OS_DIRENTRY_INODE_METHODDEF
12977 OS_DIRENTRY___FSPATH___METHODDEF
Batuhan Taşkayaf9dd51e2020-04-08 00:37:19 +030012978 {"__class_getitem__", (PyCFunction)Py_GenericAlias,
12979 METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Victor Stinner6036e442015-03-08 01:58:04 +010012980 {NULL}
12981};
12982
Eddie Elizondob3966632019-11-05 07:16:14 -080012983static PyType_Slot DirEntryType_slots[] = {
12984 {Py_tp_new, _disabled_new},
12985 {Py_tp_dealloc, DirEntry_dealloc},
12986 {Py_tp_repr, DirEntry_repr},
12987 {Py_tp_methods, DirEntry_methods},
12988 {Py_tp_members, DirEntry_members},
12989 {0, 0},
Victor Stinner6036e442015-03-08 01:58:04 +010012990};
12991
Eddie Elizondob3966632019-11-05 07:16:14 -080012992static PyType_Spec DirEntryType_spec = {
12993 MODNAME ".DirEntry",
12994 sizeof(DirEntry),
12995 0,
12996 Py_TPFLAGS_DEFAULT,
12997 DirEntryType_slots
12998};
12999
13000
Victor Stinner6036e442015-03-08 01:58:04 +010013001#ifdef MS_WINDOWS
13002
13003static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030013004join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010013005{
13006 Py_ssize_t path_len;
13007 Py_ssize_t size;
13008 wchar_t *result;
13009 wchar_t ch;
13010
13011 if (!path_wide) { /* Default arg: "." */
13012 path_wide = L".";
13013 path_len = 1;
13014 }
13015 else {
13016 path_len = wcslen(path_wide);
13017 }
13018
13019 /* The +1's are for the path separator and the NUL */
13020 size = path_len + 1 + wcslen(filename) + 1;
13021 result = PyMem_New(wchar_t, size);
13022 if (!result) {
13023 PyErr_NoMemory();
13024 return NULL;
13025 }
13026 wcscpy(result, path_wide);
13027 if (path_len > 0) {
13028 ch = result[path_len - 1];
13029 if (ch != SEP && ch != ALTSEP && ch != L':')
13030 result[path_len++] = SEP;
13031 wcscpy(result + path_len, filename);
13032 }
13033 return result;
13034}
13035
13036static PyObject *
13037DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
13038{
13039 DirEntry *entry;
13040 BY_HANDLE_FILE_INFORMATION file_info;
13041 ULONG reparse_tag;
13042 wchar_t *joined_path;
13043
Eddie Elizondob3966632019-11-05 07:16:14 -080013044 PyObject *DirEntryType = _posixstate_global->DirEntryType;
13045 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013046 if (!entry)
13047 return NULL;
13048 entry->name = NULL;
13049 entry->path = NULL;
13050 entry->stat = NULL;
13051 entry->lstat = NULL;
13052 entry->got_file_index = 0;
13053
13054 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
13055 if (!entry->name)
13056 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013057 if (path->narrow) {
13058 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
13059 if (!entry->name)
13060 goto error;
13061 }
Victor Stinner6036e442015-03-08 01:58:04 +010013062
13063 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
13064 if (!joined_path)
13065 goto error;
13066
13067 entry->path = PyUnicode_FromWideChar(joined_path, -1);
13068 PyMem_Free(joined_path);
13069 if (!entry->path)
13070 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013071 if (path->narrow) {
13072 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
13073 if (!entry->path)
13074 goto error;
13075 }
Victor Stinner6036e442015-03-08 01:58:04 +010013076
Steve Dowercc16be82016-09-08 10:35:16 -070013077 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010013078 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
13079
13080 return (PyObject *)entry;
13081
13082error:
13083 Py_DECREF(entry);
13084 return NULL;
13085}
13086
13087#else /* POSIX */
13088
13089static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020013090join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010013091{
13092 Py_ssize_t path_len;
13093 Py_ssize_t size;
13094 char *result;
13095
13096 if (!path_narrow) { /* Default arg: "." */
13097 path_narrow = ".";
13098 path_len = 1;
13099 }
13100 else {
13101 path_len = strlen(path_narrow);
13102 }
13103
13104 if (filename_len == -1)
13105 filename_len = strlen(filename);
13106
13107 /* The +1's are for the path separator and the NUL */
13108 size = path_len + 1 + filename_len + 1;
13109 result = PyMem_New(char, size);
13110 if (!result) {
13111 PyErr_NoMemory();
13112 return NULL;
13113 }
13114 strcpy(result, path_narrow);
13115 if (path_len > 0 && result[path_len - 1] != '/')
13116 result[path_len++] = '/';
13117 strcpy(result + path_len, filename);
13118 return result;
13119}
13120
13121static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020013122DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010013123 ino_t d_ino
13124#ifdef HAVE_DIRENT_D_TYPE
13125 , unsigned char d_type
13126#endif
13127 )
Victor Stinner6036e442015-03-08 01:58:04 +010013128{
13129 DirEntry *entry;
13130 char *joined_path;
13131
Eddie Elizondob3966632019-11-05 07:16:14 -080013132 PyObject *DirEntryType = _posixstate_global->DirEntryType;
13133 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013134 if (!entry)
13135 return NULL;
13136 entry->name = NULL;
13137 entry->path = NULL;
13138 entry->stat = NULL;
13139 entry->lstat = NULL;
13140
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013141 if (path->fd != -1) {
13142 entry->dir_fd = path->fd;
13143 joined_path = NULL;
13144 }
13145 else {
13146 entry->dir_fd = DEFAULT_DIR_FD;
13147 joined_path = join_path_filename(path->narrow, name, name_len);
13148 if (!joined_path)
13149 goto error;
13150 }
Victor Stinner6036e442015-03-08 01:58:04 +010013151
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030013152 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010013153 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013154 if (joined_path)
13155 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013156 }
13157 else {
13158 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013159 if (joined_path)
13160 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013161 }
13162 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013163 if (!entry->name)
13164 goto error;
13165
13166 if (path->fd != -1) {
13167 entry->path = entry->name;
13168 Py_INCREF(entry->path);
13169 }
13170 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010013171 goto error;
13172
Victor Stinner35a97c02015-03-08 02:59:09 +010013173#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013174 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013175#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013176 entry->d_ino = d_ino;
13177
13178 return (PyObject *)entry;
13179
13180error:
13181 Py_XDECREF(entry);
13182 return NULL;
13183}
13184
13185#endif
13186
13187
13188typedef struct {
13189 PyObject_HEAD
13190 path_t path;
13191#ifdef MS_WINDOWS
13192 HANDLE handle;
13193 WIN32_FIND_DATAW file_data;
13194 int first_time;
13195#else /* POSIX */
13196 DIR *dirp;
13197#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013198#ifdef HAVE_FDOPENDIR
13199 int fd;
13200#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013201} ScandirIterator;
13202
13203#ifdef MS_WINDOWS
13204
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013205static int
13206ScandirIterator_is_closed(ScandirIterator *iterator)
13207{
13208 return iterator->handle == INVALID_HANDLE_VALUE;
13209}
13210
Victor Stinner6036e442015-03-08 01:58:04 +010013211static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013212ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013213{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013214 HANDLE handle = iterator->handle;
13215
13216 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013217 return;
13218
Victor Stinner6036e442015-03-08 01:58:04 +010013219 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013220 Py_BEGIN_ALLOW_THREADS
13221 FindClose(handle);
13222 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013223}
13224
13225static PyObject *
13226ScandirIterator_iternext(ScandirIterator *iterator)
13227{
13228 WIN32_FIND_DATAW *file_data = &iterator->file_data;
13229 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013230 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013231
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013232 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013233 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013234 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013235
13236 while (1) {
13237 if (!iterator->first_time) {
13238 Py_BEGIN_ALLOW_THREADS
13239 success = FindNextFileW(iterator->handle, file_data);
13240 Py_END_ALLOW_THREADS
13241 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013242 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013243 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013244 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013245 break;
13246 }
13247 }
13248 iterator->first_time = 0;
13249
13250 /* Skip over . and .. */
13251 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013252 wcscmp(file_data->cFileName, L"..") != 0) {
13253 entry = DirEntry_from_find_data(&iterator->path, file_data);
13254 if (!entry)
13255 break;
13256 return entry;
13257 }
Victor Stinner6036e442015-03-08 01:58:04 +010013258
13259 /* Loop till we get a non-dot directory or finish iterating */
13260 }
13261
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013262 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013263 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013264 return NULL;
13265}
13266
13267#else /* POSIX */
13268
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013269static int
13270ScandirIterator_is_closed(ScandirIterator *iterator)
13271{
13272 return !iterator->dirp;
13273}
13274
Victor Stinner6036e442015-03-08 01:58:04 +010013275static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013276ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013277{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013278 DIR *dirp = iterator->dirp;
13279
13280 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013281 return;
13282
Victor Stinner6036e442015-03-08 01:58:04 +010013283 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013284 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013285#ifdef HAVE_FDOPENDIR
13286 if (iterator->path.fd != -1)
13287 rewinddir(dirp);
13288#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013289 closedir(dirp);
13290 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013291 return;
13292}
13293
13294static PyObject *
13295ScandirIterator_iternext(ScandirIterator *iterator)
13296{
13297 struct dirent *direntp;
13298 Py_ssize_t name_len;
13299 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013300 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013301
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013302 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013303 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013304 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013305
13306 while (1) {
13307 errno = 0;
13308 Py_BEGIN_ALLOW_THREADS
13309 direntp = readdir(iterator->dirp);
13310 Py_END_ALLOW_THREADS
13311
13312 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013313 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013314 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013315 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013316 break;
13317 }
13318
13319 /* Skip over . and .. */
13320 name_len = NAMLEN(direntp);
13321 is_dot = direntp->d_name[0] == '.' &&
13322 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13323 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013324 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013325 name_len, direntp->d_ino
13326#ifdef HAVE_DIRENT_D_TYPE
13327 , direntp->d_type
13328#endif
13329 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013330 if (!entry)
13331 break;
13332 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013333 }
13334
13335 /* Loop till we get a non-dot directory or finish iterating */
13336 }
13337
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013338 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013339 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013340 return NULL;
13341}
13342
13343#endif
13344
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013345static PyObject *
13346ScandirIterator_close(ScandirIterator *self, PyObject *args)
13347{
13348 ScandirIterator_closedir(self);
13349 Py_RETURN_NONE;
13350}
13351
13352static PyObject *
13353ScandirIterator_enter(PyObject *self, PyObject *args)
13354{
13355 Py_INCREF(self);
13356 return self;
13357}
13358
13359static PyObject *
13360ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13361{
13362 ScandirIterator_closedir(self);
13363 Py_RETURN_NONE;
13364}
13365
Victor Stinner6036e442015-03-08 01:58:04 +010013366static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013367ScandirIterator_finalize(ScandirIterator *iterator)
13368{
13369 PyObject *error_type, *error_value, *error_traceback;
13370
13371 /* Save the current exception, if any. */
13372 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13373
13374 if (!ScandirIterator_is_closed(iterator)) {
13375 ScandirIterator_closedir(iterator);
13376
13377 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13378 "unclosed scandir iterator %R", iterator)) {
13379 /* Spurious errors can appear at shutdown */
13380 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13381 PyErr_WriteUnraisable((PyObject *) iterator);
13382 }
13383 }
13384 }
13385
Victor Stinner7bfa4092016-03-23 00:43:54 +010013386 path_cleanup(&iterator->path);
13387
13388 /* Restore the saved exception. */
13389 PyErr_Restore(error_type, error_value, error_traceback);
13390}
13391
13392static void
Victor Stinner6036e442015-03-08 01:58:04 +010013393ScandirIterator_dealloc(ScandirIterator *iterator)
13394{
Eddie Elizondob3966632019-11-05 07:16:14 -080013395 PyTypeObject *tp = Py_TYPE(iterator);
Victor Stinner7bfa4092016-03-23 00:43:54 +010013396 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13397 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013398
Eddie Elizondob3966632019-11-05 07:16:14 -080013399 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
13400 free_func(iterator);
13401 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010013402}
13403
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013404static PyMethodDef ScandirIterator_methods[] = {
13405 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13406 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13407 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13408 {NULL}
13409};
13410
Eddie Elizondob3966632019-11-05 07:16:14 -080013411static PyType_Slot ScandirIteratorType_slots[] = {
13412 {Py_tp_new, _disabled_new},
13413 {Py_tp_dealloc, ScandirIterator_dealloc},
13414 {Py_tp_finalize, ScandirIterator_finalize},
13415 {Py_tp_iter, PyObject_SelfIter},
13416 {Py_tp_iternext, ScandirIterator_iternext},
13417 {Py_tp_methods, ScandirIterator_methods},
13418 {0, 0},
13419};
13420
13421static PyType_Spec ScandirIteratorType_spec = {
13422 MODNAME ".ScandirIterator",
13423 sizeof(ScandirIterator),
13424 0,
13425 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
13426 ScandirIteratorType_slots
Victor Stinner6036e442015-03-08 01:58:04 +010013427};
13428
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013429/*[clinic input]
13430os.scandir
13431
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013432 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013433
13434Return an iterator of DirEntry objects for given path.
13435
BNMetricsb9427072018-11-02 15:20:19 +000013436path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013437is bytes, the names of yielded DirEntry objects will also be bytes; in
13438all other circumstances they will be str.
13439
13440If path is None, uses the path='.'.
13441[clinic start generated code]*/
13442
Victor Stinner6036e442015-03-08 01:58:04 +010013443static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013444os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013445/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013446{
13447 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013448#ifdef MS_WINDOWS
13449 wchar_t *path_strW;
13450#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013451 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013452#ifdef HAVE_FDOPENDIR
13453 int fd = -1;
13454#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013455#endif
13456
Steve Dower60419a72019-06-24 08:42:54 -070013457 if (PySys_Audit("os.scandir", "O",
13458 path->object ? path->object : Py_None) < 0) {
13459 return NULL;
13460 }
13461
Hai Shif707d942020-03-16 21:15:01 +080013462 PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080013463 iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
Victor Stinner6036e442015-03-08 01:58:04 +010013464 if (!iterator)
13465 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013466
13467#ifdef MS_WINDOWS
13468 iterator->handle = INVALID_HANDLE_VALUE;
13469#else
13470 iterator->dirp = NULL;
13471#endif
13472
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013473 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013474 /* Move the ownership to iterator->path */
13475 path->object = NULL;
13476 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013477
13478#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013479 iterator->first_time = 1;
13480
13481 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13482 if (!path_strW)
13483 goto error;
13484
13485 Py_BEGIN_ALLOW_THREADS
13486 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13487 Py_END_ALLOW_THREADS
13488
13489 PyMem_Free(path_strW);
13490
13491 if (iterator->handle == INVALID_HANDLE_VALUE) {
13492 path_error(&iterator->path);
13493 goto error;
13494 }
13495#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013496 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013497#ifdef HAVE_FDOPENDIR
13498 if (path->fd != -1) {
13499 /* closedir() closes the FD, so we duplicate it */
13500 fd = _Py_dup(path->fd);
13501 if (fd == -1)
13502 goto error;
13503
13504 Py_BEGIN_ALLOW_THREADS
13505 iterator->dirp = fdopendir(fd);
13506 Py_END_ALLOW_THREADS
13507 }
13508 else
13509#endif
13510 {
13511 if (iterator->path.narrow)
13512 path_str = iterator->path.narrow;
13513 else
13514 path_str = ".";
13515
13516 Py_BEGIN_ALLOW_THREADS
13517 iterator->dirp = opendir(path_str);
13518 Py_END_ALLOW_THREADS
13519 }
Victor Stinner6036e442015-03-08 01:58:04 +010013520
13521 if (!iterator->dirp) {
13522 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013523#ifdef HAVE_FDOPENDIR
13524 if (fd != -1) {
13525 Py_BEGIN_ALLOW_THREADS
13526 close(fd);
13527 Py_END_ALLOW_THREADS
13528 }
13529#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013530 goto error;
13531 }
13532#endif
13533
13534 return (PyObject *)iterator;
13535
13536error:
13537 Py_DECREF(iterator);
13538 return NULL;
13539}
13540
Ethan Furman410ef8e2016-06-04 12:06:26 -070013541/*
13542 Return the file system path representation of the object.
13543
13544 If the object is str or bytes, then allow it to pass through with
13545 an incremented refcount. If the object defines __fspath__(), then
13546 return the result of that method. All other types raise a TypeError.
13547*/
13548PyObject *
13549PyOS_FSPath(PyObject *path)
13550{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013551 /* For error message reasons, this function is manually inlined in
13552 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013553 PyObject *func = NULL;
13554 PyObject *path_repr = NULL;
13555
13556 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13557 Py_INCREF(path);
13558 return path;
13559 }
13560
13561 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13562 if (NULL == func) {
13563 return PyErr_Format(PyExc_TypeError,
13564 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013565 "not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -080013566 _PyType_Name(Py_TYPE(path)));
Ethan Furman410ef8e2016-06-04 12:06:26 -070013567 }
13568
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013569 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013570 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013571 if (NULL == path_repr) {
13572 return NULL;
13573 }
13574
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013575 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13576 PyErr_Format(PyExc_TypeError,
13577 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -080013578 "not %.200s", _PyType_Name(Py_TYPE(path)),
13579 _PyType_Name(Py_TYPE(path_repr)));
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013580 Py_DECREF(path_repr);
13581 return NULL;
13582 }
13583
Ethan Furman410ef8e2016-06-04 12:06:26 -070013584 return path_repr;
13585}
13586
13587/*[clinic input]
13588os.fspath
13589
13590 path: object
13591
13592Return the file system path representation of the object.
13593
Brett Cannonb4f43e92016-06-09 14:32:08 -070013594If the object is str or bytes, then allow it to pass through as-is. If the
13595object defines __fspath__(), then return the result of that method. All other
13596types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013597[clinic start generated code]*/
13598
13599static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013600os_fspath_impl(PyObject *module, PyObject *path)
13601/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013602{
13603 return PyOS_FSPath(path);
13604}
Victor Stinner6036e442015-03-08 01:58:04 +010013605
Victor Stinner9b1f4742016-09-06 16:18:52 -070013606#ifdef HAVE_GETRANDOM_SYSCALL
13607/*[clinic input]
13608os.getrandom
13609
13610 size: Py_ssize_t
13611 flags: int=0
13612
13613Obtain a series of random bytes.
13614[clinic start generated code]*/
13615
13616static PyObject *
13617os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13618/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13619{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013620 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013621 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013622
13623 if (size < 0) {
13624 errno = EINVAL;
13625 return posix_error();
13626 }
13627
Victor Stinnerec2319c2016-09-20 23:00:59 +020013628 bytes = PyBytes_FromStringAndSize(NULL, size);
13629 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013630 PyErr_NoMemory();
13631 return NULL;
13632 }
13633
13634 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013635 n = syscall(SYS_getrandom,
13636 PyBytes_AS_STRING(bytes),
13637 PyBytes_GET_SIZE(bytes),
13638 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013639 if (n < 0 && errno == EINTR) {
13640 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013641 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013642 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013643
13644 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013645 continue;
13646 }
13647 break;
13648 }
13649
13650 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013651 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013652 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013653 }
13654
Victor Stinnerec2319c2016-09-20 23:00:59 +020013655 if (n != size) {
13656 _PyBytes_Resize(&bytes, n);
13657 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013658
13659 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013660
13661error:
13662 Py_DECREF(bytes);
13663 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013664}
13665#endif /* HAVE_GETRANDOM_SYSCALL */
13666
Steve Dower2438cdf2019-03-29 16:37:16 -070013667#ifdef MS_WINDOWS
13668/* bpo-36085: Helper functions for managing DLL search directories
13669 * on win32
13670 */
13671
13672typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13673typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13674
13675/*[clinic input]
13676os._add_dll_directory
13677
13678 path: path_t
13679
13680Add a path to the DLL search path.
13681
13682This search path is used when resolving dependencies for imported
13683extension modules (the module itself is resolved through sys.path),
13684and also by ctypes.
13685
13686Returns an opaque value that may be passed to os.remove_dll_directory
13687to remove this directory from the search path.
13688[clinic start generated code]*/
13689
13690static PyObject *
13691os__add_dll_directory_impl(PyObject *module, path_t *path)
13692/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13693{
13694 HMODULE hKernel32;
13695 PAddDllDirectory AddDllDirectory;
13696 DLL_DIRECTORY_COOKIE cookie = 0;
13697 DWORD err = 0;
13698
Saiyang Gou7514f4f2020-02-12 23:47:42 -080013699 if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) {
13700 return NULL;
13701 }
13702
Steve Dower2438cdf2019-03-29 16:37:16 -070013703 /* For Windows 7, we have to load this. As this will be a fairly
13704 infrequent operation, just do it each time. Kernel32 is always
13705 loaded. */
13706 Py_BEGIN_ALLOW_THREADS
13707 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13708 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13709 hKernel32, "AddDllDirectory")) ||
13710 !(cookie = (*AddDllDirectory)(path->wide))) {
13711 err = GetLastError();
13712 }
13713 Py_END_ALLOW_THREADS
13714
13715 if (err) {
13716 return win32_error_object_err("add_dll_directory",
13717 path->object, err);
13718 }
13719
13720 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13721}
13722
13723/*[clinic input]
13724os._remove_dll_directory
13725
13726 cookie: object
13727
13728Removes a path from the DLL search path.
13729
13730The parameter is an opaque value that was returned from
13731os.add_dll_directory. You can only remove directories that you added
13732yourself.
13733[clinic start generated code]*/
13734
13735static PyObject *
13736os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13737/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13738{
13739 HMODULE hKernel32;
13740 PRemoveDllDirectory RemoveDllDirectory;
13741 DLL_DIRECTORY_COOKIE cookieValue;
13742 DWORD err = 0;
13743
13744 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13745 PyErr_SetString(PyExc_TypeError,
13746 "Provided cookie was not returned from os.add_dll_directory");
13747 return NULL;
13748 }
13749
13750 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13751 cookie, "DLL directory cookie");
13752
13753 /* For Windows 7, we have to load this. As this will be a fairly
13754 infrequent operation, just do it each time. Kernel32 is always
13755 loaded. */
13756 Py_BEGIN_ALLOW_THREADS
13757 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13758 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13759 hKernel32, "RemoveDllDirectory")) ||
13760 !(*RemoveDllDirectory)(cookieValue)) {
13761 err = GetLastError();
13762 }
13763 Py_END_ALLOW_THREADS
13764
13765 if (err) {
13766 return win32_error_object_err("remove_dll_directory",
13767 NULL, err);
13768 }
13769
13770 if (PyCapsule_SetName(cookie, NULL)) {
13771 return NULL;
13772 }
13773
13774 Py_RETURN_NONE;
13775}
13776
13777#endif
Larry Hastings31826802013-10-19 00:09:25 -070013778
Victor Stinner65a796e2020-04-01 18:49:29 +020013779
13780/* Only check if WIFEXITED is available: expect that it comes
13781 with WEXITSTATUS, WIFSIGNALED, etc.
13782
13783 os.waitstatus_to_exitcode() is implemented in C and not in Python, so
13784 subprocess can safely call it during late Python finalization without
13785 risking that used os attributes were set to None by _PyImport_Cleanup(). */
13786#if defined(WIFEXITED) || defined(MS_WINDOWS)
13787/*[clinic input]
13788os.waitstatus_to_exitcode
13789
13790 status: int
13791
13792Convert a wait status to an exit code.
13793
13794On Unix:
13795
13796* If WIFEXITED(status) is true, return WEXITSTATUS(status).
13797* If WIFSIGNALED(status) is true, return -WTERMSIG(status).
13798* Otherwise, raise a ValueError.
13799
13800On Windows, return status shifted right by 8 bits.
13801
13802On Unix, if the process is being traced or if waitpid() was called with
13803WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true.
13804This function must not be called if WIFSTOPPED(status) is true.
13805[clinic start generated code]*/
13806
13807static PyObject *
13808os_waitstatus_to_exitcode_impl(PyObject *module, int status)
13809/*[clinic end generated code: output=c7c2265731f79b7a input=edfa5ca5006276fb]*/
13810{
13811#ifndef MS_WINDOWS
13812 WAIT_TYPE wait_status;
13813 WAIT_STATUS_INT(wait_status) = status;
13814 int exitcode;
13815 if (WIFEXITED(wait_status)) {
13816 exitcode = WEXITSTATUS(wait_status);
13817 /* Sanity check to provide warranty on the function behavior.
13818 It should not occur in practice */
13819 if (exitcode < 0) {
13820 PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode);
13821 return NULL;
13822 }
13823 }
13824 else if (WIFSIGNALED(wait_status)) {
13825 int signum = WTERMSIG(wait_status);
13826 /* Sanity check to provide warranty on the function behavior.
13827 It should not occurs in practice */
13828 if (signum <= 0) {
13829 PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum);
13830 return NULL;
13831 }
13832 exitcode = -signum;
13833 } else if (WIFSTOPPED(wait_status)) {
13834 /* Status only received if the process is being traced
13835 or if waitpid() was called with WUNTRACED option. */
13836 int signum = WSTOPSIG(wait_status);
13837 PyErr_Format(PyExc_ValueError,
13838 "process stopped by delivery of signal %i",
13839 signum);
13840 return NULL;
13841 }
13842 else {
13843 PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status);
13844 return NULL;
13845 }
13846 return PyLong_FromLong(exitcode);
13847#else
13848 /* Windows implementation: see os.waitpid() implementation
13849 which uses _cwait(). */
13850 int exitcode = (status >> 8);
13851 return PyLong_FromLong(exitcode);
13852#endif
13853}
13854#endif
13855
13856
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013857static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013858
13859 OS_STAT_METHODDEF
13860 OS_ACCESS_METHODDEF
13861 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013862 OS_CHDIR_METHODDEF
13863 OS_CHFLAGS_METHODDEF
13864 OS_CHMOD_METHODDEF
13865 OS_FCHMOD_METHODDEF
13866 OS_LCHMOD_METHODDEF
13867 OS_CHOWN_METHODDEF
13868 OS_FCHOWN_METHODDEF
13869 OS_LCHOWN_METHODDEF
13870 OS_LCHFLAGS_METHODDEF
13871 OS_CHROOT_METHODDEF
13872 OS_CTERMID_METHODDEF
13873 OS_GETCWD_METHODDEF
13874 OS_GETCWDB_METHODDEF
13875 OS_LINK_METHODDEF
13876 OS_LISTDIR_METHODDEF
13877 OS_LSTAT_METHODDEF
13878 OS_MKDIR_METHODDEF
13879 OS_NICE_METHODDEF
13880 OS_GETPRIORITY_METHODDEF
13881 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013882 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013883 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013884 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013885 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013886 OS_RENAME_METHODDEF
13887 OS_REPLACE_METHODDEF
13888 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013889 OS_SYMLINK_METHODDEF
13890 OS_SYSTEM_METHODDEF
13891 OS_UMASK_METHODDEF
13892 OS_UNAME_METHODDEF
13893 OS_UNLINK_METHODDEF
13894 OS_REMOVE_METHODDEF
13895 OS_UTIME_METHODDEF
13896 OS_TIMES_METHODDEF
13897 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013898 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013899 OS_EXECV_METHODDEF
13900 OS_EXECVE_METHODDEF
13901 OS_SPAWNV_METHODDEF
13902 OS_SPAWNVE_METHODDEF
13903 OS_FORK1_METHODDEF
13904 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013905 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013906 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13907 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13908 OS_SCHED_GETPARAM_METHODDEF
13909 OS_SCHED_GETSCHEDULER_METHODDEF
13910 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13911 OS_SCHED_SETPARAM_METHODDEF
13912 OS_SCHED_SETSCHEDULER_METHODDEF
13913 OS_SCHED_YIELD_METHODDEF
13914 OS_SCHED_SETAFFINITY_METHODDEF
13915 OS_SCHED_GETAFFINITY_METHODDEF
13916 OS_OPENPTY_METHODDEF
13917 OS_FORKPTY_METHODDEF
13918 OS_GETEGID_METHODDEF
13919 OS_GETEUID_METHODDEF
13920 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013921#ifdef HAVE_GETGROUPLIST
13922 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13923#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013924 OS_GETGROUPS_METHODDEF
13925 OS_GETPID_METHODDEF
13926 OS_GETPGRP_METHODDEF
13927 OS_GETPPID_METHODDEF
13928 OS_GETUID_METHODDEF
13929 OS_GETLOGIN_METHODDEF
13930 OS_KILL_METHODDEF
13931 OS_KILLPG_METHODDEF
13932 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013933#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013934 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013935#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013936 OS_SETUID_METHODDEF
13937 OS_SETEUID_METHODDEF
13938 OS_SETREUID_METHODDEF
13939 OS_SETGID_METHODDEF
13940 OS_SETEGID_METHODDEF
13941 OS_SETREGID_METHODDEF
13942 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013943#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013944 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013945#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013946 OS_GETPGID_METHODDEF
13947 OS_SETPGRP_METHODDEF
13948 OS_WAIT_METHODDEF
13949 OS_WAIT3_METHODDEF
13950 OS_WAIT4_METHODDEF
13951 OS_WAITID_METHODDEF
13952 OS_WAITPID_METHODDEF
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -080013953 OS_PIDFD_OPEN_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013954 OS_GETSID_METHODDEF
13955 OS_SETSID_METHODDEF
13956 OS_SETPGID_METHODDEF
13957 OS_TCGETPGRP_METHODDEF
13958 OS_TCSETPGRP_METHODDEF
13959 OS_OPEN_METHODDEF
13960 OS_CLOSE_METHODDEF
13961 OS_CLOSERANGE_METHODDEF
13962 OS_DEVICE_ENCODING_METHODDEF
13963 OS_DUP_METHODDEF
13964 OS_DUP2_METHODDEF
13965 OS_LOCKF_METHODDEF
13966 OS_LSEEK_METHODDEF
13967 OS_READ_METHODDEF
13968 OS_READV_METHODDEF
13969 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013970 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013971 OS_WRITE_METHODDEF
13972 OS_WRITEV_METHODDEF
13973 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013974 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013975#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013976 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013977 posix_sendfile__doc__},
13978#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013979 OS_FSTAT_METHODDEF
13980 OS_ISATTY_METHODDEF
13981 OS_PIPE_METHODDEF
13982 OS_PIPE2_METHODDEF
13983 OS_MKFIFO_METHODDEF
13984 OS_MKNOD_METHODDEF
13985 OS_MAJOR_METHODDEF
13986 OS_MINOR_METHODDEF
13987 OS_MAKEDEV_METHODDEF
13988 OS_FTRUNCATE_METHODDEF
13989 OS_TRUNCATE_METHODDEF
13990 OS_POSIX_FALLOCATE_METHODDEF
13991 OS_POSIX_FADVISE_METHODDEF
13992 OS_PUTENV_METHODDEF
13993 OS_UNSETENV_METHODDEF
13994 OS_STRERROR_METHODDEF
13995 OS_FCHDIR_METHODDEF
13996 OS_FSYNC_METHODDEF
13997 OS_SYNC_METHODDEF
13998 OS_FDATASYNC_METHODDEF
13999 OS_WCOREDUMP_METHODDEF
14000 OS_WIFCONTINUED_METHODDEF
14001 OS_WIFSTOPPED_METHODDEF
14002 OS_WIFSIGNALED_METHODDEF
14003 OS_WIFEXITED_METHODDEF
14004 OS_WEXITSTATUS_METHODDEF
14005 OS_WTERMSIG_METHODDEF
14006 OS_WSTOPSIG_METHODDEF
14007 OS_FSTATVFS_METHODDEF
14008 OS_STATVFS_METHODDEF
14009 OS_CONFSTR_METHODDEF
14010 OS_SYSCONF_METHODDEF
14011 OS_FPATHCONF_METHODDEF
14012 OS_PATHCONF_METHODDEF
14013 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030014014 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014015 OS__GETDISKUSAGE_METHODDEF
14016 OS__GETFINALPATHNAME_METHODDEF
14017 OS__GETVOLUMEPATHNAME_METHODDEF
14018 OS_GETLOADAVG_METHODDEF
14019 OS_URANDOM_METHODDEF
14020 OS_SETRESUID_METHODDEF
14021 OS_SETRESGID_METHODDEF
14022 OS_GETRESUID_METHODDEF
14023 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000014024
Larry Hastings2f936352014-08-05 14:04:04 +100014025 OS_GETXATTR_METHODDEF
14026 OS_SETXATTR_METHODDEF
14027 OS_REMOVEXATTR_METHODDEF
14028 OS_LISTXATTR_METHODDEF
14029
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014030#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
14031 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
14032#endif
Larry Hastings2f936352014-08-05 14:04:04 +100014033 OS_CPU_COUNT_METHODDEF
14034 OS_GET_INHERITABLE_METHODDEF
14035 OS_SET_INHERITABLE_METHODDEF
14036 OS_GET_HANDLE_INHERITABLE_METHODDEF
14037 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020014038#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030014039 OS_GET_BLOCKING_METHODDEF
14040 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020014041#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014042 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070014043 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070014044 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014045 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070014046#ifdef MS_WINDOWS
14047 OS__ADD_DLL_DIRECTORY_METHODDEF
14048 OS__REMOVE_DLL_DIRECTORY_METHODDEF
14049#endif
Victor Stinner65a796e2020-04-01 18:49:29 +020014050 OS_WAITSTATUS_TO_EXITCODE_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000014051 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014052};
14053
Barry Warsaw4a342091996-12-19 23:50:02 +000014054static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014055all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000014056{
Guido van Rossum94f6f721999-01-06 18:42:14 +000014057#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014058 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014059#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014060#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014061 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014062#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014063#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014064 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014065#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014066#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014067 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014068#endif
Fred Drakec9680921999-12-13 16:37:25 +000014069#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014070 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000014071#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014072#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014073 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014074#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014075#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014076 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014077#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014078#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014079 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014080#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014081#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014082 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014083#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014084#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014085 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014086#endif
14087#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014088 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014089#endif
14090#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014091 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014092#endif
14093#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014094 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014095#endif
14096#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014097 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014098#endif
14099#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014100 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014101#endif
14102#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014103 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014104#endif
14105#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014106 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014107#endif
14108#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014109 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014110#endif
14111#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014112 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014113#endif
14114#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014115 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014116#endif
14117#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014118 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014119#endif
14120#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014121 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014122#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000014123#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014124 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014125#endif
14126#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014127 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014128#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014129#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014130 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014131#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014132#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014133 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014134#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014135#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000014136#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014137 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014138#endif
14139#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014140 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014141#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014142#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014143#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014144 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014145#endif
14146#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014147 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014148#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014149#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014150 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014151#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014152#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014153 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014154#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020014155#ifdef O_TMPFILE
14156 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
14157#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014158#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014159 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014160#endif
14161#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014162 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014163#endif
14164#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014165 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014166#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020014167#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014168 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020014169#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014170#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014171 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014172#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014173
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014174
Jesus Cea94363612012-06-22 18:32:07 +020014175#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014176 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014177#endif
14178#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014179 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014180#endif
14181
Tim Peters5aa91602002-01-30 05:46:57 +000014182/* MS Windows */
14183#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000014184 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014185 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014186#endif
14187#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000014188 /* Optimize for short life (keep in memory). */
14189 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014190 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014191#endif
14192#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000014193 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014194 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014195#endif
14196#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000014197 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014198 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014199#endif
14200#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000014201 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014202 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014203#endif
14204
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014205/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014206#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000014207 /* Send a SIGIO signal whenever input or output
14208 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014209 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014210#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014211#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000014212 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014213 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014214#endif
14215#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000014216 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014217 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014218#endif
14219#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000014220 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014221 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014222#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014223#ifdef O_NOLINKS
14224 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014225 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014226#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014227#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000014228 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014229 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014230#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000014231
Victor Stinner8c62be82010-05-06 00:08:46 +000014232 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014233#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014234 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014235#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014236#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014237 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014238#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014239#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014240 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014241#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014242#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014243 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014244#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014245#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014246 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014247#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014248#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014249 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014250#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014251#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014252 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014253#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014254#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014255 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014256#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014257#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014258 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014259#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014260#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014261 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014262#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014263#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014264 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014265#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014266#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014267 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014268#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014269#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014270 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014271#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014272#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014273 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014274#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014275#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014276 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014277#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014278#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014279 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014280#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014281#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014282 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014283#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014284
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000014285 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014286#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014287 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014288#endif /* ST_RDONLY */
14289#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014290 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014291#endif /* ST_NOSUID */
14292
doko@ubuntu.comca616a22013-12-08 15:23:07 +010014293 /* GNU extensions */
14294#ifdef ST_NODEV
14295 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
14296#endif /* ST_NODEV */
14297#ifdef ST_NOEXEC
14298 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
14299#endif /* ST_NOEXEC */
14300#ifdef ST_SYNCHRONOUS
14301 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
14302#endif /* ST_SYNCHRONOUS */
14303#ifdef ST_MANDLOCK
14304 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
14305#endif /* ST_MANDLOCK */
14306#ifdef ST_WRITE
14307 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
14308#endif /* ST_WRITE */
14309#ifdef ST_APPEND
14310 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
14311#endif /* ST_APPEND */
14312#ifdef ST_NOATIME
14313 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
14314#endif /* ST_NOATIME */
14315#ifdef ST_NODIRATIME
14316 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
14317#endif /* ST_NODIRATIME */
14318#ifdef ST_RELATIME
14319 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
14320#endif /* ST_RELATIME */
14321
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014322 /* FreeBSD sendfile() constants */
14323#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014324 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014325#endif
14326#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014327 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014328#endif
14329#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014330 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014331#endif
14332
Ross Lagerwall7807c352011-03-17 20:20:30 +020014333 /* constants for posix_fadvise */
14334#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014335 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014336#endif
14337#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014338 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014339#endif
14340#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014341 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014342#endif
14343#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014344 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014345#endif
14346#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014347 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014348#endif
14349#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014350 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014351#endif
14352
14353 /* constants for waitid */
14354#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014355 if (PyModule_AddIntMacro(m, P_PID)) return -1;
14356 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14357 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080014358#ifdef P_PIDFD
14359 if (PyModule_AddIntMacro(m, P_PIDFD)) return -1;
14360#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014361#endif
14362#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014363 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014364#endif
14365#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014366 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014367#endif
14368#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014369 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014370#endif
14371#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014372 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014373#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014374#ifdef CLD_KILLED
14375 if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1;
14376#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014377#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014378 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014379#endif
14380#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014381 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014382#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014383#ifdef CLD_STOPPED
14384 if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1;
14385#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014386#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014387 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014388#endif
14389
14390 /* constants for lockf */
14391#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014392 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014393#endif
14394#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014395 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014396#endif
14397#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014398 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014399#endif
14400#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014401 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014402#endif
14403
Pablo Galindo4defba32018-01-27 16:16:37 +000014404#ifdef RWF_DSYNC
14405 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14406#endif
14407#ifdef RWF_HIPRI
14408 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14409#endif
14410#ifdef RWF_SYNC
14411 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14412#endif
14413#ifdef RWF_NOWAIT
14414 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14415#endif
14416
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014417/* constants for posix_spawn */
14418#ifdef HAVE_POSIX_SPAWN
14419 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14420 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14421 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14422#endif
14423
pxinwrf2d7ac72019-05-21 18:46:37 +080014424#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014425 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14426 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014427 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014428#endif
14429#ifdef HAVE_SPAWNV
14430 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014431 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014432#endif
14433
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014434#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014435#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014436 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014437#endif
14438#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014439 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014440#endif
14441#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014442 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014443#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014444#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014445 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014446#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014447#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014448 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014449#endif
14450#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014451 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014452#endif
14453#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014454 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014455#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014456#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014457 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014458#endif
14459#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014460 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014461#endif
14462#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014463 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014464#endif
14465#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014466 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014467#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014468#endif
14469
Benjamin Peterson9428d532011-09-14 11:45:52 -040014470#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014471 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14472 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14473 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014474#endif
14475
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014476#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014477 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014478#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014479#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014480 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014481#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014482#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014483 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014484#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014485#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014486 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014487#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014488#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014489 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014490#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014491#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014492 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014493#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014494#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014495 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014496#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014497#if HAVE_DECL_RTLD_MEMBER
14498 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14499#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014500
Victor Stinner9b1f4742016-09-06 16:18:52 -070014501#ifdef HAVE_GETRANDOM_SYSCALL
14502 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14503 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14504#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014505#ifdef HAVE_MEMFD_CREATE
14506 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14507 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14508#ifdef MFD_HUGETLB
14509 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014510#endif
14511#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014512 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014513#endif
14514#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014515 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014516#endif
14517#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014518 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014519#endif
14520#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014521 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014522#endif
14523#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014524 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014525#endif
14526#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014527 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014528#endif
14529#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014530 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014531#endif
14532#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014533 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014534#endif
14535#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014536 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014537#endif
14538#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014539 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014540#endif
14541#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014542 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014543#endif
14544#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014545 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014546#endif
14547#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014548 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014549#endif
14550#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014551 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14552#endif
14553#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014554
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014555#if defined(__APPLE__)
14556 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14557#endif
14558
Steve Dower2438cdf2019-03-29 16:37:16 -070014559#ifdef MS_WINDOWS
14560 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14561 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14562 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14563 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14564 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14565#endif
14566
Victor Stinner8c62be82010-05-06 00:08:46 +000014567 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014568}
14569
14570
Martin v. Löwis1a214512008-06-11 05:26:20 +000014571static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014572 PyModuleDef_HEAD_INIT,
14573 MODNAME,
14574 posix__doc__,
Eddie Elizondob3966632019-11-05 07:16:14 -080014575 sizeof(_posixstate),
Victor Stinner8c62be82010-05-06 00:08:46 +000014576 posix_methods,
14577 NULL,
Eddie Elizondob3966632019-11-05 07:16:14 -080014578 _posix_traverse,
14579 _posix_clear,
14580 _posix_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +000014581};
14582
14583
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014584static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014585
14586#ifdef HAVE_FACCESSAT
14587 "HAVE_FACCESSAT",
14588#endif
14589
14590#ifdef HAVE_FCHDIR
14591 "HAVE_FCHDIR",
14592#endif
14593
14594#ifdef HAVE_FCHMOD
14595 "HAVE_FCHMOD",
14596#endif
14597
14598#ifdef HAVE_FCHMODAT
14599 "HAVE_FCHMODAT",
14600#endif
14601
14602#ifdef HAVE_FCHOWN
14603 "HAVE_FCHOWN",
14604#endif
14605
Larry Hastings00964ed2013-08-12 13:49:30 -040014606#ifdef HAVE_FCHOWNAT
14607 "HAVE_FCHOWNAT",
14608#endif
14609
Larry Hastings9cf065c2012-06-22 16:30:09 -070014610#ifdef HAVE_FEXECVE
14611 "HAVE_FEXECVE",
14612#endif
14613
14614#ifdef HAVE_FDOPENDIR
14615 "HAVE_FDOPENDIR",
14616#endif
14617
Georg Brandl306336b2012-06-24 12:55:33 +020014618#ifdef HAVE_FPATHCONF
14619 "HAVE_FPATHCONF",
14620#endif
14621
Larry Hastings9cf065c2012-06-22 16:30:09 -070014622#ifdef HAVE_FSTATAT
14623 "HAVE_FSTATAT",
14624#endif
14625
14626#ifdef HAVE_FSTATVFS
14627 "HAVE_FSTATVFS",
14628#endif
14629
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014630#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014631 "HAVE_FTRUNCATE",
14632#endif
14633
Larry Hastings9cf065c2012-06-22 16:30:09 -070014634#ifdef HAVE_FUTIMENS
14635 "HAVE_FUTIMENS",
14636#endif
14637
14638#ifdef HAVE_FUTIMES
14639 "HAVE_FUTIMES",
14640#endif
14641
14642#ifdef HAVE_FUTIMESAT
14643 "HAVE_FUTIMESAT",
14644#endif
14645
14646#ifdef HAVE_LINKAT
14647 "HAVE_LINKAT",
14648#endif
14649
14650#ifdef HAVE_LCHFLAGS
14651 "HAVE_LCHFLAGS",
14652#endif
14653
14654#ifdef HAVE_LCHMOD
14655 "HAVE_LCHMOD",
14656#endif
14657
14658#ifdef HAVE_LCHOWN
14659 "HAVE_LCHOWN",
14660#endif
14661
14662#ifdef HAVE_LSTAT
14663 "HAVE_LSTAT",
14664#endif
14665
14666#ifdef HAVE_LUTIMES
14667 "HAVE_LUTIMES",
14668#endif
14669
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014670#ifdef HAVE_MEMFD_CREATE
14671 "HAVE_MEMFD_CREATE",
14672#endif
14673
Larry Hastings9cf065c2012-06-22 16:30:09 -070014674#ifdef HAVE_MKDIRAT
14675 "HAVE_MKDIRAT",
14676#endif
14677
14678#ifdef HAVE_MKFIFOAT
14679 "HAVE_MKFIFOAT",
14680#endif
14681
14682#ifdef HAVE_MKNODAT
14683 "HAVE_MKNODAT",
14684#endif
14685
14686#ifdef HAVE_OPENAT
14687 "HAVE_OPENAT",
14688#endif
14689
14690#ifdef HAVE_READLINKAT
14691 "HAVE_READLINKAT",
14692#endif
14693
14694#ifdef HAVE_RENAMEAT
14695 "HAVE_RENAMEAT",
14696#endif
14697
14698#ifdef HAVE_SYMLINKAT
14699 "HAVE_SYMLINKAT",
14700#endif
14701
14702#ifdef HAVE_UNLINKAT
14703 "HAVE_UNLINKAT",
14704#endif
14705
14706#ifdef HAVE_UTIMENSAT
14707 "HAVE_UTIMENSAT",
14708#endif
14709
14710#ifdef MS_WINDOWS
14711 "MS_WINDOWS",
14712#endif
14713
14714 NULL
14715};
14716
14717
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014718PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014719INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014720{
Victor Stinner8c62be82010-05-06 00:08:46 +000014721 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014722 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014723 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014724
Eddie Elizondob3966632019-11-05 07:16:14 -080014725 m = PyState_FindModule(&posixmodule);
14726 if (m != NULL) {
14727 Py_INCREF(m);
14728 return m;
14729 }
14730
Victor Stinner8c62be82010-05-06 00:08:46 +000014731 m = PyModule_Create(&posixmodule);
14732 if (m == NULL)
14733 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014734
Victor Stinner8c62be82010-05-06 00:08:46 +000014735 /* Initialize environ dictionary */
14736 v = convertenviron();
14737 Py_XINCREF(v);
14738 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14739 return NULL;
14740 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014741
Victor Stinner8c62be82010-05-06 00:08:46 +000014742 if (all_ins(m))
14743 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014744
Victor Stinner8c62be82010-05-06 00:08:46 +000014745 if (setup_confname_tables(m))
14746 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014747
Victor Stinner8c62be82010-05-06 00:08:46 +000014748 Py_INCREF(PyExc_OSError);
14749 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014750
Ross Lagerwall7807c352011-03-17 20:20:30 +020014751#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondob3966632019-11-05 07:16:14 -080014752 waitid_result_desc.name = MODNAME ".waitid_result";
14753 PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
14754 if (WaitidResultType == NULL) {
14755 return NULL;
14756 }
14757 Py_INCREF(WaitidResultType);
14758 PyModule_AddObject(m, "waitid_result", WaitidResultType);
Hai Shif707d942020-03-16 21:15:01 +080014759 get_posix_state(m)->WaitidResultType = WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014760#endif
14761
Eddie Elizondob3966632019-11-05 07:16:14 -080014762 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
14763 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14764 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14765 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
14766 PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
14767 if (StatResultType == NULL) {
14768 return NULL;
14769 }
14770 Py_INCREF(StatResultType);
14771 PyModule_AddObject(m, "stat_result", StatResultType);
Hai Shif707d942020-03-16 21:15:01 +080014772 get_posix_state(m)->StatResultType = StatResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014773 structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
14774 ((PyTypeObject *)StatResultType)->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014775
Eddie Elizondob3966632019-11-05 07:16:14 -080014776 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
14777 PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
14778 if (StatVFSResultType == NULL) {
14779 return NULL;
14780 }
14781 Py_INCREF(StatVFSResultType);
14782 PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
Hai Shif707d942020-03-16 21:15:01 +080014783 get_posix_state(m)->StatVFSResultType = StatVFSResultType;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014784#ifdef NEED_TICKS_PER_SECOND
14785# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Eddie Elizondob3966632019-11-05 07:16:14 -080014786 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014787# elif defined(HZ)
Eddie Elizondob3966632019-11-05 07:16:14 -080014788 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014789# else
Eddie Elizondob3966632019-11-05 07:16:14 -080014790 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014791# endif
14792#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014793
William Orr81574b82018-10-01 22:19:56 -070014794#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondob3966632019-11-05 07:16:14 -080014795 sched_param_desc.name = MODNAME ".sched_param";
14796 PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
14797 if (SchedParamType == NULL) {
14798 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014799 }
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014800 Py_INCREF(SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -080014801 PyModule_AddObject(m, "sched_param", SchedParamType);
Hai Shif707d942020-03-16 21:15:01 +080014802 get_posix_state(m)->SchedParamType = SchedParamType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014803 ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014804#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014805
Eddie Elizondob3966632019-11-05 07:16:14 -080014806 /* initialize TerminalSize_info */
14807 PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
14808 if (TerminalSizeType == NULL) {
14809 return NULL;
14810 }
14811 Py_INCREF(TerminalSizeType);
14812 PyModule_AddObject(m, "terminal_size", TerminalSizeType);
Hai Shif707d942020-03-16 21:15:01 +080014813 get_posix_state(m)->TerminalSizeType = TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014814
14815 /* initialize scandir types */
14816 PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec);
14817 if (ScandirIteratorType == NULL) {
14818 return NULL;
14819 }
Hai Shif707d942020-03-16 21:15:01 +080014820 get_posix_state(m)->ScandirIteratorType = ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014821
14822 PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec);
14823 if (DirEntryType == NULL) {
14824 return NULL;
14825 }
14826 Py_INCREF(DirEntryType);
14827 PyModule_AddObject(m, "DirEntry", DirEntryType);
Hai Shif707d942020-03-16 21:15:01 +080014828 get_posix_state(m)->DirEntryType = DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014829
Larry Hastings605a62d2012-06-24 04:33:36 -070014830 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondob3966632019-11-05 07:16:14 -080014831 PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014832 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014833 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014834 }
Eddie Elizondob3966632019-11-05 07:16:14 -080014835 Py_INCREF(TimesResultType);
14836 PyModule_AddObject(m, "times_result", TimesResultType);
Hai Shif707d942020-03-16 21:15:01 +080014837 get_posix_state(m)->TimesResultType = TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070014838
Eddie Elizondob3966632019-11-05 07:16:14 -080014839 PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014840 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014841 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014842 }
Eddie Elizondob3966632019-11-05 07:16:14 -080014843 Py_INCREF(UnameResultType);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014844 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Hai Shif707d942020-03-16 21:15:01 +080014845 get_posix_state(m)->UnameResultType = (PyObject *)UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070014846
Thomas Wouters477c8d52006-05-27 19:21:47 +000014847#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014848 /*
14849 * Step 2 of weak-linking support on Mac OS X.
14850 *
14851 * The code below removes functions that are not available on the
14852 * currently active platform.
14853 *
14854 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014855 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014856 * OSX 10.4.
14857 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014858#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014859 if (fstatvfs == NULL) {
14860 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14861 return NULL;
14862 }
14863 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014864#endif /* HAVE_FSTATVFS */
14865
14866#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014867 if (statvfs == NULL) {
14868 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14869 return NULL;
14870 }
14871 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014872#endif /* HAVE_STATVFS */
14873
14874# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014875 if (lchown == NULL) {
14876 if (PyObject_DelAttrString(m, "lchown") == -1) {
14877 return NULL;
14878 }
14879 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014880#endif /* HAVE_LCHOWN */
14881
14882
14883#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014884
Hai Shif707d942020-03-16 21:15:01 +080014885 if ((get_posix_state(m)->billion = PyLong_FromLong(1000000000)) == NULL)
Eddie Elizondob3966632019-11-05 07:16:14 -080014886 return NULL;
14887#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Hai Shif707d942020-03-16 21:15:01 +080014888 get_posix_state(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage");
14889 if (get_posix_state(m)->struct_rusage == NULL)
Eddie Elizondob3966632019-11-05 07:16:14 -080014890 return NULL;
14891#endif
Hai Shif707d942020-03-16 21:15:01 +080014892 get_posix_state(m)->st_mode = PyUnicode_InternFromString("st_mode");
14893 if (get_posix_state(m)->st_mode == NULL)
Larry Hastings6fe20b32012-04-19 15:07:49 -070014894 return NULL;
14895
Larry Hastings9cf065c2012-06-22 16:30:09 -070014896 /* suppress "function not used" warnings */
14897 {
14898 int ignored;
14899 fd_specified("", -1);
14900 follow_symlinks_specified("", 1);
14901 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14902 dir_fd_converter(Py_None, &ignored);
14903 dir_fd_unavailable(Py_None, &ignored);
14904 }
14905
14906 /*
14907 * provide list of locally available functions
14908 * so os.py can populate support_* lists
14909 */
14910 list = PyList_New(0);
14911 if (!list)
14912 return NULL;
14913 for (trace = have_functions; *trace; trace++) {
14914 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14915 if (!unicode)
14916 return NULL;
14917 if (PyList_Append(list, unicode))
14918 return NULL;
14919 Py_DECREF(unicode);
14920 }
14921 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014922
Victor Stinner8c62be82010-05-06 00:08:46 +000014923 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014924}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014925
14926#ifdef __cplusplus
14927}
14928#endif