blob: 65e8d5e7bd984db21698c358587f2164e0b9ba6a [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* POSIX module implementation */
2
Jesus Ceaab70e2a2012-10-05 01:48:08 +02003/* This file is also used for Windows NT/MS-Win. In that case the
4 module actually calls itself 'nt', not 'posix', and a few
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00007 of the compiler used. Different compilers define their own feature
Victor Stinnerf427a142014-10-22 12:33:23 +02008 test macro, e.g. '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009
Thomas Wouters68bc4f92006-03-01 01:05:10 +000010#define PY_SSIZE_T_CLEAN
11
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000012#include "Python.h"
Kyle Evans79925792020-10-13 15:04:44 -050013#include "pycore_fileutils.h"
Victor Stinnerd5d9e812019-05-13 12:35:37 +020014#ifdef MS_WINDOWS
15 /* include <windows.h> early to avoid conflict with pycore_condvar.h:
16
17 #define WIN32_LEAN_AND_MEAN
18 #include <windows.h>
19
20 FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */
21# include <windows.h>
Steve Dower04732ca2021-04-07 01:02:07 +010022# include <pathcch.h>
Victor Stinnerd5d9e812019-05-13 12:35:37 +020023#endif
24
pxinwr3405e052020-08-07 13:21:52 +080025#ifdef __VXWORKS__
26# include "pycore_bitutils.h" // _Py_popcount32()
27#endif
Victor Stinner4a21e572020-04-15 02:35:41 +020028#include "pycore_ceval.h" // _PyEval_ReInitThreads()
29#include "pycore_import.h" // _PyImport_ReInitLock()
Victor Stinner26881c82020-06-02 15:51:37 +020030#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
Victor Stinner4a21e572020-04-15 02:35:41 +020031#include "pycore_pystate.h" // _PyInterpreterState_GET()
32#include "structmember.h" // PyMemberDef
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020033#ifndef MS_WINDOWS
Victor Stinnerd5d9e812019-05-13 12:35:37 +020034# include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010035#else
Victor Stinnerd5d9e812019-05-13 12:35:37 +020036# include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020037#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000038
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020039/* On android API level 21, 'AT_EACCESS' is not declared although
40 * HAVE_FACCESSAT is defined. */
41#ifdef __ANDROID__
Victor Stinner5eca75d2020-04-15 15:07:31 +020042# undef HAVE_FACCESSAT
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020043#endif
44
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000045#include <stdio.h> /* needed for ctermid() */
46
Ronald Oussoren41761932020-11-08 10:05:27 +010047/*
48 * A number of APIs are available on macOS from a certain macOS version.
49 * To support building with a new SDK while deploying to older versions
50 * the availability test is split into two:
51 * - HAVE_<FUNCTION>: The configure check for compile time availability
52 * - HAVE_<FUNCTION>_RUNTIME: Runtime check for availability
53 *
54 * The latter is always true when not on macOS, or when using a compiler
55 * that does not support __has_builtin (older versions of Xcode).
56 *
57 * Due to compiler restrictions there is one valid use of HAVE_<FUNCTION>_RUNTIME:
58 * if (HAVE_<FUNCTION>_RUNTIME) { ... }
59 *
60 * In mixing the test with other tests or using negations will result in compile
61 * errors.
62 */
63#if defined(__APPLE__)
64
Joshua Rootdf21f502021-01-04 21:36:58 +110065#if defined(__has_builtin)
66#if __has_builtin(__builtin_available)
67#define HAVE_BUILTIN_AVAILABLE 1
68#endif
69#endif
70
71#ifdef HAVE_BUILTIN_AVAILABLE
Ronald Oussoren41761932020-11-08 10:05:27 +010072# define HAVE_FSTATAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
73# define HAVE_FACCESSAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
74# define HAVE_FCHMODAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
75# define HAVE_FCHOWNAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
76# define HAVE_LINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
77# define HAVE_FDOPENDIR_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
78# define HAVE_MKDIRAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
79# define HAVE_RENAMEAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
80# define HAVE_UNLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
81# define HAVE_OPENAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
82# define HAVE_READLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
83# define HAVE_SYMLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
84# define HAVE_FUTIMENS_RUNTIME __builtin_available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
85# define HAVE_UTIMENSAT_RUNTIME __builtin_available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
86# define HAVE_PWRITEV_RUNTIME __builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
87
88# define HAVE_POSIX_SPAWN_SETSID_RUNTIME __builtin_available(macOS 10.15, *)
89
90#else /* Xcode 8 or earlier */
91
92 /* __builtin_available is not present in these compilers, but
93 * some of the symbols might be weak linked (10.10 SDK or later
94 * deploying on 10.9.
95 *
96 * Fall back to the older style of availability checking for
97 * symbols introduced in macOS 10.10.
98 */
99
100# ifdef HAVE_FSTATAT
101# define HAVE_FSTATAT_RUNTIME (fstatat != NULL)
102# endif
103
104# ifdef HAVE_FACCESSAT
105# define HAVE_FACCESSAT_RUNTIME (faccessat != NULL)
106# endif
107
108# ifdef HAVE_FCHMODAT
109# define HAVE_FCHMODAT_RUNTIME (fchmodat != NULL)
110# endif
111
112# ifdef HAVE_FCHOWNAT
113# define HAVE_FCHOWNAT_RUNTIME (fchownat != NULL)
114# endif
115
116# ifdef HAVE_LINKAT
117# define HAVE_LINKAT_RUNTIME (linkat != NULL)
118# endif
119
120# ifdef HAVE_FDOPENDIR
121# define HAVE_FDOPENDIR_RUNTIME (fdopendir != NULL)
122# endif
123
124# ifdef HAVE_MKDIRAT
125# define HAVE_MKDIRAT_RUNTIME (mkdirat != NULL)
126# endif
127
128# ifdef HAVE_RENAMEAT
129# define HAVE_RENAMEAT_RUNTIME (renameat != NULL)
130# endif
131
132# ifdef HAVE_UNLINKAT
133# define HAVE_UNLINKAT_RUNTIME (unlinkat != NULL)
134# endif
135
136# ifdef HAVE_OPENAT
137# define HAVE_OPENAT_RUNTIME (openat != NULL)
138# endif
139
140# ifdef HAVE_READLINKAT
141# define HAVE_READLINKAT_RUNTIME (readlinkat != NULL)
142# endif
143
144# ifdef HAVE_SYMLINKAT
145# define HAVE_SYMLINKAT_RUNTIME (symlinkat != NULL)
146# endif
147
148#endif
149
150#ifdef HAVE_FUTIMESAT
151/* Some of the logic for weak linking depends on this assertion */
152# error "HAVE_FUTIMESAT unexpectedly defined"
153#endif
154
155#else
156# define HAVE_FSTATAT_RUNTIME 1
157# define HAVE_FACCESSAT_RUNTIME 1
158# define HAVE_FCHMODAT_RUNTIME 1
159# define HAVE_FCHOWNAT_RUNTIME 1
160# define HAVE_LINKAT_RUNTIME 1
161# define HAVE_FDOPENDIR_RUNTIME 1
162# define HAVE_MKDIRAT_RUNTIME 1
163# define HAVE_RENAMEAT_RUNTIME 1
164# define HAVE_UNLINKAT_RUNTIME 1
165# define HAVE_OPENAT_RUNTIME 1
166# define HAVE_READLINKAT_RUNTIME 1
167# define HAVE_SYMLINKAT_RUNTIME 1
168# define HAVE_FUTIMENS_RUNTIME 1
169# define HAVE_UTIMENSAT_RUNTIME 1
170# define HAVE_PWRITEV_RUNTIME 1
171#endif
172
173
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000174#ifdef __cplusplus
175extern "C" {
176#endif
177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000178PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000179"This module provides access to operating system functionality that is\n\
180standardized by the C Standard and the POSIX standard (a thinly\n\
181disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000182corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000184
Ross Lagerwall4d076da2011-03-18 06:56:53 +0200185#ifdef HAVE_SYS_UIO_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200186# include <sys/uio.h>
Ross Lagerwall4d076da2011-03-18 06:56:53 +0200187#endif
188
Christian Heimes75b96182017-09-05 15:53:09 +0200189#ifdef HAVE_SYS_SYSMACROS_H
190/* GNU C Library: major(), minor(), makedev() */
Victor Stinner5eca75d2020-04-15 15:07:31 +0200191# include <sys/sysmacros.h>
Christian Heimes75b96182017-09-05 15:53:09 +0200192#endif
193
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000194#ifdef HAVE_SYS_TYPES_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200195# include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000196#endif /* HAVE_SYS_TYPES_H */
197
198#ifdef HAVE_SYS_STAT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200199# include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000200#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +0000201
Guido van Rossum36bc6801995-06-14 22:54:23 +0000202#ifdef HAVE_SYS_WAIT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200203# include <sys/wait.h> // WNOHANG
Guido van Rossum36bc6801995-06-14 22:54:23 +0000204#endif
Benjamin Peterson5c0c3252019-11-05 21:58:31 -0800205#ifdef HAVE_LINUX_WAIT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200206# include <linux/wait.h> // P_PIDFD
Benjamin Peterson5c0c3252019-11-05 21:58:31 -0800207#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000209#ifdef HAVE_SIGNAL_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200210# include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +0000212
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213#ifdef HAVE_FCNTL_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200214# include <fcntl.h>
215#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216
Guido van Rossuma6535fd2001-10-18 19:44:10 +0000217#ifdef HAVE_GRP_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200218# include <grp.h>
Guido van Rossuma6535fd2001-10-18 19:44:10 +0000219#endif
220
Barry Warsaw5676bd12003-01-07 20:57:09 +0000221#ifdef HAVE_SYSEXITS_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200222# include <sysexits.h>
223#endif
Barry Warsaw5676bd12003-01-07 20:57:09 +0000224
Anthony Baxter8a560de2004-10-13 15:30:56 +0000225#ifdef HAVE_SYS_LOADAVG_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200226# include <sys/loadavg.h>
Anthony Baxter8a560de2004-10-13 15:30:56 +0000227#endif
228
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000229#ifdef HAVE_SYS_SENDFILE_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200230# include <sys/sendfile.h>
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000231#endif
232
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200233#if defined(__APPLE__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200234# include <copyfile.h>
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200235#endif
236
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500237#ifdef HAVE_SCHED_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200238# include <sched.h>
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500239#endif
240
Pablo Galindoaac4d032019-05-31 19:39:47 +0100241#ifdef HAVE_COPY_FILE_RANGE
Victor Stinner5eca75d2020-04-15 15:07:31 +0200242# include <unistd.h>
Pablo Galindoaac4d032019-05-31 19:39:47 +0100243#endif
244
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500245#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200246# undef HAVE_SCHED_SETAFFINITY
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500247#endif
248
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200249#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200250# define USE_XATTRS
Benjamin Peterson9428d532011-09-14 11:45:52 -0400251#endif
252
253#ifdef USE_XATTRS
Victor Stinner5eca75d2020-04-15 15:07:31 +0200254# include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400255#endif
256
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000257#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200258# ifdef HAVE_SYS_SOCKET_H
259# include <sys/socket.h>
260# endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000261#endif
262
Victor Stinner8b905bd2011-10-25 13:34:04 +0200263#ifdef HAVE_DLFCN_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200264# include <dlfcn.h>
Victor Stinner8b905bd2011-10-25 13:34:04 +0200265#endif
266
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200267#ifdef __hpux
Victor Stinner5eca75d2020-04-15 15:07:31 +0200268# include <sys/mpctl.h>
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200269#endif
270
271#if defined(__DragonFly__) || \
272 defined(__OpenBSD__) || \
273 defined(__FreeBSD__) || \
274 defined(__NetBSD__) || \
275 defined(__APPLE__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200276# include <sys/sysctl.h>
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200277#endif
278
Victor Stinner9b1f4742016-09-06 16:18:52 -0700279#ifdef HAVE_LINUX_RANDOM_H
280# include <linux/random.h>
281#endif
282#ifdef HAVE_GETRANDOM_SYSCALL
283# include <sys/syscall.h>
284#endif
285
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100286#if defined(MS_WINDOWS)
287# define TERMSIZE_USE_CONIO
288#elif defined(HAVE_SYS_IOCTL_H)
289# include <sys/ioctl.h>
290# if defined(HAVE_TERMIOS_H)
291# include <termios.h>
292# endif
293# if defined(TIOCGWINSZ)
294# define TERMSIZE_USE_IOCTL
295# endif
296#endif /* MS_WINDOWS */
297
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000298/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000299/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000300#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Victor Stinner5eca75d2020-04-15 15:07:31 +0200301# define HAVE_OPENDIR 1
302# define HAVE_SYSTEM 1
303# include <process.h>
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000304#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200305# ifdef _MSC_VER
306 /* Microsoft compiler */
307# define HAVE_GETPPID 1
308# define HAVE_GETLOGIN 1
309# define HAVE_SPAWNV 1
310# define HAVE_EXECV 1
311# define HAVE_WSPAWNV 1
312# define HAVE_WEXECV 1
313# define HAVE_PIPE 1
314# define HAVE_SYSTEM 1
315# define HAVE_CWAIT 1
316# define HAVE_FSYNC 1
317# define fsync _commit
318# else
319 /* Unix functions that the configure script doesn't check for */
320# ifndef __VXWORKS__
321# define HAVE_EXECV 1
322# define HAVE_FORK 1
323# if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
324# define HAVE_FORK1 1
325# endif
326# endif
327# define HAVE_GETEGID 1
328# define HAVE_GETEUID 1
329# define HAVE_GETGID 1
330# define HAVE_GETPPID 1
331# define HAVE_GETUID 1
332# define HAVE_KILL 1
333# define HAVE_OPENDIR 1
334# define HAVE_PIPE 1
335# define HAVE_SYSTEM 1
336# define HAVE_WAIT 1
337# define HAVE_TTYNAME 1
338# endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000339#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000340
Eddie Elizondob3966632019-11-05 07:16:14 -0800341_Py_IDENTIFIER(__fspath__);
Victor Stinnera2f7c002012-02-08 03:36:25 +0100342
Larry Hastings61272b72014-01-07 12:41:53 -0800343/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000344# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800345module os
Larry Hastings61272b72014-01-07 12:41:53 -0800346[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000347/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100348
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000349#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000350
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000351#if defined(__sgi)&&_COMPILER_VERSION>=700
352/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
353 (default) */
354extern char *ctermid_r(char *);
355#endif
356
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000357#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358
pxinwrf2d7ac72019-05-21 18:46:37 +0800359#if defined(__VXWORKS__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200360# include <vxCpuLib.h>
361# include <rtpLib.h>
362# include <wait.h>
363# include <taskLib.h>
364# ifndef _P_WAIT
365# define _P_WAIT 0
366# define _P_NOWAIT 1
367# define _P_NOWAITO 1
368# endif
pxinwrf2d7ac72019-05-21 18:46:37 +0800369#endif /* __VXWORKS__ */
370
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000371#ifdef HAVE_POSIX_SPAWN
Victor Stinner5eca75d2020-04-15 15:07:31 +0200372# include <spawn.h>
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000373#endif
374
Guido van Rossumb6775db1994-08-01 11:34:53 +0000375#ifdef HAVE_UTIME_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200376# include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000377#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000378
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000379#ifdef HAVE_SYS_UTIME_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200380# include <sys/utime.h>
381# define HAVE_UTIME_H /* pretend we do for the rest of this file */
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000382#endif /* HAVE_SYS_UTIME_H */
383
Guido van Rossumb6775db1994-08-01 11:34:53 +0000384#ifdef HAVE_SYS_TIMES_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200385# include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000386#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000387
388#ifdef HAVE_SYS_PARAM_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200389# include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000390#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000391
392#ifdef HAVE_SYS_UTSNAME_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200393# include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000394#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000395
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000396#ifdef HAVE_DIRENT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200397# include <dirent.h>
398# define NAMLEN(dirent) strlen((dirent)->d_name)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000399#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200400# if defined(__WATCOMC__) && !defined(__QNX__)
401# include <direct.h>
402# define NAMLEN(dirent) strlen((dirent)->d_name)
403# else
404# define dirent direct
405# define NAMLEN(dirent) (dirent)->d_namlen
406# endif
407# ifdef HAVE_SYS_NDIR_H
408# include <sys/ndir.h>
409# endif
410# ifdef HAVE_SYS_DIR_H
411# include <sys/dir.h>
412# endif
413# ifdef HAVE_NDIR_H
414# include <ndir.h>
415# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000416#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000417
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000418#ifdef _MSC_VER
Victor Stinner5eca75d2020-04-15 15:07:31 +0200419# ifdef HAVE_DIRECT_H
420# include <direct.h>
421# endif
422# ifdef HAVE_IO_H
423# include <io.h>
424# endif
425# ifdef HAVE_PROCESS_H
426# include <process.h>
427# endif
428# ifndef IO_REPARSE_TAG_SYMLINK
429# define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
430# endif
431# ifndef IO_REPARSE_TAG_MOUNT_POINT
432# define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
433# endif
434# include "osdefs.h" // SEP
435# include <malloc.h>
436# include <windows.h>
437# include <shellapi.h> // ShellExecute()
438# include <lmcons.h> // UNLEN
439# define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000440#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000441
Tim Petersbc2e10e2002-03-03 23:17:02 +0000442#ifndef MAXPATHLEN
Victor Stinner5eca75d2020-04-15 15:07:31 +0200443# if defined(PATH_MAX) && PATH_MAX > 1024
444# define MAXPATHLEN PATH_MAX
445# else
446# define MAXPATHLEN 1024
447# endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000448#endif /* MAXPATHLEN */
449
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000450#ifdef UNION_WAIT
Victor Stinner5eca75d2020-04-15 15:07:31 +0200451 /* Emulate some macros on systems that have a union instead of macros */
452# ifndef WIFEXITED
453# define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
454# endif
455# ifndef WEXITSTATUS
456# define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
457# endif
458# ifndef WTERMSIG
459# define WTERMSIG(u_wait) ((u_wait).w_termsig)
460# endif
461# define WAIT_TYPE union wait
462# define WAIT_STATUS_INT(s) (s.w_status)
463#else
464 /* !UNION_WAIT */
465# define WAIT_TYPE int
466# define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000467#endif /* UNION_WAIT */
468
Greg Wardb48bc172000-03-01 21:51:56 +0000469/* Don't use the "_r" form if we don't need it (also, won't have a
470 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200471#if defined(HAVE_CTERMID_R)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200472# define USE_CTERMID_R
Greg Wardb48bc172000-03-01 21:51:56 +0000473#endif
474
Fred Drake699f3522000-06-29 21:12:41 +0000475/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000476#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000477#undef FSTAT
478#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200479#ifdef MS_WINDOWS
Victor Stinner5eca75d2020-04-15 15:07:31 +0200480# define STAT win32_stat
481# define LSTAT win32_lstat
482# define FSTAT _Py_fstat_noraise
483# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000484#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200485# define STAT stat
486# define LSTAT lstat
487# define FSTAT fstat
488# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000489#endif
490
Tim Peters11b23062003-04-23 02:39:17 +0000491#if defined(MAJOR_IN_MKDEV)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200492# include <sys/mkdev.h>
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000493#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200494# if defined(MAJOR_IN_SYSMACROS)
495# include <sys/sysmacros.h>
496# endif
497# if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
498# include <sys/mkdev.h>
499# endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000500#endif
Fred Drake699f3522000-06-29 21:12:41 +0000501
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200502#ifdef MS_WINDOWS
Victor Stinner5eca75d2020-04-15 15:07:31 +0200503# define INITFUNC PyInit_nt
504# define MODNAME "nt"
Victor Stinner6036e442015-03-08 01:58:04 +0100505#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200506# define INITFUNC PyInit_posix
507# define MODNAME "posix"
Victor Stinner6036e442015-03-08 01:58:04 +0100508#endif
509
jcea6c51d512018-01-28 14:00:08 +0100510#if defined(__sun)
511/* Something to implement in autoconf, not present in autoconf 2.69 */
Victor Stinner5eca75d2020-04-15 15:07:31 +0200512# define HAVE_STRUCT_STAT_ST_FSTYPE 1
jcea6c51d512018-01-28 14:00:08 +0100513#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200514
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600515/* memfd_create is either defined in sys/mman.h or sys/memfd.h
516 * linux/memfd.h defines additional flags
517 */
518#ifdef HAVE_SYS_MMAN_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200519# include <sys/mman.h>
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600520#endif
521#ifdef HAVE_SYS_MEMFD_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200522# include <sys/memfd.h>
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600523#endif
524#ifdef HAVE_LINUX_MEMFD_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200525# include <linux/memfd.h>
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600526#endif
527
Christian Heimescd9fed62020-11-13 19:48:52 +0100528/* eventfd() */
529#ifdef HAVE_SYS_EVENTFD_H
530# include <sys/eventfd.h>
531#endif
532
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800533#ifdef _Py_MEMORY_SANITIZER
Victor Stinner5eca75d2020-04-15 15:07:31 +0200534# include <sanitizer/msan_interface.h>
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800535#endif
536
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200537#ifdef HAVE_FORK
538static void
539run_at_forkers(PyObject *lst, int reverse)
540{
541 Py_ssize_t i;
542 PyObject *cpy;
543
544 if (lst != NULL) {
545 assert(PyList_CheckExact(lst));
546
547 /* Use a list copy in case register_at_fork() is called from
548 * one of the callbacks.
549 */
550 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
551 if (cpy == NULL)
552 PyErr_WriteUnraisable(lst);
553 else {
554 if (reverse)
555 PyList_Reverse(cpy);
556 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
557 PyObject *func, *res;
558 func = PyList_GET_ITEM(cpy, i);
Jeroen Demeyer7f41c8e2019-07-04 12:35:31 +0200559 res = _PyObject_CallNoArg(func);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200560 if (res == NULL)
561 PyErr_WriteUnraisable(func);
562 else
563 Py_DECREF(res);
564 }
565 Py_DECREF(cpy);
566 }
567 }
568}
569
570void
571PyOS_BeforeFork(void)
572{
Victor Stinner81a7be32020-04-14 15:14:01 +0200573 run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200574
575 _PyImport_AcquireLock();
576}
577
578void
579PyOS_AfterFork_Parent(void)
580{
581 if (_PyImport_ReleaseLock() <= 0)
582 Py_FatalError("failed releasing import lock after fork");
583
Victor Stinner81a7be32020-04-14 15:14:01 +0200584 run_at_forkers(_PyInterpreterState_GET()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200585}
586
587void
588PyOS_AfterFork_Child(void)
589{
Victor Stinner26881c82020-06-02 15:51:37 +0200590 PyStatus status;
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200591 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner26881c82020-06-02 15:51:37 +0200592
593 status = _PyGILState_Reinit(runtime);
594 if (_PyStatus_EXCEPTION(status)) {
595 goto fatal_error;
596 }
597
Victor Stinner317bab02020-06-02 18:44:54 +0200598 PyThreadState *tstate = _PyThreadState_GET();
599 _Py_EnsureTstateNotNULL(tstate);
600
601 status = _PyEval_ReInitThreads(tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200602 if (_PyStatus_EXCEPTION(status)) {
603 goto fatal_error;
604 }
605
606 status = _PyImport_ReInitLock();
607 if (_PyStatus_EXCEPTION(status)) {
608 goto fatal_error;
609 }
610
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200611 _PySignal_AfterFork();
Victor Stinner26881c82020-06-02 15:51:37 +0200612
613 status = _PyRuntimeState_ReInitThreads(runtime);
614 if (_PyStatus_EXCEPTION(status)) {
615 goto fatal_error;
616 }
617
618 status = _PyInterpreterState_DeleteExceptMain(runtime);
619 if (_PyStatus_EXCEPTION(status)) {
620 goto fatal_error;
621 }
Victor Stinner317bab02020-06-02 18:44:54 +0200622 assert(_PyThreadState_GET() == tstate);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200623
Victor Stinner317bab02020-06-02 18:44:54 +0200624 run_at_forkers(tstate->interp->after_forkers_child, 0);
Victor Stinner26881c82020-06-02 15:51:37 +0200625 return;
626
627fatal_error:
628 Py_ExitStatusException(status);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200629}
630
631static int
632register_at_forker(PyObject **lst, PyObject *func)
633{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700634 if (func == NULL) /* nothing to register? do nothing. */
635 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200636 if (*lst == NULL) {
637 *lst = PyList_New(0);
638 if (*lst == NULL)
639 return -1;
640 }
641 return PyList_Append(*lst, func);
642}
Victor Stinner87255be2020-04-07 23:11:49 +0200643#endif /* HAVE_FORK */
644
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200645
646/* Legacy wrapper */
647void
648PyOS_AfterFork(void)
649{
650#ifdef HAVE_FORK
651 PyOS_AfterFork_Child();
652#endif
653}
654
655
Victor Stinner6036e442015-03-08 01:58:04 +0100656#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200657/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700658void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
659void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200660 ULONG, struct _Py_stat_struct *);
661#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700662
Larry Hastings9cf065c2012-06-22 16:30:09 -0700663
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200664#ifndef MS_WINDOWS
665PyObject *
666_PyLong_FromUid(uid_t uid)
667{
668 if (uid == (uid_t)-1)
669 return PyLong_FromLong(-1);
670 return PyLong_FromUnsignedLong(uid);
671}
672
673PyObject *
674_PyLong_FromGid(gid_t gid)
675{
676 if (gid == (gid_t)-1)
677 return PyLong_FromLong(-1);
678 return PyLong_FromUnsignedLong(gid);
679}
680
681int
Jakub Kulík0159e5e2020-12-29 13:58:27 +0100682_Py_Uid_Converter(PyObject *obj, uid_t *p)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200683{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700684 uid_t uid;
685 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200686 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200687 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700688 unsigned long uresult;
689
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300690 index = _PyNumber_Index(obj);
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700691 if (index == NULL) {
692 PyErr_Format(PyExc_TypeError,
693 "uid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800694 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200695 return 0;
696 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700697
698 /*
699 * Handling uid_t is complicated for two reasons:
700 * * Although uid_t is (always?) unsigned, it still
701 * accepts -1.
702 * * We don't know its size in advance--it may be
703 * bigger than an int, or it may be smaller than
704 * a long.
705 *
706 * So a bit of defensive programming is in order.
707 * Start with interpreting the value passed
708 * in as a signed long and see if it works.
709 */
710
711 result = PyLong_AsLongAndOverflow(index, &overflow);
712
713 if (!overflow) {
714 uid = (uid_t)result;
715
716 if (result == -1) {
717 if (PyErr_Occurred())
718 goto fail;
719 /* It's a legitimate -1, we're done. */
720 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200721 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700722
723 /* Any other negative number is disallowed. */
724 if (result < 0)
725 goto underflow;
726
727 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200728 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700729 (long)uid != result)
730 goto underflow;
731 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200732 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700733
734 if (overflow < 0)
735 goto underflow;
736
737 /*
738 * Okay, the value overflowed a signed long. If it
739 * fits in an *unsigned* long, it may still be okay,
740 * as uid_t may be unsigned long on this platform.
741 */
742 uresult = PyLong_AsUnsignedLong(index);
743 if (PyErr_Occurred()) {
744 if (PyErr_ExceptionMatches(PyExc_OverflowError))
745 goto overflow;
746 goto fail;
747 }
748
749 uid = (uid_t)uresult;
750
751 /*
752 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
753 * but this value would get interpreted as (uid_t)-1 by chown
754 * and its siblings. That's not what the user meant! So we
755 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100756 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700757 */
758 if (uid == (uid_t)-1)
759 goto overflow;
760
761 /* Ensure the value wasn't truncated. */
762 if (sizeof(uid_t) < sizeof(long) &&
763 (unsigned long)uid != uresult)
764 goto overflow;
765 /* fallthrough */
766
767success:
768 Py_DECREF(index);
Jakub Kulík0159e5e2020-12-29 13:58:27 +0100769 *p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200770 return 1;
771
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700772underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200773 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700774 "uid is less than minimum");
775 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200776
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700777overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200778 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700779 "uid is greater than maximum");
780 /* fallthrough */
781
782fail:
783 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200784 return 0;
785}
786
787int
Jakub Kulík0159e5e2020-12-29 13:58:27 +0100788_Py_Gid_Converter(PyObject *obj, gid_t *p)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200789{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700790 gid_t gid;
791 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200792 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200793 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700794 unsigned long uresult;
795
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300796 index = _PyNumber_Index(obj);
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700797 if (index == NULL) {
798 PyErr_Format(PyExc_TypeError,
799 "gid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800800 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200801 return 0;
802 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700803
804 /*
805 * Handling gid_t is complicated for two reasons:
806 * * Although gid_t is (always?) unsigned, it still
807 * accepts -1.
808 * * We don't know its size in advance--it may be
809 * bigger than an int, or it may be smaller than
810 * a long.
811 *
812 * So a bit of defensive programming is in order.
813 * Start with interpreting the value passed
814 * in as a signed long and see if it works.
815 */
816
817 result = PyLong_AsLongAndOverflow(index, &overflow);
818
819 if (!overflow) {
820 gid = (gid_t)result;
821
822 if (result == -1) {
823 if (PyErr_Occurred())
824 goto fail;
825 /* It's a legitimate -1, we're done. */
826 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200827 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700828
829 /* Any other negative number is disallowed. */
830 if (result < 0) {
831 goto underflow;
832 }
833
834 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200835 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700836 (long)gid != result)
837 goto underflow;
838 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200839 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700840
841 if (overflow < 0)
842 goto underflow;
843
844 /*
845 * Okay, the value overflowed a signed long. If it
846 * fits in an *unsigned* long, it may still be okay,
847 * as gid_t may be unsigned long on this platform.
848 */
849 uresult = PyLong_AsUnsignedLong(index);
850 if (PyErr_Occurred()) {
851 if (PyErr_ExceptionMatches(PyExc_OverflowError))
852 goto overflow;
853 goto fail;
854 }
855
856 gid = (gid_t)uresult;
857
858 /*
859 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
860 * but this value would get interpreted as (gid_t)-1 by chown
861 * and its siblings. That's not what the user meant! So we
862 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100863 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700864 */
865 if (gid == (gid_t)-1)
866 goto overflow;
867
868 /* Ensure the value wasn't truncated. */
869 if (sizeof(gid_t) < sizeof(long) &&
870 (unsigned long)gid != uresult)
871 goto overflow;
872 /* fallthrough */
873
874success:
875 Py_DECREF(index);
Jakub Kulík0159e5e2020-12-29 13:58:27 +0100876 *p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200877 return 1;
878
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700879underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200880 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700881 "gid is less than minimum");
882 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200883
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700884overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200885 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700886 "gid is greater than maximum");
887 /* fallthrough */
888
889fail:
890 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200891 return 0;
892}
893#endif /* MS_WINDOWS */
894
895
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700896#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800897
898
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200899#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
900static int
901_Py_Dev_Converter(PyObject *obj, void *p)
902{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200903 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200904 if (PyErr_Occurred())
905 return 0;
906 return 1;
907}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800908#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200909
910
Larry Hastings9cf065c2012-06-22 16:30:09 -0700911#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400912/*
913 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
914 * without the int cast, the value gets interpreted as uint (4291925331),
915 * which doesn't play nicely with all the initializer lines in this file that
916 * look like this:
917 * int dir_fd = DEFAULT_DIR_FD;
918 */
919#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700920#else
921#define DEFAULT_DIR_FD (-100)
922#endif
923
924static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300925_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200926{
927 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700928 long long_value;
929
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300930 PyObject *index = _PyNumber_Index(o);
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700931 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700932 return 0;
933 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700934
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300935 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700936 long_value = PyLong_AsLongAndOverflow(index, &overflow);
937 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300938 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200939 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700940 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700941 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700942 return 0;
943 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200944 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700945 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700946 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700947 return 0;
948 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700949
Larry Hastings9cf065c2012-06-22 16:30:09 -0700950 *p = (int)long_value;
951 return 1;
952}
953
954static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200955dir_fd_converter(PyObject *o, void *p)
956{
957 if (o == Py_None) {
958 *(int *)p = DEFAULT_DIR_FD;
959 return 1;
960 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300961 else if (PyIndex_Check(o)) {
962 return _fd_converter(o, (int *)p);
963 }
964 else {
965 PyErr_Format(PyExc_TypeError,
966 "argument should be integer or None, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800967 _PyType_Name(Py_TYPE(o)));
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300968 return 0;
969 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700970}
971
Eddie Elizondob3966632019-11-05 07:16:14 -0800972typedef struct {
973 PyObject *billion;
Eddie Elizondob3966632019-11-05 07:16:14 -0800974 PyObject *DirEntryType;
975 PyObject *ScandirIteratorType;
976#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
977 PyObject *SchedParamType;
978#endif
979 PyObject *StatResultType;
980 PyObject *StatVFSResultType;
981 PyObject *TerminalSizeType;
982 PyObject *TimesResultType;
983 PyObject *UnameResultType;
984#if defined(HAVE_WAITID) && !defined(__APPLE__)
985 PyObject *WaitidResultType;
986#endif
987#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
988 PyObject *struct_rusage;
989#endif
990 PyObject *st_mode;
991} _posixstate;
992
Eddie Elizondob3966632019-11-05 07:16:14 -0800993
Hai Shif707d942020-03-16 21:15:01 +0800994static inline _posixstate*
995get_posix_state(PyObject *module)
996{
997 void *state = PyModule_GetState(module);
998 assert(state != NULL);
999 return (_posixstate *)state;
1000}
1001
Larry Hastings9cf065c2012-06-22 16:30:09 -07001002/*
1003 * A PyArg_ParseTuple "converter" function
1004 * that handles filesystem paths in the manner
1005 * preferred by the os module.
1006 *
1007 * path_converter accepts (Unicode) strings and their
1008 * subclasses, and bytes and their subclasses. What
1009 * it does with the argument depends on the platform:
1010 *
1011 * * On Windows, if we get a (Unicode) string we
1012 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -07001013 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001014 *
1015 * * On all other platforms, strings are encoded
1016 * to bytes using PyUnicode_FSConverter, then we
1017 * extract the char * from the bytes object and
1018 * return that.
1019 *
1020 * path_converter also optionally accepts signed
1021 * integers (representing open file descriptors) instead
1022 * of path strings.
1023 *
1024 * Input fields:
1025 * path.nullable
1026 * If nonzero, the path is permitted to be None.
1027 * path.allow_fd
1028 * If nonzero, the path is permitted to be a file handle
1029 * (a signed int) instead of a string.
1030 * path.function_name
1031 * If non-NULL, path_converter will use that as the name
1032 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -07001033 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001034 * path.argument_name
1035 * If non-NULL, path_converter will use that as the name
1036 * of the parameter in error messages.
1037 * (If path.argument_name is NULL it uses "path".)
1038 *
1039 * Output fields:
1040 * path.wide
1041 * Points to the path if it was expressed as Unicode
1042 * and was not encoded. (Only used on Windows.)
1043 * path.narrow
1044 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -07001045 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +00001046 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -07001047 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001048 * path.fd
1049 * Contains a file descriptor if path.accept_fd was true
1050 * and the caller provided a signed integer instead of any
1051 * sort of string.
1052 *
1053 * WARNING: if your "path" parameter is optional, and is
1054 * unspecified, path_converter will never get called.
1055 * So if you set allow_fd, you *MUST* initialize path.fd = -1
1056 * yourself!
1057 * path.length
1058 * The length of the path in characters, if specified as
1059 * a string.
1060 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +08001061 * The original object passed in (if get a PathLike object,
1062 * the result of PyOS_FSPath() is treated as the original object).
1063 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001064 * path.cleanup
1065 * For internal use only. May point to a temporary object.
1066 * (Pay no attention to the man behind the curtain.)
1067 *
1068 * At most one of path.wide or path.narrow will be non-NULL.
1069 * If path was None and path.nullable was set,
1070 * or if path was an integer and path.allow_fd was set,
1071 * both path.wide and path.narrow will be NULL
1072 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +02001073 *
Larry Hastings9cf065c2012-06-22 16:30:09 -07001074 * path_converter takes care to not write to the path_t
1075 * unless it's successful. However it must reset the
1076 * "cleanup" field each time it's called.
1077 *
1078 * Use as follows:
1079 * path_t path;
1080 * memset(&path, 0, sizeof(path));
1081 * PyArg_ParseTuple(args, "O&", path_converter, &path);
1082 * // ... use values from path ...
1083 * path_cleanup(&path);
1084 *
1085 * (Note that if PyArg_Parse fails you don't need to call
1086 * path_cleanup(). However it is safe to do so.)
1087 */
1088typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +01001089 const char *function_name;
1090 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001091 int nullable;
1092 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001093 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -07001094#ifdef MS_WINDOWS
1095 BOOL narrow;
1096#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001097 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -07001098#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001099 int fd;
1100 Py_ssize_t length;
1101 PyObject *object;
1102 PyObject *cleanup;
1103} path_t;
1104
Steve Dowercc16be82016-09-08 10:35:16 -07001105#ifdef MS_WINDOWS
1106#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
1107 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
1108#else
Larry Hastings2f936352014-08-05 14:04:04 +10001109#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
1110 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -07001111#endif
Larry Hastings31826802013-10-19 00:09:25 -07001112
Larry Hastings9cf065c2012-06-22 16:30:09 -07001113static void
Xiang Zhang04316c42017-01-08 23:26:57 +08001114path_cleanup(path_t *path)
1115{
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001116#if !USE_UNICODE_WCHAR_CACHE
1117 wchar_t *wide = (wchar_t *)path->wide;
1118 path->wide = NULL;
1119 PyMem_Free(wide);
1120#endif /* USE_UNICODE_WCHAR_CACHE */
Xiang Zhang04316c42017-01-08 23:26:57 +08001121 Py_CLEAR(path->object);
1122 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001123}
1124
1125static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001126path_converter(PyObject *o, void *p)
1127{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001128 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +08001129 PyObject *bytes = NULL;
1130 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001131 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001132 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -07001133#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +08001134 PyObject *wo = NULL;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001135 wchar_t *wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001136#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001137
1138#define FORMAT_EXCEPTION(exc, fmt) \
1139 PyErr_Format(exc, "%s%s" fmt, \
1140 path->function_name ? path->function_name : "", \
1141 path->function_name ? ": " : "", \
1142 path->argument_name ? path->argument_name : "path")
1143
1144 /* Py_CLEANUP_SUPPORTED support */
1145 if (o == NULL) {
1146 path_cleanup(path);
1147 return 1;
1148 }
1149
Brett Cannon3f9183b2016-08-26 14:44:48 -07001150 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +08001151 path->object = path->cleanup = NULL;
1152 /* path->object owns a reference to the original object */
1153 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001154
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001155 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001156 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001157#ifdef MS_WINDOWS
1158 path->narrow = FALSE;
1159#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001160 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001161#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001162 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001163 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001164 }
1165
Brett Cannon3f9183b2016-08-26 14:44:48 -07001166 /* Only call this here so that we don't treat the return value of
1167 os.fspath() as an fd or buffer. */
1168 is_index = path->allow_fd && PyIndex_Check(o);
1169 is_buffer = PyObject_CheckBuffer(o);
1170 is_bytes = PyBytes_Check(o);
1171 is_unicode = PyUnicode_Check(o);
1172
1173 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
1174 /* Inline PyOS_FSPath() for better error messages. */
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001175 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001176
1177 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
1178 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001179 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001180 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001181 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001182 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001183 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001184 goto error_exit;
1185 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001186 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001187 is_unicode = 1;
1188 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001189 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001190 is_bytes = 1;
1191 }
1192 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001193 PyErr_Format(PyExc_TypeError,
1194 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -08001195 "not %.200s", _PyType_Name(Py_TYPE(o)),
1196 _PyType_Name(Py_TYPE(res)));
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001197 Py_DECREF(res);
1198 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001199 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001200
1201 /* still owns a reference to the original object */
1202 Py_DECREF(o);
1203 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001204 }
1205
1206 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001207#ifdef MS_WINDOWS
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001208#if USE_UNICODE_WCHAR_CACHE
1209_Py_COMP_DIAG_PUSH
1210_Py_COMP_DIAG_IGNORE_DEPR_DECLS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001211 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001212_Py_COMP_DIAG_POP
1213#else /* USE_UNICODE_WCHAR_CACHE */
1214 wide = PyUnicode_AsWideCharString(o, &length);
1215#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner59799a82013-11-13 14:17:30 +01001216 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001217 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001218 }
Victor Stinner59799a82013-11-13 14:17:30 +01001219 if (length > 32767) {
1220 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001221 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001222 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001223 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001224 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001225 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001226 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001227
1228 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001229 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001230 path->fd = -1;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001231#if !USE_UNICODE_WCHAR_CACHE
1232 wide = NULL;
1233#endif /* USE_UNICODE_WCHAR_CACHE */
Xiang Zhang04316c42017-01-08 23:26:57 +08001234 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001235#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001236 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001237 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001238 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001239#endif
1240 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001241 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001242 bytes = o;
1243 Py_INCREF(bytes);
1244 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001245 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001246 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001247 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001248 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1249 "%s%s%s should be %s, not %.200s",
1250 path->function_name ? path->function_name : "",
1251 path->function_name ? ": " : "",
1252 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001253 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1254 "integer or None" :
1255 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1256 path->nullable ? "string, bytes, os.PathLike or None" :
1257 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001258 _PyType_Name(Py_TYPE(o)))) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001259 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001260 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001261 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001262 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001263 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001264 }
1265 }
Steve Dowercc16be82016-09-08 10:35:16 -07001266 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001267 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001268 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001269 }
1270 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001271#ifdef MS_WINDOWS
1272 path->narrow = FALSE;
1273#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001274 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001275#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001276 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001277 }
1278 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001279 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001280 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1281 path->function_name ? path->function_name : "",
1282 path->function_name ? ": " : "",
1283 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001284 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1285 "integer or None" :
1286 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1287 path->nullable ? "string, bytes, os.PathLike or None" :
1288 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001289 _PyType_Name(Py_TYPE(o)));
Xiang Zhang04316c42017-01-08 23:26:57 +08001290 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001291 }
1292
Larry Hastings9cf065c2012-06-22 16:30:09 -07001293 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001294 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001295 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001296 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001297 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001298 }
1299
Steve Dowercc16be82016-09-08 10:35:16 -07001300#ifdef MS_WINDOWS
1301 wo = PyUnicode_DecodeFSDefaultAndSize(
1302 narrow,
1303 length
1304 );
1305 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001306 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001307 }
1308
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001309#if USE_UNICODE_WCHAR_CACHE
1310_Py_COMP_DIAG_PUSH
1311_Py_COMP_DIAG_IGNORE_DEPR_DECLS
Xiang Zhang04316c42017-01-08 23:26:57 +08001312 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001313_Py_COMP_DIAG_POP
1314#else /* USE_UNICODE_WCHAR_CACHE */
1315 wide = PyUnicode_AsWideCharString(wo, &length);
1316 Py_DECREF(wo);
1317#endif /* USE_UNICODE_WCHAR_CACHE */
Steve Dowercc16be82016-09-08 10:35:16 -07001318 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001319 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001320 }
1321 if (length > 32767) {
1322 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001323 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001324 }
1325 if (wcslen(wide) != length) {
1326 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001327 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001328 }
1329 path->wide = wide;
1330 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001331 Py_DECREF(bytes);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001332#if USE_UNICODE_WCHAR_CACHE
1333 path->cleanup = wo;
1334#else /* USE_UNICODE_WCHAR_CACHE */
1335 wide = NULL;
1336#endif /* USE_UNICODE_WCHAR_CACHE */
Steve Dowercc16be82016-09-08 10:35:16 -07001337#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001338 path->wide = NULL;
1339 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001340 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001341 /* Still a reference owned by path->object, don't have to
1342 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001343 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001344 }
1345 else {
1346 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001347 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001348#endif
1349 path->fd = -1;
1350
1351 success_exit:
1352 path->length = length;
1353 path->object = o;
1354 return Py_CLEANUP_SUPPORTED;
1355
1356 error_exit:
1357 Py_XDECREF(o);
1358 Py_XDECREF(bytes);
1359#ifdef MS_WINDOWS
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001360#if USE_UNICODE_WCHAR_CACHE
Xiang Zhang04316c42017-01-08 23:26:57 +08001361 Py_XDECREF(wo);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001362#else /* USE_UNICODE_WCHAR_CACHE */
1363 PyMem_Free(wide);
1364#endif /* USE_UNICODE_WCHAR_CACHE */
Xiang Zhang04316c42017-01-08 23:26:57 +08001365#endif
1366 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001367}
1368
1369static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001370argument_unavailable_error(const char *function_name, const char *argument_name)
1371{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001372 PyErr_Format(PyExc_NotImplementedError,
1373 "%s%s%s unavailable on this platform",
1374 (function_name != NULL) ? function_name : "",
1375 (function_name != NULL) ? ": ": "",
1376 argument_name);
1377}
1378
1379static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001380dir_fd_unavailable(PyObject *o, void *p)
1381{
1382 int dir_fd;
1383 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001384 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001385 if (dir_fd != DEFAULT_DIR_FD) {
1386 argument_unavailable_error(NULL, "dir_fd");
1387 return 0;
1388 }
1389 *(int *)p = dir_fd;
1390 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001391}
1392
1393static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001394fd_specified(const char *function_name, int fd)
1395{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001396 if (fd == -1)
1397 return 0;
1398
1399 argument_unavailable_error(function_name, "fd");
1400 return 1;
1401}
1402
1403static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001404follow_symlinks_specified(const char *function_name, int follow_symlinks)
1405{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001406 if (follow_symlinks)
1407 return 0;
1408
1409 argument_unavailable_error(function_name, "follow_symlinks");
1410 return 1;
1411}
1412
1413static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001414path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1415{
Steve Dowercc16be82016-09-08 10:35:16 -07001416 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1417#ifndef MS_WINDOWS
1418 && !path->narrow
1419#endif
1420 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001421 PyErr_Format(PyExc_ValueError,
1422 "%s: can't specify dir_fd without matching path",
1423 function_name);
1424 return 1;
1425 }
1426 return 0;
1427}
1428
1429static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001430dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1431{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001432 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1433 PyErr_Format(PyExc_ValueError,
1434 "%s: can't specify both dir_fd and fd",
1435 function_name);
1436 return 1;
1437 }
1438 return 0;
1439}
1440
1441static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001442fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1443 int follow_symlinks)
1444{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001445 if ((fd > 0) && (!follow_symlinks)) {
1446 PyErr_Format(PyExc_ValueError,
1447 "%s: cannot use fd and follow_symlinks together",
1448 function_name);
1449 return 1;
1450 }
1451 return 0;
1452}
1453
1454static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001455dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1456 int follow_symlinks)
1457{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001458 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1459 PyErr_Format(PyExc_ValueError,
1460 "%s: cannot use dir_fd and follow_symlinks together",
1461 function_name);
1462 return 1;
1463 }
1464 return 0;
1465}
1466
Larry Hastings2f936352014-08-05 14:04:04 +10001467#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001468 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001469#else
Larry Hastings2f936352014-08-05 14:04:04 +10001470 typedef off_t Py_off_t;
1471#endif
1472
1473static int
1474Py_off_t_converter(PyObject *arg, void *addr)
1475{
1476#ifdef HAVE_LARGEFILE_SUPPORT
1477 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1478#else
1479 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001480#endif
1481 if (PyErr_Occurred())
1482 return 0;
1483 return 1;
1484}
Larry Hastings2f936352014-08-05 14:04:04 +10001485
1486static PyObject *
1487PyLong_FromPy_off_t(Py_off_t offset)
1488{
1489#ifdef HAVE_LARGEFILE_SUPPORT
1490 return PyLong_FromLongLong(offset);
1491#else
1492 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001493#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001494}
1495
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001496#ifdef HAVE_SIGSET_T
1497/* Convert an iterable of integers to a sigset.
1498 Return 1 on success, return 0 and raise an exception on error. */
1499int
1500_Py_Sigset_Converter(PyObject *obj, void *addr)
1501{
1502 sigset_t *mask = (sigset_t *)addr;
1503 PyObject *iterator, *item;
1504 long signum;
1505 int overflow;
1506
Rémi Lapeyref0900192019-05-04 01:30:53 +02001507 // The extra parens suppress the unreachable-code warning with clang on MacOS
1508 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001509 /* Probably only if mask == NULL. */
1510 PyErr_SetFromErrno(PyExc_OSError);
1511 return 0;
1512 }
1513
1514 iterator = PyObject_GetIter(obj);
1515 if (iterator == NULL) {
1516 return 0;
1517 }
1518
1519 while ((item = PyIter_Next(iterator)) != NULL) {
1520 signum = PyLong_AsLongAndOverflow(item, &overflow);
1521 Py_DECREF(item);
1522 if (signum <= 0 || signum >= NSIG) {
1523 if (overflow || signum != -1 || !PyErr_Occurred()) {
1524 PyErr_Format(PyExc_ValueError,
1525 "signal number %ld out of range", signum);
1526 }
1527 goto error;
1528 }
1529 if (sigaddset(mask, (int)signum)) {
1530 if (errno != EINVAL) {
1531 /* Probably impossible */
1532 PyErr_SetFromErrno(PyExc_OSError);
1533 goto error;
1534 }
1535 /* For backwards compatibility, allow idioms such as
1536 * `range(1, NSIG)` but warn about invalid signal numbers
1537 */
1538 const char msg[] =
1539 "invalid signal number %ld, please use valid_signals()";
1540 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1541 goto error;
1542 }
1543 }
1544 }
1545 if (!PyErr_Occurred()) {
1546 Py_DECREF(iterator);
1547 return 1;
1548 }
1549
1550error:
1551 Py_DECREF(iterator);
1552 return 0;
1553}
1554#endif /* HAVE_SIGSET_T */
1555
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001556#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001557
1558static int
Brian Curtind25aef52011-06-13 15:16:04 -05001559win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001560{
Martin Panter70214ad2016-08-04 02:38:59 +00001561 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1562 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001563 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001564
1565 if (0 == DeviceIoControl(
1566 reparse_point_handle,
1567 FSCTL_GET_REPARSE_POINT,
1568 NULL, 0, /* in buffer */
1569 target_buffer, sizeof(target_buffer),
1570 &n_bytes_returned,
1571 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001572 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001573
1574 if (reparse_tag)
1575 *reparse_tag = rdb->ReparseTag;
1576
Brian Curtind25aef52011-06-13 15:16:04 -05001577 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001578}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001579
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001580#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001581
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001582/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001583#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001584/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001585** environ directly, we must obtain it with _NSGetEnviron(). See also
1586** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001587*/
1588#include <crt_externs.h>
pxinwrf2d7ac72019-05-21 18:46:37 +08001589#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001590extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001591#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001592
Barry Warsaw53699e91996-12-10 23:23:01 +00001593static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001594convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001595{
Victor Stinner8c62be82010-05-06 00:08:46 +00001596 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001597#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001598 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001599#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001600 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001601#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001602
Victor Stinner8c62be82010-05-06 00:08:46 +00001603 d = PyDict_New();
1604 if (d == NULL)
1605 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001606#ifdef MS_WINDOWS
1607 /* _wenviron must be initialized in this way if the program is started
1608 through main() instead of wmain(). */
1609 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001610 e = _wenviron;
Benoit Hudson723f71a2019-12-06 14:15:03 -05001611#elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1612 /* environ is not accessible as an extern in a shared object on OSX; use
1613 _NSGetEnviron to resolve it. The value changes if you add environment
1614 variables between calls to Py_Initialize, so don't cache the value. */
1615 e = *_NSGetEnviron();
Victor Stinner8c62be82010-05-06 00:08:46 +00001616#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001617 e = environ;
1618#endif
1619 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001620 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001621 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 PyObject *k;
1623 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001624#ifdef MS_WINDOWS
1625 const wchar_t *p = wcschr(*e, L'=');
1626#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001627 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001628#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001629 if (p == NULL)
1630 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001631#ifdef MS_WINDOWS
1632 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1633#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001634 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001635#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001636 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001637 Py_DECREF(d);
1638 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001639 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001640#ifdef MS_WINDOWS
1641 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1642#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001643 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001644#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001645 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001646 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001647 Py_DECREF(d);
1648 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001649 }
Serhiy Storchakab510e102020-10-26 12:47:57 +02001650 if (PyDict_SetDefault(d, k, v) == NULL) {
1651 Py_DECREF(v);
1652 Py_DECREF(k);
1653 Py_DECREF(d);
1654 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001655 }
1656 Py_DECREF(k);
1657 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001658 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001659 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001660}
1661
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001662/* Set a POSIX-specific error from errno, and return NULL */
1663
Barry Warsawd58d7641998-07-23 16:14:40 +00001664static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001665posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001666{
Victor Stinner8c62be82010-05-06 00:08:46 +00001667 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001668}
Mark Hammondef8b6542001-05-13 08:04:26 +00001669
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001670#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001671static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001672win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001673{
Victor Stinner8c62be82010-05-06 00:08:46 +00001674 /* XXX We should pass the function name along in the future.
1675 (winreg.c also wants to pass the function name.)
1676 This would however require an additional param to the
1677 Windows error object, which is non-trivial.
1678 */
1679 errno = GetLastError();
1680 if (filename)
1681 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1682 else
1683 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001684}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001685
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001686static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001687win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001688{
1689 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001690 if (filename)
1691 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001692 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001693 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001694 filename);
1695 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001696 return PyErr_SetFromWindowsErr(err);
1697}
1698
1699static PyObject *
1700win32_error_object(const char* function, PyObject* filename)
1701{
1702 errno = GetLastError();
1703 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001704}
1705
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001706#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001707
Larry Hastings9cf065c2012-06-22 16:30:09 -07001708static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001709posix_path_object_error(PyObject *path)
1710{
1711 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1712}
1713
1714static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001715path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001716{
1717#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001718 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1719 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001720#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001721 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001722#endif
1723}
1724
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001725static PyObject *
1726path_object_error2(PyObject *path, PyObject *path2)
1727{
1728#ifdef MS_WINDOWS
1729 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1730 PyExc_OSError, 0, path, path2);
1731#else
1732 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1733#endif
1734}
1735
1736static PyObject *
1737path_error(path_t *path)
1738{
1739 return path_object_error(path->object);
1740}
Larry Hastings31826802013-10-19 00:09:25 -07001741
Larry Hastingsb0827312014-02-09 22:05:19 -08001742static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001743posix_path_error(path_t *path)
1744{
1745 return posix_path_object_error(path->object);
1746}
1747
1748static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001749path_error2(path_t *path, path_t *path2)
1750{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001751 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001752}
1753
1754
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001755/* POSIX generic methods */
1756
Larry Hastings2f936352014-08-05 14:04:04 +10001757static PyObject *
1758posix_fildes_fd(int fd, int (*func)(int))
1759{
1760 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001761 int async_err = 0;
1762
1763 do {
1764 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001765 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001766 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001767 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001768 Py_END_ALLOW_THREADS
1769 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1770 if (res != 0)
1771 return (!async_err) ? posix_error() : NULL;
1772 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001773}
Guido van Rossum21142a01999-01-08 21:05:37 +00001774
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001775
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001776#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001777/* This is a reimplementation of the C library's chdir function,
1778 but one that produces Win32 errors instead of DOS error codes.
1779 chdir is essentially a wrapper around SetCurrentDirectory; however,
1780 it also needs to set "magic" environment variables indicating
1781 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001782static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001783win32_wchdir(LPCWSTR path)
1784{
Victor Stinnered537822015-12-13 21:40:26 +01001785 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001786 int result;
1787 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001788
Victor Stinner8c62be82010-05-06 00:08:46 +00001789 if(!SetCurrentDirectoryW(path))
1790 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001791 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 if (!result)
1793 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001794 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001795 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001796 if (!new_path) {
1797 SetLastError(ERROR_OUTOFMEMORY);
1798 return FALSE;
1799 }
1800 result = GetCurrentDirectoryW(result, new_path);
1801 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001802 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001803 return FALSE;
1804 }
1805 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001806 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1807 wcsncmp(new_path, L"//", 2) == 0);
1808 if (!is_unc_like_path) {
1809 env[1] = new_path[0];
1810 result = SetEnvironmentVariableW(env, new_path);
1811 }
Victor Stinnered537822015-12-13 21:40:26 +01001812 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001813 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001814 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001815}
1816#endif
1817
Martin v. Löwis14694662006-02-03 12:54:16 +00001818#ifdef MS_WINDOWS
1819/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1820 - time stamps are restricted to second resolution
1821 - file modification times suffer from forth-and-back conversions between
1822 UTC and local time
1823 Therefore, we implement our own stat, based on the Win32 API directly.
1824*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001825#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001826#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001827#define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001828
Victor Stinner6036e442015-03-08 01:58:04 +01001829static void
Steve Dowercc16be82016-09-08 10:35:16 -07001830find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1831 BY_HANDLE_FILE_INFORMATION *info,
1832 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001833{
1834 memset(info, 0, sizeof(*info));
1835 info->dwFileAttributes = pFileData->dwFileAttributes;
1836 info->ftCreationTime = pFileData->ftCreationTime;
1837 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1838 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1839 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1840 info->nFileSizeLow = pFileData->nFileSizeLow;
1841/* info->nNumberOfLinks = 1; */
1842 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1843 *reparse_tag = pFileData->dwReserved0;
1844 else
1845 *reparse_tag = 0;
1846}
1847
Guido van Rossumd8faa362007-04-27 19:54:29 +00001848static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001849attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001850{
Victor Stinner8c62be82010-05-06 00:08:46 +00001851 HANDLE hFindFile;
1852 WIN32_FIND_DATAW FileData;
1853 hFindFile = FindFirstFileW(pszFile, &FileData);
1854 if (hFindFile == INVALID_HANDLE_VALUE)
1855 return FALSE;
1856 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001857 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001858 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001859}
1860
Brian Curtind25aef52011-06-13 15:16:04 -05001861static int
Steve Dowercc16be82016-09-08 10:35:16 -07001862win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001863 BOOL traverse)
1864{
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001865 HANDLE hFile;
1866 BY_HANDLE_FILE_INFORMATION fileInfo;
1867 FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
1868 DWORD fileType, error;
1869 BOOL isUnhandledTag = FALSE;
1870 int retval = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001871
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001872 DWORD access = FILE_READ_ATTRIBUTES;
1873 DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
1874 if (!traverse) {
1875 flags |= FILE_FLAG_OPEN_REPARSE_POINT;
1876 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001877
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001878 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001879 if (hFile == INVALID_HANDLE_VALUE) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001880 /* Either the path doesn't exist, or the caller lacks access. */
1881 error = GetLastError();
1882 switch (error) {
1883 case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */
1884 case ERROR_SHARING_VIOLATION: /* It's a paging file. */
1885 /* Try reading the parent directory. */
1886 if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
1887 /* Cannot read the parent directory. */
1888 SetLastError(error);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001889 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001890 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001891 if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1892 if (traverse ||
1893 !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1894 /* The stat call has to traverse but cannot, so fail. */
1895 SetLastError(error);
Brian Curtind25aef52011-06-13 15:16:04 -05001896 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001897 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001898 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001899 break;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001900
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001901 case ERROR_INVALID_PARAMETER:
1902 /* \\.\con requires read or write access. */
1903 hFile = CreateFileW(path, access | GENERIC_READ,
1904 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1905 OPEN_EXISTING, flags, NULL);
1906 if (hFile == INVALID_HANDLE_VALUE) {
1907 SetLastError(error);
1908 return -1;
1909 }
1910 break;
1911
1912 case ERROR_CANT_ACCESS_FILE:
1913 /* bpo37834: open unhandled reparse points if traverse fails. */
1914 if (traverse) {
1915 traverse = FALSE;
1916 isUnhandledTag = TRUE;
1917 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
1918 flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
1919 }
1920 if (hFile == INVALID_HANDLE_VALUE) {
1921 SetLastError(error);
1922 return -1;
1923 }
1924 break;
1925
1926 default:
1927 return -1;
1928 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001929 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001930
1931 if (hFile != INVALID_HANDLE_VALUE) {
1932 /* Handle types other than files on disk. */
1933 fileType = GetFileType(hFile);
1934 if (fileType != FILE_TYPE_DISK) {
1935 if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1936 retval = -1;
1937 goto cleanup;
1938 }
1939 DWORD fileAttributes = GetFileAttributesW(path);
1940 memset(result, 0, sizeof(*result));
1941 if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1942 fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1943 /* \\.\pipe\ or \\.\mailslot\ */
1944 result->st_mode = _S_IFDIR;
1945 } else if (fileType == FILE_TYPE_CHAR) {
1946 /* \\.\nul */
1947 result->st_mode = _S_IFCHR;
1948 } else if (fileType == FILE_TYPE_PIPE) {
1949 /* \\.\pipe\spam */
1950 result->st_mode = _S_IFIFO;
1951 }
1952 /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1953 goto cleanup;
1954 }
1955
1956 /* Query the reparse tag, and traverse a non-link. */
1957 if (!traverse) {
1958 if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
1959 &tagInfo, sizeof(tagInfo))) {
1960 /* Allow devices that do not support FileAttributeTagInfo. */
1961 switch (GetLastError()) {
1962 case ERROR_INVALID_PARAMETER:
1963 case ERROR_INVALID_FUNCTION:
1964 case ERROR_NOT_SUPPORTED:
1965 tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1966 tagInfo.ReparseTag = 0;
1967 break;
1968 default:
1969 retval = -1;
1970 goto cleanup;
1971 }
1972 } else if (tagInfo.FileAttributes &
1973 FILE_ATTRIBUTE_REPARSE_POINT) {
1974 if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1975 if (isUnhandledTag) {
1976 /* Traversing previously failed for either this link
1977 or its target. */
1978 SetLastError(ERROR_CANT_ACCESS_FILE);
1979 retval = -1;
1980 goto cleanup;
1981 }
1982 /* Traverse a non-link, but not if traversing already failed
1983 for an unhandled tag. */
1984 } else if (!isUnhandledTag) {
1985 CloseHandle(hFile);
1986 return win32_xstat_impl(path, result, TRUE);
1987 }
1988 }
1989 }
1990
1991 if (!GetFileInformationByHandle(hFile, &fileInfo)) {
1992 switch (GetLastError()) {
1993 case ERROR_INVALID_PARAMETER:
1994 case ERROR_INVALID_FUNCTION:
1995 case ERROR_NOT_SUPPORTED:
Steve Dower772ec0f2019-09-04 14:42:54 -07001996 /* Volumes and physical disks are block devices, e.g.
1997 \\.\C: and \\.\PhysicalDrive0. */
1998 memset(result, 0, sizeof(*result));
1999 result->st_mode = 0x6000; /* S_IFBLK */
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002000 goto cleanup;
2001 }
Steve Dower772ec0f2019-09-04 14:42:54 -07002002 retval = -1;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002003 goto cleanup;
2004 }
2005 }
2006
2007 _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result);
2008
2009 if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
2010 /* Fix the file execute permissions. This hack sets S_IEXEC if
2011 the filename has an extension that is commonly used by files
2012 that CreateProcessW can execute. A real implementation calls
2013 GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
2014 AccessCheck to check for generic read, write, and execute
2015 access. */
2016 const wchar_t *fileExtension = wcsrchr(path, '.');
2017 if (fileExtension) {
2018 if (_wcsicmp(fileExtension, L".exe") == 0 ||
2019 _wcsicmp(fileExtension, L".bat") == 0 ||
2020 _wcsicmp(fileExtension, L".cmd") == 0 ||
2021 _wcsicmp(fileExtension, L".com") == 0) {
2022 result->st_mode |= 0111;
2023 }
2024 }
2025 }
2026
2027cleanup:
2028 if (hFile != INVALID_HANDLE_VALUE) {
Steve Dower772ec0f2019-09-04 14:42:54 -07002029 /* Preserve last error if we are failing */
2030 error = retval ? GetLastError() : 0;
2031 if (!CloseHandle(hFile)) {
2032 retval = -1;
2033 } else if (retval) {
2034 /* Restore last error */
2035 SetLastError(error);
2036 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002037 }
2038
2039 return retval;
Brian Curtinf5e76d02010-11-24 13:14:05 +00002040}
2041
2042static int
Steve Dowercc16be82016-09-08 10:35:16 -07002043win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00002044{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00002045 /* Protocol violation: we explicitly clear errno, instead of
2046 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05002047 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00002048 errno = 0;
2049 return code;
2050}
Brian Curtind25aef52011-06-13 15:16:04 -05002051/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00002052
2053 In Posix, stat automatically traverses symlinks and returns the stat
2054 structure for the target. In Windows, the equivalent GetFileAttributes by
2055 default does not traverse symlinks and instead returns attributes for
2056 the symlink.
2057
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002058 Instead, we will open the file (which *does* traverse symlinks by default)
2059 and GetFileInformationByHandle(). */
Brian Curtind40e6f72010-07-08 21:39:08 +00002060
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002061static int
Steve Dowercc16be82016-09-08 10:35:16 -07002062win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00002063{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00002064 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00002065}
2066
Victor Stinner8c62be82010-05-06 00:08:46 +00002067static int
Steve Dowercc16be82016-09-08 10:35:16 -07002068win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00002069{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00002070 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00002071}
2072
Martin v. Löwis14694662006-02-03 12:54:16 +00002073#endif /* MS_WINDOWS */
2074
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002075PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002076"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002077This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002078 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002079or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
2080\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002081Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
2082or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002083\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002084See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002085
2086static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002087 {"st_mode", "protection bits"},
2088 {"st_ino", "inode"},
2089 {"st_dev", "device"},
2090 {"st_nlink", "number of hard links"},
2091 {"st_uid", "user ID of owner"},
2092 {"st_gid", "group ID of owner"},
2093 {"st_size", "total size, in bytes"},
2094 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
2095 {NULL, "integer time of last access"},
2096 {NULL, "integer time of last modification"},
2097 {NULL, "integer time of last change"},
2098 {"st_atime", "time of last access"},
2099 {"st_mtime", "time of last modification"},
2100 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07002101 {"st_atime_ns", "time of last access in nanoseconds"},
2102 {"st_mtime_ns", "time of last modification in nanoseconds"},
2103 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002104#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002105 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002106#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002107#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002108 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002109#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002110#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002111 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002112#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002113#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002114 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002115#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002116#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002117 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002118#endif
2119#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002120 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002121#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002122#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2123 {"st_file_attributes", "Windows file attribute bits"},
2124#endif
jcea6c51d512018-01-28 14:00:08 +01002125#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2126 {"st_fstype", "Type of filesystem"},
2127#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002128#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2129 {"st_reparse_tag", "Windows reparse tag"},
2130#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002131 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002132};
2133
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002134#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07002135#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002136#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07002137#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002138#endif
2139
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002140#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002141#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
2142#else
2143#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
2144#endif
2145
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002146#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002147#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
2148#else
2149#define ST_RDEV_IDX ST_BLOCKS_IDX
2150#endif
2151
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002152#ifdef HAVE_STRUCT_STAT_ST_FLAGS
2153#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
2154#else
2155#define ST_FLAGS_IDX ST_RDEV_IDX
2156#endif
2157
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002158#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00002159#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002160#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00002161#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002162#endif
2163
2164#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2165#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
2166#else
2167#define ST_BIRTHTIME_IDX ST_GEN_IDX
2168#endif
2169
Zachary Ware63f277b2014-06-19 09:46:37 -05002170#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2171#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
2172#else
2173#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
2174#endif
2175
jcea6c51d512018-01-28 14:00:08 +01002176#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2177#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
2178#else
2179#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
2180#endif
2181
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002182#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2183#define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
2184#else
2185#define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
2186#endif
2187
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002188static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002189 "stat_result", /* name */
2190 stat_result__doc__, /* doc */
2191 stat_result_fields,
2192 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002193};
2194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002195PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002196"statvfs_result: Result from statvfs or fstatvfs.\n\n\
2197This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002198 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00002199or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002200\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002201See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002202
2203static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002204 {"f_bsize", },
2205 {"f_frsize", },
2206 {"f_blocks", },
2207 {"f_bfree", },
2208 {"f_bavail", },
2209 {"f_files", },
2210 {"f_ffree", },
2211 {"f_favail", },
2212 {"f_flag", },
2213 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01002214 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00002215 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002216};
2217
2218static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002219 "statvfs_result", /* name */
2220 statvfs_result__doc__, /* doc */
2221 statvfs_result_fields,
2222 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002223};
2224
Ross Lagerwall7807c352011-03-17 20:20:30 +02002225#if defined(HAVE_WAITID) && !defined(__APPLE__)
2226PyDoc_STRVAR(waitid_result__doc__,
2227"waitid_result: Result from waitid.\n\n\
2228This object may be accessed either as a tuple of\n\
2229 (si_pid, si_uid, si_signo, si_status, si_code),\n\
2230or via the attributes si_pid, si_uid, and so on.\n\
2231\n\
2232See os.waitid for more information.");
2233
2234static PyStructSequence_Field waitid_result_fields[] = {
2235 {"si_pid", },
2236 {"si_uid", },
2237 {"si_signo", },
2238 {"si_status", },
2239 {"si_code", },
2240 {0}
2241};
2242
2243static PyStructSequence_Desc waitid_result_desc = {
2244 "waitid_result", /* name */
2245 waitid_result__doc__, /* doc */
2246 waitid_result_fields,
2247 5
2248};
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002249#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002250static newfunc structseq_new;
2251
2252static PyObject *
2253statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2254{
Victor Stinner8c62be82010-05-06 00:08:46 +00002255 PyStructSequence *result;
2256 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002257
Victor Stinner8c62be82010-05-06 00:08:46 +00002258 result = (PyStructSequence*)structseq_new(type, args, kwds);
2259 if (!result)
2260 return NULL;
2261 /* If we have been initialized from a tuple,
2262 st_?time might be set to None. Initialize it
2263 from the int slots. */
2264 for (i = 7; i <= 9; i++) {
2265 if (result->ob_item[i+3] == Py_None) {
2266 Py_DECREF(Py_None);
2267 Py_INCREF(result->ob_item[i]);
2268 result->ob_item[i+3] = result->ob_item[i];
2269 }
2270 }
2271 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002272}
2273
Eddie Elizondob3966632019-11-05 07:16:14 -08002274static int
2275_posix_clear(PyObject *module)
2276{
Victor Stinner97f33c32020-05-14 18:05:58 +02002277 _posixstate *state = get_posix_state(module);
2278 Py_CLEAR(state->billion);
2279 Py_CLEAR(state->DirEntryType);
2280 Py_CLEAR(state->ScandirIteratorType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002281#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Victor Stinner97f33c32020-05-14 18:05:58 +02002282 Py_CLEAR(state->SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002283#endif
Victor Stinner97f33c32020-05-14 18:05:58 +02002284 Py_CLEAR(state->StatResultType);
2285 Py_CLEAR(state->StatVFSResultType);
2286 Py_CLEAR(state->TerminalSizeType);
2287 Py_CLEAR(state->TimesResultType);
2288 Py_CLEAR(state->UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002289#if defined(HAVE_WAITID) && !defined(__APPLE__)
Victor Stinner97f33c32020-05-14 18:05:58 +02002290 Py_CLEAR(state->WaitidResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002291#endif
2292#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Victor Stinner97f33c32020-05-14 18:05:58 +02002293 Py_CLEAR(state->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08002294#endif
Victor Stinner97f33c32020-05-14 18:05:58 +02002295 Py_CLEAR(state->st_mode);
Eddie Elizondob3966632019-11-05 07:16:14 -08002296 return 0;
2297}
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002298
Eddie Elizondob3966632019-11-05 07:16:14 -08002299static int
2300_posix_traverse(PyObject *module, visitproc visit, void *arg)
2301{
Victor Stinner97f33c32020-05-14 18:05:58 +02002302 _posixstate *state = get_posix_state(module);
2303 Py_VISIT(state->billion);
2304 Py_VISIT(state->DirEntryType);
2305 Py_VISIT(state->ScandirIteratorType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002306#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Victor Stinner97f33c32020-05-14 18:05:58 +02002307 Py_VISIT(state->SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002308#endif
Victor Stinner97f33c32020-05-14 18:05:58 +02002309 Py_VISIT(state->StatResultType);
2310 Py_VISIT(state->StatVFSResultType);
2311 Py_VISIT(state->TerminalSizeType);
2312 Py_VISIT(state->TimesResultType);
2313 Py_VISIT(state->UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002314#if defined(HAVE_WAITID) && !defined(__APPLE__)
Victor Stinner97f33c32020-05-14 18:05:58 +02002315 Py_VISIT(state->WaitidResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002316#endif
2317#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Victor Stinner97f33c32020-05-14 18:05:58 +02002318 Py_VISIT(state->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08002319#endif
Victor Stinner97f33c32020-05-14 18:05:58 +02002320 Py_VISIT(state->st_mode);
Eddie Elizondob3966632019-11-05 07:16:14 -08002321 return 0;
2322}
2323
2324static void
2325_posix_free(void *module)
2326{
2327 _posix_clear((PyObject *)module);
2328}
Larry Hastings6fe20b32012-04-19 15:07:49 -07002329
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002330static void
Victor Stinner1c2fa782020-05-10 11:05:29 +02002331fill_time(PyObject *module, PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002332{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002333 PyObject *s = _PyLong_FromTime_t(sec);
2334 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2335 PyObject *s_in_ns = NULL;
2336 PyObject *ns_total = NULL;
2337 PyObject *float_s = NULL;
2338
2339 if (!(s && ns_fractional))
2340 goto exit;
2341
Victor Stinner1c2fa782020-05-10 11:05:29 +02002342 s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion);
Larry Hastings6fe20b32012-04-19 15:07:49 -07002343 if (!s_in_ns)
2344 goto exit;
2345
2346 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2347 if (!ns_total)
2348 goto exit;
2349
Victor Stinner01b5aab2017-10-24 02:02:00 -07002350 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2351 if (!float_s) {
2352 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002353 }
2354
2355 PyStructSequence_SET_ITEM(v, index, s);
2356 PyStructSequence_SET_ITEM(v, index+3, float_s);
2357 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2358 s = NULL;
2359 float_s = NULL;
2360 ns_total = NULL;
2361exit:
2362 Py_XDECREF(s);
2363 Py_XDECREF(ns_fractional);
2364 Py_XDECREF(s_in_ns);
2365 Py_XDECREF(ns_total);
2366 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002367}
2368
Tim Peters5aa91602002-01-30 05:46:57 +00002369/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002370 (used by posix_stat() and posix_fstat()) */
2371static PyObject*
Victor Stinner1c2fa782020-05-10 11:05:29 +02002372_pystat_fromstructstat(PyObject *module, STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002373{
Victor Stinner8c62be82010-05-06 00:08:46 +00002374 unsigned long ansec, mnsec, cnsec;
Victor Stinner1c2fa782020-05-10 11:05:29 +02002375 PyObject *StatResultType = get_posix_state(module)->StatResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08002376 PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002377 if (v == NULL)
2378 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002379
Victor Stinner8c62be82010-05-06 00:08:46 +00002380 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002381 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002382 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002383#ifdef MS_WINDOWS
2384 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002385#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002386 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002387#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002388 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002389#if defined(MS_WINDOWS)
2390 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2391 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2392#else
2393 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2394 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2395#endif
xdegaye50e86032017-05-22 11:15:08 +02002396 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2397 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002398
Martin v. Löwis14694662006-02-03 12:54:16 +00002399#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002400 ansec = st->st_atim.tv_nsec;
2401 mnsec = st->st_mtim.tv_nsec;
2402 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002403#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002404 ansec = st->st_atimespec.tv_nsec;
2405 mnsec = st->st_mtimespec.tv_nsec;
2406 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002407#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002408 ansec = st->st_atime_nsec;
2409 mnsec = st->st_mtime_nsec;
2410 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002411#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002412 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002413#endif
Victor Stinner1c2fa782020-05-10 11:05:29 +02002414 fill_time(module, v, 7, st->st_atime, ansec);
2415 fill_time(module, v, 8, st->st_mtime, mnsec);
2416 fill_time(module, v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002417
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002418#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002419 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2420 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002421#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002422#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002423 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2424 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002425#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002426#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002427 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2428 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002429#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002430#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002431 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2432 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002433#endif
2434#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002435 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002436 PyObject *val;
2437 unsigned long bsec,bnsec;
2438 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002439#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002440 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002441#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002442 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002443#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002444 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002445 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2446 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002447 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002448#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002449#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002450 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2451 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002452#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002453#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2454 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2455 PyLong_FromUnsignedLong(st->st_file_attributes));
2456#endif
jcea6c51d512018-01-28 14:00:08 +01002457#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2458 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2459 PyUnicode_FromString(st->st_fstype));
2460#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002461#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2462 PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX,
2463 PyLong_FromUnsignedLong(st->st_reparse_tag));
2464#endif
Fred Drake699f3522000-06-29 21:12:41 +00002465
Victor Stinner8c62be82010-05-06 00:08:46 +00002466 if (PyErr_Occurred()) {
2467 Py_DECREF(v);
2468 return NULL;
2469 }
Fred Drake699f3522000-06-29 21:12:41 +00002470
Victor Stinner8c62be82010-05-06 00:08:46 +00002471 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002472}
2473
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002474/* POSIX methods */
2475
Guido van Rossum94f6f721999-01-06 18:42:14 +00002476
2477static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +02002478posix_do_stat(PyObject *module, const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002479 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002480{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002481 STRUCT_STAT st;
2482 int result;
2483
Ronald Oussoren41761932020-11-08 10:05:27 +01002484#ifdef HAVE_FSTATAT
2485 int fstatat_unavailable = 0;
2486#endif
2487
Larry Hastings9cf065c2012-06-22 16:30:09 -07002488#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2489 if (follow_symlinks_specified(function_name, follow_symlinks))
2490 return NULL;
2491#endif
2492
2493 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2494 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2495 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2496 return NULL;
2497
2498 Py_BEGIN_ALLOW_THREADS
2499 if (path->fd != -1)
2500 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002501#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002502 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002503 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002504 else
Steve Dowercc16be82016-09-08 10:35:16 -07002505 result = win32_lstat(path->wide, &st);
2506#else
2507 else
2508#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002509 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2510 result = LSTAT(path->narrow, &st);
2511 else
Steve Dowercc16be82016-09-08 10:35:16 -07002512#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002513#ifdef HAVE_FSTATAT
Ronald Oussoren41761932020-11-08 10:05:27 +01002514 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2515 if (HAVE_FSTATAT_RUNTIME) {
2516 result = fstatat(dir_fd, path->narrow, &st,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002517 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
Ronald Oussoren41761932020-11-08 10:05:27 +01002518
2519 } else {
2520 fstatat_unavailable = 1;
2521 }
2522 } else
Steve Dowercc16be82016-09-08 10:35:16 -07002523#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002524 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002525#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002526 Py_END_ALLOW_THREADS
2527
Ronald Oussoren41761932020-11-08 10:05:27 +01002528#ifdef HAVE_FSTATAT
2529 if (fstatat_unavailable) {
2530 argument_unavailable_error("stat", "dir_fd");
2531 return NULL;
2532 }
2533#endif
2534
Victor Stinner292c8352012-10-30 02:17:38 +01002535 if (result != 0) {
2536 return path_error(path);
2537 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002538
Victor Stinner1c2fa782020-05-10 11:05:29 +02002539 return _pystat_fromstructstat(module, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002540}
2541
Larry Hastings2f936352014-08-05 14:04:04 +10002542/*[python input]
2543
2544for s in """
2545
2546FACCESSAT
2547FCHMODAT
2548FCHOWNAT
2549FSTATAT
2550LINKAT
2551MKDIRAT
2552MKFIFOAT
2553MKNODAT
2554OPENAT
2555READLINKAT
2556SYMLINKAT
2557UNLINKAT
2558
2559""".strip().split():
2560 s = s.strip()
2561 print("""
2562#ifdef HAVE_{s}
2563 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002564#else
Larry Hastings2f936352014-08-05 14:04:04 +10002565 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002566#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002567""".rstrip().format(s=s))
2568
2569for s in """
2570
2571FCHDIR
2572FCHMOD
2573FCHOWN
2574FDOPENDIR
2575FEXECVE
2576FPATHCONF
2577FSTATVFS
2578FTRUNCATE
2579
2580""".strip().split():
2581 s = s.strip()
2582 print("""
2583#ifdef HAVE_{s}
2584 #define PATH_HAVE_{s} 1
2585#else
2586 #define PATH_HAVE_{s} 0
2587#endif
2588
2589""".rstrip().format(s=s))
2590[python start generated code]*/
2591
2592#ifdef HAVE_FACCESSAT
2593 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2594#else
2595 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2596#endif
2597
2598#ifdef HAVE_FCHMODAT
2599 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2600#else
2601 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2602#endif
2603
2604#ifdef HAVE_FCHOWNAT
2605 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2606#else
2607 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2608#endif
2609
2610#ifdef HAVE_FSTATAT
2611 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2612#else
2613 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2614#endif
2615
2616#ifdef HAVE_LINKAT
2617 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2618#else
2619 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2620#endif
2621
2622#ifdef HAVE_MKDIRAT
2623 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2624#else
2625 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2626#endif
2627
2628#ifdef HAVE_MKFIFOAT
2629 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2630#else
2631 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2632#endif
2633
2634#ifdef HAVE_MKNODAT
2635 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2636#else
2637 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2638#endif
2639
2640#ifdef HAVE_OPENAT
2641 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2642#else
2643 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2644#endif
2645
2646#ifdef HAVE_READLINKAT
2647 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2648#else
2649 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2650#endif
2651
2652#ifdef HAVE_SYMLINKAT
2653 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2654#else
2655 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2656#endif
2657
2658#ifdef HAVE_UNLINKAT
2659 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2660#else
2661 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2662#endif
2663
2664#ifdef HAVE_FCHDIR
2665 #define PATH_HAVE_FCHDIR 1
2666#else
2667 #define PATH_HAVE_FCHDIR 0
2668#endif
2669
2670#ifdef HAVE_FCHMOD
2671 #define PATH_HAVE_FCHMOD 1
2672#else
2673 #define PATH_HAVE_FCHMOD 0
2674#endif
2675
2676#ifdef HAVE_FCHOWN
2677 #define PATH_HAVE_FCHOWN 1
2678#else
2679 #define PATH_HAVE_FCHOWN 0
2680#endif
2681
2682#ifdef HAVE_FDOPENDIR
2683 #define PATH_HAVE_FDOPENDIR 1
2684#else
2685 #define PATH_HAVE_FDOPENDIR 0
2686#endif
2687
2688#ifdef HAVE_FEXECVE
2689 #define PATH_HAVE_FEXECVE 1
2690#else
2691 #define PATH_HAVE_FEXECVE 0
2692#endif
2693
2694#ifdef HAVE_FPATHCONF
2695 #define PATH_HAVE_FPATHCONF 1
2696#else
2697 #define PATH_HAVE_FPATHCONF 0
2698#endif
2699
2700#ifdef HAVE_FSTATVFS
2701 #define PATH_HAVE_FSTATVFS 1
2702#else
2703 #define PATH_HAVE_FSTATVFS 0
2704#endif
2705
2706#ifdef HAVE_FTRUNCATE
2707 #define PATH_HAVE_FTRUNCATE 1
2708#else
2709 #define PATH_HAVE_FTRUNCATE 0
2710#endif
2711/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002712
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002713#ifdef MS_WINDOWS
2714 #undef PATH_HAVE_FTRUNCATE
2715 #define PATH_HAVE_FTRUNCATE 1
2716#endif
Larry Hastings31826802013-10-19 00:09:25 -07002717
Larry Hastings61272b72014-01-07 12:41:53 -08002718/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002719
2720class path_t_converter(CConverter):
2721
2722 type = "path_t"
2723 impl_by_reference = True
2724 parse_by_reference = True
2725
2726 converter = 'path_converter'
2727
2728 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002729 # right now path_t doesn't support default values.
2730 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002731 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002732 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002733
Larry Hastings2f936352014-08-05 14:04:04 +10002734 if self.c_default not in (None, 'Py_None'):
2735 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002736
2737 self.nullable = nullable
2738 self.allow_fd = allow_fd
2739
Larry Hastings7726ac92014-01-31 22:03:12 -08002740 def pre_render(self):
2741 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002742 if isinstance(value, str):
2743 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002744 return str(int(bool(value)))
2745
2746 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002747 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002748 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002749 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002750 strify(self.nullable),
2751 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002752 )
2753
2754 def cleanup(self):
2755 return "path_cleanup(&" + self.name + ");\n"
2756
2757
2758class dir_fd_converter(CConverter):
2759 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002760
Larry Hastings2f936352014-08-05 14:04:04 +10002761 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002762 if self.default in (unspecified, None):
2763 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002764 if isinstance(requires, str):
2765 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2766 else:
2767 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002768
Larry Hastings2f936352014-08-05 14:04:04 +10002769class uid_t_converter(CConverter):
2770 type = "uid_t"
2771 converter = '_Py_Uid_Converter'
2772
2773class gid_t_converter(CConverter):
2774 type = "gid_t"
2775 converter = '_Py_Gid_Converter'
2776
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002777class dev_t_converter(CConverter):
2778 type = 'dev_t'
2779 converter = '_Py_Dev_Converter'
2780
2781class dev_t_return_converter(unsigned_long_return_converter):
2782 type = 'dev_t'
2783 conversion_fn = '_PyLong_FromDev'
2784 unsigned_cast = '(dev_t)'
2785
Larry Hastings2f936352014-08-05 14:04:04 +10002786class FSConverter_converter(CConverter):
2787 type = 'PyObject *'
2788 converter = 'PyUnicode_FSConverter'
2789 def converter_init(self):
2790 if self.default is not unspecified:
2791 fail("FSConverter_converter does not support default values")
2792 self.c_default = 'NULL'
2793
2794 def cleanup(self):
2795 return "Py_XDECREF(" + self.name + ");\n"
2796
2797class pid_t_converter(CConverter):
2798 type = 'pid_t'
2799 format_unit = '" _Py_PARSE_PID "'
2800
2801class idtype_t_converter(int_converter):
2802 type = 'idtype_t'
2803
2804class id_t_converter(CConverter):
2805 type = 'id_t'
2806 format_unit = '" _Py_PARSE_PID "'
2807
Benjamin Petersonca470632016-09-06 13:47:26 -07002808class intptr_t_converter(CConverter):
2809 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002810 format_unit = '" _Py_PARSE_INTPTR "'
2811
2812class Py_off_t_converter(CConverter):
2813 type = 'Py_off_t'
2814 converter = 'Py_off_t_converter'
2815
2816class Py_off_t_return_converter(long_return_converter):
2817 type = 'Py_off_t'
2818 conversion_fn = 'PyLong_FromPy_off_t'
2819
2820class path_confname_converter(CConverter):
2821 type="int"
2822 converter="conv_path_confname"
2823
2824class confstr_confname_converter(path_confname_converter):
2825 converter='conv_confstr_confname'
2826
2827class sysconf_confname_converter(path_confname_converter):
2828 converter="conv_sysconf_confname"
2829
Larry Hastings61272b72014-01-07 12:41:53 -08002830[python start generated code]*/
Serhiy Storchaka9975cc52020-10-09 23:00:45 +03002831/*[python end generated code: output=da39a3ee5e6b4b0d input=3338733161aa7879]*/
Larry Hastings31826802013-10-19 00:09:25 -07002832
Larry Hastings61272b72014-01-07 12:41:53 -08002833/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002834
Larry Hastings2a727912014-01-16 11:32:01 -08002835os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002836
2837 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002838 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002839 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002840
2841 *
2842
Larry Hastings2f936352014-08-05 14:04:04 +10002843 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002844 If not None, it should be a file descriptor open to a directory,
2845 and path should be a relative string; path will then be relative to
2846 that directory.
2847
2848 follow_symlinks: bool = True
2849 If False, and the last element of the path is a symbolic link,
2850 stat will examine the symbolic link itself instead of the file
2851 the link points to.
2852
2853Perform a stat system call on the given path.
2854
2855dir_fd and follow_symlinks may not be implemented
2856 on your platform. If they are unavailable, using them will raise a
2857 NotImplementedError.
2858
2859It's an error to use dir_fd or follow_symlinks when specifying path as
2860 an open file descriptor.
2861
Larry Hastings61272b72014-01-07 12:41:53 -08002862[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002863
Larry Hastings31826802013-10-19 00:09:25 -07002864static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002865os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002866/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002867{
Victor Stinner1c2fa782020-05-10 11:05:29 +02002868 return posix_do_stat(module, "stat", path, dir_fd, follow_symlinks);
Larry Hastings31826802013-10-19 00:09:25 -07002869}
2870
Larry Hastings2f936352014-08-05 14:04:04 +10002871
2872/*[clinic input]
2873os.lstat
2874
2875 path : path_t
2876
2877 *
2878
2879 dir_fd : dir_fd(requires='fstatat') = None
2880
2881Perform a stat system call on the given path, without following symbolic links.
2882
2883Like stat(), but do not follow symbolic links.
2884Equivalent to stat(path, follow_symlinks=False).
2885[clinic start generated code]*/
2886
Larry Hastings2f936352014-08-05 14:04:04 +10002887static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002888os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2889/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002890{
2891 int follow_symlinks = 0;
Victor Stinner1c2fa782020-05-10 11:05:29 +02002892 return posix_do_stat(module, "lstat", path, dir_fd, follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10002893}
Larry Hastings31826802013-10-19 00:09:25 -07002894
Larry Hastings2f936352014-08-05 14:04:04 +10002895
Larry Hastings61272b72014-01-07 12:41:53 -08002896/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002897os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002898
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002899 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002900 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002901
2902 mode: int
2903 Operating-system mode bitfield. Can be F_OK to test existence,
2904 or the inclusive-OR of R_OK, W_OK, and X_OK.
2905
2906 *
2907
Larry Hastings2f936352014-08-05 14:04:04 +10002908 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002909 If not None, it should be a file descriptor open to a directory,
2910 and path should be relative; path will then be relative to that
2911 directory.
2912
2913 effective_ids: bool = False
2914 If True, access will use the effective uid/gid instead of
2915 the real uid/gid.
2916
2917 follow_symlinks: bool = True
2918 If False, and the last element of the path is a symbolic link,
2919 access will examine the symbolic link itself instead of the file
2920 the link points to.
2921
2922Use the real uid/gid to test for access to a path.
2923
2924{parameters}
2925dir_fd, effective_ids, and follow_symlinks may not be implemented
2926 on your platform. If they are unavailable, using them will raise a
2927 NotImplementedError.
2928
2929Note that most operations will use the effective uid/gid, therefore this
2930 routine can be used in a suid/sgid environment to test if the invoking user
2931 has the specified access to the path.
2932
Larry Hastings61272b72014-01-07 12:41:53 -08002933[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002934
Larry Hastings2f936352014-08-05 14:04:04 +10002935static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002936os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002937 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002938/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002939{
Larry Hastings2f936352014-08-05 14:04:04 +10002940 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002941
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002942#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002943 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002944#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002945 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002946#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002947
Ronald Oussoren41761932020-11-08 10:05:27 +01002948#ifdef HAVE_FACCESSAT
2949 int faccessat_unavailable = 0;
2950#endif
2951
Larry Hastings9cf065c2012-06-22 16:30:09 -07002952#ifndef HAVE_FACCESSAT
2953 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002954 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002955
2956 if (effective_ids) {
2957 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002958 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002959 }
2960#endif
2961
2962#ifdef MS_WINDOWS
2963 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002964 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002965 Py_END_ALLOW_THREADS
2966
2967 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002968 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002969 * * we didn't get a -1, and
2970 * * write access wasn't requested,
2971 * * or the file isn't read-only,
2972 * * or it's a directory.
2973 * (Directories cannot be read-only on Windows.)
2974 */
Larry Hastings2f936352014-08-05 14:04:04 +10002975 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002976 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002977 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002978 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002979#else
2980
2981 Py_BEGIN_ALLOW_THREADS
2982#ifdef HAVE_FACCESSAT
2983 if ((dir_fd != DEFAULT_DIR_FD) ||
2984 effective_ids ||
2985 !follow_symlinks) {
Ronald Oussoren41761932020-11-08 10:05:27 +01002986
2987 if (HAVE_FACCESSAT_RUNTIME) {
2988 int flags = 0;
2989 if (!follow_symlinks)
2990 flags |= AT_SYMLINK_NOFOLLOW;
2991 if (effective_ids)
2992 flags |= AT_EACCESS;
2993 result = faccessat(dir_fd, path->narrow, mode, flags);
2994 } else {
2995 faccessat_unavailable = 1;
2996 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002997 }
2998 else
2999#endif
Larry Hastings31826802013-10-19 00:09:25 -07003000 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003001 Py_END_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +01003002
3003#ifdef HAVE_FACCESSAT
3004 if (faccessat_unavailable) {
3005 if (dir_fd != DEFAULT_DIR_FD) {
3006 argument_unavailable_error("access", "dir_fd");
3007 return -1;
3008 }
3009 if (follow_symlinks_specified("access", follow_symlinks))
3010 return -1;
3011
3012 if (effective_ids) {
3013 argument_unavailable_error("access", "effective_ids");
3014 return -1;
3015 }
3016 /* should be unreachable */
3017 return -1;
3018 }
3019#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003020 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003021#endif
3022
Larry Hastings9cf065c2012-06-22 16:30:09 -07003023 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003024}
3025
Guido van Rossumd371ff11999-01-25 16:12:23 +00003026#ifndef F_OK
3027#define F_OK 0
3028#endif
3029#ifndef R_OK
3030#define R_OK 4
3031#endif
3032#ifndef W_OK
3033#define W_OK 2
3034#endif
3035#ifndef X_OK
3036#define X_OK 1
3037#endif
3038
Larry Hastings31826802013-10-19 00:09:25 -07003039
Guido van Rossumd371ff11999-01-25 16:12:23 +00003040#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08003041/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02003042os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07003043
3044 fd: int
3045 Integer file descriptor handle.
3046
3047 /
3048
3049Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08003050[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07003051
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02003052static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003053os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02003054/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07003055{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003056
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02003057 long size = sysconf(_SC_TTY_NAME_MAX);
3058 if (size == -1) {
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02003059 return posix_error();
3060 }
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02003061 char *buffer = (char *)PyMem_RawMalloc(size);
3062 if (buffer == NULL) {
3063 return PyErr_NoMemory();
3064 }
3065 int ret = ttyname_r(fd, buffer, size);
3066 if (ret != 0) {
3067 PyMem_RawFree(buffer);
3068 errno = ret;
3069 return posix_error();
3070 }
3071 PyObject *res = PyUnicode_DecodeFSDefault(buffer);
3072 PyMem_RawFree(buffer);
3073 return res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003074}
Guido van Rossumd371ff11999-01-25 16:12:23 +00003075#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003076
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003077#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10003078/*[clinic input]
3079os.ctermid
3080
3081Return the name of the controlling terminal for this process.
3082[clinic start generated code]*/
3083
Larry Hastings2f936352014-08-05 14:04:04 +10003084static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003085os_ctermid_impl(PyObject *module)
3086/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003087{
Victor Stinner8c62be82010-05-06 00:08:46 +00003088 char *ret;
3089 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003090
Greg Wardb48bc172000-03-01 21:51:56 +00003091#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00003092 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003093#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003094 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003095#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003096 if (ret == NULL)
3097 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00003098 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003099}
Larry Hastings2f936352014-08-05 14:04:04 +10003100#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003101
Larry Hastings2f936352014-08-05 14:04:04 +10003102
3103/*[clinic input]
3104os.chdir
3105
3106 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
3107
3108Change the current working directory to the specified path.
3109
3110path may always be specified as a string.
3111On some platforms, path may also be specified as an open file descriptor.
3112 If this functionality is unavailable, using it raises an exception.
3113[clinic start generated code]*/
3114
Larry Hastings2f936352014-08-05 14:04:04 +10003115static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003116os_chdir_impl(PyObject *module, path_t *path)
3117/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003118{
3119 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003120
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003121 if (PySys_Audit("os.chdir", "(O)", path->object) < 0) {
3122 return NULL;
3123 }
3124
Larry Hastings9cf065c2012-06-22 16:30:09 -07003125 Py_BEGIN_ALLOW_THREADS
3126#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003127 /* on unix, success = 0, on windows, success = !0 */
3128 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003129#else
3130#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10003131 if (path->fd != -1)
3132 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003133 else
3134#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003135 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003136#endif
3137 Py_END_ALLOW_THREADS
3138
3139 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10003140 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003141 }
3142
Larry Hastings2f936352014-08-05 14:04:04 +10003143 Py_RETURN_NONE;
3144}
3145
3146
3147#ifdef HAVE_FCHDIR
3148/*[clinic input]
3149os.fchdir
3150
3151 fd: fildes
3152
3153Change to the directory of the given file descriptor.
3154
3155fd must be opened on a directory, not a file.
3156Equivalent to os.chdir(fd).
3157
3158[clinic start generated code]*/
3159
Fred Drake4d1e64b2002-04-15 19:40:07 +00003160static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003161os_fchdir_impl(PyObject *module, int fd)
3162/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00003163{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003164 if (PySys_Audit("os.chdir", "(i)", fd) < 0) {
3165 return NULL;
3166 }
Larry Hastings2f936352014-08-05 14:04:04 +10003167 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00003168}
3169#endif /* HAVE_FCHDIR */
3170
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003171
Larry Hastings2f936352014-08-05 14:04:04 +10003172/*[clinic input]
3173os.chmod
3174
3175 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00003176 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10003177 On some platforms, path may also be specified as an open file descriptor.
3178 If this functionality is unavailable, using it raises an exception.
3179
3180 mode: int
3181 Operating-system mode bitfield.
3182
3183 *
3184
3185 dir_fd : dir_fd(requires='fchmodat') = None
3186 If not None, it should be a file descriptor open to a directory,
3187 and path should be relative; path will then be relative to that
3188 directory.
3189
3190 follow_symlinks: bool = True
3191 If False, and the last element of the path is a symbolic link,
3192 chmod will modify the symbolic link itself instead of the file
3193 the link points to.
3194
3195Change the access permissions of a file.
3196
3197It is an error to use dir_fd or follow_symlinks when specifying path as
3198 an open file descriptor.
3199dir_fd and follow_symlinks may not be implemented on your platform.
3200 If they are unavailable, using them will raise a NotImplementedError.
3201
3202[clinic start generated code]*/
3203
Larry Hastings2f936352014-08-05 14:04:04 +10003204static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003205os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003206 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003207/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003208{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003209 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003210
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003211#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003212 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003213#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003214
Larry Hastings9cf065c2012-06-22 16:30:09 -07003215#ifdef HAVE_FCHMODAT
3216 int fchmodat_nofollow_unsupported = 0;
Ronald Oussoren41761932020-11-08 10:05:27 +01003217 int fchmodat_unsupported = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003218#endif
3219
Larry Hastings9cf065c2012-06-22 16:30:09 -07003220#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
3221 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003222 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003223#endif
3224
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003225 if (PySys_Audit("os.chmod", "Oii", path->object, mode,
3226 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3227 return NULL;
3228 }
3229
Larry Hastings9cf065c2012-06-22 16:30:09 -07003230#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003231 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003232 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01003233 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003234 result = 0;
3235 else {
3236 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00003237 attr &= ~FILE_ATTRIBUTE_READONLY;
3238 else
3239 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07003240 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003241 }
3242 Py_END_ALLOW_THREADS
3243
3244 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10003245 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003246 }
3247#else /* MS_WINDOWS */
3248 Py_BEGIN_ALLOW_THREADS
3249#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003250 if (path->fd != -1)
3251 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003252 else
Ronald Oussoren41761932020-11-08 10:05:27 +01003253#endif /* HAVE_CHMOD */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003254#ifdef HAVE_LCHMOD
3255 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003256 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003257 else
Ronald Oussoren41761932020-11-08 10:05:27 +01003258#endif /* HAVE_LCHMOD */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003259#ifdef HAVE_FCHMODAT
3260 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
Ronald Oussoren41761932020-11-08 10:05:27 +01003261 if (HAVE_FCHMODAT_RUNTIME) {
3262 /*
3263 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
3264 * The documentation specifically shows how to use it,
3265 * and then says it isn't implemented yet.
3266 * (true on linux with glibc 2.15, and openindiana 3.x)
3267 *
3268 * Once it is supported, os.chmod will automatically
3269 * support dir_fd and follow_symlinks=False. (Hopefully.)
3270 * Until then, we need to be careful what exception we raise.
3271 */
3272 result = fchmodat(dir_fd, path->narrow, mode,
3273 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3274 /*
3275 * But wait! We can't throw the exception without allowing threads,
3276 * and we can't do that in this nested scope. (Macro trickery, sigh.)
3277 */
3278 fchmodat_nofollow_unsupported =
3279 result &&
3280 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
3281 !follow_symlinks;
3282 } else {
3283 fchmodat_unsupported = 1;
3284 fchmodat_nofollow_unsupported = 1;
3285
3286 result = -1;
3287 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003288 }
3289 else
Ronald Oussoren41761932020-11-08 10:05:27 +01003290#endif /* HAVE_FHCMODAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003291 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003292 Py_END_ALLOW_THREADS
3293
3294 if (result) {
3295#ifdef HAVE_FCHMODAT
Ronald Oussoren41761932020-11-08 10:05:27 +01003296 if (fchmodat_unsupported) {
3297 if (dir_fd != DEFAULT_DIR_FD) {
3298 argument_unavailable_error("chmod", "dir_fd");
3299 return NULL;
3300 }
3301 }
3302
Larry Hastings9cf065c2012-06-22 16:30:09 -07003303 if (fchmodat_nofollow_unsupported) {
3304 if (dir_fd != DEFAULT_DIR_FD)
3305 dir_fd_and_follow_symlinks_invalid("chmod",
3306 dir_fd, follow_symlinks);
3307 else
3308 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08003309 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003310 }
3311 else
Ronald Oussoren41761932020-11-08 10:05:27 +01003312#endif /* HAVE_FCHMODAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003313 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003314 }
Ronald Oussoren41761932020-11-08 10:05:27 +01003315#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003316
Larry Hastings2f936352014-08-05 14:04:04 +10003317 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003318}
3319
Larry Hastings9cf065c2012-06-22 16:30:09 -07003320
Christian Heimes4e30a842007-11-30 22:12:06 +00003321#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003322/*[clinic input]
3323os.fchmod
3324
3325 fd: int
3326 mode: int
3327
3328Change the access permissions of the file given by file descriptor fd.
3329
3330Equivalent to os.chmod(fd, mode).
3331[clinic start generated code]*/
3332
Larry Hastings2f936352014-08-05 14:04:04 +10003333static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003334os_fchmod_impl(PyObject *module, int fd, int mode)
3335/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003336{
3337 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003338 int async_err = 0;
3339
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003340 if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) {
3341 return NULL;
3342 }
3343
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003344 do {
3345 Py_BEGIN_ALLOW_THREADS
3346 res = fchmod(fd, mode);
3347 Py_END_ALLOW_THREADS
3348 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3349 if (res != 0)
3350 return (!async_err) ? posix_error() : NULL;
3351
Victor Stinner8c62be82010-05-06 00:08:46 +00003352 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003353}
3354#endif /* HAVE_FCHMOD */
3355
Larry Hastings2f936352014-08-05 14:04:04 +10003356
Christian Heimes4e30a842007-11-30 22:12:06 +00003357#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003358/*[clinic input]
3359os.lchmod
3360
3361 path: path_t
3362 mode: int
3363
3364Change the access permissions of a file, without following symbolic links.
3365
3366If path is a symlink, this affects the link itself rather than the target.
3367Equivalent to chmod(path, mode, follow_symlinks=False)."
3368[clinic start generated code]*/
3369
Larry Hastings2f936352014-08-05 14:04:04 +10003370static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003371os_lchmod_impl(PyObject *module, path_t *path, int mode)
3372/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003373{
Victor Stinner8c62be82010-05-06 00:08:46 +00003374 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003375 if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) {
3376 return NULL;
3377 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003378 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003379 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003380 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003381 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003382 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003383 return NULL;
3384 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003385 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003386}
3387#endif /* HAVE_LCHMOD */
3388
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003389
Thomas Wouterscf297e42007-02-23 15:07:44 +00003390#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003391/*[clinic input]
3392os.chflags
3393
3394 path: path_t
3395 flags: unsigned_long(bitwise=True)
3396 follow_symlinks: bool=True
3397
3398Set file flags.
3399
3400If follow_symlinks is False, and the last element of the path is a symbolic
3401 link, chflags will change flags on the symbolic link itself instead of the
3402 file the link points to.
3403follow_symlinks may not be implemented on your platform. If it is
3404unavailable, using it will raise a NotImplementedError.
3405
3406[clinic start generated code]*/
3407
Larry Hastings2f936352014-08-05 14:04:04 +10003408static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003409os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003410 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003411/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003412{
3413 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003414
3415#ifndef HAVE_LCHFLAGS
3416 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003417 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003418#endif
3419
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003420 if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3421 return NULL;
3422 }
3423
Victor Stinner8c62be82010-05-06 00:08:46 +00003424 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003425#ifdef HAVE_LCHFLAGS
3426 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003427 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003428 else
3429#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003430 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003431 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003432
Larry Hastings2f936352014-08-05 14:04:04 +10003433 if (result)
3434 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003435
Larry Hastings2f936352014-08-05 14:04:04 +10003436 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003437}
3438#endif /* HAVE_CHFLAGS */
3439
Larry Hastings2f936352014-08-05 14:04:04 +10003440
Thomas Wouterscf297e42007-02-23 15:07:44 +00003441#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003442/*[clinic input]
3443os.lchflags
3444
3445 path: path_t
3446 flags: unsigned_long(bitwise=True)
3447
3448Set file flags.
3449
3450This function will not follow symbolic links.
3451Equivalent to chflags(path, flags, follow_symlinks=False).
3452[clinic start generated code]*/
3453
Larry Hastings2f936352014-08-05 14:04:04 +10003454static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003455os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3456/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003457{
Victor Stinner8c62be82010-05-06 00:08:46 +00003458 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003459 if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3460 return NULL;
3461 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003462 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003463 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003464 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003465 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003466 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003467 }
Victor Stinner292c8352012-10-30 02:17:38 +01003468 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003469}
3470#endif /* HAVE_LCHFLAGS */
3471
Larry Hastings2f936352014-08-05 14:04:04 +10003472
Martin v. Löwis244edc82001-10-04 22:44:26 +00003473#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003474/*[clinic input]
3475os.chroot
3476 path: path_t
3477
3478Change root directory to path.
3479
3480[clinic start generated code]*/
3481
Larry Hastings2f936352014-08-05 14:04:04 +10003482static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003483os_chroot_impl(PyObject *module, path_t *path)
3484/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003485{
3486 int res;
3487 Py_BEGIN_ALLOW_THREADS
3488 res = chroot(path->narrow);
3489 Py_END_ALLOW_THREADS
3490 if (res < 0)
3491 return path_error(path);
3492 Py_RETURN_NONE;
3493}
3494#endif /* HAVE_CHROOT */
3495
Martin v. Löwis244edc82001-10-04 22:44:26 +00003496
Guido van Rossum21142a01999-01-08 21:05:37 +00003497#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003498/*[clinic input]
3499os.fsync
3500
3501 fd: fildes
3502
3503Force write of fd to disk.
3504[clinic start generated code]*/
3505
Larry Hastings2f936352014-08-05 14:04:04 +10003506static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003507os_fsync_impl(PyObject *module, int fd)
3508/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003509{
3510 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003511}
3512#endif /* HAVE_FSYNC */
3513
Larry Hastings2f936352014-08-05 14:04:04 +10003514
Ross Lagerwall7807c352011-03-17 20:20:30 +02003515#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003516/*[clinic input]
3517os.sync
3518
3519Force write of everything to disk.
3520[clinic start generated code]*/
3521
Larry Hastings2f936352014-08-05 14:04:04 +10003522static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003523os_sync_impl(PyObject *module)
3524/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003525{
3526 Py_BEGIN_ALLOW_THREADS
3527 sync();
3528 Py_END_ALLOW_THREADS
3529 Py_RETURN_NONE;
3530}
Larry Hastings2f936352014-08-05 14:04:04 +10003531#endif /* HAVE_SYNC */
3532
Ross Lagerwall7807c352011-03-17 20:20:30 +02003533
Guido van Rossum21142a01999-01-08 21:05:37 +00003534#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003535#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003536extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3537#endif
3538
Larry Hastings2f936352014-08-05 14:04:04 +10003539/*[clinic input]
3540os.fdatasync
3541
3542 fd: fildes
3543
3544Force write of fd to disk without forcing update of metadata.
3545[clinic start generated code]*/
3546
Larry Hastings2f936352014-08-05 14:04:04 +10003547static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003548os_fdatasync_impl(PyObject *module, int fd)
3549/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003550{
3551 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003552}
3553#endif /* HAVE_FDATASYNC */
3554
3555
Fredrik Lundh10723342000-07-10 16:38:09 +00003556#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003557/*[clinic input]
3558os.chown
3559
3560 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003561 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003562
3563 uid: uid_t
3564
3565 gid: gid_t
3566
3567 *
3568
3569 dir_fd : dir_fd(requires='fchownat') = None
3570 If not None, it should be a file descriptor open to a directory,
3571 and path should be relative; path will then be relative to that
3572 directory.
3573
3574 follow_symlinks: bool = True
3575 If False, and the last element of the path is a symbolic link,
3576 stat will examine the symbolic link itself instead of the file
3577 the link points to.
3578
3579Change the owner and group id of path to the numeric uid and gid.\
3580
3581path may always be specified as a string.
3582On some platforms, path may also be specified as an open file descriptor.
3583 If this functionality is unavailable, using it raises an exception.
3584If dir_fd is not None, it should be a file descriptor open to a directory,
3585 and path should be relative; path will then be relative to that directory.
3586If follow_symlinks is False, and the last element of the path is a symbolic
3587 link, chown will modify the symbolic link itself instead of the file the
3588 link points to.
3589It is an error to use dir_fd or follow_symlinks when specifying path as
3590 an open file descriptor.
3591dir_fd and follow_symlinks may not be implemented on your platform.
3592 If they are unavailable, using them will raise a NotImplementedError.
3593
3594[clinic start generated code]*/
3595
Larry Hastings2f936352014-08-05 14:04:04 +10003596static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003597os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003598 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003599/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003600{
3601 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003602
Ronald Oussoren41761932020-11-08 10:05:27 +01003603#if defined(HAVE_FCHOWNAT)
3604 int fchownat_unsupported = 0;
3605#endif
3606
Larry Hastings9cf065c2012-06-22 16:30:09 -07003607#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3608 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003609 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003610#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003611 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3612 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3613 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003614
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003615 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid,
3616 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3617 return NULL;
3618 }
3619
Victor Stinner8c62be82010-05-06 00:08:46 +00003620 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003621#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003622 if (path->fd != -1)
3623 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003624 else
3625#endif
3626#ifdef HAVE_LCHOWN
3627 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003628 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003629 else
3630#endif
3631#ifdef HAVE_FCHOWNAT
Ronald Oussoren41761932020-11-08 10:05:27 +01003632 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) {
3633 if (HAVE_FCHOWNAT_RUNTIME) {
Larry Hastings2f936352014-08-05 14:04:04 +10003634 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003635 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
Ronald Oussoren41761932020-11-08 10:05:27 +01003636 } else {
3637 fchownat_unsupported = 1;
3638 }
3639 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07003640#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003641 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003642 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003643
Ronald Oussoren41761932020-11-08 10:05:27 +01003644#ifdef HAVE_FCHOWNAT
3645 if (fchownat_unsupported) {
3646 /* This would be incorrect if the current platform
3647 * doesn't support lchown.
3648 */
3649 argument_unavailable_error(NULL, "dir_fd");
3650 return NULL;
3651 }
3652#endif
3653
Larry Hastings2f936352014-08-05 14:04:04 +10003654 if (result)
3655 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003656
Larry Hastings2f936352014-08-05 14:04:04 +10003657 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003658}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003659#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003660
Larry Hastings2f936352014-08-05 14:04:04 +10003661
Christian Heimes4e30a842007-11-30 22:12:06 +00003662#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003663/*[clinic input]
3664os.fchown
3665
3666 fd: int
3667 uid: uid_t
3668 gid: gid_t
3669
3670Change the owner and group id of the file specified by file descriptor.
3671
3672Equivalent to os.chown(fd, uid, gid).
3673
3674[clinic start generated code]*/
3675
Larry Hastings2f936352014-08-05 14:04:04 +10003676static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003677os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3678/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003679{
Victor Stinner8c62be82010-05-06 00:08:46 +00003680 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003681 int async_err = 0;
3682
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003683 if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) {
3684 return NULL;
3685 }
3686
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003687 do {
3688 Py_BEGIN_ALLOW_THREADS
3689 res = fchown(fd, uid, gid);
3690 Py_END_ALLOW_THREADS
3691 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3692 if (res != 0)
3693 return (!async_err) ? posix_error() : NULL;
3694
Victor Stinner8c62be82010-05-06 00:08:46 +00003695 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003696}
3697#endif /* HAVE_FCHOWN */
3698
Larry Hastings2f936352014-08-05 14:04:04 +10003699
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003700#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003701/*[clinic input]
3702os.lchown
3703
3704 path : path_t
3705 uid: uid_t
3706 gid: gid_t
3707
3708Change the owner and group id of path to the numeric uid and gid.
3709
3710This function will not follow symbolic links.
3711Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3712[clinic start generated code]*/
3713
Larry Hastings2f936352014-08-05 14:04:04 +10003714static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003715os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3716/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003717{
Victor Stinner8c62be82010-05-06 00:08:46 +00003718 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003719 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) {
3720 return NULL;
3721 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003722 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003723 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003724 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003725 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003726 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003727 }
Larry Hastings2f936352014-08-05 14:04:04 +10003728 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003729}
3730#endif /* HAVE_LCHOWN */
3731
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003732
Barry Warsaw53699e91996-12-10 23:23:01 +00003733static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003734posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003735{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003736#ifdef MS_WINDOWS
Victor Stinner689830e2019-06-26 17:31:12 +02003737 wchar_t wbuf[MAXPATHLEN];
3738 wchar_t *wbuf2 = wbuf;
3739 DWORD len;
3740
3741 Py_BEGIN_ALLOW_THREADS
3742 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3743 /* If the buffer is large enough, len does not include the
3744 terminating \0. If the buffer is too small, len includes
3745 the space needed for the terminator. */
3746 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerec3e20a2019-06-28 18:01:59 +02003747 if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003748 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003749 }
Victor Stinner689830e2019-06-26 17:31:12 +02003750 else {
3751 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003752 }
Victor Stinner689830e2019-06-26 17:31:12 +02003753 if (wbuf2) {
3754 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003755 }
Victor Stinner689830e2019-06-26 17:31:12 +02003756 }
3757 Py_END_ALLOW_THREADS
3758
3759 if (!wbuf2) {
3760 PyErr_NoMemory();
3761 return NULL;
3762 }
3763 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003764 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003765 PyMem_RawFree(wbuf2);
Victor Stinner689830e2019-06-26 17:31:12 +02003766 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003767 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003768
Victor Stinner689830e2019-06-26 17:31:12 +02003769 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3770 if (wbuf2 != wbuf) {
3771 PyMem_RawFree(wbuf2);
3772 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003773
Victor Stinner689830e2019-06-26 17:31:12 +02003774 if (use_bytes) {
3775 if (resobj == NULL) {
3776 return NULL;
3777 }
3778 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3779 }
3780
3781 return resobj;
3782#else
3783 const size_t chunk = 1024;
3784
3785 char *buf = NULL;
3786 char *cwd = NULL;
3787 size_t buflen = 0;
3788
Victor Stinner8c62be82010-05-06 00:08:46 +00003789 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003790 do {
Victor Stinner689830e2019-06-26 17:31:12 +02003791 char *newbuf;
3792 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3793 buflen += chunk;
3794 newbuf = PyMem_RawRealloc(buf, buflen);
3795 }
3796 else {
3797 newbuf = NULL;
3798 }
3799 if (newbuf == NULL) {
3800 PyMem_RawFree(buf);
3801 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003802 break;
3803 }
Victor Stinner689830e2019-06-26 17:31:12 +02003804 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003805
Victor Stinner4403d7d2015-04-25 00:16:10 +02003806 cwd = getcwd(buf, buflen);
3807 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003808 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003809
Victor Stinner689830e2019-06-26 17:31:12 +02003810 if (buf == NULL) {
3811 return PyErr_NoMemory();
3812 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003813 if (cwd == NULL) {
3814 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003815 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003816 }
3817
Victor Stinner689830e2019-06-26 17:31:12 +02003818 PyObject *obj;
3819 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003820 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner689830e2019-06-26 17:31:12 +02003821 }
3822 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003823 obj = PyUnicode_DecodeFSDefault(buf);
Victor Stinner689830e2019-06-26 17:31:12 +02003824 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003825 PyMem_RawFree(buf);
3826
3827 return obj;
Victor Stinner689830e2019-06-26 17:31:12 +02003828#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003829}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003830
Larry Hastings2f936352014-08-05 14:04:04 +10003831
3832/*[clinic input]
3833os.getcwd
3834
3835Return a unicode string representing the current working directory.
3836[clinic start generated code]*/
3837
Larry Hastings2f936352014-08-05 14:04:04 +10003838static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003839os_getcwd_impl(PyObject *module)
3840/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003841{
3842 return posix_getcwd(0);
3843}
3844
Larry Hastings2f936352014-08-05 14:04:04 +10003845
3846/*[clinic input]
3847os.getcwdb
3848
3849Return a bytes string representing the current working directory.
3850[clinic start generated code]*/
3851
Larry Hastings2f936352014-08-05 14:04:04 +10003852static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003853os_getcwdb_impl(PyObject *module)
3854/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003855{
3856 return posix_getcwd(1);
3857}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003858
Larry Hastings2f936352014-08-05 14:04:04 +10003859
Larry Hastings9cf065c2012-06-22 16:30:09 -07003860#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3861#define HAVE_LINK 1
3862#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003863
Guido van Rossumb6775db1994-08-01 11:34:53 +00003864#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003865/*[clinic input]
3866
3867os.link
3868
3869 src : path_t
3870 dst : path_t
3871 *
3872 src_dir_fd : dir_fd = None
3873 dst_dir_fd : dir_fd = None
3874 follow_symlinks: bool = True
3875
3876Create a hard link to a file.
3877
3878If either src_dir_fd or dst_dir_fd is not None, it should be a file
3879 descriptor open to a directory, and the respective path string (src or dst)
3880 should be relative; the path will then be relative to that directory.
3881If follow_symlinks is False, and the last element of src is a symbolic
3882 link, link will create a link to the symbolic link itself instead of the
3883 file the link points to.
3884src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3885 platform. If they are unavailable, using them will raise a
3886 NotImplementedError.
3887[clinic start generated code]*/
3888
Larry Hastings2f936352014-08-05 14:04:04 +10003889static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003890os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003891 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003892/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003893{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003894#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003895 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003896#else
3897 int result;
3898#endif
Ronald Oussoren41761932020-11-08 10:05:27 +01003899#if defined(HAVE_LINKAT)
3900 int linkat_unavailable = 0;
3901#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003902
Larry Hastings9cf065c2012-06-22 16:30:09 -07003903#ifndef HAVE_LINKAT
3904 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3905 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003906 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003907 }
3908#endif
3909
Steve Dowercc16be82016-09-08 10:35:16 -07003910#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003911 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003912 PyErr_SetString(PyExc_NotImplementedError,
3913 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003914 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003915 }
Steve Dowercc16be82016-09-08 10:35:16 -07003916#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003917
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003918 if (PySys_Audit("os.link", "OOii", src->object, dst->object,
3919 src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
3920 dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
3921 return NULL;
3922 }
3923
Brian Curtin1b9df392010-11-24 20:24:31 +00003924#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003925 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003926 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003927 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003928
Larry Hastings2f936352014-08-05 14:04:04 +10003929 if (!result)
3930 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003931#else
3932 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003933#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003934 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3935 (dst_dir_fd != DEFAULT_DIR_FD) ||
Ronald Oussoren41761932020-11-08 10:05:27 +01003936 (!follow_symlinks)) {
3937
3938 if (HAVE_LINKAT_RUNTIME) {
3939
3940 result = linkat(src_dir_fd, src->narrow,
3941 dst_dir_fd, dst->narrow,
3942 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3943
3944 }
3945#ifdef __APPLE__
3946 else {
3947 if (src_dir_fd == DEFAULT_DIR_FD && dst_dir_fd == DEFAULT_DIR_FD) {
3948 /* See issue 41355: This matches the behaviour of !HAVE_LINKAT */
3949 result = link(src->narrow, dst->narrow);
3950 } else {
3951 linkat_unavailable = 1;
3952 }
3953 }
3954#endif
3955 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003956 else
Steve Dowercc16be82016-09-08 10:35:16 -07003957#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003958 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003959 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003960
Ronald Oussoren41761932020-11-08 10:05:27 +01003961#ifdef HAVE_LINKAT
3962 if (linkat_unavailable) {
3963 /* Either or both dir_fd arguments were specified */
3964 if (src_dir_fd != DEFAULT_DIR_FD) {
3965 argument_unavailable_error("link", "src_dir_fd");
3966 } else {
3967 argument_unavailable_error("link", "dst_dir_fd");
3968 }
3969 return NULL;
3970 }
3971#endif
3972
Larry Hastings2f936352014-08-05 14:04:04 +10003973 if (result)
3974 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003975#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003976
Larry Hastings2f936352014-08-05 14:04:04 +10003977 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003978}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003979#endif
3980
Brian Curtin1b9df392010-11-24 20:24:31 +00003981
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003982#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003983static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003984_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003985{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003986 PyObject *v;
3987 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3988 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003989 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003990 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003991 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003992 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003993
Steve Dowercc16be82016-09-08 10:35:16 -07003994 WIN32_FIND_DATAW wFileData;
3995 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003996
Steve Dowercc16be82016-09-08 10:35:16 -07003997 if (!path->wide) { /* Default arg: "." */
3998 po_wchars = L".";
3999 len = 1;
4000 } else {
4001 po_wchars = path->wide;
4002 len = wcslen(path->wide);
4003 }
4004 /* The +5 is so we can append "\\*.*\0" */
4005 wnamebuf = PyMem_New(wchar_t, len + 5);
4006 if (!wnamebuf) {
4007 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07004008 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004009 }
Steve Dowercc16be82016-09-08 10:35:16 -07004010 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00004011 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07004012 wchar_t wch = wnamebuf[len-1];
4013 if (wch != SEP && wch != ALTSEP && wch != L':')
4014 wnamebuf[len++] = SEP;
4015 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00004016 }
Steve Dowercc16be82016-09-08 10:35:16 -07004017 if ((list = PyList_New(0)) == NULL) {
4018 goto exit;
4019 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00004020 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004021 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00004022 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00004023 if (hFindFile == INVALID_HANDLE_VALUE) {
4024 int error = GetLastError();
4025 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004026 goto exit;
4027 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07004028 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004029 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004030 }
4031 do {
4032 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07004033 if (wcscmp(wFileData.cFileName, L".") != 0 &&
4034 wcscmp(wFileData.cFileName, L"..") != 0) {
4035 v = PyUnicode_FromWideChar(wFileData.cFileName,
4036 wcslen(wFileData.cFileName));
4037 if (path->narrow && v) {
4038 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
4039 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004040 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004041 Py_DECREF(list);
4042 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004043 break;
4044 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004045 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004046 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004047 Py_DECREF(list);
4048 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004049 break;
4050 }
4051 Py_DECREF(v);
4052 }
4053 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004054 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00004055 Py_END_ALLOW_THREADS
4056 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
4057 it got to the end of the directory. */
4058 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004059 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07004060 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004061 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004062 }
4063 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004064
Larry Hastings9cf065c2012-06-22 16:30:09 -07004065exit:
4066 if (hFindFile != INVALID_HANDLE_VALUE) {
4067 if (FindClose(hFindFile) == FALSE) {
4068 if (list != NULL) {
4069 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07004070 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004071 }
4072 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004073 }
Victor Stinnerb6404912013-07-07 16:21:41 +02004074 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004075
Larry Hastings9cf065c2012-06-22 16:30:09 -07004076 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07004077} /* end of _listdir_windows_no_opendir */
4078
4079#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
4080
4081static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07004082_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07004083{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07004084 PyObject *v;
4085 DIR *dirp = NULL;
4086 struct dirent *ep;
4087 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07004088#ifdef HAVE_FDOPENDIR
4089 int fd = -1;
4090#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07004091
Victor Stinner8c62be82010-05-06 00:08:46 +00004092 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004093#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07004094 if (path->fd != -1) {
Ronald Oussoren41761932020-11-08 10:05:27 +01004095 if (HAVE_FDOPENDIR_RUNTIME) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004096 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02004097 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01004098 if (fd == -1)
4099 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004100
Larry Hastingsfdaea062012-06-25 04:42:23 -07004101 return_str = 1;
4102
Larry Hastings9cf065c2012-06-22 16:30:09 -07004103 Py_BEGIN_ALLOW_THREADS
4104 dirp = fdopendir(fd);
4105 Py_END_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +01004106 } else {
4107 PyErr_SetString(PyExc_TypeError,
4108 "listdir: path should be string, bytes, os.PathLike or None, not int");
4109 return NULL;
4110 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004111 }
4112 else
4113#endif
4114 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004115 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07004116 if (path->narrow) {
4117 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03004118 /* only return bytes if they specified a bytes-like object */
4119 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07004120 }
4121 else {
4122 name = ".";
4123 return_str = 1;
4124 }
4125
Larry Hastings9cf065c2012-06-22 16:30:09 -07004126 Py_BEGIN_ALLOW_THREADS
4127 dirp = opendir(name);
4128 Py_END_ALLOW_THREADS
4129 }
4130
4131 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07004132 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07004133#ifdef HAVE_FDOPENDIR
4134 if (fd != -1) {
4135 Py_BEGIN_ALLOW_THREADS
4136 close(fd);
4137 Py_END_ALLOW_THREADS
4138 }
4139#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004140 goto exit;
4141 }
4142 if ((list = PyList_New(0)) == NULL) {
4143 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004144 }
4145 for (;;) {
4146 errno = 0;
4147 Py_BEGIN_ALLOW_THREADS
4148 ep = readdir(dirp);
4149 Py_END_ALLOW_THREADS
4150 if (ep == NULL) {
4151 if (errno == 0) {
4152 break;
4153 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004154 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07004155 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004156 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004157 }
4158 }
4159 if (ep->d_name[0] == '.' &&
4160 (NAMLEN(ep) == 1 ||
4161 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
4162 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07004163 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00004164 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
4165 else
4166 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00004167 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004168 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00004169 break;
4170 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004171 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004172 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004173 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00004174 break;
4175 }
4176 Py_DECREF(v);
4177 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00004178
Larry Hastings9cf065c2012-06-22 16:30:09 -07004179exit:
4180 if (dirp != NULL) {
4181 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07004182#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07004183 if (fd > -1)
4184 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07004185#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004186 closedir(dirp);
4187 Py_END_ALLOW_THREADS
4188 }
4189
Larry Hastings9cf065c2012-06-22 16:30:09 -07004190 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07004191} /* end of _posix_listdir */
4192#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004193
Larry Hastings2f936352014-08-05 14:04:04 +10004194
4195/*[clinic input]
4196os.listdir
4197
4198 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
4199
4200Return a list containing the names of the files in the directory.
4201
BNMetricsb9427072018-11-02 15:20:19 +00004202path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10004203 the filenames returned will also be bytes; in all other circumstances
4204 the filenames returned will be str.
4205If path is None, uses the path='.'.
4206On some platforms, path may also be specified as an open file descriptor;\
4207 the file descriptor must refer to a directory.
4208 If this functionality is unavailable, using it raises NotImplementedError.
4209
4210The list is in arbitrary order. It does not include the special
4211entries '.' and '..' even if they are present in the directory.
4212
4213
4214[clinic start generated code]*/
4215
Larry Hastings2f936352014-08-05 14:04:04 +10004216static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004217os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00004218/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004219{
Steve Dower60419a72019-06-24 08:42:54 -07004220 if (PySys_Audit("os.listdir", "O",
4221 path->object ? path->object : Py_None) < 0) {
4222 return NULL;
4223 }
Larry Hastings2f936352014-08-05 14:04:04 +10004224#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
4225 return _listdir_windows_no_opendir(path, NULL);
4226#else
4227 return _posix_listdir(path, NULL);
4228#endif
4229}
4230
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004231#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00004232/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03004233/*[clinic input]
4234os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02004235
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03004236 path: path_t
4237 /
4238
4239[clinic start generated code]*/
4240
4241static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004242os__getfullpathname_impl(PyObject *module, path_t *path)
4243/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03004244{
Victor Stinner3939c322019-06-25 15:02:43 +02004245 wchar_t *abspath;
Victor Stinnereb5657a2011-09-30 01:44:27 +02004246
Victor Stinner3939c322019-06-25 15:02:43 +02004247 /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */
4248 if (_Py_abspath(path->wide, &abspath) < 0) {
4249 return win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00004250 }
Victor Stinner3939c322019-06-25 15:02:43 +02004251 if (abspath == NULL) {
4252 return PyErr_NoMemory();
4253 }
4254
4255 PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
4256 PyMem_RawFree(abspath);
4257 if (str == NULL) {
4258 return NULL;
4259 }
4260 if (path->narrow) {
4261 Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
4262 }
4263 return str;
Larry Hastings2f936352014-08-05 14:04:04 +10004264}
Brian Curtind40e6f72010-07-08 21:39:08 +00004265
Brian Curtind25aef52011-06-13 15:16:04 -05004266
Larry Hastings2f936352014-08-05 14:04:04 +10004267/*[clinic input]
4268os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00004269
Steve Dower23ad6d02018-02-22 10:39:10 -08004270 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004271 /
4272
4273A helper function for samepath on windows.
4274[clinic start generated code]*/
4275
Larry Hastings2f936352014-08-05 14:04:04 +10004276static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004277os__getfinalpathname_impl(PyObject *module, path_t *path)
4278/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00004279{
4280 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004281 wchar_t buf[MAXPATHLEN], *target_path = buf;
4282 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00004283 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10004284 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004285
Steve Dower23ad6d02018-02-22 10:39:10 -08004286 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004287 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08004288 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00004289 0, /* desired access */
4290 0, /* share mode */
4291 NULL, /* security attributes */
4292 OPEN_EXISTING,
4293 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
4294 FILE_FLAG_BACKUP_SEMANTICS,
4295 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004296 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004297
Steve Dower23ad6d02018-02-22 10:39:10 -08004298 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004299 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08004300 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004301
4302 /* We have a good handle to the target, use it to determine the
4303 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004304 while (1) {
4305 Py_BEGIN_ALLOW_THREADS
4306 result_length = GetFinalPathNameByHandleW(hFile, target_path,
4307 buf_size, VOLUME_NAME_DOS);
4308 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004309
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004310 if (!result_length) {
4311 result = win32_error_object("GetFinalPathNameByHandleW",
4312 path->object);
4313 goto cleanup;
4314 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004315
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004316 if (result_length < buf_size) {
4317 break;
4318 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004319
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004320 wchar_t *tmp;
4321 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
4322 result_length * sizeof(*tmp));
4323 if (!tmp) {
4324 result = PyErr_NoMemory();
4325 goto cleanup;
4326 }
4327
4328 buf_size = result_length;
4329 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08004330 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004331
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004332 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004333 if (result && path->narrow) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004334 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004335 }
Steve Dower23ad6d02018-02-22 10:39:10 -08004336
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004337cleanup:
4338 if (target_path != buf) {
4339 PyMem_Free(target_path);
4340 }
4341 CloseHandle(hFile);
4342 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10004343}
Brian Curtin62857742010-09-06 17:07:27 +00004344
Tim Golden6b528062013-08-01 12:44:00 +01004345
Larry Hastings2f936352014-08-05 14:04:04 +10004346/*[clinic input]
4347os._getvolumepathname
4348
Steve Dower23ad6d02018-02-22 10:39:10 -08004349 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004350
4351A helper function for ismount on Win32.
4352[clinic start generated code]*/
4353
Larry Hastings2f936352014-08-05 14:04:04 +10004354static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004355os__getvolumepathname_impl(PyObject *module, path_t *path)
4356/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004357{
4358 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004359 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01004360 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01004361 BOOL ret;
4362
Tim Golden6b528062013-08-01 12:44:00 +01004363 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08004364 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01004365
Victor Stinner850a18e2017-10-24 16:53:32 -07004366 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01004367 PyErr_SetString(PyExc_OverflowError, "path too long");
4368 return NULL;
4369 }
4370
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004371 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01004372 if (mountpath == NULL)
4373 return PyErr_NoMemory();
4374
4375 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08004376 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01004377 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01004378 Py_END_ALLOW_THREADS
4379
4380 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004381 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01004382 goto exit;
4383 }
4384 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08004385 if (path->narrow)
4386 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01004387
4388exit:
4389 PyMem_Free(mountpath);
4390 return result;
4391}
Tim Golden6b528062013-08-01 12:44:00 +01004392
Steve Dower04732ca2021-04-07 01:02:07 +01004393
4394/*[clinic input]
4395os._path_splitroot
4396
4397 path: path_t
4398
4399Removes everything after the root on Win32.
4400[clinic start generated code]*/
4401
4402static PyObject *
4403os__path_splitroot_impl(PyObject *module, path_t *path)
4404/*[clinic end generated code: output=ab7f1a88b654581c input=dc93b1d3984cffb6]*/
4405{
4406 wchar_t *buffer;
4407 wchar_t *end;
4408 PyObject *result = NULL;
4409 HRESULT ret;
4410
4411 buffer = (wchar_t*)PyMem_Malloc(sizeof(wchar_t) * (wcslen(path->wide) + 1));
4412 if (!buffer) {
4413 return NULL;
4414 }
4415 wcscpy(buffer, path->wide);
4416 for (wchar_t *p = wcschr(buffer, L'/'); p; p = wcschr(p, L'/')) {
4417 *p = L'\\';
4418 }
4419
4420 Py_BEGIN_ALLOW_THREADS
4421 ret = PathCchSkipRoot(buffer, &end);
4422 Py_END_ALLOW_THREADS
4423 if (FAILED(ret)) {
4424 result = Py_BuildValue("sO", "", path->object);
4425 } else if (end != buffer) {
4426 size_t rootLen = (size_t)(end - buffer);
4427 result = Py_BuildValue("NN",
4428 PyUnicode_FromWideChar(path->wide, rootLen),
4429 PyUnicode_FromWideChar(path->wide + rootLen, -1)
4430 );
4431 } else {
4432 result = Py_BuildValue("Os", path->object, "");
4433 }
4434 PyMem_Free(buffer);
4435
4436 return result;
4437}
4438
4439
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004440#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004441
Larry Hastings2f936352014-08-05 14:04:04 +10004442
4443/*[clinic input]
4444os.mkdir
4445
4446 path : path_t
4447
4448 mode: int = 0o777
4449
4450 *
4451
4452 dir_fd : dir_fd(requires='mkdirat') = None
4453
4454# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4455
4456Create a directory.
4457
4458If dir_fd is not None, it should be a file descriptor open to a directory,
4459 and path should be relative; path will then be relative to that directory.
4460dir_fd may not be implemented on your platform.
4461 If it is unavailable, using it will raise a NotImplementedError.
4462
4463The mode argument is ignored on Windows.
4464[clinic start generated code]*/
4465
Larry Hastings2f936352014-08-05 14:04:04 +10004466static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004467os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4468/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004469{
4470 int result;
Ronald Oussoren41761932020-11-08 10:05:27 +01004471#ifdef HAVE_MKDIRAT
4472 int mkdirat_unavailable = 0;
4473#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004474
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004475 if (PySys_Audit("os.mkdir", "Oii", path->object, mode,
4476 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4477 return NULL;
4478 }
4479
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004480#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004481 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004482 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004483 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004484
Larry Hastings2f936352014-08-05 14:04:04 +10004485 if (!result)
4486 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004487#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004488 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004489#if HAVE_MKDIRAT
Ronald Oussoren41761932020-11-08 10:05:27 +01004490 if (dir_fd != DEFAULT_DIR_FD) {
4491 if (HAVE_MKDIRAT_RUNTIME) {
Larry Hastings2f936352014-08-05 14:04:04 +10004492 result = mkdirat(dir_fd, path->narrow, mode);
Ronald Oussoren41761932020-11-08 10:05:27 +01004493
4494 } else {
4495 mkdirat_unavailable = 1;
4496 }
4497 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004498#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004499#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004500 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004501#else
Larry Hastings2f936352014-08-05 14:04:04 +10004502 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004503#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004504 Py_END_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +01004505
4506#if HAVE_MKDIRAT
4507 if (mkdirat_unavailable) {
4508 argument_unavailable_error(NULL, "dir_fd");
4509 return NULL;
4510 }
4511#endif
4512
Larry Hastings2f936352014-08-05 14:04:04 +10004513 if (result < 0)
4514 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004515#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004516 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004517}
4518
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004519
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004520/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4521#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004522#include <sys/resource.h>
4523#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004524
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004525
4526#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004527/*[clinic input]
4528os.nice
4529
4530 increment: int
4531 /
4532
4533Add increment to the priority of process and return the new priority.
4534[clinic start generated code]*/
4535
Larry Hastings2f936352014-08-05 14:04:04 +10004536static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004537os_nice_impl(PyObject *module, int increment)
4538/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004539{
4540 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004541
Victor Stinner8c62be82010-05-06 00:08:46 +00004542 /* There are two flavours of 'nice': one that returns the new
4543 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004544 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004545 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004546
Victor Stinner8c62be82010-05-06 00:08:46 +00004547 If we are of the nice family that returns the new priority, we
4548 need to clear errno before the call, and check if errno is filled
4549 before calling posix_error() on a returnvalue of -1, because the
4550 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004551
Victor Stinner8c62be82010-05-06 00:08:46 +00004552 errno = 0;
4553 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004554#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004555 if (value == 0)
4556 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004557#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004558 if (value == -1 && errno != 0)
4559 /* either nice() or getpriority() returned an error */
4560 return posix_error();
4561 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004562}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004563#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004564
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004565
4566#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004567/*[clinic input]
4568os.getpriority
4569
4570 which: int
4571 who: int
4572
4573Return program scheduling priority.
4574[clinic start generated code]*/
4575
Larry Hastings2f936352014-08-05 14:04:04 +10004576static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004577os_getpriority_impl(PyObject *module, int which, int who)
4578/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004579{
4580 int retval;
4581
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004582 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004583 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004584 if (errno != 0)
4585 return posix_error();
4586 return PyLong_FromLong((long)retval);
4587}
4588#endif /* HAVE_GETPRIORITY */
4589
4590
4591#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004592/*[clinic input]
4593os.setpriority
4594
4595 which: int
4596 who: int
4597 priority: int
4598
4599Set program scheduling priority.
4600[clinic start generated code]*/
4601
Larry Hastings2f936352014-08-05 14:04:04 +10004602static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004603os_setpriority_impl(PyObject *module, int which, int who, int priority)
4604/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004605{
4606 int retval;
4607
4608 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004609 if (retval == -1)
4610 return posix_error();
4611 Py_RETURN_NONE;
4612}
4613#endif /* HAVE_SETPRIORITY */
4614
4615
Barry Warsaw53699e91996-12-10 23:23:01 +00004616static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004617internal_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 +00004618{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004619 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004620 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004621
Ronald Oussoren41761932020-11-08 10:05:27 +01004622#ifdef HAVE_RENAMEAT
4623 int renameat_unavailable = 0;
4624#endif
4625
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004626#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004627 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004628 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004629#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004630 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004631#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004632
Larry Hastings9cf065c2012-06-22 16:30:09 -07004633 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4634 (dst_dir_fd != DEFAULT_DIR_FD);
4635#ifndef HAVE_RENAMEAT
4636 if (dir_fd_specified) {
4637 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004638 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004639 }
4640#endif
4641
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004642 if (PySys_Audit("os.rename", "OOii", src->object, dst->object,
4643 src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
4644 dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
4645 return NULL;
4646 }
4647
Larry Hastings9cf065c2012-06-22 16:30:09 -07004648#ifdef MS_WINDOWS
4649 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004650 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004651 Py_END_ALLOW_THREADS
4652
Larry Hastings2f936352014-08-05 14:04:04 +10004653 if (!result)
4654 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004655
4656#else
Steve Dowercc16be82016-09-08 10:35:16 -07004657 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4658 PyErr_Format(PyExc_ValueError,
4659 "%s: src and dst must be the same type", function_name);
4660 return NULL;
4661 }
4662
Larry Hastings9cf065c2012-06-22 16:30:09 -07004663 Py_BEGIN_ALLOW_THREADS
4664#ifdef HAVE_RENAMEAT
Ronald Oussoren41761932020-11-08 10:05:27 +01004665 if (dir_fd_specified) {
4666 if (HAVE_RENAMEAT_RUNTIME) {
4667 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
4668 } else {
4669 renameat_unavailable = 1;
4670 }
4671 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004672#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004673 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004674 Py_END_ALLOW_THREADS
4675
Ronald Oussoren41761932020-11-08 10:05:27 +01004676
4677#ifdef HAVE_RENAMEAT
4678 if (renameat_unavailable) {
4679 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
4680 return NULL;
4681 }
4682#endif
4683
Larry Hastings2f936352014-08-05 14:04:04 +10004684 if (result)
4685 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004686#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004687 Py_RETURN_NONE;
4688}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004689
Larry Hastings2f936352014-08-05 14:04:04 +10004690
4691/*[clinic input]
4692os.rename
4693
4694 src : path_t
4695 dst : path_t
4696 *
4697 src_dir_fd : dir_fd = None
4698 dst_dir_fd : dir_fd = None
4699
4700Rename a file or directory.
4701
4702If either src_dir_fd or dst_dir_fd is not None, it should be a file
4703 descriptor open to a directory, and the respective path string (src or dst)
4704 should be relative; the path will then be relative to that directory.
4705src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4706 If they are unavailable, using them will raise a NotImplementedError.
4707[clinic start generated code]*/
4708
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004709static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004710os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004711 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004712/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004713{
Larry Hastings2f936352014-08-05 14:04:04 +10004714 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004715}
4716
Larry Hastings2f936352014-08-05 14:04:04 +10004717
4718/*[clinic input]
4719os.replace = os.rename
4720
4721Rename a file or directory, overwriting the destination.
4722
4723If either src_dir_fd or dst_dir_fd is not None, it should be a file
4724 descriptor open to a directory, and the respective path string (src or dst)
4725 should be relative; the path will then be relative to that directory.
4726src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004727 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004728[clinic start generated code]*/
4729
Larry Hastings2f936352014-08-05 14:04:04 +10004730static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004731os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4732 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004733/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004734{
4735 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4736}
4737
4738
4739/*[clinic input]
4740os.rmdir
4741
4742 path: path_t
4743 *
4744 dir_fd: dir_fd(requires='unlinkat') = None
4745
4746Remove a directory.
4747
4748If dir_fd is not None, it should be a file descriptor open to a directory,
4749 and path should be relative; path will then be relative to that directory.
4750dir_fd may not be implemented on your platform.
4751 If it is unavailable, using it will raise a NotImplementedError.
4752[clinic start generated code]*/
4753
Larry Hastings2f936352014-08-05 14:04:04 +10004754static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004755os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4756/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004757{
4758 int result;
Ronald Oussoren41761932020-11-08 10:05:27 +01004759#ifdef HAVE_UNLINKAT
4760 int unlinkat_unavailable = 0;
4761#endif
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004762
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004763 if (PySys_Audit("os.rmdir", "Oi", path->object,
4764 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4765 return NULL;
4766 }
4767
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004768 Py_BEGIN_ALLOW_THREADS
4769#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004770 /* Windows, success=1, UNIX, success=0 */
4771 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004772#else
4773#ifdef HAVE_UNLINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +01004774 if (dir_fd != DEFAULT_DIR_FD) {
4775 if (HAVE_UNLINKAT_RUNTIME) {
Larry Hastings2f936352014-08-05 14:04:04 +10004776 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Ronald Oussoren41761932020-11-08 10:05:27 +01004777 } else {
4778 unlinkat_unavailable = 1;
4779 result = -1;
4780 }
4781 } else
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004782#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004783 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004784#endif
4785 Py_END_ALLOW_THREADS
4786
Ronald Oussoren41761932020-11-08 10:05:27 +01004787#ifdef HAVE_UNLINKAT
4788 if (unlinkat_unavailable) {
4789 argument_unavailable_error("rmdir", "dir_fd");
4790 return NULL;
4791 }
4792#endif
4793
Larry Hastings2f936352014-08-05 14:04:04 +10004794 if (result)
4795 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004796
Larry Hastings2f936352014-08-05 14:04:04 +10004797 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004798}
4799
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004800
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004801#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004802#ifdef MS_WINDOWS
4803/*[clinic input]
4804os.system -> long
4805
4806 command: Py_UNICODE
4807
4808Execute the command in a subshell.
4809[clinic start generated code]*/
4810
Larry Hastings2f936352014-08-05 14:04:04 +10004811static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004812os_system_impl(PyObject *module, const Py_UNICODE *command)
4813/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004814{
4815 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004816
Steve Dowerfbe3c762019-10-18 00:52:15 -07004817 if (PySys_Audit("os.system", "(u)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004818 return -1;
4819 }
4820
Victor Stinner8c62be82010-05-06 00:08:46 +00004821 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004822 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004823 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004824 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004825 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004826 return result;
4827}
4828#else /* MS_WINDOWS */
4829/*[clinic input]
4830os.system -> long
4831
4832 command: FSConverter
4833
4834Execute the command in a subshell.
4835[clinic start generated code]*/
4836
Larry Hastings2f936352014-08-05 14:04:04 +10004837static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004838os_system_impl(PyObject *module, PyObject *command)
4839/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004840{
4841 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004842 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004843
Steve Dowerfbe3c762019-10-18 00:52:15 -07004844 if (PySys_Audit("os.system", "(O)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004845 return -1;
4846 }
4847
Larry Hastings2f936352014-08-05 14:04:04 +10004848 Py_BEGIN_ALLOW_THREADS
4849 result = system(bytes);
4850 Py_END_ALLOW_THREADS
4851 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004852}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004853#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004854#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004855
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004856
Larry Hastings2f936352014-08-05 14:04:04 +10004857/*[clinic input]
4858os.umask
4859
4860 mask: int
4861 /
4862
4863Set the current numeric umask and return the previous umask.
4864[clinic start generated code]*/
4865
Larry Hastings2f936352014-08-05 14:04:04 +10004866static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004867os_umask_impl(PyObject *module, int mask)
4868/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004869{
4870 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004871 if (i < 0)
4872 return posix_error();
4873 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004874}
4875
Brian Curtind40e6f72010-07-08 21:39:08 +00004876#ifdef MS_WINDOWS
4877
4878/* override the default DeleteFileW behavior so that directory
4879symlinks can be removed with this function, the same as with
4880Unix symlinks */
4881BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4882{
4883 WIN32_FILE_ATTRIBUTE_DATA info;
4884 WIN32_FIND_DATAW find_data;
4885 HANDLE find_data_handle;
4886 int is_directory = 0;
4887 int is_link = 0;
4888
4889 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4890 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004891
Brian Curtind40e6f72010-07-08 21:39:08 +00004892 /* Get WIN32_FIND_DATA structure for the path to determine if
4893 it is a symlink */
4894 if(is_directory &&
4895 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4896 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4897
4898 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004899 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4900 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4901 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4902 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004903 FindClose(find_data_handle);
4904 }
4905 }
4906 }
4907
4908 if (is_directory && is_link)
4909 return RemoveDirectoryW(lpFileName);
4910
4911 return DeleteFileW(lpFileName);
4912}
4913#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004914
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004915
Larry Hastings2f936352014-08-05 14:04:04 +10004916/*[clinic input]
4917os.unlink
4918
4919 path: path_t
4920 *
4921 dir_fd: dir_fd(requires='unlinkat')=None
4922
4923Remove a file (same as remove()).
4924
4925If dir_fd is not None, it should be a file descriptor open to a directory,
4926 and path should be relative; path will then be relative to that directory.
4927dir_fd may not be implemented on your platform.
4928 If it is unavailable, using it will raise a NotImplementedError.
4929
4930[clinic start generated code]*/
4931
Larry Hastings2f936352014-08-05 14:04:04 +10004932static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004933os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4934/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004935{
4936 int result;
Ronald Oussoren41761932020-11-08 10:05:27 +01004937#ifdef HAVE_UNLINKAT
4938 int unlinkat_unavailable = 0;
4939#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004940
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004941 if (PySys_Audit("os.remove", "Oi", path->object,
4942 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4943 return NULL;
4944 }
4945
Larry Hastings9cf065c2012-06-22 16:30:09 -07004946 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004947 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004948#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004949 /* Windows, success=1, UNIX, success=0 */
4950 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004951#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004952#ifdef HAVE_UNLINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +01004953 if (dir_fd != DEFAULT_DIR_FD) {
4954 if (HAVE_UNLINKAT_RUNTIME) {
4955
Larry Hastings2f936352014-08-05 14:04:04 +10004956 result = unlinkat(dir_fd, path->narrow, 0);
Ronald Oussoren41761932020-11-08 10:05:27 +01004957 } else {
4958 unlinkat_unavailable = 1;
4959 }
4960 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004961#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004962 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004963#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004964 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004965 Py_END_ALLOW_THREADS
4966
Ronald Oussoren41761932020-11-08 10:05:27 +01004967#ifdef HAVE_UNLINKAT
4968 if (unlinkat_unavailable) {
4969 argument_unavailable_error(NULL, "dir_fd");
4970 return NULL;
4971 }
4972#endif
4973
Larry Hastings2f936352014-08-05 14:04:04 +10004974 if (result)
4975 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004976
Larry Hastings2f936352014-08-05 14:04:04 +10004977 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004978}
4979
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004980
Larry Hastings2f936352014-08-05 14:04:04 +10004981/*[clinic input]
4982os.remove = os.unlink
4983
4984Remove a file (same as unlink()).
4985
4986If dir_fd is not None, it should be a file descriptor open to a directory,
4987 and path should be relative; path will then be relative to that directory.
4988dir_fd may not be implemented on your platform.
4989 If it is unavailable, using it will raise a NotImplementedError.
4990[clinic start generated code]*/
4991
Larry Hastings2f936352014-08-05 14:04:04 +10004992static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004993os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4994/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004995{
4996 return os_unlink_impl(module, path, dir_fd);
4997}
4998
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004999
Larry Hastings605a62d2012-06-24 04:33:36 -07005000static PyStructSequence_Field uname_result_fields[] = {
5001 {"sysname", "operating system name"},
5002 {"nodename", "name of machine on network (implementation-defined)"},
5003 {"release", "operating system release"},
5004 {"version", "operating system version"},
5005 {"machine", "hardware identifier"},
5006 {NULL}
5007};
5008
5009PyDoc_STRVAR(uname_result__doc__,
5010"uname_result: Result from os.uname().\n\n\
5011This object may be accessed either as a tuple of\n\
5012 (sysname, nodename, release, version, machine),\n\
5013or via the attributes sysname, nodename, release, version, and machine.\n\
5014\n\
5015See os.uname for more information.");
5016
5017static PyStructSequence_Desc uname_result_desc = {
Eddie Elizondob3966632019-11-05 07:16:14 -08005018 MODNAME ".uname_result", /* name */
Larry Hastings605a62d2012-06-24 04:33:36 -07005019 uname_result__doc__, /* doc */
5020 uname_result_fields,
5021 5
5022};
5023
Larry Hastings605a62d2012-06-24 04:33:36 -07005024#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10005025/*[clinic input]
5026os.uname
5027
5028Return an object identifying the current operating system.
5029
5030The object behaves like a named tuple with the following fields:
5031 (sysname, nodename, release, version, machine)
5032
5033[clinic start generated code]*/
5034
Larry Hastings2f936352014-08-05 14:04:04 +10005035static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005036os_uname_impl(PyObject *module)
5037/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00005038{
Victor Stinner8c62be82010-05-06 00:08:46 +00005039 struct utsname u;
5040 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07005041 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00005042
Victor Stinner8c62be82010-05-06 00:08:46 +00005043 Py_BEGIN_ALLOW_THREADS
5044 res = uname(&u);
5045 Py_END_ALLOW_THREADS
5046 if (res < 0)
5047 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07005048
Hai Shif707d942020-03-16 21:15:01 +08005049 PyObject *UnameResultType = get_posix_state(module)->UnameResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08005050 value = PyStructSequence_New((PyTypeObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07005051 if (value == NULL)
5052 return NULL;
5053
5054#define SET(i, field) \
5055 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02005056 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07005057 if (!o) { \
5058 Py_DECREF(value); \
5059 return NULL; \
5060 } \
5061 PyStructSequence_SET_ITEM(value, i, o); \
5062 } \
5063
5064 SET(0, u.sysname);
5065 SET(1, u.nodename);
5066 SET(2, u.release);
5067 SET(3, u.version);
5068 SET(4, u.machine);
5069
5070#undef SET
5071
5072 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00005073}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005074#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00005075
Larry Hastings9e3e70b2011-09-08 19:29:07 -07005076
Larry Hastings9cf065c2012-06-22 16:30:09 -07005077
5078typedef struct {
5079 int now;
5080 time_t atime_s;
5081 long atime_ns;
5082 time_t mtime_s;
5083 long mtime_ns;
5084} utime_t;
5085
5086/*
Victor Stinner484df002014-10-09 13:52:31 +02005087 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07005088 * they also intentionally leak the declaration of a pointer named "time"
5089 */
5090#define UTIME_TO_TIMESPEC \
5091 struct timespec ts[2]; \
5092 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02005093 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005094 time = NULL; \
5095 else { \
Victor Stinner484df002014-10-09 13:52:31 +02005096 ts[0].tv_sec = ut->atime_s; \
5097 ts[0].tv_nsec = ut->atime_ns; \
5098 ts[1].tv_sec = ut->mtime_s; \
5099 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005100 time = ts; \
5101 } \
5102
5103#define UTIME_TO_TIMEVAL \
5104 struct timeval tv[2]; \
5105 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02005106 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005107 time = NULL; \
5108 else { \
Victor Stinner484df002014-10-09 13:52:31 +02005109 tv[0].tv_sec = ut->atime_s; \
5110 tv[0].tv_usec = ut->atime_ns / 1000; \
5111 tv[1].tv_sec = ut->mtime_s; \
5112 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005113 time = tv; \
5114 } \
5115
5116#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02005117 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005118 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02005119 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005120 time = NULL; \
5121 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02005122 u.actime = ut->atime_s; \
5123 u.modtime = ut->mtime_s; \
5124 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005125 }
5126
5127#define UTIME_TO_TIME_T \
5128 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02005129 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02005130 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005131 time = NULL; \
5132 else { \
Victor Stinner484df002014-10-09 13:52:31 +02005133 timet[0] = ut->atime_s; \
5134 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02005135 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005136 } \
5137
5138
Victor Stinner528a9ab2015-09-03 21:30:26 +02005139#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005140
5141static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02005142utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005143{
Ronald Oussoren41761932020-11-08 10:05:27 +01005144#if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
5145 if (HAVE_UTIMENSAT_RUNTIME) {
5146 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
5147 UTIME_TO_TIMESPEC;
5148 return utimensat(dir_fd, path, time, flags);
5149 } else {
5150 errno = ENOSYS;
5151 return -1;
5152 }
5153#elif defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005154 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
5155 UTIME_TO_TIMESPEC;
5156 return utimensat(dir_fd, path, time, flags);
5157#elif defined(HAVE_FUTIMESAT)
5158 UTIME_TO_TIMEVAL;
5159 /*
5160 * follow_symlinks will never be false here;
5161 * we only allow !follow_symlinks and dir_fd together
5162 * if we have utimensat()
5163 */
5164 assert(follow_symlinks);
5165 return futimesat(dir_fd, path, time);
5166#endif
5167}
5168
Larry Hastings2f936352014-08-05 14:04:04 +10005169 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
5170#else
5171 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07005172#endif
5173
Victor Stinner528a9ab2015-09-03 21:30:26 +02005174#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005175
5176static int
Victor Stinner484df002014-10-09 13:52:31 +02005177utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005178{
5179#ifdef HAVE_FUTIMENS
Ronald Oussoren41761932020-11-08 10:05:27 +01005180
5181 if (HAVE_FUTIMENS_RUNTIME) {
5182
Larry Hastings9cf065c2012-06-22 16:30:09 -07005183 UTIME_TO_TIMESPEC;
5184 return futimens(fd, time);
Ronald Oussoren41761932020-11-08 10:05:27 +01005185
5186 } else
5187#ifndef HAVE_FUTIMES
5188 {
5189 /* Not sure if this can happen */
5190 PyErr_SetString(
5191 PyExc_RuntimeError,
5192 "neither futimens nor futimes are supported"
5193 " on this system");
5194 return -1;
5195 }
5196#endif
5197
5198#endif
5199#ifdef HAVE_FUTIMES
5200 {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005201 UTIME_TO_TIMEVAL;
5202 return futimes(fd, time);
Ronald Oussoren41761932020-11-08 10:05:27 +01005203 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005204#endif
5205}
5206
Larry Hastings2f936352014-08-05 14:04:04 +10005207 #define PATH_UTIME_HAVE_FD 1
5208#else
5209 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07005210#endif
5211
Victor Stinner5ebae872015-09-22 01:29:33 +02005212#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
5213# define UTIME_HAVE_NOFOLLOW_SYMLINKS
5214#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07005215
Victor Stinner4552ced2015-09-21 22:37:15 +02005216#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005217
5218static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02005219utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005220{
5221#ifdef HAVE_UTIMENSAT
Ronald Oussoren41761932020-11-08 10:05:27 +01005222 if (HAVE_UTIMENSAT_RUNTIME) {
5223 UTIME_TO_TIMESPEC;
5224 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
5225 } else
5226#ifndef HAVE_LUTIMES
5227 {
5228 /* Not sure if this can happen */
5229 PyErr_SetString(
5230 PyExc_RuntimeError,
5231 "neither utimensat nor lutimes are supported"
5232 " on this system");
5233 return -1;
5234 }
5235#endif
5236#endif
5237
5238#ifdef HAVE_LUTIMES
5239 {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005240 UTIME_TO_TIMEVAL;
5241 return lutimes(path, time);
Ronald Oussoren41761932020-11-08 10:05:27 +01005242 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005243#endif
5244}
5245
5246#endif
5247
5248#ifndef MS_WINDOWS
5249
5250static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02005251utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005252{
Ronald Oussoren41761932020-11-08 10:05:27 +01005253#if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
5254 if (HAVE_UTIMENSAT_RUNTIME) {
5255 UTIME_TO_TIMESPEC;
5256 return utimensat(DEFAULT_DIR_FD, path, time, 0);
5257 } else {
5258 UTIME_TO_TIMEVAL;
5259 return utimes(path, time);
5260 }
5261#elif defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005262 UTIME_TO_TIMESPEC;
5263 return utimensat(DEFAULT_DIR_FD, path, time, 0);
5264#elif defined(HAVE_UTIMES)
5265 UTIME_TO_TIMEVAL;
5266 return utimes(path, time);
5267#elif defined(HAVE_UTIME_H)
5268 UTIME_TO_UTIMBUF;
5269 return utime(path, time);
5270#else
5271 UTIME_TO_TIME_T;
5272 return utime(path, time);
5273#endif
5274}
5275
5276#endif
5277
Larry Hastings76ad59b2012-05-03 00:30:07 -07005278static int
Victor Stinner1c2fa782020-05-10 11:05:29 +02005279split_py_long_to_s_and_ns(PyObject *module, PyObject *py_long, time_t *s, long *ns)
Larry Hastings76ad59b2012-05-03 00:30:07 -07005280{
5281 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04005282 PyObject *divmod;
Victor Stinner1c2fa782020-05-10 11:05:29 +02005283 divmod = PyNumber_Divmod(py_long, get_posix_state(module)->billion);
Larry Hastings76ad59b2012-05-03 00:30:07 -07005284 if (!divmod)
5285 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03005286 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
5287 PyErr_Format(PyExc_TypeError,
5288 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -08005289 _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod)));
Oren Milman0bd1a2d2018-09-12 22:14:35 +03005290 goto exit;
5291 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07005292 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
5293 if ((*s == -1) && PyErr_Occurred())
5294 goto exit;
5295 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04005296 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07005297 goto exit;
5298
5299 result = 1;
5300exit:
5301 Py_XDECREF(divmod);
5302 return result;
5303}
5304
Larry Hastings2f936352014-08-05 14:04:04 +10005305
5306/*[clinic input]
5307os.utime
5308
5309 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
Serhiy Storchaka279f4462019-09-14 12:24:05 +03005310 times: object = None
Larry Hastings2f936352014-08-05 14:04:04 +10005311 *
5312 ns: object = NULL
5313 dir_fd: dir_fd(requires='futimensat') = None
5314 follow_symlinks: bool=True
5315
Martin Panter0ff89092015-09-09 01:56:53 +00005316# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10005317
5318Set the access and modified time of path.
5319
5320path may always be specified as a string.
5321On some platforms, path may also be specified as an open file descriptor.
5322 If this functionality is unavailable, using it raises an exception.
5323
5324If times is not None, it must be a tuple (atime, mtime);
5325 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00005326If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10005327 atime_ns and mtime_ns should be expressed as integer nanoseconds
5328 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00005329If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10005330Specifying tuples for both times and ns is an error.
5331
5332If dir_fd is not None, it should be a file descriptor open to a directory,
5333 and path should be relative; path will then be relative to that directory.
5334If follow_symlinks is False, and the last element of the path is a symbolic
5335 link, utime will modify the symbolic link itself instead of the file the
5336 link points to.
5337It is an error to use dir_fd or follow_symlinks when specifying path
5338 as an open file descriptor.
5339dir_fd and follow_symlinks may not be available on your platform.
5340 If they are unavailable, using them will raise a NotImplementedError.
5341
5342[clinic start generated code]*/
5343
Larry Hastings2f936352014-08-05 14:04:04 +10005344static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005345os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
5346 int dir_fd, int follow_symlinks)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03005347/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005348{
Larry Hastings9cf065c2012-06-22 16:30:09 -07005349#ifdef MS_WINDOWS
5350 HANDLE hFile;
5351 FILETIME atime, mtime;
5352#else
5353 int result;
5354#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07005355
Larry Hastings2f936352014-08-05 14:04:04 +10005356 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005357
Christian Heimesb3c87242013-08-01 00:08:16 +02005358 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07005359
Serhiy Storchaka279f4462019-09-14 12:24:05 +03005360 if (times != Py_None && ns) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005361 PyErr_SetString(PyExc_ValueError,
5362 "utime: you may specify either 'times'"
5363 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005364 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005365 }
5366
Serhiy Storchaka279f4462019-09-14 12:24:05 +03005367 if (times != Py_None) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02005368 time_t a_sec, m_sec;
5369 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005370 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005371 PyErr_SetString(PyExc_TypeError,
5372 "utime: 'times' must be either"
5373 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005374 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005375 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005376 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04005377 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02005378 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04005379 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02005380 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005381 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07005382 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02005383 utime.atime_s = a_sec;
5384 utime.atime_ns = a_nsec;
5385 utime.mtime_s = m_sec;
5386 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005387 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005388 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07005389 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005390 PyErr_SetString(PyExc_TypeError,
5391 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005392 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005393 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005394 utime.now = 0;
Victor Stinner1c2fa782020-05-10 11:05:29 +02005395 if (!split_py_long_to_s_and_ns(module, PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07005396 &utime.atime_s, &utime.atime_ns) ||
Victor Stinner1c2fa782020-05-10 11:05:29 +02005397 !split_py_long_to_s_and_ns(module, PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07005398 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005399 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07005400 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005401 }
5402 else {
5403 /* times and ns are both None/unspecified. use "now". */
5404 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005405 }
5406
Victor Stinner4552ced2015-09-21 22:37:15 +02005407#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005408 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005409 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07005410#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04005411
Larry Hastings2f936352014-08-05 14:04:04 +10005412 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
5413 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
5414 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005415 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005416
Larry Hastings9cf065c2012-06-22 16:30:09 -07005417#if !defined(HAVE_UTIMENSAT)
5418 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02005419 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005420 "utime: cannot use dir_fd and follow_symlinks "
5421 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005422 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07005423 }
5424#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07005425
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005426 if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None,
5427 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
5428 return NULL;
5429 }
5430
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005431#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005432 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07005433 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
5434 NULL, OPEN_EXISTING,
5435 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005436 Py_END_ALLOW_THREADS
5437 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10005438 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005439 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07005440 }
5441
Larry Hastings9cf065c2012-06-22 16:30:09 -07005442 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01005443 GetSystemTimeAsFileTime(&mtime);
5444 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00005445 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005446 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08005447 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
5448 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00005449 }
5450 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
5451 /* Avoid putting the file name into the error here,
5452 as that may confuse the user into believing that
5453 something is wrong with the file, when it also
5454 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01005455 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005456 CloseHandle(hFile);
5457 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005458 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005459 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005460#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005461 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00005462
Victor Stinner4552ced2015-09-21 22:37:15 +02005463#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005464 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10005465 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005466 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07005467#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07005468
Victor Stinner528a9ab2015-09-03 21:30:26 +02005469#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Ronald Oussoren41761932020-11-08 10:05:27 +01005470 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) {
Larry Hastings2f936352014-08-05 14:04:04 +10005471 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Ronald Oussoren41761932020-11-08 10:05:27 +01005472
5473 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07005474#endif
5475
Victor Stinner528a9ab2015-09-03 21:30:26 +02005476#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10005477 if (path->fd != -1)
5478 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005479 else
5480#endif
5481
Larry Hastings2f936352014-08-05 14:04:04 +10005482 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005483
5484 Py_END_ALLOW_THREADS
5485
Ronald Oussoren41761932020-11-08 10:05:27 +01005486#if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
5487 /* See utime_dir_fd implementation */
5488 if (result == -1 && errno == ENOSYS) {
5489 argument_unavailable_error(NULL, "dir_fd");
5490 return NULL;
5491 }
5492#endif
5493
Larry Hastings9cf065c2012-06-22 16:30:09 -07005494 if (result < 0) {
5495 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005496 posix_error();
5497 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005498 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07005499
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005500#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005501
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005502 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005503}
5504
Guido van Rossum3b066191991-06-04 19:40:25 +00005505/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005506
Larry Hastings2f936352014-08-05 14:04:04 +10005507
5508/*[clinic input]
5509os._exit
5510
5511 status: int
5512
5513Exit to the system with specified status, without normal exit processing.
5514[clinic start generated code]*/
5515
Larry Hastings2f936352014-08-05 14:04:04 +10005516static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005517os__exit_impl(PyObject *module, int status)
5518/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005519{
5520 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00005521 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005522}
5523
Steve Dowercc16be82016-09-08 10:35:16 -07005524#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5525#define EXECV_CHAR wchar_t
5526#else
5527#define EXECV_CHAR char
5528#endif
5529
pxinwrf2d7ac72019-05-21 18:46:37 +08005530#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005531static void
Steve Dowercc16be82016-09-08 10:35:16 -07005532free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005533{
Victor Stinner8c62be82010-05-06 00:08:46 +00005534 Py_ssize_t i;
5535 for (i = 0; i < count; i++)
5536 PyMem_Free(array[i]);
Victor Stinner00d7abd2020-12-01 09:56:42 +01005537 PyMem_Free(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005538}
Martin v. Löwis011e8422009-05-05 04:43:17 +00005539
Berker Peksag81816462016-09-15 20:19:47 +03005540static int
5541fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00005542{
Victor Stinner8c62be82010-05-06 00:08:46 +00005543 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03005544 PyObject *ub;
5545 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07005546#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03005547 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07005548 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005549 *out = PyUnicode_AsWideCharString(ub, &size);
5550 if (*out)
5551 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07005552#else
Berker Peksag81816462016-09-15 20:19:47 +03005553 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00005554 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005555 size = PyBytes_GET_SIZE(ub);
5556 *out = PyMem_Malloc(size + 1);
5557 if (*out) {
5558 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
5559 result = 1;
5560 } else
Victor Stinner50abf222013-11-07 23:56:10 +01005561 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07005562#endif
Berker Peksag81816462016-09-15 20:19:47 +03005563 Py_DECREF(ub);
5564 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005565}
Martin v. Löwis114619e2002-10-07 06:44:21 +00005566#endif
5567
pxinwrf2d7ac72019-05-21 18:46:37 +08005568#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07005569static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00005570parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
5571{
Victor Stinner8c62be82010-05-06 00:08:46 +00005572 Py_ssize_t i, pos, envc;
5573 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03005574 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07005575 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005576
Victor Stinner8c62be82010-05-06 00:08:46 +00005577 i = PyMapping_Size(env);
5578 if (i < 0)
5579 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07005580 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005581 if (envlist == NULL) {
5582 PyErr_NoMemory();
5583 return NULL;
5584 }
5585 envc = 0;
5586 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005587 if (!keys)
5588 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005589 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005590 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005591 goto error;
5592 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5593 PyErr_Format(PyExc_TypeError,
5594 "env.keys() or env.values() is not a list");
5595 goto error;
5596 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005597
Victor Stinner8c62be82010-05-06 00:08:46 +00005598 for (pos = 0; pos < i; pos++) {
5599 key = PyList_GetItem(keys, pos);
5600 val = PyList_GetItem(vals, pos);
5601 if (!key || !val)
5602 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005603
Berker Peksag81816462016-09-15 20:19:47 +03005604#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5605 if (!PyUnicode_FSDecoder(key, &key2))
5606 goto error;
5607 if (!PyUnicode_FSDecoder(val, &val2)) {
5608 Py_DECREF(key2);
5609 goto error;
5610 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005611 /* Search from index 1 because on Windows starting '=' is allowed for
5612 defining hidden environment variables. */
5613 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5614 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5615 {
5616 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005617 Py_DECREF(key2);
5618 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005619 goto error;
5620 }
Berker Peksag81816462016-09-15 20:19:47 +03005621 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5622#else
5623 if (!PyUnicode_FSConverter(key, &key2))
5624 goto error;
5625 if (!PyUnicode_FSConverter(val, &val2)) {
5626 Py_DECREF(key2);
5627 goto error;
5628 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005629 if (PyBytes_GET_SIZE(key2) == 0 ||
5630 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5631 {
5632 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005633 Py_DECREF(key2);
5634 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005635 goto error;
5636 }
Berker Peksag81816462016-09-15 20:19:47 +03005637 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5638 PyBytes_AS_STRING(val2));
5639#endif
5640 Py_DECREF(key2);
5641 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005642 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005643 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005644
5645 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5646 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005647 goto error;
5648 }
Berker Peksag81816462016-09-15 20:19:47 +03005649
Steve Dowercc16be82016-09-08 10:35:16 -07005650 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005651 }
5652 Py_DECREF(vals);
5653 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005654
Victor Stinner8c62be82010-05-06 00:08:46 +00005655 envlist[envc] = 0;
5656 *envc_ptr = envc;
5657 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005658
5659error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005660 Py_XDECREF(keys);
5661 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005662 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005663 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005664}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005665
Steve Dowercc16be82016-09-08 10:35:16 -07005666static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005667parse_arglist(PyObject* argv, Py_ssize_t *argc)
5668{
5669 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005670 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005671 if (argvlist == NULL) {
5672 PyErr_NoMemory();
5673 return NULL;
5674 }
5675 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005676 PyObject* item = PySequence_ITEM(argv, i);
5677 if (item == NULL)
5678 goto fail;
5679 if (!fsconvert_strdup(item, &argvlist[i])) {
5680 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005681 goto fail;
5682 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005683 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005684 }
5685 argvlist[*argc] = NULL;
5686 return argvlist;
5687fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005688 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005689 free_string_array(argvlist, *argc);
5690 return NULL;
5691}
Steve Dowercc16be82016-09-08 10:35:16 -07005692
Ross Lagerwall7807c352011-03-17 20:20:30 +02005693#endif
5694
Larry Hastings2f936352014-08-05 14:04:04 +10005695
Ross Lagerwall7807c352011-03-17 20:20:30 +02005696#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005697/*[clinic input]
5698os.execv
5699
Steve Dowercc16be82016-09-08 10:35:16 -07005700 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005701 Path of executable file.
5702 argv: object
5703 Tuple or list of strings.
5704 /
5705
5706Execute an executable path with arguments, replacing current process.
5707[clinic start generated code]*/
5708
Larry Hastings2f936352014-08-05 14:04:04 +10005709static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005710os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5711/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005712{
Steve Dowercc16be82016-09-08 10:35:16 -07005713 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005714 Py_ssize_t argc;
5715
5716 /* execv has two arguments: (path, argv), where
5717 argv is a list or tuple of strings. */
5718
Ross Lagerwall7807c352011-03-17 20:20:30 +02005719 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5720 PyErr_SetString(PyExc_TypeError,
5721 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005722 return NULL;
5723 }
5724 argc = PySequence_Size(argv);
5725 if (argc < 1) {
5726 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005727 return NULL;
5728 }
5729
5730 argvlist = parse_arglist(argv, &argc);
5731 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005732 return NULL;
5733 }
Steve Dowerbce26262016-11-19 19:17:26 -08005734 if (!argvlist[0][0]) {
5735 PyErr_SetString(PyExc_ValueError,
5736 "execv() arg 2 first element cannot be empty");
5737 free_string_array(argvlist, argc);
5738 return NULL;
5739 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005740
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005741 if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005742 free_string_array(argvlist, argc);
5743 return NULL;
5744 }
5745
Steve Dowerbce26262016-11-19 19:17:26 -08005746 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005747#ifdef HAVE_WEXECV
5748 _wexecv(path->wide, argvlist);
5749#else
5750 execv(path->narrow, argvlist);
5751#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005752 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005753
5754 /* If we get here it's definitely an error */
5755
5756 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005757 return posix_error();
5758}
5759
Larry Hastings2f936352014-08-05 14:04:04 +10005760
5761/*[clinic input]
5762os.execve
5763
5764 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5765 Path of executable file.
5766 argv: object
5767 Tuple or list of strings.
5768 env: object
5769 Dictionary of strings mapping to strings.
5770
5771Execute an executable path with arguments, replacing current process.
5772[clinic start generated code]*/
5773
Larry Hastings2f936352014-08-05 14:04:04 +10005774static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005775os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5776/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005777{
Steve Dowercc16be82016-09-08 10:35:16 -07005778 EXECV_CHAR **argvlist = NULL;
5779 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005780 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005781
Victor Stinner8c62be82010-05-06 00:08:46 +00005782 /* execve has three arguments: (path, argv, env), where
5783 argv is a list or tuple of strings and env is a dictionary
5784 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005785
Ross Lagerwall7807c352011-03-17 20:20:30 +02005786 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005787 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005788 "execve: argv must be a tuple or list");
Saiyang Gou95f60012020-02-04 16:15:00 -08005789 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005790 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005791 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005792 if (argc < 1) {
5793 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5794 return NULL;
5795 }
5796
Victor Stinner8c62be82010-05-06 00:08:46 +00005797 if (!PyMapping_Check(env)) {
5798 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005799 "execve: environment must be a mapping object");
Saiyang Gou95f60012020-02-04 16:15:00 -08005800 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005801 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005802
Ross Lagerwall7807c352011-03-17 20:20:30 +02005803 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005804 if (argvlist == NULL) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005805 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005806 }
Steve Dowerbce26262016-11-19 19:17:26 -08005807 if (!argvlist[0][0]) {
5808 PyErr_SetString(PyExc_ValueError,
5809 "execve: argv first element cannot be empty");
Saiyang Gou95f60012020-02-04 16:15:00 -08005810 goto fail_0;
Steve Dowerbce26262016-11-19 19:17:26 -08005811 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005812
Victor Stinner8c62be82010-05-06 00:08:46 +00005813 envlist = parse_envlist(env, &envc);
5814 if (envlist == NULL)
Saiyang Gou95f60012020-02-04 16:15:00 -08005815 goto fail_0;
5816
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005817 if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005818 goto fail_1;
5819 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005820
Steve Dowerbce26262016-11-19 19:17:26 -08005821 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005822#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005823 if (path->fd > -1)
5824 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005825 else
5826#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005827#ifdef HAVE_WEXECV
5828 _wexecve(path->wide, argvlist, envlist);
5829#else
Larry Hastings2f936352014-08-05 14:04:04 +10005830 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005831#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005832 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005833
5834 /* If we get here it's definitely an error */
5835
Alexey Izbyshev83460312018-10-20 03:28:22 +03005836 posix_path_error(path);
Saiyang Gou95f60012020-02-04 16:15:00 -08005837 fail_1:
Steve Dowercc16be82016-09-08 10:35:16 -07005838 free_string_array(envlist, envc);
Saiyang Gou95f60012020-02-04 16:15:00 -08005839 fail_0:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005840 if (argvlist)
5841 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005842 return NULL;
5843}
Steve Dowercc16be82016-09-08 10:35:16 -07005844
Larry Hastings9cf065c2012-06-22 16:30:09 -07005845#endif /* HAVE_EXECV */
5846
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005847#ifdef HAVE_POSIX_SPAWN
5848
5849enum posix_spawn_file_actions_identifier {
5850 POSIX_SPAWN_OPEN,
5851 POSIX_SPAWN_CLOSE,
5852 POSIX_SPAWN_DUP2
5853};
5854
William Orr81574b82018-10-01 22:19:56 -07005855#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005856static int
Victor Stinner1c2fa782020-05-10 11:05:29 +02005857convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005858#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005859
5860static int
Victor Stinner1c2fa782020-05-10 11:05:29 +02005861parse_posix_spawn_flags(PyObject *module, const char *func_name, PyObject *setpgroup,
Victor Stinner325e4ba2019-02-01 15:47:24 +01005862 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005863 PyObject *setsigdef, PyObject *scheduler,
5864 posix_spawnattr_t *attrp)
5865{
5866 long all_flags = 0;
5867
5868 errno = posix_spawnattr_init(attrp);
5869 if (errno) {
5870 posix_error();
5871 return -1;
5872 }
5873
5874 if (setpgroup) {
5875 pid_t pgid = PyLong_AsPid(setpgroup);
5876 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5877 goto fail;
5878 }
5879 errno = posix_spawnattr_setpgroup(attrp, pgid);
5880 if (errno) {
5881 posix_error();
5882 goto fail;
5883 }
5884 all_flags |= POSIX_SPAWN_SETPGROUP;
5885 }
5886
5887 if (resetids) {
5888 all_flags |= POSIX_SPAWN_RESETIDS;
5889 }
5890
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005891 if (setsid) {
Ronald Oussoren41761932020-11-08 10:05:27 +01005892#ifdef HAVE_POSIX_SPAWN_SETSID_RUNTIME
5893 if (HAVE_POSIX_SPAWN_SETSID_RUNTIME) {
5894#endif
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005895#ifdef POSIX_SPAWN_SETSID
5896 all_flags |= POSIX_SPAWN_SETSID;
5897#elif defined(POSIX_SPAWN_SETSID_NP)
5898 all_flags |= POSIX_SPAWN_SETSID_NP;
5899#else
5900 argument_unavailable_error(func_name, "setsid");
5901 return -1;
5902#endif
Ronald Oussoren41761932020-11-08 10:05:27 +01005903
5904#ifdef HAVE_POSIX_SPAWN_SETSID_RUNTIME
5905 } else {
5906 argument_unavailable_error(func_name, "setsid");
5907 return -1;
5908 }
5909#endif /* HAVE_POSIX_SPAWN_SETSID_RUNTIME */
5910
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005911 }
5912
Pablo Galindo254a4662018-09-07 16:44:24 +01005913 if (setsigmask) {
5914 sigset_t set;
5915 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5916 goto fail;
5917 }
5918 errno = posix_spawnattr_setsigmask(attrp, &set);
5919 if (errno) {
5920 posix_error();
5921 goto fail;
5922 }
5923 all_flags |= POSIX_SPAWN_SETSIGMASK;
5924 }
5925
5926 if (setsigdef) {
5927 sigset_t set;
5928 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5929 goto fail;
5930 }
5931 errno = posix_spawnattr_setsigdefault(attrp, &set);
5932 if (errno) {
5933 posix_error();
5934 goto fail;
5935 }
5936 all_flags |= POSIX_SPAWN_SETSIGDEF;
5937 }
5938
5939 if (scheduler) {
5940#ifdef POSIX_SPAWN_SETSCHEDULER
5941 PyObject *py_schedpolicy;
Victor Stinner1c2fa782020-05-10 11:05:29 +02005942 PyObject *schedparam_obj;
Pablo Galindo254a4662018-09-07 16:44:24 +01005943 struct sched_param schedparam;
5944
Victor Stinner1c2fa782020-05-10 11:05:29 +02005945 if (!PyArg_ParseTuple(scheduler, "OO"
Pablo Galindo254a4662018-09-07 16:44:24 +01005946 ";A scheduler tuple must have two elements",
Victor Stinner1c2fa782020-05-10 11:05:29 +02005947 &py_schedpolicy, &schedparam_obj)) {
5948 goto fail;
5949 }
5950 if (!convert_sched_param(module, schedparam_obj, &schedparam)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005951 goto fail;
5952 }
5953 if (py_schedpolicy != Py_None) {
5954 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5955
5956 if (schedpolicy == -1 && PyErr_Occurred()) {
5957 goto fail;
5958 }
5959 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5960 if (errno) {
5961 posix_error();
5962 goto fail;
5963 }
5964 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5965 }
5966 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5967 if (errno) {
5968 posix_error();
5969 goto fail;
5970 }
5971 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5972#else
5973 PyErr_SetString(PyExc_NotImplementedError,
5974 "The scheduler option is not supported in this system.");
5975 goto fail;
5976#endif
5977 }
5978
5979 errno = posix_spawnattr_setflags(attrp, all_flags);
5980 if (errno) {
5981 posix_error();
5982 goto fail;
5983 }
5984
5985 return 0;
5986
5987fail:
5988 (void)posix_spawnattr_destroy(attrp);
5989 return -1;
5990}
5991
5992static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005993parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005994 posix_spawn_file_actions_t *file_actionsp,
5995 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005996{
5997 PyObject *seq;
5998 PyObject *file_action = NULL;
5999 PyObject *tag_obj;
6000
6001 seq = PySequence_Fast(file_actions,
6002 "file_actions must be a sequence or None");
6003 if (seq == NULL) {
6004 return -1;
6005 }
6006
6007 errno = posix_spawn_file_actions_init(file_actionsp);
6008 if (errno) {
6009 posix_error();
6010 Py_DECREF(seq);
6011 return -1;
6012 }
6013
Zackery Spytzd52a83a2019-06-26 14:54:20 -06006014 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03006015 file_action = PySequence_Fast_GET_ITEM(seq, i);
6016 Py_INCREF(file_action);
6017 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
6018 PyErr_SetString(PyExc_TypeError,
6019 "Each file_actions element must be a non-empty tuple");
6020 goto fail;
6021 }
6022 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
6023 if (tag == -1 && PyErr_Occurred()) {
6024 goto fail;
6025 }
6026
6027 /* Populate the file_actions object */
6028 switch (tag) {
6029 case POSIX_SPAWN_OPEN: {
6030 int fd, oflag;
6031 PyObject *path;
6032 unsigned long mode;
6033 if (!PyArg_ParseTuple(file_action, "OiO&ik"
6034 ";A open file_action tuple must have 5 elements",
6035 &tag_obj, &fd, PyUnicode_FSConverter, &path,
6036 &oflag, &mode))
6037 {
6038 goto fail;
6039 }
Pablo Galindocb970732018-06-19 09:19:50 +01006040 if (PyList_Append(temp_buffer, path)) {
6041 Py_DECREF(path);
6042 goto fail;
6043 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03006044 errno = posix_spawn_file_actions_addopen(file_actionsp,
6045 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01006046 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03006047 if (errno) {
6048 posix_error();
6049 goto fail;
6050 }
6051 break;
6052 }
6053 case POSIX_SPAWN_CLOSE: {
6054 int fd;
6055 if (!PyArg_ParseTuple(file_action, "Oi"
6056 ";A close file_action tuple must have 2 elements",
6057 &tag_obj, &fd))
6058 {
6059 goto fail;
6060 }
6061 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
6062 if (errno) {
6063 posix_error();
6064 goto fail;
6065 }
6066 break;
6067 }
6068 case POSIX_SPAWN_DUP2: {
6069 int fd1, fd2;
6070 if (!PyArg_ParseTuple(file_action, "Oii"
6071 ";A dup2 file_action tuple must have 3 elements",
6072 &tag_obj, &fd1, &fd2))
6073 {
6074 goto fail;
6075 }
6076 errno = posix_spawn_file_actions_adddup2(file_actionsp,
6077 fd1, fd2);
6078 if (errno) {
6079 posix_error();
6080 goto fail;
6081 }
6082 break;
6083 }
6084 default: {
6085 PyErr_SetString(PyExc_TypeError,
6086 "Unknown file_actions identifier");
6087 goto fail;
6088 }
6089 }
6090 Py_DECREF(file_action);
6091 }
Pablo Galindo254a4662018-09-07 16:44:24 +01006092
Serhiy Storchakaef347532018-05-01 16:45:04 +03006093 Py_DECREF(seq);
6094 return 0;
6095
6096fail:
6097 Py_DECREF(seq);
6098 Py_DECREF(file_action);
6099 (void)posix_spawn_file_actions_destroy(file_actionsp);
6100 return -1;
6101}
6102
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006103
6104static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006105py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
6106 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006107 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006108 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006109{
Victor Stinner325e4ba2019-02-01 15:47:24 +01006110 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006111 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006112 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03006113 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006114 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01006115 posix_spawnattr_t attr;
6116 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006117 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03006118 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01006119 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03006120 pid_t pid;
6121 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006122
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006123 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03006124 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006125 like posix.environ. */
6126
Serhiy Storchakaef347532018-05-01 16:45:04 +03006127 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01006128 PyErr_Format(PyExc_TypeError,
6129 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006130 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006131 }
6132 argc = PySequence_Size(argv);
6133 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01006134 PyErr_Format(PyExc_ValueError,
6135 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006136 return NULL;
6137 }
6138
6139 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01006140 PyErr_Format(PyExc_TypeError,
6141 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006142 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006143 }
6144
6145 argvlist = parse_arglist(argv, &argc);
6146 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006147 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006148 }
6149 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01006150 PyErr_Format(PyExc_ValueError,
6151 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006152 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006153 }
6154
6155 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006156 if (envlist == NULL) {
6157 goto exit;
6158 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006159
Anthony Shaw948ed8c2019-05-10 12:00:06 +10006160 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01006161 /* There is a bug in old versions of glibc that makes some of the
6162 * helper functions for manipulating file actions not copy the provided
6163 * buffers. The problem is that posix_spawn_file_actions_addopen does not
6164 * copy the value of path for some old versions of glibc (<2.20).
6165 * The use of temp_buffer here is a workaround that keeps the
6166 * python objects that own the buffers alive until posix_spawn gets called.
6167 * Check https://bugs.python.org/issue33630 and
6168 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
6169 temp_buffer = PyList_New(0);
6170 if (!temp_buffer) {
6171 goto exit;
6172 }
6173 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006174 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006175 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03006176 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006177 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006178
Victor Stinner1c2fa782020-05-10 11:05:29 +02006179 if (parse_posix_spawn_flags(module, func_name, setpgroup, resetids, setsid,
Victor Stinner325e4ba2019-02-01 15:47:24 +01006180 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01006181 goto exit;
6182 }
6183 attrp = &attr;
6184
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006185 if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08006186 goto exit;
6187 }
6188
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006189 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006190#ifdef HAVE_POSIX_SPAWNP
6191 if (use_posix_spawnp) {
6192 err_code = posix_spawnp(&pid, path->narrow,
6193 file_actionsp, attrp, argvlist, envlist);
6194 }
6195 else
6196#endif /* HAVE_POSIX_SPAWNP */
6197 {
6198 err_code = posix_spawn(&pid, path->narrow,
6199 file_actionsp, attrp, argvlist, envlist);
6200 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006201 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006202
Serhiy Storchakaef347532018-05-01 16:45:04 +03006203 if (err_code) {
6204 errno = err_code;
6205 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006206 goto exit;
6207 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006208#ifdef _Py_MEMORY_SANITIZER
6209 __msan_unpoison(&pid, sizeof(pid));
6210#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006211 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006212
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006213exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03006214 if (file_actionsp) {
6215 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006216 }
Pablo Galindo254a4662018-09-07 16:44:24 +01006217 if (attrp) {
6218 (void)posix_spawnattr_destroy(attrp);
6219 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006220 if (envlist) {
6221 free_string_array(envlist, envc);
6222 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006223 if (argvlist) {
6224 free_string_array(argvlist, argc);
6225 }
Pablo Galindocb970732018-06-19 09:19:50 +01006226 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006227 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006228}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006229
6230
6231/*[clinic input]
6232
6233os.posix_spawn
6234 path: path_t
6235 Path of executable file.
6236 argv: object
6237 Tuple or list of strings.
6238 env: object
6239 Dictionary of strings mapping to strings.
6240 /
6241 *
6242 file_actions: object(c_default='NULL') = ()
6243 A sequence of file action tuples.
6244 setpgroup: object = NULL
6245 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
6246 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006247 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
6248 setsid: bool(accept={int}) = False
6249 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006250 setsigmask: object(c_default='NULL') = ()
6251 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
6252 setsigdef: object(c_default='NULL') = ()
6253 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
6254 scheduler: object = NULL
6255 A tuple with the scheduler policy (optional) and parameters.
6256
6257Execute the program specified by path in a new process.
6258[clinic start generated code]*/
6259
6260static PyObject *
6261os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
6262 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006263 PyObject *setpgroup, int resetids, int setsid,
6264 PyObject *setsigmask, PyObject *setsigdef,
6265 PyObject *scheduler)
6266/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006267{
6268 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006269 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006270 scheduler);
6271}
6272 #endif /* HAVE_POSIX_SPAWN */
6273
6274
6275
6276#ifdef HAVE_POSIX_SPAWNP
6277/*[clinic input]
6278
6279os.posix_spawnp
6280 path: path_t
6281 Path of executable file.
6282 argv: object
6283 Tuple or list of strings.
6284 env: object
6285 Dictionary of strings mapping to strings.
6286 /
6287 *
6288 file_actions: object(c_default='NULL') = ()
6289 A sequence of file action tuples.
6290 setpgroup: object = NULL
6291 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
6292 resetids: bool(accept={int}) = False
6293 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006294 setsid: bool(accept={int}) = False
6295 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006296 setsigmask: object(c_default='NULL') = ()
6297 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
6298 setsigdef: object(c_default='NULL') = ()
6299 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
6300 scheduler: object = NULL
6301 A tuple with the scheduler policy (optional) and parameters.
6302
6303Execute the program specified by path in a new process.
6304[clinic start generated code]*/
6305
6306static PyObject *
6307os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
6308 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006309 PyObject *setpgroup, int resetids, int setsid,
6310 PyObject *setsigmask, PyObject *setsigdef,
6311 PyObject *scheduler)
6312/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006313{
6314 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006315 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006316 scheduler);
6317}
6318#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006319
pxinwrf2d7ac72019-05-21 18:46:37 +08006320#ifdef HAVE_RTPSPAWN
6321static intptr_t
6322_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
6323 const char *envp[])
6324{
6325 RTP_ID rtpid;
6326 int status;
6327 pid_t res;
6328 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006329
pxinwrf2d7ac72019-05-21 18:46:37 +08006330 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
6331 uStackSize=0 cannot be used, the default stack size is too small for
6332 Python. */
6333 if (envp) {
6334 rtpid = rtpSpawn(rtpFileName, argv, envp,
6335 100, 0x1000000, 0, VX_FP_TASK);
6336 }
6337 else {
6338 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
6339 100, 0x1000000, 0, VX_FP_TASK);
6340 }
6341 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
6342 do {
6343 res = waitpid((pid_t)rtpid, &status, 0);
6344 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6345
6346 if (res < 0)
6347 return RTP_ID_ERROR;
6348 return ((intptr_t)status);
6349 }
6350 return ((intptr_t)rtpid);
6351}
6352#endif
6353
6354#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10006355/*[clinic input]
6356os.spawnv
6357
6358 mode: int
6359 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07006360 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10006361 Path of executable file.
6362 argv: object
6363 Tuple or list of strings.
6364 /
6365
6366Execute the program specified by path in a new process.
6367[clinic start generated code]*/
6368
Larry Hastings2f936352014-08-05 14:04:04 +10006369static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07006370os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
6371/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006372{
Steve Dowercc16be82016-09-08 10:35:16 -07006373 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10006374 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00006375 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07006376 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00006377 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00006378
Victor Stinner8c62be82010-05-06 00:08:46 +00006379 /* spawnv has three arguments: (mode, path, argv), where
6380 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00006381
Victor Stinner8c62be82010-05-06 00:08:46 +00006382 if (PyList_Check(argv)) {
6383 argc = PyList_Size(argv);
6384 getitem = PyList_GetItem;
6385 }
6386 else if (PyTuple_Check(argv)) {
6387 argc = PyTuple_Size(argv);
6388 getitem = PyTuple_GetItem;
6389 }
6390 else {
6391 PyErr_SetString(PyExc_TypeError,
6392 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00006393 return NULL;
6394 }
Steve Dower859fd7b2016-11-19 18:53:19 -08006395 if (argc == 0) {
6396 PyErr_SetString(PyExc_ValueError,
6397 "spawnv() arg 2 cannot be empty");
6398 return NULL;
6399 }
Guido van Rossuma1065681999-01-25 23:20:23 +00006400
Steve Dowercc16be82016-09-08 10:35:16 -07006401 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00006402 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006403 return PyErr_NoMemory();
6404 }
6405 for (i = 0; i < argc; i++) {
6406 if (!fsconvert_strdup((*getitem)(argv, i),
6407 &argvlist[i])) {
6408 free_string_array(argvlist, i);
6409 PyErr_SetString(
6410 PyExc_TypeError,
6411 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00006412 return NULL;
6413 }
Steve Dower93ff8722016-11-19 19:03:54 -08006414 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02006415 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08006416 PyErr_SetString(
6417 PyExc_ValueError,
6418 "spawnv() arg 2 first element cannot be empty");
6419 return NULL;
6420 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006421 }
6422 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00006423
pxinwrf2d7ac72019-05-21 18:46:37 +08006424#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00006425 if (mode == _OLD_P_OVERLAY)
6426 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08006427#endif
Tim Peters5aa91602002-01-30 05:46:57 +00006428
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006429 if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv,
Saiyang Gou95f60012020-02-04 16:15:00 -08006430 Py_None) < 0) {
6431 free_string_array(argvlist, argc);
6432 return NULL;
6433 }
6434
Victor Stinner8c62be82010-05-06 00:08:46 +00006435 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07006436 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07006437#ifdef HAVE_WSPAWNV
6438 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08006439#elif defined(HAVE_RTPSPAWN)
6440 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07006441#else
6442 spawnval = _spawnv(mode, path->narrow, argvlist);
6443#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006444 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006445 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00006446
Victor Stinner8c62be82010-05-06 00:08:46 +00006447 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00006448
Victor Stinner8c62be82010-05-06 00:08:46 +00006449 if (spawnval == -1)
6450 return posix_error();
6451 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006452 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006453}
6454
Larry Hastings2f936352014-08-05 14:04:04 +10006455/*[clinic input]
6456os.spawnve
6457
6458 mode: int
6459 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07006460 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10006461 Path of executable file.
6462 argv: object
6463 Tuple or list of strings.
6464 env: object
6465 Dictionary of strings mapping to strings.
6466 /
6467
6468Execute the program specified by path in a new process.
6469[clinic start generated code]*/
6470
Larry Hastings2f936352014-08-05 14:04:04 +10006471static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07006472os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006473 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07006474/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006475{
Steve Dowercc16be82016-09-08 10:35:16 -07006476 EXECV_CHAR **argvlist;
6477 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006478 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00006479 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07006480 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006482 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00006483
Victor Stinner8c62be82010-05-06 00:08:46 +00006484 /* spawnve has four arguments: (mode, path, argv, env), where
6485 argv is a list or tuple of strings and env is a dictionary
6486 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00006487
Victor Stinner8c62be82010-05-06 00:08:46 +00006488 if (PyList_Check(argv)) {
6489 argc = PyList_Size(argv);
6490 getitem = PyList_GetItem;
6491 }
6492 else if (PyTuple_Check(argv)) {
6493 argc = PyTuple_Size(argv);
6494 getitem = PyTuple_GetItem;
6495 }
6496 else {
6497 PyErr_SetString(PyExc_TypeError,
6498 "spawnve() arg 2 must be a tuple or list");
6499 goto fail_0;
6500 }
Steve Dower859fd7b2016-11-19 18:53:19 -08006501 if (argc == 0) {
6502 PyErr_SetString(PyExc_ValueError,
6503 "spawnve() arg 2 cannot be empty");
6504 goto fail_0;
6505 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006506 if (!PyMapping_Check(env)) {
6507 PyErr_SetString(PyExc_TypeError,
6508 "spawnve() arg 3 must be a mapping object");
6509 goto fail_0;
6510 }
Guido van Rossuma1065681999-01-25 23:20:23 +00006511
Steve Dowercc16be82016-09-08 10:35:16 -07006512 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00006513 if (argvlist == NULL) {
6514 PyErr_NoMemory();
6515 goto fail_0;
6516 }
6517 for (i = 0; i < argc; i++) {
6518 if (!fsconvert_strdup((*getitem)(argv, i),
6519 &argvlist[i]))
6520 {
6521 lastarg = i;
6522 goto fail_1;
6523 }
Steve Dowerbce26262016-11-19 19:17:26 -08006524 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006525 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08006526 PyErr_SetString(
6527 PyExc_ValueError,
6528 "spawnv() arg 2 first element cannot be empty");
6529 goto fail_1;
6530 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006531 }
6532 lastarg = argc;
6533 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00006534
Victor Stinner8c62be82010-05-06 00:08:46 +00006535 envlist = parse_envlist(env, &envc);
6536 if (envlist == NULL)
6537 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00006538
pxinwrf2d7ac72019-05-21 18:46:37 +08006539#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00006540 if (mode == _OLD_P_OVERLAY)
6541 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08006542#endif
Tim Peters25059d32001-12-07 20:35:43 +00006543
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006544 if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08006545 goto fail_2;
6546 }
6547
Victor Stinner8c62be82010-05-06 00:08:46 +00006548 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07006549 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07006550#ifdef HAVE_WSPAWNV
6551 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08006552#elif defined(HAVE_RTPSPAWN)
6553 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
6554 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07006555#else
6556 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
6557#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006558 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006559 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00006560
Victor Stinner8c62be82010-05-06 00:08:46 +00006561 if (spawnval == -1)
6562 (void) posix_error();
6563 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006564 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006565
Saiyang Gou95f60012020-02-04 16:15:00 -08006566 fail_2:
Victor Stinner00d7abd2020-12-01 09:56:42 +01006567 while (--envc >= 0) {
6568 PyMem_Free(envlist[envc]);
6569 }
6570 PyMem_Free(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00006571 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006572 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00006573 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00006574 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00006575}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006576
Guido van Rossuma1065681999-01-25 23:20:23 +00006577#endif /* HAVE_SPAWNV */
6578
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006579#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07006580
6581/* Helper function to validate arguments.
6582 Returns 0 on success. non-zero on failure with a TypeError raised.
6583 If obj is non-NULL it must be callable. */
6584static int
6585check_null_or_callable(PyObject *obj, const char* obj_name)
6586{
6587 if (obj && !PyCallable_Check(obj)) {
6588 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
Eddie Elizondob3966632019-11-05 07:16:14 -08006589 obj_name, _PyType_Name(Py_TYPE(obj)));
Gregory P. Smith163468a2017-05-29 10:03:41 -07006590 return -1;
6591 }
6592 return 0;
6593}
6594
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006595/*[clinic input]
6596os.register_at_fork
6597
Gregory P. Smith163468a2017-05-29 10:03:41 -07006598 *
6599 before: object=NULL
6600 A callable to be called in the parent before the fork() syscall.
6601 after_in_child: object=NULL
6602 A callable to be called in the child after fork().
6603 after_in_parent: object=NULL
6604 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006605
Gregory P. Smith163468a2017-05-29 10:03:41 -07006606Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006607
Gregory P. Smith163468a2017-05-29 10:03:41 -07006608'before' callbacks are called in reverse order.
6609'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006610
6611[clinic start generated code]*/
6612
6613static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07006614os_register_at_fork_impl(PyObject *module, PyObject *before,
6615 PyObject *after_in_child, PyObject *after_in_parent)
6616/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006617{
6618 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006619
Gregory P. Smith163468a2017-05-29 10:03:41 -07006620 if (!before && !after_in_child && !after_in_parent) {
6621 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
6622 return NULL;
6623 }
6624 if (check_null_or_callable(before, "before") ||
6625 check_null_or_callable(after_in_child, "after_in_child") ||
6626 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006627 return NULL;
6628 }
Victor Stinner81a7be32020-04-14 15:14:01 +02006629 interp = _PyInterpreterState_GET();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006630
Gregory P. Smith163468a2017-05-29 10:03:41 -07006631 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006632 return NULL;
6633 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07006634 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006635 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07006636 }
6637 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6638 return NULL;
6639 }
6640 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006641}
6642#endif /* HAVE_FORK */
6643
6644
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006645#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006646/*[clinic input]
6647os.fork1
6648
6649Fork a child process with a single multiplexed (i.e., not bound) thread.
6650
6651Return 0 to child process and PID of child to parent process.
6652[clinic start generated code]*/
6653
Larry Hastings2f936352014-08-05 14:04:04 +10006654static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006655os_fork1_impl(PyObject *module)
6656/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006657{
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006659
Victor Stinner81a7be32020-04-14 15:14:01 +02006660 if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006661 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6662 return NULL;
6663 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006664 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006665 pid = fork1();
6666 if (pid == 0) {
6667 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006668 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006669 } else {
6670 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006671 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006672 }
6673 if (pid == -1)
6674 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006675 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006676}
Larry Hastings2f936352014-08-05 14:04:04 +10006677#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006678
6679
Guido van Rossumad0ee831995-03-01 10:34:45 +00006680#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006681/*[clinic input]
6682os.fork
6683
6684Fork a child process.
6685
6686Return 0 to child process and PID of child to parent process.
6687[clinic start generated code]*/
6688
Larry Hastings2f936352014-08-05 14:04:04 +10006689static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006690os_fork_impl(PyObject *module)
6691/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006692{
Victor Stinner8c62be82010-05-06 00:08:46 +00006693 pid_t pid;
Victor Stinner252346a2020-05-01 11:33:44 +02006694 PyInterpreterState *interp = _PyInterpreterState_GET();
6695 if (interp->config._isolated_interpreter) {
6696 PyErr_SetString(PyExc_RuntimeError,
6697 "fork not supported for isolated subinterpreters");
Eric Snow59032962018-09-14 14:17:20 -07006698 return NULL;
6699 }
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006700 if (PySys_Audit("os.fork", NULL) < 0) {
6701 return NULL;
6702 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006703 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006704 pid = fork();
6705 if (pid == 0) {
6706 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006707 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006708 } else {
6709 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006710 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006711 }
6712 if (pid == -1)
6713 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006714 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006715}
Larry Hastings2f936352014-08-05 14:04:04 +10006716#endif /* HAVE_FORK */
6717
Guido van Rossum85e3b011991-06-03 12:42:10 +00006718
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006719#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006720#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006721/*[clinic input]
6722os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006723
Larry Hastings2f936352014-08-05 14:04:04 +10006724 policy: int
6725
6726Get the maximum scheduling priority for policy.
6727[clinic start generated code]*/
6728
Larry Hastings2f936352014-08-05 14:04:04 +10006729static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006730os_sched_get_priority_max_impl(PyObject *module, int policy)
6731/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006732{
6733 int max;
6734
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006735 max = sched_get_priority_max(policy);
6736 if (max < 0)
6737 return posix_error();
6738 return PyLong_FromLong(max);
6739}
6740
Larry Hastings2f936352014-08-05 14:04:04 +10006741
6742/*[clinic input]
6743os.sched_get_priority_min
6744
6745 policy: int
6746
6747Get the minimum scheduling priority for policy.
6748[clinic start generated code]*/
6749
Larry Hastings2f936352014-08-05 14:04:04 +10006750static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006751os_sched_get_priority_min_impl(PyObject *module, int policy)
6752/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006753{
6754 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006755 if (min < 0)
6756 return posix_error();
6757 return PyLong_FromLong(min);
6758}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006759#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6760
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006761
Larry Hastings2f936352014-08-05 14:04:04 +10006762#ifdef HAVE_SCHED_SETSCHEDULER
6763/*[clinic input]
6764os.sched_getscheduler
6765 pid: pid_t
6766 /
6767
Min ho Kimc4cacc82019-07-31 08:16:13 +10006768Get the scheduling policy for the process identified by pid.
Larry Hastings2f936352014-08-05 14:04:04 +10006769
6770Passing 0 for pid returns the scheduling policy for the calling process.
6771[clinic start generated code]*/
6772
Larry Hastings2f936352014-08-05 14:04:04 +10006773static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006774os_sched_getscheduler_impl(PyObject *module, pid_t pid)
Min ho Kimc4cacc82019-07-31 08:16:13 +10006775/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006776{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006777 int policy;
6778
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006779 policy = sched_getscheduler(pid);
6780 if (policy < 0)
6781 return posix_error();
6782 return PyLong_FromLong(policy);
6783}
Larry Hastings2f936352014-08-05 14:04:04 +10006784#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006785
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006786
William Orr81574b82018-10-01 22:19:56 -07006787#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006788/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006789class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006790
6791@classmethod
6792os.sched_param.__new__
6793
6794 sched_priority: object
6795 A scheduling parameter.
6796
Eddie Elizondob3966632019-11-05 07:16:14 -08006797Currently has only one field: sched_priority
Larry Hastings2f936352014-08-05 14:04:04 +10006798[clinic start generated code]*/
6799
Larry Hastings2f936352014-08-05 14:04:04 +10006800static PyObject *
6801os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondob3966632019-11-05 07:16:14 -08006802/*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006803{
6804 PyObject *res;
6805
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006806 res = PyStructSequence_New(type);
6807 if (!res)
6808 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006809 Py_INCREF(sched_priority);
6810 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006811 return res;
6812}
6813
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006814PyDoc_VAR(os_sched_param__doc__);
6815
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006816static PyStructSequence_Field sched_param_fields[] = {
6817 {"sched_priority", "the scheduling priority"},
6818 {0}
6819};
6820
6821static PyStructSequence_Desc sched_param_desc = {
6822 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006823 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006824 sched_param_fields,
6825 1
6826};
6827
6828static int
Victor Stinner1c2fa782020-05-10 11:05:29 +02006829convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006830{
6831 long priority;
6832
Victor Stinner1c2fa782020-05-10 11:05:29 +02006833 if (!Py_IS_TYPE(param, (PyTypeObject *)get_posix_state(module)->SchedParamType)) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006834 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6835 return 0;
6836 }
6837 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6838 if (priority == -1 && PyErr_Occurred())
6839 return 0;
6840 if (priority > INT_MAX || priority < INT_MIN) {
6841 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6842 return 0;
6843 }
6844 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6845 return 1;
6846}
William Orr81574b82018-10-01 22:19:56 -07006847#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006848
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006849
6850#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006851/*[clinic input]
6852os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006853
Larry Hastings2f936352014-08-05 14:04:04 +10006854 pid: pid_t
6855 policy: int
Victor Stinner1c2fa782020-05-10 11:05:29 +02006856 param as param_obj: object
Larry Hastings2f936352014-08-05 14:04:04 +10006857 /
6858
6859Set the scheduling policy for the process identified by pid.
6860
6861If pid is 0, the calling process is changed.
6862param is an instance of sched_param.
6863[clinic start generated code]*/
6864
Larry Hastings2f936352014-08-05 14:04:04 +10006865static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006866os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Victor Stinner1c2fa782020-05-10 11:05:29 +02006867 PyObject *param_obj)
6868/*[clinic end generated code: output=cde27faa55dc993e input=73013d731bd8fbe9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006869{
Victor Stinner1c2fa782020-05-10 11:05:29 +02006870 struct sched_param param;
6871 if (!convert_sched_param(module, param_obj, &param)) {
6872 return NULL;
6873 }
6874
Jesus Cea9c822272011-09-10 01:40:52 +02006875 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006876 ** sched_setscheduler() returns 0 in Linux, but the previous
6877 ** scheduling policy under Solaris/Illumos, and others.
6878 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006879 */
Victor Stinner1c2fa782020-05-10 11:05:29 +02006880 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006881 return posix_error();
6882 Py_RETURN_NONE;
6883}
Larry Hastings2f936352014-08-05 14:04:04 +10006884#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006885
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006886
6887#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006888/*[clinic input]
6889os.sched_getparam
6890 pid: pid_t
6891 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006892
Larry Hastings2f936352014-08-05 14:04:04 +10006893Returns scheduling parameters for the process identified by pid.
6894
6895If pid is 0, returns parameters for the calling process.
6896Return value is an instance of sched_param.
6897[clinic start generated code]*/
6898
Larry Hastings2f936352014-08-05 14:04:04 +10006899static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006900os_sched_getparam_impl(PyObject *module, pid_t pid)
6901/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006902{
6903 struct sched_param param;
6904 PyObject *result;
6905 PyObject *priority;
6906
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006907 if (sched_getparam(pid, &param))
6908 return posix_error();
Victor Stinner1c2fa782020-05-10 11:05:29 +02006909 PyObject *SchedParamType = get_posix_state(module)->SchedParamType;
Eddie Elizondob3966632019-11-05 07:16:14 -08006910 result = PyStructSequence_New((PyTypeObject *)SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006911 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006912 return NULL;
6913 priority = PyLong_FromLong(param.sched_priority);
6914 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006915 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006916 return NULL;
6917 }
Larry Hastings2f936352014-08-05 14:04:04 +10006918 PyStructSequence_SET_ITEM(result, 0, priority);
6919 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006920}
6921
Larry Hastings2f936352014-08-05 14:04:04 +10006922
6923/*[clinic input]
6924os.sched_setparam
6925 pid: pid_t
Victor Stinner1c2fa782020-05-10 11:05:29 +02006926 param as param_obj: object
Larry Hastings2f936352014-08-05 14:04:04 +10006927 /
6928
6929Set scheduling parameters for the process identified by pid.
6930
6931If pid is 0, sets parameters for the calling process.
6932param should be an instance of sched_param.
6933[clinic start generated code]*/
6934
Larry Hastings2f936352014-08-05 14:04:04 +10006935static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +02006936os_sched_setparam_impl(PyObject *module, pid_t pid, PyObject *param_obj)
6937/*[clinic end generated code: output=f19fe020a53741c1 input=27b98337c8b2dcc7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006938{
Victor Stinner1c2fa782020-05-10 11:05:29 +02006939 struct sched_param param;
6940 if (!convert_sched_param(module, param_obj, &param)) {
6941 return NULL;
6942 }
6943
6944 if (sched_setparam(pid, &param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006945 return posix_error();
6946 Py_RETURN_NONE;
6947}
Larry Hastings2f936352014-08-05 14:04:04 +10006948#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006949
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006950
6951#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006952/*[clinic input]
6953os.sched_rr_get_interval -> double
6954 pid: pid_t
6955 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006956
Larry Hastings2f936352014-08-05 14:04:04 +10006957Return the round-robin quantum for the process identified by pid, in seconds.
6958
6959Value returned is a float.
6960[clinic start generated code]*/
6961
Larry Hastings2f936352014-08-05 14:04:04 +10006962static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006963os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6964/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006965{
6966 struct timespec interval;
6967 if (sched_rr_get_interval(pid, &interval)) {
6968 posix_error();
6969 return -1.0;
6970 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006971#ifdef _Py_MEMORY_SANITIZER
6972 __msan_unpoison(&interval, sizeof(interval));
6973#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006974 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6975}
6976#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006977
Larry Hastings2f936352014-08-05 14:04:04 +10006978
6979/*[clinic input]
6980os.sched_yield
6981
6982Voluntarily relinquish the CPU.
6983[clinic start generated code]*/
6984
Larry Hastings2f936352014-08-05 14:04:04 +10006985static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006986os_sched_yield_impl(PyObject *module)
6987/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006988{
6989 if (sched_yield())
6990 return posix_error();
6991 Py_RETURN_NONE;
6992}
6993
Benjamin Peterson2740af82011-08-02 17:41:34 -05006994#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006995/* The minimum number of CPUs allocated in a cpu_set_t */
6996static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006997
Larry Hastings2f936352014-08-05 14:04:04 +10006998/*[clinic input]
6999os.sched_setaffinity
7000 pid: pid_t
7001 mask : object
7002 /
7003
7004Set the CPU affinity of the process identified by pid to mask.
7005
7006mask should be an iterable of integers identifying CPUs.
7007[clinic start generated code]*/
7008
Larry Hastings2f936352014-08-05 14:04:04 +10007009static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007010os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
7011/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007012{
Antoine Pitrou84869872012-08-04 16:16:35 +02007013 int ncpus;
7014 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10007015 cpu_set_t *cpu_set = NULL;
7016 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007017
Larry Hastings2f936352014-08-05 14:04:04 +10007018 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02007019 if (iterator == NULL)
7020 return NULL;
7021
7022 ncpus = NCPUS_START;
7023 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10007024 cpu_set = CPU_ALLOC(ncpus);
7025 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02007026 PyErr_NoMemory();
7027 goto error;
7028 }
Larry Hastings2f936352014-08-05 14:04:04 +10007029 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02007030
7031 while ((item = PyIter_Next(iterator))) {
7032 long cpu;
7033 if (!PyLong_Check(item)) {
7034 PyErr_Format(PyExc_TypeError,
7035 "expected an iterator of ints, "
7036 "but iterator yielded %R",
7037 Py_TYPE(item));
7038 Py_DECREF(item);
7039 goto error;
7040 }
7041 cpu = PyLong_AsLong(item);
7042 Py_DECREF(item);
7043 if (cpu < 0) {
7044 if (!PyErr_Occurred())
7045 PyErr_SetString(PyExc_ValueError, "negative CPU number");
7046 goto error;
7047 }
7048 if (cpu > INT_MAX - 1) {
7049 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
7050 goto error;
7051 }
7052 if (cpu >= ncpus) {
7053 /* Grow CPU mask to fit the CPU number */
7054 int newncpus = ncpus;
7055 cpu_set_t *newmask;
7056 size_t newsetsize;
7057 while (newncpus <= cpu) {
7058 if (newncpus > INT_MAX / 2)
7059 newncpus = cpu + 1;
7060 else
7061 newncpus = newncpus * 2;
7062 }
7063 newmask = CPU_ALLOC(newncpus);
7064 if (newmask == NULL) {
7065 PyErr_NoMemory();
7066 goto error;
7067 }
7068 newsetsize = CPU_ALLOC_SIZE(newncpus);
7069 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10007070 memcpy(newmask, cpu_set, setsize);
7071 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02007072 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10007073 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02007074 ncpus = newncpus;
7075 }
Larry Hastings2f936352014-08-05 14:04:04 +10007076 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02007077 }
Brandt Bucher45a30af2019-06-27 09:10:57 -07007078 if (PyErr_Occurred()) {
7079 goto error;
7080 }
Antoine Pitrou84869872012-08-04 16:16:35 +02007081 Py_CLEAR(iterator);
7082
Larry Hastings2f936352014-08-05 14:04:04 +10007083 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02007084 posix_error();
7085 goto error;
7086 }
Larry Hastings2f936352014-08-05 14:04:04 +10007087 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007088 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02007089
7090error:
Larry Hastings2f936352014-08-05 14:04:04 +10007091 if (cpu_set)
7092 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02007093 Py_XDECREF(iterator);
7094 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007095}
7096
Larry Hastings2f936352014-08-05 14:04:04 +10007097
7098/*[clinic input]
7099os.sched_getaffinity
7100 pid: pid_t
7101 /
7102
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01007103Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10007104
7105The affinity is returned as a set of CPU identifiers.
7106[clinic start generated code]*/
7107
Larry Hastings2f936352014-08-05 14:04:04 +10007108static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007109os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03007110/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007111{
Antoine Pitrou84869872012-08-04 16:16:35 +02007112 int cpu, ncpus, count;
7113 size_t setsize;
7114 cpu_set_t *mask = NULL;
7115 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007116
Antoine Pitrou84869872012-08-04 16:16:35 +02007117 ncpus = NCPUS_START;
7118 while (1) {
7119 setsize = CPU_ALLOC_SIZE(ncpus);
7120 mask = CPU_ALLOC(ncpus);
7121 if (mask == NULL)
7122 return PyErr_NoMemory();
7123 if (sched_getaffinity(pid, setsize, mask) == 0)
7124 break;
7125 CPU_FREE(mask);
7126 if (errno != EINVAL)
7127 return posix_error();
7128 if (ncpus > INT_MAX / 2) {
7129 PyErr_SetString(PyExc_OverflowError, "could not allocate "
7130 "a large enough CPU set");
7131 return NULL;
7132 }
7133 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007134 }
Antoine Pitrou84869872012-08-04 16:16:35 +02007135
7136 res = PySet_New(NULL);
7137 if (res == NULL)
7138 goto error;
7139 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
7140 if (CPU_ISSET_S(cpu, setsize, mask)) {
7141 PyObject *cpu_num = PyLong_FromLong(cpu);
7142 --count;
7143 if (cpu_num == NULL)
7144 goto error;
7145 if (PySet_Add(res, cpu_num)) {
7146 Py_DECREF(cpu_num);
7147 goto error;
7148 }
7149 Py_DECREF(cpu_num);
7150 }
7151 }
7152 CPU_FREE(mask);
7153 return res;
7154
7155error:
7156 if (mask)
7157 CPU_FREE(mask);
7158 Py_XDECREF(res);
7159 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007160}
7161
Benjamin Peterson2740af82011-08-02 17:41:34 -05007162#endif /* HAVE_SCHED_SETAFFINITY */
7163
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007164#endif /* HAVE_SCHED_H */
7165
Larry Hastings2f936352014-08-05 14:04:04 +10007166
Neal Norwitzb59798b2003-03-21 01:43:31 +00007167/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00007168#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Victor Stinner57766632020-10-29 15:16:23 +01007169# define DEV_PTY_FILE "/dev/ptc"
7170# define HAVE_DEV_PTMX
Neal Norwitzb59798b2003-03-21 01:43:31 +00007171#else
Victor Stinner57766632020-10-29 15:16:23 +01007172# define DEV_PTY_FILE "/dev/ptmx"
Neal Norwitzb59798b2003-03-21 01:43:31 +00007173#endif
7174
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007175#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00007176#ifdef HAVE_PTY_H
7177#include <pty.h>
7178#else
7179#ifdef HAVE_LIBUTIL_H
7180#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00007181#else
7182#ifdef HAVE_UTIL_H
7183#include <util.h>
7184#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007185#endif /* HAVE_LIBUTIL_H */
7186#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00007187#ifdef HAVE_STROPTS_H
7188#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007189#endif
ngie-eign7745ec42018-02-14 11:54:28 -08007190#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007191
Larry Hastings2f936352014-08-05 14:04:04 +10007192
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007193#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10007194/*[clinic input]
7195os.openpty
7196
7197Open a pseudo-terminal.
7198
7199Return a tuple of (master_fd, slave_fd) containing open file descriptors
7200for both the master and slave ends.
7201[clinic start generated code]*/
7202
Larry Hastings2f936352014-08-05 14:04:04 +10007203static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007204os_openpty_impl(PyObject *module)
7205/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00007206{
Victor Stinnerdaf45552013-08-28 00:53:59 +02007207 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00007208#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007209 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00007210#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007211#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00007212 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01007213#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00007214 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007215#endif
7216#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00007217
Thomas Wouters70c21a12000-07-14 14:28:33 +00007218#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02007220 goto posix_error;
7221
7222 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
7223 goto error;
7224 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
7225 goto error;
7226
Neal Norwitzb59798b2003-03-21 01:43:31 +00007227#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
7229 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02007230 goto posix_error;
7231 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
7232 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00007233
Victor Stinnerdaf45552013-08-28 00:53:59 +02007234 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00007235 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01007236 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007237
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007238#else
Victor Stinner000de532013-11-25 23:19:58 +01007239 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02007241 goto posix_error;
7242
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02007244
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 /* change permission of slave */
7246 if (grantpt(master_fd) < 0) {
7247 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02007248 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02007250
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 /* unlock slave */
7252 if (unlockpt(master_fd) < 0) {
7253 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02007254 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02007256
Victor Stinner8c62be82010-05-06 00:08:46 +00007257 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02007258
Victor Stinner8c62be82010-05-06 00:08:46 +00007259 slave_name = ptsname(master_fd); /* get name of slave */
7260 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02007261 goto posix_error;
7262
7263 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01007264 if (slave_fd == -1)
7265 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01007266
7267 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
7268 goto posix_error;
7269
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02007270#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00007271 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
7272 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00007273#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00007274 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00007275#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007276#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00007277#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00007278
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00007280
Victor Stinnerdaf45552013-08-28 00:53:59 +02007281posix_error:
7282 posix_error();
7283error:
7284 if (master_fd != -1)
7285 close(master_fd);
7286 if (slave_fd != -1)
7287 close(slave_fd);
7288 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00007289}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007290#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007291
Larry Hastings2f936352014-08-05 14:04:04 +10007292
Fred Drake8cef4cf2000-06-28 16:40:38 +00007293#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10007294/*[clinic input]
7295os.forkpty
7296
7297Fork a new process with a new pseudo-terminal as controlling tty.
7298
7299Returns a tuple of (pid, master_fd).
7300Like fork(), return pid of 0 to the child process,
7301and pid of child to the parent process.
7302To both, return fd of newly opened pseudo-terminal.
7303[clinic start generated code]*/
7304
Larry Hastings2f936352014-08-05 14:04:04 +10007305static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007306os_forkpty_impl(PyObject *module)
7307/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00007308{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02007309 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00007310 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00007311
Victor Stinner81a7be32020-04-14 15:14:01 +02007312 if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07007313 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
7314 return NULL;
7315 }
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007316 if (PySys_Audit("os.forkpty", NULL) < 0) {
7317 return NULL;
7318 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02007319 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00007320 pid = forkpty(&master_fd, NULL, NULL, NULL);
7321 if (pid == 0) {
7322 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02007323 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00007324 } else {
7325 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02007326 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 }
7328 if (pid == -1)
7329 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00007331}
Larry Hastings2f936352014-08-05 14:04:04 +10007332#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007333
Ross Lagerwall7807c352011-03-17 20:20:30 +02007334
Guido van Rossumad0ee831995-03-01 10:34:45 +00007335#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007336/*[clinic input]
7337os.getegid
7338
7339Return the current process's effective group id.
7340[clinic start generated code]*/
7341
Larry Hastings2f936352014-08-05 14:04:04 +10007342static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007343os_getegid_impl(PyObject *module)
7344/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007345{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007346 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007347}
Larry Hastings2f936352014-08-05 14:04:04 +10007348#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007349
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007350
Guido van Rossumad0ee831995-03-01 10:34:45 +00007351#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007352/*[clinic input]
7353os.geteuid
7354
7355Return the current process's effective user id.
7356[clinic start generated code]*/
7357
Larry Hastings2f936352014-08-05 14:04:04 +10007358static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007359os_geteuid_impl(PyObject *module)
7360/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007361{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007362 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007363}
Larry Hastings2f936352014-08-05 14:04:04 +10007364#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007365
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007366
Guido van Rossumad0ee831995-03-01 10:34:45 +00007367#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007368/*[clinic input]
7369os.getgid
7370
7371Return the current process's group id.
7372[clinic start generated code]*/
7373
Larry Hastings2f936352014-08-05 14:04:04 +10007374static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007375os_getgid_impl(PyObject *module)
7376/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007377{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007378 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007379}
Larry Hastings2f936352014-08-05 14:04:04 +10007380#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007381
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007382
Berker Peksag39404992016-09-15 20:45:16 +03007383#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10007384/*[clinic input]
7385os.getpid
7386
7387Return the current process id.
7388[clinic start generated code]*/
7389
Larry Hastings2f936352014-08-05 14:04:04 +10007390static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007391os_getpid_impl(PyObject *module)
7392/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007393{
Victor Stinner8c62be82010-05-06 00:08:46 +00007394 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00007395}
Berker Peksag39404992016-09-15 20:45:16 +03007396#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007397
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07007398#ifdef NGROUPS_MAX
7399#define MAX_GROUPS NGROUPS_MAX
7400#else
7401 /* defined to be 16 on Solaris7, so this should be a small number */
7402#define MAX_GROUPS 64
7403#endif
7404
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007405#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10007406
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007407#ifdef __APPLE__
7408/*[clinic input]
7409os.getgrouplist
7410
7411 user: str
7412 username to lookup
7413 group as basegid: int
7414 base group id of the user
7415 /
7416
7417Returns a list of groups to which a user belongs.
7418[clinic start generated code]*/
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007419
7420static PyObject *
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007421os_getgrouplist_impl(PyObject *module, const char *user, int basegid)
7422/*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/
7423#else
7424/*[clinic input]
7425os.getgrouplist
7426
7427 user: str
7428 username to lookup
7429 group as basegid: gid_t
7430 base group id of the user
7431 /
7432
7433Returns a list of groups to which a user belongs.
7434[clinic start generated code]*/
7435
7436static PyObject *
7437os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid)
7438/*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/
7439#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007440{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007441 int i, ngroups;
7442 PyObject *list;
7443#ifdef __APPLE__
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007444 int *groups;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007445#else
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007446 gid_t *groups;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007447#endif
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07007448
7449 /*
7450 * NGROUPS_MAX is defined by POSIX.1 as the maximum
7451 * number of supplimental groups a users can belong to.
7452 * We have to increment it by one because
7453 * getgrouplist() returns both the supplemental groups
7454 * and the primary group, i.e. all of the groups the
7455 * user belongs to.
7456 */
7457 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007458
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007459 while (1) {
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007460#ifdef __APPLE__
Victor Stinner8ec73702020-03-23 20:00:57 +01007461 groups = PyMem_New(int, ngroups);
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007462#else
7463 groups = PyMem_New(gid_t, ngroups);
7464#endif
Victor Stinner8ec73702020-03-23 20:00:57 +01007465 if (groups == NULL) {
7466 return PyErr_NoMemory();
7467 }
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007468
7469 int old_ngroups = ngroups;
7470 if (getgrouplist(user, basegid, groups, &ngroups) != -1) {
7471 /* Success */
7472 break;
7473 }
7474
7475 /* getgrouplist() fails if the group list is too small */
7476 PyMem_Free(groups);
7477
7478 if (ngroups > old_ngroups) {
7479 /* If the group list is too small, the glibc implementation of
7480 getgrouplist() sets ngroups to the total number of groups and
7481 returns -1. */
7482 }
7483 else {
7484 /* Double the group list size */
7485 if (ngroups > INT_MAX / 2) {
7486 return PyErr_NoMemory();
7487 }
7488 ngroups *= 2;
7489 }
7490
7491 /* Retry getgrouplist() with a larger group list */
Victor Stinner8ec73702020-03-23 20:00:57 +01007492 }
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007493
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08007494#ifdef _Py_MEMORY_SANITIZER
7495 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
7496 __msan_unpoison(&ngroups, sizeof(ngroups));
7497 __msan_unpoison(groups, ngroups*sizeof(*groups));
7498#endif
7499
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007500 list = PyList_New(ngroups);
7501 if (list == NULL) {
Victor Stinner00d7abd2020-12-01 09:56:42 +01007502 PyMem_Free(groups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007503 return NULL;
7504 }
7505
7506 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007507#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007508 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007509#else
7510 PyObject *o = _PyLong_FromGid(groups[i]);
7511#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007512 if (o == NULL) {
7513 Py_DECREF(list);
Victor Stinner00d7abd2020-12-01 09:56:42 +01007514 PyMem_Free(groups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007515 return NULL;
7516 }
7517 PyList_SET_ITEM(list, i, o);
7518 }
7519
Victor Stinner00d7abd2020-12-01 09:56:42 +01007520 PyMem_Free(groups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007521
7522 return list;
7523}
Larry Hastings2f936352014-08-05 14:04:04 +10007524#endif /* HAVE_GETGROUPLIST */
7525
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007526
Fred Drakec9680921999-12-13 16:37:25 +00007527#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007528/*[clinic input]
7529os.getgroups
7530
7531Return list of supplemental group IDs for the process.
7532[clinic start generated code]*/
7533
Larry Hastings2f936352014-08-05 14:04:04 +10007534static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007535os_getgroups_impl(PyObject *module)
7536/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00007537{
7538 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007539 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007540
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007541 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007542 * This is a helper variable to store the intermediate result when
7543 * that happens.
7544 *
7545 * To keep the code readable the OSX behaviour is unconditional,
7546 * according to the POSIX spec this should be safe on all unix-y
7547 * systems.
7548 */
7549 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 int n;
Fred Drakec9680921999-12-13 16:37:25 +00007551
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007552#ifdef __APPLE__
7553 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
7554 * there are more groups than can fit in grouplist. Therefore, on OS X
7555 * always first call getgroups with length 0 to get the actual number
7556 * of groups.
7557 */
7558 n = getgroups(0, NULL);
7559 if (n < 0) {
7560 return posix_error();
7561 } else if (n <= MAX_GROUPS) {
7562 /* groups will fit in existing array */
7563 alt_grouplist = grouplist;
7564 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007565 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007566 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007567 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007568 }
7569 }
7570
7571 n = getgroups(n, alt_grouplist);
7572 if (n == -1) {
7573 if (alt_grouplist != grouplist) {
7574 PyMem_Free(alt_grouplist);
7575 }
7576 return posix_error();
7577 }
7578#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007579 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007580 if (n < 0) {
7581 if (errno == EINVAL) {
7582 n = getgroups(0, NULL);
7583 if (n == -1) {
7584 return posix_error();
7585 }
7586 if (n == 0) {
7587 /* Avoid malloc(0) */
7588 alt_grouplist = grouplist;
7589 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007590 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007591 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007592 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007593 }
7594 n = getgroups(n, alt_grouplist);
7595 if (n == -1) {
7596 PyMem_Free(alt_grouplist);
7597 return posix_error();
7598 }
7599 }
7600 } else {
7601 return posix_error();
7602 }
7603 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007604#endif
7605
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007606 result = PyList_New(n);
7607 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007608 int i;
7609 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007610 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00007611 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007612 Py_DECREF(result);
7613 result = NULL;
7614 break;
Fred Drakec9680921999-12-13 16:37:25 +00007615 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007616 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00007617 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007618 }
7619
7620 if (alt_grouplist != grouplist) {
7621 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00007622 }
Neal Norwitze241ce82003-02-17 18:17:05 +00007623
Fred Drakec9680921999-12-13 16:37:25 +00007624 return result;
7625}
Larry Hastings2f936352014-08-05 14:04:04 +10007626#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00007627
Antoine Pitroub7572f02009-12-02 20:46:48 +00007628#ifdef HAVE_INITGROUPS
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007629#ifdef __APPLE__
7630/*[clinic input]
7631os.initgroups
Antoine Pitroub7572f02009-12-02 20:46:48 +00007632
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007633 username as oname: FSConverter
7634 gid: int
7635 /
7636
7637Initialize the group access list.
7638
7639Call the system initgroups() to initialize the group access list with all of
7640the groups of which the specified username is a member, plus the specified
7641group id.
7642[clinic start generated code]*/
7643
Antoine Pitroub7572f02009-12-02 20:46:48 +00007644static PyObject *
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007645os_initgroups_impl(PyObject *module, PyObject *oname, int gid)
7646/*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/
7647#else
7648/*[clinic input]
7649os.initgroups
7650
7651 username as oname: FSConverter
7652 gid: gid_t
7653 /
7654
7655Initialize the group access list.
7656
7657Call the system initgroups() to initialize the group access list with all of
7658the groups of which the specified username is a member, plus the specified
7659group id.
7660[clinic start generated code]*/
7661
7662static PyObject *
7663os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid)
7664/*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/
7665#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00007666{
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007667 const char *username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007668
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007669 if (initgroups(username, gid) == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00007670 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007671
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007672 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00007673}
Larry Hastings2f936352014-08-05 14:04:04 +10007674#endif /* HAVE_INITGROUPS */
7675
Antoine Pitroub7572f02009-12-02 20:46:48 +00007676
Martin v. Löwis606edc12002-06-13 21:09:11 +00007677#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007678/*[clinic input]
7679os.getpgid
7680
7681 pid: pid_t
7682
7683Call the system call getpgid(), and return the result.
7684[clinic start generated code]*/
7685
Larry Hastings2f936352014-08-05 14:04:04 +10007686static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007687os_getpgid_impl(PyObject *module, pid_t pid)
7688/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007689{
7690 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007691 if (pgid < 0)
7692 return posix_error();
7693 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00007694}
7695#endif /* HAVE_GETPGID */
7696
7697
Guido van Rossumb6775db1994-08-01 11:34:53 +00007698#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007699/*[clinic input]
7700os.getpgrp
7701
7702Return the current process group id.
7703[clinic start generated code]*/
7704
Larry Hastings2f936352014-08-05 14:04:04 +10007705static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007706os_getpgrp_impl(PyObject *module)
7707/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007708{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007709#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007710 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007711#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007712 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007713#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007714}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007715#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007717
Guido van Rossumb6775db1994-08-01 11:34:53 +00007718#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007719/*[clinic input]
7720os.setpgrp
7721
7722Make the current process the leader of its process group.
7723[clinic start generated code]*/
7724
Larry Hastings2f936352014-08-05 14:04:04 +10007725static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007726os_setpgrp_impl(PyObject *module)
7727/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007728{
Guido van Rossum64933891994-10-20 21:56:42 +00007729#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007730 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007731#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007732 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007733#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007735 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007736}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007737#endif /* HAVE_SETPGRP */
7738
Guido van Rossumad0ee831995-03-01 10:34:45 +00007739#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007740
7741#ifdef MS_WINDOWS
7742#include <tlhelp32.h>
7743
7744static PyObject*
7745win32_getppid()
7746{
7747 HANDLE snapshot;
7748 pid_t mypid;
7749 PyObject* result = NULL;
7750 BOOL have_record;
7751 PROCESSENTRY32 pe;
7752
7753 mypid = getpid(); /* This function never fails */
7754
7755 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7756 if (snapshot == INVALID_HANDLE_VALUE)
7757 return PyErr_SetFromWindowsErr(GetLastError());
7758
7759 pe.dwSize = sizeof(pe);
7760 have_record = Process32First(snapshot, &pe);
7761 while (have_record) {
7762 if (mypid == (pid_t)pe.th32ProcessID) {
7763 /* We could cache the ulong value in a static variable. */
7764 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7765 break;
7766 }
7767
7768 have_record = Process32Next(snapshot, &pe);
7769 }
7770
7771 /* If our loop exits and our pid was not found (result will be NULL)
7772 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7773 * error anyway, so let's raise it. */
7774 if (!result)
7775 result = PyErr_SetFromWindowsErr(GetLastError());
7776
7777 CloseHandle(snapshot);
7778
7779 return result;
7780}
7781#endif /*MS_WINDOWS*/
7782
Larry Hastings2f936352014-08-05 14:04:04 +10007783
7784/*[clinic input]
7785os.getppid
7786
7787Return the parent's process id.
7788
7789If the parent process has already exited, Windows machines will still
7790return its id; others systems will return the id of the 'init' process (1).
7791[clinic start generated code]*/
7792
Larry Hastings2f936352014-08-05 14:04:04 +10007793static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007794os_getppid_impl(PyObject *module)
7795/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007796{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007797#ifdef MS_WINDOWS
7798 return win32_getppid();
7799#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007800 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007801#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007802}
7803#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007804
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007805
Fred Drake12c6e2d1999-12-14 21:25:03 +00007806#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007807/*[clinic input]
7808os.getlogin
7809
7810Return the actual login name.
7811[clinic start generated code]*/
7812
Larry Hastings2f936352014-08-05 14:04:04 +10007813static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007814os_getlogin_impl(PyObject *module)
7815/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007816{
Victor Stinner8c62be82010-05-06 00:08:46 +00007817 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007818#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007819 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007820 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007821
7822 if (GetUserNameW(user_name, &num_chars)) {
7823 /* num_chars is the number of unicode chars plus null terminator */
7824 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007825 }
7826 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007827 result = PyErr_SetFromWindowsErr(GetLastError());
7828#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 char *name;
7830 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007831
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 errno = 0;
7833 name = getlogin();
7834 if (name == NULL) {
7835 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007836 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007837 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007838 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007839 }
7840 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007841 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007843#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007844 return result;
7845}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007846#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007847
Larry Hastings2f936352014-08-05 14:04:04 +10007848
Guido van Rossumad0ee831995-03-01 10:34:45 +00007849#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007850/*[clinic input]
7851os.getuid
7852
7853Return the current process's user id.
7854[clinic start generated code]*/
7855
Larry Hastings2f936352014-08-05 14:04:04 +10007856static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007857os_getuid_impl(PyObject *module)
7858/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007859{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007860 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007861}
Larry Hastings2f936352014-08-05 14:04:04 +10007862#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007863
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007864
Brian Curtineb24d742010-04-12 17:16:38 +00007865#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007866#define HAVE_KILL
7867#endif /* MS_WINDOWS */
7868
7869#ifdef HAVE_KILL
7870/*[clinic input]
7871os.kill
7872
7873 pid: pid_t
7874 signal: Py_ssize_t
7875 /
7876
7877Kill a process with a signal.
7878[clinic start generated code]*/
7879
Larry Hastings2f936352014-08-05 14:04:04 +10007880static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007881os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7882/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007883{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007884 if (PySys_Audit("os.kill", "in", pid, signal) < 0) {
7885 return NULL;
7886 }
7887#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007888 if (kill(pid, (int)signal) == -1)
7889 return posix_error();
7890 Py_RETURN_NONE;
Larry Hastings2f936352014-08-05 14:04:04 +10007891#else /* !MS_WINDOWS */
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007892 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007893 DWORD sig = (DWORD)signal;
7894 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007896
Victor Stinner8c62be82010-05-06 00:08:46 +00007897 /* Console processes which share a common console can be sent CTRL+C or
7898 CTRL+BREAK events, provided they handle said events. */
7899 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007900 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007901 err = GetLastError();
7902 PyErr_SetFromWindowsErr(err);
7903 }
7904 else
7905 Py_RETURN_NONE;
7906 }
Brian Curtineb24d742010-04-12 17:16:38 +00007907
Victor Stinner8c62be82010-05-06 00:08:46 +00007908 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7909 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007910 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007911 if (handle == NULL) {
7912 err = GetLastError();
7913 return PyErr_SetFromWindowsErr(err);
7914 }
Brian Curtineb24d742010-04-12 17:16:38 +00007915
Victor Stinner8c62be82010-05-06 00:08:46 +00007916 if (TerminateProcess(handle, sig) == 0) {
7917 err = GetLastError();
7918 result = PyErr_SetFromWindowsErr(err);
7919 } else {
7920 Py_INCREF(Py_None);
7921 result = Py_None;
7922 }
Brian Curtineb24d742010-04-12 17:16:38 +00007923
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 CloseHandle(handle);
7925 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10007926#endif /* !MS_WINDOWS */
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007927}
Larry Hastings2f936352014-08-05 14:04:04 +10007928#endif /* HAVE_KILL */
7929
7930
7931#ifdef HAVE_KILLPG
7932/*[clinic input]
7933os.killpg
7934
7935 pgid: pid_t
7936 signal: int
7937 /
7938
7939Kill a process group with a signal.
7940[clinic start generated code]*/
7941
Larry Hastings2f936352014-08-05 14:04:04 +10007942static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007943os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7944/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007945{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007946 if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) {
7947 return NULL;
7948 }
Larry Hastings2f936352014-08-05 14:04:04 +10007949 /* XXX some man pages make the `pgid` parameter an int, others
7950 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7951 take the same type. Moreover, pid_t is always at least as wide as
7952 int (else compilation of this module fails), which is safe. */
7953 if (killpg(pgid, signal) == -1)
7954 return posix_error();
7955 Py_RETURN_NONE;
7956}
7957#endif /* HAVE_KILLPG */
7958
Brian Curtineb24d742010-04-12 17:16:38 +00007959
Guido van Rossumc0125471996-06-28 18:55:32 +00007960#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007961#ifdef HAVE_SYS_LOCK_H
7962#include <sys/lock.h>
7963#endif
7964
Larry Hastings2f936352014-08-05 14:04:04 +10007965/*[clinic input]
7966os.plock
7967 op: int
7968 /
7969
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007970Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007971[clinic start generated code]*/
7972
Larry Hastings2f936352014-08-05 14:04:04 +10007973static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007974os_plock_impl(PyObject *module, int op)
7975/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007976{
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 if (plock(op) == -1)
7978 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007979 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007980}
Larry Hastings2f936352014-08-05 14:04:04 +10007981#endif /* HAVE_PLOCK */
7982
Guido van Rossumc0125471996-06-28 18:55:32 +00007983
Guido van Rossumb6775db1994-08-01 11:34:53 +00007984#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007985/*[clinic input]
7986os.setuid
7987
7988 uid: uid_t
7989 /
7990
7991Set the current process's user id.
7992[clinic start generated code]*/
7993
Larry Hastings2f936352014-08-05 14:04:04 +10007994static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007995os_setuid_impl(PyObject *module, uid_t uid)
7996/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007997{
Victor Stinner8c62be82010-05-06 00:08:46 +00007998 if (setuid(uid) < 0)
7999 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008000 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00008001}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008002#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00008003
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008004
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008005#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10008006/*[clinic input]
8007os.seteuid
8008
8009 euid: uid_t
8010 /
8011
8012Set the current process's effective user id.
8013[clinic start generated code]*/
8014
Larry Hastings2f936352014-08-05 14:04:04 +10008015static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008016os_seteuid_impl(PyObject *module, uid_t euid)
8017/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008018{
8019 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008021 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008022}
8023#endif /* HAVE_SETEUID */
8024
Larry Hastings2f936352014-08-05 14:04:04 +10008025
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008026#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10008027/*[clinic input]
8028os.setegid
8029
8030 egid: gid_t
8031 /
8032
8033Set the current process's effective group id.
8034[clinic start generated code]*/
8035
Larry Hastings2f936352014-08-05 14:04:04 +10008036static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008037os_setegid_impl(PyObject *module, gid_t egid)
8038/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008039{
8040 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00008041 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008042 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008043}
8044#endif /* HAVE_SETEGID */
8045
Larry Hastings2f936352014-08-05 14:04:04 +10008046
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008047#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10008048/*[clinic input]
8049os.setreuid
8050
8051 ruid: uid_t
8052 euid: uid_t
8053 /
8054
8055Set the current process's real and effective user ids.
8056[clinic start generated code]*/
8057
Larry Hastings2f936352014-08-05 14:04:04 +10008058static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008059os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
8060/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008061{
Victor Stinner8c62be82010-05-06 00:08:46 +00008062 if (setreuid(ruid, euid) < 0) {
8063 return posix_error();
8064 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02008065 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00008066 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008067}
8068#endif /* HAVE_SETREUID */
8069
Larry Hastings2f936352014-08-05 14:04:04 +10008070
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008071#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10008072/*[clinic input]
8073os.setregid
8074
8075 rgid: gid_t
8076 egid: gid_t
8077 /
8078
8079Set the current process's real and effective group ids.
8080[clinic start generated code]*/
8081
Larry Hastings2f936352014-08-05 14:04:04 +10008082static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008083os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
8084/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008085{
8086 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008088 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008089}
8090#endif /* HAVE_SETREGID */
8091
Larry Hastings2f936352014-08-05 14:04:04 +10008092
Guido van Rossumb6775db1994-08-01 11:34:53 +00008093#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10008094/*[clinic input]
8095os.setgid
8096 gid: gid_t
8097 /
8098
8099Set the current process's group id.
8100[clinic start generated code]*/
8101
Larry Hastings2f936352014-08-05 14:04:04 +10008102static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008103os_setgid_impl(PyObject *module, gid_t gid)
8104/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008105{
Victor Stinner8c62be82010-05-06 00:08:46 +00008106 if (setgid(gid) < 0)
8107 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008108 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00008109}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008110#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00008111
Larry Hastings2f936352014-08-05 14:04:04 +10008112
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008113#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10008114/*[clinic input]
8115os.setgroups
8116
8117 groups: object
8118 /
8119
8120Set the groups of the current process to list.
8121[clinic start generated code]*/
8122
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008123static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008124os_setgroups(PyObject *module, PyObject *groups)
8125/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008126{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008127 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00008128 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00008129
Victor Stinner8c62be82010-05-06 00:08:46 +00008130 if (!PySequence_Check(groups)) {
8131 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
8132 return NULL;
8133 }
8134 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008135 if (len < 0) {
8136 return NULL;
8137 }
Victor Stinner8c62be82010-05-06 00:08:46 +00008138 if (len > MAX_GROUPS) {
8139 PyErr_SetString(PyExc_ValueError, "too many groups");
8140 return NULL;
8141 }
8142 for(i = 0; i < len; i++) {
8143 PyObject *elem;
8144 elem = PySequence_GetItem(groups, i);
8145 if (!elem)
8146 return NULL;
8147 if (!PyLong_Check(elem)) {
8148 PyErr_SetString(PyExc_TypeError,
8149 "groups must be integers");
8150 Py_DECREF(elem);
8151 return NULL;
8152 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02008153 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008154 Py_DECREF(elem);
8155 return NULL;
8156 }
8157 }
8158 Py_DECREF(elem);
8159 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008160
Victor Stinner8c62be82010-05-06 00:08:46 +00008161 if (setgroups(len, grouplist) < 0)
8162 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02008163 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008164}
8165#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008166
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008167#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
8168static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +02008169wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008170{
Victor Stinner8c62be82010-05-06 00:08:46 +00008171 PyObject *result;
Eddie Elizondob3966632019-11-05 07:16:14 -08008172 PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008173
Victor Stinner8c62be82010-05-06 00:08:46 +00008174 if (pid == -1)
8175 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008176
Zackery Spytz682107c2019-09-09 09:48:32 -06008177 // If wait succeeded but no child was ready to report status, ru will not
8178 // have been populated.
8179 if (pid == 0) {
8180 memset(ru, 0, sizeof(*ru));
8181 }
8182
Eddie Elizondob3966632019-11-05 07:16:14 -08008183 PyObject *m = PyImport_ImportModuleNoBlock("resource");
8184 if (m == NULL)
8185 return NULL;
Victor Stinner1c2fa782020-05-10 11:05:29 +02008186 struct_rusage = PyObject_GetAttr(m, get_posix_state(module)->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08008187 Py_DECREF(m);
8188 if (struct_rusage == NULL)
8189 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008190
Victor Stinner8c62be82010-05-06 00:08:46 +00008191 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
8192 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
Eddie Elizondoe4db1f02019-11-25 19:07:37 -08008193 Py_DECREF(struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00008194 if (!result)
8195 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008196
8197#ifndef doubletime
8198#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
8199#endif
8200
Victor Stinner8c62be82010-05-06 00:08:46 +00008201 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01008202 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00008203 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01008204 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008205#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00008206 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
8207 SET_INT(result, 2, ru->ru_maxrss);
8208 SET_INT(result, 3, ru->ru_ixrss);
8209 SET_INT(result, 4, ru->ru_idrss);
8210 SET_INT(result, 5, ru->ru_isrss);
8211 SET_INT(result, 6, ru->ru_minflt);
8212 SET_INT(result, 7, ru->ru_majflt);
8213 SET_INT(result, 8, ru->ru_nswap);
8214 SET_INT(result, 9, ru->ru_inblock);
8215 SET_INT(result, 10, ru->ru_oublock);
8216 SET_INT(result, 11, ru->ru_msgsnd);
8217 SET_INT(result, 12, ru->ru_msgrcv);
8218 SET_INT(result, 13, ru->ru_nsignals);
8219 SET_INT(result, 14, ru->ru_nvcsw);
8220 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008221#undef SET_INT
8222
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 if (PyErr_Occurred()) {
8224 Py_DECREF(result);
8225 return NULL;
8226 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008227
Victor Stinner8c62be82010-05-06 00:08:46 +00008228 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008229}
8230#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
8231
Larry Hastings2f936352014-08-05 14:04:04 +10008232
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008233#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10008234/*[clinic input]
8235os.wait3
8236
8237 options: int
8238Wait for completion of a child process.
8239
8240Returns a tuple of information about the child process:
8241 (pid, status, rusage)
8242[clinic start generated code]*/
8243
Larry Hastings2f936352014-08-05 14:04:04 +10008244static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008245os_wait3_impl(PyObject *module, int options)
8246/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008247{
Victor Stinner8c62be82010-05-06 00:08:46 +00008248 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008249 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008250 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008251 WAIT_TYPE status;
8252 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008253
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008254 do {
8255 Py_BEGIN_ALLOW_THREADS
8256 pid = wait3(&status, options, &ru);
8257 Py_END_ALLOW_THREADS
8258 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8259 if (pid < 0)
8260 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008261
Victor Stinner1c2fa782020-05-10 11:05:29 +02008262 return wait_helper(module, pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008263}
8264#endif /* HAVE_WAIT3 */
8265
Larry Hastings2f936352014-08-05 14:04:04 +10008266
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008267#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10008268/*[clinic input]
8269
8270os.wait4
8271
8272 pid: pid_t
8273 options: int
8274
8275Wait for completion of a specific child process.
8276
8277Returns a tuple of information about the child process:
8278 (pid, status, rusage)
8279[clinic start generated code]*/
8280
Larry Hastings2f936352014-08-05 14:04:04 +10008281static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008282os_wait4_impl(PyObject *module, pid_t pid, int options)
8283/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008284{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008285 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00008286 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008287 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008288 WAIT_TYPE status;
8289 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008290
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008291 do {
8292 Py_BEGIN_ALLOW_THREADS
8293 res = wait4(pid, &status, options, &ru);
8294 Py_END_ALLOW_THREADS
8295 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8296 if (res < 0)
8297 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008298
Victor Stinner1c2fa782020-05-10 11:05:29 +02008299 return wait_helper(module, res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008300}
8301#endif /* HAVE_WAIT4 */
8302
Larry Hastings2f936352014-08-05 14:04:04 +10008303
Ross Lagerwall7807c352011-03-17 20:20:30 +02008304#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10008305/*[clinic input]
8306os.waitid
8307
8308 idtype: idtype_t
8309 Must be one of be P_PID, P_PGID or P_ALL.
8310 id: id_t
8311 The id to wait on.
8312 options: int
8313 Constructed from the ORing of one or more of WEXITED, WSTOPPED
8314 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
8315 /
8316
8317Returns the result of waiting for a process or processes.
8318
8319Returns either waitid_result or None if WNOHANG is specified and there are
8320no children in a waitable state.
8321[clinic start generated code]*/
8322
Larry Hastings2f936352014-08-05 14:04:04 +10008323static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008324os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
8325/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008326{
8327 PyObject *result;
8328 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008329 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008330 siginfo_t si;
8331 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008332
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008333 do {
8334 Py_BEGIN_ALLOW_THREADS
8335 res = waitid(idtype, id, &si, options);
8336 Py_END_ALLOW_THREADS
8337 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8338 if (res < 0)
8339 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008340
8341 if (si.si_pid == 0)
8342 Py_RETURN_NONE;
8343
Hai Shif707d942020-03-16 21:15:01 +08008344 PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08008345 result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008346 if (!result)
8347 return NULL;
8348
8349 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02008350 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008351 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
8352 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
8353 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
8354 if (PyErr_Occurred()) {
8355 Py_DECREF(result);
8356 return NULL;
8357 }
8358
8359 return result;
8360}
Larry Hastings2f936352014-08-05 14:04:04 +10008361#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008362
Larry Hastings2f936352014-08-05 14:04:04 +10008363
8364#if defined(HAVE_WAITPID)
8365/*[clinic input]
8366os.waitpid
8367 pid: pid_t
8368 options: int
8369 /
8370
8371Wait for completion of a given child process.
8372
8373Returns a tuple of information regarding the child process:
8374 (pid, status)
8375
8376The options argument is ignored on Windows.
8377[clinic start generated code]*/
8378
Larry Hastings2f936352014-08-05 14:04:04 +10008379static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008380os_waitpid_impl(PyObject *module, pid_t pid, int options)
8381/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008382{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008383 pid_t res;
8384 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008385 WAIT_TYPE status;
8386 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00008387
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008388 do {
8389 Py_BEGIN_ALLOW_THREADS
8390 res = waitpid(pid, &status, options);
8391 Py_END_ALLOW_THREADS
8392 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8393 if (res < 0)
8394 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008395
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008396 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00008397}
Tim Petersab034fa2002-02-01 11:27:43 +00008398#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00008399/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10008400/*[clinic input]
8401os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07008402 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10008403 options: int
8404 /
8405
8406Wait for completion of a given process.
8407
8408Returns a tuple of information regarding the process:
8409 (pid, status << 8)
8410
8411The options argument is ignored on Windows.
8412[clinic start generated code]*/
8413
Larry Hastings2f936352014-08-05 14:04:04 +10008414static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07008415os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07008416/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008417{
8418 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07008419 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008420 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008421
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008422 do {
8423 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08008424 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008425 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08008426 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008427 Py_END_ALLOW_THREADS
8428 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008429 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008430 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008431
Victor Stinner9bee32b2020-04-22 16:30:35 +02008432 unsigned long long ustatus = (unsigned int)status;
8433
Victor Stinner8c62be82010-05-06 00:08:46 +00008434 /* shift the status left a byte so this is more like the POSIX waitpid */
Victor Stinner9bee32b2020-04-22 16:30:35 +02008435 return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00008436}
Larry Hastings2f936352014-08-05 14:04:04 +10008437#endif
8438
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008439
Guido van Rossumad0ee831995-03-01 10:34:45 +00008440#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10008441/*[clinic input]
8442os.wait
8443
8444Wait for completion of a child process.
8445
8446Returns a tuple of information about the child process:
8447 (pid, status)
8448[clinic start generated code]*/
8449
Larry Hastings2f936352014-08-05 14:04:04 +10008450static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008451os_wait_impl(PyObject *module)
8452/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00008453{
Victor Stinner8c62be82010-05-06 00:08:46 +00008454 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008455 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008456 WAIT_TYPE status;
8457 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00008458
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008459 do {
8460 Py_BEGIN_ALLOW_THREADS
8461 pid = wait(&status);
8462 Py_END_ALLOW_THREADS
8463 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8464 if (pid < 0)
8465 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008466
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00008468}
Larry Hastings2f936352014-08-05 14:04:04 +10008469#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00008470
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -08008471#if defined(__linux__) && defined(__NR_pidfd_open)
8472/*[clinic input]
8473os.pidfd_open
8474 pid: pid_t
8475 flags: unsigned_int = 0
8476
8477Return a file descriptor referring to the process *pid*.
8478
8479The descriptor can be used to perform process management without races and
8480signals.
8481[clinic start generated code]*/
8482
8483static PyObject *
8484os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
8485/*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/
8486{
8487 int fd = syscall(__NR_pidfd_open, pid, flags);
8488 if (fd < 0) {
8489 return posix_error();
8490 }
8491 return PyLong_FromLong(fd);
8492}
8493#endif
8494
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008495
Larry Hastings9cf065c2012-06-22 16:30:09 -07008496#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008497/*[clinic input]
8498os.readlink
8499
8500 path: path_t
8501 *
8502 dir_fd: dir_fd(requires='readlinkat') = None
8503
8504Return a string representing the path to which the symbolic link points.
8505
8506If dir_fd is not None, it should be a file descriptor open to a directory,
8507and path should be relative; path will then be relative to that directory.
8508
8509dir_fd may not be implemented on your platform. If it is unavailable,
8510using it will raise a NotImplementedError.
8511[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008512
Barry Warsaw53699e91996-12-10 23:23:01 +00008513static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008514os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
8515/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008516{
Berker Peksage0b5b202018-08-15 13:03:41 +03008517#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02008518 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07008519 ssize_t length;
Ronald Oussoren41761932020-11-08 10:05:27 +01008520#ifdef HAVE_READLINKAT
8521 int readlinkat_unavailable = 0;
8522#endif
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008523
8524 Py_BEGIN_ALLOW_THREADS
8525#ifdef HAVE_READLINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +01008526 if (dir_fd != DEFAULT_DIR_FD) {
8527 if (HAVE_READLINKAT_RUNTIME) {
8528 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
8529 } else {
8530 readlinkat_unavailable = 1;
8531 }
8532 } else
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008533#endif
8534 length = readlink(path->narrow, buffer, MAXPATHLEN);
8535 Py_END_ALLOW_THREADS
8536
Ronald Oussoren41761932020-11-08 10:05:27 +01008537#ifdef HAVE_READLINKAT
8538 if (readlinkat_unavailable) {
8539 argument_unavailable_error(NULL, "dir_fd");
8540 return NULL;
8541 }
8542#endif
8543
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008544 if (length < 0) {
8545 return path_error(path);
8546 }
8547 buffer[length] = '\0';
8548
8549 if (PyUnicode_Check(path->object))
8550 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
8551 else
8552 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03008553#elif defined(MS_WINDOWS)
8554 DWORD n_bytes_returned;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008555 DWORD io_result = 0;
Berker Peksage0b5b202018-08-15 13:03:41 +03008556 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03008557 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
8558 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Steve Dower993ac922019-09-03 12:50:51 -07008559 PyObject *result = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00008560
Larry Hastings2f936352014-08-05 14:04:04 +10008561 /* First get a handle to the reparse point */
8562 Py_BEGIN_ALLOW_THREADS
8563 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008564 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10008565 0,
8566 0,
8567 0,
8568 OPEN_EXISTING,
8569 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
8570 0);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008571 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
8572 /* New call DeviceIoControl to read the reparse point */
8573 io_result = DeviceIoControl(
8574 reparse_point_handle,
8575 FSCTL_GET_REPARSE_POINT,
8576 0, 0, /* in buffer */
8577 target_buffer, sizeof(target_buffer),
8578 &n_bytes_returned,
8579 0 /* we're not using OVERLAPPED_IO */
8580 );
8581 CloseHandle(reparse_point_handle);
Berker Peksage0b5b202018-08-15 13:03:41 +03008582 }
Larry Hastings2f936352014-08-05 14:04:04 +10008583 Py_END_ALLOW_THREADS
8584
Berker Peksage0b5b202018-08-15 13:03:41 +03008585 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008586 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03008587 }
Larry Hastings2f936352014-08-05 14:04:04 +10008588
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008589 wchar_t *name = NULL;
8590 Py_ssize_t nameLen = 0;
8591 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Larry Hastings2f936352014-08-05 14:04:04 +10008592 {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008593 name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
8594 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
8595 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
Larry Hastings2f936352014-08-05 14:04:04 +10008596 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008597 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
8598 {
8599 name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
8600 rdb->MountPointReparseBuffer.SubstituteNameOffset);
8601 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
8602 }
8603 else
8604 {
8605 PyErr_SetString(PyExc_ValueError, "not a symbolic link");
8606 }
8607 if (name) {
8608 if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
8609 /* Our buffer is mutable, so this is okay */
8610 name[1] = L'\\';
8611 }
8612 result = PyUnicode_FromWideChar(name, nameLen);
Steve Dower993ac922019-09-03 12:50:51 -07008613 if (result && path->narrow) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008614 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
8615 }
Berker Peksage0b5b202018-08-15 13:03:41 +03008616 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008617 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03008618#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008619}
Berker Peksage0b5b202018-08-15 13:03:41 +03008620#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008621
Larry Hastings9cf065c2012-06-22 16:30:09 -07008622#if defined(MS_WINDOWS)
8623
Steve Dower6921e732018-03-05 14:26:08 -08008624/* Remove the last portion of the path - return 0 on success */
8625static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008626_dirnameW(WCHAR *path)
8627{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008628 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08008629 size_t length = wcsnlen_s(path, MAX_PATH);
8630 if (length == MAX_PATH) {
8631 return -1;
8632 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008633
8634 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08008635 for(ptr = path + length; ptr != path; ptr--) {
8636 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04008637 break;
Steve Dower6921e732018-03-05 14:26:08 -08008638 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008639 }
8640 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08008641 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008642}
8643
Minmin Gong7f21c9a2020-05-18 09:17:19 -07008644#endif
8645
8646#ifdef HAVE_SYMLINK
8647
8648#if defined(MS_WINDOWS)
8649
Victor Stinner31b3b922013-06-05 01:49:17 +02008650/* Is this path absolute? */
8651static int
8652_is_absW(const WCHAR *path)
8653{
Steve Dower6921e732018-03-05 14:26:08 -08008654 return path[0] == L'\\' || path[0] == L'/' ||
8655 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04008656}
8657
Steve Dower6921e732018-03-05 14:26:08 -08008658/* join root and rest with a backslash - return 0 on success */
8659static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008660_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
8661{
Victor Stinner31b3b922013-06-05 01:49:17 +02008662 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08008663 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008664 }
8665
Steve Dower6921e732018-03-05 14:26:08 -08008666 if (wcscpy_s(dest_path, MAX_PATH, root)) {
8667 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008668 }
Steve Dower6921e732018-03-05 14:26:08 -08008669
8670 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
8671 return -1;
8672 }
8673
8674 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008675}
8676
Victor Stinner31b3b922013-06-05 01:49:17 +02008677/* Return True if the path at src relative to dest is a directory */
8678static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03008679_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04008680{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008681 WIN32_FILE_ATTRIBUTE_DATA src_info;
8682 WCHAR dest_parent[MAX_PATH];
8683 WCHAR src_resolved[MAX_PATH] = L"";
8684
8685 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08008686 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
8687 _dirnameW(dest_parent)) {
8688 return 0;
8689 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008690 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08008691 if (_joinW(src_resolved, dest_parent, src)) {
8692 return 0;
8693 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008694 return (
8695 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
8696 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
8697 );
8698}
Larry Hastings9cf065c2012-06-22 16:30:09 -07008699#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008700
Larry Hastings2f936352014-08-05 14:04:04 +10008701
8702/*[clinic input]
8703os.symlink
8704 src: path_t
8705 dst: path_t
8706 target_is_directory: bool = False
8707 *
8708 dir_fd: dir_fd(requires='symlinkat')=None
8709
8710# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
8711
8712Create a symbolic link pointing to src named dst.
8713
8714target_is_directory is required on Windows if the target is to be
8715 interpreted as a directory. (On Windows, symlink requires
8716 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
8717 target_is_directory is ignored on non-Windows platforms.
8718
8719If dir_fd is not None, it should be a file descriptor open to a directory,
8720 and path should be relative; path will then be relative to that directory.
8721dir_fd may not be implemented on your platform.
8722 If it is unavailable, using it will raise a NotImplementedError.
8723
8724[clinic start generated code]*/
8725
Larry Hastings2f936352014-08-05 14:04:04 +10008726static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008727os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04008728 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008729/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008730{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008731#ifdef MS_WINDOWS
8732 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008733 DWORD flags = 0;
8734
8735 /* Assumed true, set to false if detected to not be available. */
8736 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008737#else
8738 int result;
Ronald Oussoren41761932020-11-08 10:05:27 +01008739#ifdef HAVE_SYMLINKAT
8740 int symlinkat_unavailable = 0;
8741#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07008742#endif
8743
Saiyang Gou7514f4f2020-02-12 23:47:42 -08008744 if (PySys_Audit("os.symlink", "OOi", src->object, dst->object,
8745 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
8746 return NULL;
8747 }
8748
Larry Hastings9cf065c2012-06-22 16:30:09 -07008749#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008750
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008751 if (windows_has_symlink_unprivileged_flag) {
8752 /* Allow non-admin symlinks if system allows it. */
8753 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8754 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008755
Larry Hastings9cf065c2012-06-22 16:30:09 -07008756 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08008757 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008758 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8759 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8760 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8761 }
8762
8763 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08008764 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07008765 Py_END_ALLOW_THREADS
8766
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008767 if (windows_has_symlink_unprivileged_flag && !result &&
8768 ERROR_INVALID_PARAMETER == GetLastError()) {
8769
8770 Py_BEGIN_ALLOW_THREADS
8771 _Py_BEGIN_SUPPRESS_IPH
8772 /* This error might be caused by
8773 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8774 Try again, and update windows_has_symlink_unprivileged_flag if we
8775 are successful this time.
8776
8777 NOTE: There is a risk of a race condition here if there are other
8778 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8779 another process (or thread) changes that condition in between our
8780 calls to CreateSymbolicLink.
8781 */
8782 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8783 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8784 _Py_END_SUPPRESS_IPH
8785 Py_END_ALLOW_THREADS
8786
8787 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8788 windows_has_symlink_unprivileged_flag = FALSE;
8789 }
8790 }
8791
Larry Hastings2f936352014-08-05 14:04:04 +10008792 if (!result)
8793 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008794
8795#else
8796
Steve Dower6921e732018-03-05 14:26:08 -08008797 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8798 PyErr_SetString(PyExc_ValueError,
8799 "symlink: src and dst must be the same type");
8800 return NULL;
8801 }
8802
Larry Hastings9cf065c2012-06-22 16:30:09 -07008803 Py_BEGIN_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +01008804#ifdef HAVE_SYMLINKAT
8805 if (dir_fd != DEFAULT_DIR_FD) {
8806 if (HAVE_SYMLINKAT_RUNTIME) {
8807 result = symlinkat(src->narrow, dir_fd, dst->narrow);
8808 } else {
8809 symlinkat_unavailable = 1;
8810 }
8811 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008812#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008813 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008814 Py_END_ALLOW_THREADS
8815
Ronald Oussoren41761932020-11-08 10:05:27 +01008816#ifdef HAVE_SYMLINKAT
8817 if (symlinkat_unavailable) {
8818 argument_unavailable_error(NULL, "dir_fd");
8819 return NULL;
8820 }
8821#endif
8822
Larry Hastings2f936352014-08-05 14:04:04 +10008823 if (result)
8824 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008825#endif
8826
Larry Hastings2f936352014-08-05 14:04:04 +10008827 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008828}
8829#endif /* HAVE_SYMLINK */
8830
Larry Hastings9cf065c2012-06-22 16:30:09 -07008831
Brian Curtind40e6f72010-07-08 21:39:08 +00008832
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008833
Larry Hastings605a62d2012-06-24 04:33:36 -07008834static PyStructSequence_Field times_result_fields[] = {
8835 {"user", "user time"},
8836 {"system", "system time"},
8837 {"children_user", "user time of children"},
8838 {"children_system", "system time of children"},
8839 {"elapsed", "elapsed time since an arbitrary point in the past"},
8840 {NULL}
8841};
8842
8843PyDoc_STRVAR(times_result__doc__,
8844"times_result: Result from os.times().\n\n\
8845This object may be accessed either as a tuple of\n\
8846 (user, system, children_user, children_system, elapsed),\n\
8847or via the attributes user, system, children_user, children_system,\n\
8848and elapsed.\n\
8849\n\
8850See os.times for more information.");
8851
8852static PyStructSequence_Desc times_result_desc = {
8853 "times_result", /* name */
8854 times_result__doc__, /* doc */
8855 times_result_fields,
8856 5
8857};
8858
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008859#ifdef MS_WINDOWS
8860#define HAVE_TIMES /* mandatory, for the method table */
8861#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008862
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008863#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008864
8865static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +02008866build_times_result(PyObject *module, double user, double system,
Larry Hastings605a62d2012-06-24 04:33:36 -07008867 double children_user, double children_system,
8868 double elapsed)
8869{
Victor Stinner1c2fa782020-05-10 11:05:29 +02008870 PyObject *TimesResultType = get_posix_state(module)->TimesResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08008871 PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008872 if (value == NULL)
8873 return NULL;
8874
8875#define SET(i, field) \
8876 { \
8877 PyObject *o = PyFloat_FromDouble(field); \
8878 if (!o) { \
8879 Py_DECREF(value); \
8880 return NULL; \
8881 } \
8882 PyStructSequence_SET_ITEM(value, i, o); \
8883 } \
8884
8885 SET(0, user);
8886 SET(1, system);
8887 SET(2, children_user);
8888 SET(3, children_system);
8889 SET(4, elapsed);
8890
8891#undef SET
8892
8893 return value;
8894}
8895
Larry Hastings605a62d2012-06-24 04:33:36 -07008896
Larry Hastings2f936352014-08-05 14:04:04 +10008897#ifndef MS_WINDOWS
8898#define NEED_TICKS_PER_SECOND
8899static long ticks_per_second = -1;
8900#endif /* MS_WINDOWS */
8901
8902/*[clinic input]
8903os.times
8904
8905Return a collection containing process timing information.
8906
8907The object returned behaves like a named tuple with these fields:
8908 (utime, stime, cutime, cstime, elapsed_time)
8909All fields are floating point numbers.
8910[clinic start generated code]*/
8911
Larry Hastings2f936352014-08-05 14:04:04 +10008912static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008913os_times_impl(PyObject *module)
8914/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008915#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008916{
Victor Stinner8c62be82010-05-06 00:08:46 +00008917 FILETIME create, exit, kernel, user;
8918 HANDLE hProc;
8919 hProc = GetCurrentProcess();
8920 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8921 /* The fields of a FILETIME structure are the hi and lo part
8922 of a 64-bit value expressed in 100 nanosecond units.
8923 1e7 is one second in such units; 1e-7 the inverse.
8924 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8925 */
Victor Stinner1c2fa782020-05-10 11:05:29 +02008926 return build_times_result(module,
Victor Stinner8c62be82010-05-06 00:08:46 +00008927 (double)(user.dwHighDateTime*429.4967296 +
8928 user.dwLowDateTime*1e-7),
8929 (double)(kernel.dwHighDateTime*429.4967296 +
8930 kernel.dwLowDateTime*1e-7),
8931 (double)0,
8932 (double)0,
8933 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008934}
Larry Hastings2f936352014-08-05 14:04:04 +10008935#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008936{
Larry Hastings2f936352014-08-05 14:04:04 +10008937
8938
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008939 struct tms t;
8940 clock_t c;
8941 errno = 0;
8942 c = times(&t);
8943 if (c == (clock_t) -1)
8944 return posix_error();
Victor Stinner1c2fa782020-05-10 11:05:29 +02008945 return build_times_result(module,
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008946 (double)t.tms_utime / ticks_per_second,
8947 (double)t.tms_stime / ticks_per_second,
8948 (double)t.tms_cutime / ticks_per_second,
8949 (double)t.tms_cstime / ticks_per_second,
8950 (double)c / ticks_per_second);
8951}
Larry Hastings2f936352014-08-05 14:04:04 +10008952#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008953#endif /* HAVE_TIMES */
8954
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008955
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008956#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008957/*[clinic input]
8958os.getsid
8959
8960 pid: pid_t
8961 /
8962
8963Call the system call getsid(pid) and return the result.
8964[clinic start generated code]*/
8965
Larry Hastings2f936352014-08-05 14:04:04 +10008966static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008967os_getsid_impl(PyObject *module, pid_t pid)
8968/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008969{
Victor Stinner8c62be82010-05-06 00:08:46 +00008970 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008971 sid = getsid(pid);
8972 if (sid < 0)
8973 return posix_error();
8974 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008975}
8976#endif /* HAVE_GETSID */
8977
8978
Guido van Rossumb6775db1994-08-01 11:34:53 +00008979#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008980/*[clinic input]
8981os.setsid
8982
8983Call the system call setsid().
8984[clinic start generated code]*/
8985
Larry Hastings2f936352014-08-05 14:04:04 +10008986static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008987os_setsid_impl(PyObject *module)
8988/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008989{
Victor Stinner8c62be82010-05-06 00:08:46 +00008990 if (setsid() < 0)
8991 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008992 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008993}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008994#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008995
Larry Hastings2f936352014-08-05 14:04:04 +10008996
Guido van Rossumb6775db1994-08-01 11:34:53 +00008997#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008998/*[clinic input]
8999os.setpgid
9000
9001 pid: pid_t
9002 pgrp: pid_t
9003 /
9004
9005Call the system call setpgid(pid, pgrp).
9006[clinic start generated code]*/
9007
Larry Hastings2f936352014-08-05 14:04:04 +10009008static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009009os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
9010/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009011{
Victor Stinner8c62be82010-05-06 00:08:46 +00009012 if (setpgid(pid, pgrp) < 0)
9013 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10009014 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00009015}
Guido van Rossumb6775db1994-08-01 11:34:53 +00009016#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00009017
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009018
Guido van Rossumb6775db1994-08-01 11:34:53 +00009019#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10009020/*[clinic input]
9021os.tcgetpgrp
9022
9023 fd: int
9024 /
9025
9026Return the process group associated with the terminal specified by fd.
9027[clinic start generated code]*/
9028
Larry Hastings2f936352014-08-05 14:04:04 +10009029static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009030os_tcgetpgrp_impl(PyObject *module, int fd)
9031/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009032{
9033 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00009034 if (pgid < 0)
9035 return posix_error();
9036 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00009037}
Guido van Rossumb6775db1994-08-01 11:34:53 +00009038#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00009039
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009040
Guido van Rossumb6775db1994-08-01 11:34:53 +00009041#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10009042/*[clinic input]
9043os.tcsetpgrp
9044
9045 fd: int
9046 pgid: pid_t
9047 /
9048
9049Set the process group associated with the terminal specified by fd.
9050[clinic start generated code]*/
9051
Larry Hastings2f936352014-08-05 14:04:04 +10009052static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009053os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
9054/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009055{
Victor Stinner8c62be82010-05-06 00:08:46 +00009056 if (tcsetpgrp(fd, pgid) < 0)
9057 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10009058 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00009059}
Guido van Rossumb6775db1994-08-01 11:34:53 +00009060#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00009061
Guido van Rossum687dd131993-05-17 08:34:16 +00009062/* Functions acting on file descriptors */
9063
Victor Stinnerdaf45552013-08-28 00:53:59 +02009064#ifdef O_CLOEXEC
9065extern int _Py_open_cloexec_works;
9066#endif
9067
Larry Hastings2f936352014-08-05 14:04:04 +10009068
9069/*[clinic input]
9070os.open -> int
9071 path: path_t
9072 flags: int
9073 mode: int = 0o777
9074 *
9075 dir_fd: dir_fd(requires='openat') = None
9076
9077# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
9078
9079Open a file for low level IO. Returns a file descriptor (integer).
9080
9081If dir_fd is not None, it should be a file descriptor open to a directory,
9082 and path should be relative; path will then be relative to that directory.
9083dir_fd may not be implemented on your platform.
9084 If it is unavailable, using it will raise a NotImplementedError.
9085[clinic start generated code]*/
9086
Larry Hastings2f936352014-08-05 14:04:04 +10009087static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009088os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
9089/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009090{
9091 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009092 int async_err = 0;
Ronald Oussoren41761932020-11-08 10:05:27 +01009093#ifdef HAVE_OPENAT
9094 int openat_unavailable = 0;
9095#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009096
Victor Stinnerdaf45552013-08-28 00:53:59 +02009097#ifdef O_CLOEXEC
9098 int *atomic_flag_works = &_Py_open_cloexec_works;
9099#elif !defined(MS_WINDOWS)
9100 int *atomic_flag_works = NULL;
9101#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00009102
Victor Stinnerdaf45552013-08-28 00:53:59 +02009103#ifdef MS_WINDOWS
9104 flags |= O_NOINHERIT;
9105#elif defined(O_CLOEXEC)
9106 flags |= O_CLOEXEC;
9107#endif
9108
Steve Dowerb82e17e2019-05-23 08:45:22 -07009109 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
9110 return -1;
9111 }
9112
Steve Dower8fc89802015-04-12 00:26:27 -04009113 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009114 do {
9115 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07009116#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009117 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07009118#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07009119#ifdef HAVE_OPENAT
Ronald Oussoren41761932020-11-08 10:05:27 +01009120 if (dir_fd != DEFAULT_DIR_FD) {
9121 if (HAVE_OPENAT_RUNTIME) {
9122 fd = openat(dir_fd, path->narrow, flags, mode);
9123
9124 } else {
9125 openat_unavailable = 1;
9126 fd = -1;
9127 }
9128 } else
Steve Dower6230aaf2016-09-09 09:03:15 -07009129#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009130 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07009131#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009132 Py_END_ALLOW_THREADS
9133 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009134 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00009135
Ronald Oussoren41761932020-11-08 10:05:27 +01009136#ifdef HAVE_OPENAT
9137 if (openat_unavailable) {
9138 argument_unavailable_error(NULL, "dir_fd");
9139 return -1;
9140 }
9141#endif
9142
Victor Stinnerd3ffd322015-09-15 10:11:03 +02009143 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009144 if (!async_err)
9145 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10009146 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009147 }
9148
Victor Stinnerdaf45552013-08-28 00:53:59 +02009149#ifndef MS_WINDOWS
9150 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
9151 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10009152 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009153 }
9154#endif
9155
Larry Hastings2f936352014-08-05 14:04:04 +10009156 return fd;
9157}
9158
9159
9160/*[clinic input]
9161os.close
9162
9163 fd: int
9164
9165Close a file descriptor.
9166[clinic start generated code]*/
9167
Barry Warsaw53699e91996-12-10 23:23:01 +00009168static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009169os_close_impl(PyObject *module, int fd)
9170/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009171{
Larry Hastings2f936352014-08-05 14:04:04 +10009172 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009173 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
9174 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
9175 * for more details.
9176 */
Victor Stinner8c62be82010-05-06 00:08:46 +00009177 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009178 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00009179 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04009180 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00009181 Py_END_ALLOW_THREADS
9182 if (res < 0)
9183 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10009184 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00009185}
9186
Larry Hastings2f936352014-08-05 14:04:04 +10009187/*[clinic input]
9188os.closerange
9189
9190 fd_low: int
9191 fd_high: int
9192 /
9193
9194Closes all file descriptors in [fd_low, fd_high), ignoring errors.
9195[clinic start generated code]*/
9196
Larry Hastings2f936352014-08-05 14:04:04 +10009197static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009198os_closerange_impl(PyObject *module, int fd_low, int fd_high)
9199/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009200{
Victor Stinner8c62be82010-05-06 00:08:46 +00009201 Py_BEGIN_ALLOW_THREADS
Kyle Evansc230fde2020-10-11 13:54:11 -05009202 _Py_closerange(fd_low, fd_high - 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00009203 Py_END_ALLOW_THREADS
9204 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00009205}
9206
9207
Larry Hastings2f936352014-08-05 14:04:04 +10009208/*[clinic input]
9209os.dup -> int
9210
9211 fd: int
9212 /
9213
9214Return a duplicate of a file descriptor.
9215[clinic start generated code]*/
9216
Larry Hastings2f936352014-08-05 14:04:04 +10009217static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009218os_dup_impl(PyObject *module, int fd)
9219/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009220{
9221 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00009222}
9223
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009224
Larry Hastings2f936352014-08-05 14:04:04 +10009225/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009226os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10009227 fd: int
9228 fd2: int
9229 inheritable: bool=True
9230
9231Duplicate file descriptor.
9232[clinic start generated code]*/
9233
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009234static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009235os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009236/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009237{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01009238 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009239#if defined(HAVE_DUP3) && \
9240 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
9241 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03009242 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009243#endif
9244
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009245 if (fd < 0 || fd2 < 0) {
9246 posix_error();
9247 return -1;
9248 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02009249
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009250 /* dup2() can fail with EINTR if the target FD is already open, because it
9251 * then has to be closed. See os_close_impl() for why we don't handle EINTR
9252 * upon close(), and therefore below.
9253 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02009254#ifdef MS_WINDOWS
9255 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009256 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00009257 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04009258 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009259 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009260 if (res < 0) {
9261 posix_error();
9262 return -1;
9263 }
9264 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02009265
9266 /* Character files like console cannot be make non-inheritable */
9267 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
9268 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009269 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009270 }
9271
9272#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
9273 Py_BEGIN_ALLOW_THREADS
9274 if (!inheritable)
9275 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
9276 else
9277 res = dup2(fd, fd2);
9278 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009279 if (res < 0) {
9280 posix_error();
9281 return -1;
9282 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02009283
9284#else
9285
9286#ifdef HAVE_DUP3
9287 if (!inheritable && dup3_works != 0) {
9288 Py_BEGIN_ALLOW_THREADS
9289 res = dup3(fd, fd2, O_CLOEXEC);
9290 Py_END_ALLOW_THREADS
9291 if (res < 0) {
9292 if (dup3_works == -1)
9293 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009294 if (dup3_works) {
9295 posix_error();
9296 return -1;
9297 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02009298 }
9299 }
9300
9301 if (inheritable || dup3_works == 0)
9302 {
9303#endif
9304 Py_BEGIN_ALLOW_THREADS
9305 res = dup2(fd, fd2);
9306 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009307 if (res < 0) {
9308 posix_error();
9309 return -1;
9310 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02009311
9312 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
9313 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009314 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009315 }
9316#ifdef HAVE_DUP3
9317 }
9318#endif
9319
9320#endif
9321
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009322 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00009323}
9324
Larry Hastings2f936352014-08-05 14:04:04 +10009325
Ross Lagerwall7807c352011-03-17 20:20:30 +02009326#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10009327/*[clinic input]
9328os.lockf
9329
9330 fd: int
9331 An open file descriptor.
9332 command: int
9333 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
9334 length: Py_off_t
9335 The number of bytes to lock, starting at the current position.
9336 /
9337
9338Apply, test or remove a POSIX lock on an open file descriptor.
9339
9340[clinic start generated code]*/
9341
Larry Hastings2f936352014-08-05 14:04:04 +10009342static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009343os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
9344/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009345{
9346 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009347
Saiyang Gou7514f4f2020-02-12 23:47:42 -08009348 if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) {
9349 return NULL;
9350 }
9351
Ross Lagerwall7807c352011-03-17 20:20:30 +02009352 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009353 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009354 Py_END_ALLOW_THREADS
9355
9356 if (res < 0)
9357 return posix_error();
9358
9359 Py_RETURN_NONE;
9360}
Larry Hastings2f936352014-08-05 14:04:04 +10009361#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009362
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009363
Larry Hastings2f936352014-08-05 14:04:04 +10009364/*[clinic input]
9365os.lseek -> Py_off_t
9366
9367 fd: int
9368 position: Py_off_t
9369 how: int
9370 /
9371
9372Set the position of a file descriptor. Return the new position.
9373
9374Return the new cursor position in number of bytes
9375relative to the beginning of the file.
9376[clinic start generated code]*/
9377
Larry Hastings2f936352014-08-05 14:04:04 +10009378static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009379os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
9380/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009381{
9382 Py_off_t result;
9383
Guido van Rossum687dd131993-05-17 08:34:16 +00009384#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00009385 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
9386 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10009387 case 0: how = SEEK_SET; break;
9388 case 1: how = SEEK_CUR; break;
9389 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00009390 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009391#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00009392
Victor Stinner8c62be82010-05-06 00:08:46 +00009393 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009394 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02009395#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009396 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00009397#else
Larry Hastings2f936352014-08-05 14:04:04 +10009398 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00009399#endif
Steve Dower8fc89802015-04-12 00:26:27 -04009400 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00009401 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009402 if (result < 0)
9403 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00009404
Larry Hastings2f936352014-08-05 14:04:04 +10009405 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00009406}
9407
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009408
Larry Hastings2f936352014-08-05 14:04:04 +10009409/*[clinic input]
9410os.read
9411 fd: int
9412 length: Py_ssize_t
9413 /
9414
9415Read from a file descriptor. Returns a bytes object.
9416[clinic start generated code]*/
9417
Larry Hastings2f936352014-08-05 14:04:04 +10009418static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009419os_read_impl(PyObject *module, int fd, Py_ssize_t length)
9420/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009421{
Victor Stinner8c62be82010-05-06 00:08:46 +00009422 Py_ssize_t n;
9423 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10009424
9425 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009426 errno = EINVAL;
9427 return posix_error();
9428 }
Larry Hastings2f936352014-08-05 14:04:04 +10009429
Victor Stinner9a0d7a72018-11-22 15:03:40 +01009430 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10009431
9432 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00009433 if (buffer == NULL)
9434 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009435
Victor Stinner66aab0c2015-03-19 22:53:20 +01009436 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
9437 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009438 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01009439 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009440 }
Larry Hastings2f936352014-08-05 14:04:04 +10009441
9442 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00009443 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10009444
Victor Stinner8c62be82010-05-06 00:08:46 +00009445 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00009446}
9447
Ross Lagerwall7807c352011-03-17 20:20:30 +02009448#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009449 || defined(__APPLE__))) \
9450 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
9451 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9452static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009453iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009454{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009455 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009456
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009457 *iov = PyMem_New(struct iovec, cnt);
9458 if (*iov == NULL) {
9459 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01009460 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009461 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009462
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009463 *buf = PyMem_New(Py_buffer, cnt);
9464 if (*buf == NULL) {
Victor Stinner00d7abd2020-12-01 09:56:42 +01009465 PyMem_Free(*iov);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009466 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01009467 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009468 }
9469
9470 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02009471 PyObject *item = PySequence_GetItem(seq, i);
9472 if (item == NULL)
9473 goto fail;
9474 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
9475 Py_DECREF(item);
9476 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009477 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02009478 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009479 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009480 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009481 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009482 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02009483
9484fail:
Victor Stinner00d7abd2020-12-01 09:56:42 +01009485 PyMem_Free(*iov);
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02009486 for (j = 0; j < i; j++) {
9487 PyBuffer_Release(&(*buf)[j]);
9488 }
Victor Stinner00d7abd2020-12-01 09:56:42 +01009489 PyMem_Free(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01009490 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009491}
9492
9493static void
9494iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
9495{
9496 int i;
Victor Stinner00d7abd2020-12-01 09:56:42 +01009497 PyMem_Free(iov);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009498 for (i = 0; i < cnt; i++) {
9499 PyBuffer_Release(&buf[i]);
9500 }
Victor Stinner00d7abd2020-12-01 09:56:42 +01009501 PyMem_Free(buf);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009502}
9503#endif
9504
Larry Hastings2f936352014-08-05 14:04:04 +10009505
Ross Lagerwall7807c352011-03-17 20:20:30 +02009506#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10009507/*[clinic input]
9508os.readv -> Py_ssize_t
9509
9510 fd: int
9511 buffers: object
9512 /
9513
9514Read from a file descriptor fd into an iterable of buffers.
9515
9516The buffers should be mutable buffers accepting bytes.
9517readv will transfer data into each buffer until it is full
9518and then move on to the next buffer in the sequence to hold
9519the rest of the data.
9520
9521readv returns the total number of bytes read,
9522which may be less than the total capacity of all the buffers.
9523[clinic start generated code]*/
9524
Larry Hastings2f936352014-08-05 14:04:04 +10009525static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009526os_readv_impl(PyObject *module, int fd, PyObject *buffers)
9527/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009528{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009529 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009530 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009531 struct iovec *iov;
9532 Py_buffer *buf;
9533
Larry Hastings2f936352014-08-05 14:04:04 +10009534 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009535 PyErr_SetString(PyExc_TypeError,
9536 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009537 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009538 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02009539
Larry Hastings2f936352014-08-05 14:04:04 +10009540 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009541 if (cnt < 0)
9542 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10009543
9544 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
9545 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009546
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009547 do {
9548 Py_BEGIN_ALLOW_THREADS
9549 n = readv(fd, iov, cnt);
9550 Py_END_ALLOW_THREADS
9551 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009552
9553 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10009554 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009555 if (!async_err)
9556 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10009557 return -1;
9558 }
Victor Stinner57ddf782014-01-08 15:21:28 +01009559
Larry Hastings2f936352014-08-05 14:04:04 +10009560 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009561}
Larry Hastings2f936352014-08-05 14:04:04 +10009562#endif /* HAVE_READV */
9563
Ross Lagerwall7807c352011-03-17 20:20:30 +02009564
9565#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10009566/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10009567os.pread
9568
9569 fd: int
Dong-hee Naad7736f2019-09-25 14:47:04 +09009570 length: Py_ssize_t
Larry Hastings2f936352014-08-05 14:04:04 +10009571 offset: Py_off_t
9572 /
9573
9574Read a number of bytes from a file descriptor starting at a particular offset.
9575
9576Read length bytes from file descriptor fd, starting at offset bytes from
9577the beginning of the file. The file offset remains unchanged.
9578[clinic start generated code]*/
9579
Larry Hastings2f936352014-08-05 14:04:04 +10009580static PyObject *
Dong-hee Naad7736f2019-09-25 14:47:04 +09009581os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
9582/*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009583{
Ross Lagerwall7807c352011-03-17 20:20:30 +02009584 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009585 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009586 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009587
Larry Hastings2f936352014-08-05 14:04:04 +10009588 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009589 errno = EINVAL;
9590 return posix_error();
9591 }
Larry Hastings2f936352014-08-05 14:04:04 +10009592 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009593 if (buffer == NULL)
9594 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009595
9596 do {
9597 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009598 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009599 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009600 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009601 Py_END_ALLOW_THREADS
9602 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9603
Ross Lagerwall7807c352011-03-17 20:20:30 +02009604 if (n < 0) {
9605 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009606 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009607 }
Larry Hastings2f936352014-08-05 14:04:04 +10009608 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02009609 _PyBytes_Resize(&buffer, n);
9610 return buffer;
9611}
Larry Hastings2f936352014-08-05 14:04:04 +10009612#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009613
Pablo Galindo4defba32018-01-27 16:16:37 +00009614#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
9615/*[clinic input]
9616os.preadv -> Py_ssize_t
9617
9618 fd: int
9619 buffers: object
9620 offset: Py_off_t
9621 flags: int = 0
9622 /
9623
9624Reads from a file descriptor into a number of mutable bytes-like objects.
9625
9626Combines the functionality of readv() and pread(). As readv(), it will
9627transfer data into each buffer until it is full and then move on to the next
9628buffer in the sequence to hold the rest of the data. Its fourth argument,
9629specifies the file offset at which the input operation is to be performed. It
9630will return the total number of bytes read (which can be less than the total
9631capacity of all the objects).
9632
9633The flags argument contains a bitwise OR of zero or more of the following flags:
9634
9635- RWF_HIPRI
9636- RWF_NOWAIT
9637
9638Using non-zero flags requires Linux 4.6 or newer.
9639[clinic start generated code]*/
9640
9641static Py_ssize_t
9642os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9643 int flags)
9644/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
9645{
9646 Py_ssize_t cnt, n;
9647 int async_err = 0;
9648 struct iovec *iov;
9649 Py_buffer *buf;
9650
9651 if (!PySequence_Check(buffers)) {
9652 PyErr_SetString(PyExc_TypeError,
9653 "preadv2() arg 2 must be a sequence");
9654 return -1;
9655 }
9656
9657 cnt = PySequence_Size(buffers);
9658 if (cnt < 0) {
9659 return -1;
9660 }
9661
9662#ifndef HAVE_PREADV2
9663 if(flags != 0) {
9664 argument_unavailable_error("preadv2", "flags");
9665 return -1;
9666 }
9667#endif
9668
9669 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
9670 return -1;
9671 }
9672#ifdef HAVE_PREADV2
9673 do {
9674 Py_BEGIN_ALLOW_THREADS
9675 _Py_BEGIN_SUPPRESS_IPH
9676 n = preadv2(fd, iov, cnt, offset, flags);
9677 _Py_END_SUPPRESS_IPH
9678 Py_END_ALLOW_THREADS
9679 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9680#else
9681 do {
Ronald Oussoren41761932020-11-08 10:05:27 +01009682#ifdef __APPLE__
9683/* This entire function will be removed from the module dict when the API
9684 * is not available.
9685 */
9686#pragma clang diagnostic push
9687#pragma clang diagnostic ignored "-Wunguarded-availability"
9688#pragma clang diagnostic ignored "-Wunguarded-availability-new"
9689#endif
Pablo Galindo4defba32018-01-27 16:16:37 +00009690 Py_BEGIN_ALLOW_THREADS
9691 _Py_BEGIN_SUPPRESS_IPH
9692 n = preadv(fd, iov, cnt, offset);
9693 _Py_END_SUPPRESS_IPH
9694 Py_END_ALLOW_THREADS
9695 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ronald Oussoren41761932020-11-08 10:05:27 +01009696
9697#ifdef __APPLE__
9698#pragma clang diagnostic pop
9699#endif
9700
Pablo Galindo4defba32018-01-27 16:16:37 +00009701#endif
9702
9703 iov_cleanup(iov, buf, cnt);
9704 if (n < 0) {
9705 if (!async_err) {
9706 posix_error();
9707 }
9708 return -1;
9709 }
9710
9711 return n;
9712}
9713#endif /* HAVE_PREADV */
9714
Larry Hastings2f936352014-08-05 14:04:04 +10009715
9716/*[clinic input]
9717os.write -> Py_ssize_t
9718
9719 fd: int
9720 data: Py_buffer
9721 /
9722
9723Write a bytes object to a file descriptor.
9724[clinic start generated code]*/
9725
Larry Hastings2f936352014-08-05 14:04:04 +10009726static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009727os_write_impl(PyObject *module, int fd, Py_buffer *data)
9728/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009729{
Victor Stinner66aab0c2015-03-19 22:53:20 +01009730 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009731}
9732
9733#ifdef HAVE_SENDFILE
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009734#ifdef __APPLE__
9735/*[clinic input]
9736os.sendfile
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009737
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009738 out_fd: int
9739 in_fd: int
9740 offset: Py_off_t
9741 count as sbytes: Py_off_t
9742 headers: object(c_default="NULL") = ()
9743 trailers: object(c_default="NULL") = ()
9744 flags: int = 0
9745
9746Copy count bytes from file descriptor in_fd to file descriptor out_fd.
9747[clinic start generated code]*/
9748
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009749static PyObject *
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009750os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
9751 Py_off_t sbytes, PyObject *headers, PyObject *trailers,
9752 int flags)
9753/*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/
9754#elif defined(__FreeBSD__) || defined(__DragonFly__)
9755/*[clinic input]
9756os.sendfile
9757
9758 out_fd: int
9759 in_fd: int
9760 offset: Py_off_t
9761 count: Py_ssize_t
9762 headers: object(c_default="NULL") = ()
9763 trailers: object(c_default="NULL") = ()
9764 flags: int = 0
9765
9766Copy count bytes from file descriptor in_fd to file descriptor out_fd.
9767[clinic start generated code]*/
9768
9769static PyObject *
9770os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
9771 Py_ssize_t count, PyObject *headers, PyObject *trailers,
9772 int flags)
9773/*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/
9774#else
9775/*[clinic input]
9776os.sendfile
9777
9778 out_fd: int
9779 in_fd: int
9780 offset as offobj: object
9781 count: Py_ssize_t
9782
9783Copy count bytes from file descriptor in_fd to file descriptor out_fd.
9784[clinic start generated code]*/
9785
9786static PyObject *
9787os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
9788 Py_ssize_t count)
9789/*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/
9790#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009791{
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009792 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009793 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009794
9795#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9796#ifndef __APPLE__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009797 off_t sbytes;
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009798#endif
9799 Py_buffer *hbuf, *tbuf;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009800 struct sf_hdtr sf;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009801
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02009802 sf.headers = NULL;
9803 sf.trailers = NULL;
9804
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009805 if (headers != NULL) {
9806 if (!PySequence_Check(headers)) {
9807 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009808 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009809 return NULL;
9810 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009811 Py_ssize_t i = PySequence_Size(headers);
9812 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009813 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009814 if (i > INT_MAX) {
9815 PyErr_SetString(PyExc_OverflowError,
9816 "sendfile() header is too large");
9817 return NULL;
9818 }
9819 if (i > 0) {
9820 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009821 if (iov_setup(&(sf.headers), &hbuf,
9822 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009823 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009824#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009825 for (i = 0; i < sf.hdr_cnt; i++) {
9826 Py_ssize_t blen = sf.headers[i].iov_len;
9827# define OFF_T_MAX 0x7fffffffffffffff
9828 if (sbytes >= OFF_T_MAX - blen) {
9829 PyErr_SetString(PyExc_OverflowError,
9830 "sendfile() header is too large");
9831 return NULL;
9832 }
9833 sbytes += blen;
9834 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009835#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009836 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009837 }
9838 }
9839 if (trailers != NULL) {
9840 if (!PySequence_Check(trailers)) {
9841 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009842 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009843 return NULL;
9844 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009845 Py_ssize_t i = PySequence_Size(trailers);
9846 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009847 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009848 if (i > INT_MAX) {
9849 PyErr_SetString(PyExc_OverflowError,
9850 "sendfile() trailer is too large");
9851 return NULL;
9852 }
9853 if (i > 0) {
9854 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009855 if (iov_setup(&(sf.trailers), &tbuf,
9856 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009857 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009858 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009859 }
9860 }
9861
Steve Dower8fc89802015-04-12 00:26:27 -04009862 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009863 do {
9864 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009865#ifdef __APPLE__
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009866 ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009867#else
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009868 ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009869#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009870 Py_END_ALLOW_THREADS
9871 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009872 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009873
9874 if (sf.headers != NULL)
9875 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9876 if (sf.trailers != NULL)
9877 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9878
9879 if (ret < 0) {
9880 if ((errno == EAGAIN) || (errno == EBUSY)) {
9881 if (sbytes != 0) {
9882 // some data has been sent
9883 goto done;
9884 }
9885 else {
9886 // no data has been sent; upper application is supposed
9887 // to retry on EAGAIN or EBUSY
9888 return posix_error();
9889 }
9890 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009891 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009892 }
9893 goto done;
9894
9895done:
9896 #if !defined(HAVE_LARGEFILE_SUPPORT)
9897 return Py_BuildValue("l", sbytes);
9898 #else
9899 return Py_BuildValue("L", sbytes);
9900 #endif
9901
9902#else
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009903#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009904 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009905 do {
9906 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009907 ret = sendfile(out_fd, in_fd, NULL, count);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009908 Py_END_ALLOW_THREADS
9909 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009910 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009911 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009912 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009913 }
9914#endif
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009915 off_t offset;
Larry Hastings2f936352014-08-05 14:04:04 +10009916 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009917 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009918
Jakub Kulík8c0be6f2020-09-05 21:10:01 +02009919#if defined(__sun) && defined(__SVR4)
9920 // On Solaris, sendfile raises EINVAL rather than returning 0
9921 // when the offset is equal or bigger than the in_fd size.
Jakub Kulík8c0be6f2020-09-05 21:10:01 +02009922 struct stat st;
9923
9924 do {
9925 Py_BEGIN_ALLOW_THREADS
Jakub Kulíkfa8c9e72020-09-09 21:29:42 +02009926 ret = fstat(in_fd, &st);
Jakub Kulík8c0be6f2020-09-05 21:10:01 +02009927 Py_END_ALLOW_THREADS
Jakub Kulíkfa8c9e72020-09-09 21:29:42 +02009928 } while (ret != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Jakub Kulík8c0be6f2020-09-05 21:10:01 +02009929 if (ret < 0)
9930 return (!async_err) ? posix_error() : NULL;
9931
9932 if (offset >= st.st_size) {
9933 return Py_BuildValue("i", 0);
9934 }
Jakub Stasiakfd4ed572020-11-12 10:49:30 +01009935
9936 // On illumos specifically sendfile() may perform a partial write but
9937 // return -1/an error (in one confirmed case the destination socket
9938 // had a 5 second timeout set and errno was EAGAIN) and it's on the client
9939 // code to check if the offset parameter was modified by sendfile().
9940 //
9941 // We need this variable to track said change.
9942 off_t original_offset = offset;
Jakub Kulík8c0be6f2020-09-05 21:10:01 +02009943#endif
9944
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009945 do {
9946 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009947 ret = sendfile(out_fd, in_fd, &offset, count);
Jakub Stasiakfd4ed572020-11-12 10:49:30 +01009948#if defined(__sun) && defined(__SVR4)
9949 // This handles illumos-specific sendfile() partial write behavior,
9950 // see a comment above for more details.
9951 if (ret < 0 && offset != original_offset) {
9952 ret = offset - original_offset;
9953 }
9954#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009955 Py_END_ALLOW_THREADS
9956 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009957 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009958 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009959 return Py_BuildValue("n", ret);
9960#endif
9961}
Larry Hastings2f936352014-08-05 14:04:04 +10009962#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009963
Larry Hastings2f936352014-08-05 14:04:04 +10009964
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009965#if defined(__APPLE__)
9966/*[clinic input]
9967os._fcopyfile
9968
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009969 in_fd: int
9970 out_fd: int
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009971 flags: int
9972 /
9973
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009974Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009975[clinic start generated code]*/
9976
9977static PyObject *
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009978os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
9979/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009980{
9981 int ret;
9982
9983 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009984 ret = fcopyfile(in_fd, out_fd, NULL, flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009985 Py_END_ALLOW_THREADS
9986 if (ret < 0)
9987 return posix_error();
9988 Py_RETURN_NONE;
9989}
9990#endif
9991
9992
Larry Hastings2f936352014-08-05 14:04:04 +10009993/*[clinic input]
9994os.fstat
9995
9996 fd : int
9997
9998Perform a stat system call on the given file descriptor.
9999
10000Like stat(), but for an open file descriptor.
10001Equivalent to os.stat(fd).
10002[clinic start generated code]*/
10003
Larry Hastings2f936352014-08-05 14:04:04 +100010004static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010005os_fstat_impl(PyObject *module, int fd)
10006/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010007{
Victor Stinner8c62be82010-05-06 00:08:46 +000010008 STRUCT_STAT st;
10009 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010010 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010011
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010012 do {
10013 Py_BEGIN_ALLOW_THREADS
10014 res = FSTAT(fd, &st);
10015 Py_END_ALLOW_THREADS
10016 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +000010017 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +000010018#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +010010019 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +000010020#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010021 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +000010022#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010023 }
Tim Peters5aa91602002-01-30 05:46:57 +000010024
Victor Stinner1c2fa782020-05-10 11:05:29 +020010025 return _pystat_fromstructstat(module, &st);
Guido van Rossum687dd131993-05-17 08:34:16 +000010026}
10027
Larry Hastings2f936352014-08-05 14:04:04 +100010028
10029/*[clinic input]
10030os.isatty -> bool
10031 fd: int
10032 /
10033
10034Return True if the fd is connected to a terminal.
10035
10036Return True if the file descriptor is an open file descriptor
10037connected to the slave end of a terminal.
10038[clinic start generated code]*/
10039
Larry Hastings2f936352014-08-05 14:04:04 +100010040static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010041os_isatty_impl(PyObject *module, int fd)
10042/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010043{
Steve Dower8fc89802015-04-12 00:26:27 -040010044 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040010045 _Py_BEGIN_SUPPRESS_IPH
10046 return_value = isatty(fd);
10047 _Py_END_SUPPRESS_IPH
10048 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100010049}
10050
10051
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010052#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +100010053/*[clinic input]
10054os.pipe
10055
10056Create a pipe.
10057
10058Returns a tuple of two file descriptors:
10059 (read_fd, write_fd)
10060[clinic start generated code]*/
10061
Larry Hastings2f936352014-08-05 14:04:04 +100010062static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010063os_pipe_impl(PyObject *module)
10064/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +000010065{
Victor Stinner8c62be82010-05-06 00:08:46 +000010066 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +020010067#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010068 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +020010069 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +000010070 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +020010071#else
10072 int res;
10073#endif
10074
10075#ifdef MS_WINDOWS
10076 attr.nLength = sizeof(attr);
10077 attr.lpSecurityDescriptor = NULL;
10078 attr.bInheritHandle = FALSE;
10079
10080 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -080010081 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +020010082 ok = CreatePipe(&read, &write, &attr, 0);
10083 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -070010084 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
10085 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +020010086 if (fds[0] == -1 || fds[1] == -1) {
10087 CloseHandle(read);
10088 CloseHandle(write);
10089 ok = 0;
10090 }
10091 }
Steve Dowerc3630612016-11-19 18:41:16 -080010092 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +020010093 Py_END_ALLOW_THREADS
10094
Victor Stinner8c62be82010-05-06 00:08:46 +000010095 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +010010096 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +020010097#else
10098
10099#ifdef HAVE_PIPE2
10100 Py_BEGIN_ALLOW_THREADS
10101 res = pipe2(fds, O_CLOEXEC);
10102 Py_END_ALLOW_THREADS
10103
10104 if (res != 0 && errno == ENOSYS)
10105 {
10106#endif
10107 Py_BEGIN_ALLOW_THREADS
10108 res = pipe(fds);
10109 Py_END_ALLOW_THREADS
10110
10111 if (res == 0) {
10112 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
10113 close(fds[0]);
10114 close(fds[1]);
10115 return NULL;
10116 }
10117 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
10118 close(fds[0]);
10119 close(fds[1]);
10120 return NULL;
10121 }
10122 }
10123#ifdef HAVE_PIPE2
10124 }
10125#endif
10126
10127 if (res != 0)
10128 return PyErr_SetFromErrno(PyExc_OSError);
10129#endif /* !MS_WINDOWS */
10130 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +000010131}
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010132#endif /* HAVE_PIPE */
10133
Larry Hastings2f936352014-08-05 14:04:04 +100010134
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010135#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +100010136/*[clinic input]
10137os.pipe2
10138
10139 flags: int
10140 /
10141
10142Create a pipe with flags set atomically.
10143
10144Returns a tuple of two file descriptors:
10145 (read_fd, write_fd)
10146
10147flags can be constructed by ORing together one or more of these values:
10148O_NONBLOCK, O_CLOEXEC.
10149[clinic start generated code]*/
10150
Larry Hastings2f936352014-08-05 14:04:04 +100010151static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010152os_pipe2_impl(PyObject *module, int flags)
10153/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010154{
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010155 int fds[2];
10156 int res;
10157
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010158 res = pipe2(fds, flags);
10159 if (res != 0)
10160 return posix_error();
10161 return Py_BuildValue("(ii)", fds[0], fds[1]);
10162}
10163#endif /* HAVE_PIPE2 */
10164
Larry Hastings2f936352014-08-05 14:04:04 +100010165
Ross Lagerwall7807c352011-03-17 20:20:30 +020010166#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +100010167/*[clinic input]
10168os.writev -> Py_ssize_t
10169 fd: int
10170 buffers: object
10171 /
10172
10173Iterate over buffers, and write the contents of each to a file descriptor.
10174
10175Returns the total number of bytes written.
10176buffers must be a sequence of bytes-like objects.
10177[clinic start generated code]*/
10178
Larry Hastings2f936352014-08-05 14:04:04 +100010179static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010180os_writev_impl(PyObject *module, int fd, PyObject *buffers)
10181/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010182{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +030010183 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +100010184 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010185 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010186 struct iovec *iov;
10187 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +100010188
10189 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010190 PyErr_SetString(PyExc_TypeError,
10191 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +100010192 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010193 }
Larry Hastings2f936352014-08-05 14:04:04 +100010194 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +030010195 if (cnt < 0)
10196 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010197
Larry Hastings2f936352014-08-05 14:04:04 +100010198 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
10199 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010200 }
10201
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010202 do {
10203 Py_BEGIN_ALLOW_THREADS
10204 result = writev(fd, iov, cnt);
10205 Py_END_ALLOW_THREADS
10206 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +020010207
10208 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010209 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +100010210 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +010010211
Georg Brandl306336b2012-06-24 12:55:33 +020010212 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010213}
Larry Hastings2f936352014-08-05 14:04:04 +100010214#endif /* HAVE_WRITEV */
10215
10216
10217#ifdef HAVE_PWRITE
10218/*[clinic input]
10219os.pwrite -> Py_ssize_t
10220
10221 fd: int
10222 buffer: Py_buffer
10223 offset: Py_off_t
10224 /
10225
10226Write bytes to a file descriptor starting at a particular offset.
10227
10228Write buffer to fd, starting at offset bytes from the beginning of
10229the file. Returns the number of bytes writte. Does not change the
10230current file offset.
10231[clinic start generated code]*/
10232
Larry Hastings2f936352014-08-05 14:04:04 +100010233static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010234os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
10235/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010236{
10237 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010238 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010239
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010240 do {
10241 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -040010242 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010243 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -040010244 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010245 Py_END_ALLOW_THREADS
10246 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010247
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010248 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +100010249 posix_error();
10250 return size;
10251}
10252#endif /* HAVE_PWRITE */
10253
Pablo Galindo4defba32018-01-27 16:16:37 +000010254#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
10255/*[clinic input]
10256os.pwritev -> Py_ssize_t
10257
10258 fd: int
10259 buffers: object
10260 offset: Py_off_t
10261 flags: int = 0
10262 /
10263
10264Writes the contents of bytes-like objects to a file descriptor at a given offset.
10265
10266Combines the functionality of writev() and pwrite(). All buffers must be a sequence
10267of bytes-like objects. Buffers are processed in array order. Entire contents of first
10268buffer is written before proceeding to second, and so on. The operating system may
10269set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
10270This function writes the contents of each object to the file descriptor and returns
10271the total number of bytes written.
10272
10273The flags argument contains a bitwise OR of zero or more of the following flags:
10274
10275- RWF_DSYNC
10276- RWF_SYNC
YoSTEALTH76ef2552020-05-27 15:32:22 -060010277- RWF_APPEND
Pablo Galindo4defba32018-01-27 16:16:37 +000010278
10279Using non-zero flags requires Linux 4.7 or newer.
10280[clinic start generated code]*/
10281
10282static Py_ssize_t
10283os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
10284 int flags)
YoSTEALTH76ef2552020-05-27 15:32:22 -060010285/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=35358c327e1a2a8e]*/
Pablo Galindo4defba32018-01-27 16:16:37 +000010286{
10287 Py_ssize_t cnt;
10288 Py_ssize_t result;
10289 int async_err = 0;
10290 struct iovec *iov;
10291 Py_buffer *buf;
10292
10293 if (!PySequence_Check(buffers)) {
10294 PyErr_SetString(PyExc_TypeError,
10295 "pwritev() arg 2 must be a sequence");
10296 return -1;
10297 }
10298
10299 cnt = PySequence_Size(buffers);
10300 if (cnt < 0) {
10301 return -1;
10302 }
10303
10304#ifndef HAVE_PWRITEV2
10305 if(flags != 0) {
10306 argument_unavailable_error("pwritev2", "flags");
10307 return -1;
10308 }
10309#endif
10310
10311 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
10312 return -1;
10313 }
10314#ifdef HAVE_PWRITEV2
10315 do {
10316 Py_BEGIN_ALLOW_THREADS
10317 _Py_BEGIN_SUPPRESS_IPH
10318 result = pwritev2(fd, iov, cnt, offset, flags);
10319 _Py_END_SUPPRESS_IPH
10320 Py_END_ALLOW_THREADS
10321 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10322#else
Ronald Oussoren41761932020-11-08 10:05:27 +010010323
10324#ifdef __APPLE__
10325/* This entire function will be removed from the module dict when the API
10326 * is not available.
10327 */
10328#pragma clang diagnostic push
10329#pragma clang diagnostic ignored "-Wunguarded-availability"
10330#pragma clang diagnostic ignored "-Wunguarded-availability-new"
10331#endif
Pablo Galindo4defba32018-01-27 16:16:37 +000010332 do {
10333 Py_BEGIN_ALLOW_THREADS
10334 _Py_BEGIN_SUPPRESS_IPH
10335 result = pwritev(fd, iov, cnt, offset);
10336 _Py_END_SUPPRESS_IPH
10337 Py_END_ALLOW_THREADS
10338 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ronald Oussoren41761932020-11-08 10:05:27 +010010339
10340#ifdef __APPLE__
10341#pragma clang diagnostic pop
10342#endif
10343
Pablo Galindo4defba32018-01-27 16:16:37 +000010344#endif
10345
10346 iov_cleanup(iov, buf, cnt);
10347 if (result < 0) {
10348 if (!async_err) {
10349 posix_error();
10350 }
10351 return -1;
10352 }
10353
10354 return result;
10355}
10356#endif /* HAVE_PWRITEV */
10357
Pablo Galindoaac4d032019-05-31 19:39:47 +010010358#ifdef HAVE_COPY_FILE_RANGE
10359/*[clinic input]
10360
10361os.copy_file_range
10362 src: int
10363 Source file descriptor.
10364 dst: int
10365 Destination file descriptor.
10366 count: Py_ssize_t
10367 Number of bytes to copy.
10368 offset_src: object = None
10369 Starting offset in src.
10370 offset_dst: object = None
10371 Starting offset in dst.
10372
10373Copy count bytes from one file descriptor to another.
10374
10375If offset_src is None, then src is read from the current position;
10376respectively for offset_dst.
10377[clinic start generated code]*/
10378
10379static PyObject *
10380os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
10381 PyObject *offset_src, PyObject *offset_dst)
10382/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
10383{
10384 off_t offset_src_val, offset_dst_val;
10385 off_t *p_offset_src = NULL;
10386 off_t *p_offset_dst = NULL;
10387 Py_ssize_t ret;
10388 int async_err = 0;
10389 /* The flags argument is provided to allow
10390 * for future extensions and currently must be to 0. */
10391 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +000010392
10393
Pablo Galindoaac4d032019-05-31 19:39:47 +010010394 if (count < 0) {
10395 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
10396 return NULL;
10397 }
10398
10399 if (offset_src != Py_None) {
10400 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
10401 return NULL;
10402 }
10403 p_offset_src = &offset_src_val;
10404 }
10405
10406 if (offset_dst != Py_None) {
10407 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
10408 return NULL;
10409 }
10410 p_offset_dst = &offset_dst_val;
10411 }
10412
10413 do {
10414 Py_BEGIN_ALLOW_THREADS
10415 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
10416 Py_END_ALLOW_THREADS
10417 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10418
10419 if (ret < 0) {
10420 return (!async_err) ? posix_error() : NULL;
10421 }
10422
10423 return PyLong_FromSsize_t(ret);
10424}
10425#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +100010426
Pablo Galindodedc2cd2020-12-02 17:57:18 +000010427#if (defined(HAVE_SPLICE) && !defined(_AIX))
Pablo Galindoa57b3d32020-11-17 00:00:38 +000010428/*[clinic input]
10429
10430os.splice
10431 src: int
10432 Source file descriptor.
10433 dst: int
10434 Destination file descriptor.
10435 count: Py_ssize_t
10436 Number of bytes to copy.
10437 offset_src: object = None
10438 Starting offset in src.
10439 offset_dst: object = None
10440 Starting offset in dst.
10441 flags: unsigned_int = 0
10442 Flags to modify the semantics of the call.
10443
10444Transfer count bytes from one pipe to a descriptor or vice versa.
10445
10446If offset_src is None, then src is read from the current position;
10447respectively for offset_dst. The offset associated to the file
10448descriptor that refers to a pipe must be None.
10449[clinic start generated code]*/
10450
10451static PyObject *
10452os_splice_impl(PyObject *module, int src, int dst, Py_ssize_t count,
10453 PyObject *offset_src, PyObject *offset_dst,
10454 unsigned int flags)
10455/*[clinic end generated code: output=d0386f25a8519dc5 input=047527c66c6d2e0a]*/
10456{
10457 off_t offset_src_val, offset_dst_val;
10458 off_t *p_offset_src = NULL;
10459 off_t *p_offset_dst = NULL;
10460 Py_ssize_t ret;
10461 int async_err = 0;
10462
10463 if (count < 0) {
10464 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
10465 return NULL;
10466 }
10467
10468 if (offset_src != Py_None) {
10469 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
10470 return NULL;
10471 }
10472 p_offset_src = &offset_src_val;
10473 }
10474
10475 if (offset_dst != Py_None) {
10476 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
10477 return NULL;
10478 }
10479 p_offset_dst = &offset_dst_val;
10480 }
10481
10482 do {
10483 Py_BEGIN_ALLOW_THREADS
10484 ret = splice(src, p_offset_src, dst, p_offset_dst, count, flags);
10485 Py_END_ALLOW_THREADS
10486 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10487
10488 if (ret < 0) {
10489 return (!async_err) ? posix_error() : NULL;
10490 }
10491
10492 return PyLong_FromSsize_t(ret);
10493}
10494#endif /* HAVE_SPLICE*/
10495
Larry Hastings2f936352014-08-05 14:04:04 +100010496#ifdef HAVE_MKFIFO
10497/*[clinic input]
10498os.mkfifo
10499
10500 path: path_t
10501 mode: int=0o666
10502 *
10503 dir_fd: dir_fd(requires='mkfifoat')=None
10504
10505Create a "fifo" (a POSIX named pipe).
10506
10507If dir_fd is not None, it should be a file descriptor open to a directory,
10508 and path should be relative; path will then be relative to that directory.
10509dir_fd may not be implemented on your platform.
10510 If it is unavailable, using it will raise a NotImplementedError.
10511[clinic start generated code]*/
10512
Larry Hastings2f936352014-08-05 14:04:04 +100010513static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010514os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
10515/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010516{
10517 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010518 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010519
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010520 do {
10521 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +100010522#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010523 if (dir_fd != DEFAULT_DIR_FD)
10524 result = mkfifoat(dir_fd, path->narrow, mode);
10525 else
Ross Lagerwall7807c352011-03-17 20:20:30 +020010526#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010527 result = mkfifo(path->narrow, mode);
10528 Py_END_ALLOW_THREADS
10529 } while (result != 0 && errno == EINTR &&
10530 !(async_err = PyErr_CheckSignals()));
10531 if (result != 0)
10532 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010533
10534 Py_RETURN_NONE;
10535}
10536#endif /* HAVE_MKFIFO */
10537
10538
10539#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
10540/*[clinic input]
10541os.mknod
10542
10543 path: path_t
10544 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010545 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +100010546 *
10547 dir_fd: dir_fd(requires='mknodat')=None
10548
10549Create a node in the file system.
10550
10551Create a node in the file system (file, device special file or named pipe)
10552at path. mode specifies both the permissions to use and the
10553type of node to be created, being combined (bitwise OR) with one of
10554S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
10555device defines the newly created device special file (probably using
10556os.makedev()). Otherwise device is ignored.
10557
10558If dir_fd is not None, it should be a file descriptor open to a directory,
10559 and path should be relative; path will then be relative to that directory.
10560dir_fd may not be implemented on your platform.
10561 If it is unavailable, using it will raise a NotImplementedError.
10562[clinic start generated code]*/
10563
Larry Hastings2f936352014-08-05 14:04:04 +100010564static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010565os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -040010566 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010567/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010568{
10569 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010570 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010571
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010572 do {
10573 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +100010574#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010575 if (dir_fd != DEFAULT_DIR_FD)
10576 result = mknodat(dir_fd, path->narrow, mode, device);
10577 else
Larry Hastings2f936352014-08-05 14:04:04 +100010578#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010579 result = mknod(path->narrow, mode, device);
10580 Py_END_ALLOW_THREADS
10581 } while (result != 0 && errno == EINTR &&
10582 !(async_err = PyErr_CheckSignals()));
10583 if (result != 0)
10584 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010585
10586 Py_RETURN_NONE;
10587}
10588#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
10589
10590
10591#ifdef HAVE_DEVICE_MACROS
10592/*[clinic input]
10593os.major -> unsigned_int
10594
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010595 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010596 /
10597
10598Extracts a device major number from a raw device number.
10599[clinic start generated code]*/
10600
Larry Hastings2f936352014-08-05 14:04:04 +100010601static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010602os_major_impl(PyObject *module, dev_t device)
10603/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010604{
10605 return major(device);
10606}
10607
10608
10609/*[clinic input]
10610os.minor -> unsigned_int
10611
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010612 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010613 /
10614
10615Extracts a device minor number from a raw device number.
10616[clinic start generated code]*/
10617
Larry Hastings2f936352014-08-05 14:04:04 +100010618static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010619os_minor_impl(PyObject *module, dev_t device)
10620/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010621{
10622 return minor(device);
10623}
10624
10625
10626/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010627os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010628
10629 major: int
10630 minor: int
10631 /
10632
10633Composes a raw device number from the major and minor device numbers.
10634[clinic start generated code]*/
10635
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010636static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010637os_makedev_impl(PyObject *module, int major, int minor)
10638/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010639{
10640 return makedev(major, minor);
10641}
10642#endif /* HAVE_DEVICE_MACROS */
10643
10644
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010645#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010646/*[clinic input]
10647os.ftruncate
10648
10649 fd: int
10650 length: Py_off_t
10651 /
10652
10653Truncate a file, specified by file descriptor, to a specific length.
10654[clinic start generated code]*/
10655
Larry Hastings2f936352014-08-05 14:04:04 +100010656static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010657os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
10658/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010659{
10660 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010661 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010662
Steve Dowerb82e17e2019-05-23 08:45:22 -070010663 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
10664 return NULL;
10665 }
10666
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010667 do {
10668 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010669 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010670#ifdef MS_WINDOWS
10671 result = _chsize_s(fd, length);
10672#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010673 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010674#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010675 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010676 Py_END_ALLOW_THREADS
10677 } while (result != 0 && errno == EINTR &&
10678 !(async_err = PyErr_CheckSignals()));
10679 if (result != 0)
10680 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010681 Py_RETURN_NONE;
10682}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010683#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010684
10685
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010686#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010687/*[clinic input]
10688os.truncate
10689 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
10690 length: Py_off_t
10691
10692Truncate a file, specified by path, to a specific length.
10693
10694On some platforms, path may also be specified as an open file descriptor.
10695 If this functionality is unavailable, using it raises an exception.
10696[clinic start generated code]*/
10697
Larry Hastings2f936352014-08-05 14:04:04 +100010698static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010699os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
10700/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010701{
10702 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010703#ifdef MS_WINDOWS
10704 int fd;
10705#endif
10706
10707 if (path->fd != -1)
10708 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010709
Steve Dowerb82e17e2019-05-23 08:45:22 -070010710 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
10711 return NULL;
10712 }
10713
Larry Hastings2f936352014-08-05 14:04:04 +100010714 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010715 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010716#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070010717 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +020010718 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010719 result = -1;
10720 else {
10721 result = _chsize_s(fd, length);
10722 close(fd);
10723 if (result < 0)
10724 errno = result;
10725 }
10726#else
10727 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010728#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010729 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +100010730 Py_END_ALLOW_THREADS
10731 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +030010732 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +100010733
10734 Py_RETURN_NONE;
10735}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010736#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010737
Ross Lagerwall7807c352011-03-17 20:20:30 +020010738
Victor Stinnerd6b17692014-09-30 12:20:05 +020010739/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
10740 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
10741 defined, which is the case in Python on AIX. AIX bug report:
10742 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
10743#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
10744# define POSIX_FADVISE_AIX_BUG
10745#endif
10746
Victor Stinnerec39e262014-09-30 12:35:58 +020010747
Victor Stinnerd6b17692014-09-30 12:20:05 +020010748#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010749/*[clinic input]
10750os.posix_fallocate
10751
10752 fd: int
10753 offset: Py_off_t
10754 length: Py_off_t
10755 /
10756
10757Ensure a file has allocated at least a particular number of bytes on disk.
10758
10759Ensure that the file specified by fd encompasses a range of bytes
10760starting at offset bytes from the beginning and continuing for length bytes.
10761[clinic start generated code]*/
10762
Larry Hastings2f936352014-08-05 14:04:04 +100010763static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010764os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010765 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010766/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010767{
10768 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010769 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010770
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010771 do {
10772 Py_BEGIN_ALLOW_THREADS
10773 result = posix_fallocate(fd, offset, length);
10774 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010775 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10776
10777 if (result == 0)
10778 Py_RETURN_NONE;
10779
10780 if (async_err)
10781 return NULL;
10782
10783 errno = result;
10784 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010785}
Victor Stinnerec39e262014-09-30 12:35:58 +020010786#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +100010787
Ross Lagerwall7807c352011-03-17 20:20:30 +020010788
Victor Stinnerd6b17692014-09-30 12:20:05 +020010789#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010790/*[clinic input]
10791os.posix_fadvise
10792
10793 fd: int
10794 offset: Py_off_t
10795 length: Py_off_t
10796 advice: int
10797 /
10798
10799Announce an intention to access data in a specific pattern.
10800
10801Announce an intention to access data in a specific pattern, thus allowing
10802the kernel to make optimizations.
10803The advice applies to the region of the file specified by fd starting at
10804offset and continuing for length bytes.
10805advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
10806POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
10807POSIX_FADV_DONTNEED.
10808[clinic start generated code]*/
10809
Larry Hastings2f936352014-08-05 14:04:04 +100010810static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010811os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010812 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010813/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010814{
10815 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010816 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010817
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010818 do {
10819 Py_BEGIN_ALLOW_THREADS
10820 result = posix_fadvise(fd, offset, length, advice);
10821 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010822 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10823
10824 if (result == 0)
10825 Py_RETURN_NONE;
10826
10827 if (async_err)
10828 return NULL;
10829
10830 errno = result;
10831 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010832}
Victor Stinnerec39e262014-09-30 12:35:58 +020010833#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010834
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000010835
Thomas Hellerf78f12a2007-11-08 19:33:05 +000010836#ifdef MS_WINDOWS
Victor Stinner161e7b32020-01-24 11:53:44 +010010837static PyObject*
10838win32_putenv(PyObject *name, PyObject *value)
10839{
10840 /* Search from index 1 because on Windows starting '=' is allowed for
10841 defining hidden environment variables. */
10842 if (PyUnicode_GET_LENGTH(name) == 0 ||
10843 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
10844 {
10845 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10846 return NULL;
10847 }
10848 PyObject *unicode;
10849 if (value != NULL) {
10850 unicode = PyUnicode_FromFormat("%U=%U", name, value);
10851 }
10852 else {
10853 unicode = PyUnicode_FromFormat("%U=", name);
10854 }
10855 if (unicode == NULL) {
10856 return NULL;
10857 }
10858
10859 Py_ssize_t size;
10860 /* PyUnicode_AsWideCharString() rejects embedded null characters */
10861 wchar_t *env = PyUnicode_AsWideCharString(unicode, &size);
10862 Py_DECREF(unicode);
10863
10864 if (env == NULL) {
10865 return NULL;
10866 }
10867 if (size > _MAX_ENV) {
10868 PyErr_Format(PyExc_ValueError,
10869 "the environment variable is longer than %u characters",
10870 _MAX_ENV);
10871 PyMem_Free(env);
10872 return NULL;
10873 }
10874
10875 /* _wputenv() and SetEnvironmentVariableW() update the environment in the
10876 Process Environment Block (PEB). _wputenv() also updates CRT 'environ'
10877 and '_wenviron' variables, whereas SetEnvironmentVariableW() does not.
10878
10879 Prefer _wputenv() to be compatible with C libraries using CRT
10880 variables and CRT functions using these variables (ex: getenv()). */
10881 int err = _wputenv(env);
10882 PyMem_Free(env);
10883
10884 if (err) {
10885 posix_error();
10886 return NULL;
10887 }
10888
10889 Py_RETURN_NONE;
10890}
10891#endif
10892
10893
10894#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010895/*[clinic input]
10896os.putenv
10897
10898 name: unicode
10899 value: unicode
10900 /
10901
10902Change or add an environment variable.
10903[clinic start generated code]*/
10904
Larry Hastings2f936352014-08-05 14:04:04 +100010905static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010906os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10907/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010908{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010909 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10910 return NULL;
10911 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010912 return win32_putenv(name, value);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010913}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010914#else
Larry Hastings2f936352014-08-05 14:04:04 +100010915/*[clinic input]
10916os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010917
Larry Hastings2f936352014-08-05 14:04:04 +100010918 name: FSConverter
10919 value: FSConverter
10920 /
10921
10922Change or add an environment variable.
10923[clinic start generated code]*/
10924
Larry Hastings2f936352014-08-05 14:04:04 +100010925static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010926os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10927/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010928{
Serhiy Storchaka77703942017-06-25 07:33:01 +030010929 const char *name_string = PyBytes_AS_STRING(name);
10930 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010931
Serhiy Storchaka77703942017-06-25 07:33:01 +030010932 if (strchr(name_string, '=') != NULL) {
10933 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10934 return NULL;
10935 }
Victor Stinnerb477d192020-01-22 22:48:16 +010010936
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010937 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10938 return NULL;
10939 }
10940
Victor Stinnerb477d192020-01-22 22:48:16 +010010941 if (setenv(name_string, value_string, 1)) {
10942 return posix_error();
10943 }
Larry Hastings2f936352014-08-05 14:04:04 +100010944 Py_RETURN_NONE;
10945}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010946#endif /* !defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100010947
10948
Victor Stinner161e7b32020-01-24 11:53:44 +010010949#ifdef MS_WINDOWS
10950/*[clinic input]
10951os.unsetenv
10952 name: unicode
10953 /
10954
10955Delete an environment variable.
10956[clinic start generated code]*/
10957
10958static PyObject *
10959os_unsetenv_impl(PyObject *module, PyObject *name)
10960/*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/
10961{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010962 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10963 return NULL;
10964 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010965 return win32_putenv(name, NULL);
10966}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010967#else
Larry Hastings2f936352014-08-05 14:04:04 +100010968/*[clinic input]
10969os.unsetenv
10970 name: FSConverter
10971 /
10972
10973Delete an environment variable.
10974[clinic start generated code]*/
10975
Larry Hastings2f936352014-08-05 14:04:04 +100010976static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010977os_unsetenv_impl(PyObject *module, PyObject *name)
10978/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010979{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010980 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10981 return NULL;
10982 }
Victor Stinner984890f2011-11-24 13:53:38 +010010983#ifdef HAVE_BROKEN_UNSETENV
10984 unsetenv(PyBytes_AS_STRING(name));
10985#else
Victor Stinner161e7b32020-01-24 11:53:44 +010010986 int err = unsetenv(PyBytes_AS_STRING(name));
10987 if (err) {
Victor Stinner60b385e2011-11-22 22:01:28 +010010988 return posix_error();
Victor Stinner161e7b32020-01-24 11:53:44 +010010989 }
Victor Stinner984890f2011-11-24 13:53:38 +010010990#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010991
Victor Stinner84ae1182010-05-06 22:05:07 +000010992 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010993}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010994#endif /* !MS_WINDOWS */
Guido van Rossumc524d952001-10-19 01:31:59 +000010995
Larry Hastings2f936352014-08-05 14:04:04 +100010996
10997/*[clinic input]
10998os.strerror
10999
11000 code: int
11001 /
11002
11003Translate an error code to a message string.
11004[clinic start generated code]*/
11005
Larry Hastings2f936352014-08-05 14:04:04 +100011006static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011007os_strerror_impl(PyObject *module, int code)
11008/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011009{
11010 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000011011 if (message == NULL) {
11012 PyErr_SetString(PyExc_ValueError,
11013 "strerror() argument out of range");
11014 return NULL;
11015 }
Victor Stinner1b579672011-12-17 05:47:23 +010011016 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000011017}
Guido van Rossumb6a47161997-09-15 22:54:34 +000011018
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000011019
Guido van Rossumc9641791998-08-04 15:26:23 +000011020#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000011021#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100011022/*[clinic input]
11023os.WCOREDUMP -> bool
11024
11025 status: int
11026 /
11027
11028Return True if the process returning status was dumped to a core file.
11029[clinic start generated code]*/
11030
Larry Hastings2f936352014-08-05 14:04:04 +100011031static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011032os_WCOREDUMP_impl(PyObject *module, int status)
11033/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011034{
11035 WAIT_TYPE wait_status;
11036 WAIT_STATUS_INT(wait_status) = status;
11037 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000011038}
11039#endif /* WCOREDUMP */
11040
Larry Hastings2f936352014-08-05 14:04:04 +100011041
Fred Drake106c1a02002-04-23 15:58:02 +000011042#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100011043/*[clinic input]
11044os.WIFCONTINUED -> bool
11045
11046 status: int
11047
11048Return True if a particular process was continued from a job control stop.
11049
11050Return True if the process returning status was continued from a
11051job control stop.
11052[clinic start generated code]*/
11053
Larry Hastings2f936352014-08-05 14:04:04 +100011054static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011055os_WIFCONTINUED_impl(PyObject *module, int status)
11056/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011057{
11058 WAIT_TYPE wait_status;
11059 WAIT_STATUS_INT(wait_status) = status;
11060 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000011061}
11062#endif /* WIFCONTINUED */
11063
Larry Hastings2f936352014-08-05 14:04:04 +100011064
Guido van Rossumc9641791998-08-04 15:26:23 +000011065#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100011066/*[clinic input]
11067os.WIFSTOPPED -> bool
11068
11069 status: int
11070
11071Return True if the process returning status was stopped.
11072[clinic start generated code]*/
11073
Larry Hastings2f936352014-08-05 14:04:04 +100011074static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011075os_WIFSTOPPED_impl(PyObject *module, int status)
11076/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011077{
11078 WAIT_TYPE wait_status;
11079 WAIT_STATUS_INT(wait_status) = status;
11080 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011081}
11082#endif /* WIFSTOPPED */
11083
Larry Hastings2f936352014-08-05 14:04:04 +100011084
Guido van Rossumc9641791998-08-04 15:26:23 +000011085#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100011086/*[clinic input]
11087os.WIFSIGNALED -> bool
11088
11089 status: int
11090
11091Return True if the process returning status was terminated by a signal.
11092[clinic start generated code]*/
11093
Larry Hastings2f936352014-08-05 14:04:04 +100011094static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011095os_WIFSIGNALED_impl(PyObject *module, int status)
11096/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011097{
11098 WAIT_TYPE wait_status;
11099 WAIT_STATUS_INT(wait_status) = status;
11100 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011101}
11102#endif /* WIFSIGNALED */
11103
Larry Hastings2f936352014-08-05 14:04:04 +100011104
Guido van Rossumc9641791998-08-04 15:26:23 +000011105#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100011106/*[clinic input]
11107os.WIFEXITED -> bool
11108
11109 status: int
11110
11111Return True if the process returning status exited via the exit() system call.
11112[clinic start generated code]*/
11113
Larry Hastings2f936352014-08-05 14:04:04 +100011114static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011115os_WIFEXITED_impl(PyObject *module, int status)
11116/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011117{
11118 WAIT_TYPE wait_status;
11119 WAIT_STATUS_INT(wait_status) = status;
11120 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011121}
11122#endif /* WIFEXITED */
11123
Larry Hastings2f936352014-08-05 14:04:04 +100011124
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000011125#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100011126/*[clinic input]
11127os.WEXITSTATUS -> int
11128
11129 status: int
11130
11131Return the process return code from status.
11132[clinic start generated code]*/
11133
Larry Hastings2f936352014-08-05 14:04:04 +100011134static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011135os_WEXITSTATUS_impl(PyObject *module, int status)
11136/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011137{
11138 WAIT_TYPE wait_status;
11139 WAIT_STATUS_INT(wait_status) = status;
11140 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011141}
11142#endif /* WEXITSTATUS */
11143
Larry Hastings2f936352014-08-05 14:04:04 +100011144
Guido van Rossumc9641791998-08-04 15:26:23 +000011145#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100011146/*[clinic input]
11147os.WTERMSIG -> int
11148
11149 status: int
11150
11151Return the signal that terminated the process that provided the status value.
11152[clinic start generated code]*/
11153
Larry Hastings2f936352014-08-05 14:04:04 +100011154static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011155os_WTERMSIG_impl(PyObject *module, int status)
11156/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011157{
11158 WAIT_TYPE wait_status;
11159 WAIT_STATUS_INT(wait_status) = status;
11160 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011161}
11162#endif /* WTERMSIG */
11163
Larry Hastings2f936352014-08-05 14:04:04 +100011164
Guido van Rossumc9641791998-08-04 15:26:23 +000011165#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100011166/*[clinic input]
11167os.WSTOPSIG -> int
11168
11169 status: int
11170
11171Return the signal that stopped the process that provided the status value.
11172[clinic start generated code]*/
11173
Larry Hastings2f936352014-08-05 14:04:04 +100011174static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011175os_WSTOPSIG_impl(PyObject *module, int status)
11176/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011177{
11178 WAIT_TYPE wait_status;
11179 WAIT_STATUS_INT(wait_status) = status;
11180 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011181}
11182#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000011183#endif /* HAVE_SYS_WAIT_H */
11184
11185
Thomas Wouters477c8d52006-05-27 19:21:47 +000011186#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000011187#ifdef _SCO_DS
11188/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
11189 needed definitions in sys/statvfs.h */
11190#define _SVID3
11191#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011192#include <sys/statvfs.h>
11193
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011194static PyObject*
Victor Stinner1c2fa782020-05-10 11:05:29 +020011195_pystatvfs_fromstructstatvfs(PyObject *module, struct statvfs st) {
11196 PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -080011197 PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000011198 if (v == NULL)
11199 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011200
11201#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000011202 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
11203 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
11204 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
11205 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
11206 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
11207 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
11208 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
11209 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
11210 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
11211 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011212#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011213 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
11214 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
11215 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011216 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000011217 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011218 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000011219 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011220 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000011221 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011222 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000011223 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011224 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000011225 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011226 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000011227 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
11228 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011229#endif
Michael Felt502d5512018-01-05 13:01:58 +010011230/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
11231 * (issue #32390). */
11232#if defined(_AIX) && defined(_ALL_SOURCE)
11233 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
11234#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010011235 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010011236#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010011237 if (PyErr_Occurred()) {
11238 Py_DECREF(v);
11239 return NULL;
11240 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011241
Victor Stinner8c62be82010-05-06 00:08:46 +000011242 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011243}
11244
Larry Hastings2f936352014-08-05 14:04:04 +100011245
11246/*[clinic input]
11247os.fstatvfs
11248 fd: int
11249 /
11250
11251Perform an fstatvfs system call on the given fd.
11252
11253Equivalent to statvfs(fd).
11254[clinic start generated code]*/
11255
Larry Hastings2f936352014-08-05 14:04:04 +100011256static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011257os_fstatvfs_impl(PyObject *module, int fd)
11258/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011259{
11260 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000011261 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000011262 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011263
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000011264 do {
11265 Py_BEGIN_ALLOW_THREADS
11266 result = fstatvfs(fd, &st);
11267 Py_END_ALLOW_THREADS
11268 } while (result != 0 && errno == EINTR &&
11269 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100011270 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000011271 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011272
Victor Stinner1c2fa782020-05-10 11:05:29 +020011273 return _pystatvfs_fromstructstatvfs(module, st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000011274}
Larry Hastings2f936352014-08-05 14:04:04 +100011275#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000011276
11277
Thomas Wouters477c8d52006-05-27 19:21:47 +000011278#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000011279#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100011280/*[clinic input]
11281os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000011282
Larry Hastings2f936352014-08-05 14:04:04 +100011283 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
11284
11285Perform a statvfs system call on the given path.
11286
11287path may always be specified as a string.
11288On some platforms, path may also be specified as an open file descriptor.
11289 If this functionality is unavailable, using it raises an exception.
11290[clinic start generated code]*/
11291
Larry Hastings2f936352014-08-05 14:04:04 +100011292static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011293os_statvfs_impl(PyObject *module, path_t *path)
11294/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011295{
11296 int result;
11297 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011298
11299 Py_BEGIN_ALLOW_THREADS
11300#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100011301 if (path->fd != -1) {
Larry Hastings2f936352014-08-05 14:04:04 +100011302 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011303 }
11304 else
11305#endif
Larry Hastings2f936352014-08-05 14:04:04 +100011306 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011307 Py_END_ALLOW_THREADS
11308
11309 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011310 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011311 }
11312
Victor Stinner1c2fa782020-05-10 11:05:29 +020011313 return _pystatvfs_fromstructstatvfs(module, st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000011314}
Larry Hastings2f936352014-08-05 14:04:04 +100011315#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
11316
Guido van Rossum94f6f721999-01-06 18:42:14 +000011317
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011318#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100011319/*[clinic input]
11320os._getdiskusage
11321
Steve Dower23ad6d02018-02-22 10:39:10 -080011322 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100011323
11324Return disk usage statistics about the given path as a (total, free) tuple.
11325[clinic start generated code]*/
11326
Larry Hastings2f936352014-08-05 14:04:04 +100011327static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080011328os__getdiskusage_impl(PyObject *module, path_t *path)
11329/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011330{
11331 BOOL retval;
11332 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040011333 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011334
11335 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080011336 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011337 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040011338 if (retval == 0) {
11339 if (GetLastError() == ERROR_DIRECTORY) {
11340 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011341
Joe Pamerc8c02492018-09-25 10:57:36 -040011342 dir_path = PyMem_New(wchar_t, path->length + 1);
11343 if (dir_path == NULL) {
11344 return PyErr_NoMemory();
11345 }
11346
11347 wcscpy_s(dir_path, path->length + 1, path->wide);
11348
11349 if (_dirnameW(dir_path) != -1) {
11350 Py_BEGIN_ALLOW_THREADS
11351 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
11352 Py_END_ALLOW_THREADS
11353 }
11354 /* Record the last error in case it's modified by PyMem_Free. */
11355 err = GetLastError();
11356 PyMem_Free(dir_path);
11357 if (retval) {
11358 goto success;
11359 }
11360 }
11361 return PyErr_SetFromWindowsErr(err);
11362 }
11363
11364success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011365 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
11366}
Larry Hastings2f936352014-08-05 14:04:04 +100011367#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011368
11369
Fred Drakec9680921999-12-13 16:37:25 +000011370/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
11371 * It maps strings representing configuration variable names to
11372 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000011373 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000011374 * rarely-used constants. There are three separate tables that use
11375 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000011376 *
11377 * This code is always included, even if none of the interfaces that
11378 * need it are included. The #if hackery needed to avoid it would be
11379 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000011380 */
11381struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011382 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030011383 int value;
Fred Drakec9680921999-12-13 16:37:25 +000011384};
11385
Fred Drake12c6e2d1999-12-14 21:25:03 +000011386static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011387conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000011388 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000011389{
Christian Heimes217cfd12007-12-02 14:31:20 +000011390 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030011391 int value = _PyLong_AsInt(arg);
11392 if (value == -1 && PyErr_Occurred())
11393 return 0;
11394 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000011395 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000011396 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000011397 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000011398 /* look up the value in the table using a binary search */
11399 size_t lo = 0;
11400 size_t mid;
11401 size_t hi = tablesize;
11402 int cmp;
11403 const char *confname;
11404 if (!PyUnicode_Check(arg)) {
11405 PyErr_SetString(PyExc_TypeError,
11406 "configuration names must be strings or integers");
11407 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000011408 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020011409 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000011410 if (confname == NULL)
11411 return 0;
11412 while (lo < hi) {
11413 mid = (lo + hi) / 2;
11414 cmp = strcmp(confname, table[mid].name);
11415 if (cmp < 0)
11416 hi = mid;
11417 else if (cmp > 0)
11418 lo = mid + 1;
11419 else {
11420 *valuep = table[mid].value;
11421 return 1;
11422 }
11423 }
11424 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
11425 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000011426 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000011427}
11428
11429
11430#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
11431static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000011432#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011433 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011434#endif
11435#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011436 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011437#endif
Fred Drakec9680921999-12-13 16:37:25 +000011438#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011439 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011440#endif
11441#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000011442 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000011443#endif
11444#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000011445 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000011446#endif
11447#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000011448 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000011449#endif
11450#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011451 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011452#endif
11453#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000011454 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000011455#endif
11456#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011457 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000011458#endif
11459#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011460 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011461#endif
11462#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011463 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000011464#endif
11465#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011466 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011467#endif
11468#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000011469 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000011470#endif
11471#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011472 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011473#endif
11474#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000011475 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000011476#endif
11477#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011478 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011479#endif
11480#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011481 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000011482#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000011483#ifdef _PC_ACL_ENABLED
11484 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
11485#endif
11486#ifdef _PC_MIN_HOLE_SIZE
11487 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
11488#endif
11489#ifdef _PC_ALLOC_SIZE_MIN
11490 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
11491#endif
11492#ifdef _PC_REC_INCR_XFER_SIZE
11493 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
11494#endif
11495#ifdef _PC_REC_MAX_XFER_SIZE
11496 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
11497#endif
11498#ifdef _PC_REC_MIN_XFER_SIZE
11499 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
11500#endif
11501#ifdef _PC_REC_XFER_ALIGN
11502 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
11503#endif
11504#ifdef _PC_SYMLINK_MAX
11505 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
11506#endif
11507#ifdef _PC_XATTR_ENABLED
11508 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
11509#endif
11510#ifdef _PC_XATTR_EXISTS
11511 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
11512#endif
11513#ifdef _PC_TIMESTAMP_RESOLUTION
11514 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
11515#endif
Fred Drakec9680921999-12-13 16:37:25 +000011516};
11517
Fred Drakec9680921999-12-13 16:37:25 +000011518static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011519conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011520{
11521 return conv_confname(arg, valuep, posix_constants_pathconf,
11522 sizeof(posix_constants_pathconf)
11523 / sizeof(struct constdef));
11524}
11525#endif
11526
Larry Hastings2f936352014-08-05 14:04:04 +100011527
Fred Drakec9680921999-12-13 16:37:25 +000011528#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100011529/*[clinic input]
11530os.fpathconf -> long
11531
Gregory P. Smith3ccb96c2020-06-20 15:06:48 -070011532 fd: fildes
Larry Hastings2f936352014-08-05 14:04:04 +100011533 name: path_confname
11534 /
11535
11536Return the configuration limit name for the file descriptor fd.
11537
11538If there is no limit, return -1.
11539[clinic start generated code]*/
11540
Larry Hastings2f936352014-08-05 14:04:04 +100011541static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011542os_fpathconf_impl(PyObject *module, int fd, int name)
Gregory P. Smith3ccb96c2020-06-20 15:06:48 -070011543/*[clinic end generated code: output=d5b7042425fc3e21 input=5b8d2471cfaae186]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011544{
11545 long limit;
11546
11547 errno = 0;
11548 limit = fpathconf(fd, name);
11549 if (limit == -1 && errno != 0)
11550 posix_error();
11551
11552 return limit;
11553}
11554#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011555
11556
11557#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100011558/*[clinic input]
11559os.pathconf -> long
11560 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
11561 name: path_confname
11562
11563Return the configuration limit name for the file or directory path.
11564
11565If there is no limit, return -1.
11566On some platforms, path may also be specified as an open file descriptor.
11567 If this functionality is unavailable, using it raises an exception.
11568[clinic start generated code]*/
11569
Larry Hastings2f936352014-08-05 14:04:04 +100011570static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011571os_pathconf_impl(PyObject *module, path_t *path, int name)
11572/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011573{
Victor Stinner8c62be82010-05-06 00:08:46 +000011574 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000011575
Victor Stinner8c62be82010-05-06 00:08:46 +000011576 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020011577#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100011578 if (path->fd != -1)
11579 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020011580 else
11581#endif
Larry Hastings2f936352014-08-05 14:04:04 +100011582 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000011583 if (limit == -1 && errno != 0) {
11584 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000011585 /* could be a path or name problem */
11586 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000011587 else
Larry Hastings2f936352014-08-05 14:04:04 +100011588 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000011589 }
Larry Hastings2f936352014-08-05 14:04:04 +100011590
11591 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000011592}
Larry Hastings2f936352014-08-05 14:04:04 +100011593#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011594
11595#ifdef HAVE_CONFSTR
11596static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000011597#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000011598 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000011599#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000011600#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011601 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000011602#endif
11603#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011604 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000011605#endif
Fred Draked86ed291999-12-15 15:34:33 +000011606#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011607 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000011608#endif
11609#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000011610 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000011611#endif
11612#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011613 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011614#endif
11615#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011616 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011617#endif
Fred Drakec9680921999-12-13 16:37:25 +000011618#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011619 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011620#endif
11621#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011622 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011623#endif
11624#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011625 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011626#endif
11627#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011628 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011629#endif
11630#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011631 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011632#endif
11633#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011634 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011635#endif
11636#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011637 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011638#endif
11639#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011640 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011641#endif
Fred Draked86ed291999-12-15 15:34:33 +000011642#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000011643 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000011644#endif
Fred Drakec9680921999-12-13 16:37:25 +000011645#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000011646 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000011647#endif
Fred Draked86ed291999-12-15 15:34:33 +000011648#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011649 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000011650#endif
11651#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011652 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000011653#endif
11654#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011655 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000011656#endif
11657#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011658 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000011659#endif
Fred Drakec9680921999-12-13 16:37:25 +000011660#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011661 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011662#endif
11663#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011664 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011665#endif
11666#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011667 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011668#endif
11669#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011670 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011671#endif
11672#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011673 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011674#endif
11675#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011676 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011677#endif
11678#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011679 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011680#endif
11681#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011682 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011683#endif
11684#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011685 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011686#endif
11687#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011688 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011689#endif
11690#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011691 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011692#endif
11693#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011694 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011695#endif
11696#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011697 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011698#endif
11699#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011700 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011701#endif
11702#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011703 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011704#endif
11705#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011706 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011707#endif
Fred Draked86ed291999-12-15 15:34:33 +000011708#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011709 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011710#endif
11711#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011712 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000011713#endif
11714#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000011715 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000011716#endif
11717#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011718 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011719#endif
11720#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011721 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011722#endif
11723#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000011724 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000011725#endif
11726#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011727 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000011728#endif
11729#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000011730 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000011731#endif
11732#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011733 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011734#endif
11735#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000011736 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000011737#endif
11738#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011739 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011740#endif
11741#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011742 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011743#endif
11744#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000011745 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000011746#endif
Fred Drakec9680921999-12-13 16:37:25 +000011747};
11748
11749static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011750conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011751{
11752 return conv_confname(arg, valuep, posix_constants_confstr,
11753 sizeof(posix_constants_confstr)
11754 / sizeof(struct constdef));
11755}
11756
Larry Hastings2f936352014-08-05 14:04:04 +100011757
11758/*[clinic input]
11759os.confstr
11760
11761 name: confstr_confname
11762 /
11763
11764Return a string-valued system configuration variable.
11765[clinic start generated code]*/
11766
Larry Hastings2f936352014-08-05 14:04:04 +100011767static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011768os_confstr_impl(PyObject *module, int name)
11769/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000011770{
11771 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000011772 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011773 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000011774
Victor Stinnercb043522010-09-10 23:49:04 +000011775 errno = 0;
11776 len = confstr(name, buffer, sizeof(buffer));
11777 if (len == 0) {
11778 if (errno) {
11779 posix_error();
11780 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000011781 }
11782 else {
Victor Stinnercb043522010-09-10 23:49:04 +000011783 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000011784 }
11785 }
Victor Stinnercb043522010-09-10 23:49:04 +000011786
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011787 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010011788 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000011789 char *buf = PyMem_Malloc(len);
11790 if (buf == NULL)
11791 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010011792 len2 = confstr(name, buf, len);
11793 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020011794 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000011795 PyMem_Free(buf);
11796 }
11797 else
11798 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000011799 return result;
11800}
Larry Hastings2f936352014-08-05 14:04:04 +100011801#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000011802
11803
11804#ifdef HAVE_SYSCONF
11805static struct constdef posix_constants_sysconf[] = {
11806#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011807 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000011808#endif
11809#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000011810 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000011811#endif
11812#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011813 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011814#endif
11815#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011816 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011817#endif
11818#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011819 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011820#endif
11821#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000011822 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000011823#endif
11824#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000011825 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000011826#endif
11827#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011828 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011829#endif
11830#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000011831 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000011832#endif
11833#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011834 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011835#endif
Fred Draked86ed291999-12-15 15:34:33 +000011836#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011837 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011838#endif
11839#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000011840 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000011841#endif
Fred Drakec9680921999-12-13 16:37:25 +000011842#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011843 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011844#endif
Fred Drakec9680921999-12-13 16:37:25 +000011845#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011846 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011847#endif
11848#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011849 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011850#endif
11851#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011852 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011853#endif
11854#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011855 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011856#endif
11857#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011858 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011859#endif
Fred Draked86ed291999-12-15 15:34:33 +000011860#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011861 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000011862#endif
Fred Drakec9680921999-12-13 16:37:25 +000011863#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011864 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011865#endif
11866#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011867 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011868#endif
11869#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011870 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011871#endif
11872#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011873 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011874#endif
11875#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011876 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011877#endif
Fred Draked86ed291999-12-15 15:34:33 +000011878#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000011879 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000011880#endif
Fred Drakec9680921999-12-13 16:37:25 +000011881#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011882 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011883#endif
11884#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011885 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011886#endif
11887#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011888 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011889#endif
11890#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011891 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011892#endif
11893#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011894 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011895#endif
11896#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011897 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011898#endif
11899#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011900 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011901#endif
11902#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011903 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011904#endif
11905#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011906 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011907#endif
11908#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011909 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011910#endif
11911#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011912 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011913#endif
11914#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011915 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011916#endif
11917#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011918 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011919#endif
11920#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011921 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011922#endif
11923#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011924 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011925#endif
11926#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011927 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011928#endif
11929#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011930 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011931#endif
11932#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011933 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011934#endif
11935#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011936 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011937#endif
11938#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011939 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011940#endif
11941#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011942 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011943#endif
11944#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011945 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011946#endif
11947#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011948 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011949#endif
Fred Draked86ed291999-12-15 15:34:33 +000011950#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011951 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011952#endif
Fred Drakec9680921999-12-13 16:37:25 +000011953#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011954 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011955#endif
11956#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011957 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011958#endif
11959#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011960 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011961#endif
Fred Draked86ed291999-12-15 15:34:33 +000011962#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011963 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011964#endif
Fred Drakec9680921999-12-13 16:37:25 +000011965#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011966 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011967#endif
Fred Draked86ed291999-12-15 15:34:33 +000011968#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011969 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011970#endif
11971#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011972 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011973#endif
Fred Drakec9680921999-12-13 16:37:25 +000011974#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011975 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011976#endif
11977#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011978 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011979#endif
11980#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011981 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011982#endif
11983#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011984 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011985#endif
Fred Draked86ed291999-12-15 15:34:33 +000011986#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011987 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011988#endif
Fred Drakec9680921999-12-13 16:37:25 +000011989#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011990 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011991#endif
11992#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011993 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011994#endif
11995#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011996 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011997#endif
11998#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011999 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000012000#endif
12001#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000012002 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000012003#endif
12004#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000012005 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000012006#endif
12007#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000012008 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000012009#endif
Fred Draked86ed291999-12-15 15:34:33 +000012010#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000012011 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000012012#endif
Fred Drakec9680921999-12-13 16:37:25 +000012013#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012014 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012015#endif
12016#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012017 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012018#endif
Fred Draked86ed291999-12-15 15:34:33 +000012019#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012020 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000012021#endif
Fred Drakec9680921999-12-13 16:37:25 +000012022#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012023 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012024#endif
12025#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012026 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000012027#endif
12028#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012029 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000012030#endif
12031#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012032 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000012033#endif
12034#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012035 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000012036#endif
12037#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012038 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000012039#endif
12040#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012041 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000012042#endif
12043#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000012044 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000012045#endif
12046#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000012047 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000012048#endif
Fred Draked86ed291999-12-15 15:34:33 +000012049#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000012050 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000012051#endif
12052#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000012053 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000012054#endif
Fred Drakec9680921999-12-13 16:37:25 +000012055#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000012056 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000012057#endif
12058#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012059 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012060#endif
12061#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000012062 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000012063#endif
12064#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000012065 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000012066#endif
Batuhan Taşkaya909f4a32020-04-05 03:40:49 +030012067#ifdef _SC_AIX_REALMEM
12068 {"SC_AIX_REALMEM", _SC_AIX_REALMEM},
12069#endif
Fred Drakec9680921999-12-13 16:37:25 +000012070#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012071 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012072#endif
12073#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000012074 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000012075#endif
12076#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000012077 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000012078#endif
12079#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000012080 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000012081#endif
12082#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000012083 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000012084#endif
12085#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000012086 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000012087#endif
12088#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000012089 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000012090#endif
12091#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000012092 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000012093#endif
12094#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000012095 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000012096#endif
12097#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000012098 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000012099#endif
12100#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000012101 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000012102#endif
12103#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000012104 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000012105#endif
12106#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000012107 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000012108#endif
12109#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000012110 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000012111#endif
12112#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000012113 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000012114#endif
12115#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000012116 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000012117#endif
12118#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012119 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012120#endif
12121#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012122 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012123#endif
12124#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000012125 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000012126#endif
12127#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012128 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012129#endif
12130#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012131 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012132#endif
12133#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000012134 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000012135#endif
12136#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000012137 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000012138#endif
12139#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012140 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012141#endif
12142#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012143 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012144#endif
12145#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000012146 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000012147#endif
12148#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012149 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012150#endif
12151#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012152 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012153#endif
12154#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012155 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012156#endif
12157#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012158 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012159#endif
12160#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012161 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012162#endif
Fred Draked86ed291999-12-15 15:34:33 +000012163#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000012164 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000012165#endif
Fred Drakec9680921999-12-13 16:37:25 +000012166#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000012167 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000012168#endif
12169#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012170 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012171#endif
12172#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000012173 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000012174#endif
12175#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012176 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012177#endif
12178#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000012179 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000012180#endif
12181#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000012182 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000012183#endif
12184#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000012185 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000012186#endif
12187#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000012188 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000012189#endif
12190#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000012191 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000012192#endif
12193#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012194 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012195#endif
12196#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000012197 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000012198#endif
12199#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012200 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000012201#endif
12202#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000012203 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000012204#endif
12205#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000012206 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000012207#endif
12208#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000012209 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000012210#endif
12211#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012212 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012213#endif
12214#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012215 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012216#endif
12217#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000012218 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000012219#endif
12220#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012221 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012222#endif
12223#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012224 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012225#endif
12226#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012227 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012228#endif
12229#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012230 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012231#endif
12232#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012233 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012234#endif
12235#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012236 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012237#endif
12238#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000012239 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000012240#endif
12241#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012242 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012243#endif
12244#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012245 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012246#endif
12247#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000012248 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000012249#endif
12250#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012251 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000012252#endif
12253#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000012254 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000012255#endif
12256#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000012257 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000012258#endif
12259#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000012260 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000012261#endif
12262#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000012263 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000012264#endif
12265#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000012266 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000012267#endif
12268#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000012269 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000012270#endif
12271#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000012272 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000012273#endif
12274#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000012275 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000012276#endif
12277#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000012278 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000012279#endif
12280#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000012281 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000012282#endif
12283#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000012284 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000012285#endif
12286#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000012287 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000012288#endif
12289#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000012290 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000012291#endif
12292#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000012293 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000012294#endif
12295#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000012296 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000012297#endif
12298#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000012299 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000012300#endif
12301};
12302
12303static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000012304conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000012305{
12306 return conv_confname(arg, valuep, posix_constants_sysconf,
12307 sizeof(posix_constants_sysconf)
12308 / sizeof(struct constdef));
12309}
12310
Larry Hastings2f936352014-08-05 14:04:04 +100012311
12312/*[clinic input]
12313os.sysconf -> long
12314 name: sysconf_confname
12315 /
12316
12317Return an integer-valued system configuration variable.
12318[clinic start generated code]*/
12319
Larry Hastings2f936352014-08-05 14:04:04 +100012320static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012321os_sysconf_impl(PyObject *module, int name)
12322/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012323{
12324 long value;
12325
12326 errno = 0;
12327 value = sysconf(name);
12328 if (value == -1 && errno != 0)
12329 posix_error();
12330 return value;
12331}
12332#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000012333
12334
Fred Drakebec628d1999-12-15 18:31:10 +000012335/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020012336 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000012337 * the exported dictionaries that are used to publish information about the
12338 * names available on the host platform.
12339 *
12340 * Sorting the table at runtime ensures that the table is properly ordered
12341 * when used, even for platforms we're not able to test on. It also makes
12342 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000012343 */
Fred Drakebec628d1999-12-15 18:31:10 +000012344
12345static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000012346cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000012347{
12348 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000012349 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000012350 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000012351 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000012352
12353 return strcmp(c1->name, c2->name);
12354}
12355
12356static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000012357setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012358 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000012359{
Fred Drakebec628d1999-12-15 18:31:10 +000012360 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000012361 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000012362
12363 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
12364 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000012365 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000012366 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012367
Barry Warsaw3155db32000-04-13 15:20:40 +000012368 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000012369 PyObject *o = PyLong_FromLong(table[i].value);
12370 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
12371 Py_XDECREF(o);
12372 Py_DECREF(d);
12373 return -1;
12374 }
12375 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000012376 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000012377 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000012378}
12379
Fred Drakebec628d1999-12-15 18:31:10 +000012380/* Return -1 on failure, 0 on success. */
12381static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000012382setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000012383{
12384#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000012385 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000012386 sizeof(posix_constants_pathconf)
12387 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000012388 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000012389 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012390#endif
12391#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000012392 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000012393 sizeof(posix_constants_confstr)
12394 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000012395 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000012396 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012397#endif
12398#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000012399 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000012400 sizeof(posix_constants_sysconf)
12401 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000012402 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000012403 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012404#endif
Fred Drakebec628d1999-12-15 18:31:10 +000012405 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000012406}
Fred Draked86ed291999-12-15 15:34:33 +000012407
12408
Larry Hastings2f936352014-08-05 14:04:04 +100012409/*[clinic input]
12410os.abort
12411
12412Abort the interpreter immediately.
12413
12414This function 'dumps core' or otherwise fails in the hardest way possible
12415on the hosting operating system. This function never returns.
12416[clinic start generated code]*/
12417
Larry Hastings2f936352014-08-05 14:04:04 +100012418static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012419os_abort_impl(PyObject *module)
12420/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012421{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012422 abort();
12423 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010012424#ifndef __clang__
12425 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
12426 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
12427 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012428 Py_FatalError("abort() called from Python code didn't abort!");
12429 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010012430#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012431}
Fred Drakebec628d1999-12-15 18:31:10 +000012432
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000012433#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080012434/* Grab ShellExecute dynamically from shell32 */
12435static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080012436static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
12437 LPCWSTR, INT);
12438static int
12439check_ShellExecute()
12440{
12441 HINSTANCE hShell32;
12442
12443 /* only recheck */
12444 if (-1 == has_ShellExecute) {
12445 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070012446 /* Security note: this call is not vulnerable to "DLL hijacking".
12447 SHELL32 is part of "KnownDLLs" and so Windows always load
12448 the system SHELL32.DLL, even if there is another SHELL32.DLL
12449 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080012450 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080012451 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080012452 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
12453 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070012454 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080012455 } else {
12456 has_ShellExecute = 0;
12457 }
Tony Roberts4860f012019-02-02 18:16:42 +010012458 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080012459 }
12460 return has_ShellExecute;
12461}
12462
12463
Steve Dowercc16be82016-09-08 10:35:16 -070012464/*[clinic input]
12465os.startfile
12466 filepath: path_t
12467 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000012468
Steve Dowercc16be82016-09-08 10:35:16 -070012469Start a file with its associated application.
12470
12471When "operation" is not specified or "open", this acts like
12472double-clicking the file in Explorer, or giving the file name as an
12473argument to the DOS "start" command: the file is opened with whatever
12474application (if any) its extension is associated.
12475When another "operation" is given, it specifies what should be done with
12476the file. A typical operation is "print".
12477
12478startfile returns as soon as the associated application is launched.
12479There is no option to wait for the application to close, and no way
12480to retrieve the application's exit status.
12481
12482The filepath is relative to the current directory. If you want to use
12483an absolute path, make sure the first character is not a slash ("/");
12484the underlying Win32 ShellExecute function doesn't work if it is.
12485[clinic start generated code]*/
12486
12487static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020012488os_startfile_impl(PyObject *module, path_t *filepath,
12489 const Py_UNICODE *operation)
Serhiy Storchaka279f4462019-09-14 12:24:05 +030012490/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070012491{
12492 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080012493
12494 if(!check_ShellExecute()) {
12495 /* If the OS doesn't have ShellExecute, return a
12496 NotImplementedError. */
12497 return PyErr_Format(PyExc_NotImplementedError,
12498 "startfile not available on this platform");
12499 }
12500
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012501 if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -080012502 return NULL;
12503 }
12504
Victor Stinner8c62be82010-05-06 00:08:46 +000012505 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070012506 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080012507 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000012508 Py_END_ALLOW_THREADS
12509
Victor Stinner8c62be82010-05-06 00:08:46 +000012510 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070012511 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020012512 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000012513 }
Steve Dowercc16be82016-09-08 10:35:16 -070012514 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000012515}
Larry Hastings2f936352014-08-05 14:04:04 +100012516#endif /* MS_WINDOWS */
12517
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012518
Martin v. Löwis438b5342002-12-27 10:16:42 +000012519#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100012520/*[clinic input]
12521os.getloadavg
12522
12523Return average recent system load information.
12524
12525Return the number of processes in the system run queue averaged over
12526the last 1, 5, and 15 minutes as a tuple of three floats.
12527Raises OSError if the load average was unobtainable.
12528[clinic start generated code]*/
12529
Larry Hastings2f936352014-08-05 14:04:04 +100012530static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012531os_getloadavg_impl(PyObject *module)
12532/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000012533{
12534 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000012535 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000012536 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
12537 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000012538 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000012539 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000012540}
Larry Hastings2f936352014-08-05 14:04:04 +100012541#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000012542
Larry Hastings2f936352014-08-05 14:04:04 +100012543
12544/*[clinic input]
12545os.device_encoding
12546 fd: int
12547
12548Return a string describing the encoding of a terminal's file descriptor.
12549
12550The file descriptor must be attached to a terminal.
12551If the device is not a terminal, return None.
12552[clinic start generated code]*/
12553
Larry Hastings2f936352014-08-05 14:04:04 +100012554static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012555os_device_encoding_impl(PyObject *module, int fd)
12556/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012557{
Brett Cannonefb00c02012-02-29 18:31:31 -050012558 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000012559}
12560
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012561
Larry Hastings2f936352014-08-05 14:04:04 +100012562#ifdef HAVE_SETRESUID
12563/*[clinic input]
12564os.setresuid
12565
12566 ruid: uid_t
12567 euid: uid_t
12568 suid: uid_t
12569 /
12570
12571Set the current process's real, effective, and saved user ids.
12572[clinic start generated code]*/
12573
Larry Hastings2f936352014-08-05 14:04:04 +100012574static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012575os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
12576/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012577{
Victor Stinner8c62be82010-05-06 00:08:46 +000012578 if (setresuid(ruid, euid, suid) < 0)
12579 return posix_error();
12580 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012581}
Larry Hastings2f936352014-08-05 14:04:04 +100012582#endif /* HAVE_SETRESUID */
12583
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012584
12585#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100012586/*[clinic input]
12587os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012588
Larry Hastings2f936352014-08-05 14:04:04 +100012589 rgid: gid_t
12590 egid: gid_t
12591 sgid: gid_t
12592 /
12593
12594Set the current process's real, effective, and saved group ids.
12595[clinic start generated code]*/
12596
Larry Hastings2f936352014-08-05 14:04:04 +100012597static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012598os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
12599/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012600{
Victor Stinner8c62be82010-05-06 00:08:46 +000012601 if (setresgid(rgid, egid, sgid) < 0)
12602 return posix_error();
12603 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012604}
Larry Hastings2f936352014-08-05 14:04:04 +100012605#endif /* HAVE_SETRESGID */
12606
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012607
12608#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100012609/*[clinic input]
12610os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012611
Larry Hastings2f936352014-08-05 14:04:04 +100012612Return a tuple of the current process's real, effective, and saved user ids.
12613[clinic start generated code]*/
12614
Larry Hastings2f936352014-08-05 14:04:04 +100012615static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012616os_getresuid_impl(PyObject *module)
12617/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012618{
Victor Stinner8c62be82010-05-06 00:08:46 +000012619 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000012620 if (getresuid(&ruid, &euid, &suid) < 0)
12621 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020012622 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
12623 _PyLong_FromUid(euid),
12624 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012625}
Larry Hastings2f936352014-08-05 14:04:04 +100012626#endif /* HAVE_GETRESUID */
12627
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012628
12629#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100012630/*[clinic input]
12631os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012632
Larry Hastings2f936352014-08-05 14:04:04 +100012633Return a tuple of the current process's real, effective, and saved group ids.
12634[clinic start generated code]*/
12635
Larry Hastings2f936352014-08-05 14:04:04 +100012636static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012637os_getresgid_impl(PyObject *module)
12638/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012639{
12640 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000012641 if (getresgid(&rgid, &egid, &sgid) < 0)
12642 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020012643 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
12644 _PyLong_FromGid(egid),
12645 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012646}
Larry Hastings2f936352014-08-05 14:04:04 +100012647#endif /* HAVE_GETRESGID */
12648
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012649
Benjamin Peterson9428d532011-09-14 11:45:52 -040012650#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100012651/*[clinic input]
12652os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040012653
Larry Hastings2f936352014-08-05 14:04:04 +100012654 path: path_t(allow_fd=True)
12655 attribute: path_t
12656 *
12657 follow_symlinks: bool = True
12658
12659Return the value of extended attribute attribute on path.
12660
BNMetricsb9427072018-11-02 15:20:19 +000012661path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012662If follow_symlinks is False, and the last element of the path is a symbolic
12663 link, getxattr will examine the symbolic link itself instead of the file
12664 the link points to.
12665
12666[clinic start generated code]*/
12667
Larry Hastings2f936352014-08-05 14:04:04 +100012668static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012669os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012670 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012671/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012672{
12673 Py_ssize_t i;
12674 PyObject *buffer = NULL;
12675
12676 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
12677 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012678
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012679 if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) {
12680 return NULL;
12681 }
12682
Larry Hastings9cf065c2012-06-22 16:30:09 -070012683 for (i = 0; ; i++) {
12684 void *ptr;
12685 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012686 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070012687 Py_ssize_t buffer_size = buffer_sizes[i];
12688 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100012689 path_error(path);
12690 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012691 }
12692 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
12693 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100012694 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012695 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012696
Larry Hastings9cf065c2012-06-22 16:30:09 -070012697 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012698 if (path->fd >= 0)
12699 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012700 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012701 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012702 else
Larry Hastings2f936352014-08-05 14:04:04 +100012703 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012704 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012705
Larry Hastings9cf065c2012-06-22 16:30:09 -070012706 if (result < 0) {
12707 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012708 if (errno == ERANGE)
12709 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100012710 path_error(path);
12711 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012712 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012713
Larry Hastings9cf065c2012-06-22 16:30:09 -070012714 if (result != buffer_size) {
12715 /* Can only shrink. */
12716 _PyBytes_Resize(&buffer, result);
12717 }
12718 break;
12719 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012720
Larry Hastings9cf065c2012-06-22 16:30:09 -070012721 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012722}
12723
Larry Hastings2f936352014-08-05 14:04:04 +100012724
12725/*[clinic input]
12726os.setxattr
12727
12728 path: path_t(allow_fd=True)
12729 attribute: path_t
12730 value: Py_buffer
12731 flags: int = 0
12732 *
12733 follow_symlinks: bool = True
12734
12735Set extended attribute attribute on path to value.
12736
BNMetricsb9427072018-11-02 15:20:19 +000012737path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012738If follow_symlinks is False, and the last element of the path is a symbolic
12739 link, setxattr will modify the symbolic link itself instead of the file
12740 the link points to.
12741
12742[clinic start generated code]*/
12743
Benjamin Peterson799bd802011-08-31 22:15:17 -040012744static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012745os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012746 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012747/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040012748{
Larry Hastings2f936352014-08-05 14:04:04 +100012749 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012750
Larry Hastings2f936352014-08-05 14:04:04 +100012751 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040012752 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012753
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012754 if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object,
12755 value->buf, value->len, flags) < 0) {
12756 return NULL;
12757 }
12758
Benjamin Peterson799bd802011-08-31 22:15:17 -040012759 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012760 if (path->fd > -1)
12761 result = fsetxattr(path->fd, attribute->narrow,
12762 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012763 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012764 result = setxattr(path->narrow, attribute->narrow,
12765 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012766 else
Larry Hastings2f936352014-08-05 14:04:04 +100012767 result = lsetxattr(path->narrow, attribute->narrow,
12768 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012769 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012770
Larry Hastings9cf065c2012-06-22 16:30:09 -070012771 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100012772 path_error(path);
12773 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012774 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012775
Larry Hastings2f936352014-08-05 14:04:04 +100012776 Py_RETURN_NONE;
12777}
12778
12779
12780/*[clinic input]
12781os.removexattr
12782
12783 path: path_t(allow_fd=True)
12784 attribute: path_t
12785 *
12786 follow_symlinks: bool = True
12787
12788Remove extended attribute attribute on path.
12789
BNMetricsb9427072018-11-02 15:20:19 +000012790path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012791If follow_symlinks is False, and the last element of the path is a symbolic
12792 link, removexattr will modify the symbolic link itself instead of the file
12793 the link points to.
12794
12795[clinic start generated code]*/
12796
Larry Hastings2f936352014-08-05 14:04:04 +100012797static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012798os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012799 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012800/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012801{
12802 ssize_t result;
12803
12804 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
12805 return NULL;
12806
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012807 if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) {
12808 return NULL;
12809 }
12810
Larry Hastings2f936352014-08-05 14:04:04 +100012811 Py_BEGIN_ALLOW_THREADS;
12812 if (path->fd > -1)
12813 result = fremovexattr(path->fd, attribute->narrow);
12814 else if (follow_symlinks)
12815 result = removexattr(path->narrow, attribute->narrow);
12816 else
12817 result = lremovexattr(path->narrow, attribute->narrow);
12818 Py_END_ALLOW_THREADS;
12819
12820 if (result) {
12821 return path_error(path);
12822 }
12823
12824 Py_RETURN_NONE;
12825}
12826
12827
12828/*[clinic input]
12829os.listxattr
12830
12831 path: path_t(allow_fd=True, nullable=True) = None
12832 *
12833 follow_symlinks: bool = True
12834
12835Return a list of extended attributes on path.
12836
BNMetricsb9427072018-11-02 15:20:19 +000012837path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012838if path is None, listxattr will examine the current directory.
12839If follow_symlinks is False, and the last element of the path is a symbolic
12840 link, listxattr will examine the symbolic link itself instead of the file
12841 the link points to.
12842[clinic start generated code]*/
12843
Larry Hastings2f936352014-08-05 14:04:04 +100012844static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012845os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012846/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012847{
Larry Hastings9cf065c2012-06-22 16:30:09 -070012848 Py_ssize_t i;
12849 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100012850 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012851 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012852
Larry Hastings2f936352014-08-05 14:04:04 +100012853 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070012854 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012855
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012856 if (PySys_Audit("os.listxattr", "(O)",
12857 path->object ? path->object : Py_None) < 0) {
12858 return NULL;
12859 }
12860
Larry Hastings2f936352014-08-05 14:04:04 +100012861 name = path->narrow ? path->narrow : ".";
12862
Larry Hastings9cf065c2012-06-22 16:30:09 -070012863 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012864 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012865 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012866 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070012867 Py_ssize_t buffer_size = buffer_sizes[i];
12868 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020012869 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100012870 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012871 break;
12872 }
Victor Stinner00d7abd2020-12-01 09:56:42 +010012873 buffer = PyMem_Malloc(buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012874 if (!buffer) {
12875 PyErr_NoMemory();
12876 break;
12877 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012878
Larry Hastings9cf065c2012-06-22 16:30:09 -070012879 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012880 if (path->fd > -1)
12881 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012882 else if (follow_symlinks)
12883 length = listxattr(name, buffer, buffer_size);
12884 else
12885 length = llistxattr(name, buffer, buffer_size);
12886 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012887
Larry Hastings9cf065c2012-06-22 16:30:09 -070012888 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020012889 if (errno == ERANGE) {
Victor Stinner00d7abd2020-12-01 09:56:42 +010012890 PyMem_Free(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050012891 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012892 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020012893 }
Larry Hastings2f936352014-08-05 14:04:04 +100012894 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012895 break;
12896 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012897
Larry Hastings9cf065c2012-06-22 16:30:09 -070012898 result = PyList_New(0);
12899 if (!result) {
12900 goto exit;
12901 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012902
Larry Hastings9cf065c2012-06-22 16:30:09 -070012903 end = buffer + length;
12904 for (trace = start = buffer; trace != end; trace++) {
12905 if (!*trace) {
12906 int error;
12907 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12908 trace - start);
12909 if (!attribute) {
12910 Py_DECREF(result);
12911 result = NULL;
12912 goto exit;
12913 }
12914 error = PyList_Append(result, attribute);
12915 Py_DECREF(attribute);
12916 if (error) {
12917 Py_DECREF(result);
12918 result = NULL;
12919 goto exit;
12920 }
12921 start = trace + 1;
12922 }
12923 }
12924 break;
12925 }
12926exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012927 if (buffer)
Victor Stinner00d7abd2020-12-01 09:56:42 +010012928 PyMem_Free(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012929 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012930}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012931#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012932
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012933
Larry Hastings2f936352014-08-05 14:04:04 +100012934/*[clinic input]
12935os.urandom
12936
12937 size: Py_ssize_t
12938 /
12939
12940Return a bytes object containing random bytes suitable for cryptographic use.
12941[clinic start generated code]*/
12942
Larry Hastings2f936352014-08-05 14:04:04 +100012943static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012944os_urandom_impl(PyObject *module, Py_ssize_t size)
12945/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012946{
12947 PyObject *bytes;
12948 int result;
12949
Georg Brandl2fb477c2012-02-21 00:33:36 +010012950 if (size < 0)
12951 return PyErr_Format(PyExc_ValueError,
12952 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012953 bytes = PyBytes_FromStringAndSize(NULL, size);
12954 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012955 return NULL;
12956
Victor Stinnere66987e2016-09-06 16:33:52 -070012957 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012958 if (result == -1) {
12959 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012960 return NULL;
12961 }
Larry Hastings2f936352014-08-05 14:04:04 +100012962 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012963}
12964
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012965#ifdef HAVE_MEMFD_CREATE
12966/*[clinic input]
12967os.memfd_create
12968
12969 name: FSConverter
12970 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12971
12972[clinic start generated code]*/
12973
12974static PyObject *
12975os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12976/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12977{
12978 int fd;
12979 const char *bytes = PyBytes_AS_STRING(name);
12980 Py_BEGIN_ALLOW_THREADS
12981 fd = memfd_create(bytes, flags);
12982 Py_END_ALLOW_THREADS
12983 if (fd == -1) {
12984 return PyErr_SetFromErrno(PyExc_OSError);
12985 }
12986 return PyLong_FromLong(fd);
12987}
12988#endif
12989
Christian Heimescd9fed62020-11-13 19:48:52 +010012990#ifdef HAVE_EVENTFD
12991/*[clinic input]
12992os.eventfd
12993
12994 initval: unsigned_int
12995 flags: int(c_default="EFD_CLOEXEC") = EFD_CLOEXEC
12996
12997Creates and returns an event notification file descriptor.
12998[clinic start generated code]*/
12999
13000static PyObject *
13001os_eventfd_impl(PyObject *module, unsigned int initval, int flags)
13002/*[clinic end generated code: output=ce9c9bbd1446f2de input=66203e3c50c4028b]*/
13003
13004{
13005 /* initval is limited to uint32_t, internal counter is uint64_t */
13006 int fd;
13007 Py_BEGIN_ALLOW_THREADS
13008 fd = eventfd(initval, flags);
13009 Py_END_ALLOW_THREADS
13010 if (fd == -1) {
13011 return PyErr_SetFromErrno(PyExc_OSError);
13012 }
13013 return PyLong_FromLong(fd);
13014}
13015
13016/*[clinic input]
13017os.eventfd_read
13018
13019 fd: fildes
13020
13021Read eventfd value
13022[clinic start generated code]*/
13023
13024static PyObject *
13025os_eventfd_read_impl(PyObject *module, int fd)
13026/*[clinic end generated code: output=8f2c7b59a3521fd1 input=110f8b57fa596afe]*/
13027{
13028 eventfd_t value;
13029 int result;
13030 Py_BEGIN_ALLOW_THREADS
13031 result = eventfd_read(fd, &value);
13032 Py_END_ALLOW_THREADS
13033 if (result == -1) {
13034 return PyErr_SetFromErrno(PyExc_OSError);
13035 }
13036 return PyLong_FromUnsignedLongLong(value);
13037}
13038
13039/*[clinic input]
13040os.eventfd_write
13041
13042 fd: fildes
13043 value: unsigned_long_long
13044
13045Write eventfd value.
13046[clinic start generated code]*/
13047
13048static PyObject *
13049os_eventfd_write_impl(PyObject *module, int fd, unsigned long long value)
13050/*[clinic end generated code: output=bebd9040bbf987f5 input=156de8555be5a949]*/
13051{
13052 int result;
13053 Py_BEGIN_ALLOW_THREADS
13054 result = eventfd_write(fd, value);
13055 Py_END_ALLOW_THREADS
13056 if (result == -1) {
13057 return PyErr_SetFromErrno(PyExc_OSError);
13058 }
13059 Py_RETURN_NONE;
13060}
13061#endif /* HAVE_EVENTFD */
13062
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013063/* Terminal size querying */
13064
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013065PyDoc_STRVAR(TerminalSize_docstring,
13066 "A tuple of (columns, lines) for holding terminal window size");
13067
13068static PyStructSequence_Field TerminalSize_fields[] = {
13069 {"columns", "width of the terminal window in characters"},
13070 {"lines", "height of the terminal window in characters"},
13071 {NULL, NULL}
13072};
13073
13074static PyStructSequence_Desc TerminalSize_desc = {
13075 "os.terminal_size",
13076 TerminalSize_docstring,
13077 TerminalSize_fields,
13078 2,
13079};
13080
13081#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Serhiy Storchaka2b560312020-04-18 19:14:10 +030013082/*[clinic input]
13083os.get_terminal_size
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013084
Serhiy Storchaka2b560312020-04-18 19:14:10 +030013085 fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1
13086 /
13087
13088Return the size of the terminal window as (columns, lines).
13089
13090The optional argument fd (default standard output) specifies
13091which file descriptor should be queried.
13092
13093If the file descriptor is not connected to a terminal, an OSError
13094is thrown.
13095
13096This function will only be defined if an implementation is
13097available for this system.
13098
13099shutil.get_terminal_size is the high-level function which should
13100normally be used, os.get_terminal_size is the low-level implementation.
13101[clinic start generated code]*/
13102
13103static PyObject *
13104os_get_terminal_size_impl(PyObject *module, int fd)
13105/*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013106{
13107 int columns, lines;
13108 PyObject *termsize;
13109
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013110 /* Under some conditions stdout may not be connected and
13111 * fileno(stdout) may point to an invalid file descriptor. For example
13112 * GUI apps don't have valid standard streams by default.
13113 *
13114 * If this happens, and the optional fd argument is not present,
13115 * the ioctl below will fail returning EBADF. This is what we want.
13116 */
13117
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013118#ifdef TERMSIZE_USE_IOCTL
13119 {
13120 struct winsize w;
13121 if (ioctl(fd, TIOCGWINSZ, &w))
13122 return PyErr_SetFromErrno(PyExc_OSError);
13123 columns = w.ws_col;
13124 lines = w.ws_row;
13125 }
13126#endif /* TERMSIZE_USE_IOCTL */
13127
13128#ifdef TERMSIZE_USE_CONIO
13129 {
13130 DWORD nhandle;
13131 HANDLE handle;
13132 CONSOLE_SCREEN_BUFFER_INFO csbi;
13133 switch (fd) {
13134 case 0: nhandle = STD_INPUT_HANDLE;
13135 break;
13136 case 1: nhandle = STD_OUTPUT_HANDLE;
13137 break;
13138 case 2: nhandle = STD_ERROR_HANDLE;
13139 break;
13140 default:
13141 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
13142 }
13143 handle = GetStdHandle(nhandle);
13144 if (handle == NULL)
13145 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
13146 if (handle == INVALID_HANDLE_VALUE)
13147 return PyErr_SetFromWindowsErr(0);
13148
13149 if (!GetConsoleScreenBufferInfo(handle, &csbi))
13150 return PyErr_SetFromWindowsErr(0);
13151
13152 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
13153 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
13154 }
13155#endif /* TERMSIZE_USE_CONIO */
13156
Serhiy Storchaka2b560312020-04-18 19:14:10 +030013157 PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080013158 termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013159 if (termsize == NULL)
13160 return NULL;
13161 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
13162 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
13163 if (PyErr_Occurred()) {
13164 Py_DECREF(termsize);
13165 return NULL;
13166 }
13167 return termsize;
13168}
13169#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
13170
Larry Hastings2f936352014-08-05 14:04:04 +100013171
13172/*[clinic input]
13173os.cpu_count
13174
Charles-François Natali80d62e62015-08-13 20:37:08 +010013175Return the number of CPUs in the system; return None if indeterminable.
13176
13177This number is not equivalent to the number of CPUs the current process can
13178use. The number of usable CPUs can be obtained with
13179``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100013180[clinic start generated code]*/
13181
Larry Hastings2f936352014-08-05 14:04:04 +100013182static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030013183os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013184/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020013185{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020013186 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020013187#ifdef MS_WINDOWS
Steve Doweraa929272019-09-11 16:15:39 +010013188 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020013189#elif defined(__hpux)
13190 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
13191#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
13192 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
pxinwr3405e052020-08-07 13:21:52 +080013193#elif defined(__VXWORKS__)
13194 ncpu = _Py_popcount32(vxCpuEnabledGet());
Charles-Francois Natali44feda32013-05-20 14:40:46 +020013195#elif defined(__DragonFly__) || \
13196 defined(__OpenBSD__) || \
13197 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020013198 defined(__NetBSD__) || \
13199 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020013200 int mib[2];
13201 size_t len = sizeof(ncpu);
13202 mib[0] = CTL_HW;
13203 mib[1] = HW_NCPU;
13204 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
13205 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020013206#endif
13207 if (ncpu >= 1)
13208 return PyLong_FromLong(ncpu);
13209 else
13210 Py_RETURN_NONE;
13211}
13212
Victor Stinnerdaf45552013-08-28 00:53:59 +020013213
Larry Hastings2f936352014-08-05 14:04:04 +100013214/*[clinic input]
13215os.get_inheritable -> bool
13216
13217 fd: int
13218 /
13219
13220Get the close-on-exe flag of the specified file descriptor.
13221[clinic start generated code]*/
13222
Larry Hastings2f936352014-08-05 14:04:04 +100013223static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030013224os_get_inheritable_impl(PyObject *module, int fd)
13225/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100013226{
Steve Dower8fc89802015-04-12 00:26:27 -040013227 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040013228 _Py_BEGIN_SUPPRESS_IPH
13229 return_value = _Py_get_inheritable(fd);
13230 _Py_END_SUPPRESS_IPH
13231 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100013232}
13233
13234
13235/*[clinic input]
13236os.set_inheritable
13237 fd: int
13238 inheritable: int
13239 /
13240
13241Set the inheritable flag of the specified file descriptor.
13242[clinic start generated code]*/
13243
Larry Hastings2f936352014-08-05 14:04:04 +100013244static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030013245os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
13246/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020013247{
Steve Dower8fc89802015-04-12 00:26:27 -040013248 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013249
Steve Dower8fc89802015-04-12 00:26:27 -040013250 _Py_BEGIN_SUPPRESS_IPH
13251 result = _Py_set_inheritable(fd, inheritable, NULL);
13252 _Py_END_SUPPRESS_IPH
13253 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020013254 return NULL;
13255 Py_RETURN_NONE;
13256}
13257
13258
13259#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100013260/*[clinic input]
13261os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070013262 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100013263 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020013264
Larry Hastings2f936352014-08-05 14:04:04 +100013265Get the close-on-exe flag of the specified file descriptor.
13266[clinic start generated code]*/
13267
Larry Hastings2f936352014-08-05 14:04:04 +100013268static int
Benjamin Petersonca470632016-09-06 13:47:26 -070013269os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070013270/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100013271{
13272 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013273
13274 if (!GetHandleInformation((HANDLE)handle, &flags)) {
13275 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100013276 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013277 }
13278
Larry Hastings2f936352014-08-05 14:04:04 +100013279 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013280}
13281
Victor Stinnerdaf45552013-08-28 00:53:59 +020013282
Larry Hastings2f936352014-08-05 14:04:04 +100013283/*[clinic input]
13284os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070013285 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100013286 inheritable: bool
13287 /
13288
13289Set the inheritable flag of the specified handle.
13290[clinic start generated code]*/
13291
Larry Hastings2f936352014-08-05 14:04:04 +100013292static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070013293os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040013294 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070013295/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100013296{
13297 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013298 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
13299 PyErr_SetFromWindowsErr(0);
13300 return NULL;
13301 }
13302 Py_RETURN_NONE;
13303}
Larry Hastings2f936352014-08-05 14:04:04 +100013304#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013305
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013306#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013307/*[clinic input]
13308os.get_blocking -> bool
13309 fd: int
13310 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013311
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013312Get the blocking mode of the file descriptor.
13313
13314Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
13315[clinic start generated code]*/
13316
13317static int
13318os_get_blocking_impl(PyObject *module, int fd)
13319/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013320{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013321 int blocking;
13322
Steve Dower8fc89802015-04-12 00:26:27 -040013323 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013324 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040013325 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013326 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013327}
13328
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013329/*[clinic input]
13330os.set_blocking
13331 fd: int
13332 blocking: bool(accept={int})
13333 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013334
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013335Set the blocking mode of the specified file descriptor.
13336
13337Set the O_NONBLOCK flag if blocking is False,
13338clear the O_NONBLOCK flag otherwise.
13339[clinic start generated code]*/
13340
13341static PyObject *
13342os_set_blocking_impl(PyObject *module, int fd, int blocking)
13343/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013344{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013345 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013346
Steve Dower8fc89802015-04-12 00:26:27 -040013347 _Py_BEGIN_SUPPRESS_IPH
13348 result = _Py_set_blocking(fd, blocking);
13349 _Py_END_SUPPRESS_IPH
13350 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013351 return NULL;
13352 Py_RETURN_NONE;
13353}
13354#endif /* !MS_WINDOWS */
13355
13356
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013357/*[clinic input]
Eddie Elizondob3966632019-11-05 07:16:14 -080013358class os.DirEntry "DirEntry *" "DirEntryType"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013359[clinic start generated code]*/
Eddie Elizondob3966632019-11-05 07:16:14 -080013360/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013361
13362typedef struct {
13363 PyObject_HEAD
13364 PyObject *name;
13365 PyObject *path;
13366 PyObject *stat;
13367 PyObject *lstat;
13368#ifdef MS_WINDOWS
13369 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010013370 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010013371 int got_file_index;
13372#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010013373#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013374 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013375#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013376 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013377 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010013378#endif
13379} DirEntry;
13380
Eddie Elizondob3966632019-11-05 07:16:14 -080013381static PyObject *
13382_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
13383{
13384 PyErr_Format(PyExc_TypeError,
13385 "cannot create '%.100s' instances", _PyType_Name(type));
13386 return NULL;
13387}
13388
Victor Stinner6036e442015-03-08 01:58:04 +010013389static void
13390DirEntry_dealloc(DirEntry *entry)
13391{
Eddie Elizondob3966632019-11-05 07:16:14 -080013392 PyTypeObject *tp = Py_TYPE(entry);
Victor Stinner6036e442015-03-08 01:58:04 +010013393 Py_XDECREF(entry->name);
13394 Py_XDECREF(entry->path);
13395 Py_XDECREF(entry->stat);
13396 Py_XDECREF(entry->lstat);
Eddie Elizondob3966632019-11-05 07:16:14 -080013397 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
13398 free_func(entry);
13399 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010013400}
13401
13402/* Forward reference */
13403static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013404DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self,
13405 int follow_symlinks, unsigned short mode_bits);
Victor Stinner6036e442015-03-08 01:58:04 +010013406
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013407/*[clinic input]
13408os.DirEntry.is_symlink -> bool
Victor Stinner97f33c32020-05-14 18:05:58 +020013409 defining_class: defining_class
13410 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013411
13412Return True if the entry is a symbolic link; cached per entry.
13413[clinic start generated code]*/
13414
Victor Stinner6036e442015-03-08 01:58:04 +010013415static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013416os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class)
13417/*[clinic end generated code: output=293096d589b6d47c input=e9acc5ee4d511113]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013418{
13419#ifdef MS_WINDOWS
13420 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010013421#elif defined(HAVE_DIRENT_D_TYPE)
13422 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013423 if (self->d_type != DT_UNKNOWN)
13424 return self->d_type == DT_LNK;
13425 else
Victor Stinner97f33c32020-05-14 18:05:58 +020013426 return DirEntry_test_mode(defining_class, self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010013427#else
13428 /* POSIX without d_type */
Victor Stinner97f33c32020-05-14 18:05:58 +020013429 return DirEntry_test_mode(defining_class, self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010013430#endif
13431}
13432
13433static PyObject *
Victor Stinner97f33c32020-05-14 18:05:58 +020013434DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks)
Victor Stinner6036e442015-03-08 01:58:04 +010013435{
13436 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013437 STRUCT_STAT st;
13438 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010013439
13440#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013441 if (!PyUnicode_FSDecoder(self->path, &ub))
13442 return NULL;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013443#if USE_UNICODE_WCHAR_CACHE
13444_Py_COMP_DIAG_PUSH
13445_Py_COMP_DIAG_IGNORE_DEPR_DECLS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013446 const wchar_t *path = PyUnicode_AsUnicode(ub);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013447_Py_COMP_DIAG_POP
13448#else /* USE_UNICODE_WCHAR_CACHE */
13449 wchar_t *path = PyUnicode_AsWideCharString(ub, NULL);
13450 Py_DECREF(ub);
13451#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner6036e442015-03-08 01:58:04 +010013452#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013453 if (!PyUnicode_FSConverter(self->path, &ub))
13454 return NULL;
13455 const char *path = PyBytes_AS_STRING(ub);
13456 if (self->dir_fd != DEFAULT_DIR_FD) {
13457#ifdef HAVE_FSTATAT
Ronald Oussoren41761932020-11-08 10:05:27 +010013458 if (HAVE_FSTATAT_RUNTIME) {
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013459 result = fstatat(self->dir_fd, path, &st,
13460 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
Ronald Oussoren41761932020-11-08 10:05:27 +010013461 } else
13462
13463#endif /* HAVE_FSTATAT */
13464 {
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013465 Py_DECREF(ub);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013466 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
13467 return NULL;
Ronald Oussoren41761932020-11-08 10:05:27 +010013468 }
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013469 }
13470 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013471#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013472 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013473 if (follow_symlinks)
13474 result = STAT(path, &st);
13475 else
13476 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013477 }
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013478#if defined(MS_WINDOWS) && !USE_UNICODE_WCHAR_CACHE
13479 PyMem_Free(path);
13480#else /* USE_UNICODE_WCHAR_CACHE */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013481 Py_DECREF(ub);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013482#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner6036e442015-03-08 01:58:04 +010013483
13484 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013485 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013486
Victor Stinner97f33c32020-05-14 18:05:58 +020013487 return _pystat_fromstructstat(module, &st);
Victor Stinner6036e442015-03-08 01:58:04 +010013488}
13489
13490static PyObject *
Victor Stinner97f33c32020-05-14 18:05:58 +020013491DirEntry_get_lstat(PyTypeObject *defining_class, DirEntry *self)
Victor Stinner6036e442015-03-08 01:58:04 +010013492{
13493 if (!self->lstat) {
Victor Stinner97f33c32020-05-14 18:05:58 +020013494 PyObject *module = PyType_GetModule(defining_class);
Victor Stinner6036e442015-03-08 01:58:04 +010013495#ifdef MS_WINDOWS
Victor Stinner97f33c32020-05-14 18:05:58 +020013496 self->lstat = _pystat_fromstructstat(module, &self->win32_lstat);
Victor Stinner6036e442015-03-08 01:58:04 +010013497#else /* POSIX */
Victor Stinner97f33c32020-05-14 18:05:58 +020013498 self->lstat = DirEntry_fetch_stat(module, self, 0);
Victor Stinner6036e442015-03-08 01:58:04 +010013499#endif
13500 }
13501 Py_XINCREF(self->lstat);
13502 return self->lstat;
13503}
13504
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013505/*[clinic input]
13506os.DirEntry.stat
Victor Stinner97f33c32020-05-14 18:05:58 +020013507 defining_class: defining_class
13508 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013509 *
13510 follow_symlinks: bool = True
13511
13512Return stat_result object for the entry; cached per entry.
13513[clinic start generated code]*/
13514
Victor Stinner6036e442015-03-08 01:58:04 +010013515static PyObject *
Victor Stinner97f33c32020-05-14 18:05:58 +020013516os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class,
13517 int follow_symlinks)
13518/*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013519{
Victor Stinner97f33c32020-05-14 18:05:58 +020013520 if (!follow_symlinks) {
13521 return DirEntry_get_lstat(defining_class, self);
13522 }
Victor Stinner6036e442015-03-08 01:58:04 +010013523
13524 if (!self->stat) {
Victor Stinner97f33c32020-05-14 18:05:58 +020013525 int result = os_DirEntry_is_symlink_impl(self, defining_class);
13526 if (result == -1) {
Victor Stinner6036e442015-03-08 01:58:04 +010013527 return NULL;
Victor Stinner97f33c32020-05-14 18:05:58 +020013528 }
13529 if (result) {
13530 PyObject *module = PyType_GetModule(defining_class);
13531 self->stat = DirEntry_fetch_stat(module, self, 1);
13532 }
13533 else {
13534 self->stat = DirEntry_get_lstat(defining_class, self);
13535 }
Victor Stinner6036e442015-03-08 01:58:04 +010013536 }
13537
13538 Py_XINCREF(self->stat);
13539 return self->stat;
13540}
13541
Victor Stinner6036e442015-03-08 01:58:04 +010013542/* Set exception and return -1 on error, 0 for False, 1 for True */
13543static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013544DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self,
13545 int follow_symlinks, unsigned short mode_bits)
Victor Stinner6036e442015-03-08 01:58:04 +010013546{
13547 PyObject *stat = NULL;
13548 PyObject *st_mode = NULL;
13549 long mode;
13550 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010013551#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013552 int is_symlink;
13553 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010013554#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013555#ifdef MS_WINDOWS
13556 unsigned long dir_bits;
13557#endif
13558
13559#ifdef MS_WINDOWS
13560 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
13561 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010013562#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013563 is_symlink = self->d_type == DT_LNK;
13564 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
13565#endif
13566
Victor Stinner35a97c02015-03-08 02:59:09 +010013567#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013568 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010013569#endif
Victor Stinner97f33c32020-05-14 18:05:58 +020013570 stat = os_DirEntry_stat_impl(self, defining_class, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010013571 if (!stat) {
13572 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
13573 /* If file doesn't exist (anymore), then return False
13574 (i.e., say it's not a file/directory) */
13575 PyErr_Clear();
13576 return 0;
13577 }
13578 goto error;
13579 }
Victor Stinner97f33c32020-05-14 18:05:58 +020013580 _posixstate* state = get_posix_state(PyType_GetModule(defining_class));
13581 st_mode = PyObject_GetAttr(stat, state->st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010013582 if (!st_mode)
13583 goto error;
13584
13585 mode = PyLong_AsLong(st_mode);
13586 if (mode == -1 && PyErr_Occurred())
13587 goto error;
13588 Py_CLEAR(st_mode);
13589 Py_CLEAR(stat);
13590 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010013591#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013592 }
13593 else if (is_symlink) {
13594 assert(mode_bits != S_IFLNK);
13595 result = 0;
13596 }
13597 else {
13598 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
13599#ifdef MS_WINDOWS
13600 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
13601 if (mode_bits == S_IFDIR)
13602 result = dir_bits != 0;
13603 else
13604 result = dir_bits == 0;
13605#else /* POSIX */
13606 if (mode_bits == S_IFDIR)
13607 result = self->d_type == DT_DIR;
13608 else
13609 result = self->d_type == DT_REG;
13610#endif
13611 }
Victor Stinner35a97c02015-03-08 02:59:09 +010013612#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013613
13614 return result;
13615
13616error:
13617 Py_XDECREF(st_mode);
13618 Py_XDECREF(stat);
13619 return -1;
13620}
13621
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013622/*[clinic input]
13623os.DirEntry.is_dir -> bool
Victor Stinner97f33c32020-05-14 18:05:58 +020013624 defining_class: defining_class
13625 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013626 *
13627 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010013628
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013629Return True if the entry is a directory; cached per entry.
13630[clinic start generated code]*/
13631
13632static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013633os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class,
13634 int follow_symlinks)
13635/*[clinic end generated code: output=0cd453b9c0987fdf input=1a4ffd6dec9920cb]*/
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013636{
Victor Stinner97f33c32020-05-14 18:05:58 +020013637 return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010013638}
13639
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013640/*[clinic input]
13641os.DirEntry.is_file -> bool
Victor Stinner97f33c32020-05-14 18:05:58 +020013642 defining_class: defining_class
13643 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013644 *
13645 follow_symlinks: bool = True
13646
13647Return True if the entry is a file; cached per entry.
13648[clinic start generated code]*/
13649
13650static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013651os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class,
13652 int follow_symlinks)
13653/*[clinic end generated code: output=f7c277ab5ba80908 input=0a64c5a12e802e3b]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013654{
Victor Stinner97f33c32020-05-14 18:05:58 +020013655 return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010013656}
13657
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013658/*[clinic input]
13659os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010013660
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013661Return inode of the entry; cached per entry.
13662[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013663
13664static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013665os_DirEntry_inode_impl(DirEntry *self)
13666/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013667{
13668#ifdef MS_WINDOWS
13669 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013670 PyObject *unicode;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013671 STRUCT_STAT stat;
13672 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010013673
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013674 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010013675 return NULL;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013676#if USE_UNICODE_WCHAR_CACHE
13677_Py_COMP_DIAG_PUSH
13678_Py_COMP_DIAG_IGNORE_DEPR_DECLS
13679 const wchar_t *path = PyUnicode_AsUnicode(unicode);
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013680 result = LSTAT(path, &stat);
13681 Py_DECREF(unicode);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013682_Py_COMP_DIAG_POP
13683#else /* USE_UNICODE_WCHAR_CACHE */
13684 wchar_t *path = PyUnicode_AsWideCharString(unicode, NULL);
13685 Py_DECREF(unicode);
13686 result = LSTAT(path, &stat);
13687 PyMem_Free(path);
13688#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner6036e442015-03-08 01:58:04 +010013689
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013690 if (result != 0)
13691 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013692
13693 self->win32_file_index = stat.st_ino;
13694 self->got_file_index = 1;
13695 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010013696 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
13697 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010013698#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020013699 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
13700 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010013701#endif
13702}
13703
13704static PyObject *
13705DirEntry_repr(DirEntry *self)
13706{
13707 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
13708}
13709
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013710/*[clinic input]
13711os.DirEntry.__fspath__
13712
13713Returns the path for the entry.
13714[clinic start generated code]*/
13715
Brett Cannon96881cd2016-06-10 14:37:21 -070013716static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013717os_DirEntry___fspath___impl(DirEntry *self)
13718/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070013719{
13720 Py_INCREF(self->path);
13721 return self->path;
13722}
13723
Victor Stinner6036e442015-03-08 01:58:04 +010013724static PyMemberDef DirEntry_members[] = {
13725 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
13726 "the entry's base filename, relative to scandir() \"path\" argument"},
13727 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
13728 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
13729 {NULL}
13730};
13731
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013732#include "clinic/posixmodule.c.h"
13733
Victor Stinner6036e442015-03-08 01:58:04 +010013734static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013735 OS_DIRENTRY_IS_DIR_METHODDEF
13736 OS_DIRENTRY_IS_FILE_METHODDEF
13737 OS_DIRENTRY_IS_SYMLINK_METHODDEF
13738 OS_DIRENTRY_STAT_METHODDEF
13739 OS_DIRENTRY_INODE_METHODDEF
13740 OS_DIRENTRY___FSPATH___METHODDEF
Batuhan Taşkayaf9dd51e2020-04-08 00:37:19 +030013741 {"__class_getitem__", (PyCFunction)Py_GenericAlias,
13742 METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Victor Stinner6036e442015-03-08 01:58:04 +010013743 {NULL}
13744};
13745
Eddie Elizondob3966632019-11-05 07:16:14 -080013746static PyType_Slot DirEntryType_slots[] = {
13747 {Py_tp_new, _disabled_new},
13748 {Py_tp_dealloc, DirEntry_dealloc},
13749 {Py_tp_repr, DirEntry_repr},
13750 {Py_tp_methods, DirEntry_methods},
13751 {Py_tp_members, DirEntry_members},
13752 {0, 0},
Victor Stinner6036e442015-03-08 01:58:04 +010013753};
13754
Eddie Elizondob3966632019-11-05 07:16:14 -080013755static PyType_Spec DirEntryType_spec = {
13756 MODNAME ".DirEntry",
13757 sizeof(DirEntry),
13758 0,
13759 Py_TPFLAGS_DEFAULT,
13760 DirEntryType_slots
13761};
13762
13763
Victor Stinner6036e442015-03-08 01:58:04 +010013764#ifdef MS_WINDOWS
13765
13766static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030013767join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010013768{
13769 Py_ssize_t path_len;
13770 Py_ssize_t size;
13771 wchar_t *result;
13772 wchar_t ch;
13773
13774 if (!path_wide) { /* Default arg: "." */
13775 path_wide = L".";
13776 path_len = 1;
13777 }
13778 else {
13779 path_len = wcslen(path_wide);
13780 }
13781
13782 /* The +1's are for the path separator and the NUL */
13783 size = path_len + 1 + wcslen(filename) + 1;
13784 result = PyMem_New(wchar_t, size);
13785 if (!result) {
13786 PyErr_NoMemory();
13787 return NULL;
13788 }
13789 wcscpy(result, path_wide);
13790 if (path_len > 0) {
13791 ch = result[path_len - 1];
13792 if (ch != SEP && ch != ALTSEP && ch != L':')
13793 result[path_len++] = SEP;
13794 wcscpy(result + path_len, filename);
13795 }
13796 return result;
13797}
13798
13799static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +020013800DirEntry_from_find_data(PyObject *module, path_t *path, WIN32_FIND_DATAW *dataW)
Victor Stinner6036e442015-03-08 01:58:04 +010013801{
13802 DirEntry *entry;
13803 BY_HANDLE_FILE_INFORMATION file_info;
13804 ULONG reparse_tag;
13805 wchar_t *joined_path;
13806
Victor Stinner1c2fa782020-05-10 11:05:29 +020013807 PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080013808 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013809 if (!entry)
13810 return NULL;
13811 entry->name = NULL;
13812 entry->path = NULL;
13813 entry->stat = NULL;
13814 entry->lstat = NULL;
13815 entry->got_file_index = 0;
13816
13817 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
13818 if (!entry->name)
13819 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013820 if (path->narrow) {
13821 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
13822 if (!entry->name)
13823 goto error;
13824 }
Victor Stinner6036e442015-03-08 01:58:04 +010013825
13826 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
13827 if (!joined_path)
13828 goto error;
13829
13830 entry->path = PyUnicode_FromWideChar(joined_path, -1);
13831 PyMem_Free(joined_path);
13832 if (!entry->path)
13833 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013834 if (path->narrow) {
13835 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
13836 if (!entry->path)
13837 goto error;
13838 }
Victor Stinner6036e442015-03-08 01:58:04 +010013839
Steve Dowercc16be82016-09-08 10:35:16 -070013840 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010013841 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
13842
13843 return (PyObject *)entry;
13844
13845error:
13846 Py_DECREF(entry);
13847 return NULL;
13848}
13849
13850#else /* POSIX */
13851
13852static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020013853join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010013854{
13855 Py_ssize_t path_len;
13856 Py_ssize_t size;
13857 char *result;
13858
13859 if (!path_narrow) { /* Default arg: "." */
13860 path_narrow = ".";
13861 path_len = 1;
13862 }
13863 else {
13864 path_len = strlen(path_narrow);
13865 }
13866
13867 if (filename_len == -1)
13868 filename_len = strlen(filename);
13869
13870 /* The +1's are for the path separator and the NUL */
13871 size = path_len + 1 + filename_len + 1;
13872 result = PyMem_New(char, size);
13873 if (!result) {
13874 PyErr_NoMemory();
13875 return NULL;
13876 }
13877 strcpy(result, path_narrow);
13878 if (path_len > 0 && result[path_len - 1] != '/')
13879 result[path_len++] = '/';
13880 strcpy(result + path_len, filename);
13881 return result;
13882}
13883
13884static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +020013885DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
13886 Py_ssize_t name_len, ino_t d_ino
Victor Stinner35a97c02015-03-08 02:59:09 +010013887#ifdef HAVE_DIRENT_D_TYPE
13888 , unsigned char d_type
13889#endif
13890 )
Victor Stinner6036e442015-03-08 01:58:04 +010013891{
13892 DirEntry *entry;
13893 char *joined_path;
13894
Victor Stinner1c2fa782020-05-10 11:05:29 +020013895 PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080013896 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013897 if (!entry)
13898 return NULL;
13899 entry->name = NULL;
13900 entry->path = NULL;
13901 entry->stat = NULL;
13902 entry->lstat = NULL;
13903
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013904 if (path->fd != -1) {
13905 entry->dir_fd = path->fd;
13906 joined_path = NULL;
13907 }
13908 else {
13909 entry->dir_fd = DEFAULT_DIR_FD;
13910 joined_path = join_path_filename(path->narrow, name, name_len);
13911 if (!joined_path)
13912 goto error;
13913 }
Victor Stinner6036e442015-03-08 01:58:04 +010013914
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030013915 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010013916 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013917 if (joined_path)
13918 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013919 }
13920 else {
13921 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013922 if (joined_path)
13923 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013924 }
13925 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013926 if (!entry->name)
13927 goto error;
13928
13929 if (path->fd != -1) {
13930 entry->path = entry->name;
13931 Py_INCREF(entry->path);
13932 }
13933 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010013934 goto error;
13935
Victor Stinner35a97c02015-03-08 02:59:09 +010013936#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013937 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013938#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013939 entry->d_ino = d_ino;
13940
13941 return (PyObject *)entry;
13942
13943error:
13944 Py_XDECREF(entry);
13945 return NULL;
13946}
13947
13948#endif
13949
13950
13951typedef struct {
13952 PyObject_HEAD
13953 path_t path;
13954#ifdef MS_WINDOWS
13955 HANDLE handle;
13956 WIN32_FIND_DATAW file_data;
13957 int first_time;
13958#else /* POSIX */
13959 DIR *dirp;
13960#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013961#ifdef HAVE_FDOPENDIR
13962 int fd;
13963#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013964} ScandirIterator;
13965
13966#ifdef MS_WINDOWS
13967
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013968static int
13969ScandirIterator_is_closed(ScandirIterator *iterator)
13970{
13971 return iterator->handle == INVALID_HANDLE_VALUE;
13972}
13973
Victor Stinner6036e442015-03-08 01:58:04 +010013974static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013975ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013976{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013977 HANDLE handle = iterator->handle;
13978
13979 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013980 return;
13981
Victor Stinner6036e442015-03-08 01:58:04 +010013982 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013983 Py_BEGIN_ALLOW_THREADS
13984 FindClose(handle);
13985 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013986}
13987
13988static PyObject *
13989ScandirIterator_iternext(ScandirIterator *iterator)
13990{
13991 WIN32_FIND_DATAW *file_data = &iterator->file_data;
13992 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013993 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013994
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013995 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013996 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013997 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013998
13999 while (1) {
14000 if (!iterator->first_time) {
14001 Py_BEGIN_ALLOW_THREADS
14002 success = FindNextFileW(iterator->handle, file_data);
14003 Py_END_ALLOW_THREADS
14004 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020014005 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010014006 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020014007 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010014008 break;
14009 }
14010 }
14011 iterator->first_time = 0;
14012
14013 /* Skip over . and .. */
14014 if (wcscmp(file_data->cFileName, L".") != 0 &&
Victor Stinner1c2fa782020-05-10 11:05:29 +020014015 wcscmp(file_data->cFileName, L"..") != 0)
14016 {
14017 PyObject *module = PyType_GetModule(Py_TYPE(iterator));
14018 entry = DirEntry_from_find_data(module, &iterator->path, file_data);
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020014019 if (!entry)
14020 break;
14021 return entry;
14022 }
Victor Stinner6036e442015-03-08 01:58:04 +010014023
14024 /* Loop till we get a non-dot directory or finish iterating */
14025 }
14026
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020014027 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020014028 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010014029 return NULL;
14030}
14031
14032#else /* POSIX */
14033
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020014034static int
14035ScandirIterator_is_closed(ScandirIterator *iterator)
14036{
14037 return !iterator->dirp;
14038}
14039
Victor Stinner6036e442015-03-08 01:58:04 +010014040static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020014041ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010014042{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030014043 DIR *dirp = iterator->dirp;
14044
14045 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010014046 return;
14047
Victor Stinner6036e442015-03-08 01:58:04 +010014048 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030014049 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014050#ifdef HAVE_FDOPENDIR
14051 if (iterator->path.fd != -1)
14052 rewinddir(dirp);
14053#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030014054 closedir(dirp);
14055 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010014056 return;
14057}
14058
14059static PyObject *
14060ScandirIterator_iternext(ScandirIterator *iterator)
14061{
14062 struct dirent *direntp;
14063 Py_ssize_t name_len;
14064 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020014065 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010014066
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020014067 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020014068 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010014069 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010014070
14071 while (1) {
14072 errno = 0;
14073 Py_BEGIN_ALLOW_THREADS
14074 direntp = readdir(iterator->dirp);
14075 Py_END_ALLOW_THREADS
14076
14077 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020014078 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010014079 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020014080 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010014081 break;
14082 }
14083
14084 /* Skip over . and .. */
14085 name_len = NAMLEN(direntp);
14086 is_dot = direntp->d_name[0] == '.' &&
14087 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
14088 if (!is_dot) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020014089 PyObject *module = PyType_GetModule(Py_TYPE(iterator));
14090 entry = DirEntry_from_posix_info(module,
14091 &iterator->path, direntp->d_name,
14092 name_len, direntp->d_ino
Victor Stinner35a97c02015-03-08 02:59:09 +010014093#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner1c2fa782020-05-10 11:05:29 +020014094 , direntp->d_type
Victor Stinner35a97c02015-03-08 02:59:09 +010014095#endif
14096 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020014097 if (!entry)
14098 break;
14099 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010014100 }
14101
14102 /* Loop till we get a non-dot directory or finish iterating */
14103 }
14104
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020014105 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020014106 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010014107 return NULL;
14108}
14109
14110#endif
14111
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020014112static PyObject *
14113ScandirIterator_close(ScandirIterator *self, PyObject *args)
14114{
14115 ScandirIterator_closedir(self);
14116 Py_RETURN_NONE;
14117}
14118
14119static PyObject *
14120ScandirIterator_enter(PyObject *self, PyObject *args)
14121{
14122 Py_INCREF(self);
14123 return self;
14124}
14125
14126static PyObject *
14127ScandirIterator_exit(ScandirIterator *self, PyObject *args)
14128{
14129 ScandirIterator_closedir(self);
14130 Py_RETURN_NONE;
14131}
14132
Victor Stinner6036e442015-03-08 01:58:04 +010014133static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010014134ScandirIterator_finalize(ScandirIterator *iterator)
14135{
14136 PyObject *error_type, *error_value, *error_traceback;
14137
14138 /* Save the current exception, if any. */
14139 PyErr_Fetch(&error_type, &error_value, &error_traceback);
14140
14141 if (!ScandirIterator_is_closed(iterator)) {
14142 ScandirIterator_closedir(iterator);
14143
14144 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
14145 "unclosed scandir iterator %R", iterator)) {
14146 /* Spurious errors can appear at shutdown */
14147 if (PyErr_ExceptionMatches(PyExc_Warning)) {
14148 PyErr_WriteUnraisable((PyObject *) iterator);
14149 }
14150 }
14151 }
14152
Victor Stinner7bfa4092016-03-23 00:43:54 +010014153 path_cleanup(&iterator->path);
14154
14155 /* Restore the saved exception. */
14156 PyErr_Restore(error_type, error_value, error_traceback);
14157}
14158
14159static void
Victor Stinner6036e442015-03-08 01:58:04 +010014160ScandirIterator_dealloc(ScandirIterator *iterator)
14161{
Eddie Elizondob3966632019-11-05 07:16:14 -080014162 PyTypeObject *tp = Py_TYPE(iterator);
Victor Stinner7bfa4092016-03-23 00:43:54 +010014163 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
14164 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020014165
Eddie Elizondob3966632019-11-05 07:16:14 -080014166 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
14167 free_func(iterator);
14168 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010014169}
14170
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020014171static PyMethodDef ScandirIterator_methods[] = {
14172 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
14173 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
14174 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
14175 {NULL}
14176};
14177
Eddie Elizondob3966632019-11-05 07:16:14 -080014178static PyType_Slot ScandirIteratorType_slots[] = {
14179 {Py_tp_new, _disabled_new},
14180 {Py_tp_dealloc, ScandirIterator_dealloc},
14181 {Py_tp_finalize, ScandirIterator_finalize},
14182 {Py_tp_iter, PyObject_SelfIter},
14183 {Py_tp_iternext, ScandirIterator_iternext},
14184 {Py_tp_methods, ScandirIterator_methods},
14185 {0, 0},
14186};
14187
14188static PyType_Spec ScandirIteratorType_spec = {
14189 MODNAME ".ScandirIterator",
14190 sizeof(ScandirIterator),
14191 0,
Victor Stinner97f33c32020-05-14 18:05:58 +020014192 // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since
14193 // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance.
Eddie Elizondob3966632019-11-05 07:16:14 -080014194 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
14195 ScandirIteratorType_slots
Victor Stinner6036e442015-03-08 01:58:04 +010014196};
14197
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014198/*[clinic input]
14199os.scandir
14200
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014201 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014202
14203Return an iterator of DirEntry objects for given path.
14204
BNMetricsb9427072018-11-02 15:20:19 +000014205path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014206is bytes, the names of yielded DirEntry objects will also be bytes; in
14207all other circumstances they will be str.
14208
14209If path is None, uses the path='.'.
14210[clinic start generated code]*/
14211
Victor Stinner6036e442015-03-08 01:58:04 +010014212static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014213os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000014214/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010014215{
14216 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010014217#ifdef MS_WINDOWS
14218 wchar_t *path_strW;
14219#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014220 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014221#ifdef HAVE_FDOPENDIR
14222 int fd = -1;
14223#endif
Victor Stinner6036e442015-03-08 01:58:04 +010014224#endif
14225
Steve Dower60419a72019-06-24 08:42:54 -070014226 if (PySys_Audit("os.scandir", "O",
14227 path->object ? path->object : Py_None) < 0) {
14228 return NULL;
14229 }
14230
Hai Shif707d942020-03-16 21:15:01 +080014231 PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014232 iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
Victor Stinner6036e442015-03-08 01:58:04 +010014233 if (!iterator)
14234 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010014235
14236#ifdef MS_WINDOWS
14237 iterator->handle = INVALID_HANDLE_VALUE;
14238#else
14239 iterator->dirp = NULL;
14240#endif
14241
Serhiy Storchaka095ef732017-02-09 20:05:51 +020014242 /* Move the ownership to iterator->path */
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030014243 memcpy(&iterator->path, path, sizeof(path_t));
14244 memset(path, 0, sizeof(path_t));
Victor Stinner6036e442015-03-08 01:58:04 +010014245
14246#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010014247 iterator->first_time = 1;
14248
14249 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
14250 if (!path_strW)
14251 goto error;
14252
14253 Py_BEGIN_ALLOW_THREADS
14254 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
14255 Py_END_ALLOW_THREADS
14256
14257 PyMem_Free(path_strW);
14258
14259 if (iterator->handle == INVALID_HANDLE_VALUE) {
14260 path_error(&iterator->path);
14261 goto error;
14262 }
14263#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010014264 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014265#ifdef HAVE_FDOPENDIR
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030014266 if (iterator->path.fd != -1) {
Ronald Oussoren41761932020-11-08 10:05:27 +010014267 if (HAVE_FDOPENDIR_RUNTIME) {
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014268 /* closedir() closes the FD, so we duplicate it */
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030014269 fd = _Py_dup(iterator->path.fd);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014270 if (fd == -1)
14271 goto error;
14272
14273 Py_BEGIN_ALLOW_THREADS
14274 iterator->dirp = fdopendir(fd);
14275 Py_END_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +010014276 } else {
14277 PyErr_SetString(PyExc_TypeError,
14278 "scandir: path should be string, bytes, os.PathLike or None, not int");
14279 return NULL;
14280 }
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014281 }
14282 else
14283#endif
14284 {
14285 if (iterator->path.narrow)
14286 path_str = iterator->path.narrow;
14287 else
14288 path_str = ".";
14289
14290 Py_BEGIN_ALLOW_THREADS
14291 iterator->dirp = opendir(path_str);
14292 Py_END_ALLOW_THREADS
14293 }
Victor Stinner6036e442015-03-08 01:58:04 +010014294
14295 if (!iterator->dirp) {
14296 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014297#ifdef HAVE_FDOPENDIR
14298 if (fd != -1) {
14299 Py_BEGIN_ALLOW_THREADS
14300 close(fd);
14301 Py_END_ALLOW_THREADS
14302 }
14303#endif
Victor Stinner6036e442015-03-08 01:58:04 +010014304 goto error;
14305 }
14306#endif
14307
14308 return (PyObject *)iterator;
14309
14310error:
14311 Py_DECREF(iterator);
14312 return NULL;
14313}
14314
Ethan Furman410ef8e2016-06-04 12:06:26 -070014315/*
14316 Return the file system path representation of the object.
14317
14318 If the object is str or bytes, then allow it to pass through with
14319 an incremented refcount. If the object defines __fspath__(), then
14320 return the result of that method. All other types raise a TypeError.
14321*/
14322PyObject *
14323PyOS_FSPath(PyObject *path)
14324{
Brett Cannon3f9183b2016-08-26 14:44:48 -070014325 /* For error message reasons, this function is manually inlined in
14326 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070014327 PyObject *func = NULL;
14328 PyObject *path_repr = NULL;
14329
14330 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
14331 Py_INCREF(path);
14332 return path;
14333 }
14334
14335 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
14336 if (NULL == func) {
14337 return PyErr_Format(PyExc_TypeError,
14338 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070014339 "not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -080014340 _PyType_Name(Py_TYPE(path)));
Ethan Furman410ef8e2016-06-04 12:06:26 -070014341 }
14342
Victor Stinnerf17c3de2016-12-06 18:46:19 +010014343 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070014344 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070014345 if (NULL == path_repr) {
14346 return NULL;
14347 }
14348
Brett Cannonc78ca1e2016-06-24 12:03:43 -070014349 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
14350 PyErr_Format(PyExc_TypeError,
14351 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -080014352 "not %.200s", _PyType_Name(Py_TYPE(path)),
14353 _PyType_Name(Py_TYPE(path_repr)));
Brett Cannonc78ca1e2016-06-24 12:03:43 -070014354 Py_DECREF(path_repr);
14355 return NULL;
14356 }
14357
Ethan Furman410ef8e2016-06-04 12:06:26 -070014358 return path_repr;
14359}
14360
14361/*[clinic input]
14362os.fspath
14363
14364 path: object
14365
14366Return the file system path representation of the object.
14367
Brett Cannonb4f43e92016-06-09 14:32:08 -070014368If the object is str or bytes, then allow it to pass through as-is. If the
14369object defines __fspath__(), then return the result of that method. All other
14370types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070014371[clinic start generated code]*/
14372
14373static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030014374os_fspath_impl(PyObject *module, PyObject *path)
14375/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070014376{
14377 return PyOS_FSPath(path);
14378}
Victor Stinner6036e442015-03-08 01:58:04 +010014379
Victor Stinner9b1f4742016-09-06 16:18:52 -070014380#ifdef HAVE_GETRANDOM_SYSCALL
14381/*[clinic input]
14382os.getrandom
14383
14384 size: Py_ssize_t
14385 flags: int=0
14386
14387Obtain a series of random bytes.
14388[clinic start generated code]*/
14389
14390static PyObject *
14391os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
14392/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
14393{
Victor Stinner9b1f4742016-09-06 16:18:52 -070014394 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020014395 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014396
14397 if (size < 0) {
14398 errno = EINVAL;
14399 return posix_error();
14400 }
14401
Victor Stinnerec2319c2016-09-20 23:00:59 +020014402 bytes = PyBytes_FromStringAndSize(NULL, size);
14403 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070014404 PyErr_NoMemory();
14405 return NULL;
14406 }
14407
14408 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020014409 n = syscall(SYS_getrandom,
14410 PyBytes_AS_STRING(bytes),
14411 PyBytes_GET_SIZE(bytes),
14412 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070014413 if (n < 0 && errno == EINTR) {
14414 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020014415 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014416 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020014417
14418 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070014419 continue;
14420 }
14421 break;
14422 }
14423
14424 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070014425 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020014426 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014427 }
14428
Victor Stinnerec2319c2016-09-20 23:00:59 +020014429 if (n != size) {
14430 _PyBytes_Resize(&bytes, n);
14431 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070014432
14433 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020014434
14435error:
14436 Py_DECREF(bytes);
14437 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014438}
14439#endif /* HAVE_GETRANDOM_SYSCALL */
14440
Steve Dower2438cdf2019-03-29 16:37:16 -070014441#ifdef MS_WINDOWS
14442/* bpo-36085: Helper functions for managing DLL search directories
14443 * on win32
14444 */
14445
14446typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
14447typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
14448
14449/*[clinic input]
14450os._add_dll_directory
14451
14452 path: path_t
14453
14454Add a path to the DLL search path.
14455
14456This search path is used when resolving dependencies for imported
14457extension modules (the module itself is resolved through sys.path),
14458and also by ctypes.
14459
14460Returns an opaque value that may be passed to os.remove_dll_directory
14461to remove this directory from the search path.
14462[clinic start generated code]*/
14463
14464static PyObject *
14465os__add_dll_directory_impl(PyObject *module, path_t *path)
14466/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
14467{
14468 HMODULE hKernel32;
14469 PAddDllDirectory AddDllDirectory;
14470 DLL_DIRECTORY_COOKIE cookie = 0;
14471 DWORD err = 0;
14472
Saiyang Gou7514f4f2020-02-12 23:47:42 -080014473 if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) {
14474 return NULL;
14475 }
14476
Steve Dower2438cdf2019-03-29 16:37:16 -070014477 /* For Windows 7, we have to load this. As this will be a fairly
14478 infrequent operation, just do it each time. Kernel32 is always
14479 loaded. */
14480 Py_BEGIN_ALLOW_THREADS
14481 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
14482 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
14483 hKernel32, "AddDllDirectory")) ||
14484 !(cookie = (*AddDllDirectory)(path->wide))) {
14485 err = GetLastError();
14486 }
14487 Py_END_ALLOW_THREADS
14488
14489 if (err) {
14490 return win32_error_object_err("add_dll_directory",
14491 path->object, err);
14492 }
14493
14494 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
14495}
14496
14497/*[clinic input]
14498os._remove_dll_directory
14499
14500 cookie: object
14501
14502Removes a path from the DLL search path.
14503
14504The parameter is an opaque value that was returned from
14505os.add_dll_directory. You can only remove directories that you added
14506yourself.
14507[clinic start generated code]*/
14508
14509static PyObject *
14510os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
14511/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
14512{
14513 HMODULE hKernel32;
14514 PRemoveDllDirectory RemoveDllDirectory;
14515 DLL_DIRECTORY_COOKIE cookieValue;
14516 DWORD err = 0;
14517
14518 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
14519 PyErr_SetString(PyExc_TypeError,
14520 "Provided cookie was not returned from os.add_dll_directory");
14521 return NULL;
14522 }
14523
14524 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
14525 cookie, "DLL directory cookie");
14526
14527 /* For Windows 7, we have to load this. As this will be a fairly
14528 infrequent operation, just do it each time. Kernel32 is always
14529 loaded. */
14530 Py_BEGIN_ALLOW_THREADS
14531 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
14532 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
14533 hKernel32, "RemoveDllDirectory")) ||
14534 !(*RemoveDllDirectory)(cookieValue)) {
14535 err = GetLastError();
14536 }
14537 Py_END_ALLOW_THREADS
14538
14539 if (err) {
14540 return win32_error_object_err("remove_dll_directory",
14541 NULL, err);
14542 }
14543
14544 if (PyCapsule_SetName(cookie, NULL)) {
14545 return NULL;
14546 }
14547
14548 Py_RETURN_NONE;
14549}
14550
14551#endif
Larry Hastings31826802013-10-19 00:09:25 -070014552
Victor Stinner65a796e2020-04-01 18:49:29 +020014553
14554/* Only check if WIFEXITED is available: expect that it comes
14555 with WEXITSTATUS, WIFSIGNALED, etc.
14556
14557 os.waitstatus_to_exitcode() is implemented in C and not in Python, so
14558 subprocess can safely call it during late Python finalization without
Victor Stinner62230712020-11-18 23:18:29 +010014559 risking that used os attributes were set to None by finalize_modules(). */
Victor Stinner65a796e2020-04-01 18:49:29 +020014560#if defined(WIFEXITED) || defined(MS_WINDOWS)
14561/*[clinic input]
14562os.waitstatus_to_exitcode
14563
Victor Stinner9bee32b2020-04-22 16:30:35 +020014564 status as status_obj: object
Victor Stinner65a796e2020-04-01 18:49:29 +020014565
14566Convert a wait status to an exit code.
14567
14568On Unix:
14569
14570* If WIFEXITED(status) is true, return WEXITSTATUS(status).
14571* If WIFSIGNALED(status) is true, return -WTERMSIG(status).
14572* Otherwise, raise a ValueError.
14573
14574On Windows, return status shifted right by 8 bits.
14575
14576On Unix, if the process is being traced or if waitpid() was called with
14577WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true.
14578This function must not be called if WIFSTOPPED(status) is true.
14579[clinic start generated code]*/
14580
14581static PyObject *
Victor Stinner9bee32b2020-04-22 16:30:35 +020014582os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj)
14583/*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/
Victor Stinner65a796e2020-04-01 18:49:29 +020014584{
14585#ifndef MS_WINDOWS
Victor Stinner9bee32b2020-04-22 16:30:35 +020014586 int status = _PyLong_AsInt(status_obj);
14587 if (status == -1 && PyErr_Occurred()) {
14588 return NULL;
14589 }
14590
Victor Stinner65a796e2020-04-01 18:49:29 +020014591 WAIT_TYPE wait_status;
14592 WAIT_STATUS_INT(wait_status) = status;
14593 int exitcode;
14594 if (WIFEXITED(wait_status)) {
14595 exitcode = WEXITSTATUS(wait_status);
14596 /* Sanity check to provide warranty on the function behavior.
14597 It should not occur in practice */
14598 if (exitcode < 0) {
14599 PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode);
14600 return NULL;
14601 }
14602 }
14603 else if (WIFSIGNALED(wait_status)) {
14604 int signum = WTERMSIG(wait_status);
14605 /* Sanity check to provide warranty on the function behavior.
14606 It should not occurs in practice */
14607 if (signum <= 0) {
14608 PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum);
14609 return NULL;
14610 }
14611 exitcode = -signum;
14612 } else if (WIFSTOPPED(wait_status)) {
14613 /* Status only received if the process is being traced
14614 or if waitpid() was called with WUNTRACED option. */
14615 int signum = WSTOPSIG(wait_status);
14616 PyErr_Format(PyExc_ValueError,
14617 "process stopped by delivery of signal %i",
14618 signum);
14619 return NULL;
14620 }
14621 else {
14622 PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status);
14623 return NULL;
14624 }
14625 return PyLong_FromLong(exitcode);
14626#else
14627 /* Windows implementation: see os.waitpid() implementation
14628 which uses _cwait(). */
Victor Stinner9bee32b2020-04-22 16:30:35 +020014629 unsigned long long status = PyLong_AsUnsignedLongLong(status_obj);
14630 if (status == (unsigned long long)-1 && PyErr_Occurred()) {
14631 return NULL;
14632 }
14633
14634 unsigned long long exitcode = (status >> 8);
14635 /* ExitProcess() accepts an UINT type:
14636 reject exit code which doesn't fit in an UINT */
14637 if (exitcode > UINT_MAX) {
14638 PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode);
14639 return NULL;
14640 }
14641 return PyLong_FromUnsignedLong((unsigned long)exitcode);
Victor Stinner65a796e2020-04-01 18:49:29 +020014642#endif
14643}
14644#endif
14645
14646
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014647static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070014648
14649 OS_STAT_METHODDEF
14650 OS_ACCESS_METHODDEF
14651 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014652 OS_CHDIR_METHODDEF
14653 OS_CHFLAGS_METHODDEF
14654 OS_CHMOD_METHODDEF
14655 OS_FCHMOD_METHODDEF
14656 OS_LCHMOD_METHODDEF
14657 OS_CHOWN_METHODDEF
14658 OS_FCHOWN_METHODDEF
14659 OS_LCHOWN_METHODDEF
14660 OS_LCHFLAGS_METHODDEF
14661 OS_CHROOT_METHODDEF
14662 OS_CTERMID_METHODDEF
14663 OS_GETCWD_METHODDEF
14664 OS_GETCWDB_METHODDEF
14665 OS_LINK_METHODDEF
14666 OS_LISTDIR_METHODDEF
14667 OS_LSTAT_METHODDEF
14668 OS_MKDIR_METHODDEF
14669 OS_NICE_METHODDEF
14670 OS_GETPRIORITY_METHODDEF
14671 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014672 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030014673 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030014674 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010014675 OS_COPY_FILE_RANGE_METHODDEF
Pablo Galindoa57b3d32020-11-17 00:00:38 +000014676 OS_SPLICE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014677 OS_RENAME_METHODDEF
14678 OS_REPLACE_METHODDEF
14679 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014680 OS_SYMLINK_METHODDEF
14681 OS_SYSTEM_METHODDEF
14682 OS_UMASK_METHODDEF
14683 OS_UNAME_METHODDEF
14684 OS_UNLINK_METHODDEF
14685 OS_REMOVE_METHODDEF
14686 OS_UTIME_METHODDEF
14687 OS_TIMES_METHODDEF
14688 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014689 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014690 OS_EXECV_METHODDEF
14691 OS_EXECVE_METHODDEF
14692 OS_SPAWNV_METHODDEF
14693 OS_SPAWNVE_METHODDEF
14694 OS_FORK1_METHODDEF
14695 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020014696 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014697 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
14698 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
14699 OS_SCHED_GETPARAM_METHODDEF
14700 OS_SCHED_GETSCHEDULER_METHODDEF
14701 OS_SCHED_RR_GET_INTERVAL_METHODDEF
14702 OS_SCHED_SETPARAM_METHODDEF
14703 OS_SCHED_SETSCHEDULER_METHODDEF
14704 OS_SCHED_YIELD_METHODDEF
14705 OS_SCHED_SETAFFINITY_METHODDEF
14706 OS_SCHED_GETAFFINITY_METHODDEF
14707 OS_OPENPTY_METHODDEF
14708 OS_FORKPTY_METHODDEF
14709 OS_GETEGID_METHODDEF
14710 OS_GETEUID_METHODDEF
14711 OS_GETGID_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014712 OS_GETGROUPLIST_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014713 OS_GETGROUPS_METHODDEF
14714 OS_GETPID_METHODDEF
14715 OS_GETPGRP_METHODDEF
14716 OS_GETPPID_METHODDEF
14717 OS_GETUID_METHODDEF
14718 OS_GETLOGIN_METHODDEF
14719 OS_KILL_METHODDEF
14720 OS_KILLPG_METHODDEF
14721 OS_PLOCK_METHODDEF
Steve Dowercc16be82016-09-08 10:35:16 -070014722 OS_STARTFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014723 OS_SETUID_METHODDEF
14724 OS_SETEUID_METHODDEF
14725 OS_SETREUID_METHODDEF
14726 OS_SETGID_METHODDEF
14727 OS_SETEGID_METHODDEF
14728 OS_SETREGID_METHODDEF
14729 OS_SETGROUPS_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014730 OS_INITGROUPS_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014731 OS_GETPGID_METHODDEF
14732 OS_SETPGRP_METHODDEF
14733 OS_WAIT_METHODDEF
14734 OS_WAIT3_METHODDEF
14735 OS_WAIT4_METHODDEF
14736 OS_WAITID_METHODDEF
14737 OS_WAITPID_METHODDEF
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -080014738 OS_PIDFD_OPEN_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014739 OS_GETSID_METHODDEF
14740 OS_SETSID_METHODDEF
14741 OS_SETPGID_METHODDEF
14742 OS_TCGETPGRP_METHODDEF
14743 OS_TCSETPGRP_METHODDEF
14744 OS_OPEN_METHODDEF
14745 OS_CLOSE_METHODDEF
14746 OS_CLOSERANGE_METHODDEF
14747 OS_DEVICE_ENCODING_METHODDEF
14748 OS_DUP_METHODDEF
14749 OS_DUP2_METHODDEF
14750 OS_LOCKF_METHODDEF
14751 OS_LSEEK_METHODDEF
14752 OS_READ_METHODDEF
14753 OS_READV_METHODDEF
14754 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000014755 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014756 OS_WRITE_METHODDEF
14757 OS_WRITEV_METHODDEF
14758 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000014759 OS_PWRITEV_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014760 OS_SENDFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014761 OS_FSTAT_METHODDEF
14762 OS_ISATTY_METHODDEF
14763 OS_PIPE_METHODDEF
14764 OS_PIPE2_METHODDEF
14765 OS_MKFIFO_METHODDEF
14766 OS_MKNOD_METHODDEF
14767 OS_MAJOR_METHODDEF
14768 OS_MINOR_METHODDEF
14769 OS_MAKEDEV_METHODDEF
14770 OS_FTRUNCATE_METHODDEF
14771 OS_TRUNCATE_METHODDEF
14772 OS_POSIX_FALLOCATE_METHODDEF
14773 OS_POSIX_FADVISE_METHODDEF
14774 OS_PUTENV_METHODDEF
14775 OS_UNSETENV_METHODDEF
14776 OS_STRERROR_METHODDEF
14777 OS_FCHDIR_METHODDEF
14778 OS_FSYNC_METHODDEF
14779 OS_SYNC_METHODDEF
14780 OS_FDATASYNC_METHODDEF
14781 OS_WCOREDUMP_METHODDEF
14782 OS_WIFCONTINUED_METHODDEF
14783 OS_WIFSTOPPED_METHODDEF
14784 OS_WIFSIGNALED_METHODDEF
14785 OS_WIFEXITED_METHODDEF
14786 OS_WEXITSTATUS_METHODDEF
14787 OS_WTERMSIG_METHODDEF
14788 OS_WSTOPSIG_METHODDEF
14789 OS_FSTATVFS_METHODDEF
14790 OS_STATVFS_METHODDEF
14791 OS_CONFSTR_METHODDEF
14792 OS_SYSCONF_METHODDEF
14793 OS_FPATHCONF_METHODDEF
14794 OS_PATHCONF_METHODDEF
14795 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030014796 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014797 OS__GETDISKUSAGE_METHODDEF
14798 OS__GETFINALPATHNAME_METHODDEF
14799 OS__GETVOLUMEPATHNAME_METHODDEF
Steve Dower04732ca2021-04-07 01:02:07 +010014800 OS__PATH_SPLITROOT_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014801 OS_GETLOADAVG_METHODDEF
14802 OS_URANDOM_METHODDEF
14803 OS_SETRESUID_METHODDEF
14804 OS_SETRESGID_METHODDEF
14805 OS_GETRESUID_METHODDEF
14806 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000014807
Larry Hastings2f936352014-08-05 14:04:04 +100014808 OS_GETXATTR_METHODDEF
14809 OS_SETXATTR_METHODDEF
14810 OS_REMOVEXATTR_METHODDEF
14811 OS_LISTXATTR_METHODDEF
14812
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014813 OS_GET_TERMINAL_SIZE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014814 OS_CPU_COUNT_METHODDEF
14815 OS_GET_INHERITABLE_METHODDEF
14816 OS_SET_INHERITABLE_METHODDEF
14817 OS_GET_HANDLE_INHERITABLE_METHODDEF
14818 OS_SET_HANDLE_INHERITABLE_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030014819 OS_GET_BLOCKING_METHODDEF
14820 OS_SET_BLOCKING_METHODDEF
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014821 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070014822 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070014823 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014824 OS_MEMFD_CREATE_METHODDEF
Christian Heimescd9fed62020-11-13 19:48:52 +010014825 OS_EVENTFD_METHODDEF
14826 OS_EVENTFD_READ_METHODDEF
14827 OS_EVENTFD_WRITE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070014828 OS__ADD_DLL_DIRECTORY_METHODDEF
14829 OS__REMOVE_DLL_DIRECTORY_METHODDEF
Victor Stinner65a796e2020-04-01 18:49:29 +020014830 OS_WAITSTATUS_TO_EXITCODE_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000014831 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014832};
14833
Barry Warsaw4a342091996-12-19 23:50:02 +000014834static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014835all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000014836{
Guido van Rossum94f6f721999-01-06 18:42:14 +000014837#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014838 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014839#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014840#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014841 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014842#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014843#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014844 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014845#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014846#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014847 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014848#endif
Fred Drakec9680921999-12-13 16:37:25 +000014849#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014850 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000014851#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014852#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014853 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014854#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014855#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014856 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014857#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014858#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014859 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014860#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014861#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014862 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014863#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014864#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014865 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014866#endif
14867#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014868 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014869#endif
14870#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014871 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014872#endif
14873#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014874 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014875#endif
14876#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014877 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014878#endif
14879#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014880 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014881#endif
14882#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014883 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014884#endif
14885#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014886 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014887#endif
14888#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014889 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014890#endif
14891#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014892 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014893#endif
14894#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014895 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014896#endif
14897#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014898 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014899#endif
14900#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014901 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014902#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000014903#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014904 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014905#endif
14906#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014907 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014908#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014909#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014910 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014911#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014912#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014913 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014914#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014915#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000014916#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014917 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014918#endif
14919#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014920 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014921#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014922#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014923#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014924 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014925#endif
14926#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014927 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014928#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014929#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014930 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014931#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014932#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014933 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014934#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020014935#ifdef O_TMPFILE
14936 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
14937#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014938#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014939 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014940#endif
14941#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014942 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014943#endif
14944#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014945 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014946#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020014947#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014948 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020014949#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014950#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014951 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014952#endif
Dong-hee Naf917c242021-02-04 08:32:55 +090014953#ifdef O_EVTONLY
14954 if (PyModule_AddIntMacro(m, O_EVTONLY)) return -1;
14955#endif
14956#ifdef O_FSYNC
14957 if (PyModule_AddIntMacro(m, O_FSYNC)) return -1;
14958#endif
14959#ifdef O_SYMLINK
14960 if (PyModule_AddIntMacro(m, O_SYMLINK)) return -1;
14961#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014962
Jesus Cea94363612012-06-22 18:32:07 +020014963#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014964 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014965#endif
14966#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014967 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014968#endif
14969
Tim Peters5aa91602002-01-30 05:46:57 +000014970/* MS Windows */
14971#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000014972 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014973 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014974#endif
14975#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000014976 /* Optimize for short life (keep in memory). */
14977 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014978 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014979#endif
14980#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000014981 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014982 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014983#endif
14984#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000014985 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014986 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014987#endif
14988#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000014989 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014990 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014991#endif
14992
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014993/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014994#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000014995 /* Send a SIGIO signal whenever input or output
14996 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014997 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014998#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014999#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000015000 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015001 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000015002#endif
15003#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000015004 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015005 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000015006#endif
15007#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000015008 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015009 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000015010#endif
Dong-hee Naf917c242021-02-04 08:32:55 +090015011#ifdef O_NOFOLLOW_ANY
15012 if (PyModule_AddIntMacro(m, O_NOFOLLOW_ANY)) return -1;
15013#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020015014#ifdef O_NOLINKS
15015 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015016 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020015017#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000015018#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000015019 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015020 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000015021#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000015022
Victor Stinner8c62be82010-05-06 00:08:46 +000015023 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015024#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015025 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015026#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015027#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015028 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015029#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015030#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015031 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015032#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015033#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015034 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015035#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015036#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015037 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015038#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015039#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015040 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015041#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015042#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015043 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015044#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015045#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015046 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015047#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015048#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015049 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015050#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015051#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015052 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015053#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015054#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015055 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015056#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015057#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015058 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015059#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015060#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015061 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015062#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015063#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015064 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015065#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015066#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015067 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015068#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015069#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015070 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015071#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015072#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015073 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000015074#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000015075
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000015076 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000015077#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015078 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000015079#endif /* ST_RDONLY */
15080#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015081 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000015082#endif /* ST_NOSUID */
15083
doko@ubuntu.comca616a22013-12-08 15:23:07 +010015084 /* GNU extensions */
15085#ifdef ST_NODEV
15086 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
15087#endif /* ST_NODEV */
15088#ifdef ST_NOEXEC
15089 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
15090#endif /* ST_NOEXEC */
15091#ifdef ST_SYNCHRONOUS
15092 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
15093#endif /* ST_SYNCHRONOUS */
15094#ifdef ST_MANDLOCK
15095 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
15096#endif /* ST_MANDLOCK */
15097#ifdef ST_WRITE
15098 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
15099#endif /* ST_WRITE */
15100#ifdef ST_APPEND
15101 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
15102#endif /* ST_APPEND */
15103#ifdef ST_NOATIME
15104 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
15105#endif /* ST_NOATIME */
15106#ifdef ST_NODIRATIME
15107 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
15108#endif /* ST_NODIRATIME */
15109#ifdef ST_RELATIME
15110 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
15111#endif /* ST_RELATIME */
15112
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000015113 /* FreeBSD sendfile() constants */
15114#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015115 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000015116#endif
15117#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015118 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000015119#endif
15120#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015121 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000015122#endif
15123
Ross Lagerwall7807c352011-03-17 20:20:30 +020015124 /* constants for posix_fadvise */
15125#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015126 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015127#endif
15128#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015129 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015130#endif
15131#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015132 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015133#endif
15134#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015135 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015136#endif
15137#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015138 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015139#endif
15140#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015141 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015142#endif
15143
15144 /* constants for waitid */
15145#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015146 if (PyModule_AddIntMacro(m, P_PID)) return -1;
15147 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
15148 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080015149#ifdef P_PIDFD
15150 if (PyModule_AddIntMacro(m, P_PIDFD)) return -1;
15151#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020015152#endif
15153#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015154 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015155#endif
15156#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015157 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015158#endif
15159#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015160 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015161#endif
15162#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015163 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015164#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090015165#ifdef CLD_KILLED
15166 if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1;
15167#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020015168#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015169 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015170#endif
15171#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015172 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015173#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090015174#ifdef CLD_STOPPED
15175 if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1;
15176#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020015177#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015178 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015179#endif
15180
15181 /* constants for lockf */
15182#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015183 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015184#endif
15185#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015186 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015187#endif
15188#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015189 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015190#endif
15191#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015192 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015193#endif
15194
Pablo Galindo4defba32018-01-27 16:16:37 +000015195#ifdef RWF_DSYNC
15196 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
15197#endif
15198#ifdef RWF_HIPRI
15199 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
15200#endif
15201#ifdef RWF_SYNC
15202 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
15203#endif
15204#ifdef RWF_NOWAIT
15205 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
15206#endif
YoSTEALTH76ef2552020-05-27 15:32:22 -060015207#ifdef RWF_APPEND
15208 if (PyModule_AddIntConstant(m, "RWF_APPEND", RWF_APPEND)) return -1;
15209#endif
Pablo Galindo4defba32018-01-27 16:16:37 +000015210
Pablo Galindoa57b3d32020-11-17 00:00:38 +000015211/* constants for splice */
Pablo Galindo2a9eddf2020-11-17 19:57:49 +000015212#if defined(HAVE_SPLICE) && defined(__linux__)
Pablo Galindoa57b3d32020-11-17 00:00:38 +000015213 if (PyModule_AddIntConstant(m, "SPLICE_F_MOVE", SPLICE_F_MOVE)) return -1;
15214 if (PyModule_AddIntConstant(m, "SPLICE_F_NONBLOCK", SPLICE_F_NONBLOCK)) return -1;
15215 if (PyModule_AddIntConstant(m, "SPLICE_F_MORE", SPLICE_F_MORE)) return -1;
15216#endif
15217
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000015218/* constants for posix_spawn */
15219#ifdef HAVE_POSIX_SPAWN
15220 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
15221 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
15222 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
15223#endif
15224
pxinwrf2d7ac72019-05-21 18:46:37 +080015225#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015226 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
15227 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015228 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080015229#endif
15230#ifdef HAVE_SPAWNV
15231 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015232 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000015233#endif
15234
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015235#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070015236#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015237 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070015238#endif
15239#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015240 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070015241#endif
15242#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015243 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070015244#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015245#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080015246 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015247#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015248#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015249 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015250#endif
15251#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015252 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015253#endif
15254#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015255 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015256#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015257#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015258 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015259#endif
15260#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015261 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015262#endif
15263#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015264 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015265#endif
15266#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015267 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015268#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015269#endif
15270
Benjamin Peterson9428d532011-09-14 11:45:52 -040015271#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015272 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
15273 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
15274 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040015275#endif
15276
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015277#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015278 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015279#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015280#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015281 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015282#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015283#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015284 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015285#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015286#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015287 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015288#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015289#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015290 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015291#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015292#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015293 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015294#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015295#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015296 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015297#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010015298#if HAVE_DECL_RTLD_MEMBER
15299 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
15300#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020015301
Victor Stinner9b1f4742016-09-06 16:18:52 -070015302#ifdef HAVE_GETRANDOM_SYSCALL
15303 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
15304 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
15305#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015306#ifdef HAVE_MEMFD_CREATE
15307 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
15308 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
15309#ifdef MFD_HUGETLB
15310 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015311#endif
15312#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015313 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015314#endif
15315#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015316 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015317#endif
15318#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015319 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015320#endif
15321#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015322 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015323#endif
15324#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015325 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015326#endif
15327#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015328 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015329#endif
15330#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015331 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015332#endif
15333#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015334 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015335#endif
15336#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015337 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015338#endif
15339#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015340 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015341#endif
15342#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015343 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015344#endif
15345#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015346 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015347#endif
15348#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015349 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015350#endif
15351#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015352 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
15353#endif
Christian Heimescd9fed62020-11-13 19:48:52 +010015354#endif /* HAVE_MEMFD_CREATE */
15355
15356#ifdef HAVE_EVENTFD
15357 if (PyModule_AddIntMacro(m, EFD_CLOEXEC)) return -1;
15358 if (PyModule_AddIntMacro(m, EFD_NONBLOCK)) return -1;
15359 if (PyModule_AddIntMacro(m, EFD_SEMAPHORE)) return -1;
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015360#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070015361
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020015362#if defined(__APPLE__)
15363 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
15364#endif
15365
Steve Dower2438cdf2019-03-29 16:37:16 -070015366#ifdef MS_WINDOWS
15367 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
15368 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
15369 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
15370 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
15371 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
15372#endif
15373
Victor Stinner8c62be82010-05-06 00:08:46 +000015374 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000015375}
15376
15377
Ronald Oussoren41761932020-11-08 10:05:27 +010015378
15379#define PROBE(name, test) \
15380 static int name(void) \
15381 { \
15382 if (test) { \
15383 return 1; \
15384 } else { \
15385 return 0; \
15386 } \
15387 }
15388
15389#ifdef HAVE_FSTATAT
15390PROBE(probe_fstatat, HAVE_FSTATAT_RUNTIME)
15391#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070015392
15393#ifdef HAVE_FACCESSAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015394PROBE(probe_faccessat, HAVE_FACCESSAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015395#endif
15396
15397#ifdef HAVE_FCHMODAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015398PROBE(probe_fchmodat, HAVE_FCHMODAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015399#endif
15400
Larry Hastings00964ed2013-08-12 13:49:30 -040015401#ifdef HAVE_FCHOWNAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015402PROBE(probe_fchownat, HAVE_FCHOWNAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015403#endif
15404
15405#ifdef HAVE_LINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015406PROBE(probe_linkat, HAVE_LINKAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015407#endif
15408
Ronald Oussoren41761932020-11-08 10:05:27 +010015409#ifdef HAVE_FDOPENDIR
15410PROBE(probe_fdopendir, HAVE_FDOPENDIR_RUNTIME)
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015411#endif
15412
Larry Hastings9cf065c2012-06-22 16:30:09 -070015413#ifdef HAVE_MKDIRAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015414PROBE(probe_mkdirat, HAVE_MKDIRAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015415#endif
15416
15417#ifdef HAVE_RENAMEAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015418PROBE(probe_renameat, HAVE_RENAMEAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015419#endif
15420
15421#ifdef HAVE_UNLINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015422PROBE(probe_unlinkat, HAVE_UNLINKAT_RUNTIME)
15423#endif
15424
15425#ifdef HAVE_OPENAT
15426PROBE(probe_openat, HAVE_OPENAT_RUNTIME)
15427#endif
15428
15429#ifdef HAVE_READLINKAT
15430PROBE(probe_readlinkat, HAVE_READLINKAT_RUNTIME)
15431#endif
15432
15433#ifdef HAVE_SYMLINKAT
15434PROBE(probe_symlinkat, HAVE_SYMLINKAT_RUNTIME)
15435#endif
15436
15437#ifdef HAVE_FUTIMENS
15438PROBE(probe_futimens, HAVE_FUTIMENS_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015439#endif
15440
15441#ifdef HAVE_UTIMENSAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015442PROBE(probe_utimensat, HAVE_UTIMENSAT_RUNTIME)
15443#endif
15444
15445
15446
15447
15448static const struct have_function {
15449 const char * const label;
15450 int (*probe)(void);
15451} have_functions[] = {
15452
Christian Heimescd9fed62020-11-13 19:48:52 +010015453#ifdef HAVE_EVENTFD
15454 {"HAVE_EVENTFD", NULL},
15455#endif
15456
Ronald Oussoren41761932020-11-08 10:05:27 +010015457#ifdef HAVE_FACCESSAT
15458 { "HAVE_FACCESSAT", probe_faccessat },
15459#endif
15460
15461#ifdef HAVE_FCHDIR
15462 { "HAVE_FCHDIR", NULL },
15463#endif
15464
15465#ifdef HAVE_FCHMOD
15466 { "HAVE_FCHMOD", NULL },
15467#endif
15468
15469#ifdef HAVE_FCHMODAT
15470 { "HAVE_FCHMODAT", probe_fchmodat },
15471#endif
15472
15473#ifdef HAVE_FCHOWN
15474 { "HAVE_FCHOWN", NULL },
15475#endif
15476
15477#ifdef HAVE_FCHOWNAT
15478 { "HAVE_FCHOWNAT", probe_fchownat },
15479#endif
15480
15481#ifdef HAVE_FEXECVE
15482 { "HAVE_FEXECVE", NULL },
15483#endif
15484
15485#ifdef HAVE_FDOPENDIR
15486 { "HAVE_FDOPENDIR", probe_fdopendir },
15487#endif
15488
15489#ifdef HAVE_FPATHCONF
15490 { "HAVE_FPATHCONF", NULL },
15491#endif
15492
15493#ifdef HAVE_FSTATAT
15494 { "HAVE_FSTATAT", probe_fstatat },
15495#endif
15496
15497#ifdef HAVE_FSTATVFS
15498 { "HAVE_FSTATVFS", NULL },
15499#endif
15500
15501#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
15502 { "HAVE_FTRUNCATE", NULL },
15503#endif
15504
15505#ifdef HAVE_FUTIMENS
15506 { "HAVE_FUTIMENS", probe_futimens },
15507#endif
15508
15509#ifdef HAVE_FUTIMES
15510 { "HAVE_FUTIMES", NULL },
15511#endif
15512
15513#ifdef HAVE_FUTIMESAT
15514 { "HAVE_FUTIMESAT", NULL },
15515#endif
15516
15517#ifdef HAVE_LINKAT
15518 { "HAVE_LINKAT", probe_linkat },
15519#endif
15520
15521#ifdef HAVE_LCHFLAGS
15522 { "HAVE_LCHFLAGS", NULL },
15523#endif
15524
15525#ifdef HAVE_LCHMOD
15526 { "HAVE_LCHMOD", NULL },
15527#endif
15528
15529#ifdef HAVE_LCHOWN
15530 { "HAVE_LCHOWN", NULL },
15531#endif
15532
15533#ifdef HAVE_LSTAT
15534 { "HAVE_LSTAT", NULL },
15535#endif
15536
15537#ifdef HAVE_LUTIMES
15538 { "HAVE_LUTIMES", NULL },
15539#endif
15540
15541#ifdef HAVE_MEMFD_CREATE
15542 { "HAVE_MEMFD_CREATE", NULL },
15543#endif
15544
15545#ifdef HAVE_MKDIRAT
15546 { "HAVE_MKDIRAT", probe_mkdirat },
15547#endif
15548
15549#ifdef HAVE_MKFIFOAT
15550 { "HAVE_MKFIFOAT", NULL },
15551#endif
15552
15553#ifdef HAVE_MKNODAT
15554 { "HAVE_MKNODAT", NULL },
15555#endif
15556
15557#ifdef HAVE_OPENAT
15558 { "HAVE_OPENAT", probe_openat },
15559#endif
15560
15561#ifdef HAVE_READLINKAT
15562 { "HAVE_READLINKAT", probe_readlinkat },
15563#endif
15564
15565#ifdef HAVE_RENAMEAT
15566 { "HAVE_RENAMEAT", probe_renameat },
15567#endif
15568
15569#ifdef HAVE_SYMLINKAT
15570 { "HAVE_SYMLINKAT", probe_symlinkat },
15571#endif
15572
15573#ifdef HAVE_UNLINKAT
15574 { "HAVE_UNLINKAT", probe_unlinkat },
15575#endif
15576
15577#ifdef HAVE_UTIMENSAT
15578 { "HAVE_UTIMENSAT", probe_utimensat },
Larry Hastings9cf065c2012-06-22 16:30:09 -070015579#endif
15580
15581#ifdef MS_WINDOWS
Ronald Oussoren41761932020-11-08 10:05:27 +010015582 { "MS_WINDOWS", NULL },
Larry Hastings9cf065c2012-06-22 16:30:09 -070015583#endif
15584
Ronald Oussoren41761932020-11-08 10:05:27 +010015585 { NULL, NULL }
Larry Hastings9cf065c2012-06-22 16:30:09 -070015586};
15587
15588
Victor Stinner1c2fa782020-05-10 11:05:29 +020015589static int
15590posixmodule_exec(PyObject *m)
Guido van Rossumb6775db1994-08-01 11:34:53 +000015591{
Victor Stinner97f33c32020-05-14 18:05:58 +020015592 _posixstate *state = get_posix_state(m);
Tim Peters5aa91602002-01-30 05:46:57 +000015593
Ronald Oussoren41761932020-11-08 10:05:27 +010015594#if defined(HAVE_PWRITEV)
15595 if (HAVE_PWRITEV_RUNTIME) {} else {
15596 PyObject* dct = PyModule_GetDict(m);
15597
15598 if (dct == NULL) {
15599 return -1;
15600 }
15601
15602 if (PyDict_DelItemString(dct, "pwritev") == -1) {
15603 PyErr_Clear();
15604 }
15605 if (PyDict_DelItemString(dct, "preadv") == -1) {
15606 PyErr_Clear();
15607 }
15608 }
15609#endif
15610
Victor Stinner8c62be82010-05-06 00:08:46 +000015611 /* Initialize environ dictionary */
Victor Stinner97f33c32020-05-14 18:05:58 +020015612 PyObject *v = convertenviron();
Victor Stinner8c62be82010-05-06 00:08:46 +000015613 Py_XINCREF(v);
15614 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015615 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +000015616 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000015617
Victor Stinner8c62be82010-05-06 00:08:46 +000015618 if (all_ins(m))
Victor Stinner1c2fa782020-05-10 11:05:29 +020015619 return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000015620
Victor Stinner8c62be82010-05-06 00:08:46 +000015621 if (setup_confname_tables(m))
Victor Stinner1c2fa782020-05-10 11:05:29 +020015622 return -1;
Fred Drakebec628d1999-12-15 18:31:10 +000015623
Victor Stinner8c62be82010-05-06 00:08:46 +000015624 Py_INCREF(PyExc_OSError);
15625 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000015626
Ross Lagerwall7807c352011-03-17 20:20:30 +020015627#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondob3966632019-11-05 07:16:14 -080015628 waitid_result_desc.name = MODNAME ".waitid_result";
15629 PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
15630 if (WaitidResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015631 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015632 }
15633 Py_INCREF(WaitidResultType);
15634 PyModule_AddObject(m, "waitid_result", WaitidResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015635 state->WaitidResultType = WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015636#endif
15637
Eddie Elizondob3966632019-11-05 07:16:14 -080015638 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
15639 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
15640 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
15641 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
15642 PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
15643 if (StatResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015644 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015645 }
15646 Py_INCREF(StatResultType);
15647 PyModule_AddObject(m, "stat_result", StatResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015648 state->StatResultType = StatResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015649 structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
15650 ((PyTypeObject *)StatResultType)->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015651
Eddie Elizondob3966632019-11-05 07:16:14 -080015652 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
15653 PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
15654 if (StatVFSResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015655 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015656 }
15657 Py_INCREF(StatVFSResultType);
15658 PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015659 state->StatVFSResultType = StatVFSResultType;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015660#ifdef NEED_TICKS_PER_SECOND
15661# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Eddie Elizondob3966632019-11-05 07:16:14 -080015662 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015663# elif defined(HZ)
Eddie Elizondob3966632019-11-05 07:16:14 -080015664 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015665# else
Eddie Elizondob3966632019-11-05 07:16:14 -080015666 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015667# endif
15668#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015669
William Orr81574b82018-10-01 22:19:56 -070015670#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondob3966632019-11-05 07:16:14 -080015671 sched_param_desc.name = MODNAME ".sched_param";
15672 PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
15673 if (SchedParamType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015674 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +000015675 }
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015676 Py_INCREF(SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -080015677 PyModule_AddObject(m, "sched_param", SchedParamType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015678 state->SchedParamType = SchedParamType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015679 ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
Benjamin Petersone3298dd2011-08-02 18:40:46 -050015680#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000015681
Eddie Elizondob3966632019-11-05 07:16:14 -080015682 /* initialize TerminalSize_info */
15683 PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
15684 if (TerminalSizeType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015685 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015686 }
15687 Py_INCREF(TerminalSizeType);
15688 PyModule_AddObject(m, "terminal_size", TerminalSizeType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015689 state->TerminalSizeType = TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015690
15691 /* initialize scandir types */
Victor Stinner1c2fa782020-05-10 11:05:29 +020015692 PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL);
Eddie Elizondob3966632019-11-05 07:16:14 -080015693 if (ScandirIteratorType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015694 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015695 }
Victor Stinner97f33c32020-05-14 18:05:58 +020015696 state->ScandirIteratorType = ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015697
Victor Stinner1c2fa782020-05-10 11:05:29 +020015698 PyObject *DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL);
Eddie Elizondob3966632019-11-05 07:16:14 -080015699 if (DirEntryType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015700 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015701 }
15702 Py_INCREF(DirEntryType);
15703 PyModule_AddObject(m, "DirEntry", DirEntryType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015704 state->DirEntryType = DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015705
Larry Hastings605a62d2012-06-24 04:33:36 -070015706 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondob3966632019-11-05 07:16:14 -080015707 PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015708 if (TimesResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015709 return -1;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015710 }
Eddie Elizondob3966632019-11-05 07:16:14 -080015711 Py_INCREF(TimesResultType);
15712 PyModule_AddObject(m, "times_result", TimesResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015713 state->TimesResultType = TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070015714
Eddie Elizondob3966632019-11-05 07:16:14 -080015715 PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015716 if (UnameResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015717 return -1;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015718 }
Eddie Elizondob3966632019-11-05 07:16:14 -080015719 Py_INCREF(UnameResultType);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015720 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015721 state->UnameResultType = (PyObject *)UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070015722
Victor Stinner97f33c32020-05-14 18:05:58 +020015723 if ((state->billion = PyLong_FromLong(1000000000)) == NULL)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015724 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015725#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Victor Stinner97f33c32020-05-14 18:05:58 +020015726 state->struct_rusage = PyUnicode_InternFromString("struct_rusage");
15727 if (state->struct_rusage == NULL)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015728 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015729#endif
Victor Stinner97f33c32020-05-14 18:05:58 +020015730 state->st_mode = PyUnicode_InternFromString("st_mode");
15731 if (state->st_mode == NULL)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015732 return -1;
Larry Hastings6fe20b32012-04-19 15:07:49 -070015733
Larry Hastings9cf065c2012-06-22 16:30:09 -070015734 /* suppress "function not used" warnings */
15735 {
15736 int ignored;
15737 fd_specified("", -1);
15738 follow_symlinks_specified("", 1);
15739 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
15740 dir_fd_converter(Py_None, &ignored);
15741 dir_fd_unavailable(Py_None, &ignored);
15742 }
15743
15744 /*
15745 * provide list of locally available functions
15746 * so os.py can populate support_* lists
15747 */
Victor Stinner97f33c32020-05-14 18:05:58 +020015748 PyObject *list = PyList_New(0);
15749 if (!list) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015750 return -1;
Victor Stinner97f33c32020-05-14 18:05:58 +020015751 }
Ronald Oussoren41761932020-11-08 10:05:27 +010015752 for (const struct have_function *trace = have_functions; trace->label; trace++) {
15753 PyObject *unicode;
15754 if (trace->probe && !trace->probe()) continue;
15755 unicode = PyUnicode_DecodeASCII(trace->label, strlen(trace->label), NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -070015756 if (!unicode)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015757 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -070015758 if (PyList_Append(list, unicode))
Victor Stinner1c2fa782020-05-10 11:05:29 +020015759 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -070015760 Py_DECREF(unicode);
15761 }
Ronald Oussoren41761932020-11-08 10:05:27 +010015762
Larry Hastings9cf065c2012-06-22 16:30:09 -070015763 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040015764
Victor Stinner1c2fa782020-05-10 11:05:29 +020015765 return 0;
15766}
15767
15768
15769static PyModuleDef_Slot posixmodile_slots[] = {
15770 {Py_mod_exec, posixmodule_exec},
15771 {0, NULL}
15772};
15773
15774static struct PyModuleDef posixmodule = {
15775 PyModuleDef_HEAD_INIT,
15776 .m_name = MODNAME,
15777 .m_doc = posix__doc__,
15778 .m_size = sizeof(_posixstate),
15779 .m_methods = posix_methods,
15780 .m_slots = posixmodile_slots,
15781 .m_traverse = _posix_traverse,
15782 .m_clear = _posix_clear,
15783 .m_free = _posix_free,
15784};
15785
15786PyMODINIT_FUNC
15787INITFUNC(void)
15788{
15789 return PyModuleDef_Init(&posixmodule);
Guido van Rossumb6775db1994-08-01 11:34:53 +000015790}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015791
15792#ifdef __cplusplus
15793}
15794#endif